/
FlightModule.cs
1000 lines (861 loc) · 40.5 KB
/
FlightModule.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
//using System.Threading;
using KSP.UI.Screens;
using KSP.UI.Dialogs;
#if !RP_1_131
using ClickThroughFix;
#endif
namespace KRASH
{
/*
* This class contains all of the code for manipulating the GUI, and interacting with the game
* for KRASH in GameScenes.FLIGHT.
*/
[KSPAddon(KSPAddon.Startup.Flight, false)]
class FlightModule : MonoBehaviour
{
private const string TAG = "KRASH.FlightModule";
public static AltimeterSliderButtons _Recovery_button = null;
private void CallbackLevelWasLoaded(Scene scene, LoadSceneMode mode)
{
if (HighLogic.LoadedScene == GameScenes.FLIGHT)
{
Log.Info("CallbackLevelWasLoaded");
// KRASHShelter.instance.simPauseMenuInstance.AddToPostDrawQueue ();
}
}
public void Awake()
{
Log.Info("FlightModule.Awake");
}
Camera nearCamera, farCamera, scaledSpaceCamera;
Camera[] cams;
// Entry Point
void Start()
{
Log.Info("FlightModule.Start");
if (KRASHShelter.instance.simPauseMenuInstance == null)
Log.Info("KRASH.simPauseMenuInstance == null");
GameObject t = GameObject.Find("SimulationPauseMenu");
KRASHShelter.instance.simPauseMenuInstance = t.GetComponent<SimulationPauseMenu>();
// KRASHShelter.instance.simPauseMenuInstance = new SimulationPauseMenu();
// KSP isn't calling Start for the simpausemenu, so we do it here
// KRASHShelter.instance.simPauseMenuInstance.Start ();
// GameEvents.onLevelWasLoaded.Add(CallbackLevelWasLoaded);
// }
// GameEvents.onLevelWasLoaded.Add (CallbackLevelWasLoaded);
// DontDestroyOnLoad (this);
}
void OnEnable()
{
//Tell our 'OnLevelFinishedLoading' function to start listening for a scene change as soon as this script is enabled.
SceneManager.sceneLoaded += CallbackLevelWasLoaded;
}
void OnDisable()
{
//Tell our 'OnLevelFinishedLoading' function to stop listening for a scene change as soon as this script is disabled. Remember to always have an unsubscription for every delegate you subscribe to!
SceneManager.sceneLoaded -= CallbackLevelWasLoaded;
}
#if false
void OnLevelWasLoaded (int i)
{
//Log.Info ("OnLevelWasLoaded in FlightModule.cs");
if (KRASH.instance.SimulationActive && HighLogic.LoadedScene == GameScenes.FLIGHT) {
// OrbitEditor.Simple (FlightGlobals.fetch.activeVessel.orbitDriver, 100000, LaunchGUI.selectedBody);
}
}
#endif
/*
* The basic idea here is that if we are Simming, destroy the vanilla pause menu as soon
* as it appears, and replace it with out own.
*
* If the pause menu is open, close it.
*
* If the pause key is being pressed, check if our menu is active.
* if our menu is not active, make it active.
* if our menu is not active, tell it to reduce by one ui layer.
*/
int pauseCnt = 0;
int closeCnt = 0;
bool wireFrameAdded = false;
void Update()
{
// We don't want to do anything if we aren't simming
if (KRASHShelter.persistent.shelterSimulationActive)
{
#if true
if (HighLogic.CurrentGame.Parameters.CustomParams<KRASH_Settings>().wireframes)
{
if (!wireFrameAdded)
{
wireFrameAdded = true;
Log.Info("Adding wireframe");
cams = Camera.allCameras;
for (int i = 0; i < cams.Length; i++)
{
if (cams[i].name == "Camera 00")
{
nearCamera = cams[i];
}
if (cams[i].name == "Camera 01")
{
//cams [i].renderingPath=RenderingPath.DeferredShading;
farCamera = cams[i];
//cams [i].enabled=false;
}
if (cams[i].name == "Camera ScaledSpace")
{
//cams [i].renderingPath=RenderingPath.DeferredShading;
scaledSpaceCamera = cams[i];
}
}
nearCamera.gameObject.AddComponent(typeof(WireFrame));
// farCamera.gameObject.AddComponent(typeof(WireFrame));
// scaledSpaceCamera.gameObject.AddComponent(typeof(WireFrame));
#if false
// Camera.main.gameObject.AddComponent(typeof(Wireframe));
var v = FlightGlobals.ActiveVessel;
foreach (var p in v.parts)
{
var mf = p.FindModelComponents<MeshFilter>();
var smr = p.FindModelComponents<SkinnedMeshRenderer>();
// if (p.partTransform.GetComponent<MeshFilter>() || p.partTransform.GetComponent<SkinnedMeshRenderer>())
if ((mf != null && mf.Count > 0) || (smr != null && smr.Count > 0))
{
Log.Info("Wireframe added to part: " + p.partInfo.name);
// Add a WireFrame object to it.
// WireFrame added = p.gameObject.AddComponent<WireFrame>();
}
}
#endif
}
#endif
}
// Don't allow any of the recovery buttons to be used
// AltimeterSliderButtons _Recovery_button = (AltimeterSliderButtons)GameObject.FindObjectOfType (typeof(AltimeterSliderButtons));
if (_Recovery_button == null)
{
_Recovery_button = (AltimeterSliderButtons)GameObject.FindObjectOfType(typeof(AltimeterSliderButtons));
}
if (_Recovery_button != null && _Recovery_button.slidingTab.enabled)
{
_Recovery_button.hoverArea = new XSelectable();
_Recovery_button.slidingTab.enabled = false;
_Recovery_button.spaceCenterButton.enabled = false;
_Recovery_button.vesselRecoveryButton.enabled = false;
Log.Info("Recovery locked");
}
if (KRASHShelter.instance.simPauseMenuInstance == null)
{
Log.Info("FlightModule.Update KRASH.simPauseMenuInstance == null");
return;
}
if (KRASHShelter.instance == null)
{
Log.Info("FlightModule.Update KRASH.instance == null");
return;
}
//Log.Info ("jbb PauseMenu.isOpen: " + PauseMenu.isOpen.ToString ());
//Log.Info ("KRASHShelter.instance.simPauseMenuInstance.isOpen: " + KRASHShelter.instance.simPauseMenuInstance.isOpen.ToString ());
bool b;
try
{
b = FlightResultsDialog.isDisplaying;
}
catch (Exception)
{
Log.Info("FlightResultsDialog.isDisplaying exception");
b = false;
}
if (b)
{
FlightResultsDialog.showExitControls = false;
FlightResultsDialog.allowClosingDialog = true;
FlightResultsDialog.Display("Simulation ended!");
}
// Hide the vanilla pause menu.
Log.Info("Checking PauseMenu");
Log.Info("PauseMenu.isOpen: " + PauseMenu.isOpen.ToString());
if (PauseMenu.isOpen)
{
PauseMenu.Close();
pauseCnt = 1;
}
// Check for pause keypress
//pauseCnt++;
if (/* GameSettings.PAUSE.GetKeyDown() && */ pauseCnt != 0 && closeCnt == 0)
{
Log.Info("GetKeyDown menu.isOpen: " + KRASHShelter.instance.simPauseMenuInstance.isOpen);
if (GameSettings.PAUSE.GetKeyDown())
{
pauseCnt = 0;
Log.Info("GameSettings.PAUSE.GetKeyDown");
}
switch (KRASHShelter.instance.simPauseMenuInstance.isOpen)
{
case false:
if (pauseCnt == 1)
{
KRASHShelter.instance.simPauseMenuInstance.Display();
pauseCnt = 2;
}
break;
case true:
if (pauseCnt == 0)
{
Log.Info("menu.Close");
KRASHShelter.instance.simPauseMenuInstance.Close();
Log.Info("After close simPauseMenuInstance, PauseMenu.isOpen: " + PauseMenu.isOpen.ToString());
pauseCnt = 0;
closeCnt = 1;
}
break;
}
}
else
{
// The wierd issue changed from 1.1.3 to 1.2, now, it pauses for 7-10 tics after unpausing
if (closeCnt > 0 && closeCnt++ < 20)
{
FlightDriver.SetPause(false);
PauseMenu.Close();
pauseCnt = 0;
}
else
closeCnt = 0;
// This is to get around a wierd issue where the game unpauses
// for about 7-10 tics after PauseMenu.Close() is called.
// if (KRASHShelter.instance.simPauseMenuInstance.isOpen && pauseCnt < 20 /*&& FlightDriver.Pause */)
// FlightDriver.SetPause(true);
}
}
}
/*
* When this module is destroyed, make sure to clean up our menu.
* It probably won't leak, but I'd rather not tempt fate.
*/
public void OnDestroy()
{
Log.Info("FlightModule.OnDestroy");
//if (KRASHShelter.instance.simPauseMenuInstance != null)
// KRASHShelter.instance.simPauseMenuInstance.OnDestroy ();
// if (Camera.main.gameObject.GetComponent(typeof(Wireframe)))
// Component.Destroy(Camera.main.gameObject.GetComponent(typeof(Wireframe)));
var v = FlightGlobals.ActiveVessel;
if (wireFrameAdded)
{
#if false
foreach (var p in v.parts)
{
// Try to remove any WireFrame components found.
try
{
Destroy(p.partTransform.gameObject.GetComponent<WireFrame>());
}
catch { }
}
#endif
for (int i = 0; i < cams.Length; i++)
{
if (cams[i].name == "Camera 00")
{
nearCamera = cams[i];
Destroy(nearCamera.gameObject.GetComponent<WireFrame>());
break;
}
}
}
KRASHShelter.instance.simPauseMenuInstance = null;
}
}
[KSPAddon(KSPAddon.Startup.Flight, false)]
/*
* This menu is very similar to the squad PauseMenu.
*
* isOpen - returns true if this menu is in an active state
*
* Display() - makes this menu active
* Close() - closes one ui layer.
*/
public class SimulationPauseMenu : MonoBehaviour
{
private const float SPACER = 5.0f;
private const string TAG = "KRASH.FlightModule.SimulationPauseMenu";
private Rect _windowRect;
// private bool _display = false;
private Color _backgroundColor;
// private GUISkin _guiSkin;
private PopupDialog _activePopup;
private MiniSettings _miniSettings;
private bool simTermination = false;
private static string simTerminationMsg = "";
public bool isOpen = false;
public SimulationPauseMenu()
{
Log.Info("SimulationPauseMenu instantiated");
}
public void Awake()
{
Log.Info("SimulationPauseMenu.Awake");
}
// public void test(string s)
// {
// Log.Info("SimulationPauseMenu.test s: " + s);
// }
double lastUpdate = 0.0F;
void CalcStartingSimCost()
{
if (HighLogic.CurrentGame.Mode != Game.Modes.CAREER)
return;
Log.Info("SimulationPauseMenu.CalcStartingSimCost 1");
if (KRASHShelter.instance == null || KRASHShelter.instance.cfg == null || FlightGlobals.ActiveVessel == null)
{
if (FlightGlobals.ActiveVessel == null)
Log.Info("FlightGlobals.ActiveVessel is null");
else
Log.Info("SimulationPauseMenu.CalcStartingSimCost cfg == null");
return;
}
if (HighLogic.CurrentGame.Mode == Game.Modes.CAREER)
{
// float dryMass, fuelMass;
//KRASHShelter.simCost = KRASHShelter.instance.cfg.flatSetupCost +
// EditorLogic.fetch.ship.parts.Count * KRASHShelter.instance.cfg.perPartSetupCost +
// (dryMass + fuelMass) * KRASHShelter.instance.cfg.perTonSetupCost +
// KRASHShelter.shipCost * KRASHShelter.instance.cfg.percentSetupCost;
KRASHShelter.simSetupCost = KRASHShelter.instance.cfg.flatSetupCost +
FlightGlobals.ActiveVessel.parts.Count * KRASHShelter.instance.cfg.perPartSetupCost +
(KRASHShelter.dryMass + KRASHShelter.fuelMass) * KRASHShelter.instance.cfg.perTonSetupCost +
KRASHShelter.shipCost * KRASHShelter.instance.cfg.percentSetupCost;
Log.Info("simSetupCost: " + KRASHShelter.simSetupCost.ToString());
lastUpdate = Planetarium.GetUniversalTime();
simX = KRASHShelter.instance.cfg.horizontalPos;
simY = KRASHShelter.instance.cfg.verticalPos;
Log.Info("simX: " + simX.ToString() + " simY: " + simY.ToString());
Log.Info("cnt: " + KRASHShelter.instance.cfg.cnt.ToString());
}
}
public void Start()
{
Log.Info("SimulationPauseMenu Start");
//KRASH.simPauseMenuInstance = this;
PauseMenu originalMenu = GameObject.FindObjectOfType(typeof(PauseMenu)) as PauseMenu;
CalcStartingSimCost();
_backgroundColor = originalMenu.backgroundColor;
// _guiSkin = originalMenu.guiSkin;
//AddToPostDrawQueue ();
_windowRect = new Rect((float)(Screen.width / 2.0 - 125.0), (float)(Screen.height / 2.0 - 70.0), 250f, 130f);
//DontDestroyOnLoad (this);
Log.Info("SimulationPauseMenu Start complete");
}
private void OnGUI()
{
DrawGUI();
}
//~SimulationPauseMenu ()
public void OnDestroy()
{
Log.Info("SimulationPauseMenu OnDestroy");
//Destroy ();
}
public void Display()
{
isOpen = true;
// _display = true;
InputLockManager.SetControlLock(ControlTypes.PAUSE, "KRASHSimPauseMenu");
Log.Info("Display: FlightDriver.SetPause (true)");
FlightDriver.SetPause(true);
Log.Info("FlightDriver.Pause: " + FlightDriver.Pause.ToString());
}
public void Close()
{
if (_activePopup != null)
{
_activePopup.Dismiss();
_activePopup = null;
Unhide();
}
else if (_miniSettings != null)
{
// WTF SQUAD?
_miniSettings.GetType().GetMethod("Dismiss", BindingFlags.NonPublic | BindingFlags.Instance);
}
else
{
isOpen = false;
//_display = false;
InputLockManager.RemoveControlLock("KRASHSimPauseMenu");
Log.Info("Close: FlightDriver.SetPause (false)");
FlightDriver.SetPause(false);
Log.Info("FlightDriver.Pause: " + FlightDriver.Pause.ToString());
}
}
#if false
public void aDestroy ()
{
Log.Info ("SimulationPauseMenu Destroy");
// Log.Info ("RemoveFromPostDrawQueue DrawGUI");
// RenderingManager.RemoveFromPostDrawQueue (3, new Callback (DrawGUI));
}
#endif
// Screw you, MiniSettings
private void Hide()
{
Log.Info("Hide");
isOpen = false;
//_display = false;
}
private void Unhide()
{
Log.Info("Unhide");
isOpen = true;
//_display = true;
}
private const int LEFT = 10;
private const int TOP = 400;
private const int WIDTH = 80;
private const int HEIGHT = 50;
//private Rect gametimePos = new Rect(10, 200, 80, 50);
private Rect simInfoPos = new Rect(LEFT, TOP, WIDTH, HEIGHT);
[Persistent]
private GUIStyle simLabelStyle;
[Persistent]
int simSize = 10;
[Persistent]
// float simX = KRASH.cfg.horizontalPos;
float simX = 10;
[Persistent]
float simY = 50;
// float simY = KRASH.cfg.verticalPos;
const string simTitle = "Sim Costs:";
void DrawOutline(Rect r, string t, int strength, GUIStyle style, Color outColor, Color inColor)
{
Color backup = style.normal.textColor;
style.normal.textColor = outColor;
for (int i = -strength; i <= strength; i++)
{
GUI.Label(new Rect(r.x - strength, r.y + i, r.width, r.height), t, style);
GUI.Label(new Rect(r.x + strength, r.y + i, r.width, r.height), t, style);
}
for (int i = -strength + 1; i <= strength - 1; i++)
{
GUI.Label(new Rect(r.x + i, r.y - strength, r.width, r.height), t, style);
GUI.Label(new Rect(r.x + i, r.y + strength, r.width, r.height), t, style);
}
style.normal.textColor = inColor;
GUI.Label(r, t, style);
style.normal.textColor = backup;
}
void CalcAndDisplaySimCost()
{
if (!simStarted)
return;
// Log.Info ("lastUpdate: " + lastUpdate.ToString () + " Planetarium.GetUniversalTime: " + Planetarium.GetUniversalTime ().ToString ());
if (HighLogic.CurrentGame.Mode == Game.Modes.CAREER)
{
if (FlightGlobals.fetch.activeVessel == null)
return;
float m = 1.0F;
if (Planetarium.GetUniversalTime() - lastUpdate >= 1)
{
// Determine atmospheric multiplier, if any
if (FlightGlobals.fetch.activeVessel.mainBody.atmosphere)
{
double alt = FlightGlobals.fetch.activeVessel.RevealAltitude();
if (alt <= FlightGlobals.fetch.activeVessel.mainBody.atmosphereDepth)
{
m = KRASHShelter.instance.cfg.AtmoMultipler;
if (m < 1.0F)
m = 1.0F;
}
}
lastUpdate = Planetarium.GetUniversalTime();
float mass = FlightGlobals.fetch.activeVessel.GetTotalMass();
List<Part> parts = FlightGlobals.fetch.activeVessel.GetActiveParts();
int cnt = parts.Count();
KRASHShelter.simCost = KRASHShelter.simCost +
(KRASHShelter.instance.cfg.flatPerMinCost +
cnt * KRASHShelter.instance.cfg.perPartPerMinCost +
mass * KRASHShelter.instance.cfg.perTonPerMinCost +
KRASHShelter.shipCost * KRASHShelter.instance.cfg.percentPerMinCost) / 60 * m;
}
Log.Info("Funding.Instance.Funds: " + Funding.Instance.Funds.ToString());
if (Funding.Instance.Funds < KRASHShelter.simCost + KRASHShelter.simSetupCost)
{
if (!KRASHShelter.instance.cfg.ContinueIfNoCash && !KRASHShelter.persistent.shelterSimulationActive && !HighLogic.CurrentGame.Parameters.CustomParams<GameParameters.AdvancedParams>().AllowNegativeCurrency)
{
KRASHShelter.simCost = Funding.Instance.Funds - KRASHShelter.simSetupCost;
Log.Info("Funding.Instance.Funds: " + Funding.Instance.Funds.ToString());
Log.Info("KRASHShelter.simCost: " + KRASHShelter.simCost.ToString());
Log.Info("KRASHShelter.simSetupCost: " + KRASHShelter.simSetupCost.ToString());
DisplayTerminationMessage("Simulation terminated due to lack of funds");
}
}
if (KRASHShelter.simCost + KRASHShelter.simSetupCost >= KRASHShelter.LimitSimCost && KRASHShelter.LimitSimCost > 0)
{
KRASHShelter.simCost = KRASHShelter.LimitSimCost - KRASHShelter.simSetupCost;
DisplayTerminationMessage("Simulation terminated due to cost limit reached");
}
// Following code is for displaying the costs during flight
if (simLabelStyle == null)
{
simLabelStyle = new GUIStyle(GUI.skin.label);
simX = Mathf.Clamp(simX, 0, Screen.width);
simY = Mathf.Clamp(simY, 0, Screen.height);
simLabelStyle.fontSize = simSize;
}
// Check for atmospheric flight
if (FlightGlobals.fetch.activeVessel.mainBody.atmosphere &&
FlightGlobals.fetch.activeVessel.RevealAltitude() <= FlightGlobals.fetch.activeVessel.mainBody.atmosphereDepth &&
!FlightGlobals.fetch.activeVessel.mainBody.isHomeWorld)
{
foreach (PreSimStatus bodyStatus in KRASHShelter.preSimStatus)
{
if (bodyStatus.flightsGlobalIndex == FlightGlobals.fetch.activeVessel.mainBody.flightGlobalsIndex)
{
if (!bodyStatus.scienceFromAtmo)
{
Log.Info("vessel.RevealAltitude: " + FlightGlobals.fetch.activeVessel.RevealAltitude().ToString());
Log.Info("vessel.altitdue: " + FlightGlobals.fetch.activeVessel.altitude.ToString());
Log.Info("AtmosphereDepth: " + FlightGlobals.fetch.activeVessel.mainBody.atmosphereDepth.ToString());
DisplayTerminationMessage("Simulation terminated due to lack of science data from " +
FlightGlobals.fetch.activeVessel.mainBody.name + " atmosphere");
}
}
}
}
if (KRASHShelter.uiVisiblity.isVisible())
{
simLabelStyle.fontSize = 20;
Vector2 size,
sizeTitle = simLabelStyle.CalcSize(new GUIContent(simTitle));
//if (config.displayGameTime && config.logGameTime) {
simInfoPos.Set(simX, simY, 200, sizeTitle.y);
DrawOutline(simInfoPos, simTitle, 1, simLabelStyle, Color.black, Color.yellow);
string costs = Math.Round(KRASHShelter.simCost + KRASHShelter.simSetupCost, 1).ToString();
//Log.Info("current thread id: " + Thread.CurrentThread.GetHashCode().ToString());
Log.Info("KRASHShelter.simCost: " + KRASHShelter.simCost.ToString());
Log.Info("KRASHShelter.simSetupCost: " + KRASHShelter.simSetupCost.ToString());
size = simLabelStyle.CalcSize(new GUIContent(costs));
simInfoPos.Set(simX + sizeTitle.x + 5, simY, 200, size.y);
DrawOutline(simInfoPos, costs, 1, simLabelStyle, Color.black, Color.white);
simInfoPos.Set(simInfoPos.xMin, simInfoPos.yMin + size.y, 200, size.y);
//}
}
}
}
// The following callback is called for several different scenerios.
private void CallbackWillDestroy(Vessel evt)
{
Log.Info("CallbackWillDestroy");
if (evt == FlightGlobals.ActiveVessel)
{
StartCoroutine(WaitForFlightResultsDialog(true));
DisplayTerminationMessage("Simulation terminated due to vessel destruction");
}
}
private void CallbackSOIChanged(GameEvents.HostedFromToAction<Vessel, CelestialBody> action)
{
if (KRASHShelter.instance.cfg.TerminateAtSoiWithoutData)
{
if (action.to != Sun.Instance.sun && !action.to.isHomeWorld)
{
foreach (PreSimStatus bodyStatus in KRASHShelter.preSimStatus)
{
if (bodyStatus.flightsGlobalIndex == action.to.flightGlobalsIndex)
{
// if body hasn't been reached or no science from it, terminate the sim
if (!(action.to.isHomeWorld || bodyStatus.scienceFromOrbit))
{
DisplayTerminationMessage("Simulation terminated due to entering unknown SOI");
}
}
}
}
}
}
private void CallbackOnLand(Vessel vsl, CelestialBody destbody)
{
Log.Info("CallbackOnland");
if (KRASHShelter.instance.cfg.TerminateAtLandWithoutData && !destbody.isHomeWorld)
{
foreach (PreSimStatus bodyStatus in KRASHShelter.preSimStatus)
{
if (bodyStatus.flightsGlobalIndex == destbody.flightGlobalsIndex)
{
if (!bodyStatus.isReached || !bodyStatus.landed)
{
DisplayTerminationMessage("Simulation terminated due to landing on unknown body");
}
}
}
}
}
private const int PHYSICSWAIT = 1;
bool hyper = false;
bool doLanding = false;
public bool simStarted = false;
int physicsCnt = 0;
//bool pausedtest = false;
public bool SimStarted()
{
return simStarted;
}
// int ticCntr = 0;
private void DrawGUI()
{
// ticCntr++;
if (KRASHShelter.persistent.shelterSimulationActive)
{
if (!hyper && HighLogic.LoadedScene == GameScenes.FLIGHT)
{
//foreach (Part p in FlightGlobals.fetch.activeVessel.Parts) {
// p.
//}
if (LaunchGUI.simType == LaunchGUI.SimType.ORBITING)
{
if (!FlightGlobals.fetch.activeVessel.HoldPhysics && physicsCnt++ > PHYSICSWAIT)
{
Hyperedit.OrbitEditor.Simple(FlightGlobals.fetch.activeVessel.orbitDriver, LaunchGUI.newAltitude, LaunchGUI.selectedBody);
hyper = true;
physicsCnt = 0;
}
}
if (LaunchGUI.simType == LaunchGUI.SimType.LANDED &&
((!doLanding && !FlightGlobals.fetch.activeVessel.HoldPhysics) ||
(!hyper && doLanding && !FlightGlobals.fetch.activeVessel.HoldPhysics)))
{
if (!doLanding)
{
if (physicsCnt++ > PHYSICSWAIT)
{
Log.Info("Setting initial orbit");
#if false
KRASHShelter.originalUp = FlightGlobals.getUpAxis();
KRASHShelter.originalHeading = Quaternion.LookRotation (FlightGlobals.fetch.activeVessel.GetWorldPos3D ());
Log.Info ("DrawGUI KRASHShelter.originalUp: " + KRASHShelter.originalUp.ToString ());
#endif
Hyperedit.OrbitEditor.Simple(FlightGlobals.fetch.activeVessel.orbitDriver, 100000, LaunchGUI.selectedBody);
hyper = false;
physicsCnt = 0;
doLanding = true;
}
}
else if (!FlightGlobals.fetch.activeVessel.HoldPhysics && physicsCnt++ > PHYSICSWAIT * 1)
{
Action<double, double, CelestialBody> onManualEdit = (latVal, lonVal, body) =>
{
if (LaunchGUI.newLatitude == 0.0f)
LaunchGUI.newLatitude = 0.001;
if (LaunchGUI.newLongitude == 0.0f)
LaunchGUI.newLongitude = 0.001;
latVal = LaunchGUI.newLatitude;
lonVal = LaunchGUI.newLongitude;
body = LaunchGUI.selectedBody;
Log.Info("latVal: " + latVal.ToString() + " lonVal: " + lonVal.ToString());
};
Log.Info("Setting landing");
if (LaunchGUI.newLatitude == 0.0f)
LaunchGUI.newLatitude = 0.001;
if (LaunchGUI.newLongitude == 0.0f)
LaunchGUI.newLongitude = 0.001;
Log.Info("LaunchGUI.newLatitude: " + LaunchGUI.newLatitude.ToString() + " LaunchGUI.newLongitude: " + LaunchGUI.newLongitude.ToString());
Hyperedit.DoLander.ToggleLanding(LaunchGUI.newLatitude, LaunchGUI.newLongitude, LaunchGUI.newAltitude, LaunchGUI.selectedBody, onManualEdit);
hyper = true;
}
}
}
if (!simStarted && LaunchGUI.selectedBody.flightGlobalsIndex == Planetarium.fetch.CurrentMainBody.flightGlobalsIndex)
{
simStarted = true;
APIManager.ApiInstance.SimStartEvent.Fire((Vessel)FlightGlobals.ActiveVessel, 0.0f);
if (HighLogic.CurrentGame.Mode == Game.Modes.CAREER)
{
GameEvents.onVesselWillDestroy.Add(this.CallbackWillDestroy);
GameEvents.onVesselSOIChanged.Add(this.CallbackSOIChanged);
GameEvents.VesselSituation.onLand.Add(this.CallbackOnLand);
}
}
// if (simStarted && !pausedtest){
// pausedtest = true;
// PauseMenu.Close ();
// FlightDriver.SetPause (true);
// }
CalcAndDisplaySimCost();
Log.Info("isOpen: " + isOpen.ToString() + " FlightResultsDialog.isDisplaying: " + FlightResultsDialog.isDisplaying.ToString());
if (isOpen && !FlightResultsDialog.isDisplaying)
{
// Log.Info ("Displaying draw window");
// GUI.skin = _guiSkin;
GUI.backgroundColor = _backgroundColor;
_windowRect = GUILayout.Window(this.GetInstanceID() + 1, _windowRect, new GUI.WindowFunction(draw), "Game Paused", new GUILayoutOption[0]);
}
}
}
private void draw(int id)
{
if (!simTermination)
{
if (GUILayout.Button("Resume Simulation"/*, _guiSkin.button */))
{
Log.Info("Close 1");
Close();
}
GUILayout.Space(SPACER);
}
if (GUILayout.Button("<color=orange>Terminate Simulation</color>"/*, _guiSkin.button */))
{
string revertTarget;
switch (KRASHShelter.lastScene)
{
case GameScenes.EDITOR:
switch (KRASHShelter.lastEditor)
{
case EditorFacility.SPH:
revertTarget = "Spaceplane Hangar";
break;
case EditorFacility.VAB:
revertTarget = "Vehicle Assembly Building";
break;
// This should never happen. If it does, just go to the SC
default:
revertTarget = "Space Center";
KRASHShelter.lastScene = GameScenes.SPACECENTER;
break;
}
break;
case GameScenes.SPACECENTER:
revertTarget = "Space Center";
break;
default:
revertTarget = "Pre-Simulation";
break;
}
string s = "Revert to " + revertTarget + " (" + KSPUtil.PrintTimeLong(((int)(Planetarium.GetUniversalTime() - FlightDriver.PostInitState.UniversalTime)));
DialogGUIBase[] options = new DialogGUIBase[2];
options[0] = new DialogGUIButton(s, () =>
{
StartCoroutine(WaitForFlightResultsDialog());
});
options[1] = new DialogGUIButton("Cancel", () =>
{
Log.Info("Close 2");
Close();
});
var multidialog = new MultiOptionDialog("krash2", "Terminating will set the game back to an earlier state. Are you sure you want to continue?", "Terminating Simulation",
HighLogic.UISkin, 450, options);
_activePopup = PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), multidialog, false, HighLogic.UISkin, true);
Hide();
}
if (FlightDriver.CanRevertToPostInit)
{
if (GUILayout.Button("<color=orange>Restart Simulation</color>"/*, _guiSkin.button*/))
{
DialogGUIBase[] options = new DialogGUIBase[2];
string s = "Revert to Launch(" + KSPUtil.PrintTimeLong(((int)(Planetarium.GetUniversalTime() - FlightDriver.PostInitState.UniversalTime))) + " ago";
options[0] = new DialogGUIButton(s, () =>
{
Log.Info("Close 3");
Close();
Log.Info("Close 4");
Close();
FlightDriver.RevertToLaunch();
// The RevertTolaunch reloads all the objects, so we destroy them here to avoid conflicts
KRASHShelter.instance.DestroyModules();
});
options[1] = new DialogGUIButton("Cancel", () =>
{
Log.Info("Close 5");
Close();
});
var multidialog = new MultiOptionDialog("krash3", "Reverting will set the game back to an earlier state. Are you sure you want to continue?", "Reverting Simulation",
HighLogic.UISkin, 450, options);
_activePopup = PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), multidialog, false, HighLogic.UISkin, true);
// _activePopup = PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f),
// new MultiOptionDialog(null, new Callback (drawRevertWarning), "Reverting Simulation", HighLogic.Skin, new DialogOption[0]), false, HighLogic.Skin);
APIManager.ApiInstance.SimRestartEvent.Fire((Vessel)FlightGlobals.ActiveVessel, KRASHShelter.simCost + KRASHShelter.simSetupCost);
Hide();
}
}
GUILayout.Space(SPACER);
if (GUILayout.Button("Settings"/*, _guiSkin.button*/))
{
Hide();
_miniSettings = MiniSettings.Create(Unhide);
}
#if false
} else {
GUILayout.Label (simTerminationMsg);
if (GUILayout.Button ("<color=orange>Terminate Simulation</color>"/*, _guiSkin.button*/)) {
Hide ();
StartCoroutine (WaitForFlightResultsDialog ());
// _activePopup = PopupDialog.SpawnPopupDialog (new MultiOptionDialog (null, new Callback (drawTerminationWarning), "Terminating Simulation", HighLogic.Skin, new DialogOption[0]), false, HighLogic.Skin);
}
}
#endif
}
public void DisplayTerminationMessage(string msg)
{
if (!simTermination)
{
simTermination = true;
simTerminationMsg = msg;
// KRASHShelter.persistent.SetSuspendUpdate(false);
Display();
}
}
bool returnedAfterDialog = false;
public IEnumerator WaitForFlightResultsDialog(bool returnAfterDialog = false)
{
Log.Info("IEnumerator WaitForFlightResultsDialog");
if (!returnedAfterDialog)
{
FlightResultsDialog.showExitControls = false;
FlightResultsDialog.allowClosingDialog = true;
FlightResultsDialog.Display("Simulation results!");
bool b = true;
while (b)
{
try
{
b = FlightResultsDialog.isDisplaying;
}
catch (Exception)
{
Log.Info("FlightResultsDialog.isDisplaying exception");
b = false;
}
if (b)
yield return 0;
}
}
returnedAfterDialog = returnAfterDialog;
if (returnAfterDialog)
yield break;
if (HighLogic.CurrentGame.Mode == Game.Modes.CAREER)
{
GameEvents.onVesselSOIChanged.Remove(this.CallbackSOIChanged);
GameEvents.onVesselWillDestroy.Remove(this.CallbackWillDestroy);
GameEvents.VesselSituation.onLand.Remove(this.CallbackOnLand);
}
Log.Info("FlightModule.WaitForFlightResultsDialog, before SimulationNotification");
KRASHShelter.instance.SimulationNotification(false);
HighLogic.CurrentGame.Parameters.Flight.CanQuickSave = true;
HighLogic.CurrentGame.Parameters.Flight.CanQuickLoad = true;
Close();
Close();
APIManager.ApiInstance.SimTerminationEvent.Fire((Vessel)FlightGlobals.ActiveVessel, KRASHShelter.simCost + KRASHShelter.simSetupCost);
KRASHShelter.instance.Deactivate(KRASHShelter.lastScene);
//OnDestroy ();
yield return 0;
}
}
}