forked from SmellyBootRash/AirPark
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAirPark.cs
185 lines (157 loc) · 4.68 KB
/
AirPark.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
using System;
using UnityEngine;
namespace AirPark
{
public class AirPark : PartModule
{
[KSPField(isPersistant = true, guiActive = true, guiName = "AirParked")]
Boolean Parked;
[KSPField(isPersistant = true, guiActive = true, guiName = "Auto UnPark")]
Boolean autoPark;
[KSPField(isPersistant = true, guiActive = false)]
private Vector3 ParkPosition = new Vector3(0f, 0f, 0f);
[KSPField(isPersistant = true, guiActive = false)]
private Vector3 ParkVelocity = 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);
[KSPField(isPersistant = true, guiActive = false)]
Vessel.Situations previousState = Vessel.Situations.LANDED;
//have you ever clicked "AirParked"? Rember to keep interesting things from happening
[KSPField(isPersistant = true, guiActive = false)]
public bool isActive = false;
private static Vector3 zeroVector = new Vector3(0f, 0f, 0f);
public override void OnStart(StartState state)
{
if (state != StartState.Editor)
{
InitBaseState();
}
}
private void InitBaseState()
{
if (vessel != null)
{
ParkPosition = vessel.transform.position;
part.force_activate();
}
}
public override void OnSave(ConfigNode node)
{
base.OnSave(node);
if(vessel != null)
{
ParkPosition = GetVesselPostion();
}
}
private void RememberPreviousState()
{
if (!Parked && previousState != Vessel.Situations.LANDED)
{
previousState = vessel.situation;
}
}
public override void OnFixedUpdate()
{
// can't Park if we're orbiting
if (vessel.situation == Vessel.Situations.SUB_ORBITAL || vessel.situation == Vessel.Situations.ORBITING)
{
autoPark = false;
Parked = false;
}
// if we're inactive, and autopark is set
if (!vessel.isActiveVessel && autoPark)
{
// 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))
{
ParkVessel();
}
}
if (Parked)
{
ParkVessel();
}
}
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; }
FreezeVesselInPlace();
//Restore Velocity and Accleration
vessel.SetWorldVelocity(ParkVelocity);
vessel.acceleration = ParkAcceleration;
vessel.angularVelocity = ParkAngularVelocity;
}
[KSPEvent(guiActive = true, guiName = "Toggle Park")]
public void TogglePark()
{
// 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 AutoPark")]
public void ToggleAutoPark()
{
autoPark = !autoPark;
}
private void ParkVessel()
{
RememberPreviousState();
FreezeVesselInPlace();
vessel.situation = Vessel.Situations.LANDED;
vessel.Landed = true;
Parked = true;
}
private void FreezeVesselInPlace()
{
vessel.SetWorldVelocity(zeroVector);
vessel.acceleration = zeroVector;
vessel.angularVelocity = zeroVector;
vessel.geeForce = 0.0;
SetVesselPosition();
}
//Code Adapted from Hyperedit landing functions
//https://github.com/Ezriilc/HyperEdit
private Vector3d GetVesselPostion()
{
double latitude = 0, longitude = 0, altitude = 0;
var pqs = vessel.mainBody.pqsController;
if (pqs == null)
{
Destroy(this);
return zeroVector;
}
altitude = pqs.GetSurfaceHeight(vessel.mainBody.GetRelSurfaceNVector(latitude, longitude)) - vessel.mainBody.Radius;
return vessel.mainBody.GetRelSurfacePosition(latitude, longitude, altitude);
}
private void SetVesselPosition()
{
vessel.orbitDriver.pos = ParkPosition;
}
}
}