diff --git a/AutoPilot/Program.cs b/AutoPilot/Program.cs index 229c605..5f5eae9 100644 --- a/AutoPilot/Program.cs +++ b/AutoPilot/Program.cs @@ -2,6 +2,7 @@ * 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; @@ -48,11 +49,11 @@ namespace IngameScript readonly Output output; - readonly IMyRemoteControl remoteController; - readonly IMyCubeGrid grid; + IMyRemoteControl remoteController; + IMyCubeGrid grid; readonly List gyros = new List(); - readonly IMyBroadcastListener connnectorMinerPositionListener; + IMyBroadcastListener connnectorMinerPositionListener; MatrixD connectorMinerPosition; public Program() diff --git a/MissileController/Program.cs b/MissileController/Program.cs index c818a8a..dc0e79c 100644 --- a/MissileController/Program.cs +++ b/MissileController/Program.cs @@ -1,5 +1,4 @@ -using Sandbox.Game.Entities; -using Sandbox.Game.Entities.Cube; +using Sandbox.Game.Entities.Cube; using Sandbox.Game.EntityComponents; //using Sandbox.ModAPI; using Sandbox.ModAPI.Ingame; @@ -31,12 +30,13 @@ 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,14 +47,13 @@ namespace IngameScript State currentState = State.NORMAL; readonly Output output; - readonly IMyCubeGrid grid; int tickFromStart; IMyThrust forwardThruster; IMyFlightMovementBlock aiMove; IMyOffensiveCombatBlock aiCombat; IMySensorBlock sensor; - IEnumerable warheads; + List warheads = new List(); IMyGasTank gasTank; IMyLightingBlock light; @@ -65,7 +64,6 @@ 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"); @@ -74,7 +72,7 @@ namespace IngameScript void UpdateState10() { if (this.forwardThruster == null) - this.forwardThruster = this.GridTerminalSystem.GetBlock("[PM] Hydrogen Thruster 01", this.grid); + this.forwardThruster = this.GridTerminalSystem.GetBlockWithName("[PM] Hydrogen Thruster 01") as IMyThrust; if (this.forwardThruster == null) { this.output.Print("Error: Cannot find forward thruster"); @@ -82,7 +80,7 @@ namespace IngameScript } if (this.aiMove == null) - this.aiMove = this.GridTerminalSystem.GetBlock("[PM] AI Flight (Move)", this.grid); + this.aiMove = this.GridTerminalSystem.GetBlockWithName("[PM] AI Flight (Move)") as IMyFlightMovementBlock; if (this.aiMove == null) { this.output.Print("Error: Cannot find AI move"); @@ -90,31 +88,31 @@ namespace IngameScript } if (this.aiCombat == null) - this.aiCombat = this.GridTerminalSystem.GetBlock("[PM] AI Offensive (Combat)", this.grid); + this.aiCombat = this.GridTerminalSystem.GetBlockWithName("[PM] AI Offensive (Combat)") as IMyOffensiveCombatBlock; if (this.aiCombat == null) { this.output.Print("Error: Cannot find AI combat"); return; } - + if (this.sensor == null) - this.sensor = this.GridTerminalSystem.GetBlock("[PM] Sensor", this.grid); + this.sensor = this.GridTerminalSystem.GetBlockWithName("[PM] Sensor") as IMySensorBlock; if (this.sensor == null) { this.output.Print("Error: Cannot find sensor"); return; } - if (this.warheads == null) - this.warheads = this.GridTerminalSystem.GetBlocksFromGroup("[PM] Warheads", this.grid); - if (this.warheads.Count() == 0) + if (this.warheads.Count == 0) + this.GridTerminalSystem.GetBlockGroupWithName("[PM] Warheads").GetBlocksOfType(this.warheads); + if (this.warheads.Count == 0) { this.output.Print("Error: Cannot find any warhead"); return; } if (this.gasTank == null) - this.gasTank = this.GridTerminalSystem.GetBlock("[PM] Hydrogen Tank", this.grid); + this.gasTank = this.GridTerminalSystem.GetBlockWithName("[PM] Hydrogen Tank") as IMyGasTank; if (this.gasTank == null) { this.output.Print("Error: Cannot find gas tank"); @@ -122,18 +120,18 @@ namespace IngameScript } if (this.light == null) - this.light = this.GridTerminalSystem.GetBlock("[PM] Light", this.grid); + this.light = this.GridTerminalSystem.GetBlockWithName("[PM] Light") as IMyLightingBlock; 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) { @@ -169,7 +167,7 @@ namespace IngameScript void Detonate() { foreach (var warhead in this.warheads) - warhead.Detonate(); + warhead.Detonate(); } public void Save() @@ -185,6 +183,7 @@ namespace IngameScript { if ((updateSource & UpdateType.Update10) != 0) { + this.tickFromStart += 10; this.UpdateState10(); } else if ((updateSource & (UpdateType.Script | UpdateType.Terminal | UpdateType.Trigger)) != 0) @@ -197,11 +196,6 @@ 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 0bcea26..3c50631 100644 --- a/MissileLauncher/Program.cs +++ b/MissileLauncher/Program.cs @@ -26,10 +26,22 @@ 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; } @@ -54,29 +66,37 @@ namespace IngameScript LAUNCHING, } - readonly int number; - readonly Output output; - - readonly IMyGridTerminalSystem gridTerminal; - readonly IMyShipConnector connector; + int number; + IMyCubeGrid grid; + Output output; 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, Output output, IMyShipConnector connector) + public Launcher(int number, IMyGridTerminalSystem gridTerminal, IMyCubeGrid grid, 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() @@ -87,7 +107,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() @@ -103,13 +123,9 @@ 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); + IMyGasTank tank = this.gridTerminal.GetBlock("[PM] Hydrogen Tank", missileGrid); //Utils.GetBlock( this.gridTerminal.GetBlockWithName("[PM] Hydrogen Tank") as IMyGasTank; + this.output.Print($"tank: {tank}"); if (tank == null) { this.Print("Cannot find the missile hydrogen tank"); @@ -132,7 +148,6 @@ namespace IngameScript } tank.Stockpile = true; - connector.Connect(); this.missile = new Missile(tank, connector, mergeBlock, programmableBlock); @@ -140,7 +155,7 @@ namespace IngameScript break; case State.FILLING_TANK: - this.Print($"Waiting missile tank filled... ({this.missile.Tank.FilledRatio * 100:.0}%)"); + this.Print("Waiting missile tank filled..."); if (this.missile.Tank.FilledRatio >= Program.HYDRO_TANK_FILLED_PERCENT / 100) { @@ -160,7 +175,6 @@ namespace IngameScript this.missile.MergeBlock.Enabled = false; this.connector.Disconnect(); this.currentState = State.NOMINAL; - this.missile = null; break; @@ -184,43 +198,65 @@ 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; - readonly List launchers = new List(); + IMyCubeGrid grid; + 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.launchers.Add(new Launcher(n, this.GridTerminalSystem, this.output, connector)); + 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.Runtime.UpdateFrequency = UpdateFrequency.Update100; + this.Runtime.UpdateFrequency = UpdateFrequency.Update100; - this.output.Print($"Missile launcher system started ({this.launchers.Count} launcher(s))"); + this.output.Print("Missile launcher system started"); } void LaunchNext() @@ -229,11 +265,10 @@ namespace IngameScript { this.launchers[this.nextToLaunch].Launch(); this.nextToLaunch = (this.nextToLaunch + 1) % this.launchers.Count; - this.nbLaunched += 1; } } - void ResetToNominal() + void Reset() { this.nbLaunched = 0; this.currentState = State.NOMINAL; @@ -245,13 +280,13 @@ namespace IngameScript { case State.LAUNCHING_ONE: this.LaunchNext(); - this.ResetToNominal(); + this.Reset(); break; case State.LAUNCHING_ALL: if (this.nbLaunched >= launchers.Count) { - this.ResetToNominal(); + this.Reset(); } else { @@ -270,6 +305,7 @@ namespace IngameScript public void Save() { + } public void Main(string argument, UpdateType updateSource) @@ -278,7 +314,7 @@ namespace IngameScript { this.UpdateState(); } - else if ((updateSource & (UpdateType.Terminal | UpdateType.Trigger)) != 0) + else if ((updateSource & (UpdateType.Terminal | UpdateType.Trigger)) != 0) { switch (argument) { @@ -299,7 +335,7 @@ namespace IngameScript case "STOP": this.output.Print("Stopping lauching..."); - this.ResetToNominal(); + this.currentState = State.NOMINAL; break; default: