Enhance missile behavior and state management

- Introduced new constants for missile control: `DISTANCE_BEFORE_ROTATING` and `ROTATION_SPEED`.
- Expanded `State` enum to include `TURNING` and `ACCELERATING`.
- Added variables for tracking missile position, speed, and acceleration.
- Implemented logic for initializing backward thruster and gyroscope.
- Added `CheckEnemies` method to switch to AI mode upon enemy detection.
- Introduced `Detonate` method for centralized warhead detonation logic.
- Updated `DistanceFromLaunch` property for accurate distance calculation.
This commit is contained in:
Greg Burri 2025-09-14 17:24:46 +02:00
parent 3236ccb05f
commit b1ac491186

View file

@ -33,26 +33,38 @@ namespace IngameScript
{ {
partial class Program : MyGridProgram partial class Program : MyGridProgram
{ {
//const string MISSILE_GRID_PREFIX = "[PM]";
const float EPSILON = 0.05f; const float EPSILON = 0.05f;
const double DELAY_BEFORE_TRAVELLING_MODE = 5000; // [ms] (5 s). const double DISTANCE_BEFORE_ROTATING = 15; // [m].
const double AUTO_DESTRUCTION_AFTER = 120000; // [ms] (2 min). Or if the hydrogen tank is empty. const float ROTATION_SPEED = 0.5f; // [rad/s].
enum State enum State
{ {
NORMAL, NORMAL,
LAUNCHING, LAUNCHING,
TURNING,
ACCELERATING,
TRAVELLING, TRAVELLING,
AI,
} }
State currentState = State.NORMAL; State currentState = State.NORMAL;
int tickFromStart;
int begginningOfaccelerationTick;
bool launchPositionSet = false;
Vector3D launchPosition;
Vector3D directionBeforeTurning;
Vector3D lastPosition;
double speed;
double acceleration;
readonly Output output; readonly Output output;
readonly IMyCubeGrid grid; readonly IMyCubeGrid grid;
int tickFromStart;
IMyThrust forwardThruster; IMyThrust forwardThruster;
IMyThrust backwardThruster;
IMyGyro gyroscope;
IEnumerable<IMyThrust> thrusters; IEnumerable<IMyThrust> thrusters;
IMyFlightMovementBlock aiMove; IMyFlightMovementBlock aiMove;
IMyOffensiveCombatBlock aiCombat; IMyOffensiveCombatBlock aiCombat;
@ -83,6 +95,22 @@ namespace IngameScript
return false; return false;
} }
if (this.backwardThruster == null)
this.backwardThruster = this.GridTerminalSystem.GetBlock<IMyThrust>("[PM] Hydrogen Thruster 06", this.grid);
if (this.backwardThruster == null)
{
this.output.Print("Error: Cannot find backward thruster");
return false;
}
if (this.gyroscope == null)
this.gyroscope = this.GridTerminalSystem.GetBlock<IMyGyro>("[PM] Gyroscope", this.grid);
if (this.gyroscope == null)
{
this.output.Print("Error: Cannot find gyroscope");
return false;
}
if (this.thrusters == null) if (this.thrusters == null)
{ {
this.thrusters = this.GridTerminalSystem.GetBlocksFromGroup<IMyThrust>("[PM] Thrusters", this.grid); this.thrusters = this.GridTerminalSystem.GetBlocksFromGroup<IMyThrust>("[PM] Thrusters", this.grid);
@ -141,15 +169,15 @@ namespace IngameScript
return true; return true;
} }
bool BlinkBeforeBeingAutodestructed() //bool BlinkBeforeBeingAutodestructed()
{ //{
return this.MsSinceLaunch > AUTO_DESTRUCTION_AFTER - 3000; // return this.MsSinceLaunch > AUTO_DESTRUCTION_AFTER - 3000;
} //}
bool MustBeAutodestructed() //bool MustBeAutodestructed()
{ //{
return this.MsSinceLaunch > AUTO_DESTRUCTION_AFTER; // return this.MsSinceLaunch > AUTO_DESTRUCTION_AFTER;
} //}
bool EnemyAtRange() bool EnemyAtRange()
{ {
@ -160,6 +188,20 @@ namespace IngameScript
{ {
this.tickFromStart += 10; this.tickFromStart += 10;
if (this.forwardThruster != null)
{
var currentPosition = this.forwardThruster.GetPosition();
if (this.lastPosition != new Vector3D())
{
var d = (currentPosition - this.lastPosition).Length();
var speed = d * 10;
this.acceleration = (speed - this.speed) * 10;
this.speed = speed;
//this.output.Print($"Spd: {this.speed:0.0}, Acc: {this.acceleration:0.0}");
}
this.lastPosition = currentPosition;
}
switch (this.currentState) switch (this.currentState)
{ {
case State.LAUNCHING: case State.LAUNCHING:
@ -170,35 +212,69 @@ namespace IngameScript
break; break;
} }
this.forwardThruster.Enabled = true; // Only one thruster is enabled when launching. if (!this.launchPositionSet)
this.forwardThruster.ThrustOverridePercentage = 1;
if (this.MsSinceLaunch > DELAY_BEFORE_TRAVELLING_MODE && (this.EnemyAtRange() || this.BlinkBeforeBeingAutodestructed()))
{ {
foreach (var thruster in this.thrusters) this.launchPosition = this.forwardThruster.GetPosition();
{ this.launchPositionSet = true;
if (thruster != this.forwardThruster) }
thruster.Enabled = true;
}
//this.forwardThruster.Enabled = true; // Only one thruster is enabled when launching. Replaced by the loop below to avoid issues when gravity is present.
foreach (var thruster in this.thrusters)
{
if (thruster != this.backwardThruster)
thruster.Enabled = true;
}
this.forwardThruster.ThrustOverridePercentage = 1f; // Can be lowered for low gravity but set to 1f for 1G planet gravity.
//this.output.Print($"Distance from launch: {this.DistanceFromLaunch:0.0} m");
if (this.DistanceFromLaunch > DISTANCE_BEFORE_ROTATING)
{
this.directionBeforeTurning = this.forwardThruster.WorldMatrix.Forward.Normalized();
this.gyroscope.GyroOverride = true;
this.gyroscope.Roll = ROTATION_SPEED;
this.output.Print($"Turning mode");
this.currentState = State.TURNING;
}
break;
case State.TURNING:
var dotProduct = this.forwardThruster.WorldMatrix.Forward.Normalized().Dot(this.directionBeforeTurning);
//this.output.Print($"Dot product: {dotProduct:0.000}");
if (dotProduct <= EPSILON)
{
this.gyroscope.GyroOverride = false;
this.gyroscope.Roll = 0;
this.forwardThruster.ThrustOverridePercentage = 1f;
this.output.Print($"Accelerating mode");
this.begginningOfaccelerationTick = this.tickFromStart;
this.currentState = State.ACCELERATING;
}
break;
case State.ACCELERATING:
//this.CheckTank();
this.CheckEnemies();
if (this.acceleration <= EPSILON)
{
this.forwardThruster.ThrustOverridePercentage = 0; this.forwardThruster.ThrustOverridePercentage = 0;
this.aiMove.Enabled = true;
foreach (var warhead in this.warheads)
warhead.IsArmed = true;
this.output.Print($"Travelling mode"); this.output.Print($"Travelling mode");
this.currentState = State.TRAVELLING; this.currentState = State.TRAVELLING;
} }
break; break;
case State.TRAVELLING: case State.TRAVELLING:
if (this.BlinkBeforeBeingAutodestructed()) //this.CheckTank();
this.light.BlinkIntervalSeconds = 0.5f; this.CheckEnemies();
break;
if (this.gasTank.FilledRatio <= EPSILON || this.MustBeAutodestructed()) case State.AI:
{ //this.CheckTank();
Detonate();
}
break; break;
case State.NORMAL: case State.NORMAL:
@ -206,6 +282,33 @@ namespace IngameScript
} }
} }
//void CheckTank()
//{
// if (this.gasTank.FilledRatio <= EPSILON)
// {
// this.output.Print("Tank empty, detonating");
// Detonate();
// }
//}
void CheckEnemies()
{
if (this.EnemyAtRange())
{
this.forwardThruster.ThrustOverridePercentage = 0;
foreach (var warhead in this.warheads)
warhead.IsArmed = true;
this.backwardThruster.Enabled = true;
this.aiMove.Enabled = true;
this.output.Print("Enemy in range, switching to AI mode");
this.currentState = State.AI;
}
}
void Detonate() void Detonate()
{ {
foreach (var warhead in this.warheads) foreach (var warhead in this.warheads)
@ -216,9 +319,14 @@ namespace IngameScript
{ {
} }
double MsSinceLaunch //double MsSinceLaunch
//{
// get { return (double)this.tickFromStart / 60 * 1000; }
//}
double DistanceFromLaunch
{ {
get { return (double)this.tickFromStart / 60 * 1000; } get { return (this.forwardThruster.GetPosition() - this.launchPosition).Length(); }
} }
public void Main(string argument, UpdateType updateSource) public void Main(string argument, UpdateType updateSource)