-
Notifications
You must be signed in to change notification settings - Fork 1
/
KerbTown.cs
1233 lines (1026 loc) · 52.1 KB
/
KerbTown.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
/* LICENSE
* This source code is copyrighted.
* All rights reserved.
* Copyright © Ryan Irecki 2013
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using Kerbtown.EEComponents;
using Kerbtown.NativeModules;
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace Kerbtown
{
[KSPAddonFixed(KSPAddon.Startup.SpaceCentre, true, typeof(KerbTown))]
public partial class KerbTown : MonoBehaviour
{
private string _currentBodyName = "";
private CelestialObject _currentCelestialObj;
private string _currentConfigUrl = "";
private string _currentModelUrl = "";
private StaticObject _currentSelectedObject;
private Dictionary<string, Dictionary<string, string>> _staticPropertyList;
private Dictionary<string, List<StaticObject>> _eeInstancedList;
private Dictionary<string, List<StaticObject>> _instancedList;
private Dictionary<string, string> _modelList;
private PQSCity.LODRange _myLodRange;
private float _prevRotationAngle;
private Dictionary<string, List<StaticObject>> _ssInstancedList;
private void Awake()
{
PopulateLists();
InstantiateEasterEggs();
InstantiateStaticsFromInstanceList();
InstantiateStaticsFromSave();
if (FlightGlobals.currentMainBody != null)
_currentBodyName = FlightGlobals.currentMainBody.bodyName; //todo remove redundant code
GameEvents.onDominantBodyChange.Add(OnDominantBodyChangeCallback);
GameEvents.onFlightReady.Add(OnFlightReadyCallback);
GameEvents.onGameStateSaved.Add(OnSave);
GameEvents.onGameStateCreated.Add(OnLoad);
DontDestroyOnLoad(this);
}
private void OnLoad(Game data)
{
foreach (KtComponent module in from ins in _instancedList
from o in ins.Value
where o.ModuleList != null
from module in o.ModuleList
where module.ModuleComponent.GetType() == typeof (StaticObjectModule)
select module)
((StaticObjectModule) module.ModuleComponent).OnLoad(data);
}
private void OnSave(Game data)
{
foreach (KtComponent module in from ins in _instancedList
from o in ins.Value
where o.ModuleList != null
from module in o.ModuleList
where module.ModuleComponent.GetType() == typeof (StaticObjectModule)
select module)
((StaticObjectModule) module.ModuleComponent).OnSave(data);
}
private void OnDestroy()
{
Extensions.LogInfo("Removing script references ..");
GameEvents.onDominantBodyChange.Remove(OnDominantBodyChangeCallback);
GameEvents.onFlightReady.Remove(OnFlightReadyCallback);
GameEvents.onGameStateSaved.Remove(OnSave);
GameEvents.onGameStateCreated.Remove(OnLoad);
KtCamera.RestoreCameraParent();
// Don't do anything...
return;
DestroyInstances(_instancedList);
// Destroy Easter Eggs
foreach (StaticObject i in _eeInstancedList.SelectMany(ins => ins.Value))
DestroySoInstance(i);
}
private static void DestroyInstances(Dictionary<string, List<StaticObject>> instanceDictionary)
{
Extensions.LogInfo("Destoying Static Object Instances");
foreach (StaticObject i in instanceDictionary.SelectMany(ins => ins.Value))
{
DestroySoInstance(i);
}
}
// Create instanced lists.
private void PopulateLists()
{
UrlDir.UrlConfig[] staticConfigs = GameDatabase.Instance.GetConfigs("STATIC");
_staticPropertyList = new Dictionary<string, Dictionary<string, string>>();
_instancedList = new Dictionary<string, List<StaticObject>>();
_modelList = new Dictionary<string, string>();
foreach (UrlDir.UrlConfig staticUrlConfig in staticConfigs)
{
// Skip easter eggs all together.
if (staticUrlConfig.url.IndexOf("KerbTown/EE", StringComparison.OrdinalIgnoreCase) >= 0)
continue;
string model = staticUrlConfig.config.GetValue("mesh");
if (string.IsNullOrEmpty(model))
{
Extensions.LogError("Missing 'mesh' parameter for " + staticUrlConfig.url);
continue;
}
model = model.Substring(0, model.LastIndexOf('.'));
string modelUrl = staticUrlConfig.url.Substring(0, staticUrlConfig.url.SecondLastIndex('/')) + "/" +
model;
//Extensions.LogWarning("Model url: " + modelUrl);
//Extensions.LogWarning("Config url: " + staticUrlConfig.url);
if(_staticPropertyList.ContainsKey(modelUrl) == false)
_staticPropertyList[modelUrl] = new Dictionary<string,string>();
if(staticUrlConfig.config.HasValue("DefaultLaunchPadTransform"))
_staticPropertyList[modelUrl]["DefaultLaunchPadTransform"] = staticUrlConfig.config.GetValue("DefaultLaunchPadTransform");
// Skip adding the object if it is not yielding.
//string isYielding = staticUrlConfig.config.GetValue("isYielding");
//if (string.IsNullOrEmpty(isYielding) || isYielding != "0")
_modelList.Add(modelUrl, staticUrlConfig.url);
// If we already have previous instances of the object, fill up the lists so that KerbTown can start instantiating them
if (!staticUrlConfig.config.HasNode("Instances"))
continue;
foreach (ConfigNode ins in staticUrlConfig.config.GetNodes("Instances"))
{
Vector3 radPosition = ConfigNode.ParseVector3(ins.GetValue("RadialPosition"));
float rotAngle = float.Parse(ins.GetValue("RotationAngle"));
float radOffset = float.Parse(ins.GetValue("RadiusOffset"));
Vector3 orientation = ConfigNode.ParseVector3(ins.GetValue("Orientation"));
float visRange = float.Parse(ins.GetValue("VisibilityRange"));
string celestialBodyName = ins.GetValue("CelestialBody");
string launchSiteName = ins.GetValue("LaunchSiteName") ?? "";
string launchPadTransform = ins.GetValue("LaunchPadTransform") ?? "";
//string scaleStr = ""; //ins.GetValue("Scale");
//Vector3 scale = ConfigNode.ParseVector3(string.IsNullOrEmpty(scaleStr) ? "1,1,1" : scaleStr);
if (_instancedList.ContainsKey(modelUrl))
{
//_instancedList[modelUrl].Add(
// new StaticObject(radPosition, rotAngle, radOffset, orientation,
// visRange, modelUrl, staticUrlConfig.url, celestialBodyName, scale, "", launchSiteName));
_instancedList[modelUrl].Add(
new StaticObject(radPosition, rotAngle, radOffset, orientation,
visRange, modelUrl, staticUrlConfig.url, celestialBodyName, "", launchSiteName, launchPadTransform));
}
else
{
_instancedList.Add(modelUrl,
new List<StaticObject>
{
//new StaticObject(radPosition, rotAngle, radOffset, orientation,
// visRange, modelUrl, staticUrlConfig.url, celestialBodyName, scale, "",
// launchSiteName)
new StaticObject(radPosition, rotAngle, radOffset, orientation,
visRange, modelUrl, staticUrlConfig.url, celestialBodyName, "",
launchSiteName, launchPadTransform)
});
}
}
}
}
private void InstantiateStatic(PQS celestialPQS, StaticObject stObject, bool freshObject = false)
{
#region Staitc Object Core Parameters
float visibilityRange = stObject.VisRange;
float localRotationAngle = stObject.RotAngle;
float radiusOffset = stObject.RadOffset;
string modelUrl = stObject.ModelUrl;
Vector3 orientDirection = stObject.Orientation;
Vector3 radialPosition = stObject.RadPosition;
if (radialPosition == Vector3.zero)
{
radialPosition =
_currentCelestialObj.CelestialBodyComponent.transform.InverseTransformPoint(
FlightGlobals.ActiveVessel.transform.position);
stObject.RadPosition = radialPosition;
}
if (orientDirection == Vector3.zero)
{
orientDirection = Vector3.up;
stObject.Orientation = orientDirection;
}
stObject.Latitude = GetLatitude(radialPosition);
stObject.Longitude = GetLongitude(radialPosition);
#endregion
// Instantiate
GameObject ktGameObject = GameDatabase.Instance.GetModel(modelUrl);
// Add the reference component.
var soModule = ktGameObject.AddComponent<StaticObjectModule>();
// Active the game object.
ktGameObject.SetActive(true);
// Set objects to layer 15 so that they collide correctly with Kerbals.
SetLayerRecursively(ktGameObject, 15);
// Set the parent object to the celestial component's GameObject.
ktGameObject.transform.parent = celestialPQS.transform;
Debug.Log("Setting static game object's parent to: " + ktGameObject.transform.parent.name);
// Obtain all active transforms in the static game object.
Transform[] gameObjectList = ktGameObject.GetComponentsInChildren<Transform>();
// Create a list of renderers to be manipulated by the default PQSCity class.
List<GameObject> rendererList =
(from t in gameObjectList where t.gameObject.renderer != null select t.gameObject).ToList();
// Create the LOD range.
_myLodRange = new PQSCity.LODRange
{
renderers = rendererList.ToArray(),
objects = new GameObject[0],
//new[] {staticGameObject}, // Todo: change to GameObject children.
visibleRange = visibilityRange
};
// Add the PQSCity class (extended by KerbTown).
var myCity = ktGameObject.AddComponent<PQSCityEx>();
// Assign PQSCity variables.
myCity.lod = new[] {_myLodRange};
myCity.frameDelta = 1;
myCity.repositionToSphere = true;
myCity.repositionToSphereSurface = false;
myCity.repositionRadial = radialPosition;
myCity.repositionRadiusOffset = radiusOffset;
myCity.reorientFinalAngle = localRotationAngle;
myCity.reorientToSphere = true;
myCity.reorientInitialUp = orientDirection;
myCity.sphere = celestialPQS;
myCity.order = 100;
myCity.modEnabled = true;
// Assign custom variables.
myCity.StaticObjectRef = stObject;
// Setup and orientate the PQSCity instanced object.
myCity.OnSetup();
myCity.Orientate();
// If the object was instantiated by "Create", override all renderers to active.
if (freshObject)
{
foreach (GameObject renObj in rendererList)
renObj.renderer.enabled = true;
}
// Add component references to the static object.
stObject.PQSCityComponent = myCity;
stObject.StaticGameObject = ktGameObject;
//stObject.ModuleReference = soModule;
// Add the static object as a reference to the StaticObjectModule
soModule.StaticObjectRef = stObject;
// Add remaining modules.
switch (stObject.ObjectID)
{
case "MushroomCave":
AddNativeComponent(ktGameObject, typeof (MushroomCave));
break;
case "PurplePathway":
AddNativeComponent(ktGameObject, typeof (PurplePathway));
break;
default:
AddModuleComponents(stObject);
break;
}
// Alter the Launch Site spawn object name if necessary.
// Todo: optimize
if (stObject.LaunchSiteName != "")
{
if(stObject.LaunchPadTransform == "./" || ktGameObject.transform.Find(stObject.LaunchPadTransform) != null)
{
ktGameObject.transform.name = stObject.LaunchSiteName;
ktGameObject.name = stObject.LaunchSiteName;
Debug.Log("Launch pad transform found: " + stObject.LaunchPadTransform + ", object renamed to: " + ktGameObject.name);
// Need to update PSystemSetup.Instance.LaunchSites as well.
foreach (FieldInfo fi in PSystemSetup.Instance.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
{
if (fi.FieldType.Name == "LaunchSite[]")
{
Debug.Log("Acquiring launch sites.");
PSystemSetup.LaunchSite[] sites = (PSystemSetup.LaunchSite[])fi.GetValue(PSystemSetup.Instance);
if (sites == null)
Debug.Log("Fail to acquire launch sites.");
if (PSystemSetup.Instance.GetLaunchSite(stObject.LaunchSiteName) == null)
{
PSystemSetup.LaunchSite newSite = new PSystemSetup.LaunchSite();
if (stObject.LaunchPadTransform == "./")
newSite.launchPadName = stObject.LaunchSiteName;
else
newSite.launchPadName = stObject.LaunchSiteName + "/" + stObject.LaunchPadTransform;
newSite.name = stObject.LaunchSiteName;
newSite.pqsName = stObject.CelestialBodyName;
PSystemSetup.LaunchSite[] newSites = new PSystemSetup.LaunchSite[sites.Length + 1];
for (int i = 0; i < sites.Length; ++i)
{
newSites[i] = sites[i];
}
newSites[newSites.Length - 1] = newSite;
fi.SetValue(PSystemSetup.Instance, newSites);
sites = newSites;
}
else
{
Debug.Log("Launch site " + stObject.LaunchSiteName + " already exists.");
}
break;
}
}
// Now update the sites again.
MethodInfo updateSitesMI = PSystemSetup.Instance.GetType().GetMethod("SetupLaunchSites", BindingFlags.NonPublic | BindingFlags.Instance);
if (updateSitesMI == null)
Debug.Log("Fail to find SetupLaunchSites().");
else
updateSitesMI.Invoke(PSystemSetup.Instance, null);
}
else
{
Extensions.LogWarning("Launch Site '" + ktGameObject.name + "'does not have the defined spawn transform.");
}
}
}
private void InstantiateEasterEggs()
{
var configDict = new Dictionary<string, string>
{
{"Hubs/Static/KerbTown/EE01/EE01", "MushroomCave"},
{"Hubs/Static/KerbTown/EE02/EE02", "PurplePathway"}
};
_eeInstancedList = new Dictionary<string, List<StaticObject>>(configDict.Count);
foreach (var configItem in configDict)
{
ConfigNode staticUrlConfig = GameDatabase.Instance.GetConfigNode(configItem.Key);
if (staticUrlConfig == null)
continue;
string model = staticUrlConfig.GetValue("mesh");
if (string.IsNullOrEmpty(model))
{
Extensions.LogError("Missing 'mesh' parameter for " + configItem);
continue;
}
model = model.Substring(0, model.LastIndexOf('.'));
string modelUrl = configItem.Key.Substring(0, configItem.Key.SecondLastIndex('/')) + "/" + model;
// TODO: Instantiate through code rather than config.
foreach (ConfigNode ins in staticUrlConfig.GetNodes("Instances"))
{
Vector3 radPosition = ConfigNode.ParseVector3(ins.GetValue("RadialPosition"));
float rotAngle = float.Parse(ins.GetValue("RotationAngle"));
float radOffset = float.Parse(ins.GetValue("RadiusOffset"));
Vector3 orientation = ConfigNode.ParseVector3(ins.GetValue("Orientation"));
float visRange = float.Parse(ins.GetValue("VisibilityRange"));
string celestialBodyName = ins.GetValue("CelestialBody");
//string scaleStr = "";//ins.GetValue("Scale");
//Vector3 scale = ConfigNode.ParseVector3(string.IsNullOrEmpty(scaleStr) ? "1,1,1" : scaleStr);
//var staticObject = new StaticObject(radPosition, rotAngle, radOffset, orientation,
// visRange, modelUrl, configItem.Key, celestialBodyName, scale, configItem.Value);
var staticObject = new StaticObject(radPosition, rotAngle, radOffset, orientation,
visRange, modelUrl, configItem.Key, celestialBodyName, configItem.Value);
if (_eeInstancedList.ContainsKey(modelUrl))
_instancedList[modelUrl].Add(staticObject);
else
_eeInstancedList.Add(modelUrl, new List<StaticObject> {staticObject});
InstantiateStatic(
(_currentCelestialObj = GetCelestialObject(staticObject.CelestialBodyName)).PQSComponent,
staticObject);
}
}
}
// Load Global Instances
private void InstantiateStaticsFromInstanceList()
{
Stopwatch stopWatch = Stopwatch.StartNew();
foreach (StaticObject instance in _instancedList.Keys.SelectMany(instList => _instancedList[instList]))
{
InstantiateStatic((_currentCelestialObj = GetCelestialObject(instance.CelestialBodyName)).PQSComponent,
instance);
}
stopWatch.Stop();
Extensions.LogInfo(string.Format("Loaded static objects. ({0}ms)", stopWatch.ElapsedMilliseconds));
}
// Load Save Instances
private void InstantiateStaticsFromSave()
{
string saveConfigPath;
if (HighLogic.CurrentGame.Mode == Game.Modes.SANDBOX)
{
saveConfigPath = string.Format("{0}saves/{1}/KTInstances.cfg", KSPUtil.ApplicationRootPath,
HighLogic.SaveFolder);
}
else // Career / Scenario
{
saveConfigPath = string.Format("{0}saves/scenarios/KT_{1}.cfg", KSPUtil.ApplicationRootPath,
HighLogic.CurrentGame.Title);
}
if (!File.Exists(saveConfigPath)) return;
ConfigNode rootNode = ConfigNode.Load(saveConfigPath);
_ssInstancedList = new Dictionary<string, List<StaticObject>>();
foreach (ConfigNode ins in rootNode.GetNodes("Instances"))
{
Vector3 radPosition = ConfigNode.ParseVector3(ins.GetValue("RadialPosition"));
float rotAngle = float.Parse(ins.GetValue("RotationAngle"));
float radOffset = float.Parse(ins.GetValue("RadiusOffset"));
Vector3 orientation = ConfigNode.ParseVector3(ins.GetValue("Orientation"));
float visRange = float.Parse(ins.GetValue("VisibilityRange"));
string celestialBodyName = ins.GetValue("CelestialBody");
string launchSiteName = ins.GetValue("LaunchSiteName") ?? "";
string modelUrl = ins.GetValue("ModelURL");
string configUrl = ins.GetValue("ConfigURL");
if (string.IsNullOrEmpty(modelUrl) || string.IsNullOrEmpty(configUrl)) continue;
if (_ssInstancedList.ContainsKey(modelUrl))
{
_ssInstancedList[modelUrl].Add(
new StaticObject(radPosition, rotAngle, radOffset, orientation,
visRange, modelUrl, configUrl, celestialBodyName, "", launchSiteName));
}
else
{
_ssInstancedList.Add(modelUrl,
new List<StaticObject>
{
new StaticObject(radPosition, rotAngle, radOffset, orientation,
visRange, modelUrl, configUrl, celestialBodyName, "",
launchSiteName)
});
}
}
//_ssInstancedList = new Dictionary<string, List<StaticObject>>();
Stopwatch stopWatch = Stopwatch.StartNew();
foreach (StaticObject instance in _ssInstancedList.Keys.SelectMany(instList => _ssInstancedList[instList]))
{
InstantiateStatic((_currentCelestialObj = GetCelestialObject(instance.CelestialBodyName)).PQSComponent,
instance);
}
stopWatch.Stop();
Extensions.LogInfo(string.Format("Loaded save specific static objects. ({0}ms)",
stopWatch.ElapsedMilliseconds));
}
// Save
private void SaveInstances()
{
Stopwatch stopWatch = Stopwatch.StartNew();
ConfigNode modelPartRootNode = null;
foreach (string instList in _instancedList.Keys)
{
var staticNode = new ConfigNode("STATIC");
string modelPhysPath = "";
bool nodesCleared = false;
foreach (StaticObject inst in _instancedList[instList])
{
if (!nodesCleared)
{
// Assign the root node for this static part.
modelPartRootNode = GameDatabase.Instance.GetConfigNode(inst.ConfigURL);
// Assign physical path to object config.
modelPhysPath = inst.ConfigURL.Substring(0, inst.ConfigURL.LastIndexOf('/')) + ".cfg";
// Remove existing nodes.
modelPartRootNode.RemoveNodes("Instances");
// Skip this until next static part.
nodesCleared = true;
}
var instanceNode = new ConfigNode("Instances");
instanceNode.AddValue("RadialPosition", ConfigNode.WriteVector(inst.RadPosition));
instanceNode.AddValue("RotationAngle", inst.RotAngle.ToString(CultureInfo.InvariantCulture));
instanceNode.AddValue("RadiusOffset", inst.RadOffset.ToString(CultureInfo.InvariantCulture));
instanceNode.AddValue("Orientation", ConfigNode.WriteVector(inst.Orientation));
instanceNode.AddValue("VisibilityRange", inst.VisRange.ToString(CultureInfo.InvariantCulture));
instanceNode.AddValue("CelestialBody", inst.CelestialBodyName);
instanceNode.AddValue("LaunchSiteName", inst.LaunchSiteName);
instanceNode.AddValue("LaunchPadTransform", inst.LaunchPadTransform);
//instanceNode.AddValue("Scale", ConfigNode.WriteVector(inst.Scale));
modelPartRootNode.nodes.Add(instanceNode);
}
// No current instances - find the config url that is paired with the model url.
if (_instancedList[instList].Count == 0)
{
modelPartRootNode = GameDatabase.Instance.GetConfigNode(_modelList[instList]);
modelPhysPath = _modelList[instList].Substring(0, _modelList[instList].LastIndexOf('/')) + ".cfg";
modelPartRootNode.RemoveNodes("Instances");
}
staticNode.AddNode(modelPartRootNode);
staticNode.Save(KSPUtil.ApplicationRootPath + "GameData/" + modelPhysPath,
" Generated by KerbTown - Hubs' Electrical");
}
stopWatch.Stop();
Extensions.LogInfo(string.Format("Saved static objects. ({0}ms)", stopWatch.ElapsedMilliseconds));
}
private void WritePersistence(bool deleteInstead = false)
{
string saveConfigPath;
if (HighLogic.CurrentGame.Mode == Game.Modes.SANDBOX)
{
saveConfigPath = string.Format("{0}saves/{1}/KTInstances.cfg", KSPUtil.ApplicationRootPath,
HighLogic.SaveFolder);
}
else // Career / Scenario
{
saveConfigPath = string.Format("{0}saves/scenarios/KT_{1}.cfg", KSPUtil.ApplicationRootPath,
HighLogic.CurrentGame.Title);
}
if (deleteInstead)
{
DestroyInstances(_ssInstancedList);
File.Delete(saveConfigPath);
return;
}
Stopwatch stopWatch = Stopwatch.StartNew();
var staticNode = new ConfigNode("KtStaticPersistence");
foreach (string instList in _instancedList.Keys)
{
foreach (StaticObject inst in _instancedList[instList])
{
var instanceNode = new ConfigNode("Instances");
instanceNode.AddValue("RadialPosition", ConfigNode.WriteVector(inst.RadPosition));
instanceNode.AddValue("RotationAngle", inst.RotAngle.ToString(CultureInfo.InvariantCulture));
instanceNode.AddValue("RadiusOffset", inst.RadOffset.ToString(CultureInfo.InvariantCulture));
instanceNode.AddValue("Orientation", ConfigNode.WriteVector(inst.Orientation));
instanceNode.AddValue("VisibilityRange", inst.VisRange.ToString(CultureInfo.InvariantCulture));
instanceNode.AddValue("CelestialBody", inst.CelestialBodyName);
instanceNode.AddValue("LaunchSiteName", inst.LaunchSiteName);
instanceNode.AddValue("LaunchPadTransform", inst.LaunchPadTransform);
instanceNode.AddValue("ModelURL", inst.ModelUrl);
instanceNode.AddValue("ConfigURL", inst.ConfigURL);
staticNode.nodes.Add(instanceNode);
}
}
staticNode.Save(saveConfigPath, " Generated by KerbTown - Hubs' Electrical");
stopWatch.Stop();
Extensions.LogInfo(string.Format("Saved static objects. [*] ({0}ms)", stopWatch.ElapsedMilliseconds));
}
private void Update()
{
// CTRL + K for show/hide.
if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
{
if (Input.GetKeyDown(KeyCode.K))
{
_mainWindowVisible = !_mainWindowVisible;
}
}
if (_mainWindowVisible)
{
_deletePersistence = (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift));
}
}
/*
private static Vector3 GetLocalPosition(CelestialBody celestialObject, double latitude, double longitude)
{
return Vector3.zero;
}
*/
private static double GetLongitude(Vector3d radialPosition)
{
Vector3d norm = radialPosition.normalized;
double longitude = Math.Atan2(norm.z, norm.x)*57.295780181884766 + 180; // Todo: Recheck validity.
return (!double.IsNaN(longitude) ? longitude : 0.0);
}
private static double GetLatitude(Vector3d radialPosition)
{
double latitude = Math.Asin(radialPosition.normalized.y)*57.295780181884766;
return (!double.IsNaN(latitude) ? latitude : 0.0);
}
private static Vector3d GetRadialPosition(double lat, double lon)
{
double x, y, z;
x = Math.Cos(lat / 57.295780181884766) * Math.Cos((lon - 180) / 57.295780181884766) * 100000.0;
y = Math.Sin(lat / 57.295780181884766) * 100000.0;
z = Math.Cos(lat / 57.295780181884766) * Math.Sin((lon - 180) / 57.295780181884766) * 100000.0;
return new Vector3d(x, y, z);
}
private static void SetLayerRecursively(GameObject sGameObject, int newLayerNumber)
{
// Only set to layer 'newLayerNumber' if the collider is not a trigger.
if ((sGameObject.collider != null &&
sGameObject.collider.enabled &&
!sGameObject.collider.isTrigger) || sGameObject.collider == null)
{
sGameObject.layer = newLayerNumber;
}
foreach (Transform child in sGameObject.transform)
{
SetLayerRecursively(child.gameObject, newLayerNumber);
}
}
private CelestialObject GetCelestialObject(string celestialName)
{
if (_currentCelestialObj != null &&
(_currentCelestialObj.CelestialBodyComponent != null &&
_currentCelestialObj.CelestialBodyComponent.bodyName == celestialName))
return _currentCelestialObj;
return (from PQS gameObjectInScene in FindObjectsOfType(typeof (PQS))
where gameObjectInScene.name == celestialName
select new CelestialObject(gameObjectInScene.transform.parent.gameObject)).FirstOrDefault();
}
private StaticObject GetDefaultStaticObject(string modelUrl, string configUrl)
{
// 150000f is flightcamera max distance
// Space Center clips at about 80-90km
return new
StaticObject(Vector3.zero, 0, GetSurfaceRadiusOffset(), Vector3.up, 100000, modelUrl, configUrl, "");
}
private float GetSurfaceRadiusOffset()
{
// Todo: change to activevessel.altitude after further testing or just to surface height..
Vector3d relativePosition =
_currentCelestialObj.PQSComponent.GetRelativePosition(FlightGlobals.ActiveVessel.GetWorldPos3D());
Vector3d rpNormalized = relativePosition.normalized;
return (float) (relativePosition.x/rpNormalized.x - _currentCelestialObj.PQSComponent.radius);
}
private static void DestroySoInstance(StaticObject staticObject)
{
try
{
Extensions.LogInfo("Disabling PQSCityComponent");
staticObject.PQSCityComponent.modEnabled = false;
Extensions.LogInfo("Destroying PQSCityComponent LODs");
foreach (PQSCity.LODRange lod in staticObject.PQSCityComponent.lod)
{
lod.SetActive(false);
foreach (GameObject lodren in lod.renderers)
Destroy(lodren);
foreach (GameObject lodobj in lod.objects)
Destroy(lodobj);
}
if (staticObject.ModuleList != null)
{
Extensions.LogInfo("Unloading Static Object Modules");
foreach (KtComponent module in staticObject.ModuleList.Where
(module => module.ModuleComponent.GetType() == typeof (StaticObjectModule)))
{
((StaticObjectModule) module.ModuleComponent).OnUnload();
}
}
Extensions.LogInfo("Unparent GameObject");
staticObject.StaticGameObject.transform.parent = null;
Extensions.LogInfo("Destoying PQSCityComponent and GameObject");
Destroy(staticObject.PQSCityComponent);
Destroy(staticObject.StaticGameObject);
}
catch (Exception ex)
{
Extensions.LogError("An exception was caught while destroying a static object.");
Debug.LogException(ex);
}
}
private static void InvokeSetup(StaticObject staticObject)
{
if (staticObject.ModuleList == null)
return;
// Loop incase the content creator has decided to add multiple SOM's.
foreach (KtComponent module in staticObject.ModuleList.Where
(module => module.ModuleComponent.GetType() == typeof (StaticObjectModule)))
{
((StaticObjectModule) module.ModuleComponent).OnFirstSetup();
}
}
#region Static Components
private void AddModuleComponents(StaticObject staticObject)
{
Stopwatch stopWatch = Stopwatch.StartNew();
string rootNodeUrl = staticObject.ConfigURL;
ConfigNode rootNode = GameDatabase.Instance.GetConfigNode(rootNodeUrl);
IEnumerator nodeEnum = rootNode.nodes.GetEnumerator();
while (nodeEnum.MoveNext())
{
var currentNode = (ConfigNode) nodeEnum.Current;
if (currentNode == null) continue;
name = currentNode.name;
if (name == null) continue;
var moduleTypes = new Dictionary<string, int> {{"MODULE", 0}, {"RESOURCE", 1}, {"RIGIDBODY", 2}};
int nodeType;
if (!moduleTypes.TryGetValue(name, out nodeType))
continue;
switch (nodeType)
{
case 0: // Module
AddModule(currentNode, staticObject);
break;
case 1: // Resource
// Not used any more.
break;
case 2: // Rigidbody
AddRigidBody(currentNode, staticObject.StaticGameObject);
break;
}
}
stopWatch.Stop();
Extensions.LogInfo("Modules loaded for " + staticObject.NameID + ". (" + stopWatch.ElapsedMilliseconds +
"ms)");
}
private static void AddModule(ConfigNode configNode, StaticObject staticObject)
{
string namespaceName = configNode.GetValue("namespace");
string className = configNode.GetValue("name");
if (string.IsNullOrEmpty(namespaceName) || string.IsNullOrEmpty(className))
{
Extensions.LogError(
string.Format(
"Could not add a module to the static object because the node has no name{0} parameter.",
string.IsNullOrEmpty(namespaceName) ? "space" : ""));
return;
}
if (namespaceName == "KerbTown")
{
AddNativeComponent(staticObject.StaticGameObject, configNode, className);
return;
}
Type moduleClass =
AssemblyLoader.loadedAssemblies.SelectMany(asm => asm.assembly.GetTypes())
.FirstOrDefault(t => t.Namespace == namespaceName && t.Name == className);
if (moduleClass == null)
{
Extensions.LogError("Could not obtain module of type \"" + namespaceName + "." + className +
"\" from AssemblyLoader.");
return;
}
var moduleComponent = staticObject.StaticGameObject.AddComponent(moduleClass) as MonoBehaviour;
if (moduleComponent == null)
{
Extensions.LogError("Could not add the obtained module \"" + moduleClass.Name +
"\" to the static game object.");
return;
}
// Assign variables specified in the config for the module.
AssignVariables(configNode, moduleComponent);
// Add the module to the module list. Creating the list if it hasn't been created already.
if (staticObject.ModuleList == null) staticObject.ModuleList = new List<KtComponent>();
staticObject.ModuleList.Add(new KtComponent(moduleComponent));
}
private static void AddNativeComponent(GameObject staticGameObject, Type classType)
{
staticGameObject.AddComponent(classType);
}
private static void AddNativeComponent(GameObject staticGameObject, ConfigNode configNode, string className)
{
switch (className)
{
case "Ladder":
string ladderObjectName = configNode.GetValue("name");
if (string.IsNullOrEmpty(ladderObjectName))
{
Extensions.LogError("The GenericLadder component requires the 'name' field to be set.");
return;
}
var genericLadder = staticGameObject.AddComponent<GenericLadder>();
genericLadder.ObjectName = ladderObjectName;
genericLadder.Setup();
break;
case "TimeOfDayController":
var todController = staticGameObject.AddComponent<TimeOfDayController>();
//Todo add variables for config entries.
break;
case "AnimateOnCollision":
case "AnimateOnClick":
string objectName = configNode.GetValue("collider");
string animName = configNode.GetValue("animationName");
if (string.IsNullOrEmpty(objectName) || string.IsNullOrEmpty(animName))
{
Extensions.LogError(string.Format("GenericAnimation is missing the '{0}' parameter.",
string.IsNullOrEmpty(objectName) ? "collider" : "animationName"));
return;
}
bool shouldHighlight;
float animationSpeed;
var genericAnimationModule = staticGameObject.AddComponent<GenericAnimation>();
if (bool.TryParse(configNode.GetValue("HighlightOnHover"), out shouldHighlight))
genericAnimationModule.HighlightOnHover = shouldHighlight;
if (float.TryParse(configNode.GetValue("animationSpeed"), out animationSpeed))
genericAnimationModule.AnimationSpeed = animationSpeed;
genericAnimationModule.ClassName = className;
genericAnimationModule.AnimationName = animName;
genericAnimationModule.ObjectName = objectName;
genericAnimationModule.Setup();
break;
default:
Extensions.LogWarning("KerbTown." + className + " does not exist or is not accessible.");
Extensions.LogWarning(
"The 'name' parameter should be either: 'AnimateOnCollision' or 'AnimateOnClick'.");
break;
}
}
private static void AddRigidBody(ConfigNode currentNode, GameObject staticGameObject)
{
string objectName = currentNode.GetValue("name");
if (string.IsNullOrEmpty(objectName))
{
Extensions.LogError(
"The 'name' parameter is empty. You must specify the GameObject name for a Rigidbody component to be added.");
return;
}
float fVal;
float rbMass = 1f;
float rbDrag = 0f;
float rbAngularDrag = 0.05f;
bool rbUseGravity;
bool rbIsKinematic;
RigidbodyInterpolation rbInterpolation;
CollisionDetectionMode rbCollisionDetectionMode;
if (float.TryParse(currentNode.GetValue("mass"), out fVal))
rbMass = fVal;
if (float.TryParse(currentNode.GetValue("drag"), out fVal))
rbDrag = fVal;
if (float.TryParse(currentNode.GetValue("angularDrag"), out fVal))
rbAngularDrag = fVal;
if (!bool.TryParse(currentNode.GetValue("useGravity"), out rbUseGravity))
rbUseGravity = false; // Failed, set default.
if (!bool.TryParse(currentNode.GetValue("isKinematic"), out rbIsKinematic))
rbIsKinematic = true; // Failed, set default.
switch (currentNode.GetValue("interpolationMode"))
{
case "Extrapolate":
rbInterpolation = RigidbodyInterpolation.Extrapolate;
break;
case "Interpolate":
rbInterpolation = RigidbodyInterpolation.Interpolate;
break;
default:
rbInterpolation = RigidbodyInterpolation.None;
break;
}
switch (currentNode.GetValue("collisionDetectionMode"))
{
case "ContinuousDynamic":
rbCollisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
break;
case "Continuous":
rbCollisionDetectionMode = CollisionDetectionMode.Continuous;
break;
default:
rbCollisionDetectionMode = CollisionDetectionMode.Discrete;
break;
}