-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCItemEnhancementStack.cpp
780 lines (566 loc) · 15 KB
/
CItemEnhancementStack.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
// CItemEnhancementStack.cpp
//
// CItemEnhancementStack class
// Copyright (c) 2013 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
void CItemEnhancementStack::AccumulateAttributes (CItemCtx &Ctx, TArray<SDisplayAttribute> *retList) const
// AccumulateAttributes
//
// Add enhancement attributes as appropriate.
{
int i;
CDeviceClass *pClass = Ctx.GetDeviceClass();
// Add all damage resistance enhancements
bool bNoDamageAdj = false;
int iAdj;
if ((iAdj = GetResistEnergyAdj()) != 100)
{
retList->Insert(SDisplayAttribute((iAdj < 100 ? attribPositive : attribNegative), (iAdj < 100 ? CONSTLIT("+energy resist") : CONSTLIT("-energy vulnerable")), true));
bNoDamageAdj = true;
}
if ((iAdj = GetResistMatterAdj()) != 100)
{
retList->Insert(SDisplayAttribute((iAdj < 100 ? attribPositive : attribNegative), (iAdj < 100 ? CONSTLIT("+matter resist") : CONSTLIT("-matter vulnerable")), true));
bNoDamageAdj = true;
}
// Per damage-type bonuses
if (!bNoDamageAdj)
{
for (i = 0; i < damageCount; i++)
{
if ((iAdj = GetResistDamageAdj((DamageTypes)i)) != 100)
retList->Insert(SDisplayAttribute((iAdj < 100 ? attribPositive : attribNegative), GetDamageShortName((DamageTypes)i), true));
}
}
// Add some binary enhancements
for (i = 0; i < m_Stack.GetCount(); i++)
{
bool bDisadvantage = m_Stack[i].IsDisadvantage();
EDisplayAttributeTypes iDisplayType = (bDisadvantage ? attribNegative : attribPositive);
switch (m_Stack[i].GetType())
{
case etRegenerate:
case etHealerRegenerate:
case etReflect:
case etRepairOnHit:
case etSpecialDamage:
case etConferSpecialDamage:
case etImmunityIonEffects:
case etPhotoRegenerate:
case etPhotoRecharge:
m_Stack[i].AccumulateAttributes(Ctx, retList);
break;
}
}
// Power adjustment
int iPowerAdj = GetPowerAdj();
if (iPowerAdj > 100)
retList->Insert(SDisplayAttribute(attribNegative, CONSTLIT("-drain"), true));
else if (iPowerAdj < 100)
retList->Insert(SDisplayAttribute(attribPositive, CONSTLIT("+efficient"), true));
// Enhancements to fire rate
int iFireAdj = GetActivateDelayAdj();
if (iFireAdj > 100)
retList->Insert(SDisplayAttribute(attribNegative, CONSTLIT("-slow"), true));
else if (iFireAdj < 100)
retList->Insert(SDisplayAttribute(attribPositive, CONSTLIT("+fast"), true));
// Add bonus
int iBonus = GetBonus();
if (iBonus < 0)
retList->Insert(SDisplayAttribute(attribNegative, strPatternSubst(CONSTLIT("%d%%"), iBonus), true));
else if (iBonus > 0)
retList->Insert(SDisplayAttribute(attribPositive, strPatternSubst(CONSTLIT("+%d%%"), iBonus), true));
// Add tracking
if (IsTracking() && pClass && !pClass->IsTrackingWeapon(CItemCtx()))
retList->Insert(SDisplayAttribute(attribPositive, CONSTLIT("+tracking"), true));
}
int CItemEnhancementStack::ApplyDamageAdj (const DamageDesc &Damage, int iDamageAdj) const
// ApplyDamageAdj
//
// Adds the stack's damage adj to the given one.
{
int i;
Metric rValue = (Metric)iDamageAdj;
// Apply enhancements (including on the item itself)
for (i = 0; i < m_Stack.GetCount(); i++)
{
int iAdj = m_Stack[i].GetDamageAdj(Damage);
if (iAdj != 100)
rValue = iAdj * rValue / 100.0;
}
if (rValue > CDamageAdjDesc::MAX_DAMAGE_ADJ)
return CDamageAdjDesc::MAX_DAMAGE_ADJ;
return (int)(rValue + 0.5);
}
void CItemEnhancementStack::ApplySpecialDamage (DamageDesc *pDamage) const
// ApplySpecialDamage
//
// Adds special damage types to the given damage descriptor.
{
int i;
for (i = 0; i < m_Stack.GetCount(); i++)
{
int iLevel;
SpecialDamageTypes iSpecial;
if ((iSpecial = m_Stack[i].GetSpecialDamage(&iLevel)) != specialNone)
{
if (iLevel > pDamage->GetSpecialDamage(iSpecial))
pDamage->SetSpecialDamage(iSpecial, iLevel);
}
}
}
int CItemEnhancementStack::CalcActivateDelay (CItemCtx &DeviceCtx) const
// CalcActivateDelay
//
// Calculates the activation delay (in ticks) for the given device if we apply
// this enhancement stack.
{
int i;
CDeviceClass *pClass = DeviceCtx.GetDeviceClass();
if (pClass == NULL)
pClass = DeviceCtx.GetVariantDevice();
if (pClass == NULL)
return 0;
// Get the raw activation delay. NOTE: This DOES NOT include
// any enhancements on the item.
Metric rDelay = pClass->GetActivateDelay(DeviceCtx);
// Apply enhancements (including on the item itself)
for (i = 0; i < m_Stack.GetCount(); i++)
{
int iMin, iMax;
int iAdj = m_Stack[i].GetActivateRateAdj(&iMin, &iMax);
if (iAdj != 100)
{
rDelay = iAdj * rDelay / 100.0;
if (rDelay < (Metric)iMin)
rDelay = (Metric)iMin;
else if (iMax > 0 && rDelay > (Metric)iMax)
rDelay = (Metric)iMax;
}
}
return (int)(rDelay + 0.5);
}
Metric CItemEnhancementStack::CalcRegen180 (CItemCtx &ItemCtx, int iTicksPerUpdate) const
// CalcRegen180
//
// Calculates the regeneration in 180 ticks.
{
Metric rRegen = 0.0;
for (int i = 0; i < m_Stack.GetCount(); i++)
rRegen += m_Stack[i].GetRegen180(ItemCtx, iTicksPerUpdate);
return rRegen;
}
void CItemEnhancementStack::CalcCache (void) const
// CalcCache
//
// Computes accumulated values
{
int i;
m_iBonus = 0;
m_Damage = DamageDesc(damageGeneric, DiceRange());
for (i = 0; i < m_Stack.GetCount(); i++)
{
m_iBonus += m_Stack[i].GetHPBonus();
int iLevel;
SpecialDamageTypes iSpecial;
if ((iSpecial = m_Stack[i].GetSpecialDamage(&iLevel)) != specialNone)
m_Damage.SetSpecialDamage(iSpecial, iLevel);
}
// Done
m_bCacheValid = true;
}
void CItemEnhancementStack::Delete (void)
// Delete
//
// Delete reference
{
if (--m_dwRefCount <= 0)
delete this;
}
int CItemEnhancementStack::GetAbsorbAdj (const DamageDesc &Damage) const
// GetAbsorbAdj
//
// Compute the absorb adjustment (for shields)
{
int i;
Metric rValue = 100.0;
// Apply enhancements (including on the item itself)
for (i = 0; i < m_Stack.GetCount(); i++)
{
int iAdj = m_Stack[i].GetAbsorbAdj(Damage);
if (iAdj != 100)
rValue = iAdj * rValue / 100.0;
}
return (int)(rValue + 0.5);
}
int CItemEnhancementStack::GetActivateDelayAdj (void) const
// GetActivateDelayAdj
//
// Computes the activation delay
{
int i;
Metric rDelay = 100.0;
// Apply enhancements (including on the item itself)
for (i = 0; i < m_Stack.GetCount(); i++)
{
int iMin, iMax;
int iAdj = m_Stack[i].GetActivateRateAdj(&iMin, &iMax);
if (iAdj != 100)
rDelay = iAdj * rDelay / 100.0;
}
return (int)(rDelay + 0.5);
}
int CItemEnhancementStack::GetBonus (void) const
// GetBonus
//
// Returns the accumulated bonus for all enhancements on the stack.
{
if (!m_bCacheValid)
CalcCache();
return m_iBonus;
}
const DamageDesc &CItemEnhancementStack::GetDamage (void) const
// GetDamage
//
// Returns special damage.
{
if (!m_bCacheValid)
CalcCache();
return m_Damage;
}
int CItemEnhancementStack::GetDamageAdj (const DamageDesc &Damage) const
// GetDamageAdj
//
// Compute the damage adjustment.
//
// NOTE: We are guaranteed to return values from 0 to MAX_DAMAGE_ADJ (100,000)
{
int i;
Metric rValue = 100.0;
// Apply enhancements (including on the item itself)
for (i = 0; i < m_Stack.GetCount(); i++)
{
int iAdj = m_Stack[i].GetDamageAdj(Damage);
if (iAdj != 100)
rValue = iAdj * rValue / 100.0;
}
if (rValue > CDamageAdjDesc::MAX_DAMAGE_ADJ)
return CDamageAdjDesc::MAX_DAMAGE_ADJ;
return (int)(rValue + 0.5);
}
int CItemEnhancementStack::GetFireArc (void) const
// GetFireArc
//
// Returns the imposed fire arc. -1 = stuck.
{
int i;
int iBestFireArc = 0;
for (i = 0; i < m_Stack.GetCount(); i++)
{
int iFireArc = m_Stack[i].GetFireArc();
// If any element is a disadvantage (stuck turret) then we're stuck.
if (iFireArc == -1)
return -1;
// Otherwise, take the best fire arc
iBestFireArc = Max(iBestFireArc, iFireArc);
}
// Done
return iBestFireArc;
}
int CItemEnhancementStack::GetManeuverRate (void) const
// GetManeuverRate
//
// Returns the tracking maneuver rate.
{
int i;
int iRate = 0;
for (i = 0; i < m_Stack.GetCount(); i++)
iRate = Max(iRate, m_Stack[i].GetManeuverRate());
return iRate;
}
int CItemEnhancementStack::GetPowerAdj (void) const
// GetPowerAdj
//
// Returns the power consumption adjustment
{
int i;
Metric rValue = 100.0;
for (i = 0; i < m_Stack.GetCount(); i++)
{
int iAdj = m_Stack[i].GetPowerAdj();
if (iAdj != 100)
rValue = iAdj * rValue / 100.0;
}
return (int)(rValue + 0.5);
}
int CItemEnhancementStack::GetResistDamageAdj (DamageTypes iDamage) const
// GetResistDamageAdj
//
// Gets the damage adjustment for the given type
{
int i;
DamageDesc Damage(iDamage, DiceRange(0, 0, 10));
Metric rValue = 100.0;
for (i = 0; i < m_Stack.GetCount(); i++)
{
int iAdj = m_Stack[i].GetDamageAdj(Damage);
if (iAdj != 100)
rValue = iAdj * rValue / 100.0;
}
return (int)(rValue + 0.5);
}
int CItemEnhancementStack::GetResistEnergyAdj (void) const
// GetResistEnergyAdj
//
// Gets the damage adjustment for energy. We only return an adjustment if there
// are no other damage adjustments (otherwise we return 100).
{
int i;
Metric rValue = 100.0;
for (i = 0; i < m_Stack.GetCount(); i++)
{
if (m_Stack[i].HasCustomDamageAdj())
return 100;
int iAdj = m_Stack[i].GetResistEnergyAdj();
if (iAdj != 100)
rValue = iAdj * rValue / 100.0;
}
return (int)(rValue + 0.5);
}
int CItemEnhancementStack::GetResistMatterAdj (void) const
// GetResistMatterAdj
//
// Gets the damage adjustment for matter. We only return an adjustment if there
// are no other damage adjustments (otherwise we return 100).
{
int i;
Metric rValue = 100.0;
for (i = 0; i < m_Stack.GetCount(); i++)
{
if (m_Stack[i].HasCustomDamageAdj())
return 100;
int iAdj = m_Stack[i].GetResistMatterAdj();
if (iAdj != 100)
rValue = iAdj * rValue / 100.0;
}
return (int)(rValue + 0.5);
}
void CItemEnhancementStack::Insert (const CItemEnhancement &Mods)
// Insert
//
// Appends the given enhancement to the stack.
{
if (!Mods.IsEmpty())
m_Stack.Insert(Mods);
}
void CItemEnhancementStack::InsertActivateAdj (int iAdj, int iMin, int iMax)
// InsertActivateAdj
//
// Appends an activate adjustment enhancement.
{
m_Stack.InsertEmpty();
m_Stack[m_Stack.GetCount() - 1].SetModSpeed(iAdj, iMin, iMax);
}
void CItemEnhancementStack::InsertHPBonus (int iBonus)
// InsertHPBonus
//
// Appends an HP bonus enhancement.
{
m_Stack.InsertEmpty();
m_Stack[m_Stack.GetCount() - 1].SetModBonus(iBonus);
}
bool CItemEnhancementStack::IsBlindingImmune (void) const
// IsBlindingImmune
//
// Returns true if we're immune to blinding
{
int i;
for (i = 0; i < m_Stack.GetCount(); i++)
if (m_Stack[i].IsBlindingImmune())
return true;
return false;
}
bool CItemEnhancementStack::IsDecaying (void) const
// IsDecaying
//
// Returns true if we're decaying
{
int i;
for (i = 0; i < m_Stack.GetCount(); i++)
if (m_Stack[i].IsDecaying())
return true;
return false;
}
bool CItemEnhancementStack::IsDeviceDamageImmune (void) const
// IsDeviceDamageImmune
//
// Returns true if we're immune to device damage
{
int i;
for (i = 0; i < m_Stack.GetCount(); i++)
if (m_Stack[i].IsDeviceDamageImmune())
return true;
return false;
}
bool CItemEnhancementStack::IsDisintegrationImmune (void) const
// IsDisintegrationImmune
//
// Returns true if we're immune to disintegration
{
int i;
for (i = 0; i < m_Stack.GetCount(); i++)
if (m_Stack[i].IsDisintegrationImmune())
return true;
return false;
}
bool CItemEnhancementStack::IsEMPImmune (void) const
// IsEMPImmune
//
// Returns true if we're immune to EMP
{
int i;
for (i = 0; i < m_Stack.GetCount(); i++)
if (m_Stack[i].IsEMPImmune())
return true;
return false;
}
bool CItemEnhancementStack::IsPhotoRecharging (void) const
// IsPhotoRecharging
//
// Returns true if we're photo recharging
{
int i;
for (i = 0; i < m_Stack.GetCount(); i++)
if (m_Stack[i].IsPhotoRecharge())
return true;
return false;
}
bool CItemEnhancementStack::IsPhotoRegenerating (void) const
// IsPhotoRegenerating
//
// Returns true if we're photo regenerating
{
int i;
for (i = 0; i < m_Stack.GetCount(); i++)
if (m_Stack[i].IsPhotoRegenerating())
return true;
return false;
}
bool CItemEnhancementStack::IsRadiationImmune (void) const
// IsRadiationImmune
//
// Returns true if we're immune to radiation
{
int i;
for (i = 0; i < m_Stack.GetCount(); i++)
if (m_Stack[i].IsRadiationImmune())
return true;
return false;
}
bool CItemEnhancementStack::IsShatterImmune (void) const
// IsShatterImmune
//
// Returns true if we're immune to shatter
{
int i;
for (i = 0; i < m_Stack.GetCount(); i++)
if (m_Stack[i].IsShatterImmune())
return true;
return false;
}
bool CItemEnhancementStack::IsShieldInterfering (void) const
// IsShieldInterfering
//
// Returns true if we're shield interfering
{
int i;
for (i = 0; i < m_Stack.GetCount(); i++)
if (m_Stack[i].IsShieldInterfering())
return true;
return false;
}
TSharedPtr<CItemEnhancementStack> CItemEnhancementStack::ReadFromStream (SLoadCtx &Ctx)
// ReadFromStream
//
// Reads from stream
{
int i;
DWORD dwLoad;
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
if (dwLoad == 0)
return NULL;
TSharedPtr<CItemEnhancementStack> pStack(new CItemEnhancementStack);
pStack->m_Stack.InsertEmpty(dwLoad);
for (i = 0; i < pStack->m_Stack.GetCount(); i++)
pStack->m_Stack[i].ReadFromStream(Ctx);
return pStack;
}
bool CItemEnhancementStack::ReflectsDamage (DamageTypes iDamage, int *retiChance) const
// ReflectsDamage
//
// Returns TRUE if we reflect the given damage type
{
int i;
if (retiChance)
*retiChance = 0;
for (i = 0; i < m_Stack.GetCount(); i++)
{
int iChance = m_Stack[i].GetReflectChance(iDamage);
if (iChance > 0)
{
if (retiChance)
*retiChance = Max(iChance, *retiChance);
else
return true;
}
}
return (retiChance ? (*retiChance > 0) : false);
}
bool CItemEnhancementStack::RepairOnDamage (DamageTypes iDamage) const
// RepairOnDamage
//
// Returns TRUE if we repair when hit with damage
{
int i;
for (i = 0; i < m_Stack.GetCount(); i++)
if (m_Stack[i].IsRepairOnDamage() && m_Stack[i].GetDamageType() == iDamage)
return true;
return false;
}
bool CItemEnhancementStack::UpdateArmorRegen (CItemCtx &ArmorCtx, SUpdateCtx &UpdateCtx, int iTick) const
// UpdateArmorRegen
//
// Regenerates the armor if we have regen enhancements. Returns TRUE if we
// regenerated anything.
{
int i;
bool bRegenerated = false;
for (i = 0; i < m_Stack.GetCount(); i++)
{
if (m_Stack[i].UpdateArmorRegen(ArmorCtx, UpdateCtx, iTick))
bRegenerated = true;
}
return bRegenerated;
}
void CItemEnhancementStack::WriteToStream (CItemEnhancementStack *pStack, IWriteStream *pStream)
// WriteToStream
//
// Writes to stream
//
// DWORD Number of items in stack
// CItemEnhancement
{
int i;
if (pStack == NULL)
{
DWORD dwSave = 0;
pStream->Write((char *)&dwSave, sizeof(DWORD));
}
else
{
DWORD dwSave = pStack->m_Stack.GetCount();
pStream->Write((char *)&dwSave, sizeof(DWORD));
for (i = 0; i < pStack->m_Stack.GetCount(); i++)
pStack->m_Stack[i].WriteToStream(pStream);
}
}