/
MechJebModuleSuicideTimer.cs
85 lines (66 loc) · 2.46 KB
/
MechJebModuleSuicideTimer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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();
}
}
}