Consolidated Inventory – Improved 0/5 (5)

This script is similar to the Consolidated Inventory on an LCD Panel. It also shows the percentage that your cargo containers are full (at the top).

  1. const string PANEL_NAME = "Raw Ore Panel";  
  2. const string CONTAINER_NAME = "Raw Ore";  
  3. const int PANEL_LINES = 22;  
  4. int lineOffset = 0;
  5.  
  6. void Main()  
  7. {  
  8.     try {
  9.     List<IMyTerminalBlock> work = new List<IMyTerminalBlock>();  
  10.     Dictionary<String,float> consolidated = new Dictionary<String,float>();
  11.     GridTerminalSystem.SearchBlocksOfName(PANEL_NAME, work);      
  12.     IMyTextPanel panel = null;
  13.     for (int i = 0; i < work.Count; i++)    
  14.     {    
  15.         if (work&#91;i] is IMyTextPanel) {
  16.            panel = (IMyTextPanel)work&#91;i];  
  17.            break;
  18.         }
  19.     }
  20.     List<IMyTerminalBlock> containerList = new List<IMyTerminalBlock>();  
  21.     GridTerminalSystem.SearchBlocksOfName(CONTAINER_NAME, containerList);      
  22.  
  23.     float maxVolume = 0.0f;
  24.     float currentVolume = 0.0f;
  25.     for (int i = 0; i < containerList.Count; i++)    
  26.     {    
  27.         if (containerList&#91;i] is IMyCargoContainer) {
  28.             var containerInvOwner = containerList&#91;i] as IMyInventoryOwner;
  29.             var containerInventory = containerInvOwner.GetInventory(0);
  30.             maxVolume += (float)containerInventory.MaxVolume;
  31.             currentVolume += (float)containerInventory.CurrentVolume;
  32.             var containerItems = containerInventory.GetItems();    
  33.             for(int j = containerItems.Count - 1; j >= 0; j--)    
  34.             {    
  35.                 String itemName = decodeItemName(containerItems[j].Content.SubtypeName,  
  36.                                   containerItems[j].Content.TypeId.ToString()) + "|" +  
  37.                                   containerItems[j].Content.TypeId.ToString();
  38.                 float amount = (float)containerItems[j].Amount;
  39.                 if (!consolidated.ContainsKey(itemName)) {
  40.                    consolidated.Add(itemName, amount);
  41.                 } else {
  42.                    consolidated[itemName] += amount;
  43.                 }
  44.             }  
  45.         }
  46.     }
  47.     List<String> list = new List<String>();
  48.     var enumerator = consolidated.GetEnumerator();
  49.     while (enumerator.MoveNext())
  50.     {      
  51.         var pair = enumerator.Current;
  52.         String itemKey = pair.Key;
  53.         float itemValue = pair.Value;
  54.          
  55.         String txt = itemKey.Split('|')[0] + "  -  ";  
  56.         String amt = amountFormatter(itemValue,itemKey.Split('|')[1]);  
  57.         txt += amt;  
  58.         list.Add(txt);
  59.     }      
  60.     list.Sort();
  61.     list.Insert(0,"--------------------------------------------------------");
  62.     float percentageFull = (float)Math.Round((currentVolume / maxVolume) * 100.0f,2);
  63.     list.Insert(0,CONTAINER_NAME + " Inventory                 " + percentageFull.ToString("##0.00") + "% full");
  64.     for (int o=0; o < lineOffset; o++) {
  65.         String shiftedItem = list&#91;0];
  66.         list.RemoveAt(0);
  67.         list.Add(shiftedItem);
  68.     }
  69.     panel.WritePublicText(String.Join("\n",list.ToArray()), false);
  70.  
  71.     panel.ShowTextureOnScreen();  
  72.     panel.ShowPublicTextOnScreen();  
  73.     if (list.Count > PANEL_LINES) {
  74.         lineOffset++;
  75.         if (list.Count - lineOffset < PANEL_LINES) {
  76.            lineOffset = 0;
  77.         }
  78.     }
  79.     } catch (Exception ex) {
  80.         throw new Exception(ex.StackTrace);
  81.     }
  82. }  
  83.  
  84. String amountFormatter(float amt, String typeId) {  
  85.     if (typeId.EndsWith("_Ore") || typeId.EndsWith("_Ingot")) {
  86.         if (amt > 1000.0f) {
  87.           return "" + Math.Round((float)amt/1000,2).ToString("###,###,##0.00") + "K";  
  88.         } else {
  89.           return "" + Math.Round((float)amt,2).ToString("###,###,##0.00");  
  90.         }
  91.     }
  92.     return "" + Math.Round((float)amt,0).ToString("###,###,##0");  
  93. }  
  94.  
  95. String decodeItemName(String name, String typeId)  
  96. {  
  97.     if (name.Equals("Construction")) { return "Construction Component"; }  
  98.     if (name.Equals("MetalGrid")) { return "Metal Grid"; }  
  99.     if (name.Equals("InteriorPlate")) { return "Interior Plate"; }  
  100.     if (name.Equals("SteelPlate")) { return "Steel Plate"; }  
  101.     if (name.Equals("SmallTube")) { return "Small Steel Tube"; }  
  102.     if (name.Equals("LargeTube")) { return "Large Steel Tube"; }  
  103.     if (name.Equals("BulletproofGlass")) { return "Bulletproof Glass"; }  
  104.     if (name.Equals("Reactor")) { return "Reactor Component"; }  
  105.     if (name.Equals("Thrust")) { return "Thruster Component"; }  
  106.     if (name.Equals("GravityGenerator")) { return "GravGen Component"; }  
  107.     if (name.Equals("Medical")) { return "Medical Component"; }  
  108.     if (name.Equals("RadioCommunication")) { return "Radio Component"; }  
  109.     if (name.Equals("Detector")) { return "Detector Component"; }  
  110.     if (name.Equals("SolarCell")) { return "Solar Cell"; }  
  111.     if (name.Equals("PowerCell")) { return "Power Cell"; }  
  112.     if (name.Equals("AutomaticRifleItem")) { return "Rifle"; }  
  113.     if (name.Equals("AutomaticRocketLauncher")) { return "Rocket Launcher"; }  
  114.     if (name.Equals("WelderItem")) { return "Welder"; }  
  115.     if (name.Equals("AngleGrinderItem")) { return "Grinder"; }  
  116.     if (name.Equals("HandDrillItem")) { return "Hand Drill"; }  
  117.     if (typeId.EndsWith("_Ore")) {
  118.         if (name.Equals("Stone")) {
  119.             return name;
  120.         }
  121.         return name + " Ore";
  122.     }
  123.     if (typeId.EndsWith("_Ingot")) {
  124.         if (name.Equals("Stone")) {
  125.             return "Gravel";
  126.         }
  127.         if (name.Equals("Magnesium")) {
  128.             return name + " Powder";
  129.         }
  130.         if (name.Equals("Silicon")) {
  131.             return name + " Wafer";
  132.         }
  133.        return name + " Ingot";
  134.     }
  135.     return name;  
  136. }

Please rate this post

3 Responses

  1. […] Refinery Status Assembler Status (unpublished, still tweaking) Consolidated Inventory – Improved (showing Raw Ore) Consolidated Inventory – Improved (showing Refined Ore) […]

  2. Hi, this is mote454 from Steam. I hate to ask, but I’ve been looking around for a special script, and I’ve come up empty! And I really like how you write your scripts to let the user use custom names for everything. And I don’t think(?) this would be a difficult script to write. But would be extremely useful. Looking at the Official Guide: Programmable block on Steam, it seems their is a IMyFunctionalBlock search that can be performed, that is for all blocks that can be turned on or off. I’m looking for a script that can, when run, check the status of one block (if its on or off) and change the status of another block (to either on or off, my choosing in the program which one). Example: when I turn on the engines, the script would see that they are ‘on’ and in turn would turn ‘on’ a group of lights for me. Or I could have my gyro’s turn off, when I turn on my drills for mining etc. I know these actions can be performed with timers, but then you loose the on/off function and would need two timers for each. One timer to turn things on, and another to turn off. And all the hot keys would look like timers lol. Any possible way you could write a script for this? Appreciate your work even if this can’t be done. And again thanks for all your scripts.

Leave a Reply