Advertisement
Guest User

Untitled

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