forked from KSP-RO/TestFlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestFlightFailure_IgnitionFail.cs
297 lines (261 loc) · 12.3 KB
/
TestFlightFailure_IgnitionFail.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
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using TestFlightAPI;
using TestFlightCore;
namespace TestFlight
{
public class TestFlightFailure_IgnitionFail : TestFlightFailure_Engine
{
[KSPField]
public bool restoreIgnitionCharge = false;
[KSPField]
public bool ignorePressureOnPad = true;
[KSPField]
public FloatCurve baseIgnitionChance = null;
[KSPField]
public FloatCurve pressureCurve = null;
[KSPField]
public FloatCurve ignitionUseMultiplier = null;
[KSPField]
public float additionalFailureChance = 0f;
[KSPField(isPersistant=true)]
public int numIgnitions = 0;
private ITestFlightCore core = null;
private bool preLaunchFailures;
private bool dynPressurePenalties;
private bool verboseDebugging;
public override void OnStart(StartState state)
{
base.OnStart(state);
core = TestFlightUtil.GetCore(this.part, Configuration);
if (core != null)
Startup();
verboseDebugging = core.DebugEnabled;
// Get the in-game settings
preLaunchFailures = HighLogic.CurrentGame.Parameters.CustomParams<TestFlightGameSettings>().preLaunchFailures;
dynPressurePenalties = HighLogic.CurrentGame.Parameters.CustomParams<TestFlightGameSettings>().dynPressurePenalties;
}
public override void Startup()
{
base.Startup();
if (core == null)
return;
// We don't want this getting triggered as a random failure
core.DisableFailure("TestFlightFailure_IgnitionFail");
}
public void OnEnable()
{
if (core == null)
core = TestFlightUtil.GetCore(this.part, Configuration);
if (core != null)
Startup();
}
public override void OnUpdate()
{
if (!TestFlightEnabled)
return;
// For each engine we are tracking, compare its current ignition state to our last known ignition state
for (int i = 0; i < engines.Count; i++)
{
EngineHandler engine = engines[i];
EngineModuleWrapper.EngineIgnitionState currentIgnitionState = engine.engine.IgnitionState;
// If we are transitioning from not ignited to ignited, we do our check
// The ignitionFailueRate defines the failure rate per flight data
if (currentIgnitionState == EngineModuleWrapper.EngineIgnitionState.IGNITED)
{
if (engine.ignitionState == EngineModuleWrapper.EngineIgnitionState.NOT_IGNITED || engine.ignitionState == EngineModuleWrapper.EngineIgnitionState.UNKNOWN)
{
double failureRoll = 0d;
if (verboseDebugging)
{
Log(String.Format("IgnitionFail: Engine {0} transitioning to INGITED state", engine.engine.Module.GetInstanceID()));
Log(String.Format("IgnitionFail: Checking curves..."));
} numIgnitions++;
double initialFlightData = core.GetInitialFlightData();
float ignitionChance = 1f;
float multiplier = 1f;
// Check to see if the vessel has not launched and if the player disabled pad failures
if (this.vessel.situation == Vessel.Situations.PRELAUNCH && !preLaunchFailures) {
ignitionChance = 1.0f;
} else {
ignitionChance = baseIgnitionChance.Evaluate((float)initialFlightData);
if (ignitionChance <= 0)
ignitionChance = 1f;
}
if (dynPressurePenalties)
{
multiplier = pressureCurve.Evaluate((float)(part.dynamicPressurekPa * 1000d));
if (multiplier <= 0f)
multiplier = 1f;
}
float minValue, maxValue = -1f;
baseIgnitionChance.FindMinMaxValue(out minValue, out maxValue);
if (verboseDebugging)
{
Log(String.Format("TestFlightFailure_IgnitionFail: IgnitionChance Curve, Min Value {0:F2}:{1:F6}, Max Value {2:F2}:{3:F6}", baseIgnitionChance.minTime, minValue, baseIgnitionChance.maxTime, maxValue));
}
if (this.vessel.situation != Vessel.Situations.PRELAUNCH)
ignitionChance = ignitionChance * multiplier * ignitionUseMultiplier.Evaluate(numIgnitions);
failureRoll = core.RandomGenerator.NextDouble();
if (verboseDebugging)
{
Log(String.Format("IgnitionFail: Engine {0} ignition chance {1:F4}, roll {2:F4}", engine.engine.Module.GetInstanceID(), ignitionChance, failureRoll));
}
if (failureRoll > ignitionChance)
{
engine.failEngine = true;
core.TriggerNamedFailure("TestFlightFailure_IgnitionFail");
failureRoll = core.RandomGenerator.NextDouble();
if (failureRoll < additionalFailureChance)
{
core.TriggerFailure();
}
}
}
}
engine.ignitionState = currentIgnitionState;
}
}
// Failure methods
public override void DoFailure()
{
if (!TestFlightEnabled)
return;
Failed = true;
float multiplier = 0;
ITestFlightCore core = TestFlightUtil.GetCore(this.part, Configuration);
if (core != null)
{
core.ModifyFlightData(duFail, true);
string met = KSPUtil.PrintTimeCompact((int)Math.Floor(this.vessel.missionTime), false);
if (dynPressurePenalties)
{
multiplier = pressureCurve.Evaluate((float)(part.dynamicPressurekPa * 1000d));
if (multiplier <= 0f)
multiplier = 1f;
}
if (multiplier > float.Epsilon)
{
FlightLogger.eventLog.Add($"[{met}] {core.Title} failed: Ignition Failure. {multiplier} penalty for {(float)(part.dynamicPressurekPa * 1000d)}Pa dynamic pressure.");
}
else
{
FlightLogger.eventLog.Add($"[{met}] {core.Title} failed: Ignition Failure");
}
}
Log(String.Format("IgnitionFail: Failing {0} engine(s)", engines.Count));
for (int i = 0; i < engines.Count; i++)
{
EngineHandler engine = engines[i];
if (engine.failEngine)
{
engine.engine.Shutdown();
// For some reason, need to disable GUI as well
engine.engine.Events["Activate"].active = false;
engine.engine.Events["Shutdown"].active = false;
engine.engine.Events["Activate"].guiActive = false;
engine.engine.Events["Shutdown"].guiActive = false;
if ((restoreIgnitionCharge) || (this.vessel.situation == Vessel.Situations.PRELAUNCH) )
RestoreIgnitor();
engines[i].failEngine = false;
}
}
}
public override float DoRepair()
{
base.DoRepair();
for (int i = 0; i < engines.Count; i++)
{
EngineHandler engine = engines[i];
{
// Prevent auto-ignition on repair
engine.engine.Shutdown();
engine.engine.Events["Activate"].active = true;
engine.engine.Events["Activate"].guiActive = true;
engine.engine.Events["Shutdown"].guiActive = true;
if (restoreIgnitionCharge || this.vessel.situation == Vessel.Situations.PRELAUNCH)
RestoreIgnitor();
engines[i].failEngine = false;
}
}
return 0;
}
public void RestoreIgnitor()
{
// part.Modules["ModuleEngineIgnitor"].GetType().GetField("ignitionsRemained").GetValue(part.Modules["ModuleEngineIgnitor"]));
if (this.part.Modules.Contains("ModuleEngineIgnitor"))
{
int currentIgnitions = (int)part.Modules["ModuleEngineIgnitor"].GetType().GetField("ignitionsRemained").GetValue(part.Modules["ModuleEngineIgnitor"]);
part.Modules["ModuleEngineIgnitor"].GetType().GetField("ignitionsRemained").SetValue(part.Modules["ModuleEngineIgnitor"], currentIgnitions + 1);
}
}
public override void OnAwake()
{
base.OnAwake();
if (baseIgnitionChance == null)
{
baseIgnitionChance = new FloatCurve();
baseIgnitionChance.Add(0f, 1f);
}
if (pressureCurve == null)
{
pressureCurve = new FloatCurve();
pressureCurve.Add(0f, 1f);
}
if (ignitionUseMultiplier == null)
{
ignitionUseMultiplier = new FloatCurve();
ignitionUseMultiplier.Add(0f, 1f);
}
}
public override string GetModuleInfo()
{
string infoString = "";
if (baseIgnitionChance != null)
{
float pMin = baseIgnitionChance.Evaluate(baseIgnitionChance.minTime);
float pMax = baseIgnitionChance.Evaluate(baseIgnitionChance.maxTime);
infoString = $"Ignition chance at 0 data: <color=#859900ff>{pMin:P1}</color>\nIgnition chance at max data: <color=#859900ff>{pMax:P1}</color>";
}
if (pressureCurve != null & pressureCurve.Curve.keys.Length > 1)
{
infoString = $"{infoString}.\n<b>NOTE</b>: This engine suffers a penalty to ignition when air lighting due to dynamic pressure";
}
return infoString;
}
public override List<string> GetTestFlightInfo()
{
List<string> infoStrings = new List<string>();
if (core == null)
{
Log("Core is null");
return infoStrings;
}
if (baseIgnitionChance == null)
{
Log("Curve is null");
return infoStrings;
}
float flightData = TestFlightManagerScenario.Instance.GetFlightDataForPartName(Configuration);
if (flightData < 0f)
flightData = 0f;
infoStrings.Add("<b>Ignition Reliability</b>");
infoStrings.Add(String.Format("<b>Current Ignition Chance</b>: {0:P}", baseIgnitionChance.Evaluate(flightData)));
infoStrings.Add(String.Format("<b>Maximum Ignition Chance</b>: {0:P}", baseIgnitionChance.Evaluate(baseIgnitionChance.maxTime)));
if (additionalFailureChance > 0f)
infoStrings.Add(String.Format("<b>Additional Failure Chance</b>: {0:P}", additionalFailureChance));
if (pressureCurve != null & pressureCurve.Curve.keys.Length > 1)
{
float maxTime = pressureCurve.maxTime;
infoStrings.Add("<b>This engine suffers a penalty to ignition based on dynamic pressure</b>");
infoStrings.Add($"<B>0 Pa Pressure Modifier: {pressureCurve.Evaluate(0)}");
infoStrings.Add($"<b>{maxTime} Pa Pressure Modifier</b>: {pressureCurve.Evaluate(maxTime):N}");
}
return infoStrings;
}
}
}