/
TimeSelector.cs
256 lines (226 loc) · 10.7 KB
/
TimeSelector.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System;
using JetBrains.Annotations;
using KSP.Localization;
using UnityEngine;
namespace MuMech
{
public enum TimeReference
{
COMPUTED, X_FROM_NOW, APOAPSIS, PERIAPSIS, ALTITUDE, EQ_ASCENDING, EQ_DESCENDING,
REL_ASCENDING, REL_DESCENDING, CLOSEST_APPROACH,
EQ_HIGHEST_AD, EQ_NEAREST_AD, REL_HIGHEST_AD, REL_NEAREST_AD
}
public class TimeSelector
{
private readonly string[] _timeRefNames;
private double _universalTime;
private readonly TimeReference[] allowedTimeRef;
[Persistent(pass = (int)Pass.GLOBAL)]
private int _currentTimeRef;
public TimeReference TimeReference => allowedTimeRef[_currentTimeRef];
// Input parameters
[UsedImplicitly]
[Persistent(pass = (int)Pass.GLOBAL)]
public readonly EditableTime LeadTime = 0;
[UsedImplicitly]
[Persistent(pass = (int)Pass.GLOBAL)]
public readonly EditableDoubleMult CircularizeAltitude = new EditableDoubleMult(150000, 1000);
public TimeSelector(TimeReference[] allowedTimeRef)
{
this.allowedTimeRef = allowedTimeRef;
_universalTime = 0;
_timeRefNames = new string[allowedTimeRef.Length];
for (int i = 0; i < allowedTimeRef.Length; ++i)
{
switch (allowedTimeRef[i])
{
case TimeReference.COMPUTED:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect1");
break; //at the optimum time
case TimeReference.APOAPSIS:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect2");
break; //"at the next apoapsis"
case TimeReference.CLOSEST_APPROACH:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect3");
break; //"at closest approach to target"
case TimeReference.EQ_ASCENDING:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect4");
break; //"at the equatorial AN"
case TimeReference.EQ_DESCENDING:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect5");
break; //"at the equatorial DN"
case TimeReference.PERIAPSIS:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect6");
break; //"at the next periapsis"
case TimeReference.REL_ASCENDING:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect7");
break; //"at the next AN with the target."
case TimeReference.REL_DESCENDING:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect8");
break; //"at the next DN with the target."
case TimeReference.X_FROM_NOW:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect9");
break; //"after a fixed time"
case TimeReference.ALTITUDE:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect10");
break; //"at an altitude"
case TimeReference.EQ_NEAREST_AD:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect11");
break; //"at the nearest equatorial AN/DN"
case TimeReference.EQ_HIGHEST_AD:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect12");
break; //"at the cheapest equatorial AN/DN"
case TimeReference.REL_NEAREST_AD:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect13");
break; //"at the nearest AN/DN with the target"
case TimeReference.REL_HIGHEST_AD:
_timeRefNames[i] = Localizer.Format("#MechJeb_Maneu_TimeSelect14");
break; //"at the cheapest AN/DN with the target"
}
}
}
public void DoChooseTimeGUI()
{
GUILayout.Label(Localizer.Format("#MechJeb_Maneu_STB")); //Schedule the burn
GUILayout.BeginHorizontal();
_currentTimeRef = GuiUtils.ComboBox.Box(_currentTimeRef, _timeRefNames, this);
switch (TimeReference)
{
// No additional parameters required
case TimeReference.COMPUTED:
case TimeReference.APOAPSIS:
case TimeReference.CLOSEST_APPROACH:
case TimeReference.EQ_ASCENDING:
case TimeReference.EQ_DESCENDING:
case TimeReference.PERIAPSIS:
case TimeReference.REL_ASCENDING:
case TimeReference.REL_DESCENDING:
case TimeReference.EQ_NEAREST_AD:
case TimeReference.EQ_HIGHEST_AD:
case TimeReference.REL_NEAREST_AD:
case TimeReference.REL_HIGHEST_AD:
break;
case TimeReference.X_FROM_NOW:
GuiUtils.SimpleTextBox(Localizer.Format("#MechJeb_of"), LeadTime); //"of"
break;
case TimeReference.ALTITUDE:
GuiUtils.SimpleTextBox(Localizer.Format("#MechJeb_of"), CircularizeAltitude, "km"); //"of"
break;
}
GUILayout.EndHorizontal();
}
public double ComputeManeuverTime(Orbit o, double ut, MechJebModuleTargetController target)
{
switch (allowedTimeRef[_currentTimeRef])
{
case TimeReference.X_FROM_NOW:
ut += LeadTime.val;
break;
case TimeReference.APOAPSIS:
if (o.eccentricity < 1)
{
ut = o.NextApoapsisTime(ut);
}
else
{
throw new OperationException(
Localizer.Format("#MechJeb_Maneu_Exception1")); //"Warning: orbit is hyperbolic, so apoapsis doesn't exist."
}
break;
case TimeReference.PERIAPSIS:
ut = o.NextPeriapsisTime(ut);
break;
case TimeReference.CLOSEST_APPROACH:
if (target.NormalTargetExists)
{
ut = o.NextClosestApproachTime(target.TargetOrbit, ut);
}
else
{
throw new OperationException(Localizer.Format("#MechJeb_Maneu_Exception2")); //"Warning: no target selected."
}
break;
case TimeReference.ALTITUDE:
if (CircularizeAltitude > o.PeA && (CircularizeAltitude < o.ApA || o.eccentricity >= 1))
{
ut = o.NextTimeOfRadius(ut, o.referenceBody.Radius + CircularizeAltitude);
}
else
{
throw
new OperationException(
Localizer.Format(
"#MechJeb_Maneu_Exception3")); //"Warning: can't circularize at this altitude, since current orbit does not reach it."
}
break;
case TimeReference.EQ_ASCENDING:
if (o.AscendingNodeEquatorialExists())
{
ut = o.TimeOfAscendingNodeEquatorial(ut);
}
else
{
throw new OperationException(
Localizer.Format("#MechJeb_Maneu_Exception4")); //"Warning: equatorial ascending node doesn't exist."
}
break;
case TimeReference.EQ_DESCENDING:
if (o.DescendingNodeEquatorialExists())
{
ut = o.TimeOfDescendingNodeEquatorial(ut);
}
else
{
throw new OperationException(
Localizer.Format("#MechJeb_Maneu_Exception5")); //"Warning: equatorial descending node doesn't exist."
}
break;
case TimeReference.EQ_NEAREST_AD:
if (o.AscendingNodeEquatorialExists())
{
ut = o.DescendingNodeEquatorialExists()
? Math.Min(o.TimeOfAscendingNodeEquatorial(ut), o.TimeOfDescendingNodeEquatorial(ut))
: o.TimeOfAscendingNodeEquatorial(ut);
}
else if (o.DescendingNodeEquatorialExists())
{
ut = o.TimeOfDescendingNodeEquatorial(ut);
}
else
{
throw new OperationException(
Localizer.Format("#MechJeb_Maneu_Exception6")); //Warning: neither ascending nor descending node exists.
}
break;
case TimeReference.EQ_HIGHEST_AD:
if (o.AscendingNodeEquatorialExists())
{
if (o.DescendingNodeEquatorialExists())
{
double anTime = o.TimeOfAscendingNodeEquatorial(ut);
double dnTime = o.TimeOfDescendingNodeEquatorial(ut);
ut = o.getOrbitalVelocityAtUT(anTime).magnitude <= o.getOrbitalVelocityAtUT(dnTime).magnitude
? anTime
: dnTime;
}
else
{
ut = o.TimeOfAscendingNodeEquatorial(ut);
}
}
else if (o.DescendingNodeEquatorialExists())
{
ut = o.TimeOfDescendingNodeEquatorial(ut);
}
else
{
throw new OperationException(
Localizer.Format("#MechJeb_Maneu_Exception7")); //"Warning: neither ascending nor descending node exists."
}
break;
}
_universalTime = ut;
return _universalTime;
}
}
}