Add missile launcher and missile controller projects
[SEScripts.git] / MissileLauncher / Program.cs
diff --git a/MissileLauncher/Program.cs b/MissileLauncher/Program.cs
new file mode 100644 (file)
index 0000000..130d36d
--- /dev/null
@@ -0,0 +1,193 @@
+using Sandbox.Game.Entities.Cube;
+using Sandbox.Game.EntityComponents;
+using Sandbox.Game.GameSystems;
+using Sandbox.ModAPI.Ingame;
+using Sandbox.ModAPI.Interfaces;
+
+using SpaceEngineers.Game.ModAPI.Ingame;
+
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Collections.Immutable;
+using System.Linq;
+using System.Text;
+
+using VRage;
+using VRage.Collections;
+using VRage.Game;
+using VRage.Game.Components;
+using VRage.Game.GUI.TextPanel;
+using VRage.Game.ModAPI.Ingame;
+using VRage.Game.ModAPI.Ingame.Utilities;
+using VRage.Game.ObjectBuilders.Definitions;
+
+using VRageMath;
+
+using static VRageRender.Utils.MyWingedEdgeMesh;
+
+namespace IngameScript
+{
+    enum State
+    {
+        NORMAL,
+        STARTING_SEQUENCE,
+        FILLING_TANK,
+        LAUNCHING,
+    }
+
+    class Missile
+    {
+        string prefix;
+        readonly string tankName = "Hydrogen Tank";
+        readonly string connectorName = "Connector";
+        readonly string mergeBlockName = "Merge Block";
+        readonly string programmableBlockName = "Programmable Block";
+
+        State currentState = State.NORMAL;
+
+        IMyGridTerminalSystem gridTerminalSystem;
+
+        IMyGasTank tank = null;
+        IMyShipConnector connector = null;
+        IMyShipMergeBlock mergeBlock = null;
+        IMyProgrammableBlock programmableBlock = null;
+
+        // bool loop = false; // Not used for the moment.
+        public Missile(string prefix, IMyGridTerminalSystem gridTerminalSystem)
+        {
+            this.prefix = prefix;
+            this.gridTerminalSystem = gridTerminalSystem;
+        }
+    }
+
+    partial class Program : MyGridProgram
+    {
+        const string GRID_PREFIX = "[PML]";
+        const string MISSILE_GRID_PREFIX = "[PM]";
+        const double HYDRO_TANK_FILLED_PERCENT = 20;
+
+        const float EPSILON = 0.05f;
+
+        readonly Output output;
+
+        public Program()
+        {
+            var output = this.Me.GetSurface(0);
+            this.output = new Output(output);
+            this.output.Print("Missile launcher system starting...");
+
+            this.Runtime.UpdateFrequency = UpdateFrequency.Update100;
+
+            this.output.Print("Missile launcher system started");
+        }
+
+        void UpdateState()
+        {
+            switch (this.currentState)
+            {
+                case State.STARTING_SEQUENCE:
+                    this.tank = this.GridTerminalSystem.GetBlockWithName("[PM] Hydrogen Tank") as IMyGasTank;
+                    if (this.tank == null)
+                    {
+                        this.output.Print("Cannot find the missile hydrogen tank");
+                        break;
+                    }
+
+                    this.connector = this.GridTerminalSystem.GetBlockWithName("[PM] Connector") as IMyShipConnector;
+                    if (this.connector == null)
+                    {
+                        this.output.Print("Cannot find the missile connector");
+                        break;
+                    }
+
+                    this.mergeBlock = this.GridTerminalSystem.GetBlockWithName("[PM] Merge Block") as IMyShipMergeBlock;
+                    if (this.mergeBlock == null)
+                    {
+                        this.output.Print("Cannot find the missile merge block");
+                        break;
+                    }
+
+                    this.programmableBlock = this.GridTerminalSystem.GetBlockWithName("[PM] Programmable Block") as IMyProgrammableBlock;
+                    if (this.programmableBlock == null)
+                    {
+                        this.output.Print("Cannot find the missile programmable block");
+                        break;
+                    }
+
+                    this.tank.Stockpile = true;
+                    this.connector.Connect();
+
+                    this.currentState = State.FILLING_TANK;
+                    break; ;
+
+                case State.FILLING_TANK:
+                    this.output.Print("Waiting missile tank filled...");
+                    if (this.tank.FilledRatio >= HYDRO_TANK_FILLED_PERCENT / 100)
+                    {
+                        this.tank.Stockpile = false;
+                        this.currentState = State.LAUNCHING;
+                    }
+                    break;
+
+                case State.LAUNCHING:
+                    this.output.Print("Launching missile...");
+
+                    if (this.programmableBlock.TryRun("START"))
+                        this.output.Print("Missile launched!");
+                    else
+                        this.output.Print("ERROR: Can't send START command to missile");
+
+                    this.mergeBlock.Enabled = false;
+                    this.connector.Disconnect();
+
+                    if (this.loop)
+                        this.currentState = State.STARTING_SEQUENCE;
+                    else
+                        this.currentState = State.NORMAL;
+
+                    break;
+
+                case State.NORMAL:
+                    break; // Nothing;
+            }
+
+        }
+
+        public void Save()
+        {
+        }
+
+        public void Main(string argument, UpdateType updateSource)
+        {
+            if ((updateSource & UpdateType.Update100) != 0)
+            {
+                this.UpdateState();
+            }
+            else if ((updateSource & (UpdateType.Terminal | UpdateType.Trigger)) != 0)
+            {
+                switch (argument)
+                {
+                    case "LAUNCH ONE":
+                        this.loop = false;
+                        this.currentState = State.STARTING_SEQUENCE;
+                        break;
+
+                    case "LAUNCH SOME":
+                        this.loop = true;
+                        this.currentState = State.STARTING_SEQUENCE;
+                        break;
+
+                    case "STOP":
+                        this.loop = false;
+                        this.currentState = State.NORMAL;
+                        break;
+
+                    default:
+                        this.output.Print($"Uknown command: {argument}");
+                        break;
+                }
+            }
+        }
+    }
+}