Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Sandbox.Game.EntityComponents;
- using Sandbox.ModAPI.Ingame;
- using Sandbox.ModAPI.Interfaces;
- using SpaceEngineers.Game.ModAPI.Ingame;
- using System.Collections.Generic;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using System;
- using VRage.Collections;
- using VRage.Game.Components;
- using VRage.Game.GUI.TextPanel;
- using VRage.Game.ModAPI.Ingame.Utilities;
- using VRage.Game.ModAPI.Ingame;
- using VRage.Game.ObjectBuilders.Definitions;
- using VRage.Game;
- using VRage;
- using VRageMath;
- namespace IngameScript
- {
- partial class Program : MyGridProgram
- {
- //Block Names: customizable
- string oVName = "Air Vent Outside";
- string iVNameBase = "Air Vent Garage ";
- string mergeBlock = "Merge Block";
- string hangarDoorGroup = "Hangar Door Ramp";
- string pistonGroup = "Pistons Ramp";
- string rotor1 = "_Ramp Rotor 1";
- string rotor2 = "_Ramp Rotor 2";
- int numOfVents = 8;
- int li = 0;
- int pc = 0;
- int rc = 0;
- int rcd = 0;
- float cps = 0;
- float ra = 0;
- bool ready;
- bool deping;
- bool opening;
- bool closing;
- bool ramping;
- bool ramp1;
- bool ramp2;
- bool deramping;
- bool ramped; //checks if the ramp is open, so the Garage doesn't close with open ramp
- MyIni _ini = new MyIni();
- int _statusID = 0;
- IMyTextSurface _drawingSurface;
- RectangleF _viewport;
- MyCommandLine _commandLine = new MyCommandLine();
- Dictionary<string, Action> _commands = new Dictionary<string, Action>(StringComparer.OrdinalIgnoreCase);
- public Program()
- {
- _drawingSurface = Me.GetSurface(0);
- var customData = Me.CustomData;
- _commands["c"] = Cycle;
- _commands["s"] = Status;
- _commands["q"] = Quit;
- _commands["reset"] = Reset;
- _commands["debug"] = Debug;
- MyIniParseResult result;
- if (!_ini.TryParse(customData, out result))
- throw new Exception(result.ToString());
- _statusID = _ini.Get("status", "statusID").ToInt32();
- Runtime.UpdateFrequency = UpdateFrequency.Update100;
- _viewport = new RectangleF(
- (_drawingSurface.TextureSize - _drawingSurface.SurfaceSize) / 2f,
- _drawingSurface.SurfaceSize
- );
- }
- public void Save()
- {
- // Called when the program needs to save its state. Use
- // this method to save your state to the Storage field
- // or some other means.
- //
- // This method is optional and can be removed if not
- // needed.
- }
- public void Main(string argument, UpdateType updateSource)
- {
- var frame = _drawingSurface.DrawFrame();
- DrawSprites(ref frame);
- frame.Dispose();
- IMyAirVent firstV = GridTerminalSystem.GetBlockWithName(iVNameBase + "1") as IMyAirVent;
- IMyPistonBase pistonBase = GridTerminalSystem.GetBlockWithName("_GPiston1") as IMyPistonBase;
- if (_commandLine.TryParse(argument))
- {
- Action commandAction;
- string command = _commandLine.Argument(0);
- if (command == null)
- {
- Echo("No command specified");
- }
- else if (_commands.TryGetValue(command, out commandAction))
- {
- commandAction();
- }
- else
- {
- Echo($"Unknown command {command}");
- }
- }
- else
- {
- string s;
- if (li == 0) { s = ""; li++; }
- else if (li == 1) { s = "."; li++; }
- else if (li == 2) { s = ".."; li++; }
- else
- {
- s = "...";
- li = 0;
- }
- Echo("idling" + s);
- }
- float fVL = firstV.GetOxygenLevel();
- IMyAirVent outsideVent = GridTerminalSystem.GetBlockWithName(oVName) as IMyAirVent;
- float oVL = outsideVent.GetOxygenLevel();
- if ((firstV.GetValueBool("Depressurize") && fVL <= 0.1) || oVL >= 0.9 || (fVL - oVL) <= 0.1)
- {
- ready = true;
- }
- else ready = false;
- // TODO: add full oxygen tanks
- if (deping && fVL - oVL <= 0.1)
- {
- Open(true, false);
- deping = false;
- }
- float pisCP = pistonBase.CurrentPosition;
- if (pisCP >= 5 && opening)
- {
- switch (pc)
- {
- case 0:
- cps = pisCP;
- pc++;
- break;
- case 1:
- if (cps == pisCP)
- {
- opening = false;
- ramping = true;
- Ramp(false, true);
- }
- pc = 0;
- break;
- }
- }
- float rotCA = (GridTerminalSystem.GetBlockWithName("_Ramp Rotor i1") as IMyMotorAdvancedStator).Angle;
- if ((ramping && ramp1) || (deramping && !ramp2))
- {
- switch (rc)
- {
- case 0:
- ra = rotCA;
- rc++;
- break;
- case 1:
- if (ra*ra - rotCA*rotCA < 0.1 && ramping)
- {
- ramp1 = false;
- Ramp(true, false);
- }
- else if (ra * ra - rotCA * rotCA < 0.1 && deramping)
- {
- deramping = false;
- Open(false, false);
- }
- rc = 0;
- break;
- }
- }
- float rot2CA = (GridTerminalSystem.GetBlockWithName("_Ramp Rotor i2") as IMyMotorAdvancedStator).Angle;
- if (deramping && ramp2)
- {
- switch (rcd)
- {
- case 0:
- ra = rot2CA;
- ra++;
- break;
- case 1:
- if (ra*ra - rot2CA*rot2CA <0.1)
- {
- ramp2 = false;
- Ramp(false, false);
- }
- rcd = 0;
- break;
- }
- }
- if (_statusID < 100)
- {
- if (deping) _statusID = 3;
- else if (opening) _statusID = 4;
- else if (ramping) _statusID = 5;
- }
- if ((GridTerminalSystem.GetBlockWithName(mergeBlock) as IMyShipMergeBlock).IsConnected) { closing = false; DePressurize(false); }
- }
- // TODO
- public void DrawSprites(ref MySpriteDrawFrame frame)
- {
- var position = new Vector2(256, 20) + _viewport.Position;
- var sprite = new MySprite()
- {
- Type = SpriteType.TEXT,
- Data = _statusID.ToString(),
- Position = position,
- RotationOrScale = 0.8f,
- Color = Color.White,
- Alignment = TextAlignment.LEFT,
- FontId = "White"
- };
- frame.Add(sprite);
- }
- public void Cycle()
- {
- IMyAirVent outsideVent = GridTerminalSystem.GetBlockWithName(oVName) as IMyAirVent;
- float airlevelO = outsideVent.GetOxygenLevel();
- string dir = _commandLine.Argument(1);
- if (dir == null)
- {
- Echo("No Direction given");
- return;
- }
- if (string.Equals(dir, "o", StringComparison.OrdinalIgnoreCase))
- {
- string argO = _commandLine.Argument(1);
- if (airlevelO < 0.9 && argO != "sos")
- {
- DePressurize(true);
- deping = true;
- }
- else if (argO == "sos")
- {
- Echo("Warning! Emergency Opening Procedure has been started (Code: 505)");
- _statusID = 505;
- DePressurize(true);
- Open(true, true);
- }
- else Open(true, false);
- }
- else if (string.Equals(dir, "c", StringComparison.OrdinalIgnoreCase))
- {
- if (ramped)
- {
- deramping = true;
- Ramp(false, true);
- }
- }
- else
- {
- Echo($"Direction {dir} has not been found");
- }
- }
- public void Status()
- {
- string lcdString = _commandLine.Argument(1);
- // TODO: LCD Types
- //string lcdType = _commandLine.Argument(2);
- string lcdOutputID = _commandLine.Argument(3);
- int lcd;
- //int typeID;
- int outID;
- if (!Int32.TryParse(lcdString, out lcd))
- {
- Echo(lcdString + " is not an Integer");
- _statusID = 1;
- return;
- }
- Echo("LCD " + lcd.ToString());
- if (!Int32.TryParse(lcdOutputID, out outID))
- {
- Echo(lcdOutputID + " is not an Integer");
- return;
- }
- Echo("Output ID: " + outID.ToString());
- if (outID == 0)
- {
- }
- WriteStatus(false, lcd, outID);
- }
- // TODO: Write Status
- public void WriteStatus(bool self, int lcd, int output)
- {
- if (self)
- {
- }
- }
- public void DePressurize(bool de)
- {
- string pressDe;
- if (de) pressDe = "Depressurize_On";
- else pressDe = "Depressurize_Off";
- for (int i = 0; i < numOfVents; i++)
- {
- GridTerminalSystem.GetBlockWithName(iVNameBase + i).ApplyAction(pressDe);
- }
- }
- public void Open(bool o, bool em)
- {
- IMyShipMergeBlock mB = GridTerminalSystem.GetBlockWithName(mergeBlock) as IMyShipMergeBlock;
- IMyBlockGroup doorsG = GridTerminalSystem.GetBlockGroupWithName(hangarDoorGroup) as IMyBlockGroup;
- List<IMyAirtightHangarDoor> doors = new List<IMyAirtightHangarDoor>();
- doorsG.GetBlocksOfType(doors);
- IMyBlockGroup pistonsG = GridTerminalSystem.GetBlockGroupWithName(pistonGroup) as IMyBlockGroup;
- List<IMyPistonBase> pistons = new List<IMyPistonBase>();
- pistonsG.GetBlocksOfType(pistons);
- if (o)
- {
- if (em || ready)
- {
- mB.ApplyAction("OnOff_Off");
- foreach (var block in doors)
- {
- block.ApplyAction("Open_On");
- }
- foreach (var block in pistons)
- {
- block.ApplyAction("Extend");
- }
- opening = true;
- }
- else { Echo("Opening failed: Not able to open. (This really shouldn't happen)"); _statusID = 101; }
- } else if (!deramping)
- {
- closing = true;
- foreach (var block in pistons)
- {
- block.ApplyAction("Retract");
- }
- foreach (var block in doors)
- {
- block.ApplyAction("Open_Off");
- }
- mB.ApplyAction("OnOff_On");
- }
- }
- public void Ramp(bool open, bool pos)
- {
- IMyMotorAdvancedStator r1 = GridTerminalSystem.GetBlockWithName(rotor1) as IMyMotorAdvancedStator;
- IMyMotorStator r2 = GridTerminalSystem.GetBlockWithName(rotor2) as IMyMotorStator;
- if (ramping && open && !closing)
- {
- if (pos) { r1.ApplyAction("Reverse"); ramp1 = true; }
- else if (!pos && !ramp1) {
- r2.ApplyAction("Reverse");
- ramping = false;
- _statusID = 2;
- ramped = true;
- return;
- }
- }
- if (deramping && !open)
- {
- if (pos)
- {
- ramp2 = true;
- r2.ApplyAction("Reverse");
- }
- else if (!ramp2 && !pos)
- {
- r1.ApplyAction("Reverse");
- }
- }
- }
- public void Quit()
- {
- if (_statusID >= 100)
- {
- Echo($"Error/Warning {_statusID} quitted!");
- _statusID = 0;
- }
- }
- public void Reset()
- {
- li = 0;
- pc = 0;
- rc = 0;
- cps = 0;
- ra = 0;
- ready = false;
- deping = false;
- opening = false;
- ramping = false;
- ramp1 = false;
- }
- public void Debug()
- {
- Echo("empty");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement