diff --git a/AutoPilot/Program.cs b/AutoPilot/Program.cs index 5f5eae9..229c605 100644 --- a/AutoPilot/Program.cs +++ b/AutoPilot/Program.cs @@ -2,7 +2,6 @@ * API index: https://github.com/malware-dev/MDK-SE/wiki/Api-Index * Vector transformation: https://github.com/malware-dev/MDK-SE/wiki/Vector-Transformations-with-World-Matrices * How to get rotation/position: https://forum.keenswh.com/threads/how-do-i-get-the-world-position-and-rotation-of-a-ship.7363867/ - * */ using Sandbox.Game.EntityComponents; @@ -49,11 +48,11 @@ namespace IngameScript readonly Output output; - IMyRemoteControl remoteController; - IMyCubeGrid grid; + readonly IMyRemoteControl remoteController; + readonly IMyCubeGrid grid; readonly List gyros = new List(); - IMyBroadcastListener connnectorMinerPositionListener; + readonly IMyBroadcastListener connnectorMinerPositionListener; MatrixD connectorMinerPosition; public Program() diff --git a/MissileController/Program.cs b/MissileController/Program.cs index dc0e79c..c818a8a 100644 --- a/MissileController/Program.cs +++ b/MissileController/Program.cs @@ -1,4 +1,5 @@ -using Sandbox.Game.Entities.Cube; +using Sandbox.Game.Entities; +using Sandbox.Game.Entities.Cube; using Sandbox.Game.EntityComponents; //using Sandbox.ModAPI; using Sandbox.ModAPI.Ingame; @@ -30,13 +31,12 @@ namespace IngameScript { partial class Program : MyGridProgram { - const string MISSILE_GRID_PREFIX = "[PM]"; + //const string MISSILE_GRID_PREFIX = "[PM]"; const float EPSILON = 0.05f; const double DELAY_BEFORE_TRAVELLING_MODE = 3000; // [ms]. const double AUTO_DESTRUCTION_AFTER = 60000; // [ms] (1 min). Or if the hydrogen tank is empty. - enum State { NORMAL, @@ -47,13 +47,14 @@ namespace IngameScript State currentState = State.NORMAL; readonly Output output; + readonly IMyCubeGrid grid; int tickFromStart; IMyThrust forwardThruster; IMyFlightMovementBlock aiMove; IMyOffensiveCombatBlock aiCombat; IMySensorBlock sensor; - List warheads = new List(); + IEnumerable warheads; IMyGasTank gasTank; IMyLightingBlock light; @@ -64,6 +65,7 @@ namespace IngameScript this.output.Print("Missile controller system starting..."); + this.grid = this.Me.CubeGrid; this.Runtime.UpdateFrequency = UpdateFrequency.Update10; this.output.Print("Missile controller system started"); @@ -72,7 +74,7 @@ namespace IngameScript void UpdateState10() { if (this.forwardThruster == null) - this.forwardThruster = this.GridTerminalSystem.GetBlockWithName("[PM] Hydrogen Thruster 01") as IMyThrust; + this.forwardThruster = this.GridTerminalSystem.GetBlock("[PM] Hydrogen Thruster 01", this.grid); if (this.forwardThruster == null) { this.output.Print("Error: Cannot find forward thruster"); @@ -80,7 +82,7 @@ namespace IngameScript } if (this.aiMove == null) - this.aiMove = this.GridTerminalSystem.GetBlockWithName("[PM] AI Flight (Move)") as IMyFlightMovementBlock; + this.aiMove = this.GridTerminalSystem.GetBlock("[PM] AI Flight (Move)", this.grid); if (this.aiMove == null) { this.output.Print("Error: Cannot find AI move"); @@ -88,31 +90,31 @@ namespace IngameScript } if (this.aiCombat == null) - this.aiCombat = this.GridTerminalSystem.GetBlockWithName("[PM] AI Offensive (Combat)") as IMyOffensiveCombatBlock; + this.aiCombat = this.GridTerminalSystem.GetBlock("[PM] AI Offensive (Combat)", this.grid); if (this.aiCombat == null) { this.output.Print("Error: Cannot find AI combat"); return; } - + if (this.sensor == null) - this.sensor = this.GridTerminalSystem.GetBlockWithName("[PM] Sensor") as IMySensorBlock; + this.sensor = this.GridTerminalSystem.GetBlock("[PM] Sensor", this.grid); if (this.sensor == null) { this.output.Print("Error: Cannot find sensor"); return; } - if (this.warheads.Count == 0) - this.GridTerminalSystem.GetBlockGroupWithName("[PM] Warheads").GetBlocksOfType(this.warheads); - if (this.warheads.Count == 0) + if (this.warheads == null) + this.warheads = this.GridTerminalSystem.GetBlocksFromGroup("[PM] Warheads", this.grid); + if (this.warheads.Count() == 0) { this.output.Print("Error: Cannot find any warhead"); return; } if (this.gasTank == null) - this.gasTank = this.GridTerminalSystem.GetBlockWithName("[PM] Hydrogen Tank") as IMyGasTank; + this.gasTank = this.GridTerminalSystem.GetBlock("[PM] Hydrogen Tank", this.grid); if (this.gasTank == null) { this.output.Print("Error: Cannot find gas tank"); @@ -120,18 +122,18 @@ namespace IngameScript } if (this.light == null) - this.light = this.GridTerminalSystem.GetBlockWithName("[PM] Light") as IMyLightingBlock; + this.light = this.GridTerminalSystem.GetBlock("[PM] Light", this.grid); if (this.light == null) { this.output.Print("Error: Cannot find light"); return; } + this.tickFromStart += 10; + switch (this.currentState) { case State.LAUNCHING: - // this.output.Print($"Tick: {this.tickFromStart}"); - //this.forwardThruster.ove this.forwardThruster.ThrustOverridePercentage = 1; if (this.MsSinceLaunch > DELAY_BEFORE_TRAVELLING_MODE) { @@ -167,7 +169,7 @@ namespace IngameScript void Detonate() { foreach (var warhead in this.warheads) - warhead.Detonate(); + warhead.Detonate(); } public void Save() @@ -183,7 +185,6 @@ namespace IngameScript { if ((updateSource & UpdateType.Update10) != 0) { - this.tickFromStart += 10; this.UpdateState10(); } else if ((updateSource & (UpdateType.Script | UpdateType.Terminal | UpdateType.Trigger)) != 0) @@ -196,6 +197,11 @@ namespace IngameScript this.currentState = State.LAUNCHING; break; + case "STOP": + this.output.Print("Stop mode"); + this.currentState = State.NORMAL; + break; + default: this.output.Print($"Uknown command: {argument}"); break; 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: