/
MechJebModuleSpaceplaneGuidance.cs
123 lines (101 loc) · 5.44 KB
/
MechJebModuleSpaceplaneGuidance.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
using System;
using System.Linq;
using JetBrains.Annotations;
using KSP.Localization;
using UnityEngine;
using static MechJebLib.Utils.Statics;
namespace MuMech
{
[UsedImplicitly]
public class MechJebModuleSpaceplaneGuidance : DisplayModule
{
private MechJebModuleSpaceplaneAutopilot autoland;
protected bool _showLandingTarget;
[Persistent(pass = (int)(Pass.Global | Pass.Local))]
public int runwayIndex;
public bool showLandingTarget
{
get => _showLandingTarget;
set
{
if (value && !_showLandingTarget) core.Target.SetDirectionTarget("ILS Guidance");
if (!value && core.Target.Target is DirectionTarget && core.Target.Name == "ILS Guidance") core.Target.Unset();
_showLandingTarget = value;
}
}
protected override void WindowGUI(int windowID)
{
GUILayout.BeginVertical();
var availableRunways = MechJebModuleSpaceplaneAutopilot.runways.Where(p => p.body == mainBody).ToList();
if (runwayIndex > availableRunways.Count)
runwayIndex = 0;
if (availableRunways.Any())
{
GUILayout.Label(Localizer.Format("#MechJeb_ApproAndLand_label1"), GuiUtils.middleCenterLabel); //Landing
runwayIndex = GuiUtils.ComboBox.Box(runwayIndex, availableRunways.Select(p => p.name).ToArray(), this);
autoland.runway = availableRunways[runwayIndex];
GUILayout.Label(Localizer.Format("#MechJeb_ApproAndLand_label2") +
Vector3d.Distance(vesselState.CoM, autoland.runway.Start()).ToSI() + "m"); //Distance to runway:
showLandingTarget =
GUILayout.Toggle(showLandingTarget, Localizer.Format("#MechJeb_ApproAndLand_label3")); //Show landing navball guidance
if (GUILayout.Button(Localizer.Format("#MechJeb_ApproAndLan_button1"))) //Autoland
autoland.Autoland(this);
if (autoland.enabled && GUILayout.Button(Localizer.Format("#MechJeb_ApproAndLan_button2"))) //Abort
autoland.AutopilotOff();
GuiUtils.SimpleTextBox(Localizer.Format("#MechJeb_ApproAndLand_label14"), autoland.glideslope, "°"); //Autoland glideslope:
GuiUtils.SimpleTextBox(Localizer.Format("#MechJeb_ApproAndLand_label4"), autoland.approachSpeed, "m/s"); //Approach speed:
GuiUtils.SimpleTextBox(Localizer.Format("#MechJeb_ApproAndLand_label5"), autoland.touchdownSpeed, "m/s"); //Touchdown speed:
autoland.bEngageReverseIfAvailable =
GUILayout.Toggle(autoland.bEngageReverseIfAvailable,
Localizer.Format("#MechJeb_ApproAndLand_label6")); //Reverse thrust upon touchdown
autoland.bBreakAsSoonAsLanded =
GUILayout.Toggle(autoland.bBreakAsSoonAsLanded, Localizer.Format("#MechJeb_ApproAndLand_label7")); //Brake as soon as landed
if (autoland.enabled)
{
GUILayout.Label(Localizer.Format("#MechJeb_ApproAndLand_label8") +
autoland.AutolandApproachStateToHumanReadableDescription()); //State:
GUILayout.Label(Localizer.Format("#MechJeb_ApproAndLand_label9",
Math.Round(autoland.GetAutolandLateralDistanceToNextWaypoint(), 0))); //Distance to waypoint: {0}m
GUILayout.Label(Localizer.Format("#MechJeb_ApproAndLand_label10",
Math.Round(autoland.Autopilot.SpeedTarget, 1))); //Target speed: {0} m/s
GUILayout.Label(Localizer.Format("#MechJeb_ApproAndLand_label11",
Math.Round(autoland.GetAutolandTargetAltitude(autoland.GetAutolandTargetVector()), 0))); //Target altitude: {0} m
GUILayout.Label(Localizer.Format("#MechJeb_ApproAndLand_label12",
Math.Round(autoland.Autopilot.VertSpeedTarget, 1))); //Target vertical speed: {0} m/s
GUILayout.Label(Localizer.Format("#MechJeb_ApproAndLand_label13",
Math.Round(autoland.Autopilot.HeadingTarget, 0))); //Target heading: {0}º
}
}
GUILayout.EndVertical();
base.WindowGUI(windowID);
}
public override GUILayoutOption[] WindowOptions()
{
return new[] { GUILayout.Width(350), GUILayout.Height(200) };
}
public override void OnFixedUpdate()
{
if (showLandingTarget && autoland != null)
{
if (!(core.Target.Target is DirectionTarget && core.Target.Name == "ILS Guidance")) showLandingTarget = false;
else
{
core.Target.UpdateDirectionTarget(autoland.GetAutolandTargetVector());
}
}
}
public override void OnStart(PartModule.StartState state)
{
autoland = core.GetComputerModule<MechJebModuleSpaceplaneAutopilot>();
}
public MechJebModuleSpaceplaneGuidance(MechJebCore core) : base(core) { }
public override string GetName()
{
return Localizer.Format("#MechJeb_ApproAndLand_title"); //Aircraft Approach & Autoland
}
public override string IconName()
{
return "Aircraft Approach & Autoland";
}
}
}