-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathModuleHullBreach.cs
358 lines (293 loc) · 12.2 KB
/
ModuleHullBreach.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
using System;
using UnityEngine;
using BDArmory.Core.Extension;
using BDArmory.Core.Module;
namespace HullBreach
{
public class ModuleHullBreach : PartModule
{
static ModuleHullBreach instance;
public static ModuleHullBreach Instance => instance;
#region KSP Fields
public bool isHullBreached;
public string DamageState = "None"; //None, Normal,Critical
[KSPField(isPersistant = false)]
public double flowRate = .5;
[KSPField(isPersistant = false)]
public double critFlowRate = 1;
[KSPField(isPersistant = false)]
public double breachTemp = 0.6;
[KSPField(isPersistant = false)]
public double critBreachTemp = 0.9;
[KSPField(isPersistant = true)]
public bool hydroExplosive = false;
[KSPField(isPersistant = true)]
public bool hull = false;
[KSPField(guiActive = true, guiActiveEditor = true, guiName = "Crush Depth", isPersistant = true), UI_FloatRange(minValue = 0f, maxValue = 600f, stepIncrement = 1f)]
public float crushDepth = 200f;
[KSPField(isPersistant = true)]
public bool DepthCharge = false;
#region Debug Fields
[KSPField(isPersistant = true)]
public bool partDebug = true;
//[KSPField(guiActive = true, isPersistant = false, guiName = "Submerged Portion")]
//public double sumergedPortion;
//[KSPField(guiActive = true, isPersistant = false, guiName = "Current Situation")]
//public string vesselSituation;
//[KSPField(guiActive = true, isPersistant = false, guiName = "Heat Level")]
//public double pctHeat = 0;
[KSPField(guiActive = true, isPersistant = false, guiName = "Current Depth")]
public double currentDepth = 0;
[KSPField(guiActive = true, isPersistant = false, guiName = "Vessel Mass")]
public double VesselMass;
#endregion DebugFields
[UI_FloatRange(minValue = 1, maxValue = 100, stepIncrement = 1)] [KSPField(guiActive = true, guiActiveEditor = true,isPersistant = true, guiName = "Flow Rate")]
public float flowMultiplier = 1;
[KSPField(isPersistant = true, guiActive = true,guiActiveEditor = false, guiName = "Test Breach")]
public static Boolean forceHullBreach;
[KSPEvent(guiActive = true, guiActiveEditor = false, guiName = "Test Breach")]
#endregion KSPFields
public void ToggleHullBreach()
{
//if (!(vessel.id == FlightGlobals.ActiveVessel.id)) { return; }
//if (!(vessel.id == FlightGlobals.ActiveVessel.id)) { return; }
if (!vessel.isActiveVessel) { return; }
if (isHullBreached)
{
isHullBreached = false;
forceHullBreach = false;
DamageState = "None";
FixedUpdate();
}
else
{
isHullBreached = true;
forceHullBreach = true;
DamageState = "Critical";
FixedUpdate();
}
}
#region GameEvents
public override void OnStart(StartState state)
{
if (state != StartState.Editor && vessel != null)
{
part.force_activate();
instance = this;
if(part.FindModulesImplementing<ModuleHullBreach>().Count !=0)
config.Instance.vesselHullBreach = this;
}
if (!crushable)
{
Fields["crushDepth"].guiActive = false;
Fields["crushDepth"].guiActiveEditor = false;
}
else
{
Fields["flowMultiplier"].guiActive = false;
Fields["flowMultiplier"].guiActiveEditor = false;
}
//if (state != StartState.Editor & vessel != null & partDebug == false)
//hiding info fields for troubleshooting
//foreach (BaseField f in Fields) { f.guiActive = false; } ???
//{
//Fields["Submerged Portion"].guiActive = false;
//Fields["Current Situation"].guiActive = false;
//Fields["Heat Level"].guiActive = false;
//Fields["Current Depth"].guiActive = false;
//Fields["Current Altitude"].guiActive = false;
//}
// GameEvents.onVesselStandardModification.Add(triggerCatastrophicBreach);
// onVesselStandardModification collects various vessel events and fires them off with a single one.
// Specifically - onPartAttach,onPartRemove,onPartCouple,onPartDie,onPartUndock,onVesselWasModified,onVesselPartCountChanged
// List<Part> HullParts = new List<Part>();
// GameEvents.onVesselPartCountChanged.Add(triggerCatastrophicBreach);
//if(part.Modules.Contains("ModuleHullBreach")) GameEvents.onPartJointBreak.Add(CheckCatastrophicBreach);
}
public void CheckCatastrophicBreach(PartJoint partJoint, float breakForce)
{
if (vessel.situation != Vessel.Situations.SPLASHED) return;
//if (hull)
//{
// ScreenMessages.PostScreenMessage("Catastrophic Hull Damage", 30.0f, ScreenMessageStyle.UPPER_CENTER);
//}
}
public void FixedUpdate()
{
if (!HighLogic.LoadedSceneIsFlight) return;
try
{
if (part == null ||
!part.Modules.Contains("ModuleHullBreach") ||
!part.Modules.Contains("HitpointTracker")
)
return;
}
catch (Exception)
{ }
part.rigidAttachment = true;
if (vessel.situation != Vessel.Situations.SPLASHED) return;
if (part.WaterContact & ShipIsDamaged() & isHullBreached & hull)
{
if (FlightGlobals.ActiveVessel)
{
if (DamageState == "Critical")
{
ScreenMessages.PostScreenMessage("Warning: Critical Hull Breach", 5.0f, ScreenMessageStyle.UPPER_CENTER);
}
else
{
ScreenMessages.PostScreenMessage("Warning: Hull Breach", 5.0f, ScreenMessageStyle.UPPER_CENTER);
}
}
switch (DamageState)
{
case "Normal":
vessel.IgnoreGForces(240);
part.RequestResource("SeaWater", (0 - (flowRate*(0.1 + part.submergedPortion)*flowMultiplier)));
break;
case "Critical":
vessel.IgnoreGForces(240);
part.RequestResource("SeaWater",
(0 - (critFlowRate*(0.1 + part.submergedPortion)*flowMultiplier)));
break;
}
}
//If part underwater add heat (damage) at a greater rate based on depth to simulate pressure
//sumergedPortion = Math.Round(this.part.submergedPortion, 4);
if (part.submergedPortion == 1.00 & hydroExplosive)
{
part.temperature += (0.1*part.depth);
}
else if (crushable && part.submergedPortion == 1.00 && !part.localRoot.name.StartsWith("Sub"))
{
if(config.ecDrain)
part.RequestResource("ElectricCharge", 1000); //kill EC if sumberged
if (crushable) part.buoyancy = -1.0f; // trying to kill floaty bits that never sink
if (warnTimer > 0f) warnTimer -= Time.deltaTime;
if (part.depth > warnDepth && oldVesselDepth > warnDepth && warnTimer <= 0)
{
if (FlightGlobals.ActiveVessel)
{
if (DepthCharge == false)
{
ScreenMessages.PostScreenMessage(
"Warning! Vessel will be crushed at " + (crushDepth) + "m depth!", 3,
ScreenMessageStyle.LOWER_CENTER);
}
else
{
ScreenMessages.PostScreenMessage(
(crushDepth) + "m Charge Deployed!", 3,
ScreenMessageStyle.LOWER_CENTER);
}
}
warnTimer = 5;
}
oldVesselDepth = part.depth;
crushingDepth();
}
}
public void LateUpdate()
{
if (!HighLogic.LoadedSceneIsFlight) return;
try
{
if (part == null ||
!part.Modules.Contains("ModuleHullBreach") ||
!part.Modules.Contains("HitpointTracker")
)
return;
}
catch (Exception)
{ }
//vesselSituation = vessel.situation.ToString();
//currentAlt = Math.Round(TrueAlt(),2);
//pctHeat = Math.Round((part.temperature/part.maxTemp)*100);
currentDepth = Math.Round(part.depth, 2);
VesselMass = Math.Round(vessel.totalMass);
}
public void OnDestroy()
{
instance = null;
}
#endregion
#region HullBreach Events
public bool ShipIsDamaged()
{
//Check Damage Based on Heat
//Increase DamageState Nomal/Crit Level
//Flip isHullBreached to Trigger adding SeaWater
//if (part.temperature >= (part.maxTemp*breachTemp))
float damage = part.Damage();
float max_damage = part.MaxDamage();
float dmg_pct = damage / max_damage;
if (dmg_pct >= critBreachTemp && dmg_pct <= breachTemp )
{
isHullBreached = true;
DamageState = "Normal";
}
else if (dmg_pct >= 0 && dmg_pct <= critBreachTemp)
{
isHullBreached = true;
DamageState = "Critical";
}
else
{
isHullBreached = false;
DamageState = "None";
}
if (forceHullBreach == true) //forcing if testing hull breach or if Catastrophic damage triggerd
{
return true;
}
else if (DamageState == "None")
{
return false;
}
else
{
return true;
}
}
#region Parts that do not take on water crushed by going below a certain depth
[KSPField(isPersistant = true)] public bool crushable = false;
public double warnTimer = 0;
public double warnDepth = 100;
public double oldVesselDepth;
private void crushingDepth()
{
//Nothing crushed unless : Vessel is under water, part is crushable,part is fully submerged, part is not a hull and part is not hydroexplosive
// Any of these true do not crush
if (!crushable || hull || hydroExplosive || part.submergedPortion != 1.00 || TrueAlt() > 0.01) return;
if (crushable & part.depth > crushDepth & (TrueAlt()*-1) > crushDepth)
{
if (DepthCharge)
{
part.explode();
}
else
{
part.Destroy();
}
}
}
public double TrueAlt()
{
Vector3 pos = vessel.GetWorldPos3D();
double ASL = FlightGlobals.getAltitudeAtPos(pos);
if (vessel.mainBody.pqsController == null)
{
return ASL;
}
double terrainAlt = vessel.pqsAltitude;
if (vessel.mainBody.ocean && terrainAlt <= 0)
{
return ASL;
} //Checks for oceans
return ASL - terrainAlt;
}
#endregion
#endregion
}
}