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.
This commit is contained in:
parent
5dc10e361b
commit
0cbd5370f9
1 changed files with 67 additions and 27 deletions
|
|
@ -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.Entities.Cube;
|
||||||
using Sandbox.Game.EntityComponents;
|
using Sandbox.Game.EntityComponents;
|
||||||
//using Sandbox.ModAPI;
|
//using Sandbox.ModAPI;
|
||||||
|
|
@ -34,8 +36,8 @@ namespace IngameScript
|
||||||
//const string MISSILE_GRID_PREFIX = "[PM]";
|
//const string MISSILE_GRID_PREFIX = "[PM]";
|
||||||
|
|
||||||
const float EPSILON = 0.05f;
|
const float EPSILON = 0.05f;
|
||||||
const double DELAY_BEFORE_TRAVELLING_MODE = 3000; // [ms].
|
const double DELAY_BEFORE_TRAVELLING_MODE = 5000; // [ms] (5 s).
|
||||||
const double AUTO_DESTRUCTION_AFTER = 60000; // [ms] (1 min). Or if the hydrogen tank is empty.
|
const double AUTO_DESTRUCTION_AFTER = 120000; // [ms] (2 min). Or if the hydrogen tank is empty.
|
||||||
|
|
||||||
enum State
|
enum State
|
||||||
{
|
{
|
||||||
|
|
@ -50,10 +52,10 @@ namespace IngameScript
|
||||||
readonly IMyCubeGrid grid;
|
readonly IMyCubeGrid grid;
|
||||||
|
|
||||||
int tickFromStart;
|
int tickFromStart;
|
||||||
IMyThrust forwardThruster;
|
IMyThrust forwardThruster;
|
||||||
|
IEnumerable<IMyThrust> thrusters;
|
||||||
IMyFlightMovementBlock aiMove;
|
IMyFlightMovementBlock aiMove;
|
||||||
IMyOffensiveCombatBlock aiCombat;
|
IMyOffensiveCombatBlock aiCombat;
|
||||||
IMySensorBlock sensor;
|
|
||||||
IEnumerable<IMyWarhead> warheads;
|
IEnumerable<IMyWarhead> warheads;
|
||||||
IMyGasTank gasTank;
|
IMyGasTank gasTank;
|
||||||
IMyLightingBlock light;
|
IMyLightingBlock light;
|
||||||
|
|
@ -71,14 +73,24 @@ namespace IngameScript
|
||||||
this.output.Print("Missile controller system started");
|
this.output.Print("Missile controller system started");
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateState10()
|
bool FindElements()
|
||||||
{
|
{
|
||||||
if (this.forwardThruster == null)
|
if (this.forwardThruster == null)
|
||||||
this.forwardThruster = this.GridTerminalSystem.GetBlock<IMyThrust>("[PM] Hydrogen Thruster 01", this.grid);
|
this.forwardThruster = this.GridTerminalSystem.GetBlock<IMyThrust>("[PM] Hydrogen Thruster 01", this.grid);
|
||||||
if (this.forwardThruster == null)
|
if (this.forwardThruster == null)
|
||||||
{
|
{
|
||||||
this.output.Print("Error: Cannot find forward thruster");
|
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)
|
if (this.aiMove == null)
|
||||||
|
|
@ -86,7 +98,7 @@ namespace IngameScript
|
||||||
if (this.aiMove == null)
|
if (this.aiMove == null)
|
||||||
{
|
{
|
||||||
this.output.Print("Error: Cannot find AI move");
|
this.output.Print("Error: Cannot find AI move");
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.aiCombat == null)
|
if (this.aiCombat == null)
|
||||||
|
|
@ -94,15 +106,7 @@ namespace IngameScript
|
||||||
if (this.aiCombat == null)
|
if (this.aiCombat == null)
|
||||||
{
|
{
|
||||||
this.output.Print("Error: Cannot find AI combat");
|
this.output.Print("Error: Cannot find AI combat");
|
||||||
return;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.warheads == null)
|
if (this.warheads == null)
|
||||||
|
|
@ -110,15 +114,20 @@ namespace IngameScript
|
||||||
if (this.warheads.Count() == 0)
|
if (this.warheads.Count() == 0)
|
||||||
{
|
{
|
||||||
this.output.Print("Error: Cannot find any warhead");
|
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)
|
if (this.gasTank == null)
|
||||||
this.gasTank = this.GridTerminalSystem.GetBlock<IMyGasTank>("[PM] Hydrogen Tank", this.grid);
|
this.gasTank = this.GridTerminalSystem.GetBlock<IMyGasTank>("[PM] Hydrogen Tank", this.grid);
|
||||||
if (this.gasTank == null)
|
if (this.gasTank == null)
|
||||||
{
|
{
|
||||||
this.output.Print("Error: Cannot find gas tank");
|
this.output.Print("Error: Cannot find gas tank");
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.light == null)
|
if (this.light == null)
|
||||||
|
|
@ -126,20 +135,53 @@ namespace IngameScript
|
||||||
if (this.light == null)
|
if (this.light == null)
|
||||||
{
|
{
|
||||||
this.output.Print("Error: Cannot find light");
|
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;
|
this.tickFromStart += 10;
|
||||||
|
|
||||||
switch (this.currentState)
|
switch (this.currentState)
|
||||||
{
|
{
|
||||||
case State.LAUNCHING:
|
case State.LAUNCHING:
|
||||||
this.forwardThruster.ThrustOverridePercentage = 1;
|
if (!this.FindElements())
|
||||||
if (this.MsSinceLaunch > DELAY_BEFORE_TRAVELLING_MODE)
|
|
||||||
{
|
{
|
||||||
|
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.forwardThruster.ThrustOverridePercentage = 0;
|
||||||
this.aiMove.Enabled = true;
|
this.aiMove.Enabled = true;
|
||||||
this.aiCombat.Enabled = true;
|
|
||||||
|
|
||||||
foreach (var warhead in this.warheads)
|
foreach (var warhead in this.warheads)
|
||||||
warhead.IsArmed = true;
|
warhead.IsArmed = true;
|
||||||
|
|
@ -150,12 +192,10 @@ namespace IngameScript
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case State.TRAVELLING:
|
case State.TRAVELLING:
|
||||||
var detectedEntity = this.sensor.LastDetectedEntity;
|
if (this.BlinkBeforeBeingAutodestructed())
|
||||||
|
|
||||||
if (this.MsSinceLaunch > AUTO_DESTRUCTION_AFTER - 3000)
|
|
||||||
this.light.BlinkIntervalSeconds = 0.5f;
|
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();
|
Detonate();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue