Component Sorting 5/5 (1)

posted in: Inventory Management, Scripts | 3

This script will take all the components in your containers, assemblers and connectors and move it to a container (or containers) called “Components” (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. // components. You can have as many containers with this name
  3. // as you want
  4. const string COMPONENT_CONTAINER = "Components";  
  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> assemblers = new List<IMyTerminalBlock>();  
  10. List<IMyTerminalBlock> connectors = new List<IMyTerminalBlock>();  
  11.  
  12. List<IMyTerminalBlock> componentContainers = new List<IMyTerminalBlock>();  
  13.  
  14. // Work variable to contain the inventory to which we will transfer
  15. // components. It will change over the course of running the
  16. // program  
  17. IMyInventory componentDestination;  
  18.    
  19. // Main method (entry point)  
  20. void Main()      
  21. {        
  22.     // Loads the lists with IMyCargoContainer, IMyAssembler and
  23.     // IMyShipConnector blocks
  24.     GridTerminalSystem.GetBlocksOfType<IMyCargoContainer>(containers);  
  25.     GridTerminalSystem.GetBlocksOfType<IMyAssembler>(assemblers);  
  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(COMPONENT_CONTAINER, componentContainers);  
  31.    
  32.     // Call the sortComponents() method
  33.     sortComponents();  
  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. void moveItems(List<IMyTerminalBlock> containerList, String itemType, List<IMyTerminalBlock> destination, String destinationType, int inventoryIndex) {  
  65.  
  66.     // Loop through all the blocks in containerList
  67.     for (int i = 0; i < containerList.Count; i++)  
  68.     {  
  69.         // Get inventory owner object for the block
  70.         var containerInvOwner = (IMyInventoryOwner)containerList&#91;i];    
  71.  
  72.         // Get the inventory from the inventory owner. Get the nth inventory
  73.         // as pointed to by inventoryIndex
  74.         var containerInv = containerInvOwner.GetInventory(inventoryIndex);  
  75.  
  76.         // Get all the items in the inventory
  77.         var containerItems = containerInv.GetItems();  
  78.  
  79.         // Loop through all the items in the list, starting at the last item
  80.         // and working our way to the first
  81.         for(int j = containerItems.Count - 1; j >= 0; j--)    
  82.         {    
  83.             // Call the getDestination method with the list of destination
  84.             // containers to get back one that has space available.
  85.             IMyInventory destinationContainerInventory = getDestination(destination);  
  86.  
  87.             // If we got back null, that means that all destination containers are
  88.             // full (or there are none), so we exit out of this method
  89.             if (destinationContainerInventory == null) {  
  90.                return;  
  91.             }  
  92.             // Check to see whether the item we are handling is of the type
  93.             // specified by itemType and also check to see if the container
  94.             // is not one of the target containers (no sense in transferring stuff
  95.             // from one "Components" container to another "Components" container
  96.             if (containerItems[j].Content.ToString().Contains(itemType) &&
  97.                !containerList[i].CustomName.Contains(destinationType))    
  98.             {    
  99.                // Transfer the item in slot "j" of containerInv to the
  100.                // destinationContainerInventory we retrieved above. The
  101.                // "true" in the fourth parameter is "Stack if possible"
  102.                containerInv.TransferItemTo(destinationContainerInventory, j, null, true, null);    
  103.             }    
  104.         }    
  105.     }  
  106. }  
  107.    
  108. // Little method that makes the calls to the moveItems method.
  109. void sortComponents()  
  110. {  
  111.     moveItems(containers, "Component", componentContainers, COMPONENT_CONTAINER, 0);  
  112.     // The inventory index for the assemblers is "1", because the assemblers
  113.     // have two inventories: one for refined ore, the other for components.
  114.     // Since this script is sorting components, we process the second one.    
  115.     moveItems(assemblers, "Component", componentContainers, COMPONENT_CONTAINER, 1);  
  116.     moveItems(connectors, "Component", componentContainers, COMPONENT_CONTAINER, 0);  
  117. }

Please rate this post

3 Responses

  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 the second inventory […]

  2. […] of the script to match the name that you use for your component storage. You might need to use the Component Sorting script to have all your components in similarly named […]

  3. Those comments in scripts do really help!
    that enable me to learn from it and tweak it as i need.
    Thanks a lot. Great job!

Leave a Reply