-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWarpableEngine.cs
More file actions
336 lines (305 loc) · 11.1 KB
/
Copy pathWarpableEngine.cs
File metadata and controls
336 lines (305 loc) · 11.1 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using UnityEngine;
namespace NBody
{
[KSPAddon(KSPAddon.Startup.Flight, true)]
public class WarpableEngineThrottleGUI : MonoBehaviour
{
public static WarpableEngineThrottleGUI s_singleton = null;
public static WarpableEngineThrottleGUI GetInstance() { return s_singleton; }
public IButton btnWarpableEngineList = null;
public bool activated = true;
public bool killThrottleWhenExitingTimeWarp = true;
private Rect windowRect = new Rect(60, 120, 200, 50);
private List<WarpableEngine> engines = new List<WarpableEngine>();
public void Awake()
{
Debug.Log("NBody Awake()");
if (s_singleton == null)
s_singleton = this;
DontDestroyOnLoad(s_singleton);
if (ToolbarManager.ToolbarAvailable)
{
btnWarpableEngineList = ToolbarManager.Instance.add("NBody", "warpableenginelist");
btnWarpableEngineList.TexturePath = "NBody/Textures/WarpableEngineListOn";
btnWarpableEngineList.ToolTip = "WarpableEngine List";
btnWarpableEngineList.OnClick += (e) =>
{
activated = !activated;
btnWarpableEngineList.TexturePath = activated ? "NBody/Textures/WarpableEngineListOn" : "NBody/Textures/WarpableEngineListOff";
OrbitManipulator.s_singleton.SaveConfigs();
};
}
engines.Clear();
}
public void OnDestroy()
{
if (btnWarpableEngineList != null)
btnWarpableEngineList.Destroy();
engines.Clear();
}
void OnGUI()
{
if (HighLogic.LoadedSceneIsFlight && activated && engines.Count > 0)
{
GUI.skin = HighLogic.Skin;
windowRect = GUILayout.Window(22113141, windowRect, DrawGUI, "Warpable Engines", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
}
}
void Update()
{
if (InputLockManager.IsLocked(ControlTypes.THROTTLE))
{
List<string> locksToBeRemoved = new List<string>();
foreach (KeyValuePair<string, ulong> kv in InputLockManager.lockStack)
{
//Debug.Log(kv.Key + ": " + Convert.ToString((long)kv.Value, 2));
//if ((kv.Value & (uint)(ControlTypes.THROTTLE)) != 0)
// locksToBeRemoved.Add(kv.Key);
}
locksToBeRemoved.Add("TimeWarpLock");
foreach(string lockName in locksToBeRemoved)
InputLockManager.RemoveControlLock(lockName);
}
if(FlightGlobals.fetch != null && FlightGlobals.fetch.activeVessel != null)
{
foreach (Part p in FlightGlobals.fetch.activeVessel.Parts)
{
if (p.Modules.Contains("WarpableEngine"))
{
WarpableEngine we = p.Modules["WarpableEngine"] as WarpableEngine;
if (!engines.Contains(we))
{
engines.Add(we);
}
}
}
for (int i = 0; i < engines.Count; ++i)
{
if (engines[i] == null || engines[i].vessel != FlightGlobals.fetch.activeVessel)
{
engines.RemoveAt(i);
i--;
}
}
}
}
private void DrawGUI(int windowId)
{
GUILayout.BeginVertical();
bool orgKillThrottleWhenExitingTimeWarp = killThrottleWhenExitingTimeWarp;
killThrottleWhenExitingTimeWarp = GUILayout.Toggle(killThrottleWhenExitingTimeWarp, "Auto Cut-Off", GUI.skin.button);
if (killThrottleWhenExitingTimeWarp != orgKillThrottleWhenExitingTimeWarp)
OrbitManipulator.s_singleton.SaveConfigs();
GUILayout.BeginHorizontal();
bool killThrottle = GUILayout.Button(" Cut-Off ");
bool fullThrottle = GUILayout.Button("MaxThrust");
GUILayout.EndHorizontal();
foreach (WarpableEngine we in engines)
{
string title = we.part.partInfo.title;
if (title.Length >= 33)
title = title.Substring(0, 29) + "...";
GUILayout.Label(title, GUILayout.MaxWidth(200));
we.throttle = Mathf.RoundToInt(GUILayout.HorizontalSlider(we.throttle, 0.0f, 100.0f, GUILayout.Width(200)));
if (killThrottle) we.throttle = 0.0f;
if (fullThrottle) we.throttle = 100.0f;
}
GUILayout.EndVertical();
GUI.DragWindow(new Rect(0, 0, 200, 20));
}
}
public class WarpableEngine : PartModule
{
// This is a class used to allow low-thrust engines to work during time-warp.
[KSPField(isPersistant = true, guiName = "Throttle", guiActive = true, guiActiveEditor = false),
UI_FloatRange(controlEnabled = true, maxValue = 100, minValue = 0, scene = UI_Scene.Flight, stepIncrement = 1)]
public float throttle = 0f;
[KSPField(isPersistant = false)]
public int engineIndex = 0;
private ModuleEngines engine = null;
private ModuleEnginesFX engineFX = null;
private bool isEngineFX = false;
bool lastUpdateIsWarping = false;
float lastThrottle = 0f;
public override void OnStart(StartState state)
{
BindEngine();
}
private void BindEngine()
{
engine = null;
engineFX = null;
int index = 0;
foreach(PartModule pm in part.Modules)
{
if (pm is ModuleEngines)
{
if (index == engineIndex)
{
engine = pm as ModuleEngines;
isEngineFX = false;
return;
}
else
index++;
}
if (pm is ModuleEnginesFX)
{
if (index == engineIndex)
{
engineFX = pm as ModuleEnginesFX;
isEngineFX = true;
return;
}
else
index++;
}
}
}
static Vector3d FixedUpdateFor(WarpableEngine warpableEngine, float throttle)
{
// Need to calculate resource consumption, calculate thrust and finally apply the acceleration.
Vector3d finalAcc = new Vector3d();
if (warpableEngine.isEngineFX == false && warpableEngine.engine != null)
{
warpableEngine.engine.requestedThrottle = warpableEngine.engine.currentThrottle = throttle / 100.0f;
warpableEngine.engine.GetType().GetMethod("UpdatePropellantStatus", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(warpableEngine.engine, null);
warpableEngine.engine.finalThrust = warpableEngine.engine.CalculateThrust();
try
{
if (warpableEngine.engine.flameout || warpableEngine.engine.engineShutdown)
{
warpableEngine.engine.DeactivatePowerFX();
warpableEngine.engine.DeactivateRunningFX();
warpableEngine.engine.finalThrust = 0.0f;
}
else
{
warpableEngine.engine.ActivatePowerFX();
warpableEngine.engine.ActivateRunningFX();
warpableEngine.engine.part.findFxGroup("power").SetPower(warpableEngine.engine.finalThrust / warpableEngine.engine.maxThrust);
warpableEngine.engine.part.findFxGroup("running").SetPower(warpableEngine.engine.finalThrust / warpableEngine.engine.maxThrust);
if (warpableEngine.engine.part.Modules.Contains("FXModuleAnimateThrottle"))
{
FXModuleAnimateThrottle m = warpableEngine.engine.part.Modules["FXModuleAnimateThrottle"] as FXModuleAnimateThrottle;
//m.isEnabled = false;
if (m.animation.IsPlaying(m.animationName) == false)
m.animation.Play(m.animationName);
m.animation[m.animationName].normalizedTime = throttle;
m.gameObject.SampleAnimation(m.animation[m.animationName].clip, throttle / m.animation[m.animationName].clip.length);
}
}
}
catch (Exception e)
{ }
Vector3d averageThrustVector = new Vector3d();
foreach (Transform tf in warpableEngine.engine.thrustTransforms)
averageThrustVector += tf.forward * -1;
averageThrustVector /= warpableEngine.engine.thrustTransforms.Count;
finalAcc = averageThrustVector.normalized * warpableEngine.engine.finalThrust / warpableEngine.vessel.GetTotalMass();
return finalAcc;
}
else if (warpableEngine.isEngineFX == true && warpableEngine.engineFX != null)
{
warpableEngine.engineFX.requestedThrottle = warpableEngine.engineFX.currentThrottle = throttle / 100.0f;
warpableEngine.engineFX.GetType().GetMethod("UpdatePropellantStatus", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(warpableEngine.engineFX, null);
warpableEngine.engineFX.finalThrust = warpableEngine.engineFX.CalculateThrust();
try
{
if (warpableEngine.engineFX.flameout || warpableEngine.engineFX.engineShutdown)
{
warpableEngine.engineFX.part.Effect(warpableEngine.engineFX.powerEffectName, 0.0f);
warpableEngine.engineFX.part.Effect(warpableEngine.engineFX.runningEffectName, 0.0f);
warpableEngine.engineFX.finalThrust = 0.0f;
}
else
{
warpableEngine.engineFX.part.Effect(warpableEngine.engineFX.powerEffectName, warpableEngine.engineFX.finalThrust / warpableEngine.engineFX.maxThrust);
warpableEngine.engineFX.part.Effect(warpableEngine.engineFX.runningEffectName, warpableEngine.engineFX.finalThrust / warpableEngine.engineFX.maxThrust);
if (warpableEngine.engineFX.part.Modules.Contains("FXModuleAnimateThrottle"))
{
FXModuleAnimateThrottle m = warpableEngine.engineFX.part.Modules["FXModuleAnimateThrottle"] as FXModuleAnimateThrottle;
//m.isEnabled = false;
if (m.animation.IsPlaying(m.animationName) == false)
m.animation.Play(m.animationName);
m.animation[m.animationName].normalizedTime = throttle;
m.gameObject.SampleAnimation(m.animation[m.animationName].clip, throttle / m.animation[m.animationName].clip.length);
}
}
}
catch (Exception e)
{ }
Vector3d averageThrustVector = new Vector3d();
foreach (Transform tf in warpableEngine.engineFX.thrustTransforms)
averageThrustVector += tf.forward * -1;
averageThrustVector /= warpableEngine.engineFX.thrustTransforms.Count;
finalAcc = averageThrustVector.normalized * warpableEngine.engineFX.finalThrust / warpableEngine.vessel.GetTotalMass();
return finalAcc;
}
else
{
return Vector3d.zero;
}
}
void FixedUpdate()
{
if (HighLogic.LoadedSceneIsFlight == false) return;
if (throttle != lastThrottle)
{
foreach(Part p in this.part.symmetryCounterparts)
{
if (p == this.part) continue;
if (p.Modules.Contains("WarpableEngine"))
{
WarpableEngine we = p.Modules["WarpableEngine"] as WarpableEngine;
we.lastThrottle = we.throttle = throttle;
}
}
lastThrottle = throttle;
}
if (TimeWarp.WarpMode == TimeWarp.Modes.HIGH && TimeWarp.CurrentRate > TimeWarp.MaxPhysicsRate)
{
// Warping.
Vector3d totalAccOnVessel = WarpableEngine.FixedUpdateFor(this, this.throttle);
if (totalAccOnVessel.magnitude > 0.0001)
{
if (OrbitManipulator.s_singleton != null)
OrbitManipulator.s_singleton.AddManipulation(vessel, new Vector3d(totalAccOnVessel.x, totalAccOnVessel.z, totalAccOnVessel.y));
}
lastUpdateIsWarping = true;
}
else
{
// Reset the throttle after exiting the warp.
if (lastUpdateIsWarping == true)
{
if (WarpableEngineThrottleGUI.s_singleton.killThrottleWhenExitingTimeWarp)
throttle = 0f;
//engine.DeactivatePowerFX();
}
lastUpdateIsWarping = false;
//if(isEngineFX == false)
//{
// if (engine.part.Modules.Contains("FXModuleAnimateThrottle"))
// {
// FXModuleAnimateThrottle m = engine.part.Modules["FXModuleAnimateThrottle"] as FXModuleAnimateThrottle;
// m.isEnabled = true;
// }
//}
//else
//{
// if (engineFX.part.Modules.Contains("FXModuleAnimateThrottle"))
// {
// FXModuleAnimateThrottle m = engineFX.part.Modules["FXModuleAnimateThrottle"] as FXModuleAnimateThrottle;
// m.isEnabled = true;
// }
//}
}
}
}
}