Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. /*
  2. * You need:
  3. * LCD/TextPanel named "LCD SortList"
  4. * LCD/TextPanel named "LCD Status Sorter"
  5. * ...or you choose your own name by editing the strings below.
  6. *
  7. * List the items and target containers on the public text of the LCD SortList:
  8. *
  9. * <Item Type>/<Item>:<Container1>,<Container2>
  10. *
  11. * "Ingot/Iron:Large Cargo Container 1,Large Cargo Container 2"
  12. * "Component/Thruster:Large Cargo Container 3"
  13. *
  14. * Exclusion:
  15. * There are two ways to exclude containers, refineries, assemblers and connectors.
  16. * 1) add them in the sortlist as "Exclude:<Container1>,<Container2>"
  17. * 2) add a "#" in the name of the block. You can change this token below.
  18. *
  19. */
  20.  
  21. // if you want to rename the LCDs, here are the strings
  22. string LCDNameDebug = "LCD Status Sorter";
  23. string LCDNameSortList = "LCD SortList";
  24. string ExcludeToken = "#";
  25.  
  26. void Main()
  27. {
  28. NewSort();
  29. }
  30.  
  31. void DebugMessage(string Message)
  32. {
  33. DisplayOnLCD(LCDNameDebug, Message);
  34. }
  35.  
  36. void DisplayOnLCD(string LCDName, string Content)
  37. {
  38. var LCDPanel = GridTerminalSystem.GetBlockWithName(LCDName) as IMyTextPanel;
  39. if (LCDPanel == null) return;
  40. LCDPanel.ShowTextureOnScreen();
  41. LCDPanel.WritePublicText(Content);
  42. LCDPanel.ShowPublicTextOnScreen();
  43. return;
  44. }
  45.  
  46. void NewSort()
  47. {
  48. string Output = " Last Moves at "
  49. + DateTime.Now.Hour.ToString() + ":"
  50. + DateTime.Now.Minute.ToString() + ":"
  51. + DateTime.Now.Second.ToString()
  52. + "\n ----------------------------------------\n";
  53. DebugMessage(Output);
  54. var LCDPanel = GridTerminalSystem.GetBlockWithName(LCDNameSortList) as IMyTextPanel;
  55. if (LCDPanel == null) return;
  56.  
  57. var RawMessages = LCDPanel.GetPublicText();
  58.  
  59. string[] Messages = RawMessages.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
  60.  
  61. for (int i = 0; i < Messages.Length; i++)
  62. {
  63. var Message = Messages[i].Trim();
  64. if (Message == "") return;
  65. }
  66.  
  67. List<IMyTerminalBlock> Containers = new List<IMyTerminalBlock>();
  68. GridTerminalSystem.GetBlocksOfType<IMyCargoContainer>(Containers);
  69. if (Containers.Count == 0) return;
  70.  
  71. List<IMyTerminalBlock> Refineries = new List<IMyTerminalBlock>();
  72. GridTerminalSystem.GetBlocksOfType<IMyRefinery>(Refineries);
  73. Containers.AddList<IMyTerminalBlock>(Refineries);
  74.  
  75. List<IMyTerminalBlock> Assembler = new List<IMyTerminalBlock>();
  76. GridTerminalSystem.GetBlocksOfType<IMyAssembler>(Assembler);
  77. Containers.AddList<IMyTerminalBlock>(Assembler);
  78.  
  79. List<IMyTerminalBlock> Connector = new List<IMyTerminalBlock>();
  80. GridTerminalSystem.GetBlocksOfType<IMyShipConnector>(Connector);
  81. Containers.AddList<IMyTerminalBlock>(Connector);
  82. List<string> SortList = new List<string>(Message.Split('\n'));
  83. List<string> SortItemNames = new List<string>();
  84. List<string> SortTargets = new List<string>();
  85. List<string> Excludes = new List<string>();
  86.  
  87. for (int SortIndex = 0; SortIndex < SortList.Count; SortIndex++)
  88. {
  89. var ItemName = SortList[SortIndex].Split(':')[0];
  90. var Targets = SortList[SortIndex].Split(':')[1];
  91.  
  92. if (ItemName == "Exclude")
  93. {
  94. if (Targets.Contains(","))
  95. {
  96. Excludes.AddArray<string>(Targets.Split(','));
  97. }
  98. else
  99. {
  100. Excludes.Add(Targets);
  101. }
  102. continue;
  103. }
  104.  
  105. SortItemNames.Add(ItemName);
  106. SortTargets.Add(Targets);
  107. }
  108.  
  109. for (int SourceContainerIndex = 0; SourceContainerIndex < Containers.Count; SourceContainerIndex++)
  110. {
  111. var SourceContainer = Containers[SourceContainerIndex];
  112. if (Excludes.Contains(SourceContainer.CustomName)) continue;
  113. if (SourceContainer.CustomName.Contains(ExcludeToken)) continue;
  114. var SourceInventory = SourceContainer.GetInventory(SourceContainer.GetInventoryCount() - 1);
  115. var SourceItems = SourceInventory.GetItems();
  116. if (SourceItems.Count == 0) continue;
  117.  
  118. int SourceItemIndex = 0;
  119. while (SourceItemIndex < SourceItems.Count)
  120. {
  121. var SourceItem = SourceItems[SourceItemIndex];
  122. var SourceItemName = GetInventoryItemName(SourceItem);
  123.  
  124. int Index = SortItemNames.IndexOf(SourceItemName);
  125. if (Index == -1)
  126. {
  127. SourceItemIndex++;
  128. continue;
  129. }
  130. List<string> TargetContainerList = new List<string>();
  131.  
  132. if (SortTargets[Index] == null || SortTargets[Index] == "")
  133. {
  134. SourceItemIndex++;
  135. continue;
  136. }
  137.  
  138. if (SortTargets[Index].Contains(","))
  139. {
  140. TargetContainerList.AddArray<string>(SortTargets[Index].Split(','));
  141. }
  142. else
  143. {
  144. TargetContainerList.Add(SortTargets[Index]);
  145. }
  146.  
  147. if (TargetContainerList.Contains(SourceContainer.CustomName))
  148. {
  149. SourceItemIndex++;
  150. continue;
  151. }
  152.  
  153. bool DidMove = false;
  154. for (int TargetIndex = 0; TargetIndex < TargetContainerList.Count; TargetIndex++)
  155. {
  156. var TargetContainer = GridTerminalSystem.GetBlockWithName(TargetContainerList[TargetIndex]);
  157. if (TargetContainer == null) continue;
  158. if (TargetContainer == SourceContainer) continue;
  159. var TargetInventory = TargetContainer.GetInventory(0);
  160. if (!SourceInventory.IsConnectedTo(TargetInventory) || TargetInventory.IsFull) continue;
  161. if (!SourceInventory.TransferItemTo(TargetInventory, SourceItemIndex, null, true, null))
  162. {
  163. string FailMessage = "ITEM TRANSFER FAILURE" + "\n"
  164. + "SourceItemName: " + SourceItemName + "\n"
  165. + "SourceContainer: " + SourceContainer.CustomName + "\n"
  166. + "TargetContainer: " + TargetContainerList[TargetIndex] + "\n"
  167. + "\n" + Output;
  168. DebugMessage(FailMessage);
  169. return;
  170. }
  171. Output += " " + SourceItemName + " : " + SourceContainer.CustomName + " >>> " + TargetContainerList[TargetIndex] + "\n";
  172. DebugMessage(Output);
  173.  
  174. var TestSourceItemName = GetInventoryItemName(SourceItems[SourceItemIndex]);
  175. if (TestSourceItemName != SourceItemName) break;
  176.  
  177. SourceItems.RemoveAt(SourceItemIndex);
  178. DidMove = true;
  179. if (!TargetInventory.IsFull) break;
  180. }
  181. if (!DidMove) SourceItemIndex++;
  182. }
  183. }
  184. }
  185.  
  186. string GetInventoryItemName(IMyInventoryItem item)
  187. {
  188. return item.Content.GetType().ToString().Split('_')[1] + "/" + item.Content.SubtypeName;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement