Ingot Sorting No rating results yet

posted in: Inventory Management, Scripts | 1

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

Please rate this post

One Response

  1. […] you look at my inventory sorting scripts (Ore Sorting, Ingot Sorting or Component Sorting) you will see that in some cases I use the first inventory, and in some cases […]

Leave a Reply