Skip to content

Instantly share code, notes, and snippets.

@awstanley
Created January 5, 2015 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awstanley/2a78f4d17aa5c87643ce to your computer and use it in GitHub Desktop.
Save awstanley/2a78f4d17aa5c87643ce to your computer and use it in GitHub Desktop.
Space Engineers "reactor sorting" programming block.
// Name Syntax: "<Friendly List Alias><Cfg_Separator><Resource(s)>"
// Valid resources (without quotation marks):
// "Energetic", "Cobalt", "Gold", "Iron",
// "Nickel", "Platinum", "Silver", "Uranium"
//
// Divide them using the 'Cfg_Separator_Internal' value. e.g.
// [1] Reactor§Uranium
// [2] TwigglyDeFooBarDeBopDeBoop Hrm? Yes, This is quite something§Nickel,Cobalt,Gold
// USE:
// 1. Place a timer block and a code block.
// 2. Put the code into the code block.
// 3. "Setup Actions" in the Timer:
// (a) "Run" on the code block
// (b) "Trigger Now" on the timer
// 4. Set the timer delay to your desired delay
// 5. Start the timer.
// Global Separator (hold alt then press 2 then 1, then release alt)
char Cfg_Separator = '§';
// Global Separator (hold alt then press 2 then 1, then release alt)
char Cfg_Separator_Internal = ',';
String Cfg_TypeId_Ore = "MyObjectBuilder_Ore";
String Cfg_TypeId_Ingot = "MyObjectBuilder_Ingot";
void Process(IMyRefinery Block, List<String> Resources,
IMyInventory Input, IMyInventory RefusedCargoStorage)
{
var InputItems = Input.GetItems();
// Remove anything that does not belong
for (var i = 0; i < InputItems.Count; i++)
{
if (!Resources.Contains(InputItems[i].Content.SubtypeId.ToString()))
{
Input.TransferItemTo(RefusedCargoStorage, i);
}
}
}
void Process()
{
var CargoBlocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyCargoContainer>(CargoBlocks);
if(CargoBlocks.Count == 0)
{
return;
}
IMyInventoryOwner OutOwner = (IMyInventoryOwner)CargoBlocks[0];
// Begin iteration over the blocks
var RefineryBlocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyRefinery>(RefineryBlocks);
for (int i = 0; i < RefineryBlocks.Count; i++)
{
IMyRefinery Block = (IMyRefinery)RefineryBlocks[i];
List<String> ResList = new List<String>();
// Get it as fragments
String[] Fragments = Block.CustomName.Split(Cfg_Separator);
// If there are two, look at the second
if (Fragments.Length == 2)
{
// Get the resources we want to process here
String[] Resources = Fragments[1].Split(Cfg_Separator_Internal);
foreach (String Res in Resources)
{
ResList.Add(Res);
}
}
if (ResList.Count > 0)
{
// Cast the refinery to own what's stored
IMyInventoryOwner InventoryOwner = (IMyInventoryOwner)Block;
// We expect 2 (input + output)
if (InventoryOwner.InventoryCount == 2)
{
IMyInventory Input = InventoryOwner.GetInventory(0);
IMyInventory Output = InventoryOwner.GetInventory(1);
Process(Block, ResList, Input, OutOwner.GetInventory(0));
}
}
}
}
void Main()
{
Process();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment