/
LHFlight.cs
387 lines (337 loc) · 16.7 KB
/
LHFlight.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace LandingHeight
{
public static class LHFlightData
{
public static int lhGUImodeStatic; //1 auto, hidden text, 2 auto green text, 3 red ASL, 4 red AGL
}
[KSPScenario(ScenarioCreationOptions.AddToAllGames,GameScenes.FLIGHT)]
public class LHScenario : ScenarioModule
{
public override void OnLoad(ConfigNode node)
{
try
{
LHFlightData.lhGUImodeStatic = int.Parse(node.GetValue("GUIdisplay"));
}
catch
{
LHFlightData.lhGUImodeStatic = 1;
}
if(LHFlightData.lhGUImodeStatic <1 || LHFlightData.lhGUImodeStatic > 4)
{
LHFlightData.lhGUImodeStatic = 1;
}
//Debug.Log("LH Scen " + LHFlightData.lhGUImodeStatic);
}
public override void OnSave(ConfigNode node)
{
if(node.HasValue("GUIdisplay"))
{
node.RemoveValue("GUIdisplay");
}
node.AddValue("GUIdisplay", LHFlightData.lhGUImodeStatic.ToString());
}
}
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class LHFlight : MonoBehaviour
{
private KSP.UI.Screens.Flight.AltitudeTumbler _tumbler; //stock altitude tumbler object
public TMPro.TextMeshProUGUI tumblerASLtext; //ASL/AGL text object
RectTransform lhGUIrect; //supporting component
GameObject lhGUIgo; //master gameObject for ASL/AGL text
Button lhBtn; //button for clickage
FlightGlobals.SpeedDisplayModes spdDisp;
public void Start()
{
Debug.Log("Landing Height v2.1 start.");
}
public void lhButtonClick()
{
switch(LHFlightData.lhGUImodeStatic)
{
case 1:
{
LHFlightData.lhGUImodeStatic = 2;
SetGUITextMode();
break;
}
case 2:
{
LHFlightData.lhGUImodeStatic = 3;
SetGUITextMode();
break;
}
case 3:
{
LHFlightData.lhGUImodeStatic = 4;
SetGUITextMode();
break;
}
case 4:
{
LHFlightData.lhGUImodeStatic = 1;
SetGUITextMode();
break;
}
default:
{
LHFlightData.lhGUImodeStatic = 1;
SetGUITextMode();
break;
}
}
}
public void LateUpdate() //modify UI in late update or KSP default overrides afaik
{
try
{
if (_tumbler == null || _tumbler.tumbler == null)
{
_tumbler = UnityEngine.Object.FindObjectOfType<KSP.UI.Screens.Flight.AltitudeTumbler>();
lhBtn = _tumbler.gameObject.AddComponent<Button>();
lhBtn.onClick.AddListener(() => { lhButtonClick(); });
AddText();
}
if (FlightGlobals.speedDisplayMode == FlightGlobals.SpeedDisplayModes.Surface && LHFlightData.lhGUImodeStatic == 1 || FlightGlobals.speedDisplayMode == FlightGlobals.SpeedDisplayModes.Surface && LHFlightData.lhGUImodeStatic == 2 || LHFlightData.lhGUImodeStatic == 4) //only override if in surface mode
{
_tumbler.tumbler.SetValue(heightToLand());
}
if(spdDisp != FlightGlobals.speedDisplayMode) //how we detect mouse click on speed display mode.
{
SetGUITextMode();
}
}
catch
{
//no tumbler object found, we hit this on scene change, silently fail
}
}
public void AddText()
{
lhGUIgo = new GameObject("LHGUI");
lhGUIrect = lhGUIgo.AddComponent<RectTransform>();
lhGUIrect.SetParent(lhGUIgo.transform, false);
tumblerASLtext = lhGUIgo.AddComponent<TMPro.TextMeshProUGUI>();
lhGUIgo.transform.SetParent(_tumbler.transform.GetChild(0).gameObject.GetComponent<RectTransform>());
tumblerASLtext.transform.localPosition = new Vector3(.5f, -.8f, 0); //CRITICAL, set this because defaults block tumbler number rendering
tumblerASLtext.alignment = TMPro.TextAlignmentOptions.Center;
tumblerASLtext.fontSize = 14;
tumblerASLtext.fontStyle = TMPro.FontStyles.Normal;
tumblerASLtext.font = Resources.Load("Fonts/Arial SDF", typeof(TMPro.TMP_FontAsset)) as TMPro.TMP_FontAsset;
SetGUITextMode();
}
public void SetGUITextMode()
{
switch (LHFlightData.lhGUImodeStatic)
{
case 1:
{
tumblerASLtext.text = "";
break;
}
case 2:
{
if (FlightGlobals.speedDisplayMode == FlightGlobals.SpeedDisplayModes.Surface) //only override if in surface mode
{
tumblerASLtext.text = "AGL";
}
else
{
tumblerASLtext.text = "ASL";
}
tumblerASLtext.color = Color.green;
break;
}
case 3:
{
tumblerASLtext.text = "ASL";
tumblerASLtext.color = Color.red;
break;
}
case 4:
{
tumblerASLtext.text = "AGL";
tumblerASLtext.color = Color.red;
break;
}
}
spdDisp = FlightGlobals.speedDisplayMode;
}
public class partDist //part's distace to CelestialBody CoM for distance sort
{
public Part prt;
public float dist;
}
public double doubleMax(double val1, double val2)
{
if (val1 > val2)
{
return val1;
}
else
{
return val2;
}
}
public double heightToLand() //leave public so other mods can call
{
double landHeight = 0;
double landHeightBackup = 0;
bool firstRay = true;
if (FlightGlobals.ActiveVessel.LandedOrSplashed) //if landed or splashed, height is 0
{
landHeight = 0;
landHeightBackup = 0;
//Debug.Log("LH-A");
}
else if ((FlightGlobals.ActiveVessel.mainBody.ocean && (FlightGlobals.ActiveVessel.altitude - doubleMax(FlightGlobals.ActiveVessel.pqsAltitude,0)) > 2400) || (!FlightGlobals.ActiveVessel.mainBody.ocean && (FlightGlobals.ActiveVessel.altitude - FlightGlobals.ActiveVessel.pqsAltitude) > 2400)) //raycast goes wierd outside physics range
{
//Debug.Log("LH " + FlightGlobals.ActiveVessel.altitude + "|" + FlightGlobals.ActiveVessel.pqsAltitude + "|" + FlightGlobals.ActiveVessel.mainBody.Radius);
if (FlightGlobals.ActiveVessel.mainBody.ocean)
{
landHeight = FlightGlobals.ActiveVessel.altitude - doubleMax(FlightGlobals.ActiveVessel.pqsAltitude,0); //more then 2400 above ground, just use vessel CoM
landHeightBackup = FlightGlobals.ActiveVessel.altitude - doubleMax(FlightGlobals.ActiveVessel.pqsAltitude,0); //more then 2400 above ground, just use vessel CoM
}
else
{
landHeight = FlightGlobals.ActiveVessel.altitude - FlightGlobals.ActiveVessel.pqsAltitude; //more then 2400 above ground, just use vessel CoM
landHeightBackup = FlightGlobals.ActiveVessel.altitude - FlightGlobals.ActiveVessel.pqsAltitude; //more then 2400 above ground, just use vessel CoM
}
//Debug.Log("LH-B");
}
else //inside physics range, goto raycast
{
//Debug.Log("LH-c");
List<Part> partToRay = new List<Part>(); //list of parts to ray
if (FlightGlobals.ActiveVessel.Parts.Count < 50) //if less then 50 parts, just raycast all parts
{
partToRay = FlightGlobals.ActiveVessel.Parts;
}
else //if over 50 parts, only raycast the 30 parts closest to ground. difference between 30 and 50 parts to make the sort worth the processing cost, no point in running the sort on 31 parts as the sort costs more processor power then 1 raycast
{
List<partDist> partHeights = new List<partDist>(); //only used above 50 parts, links part to distance to ground
foreach (Part p in FlightGlobals.ActiveVessel.Parts)
{
partHeights.Add(new partDist() { prt = p, dist = Vector3.Distance(p.transform.position, FlightGlobals.ActiveVessel.mainBody.position) }); //create list of parts and their distance to ground
//print("a: " + Vector3.Distance(p.transform.position, FlightGlobals.ActiveVessel.mainBody.position));
}
partHeights.Sort((i, j) => i.dist.CompareTo(j.dist)); //sort parts so parts closest to ground are at top of list
for (int i = 0; i < 30; i = i + 1)
{
partToRay.Add(partHeights[i].prt); //make list of 30 parts closest to ground
//print("b: " + i + " " + partHeights[i].prt.name + " " + partHeights[i].dist + " " + Vector3.Distance(FlightGlobals.ActiveVessel.CoM, FlightGlobals.ActiveVessel.mainBody.position));
}
}
////test code START
//GameObject go = pHit.collider.gameObject;
//Debug.Log("LH Hit GO " + go.layer + "|" + go.name + "|" + go.tag +"|"+go.GetType()+"|"+pHit.distance);
//if(go.GetComponent<PQ>() != null)
//{
// Debug.Log("LH Hit PQS!");
//}
//else
//{
// Debug.Log("LH Hit something else");
//}
//foreach (Component co in go.GetComponents<UnityEngine.Object>())
//{
// Debug.Log("LH Co in Go hit " + co.name + "|" + co.tag + "|" + co.GetType());
//}
//foreach (Part cop in go.GetComponentsInParent<Part>())
//{
// Debug.Log("LH part hit " + FlightGlobals.ActiveVessel.Parts.Contains(cop));
//}
//test code END
//Debug.Log("LH Alt " + FlightGlobals.ActiveVessel.pqsAltitude + "|" + FlightGlobals.ActiveVessel.altitude);
//Debug.Log("LH START!");
// Debug.Log("LH distances" + FlightGlobals.ActiveVessel.mainBody.Radius + "|" + FlightGlobals.ActiveVessel.PQSAltitude());
foreach (Part p in partToRay)
{
try
{
if (p.collider.enabled) //only check part if it has collider enabled
{
Vector3 partEdge = p.collider.ClosestPointOnBounds(FlightGlobals.currentMainBody.position); //find collider edge closest to ground
RaycastHit pHit;
Vector3 rayDir = (FlightGlobals.currentMainBody.position - partEdge).normalized;
Ray pRayDown = new Ray(partEdge, rayDir);
LayerMask testMask = 32769; //hit layer 0 for parts and layer 15 for groud/buildings
// Debug.Log("LH test mask " + testMask.ToString());
//LayerMask testMask = 32769;
//if (Physics.Raycast(pRayDown, out pHit, (float)(FlightGlobals.ActiveVessel.mainBody.Radius + FlightGlobals.ActiveVessel.altitude), pRayMask)) //cast ray
if (Physics.Raycast(pRayDown, out pHit, (float)(FlightGlobals.ActiveVessel.mainBody.Radius + FlightGlobals.ActiveVessel.altitude), testMask)) //cast ray
{
// Debug.Log("LH hit dist orig " + pHit.distance);
if (pHit.collider.gameObject.layer == 15 || pHit.collider.gameObject.layer == 0 && !FlightGlobals.ActiveVessel.parts.Contains(pHit.collider.gameObject.GetComponentInParent<Part>()))
{//valid hit, accept all layer 15 hits but only those on layer 0 when part hit is not part of ActiveVessel
float hitDist = pHit.distance; //need to do math so make a local variable for it
if(FlightGlobals.ActiveVessel.mainBody.ocean)
{
if(FlightGlobals.ActiveVessel.PQSAltitude() < 0) //if negative, over ocean and pqs layer is seabed
{
hitDist = hitDist + (float)FlightGlobals.ActiveVessel.PQSAltitude(); //reduce distance of raycast by distance from seabed to sea level, remebed PQSAltitude is negative so 'add' it to reduce the distance
//Debug.Log("LH hit dist after ocean" + hitDist);
}
}
if (firstRay) //first ray this update, always set height to this
{
landHeight = hitDist;
firstRay = false;
//Debug.Log("LH 3 " + landHeight);
}
else
{
landHeight = Math.Min(landHeight, hitDist);
//Debug.Log("LH 3a " + landHeight + "|" + pHit.distance);
}
}
}
else if (!firstRay) //error trap, ray hit nothing
{
landHeight = Math.Min(landHeight, FlightGlobals.ActiveVessel.altitude - FlightGlobals.ActiveVessel.pqsAltitude);
firstRay = false;
}
//Debug.Log("LH hit dist orig " + pHit.distance);
}
}
catch
{
landHeight = FlightGlobals.ActiveVessel.altitude - FlightGlobals.ActiveVessel.pqsAltitude;
firstRay = false;
//Debug.Log("LH 2");
}
}
//if(landHeight == 0)
//{
// landHeight = landHeightBackup;
//}
if (!firstRay && landHeight == 0)
{
landHeight = FlightGlobals.ActiveVessel.altitude - FlightGlobals.ActiveVessel.pqsAltitude;
firstRay = false;
}
if (landHeight < 1) //if we are in the air, always display an altitude of at least 1
{
//Debug.Log("LH 4 " + landHeight);
landHeight = 1;
//Debug.Log("LH 4");
}
//Debug.Log("LH 5 " + landHeight);
}
//if (FlightGlobals.ActiveVessel.mainBody.ocean) //if mainbody has ocean we land on water before the seabed
//{
// //Debug.Log("LH 5");
// if (landHeight > FlightGlobals.ActiveVessel.altitude)
// {
// landHeight = FlightGlobals.ActiveVessel.altitude;
// //Debug.Log("LH 6");
// }
//}
return landHeight;
}
}
}