Compare commits

...

2 commits

Author SHA1 Message Date
736aec97db 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`.
2025-08-30 23:31:22 +02:00
0cbd5370f9 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.
2025-08-30 23:30:07 +02:00
2 changed files with 82 additions and 32 deletions

View file

@ -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<IMyThrust> thrusters;
IMyFlightMovementBlock aiMove;
IMyOffensiveCombatBlock aiCombat;
IMySensorBlock sensor;
IEnumerable<IMyWarhead> 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<IMyThrust>("[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<IMyThrust>("[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<IMySensorBlock>("[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<IMyGasTank>("[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();
}

View file

@ -124,13 +124,23 @@ namespace IngameScript
}
IMyProgrammableBlock programmableBlock = this.gridTerminal.GetBlock<IMyProgrammableBlock>("[PM] Programmable Block", missileGrid);
if (programmableBlock == null)
{
this.Print("Cannot find the missile programmable block");
break;
}
IEnumerable<IMyThrust> thrusters = this.gridTerminal.GetBlocksFromGroup<IMyThrust>("[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<IMyDoor>(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();
}