/
MechJebModuleRendezvousGuidance.cs
213 lines (180 loc) · 9.36 KB
/
MechJebModuleRendezvousGuidance.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System.Linq;
using JetBrains.Annotations;
using KSP.Localization;
using UnityEngine;
using static MechJebLib.Utils.Statics;
namespace MuMech
{
[UsedImplicitly]
public class MechJebModuleRendezvousGuidance : DisplayModule
{
public MechJebModuleRendezvousGuidance(MechJebCore core) : base(core) { }
private readonly EditableDoubleMult phasingOrbitAltitude = new EditableDoubleMult(200000, 1000);
protected override void WindowGUI(int windowID)
{
if (!core.target.NormalTargetExists)
{
GUILayout.Label(Localizer.Format("#MechJeb_RZplan_label1")); //"Select a target to rendezvous with."
base.WindowGUI(windowID);
return;
}
if (core.target.TargetOrbit.referenceBody != orbit.referenceBody)
{
GUILayout.Label(Localizer.Format("#MechJeb_RZplan_label2")); //"Rendezvous target must be in the same sphere of influence."
base.WindowGUI(windowID);
return;
}
GUILayout.BeginVertical();
//Information readouts:
GuiUtils.SimpleLabel(Localizer.Format("#MechJeb_RZplan_label3"), core.target.Name); //"Rendezvous target"
const double leadTime = 30;
GuiUtils.SimpleLabel(Localizer.Format("#MechJeb_RZplan_label4"),
core.target.TargetOrbit.PeA.ToSI(3) + "m x " + core.target.TargetOrbit.ApA.ToSI(3) + "m"); //"Target orbit"
GuiUtils.SimpleLabel(Localizer.Format("#MechJeb_RZplan_label5"), orbit.PeA.ToSI(3) + "m x " + orbit.ApA.ToSI(3) + "m"); //"Current orbit"
GuiUtils.SimpleLabel(Localizer.Format("#MechJeb_RZplan_label6"),
orbit.RelativeInclination(core.target.TargetOrbit).ToString("F2") + "º"); //"Relative inclination"
double closestApproachTime = orbit.NextClosestApproachTime(core.target.TargetOrbit, vesselState.time);
GuiUtils.SimpleLabel(Localizer.Format("#MechJeb_RZplan_label7"),
GuiUtils.TimeToDHMS(closestApproachTime - vesselState.time)); //"Time until closest approach"
GuiUtils.SimpleLabel(Localizer.Format("#MechJeb_RZplan_label8"),
orbit.Separation(core.target.TargetOrbit, closestApproachTime).ToSI() + "m"); //"Separation at closest approach"
//Maneuver planning buttons:
if (GUILayout.Button(Localizer.Format("#MechJeb_RZplan_button1"))) //"Align Planes"
{
double UT;
Vector3d dV;
if (orbit.AscendingNodeExists(core.target.TargetOrbit))
{
dV = OrbitalManeuverCalculator.DeltaVAndTimeToMatchPlanesAscending(orbit, core.target.TargetOrbit, vesselState.time, out UT);
}
else
{
dV = OrbitalManeuverCalculator.DeltaVAndTimeToMatchPlanesDescending(orbit, core.target.TargetOrbit, vesselState.time, out UT);
}
vessel.RemoveAllManeuverNodes();
vessel.PlaceManeuverNode(orbit, dV, UT);
}
GUILayout.BeginHorizontal();
if (GUILayout.Button(Localizer.Format("#MechJeb_RZplan_button2"))) //"Establish new orbit at"
{
double phasingOrbitRadius = phasingOrbitAltitude + mainBody.Radius;
vessel.RemoveAllManeuverNodes();
if (orbit.ApR < phasingOrbitRadius)
{
double UT1 = vesselState.time + leadTime;
Vector3d dV1 = OrbitalManeuverCalculator.DeltaVToChangeApoapsis(orbit, UT1, phasingOrbitRadius);
vessel.PlaceManeuverNode(orbit, dV1, UT1);
Orbit transferOrbit = vessel.patchedConicSolver.maneuverNodes[0].nextPatch;
double UT2 = transferOrbit.NextApoapsisTime(UT1);
Vector3d dV2 = OrbitalManeuverCalculator.DeltaVToCircularize(transferOrbit, UT2);
vessel.PlaceManeuverNode(transferOrbit, dV2, UT2);
}
else if (orbit.PeR > phasingOrbitRadius)
{
double UT1 = vesselState.time + leadTime;
Vector3d dV1 = OrbitalManeuverCalculator.DeltaVToChangePeriapsis(orbit, UT1, phasingOrbitRadius);
vessel.PlaceManeuverNode(orbit, dV1, UT1);
Orbit transferOrbit = vessel.patchedConicSolver.maneuverNodes[0].nextPatch;
double UT2 = transferOrbit.NextPeriapsisTime(UT1);
Vector3d dV2 = OrbitalManeuverCalculator.DeltaVToCircularize(transferOrbit, UT2);
vessel.PlaceManeuverNode(transferOrbit, dV2, UT2);
}
else
{
double UT = orbit.NextTimeOfRadius(vesselState.time, phasingOrbitRadius);
Vector3d dV = OrbitalManeuverCalculator.DeltaVToCircularize(orbit, UT);
vessel.PlaceManeuverNode(orbit, dV, UT);
}
}
phasingOrbitAltitude.text = GUILayout.TextField(phasingOrbitAltitude.text, GUILayout.Width(70));
GUILayout.Label("km", GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
if (GUILayout.Button(Localizer.Format("#MechJeb_RZplan_button3"))) //"Intercept with Hohmann transfer"
{
double UT;
Vector3d dV = OrbitalManeuverCalculator.DeltaVAndTimeForHohmannTransfer(orbit, core.target.TargetOrbit, vesselState.time, out UT);
vessel.RemoveAllManeuverNodes();
vessel.PlaceManeuverNode(orbit, dV, UT);
}
if (GUILayout.Button(Localizer.Format("#MechJeb_RZplan_button4"))) //"Match velocities at closest approach"
{
double UT = closestApproachTime;
Vector3d dV = OrbitalManeuverCalculator.DeltaVToMatchVelocities(orbit, UT, core.target.TargetOrbit);
vessel.RemoveAllManeuverNodes();
vessel.PlaceManeuverNode(orbit, dV, UT);
}
if (GUILayout.Button(Localizer.Format("#MechJeb_RZplan_button5"))) //"Get closer"
{
double UT = vesselState.time;
(Vector3d dV, _) = OrbitalManeuverCalculator.DeltaVToInterceptAtTime(orbit, UT, core.target.TargetOrbit, 100, 10);
vessel.RemoveAllManeuverNodes();
vessel.PlaceManeuverNode(orbit, dV, UT);
}
if (GUILayout.Button(Localizer.Format("#MechJeb_RZplan_button9"))) //Remove ALL nodes
{
vessel.RemoveAllManeuverNodes();
}
if (core.node != null)
{
if (vessel.patchedConicSolver.maneuverNodes.Any() && !core.node.enabled)
{
if (GUILayout.Button(Localizer.Format("#MechJeb_RZplan_button6"))) //"Execute next node"
{
core.node.ExecuteOneNode(this);
}
if (vessel.patchedConicSolver.maneuverNodes.Count > 1)
{
if (GUILayout.Button(Localizer.Format("#MechJeb_RZplan_button7"))) //"Execute all nodes"
{
core.node.ExecuteAllNodes(this);
}
}
}
else if (core.node.enabled)
{
if (GUILayout.Button(Localizer.Format("#MechJeb_RZplan_button8"))) //"Abort node execution"
{
core.node.Abort();
}
}
GUILayout.BeginHorizontal();
core.node.autowarp =
GUILayout.Toggle(core.node.autowarp, Localizer.Format("#MechJeb_RZplan_checkbox"), GUILayout.ExpandWidth(true)); //"Auto-warp"
GUILayout.Label(Localizer.Format("#MechJeb_RZplan_label9"), GUILayout.ExpandWidth(false)); //"Tolerance:"
core.node.tolerance.text = GUILayout.TextField(core.node.tolerance.text, GUILayout.Width(35), GUILayout.ExpandWidth(false));
if (GUILayout.Button("+", GUILayout.ExpandWidth(false)))
{
core.node.tolerance.val += 0.1;
}
if (GUILayout.Button("-", GUILayout.ExpandWidth(false)))
{
core.node.tolerance.val -= core.node.tolerance.val > 0.1 ? 0.1 : 0.0;
}
if (GUILayout.Button("R", GUILayout.ExpandWidth(false)))
{
core.node.tolerance.val = 0.1;
}
GUILayout.Label("m/s", GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
base.WindowGUI(windowID);
}
public override GUILayoutOption[] WindowOptions()
{
return new[] { GUILayout.Width(300), GUILayout.Height(150) };
}
public override string GetName()
{
return Localizer.Format("#MechJeb_RZplan_title"); //"Rendezvous Planner"
}
public override string IconName()
{
return "Rendezvous Planner";
}
public override bool IsSpaceCenterUpgradeUnlocked()
{
return vessel.patchedConicsUnlocked();
}
}
}