forked from kethernet/StationScience
-
Notifications
You must be signed in to change notification settings - Fork 8
/
StationScienceModule.cs
executable file
·383 lines (354 loc) · 14.6 KB
/
StationScienceModule.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
/*
This file is part of Station Science.
Station Science is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Station Science is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Station Science. If not, see <http://www.gnu.org/licenses/>.
*/
using KSP.Localization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StationScience
{
public class StationScienceModule : ModuleResourceConverter
{
[KSPField(isPersistant = true)]
public int lightsMode = 1;
// 0: force off; 1: auto; 2: force on
[KSPField]
public string requiredSkills = "NA";
public IEnumerable<String> skills;
[KSPField]
public double experienceBonus = 0.5;
public bool CheckSkill()
{
if (requiredSkills == "" || requiredSkills == "NA")
return true;
if (skills == null)
{
skills = requiredSkills.Split(',').Select(s => s.Trim());
}
foreach (var crew in part.protoModuleCrew)
{
foreach (String skill in skills) {
if (crew.HasEffect(skill))
return true;
}
}
return false;
}
private float lastCheck = 0;
protected override void PreProcessing()
{
float curTime;
if (IsActivated && (curTime = UnityEngine.Time.realtimeSinceStartup) - lastCheck > 0.1)
{
lastCheck = curTime;
if (!CheckSkill())
{
StopResourceConverter();
this.status = "Inactive; no " + requiredSkills;
}
else if (StationExperiment.CheckBoring(vessel, false))
{
StopResourceConverter();
this.status = "Inactive; on home planet";
}
else
{
int nsci = 0;
int nstars = 0;
foreach (var crew in part.protoModuleCrew)
{
foreach (String skill in skills)
{
if (crew.HasEffect(skill))
{
nsci += 1;
nstars += crew.experienceLevel;
}
}
}
SetEfficiencyBonus((float)Math.Max(nsci + nstars * experienceBonus, 1.0));
}
}
base.PreProcessing();
}
#if false
private System.Collections.IEnumerator doUpdateStatus()
{
while (true)
{
updateStatus();
yield return new UnityEngine.WaitForSeconds(1f);
}
}
#endif
private V GetOrDefault<K, V>(Dictionary<K, V> dict, K key)
{
try
{
return dict[key];
}
catch (KeyNotFoundException)
{
return default(V);
}
}
private void UpdateStatus()
{
UpdateLights();
#if false
bool animActive = false;
if (!doResearch)
{
displayStatusMessage("Paused");
}
else if (minimumCrew > 0 && part.protoModuleCrew.Count < minimumCrew)
{
displayStatusMessage("Understaffed (" + part.protoModuleCrew.Count + "/" + minimumCrew + ")");
}
else if (StationExperiment.checkBoring(vessel, false))
{
displayStatusMessage("Go to space!");
}
else
{
Fields["researchStatus"].guiActive = false;
foreach (ResearchGenerator generator in generators)
{
generator.updateStatus();
animActive = animActive || (generator.last_time_step != 0);
}
/*
eurekasStatus = "";
var r = getOrDefault(EurekasGenerator.rates,"Eurekas");
if (r != null)
{
if (r.last_available == 0)
eurekasStatus = "No Experiments";
else
{
eurekasStatus = String.Format("{0:F2} per hour", -r.ratePerHour * r.rateMultiplier);
animActive = true;
}
}
Fields["eurekasStatus"].guiName = "Eurekas";
Fields["eurekasStatus"].guiActive = (eurekasStatus != "");
kuarqStatus = "";
var qr = getOrDefault(KuarqGenerator.rates, "Kuarqs");
var cr = getOrDefault(KuarqGenerator.rates, "ElectricCharge");
if (qr != null)
{
if (qr.last_available == 0)
kuarqStatus = "No Experiments";
else if (cr != null && cr.last_available < 0.000001)
kuarqStatus = "Not Enough Charge";
else if (qr.last_produced != 0)
{
animActive = true;
kuarqStatus = String.Format("{0:F2} per second", -qr.ratePerSecond * qr.rateMultiplier);
}
}
Fields["kuarqStatus"].guiActive = (kuarqStatus != "");
bioproductsStatus = "";
var br = getOrDefault(BioproductsGenerator.rates,"Bioproducts");
var kr = getOrDefault(BioproductsGenerator.rates,"Kibbal");
if (br != null)
{
if (br.last_available == 0)
bioproductsStatus = "No Experiments";
else if (kr != null && kr.last_available == 0)
bioproductsStatus = "Not Enough Kibbal";
else if (br.last_produced != 0)
{
animActive = true;
bioproductsStatus = String.Format("{0:F2} per hour", -br.ratePerHour * br.rateMultiplier);
}
}
Fields["bioproductsStatus"].guiActive = (bioproductsStatus != "");
scienceStatus = "";
var sr = getOrDefault(ScienceGenerator.rates,"__SCIENCE__zoologyBay");
var skr = getOrDefault(ScienceGenerator.rates,"Kibbal");
if (skr != null && skr.last_available == 0)
scienceStatus = "Hibernating";
else if(sr != null && sr.last_produced != 0)
scienceStatus = String.Format("{0:F2} per day", -sr.ratePerDay * sr.rateMultiplier);
Fields["scienceStatus"].guiActive = (scienceStatus != "");
kibbalStatus = "";
double total_produced = 0, total_rate = 0;
if (kr != null && kr.last_produced != 0)
{
total_produced += kr.last_produced; total_rate += kr.ratePerDay * kr.rateMultiplier;
}
if (skr != null && skr.last_produced != 0)
{
total_produced += skr.last_produced; total_rate += skr.ratePerDay * skr.rateMultiplier;
}
if (total_produced != 0)
kibbalStatus = String.Format("{0:F2} per day", total_rate);
Fields["kibbalStatus"].guiActive = (kibbalStatus != "");
*/
}
if (animator != null)
{
if (lightsMode == 2) animActive = true;
if (lightsMode == 0) animActive = false;
if (animActive && animator.Progress == 0 && animator.status.StartsWith("Locked", true, null))
{
animator.allowManualControl = true;
animator.Toggle();
animator.allowManualControl = false;
}
else if (!animActive && animator.Progress == 1 && animator.status.StartsWith("Locked", true, null))
{
animator.allowManualControl = true;
animator.Toggle();
animator.allowManualControl = false;
}
}
#endif
}
bool actuallyProducing = false;
ConversionRecipe lastRecipe = null;
protected override ConversionRecipe PrepareRecipe(double deltatime)
{
lastRecipe = base.PrepareRecipe(deltatime);
return lastRecipe;
}
protected override void PostProcess(ConverterResults result, double deltaTime)
{
base.PostProcess(result, deltaTime);
actuallyProducing = (result.TimeFactor > 0);
if(lightsMode == 1)
UpdateLights();
if (lastRecipe == null)
return;
/*
foreach (var ratio in lastRecipe.Inputs)
{
print(ratio.ResourceName + " output rate: " + ((60 * 60 * ratio.Ratio * result.TimeFactor) / deltaTime) + " per hour");
}
foreach (var ratio in lastRecipe.Outputs)
{
print(ratio.ResourceName + " output rate: " + ((60 * 60 * ratio.Ratio * result.TimeFactor) / deltaTime) + " per hour");
}
*/
}
void UpdateLights()
{
if (animator != null)
{
bool animActive = this.IsActivated && actuallyProducing;
if (lightsMode == 2) animActive = true;
if (lightsMode == 0) animActive = false;
if (animActive && animator.Progress == 0 && animator.status.StartsWith("Locked", true, null))
{
animator.allowManualControl = true;
animator.Toggle();
animator.allowManualControl = false;
}
else if (!animActive && animator.Progress == 1 && animator.status.StartsWith("Locked", true, null))
{
animator.allowManualControl = true;
animator.Toggle();
animator.allowManualControl = false;
}
}
}
private ModuleAnimateGeneric animator = null;
public override void OnStart(PartModule.StartState state)
{
base.OnStart(state);
var animators = this.part.FindModulesImplementing<ModuleAnimateGeneric>();
if (animators != null && animators.Count >= 1)
{
this.animator = animators[0];
for (int i = 0; i < animator.Fields.Count; i++)
{
if (animator.Fields[i] != null)
animator.Fields[i].guiActive = false;
}
/*for(int i = 0; i < animator.Actions.Count; i++) {
if(animator.Actions[i] != null)
animator.Actions[i].active = false;
}
for(int i = 0; i < animator.Events.Count; i++) {
if (animator.Events[i] != null)
{
animator.Events[i].guiActive = false;
animator.Events[i].active = false;
}
}*/
}
if (state == StartState.Editor) { return; }
this.part.force_activate();
UpdateLightsMode();
//StartCoroutine(doUpdateStatus());
}
string lightsOn = Localizer.Format("#autoLOC_StatSci_LightsOn");
string lightsOff = Localizer.Format("#autoLOC_StatSci_LightsOff");
string lightsAuto = Localizer.Format("#autoLOC_StatSci_LightsAuto");
public void UpdateLightsMode()
{
switch (lightsMode)
{
case 0:
Events["LightsMode"].guiName = lightsOff;
break;
case 1:
Events["LightsMode"].guiName = lightsAuto;
break;
case 2:
Events["LightsMode"].guiName = lightsOn;
break;
}
UpdateLights();
}
[KSPEvent(guiActive = true, guiName = "#autoLOC_StatSci_LightsAuto", active = true)]
public void LightsMode()
{
lightsMode += 1;
if (lightsMode > 2)
lightsMode = 0;
UpdateLightsMode();
}
public override string GetInfo()
{
string ret = base.GetInfo();
if (requiredSkills != "" && requiredSkills != "NA")
{
ret += Localizer.Format("#autoLOC_StatSci_skillReq", requiredSkills);
}
return ret;
#if false
string ret = "";
if (eurekasPerHour > 0)
ret += (ret == "" ? "" : "\n") + "Eurekas per hour: " + eurekasPerHour;
if (kuarqsPerSec > 0) {
ret += (ret == "" ? "" : "\n") + "Kuarqs per second: " + kuarqsPerSec;
if(chargePerKuarq > 0)
ret += (ret == "" ? "" : "\n") + "Electric Charge per Kuarq: " + chargePerKuarq;
}
if (sciPerDay > 0)
ret += (ret == "" ? "" : "\n") + "Science per day: " + sciPerDay;
if (kibbalPerDay > 0)
ret += (ret == "" ? "" : "\n") + "Kibbal per day: " + kibbalPerDay;
if(bioproductsPerHour > 0) {
ret += (ret == "" ? "" : "\n") + "Bioproducts per hour: " + bioproductsPerHour;
if(kibbalPerBioproduct > 0)
ret += (ret == "" ? "" : "\n") + "Kibbal per bioproduct: " + kibbalPerBioproduct;
}
return ret;
#endif
}
}
}