/
MechJebModuleManeuverPlanner.cs
198 lines (161 loc) · 7.52 KB
/
MechJebModuleManeuverPlanner.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
extern alias JetBrainsAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrainsAnnotations::JetBrains.Annotations;
using KSP.Localization;
using UnityEngine;
namespace MuMech
{
public class MechJebModuleManeuverPlanner : DisplayModule
{
public MechJebModuleManeuverPlanner(MechJebCore core) : base(core) { }
// Keep all Operation objects so parameters are saved
private static readonly Operation[] _operation = Operation.GetAvailableOperations();
private readonly string[] _operationNames =
new List<Operation>(_operation).ConvertAll(x => x.GetName()).ToArray();
[UsedImplicitly]
[Persistent(pass = (int)Pass.GLOBAL)]
public int _operationId;
// Creation or replacement mode
private bool _createNode = true;
protected override void WindowGUI(int windowID)
{
_operationId = Mathf.Clamp(_operationId, 0, _operation.Length - 1);
GUILayout.BeginVertical();
List<ManeuverNode> maneuverNodes = GetManeuverNodes();
bool anyNodeExists = GetManeuverNodes().Any();
if (anyNodeExists)
{
GUILayout.BeginHorizontal();
if (GUILayout.Button(_createNode
? Localizer.Format("#MechJeb_Maneu_createNodeBtn01")
: Localizer.Format("#MechJeb_Maneu_createNodeBtn02"))) //"Create a new":"Change the last"
{
_createNode = !_createNode;
}
GUILayout.Label(Localizer.Format("#MechJeb_Maneu_createlab1")); //"maneuver node to:"
GUILayout.EndHorizontal();
}
else
{
GUILayout.Label(Localizer.Format("#MechJeb_Maneu_createlab2")); //"Create a new maneuver node to:"
_createNode = true;
}
_operationId = GuiUtils.ComboBox.Box(_operationId, _operationNames, this);
// Compute orbit and universal time parameters for next maneuver
double UT = VesselState.time;
Orbit o = Orbit;
if (anyNodeExists)
{
if (_createNode)
{
ManeuverNode last = maneuverNodes.Last();
UT = last.UT;
o = last.nextPatch;
}
else if (maneuverNodes.Count > 1)
{
ManeuverNode last = maneuverNodes[maneuverNodes.Count - 1];
UT = last.UT;
o = last.nextPatch;
}
}
try
{
_operation[_operationId].DoParametersGUI(o, UT, Core.Target);
}
catch (Exception) { } // TODO: Would be better to fix the problem but this will do for now
if (anyNodeExists)
GUILayout.Label(Localizer.Format("#MechJeb_Maneu_createlab3")); //"after the last maneuver node."
bool makingNode = false;
bool executingNode = false;
GUILayout.BeginHorizontal();
if (GUILayout.Button(Localizer.Format("#MechJeb_Maneu_button1"))) //"Create node"
{
makingNode = true;
}
if (Core.Node != null && GUILayout.Button(Localizer.Format("#MechJeb_Maneu_button2"))) //"Create and execute"
{
makingNode = true;
executingNode = true;
}
GUILayout.EndHorizontal();
if (makingNode)
{
List<ManeuverParameters> nodeList = _operation[_operationId].MakeNodes(o, UT, Core.Target);
if (nodeList != null)
{
if (!_createNode)
maneuverNodes.Last().RemoveSelf();
for (int i = 0; i < nodeList.Count; i++)
{
Vessel.PlaceManeuverNode(o, nodeList[i].dV, nodeList[i].UT);
}
}
if (executingNode && Core.Node != null)
Core.Node.ExecuteOneNode(this);
}
if (_operation[_operationId].GetErrorMessage().Length > 0)
{
GUILayout.Label(_operation[_operationId].GetErrorMessage(), GuiUtils.YellowLabel);
}
if (GUILayout.Button(Localizer.Format("#MechJeb_Maneu_button3"))) //Remove ALL nodes
{
Vessel.RemoveAllManeuverNodes();
}
if (Core.Node != null)
{
if (anyNodeExists && !Core.Node.Enabled)
{
if (GUILayout.Button(Localizer.Format("#MechJeb_Maneu_button4"))) //Execute next node
{
Core.Node.ExecuteOneNode(this);
}
if (Vessel.patchedConicSolver.maneuverNodes.Count > 1 || VesselState.isLoadedPrincipia)
{
if (GUILayout.Button(Localizer.Format("#MechJeb_Maneu_button5"))) //Execute all nodes
{
Core.Node.ExecuteAllNodes(this);
}
}
}
else if (Core.Node.Enabled)
{
if (GUILayout.Button(Localizer.Format("#MechJeb_Maneu_button6"))) //Abort node execution
{
Core.Node.Abort();
}
}
GUILayout.BeginHorizontal();
Core.Node.Autowarp =
GUILayout.Toggle(Core.Node.Autowarp, Localizer.Format("#MechJeb_Maneu_Autowarp"), GUILayout.ExpandWidth(true)); //"Auto-warp"
Core.Node.RCSOnly = GUILayout.Toggle(Core.Node.RCSOnly, "RCS Burn", GUILayout.ExpandWidth(true));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label(Localizer.Format("#MechJeb_Maneu_Lead_time"), GUILayout.ExpandWidth(false)); //Lead time:
Core.Node.LeadTime.Text = GUILayout.TextField(Core.Node.LeadTime.Text, GUILayout.Width(35), GUILayout.ExpandWidth(false));
GUILayout.Label("s", GUILayout.ExpandWidth(false));
if (GUILayout.Button("+", GUILayout.ExpandWidth(false)))
Core.Node.LeadTime.Val += 1;
if (GUILayout.Button("-", GUILayout.ExpandWidth(false)))
Core.Node.LeadTime.Val -= 1;
if (GUILayout.Button("R", GUILayout.ExpandWidth(false)))
Core.Node.LeadTime.Val = 3;
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
base.WindowGUI(windowID, _operation[_operationId].Draggable);
}
public List<ManeuverNode> GetManeuverNodes()
{
MechJebModuleLandingPredictions predictor = Core.GetComputerModule<MechJebModuleLandingPredictions>();
if (predictor == null) return Vessel.patchedConicSolver.maneuverNodes;
return Vessel.patchedConicSolver.maneuverNodes.Where(n => n != predictor.aerobrakeNode).ToList();
}
protected override GUILayoutOption[] WindowOptions() => new[] { GUILayout.Width(300), GUILayout.Height(150) };
public override string GetName() => Localizer.Format("#MechJeb_Maneuver_Planner_title"); //Maneuver Planner
public override string IconName() => "Maneuver Planner";
protected override bool IsSpaceCenterUpgradeUnlocked() => Vessel.patchedConicsUnlocked();
}
}