Last active
April 26, 2018 08:57
-
-
Save error418/c6b1e3447cfbb33c6fcb to your computer and use it in GitHub Desktop.
Space Engineers resource indicator lights
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
// v0.2 | |
// Place lights starting with following string and the appended resource type to create a resource check. -- TypeId:SubtypeId | |
// e.g.: "Resource Status Light Ore:Stone" for a check for stone | |
// TypeIds: Ore Ingot Component | |
const string WARNING_LIGHT_PREFIX = "RSL "; | |
const string SOUND_BLOCK_NAME = null; | |
const string TYPE_SEPARATOR = ":"; | |
// checks, if resources fall below a specific amount. | |
var resources = new Dictionary<string, VRage.MyFixedPoint>(); | |
var containers = new List<IMyTerminalBlock>(); | |
var outputEntities = new List<IMyTerminalBlock>(); | |
float WARN_VALUE = 200; | |
// acquire blocks with inventory | |
GridTerminalSystem.GetBlocksOfType<IMyInventoryOwner>(containers); | |
// get signal lights | |
GridTerminalSystem.SearchBlocksOfName(WARNING_LIGHT_PREFIX, outputEntities); | |
for (int i = 0; i < outputEntities.Count; i++) | |
{ | |
resources[outputEntities[i].CustomName.Replace(WARNING_LIGHT_PREFIX, "").Trim()] = 0; | |
} | |
// calculate resource availability | |
for (int i = 0; i < containers.Count; i++) | |
{ | |
var containedItems = ((IMyInventoryOwner)containers[i]).GetInventory(0).GetItems(); | |
for (int k = 0; k < containedItems.Count; k++) | |
{ | |
var currentItem = ((IMyInventoryItem)containedItems[k]); | |
string resourceKey = currentItem.Content.TypeId + TYPE_SEPARATOR + currentItem.Content.SubtypeId; | |
resourceKey = resourceKey.Replace("MyObjectBuilder_", ""); | |
// only process requested resources | |
if (resources.ContainsKey(resourceKey)) | |
{ | |
resources[resourceKey] = currentItem.Amount + resources[resourceKey]; | |
} | |
} | |
} | |
// iterate over resource dictionary | |
var iterator = resources.GetEnumerator(); | |
var playSoundAlert = false; | |
do | |
{ | |
string currentType = iterator.Current.Key; | |
var outputEntity = GridTerminalSystem.GetBlockWithName(WARNING_LIGHT_PREFIX + currentType); | |
// skip, if no output entity is available | |
if (outputEntity == null) continue; | |
float currentValue = 0; | |
float.TryParse("" + resources[currentType], out currentValue); | |
// apply values to output entity | |
if (currentValue < WARN_VALUE) | |
{ | |
outputEntity.GetActionWithName("OnOff_On").Apply(outputEntity); | |
playSoundAlert = true; | |
} | |
else | |
{ | |
outputEntity.GetActionWithName("OnOff_Off").Apply(outputEntity); | |
} | |
} | |
while (iterator.MoveNext()); | |
// play sound from soundblock, if block name defined and at least one resource is low | |
if (playSoundAlert && SOUND_BLOCK_NAME != null) | |
{ | |
var soundBlock = GridTerminalSystem.GetBlockWithName(SOUND_BLOCK_NAME); | |
soundBlock.GetActionWithName("PlaySound").Apply(soundBlock); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment