forked from TehGimp/KerbalMultiPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KMPVessel.cs
538 lines (419 loc) · 11.8 KB
/
KMPVessel.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace KMP
{
public class KMPVessel
{
//Properties
public KMPVesselInfo info;
public String vesselName
{
private set;
get;
}
public String ownerName
{
private set;
get;
}
public Guid id
{
private set;
get;
}
public Vector3 localDirection
{
private set;
get;
}
public Vector3 localPosition
{
private set;
get;
}
public Vector3 localVelocity
{
private set;
get;
}
public Vector3 translationFromBody
{
private set;
get;
}
public Vector3 worldDirection
{
private set;
get;
}
public Vector3 worldPosition
{
get
{
if (!orbitValid)
return Vector3.zero;
if (mainBody != null)
{
if (situationIsGrounded(info.situation))
{
//Vessel is fixed in relation to body
return mainBody.transform.TransformPoint(localPosition);
}
else
{
//Calculate vessel's position at the current (real-world) time
double time = adjustedUT;
if (mainBody.referenceBody != null && mainBody.referenceBody != mainBody && mainBody.orbit != null)
{
//Adjust for the movement of the vessel's parent body
Vector3 body_pos_at_ref = mainBody.orbit.getTruePositionAtUT(time);
Vector3 body_pos_now = mainBody.orbit.getTruePositionAtUT(Planetarium.GetUniversalTime());
return body_pos_now + (orbitRenderer.driver.orbit.getTruePositionAtUT(time) - body_pos_at_ref);
}
else
{
//Vessel is probably orbiting the sun
return orbitRenderer.driver.orbit.getTruePositionAtUT(time);
}
}
}
else
return localPosition;
}
}
public Vector3 worldVelocity
{
private set;
get;
}
public CelestialBody mainBody
{
private set;
get;
}
public GameObject gameObj
{
private set;
get;
}
public LineRenderer line
{
private set;
get;
}
public OrbitRenderer orbitRenderer
{
private set;
get;
}
public Color activeColor
{
private set;
get;
}
public bool orbitValid
{
private set;
get;
}
public bool shouldShowOrbit
{
get
{
if (!orbitValid || situationIsGrounded(info.situation))
return false;
else
return info.state == State.ACTIVE || orbitRenderer.mouseOver;
}
}
public double referenceUT
{
private set;
get;
}
public double referenceFixedTime
{
private set;
get;
}
public double adjustedUT
{
get
{
return referenceUT + (UnityEngine.Time.fixedTime - referenceFixedTime) * info.timeScale;
}
}
public Vessel vesselRef
{
set;
get;
}
//Methods
public KMPVessel(String vessel_name, String owner_name, Guid _id)
{
info = new KMPVesselInfo();
vesselName = vessel_name;
ownerName = owner_name;
id = _id;
//Build the name of the game object
System.Text.StringBuilder sb = new StringBuilder();
sb.Append(vesselName);
// sb.Append(" (");
// sb.Append(ownerName);
// sb.Append(')');
gameObj = new GameObject(sb.ToString());
gameObj.layer = 9;
generateActiveColor();
line = gameObj.AddComponent<LineRenderer>();
orbitRenderer = gameObj.AddComponent<OrbitRenderer>();
orbitRenderer.driver = new OrbitDriver();
line.transform.parent = gameObj.transform;
line.transform.localPosition = Vector3.zero;
line.transform.localEulerAngles = Vector3.zero;
line.useWorldSpace = true;
line.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
line.SetVertexCount(2);
line.enabled = false;
//orbitRenderer.forceDraw = true;
mainBody = null;
localDirection = Vector3.zero;
localVelocity = Vector3.zero;
localPosition = Vector3.zero;
worldDirection = Vector3.zero;
worldVelocity = Vector3.zero;
}
~KMPVessel()
{
}
public void generateActiveColor()
{
//Generate a display color from the owner name
activeColor = generateActiveColor(ownerName);
}
public static Color generateActiveColor(String str)
{
int val = 5381;
foreach (char c in str)
{
val = ((val << 5) + val) + c;
}
return generateActiveColor(Math.Abs(val));
}
public static Color generateActiveColor(int val)
{
switch (val % 17)
{
case 0:
return Color.red;
case 1:
return new Color(1, 0, 0.5f, 1); //Rosy pink
case 2:
return new Color(0.6f, 0, 0.5f, 1); //OU Crimson
case 3:
return new Color(1, 0.5f, 0, 1); //Orange
case 4:
return Color.yellow;
case 5:
return new Color(1, 0.84f, 0, 1); //Gold
case 6:
return Color.green;
case 7:
return new Color(0, 0.651f, 0.576f, 1); //Persian Green
case 8:
return new Color(0, 0.651f, 0.576f, 1); //Persian Green
case 9:
return new Color(0, 0.659f, 0.420f, 1); //Jade
case 10:
return new Color(0.043f, 0.855f, 0.318f, 1); //Malachite
case 11:
return Color.cyan;
case 12:
return new Color(0.537f, 0.812f, 0.883f, 1); //Baby blue;
case 13:
return new Color(0, 0.529f, 0.741f, 1); //NCS blue
case 14:
return new Color(0.255f, 0.412f, 0.882f, 1); //Royal Blue
case 15:
return new Color(0.5f, 0, 1, 1); //Violet
default:
return Color.magenta;
}
}
public void setOrbitalData(CelestialBody body, Vector3 local_pos, Vector3 local_vel, Vector3 local_dir) {
mainBody = body;
if (mainBody != null)
{
localPosition = local_pos;
translationFromBody = mainBody.transform.TransformPoint(localPosition) - mainBody.transform.position;
localDirection = local_dir;
localVelocity = local_vel;
orbitValid = true;
//Check for invalid values in the physics data
if (!situationIsGrounded(info.situation)
&& ((localPosition.x == 0.0f && localPosition.y == 0.0f && localPosition.z == 0.0f)
|| (localVelocity.x == 0.0f && localVelocity.y == 0.0f && localVelocity.z == 0.0f)
|| localPosition.magnitude > mainBody.sphereOfInfluence)
)
{
orbitValid = false;
}
for (int i = 0; i < 3; i++)
{
if (float.IsNaN(localPosition[i]) || float.IsInfinity(localPosition[i]))
{
orbitValid = false;
break;
}
if (float.IsNaN(localDirection[i]) || float.IsInfinity(localDirection[i]))
{
orbitValid = false;
break;
}
if (float.IsNaN(localVelocity[i]) || float.IsInfinity(localVelocity[i]))
{
orbitValid = false;
break;
}
}
if (!orbitValid)
{
//Debug.Log("Orbit invalid: " + vesselName);
//Spoof some values so the game doesn't freak out
localPosition = new Vector3(1000.0f, 1000.0f, 1000.0f);
translationFromBody = localPosition;
localDirection = new Vector3(1.0f, 0.0f, 0.0f);
localVelocity = new Vector3(1.0f, 0.0f, 0.0f);
}
//Calculate world-space properties
worldDirection = mainBody.transform.TransformDirection(localDirection);
worldVelocity = mainBody.transform.TransformDirection(localVelocity);
//Update game object transform
updateOrbitProperties();
updatePosition();
}
}
public void updatePosition()
{
if (!orbitValid)
return;
gameObj.transform.localPosition = worldPosition;
Vector3 scaled_pos = ScaledSpace.LocalToScaledSpace(worldPosition);
//Determine the scale of the line so its thickness is constant from the map camera view
float apparent_size = 0.01f;
bool pointed = true;
switch (info.state)
{
case State.ACTIVE:
apparent_size = 0.015f;
pointed = true;
break;
case State.INACTIVE:
apparent_size = 0.01f;
pointed = true;
break;
case State.DEAD:
apparent_size = 0.01f;
pointed = false;
break;
}
float scale = (float)(apparent_size * Vector3.Distance(MapView.MapCamera.transform.position, scaled_pos));
//Set line vertex positions
Vector3 line_half_dir = worldDirection * (scale * ScaledSpace.ScaleFactor);
if (pointed)
{
line.SetWidth(scale, 0);
}
else
{
line.SetWidth(scale, scale);
line_half_dir *= 0.5f;
}
line.SetPosition(0, ScaledSpace.LocalToScaledSpace(worldPosition - line_half_dir));
line.SetPosition(1, ScaledSpace.LocalToScaledSpace(worldPosition + line_half_dir));
if (!situationIsGrounded(info.situation))
orbitRenderer.driver.orbit.UpdateFromUT(adjustedUT);
}
public void updateOrbitProperties()
{
if (mainBody != null)
{
Vector3 orbit_pos = translationFromBody;
Vector3 orbit_vel = worldVelocity;
//Swap the y and z values of the orbital position/velocities because that's the way it goes?
float temp = orbit_pos.y;
orbit_pos.y = orbit_pos.z;
orbit_pos.z = temp;
temp = orbit_vel.y;
orbit_vel.y = orbit_vel.z;
orbit_vel.z = temp;
//Update orbit
orbitRenderer.driver.orbit.UpdateFromStateVectors(orbit_pos, orbit_vel, mainBody, Planetarium.GetUniversalTime());
referenceUT = Planetarium.GetUniversalTime();
referenceFixedTime = UnityEngine.Time.fixedTime;
}
}
public void updateRenderProperties(bool force_hide = false)
{
line.enabled = !force_hide && orbitValid && gameObj != null && MapView.MapIsEnabled;
if (gameObj != null && !force_hide && shouldShowOrbit)
orbitRenderer.drawMode = OrbitRenderer.DrawMode.REDRAW_AND_RECALCULATE;
else
orbitRenderer.drawMode = OrbitRenderer.DrawMode.OFF;
//Determine the color
Color color = activeColor;
if (orbitRenderer.mouseOver)
color = Color.white; //Change line color when moused over
else
{
switch (info.state)
{
case State.ACTIVE:
color = activeColor;
break;
case State.INACTIVE:
color = activeColor * 0.75f;
color.a = 1;
break;
case State.DEAD:
color = activeColor * 0.5f;
break;
}
}
line.SetColors(color, color);
orbitRenderer.orbitColor = color * 0.5f;
if (force_hide || !orbitValid)
orbitRenderer.drawIcons = OrbitRenderer.DrawIcons.NONE;
else if (info.state == State.ACTIVE && shouldShowOrbit)
orbitRenderer.drawIcons = OrbitRenderer.DrawIcons.OBJ_PE_AP;
else
orbitRenderer.drawIcons = OrbitRenderer.DrawIcons.OBJ;
}
public static bool situationIsGrounded(Situation situation) {
switch (situation) {
case Situation.LANDED:
case Situation.SPLASHED:
case Situation.PRELAUNCH:
case Situation.DESTROYED:
case Situation.UNKNOWN:
return true;
default:
return false;
}
}
public static bool situationIsOrbital(Situation situation) {
switch (situation) {
case Situation.ASCENDING:
case Situation.DESCENDING:
case Situation.ENCOUNTERING:
case Situation.ESCAPING:
case Situation.ORBITING:
return true;
default:
return false;
}
}
}
}