diff --git a/MissileController/Program.cs b/MissileController/Program.cs index 9a2ce30..c818a8a 100644 --- a/MissileController/Program.cs +++ b/MissileController/Program.cs @@ -1,6 +1,4 @@ -using EmptyKeys.UserInterface.Generated.DataTemplatesContracts_Bindings; - -using Sandbox.Game.Entities; +using Sandbox.Game.Entities; using Sandbox.Game.Entities.Cube; using Sandbox.Game.EntityComponents; //using Sandbox.ModAPI; @@ -36,8 +34,8 @@ namespace IngameScript //const string MISSILE_GRID_PREFIX = "[PM]"; const float EPSILON = 0.05f; - 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. + 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 { @@ -52,10 +50,10 @@ namespace IngameScript readonly IMyCubeGrid grid; int tickFromStart; - IMyThrust forwardThruster; - IEnumerable thrusters; + IMyThrust forwardThruster; IMyFlightMovementBlock aiMove; IMyOffensiveCombatBlock aiCombat; + IMySensorBlock sensor; IEnumerable warheads; IMyGasTank gasTank; IMyLightingBlock light; @@ -73,24 +71,14 @@ namespace IngameScript this.output.Print("Missile controller system started"); } - bool FindElements() + void UpdateState10() { 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 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; - } + return; } if (this.aiMove == null) @@ -98,7 +86,7 @@ namespace IngameScript if (this.aiMove == null) { this.output.Print("Error: Cannot find AI move"); - return false; + return; } if (this.aiCombat == null) @@ -106,7 +94,15 @@ namespace IngameScript if (this.aiCombat == null) { this.output.Print("Error: Cannot find AI combat"); - return false; + 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; } if (this.warheads == null) @@ -114,20 +110,15 @@ namespace IngameScript if (this.warheads.Count() == 0) { this.output.Print("Error: Cannot find any warhead"); - return false; + return; } - // 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 false; + return; } if (this.light == null) @@ -135,53 +126,20 @@ namespace IngameScript if (this.light == null) { this.output.Print("Error: Cannot find light"); - return false; + return; } - 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: - 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())) + if (this.MsSinceLaunch > DELAY_BEFORE_TRAVELLING_MODE) { - 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; @@ -192,10 +150,12 @@ namespace IngameScript break; case State.TRAVELLING: - if (this.BlinkBeforeBeingAutodestructed()) + var detectedEntity = this.sensor.LastDetectedEntity; + + if (this.MsSinceLaunch > AUTO_DESTRUCTION_AFTER - 3000) this.light.BlinkIntervalSeconds = 0.5f; - if (this.gasTank.FilledRatio <= EPSILON || this.MustBeAutodestructed()) + if (this.gasTank.FilledRatio <= EPSILON || detectedEntity.Type != MyDetectedEntityType.None || this.MsSinceLaunch > AUTO_DESTRUCTION_AFTER) { Detonate(); } diff --git a/MissileLauncher/Program.cs b/MissileLauncher/Program.cs index 23ecdbe..1116d01 100644 --- a/MissileLauncher/Program.cs +++ b/MissileLauncher/Program.cs @@ -124,23 +124,13 @@ 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(); @@ -192,7 +182,7 @@ namespace IngameScript public const string GRID_PREFIX = "[PML]"; public const string MISSILE_GRID_PREFIX = "[PM]"; - public const double HYDRO_TANK_FILLED_PERCENT = 40; + public const double HYDRO_TANK_FILLED_PERCENT = 20; const string LAUNCHER_SMALL_CONNECTOR_NAME = "Connector Launcher"; // Following by a number: "01", "02", etc. const string DOORS_MISSILES_GROUP = "Doors Missiles"; @@ -212,7 +202,7 @@ namespace IngameScript public Program() { var output = this.Me.GetSurface(0); - this.output = new Output(output, 14); + this.output = new Output(output); this.output.Print("Missile launcher system starting..."); this.missilesDoors = Utils.GetBlocksFromGroup(this.GridTerminalSystem, String.Format("{0} {1}", GRID_PREFIX, DOORS_MISSILES_GROUP)); @@ -237,7 +227,7 @@ namespace IngameScript this.launchers.Add(new Launcher(n, this.GridTerminalSystem, this.output, connector)); } - this.Runtime.UpdateFrequency = UpdateFrequency.Update10; + this.Runtime.UpdateFrequency = UpdateFrequency.Update100; this.output.Print($"Missile launcher system started ({this.launchers.Count} launcher(s))"); } @@ -303,7 +293,7 @@ namespace IngameScript public void Main(string argument, UpdateType updateSource) { - if ((updateSource & UpdateType.Update10) != 0) + if ((updateSource & UpdateType.Update100) != 0) { this.UpdateState(); }