-
Notifications
You must be signed in to change notification settings - Fork 96
/
SCANcontroller.cs
2235 lines (1821 loc) · 75.8 KB
/
SCANcontroller.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
#region license
/*
* [Scientific Committee on Advanced Navigation]
* S.C.A.N. Satellite
*
* SCANcontroller - scenario module that handles all scanning
*
* Copyright (c)2013 damny;
* Copyright (c)2014 David Grandy <david.grandy@gmail.com>;
* Copyright (c)2014 technogeeky <technogeeky@gmail.com>;
* Copyright (c)2014 (Your Name Here) <your email here>; see LICENSE.txt for licensing details.
*/
#endregion
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using Contracts;
using FinePrint.Contracts;
using FinePrint.Utilities;
using SCANsat.SCAN_UI.UI_Framework;
using SCANsat.SCAN_Unity;
using SCANsat.SCAN_Data;
using SCANsat.SCAN_Map;
using SCANsat.SCAN_Platform.Extensions.ConfigNodes;
using SCANsat.SCAN_Palettes;
using SCANsat.SCAN_Toolbar;
using palette = SCANsat.SCAN_UI.UI_Framework.SCANcolorUtil;
using FinePrint.Contracts.Parameters;
using UnityEngine.Profiling;
namespace SCANsat
{
[KSPScenario(ScenarioCreationOptions.AddToAllGames | ScenarioCreationOptions.AddToExistingGames, GameScenes.FLIGHT, GameScenes.SPACECENTER, GameScenes.TRACKSTATION)]
public class SCANcontroller : ScenarioModule
{
public static SCANcontroller controller
{
get { return instance; }
}
private static int minScanAlt = 5000;
private static int maxScanAlt = 500000;
private static int bestScanAlt = 250000;
[KSPField(isPersistant = true)]
public bool mainMapVisible = false;
[KSPField(isPersistant = true)]
public bool mainMapColor = true;
[KSPField(isPersistant = true)]
public bool mainMapTerminator = false;
[KSPField(isPersistant = true)]
public bool mainMapBiome = false;
[KSPField(isPersistant = true)]
public bool mainMapMinimized = false;
[KSPField(isPersistant = true)]
public bool bigMapVisible = false;
[KSPField(isPersistant = true)]
public bool bigMapColor = true;
[KSPField(isPersistant = true)]
public bool bigMapTerminator = false;
[KSPField(isPersistant = true)]
public bool bigMapGrid = true;
[KSPField(isPersistant = true)]
public bool bigMapOrbit = true;
[KSPField(isPersistant = true)]
public bool bigMapWaypoint = true;
[KSPField(isPersistant = true)]
public bool bigMapAnomaly = true;
[KSPField(isPersistant = true)]
public bool bigMapFlag = true;
[KSPField(isPersistant = true)]
public bool bigMapLegend = true;
[KSPField(isPersistant = true)]
public bool bigMapResourceOn = false;
[KSPField(isPersistant = true)]
public string bigMapProjection = "Rectangular";
[KSPField(isPersistant = true)]
public string bigMapType = "Altimetry";
[KSPField(isPersistant = true)]
public string bigMapResource = "Ore";
[KSPField(isPersistant = true)]
public string bigMapBody = "Kerbin";
[KSPField(isPersistant = true)]
public bool zoomMapVesselLock = false;
[KSPField(isPersistant = true)]
public bool zoomMapColor = true;
[KSPField(isPersistant = true)]
public bool zoomMapTerminator = false;
[KSPField(isPersistant = true)]
public bool zoomMapOrbit = true;
[KSPField(isPersistant = true)]
public bool zoomMapIcons = true;
[KSPField(isPersistant = true)]
public bool zoomMapLegend = true;
[KSPField(isPersistant = true)]
public bool zoomMapResourceOn = false;
[KSPField(isPersistant = true)]
public string zoomMapType = "Altimetry";
[KSPField(isPersistant = true)]
public string zoomMapResource = "Ore";
[KSPField(isPersistant = true)]
public int zoomMapState = 0;
[KSPField(isPersistant = true)]
public int overlaySelection = 0;
[KSPField(isPersistant = true)]
public string overlayResource = "Ore";
public Color32 lowBiomeColor32 = new Color(0, 0.46f, 0.02345098f, 1);
public Color32 highBiomeColor32 = new Color(0.7f, 0.2388235f, 0, 1);
public Color32 lowSlopeColorOne32 = new Color(0.004705883f, 0.6f, 0.3788235f, 1);
public Color32 highSlopeColorOne32 = new Color(0.9764706f, 1, 0.4627451f, 1);
public Color32 lowSlopeColorTwo32 = new Color(0.9764706f, 1, 0.4627451f, 1);
public Color32 highSlopeColorTwo32 = new Color(0.94f, 0.2727843f, 0.007372549f, 1);
/* Available resources for overlays; loaded from SCANsat configs; only loaded once */
private static DictionaryValueList<string, SCANresourceGlobal> masterResourceNodes = new DictionaryValueList<string, SCANresourceGlobal>();
/* Resource types loaded from SCANsat configs; only needs to be loaded once */
private static Dictionary<string, SCANresourceType> resourceTypes = new Dictionary<string, SCANresourceType>();
/* Terrain height and color option containers loaded from SCANsat configs; only needs to be loaded once */
private static Dictionary<string, SCANterrainConfig> masterTerrainNodes = new Dictionary<string, SCANterrainConfig>();
/* List of resources currently loaded from resource addons */
private static List<string> loadedResources = new List<string>();
private static List<SCANtype> loadedResourceTypes = new List<SCANtype>();
/* Primary SCANsat vessel dictionary; loaded every time */
public DictionaryValueList<Guid, SCANvessel> knownVessels = new DictionaryValueList<Guid, SCANvessel>();
/* Primary SCANdata dictionary; loaded every time*/
private DictionaryValueList<string, SCANdata> body_data = new DictionaryValueList<string, SCANdata>();
/* MechJeb Landing Target Integration */
private bool mechjebLoaded;
private SCANwaypoint landingTarget;
/* Kopernicus On Demand Loading Data */
private List<CelestialBody> dataBodies = new List<CelestialBody>();
private CelestialBody bigMapBodyPQS;
private CelestialBody zoomMapBody;
private PQSMod KopernicusOnDemand;
private SCAN_UI_MainMap _mainMap;
private SCAN_UI_Instruments _instruments;
private SCAN_UI_BigMap _bigMap;
private SCAN_UI_ZoomMap _zoomMap;
private SCAN_UI_Overlay _overlay;
private SCAN_UI_Settings _settings;
/* App launcher object */
internal SCANappLauncher appLauncher;
/* Used in case the loading process is interupted somehow */
private bool loaded = false;
/* Used to make sure all contracts are loaded */
private bool contractsLoaded = false;
/* Used as holder for vessel id's while loading */
private List<Guid> tempIDs = new List<Guid>();
private bool heightMapsBuilt = false;
private static SCANcontroller instance;
#region Public Accessors
public SCANdata getData(string bodyName)
{
if (body_data.Contains(bodyName))
return body_data[bodyName];
return null;
}
public SCANdata getData(int index)
{
if (body_data.Count > index)
return body_data.At(index);
else
SCANUtil.SCANdebugLog("SCANdata dictionary index out of range; something went wrong here...");
return null;
}
public List<SCANdata> GetAllData
{
get { return body_data.Values.ToList(); }
}
public int GetDataCount
{
get { return body_data.Count; }
}
/* Use this method to protect against duplicate dictionary keys */
public void addToBodyData(CelestialBody b, SCANdata data)
{
if (!body_data.Contains(b.bodyName))
body_data.Add(b.bodyName, data);
else
UnityEngine.Debug.LogError("[SCANsat] Warning: SCANdata Dictionary Already Contains Key of This Type");
}
public static List<SCANterrainConfig> EncodeTerrainConfigs
{
get
{
try
{
return masterTerrainNodes.Values.ToList();
}
catch (Exception e)
{
SCANUtil.SCANlog("Error while saving SCANsat altimetry config data: {0}", e);
}
return new List<SCANterrainConfig>();
}
}
public static void setMasterTerrainNodes(List<SCANterrainConfig> terrainConfigs)
{
masterTerrainNodes.Clear();
try
{
masterTerrainNodes = terrainConfigs.ToDictionary(a => a.Name, a => a);
}
catch (Exception e)
{
SCANUtil.SCANlog("Error while loading SCANsat terrain config settings: {0}", e);
}
}
public static void checkLoadedTerrainNodes()
{
for (int i = 0; i < FlightGlobals.Bodies.Count; i++)
{
CelestialBody b = FlightGlobals.Bodies[i];
if (b == null)
continue;
if (getTerrainNode(b.bodyName) == null)
{
float? clamp = null;
if (b.ocean)
clamp = 0;
float newMax;
try
{
newMax = ((float)CelestialUtilities.GetHighestPeak(b)).Mathf_Round(-2);
}
catch (Exception e)
{
SCANUtil.SCANlog("Error in calculating Max Height for {0}; using default value/n{1}", b.bodyName, e);
newMax = SCANconfigLoader.SCANNode.DefaultMaxHeightRange;
}
SCANUtil.SCANlog("Generating new SCANsat Terrain Config for [{0}] - Max Height: [{1:F0}m]", b.bodyName, newMax);
addToTerrainConfigData(b.bodyName, new SCANterrainConfig(SCANconfigLoader.SCANNode.DefaultMinHeightRange, newMax, clamp, SCANUtil.PaletteLoader(SCANconfigLoader.SCANNode.DefaultPalette, 7), 7, false, false, b));
}
}
}
public static SCANterrainConfig getTerrainNode(string name)
{
if (masterTerrainNodes.ContainsKey(name))
return masterTerrainNodes[name];
else
SCANUtil.SCANlog("SCANsat terrain config [{0}] cannot be found in master terrain storage list", name);
return null;
}
public static void updateTerrainConfig(SCANterrainConfig t)
{
SCANterrainConfig update = getTerrainNode(t.Name);
if (update != null)
{
update.MinTerrain = t.MinTerrain;
update.MaxTerrain = t.MaxTerrain;
update.ClampTerrain = t.ClampTerrain;
update.PalSize = t.PalSize;
update.PalRev = t.PalRev;
update.PalDis = t.PalDis;
update.ColorPal = t.ColorPal;
}
}
public static void addToTerrainConfigData(string name, SCANterrainConfig data)
{
if (!masterTerrainNodes.ContainsKey(name))
masterTerrainNodes.Add(name, data);
else
UnityEngine.Debug.LogError("[SCANsat] Warning: SCANterrain Data Dictionary Already Contains Key Of This Type");
}
public static int MasterResourceCount
{
get { return loadedResources.Count; }
}
public static List<SCANresourceGlobal> EncodeResourceConfigs
{
get
{
try
{
return masterResourceNodes.Values.ToList();
}
catch (Exception e)
{
SCANUtil.SCANlog("Error while saving SCANsat resource config data: {0}", e);
}
return new List<SCANresourceGlobal>();
}
}
public static void setMasterResourceNodes(List<SCANresourceGlobal> resourceConfigs)
{
masterResourceNodes.Clear();
try
{
int l = resourceConfigs.Count;
for (int i = 0; i < l; i++)
{
SCANresourceGlobal r = resourceConfigs[i];
if (r == null)
continue;
if (!masterResourceNodes.Contains(r.Name))
masterResourceNodes.Add(r.Name, r);
}
}
catch (Exception e)
{
SCANUtil.SCANlog("Error while loading SCANsat resource config settings: {0}", e);
}
}
public static SCANresourceGlobal getResourceNode(string resourceName, bool warn = false)
{
if (masterResourceNodes.Contains(resourceName))
return masterResourceNodes[resourceName];
else if (warn)
SCANUtil.SCANlog("SCANsat resource [{0}] cannot be found in master resource storage list", resourceName);
return null;
}
public static SCANresourceGlobal GetFirstResource
{
get
{
if (masterResourceNodes.Count > 0)
return masterResourceNodes.At(0);
else
SCANUtil.SCANlog("SCANsat resource storage list is empty; something probably went wrong here...");
return null;
}
}
public static void updateSCANresource(SCANresourceGlobal r, bool all)
{
SCANresourceGlobal update = getResourceNode(r.Name, true);
if (update != null)
{
update.MinColor = r.MinColor;
update.MaxColor = r.MaxColor;
update.Transparency = r.Transparency;
if (all)
{
for (int i = 0; i < update.getBodyCount; i++)
{
SCANresourceBody b = update.getBodyConfig(i);
if (b != null)
{
SCANresourceBody bNew = r.getBodyConfig(b.BodyName);
if (bNew != null)
{
b.MinValue = bNew.MinValue;
b.MaxValue = bNew.MaxValue;
}
}
}
}
else
{
SCANresourceBody b = update.getBodyConfig(r.CurrentBody.BodyName);
if (b != null)
{
b.MinValue = r.CurrentBody.MinValue;
b.MaxValue = r.CurrentBody.MaxValue;
}
}
}
}
public static void addToResourceData(string name, SCANresourceGlobal res)
{
if (!masterResourceNodes.Contains(name))
{
masterResourceNodes.Add(name, res);
}
else
UnityEngine.Debug.LogError(string.Format("[SCANsat] Warning: SCANResource Dictionary Already Contains Key of This Type: Resource: {0}", name));
}
public static SCANresourceType getResourceType(string name, bool warn = true)
{
if (resourceTypes.ContainsKey(name))
return resourceTypes[name];
else if (warn)
SCANUtil.SCANlog("SCANsat resource type [{0}] cannot be found in master resource type storage list", name);
return null;
}
public static bool getLoadedResourceTypeStatus(SCANtype type, bool warn = false)
{
for (int i = loadedResourceTypes.Count - 1; i >= 0; i--)
{
if (loadedResourceTypes[i] == type)
return true;
}
if (warn)
SCANUtil.SCANlog("SCANsat resource type [{0}] cannot be found in loaded resource type storage list", type);
return false;
}
public static void addToResourceTypes(string name, SCANresourceType type)
{
if (!resourceTypes.ContainsKey(name))
{
resourceTypes.Add(name, type);
}
else
UnityEngine.Debug.LogError(string.Format("[SCANsat] Warning: SCANResourceType Dictionary Already Contains Key of This Name: SCAN Resource Type: {0}", name));
}
public static void addToLoadedResourceNames(string name)
{
if (!loadedResources.Contains(name))
loadedResources.Add(name);
else
UnityEngine.Debug.LogError(string.Format("[SCANsat] Warning: Loaded Resource List Already Contains Resource Of Name: {0}", name));
}
public static void addToLoadedResourceTypes(SCANtype type)
{
if (!loadedResourceTypes.Contains(type))
loadedResourceTypes.Add(type);
else
UnityEngine.Debug.LogError(string.Format("[SCANsat] Warning: Loaded Resource Type List Already Contains Resource Of Type: {0}", type));
}
public static List<SCANresourceGlobal> setLoadedResourceList()
{
List<SCANresourceGlobal> rList = new List<SCANresourceGlobal>();
SCANresourceGlobal ore = null;
int l = loadedResources.Count;
for (int i = 0; i < l; i++)
{
string r = loadedResources[i];
if (string.IsNullOrEmpty(r))
continue;
if (masterResourceNodes.Contains(r))
{
if (r != "Ore")
rList.Add(masterResourceNodes[r]);
else
ore = masterResourceNodes[r];
}
}
if (ore != null)
rList.Insert(0, ore);
return rList;
}
public static List<SCANresourceGlobal> resources()
{
return masterResourceNodes.Values.ToList();
}
public List<SCANvessel> Known_Vessels
{
get { return knownVessels.Values.ToList(); }
}
public int ActiveSensors
{
get { return activeSensors; }
}
public int ActiveVessels
{
get { return activeVessels; }
}
public int ActualPasses
{
get { return actualPasses; }
}
public bool ContractsLoaded
{
get { return contractsLoaded; }
}
public bool MechJebLoaded
{
get { return mechjebLoaded; }
set { mechjebLoaded = value; }
}
public SCANwaypoint LandingTarget
{
get { return landingTarget; }
set { landingTarget = value; }
}
public class OnMJTargetSet : UnityEvent<Vector2d, CelestialBody> { }
public OnMJTargetSet MJTargetSet = new OnMJTargetSet();
#endregion
#region save/load
public override void OnLoad(ConfigNode node)
{
ConfigNode node_vessels = node.GetNode("Scanners");
if (node_vessels != null)
{
SCANUtil.SCANlog("SCANsat Controller: Loading {0} known vessels", node_vessels.CountNodes);
foreach (ConfigNode node_vessel in node_vessels.GetNodes("Vessel"))
{
Guid id = node_vessel.parse("guid", new Guid());
if (id == new Guid())
{
SCANUtil.SCANlog("Something Went Wrong Loading This SCAN Vessel; Moving On To The Next");
continue;
}
foreach (ConfigNode node_sensor in node_vessel.GetNodes("Sensor"))
{
int sensor = node_sensor.parse("type", (int)0);
double fov = node_sensor.parse("fov", 3d);
double min_alt = node_sensor.parse("min_alt", (double)minScanAlt);
double max_alt = node_sensor.parse("max_alt", (double)maxScanAlt);
double best_alt = node_sensor.parse("best_alt", (double)bestScanAlt);
registerSensorTemp(id, (SCANtype)sensor, fov, min_alt, max_alt, best_alt);
tempIDs.Add(id);
}
}
}
ConfigNode node_progress = node.GetNode("Progress");
if (node_progress != null)
{
foreach (ConfigNode node_body in node_progress.GetNodes("Body"))
{
string body_name = node_body.parse("Name", "");
if (string.IsNullOrEmpty(body_name))
{
SCANUtil.SCANlog("SCANsat Controller: Error while loading Celestial Body data; skipping value...");
continue;
}
SCANUtil.SCANlog("SCANsat Controller: Loading map for {0}", body_name);
CelestialBody body;
try
{
body = FlightGlobals.Bodies.FirstOrDefault(b => b.bodyName == body_name);
}
catch (Exception e)
{
UnityEngine.Debug.LogError(string.Format("[SCANsat] Error in loading Celestial Body [{0}]...\n{1}", body_name, e));
continue;
}
if (body != null)
{
SCANdata data = getData(body.bodyName);
if (data == null)
data = new SCANdata(body);
if (!body_data.Contains(body_name))
body_data.Add(body_name, data);
else
body_data[body_name] = data;
try
{
string mapdata = node_body.parse("Map", "");
if (string.IsNullOrEmpty(mapdata))
{
SCANUtil.SCANlog("SCANsat Controller: Error while loading Celestial Body map data; skipping value...");
continue;
}
data.integerDeserialize(mapdata);
}
catch (Exception e)
{
SCANUtil.SCANlog("Something Went Wrong Loading Scanning Data; Resetting Coverage: {0}", e);
data.reset();
// fail somewhat gracefully; don't make the save unloadable
}
try
{
if (SCANmainMenuLoader.MechJebLoaded && SCAN_Settings_Config.Instance.MechJebTarget && SCAN_Settings_Config.Instance.MechJebTargetLoad)
{
string targetName = node_body.parse("LandingTarget", "");
if (!string.IsNullOrEmpty(targetName))
loadWaypoint(targetName, body);
}
data.Disabled = node_body.parse("Disabled", false);
float min = node_body.parse("MinHeightRange", data.TerrainConfig.DefaultMinHeight);
float max = node_body.parse("MaxHeightRange", data.TerrainConfig.DefaultMaxHeight);
float? clampState = node_body.parse("ClampHeight", (float?)null);
int pSize = node_body.parse("PaletteSize", data.TerrainConfig.DefaultPaletteSize);
bool pRev = node_body.parse("PaletteReverse", data.TerrainConfig.DefaultReverse);
bool pDis = node_body.parse("PaletteDiscrete", data.TerrainConfig.DefaultDiscrete);
string paletteName = node_body.parse("PaletteName", "");
if (string.IsNullOrEmpty(paletteName))
paletteName = data.TerrainConfig.DefaultPalette.Name;
SCANPalette dataPalette = SCANUtil.PaletteLoader(paletteName, pSize);
if (dataPalette.Hash == SCAN_Palette_Config.DefaultPalette.GetPalette(0).Hash)
{
paletteName = "Default";
pSize = 7;
}
SCANterrainConfig dataTerrainConfig = getTerrainNode(body.bodyName);
if (dataTerrainConfig == null)
dataTerrainConfig = new SCANterrainConfig(min, max, clampState, dataPalette, pSize, pRev, pDis, body);
else
setNewTerrainConfigValues(dataTerrainConfig, min, max, clampState, dataPalette, pSize, pRev, pDis);
data.TerrainConfig = dataTerrainConfig;
}
catch (Exception e)
{
SCANUtil.SCANlog("Error Loading SCANdata; Reverting To Default Settings: {0}", e);
}
}
}
}
ConfigNode node_resources = node.GetNode("SCANResources");
if (node_resources != null)
{
foreach (ConfigNode node_resource_type in node_resources.GetNodes("ResourceType"))
{
if (node_resource_type != null)
{
loadCustomResourceValues(node_resource_type);
}
}
}
loaded = true;
}
public override void OnSave(ConfigNode node)
{
int l = knownVessels.Count;
ConfigNode node_vessels = new ConfigNode("Scanners");
for (int i = 0; i < l; i++)
{
SCANvessel sv = knownVessels.At(i);
ConfigNode node_vessel = new ConfigNode("Vessel");
node_vessel.AddValue("guid", sv.id.ToString());
if (sv.vessel != null)
node_vessel.AddValue("name", sv.vessel.vesselName);
foreach (SCANsensor sensor in sv.sensors)
{
ConfigNode node_sensor = new ConfigNode("Sensor");
node_sensor.AddValue("type", (int)sensor.sensor);
node_sensor.AddValue("fov", sensor.fov);
node_sensor.AddValue("min_alt", sensor.min_alt);
node_sensor.AddValue("max_alt", sensor.max_alt);
node_sensor.AddValue("best_alt", sensor.best_alt);
node_vessel.AddNode(node_sensor);
}
node_vessels.AddNode(node_vessel);
}
node.AddNode(node_vessels);
if (body_data != null)
{
ConfigNode node_progress = new ConfigNode("Progress");
foreach (string body_name in body_data.Keys)
{
ConfigNode node_body = new ConfigNode("Body");
SCANdata body_scan = body_data[body_name];
node_body.AddValue("Name", body_name);
node_body.AddValue("Disabled", body_scan.Disabled);
if (SCANmainMenuLoader.MechJebLoaded && SCAN_Settings_Config.Instance.MechJebTarget && SCAN_Settings_Config.Instance.MechJebTargetLoad)
{
SCANwaypoint w = body_scan.Waypoints.FirstOrDefault(a => a.LandingTarget);
if (w != null)
node_body.AddValue("LandingTarget", string.Format("{0:N4},{1:N4}", w.Latitude, w.Longitude));
}
node_body.AddValue("MinHeightRange", body_scan.TerrainConfig.MinTerrain / body_scan.TerrainConfig.MinHeightMultiplier);
node_body.AddValue("MaxHeightRange", body_scan.TerrainConfig.MaxTerrain / body_scan.TerrainConfig.MaxHeightMultiplier);
if (body_scan.TerrainConfig.ClampTerrain != null)
node_body.AddValue("ClampHeight", body_scan.TerrainConfig.ClampTerrain / body_scan.TerrainConfig.ClampHeightMultiplier);
node_body.AddValue("PaletteName", body_scan.TerrainConfig.ColorPal.Name);
node_body.AddValue("PaletteSize", body_scan.TerrainConfig.PalSize);
node_body.AddValue("PaletteReverse", body_scan.TerrainConfig.PalRev);
node_body.AddValue("PaletteDiscrete", body_scan.TerrainConfig.PalDis);
node_body.AddValue("Map", body_scan.integerSerialize());
node_progress.AddNode(node_body);
}
node.AddNode(node_progress);
}
if (resourceTypes.Count > 0 && masterResourceNodes.Count > 0)
{
ConfigNode node_resources = new ConfigNode("SCANResources");
foreach (SCANresourceGlobal r in masterResourceNodes.Values)
{
if (r != null)
{
ConfigNode node_resource_type = new ConfigNode("ResourceType");
node_resource_type.AddValue("Resource", r.Name);
node_resource_type.AddValue("MinColor", ConfigNode.WriteColor(r.MinColor));
node_resource_type.AddValue("MaxColor", ConfigNode.WriteColor(r.MaxColor));
node_resource_type.AddValue("Transparency", r.Transparency);
string rMinMax = saveResources(r);
node_resource_type.AddValue("MinMaxValues", rMinMax);
node_resources.AddNode(node_resource_type);
}
}
node.AddNode(node_resources);
}
}
#endregion
public override void OnAwake()
{
instance = this;
if (SCAN_Settings_Config.Instance == null)
return;
SCANUtil.fillLocalizedNames();
lowBiomeColor32 = SCAN_Settings_Config.Instance.LowBiomeColor;
highBiomeColor32 = SCAN_Settings_Config.Instance.HighBiomeColor;
lowSlopeColorOne32 = SCAN_Settings_Config.Instance.BottomLowSlopeColor;
highSlopeColorOne32 = SCAN_Settings_Config.Instance.BottomHighSlopeColor;
lowSlopeColorTwo32 = SCAN_Settings_Config.Instance.TopLowSlopeColor;
highSlopeColorTwo32 = SCAN_Settings_Config.Instance.TopHighSlopeColor;
}
private void Start()
{
for (int i = tempIDs.Count - 1; i >= 0; i--)
{
finishRegistration(tempIDs[i]);
}
GameEvents.OnScienceRecieved.Add(watcher);
GameEvents.OnOrbitalSurveyCompleted.Add(onSurvey);
GameEvents.onVesselSOIChanged.Add(SOIChange);
GameEvents.onVesselCreate.Add(newVesselCheck);
GameEvents.onPartCouple.Add(dockingEventCheck);
GameEvents.Contract.onContractsLoaded.Add(contractsCheck);
GameEvents.Contract.onParameterChange.Add(onParamChange);
if (HighLogic.LoadedSceneIsFlight)
{
if (!body_data.Contains(FlightGlobals.currentMainBody.bodyName))
body_data.Add(FlightGlobals.currentMainBody.bodyName, new SCANdata(FlightGlobals.currentMainBody));
try
{
_mainMap = new SCAN_UI_MainMap();
_bigMap = new SCAN_UI_BigMap();
_zoomMap = new SCAN_UI_ZoomMap();
_instruments = new SCAN_UI_Instruments();
_overlay = new SCAN_UI_Overlay();
_settings = new SCAN_UI_Settings();
}
catch (Exception e)
{
SCANUtil.SCANlog("Something Went Wrong Initializing UI Objects:\n{0}", e);
}
StartCoroutine(WaitForScienceUpdate());
}
else if (HighLogic.LoadedSceneHasPlanetarium)
{
if (!body_data.Contains(Planetarium.fetch.Home.bodyName))
body_data.Add(Planetarium.fetch.Home.bodyName, new SCANdata(Planetarium.fetch.Home));
try
{
_bigMap = new SCAN_UI_BigMap();
_settings = new SCAN_UI_Settings();
_zoomMap = new SCAN_UI_ZoomMap();
if (HighLogic.LoadedScene == GameScenes.TRACKSTATION)
{
_overlay = new SCAN_UI_Overlay();
}
}
catch (Exception e)
{
SCANUtil.SCANlog("Something Went Wrong Initializing UI Objects:\n{0}", e);
}
}
if (SCAN_Settings_Config.Instance.StockToolbar)
appLauncher = gameObject.AddComponent<SCANappLauncher>();
if (SCAN_Settings_Config.Instance.DisableStockResource && SCAN_Settings_Config.Instance.UseStockTreshold)
{
for (int i = FlightGlobals.Bodies.Count - 1; i >= 0; i--)
{
CelestialBody b = FlightGlobals.Bodies[i];
checkResourceScanStatus(b);
}
}
else if (!SCAN_Settings_Config.Instance.DisableStockResource && SCAN_Settings_Config.Instance.InstantScan)
{
for (int i = FlightGlobals.Bodies.Count - 1; i >= 0; i--)
{
CelestialBody b = FlightGlobals.Bodies[i];
checkStockResourceScanStatus(b);
}
}
for (int i = body_data.Count - 1; i >= 0; i--)
{
body_data.At(i).updateCoverage();
}
}
private void Update()
{
if (SCAN_Settings_Config.Instance.BackgroundScanning && loaded)
scanFromAllVessels();
if (!heightMapsBuilt)
checkHeightMapStatus();
}
private IEnumerator WaitForScienceUpdate()
{
while (!FlightGlobals.ready || FlightGlobals.ActiveVessel == null)
yield return null;
SCANUtil.UpdateAllVesselData(FlightGlobals.ActiveVessel);
}
public void checkStockResourceScanStatus(CelestialBody body)
{
if (SCAN_Settings_Config.Instance.DisableStockResource || !SCAN_Settings_Config.Instance.InstantScan)
return;
if (body == null)
return;
if (!ResourceMap.Instance.IsPlanetScanned(body.flightGlobalsIndex))
return;
if (SCANUtil.GetCoverage((int)SCANtype.AllResources, body) >= 100)
return;
SCANdata data = getData(body.bodyName);
if (data == null)
return;
data.fillResourceMap();
}
public void checkResourceScanStatus(CelestialBody body)
{
if (!SCAN_Settings_Config.Instance.UseStockTreshold)
return;
if (body == null)
return;
if (ResourceMap.Instance.IsPlanetScanned(body.flightGlobalsIndex))
return;
SCANdata data = getData(body.bodyName);
if (data == null)
return;
if (SCANUtil.getCoveragePercentage(data, SCANtype.FuzzyResources) > (SCAN_Settings_Config.Instance.StockTreshold * 100))
{
SCANUtil.SCANlog("SCANsat resource scanning for {0} meets threshold value [{1:P0}]\nConducting stock orbital resource scan...", body.bodyName, SCAN_Settings_Config.Instance.StockTreshold);
ResourceMap.Instance.UnlockPlanet(body.flightGlobalsIndex);
}
}
private int dataStep, dataStart;
private bool currentlyBuilding;
private SCANdata buildingData;
private void checkHeightMapStatus()
{
if (!currentlyBuilding)
{
for (int i = 0; i < body_data.Count; i++)
{
buildingData = getData(i);
if (buildingData == null)
continue;
if (buildingData.Built)
continue;
if (buildingData.MapBuilding || buildingData.OverlayBuilding)
continue;
buildingData.ControllerBuilding = true;
currentlyBuilding = true;
return;
}
}
else
{
if (buildingData == null)
{
currentlyBuilding = false;
return;
}
if (buildingData.Built)
{
currentlyBuilding = false;
buildingData.ControllerBuilding = false;
return;
}
if (buildingData.ControllerBuilding)
{
buildingData.generateHeightMap(ref dataStep, ref dataStart, 120);
return;
}
}
SCANUtil.SCANlog("All Height Maps Generated");
buildingData = null;
heightMapsBuilt = true;
}
private void OnDestroy()
{
GameEvents.OnScienceRecieved.Remove(watcher);
GameEvents.OnOrbitalSurveyCompleted.Remove(onSurvey);
GameEvents.onVesselSOIChanged.Remove(SOIChange);