Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public SortedList<string, List<IMyTerminalBlock>> balancedLists = new SortedList<string, List<IMyTerminalBlock>>();
- public List<string> knownItems = new List<string>();
- public int
- currentBalanceList = 0, currentBalanceIndex = 0, nextBalanceIndex = 1, currentBalanceItem = 0,
- currentLearningList = 0, currentLearningIndex = 0, currentCountIndex = 0;
- public double currentItemCount = 0;
- public bool learningItems = true, counted = false;
- public Program()
- {
- Runtime.UpdateFrequency = UpdateFrequency.Update10;
- GetBalancedLists();
- Save();
- }
- public void Save() {}
- public void Main(string arg, UpdateType updateSource)
- {
- Echo("Balance Lists: " + balancedLists.Values.Count);
- Echo("Known Items: " + knownItems.Count);
- if (balancedLists.Values.Count > 0)
- {
- try {
- if (learningItems) LearnItems();
- else BalanceLists();
- }
- catch {
- Echo("Error caught in main");
- Echo("Try recompiling");
- Echo("If the error persists, report to the author");
- }
- }
- }
- public void LearnItems()
- {
- Echo("Learning Items");
- List<MyInventoryItem> items = new List<MyInventoryItem>();
- balancedLists.Values[currentLearningList][currentLearningIndex].GetInventory(0).GetItems(items, b => UnknownItem(b));
- currentLearningIndex++;
- if (currentLearningIndex >= balancedLists.Values[currentLearningList].Count)
- {
- currentLearningIndex = 0;
- currentLearningList++;
- }
- if (currentLearningList >= balancedLists.Values.Count) {
- currentLearningList = 0;
- learningItems = false;
- }
- }
- public bool UnknownItem(MyInventoryItem item)
- {
- string itemType = item.Type.TypeId.ToString();
- if (itemType == "MyObjectBuilder_PhysicalGunObject" || itemType == "MyObjectBuilder_GasContainerObject" || itemType == "MyObjectBuilder_OxygenContainerObject")
- return false;
- if (!knownItems.Contains(item.Type.ToString()))
- {
- knownItems.Add(item.Type.ToString());
- return true;
- }
- return false;
- }
- public void BalanceLists()
- {
- if (counted || CountItems())
- {
- Echo("Balancing: " + currentItemCount + ": " + knownItems[currentBalanceItem]);
- try {
- BalanceInventories(balancedLists.Values[currentBalanceList][currentBalanceIndex].GetInventory(0), balancedLists.Values[currentBalanceList][nextBalanceIndex].GetInventory(0));
- }
- catch { Echo("Error caught balancing lists"); }
- nextBalanceIndex++;
- if (nextBalanceIndex >= balancedLists.Values[currentBalanceList].Count)
- {
- nextBalanceIndex = 0;
- currentBalanceIndex++;
- }
- if (currentBalanceIndex == nextBalanceIndex && currentBalanceIndex + 1 < balancedLists.Values[currentBalanceList].Count) nextBalanceIndex++;
- if (currentBalanceIndex >= balancedLists.Values[currentBalanceList].Count)
- {
- nextBalanceIndex = 1;
- currentBalanceIndex = 0;
- currentBalanceItem++;
- currentItemCount = 0;
- counted = false;
- }
- if (currentBalanceItem >= knownItems.Count)
- {
- currentBalanceList++;
- currentBalanceItem = 0;
- }
- if (currentBalanceList >= balancedLists.Values.Count)
- {
- currentBalanceList = 0;
- learningItems = true;
- }
- }
- }
- public void BalanceInventories(IMyInventory invA, IMyInventory invB)
- {
- List<MyInventoryItem> itemsA = new List<MyInventoryItem>(), itemsB = new List<MyInventoryItem>();
- invA.GetItems(itemsA, b => b.Type.ToString() == knownItems[currentBalanceItem]);
- invB.GetItems(itemsB, b => b.Type.ToString() == knownItems[currentBalanceItem]);
- if (itemsA.Count > 0 || itemsB.Count > 0)
- {
- double amountInEachInventory = currentItemCount / (double)balancedLists.Values[currentBalanceList].Count;
- bool whole = ((itemsA.Count > 0 && IsWholeItem(itemsA[0])) || (itemsB.Count > 0 && IsWholeItem(itemsB[0])));
- if (!whole || currentItemCount >= 2.0)
- {
- if (whole) amountInEachInventory = Math.Floor(amountInEachInventory);
- double amountInA = 0, amountInB = 0, moveAmount = 0;
- if (itemsA.Count > 0) amountInA = (double)itemsA[0].Amount;
- if (itemsB.Count > 0) amountInB = (double)itemsB[0].Amount;
- if (amountInA > amountInEachInventory && amountInB < amountInEachInventory)
- {
- moveAmount = amountInA - amountInEachInventory;
- if (amountInB + moveAmount > amountInEachInventory) moveAmount = amountInEachInventory - amountInB;
- invB.TransferItemFrom(invA, itemsA[0], (VRage.MyFixedPoint)moveAmount);
- }
- else if (amountInA < amountInEachInventory && amountInB > amountInEachInventory)
- {
- moveAmount = amountInB - amountInEachInventory;
- if (amountInA + moveAmount > amountInEachInventory) moveAmount = amountInEachInventory - amountInA;
- invA.TransferItemFrom(invB, itemsB[0], (VRage.MyFixedPoint)moveAmount);
- }
- }
- }
- }
- public bool CountItems()
- {
- List<MyInventoryItem> items = new List<MyInventoryItem>();
- balancedLists.Values[currentBalanceList][currentCountIndex].GetInventory(0).GetItems(items, b => b.Type.ToString() == knownItems[currentBalanceItem]);
- for (int i = 0; i < items.Count; i++)
- currentItemCount += (double)items[i].Amount;
- currentCountIndex++;
- if (currentCountIndex >= balancedLists.Values[currentBalanceList].Count)
- {
- currentCountIndex = 0;
- counted = true;
- return true;
- }
- return false;
- }
- public bool IsWholeItem(MyInventoryItem item)
- {
- return (item.Type.TypeId != "MyObjectBuilder_Ore" && item.Type.TypeId != "MyObjectBuilder_Ingot");
- }
- public void GetBalancedLists()
- {
- List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyEntity>(blocks, b => b.InventoryCount > 0 && b.CustomData.ToLower().Contains("[balance") && b.CustomData.Contains("]"));
- for (int i = 0; i < blocks.Count; i++)
- {
- string key = blocks[i].CustomData.ToLower();
- key = key.Substring(key.IndexOf("[") + 1);
- key = key.Substring(0, key.IndexOf("]"));
- if (!balancedLists.ContainsKey(key)) balancedLists[key] = new List<IMyTerminalBlock>();
- balancedLists[key].Add(blocks[i]);
- }
- for (int i = 0; i < balancedLists.Values.Count; i += 0)
- {
- if (balancedLists.Values[i].Count == 1) balancedLists.RemoveAt(i);
- else i++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement