-
Notifications
You must be signed in to change notification settings - Fork 8
/
AmpYearModule.cs
1249 lines (1013 loc) · 31.9 KB
/
AmpYearModule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace AmpYear
{
public class AmpYearModule : PartModule
{
public enum Subsystem
{
TURNING,
POWER_TURN,
SAS,
ASAS,
RCS,
FLIGHT_COMPUTER,
HEATER,
COOLER,
MUSIC,
MASSAGE
}
public enum GUISection
{
SUBSYSTEM,
RESERVE,
LUXURY
}
//Constants
public const double MANAGER_ACTIVE_DRAIN = 1.0 / 300.0;
public const double ASAS_DRAIN = 3.0 / 60.0;
public const double RCS_DRAIN = 1.0 / 60.0;
public const double FLIGHT_COMPUTER_DRAIN = 1.0;
public const double TURN_ROT_POWER_DRAIN_FACTOR = 1.0 / 40.0;
public const float TURN_INACTIVE_ROT_FACTOR = 0.1f;
public const float POWER_UP_DELAY = 0.5f;
public const double SAS_BASE_DRAIN = 1.0 / 60.0;
public const double SAS_TORQUE_DRAIN_FACTOR = 1.0 / 160.0;
public const double POWER_TURN_DRAIN_FACTOR = 1.0 / 5.0;
public const float SAS_POWER_TURN_TORQUE_FACTOR = 0.25f;
public const float HEATER_HEAT_RATE = 5.0f;
public const double HEATER_BASE_DRAIN_FACTOR = 1.0;
public const double HEATER_CAPACITY_DRAIN_FACTOR = 0.5;
public const float HEATER_TARGET_TEMP = 20.0f;
public const float COOLER_TARGET_TEMP = 15.0f;
public readonly double[] RESERVE_TRANSFER_INCREMENTS = new double[3] { 0.25, 0.1, 0.01 };
public const int MAX_TRANSFER_ATTEMPTS = 8;
public const double RECHARGE_RESERVE_THRESHOLD = 0.95;
public const double RECHARGE_RESERVE_RATE = 30.0 / 60.0;
public const double RECHARGE_OVERFLOW_AVOID_FACTOR = 1.0;
public const float WINDOW_WIDTH = 200;
public const float WINDOW_BASE_HEIGHT = 140;
public const double DRAIN_ESTIMATE_INTERVAL = 0.5;
public const String MAIN_POWER_NAME = "ElectricCharge";
public const String RESERVE_POWER_NAME = "ReservePower";
public const String SUBSYS_STATE_LABEL = "Subsys";
public const String GUI_SECTION_LABEL = "Section";
//Properties
public int windowID = new System.Random().Next();
public AmpYearPart ayPart
{
private set;
get;
}
public Vessel lastVessel;
public FlightInputCallback flightCallback;
private RemoteTech.FlightComputer flightComputer;
private RemoteTech.FlightComputerGUI flightComputerGUI;
private bool managerEnabled = true;
private bool[] subsystemToggle = new bool [Enum.GetValues(typeof(Subsystem)).Length];
private double[] subsystemDrain = new double [Enum.GetValues(typeof(Subsystem)).Length];
private bool hasPower = true;
private bool hasReservePower = true;
private bool isPrimaryPart = false;
private float powerUpTime = 0.0f;
private bool subsysLoaded = false;
private bool guiSectionLoaded = false;
private bool guiEnabled = true;
public List<Part> crewablePartList = new List<Part>();
public List<CommandPod> commandPods = new List<CommandPod>();
float totalDefaultRotPower = 0.0f;
float sasTorque = 0.0f;
float sasAdditionalRotPower = 0.0f;
double turningFactor = 0.0;
double totalElectricCharge = 0.0;
double totalElectricChargeCapacity = 0.0;
double totalReservePower = 0.0;
double totalReservePowerCapacity = 0.0;
bool hasRCS = false;
float currentRCSThrust = 0.0f;
float currentPoweredRCSDrain = 0.0f;
int totalHeatedParts = 0;
int totalCooledParts = 0;
bool sasWasFirst = false;
bool reenableRCS = false;
bool reenableSAS = false;
bool reenableFlightComp = false;
RemoteTech.AttitudeMode reenableAttitudeMode = RemoteTech.AttitudeMode.KILLROT;
//GUI Properties
public bool[] guiSectionEnableFlag = new bool[Enum.GetValues(typeof(GUISection)).Length];
public GUIStyle sectionTitleStyle, subsystemButtonStyle, subsystemConsumptionStyle, statusStyle, warningStyle, powerSinkStyle;
public GUILayoutOption[] subsystemButtonOptions;
//Static Properties
public static Dictionary<String, float> podRotPowerMap = new Dictionary<string, float>();
//PartModule
public bool primaryCheck
{
get
{
foreach (Part p in vessel.parts)
{
if (p is AmpYearPart)
return p == part;
}
return false;
}
}
public bool partIsActive
{
get
{
return FlightGlobals.ready && FlightGlobals.ActiveVessel != null && vessel == FlightGlobals.ActiveVessel;
}
}
public override void OnAwake()
{
base.OnAwake();
AYSettings.loadGlobalSettings();
}
public override void OnStart(PartModule.StartState state)
{
base.OnStart(state);
Debug.Log("AY Start");
//Init properties
ayPart = (AmpYearPart)part;
if (!subsysLoaded)
{
for (int i = 0; i < Enum.GetValues(typeof(Subsystem)).Length; i++)
subsystemToggle[i] = false;
subsystemToggle[(int)Subsystem.TURNING] = true;
subsystemToggle[(int)Subsystem.ASAS] = true;
subsystemToggle[(int)Subsystem.FLIGHT_COMPUTER] = true;
}
if (!guiSectionLoaded)
{
for (int i = 0; i < Enum.GetValues(typeof(GUISection)).Length; i++)
guiSectionEnableFlag[i] = false;
guiSectionEnableFlag[(int)GUISection.SUBSYSTEM] = true;
}
flightComputerGUI = new RemoteTech.FlightComputerGUI();
flightComputer = new RemoteTech.FlightComputer(part, flightComputerGUI);
flightComputerGUI.computer = flightComputer;
flightCallback = new FlightInputCallback(this.flightControl);
RenderingManager.AddToPostDrawQueue(3, new Callback(this.drawGUI));
}
public override void OnUpdate()
{
base.OnUpdate();
if (FlightGlobals.ready && FlightGlobals.ActiveVessel != null)
{
//Check if the part's vessel has changed
if (lastVessel != vessel)
{
if (lastVessel != null)
{
//Remove the flight callback from the previous vessel
try
{
lastVessel.OnFlyByWire -= flightCallback;
}
catch { }
}
//Add the flight callback to the current vessel
try
{
vessel.OnFlyByWire += flightCallback;
}
catch { }
lastVessel = vessel;
}
//Compile information about the vessel and its parts
totalDefaultRotPower = 0.0f;
sasAdditionalRotPower = 0.0f;
sasTorque = 0.0f;
totalElectricCharge = 0.0;
totalElectricChargeCapacity = 0.0;
totalReservePower = 0.0;
totalReservePowerCapacity = 0.0;
totalHeatedParts = 0;
totalCooledParts = 0;
hasRCS = false;
currentRCSThrust = 0.0f;
currentPoweredRCSDrain = 0.0f;
crewablePartList.Clear();
commandPods.Clear();
bool determined_primary = false;
foreach (Part current_part in vessel.parts)
{
if (!determined_primary && current_part is AmpYearPart)
{
isPrimaryPart = current_part == part;
determined_primary = true;
if (!isPrimaryPart)
break;
}
if (current_part is CommandPod)
{
CommandPod pod = (CommandPod)current_part;
String name = pod.partInfo.name;
float default_rot_power = pod.rotPower;
if (!podRotPowerMap.ContainsKey(name))
{
//Map the part's default rot power to its name
podRotPowerMap.Add(name, pod.rotPower);
Debug.Log("Rot Power: " + name + " - " + pod.rotPower);
}
else
podRotPowerMap.TryGetValue(name, out default_rot_power);
commandPods.Add(pod);
totalDefaultRotPower += default_rot_power;
sasTorque += pod.maxTorque;
}
if (current_part is SASModule)
{
SASModule sas_module = (SASModule)current_part;
sasTorque += sas_module.maxTorque;
sasAdditionalRotPower += sas_module.maxTorque * SAS_POWER_TURN_TORQUE_FACTOR;
}
bool has_alternator = false;
foreach (PartModule module in current_part.Modules)
{
if (module is ModuleAlternator)
has_alternator = true;
if (module is ModuleRCS)
{
hasRCS = true;
ModuleRCS rcs = (ModuleRCS)module;
foreach (float thrust in rcs.thrustForces)
currentRCSThrust += thrust;
if (module is ModuleAmpYearPoweredRCS)
{
currentPoweredRCSDrain += ((ModuleAmpYearPoweredRCS)module).electricityUse;
((ModuleAmpYearPoweredRCS)module).isManaged = true; //Inform the thruster that it is being managed
}
}
}
//Sum up the power capacity of all parts
if (!has_alternator) //Ignore parts with alternators in power-capacity calculate because they don't actually store power
{
foreach (PartResource resource in current_part.Resources)
{
if (resource.resourceName == MAIN_POWER_NAME)
{
totalElectricCharge += resource.amount;
totalElectricChargeCapacity += resource.maxAmount;
}
else if (resource.resourceName == RESERVE_POWER_NAME)
{
totalReservePower += resource.amount;
totalReservePowerCapacity += resource.maxAmount;
}
}
}
if (current_part.CrewCapacity > 0)
{
if (current_part.temperature >= HEATER_TARGET_TEMP)
totalHeatedParts++;
if (current_part.temperature <= COOLER_TARGET_TEMP)
totalCooledParts++;
crewablePartList.Add(current_part);
}
}
if (isPrimaryPart)
subsystemUpdate();
else
ayPart.ASASActive = false;
}
else {
if (ayPart != null)
ayPart.ASASActive = false;
}
}
public override void OnSave(ConfigNode node)
{
Debug.Log("AY Save");
AYSettings.saveGlobalSettings();
try
{
node.AddValue(SUBSYS_STATE_LABEL, subsystemStateToInt());
node.AddValue(GUI_SECTION_LABEL, guiSectionStateToInt());
}
catch
{
}
}
public override void OnLoad(ConfigNode node)
{
Debug.Log("AY Load");
subsysLoaded = false;
guiSectionLoaded = false;
try
{
int state = 0;
if (int.TryParse(node.GetValue(SUBSYS_STATE_LABEL), out state))
{
setSubsystemStateFromInt(state);
subsysLoaded = true;
}
if (int.TryParse(node.GetValue(GUI_SECTION_LABEL), out state))
{
setGuiSectionStateFromInt(state);
guiSectionLoaded = true;
}
}
catch
{
}
}
[KSPEvent(guiActive = true, guiName = "Toggle GUI")]
public void toggleGUI()
{
guiEnabled = !guiEnabled;
}
//Manager
public bool timewarpIsValid
{
get
{
return TimeWarp.CurrentRate <= 4;
}
}
public bool managerIsActive
{
get
{
return timewarpIsValid && managerEnabled && (hasPower || hasReservePower);
}
}
public double managerActiveDrain
{
get
{
return MANAGER_ACTIVE_DRAIN;
}
}
public double managerCurrentDrain
{
get
{
if (managerIsActive)
return managerActiveDrain;
else
return 0.0;
}
}
//Subsystem
public bool subsystemEnabled(Subsystem subsystem)
{
switch (subsystem)
{
case Subsystem.SAS:
return vessel.ActionGroups[KSPActionGroup.SAS];
case Subsystem.RCS:
return vessel.ActionGroups[KSPActionGroup.RCS];
default:
return subsystemToggle[(int)subsystem];
}
}
public void setSubsystemEnabled(Subsystem subsystem, bool enabled)
{
switch (subsystem)
{
case Subsystem.SAS:
vessel.ActionGroups.SetGroup(KSPActionGroup.SAS, enabled);
break;
case Subsystem.RCS:
vessel.ActionGroups.SetGroup(KSPActionGroup.RCS, enabled);
break;
default:
subsystemToggle[(int)subsystem] = enabled;
break;
}
}
public bool subsystemActive(Subsystem subsystem)
{
if (!subsystemEnabled(subsystem))
return false;
switch (subsystem)
{
case Subsystem.POWER_TURN:
return subsystemEnabled(Subsystem.TURNING);
case Subsystem.ASAS:
return subsystemEnabled(Subsystem.SAS);
case Subsystem.FLIGHT_COMPUTER:
return flightComputer.AttitudeActive;
case Subsystem.HEATER:
return totalHeatedParts < crewablePartList.Count;
case Subsystem.COOLER:
return totalCooledParts < crewablePartList.Count;
default:
return true;
}
}
public bool subsystemPowered(Subsystem subsystem)
{
return hasPower && managerIsActive && subsystemActive(subsystem);
}
public bool subsystemVisible(Subsystem subsystem)
{
switch (subsystem)
{
case Subsystem.POWER_TURN:
return sasAdditionalRotPower > 0.0f;
case Subsystem.RCS:
return hasRCS;
case Subsystem.HEATER:
case Subsystem.COOLER:
case Subsystem.MUSIC:
return crewablePartList.Count > 0;
case Subsystem.MASSAGE:
return vessel.GetCrewCount() > 0;
default:
return true;
}
}
public double subsystemActiveDrain(Subsystem subsystem)
{
switch (subsystem)
{
case Subsystem.TURNING:
return totalDefaultRotPower * TURN_ROT_POWER_DRAIN_FACTOR;
case Subsystem.SAS:
return sasTorque * SAS_TORQUE_DRAIN_FACTOR;
case Subsystem.RCS:
return RCS_DRAIN + currentPoweredRCSDrain;
case Subsystem.ASAS:
return ASAS_DRAIN;
case Subsystem.FLIGHT_COMPUTER:
return FLIGHT_COMPUTER_DRAIN;
case Subsystem.POWER_TURN:
return sasAdditionalRotPower * POWER_TURN_DRAIN_FACTOR;
case Subsystem.HEATER:
case Subsystem.COOLER:
return HEATER_HEAT_RATE
* (crewablePartList.Count * HEATER_BASE_DRAIN_FACTOR + HEATER_CAPACITY_DRAIN_FACTOR * vessel.GetCrewCapacity());
case Subsystem.MUSIC:
return 1.0 / 120.0 * crewablePartList.Count;
case Subsystem.MASSAGE:
return 5.0 * vessel.GetCrewCount();
default:
return 0.0;
}
}
public double subsystemCurrentDrain(Subsystem subsystem)
{
if (!subsystemActive(subsystem) || !managerIsActive || !hasPower)
return 0.0;
switch (subsystem)
{
case Subsystem.RCS:
if (currentRCSThrust > 0.0f)
return subsystemActiveDrain(subsystem);
else
return 0.0;
case Subsystem.TURNING:
return turningFactor * subsystemActiveDrain(subsystem);
case Subsystem.POWER_TURN:
if (subsystemEnabled(Subsystem.TURNING))
return turningFactor * subsystemActiveDrain(subsystem);
else
return 0.0;
default:
return subsystemActiveDrain(subsystem);
}
}
public static string subsystemName(Subsystem subsystem)
{
switch (subsystem)
{
case Subsystem.TURNING:
return "Turning";
case Subsystem.POWER_TURN:
return "PowerTurn";
case Subsystem.SAS:
return "SAS";
case Subsystem.RCS:
return "RCS";
case Subsystem.ASAS:
return "ASAS";
case Subsystem.FLIGHT_COMPUTER:
return "FlightComp.";
case Subsystem.HEATER:
return "Heater";
case Subsystem.COOLER:
return "Air Cond.";
case Subsystem.MUSIC:
return "Smooth Jazz";
case Subsystem.MASSAGE:
return "Massage Chair";
default:
return String.Empty;
}
}
public static bool subsystemIsLuxury(Subsystem subsystem)
{
switch (subsystem)
{
case Subsystem.HEATER:
case Subsystem.COOLER:
case Subsystem.MUSIC:
case Subsystem.MASSAGE:
return true;
default:
return false;
}
}
private void subsystemUpdate()
{
if (ayPart != null)
ayPart.ASASActive = subsystemPowered(Subsystem.ASAS);
if (vessel.ActionGroups[KSPActionGroup.RCS] && !subsystemPowered(Subsystem.RCS))
{
//Disable RCS when the subsystem isn't powered
vessel.ActionGroups.SetGroup(KSPActionGroup.RCS, false);
reenableRCS = true;
}
if (vessel.ActionGroups[KSPActionGroup.SAS]) {
if (!subsystemPowered(Subsystem.SAS))
{
//Disable SAS when the subsystem isn't powered
vessel.ActionGroups.SetGroup(KSPActionGroup.SAS, false);
reenableSAS = true;
}
else
{
if (subsystemPowered(Subsystem.FLIGHT_COMPUTER))
{
//Both SAS and flight computer are active
if (sasWasFirst)
{
//Turn off SAS if the flight computer was turned on second
vessel.ActionGroups.SetGroup(KSPActionGroup.SAS, false);
sasWasFirst = false;
}
else
{
//Turn off flight computer if SAS was turned on second
sasWasFirst = true;
deactivateFlightComputer();
}
}
else
sasWasFirst = true;
}
}
if (!subsystemEnabled(Subsystem.FLIGHT_COMPUTER) || !hasPower || !managerIsActive)
{
RemoteTech.AttitudeMode reenable_mode = RemoteTech.AttitudeMode.KILLROT;
//Turn off flight computer if it isn't enabled and receiving power
if (deactivateFlightComputer(ref reenable_mode) && subsystemEnabled(Subsystem.FLIGHT_COMPUTER))
{
reenableFlightComp = true;
reenableAttitudeMode = reenable_mode;
}
}
if (managerIsActive && hasPower)
{
//Re-enable SAS/RCS if they were shut off by the manager and can be run again
if (reenableSAS)
{
setSubsystemEnabled(Subsystem.SAS, true);
reenableSAS = false;
}
if (reenableRCS)
{
setSubsystemEnabled(Subsystem.RCS, true);
reenableRCS = false;
}
if (reenableFlightComp)
{
foreach (RemoteTech.AttitudeStateButton button in flightComputerGUI.attitudeButtons)
{
if (button.mode == reenableAttitudeMode)
{
button.on = true;
button.Update();
break;
}
}
reenableFlightComp = false;
}
}
//Update command pod rot powers
bool turning_on = subsystemPowered(Subsystem.TURNING);
bool power_turn_on = subsystemPowered(Subsystem.POWER_TURN);
bool first = true;
foreach (CommandPod pod in commandPods)
{
float default_rot_power = pod.rotPower;
podRotPowerMap.TryGetValue(pod.partInfo.name, out default_rot_power);
if (turning_on)
{
if (power_turn_on && first)
{
//Apply power turn rotPower
pod.rotPower = default_rot_power + sasAdditionalRotPower;
first = false;
}
else
pod.rotPower = default_rot_power; //Use default rot power
}
else
pod.rotPower = default_rot_power * TURN_INACTIVE_ROT_FACTOR;
}
if (subsystemPowered(Subsystem.FLIGHT_COMPUTER) && !subsystemPowered(Subsystem.SAS))
sasWasFirst = false;
if (subsystemPowered(Subsystem.HEATER))
changeCrewedPartsTemperature(HEATER_TARGET_TEMP, true);
if (subsystemPowered(Subsystem.COOLER))
changeCrewedPartsTemperature(COOLER_TARGET_TEMP, false);
flightComputerGUI.update();
//Calculate total drain from subsystems
double subsystem_drain = 0.0;
foreach (Subsystem subsystem in Enum.GetValues(typeof(Subsystem)))
{
subsystemDrain[(int)subsystem] = subsystemCurrentDrain(subsystem);
subsystem_drain += subsystemDrain[(int)subsystem];
}
double manager_drain = managerCurrentDrain;
double total_manager_drain = subsystem_drain + manager_drain;
//Recharge reserve power if main power is above a certain threshold
if (managerIsActive && totalElectricCharge > 0 && totalElectricCharge / totalElectricChargeCapacity > RECHARGE_RESERVE_THRESHOLD
&& totalReservePower < totalReservePowerCapacity)
transferMainToReserve(RECHARGE_RESERVE_RATE * TimeWarp.deltaTime);
//Drain main power
double timestep_drain = total_manager_drain * TimeWarp.deltaTime;
double minimum_sufficient_charge = managerActiveDrain * TimeWarp.deltaTime;
if (totalElectricCharge >= minimum_sufficient_charge)
{
hasPower = (UnityEngine.Time.realtimeSinceStartup > powerUpTime)
&& (timestep_drain <= 0.0 || requestResource(MAIN_POWER_NAME, timestep_drain) >= (timestep_drain * 0.99));
}
else
hasPower = false;
if (!hasPower && UnityEngine.Time.realtimeSinceStartup > powerUpTime)
{
//Set a delay for powering back up to avoid rapid flickering of the system
powerUpTime = UnityEngine.Time.realtimeSinceStartup + POWER_UP_DELAY;
}
if (!hasPower && totalReservePower > minimum_sufficient_charge)
{
//If main power is insufficient, drain reserve power for manager
double manager_timestep_drain = manager_drain * TimeWarp.deltaTime;
hasReservePower = manager_drain <= 0.0
|| requestResource(RESERVE_POWER_NAME, manager_timestep_drain) >= (manager_timestep_drain * 0.99);
}
else
hasReservePower = totalReservePower > minimum_sufficient_charge;
}
private int subsystemStateToInt()
{
int val = 0;
foreach (Subsystem subsystem in Enum.GetValues(typeof(Subsystem)))
{
if (subsystemEnabled(subsystem))
val |= (1 << (int)subsystem);
}
return val;
}
private void setSubsystemStateFromInt(int state)
{
foreach (Subsystem subsystem in Enum.GetValues(typeof(Subsystem)))
setSubsystemEnabled(subsystem, (state & (1 << (int)subsystem)) != 0);
}
void changeCrewedPartsTemperature(double target_temp, bool heat)
{
foreach (Part crewed_part in crewablePartList) {
if (heat)
{
if (crewed_part.temperature < target_temp)
crewed_part.temperature += HEATER_HEAT_RATE * TimeWarp.deltaTime;
}
else
{
if (crewed_part.temperature > target_temp)
crewed_part.temperature -= HEATER_HEAT_RATE * TimeWarp.deltaTime;
}
}
}
private bool deactivateFlightComputer()
{
RemoteTech.AttitudeMode mode = RemoteTech.AttitudeMode.KILLROT;
return deactivateFlightComputer(ref mode);
}
private bool deactivateFlightComputer(ref RemoteTech.AttitudeMode mode)
{
foreach (RemoteTech.AttitudeStateButton button in flightComputerGUI.attitudeButtons)
{
if (button.on)
{
button.on = false;
button.Update();
mode = button.mode;
return true;
}
}
return false;
}
//GUI Section
public static string guiSectionName(GUISection section)
{
switch (section)
{
case GUISection.SUBSYSTEM:
return "Subsys";
case GUISection.RESERVE:
return "Reserve";
case GUISection.LUXURY:
return "Luxury";
default:
return String.Empty;
}
}
public bool guiSectionEnabled(GUISection section)
{
return guiSectionEnableFlag[(int)section];
}
private int guiSectionStateToInt()
{
int val = 0;
for (int i = 0; i < Enum.GetValues(typeof(GUISection)).Length; i++)
{
if (guiSectionEnableFlag[i])
val |= (1 << i);
}
return val;
}
private void setGuiSectionStateFromInt(int state)
{
for (int i = 0; i < Enum.GetValues(typeof(GUISection)).Length; i++)
{
guiSectionEnableFlag[i] = (state & (1 << i)) != 0;
}
}
//Turning
private void flightControl(FlightCtrlState state)
{
if (isPrimaryPart)
{
flightComputer.drive(state);
turningFactor = (Math.Abs(state.pitch) + Math.Abs(state.roll) + Math.Abs(state.yaw)) / 3.0;
}
}
//Resources
public double requestResource(String name, double amount)
{
return requestResource(name, amount, part);
}
public static double requestResource(String name, double amount, Part part)
{
if (amount <= 0.0)
return 0.0;
double total_received = 0.0;
double request_amount = amount;
for (int attempts = 0; attempts < MAX_TRANSFER_ATTEMPTS && amount > 0.0; attempts++)
{
double received = part.RequestResource(name, request_amount);
total_received += received;
amount -= received;
if (received <= 0.0)
request_amount = amount * 0.5;
else
request_amount = amount;
}
return total_received;
}
private void transferReserveToMain(double amount)
{
//Debug.Log("main: " + totalElectricCharge);
//Debug.Log("reserve: " + totalReservePower);
if (amount > totalReservePower * RECHARGE_OVERFLOW_AVOID_FACTOR)
amount = totalReservePower * RECHARGE_OVERFLOW_AVOID_FACTOR;
if (amount > (totalElectricChargeCapacity - totalElectricCharge))
amount = (totalElectricChargeCapacity - totalElectricCharge);
//Debug.Log("amount: " + amount);
double received = requestResource(RESERVE_POWER_NAME, amount);
//Debug.Log("received: " + received);
int transfer_attempts = 0;
while (received > 0.0 && transfer_attempts < MAX_TRANSFER_ATTEMPTS)
{
received += part.RequestResource(MAIN_POWER_NAME, -received);
transfer_attempts++;
}
//Debug.Log("generated: " + (amount-received).ToString());
}
private void transferMainToReserve(double amount)
{
if (amount > totalElectricCharge * RECHARGE_OVERFLOW_AVOID_FACTOR)
amount = totalElectricCharge * RECHARGE_OVERFLOW_AVOID_FACTOR;
if (amount > (totalReservePowerCapacity - totalReservePower))
amount = (totalReservePowerCapacity - totalReservePower);