/
MechJebModuleLandingGuidance.cs
406 lines (349 loc) · 19.2 KB
/
MechJebModuleLandingGuidance.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using UnityEngine;
using KSP.Localization;
using static MechJebLib.Utils.Statics;
namespace MuMech
{
[UsedImplicitly]
public class MechJebModuleLandingGuidance : DisplayModule
{
public MechJebModuleLandingPredictions predictor;
public static List<LandingSite> landingSites;
[Persistent(pass = (int)(Pass.Global|Pass.Local))]
public int landingSiteIdx = 0;
public struct LandingSite
{
public string name;
public CelestialBody body;
public double latitude;
public double longitude;
}
public override void OnStart(PartModule.StartState state)
{
predictor = core.GetComputerModule<MechJebModuleLandingPredictions>();
if (landingSites == null && HighLogic.LoadedSceneIsFlight)
InitLandingSitesList();
}
public override GUILayoutOption[] WindowOptions()
{
return new GUILayoutOption[] { GUILayout.Width(200), GUILayout.Height(150) };
}
private void moveByMeter(ref EditableAngle angle, double distance, double Alt)
{
double angularDelta = distance * UtilMath.Rad2Deg / (Alt + mainBody.Radius);
angle += angularDelta;
}
protected override void WindowGUI(int windowID)
{
GUILayout.BeginVertical();
if (core.target.PositionTargetExists)
{
var ASL = core.vessel.mainBody.TerrainAltitude(core.target.targetLatitude, core.target.targetLongitude);
GUILayout.Label(Localizer.Format("#MechJeb_LandingGuidance_label1"));//Target coordinates:
GUILayout.BeginHorizontal();
core.target.targetLatitude.DrawEditGUI(EditableAngle.Direction.NS);
if (GUILayout.Button("▲"))
{
moveByMeter(ref core.target.targetLatitude, 10, ASL);
}
GUILayout.Label("10m");
if (GUILayout.Button("▼"))
{
moveByMeter(ref core.target.targetLatitude, -10, ASL);
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
core.target.targetLongitude.DrawEditGUI(EditableAngle.Direction.EW);
if (GUILayout.Button("◄"))
{
moveByMeter(ref core.target.targetLongitude, -10, ASL);
}
GUILayout.Label("10m");
if (GUILayout.Button("►"))
{
moveByMeter(ref core.target.targetLongitude, 10, ASL);
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("ASL: " + ASL.ToSI(-1, 4) + "m");
GUILayout.Label(core.target.targetBody.GetExperimentBiomeSafe(core.target.targetLatitude, core.target.targetLongitude));
GUILayout.EndHorizontal();
}
else
{
if (GUILayout.Button(Localizer.Format("#MechJeb_LandingGuidance_button1")))//Enter target coordinates
{
core.target.SetPositionTarget(mainBody, core.target.targetLatitude, core.target.targetLongitude);
}
}
if (GUILayout.Button(Localizer.Format("#MechJeb_LandingGuidance_button2"))) core.target.PickPositionTargetOnMap();//Pick target on map
List<LandingSite> availableLandingSites = landingSites.Where(p => p.body == mainBody).ToList();
if (availableLandingSites.Any())
{
GUILayout.BeginHorizontal();
landingSiteIdx = GuiUtils.ComboBox.Box(landingSiteIdx, availableLandingSites.Select(p => p.name).ToArray(), this);
if (GUILayout.Button("Set", GUILayout.ExpandWidth(false)))
{
core.target.SetPositionTarget(mainBody, availableLandingSites[landingSiteIdx].latitude, availableLandingSites[landingSiteIdx].longitude);
}
GUILayout.EndHorizontal();
}
DrawGUITogglePredictions();
if (core.landing != null)
{
GUILayout.Label(Localizer.Format("#MechJeb_LandingGuidance_label2"));//Autopilot:
predictor.maxOrbits = core.landing.enabled ? 0.5 : 4;
predictor.noSkipToFreefall = !core.landing.enabled;
if (core.landing.enabled)
{
if (GUILayout.Button(Localizer.Format("#MechJeb_LandingGuidance_button3"))) core.landing.StopLanding();//Abort autoland
}
else
{
GUILayout.BeginHorizontal();
if (!core.target.PositionTargetExists || vessel.LandedOrSplashed) GUI.enabled = false;
if (GUILayout.Button(Localizer.Format("#MechJeb_LandingGuidance_button4"))) core.landing.LandAtPositionTarget(this);//Land at target
GUI.enabled = !vessel.LandedOrSplashed;
if (GUILayout.Button(Localizer.Format("#MechJeb_LandingGuidance_button5"))) core.landing.LandUntargeted(this);//Land somewhere
GUI.enabled = true;
GUILayout.EndHorizontal();
}
GuiUtils.SimpleTextBox(Localizer.Format("#MechJeb_LandingGuidance_label3"), core.landing.touchdownSpeed, "m/s", 35);//Touchdown speed:
if (core.landing != null) core.node.autowarp = GUILayout.Toggle(core.node.autowarp, Localizer.Format("#MechJeb_LandingGuidance_checkbox1"));//Auto-warp
core.landing.deployGears = GUILayout.Toggle(core.landing.deployGears, Localizer.Format("#MechJeb_LandingGuidance_checkbox2"));//Deploy Landing Gear
GuiUtils.SimpleTextBox(Localizer.Format("#MechJeb_LandingGuidance_label4"), core.landing.limitGearsStage, "", 35);//"Stage Limit:"
core.landing.deployChutes = GUILayout.Toggle(core.landing.deployChutes, Localizer.Format("#MechJeb_LandingGuidance_checkbox3"));//Deploy Parachutes
predictor.deployChutes = core.landing.deployChutes;
GuiUtils.SimpleTextBox(Localizer.Format("#MechJeb_LandingGuidance_label5"), core.landing.limitChutesStage, "", 35);//Stage Limit:
predictor.limitChutesStage = core.landing.limitChutesStage;
core.landing.rcsAdjustment = GUILayout.Toggle(core.landing.rcsAdjustment, Localizer.Format("#MechJeb_LandingGuidance_checkbox4"));//Use RCS for small adjustment
if (core.landing.enabled)
{
GUILayout.Label(Localizer.Format("#MechJeb_LandingGuidance_label6") + core.landing.status);//Status:
GUILayout.Label(Localizer.Format("#MechJeb_LandingGuidance_label7") + (core.landing.CurrentStep != null ? core.landing.CurrentStep.GetType().Name : "N/A"));//Step:
GUILayout.Label(Localizer.Format("#MechJeb_LandingGuidance_label8") + (core.landing.descentSpeedPolicy != null ? core.landing.descentSpeedPolicy.GetType().Name : "N/A") + " (" + core.landing.UseAtmosphereToBrake().ToString() + ")");//Mode
//GUILayout.Label("DecEndAlt: " + core.landing.DecelerationEndAltitude().ToString("F2"));
//var dragLength = mainBody.DragLength(core.landing.LandingAltitude, core.landing.vesselAverageDrag, vesselState.mass);
//GUILayout.Label("Drag Length: " + ( dragLength < double.MaxValue ? dragLength.ToString("F2") : "infinite"));
//
//string parachuteInfo = core.landing.ParachuteControlInfo;
//if (null != parachuteInfo)
//{
// GUILayout.Label(parachuteInfo);
//}
}
}
GUILayout.EndVertical();
base.WindowGUI(windowID);
}
public void SetAndLandTargetKSC()
{
var ksc = landingSites.First(x => x.name == "KSC Pad");
core.target.SetPositionTarget(mainBody, ksc.latitude, ksc.longitude);
core.landing.LandAtPositionTarget(this);
}
public void LandSomewhere()
{
core.landing.StopLanding();
core.landing.LandUntargeted(this);
}
[GeneralInfoItem("#MechJeb_LandingPredictions", InfoItem.Category.Misc)]//Landing predictions
void DrawGUITogglePredictions()
{
GUILayout.BeginVertical();
bool active = GUILayout.Toggle(predictor.enabled, Localizer.Format("#MechJeb_LandingGuidance_checkbox5"));//Show landing predictions
if (predictor.enabled != active)
{
if (active)
{
predictor.users.Add(this);
}
else
{
predictor.users.Remove(this);
}
}
if (predictor.enabled)
{
predictor.makeAerobrakeNodes = GUILayout.Toggle(predictor.makeAerobrakeNodes, Localizer.Format("#MechJeb_LandingGuidance_checkbox6"));//"Show aerobrake nodes"
predictor.showTrajectory = GUILayout.Toggle(predictor.showTrajectory, Localizer.Format("#MechJeb_LandingGuidance_checkbox7"));//Show trajectory
predictor.worldTrajectory = GUILayout.Toggle(predictor.worldTrajectory, Localizer.Format("#MechJeb_LandingGuidance_checkbox8"));//World trajectory
predictor.camTrajectory = GUILayout.Toggle(predictor.camTrajectory, Localizer.Format("#MechJeb_LandingGuidance_checkbox9"));//Camera trajectory (WIP)
DrawGUIPrediction();
}
GUILayout.EndVertical();
}
void DrawGUIPrediction()
{
ReentrySimulation.Result result = predictor.Result;
if (result != null)
{
switch (result.Outcome)
{
case ReentrySimulation.Outcome.LANDED:
GUILayout.Label(Localizer.Format("#MechJeb_LandingGuidance_label9"));//Landing Predictions:
GUILayout.Label(Coordinates.ToStringDMS(result.EndPosition.Latitude, result.EndPosition.Longitude) + "\nASL:" + result.EndASL.ToSI(-1, 4) + "m");
GUILayout.Label(result.Body.GetExperimentBiomeSafe(result.EndPosition.Latitude, result.EndPosition.Longitude));
double error = Vector3d.Distance(mainBody.GetWorldSurfacePosition(result.EndPosition.Latitude, result.EndPosition.Longitude, 0) - mainBody.position,
mainBody.GetWorldSurfacePosition(core.target.targetLatitude, core.target.targetLongitude, 0) - mainBody.position);
GUILayout.Label(Localizer.Format("#MechJeb_LandingGuidance_Label10") + error.ToSI(0) + "m"
+ Localizer.Format("#MechJeb_LandingGuidance_Label11") + result.MaxDragGees.ToString("F1") +"g"
+Localizer.Format("#MechJeb_LandingGuidance_Label12") + result.DeltaVExpended.ToString("F1") + "m/s"
+Localizer.Format("#MechJeb_LandingGuidance_Label13") + (vessel.Landed ? "0.0s" : GuiUtils.TimeToDHMS(result.EndUT - Planetarium.GetUniversalTime(), 1)));//Target difference = \nMax drag: \nDelta-v needed: \nTime to land:
break;
case ReentrySimulation.Outcome.AEROBRAKED:
GUILayout.Label(Localizer.Format("#MechJeb_LandingGuidance_Label14"));//Predicted orbit after aerobraking:
Orbit o = result.AeroBrakeOrbit();
if (o.eccentricity > 1) GUILayout.Label(Localizer.Format("#MechJeb_LandingGuidance_Label15") + o.eccentricity.ToString("F2"));//Hyperbolic, eccentricity =
else GUILayout.Label(o.PeA.ToSI(3) + "m x " + o.ApA.ToSI(3) + "m");
GUILayout.Label(Localizer.Format("#MechJeb_LandingGuidance_Label16", result.MaxDragGees.ToString("F1"))+Localizer.Format("#MechJeb_LandingGuidance_Label17", GuiUtils.TimeToDHMS(result.AeroBrakeUT - Planetarium.GetUniversalTime(), 1)));//Max drag:<<1>>g \nExit atmosphere in:
break;
case ReentrySimulation.Outcome.NO_REENTRY:
GUILayout.Label(Localizer.Format("#MechJeb_LandingGuidance_Label18_1")
+ orbit.PeA.ToSI(3) + "m Pe > " + mainBody.RealMaxAtmosphereAltitude().ToSI(3) + (mainBody.atmosphere ? Localizer.Format("#MechJeb_LandingGuidance_Label18_2") : Localizer.Format("#MechJeb_LandingGuidance_Label18_3")));//"Orbit does not reenter:\n""m atmosphere height""m ground"
break;
case ReentrySimulation.Outcome.TIMED_OUT:
GUILayout.Label(Localizer.Format("#MechJeb_LandingGuidance_Label19"));//Reentry simulation timed out.
break;
}
}
}
private void InitLandingSitesList()
{
landingSites = new List<LandingSite>();
// Import landing sites from users createded .cfg
foreach (var mjConf in GameDatabase.Instance.GetConfigs("MechJeb2Landing"))
{
foreach (ConfigNode site in mjConf.config.GetNode("LandingSites").GetNodes("Site"))
{
print("site " + site);
string launchSiteName = site.GetValue("name");
string lat = site.GetValue("latitude");
string lon = site.GetValue("longitude");
if (launchSiteName == null || lat == null || lon == null)
{
print("Ignore landing site with null value");
continue;
}
double.TryParse(lat, out double latitude);
double.TryParse(lon, out double longitude);
string bodyName = site.GetValue("body");
CelestialBody body = bodyName != null ? FlightGlobals.Bodies.Find(b => b.bodyName == bodyName) : Planetarium.fetch.Home;
if (landingSites.All(p => p.name != launchSiteName))
{
print("Adding " + launchSiteName);
landingSites.Add(new LandingSite()
{
name = launchSiteName,
latitude = latitude,
longitude = longitude,
body = body
});
}
}
}
// Import KSP launch sites
foreach (LaunchSite site in PSystemSetup.Instance.LaunchSites)
{
if (site.spawnPoints.Length > 0)
{
LaunchSite.SpawnPoint point = site.spawnPoints[0];
landingSites.Add(new LandingSite()
{
name = point.name.Replace("_", " "),
latitude = point.latitude,
longitude = point.longitude,
body = site.Body
});
}
}
// Import KerbTown/Kerbal-Konstructs launch site
foreach (var config in GameDatabase.Instance.GetConfigs("STATIC"))
{
foreach (ConfigNode instances in config.config.GetNodes("Instances"))
{
string bodyName = instances.GetValue("CelestialBody");
string radialPos = instances.GetValue("RadialPosition");
string launchSiteName = instances.GetValue("LaunchSiteName");
string launchSiteType = instances.GetValue("LaunchSiteType");
if (bodyName == null || radialPos == null || launchSiteName == null || launchSiteType == null ||
launchSiteType != "VAB")
{
continue;
}
Vector3d pos = ConfigNode.ParseVector3D(radialPos).normalized;
CelestialBody body = FlightGlobals.Bodies.Find(b => b.bodyName == bodyName);
double latitude = Math.Asin(pos.y) * UtilMath.Rad2Deg;
double longitude = Math.Atan2(pos.z, pos.x) * UtilMath.Rad2Deg;
if (body != null && landingSites.All(p => p.name != launchSiteName))
{
landingSites.Add(new LandingSite()
{
name = launchSiteName,
latitude = !double.IsNaN(latitude) ? latitude : 0,
longitude = !double.IsNaN(longitude) ? longitude : 0,
body = body
});
}
}
}
// Import RSS Launch sites
UrlDir.UrlConfig rssSites = GameDatabase.Instance.GetConfigs("KSCSWITCHER").FirstOrDefault();
if (rssSites != null)
{
ConfigNode launchSites = rssSites.config.GetNode("LaunchSites");
if (launchSites != null)
{
foreach (ConfigNode site in launchSites.GetNodes("Site"))
{
string launchSiteName = site.GetValue("displayName");
ConfigNode pqsCity = site.GetNode("PQSCity");
if (pqsCity == null)
{
continue;
}
string lat = pqsCity.GetValue("latitude");
string lon = pqsCity.GetValue("longitude");
if (launchSiteName == null || lat == null || lon == null)
{
continue;
}
double latitude, longitude;
double.TryParse(lat, out latitude);
double.TryParse(lon, out longitude);
if (landingSites.All(p => p.name != launchSiteName))
{
landingSites.Add(new LandingSite()
{
name = launchSiteName,
latitude = latitude,
longitude = longitude,
body = Planetarium.fetch.Home
});
}
}
}
}
if (landingSiteIdx > landingSites.Count)
{
landingSiteIdx = 0;
}
}
public override string GetName()
{
return Localizer.Format("#MechJeb_LandingGuidance_title");//Landing Guidance
}
public override string IconName()
{
return "Landing Guidance";
}
public override bool IsSpaceCenterUpgradeUnlocked()
{
return vessel.patchedConicsUnlocked();
}
public MechJebModuleLandingGuidance(MechJebCore core) : base(core) { }
}
}