Skip to content

Instantly share code, notes, and snippets.

@aballano
Last active March 26, 2024 00:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aballano/dbe6b11661bd5c48c56653ae6d59a729 to your computer and use it in GitHub Desktop.
Save aballano/dbe6b11661bd5c48c56653ae6d59a729 to your computer and use it in GitHub Desktop.
Space Engineers script to handle all Collectors' automatic on/off switch for a given Ice threshold is reached
// Define the amount of ice to trigger the collector deactivation
const int iceThreshold = 100000;
// Quick-access variables to avoid reinstantiation
IMyGridTerminalSystem grid;
List<IMyCollector> collectors;
List<IMyTerminalBlock> containers;
public Program()
{
grid = GridTerminalSystem;
collectors = new List<IMyCollector>();
containers = new List<IMyTerminalBlock>();
// It's recommended to set RuntimeInfo.UpdateFrequency
// here, which will allow your script to run itself without a
// timer block.
Runtime.UpdateFrequency = UpdateFrequency.Update10;
}
public void Save()
{}
void Main(string argument)
{
grid.GetBlocksOfType<IMyCollector>(collectors);
Echo($"Total collectors found: {collectors.Count()}");
// Options:
// MakeAmmo
// MakeComponent
// MakeIngot
// MakeOre
// MakeTool
var iceInShip = GetResourceAmountFrom<IMyTerminalBlock>(grid, MyItemType.MakeOre("Ice"));
Echo($"Total ice in ship: {iceInShip}/{iceThreshold}");
if (iceInShip >= iceThreshold)
{
collectors.ForEach(collector => collector.ApplyAction("OnOff_Off"));
Echo("Collectors deactivated.");
}
if (iceInShip < (iceThreshold * 0.9))
{
collectors.ForEach(collector => collector.ApplyAction("OnOff_On"));
Echo("Collectors activated!");
}
}
int GetResourceAmountFrom<T>(IMyGridTerminalSystem grid, MyItemType itemType) where T : class
{
var resourceAmmount = 0;
grid.GetBlocksOfType<T>(containers);
Echo($"Found {containers.Count()} containers");
foreach (var container in containers)
{
for (int i = 0; i < container.InventoryCount; i++)
{
IMyInventory inv = container.GetInventory(i);
List<MyInventoryItem> items = new List<MyInventoryItem>();
var iceItem = inv.FindItem(itemType);
if (iceItem.HasValue)
{
resourceAmmount += (int)iceItem.Value.Amount;
}
}
}
return resourceAmmount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment