Autofeed Reactors with Uranium Ingots 5/5 (1)

posted in: Scripts | 2

The below script supply your reactors with Uranium Ingots (assuming you have some in storage).

You set the name of the reactors to a string specified in the script (default: “Autofed Reactor”) and follow it with the number of ingots you want the reactor to have (i.e. “Autofed Reactor 100”).

The script will search all your cargo containers and refineries and transfer uranium to the reactor until it has exactly 100 ingots (in the above example).

Two small details:
* The reactors need to be on your conveyor system
* The reactors should have “Use Conveyor System” turned off (otherwise they will pull uranium on their own and won’t stop when they are “full”)

  1. // This script autofeeds your reactors with specified amounts
  2. // of uranium.
  3. //
  4. // To make it work, change the name of all the reactors that
  5. // you want to autofeed to the constant specified below, using
  6. // a suffix of the quantity of uranium you want the reactor to
  7. // have:
  8. //
  9. // Example: "Autofed Reactor 20" - would autofeed to a quantity
  10. //                                 of 20 uranium
  11. //
  12. //          "Autofed Reactor 100" - would autofeed to a quantity
  13. //                                  of 100 uranium
  14. //
  15. // The prefix ("Autofed Reactor") can be whatever you want, but
  16. // all the reactors that you want to autofeed must use the same
  17. // prefix. That prefix is specified in the constant "REACTORS"
  18. // below.
  19. //
  20. // The script will search all cargo containers and the OUTPUT
  21. // of all refineries to locate uranium to move to the reactors.
  22. //
  23. // If there is not enough available uranium to fulfill the
  24. // quantities specified, the script will move all available
  25. // uranium and stop.
  26. //
  27. // Example: 2 reactors:
  28. //            Autofed Reactor 20 with 5 uranium
  29. //            Autofed Reactor 50 with 5 uranium
  30. //          1 Cargo Container with 32 uranium
  31. //
  32. // After the script runs, "Autofed Reactor 20" will have
  33. // 20 uranium; "Autofed Reactor 50" will have 22 uranium
  34. // and the cargo container will be empty. This assumes
  35. // that the reactors are processed in the order listed
  36. // above. There is no guarantee as to which order the
  37. // reactors will be processed.
  38.  
  39. const String REACTORS = "Autofed Reactor";
  40.  
  41. // Allocate list to hold containers we will search for uranium
  42. List<IMyTerminalBlock> containers = new List<IMyTerminalBlock>();  
  43.  
  44. void Main()  
  45. {  
  46.     // Clear list of containers
  47.     containers.Clear();
  48.    
  49.     // Allocate list to hold reactors
  50.     List<IMyTerminalBlock> reactors = new List<IMyTerminalBlock>();  
  51.    
  52.     // Allocate list for temporary work
  53.     List<IMyTerminalBlock> work = new List<IMyTerminalBlock>();  
  54.    
  55.     // Load "containers" list with cargo containers
  56.     GridTerminalSystem.GetBlocksOfType<IMyCargoContainer>(work);    
  57.     containers.AddRange(work);
  58.     // and refineries
  59.     GridTerminalSystem.GetBlocksOfType<IMyRefinery>(work);    
  60.     containers.AddRange(work);
  61.    
  62.     // Find the reactors to autofeed
  63.     GridTerminalSystem.SearchBlocksOfName(REACTORS, reactors);        
  64.    
  65.     // Loop through the reactors
  66.     for (int i = 0; i < reactors.Count; i++)    
  67.     {    
  68.    
  69.         // Retrieve reactor InventoryOwner, Inventory and Items
  70.         var invOwner = reactors&#91;i] as IMyInventoryOwner;
  71.         var inventory = invOwner.GetInventory(0);
  72.         var items = inventory.GetItems();  
  73.        
  74.         // Find the current total uranium in the reactor
  75.         float totalUranium = 0.0f;
  76.         for(int j = items.Count - 1; j >= 0; j--)    
  77.         {    
  78.             totalUranium += (float)items[j].Amount;
  79.         }  
  80.        
  81.         // Find the desired uranium quantity in the name
  82.         // of the reactor
  83.         float desiredQuantity = float.Parse(reactors[i].CustomName.Replace(REACTORS,"").Trim());
  84.        
  85.         // If current uranium amount is less than desired amount,
  86.         // start loop until amount is the desired amount OR we
  87.         // run out of uranium to move
  88.         while (totalUranium < desiredQuantity)
  89.         {
  90.             // Call the "moveUranium" method telling it how much
  91.             // uranium we need to move. The method returns the
  92.             // actual amount moved so we can update the current
  93.             // total amount
  94.             float movedUranium = moveUranium(desiredQuantity - totalUranium, inventory);
  95.            
  96.             // If the "moveUranium" method returns 0, it means
  97.             // there is no more uranium to move, so we break
  98.             // out of the loop
  99.             if (movedUranium == 0.0f) { break; }
  100.            
  101.             // Add the amount of uranium moved to the current
  102.             // totalUranium
  103.             totalUranium += movedUranium;
  104.         }        
  105.     }  
  106. }  
  107.  
  108. // Move uranium methods that moves the specified amount
  109. // of uranium, to the specified inventory
  110. float moveUranium(float neededAmount, IMyInventory reactorInventory)
  111. {
  112.     // Loop through all objects in the "containers" list.
  113.     // (Cargo containers and refineries)
  114.     for (int i = 0; i < containers.Count; i++)    
  115.     {    
  116.         // Get container InventoryOwner
  117.         var invOwner = containers&#91;i] as IMyInventoryOwner;
  118.         IMyInventory inventory;
  119.         // If the container object is a "Cargo Container", we use the
  120.         // first inventory.
  121.         if (containers&#91;i] is IMyCargoContainer) {
  122.             inventory = invOwner.GetInventory(0);
  123.         }
  124.         // If the container object is a refinery, use the second
  125.         // inventory, since we want ingots and not raw ore
  126.         if (containers&#91;i] is IMyRefinery) {
  127.             inventory = invOwner.GetInventory(1);
  128.         }
  129.        
  130.         // Get the items in the container's inventory
  131.         var items = inventory.GetItems();  
  132.        
  133.         // Loop through all items in the inventory
  134.         for(int j = items.Count - 1; j >= 0; j--)    
  135.         {    
  136.             // If the item is "Uranium Ingot"
  137.             if ((items[j].Content.TypeId.ToString().EndsWith("_Ingot")) &&
  138.                 (items[j].Content.SubtypeName.Equals("Uranium")))
  139.             {
  140.                // Get the amount to transfer (either the amount
  141.                // requested or the current amoount in the inventory
  142.                // of the object we are processing, whichever is
  143.                // smaller)
  144.                float foundAmount = Math.Min((float)items[j].Amount, neededAmount);
  145.                
  146.                // Transfer the foundAmount of uranium to the
  147.                // reactor's inventory
  148.                inventory.TransferItemTo(reactorInventory, j, null, true, (VRage.MyFixedPoint)foundAmount);    
  149.                
  150.                // Return the amount of uranium transferred to the
  151.                // caller
  152.                return foundAmount;
  153.             }
  154.         }  
  155.     }  
  156.     // If we got here, it means we ran out of uranium to transfer
  157.     // (no more uranium in the cargo containers or the refineries),
  158.     // so return 0 to the caller so it knows to stop looping.
  159.     return 0.0f;
  160. }

Please rate this post

2 Responses

  1. Hi!
    First of all, thank you for this site! I’ve learned a lot from it!
    Now, Im doing some scripting on my own and i just want to ask you regarding a problem i have…

    Im trying to make a panel write out text in two columns, but i cant get the formatting to work, ive tried PadLeft, PadRight, and string.Format(“{0,20}”) syntax but with no luck… It works in Visual Studio though….

    Any suggestions?

    • Not really much you can do. Problem is the font.

      We need to have the option of a monospaced font to be able to do alignment.

Leave a Reply