Created
May 7, 2016 00:27
-
-
Save lewisd32/02f11f1f9d0da2301eb8c107c0b3e8a1 to your computer and use it in GitHub Desktop.
Automated assembler script for Space Engineers
This file contains 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
String lcdName = "Assembler Control LCD"; | |
public void Main(string argument) { | |
List<String> orderedResources = new List<String>(); | |
Dictionary<String, int> desiredQuantities = new Dictionary<String, int>(); | |
Dictionary<String, String> displayNames = new Dictionary<String, String>(); | |
Dictionary<String, String> assemblerProducts = new Dictionary<String, String>(); | |
StringBuilder text = new StringBuilder(); | |
// Find config | |
IMyTextPanel lcd = GridTerminalSystem.GetBlockWithName(lcdName) as IMyTextPanel; | |
String config = lcd.GetPrivateText(); | |
// Parse config | |
var lines = config.Split(new Char[] {'\n'}); | |
foreach (String line in lines) { | |
if (line.Equals("")) { | |
continue; | |
} | |
//Echo("Reading line: " + line); | |
var parts = line.Split(new Char[] {':'}, 2); | |
if (parts.Length < 2) { | |
Echo("Unknown config element: " + line); | |
return; | |
} | |
var args = parts[1].Split(new Char[] {','}); | |
if (parts[0].Trim().Equals("Resource")) { | |
String resourceName = args[0].Trim(); | |
int desiredQuantity = int.Parse(args[1]); | |
String displayName = args[2].Trim(); | |
orderedResources.Add(resourceName); | |
desiredQuantities[resourceName] = desiredQuantity; | |
displayNames[resourceName] = displayName; | |
} else if (parts[0].Trim().Equals("Assembler")) { | |
String assemblerName = args[0].Trim(); | |
String productName = args[1].Trim(); | |
assemblerProducts[assemblerName] = productName; | |
} else { | |
Echo("Unknown config element: " + line); | |
} | |
} | |
Dictionary<String, int> inventory = getInventoryCounts(); | |
// Show inventory and desired quantities on LCD | |
foreach (String resource in orderedResources) { | |
int quantity = inventory.ContainsKey(resource)?inventory[resource]:0; | |
int desiredQuantity = desiredQuantities[resource]; | |
String displayName = displayNames[resource]; | |
String enabled = "No Assemblers"; | |
if (quantity < desiredQuantity) { | |
// enable assemblers | |
foreach (String assemblerName in assemblerProducts.Keys) { | |
if (assemblerProducts[assemblerName].Equals(resource)) { | |
var assembler = GridTerminalSystem.GetBlockWithName(assemblerName) as IMyAssembler; | |
assembler.RequestEnable(true); | |
enabled = "enabled"; | |
} | |
} | |
} else { | |
// disable assemblers | |
foreach (String assemblerName in assemblerProducts.Keys) { | |
if (assemblerProducts[assemblerName].Equals(resource)) { | |
var assembler = GridTerminalSystem.GetBlockWithName(assemblerName) as IMyAssembler; | |
assembler.RequestEnable(false); | |
enabled = "disabled"; | |
} | |
} | |
} | |
text.Append(quantity + "/" + desiredQuantity + " " + enabled + " " + displayName + "\n"); | |
} | |
// Print in the console the names of things in inventory that match argument | |
foreach (String name in inventory.Keys) { | |
if (!argument.Equals("") && name.Contains(argument)) { | |
Echo(name + " : " + inventory[name] + "\n"); | |
} | |
} | |
// Push the "Updated" text to the last line of the screen | |
for (int i = 0; i < 17-orderedResources.Count; ++i) { | |
text.Append("\n"); | |
} | |
text.Append("Updated " + DateTime.Now.ToString()); | |
writeToLCDPanel(lcdName, text.ToString()); | |
} | |
public void writeToLCDPanel(String lcdName, String text) { | |
IMyTextPanel lcdPanel = GridTerminalSystem.GetBlockWithName(lcdName) as IMyTextPanel; | |
lcdPanel.ShowTextureOnScreen(); | |
lcdPanel.WritePublicText(text, false); | |
lcdPanel.ShowPublicTextOnScreen(); | |
} | |
public Dictionary<String, int> getInventoryCounts() { | |
// Find all the blocks that have inventories | |
List<IMyTerminalBlock> inventoryBlocks = new List<IMyTerminalBlock>(); | |
GridTerminalSystem.GetBlocksOfType<IMyInventoryOwner>(inventoryBlocks); | |
// add the inventories from the blocks to the inventory list | |
List<IMyInventory> inventories = new List<IMyInventory>(); | |
for (int i = 0; i < inventoryBlocks.Count; i++) { | |
var block = inventoryBlocks[i] as IMyInventoryOwner; | |
inventories.Add(block.GetInventory(0)); | |
// if it has more than one inventory, it's probably an assembler, | |
// since they have input and output. Add the 2nd one. | |
if (block.InventoryCount > 1) { | |
inventories.Add(block.GetInventory(1)); | |
} | |
} | |
Dictionary<String, int> results = new Dictionary<String, int>(); | |
for (int i = 0; i < inventories.Count; i++) { | |
IMyInventory inventory = (IMyInventory)inventories[i]; | |
List<IMyInventoryItem> items = inventory.GetItems(); | |
for (int j = 0; j < items.Count; j++) { | |
IMyInventoryItem item = items[j]; | |
String name = item.Content.SubtypeName; | |
if (results.ContainsKey(name)) { | |
results[name] += (int) item.Amount; | |
} else { | |
results[name] = (int) item.Amount; | |
} | |
} | |
} | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sadly...obsolete now.