forked from KSP-RO/RP-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKCTBinderModule.cs
206 lines (166 loc) · 7.61 KB
/
KCTBinderModule.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
using System;
using System.Collections;
using KerbalConstructionTime;
using RP0.Crew;
using UnityEngine;
namespace RP0
{
[KSPScenario(ScenarioCreationOptions.AddToAllGames, new GameScenes[] { GameScenes.EDITOR, GameScenes.FLIGHT, GameScenes.SPACECENTER, GameScenes.TRACKSTATION })]
public class KCTBinderModule : ScenarioModule
{
// FIXME if we change the min build rate, FIX THIS.
protected const double BuildRateOffset = -0.0001d;
protected double nextTime = -1d;
protected double checkInterval = 0.5d;
protected const int padLevels = 10;
protected int[] padCounts = new int[padLevels];
protected bool skipOne = true;
protected bool skipTwo = true;
private EventData<RDTech> onKctTechQueuedEvent;
private EventData<ProtoTechNode> onKctTechCompletedEvent;
private EventData<FacilityUpgrade> onKctFacilityUpgradeQueuedEvent;
private EventData<FacilityUpgrade> onKctFacilityUpgradeCompletedEvent;
public override void OnAwake()
{
base.OnAwake();
CareerLog.FnGetKCTUpgdCounts = GetKCTUpgradeCounts;
CareerLog.FnGetKCTSciPoints = GetSciPointTotalFromKCT;
KCT_GUI.UseAvailabilityChecker = true;
KCT_GUI.AvailabilityChecker = CheckCrewForPart;
}
public void Start()
{
onKctTechQueuedEvent = GameEvents.FindEvent<EventData<RDTech>>("OnKctTechQueued");
if (onKctTechQueuedEvent != null)
{
onKctTechQueuedEvent.Add(OnKctTechQueued);
Debug.Log($"[RP-0] Bound to OnKctTechQueued");
}
onKctTechCompletedEvent = GameEvents.FindEvent<EventData<ProtoTechNode>>("OnKctTechCompleted");
if (onKctTechCompletedEvent != null)
{
onKctTechCompletedEvent.Add(OnKctTechCompleted);
Debug.Log($"[RP-0] Bound to OnKctTechCompleted");
}
onKctFacilityUpgradeQueuedEvent = GameEvents.FindEvent<EventData<FacilityUpgrade>>("OnKctFacilityUpgradeQueued");
if (onKctFacilityUpgradeQueuedEvent != null)
{
onKctFacilityUpgradeQueuedEvent.Add(OnKctFacilityUpgdQueued);
Debug.Log($"[RP-0] Bound to OnKctFacilityUpgradeQueued");
}
onKctFacilityUpgradeCompletedEvent = GameEvents.FindEvent<EventData<FacilityUpgrade>>("OnKctFacilityUpgradeComplete");
if (onKctFacilityUpgradeCompletedEvent != null)
{
onKctFacilityUpgradeCompletedEvent.Add(OnKctFacilityUpgdComplete);
Debug.Log($"[RP-0] Bound to OnKctFacilityUpgradeComplete");
}
StartCoroutine(CreateCoursesRoutine());
}
public void OnDestroy()
{
if (onKctTechQueuedEvent != null) onKctTechQueuedEvent.Remove(OnKctTechQueued);
if (onKctTechCompletedEvent != null) onKctTechCompletedEvent.Remove(OnKctTechCompleted);
if (onKctFacilityUpgradeQueuedEvent != null) onKctFacilityUpgradeQueuedEvent.Remove(OnKctFacilityUpgdQueued);
if (onKctFacilityUpgradeCompletedEvent != null) onKctFacilityUpgradeCompletedEvent.Remove(OnKctFacilityUpgdComplete);
}
public static int GetKCTUpgradeCounts(SpaceCenterFacility facility)
{
return KerbalConstructionTime.Utilities.GetSpentUpgradesFor(facility);
}
public static float GetSciPointTotalFromKCT()
{
// KCT returns -1 if the player hasn't earned any sci yet
return Math.Max(0, KCTGameStates.SciPointsTotal);
}
public static bool CheckCrewForPart(ProtoCrewMember pcm, string partName)
{
// lolwut. But just in case.
if (pcm == null)
return false;
bool requireTraining = HighLogic.CurrentGame.Parameters.CustomParams<RP0Settings>().IsTrainingEnabled;
if (!requireTraining || EntryCostStorage.GetCost(partName) == 1)
return true;
return CrewHandler.Instance.NautHasTrainingForPart(pcm, partName);
}
protected void Update()
{
if (HighLogic.CurrentGame == null || KerbalConstructionTime.KerbalConstructionTime.Instance == null)
return;
if (skipTwo)
{
if (skipOne)
{
skipOne = false;
return;
}
skipTwo = false;
return;
}
if (MaintenanceHandler.Instance == null)
return;
double time = Planetarium.GetUniversalTime();
if (nextTime > time)
return;
nextTime = time + checkInterval;
for (int i = padCounts.Length; i-- > 0;)
padCounts[i] = 0;
foreach (KSCItem ksc in KCTGameStates.KSCs)
{
double buildRate = 0d;
for (int i = ksc.VABRates.Count; i-- > 0;)
buildRate += Math.Max(0d, ksc.VABRates[i] + BuildRateOffset);
for (int i = ksc.SPHRates.Count; i-- > 0;)
buildRate += Math.Max(0d, ksc.SPHRates[i] + BuildRateOffset);
if (buildRate < 0.01d) continue;
MaintenanceHandler.Instance.kctBuildRates[ksc.KSCName] = buildRate;
for (int i = ksc.LaunchPads.Count; i-- > 0;)
{
int lvl = ksc.LaunchPads[i].level;
if (lvl >= 0 && lvl < padLevels)
++padCounts[lvl];
}
}
double RDRate = MathParser.ParseNodeRateFormula(10, 0, false);
MaintenanceHandler.Instance.kctResearchRate = RDRate;
MaintenanceHandler.Instance.kctPadCounts = padCounts;
}
private void OnKctTechQueued(RDTech data)
{
Debug.Log($"[RP-0] OnKctTechQueued");
CrewHandler.Instance.AddCoursesForTechNode(data);
}
private void OnKctTechCompleted(ProtoTechNode data)
{
Debug.Log($"[RP-0] OnKctTechCompleted");
CareerLog.Instance?.AddTechEvent(data.techID);
}
private void OnKctFacilityUpgdQueued(FacilityUpgrade data)
{
Debug.Log($"[RP-0] OnKctFacilityUpgdQueued");
if (!data.FacilityType.HasValue) return; // can be null in case of third party mods that define custom facilities
CareerLog.Instance?.AddFacilityConstructionEvent(data.FacilityType.Value, data.UpgradeLevel, data.Cost, ConstructionState.Started);
}
private void OnKctFacilityUpgdComplete(FacilityUpgrade data)
{
Debug.Log($"[RP-0] OnKctFacilityUpgdComplete");
if (!data.FacilityType.HasValue) return; // can be null in case of third party mods that define custom facilities
CareerLog.Instance?.AddFacilityConstructionEvent(data.FacilityType.Value, data.UpgradeLevel, data.Cost, ConstructionState.Completed);
}
private IEnumerator CreateCoursesRoutine()
{
yield return new WaitForFixedUpdate();
for (int i = 0; i < PartLoader.LoadedPartsList.Count; i++)
{
var ap = PartLoader.LoadedPartsList[i];
if (ap.partPrefab.CrewCapacity > 0)
{
var kctTech = KCTGameStates.TechList.Find(t => t.TechID == ap.TechRequired);
if (kctTech != null)
{
CrewHandler.Instance.AddPartCourses(ap);
}
}
}
}
}
}