Skip to content

Commit

Permalink
Savegame: getting close
Browse files Browse the repository at this point in the history
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Jul 8, 2023
1 parent c560944 commit 663f947
Show file tree
Hide file tree
Showing 21 changed files with 482 additions and 412 deletions.
2 changes: 1 addition & 1 deletion Localization/en-us.cfg
Expand Up @@ -1021,7 +1021,7 @@ Localization
#MechJeb_InfoItems_StatsColumn10 = Atmo ΔV
#MechJeb_InfoItems_StatsColumn11 = Vac ΔV
#MechJeb_InfoItems_StatsColumn12 = Time
#MechJeb_InfoItems_StatsColumn13 = Max Thrust
#MechJeb_InfoItems_StatsColumn13 = Thrust


//Ascent Path Editor
Expand Down
1 change: 1 addition & 0 deletions MechJeb2.sln.DotSettings
Expand Up @@ -26,5 +26,6 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RK/@EntryIndexedValue">RK</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RO/@EntryIndexedValue">RO</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SI/@EntryIndexedValue">SI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SLT/@EntryIndexedValue">SLT</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SMA/@EntryIndexedValue">SMA</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SOI/@EntryIndexedValue">SOI</s:String></wpf:ResourceDictionary>
4 changes: 0 additions & 4 deletions MechJeb2/ComputerModule.cs
Expand Up @@ -123,10 +123,6 @@ public virtual void OnFixedUpdate()
{
}

public virtual void OnWaitForFixedUpdate()
{
}

public virtual void OnUpdate()
{
}
Expand Down
480 changes: 240 additions & 240 deletions MechJeb2/MechJeb2.csproj

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion MechJeb2/MechJebLib/Primitives/H1.cs
Expand Up @@ -23,9 +23,10 @@ private static H1 New()
return new H1();
}

public static H1 Get()
public static H1 Get(bool unityCompat = false)
{
H1 h = _pool.Borrow();
h.UnityCompat = unityCompat;
return h;
}

Expand Down
10 changes: 10 additions & 0 deletions MechJeb2/MechJebLib/Primitives/HBase.cs
Expand Up @@ -12,6 +12,9 @@ namespace MechJebLib.Primitives
{
public abstract class HBase<T> : IDisposable
{
// UnityCompat does no extrapolation outside of MinTime/MaxTime
public bool UnityCompat = false;

protected double MinTime = double.MaxValue;
protected double MaxTime = double.MinValue;

Expand Down Expand Up @@ -205,6 +208,9 @@ public T Evaluate(double t)

if (t <= MinTime)
{
if (UnityCompat)
return Allocate(_list.Values[0].Value);

T ret = Allocate();
Multiply(_list.Values[0].InTangent, MinTime - t, ref ret);
Subtract(_list.Values[0].Value, ret, ref ret);
Expand All @@ -213,6 +219,9 @@ public T Evaluate(double t)

if (t >= MaxTime)
{
if (UnityCompat)
return Allocate(_list.Values[_list.Count - 1].Value);

T ret = Allocate();
Multiply(_list.Values[_list.Count - 1].OutTangent, t - MaxTime, ref ret);
Addition(_list.Values[_list.Count - 1].Value, ret, ref ret);
Expand Down Expand Up @@ -246,6 +255,7 @@ public virtual void Clear()

public virtual void Dispose()
{
UnityCompat = false;
Clear();
}
}
Expand Down
15 changes: 8 additions & 7 deletions MechJeb2/MechJebLib/Simulations/FuelFlowSimulation.cs
Expand Up @@ -8,12 +8,10 @@
namespace MechJebLib.Simulations
{
// TODO:
// - isn't running in the VAB
// - add threading
// - debug remaining uses of vacStats and mjPhase vs kspStage issues
// - fix stage display to show kspStages again
// - wire up cosLoss properly in the GUI
// - the tiny-partial-stage FIXME below
// - remove all the logging
// - do some perf testing
public class FuelFlowSimulation
{
private const int MAXSTEPS = 100;
Expand All @@ -22,6 +20,7 @@ public class FuelFlowSimulation
private readonly List<SimPart> _sources = new List<SimPart>();
private FuelStats? _currentSegment;
private double _time;
public bool DVLinearThrust = true; // include cos losses

public void Run(SimVessel vessel)
{
Expand All @@ -31,10 +30,10 @@ public void Run(SimVessel vessel)
vessel.MainThrottle = 1.0;

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

while (vessel.CurrentStage >= 0)
{
vessel.UpdateMass();
Log($"current stage: {vessel.CurrentStage}");
SimulateStage(vessel);
FinishSegment(vessel);
Expand All @@ -46,9 +45,11 @@ public void Run(SimVessel vessel)

private void SimulateStage(SimVessel vessel)
{
vessel.UpdateMass();
UpdateEngineStats(vessel);
UpdateActiveEngines(vessel);
UpdateResourceDrainsAndResiduals(vessel);

GetNextSegment(vessel);
double currentThrust = vessel.ThrustMagnitude;

Expand All @@ -75,6 +76,7 @@ private void SimulateStage(SimVessel vessel)

_time += dt;
ApplyResourceDrains(vessel, dt);

vessel.UpdateMass();
UpdateEngineStats(vessel);
UpdateActiveEngines(vessel);
Expand Down Expand Up @@ -227,8 +229,7 @@ private void GetNextSegment(SimVessel vessel)
_currentSegment = new FuelStats
{
KSPStage = vessel.CurrentStage,
Thrust = vessel.ThrustMagnitude,
ThrustNoCosLoss = vessel.ThrustNoCosLoss,
Thrust = DVLinearThrust ? vessel.ThrustMagnitude : vessel.ThrustNoCosLoss,
StartTime = _time,
StartMass = vessel.Mass,
SpoolUpTime = vessel.SpoolupCurrent,
Expand Down
1 change: 0 additions & 1 deletion MechJeb2/MechJebLib/Simulations/FuelStats.cs
Expand Up @@ -16,7 +16,6 @@ public class FuelStats
public double StartMass;
public double StartTime;
public double Thrust;
public double ThrustNoCosLoss;
public double SpoolUpTime;
public double MaxAccel => EndMass > 0 ? Thrust / EndMass : 0;
public double ResourceMass => StartMass - EndMass;
Expand Down
18 changes: 8 additions & 10 deletions MechJeb2/MechJebLib/Simulations/PartModules/SimModuleEngines.cs
Expand Up @@ -59,14 +59,14 @@ public class SimModuleEngines : SimPartModule
public double ModuleSpoolupTime;
public bool NoPropellants;

public readonly H1 ThrustCurve = H1.Get();
public readonly H1 ThrottleIspCurve = H1.Get();
public readonly H1 ThrottleIspCurveAtmStrength = H1.Get();
public readonly H1 VelCurve = H1.Get();
public readonly H1 VelCurveIsp = H1.Get();
public readonly H1 ATMCurve = H1.Get();
public readonly H1 ATMCurveIsp = H1.Get();
public readonly H1 AtmosphereCurve = H1.Get();
public readonly H1 ThrustCurve = H1.Get(true);
public readonly H1 ThrottleIspCurve = H1.Get(true);
public readonly H1 ThrottleIspCurveAtmStrength = H1.Get(true);
public readonly H1 VelCurve = H1.Get(true);
public readonly H1 VelCurveIsp = H1.Get(true);
public readonly H1 ATMCurve = H1.Get(true);
public readonly H1 ATMCurveIsp = H1.Get(true);
public readonly H1 AtmosphereCurve = H1.Get(true);

private double _throttle => Part.Vessel.MainThrottle;
private double _atmPressure => Part.Vessel.ATMPressure;
Expand Down Expand Up @@ -178,7 +178,6 @@ private static void Clear(SimModuleEngines m)
m.ResourceConsumptions.Clear();
m.Propellants.Clear();
m.ThrustTransformMultipliers.Clear();

m.ThrustCurve.Clear();
m.ThrottleIspCurve.Clear();
m.ThrottleIspCurveAtmStrength.Clear();
Expand Down Expand Up @@ -325,7 +324,6 @@ private double FlowMultiplierAtConditions()
private double ISPAtConditions()
{
double isp = AtmosphereCurve.Evaluate(_atmPressure);

if (UseThrottleIspCurve)
isp *= Lerp(1f, ThrottleIspCurve.Evaluate(_throttle), ThrottleIspCurveAtmStrength.Evaluate(_atmPressure));
if (UseAtmCurveIsp)
Expand Down
9 changes: 7 additions & 2 deletions MechJeb2/MechJebLib/Simulations/SimVessel.cs
Expand Up @@ -65,15 +65,20 @@ public void Stage()

Log($"{EnginesActivatedInStage[CurrentStage].Count} engines activated in stage");

ActivateEngines();

UpdateMass();
}

public void ActivateEngines()
{
foreach (SimModuleEngines e in EnginesActivatedInStage[CurrentStage])
if (e.IsEnabled)
e.Activate();

foreach (SimModuleRCS r in RCSActivatedInStage[CurrentStage])
if (r.IsEnabled)
r.Activate();

UpdateMass();
}

public void UpdateActiveEngines()
Expand Down
2 changes: 2 additions & 0 deletions MechJeb2/MechJebLib/Simulations/SimVesselManager.cs
Expand Up @@ -13,6 +13,7 @@ public partial class SimVesselManager
private SimVessel _vessel;
private IShipconstruct _kspVessel;
private readonly FuelFlowSimulation _fuelFlowSimulation = new FuelFlowSimulation();
public bool DVLinearThrust = true; // include cos losses

private readonly Dictionary<Part, SimPart> _partMapping = new Dictionary<Part, SimPart>();
private readonly Dictionary<SimPart, Part> _inversePartMapping = new Dictionary<SimPart, Part>();
Expand Down Expand Up @@ -50,6 +51,7 @@ public void SetConditions(double atmDensity, double atmPressure, double machNumb

public void RunFuelFlowSimulation()
{
_fuelFlowSimulation.DVLinearThrust = DVLinearThrust;
_fuelFlowSimulation.Run(_vessel);
}

Expand Down
4 changes: 2 additions & 2 deletions MechJeb2/MechJebModuleAscentMenu.cs
Expand Up @@ -507,8 +507,8 @@ private string PhaseString(Solution solution, double t, int pvgPhase)
int mjPhase = solution.MJPhase(pvgPhase);
int kspStage = solution.KSPStage(pvgPhase);

if (mjPhase < Core.StageStats.vacStats.Count)
stageDeltaV = Core.StageStats.vacStats[mjPhase].DeltaV;
if (mjPhase < Core.StageStats.VacStats.Count)
stageDeltaV = Core.StageStats.VacStats[mjPhase].DeltaV;

double excessDV = stageDeltaV - solution.DV(t, pvgPhase);

Expand Down
19 changes: 10 additions & 9 deletions MechJeb2/MechJebModuleAscentPVGSettingsMenu.cs
Expand Up @@ -48,27 +48,28 @@ protected override void WindowGUI(int windowID)

int topstage = -1;

if (_ascentSettings.LastStage < Core.StageStats.vacStats.Count && Core.StageStats.vacStats.Count > 0)
var vacStats = Core.StageStats.VacStats;

if (vacStats.Count > 0 && _ascentSettings.LastStage < vacStats[vacStats.Count-1].KSPStage)
{
GUILayout.BeginVertical(GUI.skin.box);

_ascentSettings.LastStage.val = Clamp(_ascentSettings.LastStage.val, 0, Core.StageStats.VacStats.Count - 1);

_ascentSettings.LastStage.val = Clamp(_ascentSettings.LastStage.val, 0, Core.StageStats.vacStats.Count - 1);

for (int i = _ascentSettings.LastStage; i < Core.StageStats.vacStats.Count; i++)
for (int mjPhase = _ascentSettings.LastStage; mjPhase < Core.StageStats.VacStats.Count; mjPhase++)
{
FuelStats stats = Core.StageStats.vacStats[i];
FuelStats stats = Core.StageStats.VacStats[mjPhase];
if (stats.DeltaV < _ascentSettings.MinDeltaV.val)
continue;

if (topstage < 0)
topstage = i;
topstage = stats.KSPStage;

GUILayout.BeginHorizontal();
GUILayout.Label($"{i,3} {stats.DeltaV:##,###0} m/s");
if (_ascentSettings.UnguidedStages.Contains(i))
GUILayout.Label($"{mjPhase,3} {stats.DeltaV:##,###0} m/s");
if (_ascentSettings.UnguidedStages.Contains(mjPhase))
GUILayout.Label(" (unguided)");
if (_ascentSettings.OptimizeStage == i)
if (_ascentSettings.OptimizeStage == mjPhase)
GUILayout.Label(" (optimize)");
GUILayout.EndHorizontal();
}
Expand Down
26 changes: 13 additions & 13 deletions MechJeb2/MechJebModuleInfoItems.cs
Expand Up @@ -945,9 +945,9 @@ public double StageDeltaVVacuum()
MechJebModuleStageStats stats = Core.GetComputerModule<MechJebModuleStageStats>();
stats.RequestUpdate(this);

if (stats.vacStats.Count == 0) return 0;
if (stats.VacStats.Count == 0) return 0;

return stats.vacStats[stats.vacStats.Count - 1].DeltaV;
return stats.VacStats[stats.VacStats.Count - 1].DeltaV;
}

[ValueInfoItem("#MechJeb_StageDV_atmo", InfoItem.Category.Vessel, format = "F0", units = "m/s", showInEditor = true)] //Stage ΔV (atmo)
Expand All @@ -956,9 +956,9 @@ public double StageDeltaVAtmosphere()
MechJebModuleStageStats stats = Core.GetComputerModule<MechJebModuleStageStats>();
stats.RequestUpdate(this);

if (stats.atmoStats.Count == 0) return 0;
if (stats.AtmoStats.Count == 0) return 0;

return stats.atmoStats[stats.atmoStats.Count - 1].DeltaV;
return stats.AtmoStats[stats.AtmoStats.Count - 1].DeltaV;
}

[ValueInfoItem("#MechJeb_StageDV_atmo_vac", InfoItem.Category.Vessel, units = "m/s", showInEditor = true)] //Stage ΔV (atmo, vac)
Expand All @@ -967,8 +967,8 @@ public string StageDeltaVAtmosphereAndVac()
MechJebModuleStageStats stats = Core.GetComputerModule<MechJebModuleStageStats>();
stats.RequestUpdate(this);

double atmDv = stats.atmoStats.Count == 0 ? 0 : stats.atmoStats[stats.atmoStats.Count - 1].DeltaV;
double vacDv = stats.vacStats.Count == 0 ? 0 : stats.vacStats[stats.vacStats.Count - 1].DeltaV;
double atmDv = stats.AtmoStats.Count == 0 ? 0 : stats.AtmoStats[stats.AtmoStats.Count - 1].DeltaV;
double vacDv = stats.VacStats.Count == 0 ? 0 : stats.VacStats[stats.VacStats.Count - 1].DeltaV;

return string.Format("{0:F0}, {1:F0}", atmDv, vacDv);
}
Expand All @@ -980,10 +980,10 @@ public float StageTimeLeftFullThrottle()
MechJebModuleStageStats stats = Core.GetComputerModule<MechJebModuleStageStats>();
stats.RequestUpdate(this);

if (stats.vacStats.Count == 0 || stats.atmoStats.Count == 0) return 0;
if (stats.VacStats.Count == 0 || stats.AtmoStats.Count == 0) return 0;

float vacTimeLeft = (float)stats.vacStats[stats.vacStats.Count - 1].DeltaTime;
float atmoTimeLeft = (float)stats.atmoStats[stats.atmoStats.Count - 1].DeltaTime;
float vacTimeLeft = (float)stats.VacStats[stats.VacStats.Count - 1].DeltaTime;
float atmoTimeLeft = (float)stats.AtmoStats[stats.AtmoStats.Count - 1].DeltaTime;
float timeLeft = Mathf.Lerp(vacTimeLeft, atmoTimeLeft, Mathf.Clamp01((float)FlightGlobals.getStaticPressure()));

return timeLeft;
Expand Down Expand Up @@ -1013,15 +1013,15 @@ public double TotalDeltaVVaccum()
{
MechJebModuleStageStats stats = Core.GetComputerModule<MechJebModuleStageStats>();
stats.RequestUpdate(this);
return stats.vacStats.Sum(s => s.DeltaV);
return stats.VacStats.Sum(s => s.DeltaV);
}

[ValueInfoItem("#MechJeb_TotalDV_atmo", InfoItem.Category.Vessel, format = "F0", units = "m/s", showInEditor = true)] //Total ΔV (atmo)
public double TotalDeltaVAtmosphere()
{
MechJebModuleStageStats stats = Core.GetComputerModule<MechJebModuleStageStats>();
stats.RequestUpdate(this);
return stats.atmoStats.Sum(s => s.DeltaV);
return stats.AtmoStats.Sum(s => s.DeltaV);
}

[ValueInfoItem("#MechJeb_TotalDV_atmo_vac", InfoItem.Category.Vessel, units = "m/s", showInEditor = true)] //Total ΔV (atmo, vac)
Expand All @@ -1030,8 +1030,8 @@ public string TotalDeltaVAtmosphereAndVac()
MechJebModuleStageStats stats = Core.GetComputerModule<MechJebModuleStageStats>();
stats.RequestUpdate(this);

double atmDv = stats.atmoStats.Sum(s => s.DeltaV);
double vacDv = stats.vacStats.Sum(s => s.DeltaV);
double atmDv = stats.AtmoStats.Sum(s => s.DeltaV);
double vacDv = stats.VacStats.Sum(s => s.DeltaV);

return string.Format("{0:F0}, {1:F0}", atmDv, vacDv);
}
Expand Down
8 changes: 4 additions & 4 deletions MechJeb2/MechJebModuleNodeExecutor.cs
Expand Up @@ -355,16 +355,16 @@ private double BurnTime(double dv, out double halfBurnTime, out double spoolupTi
stats.RequestUpdate(this);

double lastStageBurnTime = 0;
for (int i = stats.vacStats.Count - 1; i >= 0 && dvLeft > 0; i--)
for (int mjPhase = stats.VacStats.Count - 1; mjPhase >= 0 && dvLeft > 0; mjPhase--)
{
FuelStats s = stats.vacStats[i];
FuelStats s = stats.VacStats[mjPhase];
if (s.DeltaV <= 0 || s.Thrust <= 0)
{
if (Core.Staging.Enabled)
{
// We staged again before autostagePreDelay is elapsed.
// Add the remaining wait time
if (burnTime - lastStageBurnTime < Core.Staging.autostagePreDelay && i != stats.vacStats.Count - 1)
if (burnTime - lastStageBurnTime < Core.Staging.autostagePreDelay && mjPhase != stats.VacStats.Count - 1)
burnTime += Core.Staging.autostagePreDelay - (burnTime - lastStageBurnTime);
burnTime += Core.Staging.autostagePreDelay;
lastStageBurnTime = burnTime;
Expand All @@ -391,7 +391,7 @@ private double BurnTime(double dv, out double halfBurnTime, out double spoolupTi
// all but the current stage. This is wrong, but hopefully it's
// close enough for now.
// TODO: Be smarter about throttle limits on future stages.
if (i == stats.vacStats.Count - 1)
if (mjPhase == stats.VacStats.Count - 1)
{
stageAvgAccel *= VesselState.throttleFixedLimit;
}
Expand Down

0 comments on commit 663f947

Please sign in to comment.