/
MechJebModuleWarpController.cs
297 lines (248 loc) · 11.3 KB
/
MechJebModuleWarpController.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
extern alias JetBrainsAnnotations;
using System;
using JetBrainsAnnotations::JetBrains.Annotations;
using KSP.Localization;
using UnityEngine;
namespace MuMech
{
public class MechJebModuleWarpController : ComputerModule
{
public MechJebModuleWarpController(MechJebCore core)
: base(core)
{
WarpPaused = false;
Priority = 100;
Enabled = true;
}
private double warpIncreaseAttemptTime;
private int lastAskedIndex;
public double warpToUT { get; private set; }
public bool WarpPaused { get; private set; }
[Persistent(pass = (int)Pass.GLOBAL)]
public bool activateSASOnWarp = true;
[UsedImplicitly]
[Persistent(pass = (int)Pass.GLOBAL)]
public bool useQuickWarp;
public void useQuickWarpInfoItem() =>
useQuickWarp = GUILayout.Toggle(useQuickWarp, Localizer.Format("#MechJeb_WarpHelper_checkbox1")); //"Quick warp"
[GeneralInfoItem("#MechJeb_MJWarpControl", InfoItem.Category.Misc)] //MJ Warp Control
public void ControlWarpButton()
{
if (WarpPaused && GUILayout.Button(Localizer.Format("#MechJeb_WarpHelper_button3"))) //"Resume MJ Warp"
{
ResumeWarp();
}
if (!WarpPaused && GUILayout.Button(Localizer.Format("#MechJeb_WarpHelper_button4"))) //"Pause MJ Warp"
{
PauseWarp();
}
}
public override void OnUpdate()
{
if (!WarpPaused && lastAskedIndex > 0 && lastAskedIndex != TimeWarp.CurrentRateIndex)
{
// Rate limited by the altitude so we should not care
if (!Vessel.LandedOrSplashed && TimeWarp.WarpMode == TimeWarp.Modes.HIGH &&
TimeWarp.CurrentRateIndex == TimeWarp.fetch.GetMaxRateForAltitude(Vessel.altitude, Vessel.mainBody))
return;
//print("Warppause : lastAskedIndex=" + lastAskedIndex + " CurrentRateIndex=" + TimeWarp.CurrentRateIndex + " WarpMode=" + TimeWarp.WarpMode + " MaxCurrentRate=" + TimeWarp.fetch.GetMaxRateForAltitude(vessel.altitude, vessel.mainBody));
WarpPaused = false;
//PauseWarp();
//ScreenMessages.PostScreenMessage("MJ : Warp canceled by user or an other mod");
}
}
public override void OnFixedUpdate()
{
if (warpToUT > 0)
WarpToUT(warpToUT);
}
private void PauseWarp()
{
WarpPaused = true;
if (activateSASOnWarp && TimeWarp.CurrentRateIndex == 0)
Part.vessel.ActionGroups.SetGroup(KSPActionGroup.SAS, false);
}
private void ResumeWarp()
{
if (!WarpPaused)
return;
WarpPaused = false;
SetTimeWarpRate(lastAskedIndex, false);
}
// Turn SAS on during regular warp for compatibility with PersistentRotation
private void SetTimeWarpRate(int rateIndex, bool instant)
{
if (rateIndex != TimeWarp.CurrentRateIndex)
{
if (activateSASOnWarp && TimeWarp.WarpMode == TimeWarp.Modes.HIGH && TimeWarp.CurrentRateIndex == 0)
Part.vessel.ActionGroups.SetGroup(KSPActionGroup.SAS, true);
lastAskedIndex = rateIndex;
if (WarpPaused)
{
ScreenMessages.PostScreenMessage(
Localizer.Format("#MechJeb_WarpHelper_scrmsg")); //"MJ : Warp paused - resume in the Warp Helper menu"
}
else
{
TimeWarp.SetRate(rateIndex, instant);
}
if (activateSASOnWarp && rateIndex == 0)
Part.vessel.ActionGroups.SetGroup(KSPActionGroup.SAS, false);
}
}
public void WarpToUT(double UT, double maxRate = -1)
{
if (UT <= VesselState.time)
{
warpToUT = 0.0;
return;
}
if (maxRate < 0)
maxRate = TimeWarp.fetch.warpRates[TimeWarp.fetch.warpRates.Length - 1];
double desiredRate;
if (useQuickWarp)
{
desiredRate = 1;
if (Orbit.patchEndTransition != Orbit.PatchTransitionType.FINAL && Orbit.EndUT < UT)
{
for (int i = 0; i < TimeWarp.fetch.warpRates.Length; i++)
{
if (i * Time.fixedDeltaTime * TimeWarp.fetch.warpRates[i] <= Orbit.EndUT - VesselState.time)
desiredRate = TimeWarp.fetch.warpRates[i] + 0.1;
else break;
}
}
else
{
for (int i = 0; i < TimeWarp.fetch.warpRates.Length; i++)
{
if (i * Time.fixedDeltaTime * TimeWarp.fetch.warpRates[i] <= UT - VesselState.time)
desiredRate = TimeWarp.fetch.warpRates[i] + 0.1;
else break;
}
}
}
else desiredRate = 1.0 * (UT - (VesselState.time + Time.fixedDeltaTime * TimeWarp.CurrentRateIndex));
desiredRate = MuUtils.Clamp(desiredRate, 1, maxRate);
if (!Vessel.LandedOrSplashed &&
VesselState.altitudeASL < TimeWarp.fetch.GetAltitudeLimit(1, MainBody))
{
//too low to use any regular warp rates. Use physics warp at a max of x2:
WarpPhysicsAtRate((float)Math.Min(desiredRate, 2));
}
else
{
WarpRegularAtRate((float)desiredRate, useQuickWarp, useQuickWarp);
}
warpToUT = UT;
}
//warp at the highest regular warp rate that is <= maxRate
public void WarpRegularAtRate(float maxRate, bool instantOnIncrease = false, bool instantOnDecrease = true)
{
if (!CheckRegularWarp()) return;
if (TimeWarp.fetch.warpRates[TimeWarp.CurrentRateIndex] > maxRate)
{
DecreaseRegularWarp(instantOnDecrease);
}
else if (TimeWarp.CurrentRateIndex + 1 < TimeWarp.fetch.warpRates.Length &&
TimeWarp.fetch.warpRates[TimeWarp.CurrentRateIndex + 1] <= maxRate)
{
IncreaseRegularWarp(instantOnIncrease);
}
}
//warp at the highest regular warp rate that is <= maxRate
public void WarpPhysicsAtRate(float maxRate, bool instantOnIncrease = false, bool instantOnDecrease = true)
{
if (!CheckPhysicsWarp()) return;
if (TimeWarp.fetch.physicsWarpRates[TimeWarp.CurrentRateIndex] > maxRate)
{
DecreasePhysicsWarp(instantOnDecrease);
}
else if (TimeWarp.CurrentRateIndex + 1 < TimeWarp.fetch.physicsWarpRates.Length &&
TimeWarp.fetch.physicsWarpRates[TimeWarp.CurrentRateIndex + 1] <= maxRate)
{
IncreasePhysicsWarp(instantOnIncrease);
}
}
//If the warp mode is regular warp, returns true
//If the warp mode is physics warp, switches it to regular warp and returns false
private bool CheckRegularWarp()
{
if (TimeWarp.WarpMode != TimeWarp.Modes.HIGH)
{
double instantAltitudeASL = (VesselState.CoM - MainBody.position).magnitude - MainBody.Radius;
if (instantAltitudeASL > MainBody.RealMaxAtmosphereAltitude())
{
TimeWarp.fetch.Mode = TimeWarp.Modes.HIGH;
SetTimeWarpRate(0, true);
}
return false;
}
return true;
}
//If the warp mode is physics warp, returns true
//If the warp mode is regular warp, switches it to physics warp and returns false
private bool CheckPhysicsWarp()
{
if (TimeWarp.WarpMode != TimeWarp.Modes.LOW)
{
TimeWarp.fetch.Mode = TimeWarp.Modes.LOW;
SetTimeWarpRate(0, true);
return false;
}
return true;
}
private bool IncreaseRegularWarp(bool instant = false)
{
if (!CheckRegularWarp()) return false; //make sure we are in regular warp
//do a bunch of checks to see if we can increase the warp rate:
if (TimeWarp.CurrentRateIndex + 1 == TimeWarp.fetch.warpRates.Length) return false; //already at max warp
if (!Vessel.LandedOrSplashed)
{
double instantAltitudeASL = (VesselState.CoM - MainBody.position).magnitude - MainBody.Radius;
if (TimeWarp.fetch.GetAltitudeLimit(TimeWarp.CurrentRateIndex + 1, MainBody) > instantAltitudeASL) return false;
//altitude too low to increase warp
}
if (TimeWarp.fetch.warpRates[TimeWarp.CurrentRateIndex] != TimeWarp.CurrentRate)
return false; //most recent warp change is not yet complete
if (VesselState.time - warpIncreaseAttemptTime < 2) return false; //we increased warp too recently
warpIncreaseAttemptTime = VesselState.time;
SetTimeWarpRate(TimeWarp.CurrentRateIndex + 1, instant);
return true;
}
private bool IncreasePhysicsWarp(bool instant = false)
{
if (!CheckPhysicsWarp()) return false; //make sure we are in regular warp
//do a bunch of checks to see if we can increase the warp rate:
if (TimeWarp.CurrentRateIndex + 1 == TimeWarp.fetch.physicsWarpRates.Length) return false; //already at max warp
if (TimeWarp.fetch.physicsWarpRates[TimeWarp.CurrentRateIndex] != TimeWarp.CurrentRate)
return false; //most recent warp change is not yet complete
if (VesselState.time - warpIncreaseAttemptTime < 2) return false; //we increased warp too recently
warpIncreaseAttemptTime = VesselState.time;
SetTimeWarpRate(TimeWarp.CurrentRateIndex + 1, instant);
return true;
}
private bool DecreaseRegularWarp(bool instant = false)
{
if (!CheckRegularWarp()) return false;
if (TimeWarp.CurrentRateIndex == 0) return false; //already at minimum warp
SetTimeWarpRate(TimeWarp.CurrentRateIndex - 1, instant);
return true;
}
private bool DecreasePhysicsWarp(bool instant = false)
{
if (!CheckPhysicsWarp()) return false;
if (TimeWarp.CurrentRateIndex == 0) return false; //already at minimum warp
SetTimeWarpRate(TimeWarp.CurrentRateIndex - 1, instant);
return true;
}
public bool MinimumWarp(bool instant = false)
{
warpToUT = 0.0;
if (TimeWarp.CurrentRateIndex == 0)
return false; //Somehow setting TimeWarp.SetRate to 0 when already at 0 causes unexpected rapid separation (Kraken)
SetTimeWarpRate(0, instant);
return true;
}
}
}