forked from theSpeare/RoverScience-Revisit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RoverScience.cs
625 lines (508 loc) · 20.1 KB
/
RoverScience.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
using KSP.Localization;
using System;
using System.Linq;
using UnityEngine;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// ROVERSCIENCE PLUGIN WAS CREATED BY THESPEARE //
// FOR KERBAL SPACE PROGRAM - PLEASE SEE FORUM THREAD FOR DETAILS //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
namespace RoverScience
{
public class RoverScience : PartModule
{
private const float HomeWorldScienceScalar = 0.01f;
private const float SunScienceScalar = 0f;
private const float NearMoonScienceScalar = 0.3f;
private const float FarMoonScienceScalar = 0.2f;
// Not necessarily updated per build. Mostly updated per major commits
public readonly string RSVersion = typeof(RoverScience).Assembly.GetName().Version.ToString();
public static RoverScience Instance = null;
public static CelestialBody HomeWorld;
public System.Random rand = new System.Random ();
public ModuleScienceContainer container;
public ModuleCommand command;
public Rover rover;
public int levelMaxDistance = 1;
public int levelPredictionAccuracy = 1;
public int levelAnalyzedDecay = 2;
public readonly int maximum_levelMaxDistance = 5;
public readonly int maximum_predictionAccuracy = 5;
public readonly int maximum_levelAnalyzedDecay = 5;
public double CurrentPredictionAccuracy
{
get
{
return GetUpgradeValue(RSUpgrade.predictionAccuracy, levelPredictionAccuracy);
}
}
public double CurrentMaxDistance
{
get
{
return GetUpgradeValue(RSUpgrade.maxDistance, levelMaxDistance);
}
}
private RoverScienceDB DB
{
get { return RoverScienceDB.Instance; }
}
public RoverScienceGUI roverScienceGUI = new RoverScienceGUI();
public double distCounter;
[KSPField (isPersistant = true)]
public int amountOfTimesAnalyzed = 0;
// Leave this alone. PartModule has its own vessel class which SHOULD do the job but
// for some reason removing this seemed to destroy a lot of function
Vessel Vessel {
get {
if (HighLogic.LoadedSceneIsFlight) {
return FlightGlobals.ActiveVessel;
} else {
Utilities.LogVerbose ("RoverScience.Vessel null! - not flight");
return null;
}
}
}
public float scienceMaxRadiusBoost = 1;
public double ScienceDecayPercentage
{
get
{
return Math.Round((1 - ScienceDecayScalar) * 100);
}
}
public float ScienceDecayScalar {
get {
return GetScienceDecayScalar (amountOfTimesAnalyzed);
}
}
public float BodyScienceScalar {
get {
return GetBodyScienceScalar (Vessel.mainBody);
}
}
public float BodyScienceCap {
get {
return GetBodyScienceCap (Vessel.mainBody);
}
}
[KSPEvent (guiActive = true, guiName = "#LOC_RoverScience_GUI_ToggleTerminal")] // Toggle Rover Terminal
private void ShowGUI ()
{
roverScienceGUI.consoleGUI.Toggle ();
DrawWaypoint.Instance.ToggleMarker();
}
[KSPAction ("#LOC_RoverScience_GUI_ActivateConsole", actionGroup = KSPActionGroup.None)] // Activate Console
private void ShowGUIAction (KSPActionParam param)
{
if (IsPrimary)
ShowGUI ();
}
void OnDestroy ()
{
Utilities.LogVerbose ("RoverScience OnDestroy()");
}
public void OnGUI()
{
roverScienceGUI.DrawGUI();
}
public override void OnLoad (ConfigNode vesselNode)
{
Utilities.LogVerbose("#X1 RoverScience OnLoad @" + DateTime.Now);
Instance = this;
if (rover == null)
{
Utilities.LogVerbose("rover was null, creating new rover class (OnLoad)");
rover = new Rover();
}
if (DB != null) DB.UpdateRoverScience();
}
public override void OnSave(ConfigNode vesselNode)
{
Utilities.LogVerbose("RoverScience OnSave @" + DateTime.Now);
if (DB != null) DB.UpdateDB();
}
public override void OnStart (PartModule.StartState state)
{
if (HighLogic.LoadedSceneIsFlight) {
if (IsPrimary) {
Utilities.Log ("Initiated! Version: " + RSVersion);
Instance = this;
Utilities.LogVerbose ("RS Instance set - " + Instance);
HomeWorld = FlightGlobals.Bodies.Where(cb => cb.isHomeWorld).First(); // TODO: move this somewhere more appropriate
container = part.Modules ["ModuleScienceContainer"] as ModuleScienceContainer;
command = part.Modules ["ModuleCommand"] as ModuleCommand;
// HACK: Must be called here otherwise they won't run their constructors for some reason
if (rover == null)
{
Utilities.LogVerbose("rover was null, creating new rover class (OnStart)");
rover = new Rover();
}
rover.scienceSpot = new ScienceSpot (Instance);
rover.landingSpot = new LandingSpot (Instance);
if (DB != null) DB.UpdateRoverScience();
rover.SetClosestAnomaly();
} else {
Utilities.Log ("ONSTART - Not primary");
}
// HACK: instance null unexpected.
if (Instance == null)
{
Instance = this;
Utilities.Log("Instance was null; workaround fix by declaring Instance anyway");
}
}
}
public override void OnUpdate ()
{
if (IsPrimary) {
if (roverScienceGUI.consoleGUI.isOpen) {
// Calculate rover traveled distance
if (rover.ValidStatus)
rover.CalculateDistanceTraveled (TimeWarp.deltaTime);
rover.landingSpot.SetSpot ();
if (rover.landingSpot.established)
{
rover.SetRoverLocation();
}
if ((!rover.scienceSpot.established) && (!rover.ScienceSpotReached) && (ScienceDecayPercentage < 100))
{
rover.scienceSpot.CheckAndSet();
}
}
}
KeyboardShortcuts ();
}
// Much credit to a.g. as his source helped to figure out how to utilize the experiment and its data
// https://github.com/angavrilov/ksp-surface-survey/blob/master/SurfaceSurvey.cs#L276
public void AnalyzeScienceSample ()
{
if (rover.ScienceSpotReached) {
ScienceExperiment sciExperiment = ResearchAndDevelopment.GetExperiment ("RoverScienceExperiment");
ScienceSubject sciSubject = ResearchAndDevelopment.GetExperimentSubject(sciExperiment, ExperimentSituations.SrfLanded, Vessel.mainBody, "", "");
// 20 science per data
sciSubject.subjectValue = 20;
sciSubject.scienceCap = BodyScienceCap;
// Divide by 20 to convert to data form
float sciData = (rover.scienceSpot.potentialScience) / sciSubject.subjectValue;
Utilities.LogVerbose ("sciData (potential/20): " + sciData);
// Apply multipliers
if (rover.AnomalySpotReached)
{
Utilities.LogVerbose("analyzed science at anomaly");
if (!rover.anomaliesAnalyzed.Contains(rover.closestAnomaly.id))
{
rover.anomaliesAnalyzed.Add(rover.closestAnomaly.id);
Utilities.LogVerbose($"added anomaly id: {rover.closestAnomaly.id} to save!");
}
} else
{
// if a normal spot, we shall apply factors
Utilities.LogVerbose("analyzed science at science spot");
sciData = sciData * ScienceDecayScalar * BodyScienceScalar * scienceMaxRadiusBoost;
}
Utilities.LogVerbose("rover.scienceSpot.potentialScience: " + rover.scienceSpot.potentialScience);
Utilities.LogVerbose("sciData (post scalar): " + sciData);
Utilities.LogVerbose("scienceDecayScalar: " + ScienceDecayScalar);
Utilities.LogVerbose("bodyScienceScalar: " + BodyScienceScalar);
if (sciData > 0.1) {
if (StoreScience (container, sciSubject, sciData)) {
container.ReviewData ();
amountOfTimesAnalyzed++;
Utilities.LogVerbose ("Science retrieved! - " + sciData);
} else {
Utilities.Log ("Failed to add science to container!");
}
} else {
ScreenMessages.PostScreenMessage ("#LOC_RoverScience_GUI_ScienceTooLow", 5, ScreenMessageStyle.UPPER_CENTER); // Science value was too low - deleting data!
}
rover.scienceSpot.Reset ();
} else {
Utilities.LogVerbose ("Tried to analyze while not at spot?");
}
}
public bool StoreScience (ModuleScienceContainer container, ScienceSubject subject, float data)
{
if (container.capacity > 0 && container.GetScienceCount () >= container.capacity)
return false;
if (container.GetStoredDataCount () != 0)
return false;
float xmitValue = 0.85f;
float labBoost = 0.1f;
ScienceData new_data = new ScienceData (data, xmitValue, labBoost, subject.id, subject.title);
if (container.AddData (new_data))
return true;
return false;
}
private float GetScienceDecayScalar(int numberOfTimes)
{
// This is the equation that models the decay of science per analysis made
// y = 1.20^(-0.9*(x-2))
// Always subject to adjustment
//double scalar = (1.20 * Math.Exp (-0.9 * (numberOfTimes - levelAnalyzedDecay)));
double scalar = (1.20 * Math.Exp(-0.4 * (numberOfTimes - levelAnalyzedDecay)));
// decay pattern as such: 0.8, 0.54, 0.36, 0.24, 0.16, 0.1
if (scalar > 1)
{
return 1;
}
return (float)scalar;
}
private float GetBodyScienceScalar (CelestialBody currentBody)
{
if (currentBody.isHomeWorld)
return HomeWorldScienceScalar;
if (currentBody == FlightGlobals.Bodies[0])
return SunScienceScalar;
if (currentBody.HasParent(HomeWorld))
{
return NearMoonScienceScalar; // TODO: Distinguish Mun/Minmus (and generic case)
}
return 1;
}
private float GetBodyScienceCap (CelestialBody currentBody)
{
float scalar = 1;
float scienceCap = 1500;
if (currentBody.isHomeWorld)
scalar = 0.09f;
else if (currentBody == FlightGlobals.Bodies[0])
scalar = 0f;
else if (currentBody.HasParent(HomeWorld))
{
scalar = 0.3f;
}
else
scalar = 1f;
return (scalar * scienceCap);
}
public string GetUpgradeName(RSUpgrade upgrade)
{
switch (upgrade)
{
case (RSUpgrade.maxDistance):
return Localizer.GetStringByTag("#LOC_RoverScience_GUI_MaxScanDistance"); // "Max Scan Distance";
case (RSUpgrade.predictionAccuracy):
return Localizer.GetStringByTag("#LOC_RoverScience_GUI_PrecisionAccuracy"); // "Prediction Accuracy";
case (RSUpgrade.analyzedDecay):
return Localizer.GetStringByTag("#LOC_RoverScience_GUI_DecayLimit"); // "Analyzed Decay Limit";
default:
return Localizer.GetStringByTag("#LOC_RoverScience_GUI_ErrorUpgradeName"); // "Failed to resolve getUpgradeName";
}
}
public float GetUpgradeCost(RSUpgrade upgrade, int level)
{
if (level == 0) level = 1;
if (level > GetUpgradeMaxLevel(upgrade)) return -1;
switch (upgrade)
{
case (RSUpgrade.maxDistance):
if (level == 1) return 200;
if (level == 2) return 250;
if (level == 3) return 400;
if (level == 4) return 550;
if (level == 5) return 1000;
return -1;
case (RSUpgrade.predictionAccuracy):
if (level == 1) return 200;
if (level == 2) return 400;
if (level == 3) return 500;
if (level == 4) return 1000;
if (level == 5) return 2100;
return -1;
case (RSUpgrade.analyzedDecay):
if (level == 1) return 0;
if (level == 2) return 0;
if (level == 3) return 1000;
if (level == 4) return 1000;
if (level == 5) return 1000;
return -1;
default:
return -1;
}
}
public string GetUpgradeValueString(RSUpgrade upgrade, int level)
{
// This will come with unit for display
switch (upgrade)
{
case (RSUpgrade.maxDistance):
if (levelMaxDistance >= maximum_levelMaxDistance)
{
return Localizer.GetStringByTag("#LOC_RoverScience_GUI_Max"); // "MAX";
}
else
{
return Localizer.Format("#LOC_RoverScience_GUI_Metres", GetUpgradeValue(RSUpgrade.maxDistance, level)); // <<1>>m
}
case (RSUpgrade.predictionAccuracy):
if (levelPredictionAccuracy >= maximum_predictionAccuracy)
{
return Localizer.GetStringByTag("#LOC_RoverScience_GUI_Max"); // "MAX";
}
else
{
return Localizer.Format("#LOC_RoverScience_GUI_Percentage", GetUpgradeValue(RSUpgrade.predictionAccuracy, level)); // <<1>>%
}
case (RSUpgrade.analyzedDecay):
if (levelAnalyzedDecay >= maximum_levelAnalyzedDecay)
{
return Localizer.GetStringByTag("#LOC_RoverScience_GUI_Max"); // "MAX";
}
else
{
return Localizer.Format("#LOC_RoverScience_GUI_nvalue", GetUpgradeValue(RSUpgrade.analyzedDecay, level)); // <<1>>n
}
default:
return "Unable to resolve getUpgradeValueString()";
}
}
public double GetUpgradeValue(RSUpgrade upgrade, int level)
{
if (level == 0) level = 1;
if (level > GetUpgradeMaxLevel(upgrade)) return -1;
switch (upgrade)
{
case (RSUpgrade.maxDistance):
if (level == 1) return 100;
if (level == 2) return 500;
if (level == 3) return 1000;
if (level == 4) return 2000;
if (level == 5) return 4000;
return -1;
case (RSUpgrade.predictionAccuracy):
if (level == 1) return 10;
if (level == 2) return 20;
if (level == 3) return 50;
if (level == 4) return 70;
if (level == 5) return 80;
return -1;
case (RSUpgrade.analyzedDecay):
if (level <= 2) return 2;
if (level == 3) return 3;
if (level == 4) return 4;
if (level == 5) return 5;
return -1;
default:
return -1;
}
}
public int GetUpgradeLevel(RSUpgrade upgradeType)
{
switch (upgradeType)
{
case (RSUpgrade.maxDistance):
return levelMaxDistance;
case (RSUpgrade.predictionAccuracy):
return levelPredictionAccuracy;
case (RSUpgrade.analyzedDecay):
return levelAnalyzedDecay;
default:
return -1;
}
}
public void SetUpgradeLevel(RSUpgrade upgradeType, int newValue)
{
if (upgradeType == RSUpgrade.maxDistance) {
levelMaxDistance = newValue;
} else
if (upgradeType == RSUpgrade.predictionAccuracy) {
levelPredictionAccuracy = newValue;
} else
if (upgradeType == RSUpgrade.analyzedDecay)
{
levelAnalyzedDecay = newValue;
}
}
public int GetUpgradeMaxLevel(RSUpgrade upgradeType)
{
switch (upgradeType)
{
case (RSUpgrade.maxDistance):
return maximum_levelMaxDistance;
case (RSUpgrade.predictionAccuracy):
return maximum_predictionAccuracy;
case (RSUpgrade.analyzedDecay):
return maximum_levelAnalyzedDecay;
default:
return -1;
}
}
public void UpgradeTech(RSUpgrade upgradeType)
{
Utilities.LogVerbose ("upgradeTech called: " + upgradeType);
int nextLevel = GetUpgradeLevel(upgradeType) + 1;
int currentLevel = GetUpgradeLevel(upgradeType);
int maxLevel = GetUpgradeMaxLevel(upgradeType);
float nextCost = GetUpgradeCost(upgradeType, nextLevel);
string upgradeName = GetUpgradeName(upgradeType);
// MAX LEVEL REACHED
if (currentLevel >= maxLevel)
{
ScreenMessages.PostScreenMessage(Localizer.GetStringByTag("#LOC_RoverScience_GUI_MaxUpgradeLevel"), 3, ScreenMessageStyle.UPPER_CENTER); // Max Level reached for this upgrade
return;
}
// NOT ENOUGH SCIENCE
if (nextCost > ResearchAndDevelopment.Instance.Science)
{
ScreenMessages.PostScreenMessage(Localizer.GetStringByTag("#LOC_RoverScience_GUI_NotEnoughScience"), 3, ScreenMessageStyle.UPPER_CENTER); // "Not enough science to upgrade"
return;
}
// UPGRADE METHOD
if (upgradeType == RSUpgrade.maxDistance) {
levelMaxDistance++;
Utilities.LogVerbose ("Upgraded levelMaxDistance. Now level: " + levelMaxDistance);
} else if (upgradeType == RSUpgrade.predictionAccuracy) {
levelPredictionAccuracy++;
Utilities.LogVerbose ("Upgraded predictionAccuracy. Now level: " + levelPredictionAccuracy);
} else if (upgradeType == RSUpgrade.analyzedDecay)
{
levelAnalyzedDecay++;
Utilities.LogVerbose("Upgraded levelAnalyzedDecay. Now level: " + levelAnalyzedDecay);
}
ResearchAndDevelopment.Instance.CheatAddScience(-nextCost);
ScreenMessages.PostScreenMessage(Localizer.Format("#LOC_RoverScience_GUI_UpgradeMessage", upgradeName), 3, ScreenMessageStyle.UPPER_CENTER); // <<1>> has been upgraded"
}
public void SetScienceMaxRadiusBoost(int maxRadius)
{
//maxRadius' maximum value would only ever reach 2km (2000 meters)
//this method updates the factor used to increase the science depending
//on how far a given science spot has been spawned
if (maxRadius < 150)
scienceMaxRadiusBoost = 1;
scienceMaxRadiusBoost = ((1f / 2000f) * maxRadius) + 1f;
}
public void KeyboardShortcuts ()
{
if (HighLogic.LoadedSceneIsFlight) {
// CONSOLE WINDOW
if (Input.GetKey (KeyCode.LeftControl) && Input.GetKey (KeyCode.R) && Input.GetKeyUp (KeyCode.S)) {
roverScienceGUI.consoleGUI.Toggle ();
DrawWaypoint.Instance.ToggleMarker();
}
// DEBUG WINDOW
if (Input.GetKey (KeyCode.RightControl) && Input.GetKeyUp (KeyCode.Keypad5)) {
roverScienceGUI.debugGUI.Toggle ();
}
}
}
// TAKEN FROM KERBAL ENGINEERING REDUX SOURCE by cybutek
// http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB
// This is to hopefully prevent multiple instances of this PartModule from running simultaneously
public bool IsPrimary {
get {
if (this.Vessel != null) {
foreach (Part part in this.Vessel.parts) {
if (part.Modules.Contains (this.ClassID)) {
if (this.part == part) {
return true;
} else {
break;
}
}
}
}
return false;
}
}
}
}