forked from RobertoVDMeer/Basic-Panic-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.cs
More file actions
564 lines (497 loc) · 24.2 KB
/
Helpers.cs
File metadata and controls
564 lines (497 loc) · 24.2 KB
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BattleTech;
using Harmony;
using PanicSystem.Components;
using static PanicSystem.Logger;
using static PanicSystem.PanicSystem;
using Random = UnityEngine.Random;
#if NO_CAC
#else
using CustomAmmoCategoriesPatches;
#endif
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable InconsistentNaming
namespace PanicSystem
{
public class Helpers
{
// used in strings
internal static float ActorHealth(AbstractActor actor)
{
//This is probably bugged for bonus/armor structure components.
//return
//(actor.SummaryArmorCurrent + actor.SummaryStructureCurrent) /
//(actor.SummaryArmorMax + actor.SummaryStructureMax) * 100;
float ah = 100;
if (actor is Mech mech)
{
ah=((mech.RightTorsoStructure + mech.LeftTorsoStructure + mech.CenterTorsoStructure + mech.LeftLegStructure + mech.RightLegStructure + mech.HeadStructure) +
(mech.RightTorsoFrontArmor + mech.RightTorsoRearArmor + mech.LeftTorsoFrontArmor + mech.LeftTorsoRearArmor +
mech.CenterTorsoFrontArmor + mech.CenterTorsoRearArmor + mech.LeftLegArmor + mech.RightLegArmor + mech.HeadArmor)) /
((MaxStructureForLocation(mech, (int)ChassisLocations.RightTorso) + MaxStructureForLocation(mech, (int)ChassisLocations.LeftTorso) + MaxStructureForLocation(mech, (int)ChassisLocations.CenterTorso)
+ MaxStructureForLocation(mech, (int)ChassisLocations.LeftLeg) + MaxStructureForLocation(mech, (int)ChassisLocations.RightLeg) + MaxStructureForLocation(mech, (int)ChassisLocations.Head)) +
(MaxArmorForLocation(mech, (int)ArmorLocation.RightTorso) + MaxArmorForLocation(mech, (int)ArmorLocation.RightTorsoRear)
+ MaxArmorForLocation(mech, (int)ArmorLocation.LeftTorso) + MaxArmorForLocation(mech, (int)ArmorLocation.LeftTorsoRear)
+ MaxArmorForLocation(mech, (int)ArmorLocation.CenterTorso) + MaxArmorForLocation(mech, (int)ArmorLocation.CenterTorsoRear)
+ MaxArmorForLocation(mech, (int)ArmorLocation.LeftLeg) + MaxArmorForLocation(mech, (int)ArmorLocation.RightLeg)
+ MaxArmorForLocation(mech, (int)ArmorLocation.Head) ))
*100;
}
else if (actor is Vehicle v)
{
ah=((v.LeftSideStructure + v.RightSideStructure + v.FrontStructure + v.RearStructure + v.TurretStructure) +
(v.LeftSideArmor + v.RightSideArmor + v.FrontArmor + v.RearArmor + v.TurretArmor)) /
((MaxStructureForLocation(v, (int)VehicleChassisLocations.Left) + MaxStructureForLocation(v, (int)VehicleChassisLocations.Right) + MaxStructureForLocation(v, (int)VehicleChassisLocations.Front) + MaxStructureForLocation(v, (int)VehicleChassisLocations.Rear) + MaxStructureForLocation(v, (int)VehicleChassisLocations.Turret)) +
(MaxArmorForLocation(v, (int)VehicleChassisLocations.Left) + MaxArmorForLocation(v, (int)VehicleChassisLocations.Right) + MaxArmorForLocation(v, (int)VehicleChassisLocations.Front) + MaxArmorForLocation(v, (int)VehicleChassisLocations.Rear) + MaxArmorForLocation(v, (int)VehicleChassisLocations.Turret))
)*100;
}
else
{
LogReport("Not mech or vehicle");
return ah;
}
LogReport($"ActorHealth {actor.Nickname} - {actor.DisplayName} - {actor.GUID} -{ah:F3}%");
return ah;
}
// used in calculations
internal static float PercentPilot(Pilot pilot) => 1 - (float) pilot.Injuries / pilot.Health;
public static float MaxArmorForLocation(Mech mech, int Location)
{
if (mech != null)
{
Statistic stat = mech.StatCollection.GetStatistic(mech.GetStringForArmorLocation((ArmorLocation)Location));
if (stat == null)
{
LogDebug($"Can't get armor stat { mech.DisplayName } location:{ Location.ToString()}");
return 0;
}
//LogDebug($"armor stat { mech.DisplayName } location:{ Location.ToString()} :{stat.DefaultValue<float>()}");
return stat.DefaultValue<float>();
}
LogDebug($"Mech null");
return 0;
}
public static float MaxStructureForLocation(Mech mech, int Location)
{
if (mech != null)
{
Statistic stat = mech.StatCollection.GetStatistic(mech.GetStringForStructureLocation((ChassisLocations)Location));
if (stat == null)
{
LogDebug($"Can't get structure stat { mech.DisplayName } location:{ Location.ToString()}");
return 0;
}
//LogDebug($"structure stat { mech.DisplayName } location:{ Location.ToString()}:{stat.DefaultValue<float>()}");
return stat.DefaultValue<float>();
}
LogDebug($"Mech null");
return 0;
}
public static float MaxArmorForLocation(Vehicle v, int Location)
{
if (v != null)
{
Statistic stat = v.StatCollection.GetStatistic(v.GetStringForArmorLocation((VehicleChassisLocations)Location));
if (stat == null)
{
LogDebug($"Can't get armor stat { v.DisplayName } location:{ Location.ToString()}");
return 0;
}
//LogDebug($"armor stat { v.DisplayName } location:{ Location.ToString()} :{stat.DefaultValue<float>()}");
return stat.DefaultValue<float>();
}
LogDebug($"Vehicle null");
return 0;
}
public static float MaxStructureForLocation(Vehicle v, int Location)
{
if (v != null)
{
Statistic stat = v.StatCollection.GetStatistic(v.GetStringForStructureLocation((VehicleChassisLocations)Location));
if (stat == null)
{
LogDebug($"Can't get structure stat { v.DisplayName } location:{ Location.ToString()}");
return 0;
}
//LogDebug($"structure stat { mech.DisplayName } location:{ Location.ToString()}:{stat.DefaultValue<float>()}");
return stat.DefaultValue<float>();
}
LogDebug($"Vehicle null");
return 0;
}
internal static float PercentForArmorLocation(Mech mech, int Location)
{
// Invalid locations are always 100%
// This helps makes the functions generic for the missing leg back armor, for example
if (Location == 0)
return 1;
if (mech != null)
{
Statistic stat = mech.StatCollection.GetStatistic(mech.GetStringForArmorLocation((ArmorLocation)Location));
if(stat == null) {
LogDebug($"Can't get armor stat { mech.DisplayName } location:{ Location}");
return 0;
}
//LogDebug($"Armor stat { mech.DisplayName } location:{ Location} cur:{stat.Value<float>()} max:{stat.DefaultValue<float>()}");
// if (mech.team.IsLocalPlayer)
// LogReport($"Armor stat { mech.DisplayName } location:{ Location} cur:{stat.Value<float>()} max:{stat.DefaultValue<float>()}");
float maxArmor = stat.DefaultValue<float>();
// Limit the max armor to ArmorDamageThreshold and the percent value to 1 (100%)
// This helps reduce panic effects for heavier, well-armored mechs
// Losing 30% of your armor isn't as distressing when you have 300 max as when you have 60
// Heavily armored mechs should brush this off until they're seriously damaged
if (maxArmor > modSettings.ArmorDamageThreshold && modSettings.ArmorDamageThreshold > 0)
maxArmor = modSettings.ArmorDamageThreshold;
float percentArmor = stat.Value<float>() / maxArmor;
if (percentArmor > 1)
percentArmor = 1;
return percentArmor;
}
LogDebug($"Mech null");
return 0;
}
internal static float PercentForStructureLocation(Mech mech, int Location)
{
// Invalid locations are always 100%
// This helps makes the functions generic for the missing leg back armor, for example
if (Location == 0)
return 1;
if (mech != null)
{
Statistic stat = mech.StatCollection.GetStatistic(mech.GetStringForStructureLocation((ChassisLocations)Location));
if(stat == null)
{
LogDebug($"Can't get structure stat { mech.DisplayName } location:{ Location}");
return 0;
}
//LogDebug($"Structure stat { mech.DisplayName } location:{ Location} cur:{stat.Value<float>()} max:{stat.DefaultValue<float>()}");
return (stat.Value<float>() / stat.DefaultValue<float>());
}
LogDebug($"Mech null");
return 0;
}
internal static float PercentForLocation(Mech mech, int LocationFront, int LocationBack, int LocationStructure)
{
if (mech != null)
{
float percentFront = PercentForArmorLocation(mech, LocationFront);
float percentBack = PercentForArmorLocation(mech, LocationBack);
float percentStructure = PercentForStructureLocation(mech, LocationStructure);
float percentLocation = percentStructure;
float numAdditions = 2;
// Use the minimum percentage between structure and armor
// This emphasizes internal damage from a blow through (back armor gone or tandem weapons)
percentLocation += Math.Min(percentFront, percentStructure);
if (LocationBack != 0)
{
percentLocation += Math.Min(percentBack, percentStructure);
numAdditions++;
}
percentLocation /= numAdditions;
LogReport($"{((ChassisLocations)LocationStructure).ToString(),-20} | A:{percentFront * 100,7:F3}% | S:{percentStructure * 100,7:F3}%");
if (LocationBack != 0)
{
LogReport($"{" ",-20} | [{percentBack * 100,7:F3}%] | ");
}
return percentLocation;
}
LogDebug($"Mech null");
return 0;
}
private static float PercentForLocation(Vehicle v, VehicleChassisLocations location)
{
if (v != null)
{
var maxs=MaxStructureForLocation(v,(int)location);
var maxa = MaxArmorForLocation(v, (int)location);
var cs = maxs;
var ca = maxa;
if (maxs == 0 || maxa == 0)
{
LogDebug($"Invalid location in vehicle {location.ToString()}");
return 1;
}
switch (location)
{
case VehicleChassisLocations.Turret:
cs = v.TurretStructure;
ca = v.TurretArmor;
break;
case VehicleChassisLocations.Left:
cs = v.LeftSideStructure;
ca = v.LeftSideArmor;
break;
case VehicleChassisLocations.Right:
cs = v.RightSideStructure;
ca = v.RightSideArmor;
break;
case VehicleChassisLocations.Front:
cs = v.FrontStructure;
ca = v.FrontArmor;
break;
case VehicleChassisLocations.Rear:
cs = v.RearStructure;
ca = v.RearArmor;
break;
default:
LogDebug($"Invalid location {location}");
break;
}
float percentArmor = ca/maxa;
float percentStructure = cs/maxs;
//since its easy to kill vehicles once past armor use the armor instead of structure unless structure is damaged.
//this is reverse of mechs.
//Remember the vehicle pilot motto - in armor we trust , structure is for the dead and defeated.
float percentLocation = percentArmor;
float numAdditions = 2;
// Use the minimum percentage between structure and armor
// This emphasizes internal damage from a blow through (back armor gone or tandem weapons)
percentLocation += Math.Min(percentArmor, percentStructure);
percentLocation /= numAdditions;
LogReport($"{location.ToString(),-20} | A:{ca:F3}/{maxa:F3} = {percentArmor * 100,10}% , S:{cs:F3}/{maxs:F3} = {percentStructure * 100,10:F3}%");
return percentLocation;
}
LogDebug($"Vehicle null");
return 0;
}
internal static float PercentRightTorso(Mech mech) =>
(PercentForLocation(mech, (int) ArmorLocation.RightTorso, (int) ArmorLocation.RightTorsoRear,
(int) ChassisLocations.RightTorso));
internal static float PercentLeftTorso(Mech mech) =>
(PercentForLocation(mech, (int) ArmorLocation.LeftTorso, (int) ArmorLocation.LeftTorsoRear,
(int) ChassisLocations.LeftTorso));
internal static float PercentCenterTorso(Mech mech) =>
(PercentForLocation(mech, (int) ArmorLocation.CenterTorso, (int) ArmorLocation.CenterTorsoRear,
(int) ChassisLocations.CenterTorso));
internal static float PercentLeftLeg(Mech mech) =>
(PercentForLocation(mech, (int) ArmorLocation.LeftLeg, 0,
(int) ChassisLocations.LeftLeg));
internal static float PercentRightLeg(Mech mech) =>
(PercentForLocation(mech, (int) ArmorLocation.RightLeg, 0,
(int) ChassisLocations.RightLeg));
internal static float PercentHead(Mech mech) =>
(PercentForLocation(mech, (int) ArmorLocation.Head, 0,
(int) ChassisLocations.Head));
internal static float PercentTurret(Vehicle v) =>
(PercentForLocation(v, VehicleChassisLocations.Turret));
internal static float PercentLeft(Vehicle v) =>
(PercentForLocation(v, VehicleChassisLocations.Left));
internal static float PercentRight(Vehicle v) =>
(PercentForLocation(v, VehicleChassisLocations.Right));
internal static float PercentFront(Vehicle v) =>
(PercentForLocation(v, VehicleChassisLocations.Front));
internal static float PercentRear(Vehicle v) =>
(PercentForLocation(v, VehicleChassisLocations.Rear));
// check if panic roll is possible
private static bool CanPanic(AbstractActor actor, AbstractActor attacker)
{
if (actor == null || actor.IsDead || actor.IsFlaggedForDeath && actor.HasHandledDeath)
{
LogReport($"{attacker?.DisplayName} incapacitated {actor?.DisplayName}");
return false;
}
if ((actor.team.IsLocalPlayer && !modSettings.PlayersCanPanic) ||
(!actor.team.IsLocalPlayer && !modSettings.EnemiesCanPanic) ||
(actor is Vehicle && !modSettings.VehiclesCanPanic))
{
return false;
}
return true;
}
// Returns a float to modify panic roll difficulty based on existing panic level
internal static float GetPanicModifier(PanicStatus pilotStatus)
{
// ReSharper disable once SwitchStatementMissingSomeCases
switch (pilotStatus)
{
case PanicStatus.Unsettled: return modSettings.UnsettledPanicFactor;
case PanicStatus.Stressed: return modSettings.StressedPanicFactor;
case PanicStatus.Panicked: return modSettings.PanickedPanicFactor;
default: return 1f;
}
}
internal static void DrawHeader()
{
LogReport(new string('-', 46));
LogReport($"{"Factors",-20} | {"Change",10} | {"Total",10}");
LogReport(new string('-', 46));
}
internal static void SaySpamFloatie(AbstractActor actor, string message)
{
if (!modSettings.FloatieSpam ||
string.IsNullOrEmpty(message))
{
return;
}
actor.Combat.MessageCenter.PublishMessage(new AddSequenceToStackMessage(
new ShowActorInfoSequence(actor, message, FloatieMessage.MessageNature.Neutral, false)));
}
// true implies a panic condition was met
public static bool ShouldPanic(AbstractActor actor, AbstractActor attacker, out int heatdamage, out float damageIncludingHeatDamage)
{
if (!CanPanic(actor, attacker))
{
damageIncludingHeatDamage = 0;
heatdamage = 0;
return false;
}
return SufficientDamageWasDone(actor, out heatdamage, out damageIncludingHeatDamage);
}
public static bool ShouldSkipProcessing(AbstractActor actor)
{
// can't do stuff with buildings
if (!(actor is Vehicle) &&
!(actor is Mech))
{
return true;
}
return actor?.GUID == null;
}
// returns true if enough damage was inflicted to trigger a panic save
private static bool SufficientDamageWasDone(AbstractActor actor, out int heatdamage, out float damageIncludingHeatDamage)
{
if (actor == null)
{
damageIncludingHeatDamage = 0;
heatdamage = 0;
return false;
}
float armorDamage;
float structureDamage;
float previousArmor;
float previousStructure;
//don't need the damage numbers as we can check the actor itself.
TurnDamageTracker.DamageDuringTurn(actor, out armorDamage, out structureDamage, out previousArmor, out previousStructure, out heatdamage);
// used in SavingThrows.cs
damageIncludingHeatDamage = armorDamage + structureDamage;
#if NO_CAC
if(true){
#else
if (!(actor is Mech) || actor.isHasHeat()){//Battle Armor doesn't have heat
#endif
damageIncludingHeatDamage = damageIncludingHeatDamage + (heatdamage * modSettings.HeatDamageFactor);
}
if (damageIncludingHeatDamage <= 0)//potentially negative if repairs happened.
{
LogReport($"Damage >>> A: {armorDamage:F3}/{previousArmor:F3} S: {structureDamage:F3}/{previousStructure:F3} NA%) H: {heatdamage}");
LogReport("No damage");
return false;
}
var percentDamageDone =
damageIncludingHeatDamage / (previousArmor + previousStructure) * 100;
LogReport($"Damage >>> A: {armorDamage:F3}/{previousArmor:F3} S: {structureDamage:F3}/{previousStructure:F3} ({percentDamageDone:F2}%) H: {heatdamage}");
if (modSettings.AlwaysPanic)
{
LogReport("AlwaysPanic");
return true;
}
if ((actor is Mech &&
structureDamage >= modSettings.MinimumMechStructureDamageRequired) ||
(modSettings.VehiclesCanPanic &&
actor is Vehicle &&
structureDamage >= modSettings.MinimumVehicleStructureDamageRequired))
{
LogReport("Structure damage requires panic save");
return true;
}
if (percentDamageDone <= modSettings.MinimumDamagePercentageRequired)
{
LogReport("Not enough damage");
return false;
}
LogReport("Total damage requires a panic save");
return true;
}
internal static void SetupEjectPhrases(string modDir)
{
if (!modSettings.EnableEjectPhrases)
{
return;
}
if (!modSettings.EnableEjectPhrases) return;
try
{
ejectPhraseList = File.ReadAllText(Path.Combine(modDir, "phrases.txt")).Split('\n').ToList();
}
catch (Exception ex)
{
LogReport("Error - problem loading phrases.txt but the setting is enabled");
LogDebug(ex);
// in case the file is missing but the setting is enabled
modSettings.EnableEjectPhrases = false;
}
}
internal static void ApplyPanicStatus(AbstractActor __instance, PanicStatus panicStatus, bool worsened)
{
var actor = __instance;
int Uid() => Random.Range(1, int.MaxValue);
var effectManager = UnityGameInstance.BattleTechGame.Combat.EffectManager;
// remove all PanicSystem effects first
ClearPanicEffects(actor, effectManager);
// re-apply effects
var messageNature = worsened ? FloatieMessage.MessageNature.Debuff : FloatieMessage.MessageNature.Buff;
var verb = worsened ? modSettings.PanicWorsenedString : modSettings.PanicImprovedString;
// account for the space, append it when the verb is defined
if (!string.IsNullOrEmpty(verb))
{
verb += " ";
}
var message = actor.Combat.MessageCenter;
var dummyWeapon = new WeaponHitInfo();
switch (panicStatus)
{
case PanicStatus.Unsettled:
LogReport($"{actor.DisplayName} {verb}{modSettings.PanicStates[1]}");
message.PublishMessage(new AddSequenceToStackMessage(
new ShowActorInfoSequence(actor,
$"{verb}{modSettings.PanicStates[1]}",
messageNature,
false)));
effectManager.CreateEffect(StatusEffect.UnsettledToHit, "PanicSystemToHit", Uid(), actor, actor, dummyWeapon, 0);
break;
case PanicStatus.Stressed:
LogReport($"{actor.DisplayName} {verb}{modSettings.PanicStates[2]}");
message.PublishMessage(new AddSequenceToStackMessage(
new ShowActorInfoSequence(actor,
$"{verb}{modSettings.PanicStates[2]}",
messageNature,
false)));
effectManager.CreateEffect(StatusEffect.StressedToHit, "PanicSystemToHit", Uid(), actor, actor, dummyWeapon, 0);
effectManager.CreateEffect(StatusEffect.StressedToBeHit, "PanicSystemToBeHit", Uid(), actor, actor, dummyWeapon, 0);
break;
case PanicStatus.Panicked:
LogReport($"{actor.DisplayName} {verb}{modSettings.PanicStates[3]}");
message.PublishMessage(new AddSequenceToStackMessage(
new ShowActorInfoSequence(actor,
$"{verb}{modSettings.PanicStates[3]}",
messageNature,
false)));
effectManager.CreateEffect(StatusEffect.PanickedToHit, "PanicSystemToHit", Uid(), actor, actor, dummyWeapon, 0);
effectManager.CreateEffect(StatusEffect.PanickedToBeHit, "PanicSystemToBeHit", Uid(), actor, actor, dummyWeapon, 0);
break;
default:
LogReport($"{actor.DisplayName} {verb}{modSettings.PanicStates[0]}");
message.PublishMessage(new AddSequenceToStackMessage(
new ShowActorInfoSequence(actor,
$"{verb}{modSettings.PanicStates[0]}",
messageNature,
false)));
break;
}
}
private static void ClearPanicEffects(AbstractActor actor, EffectManager effectManager)
{
var effects = Traverse.Create(effectManager).Field("effects").GetValue<List<Effect>>();
for (var i = 0; i < effects.Count; i++)
{
if (effects[i].id.StartsWith("PanicSystem") && Traverse.Create(effects[i]).Field("target").GetValue<object>() == actor)
{
effectManager.CancelEffect(effects[i]);
}
}
}
}
}