Advertisement
idiotonastic

Untitled

Jan 29th, 2021
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. /*
  2. * R e a d m e
  3. * -----------
  4. *
  5. * In this file you can include any instructions or other comments you want to have injected onto the
  6. * top of your final script. You can safely delete this file if you do not want any such comments.
  7. */
  8.  
  9.  
  10.  
  11. List<IMyBatteryBlock> batteries = new List<IMyBatteryBlock>();
  12. List<IMyShipMergeBlock> Merge = new List<IMyShipMergeBlock>();
  13.  
  14. // Battery variables
  15. float storedPowerAll = 0;
  16. float storedPowerAllMax = 0;
  17.  
  18. DateTime balanceTime = DateTime.Now;
  19.  
  20. // Command line parameters
  21. string setting = "";
  22. string message = "";
  23. string action = "";
  24. DateTime actionTime;
  25.  
  26. HashSet<string> allWarnings = new HashSet<string>();
  27. HashSet<string> currentWarnings = new HashSet<string>();
  28.  
  29. // Script timing variables
  30. int savedBlockCount = 0;
  31. int execCounter = 1;
  32. bool firstRun = true;
  33. /// <summary>
  34. /// Pre-Run preparations
  35. /// </summary>
  36. public Program()
  37. {
  38. // Set UpdateFrequency for starting the programmable block over and over again
  39. Runtime.UpdateFrequency = UpdateFrequency.Update10;
  40.  
  41. }
  42.  
  43.  
  44. /// <summary>
  45. /// Main method
  46. /// </summary>
  47. void Main(string arg)
  48. {
  49.  
  50. // First run
  51. if (firstRun)
  52. {
  53. Echo("Initializing.. ");
  54. if (execCounter == 1) GetBlocks();
  55. if (execCounter >= 2) Echo("Found: " + batteries.Count + " batteries");
  56. if (execCounter >= 10) Echo("\nStarting script..");
  57.  
  58. execCounter++;
  59. if (execCounter >= 15)
  60. {
  61. execCounter = 1;
  62. firstRun = false;
  63. }
  64.  
  65. return;
  66. }
  67.  
  68. // Store the parameter
  69. if (arg != "")
  70. {
  71. action = arg;
  72. execCounter = 0;
  73. message = "";
  74. actionTime = DateTime.Now;
  75. }
  76.  
  77. // Execute commandline argument
  78. if (ExecuteArgument(action)) return;
  79.  
  80. // Get all blocks, the script should use
  81. var allBlocks = new List<IMyTerminalBlock>();
  82. GridTerminalSystem.GetBlocks(allBlocks);
  83. int blockCount = allBlocks.Count;
  84.  
  85. if (execCounter == 0 || blockCount != savedBlockCount)
  86. {
  87. GetBlocks();
  88. savedBlockCount = blockCount;
  89.  
  90. if (execCounter == 0)
  91. {
  92. execCounter++;
  93. }
  94.  
  95. return;
  96. }
  97.  
  98. if (execCounter == 1)
  99. {
  100. // Get state of doners
  101. if (IsDonerDead())
  102. {
  103. ReleaseMerge();
  104. }
  105.  
  106. }
  107.  
  108. // Update the script execution counter
  109. if (execCounter >= 2)
  110. {
  111. // Reset the counter
  112. execCounter = 0;
  113.  
  114. // Overwrite all warnings set with current warnings
  115. allWarnings = new HashSet<string>(currentWarnings);
  116. currentWarnings.Clear();
  117. ResetMerge();
  118. }
  119. else
  120. {
  121. execCounter++;
  122. }
  123. }
  124.  
  125. private void ReleaseMerge()
  126. {
  127. foreach (IMyShipMergeBlock e in Merge)
  128. {
  129. e.Enabled = false;
  130. }
  131. }
  132. private void ResetMerge()
  133. {
  134. foreach (IMyShipMergeBlock e in Merge)
  135. {
  136. e.Enabled = true;
  137. }
  138. }
  139.  
  140. private Boolean IsDonerDead()
  141. {
  142. int x = 0;
  143. int y = 0;
  144. foreach (IMyBatteryBlock e in batteries)
  145. {
  146. if (e.ChargeMode == ChargeMode.Discharge)
  147. {
  148. x++;
  149. if (e.CurrentStoredPower.Equals(0f))
  150. {
  151. y++;
  152. }
  153. }
  154. }
  155. return y == x;
  156. }
  157.  
  158. bool ExecuteArgument(string arg)
  159. {
  160. bool validArgument = true;
  161. bool status = true;
  162.  
  163. if (arg != "msg")
  164. {
  165. if (!arg.Contains(" on") && !arg.Contains(" off") && !arg.Contains(" toggle")) return false;
  166. if (arg.Contains(" off")) status = false;
  167. }
  168.  
  169. if (arg == "msg")
  170. {
  171. }
  172. else
  173. {
  174. validArgument = false;
  175. }
  176.  
  177. if (validArgument)
  178. {
  179. TimeSpan timeSinceAction = DateTime.Now - actionTime;
  180.  
  181. if (message == "") message = setting + " temporarily " + (status ? "enabled" : "disabled") + "!\n";
  182. Echo(message);
  183. Echo("Continuing in " + Math.Ceiling(3 - timeSinceAction.TotalSeconds) + " seconds..");
  184. action = "msg";
  185.  
  186. if (timeSinceAction.TotalSeconds >= 3)
  187. {
  188. setting = "";
  189. message = "";
  190. action = "";
  191. }
  192. }
  193.  
  194. return validArgument;
  195. }
  196.  
  197.  
  198.  
  199. /// Gets all blocks that should be used by the script
  200.  
  201. void GetBlocks()
  202. {
  203. List<IMyBatteryBlock> batteriesTemp = new List<IMyBatteryBlock>();
  204. GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(batteriesTemp);
  205.  
  206. List<IMyShipMergeBlock> MergeTemp = new List<IMyShipMergeBlock>();
  207. GridTerminalSystem.GetBlocksOfType<IMyShipMergeBlock>(MergeTemp);
  208. foreach (IMyShipMergeBlock e in MergeTemp)
  209. {
  210. if (!e.DisplayName.Contains("[Bat]"))
  211. {
  212. MergeTemp.Remove(e);
  213. }
  214. }
  215. Merge = MergeTemp;
  216.  
  217. }
  218.  
  219.  
  220.  
  221.  
  222. /// Save method for recompiling the script or saving the world
  223.  
  224. public void Save()
  225. {
  226. // Activate everything
  227. foreach (var battery in batteries)
  228. {
  229. battery.Enabled = true;
  230. battery.ChargeMode = ChargeMode.Auto;
  231. }
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement