-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFieldTrainingFacilityModule.cs
446 lines (382 loc) · 17.4 KB
/
FieldTrainingFacilityModule.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
/* Field Training Facility (FTF)
* Kerbals gain experience (stars) using time and electric charge. For Kerbal Space Program.
* Copyright (C) 2016 EFour
* Copyright (C) 2019, 2022, 2023 zer0Kerbal (zer0Kerbal at hotmail dot com)
*
* This program 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using KSP;
using KSPAssets;
using KSP.Localization;
using System.Web.UI.WebControls.WebParts;
namespace FieldTrainingFacility
{
public class FieldTrainingFacility : PartModule
{
readonly string[] trainingArr =
{
"",
"Training1",
"Training2",
"Training3",
"Training4",
"Training5"
};
readonly string[] crewListArr =
{
"BoardKerbal0",
"BoardKerbal1",
"BoardKerbal2",
"BoardKerbal3",
"BoardKerbal4",
"BoardKerbal5",
"BoardKerbal6",
"BoardKerbal7"
};
readonly float[] levelUpExpTable = { 2, 6, 8, 16, 32, 0 };
readonly string[] levelNumber = { "null", "1st", "2nd", "3rd", "4th", "5th" };
readonly ProtoCrewMember[] crewArr = new ProtoCrewMember[8];
int crewCnt = 0,
maxCrew = 0;
[KSPField]
public float TimeFactor = 426 * 6 * 60 * 60; // 1Year = 426day, 1day = 6hour, 1hour = 60minutes, 1min = 60sec
[KSPField]
public float ECFactor = 4;
[KSPField]
public float SpaceFactor = 4f;
[KSPField]
public float LandedFactor = 6f;
[KSPField(isPersistant = true, guiActive = true, guiName = "Training Status", groupName = "Training", groupDisplayName = "Training Facility " + Version.SText, groupStartCollapsed = true)]
public bool TrainingStatus = false;
[KSPField(isPersistant = true)]
public double LastTimeSigniture = -1;
[KSPEvent(guiActive = true, guiName = "Start Training", groupName = "Training")]
public void ToggleTraining()
{
if(TrainingStatus == false)
{
TrainingStatus = true;
LastTimeSigniture = Planetarium.GetUniversalTime();
Events["ToggleTraining"].guiName = "Stop Training";
}
else StopTraining();
}
[KSPField(guiActive = false, groupName = "Training")]
public string BoardKerbal0;
[KSPField(guiActive = false, groupName = "Training")]
public string BoardKerbal1;
[KSPField(guiActive = false, groupName = "Training")]
public string BoardKerbal2;
[KSPField(guiActive = false, groupName = "Training")]
public string BoardKerbal3;
[KSPField(guiActive = false, groupName = "Training")]
public string BoardKerbal4;
[KSPField(guiActive = false, groupName = "Training")]
public string BoardKerbal5;
[KSPField(guiActive = false, groupName = "Training")]
public string BoardKerbal6;
[KSPField(guiActive = false, groupName = "Training")]
public string BoardKerbal7;
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
if(TrainingStatus == true) Events["ToggleTraining"].guiName = "Stop Training";
maxCrew = part.CrewCapacity;
}
public void FixedUpdate()
{
if (TrainingStatus == true)
{
if (ConsumeEC(crewCnt, TimeWarp.fixedDeltaTime) == false)
{
StopTraining();
ScreenMessages.PostScreenMessage("Electric Charge Depleted. Stopping Training.");
}
}
base.OnFixedUpdate();
}
public override void OnUpdate()
{
if (TrainingStatus == true)
{
double nowTime = Planetarium.GetUniversalTime();
string[] expStrArr = new string[8];
int index = 0;
foreach (ProtoCrewMember crew in part.protoModuleCrew)
{
if (index >= crewArr.Length) break;
crewArr[index] = crew;
expStrArr[index] = "";
int crewLevel = GetCrewTrainedLevel(crew);
if (crewLevel < 5)
{
// calculating exp section
double exp = GetKerbalTrainingExp(crew);
exp += CalculateExp(vessel, nowTime - LastTimeSigniture);
// crew leveling section
if (GetCrewTrainedLevel(crew) == 0 && exp > TimeFactor * levelUpExpTable[0] / 64)
{
SetCrewTrainingLevel(crew, 1);
exp -= TimeFactor * levelUpExpTable[0] / 64;
}
if (GetCrewTrainedLevel(crew) == 1 && exp > TimeFactor * levelUpExpTable[1] / 64)
{
SetCrewTrainingLevel(crew, 2);
exp -= TimeFactor * levelUpExpTable[1] / 64;
}
if (GetCrewTrainedLevel(crew) == 2 && exp > TimeFactor * levelUpExpTable[2] / 64)
{
SetCrewTrainingLevel(crew, 3);
exp -= TimeFactor * levelUpExpTable[2] / 64;
}
if (GetCrewTrainedLevel(crew) == 3 && exp > TimeFactor * levelUpExpTable[3] / 64)
{
SetCrewTrainingLevel(crew, 4);
exp -= TimeFactor * levelUpExpTable[3] / 64;
}
if (GetCrewTrainedLevel(crew) == 4 && exp > TimeFactor * levelUpExpTable[4] / 64)
{
SetCrewTrainingLevel(crew, 5);
exp -= TimeFactor * levelUpExpTable[4] / 64;
}
if (GetCrewTrainedLevel(crew) < 5)
{
SetKerbalTrainingExp(crew, exp);
expStrArr[index] = " (" + (exp * 100 / (TimeFactor * levelUpExpTable[crewLevel] / 64)).ToString("F2") + "%)";
}
else RemoveKerbalTrainingExp(crew);
}
Fields[crewListArr[index]].guiName = "Lv" + GetCrewTrainedLevel(crew);
Fields[crewListArr[index]].guiActive = true;
index++;
}
// save crew number
crewCnt = index;
// reset unused spaces
for (; index < crewArr.Length; index++)
{
Fields[crewListArr[index]].guiActive = false;
crewArr[index] = null;
}
BoardKerbal0 = (crewArr[0] != null ? crewArr[0].name + expStrArr[0] : "");
BoardKerbal1 = (crewArr[1] != null ? crewArr[1].name + expStrArr[1] : "");
BoardKerbal2 = (crewArr[2] != null ? crewArr[2].name + expStrArr[2] : "");
BoardKerbal3 = (crewArr[3] != null ? crewArr[3].name + expStrArr[3] : "");
BoardKerbal4 = (crewArr[4] != null ? crewArr[4].name + expStrArr[4] : "");
BoardKerbal5 = (crewArr[5] != null ? crewArr[5].name + expStrArr[5] : "");
BoardKerbal6 = (crewArr[6] != null ? crewArr[6].name + expStrArr[6] : "");
BoardKerbal7 = (crewArr[7] != null ? crewArr[7].name + expStrArr[7] : "");
LastTimeSigniture = Planetarium.GetUniversalTime();
}
else
{
string[] expStrArr = new string[8];
int index = 0;
foreach (ProtoCrewMember crew in part.protoModuleCrew)
{
if (index >= crewArr.Length) break;
crewArr[index] = crew;
expStrArr[index] = "";
int crewLevel = GetCrewTrainedLevel(crew);
if (crewLevel < 5)
{
// calculating exp section
double exp = GetKerbalTrainingExp(crew);
if (GetCrewTrainedLevel(crew) < 5)
{
SetKerbalTrainingExp(crew, exp);
expStrArr[index] = " (" + (exp * 100 / (TimeFactor * levelUpExpTable[crewLevel] / 64)).ToString("F2") + "%)";
}
else RemoveKerbalTrainingExp(crew);
}
Fields[crewListArr[index]].guiName = "Lv" + GetCrewTrainedLevel(crew);
Fields[crewListArr[index]].guiActive = true;
index++;
}
// reset unused spaces
for (; index < crewArr.Length; index++)
{
Fields[crewListArr[index]].guiActive = false;
crewArr[index] = null;
}
BoardKerbal0 = (crewArr[0] != null ? crewArr[0].name + expStrArr[0] : "");
BoardKerbal1 = (crewArr[1] != null ? crewArr[1].name + expStrArr[1] : "");
BoardKerbal2 = (crewArr[2] != null ? crewArr[2].name + expStrArr[2] : "");
BoardKerbal3 = (crewArr[3] != null ? crewArr[3].name + expStrArr[3] : "");
BoardKerbal4 = (crewArr[4] != null ? crewArr[4].name + expStrArr[4] : "");
BoardKerbal5 = (crewArr[5] != null ? crewArr[5].name + expStrArr[5] : "");
BoardKerbal6 = (crewArr[6] != null ? crewArr[6].name + expStrArr[6] : "");
BoardKerbal7 = (crewArr[7] != null ? crewArr[7].name + expStrArr[7] : "");
}
base.OnUpdate();
}
private void StopTraining()
{
TrainingStatus = false;
BoardKerbal0 = "";
BoardKerbal1 = "";
BoardKerbal2 = "";
BoardKerbal3 = "";
BoardKerbal4 = "";
BoardKerbal5 = "";
BoardKerbal6 = "";
BoardKerbal7 = "";
for (int index = 0; index < crewListArr.Length; index++) Fields[crewListArr[index]].guiActive = false;
Events["ToggleTraining"].guiName = "Start Training";
}
private int GetCrewTrainedLevel(ProtoCrewMember crew)
{
int lastLog = 0;
FlightLog totalLog = crew.careerLog.CreateCopy();
totalLog.MergeWith(crew.flightLog.CreateCopy());
int deadFlight = -1;
foreach (FlightLog.Entry entry in totalLog.Entries)
{
if (entry.flight <= deadFlight) continue;
if (entry.type == "Die") deadFlight = entry.flight;
}
foreach (FlightLog.Entry entry in totalLog.Entries)
{
if (entry.flight <= deadFlight) continue;
if (lastLog < 1 && entry.type == "Training1") lastLog = 1;
if (lastLog < 2 && entry.type == "Training2") lastLog = 2;
if (lastLog < 3 && entry.type == "Training3") lastLog = 3;
if (lastLog < 4 && entry.type == "Training4") lastLog = 4;
if (lastLog < 5 && entry.type == "Training5") lastLog = 5;
}
return lastLog;
}
private void SetCrewTrainingLevel(ProtoCrewMember crew, int level)
{
crew.flightLog.AddEntry(new FlightLog.Entry(crew.flightLog.Flight, trainingArr[level], "Kerbin"));
ScreenMessages.PostScreenMessage(levelNumber[level] + " Training Complete : " + crew.name);
}
private double GetKerbalTrainingExp(ProtoCrewMember crew)
{
string lastExpStr = "0";
FlightLog totalLog = crew.careerLog.CreateCopy();
totalLog.MergeWith(crew.flightLog.CreateCopy());
int deadFlight = -1;
foreach (FlightLog.Entry entry in totalLog.Entries)
{
if (entry.flight <= deadFlight) continue;
if (entry.type == "Die") deadFlight = entry.flight;
}
foreach (FlightLog.Entry entry in totalLog.Entries)
{
if (entry.type == "TrainingExp")
{
if (entry.flight <= deadFlight) RemoveKerbalTrainingExp(crew);
else lastExpStr = entry.target;
}
}
return double.Parse(lastExpStr);
}
private void RemoveKerbalTrainingExp(ProtoCrewMember crew)
{
foreach (FlightLog.Entry entry in crew.careerLog.Entries.ToArray())
if (entry.type == "TrainingExp")
crew.careerLog.Entries.Remove(entry);
foreach (FlightLog.Entry entry in crew.flightLog.Entries.ToArray())
if (entry.type == "TrainingExp")
crew.flightLog.Entries.Remove(entry);
}
private void SetKerbalTrainingExp(ProtoCrewMember crew, double exp)
{
RemoveKerbalTrainingExp(crew);
crew.flightLog.Entries.Add(new FlightLog.Entry(crew.flightLog.Flight, "TrainingExp", exp.ToString()));
}
private double CalculateExp(Vessel vessel, double elapsed)
{
if (this.vessel.mainBody.bodyName == "Kerbin" && this.vessel.LandedOrSplashed) return elapsed;
else if (this.vessel.LandedOrSplashed) return elapsed * LandedFactor;
else return elapsed * SpaceFactor;
}
public bool ConsumeEC(int numCrew, double elapsed)
{
if (CheatOptions.InfiniteElectricity == true) return true;
double ec = 0;
int tanks = 0;
foreach (Part part in vessel.parts)
{
foreach (PartResource res in part.Resources)
{
if (res.resourceName == "ElectricCharge" && res.amount > 0)
{
ec += res.amount;
tanks++;
}
}
}
if (tanks == 0 || ec <= 0) return false;
foreach (Part part in vessel.parts)
{
foreach (PartResource res in part.Resources)
{
if (res.resourceName == "ElectricCharge" && res.amount > 0) res.amount -= numCrew * ECFactor * elapsed / tanks;
if (res.amount < 0) res.amount = 0;
}
}
return true;
}
/// <summary>Converts consumption rate into /s /m /hour and returns a formate string.</summary>
/// <param name="Rate">The rate.</param>
/// <returns>RateString="Rate"</returns>
private static string RateString(double rate)
{
// double rate = double.Parse(value.value);
string sfx = "/s";
if (rate <= 0.004444444f)
{
rate *= 3600;
sfx = "/h";
}
else if (rate < 0.2666667f)
{
rate *= 60;
sfx = "/m";
}
// limit decimal places to 10 and add sfx
//return String.Format(FuelRateFormat, Rate, sfx);
return rate.ToString("###.#####") + " EC" + sfx;
}
/// <summary>Module information shown in editors</summary>
private string info = string.Empty;
public override string GetInfo()
{
//? this is what is show in the editor
//? As annoying as it is, pre-parsing the config MUST be done here, because this is called during part loading.
//? The config is only fully parsed after everything is fully loaded (which is why it's in OnStart())
if (info == string.Empty)
{
info += Localizer.Format("#FTF-Agency-titl"); // #FTF-manu = Kerbalnaut Training Industries, Inc.
info += "\n v" + Version.SText; // FTF Version Number text
info += "\n<color=#b4d455FF>" + Localizer.Format("#FTF-desc"); // #FTF-desc = Train Kerbals using time and Electric Charge
info += "\n\n<color=orange>Requires:</color> \n - <color=white><b>" + Localizer.Format("#autoLOC_252004"); // #autoLOC_252004 = ElectricCharge
info += "</b>: \n <color=#99FF00FF> - Per Crew: </b></color><color=white>" + RateString(ECFactor) + " </color>";
info += "</b>: \n <color=#99FF00FF> - Max Crew: </b></color><color=white>" + RateString(maxCrew * ECFactor) + "</color>";
}
// #autoLOC_252004 = ElectricCharge
// #FTF-Agency-titl = Kerbalnaut Training Industries, Inc.
// #FTF-titl = Field Training Facility
// #FTF-desc = Train Kerbals using time and Electric Charge
return info;
}
}
}