From 0cbd5370f9b30f69753d8b0b1373cc515dd7682c Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 30 Aug 2025 23:30:07 +0200 Subject: [PATCH 1/2] Enhance missile control logic in Program.cs Updated constants for delay and auto-destruction times. Added checks for required components in FindElements method with improved error handling. Introduced new methods for auto-destruction and enemy detection. Modified state update logic to ensure correct missile behavior based on component availability and current state. --- MissileController/Program.cs | 94 +++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 27 deletions(-) diff --git a/MissileController/Program.cs b/MissileController/Program.cs index c818a8a..9a2ce30 100644 --- a/MissileController/Program.cs +++ b/MissileController/Program.cs @@ -1,4 +1,6 @@ -using Sandbox.Game.Entities; +using EmptyKeys.UserInterface.Generated.DataTemplatesContracts_Bindings; + +using Sandbox.Game.Entities; using Sandbox.Game.Entities.Cube; using Sandbox.Game.EntityComponents; //using Sandbox.ModAPI; @@ -34,8 +36,8 @@ namespace IngameScript //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. + const double DELAY_BEFORE_TRAVELLING_MODE = 5000; // [ms] (5 s). + const double AUTO_DESTRUCTION_AFTER = 120000; // [ms] (2 min). Or if the hydrogen tank is empty. enum State { @@ -50,10 +52,10 @@ namespace IngameScript readonly IMyCubeGrid grid; int tickFromStart; - IMyThrust forwardThruster; + IMyThrust forwardThruster; + IEnumerable thrusters; IMyFlightMovementBlock aiMove; IMyOffensiveCombatBlock aiCombat; - IMySensorBlock sensor; IEnumerable warheads; IMyGasTank gasTank; IMyLightingBlock light; @@ -71,14 +73,24 @@ namespace IngameScript this.output.Print("Missile controller system started"); } - void UpdateState10() + bool FindElements() { if (this.forwardThruster == null) this.forwardThruster = this.GridTerminalSystem.GetBlock("[PM] Hydrogen Thruster 01", this.grid); if (this.forwardThruster == null) { this.output.Print("Error: Cannot find forward thruster"); - return; + return false; + } + + if (this.thrusters == null) + { + this.thrusters = this.GridTerminalSystem.GetBlocksFromGroup("[PM] Thrusters", this.grid); + if (this.thrusters.Count() == 0) + { + this.output.Print("Error: Cannot find thrusters"); + return false; + } } if (this.aiMove == null) @@ -86,7 +98,7 @@ namespace IngameScript if (this.aiMove == null) { this.output.Print("Error: Cannot find AI move"); - return; + return false; } if (this.aiCombat == null) @@ -94,15 +106,7 @@ namespace IngameScript 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); - if (this.sensor == null) - { - this.output.Print("Error: Cannot find sensor"); - return; + return false; } if (this.warheads == null) @@ -110,15 +114,20 @@ namespace IngameScript if (this.warheads.Count() == 0) { this.output.Print("Error: Cannot find any warhead"); - return; + return false; } + // Debug. + //else + //{ + // this.output.Print(String.Format("Number of warhead: {0}", this.warheads.Count())); + //} if (this.gasTank == null) this.gasTank = this.GridTerminalSystem.GetBlock("[PM] Hydrogen Tank", this.grid); if (this.gasTank == null) { this.output.Print("Error: Cannot find gas tank"); - return; + return false; } if (this.light == null) @@ -126,20 +135,53 @@ namespace IngameScript if (this.light == null) { this.output.Print("Error: Cannot find light"); - return; + return false; } + return true; + } + + bool BlinkBeforeBeingAutodestructed() + { + return this.MsSinceLaunch > AUTO_DESTRUCTION_AFTER - 3000; + } + + bool MustBeAutodestructed() + { + return this.MsSinceLaunch > AUTO_DESTRUCTION_AFTER; + } + + bool EnemyAtRange() + { + return this.aiCombat.SearchEnemyComponent.FoundEnemyId.HasValue; + } + + void UpdateState10() + { this.tickFromStart += 10; switch (this.currentState) { case State.LAUNCHING: - this.forwardThruster.ThrustOverridePercentage = 1; - if (this.MsSinceLaunch > DELAY_BEFORE_TRAVELLING_MODE) + if (!this.FindElements()) { + this.output.Print("Cannot find all missile elements, launching aborted"); + this.currentState = State.NORMAL; + break; + } + + this.forwardThruster.Enabled = true; // Only one thruster is enabled when launching. + this.forwardThruster.ThrustOverridePercentage = 1; + if (this.MsSinceLaunch > DELAY_BEFORE_TRAVELLING_MODE && (this.EnemyAtRange() || this.BlinkBeforeBeingAutodestructed())) + { + foreach (var thruster in this.thrusters) + { + if (thruster != this.forwardThruster) + thruster.Enabled = true; + } + this.forwardThruster.ThrustOverridePercentage = 0; this.aiMove.Enabled = true; - this.aiCombat.Enabled = true; foreach (var warhead in this.warheads) warhead.IsArmed = true; @@ -150,12 +192,10 @@ namespace IngameScript break; case State.TRAVELLING: - var detectedEntity = this.sensor.LastDetectedEntity; - - if (this.MsSinceLaunch > AUTO_DESTRUCTION_AFTER - 3000) + if (this.BlinkBeforeBeingAutodestructed()) this.light.BlinkIntervalSeconds = 0.5f; - if (this.gasTank.FilledRatio <= EPSILON || detectedEntity.Type != MyDetectedEntityType.None || this.MsSinceLaunch > AUTO_DESTRUCTION_AFTER) + if (this.gasTank.FilledRatio <= EPSILON || this.MustBeAutodestructed()) { Detonate(); } From 736aec97db4fbc56d1f5ceb9c382c8b3cab1b32a Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 30 Aug 2025 23:31:22 +0200 Subject: [PATCH 2/2] Add missile thruster checks and update configurations - Implemented validation for missile thrusters, halting if none are found. - Increased `HYDRO_TANK_FILLED_PERCENT` from 20 to 40. - Changed runtime update frequency from `Update100` to `Update10`. --- MissileLauncher/Program.cs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/MissileLauncher/Program.cs b/MissileLauncher/Program.cs index 1116d01..23ecdbe 100644 --- a/MissileLauncher/Program.cs +++ b/MissileLauncher/Program.cs @@ -124,13 +124,23 @@ namespace IngameScript } IMyProgrammableBlock programmableBlock = this.gridTerminal.GetBlock("[PM] Programmable Block", missileGrid); - if (programmableBlock == null) { this.Print("Cannot find the missile programmable block"); break; } + IEnumerable thrusters = this.gridTerminal.GetBlocksFromGroup("[PM] Thrusters", missileGrid); + if (thrusters.Count() == 0) + { + this.Print("Cannot find missile thrusters"); + break; + } + + // When launched the missile will enabled itself its thrusters. + foreach (var thruster in thrusters) + thruster.Enabled = false; + tank.Stockpile = true; connector.Connect(); @@ -182,7 +192,7 @@ namespace IngameScript public const string GRID_PREFIX = "[PML]"; public const string MISSILE_GRID_PREFIX = "[PM]"; - public const double HYDRO_TANK_FILLED_PERCENT = 20; + public const double HYDRO_TANK_FILLED_PERCENT = 40; const string LAUNCHER_SMALL_CONNECTOR_NAME = "Connector Launcher"; // Following by a number: "01", "02", etc. const string DOORS_MISSILES_GROUP = "Doors Missiles"; @@ -202,7 +212,7 @@ namespace IngameScript public Program() { var output = this.Me.GetSurface(0); - this.output = new Output(output); + this.output = new Output(output, 14); this.output.Print("Missile launcher system starting..."); this.missilesDoors = Utils.GetBlocksFromGroup(this.GridTerminalSystem, String.Format("{0} {1}", GRID_PREFIX, DOORS_MISSILES_GROUP)); @@ -227,7 +237,7 @@ namespace IngameScript this.launchers.Add(new Launcher(n, this.GridTerminalSystem, this.output, connector)); } - this.Runtime.UpdateFrequency = UpdateFrequency.Update100; + this.Runtime.UpdateFrequency = UpdateFrequency.Update10; this.output.Print($"Missile launcher system started ({this.launchers.Count} launcher(s))"); } @@ -293,7 +303,7 @@ namespace IngameScript public void Main(string argument, UpdateType updateSource) { - if ((updateSource & UpdateType.Update100) != 0) + if ((updateSource & UpdateType.Update10) != 0) { this.UpdateState(); }