From 0cbd5370f9b30f69753d8b0b1373cc515dd7682c Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 30 Aug 2025 23:30:07 +0200 Subject: [PATCH] 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(); }