Advertisement
GauHelldragon

SE - Refinery Balancer

Feb 4th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.86 KB | None | 0 0
  1. const bool REMOVE_STONE = false;
  2.  
  3. VRage.Game.ModAPI.Ingame.IMyInventory getStoneStorage() {
  4.     List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  5.     GridTerminalSystem.SearchBlocksOfName("Stone", blocks);
  6.    
  7.     for ( int i = 0; i < blocks.Count; i++ ) {
  8.         if ( blocks[i] is IMyCargoContainer ) {
  9.             VRage.Game.ModAPI.Ingame.IMyInventory inventory = blocks[i].GetInventory(0);
  10.             if ( !inventory.IsFull ) {
  11.                 return inventory;
  12.             }
  13.         }
  14.            
  15.    
  16.        
  17.     }
  18.     return null;
  19.    
  20. }
  21.  
  22.  
  23. class myRefine
  24. {
  25.     public IMyRefinery refinery;
  26.     public myRefine(IMyRefinery r)
  27.     {
  28.         refinery = r;
  29.     }
  30.    
  31.     public double getVolume()
  32.     {
  33.         VRage.Game.ModAPI.Ingame.IMyInventory inventory;
  34.         inventory = refinery.GetInventory(0);
  35.         double total = 0.0;
  36.         List<VRage.Game.ModAPI.Ingame.IMyInventoryItem> items = new List<VRage.Game.ModAPI.Ingame.IMyInventoryItem>();
  37.         items = inventory.GetItems();
  38.         for ( int i = 0; i < items.Count; i++ ) {
  39.             VRage.Game.ModAPI.Ingame.IMyInventoryItem item = items[i];
  40.             //parts.increaseCount(item.Content.SubtypeName, (int)item.Amount);
  41.             total += (double)item.Amount;
  42.         }
  43.         return total;
  44.     }
  45.     public void GrabFromNeighbor(myRefine neighbor, double amount)
  46.     {
  47.         if ( amount == 0 ) { return; }
  48.         VRage.Game.ModAPI.Ingame.IMyInventory sourceInv;
  49.         VRage.Game.ModAPI.Ingame.IMyInventory targetInv;
  50.         if ( amount > 0 ) {
  51.             sourceInv = neighbor.refinery.GetInventory(0);
  52.             targetInv = refinery.GetInventory(0);
  53.         } else {
  54.             targetInv = neighbor.refinery.GetInventory(0);
  55.             sourceInv = refinery.GetInventory(0);
  56.         }
  57.         int index;
  58.         amount = Math.Abs(amount);
  59.         if ( amount > 0 ) {
  60.             index = sourceInv.GetItems().Count-1;
  61.             targetInv.TransferItemFrom(sourceInv,index,null,null,(VRage.MyFixedPoint)amount);
  62.         }
  63.        
  64.     }
  65.    
  66.     public int RemoveStone(VRage.Game.ModAPI.Ingame.IMyInventory storageInv)
  67.     {
  68.         if ( storageInv == null ) { return 0; }
  69.         VRage.Game.ModAPI.Ingame.IMyInventory inventory = refinery.GetInventory(0);
  70.         List<VRage.Game.ModAPI.Ingame.IMyInventoryItem> items = inventory.GetItems();
  71.         int stoneRemoved = 0;
  72.         for ( int i = 0; i < items.Count; i++ ) {
  73.             VRage.Game.ModAPI.Ingame.IMyInventoryItem item = items[i];
  74.             if ( item.Content.SubtypeName == "Stone" )
  75.             {
  76.                 //Echo("Attempting to move " + item.Amount);
  77.                 stoneRemoved += (int)item.Amount;
  78.                 storageInv.TransferItemFrom(inventory,i);
  79.             }
  80.         }
  81.         return stoneRemoved;
  82.     }
  83.    
  84.     public void Balance(List<myRefine> refines,double average, int i)
  85.     {
  86.         int tRefines = refines.Count;
  87.         double myVol = getVolume();
  88.        
  89.         double margin = average * 0.05;
  90.         if ( myVol > average - margin && myVol < average + margin ) { return; }
  91.         double neededAmount = average - myVol;
  92.        
  93.         myRefine neighbor = refines[i+1];
  94.         this.GrabFromNeighbor(neighbor,neededAmount);
  95.    
  96.     }
  97.    
  98. }
  99.  
  100. void Main(string argument)
  101. {
  102.     List<IMyTerminalBlock> refineries = new List<IMyTerminalBlock>();
  103.     GridTerminalSystem.GetBlocksOfType<IMyRefinery>(refineries);
  104.     if ( refineries == null || refineries.Count < 1 )
  105.     {
  106.         Echo("No refineries found.");
  107.         return;
  108.     }
  109.    
  110.     List<myRefine> myRefines = new List<myRefine>();
  111.     for ( int i = 0; i < refineries.Count; i++ )
  112.     {
  113.         myRefines.Add(new myRefine((IMyRefinery)refineries[i]));
  114.     }
  115.    
  116.     if ( REMOVE_STONE  )
  117.     {
  118.         VRage.Game.ModAPI.Ingame.IMyInventory storage = getStoneStorage();
  119.         Echo("Removing Stone..");
  120.        
  121.         for ( int i = 0; i < myRefines.Count && (storage != null) ; i++ )
  122.         {
  123.             //Echo(i.ToString());
  124.         //  Echo("Attempting to remove " + myRefines[i].RemoveStone(storage));
  125.             myRefines[i].RemoveStone(storage);
  126.             if ( storage.IsFull ) { storage = getStoneStorage(); }
  127.         }
  128.     }
  129.    
  130.    
  131.     double total = 0.0;
  132.     for ( int i = 0; i < myRefines.Count; i++ )
  133.     {
  134.         total += myRefines[i].getVolume();
  135.     }
  136.     double average = total / myRefines.Count;
  137.    
  138.     for ( int i = 0; i < myRefines.Count - 1; i++ )
  139.     {
  140.         myRefines[i].Balance(myRefines,average,i);
  141.     }
  142.    
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement