diff --git a/MissileLauncher/Program.cs b/MissileLauncher/Program.cs index 3c50631..0bcea26 100644 --- a/MissileLauncher/Program.cs +++ b/MissileLauncher/Program.cs @@ -26,22 +26,10 @@ using VRage.GameServices; using VRageMath; -// using static VRageRender.Utils.MyWingedEdgeMesh; - namespace IngameScript { 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.NOMINAL; - - //IMyGridTerminalSystem gridTerminalSystem; - public IMyGasTank Tank { get; } public IMyShipConnector Connector { get; } public IMyShipMergeBlock MergeBlock { get; } @@ -66,37 +54,29 @@ namespace IngameScript LAUNCHING, } - int number; - IMyCubeGrid grid; - Output output; + readonly int number; + readonly Output output; + + readonly IMyGridTerminalSystem gridTerminal; + readonly IMyShipConnector connector; Missile missile; - State currentState = State.NOMINAL; - IMyGridTerminalSystem gridTerminal; - IMyShipConnector connector; - - //IReadOnlyList welders; - /// - /// + /// /// /// /// /// /// To output some text /// Connector of the launcher (not the missile) - public Launcher(int number, IMyGridTerminalSystem gridTerminal, IMyCubeGrid grid, Output output, IMyShipConnector connector) + public Launcher(int number, IMyGridTerminalSystem gridTerminal, Output output, IMyShipConnector connector) { this.number = number; - this.grid = grid; this.gridTerminal = gridTerminal; this.output = output; this.connector = connector; - - //this.connector = this.gridTerminal.GetBlockWithName(String.Format("{0} {1}", Program.GRID_PREFIX, String.Format(LAUNCHER_SMALL_CONNECTOR_NAME, number))) as IMyShipConnector; - //output.Print($"{this.connector}"); } public void Launch() @@ -107,7 +87,7 @@ namespace IngameScript void Print(string message) { - this.output.Print(String.Format("{0:00} {1}", this.number, message)); + this.output.Print(String.Format("{0:00}: {1}", this.number, message)); } public void UpdateState() @@ -123,9 +103,13 @@ namespace IngameScript } var missileGrid = missileConnector.CubeGrid; + if (missileGrid == null) + { + this.Print("Cannot find the missile grid"); + break; + } - IMyGasTank tank = this.gridTerminal.GetBlock("[PM] Hydrogen Tank", missileGrid); //Utils.GetBlock( this.gridTerminal.GetBlockWithName("[PM] Hydrogen Tank") as IMyGasTank; - this.output.Print($"tank: {tank}"); + IMyGasTank tank = this.gridTerminal.GetBlock("[PM] Hydrogen Tank", missileGrid); if (tank == null) { this.Print("Cannot find the missile hydrogen tank"); @@ -148,6 +132,7 @@ namespace IngameScript } tank.Stockpile = true; + connector.Connect(); this.missile = new Missile(tank, connector, mergeBlock, programmableBlock); @@ -155,7 +140,7 @@ namespace IngameScript break; case State.FILLING_TANK: - this.Print("Waiting missile tank filled..."); + this.Print($"Waiting missile tank filled... ({this.missile.Tank.FilledRatio * 100:.0}%)"); if (this.missile.Tank.FilledRatio >= Program.HYDRO_TANK_FILLED_PERCENT / 100) { @@ -175,6 +160,7 @@ namespace IngameScript this.missile.MergeBlock.Enabled = false; this.connector.Disconnect(); this.currentState = State.NOMINAL; + this.missile = null; break; @@ -198,65 +184,43 @@ namespace IngameScript public const string MISSILE_GRID_PREFIX = "[PM]"; public const double HYDRO_TANK_FILLED_PERCENT = 20; - const string LAUNCHER_GRID_NAME = "Launcher"; // Following by a number: "01", "02", etc. const string LAUNCHER_SMALL_CONNECTOR_NAME = "Connector Launcher"; // Following by a number: "01", "02", etc. - //const string WELDER_NAME_GROUP = "Welder {0:00}"; // Example for group 1: ["Welder 01A", "Welder 01B", "Welder 01C"]. const float EPSILON = 0.05f; readonly Output output; - IMyCubeGrid grid; + readonly List launchers = new List(); - List launchers = new List(); int nbLaunched = 0; int nextToLaunch = 0; State currentState = State.NOMINAL; - //bool continuous_launching = false; - public Program() { var output = this.Me.GetSurface(0); this.output = new Output(output); this.output.Print("Missile launcher system starting..."); - this.grid = this.Me.CubeGrid; - var connectorNamePrefix = String.Format("{0} {1} ", GRID_PREFIX, LAUNCHER_SMALL_CONNECTOR_NAME); // Find all launcher sub-grid and create the associated launcher. var launcherConnectors = new List(); this.GridTerminalSystem.GetBlocksOfType( - launcherConnectors, + launcherConnectors, (IMyShipConnector connector) => connector.CustomName.StartsWith(connectorNamePrefix) ); - this.output.Print($"launcherConnectors.Count = {launcherConnectors.Count}"); - foreach (var connector in launcherConnectors) - //for (int i = 0; i < launcherConnectors.Count; i++) { var n = int.Parse(connector.CustomName.Substring(connectorNamePrefix.Length)); - this.output.Print($"n = {n}"); - - //var n = i + 1; - //var connector = launcherConnectors[i]; - - // Find associated welders. - //var welders = new List(); - //this.GridTerminalSystem.GetBlocksOfType( - // welders, - // (IMyShipWelder welder) => welder.CustomName.StartsWith(String.Format("{0} {1}", GRID_PREFIX, String.Format(WELDER_NAME_GROUP, n))) - //); - - this.launchers.Add(new Launcher(n, this.GridTerminalSystem, connector.CubeGrid, this.output, connector)); + this.launchers.Add(new Launcher(n, this.GridTerminalSystem, this.output, connector)); } - this.Runtime.UpdateFrequency = UpdateFrequency.Update100; + this.Runtime.UpdateFrequency = UpdateFrequency.Update100; - this.output.Print("Missile launcher system started"); + this.output.Print($"Missile launcher system started ({this.launchers.Count} launcher(s))"); } void LaunchNext() @@ -265,10 +229,11 @@ namespace IngameScript { this.launchers[this.nextToLaunch].Launch(); this.nextToLaunch = (this.nextToLaunch + 1) % this.launchers.Count; + this.nbLaunched += 1; } } - void Reset() + void ResetToNominal() { this.nbLaunched = 0; this.currentState = State.NOMINAL; @@ -280,13 +245,13 @@ namespace IngameScript { case State.LAUNCHING_ONE: this.LaunchNext(); - this.Reset(); + this.ResetToNominal(); break; case State.LAUNCHING_ALL: if (this.nbLaunched >= launchers.Count) { - this.Reset(); + this.ResetToNominal(); } else { @@ -305,7 +270,6 @@ namespace IngameScript public void Save() { - } public void Main(string argument, UpdateType updateSource) @@ -314,7 +278,7 @@ namespace IngameScript { this.UpdateState(); } - else if ((updateSource & (UpdateType.Terminal | UpdateType.Trigger)) != 0) + else if ((updateSource & (UpdateType.Terminal | UpdateType.Trigger)) != 0) { switch (argument) { @@ -335,7 +299,7 @@ namespace IngameScript case "STOP": this.output.Print("Stopping lauching..."); - this.currentState = State.NOMINAL; + this.ResetToNominal(); break; default: