Advertisement
dkalaxdk

ProductiionManager

Feb 28th, 2021
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. List<IMyTerminalBlock> containers = new List<IMyTerminalBlock>();
  2.  
  3. Dictionary<string,int[]> RequiredItems = new Dictionary<string,int[]>(){
  4.   {"SteelPlate", new int[] {0,5000}},
  5.   {"BulletproofGlass", new int[] {0,1000}},
  6.   {"Computer", new int[] {0,1000}},
  7.   {"Construction", new int[] {0,5000}},
  8.   {"Detector", new int[] {0,100}},
  9.   {"Display", new int[] {0,1000}},
  10.   {"Girder", new int[] {0,1000}},
  11.   {"InteriorPlate", new int[] {0,5000}},
  12.   {"LargeTube", new int[] {0,1000}},
  13.   {"MetalGrid", new int[] {0,1000}},
  14.   {"Motor", new int[] {0,1000}},
  15.   {"PowerCell", new int[] {0,500}},
  16.   {"RadioCommunication", new int[] {0,100}},
  17.   {"SmallTube", new int[] {0,1000}},
  18.   {"SolarCell", new int[] {0,1000}}
  19. };
  20. IMyTextPanel lcd;
  21. IMyAssembler assembler;
  22.  
  23. void Main()
  24. {
  25.     assembler = GridTerminalSystem.GetBlockWithName("Assembler 1 [Master]") as IMyAssembler;
  26.     lcd = GridTerminalSystem.GetBlockWithName("LCD Panel") as IMyTextPanel;
  27.     GridTerminalSystem.SearchBlocksOfName("[Base]", containers, container => container is IMyCargoContainer);
  28.  
  29.     double rate = RateFilling();
  30.     CalculateMissingItems();
  31.     WriteLCD();
  32. }
  33.  
  34. double RateFilling() {
  35.   double maxFilling = 0.0d;
  36.   double currentFilling = 0.0d;
  37.   //DATA : take all the containers, see how they are filled (currentFilling), see what is their max filling (maxFilling)
  38.   for (int i = 0; i < containers.Count; i++)
  39.   {
  40.       var inventory = ((IMyEntity)containers[i]).GetInventory(0);
  41.       List<MyInventoryItem> inventoryItems = new List<MyInventoryItem>();
  42.       inventory.GetItems(inventoryItems);
  43.       foreach (var item in inventoryItems)
  44.       {
  45.           string itemString = item.Type.ToString();
  46.           string outputString = itemString.Substring(itemString.IndexOf("/")+1);
  47.           int[] value = new int[] {};
  48.           if (RequiredItems.TryGetValue(outputString,out value)) {
  49.             int[] array = RequiredItems[outputString];
  50.             array[0] += item.Amount.ToIntSafe();
  51.           }
  52.       }
  53.       currentFilling += Convert.ToDouble(inventory.CurrentVolume.RawValue);
  54.       maxFilling += Convert.ToDouble(inventory.MaxVolume.RawValue);
  55.   }
  56.   return Math.Round(100 * (currentFilling / maxFilling), 2);
  57. }
  58.  
  59. void CalculateMissingItems() {
  60.       foreach (var item in RequiredItems)
  61.       {
  62.         Echo(item.Key);
  63.         MyDefinitionId blueprint = new MyDefinitionId();
  64.         if(MyDefinitionId.TryParse("MyObjectBuilder_BlueprintDefinition/"+ item.Key, out blueprint)) {
  65.           if(item.Value[0] < item.Value[1]) {
  66.             assembler.AddQueueItem(blueprint, (double)item.Value[1]-item.Value[0]);
  67.           }
  68.         }
  69.         else if(MyDefinitionId.TryParse("MyObjectBuilder_BlueprintDefinition/"+ item.Key + "Component",out blueprint)){
  70.           if(item.Value[0] < item.Value[1]) {
  71.             assembler.AddQueueItem(blueprint, (double)item.Value[1]-item.Value[0]);
  72.           }
  73.         }
  74.       }
  75. }
  76.  
  77. void WriteLCD() {
  78.   string outputString = "";
  79.   foreach (var item in RequiredItems)
  80.   {
  81.       outputString += "Amount of: " + item.Key+ " " + item.Value[0] + "/" + item.Value[1] + "\n";
  82.   }
  83.   lcd.WriteText(outputString);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement