Ore Sorting 5/5 (1)

posted in: Inventory Management, Scripts | 3

This script will take all the ore in your containers, refineries and connectors and move it to a container (or containers) called “Raw Ore” (you can change the name of the destination container(s) by changing the constant string at the beginning of the script.

  1. // Defines the names of the containers that will be used to store
  2. // unrefined ore. You can have as many containers with this name
  3. // as you want
  4. const string ORE_CONTAINER = "Raw Ore";  
  5.  
  6. // Create all the list objects that will contain the lists of the
  7. // various types of blocks
  8. List<IMyTerminalBlock> containers = new List<IMyTerminalBlock>();    
  9. List<IMyTerminalBlock> refineries = new List<IMyTerminalBlock>();    
  10. List<IMyTerminalBlock> connectors = new List<IMyTerminalBlock>();  
  11.  
  12. List<IMyTerminalBlock> oreContainers = new List<IMyTerminalBlock>();  
  13.  
  14. // Work variable to contain the inventory to which we will transfer
  15. // unrefined ore. It will change over the course of running the
  16. // program  
  17. IMyInventory oreDestination;  
  18.  
  19. // Main method (entry point)  
  20. void Main()      
  21. {        
  22.     // Loads the lists with IMyCargoContainer, IMyRefinery and
  23.     // IMyShipConnector blocks
  24.     GridTerminalSystem.GetBlocksOfType<IMyCargoContainer>(containers);  
  25.     GridTerminalSystem.GetBlocksOfType<IMyRefinery>(refineries);  
  26.     GridTerminalSystem.GetBlocksOfType<IMyShipConnector>(connectors);  
  27.        
  28.     // Find all blocks that have the name specified at the top of this
  29.     // script
  30.     GridTerminalSystem.SearchBlocksOfName(ORE_CONTAINER, oreContainers);  
  31.    
  32.     // Call the SortOre() method
  33.     sortOre();  
  34. }    
  35.  
  36. // Method to find a block that has space available in its inventory
  37. // The list of blocks to search is passed in as a parameter
  38. IMyInventory getDestination(List<IMyTerminalBlock> destination) {  
  39.  
  40.     // If the list passed in is empty, return null
  41.     if (destination == null) {  
  42.        return null;  
  43.     }  
  44.  
  45.     // Loop through all blocks in the "destination" list        
  46.     for (int n = 0; n < destination.Count; n++)  
  47.     {  
  48.         // Get the first inventory of the block
  49.         var containerInv = (destination&#91;n] as IMyInventoryOwner).GetInventory(0);  
  50.  
  51.         // If the inventory is not full, return this inventory to caller
  52.         if (!containerInv.IsFull) {  
  53.            return containerInv;  
  54.         }  
  55.     }  
  56.      
  57.     // If we got here, all blocks had full inventory. There are no blocks
  58.     // to use to transfer, so return null.
  59.     return null;
  60. }  
  61.  
  62. // moveItems method take a list of blocks, an item type string, another list of
  63. // destination blocks, a destination type string, and an inventory index number.
  64. //
  65. void moveItems(List<IMyTerminalBlock> containerList, String itemType, List<IMyTerminalBlock> destination, String destinationType, int inventoryIndex) {  
  66.  
  67.     // Loop through all the blocks in containerList
  68.     for (int i = 0; i < containerList.Count; i++)  
  69.     {  
  70.         // Get inventory owner object for the block
  71.         var containerInvOwner = (IMyInventoryOwner)containerList&#91;i];    
  72.  
  73.         // Get the inventory from the inventory owner. Get the nth inventory
  74.         // as pointed to by inventoryIndex
  75.         var containerInv = containerInvOwner.GetInventory(inventoryIndex);  
  76.  
  77.         // Get all the items in the inventory
  78.         var containerItems = containerInv.GetItems();  
  79.  
  80.         // Loop through all the items in the list, starting at the last item
  81.         // and working our way to the first
  82.         for(int j = containerItems.Count - 1; j >= 0; j--)    
  83.         {    
  84.             // Call the getDestination method with the list of destination
  85.             // containers to get back one that has space available.
  86.             IMyInventory destinationContainerInventory = getDestination(destination);  
  87.  
  88.             // If we got back null, that means that all destination containers are
  89.             // full (or there are none), so we exit out of this method
  90.             if (destinationContainerInventory == null) {  
  91.                return;  
  92.             }
  93.  
  94.             // Check to see whether the item we are handling is of the type
  95.             // specified by itemType and also check to see if the container
  96.             // is not one of the target containers (no sense in transferring stuff
  97.             // from one "Raw Ore" container to another "Raw Ore" container
  98.             if (containerItems[j].Content.ToString().Contains(itemType) &&
  99.                !containerList[i].CustomName.Contains(destinationType))    
  100.             {    
  101.                // Transfer the item in slot "j" of containerInv to the
  102.                // destinationContainerInventory we retrieved above. The
  103.                // "true" in the fourth parameter is "Stack if possible"
  104.                containerInv.TransferItemTo(destinationContainerInventory, j, null, true, null);    
  105.             }    
  106.         }    
  107.     }  
  108. }  
  109.    
  110. // Little method that makes the calls to the moveItems method.
  111. void sortOre()  
  112. {  
  113.     moveItems(containers, "Ore", oreContainers, ORE_CONTAINER, 0);  
  114.     moveItems(connectors, "Ore", oreContainers, ORE_CONTAINER, 0);  
  115.     moveItems(refineries, "Ore", oreContainers, ORE_CONTAINER, 0);  
  116. }

Please rate this post

3 Responses

  1. Thank you.. for all your scripts <3

Leave a Reply