Advertisement
chojuaib

Untitled 250k

Jan 25th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.24 KB | None | 0 0
  1. untitled 250k
  2. using Sandbox.Game.EntityComponents;
  3. using Sandbox.ModAPI.Ingame;
  4. using Sandbox.ModAPI.Interfaces;
  5. using SpaceEngineers.Game.ModAPI.Ingame;
  6. using System.Collections.Generic;
  7. using System.Collections;
  8. using System.Linq;
  9. using System.Text;
  10. using System;
  11. using VRage.Collections;
  12. using VRage.Game.Components;
  13. using VRage.Game.GUI.TextPanel;
  14. using VRage.Game.ModAPI.Ingame.Utilities;
  15. using VRage.Game.ModAPI.Ingame;
  16. using VRage.Game.ObjectBuilders.Definitions;
  17. using VRage.Game;
  18. using VRage;
  19. using VRageMath;
  20.  
  21. namespace IngameScript
  22. {
  23. partial class Program : MyGridProgram
  24. {
  25. //Block Names: customizable
  26. string oVName = "Air Vent Outside";
  27. string iVNameBase = "Air Vent Garage ";
  28. string mergeBlock = "Merge Block";
  29. string hangarDoorGroup = "Hangar Door Ramp";
  30. string pistonGroup = "Pistons Ramp";
  31. string rotor1 = "_Ramp Rotor 1";
  32. string rotor2 = "_Ramp Rotor 2";
  33. int numOfVents = 8;
  34.  
  35. int li = 0;
  36. int pc = 0;
  37. int rc = 0;
  38. int rcd = 0;
  39. float cps = 0;
  40. float ra = 0;
  41.  
  42.  
  43. bool ready;
  44. bool deping;
  45. bool opening;
  46. bool closing;
  47. bool ramping;
  48. bool ramp1;
  49. bool ramp2;
  50. bool deramping;
  51. bool ramped; //checks if the ramp is open, so the Garage doesn't close with open ramp
  52.  
  53. MyIni _ini = new MyIni();
  54.  
  55. int _statusID = 0;
  56.  
  57. IMyTextSurface _drawingSurface;
  58. RectangleF _viewport;
  59.  
  60. MyCommandLine _commandLine = new MyCommandLine();
  61. Dictionary<string, Action> _commands = new Dictionary<string, Action>(StringComparer.OrdinalIgnoreCase);
  62. public Program()
  63. {
  64.  
  65.  
  66.  
  67. _drawingSurface = Me.GetSurface(0);
  68.  
  69. var customData = Me.CustomData;
  70. _commands["c"] = Cycle;
  71. _commands["s"] = Status;
  72. _commands["q"] = Quit;
  73. _commands["reset"] = Reset;
  74. _commands["debug"] = Debug;
  75.  
  76. MyIniParseResult result;
  77. if (!_ini.TryParse(customData, out result))
  78. throw new Exception(result.ToString());
  79.  
  80. _statusID = _ini.Get("status", "statusID").ToInt32();
  81.  
  82. Runtime.UpdateFrequency = UpdateFrequency.Update100;
  83.  
  84. _viewport = new RectangleF(
  85. (_drawingSurface.TextureSize - _drawingSurface.SurfaceSize) / 2f,
  86. _drawingSurface.SurfaceSize
  87. );
  88. }
  89.  
  90.  
  91. public void Save()
  92. {
  93. // Called when the program needs to save its state. Use
  94. // this method to save your state to the Storage field
  95. // or some other means.
  96. //
  97. // This method is optional and can be removed if not
  98. // needed.
  99. }
  100.  
  101. public void Main(string argument, UpdateType updateSource)
  102. {
  103.  
  104.  
  105. var frame = _drawingSurface.DrawFrame();
  106.  
  107. DrawSprites(ref frame);
  108.  
  109. frame.Dispose();
  110.  
  111. IMyAirVent firstV = GridTerminalSystem.GetBlockWithName(iVNameBase + "1") as IMyAirVent;
  112. IMyPistonBase pistonBase = GridTerminalSystem.GetBlockWithName("_GPiston1") as IMyPistonBase;
  113.  
  114. if (_commandLine.TryParse(argument))
  115. {
  116. Action commandAction;
  117.  
  118. string command = _commandLine.Argument(0);
  119.  
  120.  
  121. if (command == null)
  122. {
  123. Echo("No command specified");
  124. }
  125. else if (_commands.TryGetValue(command, out commandAction))
  126. {
  127. commandAction();
  128.  
  129. }
  130. else
  131. {
  132. Echo($"Unknown command {command}");
  133. }
  134. }
  135. else
  136. {
  137. string s;
  138. if (li == 0) { s = ""; li++; }
  139. else if (li == 1) { s = "."; li++; }
  140. else if (li == 2) { s = ".."; li++; }
  141. else
  142. {
  143. s = "...";
  144. li = 0;
  145. }
  146. Echo("idling" + s);
  147. }
  148. float fVL = firstV.GetOxygenLevel();
  149. IMyAirVent outsideVent = GridTerminalSystem.GetBlockWithName(oVName) as IMyAirVent;
  150. float oVL = outsideVent.GetOxygenLevel();
  151.  
  152. if ((firstV.GetValueBool("Depressurize") && fVL <= 0.1) || oVL >= 0.9 || (fVL - oVL) <= 0.1)
  153. {
  154. ready = true;
  155. }
  156. else ready = false;
  157.  
  158. // TODO: add full oxygen tanks
  159. if (deping && fVL - oVL <= 0.1)
  160. {
  161. Open(true, false);
  162. deping = false;
  163. }
  164.  
  165. float pisCP = pistonBase.CurrentPosition;
  166.  
  167. if (pisCP >= 5 && opening)
  168. {
  169. switch (pc)
  170. {
  171. case 0:
  172. cps = pisCP;
  173. pc++;
  174. break;
  175. case 1:
  176. if (cps == pisCP)
  177. {
  178. opening = false;
  179. ramping = true;
  180. Ramp(false, true);
  181. }
  182. pc = 0;
  183. break;
  184. }
  185. }
  186.  
  187. float rotCA = (GridTerminalSystem.GetBlockWithName("_Ramp Rotor i1") as IMyMotorAdvancedStator).Angle;
  188. if ((ramping && ramp1) || (deramping && !ramp2))
  189. {
  190. switch (rc)
  191. {
  192. case 0:
  193. ra = rotCA;
  194. rc++;
  195. break;
  196. case 1:
  197. if (ra*ra - rotCA*rotCA < 0.1 && ramping)
  198. {
  199. ramp1 = false;
  200. Ramp(true, false);
  201. }
  202. else if (ra * ra - rotCA * rotCA < 0.1 && deramping)
  203. {
  204. deramping = false;
  205. Open(false, false);
  206. }
  207. rc = 0;
  208. break;
  209. }
  210. }
  211.  
  212. float rot2CA = (GridTerminalSystem.GetBlockWithName("_Ramp Rotor i2") as IMyMotorAdvancedStator).Angle;
  213. if (deramping && ramp2)
  214. {
  215. switch (rcd)
  216. {
  217. case 0:
  218. ra = rot2CA;
  219. ra++;
  220. break;
  221. case 1:
  222. if (ra*ra - rot2CA*rot2CA <0.1)
  223. {
  224. ramp2 = false;
  225. Ramp(false, false);
  226. }
  227. rcd = 0;
  228. break;
  229. }
  230. }
  231.  
  232.  
  233. if (_statusID < 100)
  234. {
  235. if (deping) _statusID = 3;
  236. else if (opening) _statusID = 4;
  237. else if (ramping) _statusID = 5;
  238.  
  239. }
  240.  
  241. if ((GridTerminalSystem.GetBlockWithName(mergeBlock) as IMyShipMergeBlock).IsConnected) { closing = false; DePressurize(false); }
  242.  
  243. }
  244.  
  245.  
  246. // TODO
  247. public void DrawSprites(ref MySpriteDrawFrame frame)
  248. {
  249. var position = new Vector2(256, 20) + _viewport.Position;
  250.  
  251. var sprite = new MySprite()
  252. {
  253. Type = SpriteType.TEXT,
  254. Data = _statusID.ToString(),
  255. Position = position,
  256. RotationOrScale = 0.8f,
  257. Color = Color.White,
  258. Alignment = TextAlignment.LEFT,
  259. FontId = "White"
  260. };
  261.  
  262. frame.Add(sprite);
  263. }
  264.  
  265. public void Cycle()
  266. {
  267.  
  268. IMyAirVent outsideVent = GridTerminalSystem.GetBlockWithName(oVName) as IMyAirVent;
  269.  
  270. float airlevelO = outsideVent.GetOxygenLevel();
  271.  
  272. string dir = _commandLine.Argument(1);
  273. if (dir == null)
  274. {
  275. Echo("No Direction given");
  276. return;
  277. }
  278. if (string.Equals(dir, "o", StringComparison.OrdinalIgnoreCase))
  279. {
  280. string argO = _commandLine.Argument(1);
  281.  
  282. if (airlevelO < 0.9 && argO != "sos")
  283. {
  284. DePressurize(true);
  285. deping = true;
  286. }
  287. else if (argO == "sos")
  288. {
  289. Echo("Warning! Emergency Opening Procedure has been started (Code: 505)");
  290. _statusID = 505;
  291. DePressurize(true);
  292. Open(true, true);
  293. }
  294. else Open(true, false);
  295.  
  296. }
  297. else if (string.Equals(dir, "c", StringComparison.OrdinalIgnoreCase))
  298. {
  299.  
  300. if (ramped)
  301. {
  302. deramping = true;
  303. Ramp(false, true);
  304. }
  305. }
  306. else
  307. {
  308. Echo($"Direction {dir} has not been found");
  309. }
  310. }
  311.  
  312. public void Status()
  313. {
  314. string lcdString = _commandLine.Argument(1);
  315. // TODO: LCD Types
  316. //string lcdType = _commandLine.Argument(2);
  317. string lcdOutputID = _commandLine.Argument(3);
  318. int lcd;
  319. //int typeID;
  320. int outID;
  321. if (!Int32.TryParse(lcdString, out lcd))
  322. {
  323. Echo(lcdString + " is not an Integer");
  324. _statusID = 1;
  325. return;
  326. }
  327. Echo("LCD " + lcd.ToString());
  328. if (!Int32.TryParse(lcdOutputID, out outID))
  329. {
  330. Echo(lcdOutputID + " is not an Integer");
  331. return;
  332. }
  333. Echo("Output ID: " + outID.ToString());
  334.  
  335. if (outID == 0)
  336. {
  337.  
  338. }
  339.  
  340. WriteStatus(false, lcd, outID);
  341. }
  342.  
  343. // TODO: Write Status
  344. public void WriteStatus(bool self, int lcd, int output)
  345. {
  346. if (self)
  347. {
  348.  
  349. }
  350. }
  351.  
  352.  
  353. public void DePressurize(bool de)
  354. {
  355. string pressDe;
  356. if (de) pressDe = "Depressurize_On";
  357. else pressDe = "Depressurize_Off";
  358. for (int i = 0; i < numOfVents; i++)
  359. {
  360. GridTerminalSystem.GetBlockWithName(iVNameBase + i).ApplyAction(pressDe);
  361. }
  362. }
  363.  
  364. public void Open(bool o, bool em)
  365. {
  366. IMyShipMergeBlock mB = GridTerminalSystem.GetBlockWithName(mergeBlock) as IMyShipMergeBlock;
  367.  
  368.  
  369.  
  370. IMyBlockGroup doorsG = GridTerminalSystem.GetBlockGroupWithName(hangarDoorGroup) as IMyBlockGroup;
  371. List<IMyAirtightHangarDoor> doors = new List<IMyAirtightHangarDoor>();
  372. doorsG.GetBlocksOfType(doors);
  373.  
  374. IMyBlockGroup pistonsG = GridTerminalSystem.GetBlockGroupWithName(pistonGroup) as IMyBlockGroup;
  375. List<IMyPistonBase> pistons = new List<IMyPistonBase>();
  376. pistonsG.GetBlocksOfType(pistons);
  377. if (o)
  378. {
  379. if (em || ready)
  380. {
  381.  
  382. mB.ApplyAction("OnOff_Off");
  383. foreach (var block in doors)
  384. {
  385. block.ApplyAction("Open_On");
  386. }
  387. foreach (var block in pistons)
  388. {
  389. block.ApplyAction("Extend");
  390. }
  391. opening = true;
  392. }
  393. else { Echo("Opening failed: Not able to open. (This really shouldn't happen)"); _statusID = 101; }
  394. } else if (!deramping)
  395. {
  396. closing = true;
  397. foreach (var block in pistons)
  398. {
  399. block.ApplyAction("Retract");
  400. }
  401. foreach (var block in doors)
  402. {
  403. block.ApplyAction("Open_Off");
  404. }
  405. mB.ApplyAction("OnOff_On");
  406. }
  407. }
  408.  
  409.  
  410.  
  411. public void Ramp(bool open, bool pos)
  412. {
  413.  
  414. IMyMotorAdvancedStator r1 = GridTerminalSystem.GetBlockWithName(rotor1) as IMyMotorAdvancedStator;
  415. IMyMotorStator r2 = GridTerminalSystem.GetBlockWithName(rotor2) as IMyMotorStator;
  416. if (ramping && open && !closing)
  417. {
  418. if (pos) { r1.ApplyAction("Reverse"); ramp1 = true; }
  419. else if (!pos && !ramp1) {
  420. r2.ApplyAction("Reverse");
  421. ramping = false;
  422. _statusID = 2;
  423. ramped = true;
  424. return;
  425. }
  426. }
  427. if (deramping && !open)
  428. {
  429. if (pos)
  430. {
  431. ramp2 = true;
  432. r2.ApplyAction("Reverse");
  433. }
  434. else if (!ramp2 && !pos)
  435. {
  436.  
  437. r1.ApplyAction("Reverse");
  438. }
  439. }
  440.  
  441. }
  442.  
  443.  
  444. public void Quit()
  445. {
  446. if (_statusID >= 100)
  447. {
  448. Echo($"Error/Warning {_statusID} quitted!");
  449. _statusID = 0;
  450. }
  451. }
  452.  
  453. public void Reset()
  454. {
  455. li = 0;
  456. pc = 0;
  457. rc = 0;
  458. cps = 0;
  459. ra = 0;
  460.  
  461.  
  462. ready = false;
  463. deping = false;
  464. opening = false;
  465. ramping = false;
  466. ramp1 = false;
  467. }
  468.  
  469.  
  470. public void Debug()
  471. {
  472. Echo("empty");
  473.  
  474. }
  475.  
  476. }
  477. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement