Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to C# 9.0, add partial suicide burn overhaul
This is kinda weird, its got a half-finished fully numerical suicide burn overhaul I did which needs to be finished, also bumps the version to C# 9 and fixes a problem with the FlightRecorder using the 'record' newly reserved word, along with some support for BackgroundJob in the MechJebUtils class. What I really want is that support for BackgroundJobs to use with the delta-V overhaul, and all this work is drifting and merge conflicting as I rename things, so I'm going to merge the suicide burn timer a bit half-done. The module isn't wired up anywhere so shouldn't run. Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
- Loading branch information
1 parent
f6905a3
commit c206ba6
Showing
10 changed files
with
220 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using MechJebLib.Primitives; | ||
using MechJebLib.Utils; | ||
|
||
namespace MuMech.MechJebLib | ||
{ | ||
public partial class Suicide : BackgroundJob<Suicide.SuicideResult> | ||
{ | ||
public struct SuicideResult | ||
{ | ||
public V3 Rf; | ||
public double Tf; | ||
} | ||
|
||
public static SuicideBuilder Builder() | ||
{ | ||
return new SuicideBuilder(); | ||
} | ||
|
||
protected override SuicideResult Execute() | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections.Generic; | ||
using MechJebLib.Primitives; | ||
using MechJebLib.PVG; | ||
|
||
namespace MuMech.MechJebLib | ||
{ | ||
public partial class Suicide | ||
{ | ||
public class SuicideBuilder | ||
{ | ||
private readonly List<Phase> _phases = new List<Phase>(); | ||
|
||
public Suicide Build() | ||
{ | ||
var suicide = new Suicide(); | ||
|
||
return suicide; | ||
} | ||
|
||
public SuicideBuilder Initial(V3 r0, V3 v0, V3 u0, double t0, double mu, double rbody) | ||
{ | ||
return this; | ||
} | ||
|
||
public SuicideBuilder AddStageUsingFinalMass(double m0, double mf, double isp, double bt, int kspStage) | ||
{ | ||
_phases.Add(Phase.NewStageUsingFinalMass(m0, mf, isp, bt, kspStage)); | ||
|
||
return this; | ||
} | ||
|
||
public SuicideBuilder SetGround(double height) | ||
{ | ||
return this; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
#nullable enable | ||
|
||
namespace MechJebLib.Utils | ||
{ | ||
// TODO: | ||
// - cancellation | ||
// - timeout | ||
public abstract class BackgroundJob<T> | ||
{ | ||
private Task<T>? _task; | ||
public T Result = default!; | ||
|
||
protected abstract T Execute(); | ||
|
||
public void Cancel() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public bool IsRunning() | ||
{ | ||
return _task is not { IsCompleted: true }; | ||
} | ||
|
||
protected virtual void OnTaskCompleted(Task<T> task) | ||
{ | ||
Result = task.Result; | ||
_task = null; | ||
} | ||
|
||
protected virtual void OnTaskFaulted(Task<T> task) | ||
{ | ||
_task = null; | ||
} | ||
|
||
protected virtual void OnTaskCancelled(Task<T> task) | ||
{ | ||
_task = null; | ||
} | ||
|
||
public void Run() | ||
{ | ||
_task = Task.Run(Execute); | ||
_task.ContinueWith(OnTaskCompleted, TaskContinuationOptions.OnlyOnRanToCompletion); | ||
_task.ContinueWith(OnTaskFaulted, TaskContinuationOptions.OnlyOnFaulted); | ||
_task.ContinueWith(OnTaskCancelled, TaskContinuationOptions.OnlyOnCanceled); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#nullable enable | ||
|
||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
using MechJebLib.Primitives; | ||
using MuMech.MechJebLib; | ||
|
||
namespace MuMech | ||
{ | ||
[UsedImplicitly] | ||
public class MechJebModuleSuicideTimer : ComputerModule | ||
{ | ||
private Suicide? _suicide; | ||
private Suicide.SuicideResult _lastResult; | ||
public Vector3d Rf => _lastResult.Rf.V3ToWorldRotated(); | ||
|
||
public MechJebModuleSuicideTimer(MechJebCore core) : base(core) | ||
{ | ||
} | ||
|
||
protected override void OnModuleEnabled() | ||
{ | ||
Reset(); | ||
} | ||
|
||
protected override void OnModuleDisabled() | ||
{ | ||
Reset(); | ||
} | ||
|
||
private void Reset() | ||
{ | ||
_lastResult.Rf = V3.zero; | ||
_lastResult.Tf = VesselState.time; | ||
_suicide?.Cancel(); | ||
_suicide = null; | ||
} | ||
|
||
|
||
private double GetGroundHeight() | ||
{ | ||
if (Rf == Vector3d.zero) | ||
return MainBody.Radius; | ||
|
||
Vector3d latlng = MainBody.GetLatitudeAndLongitude(Rf, true); | ||
return MainBody.TerrainAltitude(latlng[0], latlng[1]); | ||
} | ||
|
||
public override void OnFixedUpdate() | ||
{ | ||
if (!Vessel.VesselOffGround()) | ||
return; | ||
|
||
// make sure our orbit at least dips into the atmosphere, or bodyRadius plus some maximum height of mountains | ||
|
||
Core.StageStats.RequestUpdate(this); | ||
|
||
if (Core.StageStats.vacStats.Length <= 0) | ||
return; | ||
|
||
if (_suicide != null) | ||
{ | ||
if (_suicide.IsRunning()) | ||
return; | ||
|
||
_lastResult = _suicide.Result; | ||
} | ||
|
||
Suicide.SuicideBuilder suicideBuilder = Suicide.Builder() | ||
.Initial(VesselState.orbitalPosition.WorldToV3Rotated(), VesselState.orbitalVelocity.WorldToV3Rotated(), | ||
VesselState.forward.WorldToV3Rotated(), VesselState.time, MainBody.gravParameter, MainBody.Radius) | ||
.SetGround(GetGroundHeight()); | ||
|
||
for (int i = Core.StageStats.vacStats.Length - 1; i >= 0; i--) | ||
{ | ||
FuelFlowSimulation.FuelStats fuelStats = Core.StageStats.vacStats[i]; | ||
|
||
suicideBuilder.AddStageUsingFinalMass(fuelStats.StartMass * 1000, fuelStats.EndMass * 1000, fuelStats.Isp, fuelStats.DeltaTime, i); | ||
} | ||
|
||
_suicide = suicideBuilder.Build(); | ||
_suicide.Run(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters