Created
April 23, 2022 15:07
-
-
Save fre-sch/d6618aec6af3669b0f41319f1a2ffa48 to your computer and use it in GitHub Desktop.
Space Engineers Toggle Block on amounts
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
class Program { | |
// item name that has to be taken into account for level check | |
// string itemNameToCheck = "MyObjectBuilder_Ore/Ice"; | |
string itemNameToCheck = "Ice"; | |
// tag for blocks that will be switched off and on | |
string tag = "[TOGGLE_LEVEL]"; | |
// do not change below | |
public List<IMyTerminalBlock> blocksWithInventory; | |
public List<IMyGasTank> blocksWithTank; | |
public List<IMyFunctionalBlock> targetBlocks; | |
public Program() { | |
blocksWithInventory = new List<IMyTerminalBlock>(); | |
blocksWithTank = new List<IMyGasTank>(); | |
targetBlocks = new List<IMyFunctionalBlock>(); | |
Runtime.UpdateFrequency = UpdateFrequency.Update100; | |
setup(); | |
} | |
private void setup() { | |
GridTerminalSystem.GetBlocksOfType<IMyFunctionalBlock>(targetBlocks, | |
block => block.IsSameConstructAs(Me) && block.CustomName.Contains(tag) && block.IsFunctional | |
); | |
if(targetBlocks.Count == 0) { | |
Echo("no functional blocks found with tag: " + tag); | |
} | |
GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(blocksWithInventory, | |
block => block.IsSameConstructAs(Me) && block.HasInventory | |
); | |
if(blocksWithInventory.Count == 0) { | |
Echo("no inventory blocks found"); | |
} | |
GridTerminalSystem.GetBlocksOfType<IMyGasTank>(blocksWithTank, block => block.IsSameConstructAs(Me)); | |
if(blocksWithTank.Count == 0) { | |
Echo("no tank blocks found"); | |
} | |
} | |
public void Main(string argument, UpdateType updateSource) { | |
if(targetBlocks.Count == 0) { | |
setup(); | |
return; | |
} | |
double currentAmount = targetItemAmount(); | |
var actionToApply = "OnOff_On"; | |
Echo("Current amount of " + itemNameToCheck + ": " + formatAmount(currentAmount)); | |
if(currentAmount >= levelToSwitchOff) { | |
Echo("it is more than " + formatAmount(levelToSwitchOff) + ", so we switch OFF"); | |
actionToApply = "OnOff_Off"; | |
} | |
else if(currentAmount <= levelToSwitchOn) { | |
Echo("it is less than " + formatAmount(levelToSwitchOn) + ", so we switch ON"); | |
actionToApply = "OnOff_On"; | |
} | |
foreach(IMyFunctionalBlock blockToSwitchOnOff in targetBlocks) { | |
blockToSwitchOnOff.ApplyAction(actionToApply); | |
} | |
} | |
void listInventoryItems(List<MyInventoryItem> items) { | |
foreach(IMyTerminalBlock blockWithInventory in blocksWithInventory) { | |
for(var i = 0; i < blockWithInventory.InventoryCount; i++) { | |
blockWithInventory.GetInventory(i).GetItems(items); | |
} | |
} | |
return items; | |
} | |
double targetItemAmount() { | |
var name = itemNameToCheck.ToLower(); | |
if(name == "hydrogen" || name == "oxygen") { | |
return totalGasAmount(); | |
} | |
return inventoryItemAmount(); | |
} | |
double inventoryItemAmount() { | |
double total = 0; | |
var items = new List<MyInventoryItem>(); | |
listInventoryItems(items); | |
foreach(MyInventoryItem item in items) { | |
//string itemName = item.Type.TypeId + "/" + item.Type.SubtypeId; | |
if(item.Type.SubtypeId == itemNameToCheck) { | |
total += (double)item.Amount; | |
} | |
} | |
return total; | |
} | |
double totalGasAmount() { | |
double total = 0; | |
foreach(IMyGasTank tank in blocksWithTank) { | |
if(gasType(tank) == itemNameToCheck.ToLower()) { | |
total += tank.FilledRatio * tank.Capacity; | |
} | |
} | |
return total; | |
} | |
string gasType(IMyGasTank tank) { | |
if (tank.DetailedInfo.Contains("Oxygen")) return "oxygen"; | |
if (tank.DetailedInfo.Contains("Hydrogen")) return "hydrogen"; | |
return "otherGas"; | |
} | |
string formatAmount(double amount) { | |
if (amount >= 1E6) | |
return String.Format("{0:0.00}M", amount / 1E6); | |
if (amount >= 1E3) | |
return String.Format("{0:0.00}k", amount / 1E3); | |
return String.Format("{0:0.00}", amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment