Skip to content

Commit

Permalink
Use callbacks and fix a few bugs
Browse files Browse the repository at this point in the history
Now works from pad to orbit with my test vehicle.

Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Jul 3, 2023
1 parent 13fb2ac commit 34eb79e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
6 changes: 3 additions & 3 deletions MechJeb2/MechJebLib/Simulations/FuelFlowSimulation.cs
Expand Up @@ -11,8 +11,7 @@ namespace MechJebLib.Simulations
// - git rid of the annoying extra stage
// - isn't running in the VAB
//
// - actually use the objectpooling
// - use vessel modification callbacks and write thin Refresh() API
// - compute both atmo + vac delta-V
// - add threading
// - make it compatible with the old simulation and wire it up to a button in the UI
public class FuelFlowSimulation
Expand All @@ -30,6 +29,7 @@ public void Run(SimVessel vessel)
_currentSegment = null;
Segments.Clear();
vessel.MainThrottle = 1.0;
vessel.UpdateMass();

Log($"starting stage: {vessel.CurrentStage}");

Expand Down Expand Up @@ -192,7 +192,7 @@ private double MinimumTimeStep(SimVessel vessel)
{
double maxTime = vessel.ResourceMaxTime();

return maxTime < double.MaxValue ? maxTime : 0;
return maxTime < double.MaxValue && maxTime >= 0 ? maxTime : 0;
}

private void UpdateActiveEngines(SimVessel vessel)
Expand Down
2 changes: 1 addition & 1 deletion MechJeb2/MechJebLib/Simulations/SimVesselBuilder.cs
Expand Up @@ -80,7 +80,7 @@ internal void BuildVessel(IShipconstruct kspVessel)

internal void BuildParts()
{
_vessel.CurrentStage = StageManager.CurrentStage + 1;
_vessel.CurrentStage = StageManager.CurrentStage;

foreach (Part kspPart in _kspVessel.Parts)
{
Expand Down
6 changes: 6 additions & 0 deletions MechJeb2/MechJebLib/Simulations/SimVesselManager.cs
Expand Up @@ -62,5 +62,11 @@ private void Clear()
_inversePartMapping.Clear();
_inversePartModuleMapping.Clear();
}

public void Release()
{
Clear();
_vessel.Dispose();
}
}
}
24 changes: 19 additions & 5 deletions MechJeb2/MechJebModuleNewFFSTesting.cs
Expand Up @@ -13,20 +13,26 @@ public MechJebModuleNewFFSTesting(MechJebCore core) : base(core)
Enabled = true;
}

private bool _needsRebuild;

protected override void OnModuleEnabled()
{
_needsRebuild = true;
}

protected override void OnModuleDisabled()
{
_vesselManager.Release();
}

private readonly SimVesselManager _vesselManager = new SimVesselManager();

public override void OnFixedUpdate()
{
_vesselManager.Build(Vessel);
_vesselManager.Update();
if (_needsRebuild)
_vesselManager.Build(Vessel);
else
_vesselManager.Update();
_vesselManager.SetConditions(0, 0, 0);
_vesselManager.RunFuelFlowSimulation();
List<FuelStats> segments = _vesselManager.GetFuelFlowResults();
Expand All @@ -36,16 +42,24 @@ public override void OnFixedUpdate()

public override void OnStart(PartModule.StartState state)
{
GameEvents.onStageActivate.Add(HandleStageEvent);
GameEvents.onVesselStandardModification.Add(RebuildOnModification);
GameEvents.StageManager.OnGUIStageSequenceModified.Add(Rebuild);
}

public override void OnDestroy()
{
GameEvents.onStageActivate.Remove(HandleStageEvent);
GameEvents.onVesselStandardModification.Remove(RebuildOnModification);
GameEvents.StageManager.OnGUIStageSequenceModified.Remove(Rebuild);
}

private void Rebuild()
{
_needsRebuild = true;
}

private void HandleStageEvent(int data)
private void RebuildOnModification(Vessel data)
{
Rebuild();
}
}
}

0 comments on commit 34eb79e

Please sign in to comment.