forked from dunclaw/AirPark
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAirParkPartModule.cs
More file actions
executable file
·290 lines (244 loc) · 10 KB
/
AirParkPartModule.cs
File metadata and controls
executable file
·290 lines (244 loc) · 10 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
using System;
using UnityEngine;
namespace AirPark
{
public class AirPark : PartModule
{
static AirPark instance;
public static AirPark Instance => instance;
#region Fields / Globals
[KSPField(isPersistant = true, guiActive = true, guiName = "AirParked")]
public static Boolean Parked;
[KSPField(isPersistant = true, guiActive = true, guiName = "Auto UnPark")]
public static Boolean autoPark;
//Velocity and Postion
[KSPField(isPersistant = true, guiActive = false)]
//private Vector3 ParkPosition = new Vector3(0f, 0f, 0f);
private Vector3 ParkPosition;
[KSPField(isPersistant = true, guiActive = false)]
Vector3 ParkVelocity = new Vector3(0f, 0f, 0f);
private static Vector3 zeroVector = new Vector3(0f, 0f, 0f);
[KSPField(isPersistant = true, guiActive = false)]
private Vector3 ParkAcceleration = new Vector3(0f, 0f, 0f);
[KSPField(isPersistant = true, guiActive = false)]
private Vector3 ParkAngularVelocity = new Vector3(0f, 0f, 0f);
//Vessel State
[KSPField(isPersistant = true, guiActive = true)] //flip to false guiactive on release
Vessel.Situations previousState;
[KSPField(isPersistant = true, guiActive = false)]
//have you ever clicked "AirParked"? Rember to keep interesting things from happening
public bool isActive = false;
#region Debug Fields
[KSPField(isPersistant = true)]
public bool partDebug = true;
[KSPField(guiActive = true, isPersistant = false, guiName = "Current Situation")]
public string vesselSituation;
#endregion DebugFields
#endregion
#region Toggles
[KSPEvent(guiActive = true, guiName = "Toggle Park")]
public void TogglePark_Event()
{
TogglePark();
}
[KSPAction("Toggle Park on/off")]
public void TogglePart_AG(KSPActionParam param)
{
TogglePark();
}
public void TogglePark()
{
//if (!FlightGlobals.ActiveVessel || !(vessel.id == FlightGlobals.ActiveVessel.id)) { return; }
//if (vessel == null || vessel == FlightGlobals.ActiveVessel) { return; }
if (vessel == null | !vessel.isActiveVessel) { return; }
// cannot Park in orbit or sub-orbit
if (vessel.situation != Vessel.Situations.SUB_ORBITAL && vessel.situation != Vessel.Situations.ORBITING)
{
if (!Parked)
{
ParkPosition = GetVesselPostion();
//we only want to remember the initial velocity, not subseqent updates by onFixedUpdate()
ParkVelocity = vessel.GetSrfVelocity();
ParkAcceleration = vessel.acceleration;
ParkAngularVelocity = vessel.angularVelocity;
ParkVessel();
}
else
{
RestoreVesselState();
}
isActive = true;
}
}
[KSPEvent(guiActive = true, guiName = "Toggle Auto UnPark")] //auto park on will awake the vessel and set Parked = false if closer than 1.5 KM and inactive
public void ToggleAutoPark()
{
autoPark = !autoPark;
}
#endregion
#region GameEvents
public override void OnStart(StartState state)
{
if (state != StartState.Editor && vessel != null)
{
part.force_activate();
ParkPosition = vessel.transform.position;
instance = this;
}
}
public override void OnSave(ConfigNode node)
{
base.OnSave(node);
if (vessel != null)
{
ParkPosition = GetVesselPostion();
}
}
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
if (vessel != null)
{
//ParkPosition = vessel.GetWorldPos3D();
//ParkPosition = vessel.transform.position;
//ParkVelocity = vessel.GetSrfVelocity();
//ParkAcceleration = vessel.acceleration;
//ParkAngularVelocity = vessel.angularVelocity;
}
}
public void FixedUpdate()
{
//if (vessel == null || vessel == FlightGlobals.ActiveVessel) { return; }
//try
//{
if (!HighLogic.LoadedSceneIsFlight) { return; }
if (vessel == null | !vessel.isActiveVessel) { return; }
//}
//catch (Exception e)
//{ }
vesselSituation = vessel.situation.ToString();
#region can't Park if we're orbitingParkPosition
if (vessel.situation == Vessel.Situations.SUB_ORBITAL || vessel.situation == Vessel.Situations.ORBITING)
{
autoPark = false;
Parked = false;
ScreenMessages.PostScreenMessage("Cannot Park While Sub-Orbital or Orbital", 5.0f, ScreenMessageStyle.UPPER_CENTER);
}
#endregion
#region If we are the Inactive Vessel and AutoPark is set
if (!vessel.isActiveVessel & autoPark)
{
//ParkPosition = vessel.GetWorldPos3D();
// if we're less than 1.5km from the active vessel and Parked, then wake up
if ((vessel.GetWorldPos3D() - FlightGlobals.ActiveVessel.GetWorldPos3D()).magnitude < 1500.0f & Parked)
{
vessel.GoOffRails();
RestoreVesselState();
}
// if we're farther than 2km, auto Park if needed
if ((vessel.GetWorldPos3D() - FlightGlobals.ActiveVessel.GetWorldPos3D()).magnitude > 2000.0f & Parked == false)
{
ParkVessel();
}
}
#endregion
#region if we're not Parked, and not active and flying, then go off rails
// I dont think it matters if we are flying when I remember previous state - gomker
//if (!vessel.isActiveVessel & Parked==false & vessel.situation == Vessel.Situations.FLYING)
//{
// vessel.GoOffRails();
// RestoreVesselState();
//}
#endregion
//If Parked is True, Park the Vessel
if (Parked)
{
ParkVessel();
}
}
public void OnDestroy()
{
instance = null;
}
#endregion
#region vessel states
private void RememberPreviousState()
{
if (!Parked & vessel.situation != Vessel.Situations.LANDED) //Keep from Vessel Situation from Sticking to Landed permanently
{
previousState = vessel.situation;
}
}
private void RestoreVesselState()
{
if (isActive == false) { return; } //we only want to restore the state if you have parked somewhere intentionally
vessel.situation = previousState;
if (vessel.situation != Vessel.Situations.LANDED) { vessel.Landed = false; }
if (Parked) { Parked = false; }
setVesselStill();
//Restore Velocity and Accleration
vessel.IgnoreGForces(240);
vessel.SetWorldVelocity(ParkVelocity);
vessel.acceleration = ParkAcceleration;
vessel.angularVelocity = ParkAngularVelocity;
}
private void ParkVessel()
{
RememberPreviousState();
setVesselStill();
vessel.situation = Vessel.Situations.LANDED;
vessel.Landed = true;
Parked = true;
}
private void setVesselStill()
{
vessel.IgnoreGForces(240);
vessel.SetWorldVelocity(zeroVector);
vessel.acceleration = zeroVector;
vessel.angularVelocity = zeroVector;
vessel.geeForce = 0.0;
setVesselPosition();
}
#endregion
#region Postion
//Code Adapted from Hyperedit landing functions
//https://github.com/Ezriilc/HyperEdit
public CelestialBody Body { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public double Altitude { get; set; }
public double alt;
public Vector3d teleportPosition;
public void SetAltitudeToCurrent()
{
var pqs = Body.pqsController;
if (pqs == null)
{
Destroy(this);
return;
}
var alt = pqs.GetSurfaceHeight(QuaternionD.AngleAxis(Longitude, Vector3d.down) * QuaternionD.AngleAxis(Latitude, Vector3d.forward) * Vector3d.right) - pqs.radius;
//alt = Math.Max(alt, 0); // No need for underwater check, allow park subs
Altitude = GetComponent<Vessel>().altitude - alt;
}
private Vector3d GetVesselPostion()
{
var pqs = vessel.mainBody.pqsController;
if (pqs == null)
{
Destroy(this);
return zeroVector;
}
alt = pqs.GetSurfaceHeight(vessel.mainBody.GetRelSurfaceNVector(Latitude, Longitude)) - vessel.mainBody.Radius;
alt = Math.Max(alt, 0); // Underwater!
teleportPosition = vessel.mainBody.GetRelSurfacePosition(Latitude, Longitude, alt + Altitude);
return teleportPosition;
}
private void setVesselPosition()
{
vessel.IgnoreGForces(240);
vessel.orbitDriver.pos = ParkPosition;
}
#endregion
}
}