Use the new utilities functions and add the STOP command

This commit is contained in:
Greg Burri 2025-08-22 23:29:17 +02:00
parent 0aa3ae21d2
commit ebf5477ad1

View file

@ -1,4 +1,5 @@
using Sandbox.Game.Entities.Cube;
using Sandbox.Game.Entities;
using Sandbox.Game.Entities.Cube;
using Sandbox.Game.EntityComponents;
//using Sandbox.ModAPI;
using Sandbox.ModAPI.Ingame;
@ -30,13 +31,12 @@ namespace IngameScript
{
partial class Program : MyGridProgram
{
const string MISSILE_GRID_PREFIX = "[PM]";
//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.
enum State
{
NORMAL,
@ -47,13 +47,14 @@ namespace IngameScript
State currentState = State.NORMAL;
readonly Output output;
readonly IMyCubeGrid grid;
int tickFromStart;
IMyThrust forwardThruster;
IMyFlightMovementBlock aiMove;
IMyOffensiveCombatBlock aiCombat;
IMySensorBlock sensor;
List<IMyWarhead> warheads = new List<IMyWarhead>();
IEnumerable<IMyWarhead> warheads;
IMyGasTank gasTank;
IMyLightingBlock light;
@ -64,6 +65,7 @@ namespace IngameScript
this.output.Print("Missile controller system starting...");
this.grid = this.Me.CubeGrid;
this.Runtime.UpdateFrequency = UpdateFrequency.Update10;
this.output.Print("Missile controller system started");
@ -72,7 +74,7 @@ namespace IngameScript
void UpdateState10()
{
if (this.forwardThruster == null)
this.forwardThruster = this.GridTerminalSystem.GetBlockWithName("[PM] Hydrogen Thruster 01") as IMyThrust;
this.forwardThruster = this.GridTerminalSystem.GetBlock<IMyThrust>("[PM] Hydrogen Thruster 01", this.grid);
if (this.forwardThruster == null)
{
this.output.Print("Error: Cannot find forward thruster");
@ -80,7 +82,7 @@ namespace IngameScript
}
if (this.aiMove == null)
this.aiMove = this.GridTerminalSystem.GetBlockWithName("[PM] AI Flight (Move)") as IMyFlightMovementBlock;
this.aiMove = this.GridTerminalSystem.GetBlock<IMyFlightMovementBlock>("[PM] AI Flight (Move)", this.grid);
if (this.aiMove == null)
{
this.output.Print("Error: Cannot find AI move");
@ -88,31 +90,31 @@ namespace IngameScript
}
if (this.aiCombat == null)
this.aiCombat = this.GridTerminalSystem.GetBlockWithName("[PM] AI Offensive (Combat)") as IMyOffensiveCombatBlock;
this.aiCombat = this.GridTerminalSystem.GetBlock<IMyOffensiveCombatBlock>("[PM] AI Offensive (Combat)", this.grid);
if (this.aiCombat == null)
{
this.output.Print("Error: Cannot find AI combat");
return;
}
if (this.sensor == null)
this.sensor = this.GridTerminalSystem.GetBlockWithName("[PM] Sensor") as IMySensorBlock;
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.Count == 0)
this.GridTerminalSystem.GetBlockGroupWithName("[PM] Warheads").GetBlocksOfType(this.warheads);
if (this.warheads.Count == 0)
if (this.warheads == null)
this.warheads = this.GridTerminalSystem.GetBlocksFromGroup<IMyWarhead>("[PM] Warheads", this.grid);
if (this.warheads.Count() == 0)
{
this.output.Print("Error: Cannot find any warhead");
return;
}
if (this.gasTank == null)
this.gasTank = this.GridTerminalSystem.GetBlockWithName("[PM] Hydrogen Tank") as IMyGasTank;
this.gasTank = this.GridTerminalSystem.GetBlock<IMyGasTank>("[PM] Hydrogen Tank", this.grid);
if (this.gasTank == null)
{
this.output.Print("Error: Cannot find gas tank");
@ -120,18 +122,18 @@ namespace IngameScript
}
if (this.light == null)
this.light = this.GridTerminalSystem.GetBlockWithName("[PM] Light") as IMyLightingBlock;
this.light = this.GridTerminalSystem.GetBlock<IMyLightingBlock>("[PM] Light", this.grid);
if (this.light == null)
{
this.output.Print("Error: Cannot find light");
return;
}
this.tickFromStart += 10;
switch (this.currentState)
{
case State.LAUNCHING:
// this.output.Print($"Tick: {this.tickFromStart}");
//this.forwardThruster.ove
this.forwardThruster.ThrustOverridePercentage = 1;
if (this.MsSinceLaunch > DELAY_BEFORE_TRAVELLING_MODE)
{
@ -167,7 +169,7 @@ namespace IngameScript
void Detonate()
{
foreach (var warhead in this.warheads)
warhead.Detonate();
warhead.Detonate();
}
public void Save()
@ -183,7 +185,6 @@ namespace IngameScript
{
if ((updateSource & UpdateType.Update10) != 0)
{
this.tickFromStart += 10;
this.UpdateState10();
}
else if ((updateSource & (UpdateType.Script | UpdateType.Terminal | UpdateType.Trigger)) != 0)
@ -196,6 +197,11 @@ namespace IngameScript
this.currentState = State.LAUNCHING;
break;
case "STOP":
this.output.Print("Stop mode");
this.currentState = State.NORMAL;
break;
default:
this.output.Print($"Uknown command: {argument}");
break;