This commit is contained in:
2022-05-18 21:49:36 +03:00
parent 5bbc4d6bab
commit 5d5e7c0ad7

109
ingots.cs Normal file
View File

@@ -0,0 +1,109 @@
// What is the name of the container(s) used to store ingots
string nameOfIngotStorage = "Cargo [Ingot]";
// What is the optimal amount per ingot type
Dictionary<String,int> targetIngots = new Dictionary<String,int>() {
{ "Nickel", 10000 },
{ "Cobalt", 10000 },
{ "Stone", 20000 },
{ "Magnesium", 5 },
{ "Silver", 5000 },
{ "Gold", 5000 },
{ "Silicon", 5000 },
{ "Uranium", 100 },
{ "Platinum", 5000 },
{ "Iron", 30000 }
};
void Main()
{
CheckIngotStatus();
}
// Function to check ingot status
// and update a beacon accordingly
void CheckIngotStatus() {
// First line of beacon
var ingotDebugString = "Ingot Status\n\r";
// Name of ingot types to workaround being unable to enumerate dictionary keys
string[] ingotNames = { "Stone", "Iron", "Uranium", "Nickel", "Gold", "Silver", "Platinum", "Cobalt", "Silicon", "Magnesium" };
// Some magic numbers we use to try and align the output
// because the game uses a variable width font so 'ili' is much narrower
// than 'num'
Dictionary<String,int> stringDisplayLengths = new Dictionary<String,int>() {
{ "Nickel", 6 },
{ "Cobalt", 5 },
{ "Stone", 5 },
{ "Magnesium", 10 },
{ "Silver", 5 },
{ "Gold", 4 },
{ "Silicon", 6 },
{ "Uranium", 7},
{ "Platinum", 7 },
{ "Iron", 3 }
};
// Ingot names and the total amount we have will be stored in here
Dictionary<String,int> currentIngots = new Dictionary<String,int>() {};
// Current inventory items will be temp stored here (One type of ingot might have multiple entries)
List<IMyInventoryItem> allIngots = new List<IMyInventoryItem>();
// Get all of the containers
var ingotStorageContainers = new List<IMyTerminalBlock>();
GridTerminalSystem.SearchBlocksOfName(nameOfIngotStorage, ingotStorageContainers);
// Loop through the containers
for( int i = 0; i < ingotStorageContainers.Count; i++ ) {
var container = ingotStorageContainers[i];
var inventoryOwner = (IMyInventoryOwner)container;
var sourceInventory = inventoryOwner.GetInventory(0);
var items = sourceInventory.GetItems();
// .. add each inventory item from THIS container to the full list
// We're going to do it this way to avoid Space Engineers
// complaining that the script is getting too complex (nested loops)
allIngots.AddRange(items);
}
// Loop through the full list of inventory items (for all containers)
for( int i = 0; i < allIngots.Count; i++ ) {
// If we've seen this ingot type already...
if( currentIngots.ContainsKey(allIngots[i].Content.SubtypeName) ) {
// ... increase the amount we're storing
currentIngots[allIngots[i].Content.SubtypeName] = currentIngots[allIngots[i].Content.SubtypeName] + (int)allIngots[i].Amount;
// ... otherwise this is the first of this ingot type we've seen, so store the amount
} else {
currentIngots.Add(allIngots[i].Content.SubtypeName, (int)allIngots[i].Amount);
}
}
// Now we have a Dictionary of Ingot Types and the amount we're storing, lets
// put together the output which will have a new line per type in the form:
// Nickel - Full (10000kg)
for (int i = 0; i < ingotNames.Length; ++i) {
var name = ingotNames[i];
var amount = currentIngots.ContainsKey(name) ? currentIngots[name] : 0;
var target = targetIngots[name];
int percentage = (int)((float)amount/(float)target * 100);
// Determine a 'level' from the % we have. Less than 10% is critical, less than 100% is low
var level = "";
if( percentage < 10 ) {
level = "Critical";
} else if ( percentage < 100 ) {
level = "Low";
} else if ( percentage < 500 ) {
level = "Full";
} else {
level = "Overfull";
}
// Put the output together, padding the string
ingotDebugString += name.PadRight(name.Length+20-(stringDisplayLengths[name]*2),' ') + "-" + level + " (" + amount + "kg)\n\r";
}
// Now lets find the first Beacon on this grid and rename it
List<IMyTerminalBlock> blocks;
blocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyBeacon>(blocks);
if (blocks.Count == 0) return;
IMyBeacon beacon = blocks[0] as IMyBeacon;
beacon.SetCustomName(ingotDebugString);
}