-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDamageSimulation.cpp
385 lines (352 loc) · 15 KB
/
DamageSimulation.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
#include "DamageSimulation.h"
#include "GlobalAbilityList.h"
#include "CombatLog.h"
#include <cfloat>
std::random_device DamageSimulation::rd;
std::mt19937 DamageSimulation::randEngine(DamageSimulation::rd());
PlayerCharacter *DamageSimulation::getPC() const
{
return PC;
}
void DamageSimulation::setPC(PlayerCharacter *value)
{
PC = value;
}
void DamageSimulation::resetIterationsData()
{
this->iterationCount = 0;
this->iterationsTotalTimeLength = 0.0f;
this->iterationsDpsSummation = 0.0f;
this->iterationsMinDps = -1.0f;
this->iterationsMaxDps = -1.0f;
this->iterationsDamageDoneByBuff.clear();
this->iterationsDamageDoneByAbility.clear();
this->iterationsRecordedDps.clear();
}
void DamageSimulation::reset()
{
std::vector<Combatant *> combatants;
this->gatherAllCombatants(combatants);
for (auto combatant : combatants) {
combatant->setHp(combatant->getMaxHp());
combatant->setIsCasting(false);
combatant->setIsGcdActive(false);
combatant->setDamageDone(0);
combatant->setLastDodgeTimestamp(-1.0f);
combatant->clearAllBuffsAndDebuffsAndFreeMemory();
}
if (this->PC != nullptr) {
this->PC->initResourceValue();
this->PC->getCombatLog()->clear();
this->PC->resetAllTalentTimestamps();
this->PC->getDamageDoneByAbility().clear();
this->PC->getDamageDoneByBuff().clear();
this->PC->setLastUsedAction(nullptr);
}
this->time = 0.0f;
if (this->globalAbilityList != nullptr) {
this->globalAbilityList->resetFields();
}
}
bool DamageSimulation::almostEqual(float a, float b, float threshold)
{
float diff = a - b;
if (diff < 0.0) {
diff = -diff;
}
return diff <= threshold;
}
void DamageSimulation::simulate(PriorityActionList *priorityActions)
{
PC->setPriorityActionList(priorityActions);
priorityActions->resetAllAbilities();
for(;;) {
if (this->noEnemiesExist()) {
COMBAT_LOG(this->time, this->PC, "Combat has ended because no enemies exist!");
break;
}
if (PC->getTarget() == nullptr && this->enemyList.size() > 0) {
PC->setTarget(this->enemyList[0]);
}
std::vector<Combatant *> combatants;
this->gatherAllCombatants(combatants);
PC->tickBuffs(this->PC, this->time);
for (auto combatant : combatants) {
combatant->applyDotDamage(this->PC, this->time);
combatant->removeExpiredBuffs(this->time);
combatant->removeExpiredDebuffs(this->time);
if (combatant->getIsCasting()) {
if (combatant->isSpellcastFinished(this->time) && combatant == PC) {
COMBAT_LOG(this->time, PC, ATTACKER_FONT_COLOR<<PC->getName()<<END_FONT<<" finished casting "<<ABILITY_FONT_COLOR<<combatant->getCastingAbility()->getName()<<END_FONT);
PriorityAction PA(combatant->getCastingAbility());
PA.execute(PC, this->enemyList, this->time);
if (combatant->getCastingAbility()->getCastedAbilityResetsAutoAttack())
PC->enableAndResetAutoAttack(this->time);
combatant->triggerSpellcastFinished();
}
}
}
if (Talent *AM = PC->getTalent("Anger Management")) {
if (AM->getRank() > 0) {
float timeDiff = this->time - AM->getLastUsedTimestamp();
timeDiff += FLOATING_POINT_SMALL_NUMBER;
if(timeDiff >= 3.0f) {
AM->setLastUsedTimestamp(this->time);
PC->setResource(PC->getResource() + 1);
COMBAT_LOG(this->time, PC, ATTACKER_FONT_COLOR<<PC->getName()<<END_FONT<<" gained 1 rage from "<<ABILITY_FONT_COLOR<<"Anger Management"<<END_FONT);
}
}
}
if (!this->allEnemiesAreDead()) {
int32_t actionsTaken = 0;
while (actionsTaken < this->maxActionsPerTimeStep) {
++actionsTaken;
PriorityAction *PA = priorityActions->getNextAction(this->PC, this->time);
if (PA != nullptr) {
if (PA->getAbility() != nullptr && PA->getAbility()->getCastTime() > 0.0f) {
COMBAT_LOG(this->time, PC, ATTACKER_FONT_COLOR<<PC->getName()<<END_FONT<<" begins casting "<<ABILITY_FONT_COLOR<<PA->getAbility()->getName()<<END_FONT);
PC->setCastingAbility(PA->getAbility());
PC->setCastStartTime(this->time);
PC->setIsCasting(true);
if (PA->getAbility()->getCastedAbilityResetsAutoAttack())
PC->disableAutoAttack();
} else if (PA->getAbility() != nullptr && PA->getAbility()->getReplacesNextMelee()) {
PC->setReplaceMeleeAction(PA);
} else {
if (PA->isMainhandAutoAttack() && PC->getReplaceMeleeAction() != nullptr) {
PC->getReplaceMeleeAction()->execute(this->PC, this->enemyList, this->time);
PC->setReplaceMeleeAction(nullptr);
PA->getAbility()->triggerCooldown(PC, this->time, PA->getIgnoreGcd());
} else {
PA->execute(this->PC, this->enemyList, this->time);
}
}
if (this->allEnemiesAreDead()) {
break;
}
}
else {
break;
}
}
}
if (this->allEnemiesAreDead()) {
COMBAT_LOG(this->time, this->PC, "Combat has ended because all enemies have perished");
break;
}
this->time += this->timeStep;
if (this->maxCombatTime > 0.0f && this->time >= this->maxCombatTime) {
COMBAT_LOG(this->time, this->PC, "Combat has ended because time has exceeded the max time of "<<this->maxCombatTime);
break;
}
}
if (globalAbilityList != nullptr) {
if (globalAbilityList->PrintDps != nullptr) {
PriorityAction PA(globalAbilityList->PrintDps);
PA.execute(this->PC, this->enemyList, this->time);
}
}
this->iterationsTotalTimeLength += this->time;
float dps = PC->getDamageDone()/this->time;
iterationsRecordedDps.push_back(dps);
if (iterationsMinDps == -1.0f) {
iterationsMinDps = dps;
}
if (iterationsMaxDps == -1.0f) {
iterationsMaxDps = dps;
}
if (dps < iterationsMinDps) {
iterationsMinDps = dps;
}
if (dps > iterationsMaxDps) {
iterationsMaxDps = dps;
}
this->iterationsDpsSummation += dps;
for (auto& it : PC->getDamageDoneByAbility()) {
auto&& found = this->iterationsDamageDoneByAbility.find(it.first);
int32_t damage = 0;
if (found != this->iterationsDamageDoneByAbility.end()) {
damage = found->second.damage;
} else {
this->iterationsDamageDoneByAbility[it.first] = {};
this->iterationsDamageDoneByAbility[it.first].ability = it.first;
}
damage += it.second.damage;
this->iterationsDamageDoneByAbility[it.first].damage = damage;
this->iterationsDamageDoneByAbility[it.first].count += it.second.count;
this->iterationsDamageDoneByAbility[it.first].nonCritCount += it.second.nonCritCount;
this->iterationsDamageDoneByAbility[it.first].nonCritDamage += it.second.nonCritDamage;
this->iterationsDamageDoneByAbility[it.first].critCount += it.second.critCount;
this->iterationsDamageDoneByAbility[it.first].critOnlyDamage += it.second.critOnlyDamage;
}
for (auto& it : PC->getDamageDoneByBuff()) {
if (PC->hasBuff(it.second.buff) || PC->getTarget()->hasDebuff(it.second.buff)) {
//buffUptime is wrong if a buff is cast just prior to battle ending. Subtract total buff duration and only add active portion.
it.second.buffUptime -= it.second.mostRecentBuffDuration;
it.second.buffUptime += (this->time - it.second.mostRecentBuffTimestamp);
}
auto&& found = this->iterationsDamageDoneByBuff.find(it.first);
int32_t damage = 0;
if (found != this->iterationsDamageDoneByBuff.end()) {
damage = found->second.damage;
} else {
this->iterationsDamageDoneByBuff[it.first] = {};
this->iterationsDamageDoneByBuff[it.first].buff = it.first;
}
damage += it.second.damage;
this->iterationsDamageDoneByBuff[it.first].damage = damage;
this->iterationsDamageDoneByBuff[it.first].count += it.second.count;
this->iterationsDamageDoneByBuff[it.first].nonCritCount += it.second.nonCritCount;
this->iterationsDamageDoneByBuff[it.first].nonCritDamage += it.second.nonCritDamage;
this->iterationsDamageDoneByBuff[it.first].critCount += it.second.critCount;
this->iterationsDamageDoneByBuff[it.first].critOnlyDamage += it.second.critOnlyDamage;
this->iterationsDamageDoneByBuff[it.first].buffUptime += it.second.buffUptime;
}
++iterationCount;
}
struct DpsElement {
std::string name;
int64_t damage = 0;
int32_t count = 0;
float buffUptime = 0.0f;
int64_t nonCritDamage = 0;
int32_t nonCritCount = 0;
int64_t critOnlyDamage = 0;
int32_t critCount = 0;
DpsElement(std::string name, int32_t damage, int32_t count,
int64_t nonCritDamage,
int32_t nonCritCount,
int64_t critOnlyDamage,
int32_t critCount,
float buffUptime) {
this->name = name;
this->damage = damage;
this->count = count;
this->nonCritDamage = nonCritDamage;
this->nonCritCount = nonCritCount;
this->critOnlyDamage = critOnlyDamage;
this->critCount = critCount;
this->buffUptime = buffUptime;
}
};
struct DpsElementSort {
bool operator()(const DpsElement& a, const DpsElement& b) {
return a.damage > b.damage;
}
};
void DamageSimulation::printIterationSummary(std::ostream &stream)
{
std::sort(this->iterationsRecordedDps.begin(), this->iterationsRecordedDps.end());
float medianDps = this->iterationsRecordedDps[this->iterationsRecordedDps.size()/2];
stream<<"Damage Summary<br>";
stream<<"Level "<<PC->getLevel()<<" "<<PC->getPlayerRace()<<" "<<PC->getPlayerClass().getClassName()<<": "<<PC->getName()<<"<br>";
stream<<"Iterations: "<<this->iterationCount<<"<br>";
stream<<"Median DPS: "<<(medianDps)<<"<br>";
stream<<"Average DPS: "<<(this->iterationsDpsSummation/this->iterationCount)<<"<br>";
stream<<"Min DPS: "<<this->iterationsMinDps<<"<br>";
stream<<"Max DPS: "<<this->iterationsMaxDps<<"<br>";
stream<<"Combat Duration: "<<(this->iterationsTotalTimeLength/this->iterationCount)<<"<br>";
stream<<"<br>";
stream<<"Attack Table:";
stream<<"<br>";
MeleeAttackTable mat;
PC->getPriorityActionList()->getMainHandAutoAttackAction()->getAbility()->generateMeleeAttackTable(PC, PC->getTarget(), mat);
PC->getPriorityActionList()->getMainHandAutoAttackAction()->getAbility()->printMeleeAttackTable(mat, stream);
stream<<"<br>";
int64_t damageTotal = 0;
std::vector<DpsElement> dpsElements;
for (auto& it : this->iterationsDamageDoneByAbility) {
damageTotal += it.second.damage;
dpsElements.emplace_back(it.first->getName(), it.second.damage, it.second.count, it.second.nonCritDamage, it.second.nonCritCount, it.second.critOnlyDamage, it.second.critCount, -1.0f);
}
for (auto& it : this->iterationsDamageDoneByBuff) {
damageTotal += it.second.damage;
dpsElements.emplace_back(it.first->getName(), it.second.damage, it.second.count, it.second.nonCritDamage, it.second.nonCritCount, it.second.critOnlyDamage, it.second.critCount, it.second.buffUptime);
}
std::sort(dpsElements.begin(), dpsElements.end(), DpsElementSort());
for (int i=0; i<dpsElements.size(); ++i) {
float dmgPct = (100.0f*(float)dpsElements[i].damage/damageTotal);
int32_t nonCritDivisor = dpsElements[i].nonCritCount;
int32_t critOnlyDivisor = dpsElements[i].critCount;
if (nonCritDivisor == 0)
nonCritDivisor = 1;
if (critOnlyDivisor == 0)
critOnlyDivisor = 1;
std::stringstream buffStream;
std::stringstream damageStream;
std::string nameColor = "<font color=\"maroon\">";
if (dpsElements[i].damage > 0) {
damageStream<<(dpsElements[i].damage/this->iterationCount)<<" (<font color=\"green\">"<<std::fixed<<std::setprecision(2)<<dmgPct<<"</font>%), average count="<<((float)dpsElements[i].count/this->iterationCount)<<", average hit="<<(dpsElements[i].nonCritDamage/nonCritDivisor)<<", average crit="<<(dpsElements[i].critOnlyDamage/critOnlyDivisor)<<", crit chance="<<(100*(float)dpsElements[i].critCount/dpsElements[i].count)<<"%";
}
if (dpsElements[i].buffUptime > 0.0f) {
float pct = dpsElements[i].buffUptime / this->iterationsTotalTimeLength;
//float pct = dpsElements[i].buffUptime;
std::string commaText = "";
if (dpsElements[i].damage > 0)
commaText = ", ";
buffStream<<commaText<<"<font color=\"blue\">buff uptime</font>="<<(int32_t)(pct*100)<<"%";
nameColor = "<font color=\"brown\">";
}
stream<<nameColor<<dpsElements[i].name<<"</font>"<<": "<<damageStream.str()<<buffStream.str()<<"<br>";
}
}
void DamageSimulation::gatherAllCombatants(std::vector<Combatant *> &combatants) {
combatants.push_back(this->PC);
for (auto enemy : this->enemyList) {
combatants.push_back(enemy);
}
}
float DamageSimulation::getTimeStep() const
{
return timeStep;
}
void DamageSimulation::setTimeStep(float value)
{
timeStep = value;
}
bool DamageSimulation::noEnemiesExist()
{
return this->enemyList.empty();
}
bool DamageSimulation::allEnemiesAreDead()
{
for (int i=0; i<this->enemyList.size(); ++i) {
if (!this->enemyList[i]->isDead()) {
return false;
}
}
return true;
}
std::vector<Enemy *>& DamageSimulation::getEnemyList()
{
return enemyList;
}
GlobalAbilityList *DamageSimulation::getGlobalAbilityList() const
{
return globalAbilityList;
}
void DamageSimulation::setGlobalAbilityList(GlobalAbilityList *value)
{
globalAbilityList = value;
}
std::mt19937& DamageSimulation::getRandEngine()
{
return randEngine;
}
float DamageSimulation::randomFloatBetween(float a, float b)
{
std::uniform_real_distribution<> dist(a, std::nextafter(b, FLT_MAX));
return dist(DamageSimulation::getRandEngine());
}
int32_t DamageSimulation::randomIntBetween(int32_t a, int32_t b)
{
std::uniform_int_distribution<> dist(a, b);
return dist(DamageSimulation::getRandEngine());
}
float DamageSimulation::getTime() const
{
return time;
}
DamageSimulation::DamageSimulation()
{
}