-
Notifications
You must be signed in to change notification settings - Fork 719
/
Copy pathattack.cpp
563 lines (454 loc) · 15 KB
/
attack.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
// ==========================================================================
// Dedmonwakeen's Raid DPS/TPS Simulator.
// Send questions to [email protected]
// ==========================================================================
#include "attack.hpp"
#include "action_state.hpp"
#include "dbc/spell_data.hpp"
#include "player/player.hpp"
#include "player/stats.hpp"
#include "sim/cooldown.hpp"
#include "sim/event.hpp"
#include "sim/sim.hpp"
#include "util/rng.hpp"
// ==========================================================================
// Attack
// ==========================================================================
attack_t::attack_t( util::string_view n, player_t* p ) : attack_t( n, p, spell_data_t::nil() ) {}
attack_t::attack_t( util::string_view n, player_t* p, const spell_data_t* s )
: action_t( ACTION_ATTACK, n, p, s ), base_attack_expertise( 0 ), attack_table()
{
crit_bonus = 1.0;
special = true; // Make sure to set this to false with autoattacks.
weapon_power_mod = 1.0 / WEAPON_POWER_COEFFICIENT;
min_gcd = p->min_gcd;
// dodge/parry/block are true by default for attacks unless corresponding flag is set
if ( data().ok() && !data().flags( spell_attribute::SX_NO_D_P_B ) )
{
may_dodge = !data().flags( spell_attribute::SX_NO_DODGE );
may_parry = !data().flags( spell_attribute::SX_NO_PARRY );
may_block = !data().flags( spell_attribute::SX_NO_BLOCK );
}
}
void attack_t::execute()
{
action_t::execute();
}
double attack_t::execute_time_pct_multiplier() const
{
return action_t::execute_time_pct_multiplier() * player->cache.auto_attack_speed();
}
result_amount_type attack_t::amount_type( const action_state_t*, bool periodic ) const
{
if ( periodic || treat_as_periodic )
return result_amount_type::DMG_OVER_TIME;
else
return result_amount_type::DMG_DIRECT;
}
result_amount_type attack_t::report_amount_type( const action_state_t* state ) const
{
auto result_type = state->result_type;
if ( result_type == result_amount_type::DMG_DIRECT )
{
// Direct ticks are direct damage, that are recorded as ticks
if ( direct_tick )
{
result_type = result_amount_type::DMG_OVER_TIME;
}
else
{
// With direct damage, we need to check if this action is a tick action of
// someone. If so, then the damage should be recorded as periodic.
if ( !stats->action_list.empty() && stats->action_list.front()->tick_action == this )
{
result_type = result_amount_type::DMG_OVER_TIME;
}
}
}
return result_type;
}
double attack_t::miss_chance( double hit, player_t* t ) const
{
if ( t->is_enemy() && sim->auto_attacks_always_land && !special )
{
return 0.0;
}
// cache.miss() contains the target's miss chance (3.0 base in almost all cases)
double miss = t->cache.miss();
// add or subtract 1.5% per level difference
miss += ( t->level() - player->level() ) * 0.015;
// asymmetric hit penalty for npcs attacking higher-level players
if ( !t->is_enemy() )
miss += std::max( t->level() - player->level() - 3, 0 ) * 0.015;
// subtract the player's hit chance
miss -= hit;
return miss;
}
double attack_t::dodge_chance( double expertise, player_t* t ) const
{
if ( t->is_enemy() && sim->auto_attacks_always_land && !special )
{
return 0.0;
}
// cache.dodge() contains the target's dodge chance (3.0 base, plus spec bonuses and rating)
double dodge = t->cache.dodge();
// WoD mechanics are unchanged from MoP add or subtract 1.5% per level difference
dodge += ( t->level() - player->level() ) * 0.015;
// subtract the player's expertise chance
dodge -= expertise;
return dodge;
}
double attack_t::block_chance( action_state_t* s ) const
{
if ( s->target->is_enemy() && sim->auto_attacks_always_land && !special )
{
return 0.0;
}
// cache.block() contains the target's block chance (3.0 base for bosses, more for shield tanks)
double block = s->target->cache.block();
// add or subtract 1.5% per level difference -- Level difference does not seem to matter anymore.
// block += ( s->target->level() - player->level() ) * 0.015;
return block;
}
double attack_t::crit_block_chance( action_state_t* s ) const
{
// This function is probably unnecessary, as we could just query
// cache.crit_block() directly.
// I'm leaving it for consistency with *_chance() and in case future changes
// modify crit block mechanics
// Crit Block does not suffer from level-based suppression, return cached
// value directly
return s->target->cache.crit_block();
}
double attack_t::bonus_da( const action_state_t* s ) const
{
double da = action_t::bonus_da( s );
if ( !special )
{
da += player->auto_attack_modifier;
da += player->auto_attack_base_modifier;
}
return da;
}
double attack_t::action_multiplier() const
{
double mul = action_t::action_multiplier();
if ( !special )
{
mul *= player->auto_attack_multiplier;
}
return mul;
}
double attack_t::composite_target_multiplier( player_t* t ) const
{
double mul = action_t::composite_target_multiplier( t );
mul *= composite_target_damage_vulnerability( t );
return mul;
}
double attack_t::composite_hit() const
{
return action_t::composite_hit() + player->cache.attack_hit();
}
double attack_t::composite_crit_chance() const
{
return action_t::composite_crit_chance() + player->cache.attack_crit_chance();
}
double attack_t::composite_crit_chance_multiplier() const
{
return action_t::composite_crit_chance_multiplier() * player->composite_melee_crit_chance_multiplier();
}
double attack_t::composite_haste() const
{
return action_t::composite_haste() * player->cache.attack_haste();
}
double attack_t::composite_expertise() const
{
return base_attack_expertise + player->cache.attack_expertise();
}
double attack_t::composite_versatility( const action_state_t* state ) const
{
return action_t::composite_versatility( state ) + player->cache.damage_versatility();
}
void attack_t::attack_table_t::build_table( double miss_chance, double dodge_chance, double parry_chance,
double glance_chance, double crit_chance, sim_t* sim )
{
sim->print_debug( "attack_t::build_table: miss={} dodge={} parry={} glance={} crit={}", miss_chance, dodge_chance,
parry_chance, glance_chance, crit_chance );
assert( crit_chance >= 0 && crit_chance <= 1.0 );
double sum = miss_chance + dodge_chance + parry_chance + crit_chance;
// Only recalculate attack table if the stats have changed
if ( attack_table_sum == sum )
return;
attack_table_sum = sum;
double limit = 1.0;
double total = 0;
num_results = 0;
if ( miss_chance > 0 && total < limit )
{
total += miss_chance;
if ( total > limit )
total = limit;
chances[ num_results ] = total;
results[ num_results ] = RESULT_MISS;
num_results++;
}
if ( dodge_chance > 0 && total < limit )
{
total += dodge_chance;
if ( total > limit )
total = limit;
chances[ num_results ] = total;
results[ num_results ] = RESULT_DODGE;
num_results++;
}
if ( parry_chance > 0 && total < limit )
{
total += parry_chance;
if ( total > limit )
total = limit;
chances[ num_results ] = total;
results[ num_results ] = RESULT_PARRY;
num_results++;
}
if ( glance_chance > 0 && total < limit )
{
total += glance_chance;
if ( total > limit )
total = limit;
chances[ num_results ] = total;
results[ num_results ] = RESULT_GLANCE;
num_results++;
}
if ( crit_chance > 0 && total < limit )
{
total += crit_chance;
if ( total > limit )
total = limit;
chances[ num_results ] = total;
results[ num_results ] = RESULT_CRIT;
num_results++;
}
if ( total < 1.0 )
{
chances[ num_results ] = 1.0;
results[ num_results ] = RESULT_HIT;
num_results++;
}
}
result_e attack_t::calculate_result( action_state_t* s ) const
{
result_e result = RESULT_NONE;
if ( !harmful || !may_hit || !s->target )
return RESULT_NONE;
int delta_level = s->target->level() - player->level();
double miss = may_miss ? miss_chance( composite_hit(), s->target ) : 0;
double dodge = may_dodge ? dodge_chance( composite_expertise(), s->target ) : 0;
double parry =
may_parry && player->position() == POSITION_FRONT ? parry_chance( composite_expertise(), s->target ) : 0;
double crit = may_crit ? std::max( s->composite_crit_chance() + s->target->cache.crit_avoidance(), 0.0 ) : 0;
// Specials are 2-roll calculations, so only pass crit chance to
// build_table for non-special attacks
attack_table.build_table( miss, dodge, parry, may_glance ? glance_chance( delta_level ) : 0.0,
!special ? std::min( 1.0, crit ) : 0.0, sim );
if ( attack_table.num_results == 1 )
{
result = attack_table.results.front();
}
else
{
// 1-roll attack table with true RNG
double random = rng().real();
for ( int i = 0; i < attack_table.num_results; ++i )
{
if ( random <= attack_table.chances[ i ] )
{
result = attack_table.results[ i ];
break;
}
}
}
assert( result != RESULT_NONE );
// if we have a special, make a second roll for hit/crit
if ( result == RESULT_HIT && special && may_crit )
{
if ( rng().roll( crit ) )
result = RESULT_CRIT;
}
return result;
}
double attack_t::recharge_multiplier( const cooldown_t& cd ) const
{
double m = action_t::recharge_multiplier( cd );
if ( cd.hasted )
{
m *= player->cache.attack_haste();
}
return m;
}
void attack_t::reschedule_auto_attack( double old_swing_haste )
{
if ( player->cache.auto_attack_speed() == old_swing_haste )
{
return;
}
// Note that if attack -> swing_haste() > old_swing_haste, this could
// probably be handled by rescheduling, but the code is slightly simpler if
// we just cancel the event and make a new one.
if ( execute_event && execute_event->remains() > timespan_t::zero() )
{
timespan_t time_to_hit = execute_event->occurs() - sim->current_time();
timespan_t new_time_to_hit = time_to_hit * player->cache.auto_attack_speed() / old_swing_haste;
if ( time_to_hit == new_time_to_hit )
{
return;
}
if ( sim->debug )
{
sim->print_debug( "Haste change, reschedule {} from {} to {} (speed {} -> {}, remains {})", *this,
execute_event->remains(), new_time_to_hit, old_swing_haste, player->cache.auto_attack_speed(),
time_to_hit );
}
if ( new_time_to_hit > time_to_hit )
execute_event->reschedule( new_time_to_hit );
else
{
event_t::cancel( execute_event );
execute_event = start_action_execute_event( new_time_to_hit );
}
}
}
void attack_t::reset()
{
attack_table.reset();
action_t::reset();
}
// ==========================================================================
// Melee Attack
// ==========================================================================
melee_attack_t::melee_attack_t( util::string_view n, player_t* p ) : melee_attack_t( n, p, spell_data_t::nil() ) {}
melee_attack_t::melee_attack_t( util::string_view n, player_t* p, const spell_data_t* s ) : attack_t( n, p, s )
{
// Prevent melee from being scheduled when player is moving
if ( range < 0 )
range = 5;
}
void melee_attack_t::init()
{
attack_t::init();
if ( !special )
may_glance = true;
}
double melee_attack_t::parry_chance( double expertise, player_t* t ) const
{
if ( t->is_enemy() && sim->auto_attacks_always_land && !special )
{
return 0.0;
}
// cache.parry() contains the target's parry chance (3.0 base, plus spec
// bonuses and rating)
double parry = t->cache.parry();
// WoD mechanics are similar to MoP
// add or subtract 1.5% per level difference
parry += ( t->level() - player->level() ) * 0.015;
// 3% additional parry for attacking a level+3 or higher NPC
if ( t->is_enemy() && ( t->level() - player->level() ) > 2 )
parry += 0.03;
// subtract the player's expertise chance - no longer depends on dodge
parry -= expertise;
return parry;
}
double melee_attack_t::glance_chance( int delta_level ) const
{
double glance = 0;
// TODO-WOD: Glance chance increase per 4+ level delta?
if ( delta_level > 3 )
{
glance += 0.10 + 0.10 * delta_level;
}
return glance;
}
proc_types melee_attack_t::proc_type() const
{
if ( s_data->ok() )
{
switch ( s_data->dmg_class() )
{
case SPELL_TYPE_NONE: return PROC1_NONE_SPELL;
case SPELL_TYPE_MAGIC: return PROC1_MAGIC_SPELL;
case SPELL_TYPE_MELEE: return special ? PROC1_MELEE_ABILITY : PROC1_MELEE;
case SPELL_TYPE_RANGED: return special ? PROC1_RANGED_ABILITY : PROC1_RANGED;
}
}
if ( special )
return PROC1_MELEE_ABILITY;
else
return PROC1_MELEE;
}
// ==========================================================================
// Ranged Attack
// ==========================================================================
ranged_attack_t::ranged_attack_t( util::string_view token, player_t* p )
: ranged_attack_t( token, p, spell_data_t::nil() )
{}
ranged_attack_t::ranged_attack_t( util::string_view token, player_t* p, const spell_data_t* s )
: attack_t( token, p, s )
{
// ranged attacks cannot be blocked or parried
may_block = may_parry = false;
}
// Ranged attacks are identical to melee attacks, but cannot be parried or dodged.
// all of the inherited *_chance() methods are accurate.
double ranged_attack_t::composite_target_multiplier( player_t* t ) const
{
double v = attack_t::composite_target_multiplier( t );
return v;
}
void ranged_attack_t::schedule_execute( action_state_t* state )
{
if ( sim->debug )
{
sim->print_log( "{} schedules execute for {} ({})", *player, *this,
player->resources.current[ player->primary_resource() ] );
}
time_to_execute = execute_time();
execute_event = start_action_execute_event( time_to_execute, state );
if ( trigger_gcd > timespan_t::zero() )
player->off_gcdactions.clear();
if ( !background )
{
if ( player->queueing == this )
{
player->queueing = nullptr;
}
player->executing = this;
player->gcd_ready = sim->current_time() + gcd();
player->gcd_type = gcd_type;
switch ( gcd_type )
{
case gcd_haste_type::SPELL_HASTE: player->gcd_current_haste_value = player->cache.spell_haste(); break;
case gcd_haste_type::ATTACK_HASTE: player->gcd_current_haste_value = player->cache.attack_haste(); break;
default: break;
}
if ( player->action_queued && sim->strict_gcd_queue )
{
player->gcd_ready -= sim->queue_gcd_reduction;
}
}
}
proc_types ranged_attack_t::proc_type() const
{
if ( s_data->ok() )
{
switch ( s_data->dmg_class() )
{
case SPELL_TYPE_NONE: return PROC1_NONE_SPELL;
case SPELL_TYPE_MAGIC: return PROC1_MAGIC_SPELL;
case SPELL_TYPE_MELEE: return special ? PROC1_MELEE_ABILITY : PROC1_MELEE;
case SPELL_TYPE_RANGED: return special ? PROC1_RANGED_ABILITY : PROC1_RANGED;
}
}
if ( special )
return PROC1_RANGED_ABILITY;
else
return PROC1_RANGED;
}