-
Notifications
You must be signed in to change notification settings - Fork 33
/
EVAController.cs
369 lines (328 loc) · 13.8 KB
/
EVAController.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;
namespace KSPAdvancedFlyByWire
{
class EVAController
{
/**
* Current EVA Bugs:
* Interact on ladder gives NullReference if not near a hatch
*/
private const float EVARotationStep = 57.29578f; // From KerbalEVA.UpdateHeading()
private List<FieldInfo> vectorFields;
private List<FieldInfo> floatFields;
private List<FieldInfo> eventFields;
private FieldInfo colliderListField;
private KFSMEventCondition runStopCondition;
private KFSMEventCondition ladderStopCondition;
private KFSMEventCondition swimStopCondition;
private KFSMEventCondition eventConditionDisabled = ((KFSMState s) => false);
private bool m_autoRunning = false;
private static EVAController instance;
public static EVAController Instance
{
get
{
if (instance == null)
{
instance = new EVAController();
}
return instance;
}
}
public EVAController()
{
LoadReflectionFields();
}
public void UpdateEVAFlightProperties(ControllerConfiguration config, FlightCtrlState state)
{
if (!FlightGlobals.ActiveVessel.isEVA)
return;
KerbalEVA eva = GetKerbalEVA();
if (this.m_autoRunning)
{
state.Z = 1f;
}
if (!state.isNeutral)
{
Vector3 moveDirection = Vector3.zero;
Vector3 ladderDirection = Vector3.zero;
//Debug.Log("State: " + eva.fsm.currentStateName + " (< " + eva.fsm.lastEventName + ")");
//Debug.Log("MovementMode: " + eva.CharacterFrameMode);
//Debug.Log("Landed? " + eva.part.GroundContact + ", " + eva.vessel.LandedOrSplashed + ", " + eva.vessel.Landed);
if (state.Y != 0)
{
moveDirection += (!eva.CharacterFrameMode ? eva.fUp : eva.transform.up) * state.Y;
ladderDirection += state.Y > 0 ? eva.transform.up : -eva.transform.up;
}
if (state.X != 0)
{
moveDirection += (!eva.CharacterFrameMode ? eva.fRgt : eva.transform.right) * state.X;
ladderDirection += (state.X > 0 ? eva.transform.right : -eva.transform.right);
}
if (state.Z != 0)
{
moveDirection += (!eva.CharacterFrameMode ? eva.fFwd : eva.transform.forward) * state.Z;
ladderDirection += (state.Z > 0 ? eva.transform.up : -eva.transform.up);
}
if (eva.vessel.LandedOrSplashed && !eva.JetpackDeployed && !eva.OnALadder && !eva.isRagdoll)
{
float moveSpeed = moveDirection.magnitude;
// Decrease max moveSpeed when not moving in a forwards direction.
if (Vector3.Angle(moveDirection, eva.transform.forward) > 45)
{
moveSpeed = Mathf.Clamp(moveSpeed, 0, 0.25f);
}
this.SetMoveSpeed(moveSpeed);
}
// We disable "Run/Ladder/Swim Stop" check while moving.
if (eva.OnALadder && (state.Y != 0 || state.Z != 0))
{
DisableLadderStopCondition(eva);
}
if (eva.vessel.Splashed && (state.X != 0 || state.Z != 0))
{
DisableSwimStopCondition(eva);
}
//Debug.Log("Runspeed: " + moveDirection.magnitude);
moveDirection.Normalize();
//Debug.Log("MoveDirection: " + moveDirection);
//Debug.Log("LadderDirection: "+ ladderDirection);
this.vectorFields[0].SetValue(eva, moveDirection); //vector3_0 = MoveDirection
this.vectorFields[2].SetValue(eva, moveDirection); //vector3_2 = JetpackDirection
this.vectorFields[6].SetValue(eva, ladderDirection); //vector3_6 = LadderDirection
Quaternion rotation = Quaternion.identity;
rotation *= Quaternion.AngleAxis(eva.turnRate * state.pitch * EVARotationStep * Time.deltaTime, -eva.transform.right);
rotation *= Quaternion.AngleAxis(eva.turnRate * state.yaw * EVARotationStep * Time.deltaTime, eva.transform.up);
rotation *= Quaternion.AngleAxis(eva.turnRate * state.roll * EVARotationStep * Time.deltaTime, -eva.transform.forward);
if (rotation != Quaternion.identity)
{
this.vectorFields[8].SetValue(eva, rotation * (Vector3)this.vectorFields[8].GetValue(eva));
this.vectorFields[13].SetValue(eva, rotation * (Vector3)this.vectorFields[13].GetValue(eva));
}
if (!moveDirection.IsZero() && !eva.OnALadder)
{
if (eva.CharacterFrameMode)
{
this.vectorFields[8].SetValue(eva, eva.fFwd); //vector3_8
this.vectorFields[13].SetValue(eva, eva.fUp); //vector3_13
}
else
{
this.vectorFields[8].SetValue(eva, moveDirection); //vector3_8
this.vectorFields[13].SetValue(eva, eva.fUp); //vector3_13
}
}
}
else
{
ReEnableStopConditions(eva);
}
}
public void DoInteract()
{
if (FlightGlobals.ActiveVessel.isEVA)
{
KerbalEVA eva = GetKerbalEVA();
List<Collider> colliders = this.GetEVAColliders(eva);
if (colliders.Count > 0)
{
if (eva.OnALadder)
{
try
{
eva.fsm.RunEvent((KFSMEvent) this.eventFields[34].GetValue(eva)); // Board Vessel
}
catch (Exception)
{
return;
}
}
else
{
try
{
eva.fsm.RunEvent((KFSMEvent) this.eventFields[22].GetValue(eva)); // Grab Ladder
}
catch (Exception)
{
return;
}
}
}
}
else
{
GoEVA();
}
}
public void DoJump()
{
if (!FlightGlobals.ActiveVessel.isEVA)
return;
KerbalEVA eva = GetKerbalEVA();
if (eva.OnALadder)
{
eva.fsm.RunEvent((KFSMEvent)this.eventFields[27].GetValue(eva)); // Ladder Let Go
}
else
{
eva.fsm.RunEvent((KFSMEvent)this.eventFields[9].GetValue(eva)); // Jump Start
}
}
public void DoPlantFlag()
{
if (!FlightGlobals.ActiveVessel.isEVA)
return;
KerbalEVA eva = GetKerbalEVA();
if (eva.part.GroundContact)
eva.PlantFlag();
}
public void ToggleJetpack()
{
if (!FlightGlobals.ActiveVessel.isEVA)
return;
KerbalEVA eva = GetKerbalEVA();
KFSMEvent togglePackEvent = (KFSMEvent)eventFields[17].GetValue(eva);
togglePackEvent.GoToStateOnEvent = eva.fsm.CurrentState;
eva.fsm.RunEvent(togglePackEvent);
}
public void SetMoveSpeed(float runSpeed)
{
if (!FlightGlobals.ActiveVessel.isEVA)
return;
KerbalEVA eva = GetKerbalEVA();
if (IsVesselActive(eva.vessel)
&& !eva.JetpackDeployed && !eva.OnALadder && !eva.isRagdoll && !eva.vessel.Splashed)
{
//Debug.Log("RunSpeed is: " + runSpeed);
if (runSpeed > 0.75f)
{
eva.fsm.RunEvent((KFSMEvent)eventFields[2].GetValue(eva)); // Start Run
DisableRunStopCondition(eva);
}
else
{
eva.fsm.RunEvent((KFSMEvent)eventFields[3].GetValue(eva)); // Stop Run
}
floatFields[6].SetValue(eva, runSpeed * eva.runSpeed);
}
}
public void ToggleHeadlamp()
{
if (!FlightGlobals.ActiveVessel.isEVA)
return;
KerbalEVA eva = GetKerbalEVA();
eva.headLamp.SetActive(!eva.lampOn);
eva.lampOn = !eva.lampOn;
}
public void ToggleAutorun()
{
this.m_autoRunning = !this.m_autoRunning;
ScreenMessages.PostScreenMessage("AutoRun: " + (this.m_autoRunning ? "ON" : "OFF"), 2f, ScreenMessageStyle.LOWER_CENTER);
}
public void GoEVA()
{
if (HighLogic.CurrentGame.Parameters.Flight.CanEVA &&
CameraManager.Instance.currentCameraMode == CameraManager.CameraMode.IVA)
{
foreach (ProtoCrewMember pcm in FlightGlobals.ActiveVessel.GetVesselCrew())
{
if (pcm.KerbalRef.eyeTransform == InternalCamera.Instance.transform.parent)
{
CameraManager.Instance.SetCameraFlight();
//Delay EVA spawn until next FixedUpdate. Prevents push to vessel.
FlightEVA.fetch.StartCoroutine(GoEVADelayed(pcm.KerbalRef));
break;
}
}
}
}
// PRIVATE METHODS //
private IEnumerator GoEVADelayed(Kerbal kerbal)
{
yield return new WaitForFixedUpdate();
FlightEVA.SpawnEVA(kerbal);
}
private void DisableRunStopCondition(KerbalEVA eva)
{
if (this.runStopCondition == null)
{
//Debug.Log("Disabling RunStop");
KFSMEvent eRunStop = (KFSMEvent)eventFields[3].GetValue(eva); // End Run
this.runStopCondition = eRunStop.OnCheckCondition;
eRunStop.OnCheckCondition = this.eventConditionDisabled;
}
}
private void DisableLadderStopCondition(KerbalEVA eva)
{
if (this.ladderStopCondition == null)
{
//Debug.Log("Disabling LadderStop");
KFSMEvent eLadderStop = (KFSMEvent)eventFields[26].GetValue(eva); // Ladder Stop
this.ladderStopCondition = eLadderStop.OnCheckCondition;
eLadderStop.OnCheckCondition = this.eventConditionDisabled;
}
}
private void DisableSwimStopCondition(KerbalEVA eva)
{
if (this.swimStopCondition == null)
{
//Debug.Log("Disabling SwimStop");
KFSMEvent eSwimStop = (KFSMEvent)eventFields[21].GetValue(eva); // Swim Stop
this.swimStopCondition = eSwimStop.OnCheckCondition;
eSwimStop.OnCheckCondition = this.eventConditionDisabled;
}
}
private void ReEnableStopConditions(KerbalEVA eva)
{
if (this.ladderStopCondition != null)
{
//Debug.Log("Re-enable LadderStop");
KFSMEvent eLadderStop = (KFSMEvent)eventFields[26].GetValue(eva);
eLadderStop.OnCheckCondition = this.ladderStopCondition;
this.ladderStopCondition = null;
}
if (this.swimStopCondition != null)
{
//Debug.Log("Re-enable SwimStop");
KFSMEvent eSwimStop = (KFSMEvent)eventFields[21].GetValue(eva);
eSwimStop.OnCheckCondition = this.swimStopCondition;
this.swimStopCondition = null;
}
if (this.runStopCondition != null)
{
//Debug.Log("Re-enable RunStop");
KFSMEvent eRunStop = (KFSMEvent)eventFields[3].GetValue(eva);
eRunStop.OnCheckCondition = this.runStopCondition;
this.runStopCondition = null;
}
}
private List<Collider> GetEVAColliders(KerbalEVA eva)
{
return (List<Collider>)this.colliderListField.GetValue(eva);
}
private bool IsVesselActive(Vessel vessel)
{
return vessel.state == Vessel.State.ACTIVE && !vessel.packed;
}
private KerbalEVA GetKerbalEVA()
{
return FlightGlobals.ActiveVessel.GetComponent<KerbalEVA>();
}
private void LoadReflectionFields()
{
List<FieldInfo> fields = new List<FieldInfo>(typeof(KerbalEVA).GetFields(
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance));
this.vectorFields = new List<FieldInfo>(fields.Where<FieldInfo>(f => f.FieldType.Equals(typeof(Vector3))));
this.floatFields = new List<FieldInfo>(fields.Where<FieldInfo>(f => f.FieldType.Equals(typeof(float))));
this.eventFields = new List<FieldInfo>(fields.Where<FieldInfo>(f => f.FieldType.Equals(typeof(KFSMEvent))));
this.colliderListField = new List<FieldInfo>(fields.Where<FieldInfo>(f => f.FieldType.Equals(typeof(List<Collider>))))[0];
}
}
}