Advertisement
GauHelldragon

SE - Build Loader

Jan 7th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. class part
  2. {
  3.     public string partName;
  4.     public int count;
  5.    
  6.     public part(string name,int c)
  7.     {
  8.         this.partName = name;
  9.         this.count = c;
  10.     }
  11. }  
  12.  
  13. class partList
  14. {
  15.     public List<part> myParts;
  16.  
  17.     public partList() {
  18.         myParts = new List<part>();
  19.         myParts.Add(new part("SteelPlate",2000));
  20.         myParts.Add(new part("Construction",500));
  21.         myParts.Add(new part("InteriorPlate",300));
  22.         myParts.Add(new part("Computer",200));
  23.         myParts.Add(new part("Motor",250));
  24.         myParts.Add(new part("SmallTube",400));
  25.         myParts.Add(new part("LargeTube",100));
  26.         myParts.Add(new part("MetalGrid",150));
  27.         myParts.Add(new part("Display",100));
  28.         myParts.Add(new part("BulletproofGlass",100));
  29.         myParts.Add(new part("Thrust",50));
  30.         myParts.Add(new part("Reactor",50));
  31.         myParts.Add(new part("RadioCommunication",10));
  32.     }
  33.    
  34.     public part getPart(string name) {
  35.         for ( int i = 0; i < myParts.Count ; i++ ) {
  36.             part myPart = myParts[i];
  37.             if ( myPart.partName == name ) { return myPart; }
  38.         }
  39.         return null;
  40.     }
  41.     public void addCount(string name,int count) {
  42.         part myPart = getPart(name);
  43.         if ( myPart != null ) { myPart.count += count; }
  44.     }
  45.     public int getPartCount(string name) {
  46.         part myPart = getPart(name);
  47.         if ( myPart != null ) { return myPart.count; }
  48.         return -1;
  49.     }
  50.    
  51.     public bool areAllPartsFufilled() {
  52.         for ( int i = 0; i < myParts.Count; i++ ) {
  53.             if ( myParts[i].count > 0 ) { return false; }
  54.         }
  55.         return true;
  56.     }
  57.                
  58.  
  59.    
  60. }
  61.  
  62. void Main(string argument)
  63. {
  64. //  IMyTextPanel tPanel = GridTerminalSystem.GetBlockWithName("Metal-Panel") as IMyTextPanel;
  65.  
  66.     IMyCargoContainer bCargo = GridTerminalSystem.GetBlockWithName("Cargo - Builder") as IMyCargoContainer;
  67.    
  68.     if ( bCargo == null ) {
  69.         Echo("No Build Ship Found");
  70.         return;
  71.     }
  72.     else { Echo("Found ship."); }
  73.  
  74.     partList parts = new partList();
  75.     IMyInventory cargoInv = bCargo.GetInventory(0);
  76.     List<IMyInventoryItem> cItems = new List<IMyInventoryItem>();
  77.     cItems = cargoInv.GetItems();
  78.    
  79.     for ( int i = 0; i < cItems.Count; i++ ) {
  80.         IMyInventoryItem item = cItems[i];
  81.         int decreaser = 0 - (int)item.Amount;
  82.         parts.addCount(item.Content.SubtypeName, decreaser);   
  83.     }
  84.    
  85.     if ( parts.areAllPartsFufilled() ) { return; }
  86.    
  87.     List<IMyTerminalBlock> containers = new List<IMyTerminalBlock>();
  88.     GridTerminalSystem.GetBlocks(containers);
  89.    
  90.     for ( int i = 0; i < containers.Count; i++ ) {
  91.        
  92.         if ( !( containers[i] is IMyAssembler ) && !(containers[i] is IMyCargoContainer) )  { continue; }
  93.         if ( containers[i] == bCargo ) { continue; }
  94.         if ( !containers[i].IsFunctional ) { continue; }
  95.        
  96.         IMyInventory inventory;
  97.         if ( containers[i] is IMyAssembler ) { inventory = containers[i].GetInventory(1); }
  98.         else { inventory = containers[i].GetInventory(0); }
  99.        
  100.        
  101.        
  102.         //Echo("Checking: " + containers[i].CustomName);
  103.        
  104.        
  105.        
  106.         if ( inventory != null ) {
  107.             //if ( !cargoInv.IsConnected(inventory) ) { continue; } BOOOOOOO
  108.            
  109.             List<IMyInventoryItem> items = new List<IMyInventoryItem>();
  110.             items = inventory.GetItems();
  111.             if ( items == null ) { Echo("Bad item list"); continue; }
  112.             for ( int i2 = items.Count-1; i2 >= 0; i2-- ) {
  113.                 IMyInventoryItem item = items[i2];
  114.                 if ( item == null ) { Echo("Bad item in slot " + i2.ToString()); continue; }
  115.                 part iPart = parts.getPart(item.Content.SubtypeName);
  116.                
  117.                 if ( iPart != null && iPart.count >= 0 ) {
  118.                     int cAmount = (int)item.Amount;
  119.                     int toRemove = Math.Min(cAmount,iPart.count);
  120.                     iPart.count -= toRemove;
  121.                     //Echo("Pulling " + toRemove.ToString() + " " + item.Content.SubtypeName + " from " + containers[i].CustomName);
  122.                     //if ( cargoInv.TransferItemFrom(inventory,i2,null,null,(VRage.MyFixedPoint)toRemove) ) { return; }
  123.                     cargoInv.TransferItemFrom(inventory,i2,null,null,(VRage.MyFixedPoint)toRemove);
  124.                     //i2--;
  125.                    
  126.                 }  
  127.                
  128.                
  129.             }
  130.         }
  131.        
  132.     }
  133.    
  134.    
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement