Advertisement
NickNDS

Item Balancer

Jul 31st, 2019
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.67 KB | None | 0 0
  1. public SortedList<string, List<IMyTerminalBlock>> balancedLists = new SortedList<string, List<IMyTerminalBlock>>();
  2.  
  3. public List<string> knownItems = new List<string>();
  4.  
  5. public int
  6. currentBalanceList = 0, currentBalanceIndex = 0, nextBalanceIndex = 1, currentBalanceItem = 0,
  7. currentLearningList = 0, currentLearningIndex = 0, currentCountIndex = 0;
  8.  
  9. public double currentItemCount = 0;
  10.  
  11. public bool learningItems = true, counted = false;
  12.  
  13. public Program()
  14. {
  15.     Runtime.UpdateFrequency = UpdateFrequency.Update10;
  16.     GetBalancedLists();
  17.     Save();
  18. }
  19.  
  20. public void Save() {}
  21.  
  22. public void Main(string arg, UpdateType updateSource)
  23. {
  24.     Echo("Balance Lists: " + balancedLists.Values.Count);
  25.     Echo("Known Items: " + knownItems.Count);
  26.     if (balancedLists.Values.Count > 0)
  27.     {
  28.         try {
  29.             if (learningItems) LearnItems();
  30.             else BalanceLists();
  31.         }
  32.         catch {
  33.             Echo("Error caught in main");
  34.             Echo("Try recompiling");
  35.             Echo("If the error persists, report to the author");
  36.         }
  37.     }
  38. }
  39.  
  40. public void LearnItems()
  41. {
  42.     Echo("Learning Items");
  43.     List<MyInventoryItem> items = new List<MyInventoryItem>();
  44.     balancedLists.Values[currentLearningList][currentLearningIndex].GetInventory(0).GetItems(items, b => UnknownItem(b));
  45.     currentLearningIndex++;
  46.     if (currentLearningIndex >= balancedLists.Values[currentLearningList].Count)
  47.     {
  48.         currentLearningIndex = 0;
  49.         currentLearningList++;
  50.     }
  51.     if (currentLearningList >= balancedLists.Values.Count) {
  52.         currentLearningList = 0;
  53.         learningItems = false;
  54.     }
  55. }
  56.  
  57. public bool UnknownItem(MyInventoryItem item)
  58. {
  59.     string itemType = item.Type.TypeId.ToString();
  60.     if (itemType == "MyObjectBuilder_PhysicalGunObject" || itemType == "MyObjectBuilder_GasContainerObject" || itemType == "MyObjectBuilder_OxygenContainerObject")
  61.         return false;
  62.     if (!knownItems.Contains(item.Type.ToString()))
  63.     {
  64.         knownItems.Add(item.Type.ToString());
  65.         return true;
  66.     }
  67.     return false;
  68. }
  69.  
  70. public void BalanceLists()
  71. {
  72.     if (counted || CountItems())
  73.     {
  74.         Echo("Balancing: " + currentItemCount + ": " + knownItems[currentBalanceItem]);
  75.         try {
  76.             BalanceInventories(balancedLists.Values[currentBalanceList][currentBalanceIndex].GetInventory(0), balancedLists.Values[currentBalanceList][nextBalanceIndex].GetInventory(0));
  77.         }
  78.         catch { Echo("Error caught balancing lists"); }
  79.         nextBalanceIndex++;
  80.         if (nextBalanceIndex >= balancedLists.Values[currentBalanceList].Count)
  81.         {
  82.             nextBalanceIndex = 0;
  83.             currentBalanceIndex++;
  84.         }
  85.         if (currentBalanceIndex == nextBalanceIndex && currentBalanceIndex + 1 < balancedLists.Values[currentBalanceList].Count) nextBalanceIndex++;
  86.         if (currentBalanceIndex >= balancedLists.Values[currentBalanceList].Count)
  87.         {
  88.             nextBalanceIndex = 1;
  89.             currentBalanceIndex = 0;
  90.             currentBalanceItem++;
  91.             currentItemCount = 0;
  92.             counted = false;
  93.         }
  94.         if (currentBalanceItem >= knownItems.Count)
  95.         {
  96.             currentBalanceList++;
  97.             currentBalanceItem = 0;
  98.         }
  99.         if (currentBalanceList >= balancedLists.Values.Count)
  100.         {
  101.             currentBalanceList = 0;
  102.             learningItems = true;
  103.         }
  104.     }
  105. }
  106.  
  107. public void BalanceInventories(IMyInventory invA, IMyInventory invB)
  108. {
  109.     List<MyInventoryItem> itemsA = new List<MyInventoryItem>(), itemsB = new List<MyInventoryItem>();
  110.     invA.GetItems(itemsA, b => b.Type.ToString() == knownItems[currentBalanceItem]);
  111.     invB.GetItems(itemsB, b => b.Type.ToString() == knownItems[currentBalanceItem]);
  112.     if (itemsA.Count > 0 || itemsB.Count > 0)
  113.     {
  114.         double amountInEachInventory = currentItemCount / (double)balancedLists.Values[currentBalanceList].Count;
  115.         bool whole = ((itemsA.Count > 0 && IsWholeItem(itemsA[0])) || (itemsB.Count > 0 && IsWholeItem(itemsB[0])));
  116.         if (!whole || currentItemCount >= 2.0)
  117.         {
  118.             if (whole) amountInEachInventory = Math.Floor(amountInEachInventory);
  119.             double amountInA = 0, amountInB = 0, moveAmount = 0;
  120.             if (itemsA.Count > 0) amountInA = (double)itemsA[0].Amount;
  121.             if (itemsB.Count > 0) amountInB = (double)itemsB[0].Amount;
  122.             if (amountInA > amountInEachInventory && amountInB < amountInEachInventory)
  123.             {
  124.                 moveAmount = amountInA - amountInEachInventory;
  125.                 if (amountInB + moveAmount > amountInEachInventory) moveAmount = amountInEachInventory - amountInB;
  126.                 invB.TransferItemFrom(invA, itemsA[0], (VRage.MyFixedPoint)moveAmount);
  127.             }
  128.             else if (amountInA < amountInEachInventory && amountInB > amountInEachInventory)
  129.             {
  130.                 moveAmount = amountInB - amountInEachInventory;
  131.                 if (amountInA + moveAmount > amountInEachInventory) moveAmount = amountInEachInventory - amountInA;
  132.                 invA.TransferItemFrom(invB, itemsB[0], (VRage.MyFixedPoint)moveAmount);
  133.             }
  134.         }
  135.     }
  136. }
  137.  
  138. public bool CountItems()
  139. {
  140.     List<MyInventoryItem> items = new List<MyInventoryItem>();
  141.     balancedLists.Values[currentBalanceList][currentCountIndex].GetInventory(0).GetItems(items, b => b.Type.ToString() == knownItems[currentBalanceItem]);
  142.     for (int i = 0; i < items.Count; i++)
  143.         currentItemCount += (double)items[i].Amount;
  144.     currentCountIndex++;
  145.     if (currentCountIndex >= balancedLists.Values[currentBalanceList].Count)
  146.     {
  147.         currentCountIndex = 0;
  148.         counted = true;
  149.         return true;
  150.     }
  151.     return false;
  152. }
  153.  
  154. public bool IsWholeItem(MyInventoryItem item)
  155. {
  156.     return (item.Type.TypeId != "MyObjectBuilder_Ore" && item.Type.TypeId != "MyObjectBuilder_Ingot");
  157. }
  158.  
  159. public void GetBalancedLists()
  160. {
  161.     List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  162.     GridTerminalSystem.GetBlocksOfType<IMyEntity>(blocks, b => b.InventoryCount > 0 && b.CustomData.ToLower().Contains("[balance") && b.CustomData.Contains("]"));
  163.     for (int i = 0; i < blocks.Count; i++)
  164.     {
  165.         string key = blocks[i].CustomData.ToLower();
  166.         key = key.Substring(key.IndexOf("[") + 1);
  167.         key = key.Substring(0, key.IndexOf("]"));
  168.         if (!balancedLists.ContainsKey(key)) balancedLists[key] = new List<IMyTerminalBlock>();
  169.         balancedLists[key].Add(blocks[i]);
  170.     }
  171.     for (int i = 0; i < balancedLists.Values.Count; i += 0)
  172.     {
  173.         if (balancedLists.Values[i].Count == 1) balancedLists.RemoveAt(i);
  174.         else i++;
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement