/
KerbalKonstructs.cs
2256 lines (1894 loc) · 68.9 KB
/
KerbalKonstructs.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.IO;
using System.Text.RegularExpressions;
using UnityEngine;
using KerbalKonstructs.StaticObjects;
using KerbalKonstructs.KerbinNations;
using KerbalKonstructs.LaunchSites;
using KerbalKonstructs.UI;
using KerbalKonstructs.Utilities;
using System.Reflection;
using KerbalKonstructs.SpaceCenters;
using KerbalKonstructs.API;
using KerbalKonstructs.API.Config;
using KSP.UI.Screens;
using Upgradeables;
using UpgradeLevel = Upgradeables.UpgradeableObject.UpgradeLevel;
namespace KerbalKonstructs
{
[KSPAddonFixed(KSPAddon.Startup.MainMenu, true, typeof(KerbalKonstructs))]
public class KerbalKonstructs : MonoBehaviour
{
// Hello
public static KerbalKonstructs instance;
public static string installDir = AssemblyLoader.loadedAssemblies.GetPathByType(typeof(KerbalKonstructs));
private Dictionary<UpgradeableFacility, int> facilityLevels = new Dictionary<UpgradeableFacility, int>();
public string sKKVersion = "0.9.6.7";
#region Holders
public StaticObject selectedObject;
public StaticModel selectedModel;
public StaticObject snapTargetInstance;
public StaticDatabase staticDB = new StaticDatabase();
public CameraController camControl = new CameraController();
private CelestialBody currentBody;
public int iMenuCount = 0;
public Double VesselCost = 0;
public Double RefundAmount = 0;
string savedObjectPath = "";
public string lastRecoveryBase = "";
public float lastRecoveryDistance = 0f;
public float fRecovFactor = 0f;
public float fRecovRange = 0f;
public float fLaunchRefund = 0f;
public Double dRecoveryValue = 0;
public Double dActualRecoveryValue = 0;
#endregion
#region Switches
private Boolean atMainMenu = false;
public Boolean InitialisedFacilities = false;
public Boolean VesselLaunched = false;
public Boolean bImportedCustom = false;
public Boolean bStylesSet = false;
public Boolean bDisablePositionEditing = false;
#endregion
#region GUI Windows
private EditorGUI GUI_Editor = new EditorGUI();
private StaticsEditorGUI GUI_StaticsEditor = new StaticsEditorGUI();
private NavGuidanceSystem GUI_NGS = new NavGuidanceSystem();
private DownlinkGUI GUI_Downlink = new DownlinkGUI();
private BaseBossFlight GUI_FlightManager = new BaseBossFlight();
private FacilityManager GUI_FacilityManager = new FacilityManager();
private LaunchSiteSelectorGUI GUI_LaunchSiteSelector = new LaunchSiteSelectorGUI();
private MapIconManager GUI_MapIconManager = new MapIconManager();
private KSCManager GUI_KSCManager = new KSCManager();
private AirRacing GUI_AirRacingApp = new AirRacing();
private BaseManager GUI_BaseManager = new BaseManager();
private KKSettingsGUI GUI_Settings = new KKSettingsGUI();
public ModelInfo GUI_ModelInfo = new ModelInfo();
#endregion
#region Show Toggles
public Boolean showEditor = false;
public Boolean showSiteSelector = false;
public Boolean showBaseManager = false;
public Boolean showFlightManager = false;
public Boolean showMapIconManager = false;
public Boolean showKSCmanager = false;
public Boolean showNGS = false;
public Boolean showRacingApp = false;
public Boolean showFacilityManager = false;
public Boolean showDownlink = false;
public Boolean showATC = false;
public Boolean showSettings = false;
public Boolean showModelInfo = false;
public GameObject go = null;
public Boolean bPreviewModel = false;
#endregion
#region App Buttons
private ApplicationLauncherButton siteSelector;
private ApplicationLauncherButton flightManager;
private ApplicationLauncherButton mapManager;
private ApplicationLauncherButton KSCmanager;
#endregion
#region Configurable Variables
[KSPField]
public String defaultVABlaunchsite = "LaunchPad";
[KSPField]
public String defaultSPHlaunchsite = "Runway";
[KSPField]
public Boolean launchFromAnySite = false;
[KSPField]
public Boolean disableCareerStrategyLayer = false;
[KSPField]
public Boolean disableCustomLaunchsites = false;
[KSPField]
public Boolean disableRemoteBaseOpening = false;
[KSPField]
public Boolean disableAllInstanceEditing = true;
[KSPField]
public Boolean enableATC = true;
[KSPField]
public Boolean enableNGS = true;
[KSPField]
public Boolean enableDownlink = true;
[KSPField]
public Double facilityUseRange = 100;
[KSPField]
public Boolean disableDisplayClosed = false;
[KSPField]
public Boolean toggleIconsWithBB = false;
[KSPField]
public Boolean mapShowOpen = true;
[KSPField]
public Boolean mapShowClosed = false;
[KSPField]
public Boolean mapShowOpenT = true;
[KSPField]
public Boolean mapShowHelipads = true;
[KSPField]
public Boolean mapShowRunways = true;
[KSPField]
public Boolean mapShowRocketbases = true;
[KSPField]
public Boolean mapShowOther = false;
[KSPField]
public Boolean mapShowRadar = false;
[KSPField]
public Boolean mapShowDownlinks = false;
[KSPField]
public Boolean mapShowUplinks = false;
[KSPField]
public Boolean mapShowGroundComms = false;
[KSPField]
public Boolean mapHideIconsBehindBody = true;
[KSPField]
public Boolean disableRemoteRecovery = false;
[KSPField]
public Double defaultRecoveryFactor = 50;
[KSPField]
public Double defaultEffectiveRange = 100000;
[KSPField]
public Double maxEditorVisRange = 100000;
[KSPField]
public Boolean spawnPreviewModels = true;
[KSPField]
public Boolean DebugMode = false;
[KSPField]
public Boolean DevMode = false;
[KSPField]
public Boolean enableRTsupport = false;
#endregion
#region Other Mods Wrappers
// StageRecovery Wrapping
void SRProcessingFinished(Vessel vessel)
{
OnVesselRecovered(vessel.protoVessel, true);
}
#endregion
void Awake()
{
instance = this;
#region Game Event Hooks
GameEvents.onDominantBodyChange.Add(onDominantBodyChange);
GameEvents.onLevelWasLoaded.Add(onLevelWasLoaded);
GameEvents.onGUIApplicationLauncherReady.Add(OnGUIAppLauncherReady);
GameEvents.OnVesselRecoveryRequested.Add(OnVesselRecoveryRequested);
GameEvents.OnFundsChanged.Add(OnDoshChanged);
GameEvents.onVesselRecovered.Add(OnVesselRecovered);
GameEvents.onVesselRecoveryProcessing.Add(OnProcessRecovery);
GameEvents.onGameStateSave.Add(SaveState);
GameEvents.onGameStateLoad.Add(LoadState);
GameEvents.OnKSCFacilityUpgraded.Add(OnKSCFacilityUpgraded);
GameEvents.OnKSCFacilityUpgrading.Add(OnKSCFacilityUpgrading);
GameEvents.OnUpgradeableObjLevelChange.Add(OnUpgradeableObjLevelChange);
GameEvents.OnVesselRollout.Add(OnVesselLaunched);
#endregion
#region Other Mods Hooks
if (StageRecoveryWrapper.StageRecoveryAvailable)
{
StageRecoveryWrapper.AddRecoveryProcessingStartListener(OnVesselRecoveryRequested);
StageRecoveryWrapper.AddRecoveryProcessingFinishListener(SRProcessingFinished);
}
#endregion
#region Model API
KKAPI.addModelSetting("mesh", new ConfigFile());
ConfigGenericString authorConfig = new ConfigGenericString();
authorConfig.setDefaultValue("Unknown");
KKAPI.addModelSetting("author", authorConfig);
KKAPI.addModelSetting("DefaultLaunchPadTransform", new ConfigGenericString());
KKAPI.addModelSetting("title", new ConfigGenericString());
KKAPI.addModelSetting("category", new ConfigGenericString());
KKAPI.addModelSetting("cost", new ConfigFloat());
KKAPI.addModelSetting("manufacturer", new ConfigGenericString());
KKAPI.addModelSetting("description", new ConfigGenericString());
KKAPI.addModelSetting("DefaultLaunchSiteLength", new ConfigFloat());
KKAPI.addModelSetting("DefaultLaunchSiteWidth", new ConfigFloat());
KKAPI.addModelSetting("pointername", new ConfigGenericString());
KKAPI.addModelSetting("name", new ConfigGenericString());
KKAPI.addModelSetting("keepConvex", new ConfigGenericString());
#endregion
#region Instance API
// Position
KKAPI.addInstanceSetting("CelestialBody", new ConfigCelestialBody());
KKAPI.addInstanceSetting("RadialPosition", new ConfigVector3());
KKAPI.addInstanceSetting("Orientation", new ConfigVector3());
KKAPI.addInstanceSetting("RadiusOffset", new ConfigFloat());
KKAPI.addInstanceSetting("RotationAngle", new ConfigFloat());
// Calculated References - do not set, it will not work
KKAPI.addInstanceSetting("RefLatitude", new ConfigFloat());
KKAPI.addInstanceSetting("RefLongitude", new ConfigFloat());
// Visibility and Grouping
ConfigFloat visibilityConfig = new ConfigFloat();
visibilityConfig.setDefaultValue(25000f);
KKAPI.addInstanceSetting("VisibilityRange", visibilityConfig);
ConfigGenericString groupConfig = new ConfigGenericString();
groupConfig.setDefaultValue("Ungrouped");
KKAPI.addInstanceSetting("Group", groupConfig);
ConfigGenericString groupCenter = new ConfigGenericString();
groupCenter.setDefaultValue("false");
KKAPI.addInstanceSetting("GroupCenter", groupCenter);
KKAPI.addInstanceSetting("RefCenter", new ConfigVector3());
// Launchsite
KKAPI.addInstanceSetting("LaunchSiteName", new ConfigGenericString());
KKAPI.addInstanceSetting("LaunchPadTransform", new ConfigGenericString());
KKAPI.addInstanceSetting("LaunchSiteAuthor", new ConfigGenericString());
ConfigGenericString descriptionConfig = new ConfigGenericString();
descriptionConfig.setDefaultValue("No description available.");
KKAPI.addInstanceSetting("LaunchSiteDescription", descriptionConfig);
KKAPI.addInstanceSetting("LaunchSiteLogo", new ConfigGenericString());
KKAPI.addInstanceSetting("LaunchSiteIcon", new ConfigGenericString());
KKAPI.addInstanceSetting("LaunchSiteType", new ConfigSiteType());
ConfigGenericString category = new ConfigGenericString();
category.setDefaultValue("Other");
KKAPI.addInstanceSetting("Category", category);
KKAPI.addInstanceSetting("LaunchSiteLength", new ConfigFloat());
KKAPI.addInstanceSetting("LaunchSiteWidth", new ConfigFloat());
KKAPI.addInstanceSetting("LaunchSiteNation", new ConfigGenericString());
// Career Mode Strategy Instances
ConfigFloat openCost = new ConfigFloat();
openCost.setDefaultValue(0f);
KKAPI.addInstanceSetting("OpenCost", openCost);
ConfigFloat closeValue = new ConfigFloat();
closeValue.setDefaultValue(0f);
KKAPI.addInstanceSetting("CloseValue", closeValue);
ConfigGenericString opencloseState = new ConfigGenericString();
opencloseState.setDefaultValue("Closed");
KKAPI.addInstanceSetting("OpenCloseState", opencloseState);
ConfigGenericString favouriteSite = new ConfigGenericString();
favouriteSite.setDefaultValue("No");
KKAPI.addInstanceSetting("FavouriteSite", favouriteSite);
ConfigFloat missionCount = new ConfigFloat();
missionCount.setDefaultValue(0f);
KKAPI.addInstanceSetting("MissionCount", missionCount);
ConfigGenericString missionlog = new ConfigGenericString();
missionlog.setDefaultValue("No missions logged");
KKAPI.addInstanceSetting("MissionLog", missionlog);
// Facility Types
ConfigGenericString facilityrole = new ConfigGenericString();
facilityrole.setDefaultValue("None");
KKAPI.addModelSetting("DefaultFacilityType", facilityrole);
KKAPI.addModelSetting("DefaultFacilityLength", new ConfigFloat());
KKAPI.addModelSetting("DefaultFacilityWidth", new ConfigFloat());
KKAPI.addModelSetting("DefaultFacilityHeight", new ConfigFloat());
KKAPI.addModelSetting("DefaultFacilityMassCapacity", new ConfigFloat());
KKAPI.addModelSetting("DefaultFacilityCraftCapacity", new ConfigFloat());
ConfigGenericString instfacilityrole = new ConfigGenericString();
instfacilityrole.setDefaultValue("None");
KKAPI.addInstanceSetting("FacilityType", instfacilityrole);
KKAPI.addInstanceSetting("FacilityLengthUsed", new ConfigFloat());
KKAPI.addInstanceSetting("FacilityWidthUsed", new ConfigFloat());
KKAPI.addInstanceSetting("FacilityHeightUsed", new ConfigFloat());
KKAPI.addInstanceSetting("FacilityMassUsed", new ConfigFloat());
KKAPI.addInstanceSetting("InStorage", new ConfigGenericString());
// Facility Ratings
// Tracking station max short range in m
KKAPI.addInstanceSetting("TrackingShort", new ConfigFloat());
// Max tracking angle
KKAPI.addInstanceSetting("TrackingAngle", new ConfigFloat());
// Target Type and ID
KKAPI.addInstanceSetting("TargetType", new ConfigGenericString());
KKAPI.addInstanceSetting("TargetID", new ConfigGenericString());
//XP
KKAPI.addInstanceSetting("FacilityXP", new ConfigFloat());
// Staff
KKAPI.addModelSetting("DefaultStaffMax", new ConfigFloat());
KKAPI.addInstanceSetting("StaffMax", new ConfigFloat());
KKAPI.addInstanceSetting("StaffCurrent", new ConfigFloat());
// Fueling
KKAPI.addModelSetting("LqFMax", new ConfigFloat());
KKAPI.addInstanceSetting("LqFCurrent", new ConfigFloat());
KKAPI.addModelSetting("OxFMax", new ConfigFloat());
KKAPI.addInstanceSetting("OxFCurrent", new ConfigFloat());
KKAPI.addModelSetting("MoFMax", new ConfigFloat());
KKAPI.addInstanceSetting("MoFCurrent", new ConfigFloat());
KKAPI.addInstanceSetting("LqFAlt", new ConfigGenericString());
KKAPI.addInstanceSetting("OxFAlt", new ConfigGenericString());
KKAPI.addInstanceSetting("MoFAlt", new ConfigGenericString());
KKAPI.addModelSetting("ECMax", new ConfigFloat());
KKAPI.addModelSetting("ECRechargeRate", new ConfigFloat());
KKAPI.addInstanceSetting("ECCurrent", new ConfigFloat());
// Industry
KKAPI.addModelSetting("DefaultProductionRateMax", new ConfigFloat());
KKAPI.addInstanceSetting("ProductionRateMax", new ConfigFloat());
KKAPI.addInstanceSetting("ProductionRateCurrent", new ConfigFloat());
KKAPI.addInstanceSetting("Producing", new ConfigGenericString());
KKAPI.addModelSetting("OreMax", new ConfigFloat());
KKAPI.addInstanceSetting("OreCurrent", new ConfigFloat());
KKAPI.addModelSetting("PrOreMax", new ConfigFloat());
KKAPI.addInstanceSetting("PrOreCurrent", new ConfigFloat());
// Science Rep Funds generation
KKAPI.addModelSetting("DefaultScienceOMax", new ConfigFloat());
KKAPI.addInstanceSetting("ScienceOMax", new ConfigFloat());
KKAPI.addInstanceSetting("ScienceOCurrent", new ConfigFloat());
KKAPI.addModelSetting("DefaultRepOMax", new ConfigFloat());
KKAPI.addInstanceSetting("RepOMax", new ConfigFloat());
KKAPI.addInstanceSetting("RepOCurrent", new ConfigFloat());
KKAPI.addModelSetting("DefaultFundsOMax", new ConfigFloat());
KKAPI.addInstanceSetting("FundsOMax", new ConfigFloat());
KKAPI.addInstanceSetting("FundsOCurrent", new ConfigFloat());
// Local to a specific save - constructed in a specific save-game
// WIP for founding
ConfigGenericString LocalToSave = new ConfigGenericString();
LocalToSave.setDefaultValue("False");
KKAPI.addInstanceSetting("LocalToSave", LocalToSave);
// Custom instances - added or modified by player with the editor
ConfigGenericString CustomInstance = new ConfigGenericString();
CustomInstance.setDefaultValue("False");
KKAPI.addInstanceSetting("CustomInstance", CustomInstance);
// Launch and Recovery
ConfigFloat flaunchrefund = new ConfigFloat();
flaunchrefund.setDefaultValue(0f);
KKAPI.addInstanceSetting("LaunchRefund", flaunchrefund);
ConfigFloat frecoveryfactor = new ConfigFloat();
frecoveryfactor.setDefaultValue((float)defaultRecoveryFactor);
KKAPI.addInstanceSetting("RecoveryFactor", frecoveryfactor);
ConfigFloat frecoveryrange = new ConfigFloat();
frecoveryrange.setDefaultValue((float)defaultEffectiveRange);
KKAPI.addInstanceSetting("RecoveryRange", frecoveryrange);
// Activity logging
KKAPI.addInstanceSetting("LastCheck", new ConfigFloat());
// Nation API
KKAPI.addNationSetting("nationName", new ConfigGenericString());
KKAPI.addNationSetting("shortname", new ConfigGenericString());
KKAPI.addNationSetting("abbreviation", new ConfigGenericString());
KKAPI.addNationSetting("nationIcon", new ConfigGenericString());
KKAPI.addNationSetting("description", new ConfigGenericString());
KKAPI.addNationSetting("rulership", new ConfigGenericString());
ConfigFloat fnationratingresources = new ConfigFloat();
fnationratingresources.setDefaultValue(0f);
KKAPI.addNationSetting("ratingresources", fnationratingresources);
ConfigFloat fnationratingmilitary = new ConfigFloat();
fnationratingmilitary.setDefaultValue(0f);
KKAPI.addNationSetting("ratingmilitary", fnationratingmilitary);
ConfigFloat fnationratingtech = new ConfigFloat();
fnationratingtech.setDefaultValue(0f);
KKAPI.addNationSetting("ratingtech", fnationratingtech);
#endregion
SpaceCenterManager.setKSC();
loadConfig();
saveConfig();
DontDestroyOnLoad(this);
loadObjects();
importCustomInstances();
Debug.Log("KK: Version is " + sKKVersion);
Debug.Log("KK: Version is " + sKKVersion);
Debug.Log("KK: Version is " + sKKVersion);
UIMain.setTextures();
}
#region Game Events
public void LoadState(ConfigNode configNode)
{
if (HighLogic.LoadedScene == GameScenes.MAINMENU)
{}
else
{
if (DebugMode) Debug.Log("KK: LoadState");
PersistenceUtils.loadPersistenceBackup();
}
}
public void SaveState(ConfigNode configNode)
{
if (HighLogic.LoadedScene == GameScenes.MAINMENU)
{}
else
{
PersistenceUtils.savePersistenceBackup();
if (DebugMode) Debug.Log("KK: SaveState");
}
}
void OnVesselLaunched(ShipConstruct vVessel)
{
if (DebugMode) Debug.Log("KK: OnVesselLaunched");
if (!MiscUtils.CareerStrategyEnabled(HighLogic.CurrentGame))
{
return;
}
else
{
if (DebugMode) Debug.Log("KK: OnVesselLaunched is Career");
PersistenceUtils.savePersistenceBackup();
string sitename = EditorLogic.fetch.launchSiteName;
if (sitename == "Runway") return;
if (sitename == "LaunchPad") return;
if (sitename == "KSC") return;
if (sitename == "") return;
LaunchSite lsSite = LaunchSiteManager.getLaunchSiteByName(sitename);
float fMissionCount = lsSite.missioncount;
lsSite.missioncount = fMissionCount + 1;
double dSecs = HighLogic.CurrentGame.UniversalTime;
double hours = dSecs / 60.0 / 60.0;
double kHours = Math.Floor(hours % 6.0);
double kMinutes = Math.Floor((dSecs / 60.0) % 60.0);
double kSeconds = Math.Floor(dSecs % 60.0);
double kYears = Math.Floor(hours / 2556.5402) + 1; // Kerbin year is 2556.5402 hours
double kDays = Math.Floor(hours % 2556.5402 / 6.0) + 1;
string sDate = "Y" + kYears.ToString() + " D" + kDays.ToString() + " " + " " + kHours.ToString("00") + ":" + kMinutes.ToString("00") + ":" + kSeconds.ToString("00");
string sCraft = vVessel.shipName;
string sWeight = vVessel.GetTotalMass().ToString();
string sLogEntry = lsSite.missionlog + sDate + ", Launched " + sCraft + ", Mass " +sWeight + " t|";
lsSite.missionlog = sLogEntry;
List<LaunchSite> sites = LaunchSiteManager.getLaunchSites();
PersistenceFile<LaunchSite>.SaveList(sites, "LAUNCHSITES", "KK");
VesselLaunched = true;
float dryCost = 0f;
float fuelCost = 0f;
float total = vVessel.GetShipCosts(out dryCost, out fuelCost);
var cm = CurrencyModifierQuery.RunQuery(TransactionReasons.VesselRollout, total, 0f, 0f);
total += cm.GetEffectDelta(Currency.Funds);
double launchcost = total;
float fRefund = 0f;
LaunchSiteManager.getSiteLaunchRefund((string)EditorLogic.fetch.launchSiteName, out fRefund);
if (fRefund < 1) return;
RefundAmount = (launchcost / 100) * fRefund;
VesselCost = launchcost - (RefundAmount);
if (fRefund > 0)
{
string sMessage = "This launch normally costs " + launchcost.ToString("#0") +
" but " + sitename + " provides a " + fRefund + "% refund. \n\nSo " + RefundAmount.ToString("#0") + " funds has been credited to you. \n\nEnjoy and thanks for using " +
sitename + ". Have a safe flight.";
MiscUtils.PostMessage("Launch Refund", sMessage, MessageSystemButton.MessageButtonColor.GREEN, MessageSystemButton.ButtonIcons.ALERT);
Funding.Instance.AddFunds(RefundAmount, TransactionReasons.Cheating);
}
}
}
void onLevelWasLoaded(GameScenes data)
{
bool bTreatBodyAsNullForStatics = true;
DeletePreviewObject();
staticDB.ToggleActiveAllStatics(false);
if (selectedObject != null)
{
deselectObject(false, true);
camControl.active = false;
}
if (!data.Equals(GameScenes.FLIGHT))
{
DownlinkGUI.DisAudio.Stop();
}
if (data.Equals(GameScenes.FLIGHT))
{
bTreatBodyAsNullForStatics = false;
InputLockManager.RemoveControlLock("KKEditorLock");
InputLockManager.RemoveControlLock("KKEditorLock2");
PersistenceUtils.savePersistenceBackup();
if (FlightGlobals.ActiveVessel != null)
{
staticDB.ToggleActiveStaticsOnPlanet(FlightGlobals.ActiveVessel.mainBody, true, true);
currentBody = FlightGlobals.ActiveVessel.mainBody;
staticDB.onBodyChanged(FlightGlobals.ActiveVessel.mainBody);
DoHangaredCraftCheck();
}
else
{
if (DebugMode) Debug.Log("KK: Flight scene load. No activevessel. Activating all statics.");
staticDB.ToggleActiveAllStatics(true);
}
InvokeRepeating("updateCache", 0, 1);
}
else
{
CancelInvoke("updateCache");
}
if (data.Equals(GameScenes.SPACECENTER))
{
InputLockManager.RemoveControlLock("KKEditorLock");
// Tighter control over what statics are active
bTreatBodyAsNullForStatics = false;
currentBody = KKAPI.getCelestialBody("HomeWorld");
if (DebugMode) Debug.Log("KK: Homeworld is " + currentBody.name);
//staticDB.onBodyChanged(KKAPI.getCelestialBody("Kerbin"));
//staticDB.onBodyChanged(null);
staticDB.ToggleActiveStaticsInGroup("KSCUpgrades", true);
staticDB.ToggleActiveStaticsInGroup("KSCRace", true);
// *********
if (MiscUtils.CareerStrategyEnabled(HighLogic.CurrentGame))
{
if (DebugMode) Debug.Log("KK: Load launchsite openclose states for career game");
PersistenceFile<LaunchSite>.LoadList(LaunchSiteManager.AllLaunchSites, "LAUNCHSITES", "KK");
}
}
if (data.Equals(GameScenes.MAINMENU))
{
if (!bImportedCustom)
{
bImportedCustom = true;
}
// Close all the launchsite objects
LaunchSiteManager.setAllLaunchsitesClosed();
atMainMenu = true;
bTreatBodyAsNullForStatics = false;
iMenuCount = iMenuCount + 1;
InitialisedFacilities = false;
}
if (data.Equals(GameScenes.EDITOR))
{
// Prevent abuse if selector left open when switching to from VAB and SPH
GUI_LaunchSiteSelector.Close();
// Default selected launchsite when switching between save games
switch (EditorDriver.editorFacility)
{
case EditorFacility.SPH:
GUI_LaunchSiteSelector.setEditorType(SiteType.SPH);
if (atMainMenu)
{
LaunchSiteManager.setLaunchSite(LaunchSiteManager.runway);
atMainMenu = false;
}
break;
case EditorFacility.VAB:
GUI_LaunchSiteSelector.setEditorType(SiteType.VAB);
if (atMainMenu)
{
LaunchSiteManager.setLaunchSite(LaunchSiteManager.launchpad);
atMainMenu = false;
}
break;
default:
GUI_LaunchSiteSelector.setEditorType(SiteType.Any);
break;
}
}
if (bTreatBodyAsNullForStatics) staticDB.onBodyChanged(null);
}
void onDominantBodyChange(GameEvents.FromToAction<CelestialBody, CelestialBody> data)
{
staticDB.ToggleActiveStaticsOnPlanet(data.to, true, true);
currentBody = data.to;
staticDB.onBodyChanged(data.to);
updateCache();
}
void OnKSCFacilityUpgraded(Upgradeables.UpgradeableFacility Facility, int iLevel)
{
}
void OnKSCFacilityUpgrading(Upgradeables.UpgradeableFacility Facility, int iLevel)
{
}
void OnUpgradeableObjLevelChange(Upgradeables.UpgradeableObject uObject, int iLevel)
{
}
void OnDoshChanged(double amount, TransactionReasons reason)
{
//PersistenceUtils.savePersistenceBackup();
}
void OnProcessRecovery(ProtoVessel vessel, MissionRecoveryDialog dialog, float fFloat)
{
if (!disableRemoteRecovery)
{
if (MiscUtils.CareerStrategyEnabled(HighLogic.CurrentGame))
{
if (DebugMode) Debug.Log("KK: OnProcessRecovery");
if (vessel == null) return;
if (dialog == null) return;
if (DebugMode) Debug.Log("KK: OnProcessRecovery");
dRecoveryValue = dialog.fundsEarned;
PersistenceUtils.savePersistenceBackup();
}
}
}
void OnVesselRecoveryRequested(Vessel data)
{
if (DebugMode) Debug.Log("KK: OnVesselRecoveryRequested");
if (!disableRemoteRecovery)
{
if (MiscUtils.CareerStrategyEnabled(HighLogic.CurrentGame))
{
if (DebugMode) Debug.Log("KK: OnVesselRecoveryRequested is career");
// Change the Space Centre to the nearest open base
fRecovFactor = 0;
float fDist = 0f;
float fRecovFact = 0f;
float fRecovRng = 0f;
string sBaseName = "";
SpaceCenter csc;
SpaceCenterManager.getClosestSpaceCenter(data.gameObject.transform.position, out csc, out fDist, out fRecovFact, out fRecovRng, out sBaseName);
SpaceCenter.Instance = csc;
if (SpaceCenter.Instance == null)
{
SpaceCenter.Instance = SpaceCenterManager.KSC;
}
lastRecoveryBase = sBaseName;
if (sBaseName == "Runway" || sBaseName == "LaunchPad") lastRecoveryBase = "KSC";
if (sBaseName == "KSC") lastRecoveryBase = "KSC";
lastRecoveryDistance = fDist;
fRecovFactor = fRecovFact;
fRecovRange = fRecovRng;
}
}
}
void OnVesselRecovered(ProtoVessel vessel, Boolean bHuhWTFIDK)
{
if (!disableRemoteRecovery)
{
if (vessel == null)
{
if (DebugMode) Debug.Log("KK: onVesselRecovered vessel was null");
if (MiscUtils.CareerStrategyEnabled(HighLogic.CurrentGame))
{
SpaceCenter.Instance = SpaceCenterManager.KSC;
}
return;
}
if (MiscUtils.CareerStrategyEnabled(HighLogic.CurrentGame))
{
// Put the KSC back as the Space Centre
if (DebugMode) Debug.Log("KK: Resetting SpaceCenter to KSC");
SpaceCenter.Instance = SpaceCenterManager.KSC;
if (lastRecoveryBase != "")
{
float fRecoveryDistance = lastRecoveryDistance / 1000;
float fBaseRecRange = fRecovRange;
if (lastRecoveryBase == "KSC") fRecovFactor = 100;
if (fRecovFactor > 0)
{
MessageSystemButton.MessageButtonColor color = MessageSystemButton.MessageButtonColor.GREEN;
if (fRecovRange >= lastRecoveryDistance) fRecovFactor = 100;
float fRefund = 0f;
LaunchSiteManager.getSiteLaunchRefund((string)lastRecoveryBase, out fRefund);
if (lastRecoveryDistance < 10000) fRecovFactor = 100 - fRefund;
if (lastRecoveryBase == "KSC") fRecovFactor = 100;
string sMessage = "";
if (fRecovFactor == 100)
{
sMessage = "\n\nRecovery value of " + dRecoveryValue.ToString("#0") + " funds is paid in full.";
dActualRecoveryValue = dRecoveryValue;
}
else
{
dActualRecoveryValue = (dRecoveryValue / 100) * fRecovFactor;
sMessage = "\n\nRecovery value of " + dRecoveryValue.ToString("#0") + " funds is reduced to " + dActualRecoveryValue.ToString("#0") + " funds.";
double dDeduct = dRecoveryValue - dActualRecoveryValue;
Funding.Instance.AddFunds(-dDeduct, TransactionReasons.Cheating);
}
if (!vessel.vesselName.Contains(" Debris"))
{
MiscUtils.PostMessage("Recovery Complete", vessel.vesselName +
" was recovered by " + lastRecoveryBase + ".\n\nDistance to vessel was " +
fRecoveryDistance.ToString() + " km" +
"\n\nRecovery Factor of " + lastRecoveryBase + " at this distance is "
+ fRecovFactor + "%" + sMessage, color, MessageSystemButton.ButtonIcons.ALERT);
}
lastRecoveryBase = "";
fRecovFactor = 0;
fRecovRange = 0;
dRecoveryValue = 0;
dActualRecoveryValue = 0;
}
}
}
}
}
void LateUpdate()
{
if (HighLogic.LoadedScene == (GameScenes)5 && (!InitialisedFacilities))
{
string saveConfigPath = string.Format("{0}saves/{1}/persistent.sfs", KSPUtil.ApplicationRootPath, HighLogic.SaveFolder);
if (File.Exists(saveConfigPath))
{
if (DebugMode) Debug.Log("KK: Found persistent.sfs");
ConfigNode rootNode = ConfigNode.Load(saveConfigPath);
ConfigNode rootrootNode = rootNode.GetNode("GAME");
foreach (ConfigNode ins in rootrootNode.GetNodes())
{
// Debug.Log("KK: ConfigNode is " + ins);
if (ins.GetValue("name") == "ScenarioUpgradeableFacilities")
{
if (DebugMode) Debug.Log("KK: Found ScenarioUpgradeableFacilities in persistent.sfs");
foreach (var s in new List<string> {
"SpaceCenter/LaunchPad",
"SpaceCenter/Runway",
"SpaceCenter/VehicleAssemblyBuilding",
"SpaceCenter/SpaceplaneHangar",
"SpaceCenter/TrackingStation",
"SpaceCenter/AstronautComplex",
"SpaceCenter/MissionControl",
"SpaceCenter/ResearchAndDevelopment",
"SpaceCenter/Administration",
"SpaceCenter/FlagPole" })
{
ConfigNode n = ins.GetNode(s);
if (n == null)
{
if (DebugMode) Debug.Log("KK: Could not find " + s + " node. Creating node.");
n = ins.AddNode(s);
n.AddValue("lvl", 0);
rootNode.Save(saveConfigPath);
InitialisedFacilities = true;
}
}
break;
}
}
if (InitialisedFacilities)
{
rootNode.Save(saveConfigPath);
foreach (UpgradeableFacility facility in GameObject.FindObjectsOfType<UpgradeableFacility>())
{
facility.SetLevel(0);
}
}
if (DebugMode) Debug.Log("KK: loadCareerObjects");
loadCareerObjects();
InitialisedFacilities = true;
}
}
if (camControl.active)
{
camControl.updateCamera();
}
if (selectedObject != null)
{
Vector3 pos = Vector3.zero;
float alt = 0;
bool changed = false;
if (showEditor)
{
if (Input.GetKey(KeyCode.W))
{
pos.y += GUI_Editor.getIncrement();
changed = true;
}
if (Input.GetKey(KeyCode.S))
{
pos.y -= GUI_Editor.getIncrement();
changed = true;
}
if (Input.GetKey(KeyCode.D))
{
pos.x += GUI_Editor.getIncrement();
changed = true;
}
if (Input.GetKey(KeyCode.A))
{
pos.x -= GUI_Editor.getIncrement();
changed = true;
}
if (Input.GetKey(KeyCode.E))
{
pos.z += GUI_Editor.getIncrement();
changed = true;
}
if (Input.GetKey(KeyCode.Q))
{
pos.z -= GUI_Editor.getIncrement();
changed = true;
}
if (Input.GetKey(KeyCode.Equals) && (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt)))
{
alt += (GUI_Editor.getIncrement()) / 10;
changed = true;
}
if (Input.GetKey(KeyCode.KeypadPlus))
{
alt += (GUI_Editor.getIncrement()) / 10;
changed = true;
}
if (Input.GetKey(KeyCode.PageUp))
{
alt += GUI_Editor.getIncrement();
changed = true;
}
if (Input.GetKey(KeyCode.Minus) && (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt)))
{
alt -= (GUI_Editor.getIncrement()) / 10;
changed = true;
}
if (Input.GetKey(KeyCode.KeypadMinus))
{
alt -= (GUI_Editor.getIncrement()) / 10;
changed = true;
}
if (Input.GetKey(KeyCode.PageDown))
{
alt -= GUI_Editor.getIncrement();
changed = true;
}
if (Input.GetKey(KeyCode.KeypadMultiply))
{
GUI_Editor.setIncrement(true, GUI_Editor.getIncrement());
}
if (Input.GetKey(KeyCode.KeypadDivide))
{
GUI_Editor.setIncrement(false, (GUI_Editor.getIncrement() / 2));
}
}
if (changed)
{
pos += (Vector3)selectedObject.getSetting("RadialPosition");
alt += (float)selectedObject.getSetting("RadiusOffset");
selectedObject.setSetting("RadialPosition", pos);
selectedObject.setSetting("RadiusOffset", alt);
EditorGUI.updateSelection(selectedObject);
}
}
if (Input.GetKeyDown(KeyCode.K) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)))
{
ToggleEditor();
}
}
public void ToggleEditor()
{
if (selectedObject != null)
deselectObject(true, true);
showEditor = !showEditor;
if (snapTargetInstance != null)
{
Color highlightColor = new Color(0, 0, 0, 0);
snapTargetInstance.HighlightObject(highlightColor);
snapTargetInstance = null;
}
}
#endregion
#region GUI Methods
void OnGUIAppLauncherReady()
{
if (ApplicationLauncher.Ready)
{
bool vis;
if (!disableCustomLaunchsites)
{
if (siteSelector == null || !ApplicationLauncher.Instance.Contains(siteSelector, out vis))
siteSelector = ApplicationLauncher.Instance.AddModApplication(onSiteSelectorOn, onSiteSelectorOff,
onSiteSelectorOnHover, doNothing, doNothing, doNothing,
ApplicationLauncher.AppScenes.SPH | ApplicationLauncher.AppScenes.VAB,
GameDatabase.Instance.GetTexture("KerbalKonstructs/Assets/SiteToolbarIcon", false));
}
if (flightManager == null || !ApplicationLauncher.Instance.Contains(flightManager, out vis))
flightManager = ApplicationLauncher.Instance.AddModApplication(onFlightManagerOn, onFlightManagerOff,
doNothing, doNothing, doNothing, doNothing,
ApplicationLauncher.AppScenes.FLIGHT,
GameDatabase.Instance.GetTexture("KerbalKonstructs/Assets/SiteToolbarIcon", false));
if (mapManager == null || !ApplicationLauncher.Instance.Contains(mapManager, out vis))
mapManager = ApplicationLauncher.Instance.AddModApplication(onMapManagerOn, onMapManagerOff,
doNothing, doNothing, doNothing, doNothing,
ApplicationLauncher.AppScenes.TRACKSTATION | ApplicationLauncher.AppScenes.MAPVIEW,
GameDatabase.Instance.GetTexture("KerbalKonstructs/Assets/SiteToolbarIcon", false));
if (KSCmanager == null || !ApplicationLauncher.Instance.Contains(KSCmanager, out vis))
KSCmanager = ApplicationLauncher.Instance.AddModApplication(onKSCmanagerOn, onKSCmanagerOff,