-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCShieldClass.cpp
1915 lines (1438 loc) · 46.6 KB
/
CShieldClass.cpp
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// CShieldClass.cpp
//
// CShieldClass class
#include "PreComp.h"
#define HIT_EFFECT_TAG CONSTLIT("HitEffect")
#define ABSORB_ADJ_ATTRIB CONSTLIT("absorbAdj")
#define AIMED_REFLECTION_ATTRIB CONSTLIT("aimedReflection")
#define ARMOR_SHIELD_ATTRIB CONSTLIT("armorShield")
#define DAMAGE_ADJ_ATTRIB CONSTLIT("damageAdj")
#define DAMAGE_ADJ_LEVEL_ATTRIB CONSTLIT("damageAdjLevel")
#define DEPLETION_DELAY_ATTRIB CONSTLIT("depletionDelay")
#define HAS_NON_REGEN_HP_ATTRIB CONSTLIT("hasNonRegenHP")
#define HIT_EFFECT_ATTRIB CONSTLIT("hitEffect")
#define HIT_POINTS_ATTRIB CONSTLIT("hitPoints")
#define IDLE_POWER_USE_ATTRIB CONSTLIT("idlePowerUse")
#define HP_ADJ_PER_CHARGE_ATTRIB CONSTLIT("HPBonusPerCharge")
#define MAX_CHARGES_ATTRIB CONSTLIT("maxCharges")
#define POWER_ADJ_PER_CHARGE_ATTRIB CONSTLIT("powerBonusPerCharge")
#define POWER_USE_ATTRIB CONSTLIT("powerUse")
#define REFLECT_ATTRIB CONSTLIT("reflect")
#define REGEN_ATTRIB CONSTLIT("regen")
#define REGEN_HP_ATTRIB CONSTLIT("regenHP")
#define REGEN_ADJ_PER_CHARGE_ATTRIB CONSTLIT("regenHPBonusPerCharge")
#define REGEN_TIME_ATTRIB CONSTLIT("regenTime")
#define REGEN_TYPE_ATTRIB CONSTLIT("regenType")
#define WEAPON_SUPPRESS_ATTRIB CONSTLIT("weaponSuppress")
#define GET_MAX_HP_EVENT CONSTLIT("GetMaxHP")
#define ON_SHIELD_DAMAGE_EVENT CONSTLIT("OnShieldDamage")
#define ON_SHIELD_DOWN_EVENT CONSTLIT("OnShieldDown")
#define FIELD_ADJUSTED_HP CONSTLIT("adjustedHP")
#define FIELD_BALANCE CONSTLIT("balance")
#define FIELD_DAMAGE_ADJ CONSTLIT("damageAdj")
#define FIELD_EFFECTIVE_HP CONSTLIT("effectiveHP")
#define FIELD_HP CONSTLIT("hp")
#define FIELD_HP_BONUS CONSTLIT("hpBonus")
#define FIELD_POWER CONSTLIT("power")
#define FIELD_REGEN CONSTLIT("regen")
#define FIELD_WEAPON_SUPPRESS CONSTLIT("weaponSuppress")
#define PROPERTY_DAMAGE_ADJ CONSTLIT("damageAdj")
#define PROPERTY_HP CONSTLIT("hp")
#define PROPERTY_HP_BONUS CONSTLIT("hpBonus")
#define PROPERTY_MAX_HP CONSTLIT("maxHP")
#define PROPERTY_REGEN CONSTLIT("regen")
#define STR_BY_SHIELD_INTEGRITY CONSTLIT("byShieldIntegrity")
#define STR_SHIELD_REFLECT CONSTLIT("reflect")
#define STR_STANDARD CONSTLIT("standard")
#define MAX_REFLECTION_CHANCE 95
const Metric MAX_REFLECTION_TARGET = 50.0 * LIGHT_SECOND;
const Metric STD_FIRE_DELAY_TICKS = 8.0;
const Metric STD_DEFENSE_RATIO = 0.8;
const int STD_DEPLETION_DELAY = 360;
const Metric REFLECTION_BALANCE_BONUS = 50.0;
const Metric BALANCE_COST_RATIO = -0.5; // Each percent of cost above standard is a 0.5%
const Metric BALANCE_RECOVERY_ADJ = 20.0; // Balance points if we recover twice as fast as normal.
const Metric BALANCE_POWER_RATIO = -0.5; // Each percent of power consumption above standard
const Metric BALANCE_NO_SLOT = 20.0; // Bonus to balance if no slot is used.
const Metric BALANCE_SLOT_FACTOR = -40.0; // Penalty to balance for every extra slot used
const Metric BALANCE_LEAKAGE_FACTOR = -25.0; // Penalty from allowing some damage through
const Metric BALANCE_LEAKAGE_POWER = 0.5; // Curve for leakage
const Metric BALANCE_MAX_DAMAGE_ADJ = 400.0; // Max change in balance due to a single damage type
static CShieldClass::SStdStats STD_STATS[MAX_ITEM_LEVEL] =
{
// HP Regen Cost Power
{ 35, 12, 350.0, 10, },
{ 45, 16, 800.0, 20, },
{ 60, 20, 1850.0, 50, },
{ 80, 28, 4000.0, 100, },
{ 100, 36, 8500.0, 200, },
{ 135, 45, 18500.0, 300, },
{ 175, 60, 39000.0, 500, },
{ 225, 80, 78500.0, 1000, },
{ 300, 100, 158000.0, 2000, },
{ 380, 130, 320000.0, 3000, },
{ 500, 170, 640000.0, 5000, },
{ 650, 220, 1300000.0, 8000, },
{ 850, 290, 2700000.0, 12000, },
{ 1100, 370, 5300000.0, 18000, },
{ 1400, 490, 10600000.0, 27500, },
{ 1850, 630, 21300000.0, 40000, },
{ 2400, 820, 42600000.0, 60000, },
{ 3100, 1100, 85200000.0, 90000, },
{ 4000, 1400, 170000000.0, 120000, },
{ 5250, 1800, 341000000.0, 160000, },
{ 6850, 2350, 682000000.0, 220000, },
{ 9000, 3050, 1400000000.0, 300000, },
{ 12000, 4000, 2700000000.0, 400000, },
{ 15000, 5150, 5500000000.0, 500000, },
{ 20000, 6700, 10900000000.0, 600000, },
};
static char *CACHED_EVENTS[CShieldClass::evtCount] =
{
"GetMaxHP",
"OnShieldDamage",
"OnShieldDown",
};
inline CShieldClass::SStdStats *GetStdStats (int iLevel)
{
if (iLevel >= 1 && iLevel <= MAX_ITEM_LEVEL)
return &STD_STATS[iLevel - 1];
else
return NULL;
}
CShieldClass::CShieldClass (void)
{
}
bool CShieldClass::AbsorbDamage (CInstalledDevice *pDevice, CSpaceObject *pShip, SDamageCtx &Ctx)
// AbsorbDamage
//
// Absorbs damage.
// NOTE: We always set Ctx.iAbsorb properly, regardless of the return value.
{
DEBUG_TRY
CItemCtx ItemCtx(pShip, pDevice);
const CItemEnhancementStack *pEnhancements = ItemCtx.GetEnhancementStack();
// If we're depleted then we cannot absorb anything
Ctx.iHPLeft = GetHPLeft(ItemCtx);
if (Ctx.iHPLeft == 0)
{
Ctx.iAbsorb = 0;
return false;
}
// Calculate how much we will absorb
int iAbsorbAdj = (Ctx.Damage.GetDamageType() == damageGeneric ? 100 : m_iAbsorbAdj[Ctx.Damage.GetDamageType()]);
Ctx.iAbsorb = mathAdjust(Ctx.iDamage, iAbsorbAdj);
if (pEnhancements)
Ctx.iAbsorb = mathAdjust(Ctx.iAbsorb, pEnhancements->GetAbsorbAdj(Ctx.Damage));
// Compute how much damage we take (based on the type of damage)
int iAdj = GetDamageAdj(Ctx.Damage, pEnhancements);
Ctx.iShieldDamage = mathAdjust(Ctx.iAbsorb, iAdj);
// If shield generator is damaged then sometimes we take extra damage
if (pDevice->IsDamaged() || pDevice->IsDisrupted())
{
int iRoll = mathRandom(1, 100);
if (iRoll <= 10)
Ctx.iAbsorb = mathAdjust(Ctx.iAbsorb, 75);
else if (iRoll <= 25)
Ctx.iShieldDamage *= 2;
}
// If the amount of shield damage is greater than HP left, then scale
// the amount of HP that we aborb
if (Ctx.iShieldDamage > Ctx.iHPLeft)
{
// ASSERT: We know that iShieldDamage is > 0 because iHPLeft is > 0.
Ctx.iAbsorb = Ctx.iHPLeft * Ctx.iAbsorb / Ctx.iShieldDamage;
Ctx.iShieldDamage = Ctx.iHPLeft;
}
// See if we're reflective
int iReflectChance = 0;
if (pEnhancements)
pEnhancements->ReflectsDamage(Ctx.Damage.GetDamageType(), &iReflectChance);
if (m_Reflective.InSet(Ctx.Damage.GetDamageType()))
iReflectChance = Max(iReflectChance, MAX_REFLECTION_CHANCE);
if (iReflectChance
&& Ctx.pCause)
{
CItemCtx ItemCtx(pShip, pDevice);
// If the shot has shield damage, we adjust the chance of reflecting.
// Adjust based on difference in level (negative numbers means the shield
// is lower than the damage level):
//
// ...
// <-3 = No reflect
// -3 = 50% reflect adj
// -2 = 75% reflect adj
// -1 = 90% reflect adj
// >=0 = Full reflect
if (Ctx.Damage.GetShieldDamageLevel() > 0)
{
int iDiff = GetLevel() - Ctx.Damage.GetShieldDamageLevel();
switch (iDiff)
{
case -3:
iReflectChance = 50 * iReflectChance / 100;
break;
case -2:
iReflectChance = 75 * iReflectChance / 100;
break;
case -1:
iReflectChance = 90 * iReflectChance / 100;
break;
default:
if (iDiff < -3)
iReflectChance = 0;
break;
}
}
// Compute the chance that we will reflect (based on the strength of
// our shields)
int iMaxHP = GetMaxHP(ItemCtx);
int iEfficiency = (iMaxHP == 0 ? 100 : 50 + (Ctx.iHPLeft * 50 / iMaxHP));
int iChance = iEfficiency * iReflectChance / 100;
// See if we reflect
Ctx.SetShotReflected(mathRandom(1, 100) <= iChance);
if (Ctx.IsShotReflected())
{
Ctx.iOriginalAbsorb = Ctx.iAbsorb;
Ctx.iOriginalShieldDamage = Ctx.iShieldDamage;
Ctx.iAbsorb = Ctx.iDamage;
Ctx.iShieldDamage = 0;
}
}
else
Ctx.SetShotReflected(false);
// If this damage is a shield penetrator, then we either penetrate the
// shields, or reflect.
int iPenetrateAdj = Ctx.Damage.GetShieldPenetratorAdj();
if (iPenetrateAdj > 0 && !Ctx.IsShotReflected())
{
// We compare the HP of damage done to the shields vs. the HP left
// on the shields. Bigger values means greater chance of penetrating.
// We scale to 0-100.
//
// NOTE: We can guarantee that Ctx.iHPLeft > 0.
Metric rPenetrate = (200.0 / (Metric)Ctx.iHPLeft) * ((Metric)Ctx.iShieldDamage * (Metric)Ctx.iShieldDamage) / ((Metric)Ctx.iShieldDamage + (Metric)Ctx.iHPLeft);
// Adjust for tech level
int iWeaponLevel = Ctx.pDesc->GetLevel();
int iShieldLevel = (pDevice ? pDevice->GetLevel() : 1);
rPenetrate *= pow(1.5, (iWeaponLevel - iShieldLevel));
// Add the adjustment
int iChance = (int)rPenetrate + iPenetrateAdj;
// Roll the chance. If we penetrate, then the shields absorb nothing
// and the shot goes clean through.
if (mathRandom(1, 100) <= iChance)
{
Ctx.iOriginalAbsorb = Ctx.iAbsorb;
Ctx.iOriginalShieldDamage = Ctx.iShieldDamage;
Ctx.iAbsorb = 0;
Ctx.iShieldDamage = Ctx.iShieldDamage / 4;
}
// Otherwise, we reflect.
else
{
Ctx.SetShotReflected(true);
Ctx.iOriginalAbsorb = Ctx.iAbsorb;
Ctx.iOriginalShieldDamage = Ctx.iShieldDamage;
Ctx.iAbsorb = Ctx.iDamage;
Ctx.iShieldDamage = 0;
}
}
// If we have time stop damage, then there is a chance that the shields
// will prevent it.
//
// The chance of negating time stop damage depends on the difference in
// levels.
if (Ctx.IsTimeStopped()
&& mathRandom(1, 100) <= Ctx.Damage.GetTimeStopResistChance(GetLevel()))
{
Ctx.SetTimeStopped(false);
}
// Give custom damage a chance to react. These events can modify the
// following variables:
//
// Ctx.bReflect
// Ctx.iAbsorb
// Ctx.iShieldDamage
//
// OR
//
// Ctx.iDamage (if we skip further processing)
if (Ctx.pDesc->FireOnDamageShields(Ctx, pDevice->GetDeviceSlot()))
return (Ctx.iDamage == 0);
FireOnShieldDamage(CItemCtx(pShip, pDevice), Ctx);
// If we reflect, then create the reflection
if (Ctx.IsShotReflected())
{
int iDirection;
// If we aim the reflection, then figure out what to aim at.
CSpaceObject *pTarget;
if (m_fAimReflection
&& (pTarget = pShip->GetNearestVisibleEnemy(MAX_REFLECTION_TARGET))
&& (iDirection = pShip->CalcFireSolution(pTarget, Ctx.pCause->GetMaxSpeed())) != -1)
{
// iDirection is set
}
// Otherwise, we reflect randomly
else
{
iDirection = AngleMod(Ctx.iDirection + mathRandom(120, 240));
}
Ctx.pCause->CreateReflection(Ctx.vHitPos, iDirection);
}
// Create shield effect
if ((Ctx.iAbsorb || Ctx.IsShotReflected())
&& m_pHitEffect
&& !Ctx.bNoHitEffect)
{
m_pHitEffect->CreateEffect(pShip->GetSystem(),
NULL,
Ctx.vHitPos,
pShip->GetVel(),
Ctx.iDirection);
}
// Shield takes damage
if (Ctx.iShieldDamage > 0)
{
if (Ctx.iShieldDamage >= Ctx.iHPLeft)
SetDepleted(pDevice, pShip);
else
SetHPLeft(pDevice, pShip, Ctx.iHPLeft - Ctx.iShieldDamage, true);
pShip->OnComponentChanged(comShields);
}
// Set the remaining damage
Ctx.iDamage -= Ctx.iAbsorb;
return (Ctx.iDamage == 0);
DEBUG_CATCH
}
bool CShieldClass::AbsorbsWeaponFire (CInstalledDevice *pDevice, CSpaceObject *pSource, CInstalledDevice *pWeapon)
// AbsorbsWeaponFire
//
// Returns TRUE if the shield absorbs fire from the given weapon
// when installed on the same ship
{
int iType = pWeapon->GetDamageType(CItemCtx(pSource, pDevice));
if (iType != -1
&& m_WeaponSuppress.InSet(iType)
&& pDevice->IsWorking()
&& !IsDepleted(pDevice))
{
// Create effect
if (m_pHitEffect)
m_pHitEffect->CreateEffect(pSource->GetSystem(),
pSource,
pWeapon->GetPos(pSource),
pSource->GetVel(),
0);
return true;
}
else
return false;
}
bool CShieldClass::Activate (CInstalledDevice *pDevice,
CSpaceObject *pSource,
CSpaceObject *pTarget,
bool *retbSourceDestroyed,
bool *retbConsumedItems)
// Activate
//
// Activates device
{
return false;
}
int CShieldClass::CalcBalance (CItemCtx &ItemCtx, SBalance &retBalance) const
// CalcBalance
//
// Computes the relative balance of this shield relative to its level
{
// Initialize
retBalance.iLevel = GetLevel();
// Get standard stats at level
SStdStats *pStd = GetStdStats(retBalance.iLevel);
if (pStd == NULL)
return 0;
// Get HP/regen stats for the shields
int iMaxHP;
CalcMinMaxHP(ItemCtx, ItemCtx.GetItem().GetMaxCharges(), 0, 0, NULL, &iMaxHP);
Metric rRegen180 = m_Regen.GetHPPer180();
// Compute our defense ratio
CalcBalanceDefense(ItemCtx, retBalance.iLevel, iMaxHP, rRegen180, &retBalance.rDefenseRatio);
// Compute balance contribution from just HP (with standard regen)
retBalance.rHPBalance = CalcBalanceDefense(ItemCtx, retBalance.iLevel, iMaxHP, pStd->iRegen);
retBalance.rBalance = retBalance.rHPBalance;
// Compute balance from regen (with standard HP)
retBalance.rRegen = CalcBalanceDefense(ItemCtx, retBalance.iLevel, pStd->iHP, rRegen180);
retBalance.rBalance += retBalance.rRegen;
// Damage adjustment
retBalance.rDamageAdj = CalcBalanceDamageAdj(ItemCtx);
retBalance.rBalance += retBalance.rDamageAdj;
// Depletion recovery adj
retBalance.rRecoveryAdj = BALANCE_RECOVERY_ADJ * mathLog2((Metric)STD_DEPLETION_DELAY / (Metric)Max(1, m_iDepletionTicks));
retBalance.rBalance += retBalance.rRecoveryAdj;
// Power use
retBalance.rPowerUse = CalcBalancePowerUse(ItemCtx, *pStd);
retBalance.rBalance += retBalance.rPowerUse;
// Slots
if (GetSlotsRequired() == 0)
retBalance.rSlots = BALANCE_NO_SLOT;
else if (GetSlotsRequired() > 1)
retBalance.rSlots = BALANCE_SLOT_FACTOR * (GetSlotsRequired() - 1);
else
retBalance.rSlots = 0.0;
retBalance.rBalance += retBalance.rSlots;
// Cost
Metric rCost = (Metric)CEconomyType::ExchangeToCredits(GetItemType()->GetCurrencyAndValue(ItemCtx, true));
Metric rCostDelta = 100.0 * (rCost - pStd->rCost) / (Metric)pStd->rCost;
retBalance.rCost = BALANCE_COST_RATIO * rCostDelta;
retBalance.rBalance += retBalance.rCost;
// Done
return (int)retBalance.rBalance;
}
Metric CShieldClass::CalcBalanceDamageAdj (CItemCtx &ItemCtx) const
// CalcBalanceDamageAdj
//
// Calculate balance contribution from damage adjustment.
{
DamageTypes iDamage;
// Loop over all damage types and accumulate balance adjustments.
Metric rTotalBalance = 0.0;
for (iDamage = damageLaser; iDamage < damageCount; iDamage = (DamageTypes)(iDamage + 1))
{
int iAdj;
int iDefaultAdj;
m_DamageAdj.GetAdjAndDefault(iDamage, &iAdj, &iDefaultAdj);
// Compare the adjustment and the standard adjustment. [We treat 0
// as 1 to avoid infinity.]
Metric rBalance = (100.0 * mathLog2((Metric)Max(1, iDefaultAdj) / (Metric)Max(1, iAdj)));
if (rBalance > BALANCE_MAX_DAMAGE_ADJ)
rBalance = BALANCE_MAX_DAMAGE_ADJ;
else if (rBalance < -BALANCE_MAX_DAMAGE_ADJ)
rBalance = -BALANCE_MAX_DAMAGE_ADJ;
// If we reflect this damage, adjust balance
if (m_Reflective.InSet(iDamage))
rBalance += REFLECTION_BALANCE_BONUS;
// If we allow some damage through, then we take a penalty
if (m_iAbsorbAdj[iDamage] < 100)
{
Metric rLeakage = (100.0 - m_iAbsorbAdj[iDamage]);
rBalance += BALANCE_LEAKAGE_FACTOR * pow(rLeakage, BALANCE_LEAKAGE_POWER);
}
// Adjust the based on how common this damage type is at the given
// armor level.
rBalance *= CDamageAdjDesc::GetDamageTypeFraction(GetLevel(), iDamage);
// Accumulate
rTotalBalance += rBalance;
}
// Done
return rTotalBalance;
}
Metric CShieldClass::CalcBalanceDefense (CItemCtx &ItemCtx, int iLevel, Metric rHP, Metric rRegen180, Metric *retrRatio) const
// CalcBalanceDefense
//
// Calc defense balance based on level, HP, and regen.
{
Metric rCycles = 3.0;
Metric rEffectiveHP = rHP + (rCycles * rRegen180);
// Our defense ratio is the ratio of effective HP to 180-tick std weapon
// damage at our level. The standard value for this ratio is STD_DEFENSE_RATIO.
Metric rDefenseRatio = rEffectiveHP / CWeaponClass::GetStdDamage180(iLevel);
// Result
if (retrRatio)
*retrRatio = rDefenseRatio;
return 100.0 * mathLog2(rDefenseRatio / STD_DEFENSE_RATIO);
}
Metric CShieldClass::CalcBalancePowerUse (CItemCtx &ItemCtx, const SStdStats &Stats) const
// CalcBalancePowerUse
//
// Balance contribution from power use.
{
int iPower = m_iPowerUse + (m_iExtraPowerPerCharge * ItemCtx.GetItem().GetMaxCharges());
Metric rPowerDelta = 100.0 * (iPower - Stats.iPower) / (Metric)Stats.iPower;
return BALANCE_POWER_RATIO * rPowerDelta;
}
void CShieldClass::CalcMinMaxHP (CItemCtx &Ctx, int iCharges, int iArmorSegs, int iTotalHP, int *retiMin, int *retiMax) const
// CalcMinMaxHP
//
// Returns the min and max HP of this shield
//
// iCharges = m_iMaxCharges or the current charges on item
// iArmorSegs = count of armor segments on ship (or 0)
// iTotalHP = current total HP of all armor segments (or 0)
{
int iMax = m_iHitPoints;
int iMin = iMax;
if (m_iExtraHPPerCharge)
iMax = Max(0, iMax + (m_iExtraHPPerCharge * iCharges));
else if (m_fHasNonRegenHPBonus)
iMax = Max(0, iMax + iCharges);
if (m_iArmorShield)
{
iMin = m_iArmorShield;
if (iArmorSegs > 0)
iMax = Min(iMax, ((m_iArmorShield * iTotalHP / iArmorSegs) + 5) / 10);
}
// If we're installed, fire the custom event to get max HPs
if (Ctx.GetSource() && Ctx.GetDevice())
iMax = FireGetMaxHP(Ctx.GetDevice(), Ctx.GetSource(), iMax);
// Adjust max HP based on enhancements
const CItemEnhancementStack *pEnhancements = Ctx.GetEnhancementStack();
if (pEnhancements)
iMax += (iMax * pEnhancements->GetBonus() / 100);
// Done
if (iMin > iMax)
iMin = iMax;
if (retiMin)
*retiMin = iMin;
if (retiMax)
*retiMax = iMax;
}
int CShieldClass::CalcPowerUsed (SUpdateCtx &UpdateCtx, CInstalledDevice *pDevice, CSpaceObject *pSource)
// CalcPowerUsed
//
// Returns the amount of power used by the shields
{
CItemCtx Ctx(pSource, pDevice);
// Only if enabled
if (!pDevice->IsEnabled())
return 0;
// Compute power
int iIdlePower;
int iPower = GetPowerRating(Ctx, &iIdlePower);
// If we're regenerating shields, then we consume more power
// otherwise, we only consume half power
if ((!m_Regen.IsEmpty() || m_iExtraRegenPerCharge > 0) && pDevice->IsRegenerating())
return iPower;
else
return iIdlePower;
}
Metric CShieldClass::CalcRegen180 (CItemCtx &Ctx, DWORD dwFlags) const
// CalcRegen180
//
// Returns the average number of HP regenerated every 180 ticks.
//
// NOTE: This is used for stats purposes; the actual regeneration uses a
// different algorithm.
{
// If there is interference, then we don't regenerate
CSpaceObject *pSource = Ctx.GetSource();
if (pSource && pSource->GetShipPerformance().HasShieldInterference())
return 0.0;
// If the device is disabled, then no regeneration
if (!Ctx.IsDeviceEnabled() && !(dwFlags & FLAG_IGNORE_DISABLED))
return 0.0;
// Compute regeneration
Metric rRegen = m_Regen.GetHPPer180();
if (m_iExtraHPPerCharge)
rRegen += m_iExtraHPPerCharge * Ctx.GetItemCharges();
return rRegen;
}
ALERROR CShieldClass::CreateFromXML (SDesignLoadCtx &Ctx, SInitCtx &InitCtx, CXMLElement *pDesc, CDeviceClass **retpShield)
// CreateFromXML
//
// Creates from an XML element
{
ALERROR error;
CShieldClass *pShield;
int i;
pShield = new CShieldClass;
if (pShield == NULL)
return ERR_MEMORY;
if (error = pShield->InitDeviceFromXML(Ctx, pDesc, InitCtx.pType))
return error;
pShield->m_iHitPoints = pDesc->GetAttributeInteger(HIT_POINTS_ATTRIB);
pShield->m_iArmorShield = pDesc->GetAttributeInteger(ARMOR_SHIELD_ATTRIB);
pShield->m_iPowerUse = pDesc->GetAttributeIntegerBounded(POWER_USE_ATTRIB, 0, -1, 0);
pShield->m_iIdlePowerUse = pDesc->GetAttributeIntegerBounded(IDLE_POWER_USE_ATTRIB, 0, -1, pShield->m_iPowerUse / 2);
// Charges
pShield->m_iExtraHPPerCharge = pDesc->GetAttributeInteger(HP_ADJ_PER_CHARGE_ATTRIB);
pShield->m_iExtraPowerPerCharge = pDesc->GetAttributeInteger(POWER_ADJ_PER_CHARGE_ATTRIB);
pShield->m_iExtraRegenPerCharge = pDesc->GetAttributeInteger(REGEN_ADJ_PER_CHARGE_ATTRIB);
pShield->m_fHasNonRegenHPBonus = pDesc->GetAttributeBool(HAS_NON_REGEN_HP_ATTRIB);
// For backwards compatibility we allow the shield descriptor to set max charges
// on the item type.
InitCtx.iMaxCharges = pDesc->GetAttributeIntegerBounded(MAX_CHARGES_ATTRIB, 0, -1, -1);
// Load regen value
CString sRegen;
if (pDesc->FindAttribute(REGEN_ATTRIB, &sRegen))
{
if (error = pShield->m_Regen.InitFromRegenString(Ctx, sRegen))
return error;
int iDepletion;
if (pDesc->FindAttributeInteger(DEPLETION_DELAY_ATTRIB, &iDepletion))
pShield->m_iDepletionTicks = Max(1, iDepletion);
else
pShield->m_iDepletionTicks = STD_DEPLETION_DELAY;
}
else
{
int iRegenTime = pDesc->GetAttributeInteger(REGEN_TIME_ATTRIB);
if (error = pShield->m_Regen.InitFromRegenTimeAndHP(Ctx, iRegenTime, pDesc->GetAttributeInteger(REGEN_HP_ATTRIB)))
return error;
int iDepletion;
if (pDesc->FindAttributeInteger(DEPLETION_DELAY_ATTRIB, &iDepletion))
pShield->m_iDepletionTicks = Max(1, iDepletion * iRegenTime);
else
pShield->m_iDepletionTicks = STD_DEPLETION_DELAY;
}
// Variable regen
pShield->m_fRegenByShieldLevel = false;
CString sAttrib;
if (pDesc->FindAttribute(REGEN_TYPE_ATTRIB, &sAttrib))
{
if (strEquals(sAttrib, STR_STANDARD))
{ }
else if (strEquals(sAttrib, STR_BY_SHIELD_INTEGRITY))
pShield->m_fRegenByShieldLevel = true;
else
{
Ctx.sError = strPatternSubst(CONSTLIT("Invalid variable regen: %s"), sAttrib);
return ERR_FAIL;
}
}
// Load damage adjustment
pShield->m_iDamageAdjLevel = pDesc->GetAttributeIntegerBounded(DAMAGE_ADJ_LEVEL_ATTRIB, 1, MAX_ITEM_LEVEL, InitCtx.pType->GetLevel());
if (error = pShield->m_DamageAdj.InitFromXML(Ctx, pDesc))
return error;
// Load absorb adjustment; if attribute not found, assume 100% for everything
CString sAbsorbAdj;
if (pDesc->FindAttribute(ABSORB_ADJ_ATTRIB, &sAbsorbAdj))
{
TArray<int> AbsorbAdj;
if (error = ::ParseAttributeIntegerList(sAbsorbAdj, &AbsorbAdj))
return error;
for (i = 0; i < damageCount; i++)
pShield->m_iAbsorbAdj[i] = (i < AbsorbAdj.GetCount() ? AbsorbAdj[i] : 0);
}
else
{
for (i = 0; i < damageCount; i++)
pShield->m_iAbsorbAdj[i] = 100;
}
// Load the weapon suppress
if (error = pShield->m_WeaponSuppress.InitFromXML(pDesc->GetAttribute(WEAPON_SUPPRESS_ATTRIB)))
{
Ctx.sError = CONSTLIT("Unable to load weapon suppress attribute");
return error;
}
// Load reflection
if (error = pShield->m_Reflective.InitFromXML(pDesc->GetAttribute(REFLECT_ATTRIB)))
{
Ctx.sError = CONSTLIT("Unable to load reflective attribute");
return error;
}
pShield->m_fAimReflection = pDesc->GetAttributeBool(AIMED_REFLECTION_ATTRIB);
// Effects
if (error = pShield->m_pHitEffect.LoadEffect(Ctx,
strPatternSubst(CONSTLIT("%d:h"), InitCtx.pType->GetUNID()),
pDesc->GetContentElementByTag(HIT_EFFECT_TAG),
pDesc->GetAttribute(HIT_EFFECT_ATTRIB)))
return error;
// Done
*retpShield = pShield;
return NOERROR;
}
void CShieldClass::Deplete (CInstalledDevice *pDevice, CSpaceObject *pSource)
// Disable
//
// Lower shields
{
SetDepleted(pDevice, pSource);
pSource->OnComponentChanged(comShields);
}
bool CShieldClass::FindDataField (const CString &sField, CString *retsValue)
// FindDataField
//
// Returns meta-data
{
int i;
if (strEquals(sField, FIELD_HP))
*retsValue = strFromInt(m_iHitPoints);
else if (strEquals(sField, FIELD_EFFECTIVE_HP))
{
int iHP;
int iHPbyDamageType[damageCount];
GetReferenceDamageAdj(NULL, NULL, &iHP, iHPbyDamageType);
*retsValue = strFromInt(::CalcEffectiveHP(GetLevel(), iHP, iHPbyDamageType));
}
else if (strEquals(sField, FIELD_REGEN))
*retsValue = strFromInt((int)m_Regen.GetHPPer180());
else if (strEquals(sField, FIELD_ADJUSTED_HP))
{
int iHP;
int iHPbyDamageType[damageCount];
GetReferenceDamageAdj(NULL, NULL, &iHP, iHPbyDamageType);
CString sResult;
for (i = 0; i < damageCount; i++)
{
if (i > 0)
sResult.Append(CONSTLIT("\t"));
sResult.Append(strFromInt(iHPbyDamageType[i]));
}
*retsValue = sResult;
}
else if (strEquals(sField, FIELD_DAMAGE_ADJ))
{
retsValue->Truncate(0);
for (i = 0; i < damageCount; i++)
{
if (i > 0)
retsValue->Append(CONSTLIT("\t"));
retsValue->Append(strFromInt(m_DamageAdj.GetAdj((DamageTypes)i)));
}
}
else if (strEquals(sField, FIELD_POWER))
*retsValue = strFromInt(m_iPowerUse * 100);
else if (strEquals(sField, FIELD_HP_BONUS))
{
CString sResult;
for (i = 0; i < damageCount; i++)
{
if (i > 0)
sResult.Append(CONSTLIT(", "));
int iBonus = m_DamageAdj.GetHPBonus((DamageTypes)i);
if (iBonus == -100)
sResult.Append(CONSTLIT("***"));
else
sResult.Append(strPatternSubst(CONSTLIT("%3d"), iBonus));
}
*retsValue = sResult;
}
else if (strEquals(sField, FIELD_BALANCE))
*retsValue = strFromInt(CalcBalance(CItemCtx(), SBalance()));
else if (strEquals(sField, FIELD_WEAPON_SUPPRESS))
{
if (m_WeaponSuppress.IsEmpty())
*retsValue = NULL_STR;
else
{
*retsValue = CONSTLIT("=(");
bool bNeedSeparator = false;
for (i = 0; i < damageCount; i++)
if (m_WeaponSuppress.InSet(i))
{
if (bNeedSeparator)
retsValue->Append(CONSTLIT(" "));
retsValue->Append(::GetDamageType((DamageTypes)i));
bNeedSeparator = true;
}
retsValue->Append(CONSTLIT(")"));
}
}
else
return false;
return true;
}
int CShieldClass::FireGetMaxHP (CInstalledDevice *pDevice, CSpaceObject *pSource, int iMaxHP) const
// FireGetMaxHP
//
// Fire GetMaxHP event
{
SEventHandlerDesc Event;
if (FindEventHandlerShieldClass(evtGetMaxHP, &Event))
{
ASSERT(pSource);
ASSERT(pDevice);
CCodeChainCtx Ctx;
Ctx.DefineContainingType(GetItemType());
Ctx.SaveAndDefineSourceVar(pSource);
Ctx.SaveAndDefineItemVar(pSource->GetItemForDevice(pDevice));
Ctx.DefineInteger(CONSTLIT("aMaxHP"), iMaxHP);
ICCItem *pResult = Ctx.Run(Event);
if (pResult->IsError())
pSource->ReportEventError(GET_MAX_HP_EVENT, pResult);
else if (!pResult->IsNil())
iMaxHP = Max(0, pResult->GetIntegerValue());
Ctx.Discard(pResult);
}
return iMaxHP;
}
void CShieldClass::FireOnShieldDamage (CItemCtx &ItemCtx, SDamageCtx &Ctx)
// FireOnShieldDamage
//
// Fire OnShieldDamage
{
SEventHandlerDesc Event;
if (FindEventHandlerShieldClass(evtOnShieldDamage, &Event))
{
// Setup arguments
CCodeChainCtx CCCtx;
CCCtx.DefineContainingType(GetItemType());
CCCtx.SaveAndDefineSourceVar(ItemCtx.GetSource());
CCCtx.SaveAndDefineItemVar(ItemCtx);
CCCtx.DefineDamageCtx(Ctx);
CCCtx.DefineInteger(CONSTLIT("aShieldHP"), Ctx.iHPLeft);
CCCtx.DefineInteger(CONSTLIT("aShieldDamageHP"), Ctx.iShieldDamage);
CCCtx.DefineInteger(CONSTLIT("aArmorDamageHP"), Ctx.iDamage - Ctx.iAbsorb);
if (Ctx.IsShotReflected())