/
MechJebModuleWarpHelper.cs
211 lines (171 loc) · 8.46 KB
/
MechJebModuleWarpHelper.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
using System;
using System.Linq;
using JetBrains.Annotations;
using KSP.Localization;
using UnityEngine;
namespace MuMech
{
[UsedImplicitly]
public class MechJebModuleWarpHelper : DisplayModule
{
public enum WarpTarget { Periapsis, Apoapsis, Node, SoI, Time, PhaseAngleT, SuicideBurn, AtmosphericEntry }
private static readonly string[] warpTargetStrings =
{
Localizer.Format("#MechJeb_WarpHelper_Combobox_text1"), Localizer.Format("#MechJeb_WarpHelper_Combobox_text2"),
Localizer.Format("#MechJeb_WarpHelper_Combobox_text3"), Localizer.Format("#MechJeb_WarpHelper_Combobox_text4"),
Localizer.Format("#MechJeb_WarpHelper_Combobox_text5"), Localizer.Format("#MechJeb_WarpHelper_Combobox_text6"),
Localizer.Format("#MechJeb_WarpHelper_Combobox_text7"), Localizer.Format("#MechJeb_WarpHelper_Combobox_text8")
}; //"periapsis""apoapsis""maneuver node""SoI transition""Time""Phase angle""suicide burn""atmospheric entry"
[Persistent(pass = (int)Pass.GLOBAL)]
public WarpTarget warpTarget = WarpTarget.Periapsis;
[Persistent(pass = (int)Pass.GLOBAL)]
public readonly EditableTime leadTime = 0;
public bool warping;
private readonly EditableTime timeOffset = 0;
private double targetUT;
[UsedImplicitly]
[Persistent(pass = (int)(Pass.LOCAL | Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble phaseAngle = 0;
protected override void WindowGUI(int windowID)
{
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUILayout.Label(Localizer.Format("#MechJeb_WarpHelper_label1"), GUILayout.ExpandWidth(false)); //"Warp to: "
warpTarget = (WarpTarget)GuiUtils.ComboBox.Box((int)warpTarget, warpTargetStrings, this);
GUILayout.EndHorizontal();
if (warpTarget == WarpTarget.Time)
{
GUILayout.BeginHorizontal();
GUILayout.Label(Localizer.Format("#MechJeb_WarpHelper_label2"), GUILayout.ExpandWidth(true)); //"Warp for: "
timeOffset.Text = GUILayout.TextField(timeOffset.Text, GUILayout.Width(100));
GUILayout.EndHorizontal();
}
else if (warpTarget == WarpTarget.PhaseAngleT)
{
// I wonder if I should check for target that don't make sense
if (!Core.Target.NormalTargetExists)
GUILayout.Label(Localizer.Format("#MechJeb_WarpHelper_label3")); //"You need a target"
else
GuiUtils.SimpleTextBox(Localizer.Format("#MechJeb_WarpHelper_label4"), phaseAngle, "º", 60); //"Phase Angle:"
}
GUILayout.BeginHorizontal();
GuiUtils.SimpleTextBox(Localizer.Format("#MechJeb_WarpHelper_label5"), leadTime, ""); //"Lead time: "
if (warping)
{
if (GUILayout.Button(Localizer.Format("#MechJeb_WarpHelper_button1"))) //"Abort"
{
warping = false;
Core.Warp.MinimumWarp(true);
}
}
else
{
if (GUILayout.Button(Localizer.Format("#MechJeb_WarpHelper_button2"))) //"Warp"
{
warping = true;
switch (warpTarget)
{
case WarpTarget.Periapsis:
targetUT = Orbit.NextPeriapsisTime(VesselState.time);
break;
case WarpTarget.Apoapsis:
if (Orbit.eccentricity < 1) targetUT = Orbit.NextApoapsisTime(VesselState.time);
break;
case WarpTarget.SoI:
if (Orbit.patchEndTransition != Orbit.PatchTransitionType.FINAL) targetUT = Orbit.EndUT;
break;
case WarpTarget.Node:
if (Vessel.patchedConicsUnlocked() && Vessel.patchedConicSolver.maneuverNodes.Any())
targetUT = Vessel.patchedConicSolver.maneuverNodes[0].UT;
break;
case WarpTarget.Time:
targetUT = VesselState.time + timeOffset;
break;
case WarpTarget.PhaseAngleT:
if (Core.Target.NormalTargetExists)
{
Orbit reference;
if (Core.Target.TargetOrbit.referenceBody == Orbit.referenceBody)
reference = Orbit; // we orbit arround the same body
else
reference = Orbit.referenceBody.orbit;
// From Kerbal Alarm Clock
double angleChangePerSec = 360 / Core.Target.TargetOrbit.period - 360 / reference.period;
double currentAngle = reference.PhaseAngle(Core.Target.TargetOrbit, VesselState.time);
double angleDigff = currentAngle - phaseAngle;
if (angleDigff > 0 && angleChangePerSec > 0)
angleDigff -= 360;
if (angleDigff < 0 && angleChangePerSec < 0)
angleDigff += 360;
double TimeToTarget = Math.Floor(Math.Abs(angleDigff / angleChangePerSec));
targetUT = VesselState.time + TimeToTarget;
}
break;
case WarpTarget.AtmosphericEntry:
try
{
targetUT = Vessel.orbit.NextTimeOfRadius(VesselState.time,
VesselState.mainBody.Radius + VesselState.mainBody.RealMaxAtmosphereAltitude());
}
catch
{
warping = false;
}
break;
case WarpTarget.SuicideBurn:
try
{
targetUT = OrbitExtensions.SuicideBurnCountdown(Orbit, VesselState, Vessel) + VesselState.time;
}
catch
{
warping = false;
}
break;
default:
targetUT = VesselState.time;
break;
}
}
}
GUILayout.EndHorizontal();
Core.Warp.useQuickWarpInfoItem();
if (warping)
GUILayout.Label(Localizer.Format("#MechJeb_WarpHelper_label6") + (leadTime > 0 ? GuiUtils.TimeToDHMS(leadTime) + " before " : "") +
warpTargetStrings[(int)warpTarget] + "."); //"Warping to "
Core.Warp.ControlWarpButton();
GUILayout.EndVertical();
base.WindowGUI(windowID);
}
public override void OnFixedUpdate()
{
if (!warping) return;
if (warpTarget == WarpTarget.SuicideBurn)
{
try
{
targetUT = OrbitExtensions.SuicideBurnCountdown(Orbit, VesselState, Vessel) + VesselState.time;
}
catch
{
warping = false;
}
}
double target = targetUT - leadTime;
if (target < VesselState.time + 1)
{
Core.Warp.MinimumWarp(true);
warping = false;
}
else
{
Core.Warp.WarpToUT(target);
}
}
protected override GUILayoutOption[] WindowOptions() => new[] { GUILayout.Width(240), GUILayout.Height(50) };
public override bool IsActive() => warping;
public override string GetName() => Localizer.Format("#MechJeb_WarpHelper_title"); //"Warp Helper"
public override string IconName() => "Warp Helper";
public MechJebModuleWarpHelper(MechJebCore core) : base(core) { }
}
}