-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombatant.cpp
662 lines (574 loc) · 20.4 KB
/
Combatant.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
#include "Combatant.h"
#include "Ability.h"
#include "DamageSimulation.h"
#include "MeleeHitResult.h"
float Combatant::getPhysicalDamageReduction() const
{
return physicalDamageReduction;
}
void Combatant::setPhysicalDamageReduction(float value)
{
physicalDamageReduction = value;
}
float Combatant::getMagicalDamageReduction() const
{
return magicalDamageReduction;
}
void Combatant::setMagicalDamageReduction(float value)
{
magicalDamageReduction = value;
}
int32_t Combatant::getCalculatedDefense()
{
return this->level*5;
}
int32_t Combatant::getCalculatedWeaponSkill()
{
return this->level*5;
}
bool Combatant::isSpellcastFinished(float timestamp)
{
if (!this->isCasting)
return true;
if (this->castingAbility != nullptr) {
float timeDiff = timestamp - this->castStartTime;
timeDiff += FLOATING_POINT_SMALL_NUMBER;
if (timeDiff >= this->castingAbility->getCastTime()) {
return true;
}
return false;
}
return true;
}
void Combatant::triggerSpellcastFinished()
{
this->isCasting = false;
this->castingAbility = nullptr;
}
int32_t Combatant::applyPhysicalDamage(Combatant *attacker, int32_t damage, MeleeHitResult mhr, float timestamp, Ability *abilitySource)
{
if (attacker != nullptr) {
int32_t calcDmg = damage * (1.0f - physicalDamageReduction);
return this->applyDamageInternal("physical", attacker, calcDmg, mhr, timestamp, abilitySource);
}
return 0;
}
int32_t Combatant::applyMagicalDamage(Combatant *attacker, int32_t damage, MeleeHitResult mhr, float timestamp, Ability *abilitySource)
{
if (attacker != nullptr) {
int32_t calcDmg = damage * (1.0f - magicalDamageReduction);
return this->applyDamageInternal("magical", attacker, calcDmg, mhr, timestamp, abilitySource);
}
return 0;
}
int32_t Combatant::applyArmorIgnoreDamage(Combatant *attacker, int32_t damage, MeleeHitResult mhr, float timestamp, Ability *abilitySource)
{
if (attacker != nullptr) {
int32_t calcDmg = damage;
return this->applyDamageInternal("armor-ignore", attacker, calcDmg, mhr, timestamp, abilitySource);
}
return 0;
}
int32_t Combatant::applyPhysicalDamage(Combatant *attacker, int32_t damage, MeleeHitResult mhr, float timestamp, Buff *buffSource)
{
if (attacker != nullptr) {
int32_t calcDmg = damage * (1.0f - physicalDamageReduction);
return this->applyDamageInternal("physical", attacker, calcDmg, mhr, timestamp, buffSource);
}
return 0;
}
int32_t Combatant::applyMagicalDamage(Combatant *attacker, int32_t damage, MeleeHitResult mhr, float timestamp, Buff *buffSource)
{
if (attacker != nullptr) {
int32_t calcDmg = damage * (1.0f - magicalDamageReduction);
return this->applyDamageInternal("magical", attacker, calcDmg, mhr, timestamp, buffSource);
}
return 0;
}
int32_t Combatant::applyArmorIgnoreDamage(Combatant *attacker, int32_t damage, MeleeHitResult mhr, float timestamp, Buff *buffSource)
{
if (attacker != nullptr) {
int32_t calcDmg = damage;
return this->applyDamageInternal("armor-ignore", attacker, calcDmg, mhr, timestamp, buffSource);
}
return 0;
}
PriorityAction *Combatant::getReplaceMeleeAction() const
{
return replaceMeleeAction;
}
void Combatant::setReplaceMeleeAction(PriorityAction *value)
{
replaceMeleeAction = value;
}
std::vector<AppliedBuff *>& Combatant::getBuffs()
{
return Buffs;
}
std::vector<AppliedBuff *>& Combatant::getDebuffs()
{
return Debuffs;
}
float Combatant::getGcdDuration() const
{
return gcdDuration;
}
void Combatant::setGcdDuration(float value)
{
gcdDuration = value;
}
std::unordered_map<Ability *, TrackedDamageInfo> &Combatant::getDamageDoneByAbility()
{
return damageDoneByAbility;
}
std::unordered_map<Buff *, TrackedDamageInfo>& Combatant::getDamageDoneByBuff()
{
return damageDoneByBuff;
}
int32_t Combatant::getLevel() const
{
return level;
}
void Combatant::setLevel(const int32_t &value)
{
level = value;
}
int32_t Combatant::getResource() const
{
return resource;
}
void Combatant::setResource(const int32_t &value)
{
resource = value;
if (this->resource > this->resourceMax) {
this->resource = this->resourceMax;
}
if (this->resource < 0) {
this->resource = 0;
}
}
int32_t Combatant::getResourceMax() const
{
return resourceMax;
}
void Combatant::setResourceMax(const int32_t &value)
{
resourceMax = value;
}
float Combatant::getLastDodgeTimestamp() const
{
return lastDodgeTimestamp;
}
void Combatant::setLastDodgeTimestamp(float value)
{
lastDodgeTimestamp = value;
}
void Combatant::addNewAbilityToMap(Combatant *attacker, Ability *toAdd, MeleeHitResult mhr)
{
attacker->damageDoneByAbility[toAdd] = {};
attacker->damageDoneByAbility[toAdd].event = toAdd->getName().append(getMeleeHitResultName(mhr));
attacker->damageDoneByAbility[toAdd].ability = toAdd;
}
void Combatant::addNewBuffToMap(Combatant *attacker, Buff *toAdd, MeleeHitResult mhr)
{
attacker->damageDoneByBuff[toAdd] = {};
attacker->damageDoneByBuff[toAdd].event = toAdd->getName().append(getMeleeHitResultName(mhr));
attacker->damageDoneByBuff[toAdd].buff = toAdd;
}
int32_t Combatant::applyDamageInternal(std::string damageTypeText, Combatant *attacker, int32_t damage, MeleeHitResult mhr, float timestamp, Ability *abilitySource)
{
if (attacker != nullptr) {
std::string abilityName = abilitySource->getName();
this->combatLogInternal(damageTypeText, attacker, damage, mhr, timestamp, abilitySource);
int32_t sourceDamage = 0;
auto&& found = attacker->damageDoneByAbility.find(abilitySource);
if (found != attacker->damageDoneByAbility.end()) {
sourceDamage = found->second.damage;
} else {
this->addNewAbilityToMap(attacker, abilitySource, mhr);
}
sourceDamage += damage;
attacker->damageDoneByAbility[abilitySource].damage = sourceDamage;
attacker->damageDoneByAbility[abilitySource].count++;
if (mhr == MeleeHitResult::CriticalHit) {
attacker->damageDoneByAbility[abilitySource].critOnlyDamage += damage;
attacker->damageDoneByAbility[abilitySource].critCount++;
} else if (mhr == MeleeHitResult::OrdinaryHit) {
attacker->damageDoneByAbility[abilitySource].nonCritDamage += damage;
attacker->damageDoneByAbility[abilitySource].nonCritCount++;
}
this->currentHp -= damage;
attacker->setDamageDone(attacker->getDamageDone() + damage);
return damage;
}
return 0;
}
int32_t Combatant::applyDamageInternal(std::string damageTypeText, Combatant *attacker, int32_t damage, MeleeHitResult mhr, float timestamp, Buff *buffSource)
{
if (attacker != nullptr) {
std::string abilityName = buffSource->getName();
this->combatLogInternal(damageTypeText, attacker, damage, mhr, timestamp, buffSource);
int32_t sourceDamage = 0;
auto&& found = attacker->damageDoneByBuff.find(buffSource);
if (found != attacker->damageDoneByBuff.end()) {
sourceDamage = found->second.damage;
} else {
this->addNewBuffToMap(attacker, buffSource, mhr);
}
sourceDamage += damage;
attacker->damageDoneByBuff[buffSource].damage = sourceDamage;
attacker->damageDoneByBuff[buffSource].count++;
if (mhr == MeleeHitResult::CriticalHit) {
attacker->damageDoneByBuff[buffSource].critOnlyDamage += damage;
attacker->damageDoneByBuff[buffSource].critCount++;
} else if (mhr == MeleeHitResult::OrdinaryHit) {
attacker->damageDoneByBuff[buffSource].nonCritDamage += damage;
attacker->damageDoneByBuff[buffSource].nonCritCount++;
}
this->currentHp -= damage;
attacker->setDamageDone(attacker->getDamageDone() + damage);
return damage;
}
return 0;
}
void Combatant::adjustUptimeForRemovedBuff(Combatant *attacker, Buff *buff, float timestamp)
{
if (attacker == nullptr || buff == nullptr) {
return;
}
auto&& found = attacker->damageDoneByBuff.find(buff);
if (found != attacker->damageDoneByBuff.end()) {
//adjust duration to only be partial duration instead of full
attacker->damageDoneByBuff[buff].buffUptime -= attacker->damageDoneByBuff[buff].mostRecentBuffDuration;
attacker->damageDoneByBuff[buff].buffUptime += (timestamp - attacker->damageDoneByBuff[buff].mostRecentBuffTimestamp);
}
}
AppliedBuff *Combatant::applyBuff(Combatant *attacker, float timestamp, Buff *buff, bool isFree)
{
if (attacker != nullptr) {
if (buff->getOnCalculateDuration() != nullptr) {
int32_t buffRank = 0;
if (buff->getParent() != nullptr) {
buffRank = buff->getParent()->getRank();
}
float duration = buff->getOnCalculateDuration()(this, buffRank);
if (duration > 0.0f) {
this->removeBuffByType(buff, timestamp);
COMBAT_LOG(timestamp, attacker, ATTACKER_FONT_COLOR<<attacker->getName()<<END_FONT<<" applied "<<(isFree ? "FREE " : "")<<"buff "<<FONT_GREEN<<buff->getName()<<END_FONT<<" to "<<RECEIVER_FONT_COLOR<<this->getName()<<END_FONT<<" with duration "<<(int32_t)duration);
AppliedBuff *AB = new AppliedBuff(buff);
AB->setAppliedTimestamp(timestamp);
AB->setLastTickedTimestamp(timestamp);
AB->setCapturedDuration(duration);
AB->setAppliedBy(attacker);
auto&& found = attacker->damageDoneByBuff.find(buff);
if (found == attacker->damageDoneByBuff.end()) {
this->addNewBuffToMap(attacker, buff, MeleeHitResult::OrdinaryHit);
}
attacker->damageDoneByBuff[buff].buffUptime += duration;
attacker->damageDoneByBuff[buff].mostRecentBuffTimestamp = timestamp;
attacker->damageDoneByBuff[buff].mostRecentBuffDuration = duration;
this->Buffs.push_back(AB);
return AB;
}
}
}
return nullptr;
}
void Combatant::applyDebuff(Combatant *attacker, float timestamp, Buff *debuff)
{
if (attacker != nullptr) {
if (debuff->getOnCalculateDuration() != nullptr) {
int32_t rank = 0;
if (debuff->getParent() != nullptr) {
rank = debuff->getParent()->getRank();
}
float duration = debuff->getOnCalculateDuration()(this, rank);
if (duration > 0.0f) {
this->removeDebuffByType(debuff, timestamp);
COMBAT_LOG(timestamp, attacker, ATTACKER_FONT_COLOR<<attacker->getName()<<END_FONT<<" applied debuff "<<FONT_RED<<debuff->getName()<<END_FONT<<" to "<<RECEIVER_FONT_COLOR<<this->getName()<<END_FONT<<" with duration "<<(int32_t)duration);
AppliedBuff *AB = new AppliedBuff(debuff);
AB->setAppliedTimestamp(timestamp);
AB->setLastTickedTimestamp(timestamp);
AB->setCapturedDuration(duration);
AB->setAppliedBy(attacker);
auto&& found = attacker->damageDoneByBuff.find(debuff);
if (found == attacker->damageDoneByBuff.end()) {
this->addNewBuffToMap(attacker, debuff, MeleeHitResult::OrdinaryHit);
}
attacker->damageDoneByBuff[debuff].buffUptime += duration;
attacker->damageDoneByBuff[debuff].mostRecentBuffTimestamp = timestamp;
attacker->damageDoneByBuff[debuff].mostRecentBuffDuration = duration;
this->Debuffs.push_back(AB);
}
}
}
}
bool Combatant::isDead()
{
return this->currentHp <= 0;
}
bool Combatant::hasBuff(Buff *Buff)
{
for (auto buff : this->Buffs) {
if (buff->getBuff() == Buff)
return true;
}
return false;
}
bool Combatant::hasDebuff(Buff *Debuff)
{
for (auto debuff : this->Debuffs) {
if (debuff->getBuff() == Debuff)
return true;
}
return false;
}
bool Combatant::removeBuffByType(Buff *toRemove, float timestamp)
{
for (auto&& it=this->Buffs.begin(); it!=this->Buffs.end(); ++it) {
if ((*it)->getBuff() == toRemove) {
this->adjustUptimeForRemovedBuff((*it)->getAppliedBy(), toRemove, timestamp);
COMBAT_LOG(timestamp, (*it)->getAppliedBy(), "Buff "<<FONT_GREEN<<toRemove->getName()<<END_FONT<<" faded from "<<this->getName());
this->Buffs.erase(it);
return true;
}
}
return false;
}
bool Combatant::removeDebuffByType(Buff *toRemove, float timestamp)
{
for (auto&& it=this->Debuffs.begin(); it!=this->Debuffs.end(); ++it) {
if ((*it)->getBuff() == toRemove) {
this->adjustUptimeForRemovedBuff((*it)->getAppliedBy(), toRemove, timestamp);
COMBAT_LOG(timestamp, (*it)->getAppliedBy(), "Debuff "<<FONT_RED<<toRemove->getName()<<END_FONT<<" faded from "<<this->getName());
this->Debuffs.erase(it);
return true;
}
}
return false;
}
bool Combatant::removeBuff(AppliedBuff *toRemove, float timestamp)
{
for (auto&& it=this->Buffs.begin(); it!=this->Buffs.end(); ++it) {
if (*it == toRemove) {
this->adjustUptimeForRemovedBuff(toRemove->getAppliedBy(), toRemove->getBuff(), timestamp);
COMBAT_LOG(timestamp, this, "Buff "<<FONT_GREEN<<toRemove->getBuff()->getName()<<END_FONT<<" faded from "<<this->getName());
this->Buffs.erase(it);
return true;
}
}
return false;
}
bool Combatant::removeDebuff(AppliedBuff *toRemove, float timestamp)
{
for (auto&& it=this->Debuffs.begin(); it!=this->Debuffs.end(); ++it) {
if (*it == toRemove) {
this->adjustUptimeForRemovedBuff(toRemove->getAppliedBy(), toRemove->getBuff(), timestamp);
COMBAT_LOG(timestamp, toRemove->getAppliedBy(), "Debuff "<<FONT_RED<<toRemove->getBuff()->getName()<<END_FONT<<" faded from "<<this->getName());
this->Debuffs.erase(it);
return true;
}
}
return false;
}
void Combatant::applyDotDamage(PlayerCharacter *PC, float timestamp)
{
for (auto buff : this->Debuffs) {
if (buff->getBuff()->getOnDotTickDamage() != nullptr) {
if (buff->isTickTimerExpired(this, timestamp)) {
buff->triggerTickTimer(timestamp);
float buffDuration = buff->getCapturedDuration();
int32_t rank = 0;
if (buff->getBuff()->getParent() != nullptr) {
rank = buff->getBuff()->getParent()->getRank();
}
int32_t damage = buff->getBuff()->getOnDotTickDamage()(PC, this, rank, buff->getTickCount(), buffDuration);
damage *= PC->calculateGlobalDamageBonus();
this->applyDamage((Combatant *)PC, damage, MeleeHitResult::OrdinaryHit, timestamp, buff->getBuff());
}
}
}
}
void Combatant::tickBuffs(PlayerCharacter *PC, float timestamp)
{
for (auto buff : this->Buffs) {
if (buff->getBuff()->getOnBuffTick() != nullptr) {
if (buff->isTickTimerExpired(this, timestamp)) {
buff->triggerTickTimer(timestamp);
float buffDuration = buff->getCapturedDuration();
int32_t resourceBefore = PC->getResource();
buff->getBuff()->getOnBuffTick()(PC, this, buff->getBuff()->getParent()->getRank(), buff->getTickCount(), buffDuration);
int32_t resourceAfter = PC->getResource();
if (resourceAfter > resourceBefore) {
int32_t resourceDiff = resourceAfter - resourceBefore;
COMBAT_LOG(timestamp, PC, ATTACKER_FONT_COLOR<<PC->getName()<<END_FONT<<" gained "<<resourceDiff<<" rage from "<<ABILITY_FONT_COLOR<<buff->getBuff()->getName()<<END_FONT);
}
}
}
}
}
int32_t Combatant::applyDamage(Combatant *attacker, int32_t damage, MeleeHitResult mhr, float timestamp, Ability *abilitySource)
{
if (abilitySource != nullptr) {
if (abilitySource->getIgnoresArmor()) {
return this->applyArmorIgnoreDamage(attacker, damage, mhr, timestamp, abilitySource);
}
else {
if (abilitySource->getAbilityDamageType() == AbilityDamageType::Physical) {
return this->applyPhysicalDamage(attacker, damage, mhr, timestamp, abilitySource);
} else if (abilitySource->getAbilityDamageType() == AbilityDamageType::Magical) {
return this->applyMagicalDamage(attacker, damage, mhr, timestamp, abilitySource);
} else {
return this->applyArmorIgnoreDamage(attacker, damage, mhr, timestamp, abilitySource);
}
}
}
}
int32_t Combatant::applyDamage(Combatant *attacker, int32_t damage, MeleeHitResult mhr, float timestamp, Buff *buffSource)
{
if (buffSource != nullptr) {
if (buffSource->getIgnoresArmor()) {
return this->applyArmorIgnoreDamage(attacker, damage, mhr, timestamp, buffSource);
}
else {
if (buffSource->getAbilityDamageType() == AbilityDamageType::Physical) {
return this->applyPhysicalDamage(attacker, damage, mhr, timestamp, buffSource);
} else if (buffSource->getAbilityDamageType() == AbilityDamageType::Magical) {
return this->applyMagicalDamage(attacker, damage, mhr, timestamp, buffSource);
} else {
return this->applyArmorIgnoreDamage(attacker, damage, mhr, timestamp, buffSource);
}
}
}
}
void Combatant::removeExpiredBuffs(float timestamp)
{
std::vector<AppliedBuff *> toRemove;
for (auto buff : this->Buffs) {
if (buff->isBuffExpired(timestamp)) {
toRemove.push_back(buff);
}
}
for (auto buff : toRemove) {
this->removeBuff(buff, timestamp);
}
}
void Combatant::removeExpiredDebuffs(float timestamp)
{
std::vector<AppliedBuff *> toRemove;
for (auto buff : this->Debuffs) {
if (buff->isBuffExpired(timestamp)) {
toRemove.push_back(buff);
}
}
for (auto buff : toRemove) {
this->removeDebuff(buff, timestamp);
}
}
void Combatant::clearAllBuffsAndDebuffsAndFreeMemory()
{
for (int i=0; i<this->Buffs.size(); ++i) {
delete this->Buffs[i];
}
this->Buffs.clear();
for (int i=0; i<this->Debuffs.size(); ++i) {
delete this->Debuffs[i];
}
this->Debuffs.clear();
}
std::string Combatant::getName() const
{
return name;
}
void Combatant::setName(const std::string &value)
{
name = value;
}
CombatLog *Combatant::getCombatLog() const
{
return combatLog;
}
int32_t Combatant::getCurrentHp() const
{
return currentHp;
}
void Combatant::setCurrentHp(const int32_t &value)
{
currentHp = value;
}
int32_t Combatant::getMaxHp() const
{
return maxHp;
}
void Combatant::setMaxHp(const int32_t &value)
{
maxHp = value;
}
void Combatant::setHp(const int32_t &value)
{
this->currentHp = value;
this->maxHp = value;
}
int32_t Combatant::getDamageDone() const
{
return damageDone;
}
void Combatant::setDamageDone(const int32_t &value)
{
damageDone = value;
}
bool Combatant::getIsGcdActive(float timestamp)
{
float timeDiff = timestamp - this->gcdStartTime;
timeDiff += FLOATING_POINT_SMALL_NUMBER;
if (timeDiff >= gcdDuration) {
this->isGcdActive = false;
return false;
}
return isGcdActive;
}
void Combatant::setIsGcdActive(bool value)
{
isGcdActive = value;
}
float Combatant::getGcdStartTime() const
{
return gcdStartTime;
}
void Combatant::setGcdStartTime(float value)
{
gcdStartTime = value;
}
bool Combatant::getIsCasting() const
{
return isCasting;
}
void Combatant::setIsCasting(bool value)
{
isCasting = value;
}
Ability *Combatant::getCastingAbility() const
{
return castingAbility;
}
void Combatant::setCastingAbility(Ability *value)
{
castingAbility = value;
}
float Combatant::getCastStartTime() const
{
return castStartTime;
}
void Combatant::setCastStartTime(float value)
{
castStartTime = value;
}
Combatant::Combatant()
{
}
Combatant::~Combatant()
{
this->clearAllBuffsAndDebuffsAndFreeMemory();
delete combatLog;
combatLog = nullptr;
}