From ebf5477ad1bb2cb1db3b2670224030b6141992d2 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Fri, 22 Aug 2025 23:29:17 +0200 Subject: [PATCH] Use the new utilities functions and add the STOP command --- MissileController/Program.cs | 42 ++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) 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;