From 06d6acb22391b4aa9f541f4e5c3ca5bab52098eb Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Mon, 22 Jun 2026 10:51:50 -0700 Subject: [PATCH] Clean up node executor state machine some Apply same pattern as the HoverslamAutopilot --- MechJeb2/MechJebModuleNodeExecutor.cs | 70 ++++++++++++++++++--------- 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/MechJeb2/MechJebModuleNodeExecutor.cs b/MechJeb2/MechJebModuleNodeExecutor.cs index d649512ab..6d6ed5ab1 100644 --- a/MechJeb2/MechJebModuleNodeExecutor.cs +++ b/MechJeb2/MechJebModuleNodeExecutor.cs @@ -84,12 +84,11 @@ public void ExecuteAllNodes(object controller) private void Init() { - State = States.WARPALIGN; _direction = Vector3d.zero; _dvLeft = Vessel.patchedConicSolver.maneuverNodes[0].GetBurnVector(Orbit).magnitude; - Core.Thrust.ThrustOff(); Core.Attitude.Users.Add(this); Core.Thrust.Users.Add(this); + TransitionTo(States.WARPALIGN); } public void Abort() @@ -194,30 +193,53 @@ public override void OnFixedUpdate() // note that in principia after our node disappears this value will change to -1 _ignitionUT = CalculateIgnitionUT(); - if (VesselState.Time >= _ignitionUT - LeadTime && State != States.BURN) - State = States.LEAD; + UpdateState(); + TickState(); + } - if (VesselState.Time >= _ignitionUT && Aligned()) - State = States.BURN; + private void UpdateState() + { + States desired = DetermineState(State); + if (desired != State) TransitionTo(desired); + } - switch (State) + private States DetermineState(States desired) + { + if (desired == States.WARPALIGN && VesselState.Time >= _ignitionUT - LeadTime) + desired = States.LEAD; + + if ((desired == States.WARPALIGN || desired == States.LEAD) && VesselState.Time >= _ignitionUT && Aligned()) + desired = States.BURN; + + return desired; + } + + private void TransitionTo(States next) + { + State = next; + switch (next) { - case States.WARPALIGN: - StateWarpAlign(); - return; - case States.LEAD: - StateLeadTime(); - return; - case States.BURN: - StateBurn(); - return; + case States.WARPALIGN: OnEnterWarpAlign(); break; + case States.LEAD: OnEnterLead(); break; + case States.BURN: OnEnterBurn(); break; + case States.IDLE: OnEnterIdle(); break; } } - private void StateWarpAlign() + private void TickState() { - Core.Thrust.ThrustOff(); + switch (State) + { + case States.WARPALIGN: TickWarpAlign(); break; + case States.LEAD: TickLead(); break; + case States.BURN: TickBurn(); break; + } + } + + private void OnEnterWarpAlign() => Core.Thrust.ThrustOff(); + private void TickWarpAlign() + { if (!Autowarp) { SetAttitude(); @@ -248,10 +270,10 @@ private void StateWarpAlign() SetAttitude(); } - private void StateLeadTime() - { - Core.Thrust.ThrustOff(); + private void OnEnterLead() => Core.Thrust.ThrustOff(); + private void TickLead() + { if (!MuUtils.PhysicsRunning()) { Core.Warp.MinimumWarp(); @@ -267,7 +289,11 @@ private void StateLeadTime() DecrementDvLeft(); } - private void StateBurn() + private void OnEnterBurn() { } + + private void OnEnterIdle() { } + + private void TickBurn() { if (!MuUtils.PhysicsRunning()) {