sironin

Clear Assemblers

Dec 11th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. List<IMyTerminalBlock> containers;
  2. IMyInventory containerDestination;
  3.  
  4. void Main()
  5. {
  6. List<IMyTerminalBlock> assemblers = new List<IMyTerminalBlock>();
  7. containers = new List<IMyTerminalBlock>();
  8.  
  9. GridTerminalSystem.GetBlocksOfType<IMyAssembler>(assemblers);
  10. GridTerminalSystem.SearchBlocksOfName("Ingot", containers);
  11. getNewContainer();
  12. for(int i = 0; i < assemblers.Count; i++)
  13. {
  14. cleanAssembler(assemblers[i] as IMyAssembler);
  15. }
  16. }
  17.  
  18. void getNewContainer()
  19. {
  20. containerDestination = null;
  21. // search our containers until we find one with space
  22. for (int n = 0; n < containers.Count; n++)
  23. {
  24. var container = containers[n];
  25. var containerInv = container.GetInventory(0);
  26. if (!IsFull(containerInv)) // check to see if it has room
  27. {
  28. Echo("Found container");
  29. Echo(container.CustomName);
  30. containerDestination = containerInv;
  31. return;
  32. }
  33. }
  34. return;
  35. }
  36.  
  37. void cleanAssembler(IMyAssembler assembler)
  38. {
  39. if (containerDestination == null)
  40. return;
  41.  
  42. var assemblerInv = assembler.GetInventory(0);
  43. List<MyInventoryItem> assemblerItems = new List<MyInventoryItem>();
  44. assemblerInv.GetItems(assemblerItems);
  45. for (int i = 0; i < assemblerItems.Count; i++){
  46. if (!IsFull(containerDestination)){
  47. Echo("moving items");
  48. assemblerInv.TransferItemTo(containerDestination, i, null, true, null);
  49. }
  50. else { //look for new container
  51. getNewContainer();
  52. }
  53. }
  54.  
  55. }
  56.  
  57. float getPercent(IMyInventory inv)
  58. {
  59. return ((float)inv.CurrentVolume / (float)inv.MaxVolume) * 100f;
  60. }
  61.  
  62.  
  63.  
  64. bool IsFull(IMyInventory inv)
  65. {
  66. if (getPercent(inv) >= 95)
  67. return true;
  68. else
  69. return false;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment