Skip to content

Commit

Permalink
Let multiple modules limit autostaging and add/remove independently
Browse files Browse the repository at this point in the history
This way they don't scribble over what each other wants, and the
staging controller is responsible for tracking and satisfying
multiple requests.

Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Jul 21, 2023
1 parent d9df5df commit 7bb88a3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions MechJeb2/MechJebModuleGuidanceController.cs
Expand Up @@ -300,7 +300,7 @@ private void HandleThrottle()
// RCS trim, autostaging will stage off the spent engine if there's no relights. This is unwanted
// since the insertion stage may still have RCS which is necessary to complete the mission.
if (coastStage >= 0 && Vessel.currentStage == coastStage && Solution.WillCoast(VesselState.time))
Core.Staging.autostageLimitInternal = coastStage;
Core.Staging.AutoStageLimitRequest(coastStage, this);

if (Solution.Coast(VesselState.time))
{
Expand All @@ -321,7 +321,7 @@ private void HandleThrottle()
return;
}

Core.Staging.autostageLimitInternal = Solution.TerminalStage();
Core.Staging.AutoStageLimitRequest(Solution.TerminalStage(), this);

ThrottleOn();

Expand Down
5 changes: 2 additions & 3 deletions MechJeb2/MechJebModuleSpinupController.cs
Expand Up @@ -37,8 +37,7 @@ protected override void OnModuleDisabled()
_state = SpinupState.FINISHED;
Core.Attitude.SetOmegaTarget(roll: double.NaN);
Core.Attitude.SetActuationControl();
// FIXME: this might overwrite someone else, but the only other consumer so far is the GuidanceController
Core.Staging.autostageLimitInternal = 0;
Core.Staging.AutoStageLimitRemove( this);
Core.Attitude.Users.Remove(this);
base.OnModuleDisabled();
}
Expand All @@ -65,7 +64,7 @@ public override void Drive(FlightCtrlState s)
if (_state == SpinupState.INITIALIZED)
return;

Core.Staging.autostageLimitInternal = Vessel.currentStage;
Core.Staging.AutoStageLimitRequest(Vessel.currentStage, this);

if (VesselState.time < _startTime)
return;
Expand Down
27 changes: 23 additions & 4 deletions MechJeb2/MechJebModuleStagingController.cs
Expand Up @@ -55,8 +55,27 @@ public MechJebModuleStagingController(MechJebCore core)

public bool waitingForFirstStaging;

// this is for other modules to temporarily disable autostaging (e.g. PVG coast phases)
public int autostageLimitInternal;
private readonly Dictionary<object, int> _autoStageModuleLimit = new Dictionary<object, int>();

public void AutoStageLimitRequest(int stage, object user)
{
_autoStageModuleLimit.TryAdd(user, stage);
}

public void AutoStageLimitRemove(object user)
{
if (_autoStageModuleLimit.ContainsKey(user))
_autoStageModuleLimit.Remove(user);
}

private int ActiveAutoStageModuleLimit()
{
int limit = 0;
foreach(int key in _autoStageModuleLimit.Values)
if (key > limit)
limit = key;
return limit;
}

private readonly List<ModuleEngines> activeModuleEngines = new List<ModuleEngines>(16);
private readonly List<ModuleEngines> allModuleEngines = new List<ModuleEngines>(16);
Expand Down Expand Up @@ -161,7 +180,7 @@ public void AutostageOnce(object user)

protected override void OnModuleEnabled()
{
autostageLimitInternal = 0;
_autoStageModuleLimit.Clear();
}

protected override void OnModuleDisabled()
Expand Down Expand Up @@ -269,7 +288,7 @@ public override void OnUpdate()

// this is for PVG preventing staging doing coasts, possibly it should be more specific of an API
// (e.g. bool PVGIsCoasting) since it is getting tightly coupled.
if (Vessel.currentStage <= autostageLimitInternal)
if (Vessel.currentStage <= ActiveAutoStageModuleLimit())
{
// force staging once if fairing conditions are met in the next stage
if (HasFairing(Vessel.currentStage - 1) && !WaitingForFairing())
Expand Down

0 comments on commit 7bb88a3

Please sign in to comment.