/
MechJebModuleDockingGuidance.cs
153 lines (121 loc) · 6.97 KB
/
MechJebModuleDockingGuidance.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
using System.Linq;
using JetBrains.Annotations;
using KSP.Localization;
using UnityEngine;
namespace MuMech
{
[UsedImplicitly]
public class MechJebModuleDockingGuidance : DisplayModule
{
public MechJebModuleDockingGuidance(MechJebCore core) : base(core) { }
private MechJebModuleDockingAutopilot autopilot;
public override void OnStart(PartModule.StartState state) => autopilot = Core.GetComputerModule<MechJebModuleDockingAutopilot>();
protected override void WindowGUI(int windowID)
{
if (!Core.Target.NormalTargetExists)
{
GUILayout.Label(Localizer.Format("#MechJeb_Docking_label1")); //"Choose a target to dock with"
base.WindowGUI(windowID);
return;
}
GUILayout.BeginVertical();
// GetReferenceTransformPart is null after undocking ...
if (Vessel.GetReferenceTransformPart() == null || !Vessel.GetReferenceTransformPart().Modules.Contains("ModuleDockingNode"))
{
GUILayout.Label(Localizer.Format("#MechJeb_Docking_label2"),
GuiUtils.yellowLabel); //Warning: You need to control the vessel from a docking port. Right click a docking port and select "Control from here"
}
if (!(Core.Target.Target is ModuleDockingNode))
{
GUILayout.Label(Localizer.Format("#MechJeb_Docking_label3"),
GuiUtils.yellowLabel); //Warning: target is not a docking port. Right click the target docking port and select "Set as target"
}
bool onAxisNodeExists = false;
foreach (ITargetable node in Vessel.GetTargetables()
.Where(t => t.GetTargetingMode() == VesselTargetModes.DirectionVelocityAndOrientation))
{
if (Vector3d.Angle(node.GetTransform().forward, Vessel.ReferenceTransform.up) < 2)
{
onAxisNodeExists = true;
break;
}
}
if (!onAxisNodeExists)
{
GUILayout.Label(Localizer.Format("#MechJeb_Docking_label4"),
GuiUtils.yellowLabel); //Warning: this vessel is not controlled from a docking node. Right click the desired docking node on this vessel and select "Control from here."
}
bool active = GUILayout.Toggle(autopilot.Enabled, Localizer.Format("#MechJeb_Docking_checkbox1")); // "Autopilot enabled"
GuiUtils.SimpleTextBox(Localizer.Format("#MechJeb_Docking_label5"), autopilot.speedLimit, "m/s"); //"Speed limit"
autopilot.overrideSafeDistance =
GUILayout.Toggle(autopilot.overrideSafeDistance, Localizer.Format("#MechJeb_Docking_checkbox2")); //"Override Safe Distance"
if (autopilot.overrideSafeDistance)
GuiUtils.SimpleTextBox(Localizer.Format("#MechJeb_Docking_checkbox3"), autopilot.overridenSafeDistance, "m"); //"Safe Distance"
autopilot.overrideTargetSize =
GUILayout.Toggle(autopilot.overrideTargetSize, Localizer.Format("#MechJeb_Docking_checkbox4")); //"Override Start Distance"
if (autopilot.overrideTargetSize)
GuiUtils.SimpleTextBox(Localizer.Format("#MechJeb_Docking_label6"), autopilot.overridenTargetSize, "m"); //"Start Distance"
if (autopilot.overridenSafeDistance < 0)
autopilot.overridenSafeDistance = 0;
if (autopilot.overridenTargetSize < 10)
autopilot.overridenTargetSize = 10;
autopilot.drawBoundingBox =
GUILayout.Toggle(autopilot.drawBoundingBox, Localizer.Format("#MechJeb_Docking_checkbox5")); //"Draw Bounding Box"
if (GUILayout.Button(Localizer.Format("#MechJeb_Docking_button"))) //"Dump Bounding Box Info"
{
Vessel.GetBoundingBox(true);
if (Core.Target.Target != null)
{
Vessel targetVessel = Core.Target.Target.GetVessel();
targetVessel.GetBoundingBox(true);
}
}
GUILayout.Label(Localizer.Format("#MechJeb_Docking_label7", autopilot.safeDistance.ToString("F2")),
GUILayout.ExpandWidth(false)); //"safeDistance "
GUILayout.Label(Localizer.Format("#MechJeb_Docking_label8", autopilot.targetSize.ToString("F2")),
GUILayout.ExpandWidth(false)); //"targetSize "
if (autopilot.speedLimit < 0)
autopilot.speedLimit = 0;
GUILayout.BeginHorizontal();
autopilot.forceRol =
GUILayout.Toggle(autopilot.forceRol, Localizer.Format("#MechJeb_Docking_checkbox6"), GUILayout.ExpandWidth(false)); //"Force Roll :"
autopilot.rol.text = GUILayout.TextField(autopilot.rol.text, GUILayout.Width(30));
GUILayout.Label("°", GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
if (autopilot.Enabled != active)
{
if (active)
{
autopilot.Users.Add(this);
}
else
{
autopilot.Users.Remove(this);
}
}
if (autopilot.Enabled)
{
GUILayout.Label(Localizer.Format("#MechJeb_Docking_label9", autopilot.status)); //"Status: <<1>>"
Vector3d error = Core.RCS.targetVelocity - VesselState.orbitalVelocity;
double error_x = Vector3d.Dot(error, Vessel.GetTransform().right);
double error_y = Vector3d.Dot(error, Vessel.GetTransform().forward);
double error_z = Vector3d.Dot(error, Vessel.GetTransform().up);
GUILayout.Label(Localizer.Format("#MechJeb_Docking_label10", error_x.ToString("F2")) + " m/s [L/J]"); //Error X: <<1>>
GUILayout.Label(Localizer.Format("#MechJeb_Docking_label11", error_y.ToString("F2")) + " m/s [I/K]"); //Error Y: <<1>>
GUILayout.Label(Localizer.Format("#MechJeb_Docking_label12", error_z.ToString("F2")) + " m/s [H/N]"); //Error Z: <<1>>
GUILayout.Label(Localizer.Format("#MechJeb_Docking_label13", autopilot.zSep.ToString("F2")) + "m"); //Distance Dock: <<1>>
GUILayout.Label(Localizer.Format("#MechJeb_Docking_label14", autopilot.lateralSep.magnitude.ToString("F2")) +
"m"); //Distance Dock Axis: <<1>>
}
GUILayout.EndVertical();
base.WindowGUI(windowID);
}
public override GUILayoutOption[] WindowOptions() => new[] { GUILayout.Width(300), GUILayout.Height(50) };
protected override void OnModuleDisabled()
{
if (autopilot != null) autopilot.Users.Remove(this);
}
public override string GetName() => Localizer.Format("#MechJeb_Docking_title"); //"Docking Autopilot"
public override string IconName() => "Docking Autopilot";
}
}