-
Notifications
You must be signed in to change notification settings - Fork 503
/
Copy pathSpellCaster.cpp
2386 lines (2007 loc) · 89.2 KB
/
SpellCaster.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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "ObjectMgr.h"
#include "SpellCaster.h"
#include "DynamicObject.h"
#include "GameObject.h"
#include "Totem.h"
#include "CreatureAI.h"
#include "Chat.h"
#include "Spell.h"
#include "SpellAuras.h"
#include "World.h"
#include "WorldPacket.h"
#include "Opcodes.h"
Unit* SpellCaster::SelectMagnetTarget(Unit* victim, Spell* spell, SpellEffectIndex eff)
{
if (!victim)
return nullptr;
SpellEntry const* pProto = spell->m_spellInfo;
if (!pProto) return nullptr;
// Example spell: Cause Insanity (Hakkar)
if (pProto->AttributesEx & SPELL_ATTR_EX_NO_REDIRECTION)
return victim;
// Magic case
if (pProto->AttributesEx3 & SPELL_ATTR_EX3_SUPPRESS_TARGET_PROCS)
return victim;
if ((pProto->DmgClass == SPELL_DAMAGE_CLASS_MAGIC || pProto->SpellVisual == 7250) && pProto->Dispel != DISPEL_POISON && !(pProto->Attributes & SPELL_ATTR_IS_ABILITY))
{
Unit::AuraList const& magnetAuras = victim->GetAurasByType(SPELL_AURA_SPELL_MAGNET);
for (const auto magnetAura : magnetAuras)
{
if (Unit* magnet = magnetAura->GetCaster())
{
if (magnet->IsAlive() && magnet->IsInMap(this) && spell->CheckTarget(magnet, eff))
{
if (SpellAuraHolder* holder = magnetAura->GetHolder())
if (holder->DropAuraCharge())
{
magnet->RemoveAurasDueToSpell(holder->GetId()); // Remove from grounding totem also
victim->RemoveSpellAuraHolder(holder);
}
return magnet;
}
}
}
}
return victim;
}
uint32 SpellCaster::GetLevelForTarget(SpellCaster const* target) const
{
// Bosses are 3 levels higher than players.
if (Creature const* pCreature = ToCreature())
{
if (!pCreature->IsWorldBoss() || !target || !target->IsUnit())
return pCreature->GetLevel();
uint32 level = static_cast<Unit const*>(target)->GetLevel() + sWorld.getConfig(CONFIG_UINT32_WORLD_BOSS_LEVEL_DIFF);
if (level < 1)
return 1;
if (level > 255)
return 255;
return level;
}
if (Unit const* pUnit = ToUnit())
return pUnit->GetLevel();
if (GameObject const* pGo = ToGameObject())
{
uint32 level = 0;
switch (pGo->GetGOInfo()->type)
{
case GAMEOBJECT_TYPE_CHEST:
level = pGo->GetGOInfo()->chest.level;
break;
case GAMEOBJECT_TYPE_TRAP:
level = pGo->GetGOInfo()->trap.level;
break;
}
if (!level)
level = pGo->GetUInt32Value(GAMEOBJECT_LEVEL);
if (level)
return level;
}
if (Unit const* pUnit = ::ToUnit(target))
return pUnit->GetLevel();
return PLAYER_MAX_LEVEL;
}
uint32 SpellCaster::GetWeaponSkillValue(WeaponAttackType attType, SpellCaster const* target) const
{
if (Player const* pPlayer = ToPlayer())
{
Item* item = pPlayer->GetWeaponForAttack(attType, true, true);
// feral or unarmed skill only for base attack
if (attType != BASE_ATTACK && !item)
return 0;
if (pPlayer->IsNoWeaponShapeShift())
return GetSkillMaxForLevel(); // always maximized SKILL_FERAL_COMBAT in fact
// weapon skill or (unarmed for base attack)
uint32 skill = item ? item->GetProto()->GetProficiencySkill() : SKILL_UNARMED;
// Daemon: pas en preBC !
// // in PvP use full skill instead current skill value
// value = (target && target->GetTypeId() == TYPEID_PLAYER)
// ? ((Player*)this)->GetSkillMax(skill)
// : ((Player*)this)->GetSkillValue(skill);
return pPlayer->GetSkillValue(skill);
}
return GetUnitMeleeSkill(target);
}
uint32 SpellCaster::GetDefenseSkillValue(SpellCaster const* target) const
{
if (Player const* pPlayer = ToPlayer())
{
// in PvP use full skill instead current skill value
uint32 value = (target && target->IsPlayer())
? pPlayer->GetSkillMax(SKILL_DEFENSE)
: pPlayer->GetSkillValue(SKILL_DEFENSE);
return value;
}
return GetUnitMeleeSkill(target);
}
// Calculate spell hit result can be:
// Every spell can: Evade/Immune/Reflect/Sucesful hit
// For melee based spells:
// Miss
// Dodge
// Parry
// For spells
// Resist
SpellMissInfo SpellCaster::SpellHitResult(Unit* pVictim, SpellEntry const* spell, SpellEffectIndex effIndex, bool CanReflect, Spell* spellPtr)
{
// Return evade for units in evade mode
if (pVictim->GetTypeId() == TYPEID_UNIT && ((Creature*)pVictim)->IsInEvadeMode())
return SPELL_MISS_EVADE;
// World of Warcraft Client Patch 1.7.0 (2005-09-13)
// - Effects that make players immune to physical will no longer be immune
// to the "Recently Bandaged" effect from First Aid.
if (/* pVictim != this && */ /* commented out due to above patch notes */
!spell->HasAttribute(SPELL_ATTR_NO_IMMUNITIES) &&
pVictim->IsImmuneToSpell(spell, pVictim == this))
return SPELL_MISS_IMMUNE;
// All positive spells can`t miss
// TODO: client not show miss log for this spells - so need find info for this in dbc and use it!
if (spell->IsPositiveSpell(this, pVictim) || spell->IsPositiveEffect(effIndex, this, pVictim))
return SPELL_MISS_NONE;
// Check for immune (use charges)
SpellSchoolMask schoolMask;
if (spellPtr)
schoolMask = spellPtr->m_spellSchoolMask;
else
schoolMask = spell->GetSpellSchoolMask();
if (pVictim != this && pVictim->IsImmuneToDamage(schoolMask, spell))
return SPELL_MISS_IMMUNE;
// Try victim reflect spell
if (CanReflect)
{
int32 reflectchance = pVictim->GetTotalAuraModifier(SPELL_AURA_REFLECT_SPELLS);
Unit::AuraList const& mReflectSpellsSchool = pVictim->GetAurasByType(SPELL_AURA_REFLECT_SPELLS_SCHOOL);
for (const auto i : mReflectSpellsSchool)
if (i->GetModifier()->m_miscvalue & spell->GetSpellSchoolMask())
reflectchance += i->GetModifier()->m_amount;
if (reflectchance > 0 && roll_chance_i(reflectchance))
{
// Start triggers for remove charges if need (trigger only for victim, and mark as active spell)
ProcDamageAndSpell(ProcSystemArguments(pVictim, PROC_FLAG_NONE, PROC_FLAG_TAKE_HARMFUL_SPELL, PROC_EX_REFLECT, 1, 1, BASE_ATTACK, spell));
return SPELL_MISS_REFLECT;
}
}
switch (spell->DmgClass)
{
case SPELL_DAMAGE_CLASS_NONE:
return SPELL_MISS_NONE;
case SPELL_DAMAGE_CLASS_MAGIC:
return MagicSpellHitResult(pVictim, spell, spellPtr);
case SPELL_DAMAGE_CLASS_MELEE:
case SPELL_DAMAGE_CLASS_RANGED:
return MeleeSpellHitResult(pVictim, spell, spellPtr);
}
return SPELL_MISS_NONE;
}
ProcSystemArguments::ProcSystemArguments(Unit* pVictim_, uint32 procFlagsAttacker_, uint32 procFlagsVictim_, uint32 procExtra_, uint32 amount_, uint32 originalAmount_, WeaponAttackType attType_,
SpellEntry const* procSpell_, Spell const* spell)
: pVictim(pVictim_), procFlagsAttacker(procFlagsAttacker_), procFlagsVictim(procFlagsVictim_), procExtra(procExtra_), amount(amount_), originalAmount(originalAmount_),
attType(attType_), procSpell(procSpell_), isSpellTriggeredByAuraOrItem(spell && (spell->IsTriggeredByAura() || spell->IsTriggered() && spell->IsCastByItem())), procTime(sWorld.GetGameTime())
{
if (spell)
appliedSpellModifiers = spell->m_appliedMods;
if (pVictim_)
victimGuid = pVictim_->GetObjectGuid();
}
void SpellCaster::UpdatePendingProcs(uint32 diff)
{
if (sWorld.getConfig(CONFIG_UINT32_SPELL_PROC_DELAY) && IsInWorld())
{
if (m_procsUpdateTimer < diff)
{
m_procsUpdateTimer = sWorld.getConfig(CONFIG_UINT32_SPELL_PROC_DELAY);
if (!m_pendingProcChecks.empty())
{
std::vector<ProcSystemArguments> procData = std::move(m_pendingProcChecks);
m_pendingProcChecks.clear();
for (auto& proc : procData)
ProcDamageAndSpell_delayed(proc);
}
}
else
m_procsUpdateTimer -= diff;
}
}
void SpellCaster::ProcDamageAndSpell(ProcSystemArguments&& data)
{
if ((data.pVictim && !IsInMap(data.pVictim)) || !IsInWorld())
return;
if (!(data.procExtra & PROC_EX_CAST_END))
{
if (data.procFlagsAttacker)
if (Unit* pUnit = ToUnit())
pUnit->ProcSkillsAndReactives(false, data.pVictim, data.procFlagsAttacker, data.procExtra, data.attType, data.procSpell);
if (data.procFlagsVictim && data.pVictim && data.pVictim->IsAlive())
data.pVictim->ProcSkillsAndReactives(true, IsUnit() ? static_cast<Unit*>(this) : data.pVictim, data.procFlagsVictim, data.procExtra, data.attType, data.procSpell);
}
// Always execute On Kill procs instantly. Fixes Improved Drain Soul talent.
if (!sWorld.getConfig(CONFIG_UINT32_SPELL_PROC_DELAY) || (data.procFlagsAttacker & PROC_FLAG_KILL))
ProcDamageAndSpell_real(data, PROC_PROCESS_ALL);
else
{
ProcDamageAndSpell_real(data, PROC_PROCESS_INSTANT);
m_pendingProcChecks.emplace_back(std::move(data));
}
}
void SpellCaster::ProcDamageAndSpell_delayed(ProcSystemArguments& data)
{
if (data.pVictim)
{
data.pVictim = GetMap()->GetUnit(data.victimGuid);
if (!data.pVictim)
return;
}
ProcDamageAndSpell_real(data, PROC_PROCESS_DELAYED);
}
void SpellCaster::ProcDamageAndSpell_real(ProcSystemArguments& data, ProcessProcsAuraType processAurasType)
{
ProcTriggeredList procTriggered;
// Not much to do if no flags are set.
if (data.procFlagsAttacker)
if (Unit* pUnit = ToUnit())
pUnit->ProcDamageAndSpellFor(false, data.pVictim, data, procTriggered, processAurasType);
// There are no auras with the SPELL_ATTR_EX3_INSTANT_TARGET_PROCS for victims
if (processAurasType != PROC_PROCESS_INSTANT &&
// Now go on with a victim's events'n'auras
// Not much to do if no flags are set or there is no victim
data.pVictim && data.pVictim->IsAlive() && data.procFlagsVictim)
{
data.pVictim->ProcDamageAndSpellFor(true, IsUnit() ? static_cast<Unit*>(this) : data.pVictim, data, procTriggered, processAurasType);
// Standing up on damage taken must happen after proc checks.
if (Player* pVictimPlayer = data.pVictim->ToPlayer())
if (pVictimPlayer->IsStandUpScheduled())
pVictimPlayer->SetStandState(UNIT_STAND_STATE_STAND);
}
if (Unit* pUnit = ToUnit())
pUnit->HandleTriggers(data.pVictim, data.procExtra, data.amount, data.originalAmount, data.procSpell, procTriggered);
}
// Melee based spells can be miss, parry or dodge on this step
// Crit or block - determined on damage calculation phase! (and can be both in some time)
float SpellCaster::MeleeSpellMissChance(Unit* pVictim, WeaponAttackType attType, int32 skillDiff, SpellEntry const* spell, Spell* spellPtr)
{
if (!pVictim || !pVictim->IsStandingUp())
return 0.0f;
// Calculate hit chance (more correct for chance mod)
float hitChance = 0.0f;
float missChance = 0.0f;
// PvP - PvE melee chances
if (pVictim->GetTypeId() == TYPEID_PLAYER)
missChance = 5.0f - skillDiff * 0.04f;
else if (skillDiff < -10)
missChance = 5.0f - skillDiff * 0.2f;
else
missChance = 5.0f - skillDiff * 0.1f;
// Low level reduction
if (!pVictim->IsPlayer() && pVictim->GetLevel() < 10)
missChance *= pVictim->GetLevel() / 10.0f;
if (Unit* pUnit = ToUnit())
{
// Spellmod from SPELLMOD_RESIST_MISS_CHANCE
if (Player* modOwner = pUnit->GetSpellModOwner())
modOwner->ApplySpellMod(spell->Id, SPELLMOD_RESIST_MISS_CHANCE, hitChance, spellPtr);
// Bonuses from attacker aura and ratings
hitChance += pUnit->GetWeaponBasedAuraModifier(attType, SPELL_AURA_MOD_HIT_CHANCE);
}
// There is some code in 1.12 that explicitly adds a modifier that causes the first 1% of +hit gained from
// talents or gear to be ignored against monsters with more than 10 Defense Skill above the attacking players Weapon Skill.
// https://us.forums.blizzard.com/en/wow/t/bug-hit-tables/185675/33
if (skillDiff < -10 && hitChance > 0)
hitChance -= 1.0f;
// Hit chance depends from victim auras
if (attType == RANGED_ATTACK)
hitChance += pVictim->GetTotalAuraModifier(SPELL_AURA_MOD_ATTACKER_RANGED_HIT_CHANCE);
else
hitChance += pVictim->GetTotalAuraModifier(SPELL_AURA_MOD_ATTACKER_MELEE_HIT_CHANCE);
missChance -= hitChance;
// Limit miss chance from 0 to 60%
if (missChance < 0.0f)
return 0.0f;
if (missChance > 60.0f)
return 60.0f;
return missChance;
}
// Melee based spells hit result calculations
SpellMissInfo SpellCaster::MeleeSpellHitResult(Unit* pVictim, SpellEntry const* spell, Spell* spellPtr)
{
WeaponAttackType attType = spell->DmgClass == SPELL_DAMAGE_CLASS_RANGED ? RANGED_ATTACK : BASE_ATTACK;
// Warrior spell Execute (5308) should never dodge, miss, resist ... Only the trigger can (20647)
if (spell->IsFitToFamily<SPELLFAMILY_WARRIOR, CF_WARRIOR_EXECUTE>() && spell->Id != 20647)
return SPELL_MISS_NONE;
// Hammer of Wrath should not use weapon skill, but Bloodthirst should.
// bonus from skills is 0.04% per skill Diff
int32 attackerWeaponSkill = (spell->rangeIndex == SPELL_RANGE_IDX_COMBAT || spell->EquippedItemClass == ITEM_CLASS_WEAPON) ?
int32(GetWeaponSkillValue(attType, pVictim)) : GetSkillMaxForLevel();
int32 skillDiff = attackerWeaponSkill - int32(pVictim->GetSkillMaxForLevel(this));
int32 fullSkillDiff = attackerWeaponSkill - int32(pVictim->GetDefenseSkillValue(this));
int32 minWeaponSkill = GetSkillMaxForLevel(pVictim) < attackerWeaponSkill ? GetSkillMaxForLevel(pVictim) : attackerWeaponSkill;
int32 cappedSkillDiff = minWeaponSkill - pVictim->GetSkillMaxForLevel(this);
uint32 roll = urand(0, 9999);
uint32 missChance = uint32(MeleeSpellMissChance(pVictim, attType, fullSkillDiff, spell, spellPtr) * 100.0f);
// Roll miss
uint32 tmp = spell->AttributesEx3 & SPELL_ATTR_EX3_ALWAYS_HIT ? 0 : missChance;
if (roll < tmp)
return SPELL_MISS_MISS;
// Chance resist mechanic for spell (effect resistance handled later)
int32 resist_mech = 0;
if (spell->Mechanic)
resist_mech = pVictim->GetTotalAuraModifierByMiscValue(SPELL_AURA_MOD_MECHANIC_RESISTANCE, spell->Mechanic) * 100;
// Roll chance
tmp += resist_mech;
if (roll < tmp)
return SPELL_MISS_RESIST;
bool canDodge = true;
bool canParry = true;
bool canBlock = spell->HasAttribute(SPELL_ATTR_EX3_COMPLETELY_BLOCKED);
// Same spells cannot be parry/dodge
if (spell->Attributes & SPELL_ATTR_NO_ACTIVE_DEFENSE)
return SPELL_MISS_NONE;
// Ranged attack cannot be parry/dodge
if (attType == RANGED_ATTACK)
return SPELL_MISS_NONE;
bool from_behind = !pVictim->HasInArc(this, M_PI_F);
// Check for attack from behind
if (from_behind)
{
// Can`t dodge from behind in PvP (but its possible in PvE)
if (GetTypeId() == TYPEID_PLAYER && pVictim->GetTypeId() == TYPEID_PLAYER)
canDodge = false;
// Can`t parry or block
canParry = false;
canBlock = false;
}
// Check creatures flags_extra for disable parry
if (Creature* pCreatureVictim = pVictim->ToCreature())
{
if (pCreatureVictim->HasExtraFlag(CREATURE_FLAG_EXTRA_NO_PARRY))
canParry = false;
if (pCreatureVictim->HasExtraFlag(CREATURE_FLAG_EXTRA_NO_BLOCK))
canBlock = false;
}
// Check if the player can parry
else
{
if (!((Player*)pVictim)->CanParry())
canParry = false;
if (!((Player*)pVictim)->CanBlock())
canBlock = false;
}
if (canDodge)
{
// Roll dodge
int32 dodgeModifier = pVictim->IsPlayer() ? skillDiff * 4 : skillDiff * 10;
int32 dodgeChance = int32(pVictim->GetUnitDodgeChance() * 100.0f) - dodgeModifier;
if (dodgeChance < 0)
dodgeChance = 0;
// Low level reduction
if (!pVictim->IsPlayer() && pVictim->GetLevel() < 10)
dodgeChance *= pVictim->GetLevel() / 10.0f;
tmp += dodgeChance;
if (roll < tmp)
return SPELL_MISS_DODGE;
}
if (canParry)
{
// Roll parry
int32 parryModifier = pVictim->IsPlayer() ? skillDiff * 4 : cappedSkillDiff < -10 ? 60 * cappedSkillDiff : 20 * cappedSkillDiff;
int32 parryChance = int32(pVictim->GetUnitParryChance() * 100.0f) - parryModifier;
// Can`t parry from behind
if (parryChance < 0)
parryChance = 0;
// Low level reduction
if (!pVictim->IsPlayer() && pVictim->GetLevel() < 10)
parryChance *= pVictim->GetLevel() / 10.0f;
tmp += parryChance;
if (roll < tmp)
return SPELL_MISS_PARRY;
}
// There are 2 types of ability blocks: partial and full
// Fully blockable spells have a specific attribute, which generates a miss instead of a partial block
// Spells with an attribute must be rolled for block once on spell hit die
// Spells without an attribute must be rolled for partial block only inside damage calculation
if (canBlock && pVictim->RollSpellBlockChanceOutcome(this, attType))
return SPELL_MISS_BLOCK;
return SPELL_MISS_NONE;
}
SpellMissInfo SpellCaster::MagicSpellHitResult(Unit* pVictim, SpellEntry const* spell, Spell* spellPtr)
{
// Can`t miss on dead target (on skinning for example)
if (!pVictim->IsAlive())
return SPELL_MISS_NONE;
// Spell cannot be resisted (not exist on dbc, custom flag)
if (spell->AttributesEx4 & SPELL_ATTR_EX4_IGNORE_RESISTANCES)
return SPELL_MISS_NONE;
int32 hitChance = MagicSpellHitChance(pVictim, spell, spellPtr);
int32 missChance = 10000 - hitChance;
int32 rand = irand(0, 10000);
if (rand < missChance)
return SPELL_MISS_RESIST;
return SPELL_MISS_NONE;
}
int32 SpellCaster::MagicSpellHitChance(Unit* pVictim, SpellEntry const* spell, Spell* spellPtr)
{
if (spell->AttributesEx3 & SPELL_ATTR_EX3_ALWAYS_HIT)
return 10000;
SpellSchoolMask schoolMask = spell->GetSpellSchoolMask();
// PvP - PvE spell misschances per leveldif > 2
int32 lchance = pVictim->GetTypeId() == TYPEID_PLAYER ? 7 : 11;
// World of Warcraft Client Patch 1.7.0 (2005-09-13)
// - Debuffs and area effect spells now use their actual cast level rather
// than effective cast level for calculating periodic resistance.
// - Fixed a bug where area of effect periodic damage spells were being
// resisted more frequently than they should have been when casting
// lower level ranks of the spell (affected spells were Blizzard,
// Consecration,Explosive Trap, Flamestrike, Hurricane, Rain of Fire and
// Volley).
#if SUPPORTED_CLIENT_BUILD > CLIENT_BUILD_1_6_1
int32 leveldif = int32(pVictim->GetLevelForTarget(this)) - int32(GetLevelForTarget(pVictim));
#else
int32 leveldif = (!spellPtr && spell->HasEffect(SPELL_EFFECT_PERSISTENT_AREA_AURA)) ?
int32(pVictim->GetLevelForTarget(this)) - std::max<int32>(1, spell->spellLevel) :
int32(pVictim->GetLevelForTarget(this)) - int32(GetLevelForTarget(pVictim));
#endif
// Base hit chance from attacker and victim levels
float modHitChance;
if (leveldif < 3)
modHitChance = 96 - leveldif;
else
modHitChance = 94 - (leveldif - 2) * lchance;
// Spellmod from SPELLMOD_RESIST_MISS_CHANCE
if (Unit* pUnit = ToUnit())
{
if (Player* modOwner = pUnit->GetSpellModOwner())
{
modOwner->ApplySpellMod(spell->Id, SPELLMOD_RESIST_MISS_CHANCE, modHitChance, spellPtr);
}
}
// Chance hit from victim SPELL_AURA_MOD_ATTACKER_SPELL_HIT_CHANCE auras
modHitChance += pVictim->GetTotalAuraModifierByMiscMask(SPELL_AURA_MOD_ATTACKER_SPELL_HIT_CHANCE, schoolMask);
// Reduce spell hit chance for Area of effect spells from victim SPELL_AURA_MOD_AOE_AVOIDANCE aura
if (spell->IsAreaOfEffectSpell())
{
modHitChance -= pVictim->GetTotalAuraModifier(SPELL_AURA_MOD_AOE_AVOIDANCE);
}
// Chance resist mechanic for spell (effect resistance handled later)
int32 resist_mech = 0;
if (spell->Mechanic)
resist_mech = pVictim->GetTotalAuraModifierByMiscValue(SPELL_AURA_MOD_MECHANIC_RESISTANCE, spell->Mechanic);
// Apply mod
modHitChance -= resist_mech;
// Chance resist debuff
modHitChance -= pVictim->GetTotalAuraModifierByMiscValue(SPELL_AURA_MOD_DEBUFF_RESISTANCE, int32(spell->Dispel));
// Increase hit chance from attacker SPELL_AURA_MOD_SPELL_HIT_CHANCE and attacker ratings
if (Unit* pUnit = ToUnit())
modHitChance += int32(pUnit->m_modSpellHitChance);
// Nostalrius: sorts binaires.
if (spell->IsBinary())
{
// Get base victim resistance for school
float resistModHitChance = GetSpellResistChance(pVictim, schoolMask, false);
modHitChance *= (1 - resistModHitChance);
}
int32 HitChance = modHitChance * 100;
if (HitChance < 100) HitChance = 100;
if (HitChance > 9900) HitChance = 9900;
return HitChance;
}
float SpellCaster::GetSpellResistChance(Unit const* victim, uint32 schoolMask, bool innateResists) const
{
Unit const* pUnit = ToUnit();
// Get base victim resistance for school
float const baseResistance = victim->GetResistance(GetFirstSchoolInMask(SpellSchoolMask(schoolMask)));
// Get attacker spell penetration from SPELL_AURA_MOD_TARGET_RESISTANCE aura
float const selfResistance = pUnit ? pUnit->GetTotalAuraModifierByMiscMask(SPELL_AURA_MOD_TARGET_RESISTANCE, schoolMask) : 0;
float resistModHitChance = baseResistance + selfResistance;
#if SUPPORTED_CLIENT_BUILD > CLIENT_BUILD_1_8_4
if ((resistModHitChance < 0.0f) && (baseResistance >= 0.0f))
resistModHitChance = 0.0f;
#endif
// Magic vulnerability calculation
if (resistModHitChance < 0.0f)
{
// Victim's level based skill, penalize when calculating for low levels (< 20):
float const skill = std::max(GetSkillMaxForLevel(victim), uint16(100));
// Convert resistance value to vulnerability percentage through comparision with skill
resistModHitChance = (float(resistModHitChance) / skill);
if (resistModHitChance < -0.75f)
resistModHitChance = -0.75f;
return resistModHitChance;
}
uint32 const uiLevel = GetLevel();
// Computing innate resists, resistance bonus when attacking a creature higher level. Not affected by modifiers.
if (innateResists && victim->GetTypeId() == TYPEID_UNIT)
{
int32 leveldiff = int32(victim->GetLevelForTarget(this)) - int32(GetLevelForTarget(victim));
resistModHitChance += int32((8.0f * leveldiff) * uiLevel / 63.0f);
}
resistModHitChance *= (float)(0.15f / uiLevel);
if (resistModHitChance < 0.0f)
resistModHitChance = 0.0f;
if (resistModHitChance > 0.75f)
resistModHitChance = 0.75f;
return resistModHitChance;
}
void SpellCaster::SendSpellMiss(Unit* target, uint32 spellId, SpellMissInfo missInfo) const
{
WorldPacket data(SMSG_SPELLLOGMISS, (4 + 8 + 1 + 4 + 8 + 1));
data << uint32(spellId);
data << GetObjectGuid();
data << uint8(0); // unk8
data << uint32(1); // target count
// for(i = 0; i < target count; ++i)
data << target->GetObjectGuid(); // target GUID
data << uint8(missInfo);
// Nostalrius: + 2 * float if unk8=1
// end loop
SendObjectMessageToSet(&data, true);
}
void SpellCaster::SendSpellDamageResist(Unit* target, uint32 spellId) const
{
WorldPacket data(SMSG_PROCRESIST, 8 + 8 + 4 + 1);
data << GetObjectGuid();
data << target->GetObjectGuid();
data << uint32(spellId);
data << uint8(0); // bool - log format: 0-default, 1-debug
SendMessageToSet(&data, true);
}
void SpellCaster::SendSpellOrDamageImmune(Unit* target, uint32 spellId) const
{
WorldPacket data(SMSG_SPELLORDAMAGE_IMMUNE, (8 + 8 + 4 + 1));
data << GetObjectGuid();
data << target->GetObjectGuid();
data << uint32(spellId);
data << uint8(0);
SendMessageToSet(&data, true);
}
uint32 SpellCaster::SpellCriticalDamageBonus(SpellEntry const* spellProto, uint32 damage, Unit* pVictim, Spell* spell)
{
// Calculate critical bonus
int32 crit_bonus;
switch (spellProto->DmgClass)
{
case SPELL_DAMAGE_CLASS_MELEE: // for melee based spells is 100%
case SPELL_DAMAGE_CLASS_RANGED:
crit_bonus = damage;
break;
default:
crit_bonus = damage / 2; // for spells is 50%
break;
}
Unit const* pUnit = ToUnit();
// adds additional damage to crit_bonus (from talents)
if (pUnit)
{
if (Player* modOwner = pUnit->GetSpellModOwner())
modOwner->ApplySpellMod(spellProto->Id, SPELLMOD_CRIT_DAMAGE_BONUS, crit_bonus, spell);
}
if (!pVictim)
return damage += crit_bonus;
uint32 creatureTypeMask = pVictim->GetCreatureTypeMask();
float critPctDamageMod = pUnit ? pUnit->GetTotalAuraMultiplierByMiscMask(SPELL_AURA_MOD_CRIT_PERCENT_VERSUS, creatureTypeMask) : 1;
damage = (damage + crit_bonus) * critPctDamageMod;
return damage;
}
float SpellCaster::SpellCriticalHealingBonus(SpellEntry const* spellProto, uint32 damage, Unit const* pVictim) const
{
// Calculate critical bonus
float crit_bonus;
switch (spellProto->DmgClass)
{
case SPELL_DAMAGE_CLASS_MELEE: // for melee based spells is 100%
case SPELL_DAMAGE_CLASS_RANGED:
// TODO: write here full calculation for melee/ranged spells
crit_bonus = damage;
break;
default:
crit_bonus = damage / 2; // for spells is 50%
break;
}
if (pVictim)
{
if (Unit const* pUnit = ToUnit())
{
uint32 creatureTypeMask = pVictim->GetCreatureTypeMask();
crit_bonus = crit_bonus * pUnit->GetTotalAuraMultiplierByMiscMask(SPELL_AURA_MOD_CRIT_PERCENT_VERSUS, creatureTypeMask);
}
}
if (crit_bonus > 0)
damage += crit_bonus;
return damage;
}
int32 SpellCaster::DealHeal(Unit* pVictim, uint32 addhealth, SpellEntry const* spellProto, bool critical)
{
Unit* pUnit = ToUnit();
// Script Event HealedBy
if (pVictim->AI() && pUnit)
pVictim->AI()->HealedBy(pUnit, addhealth);
int32 gain = pVictim->ModifyHealth(int32(addhealth));
SpellCaster* pHealer = this;
if (IsCreature() && ((Creature*)this)->IsTotem())
pHealer = pUnit->GetOwner();
if (IsPlayer() || pVictim->IsPlayer())
pHealer->SendHealSpellLog(pVictim, spellProto->Id, addhealth, critical);
return gain;
}
void SpellCaster::SendHealSpellLog(Unit const* pVictim, uint32 SpellID, uint32 Damage, bool critical) const
{
#if SUPPORTED_CLIENT_BUILD > CLIENT_BUILD_1_9_4
// we guess size
WorldPacket data(SMSG_SPELLHEALLOG, (8 + 8 + 4 + 4 + 1));
data << pVictim->GetPackGUID();
data << GetPackGUID();
data << uint32(SpellID);
data << uint32(Damage);
data << uint8(critical ? 1 : 0);
// data << uint8(0); // [-ZERO]
SendMessageToSet(&data, true);
#endif
}
void SpellCaster::EnergizeBySpell(Unit* pVictim, uint32 SpellID, uint32 Damage, Powers powertype)
{
SendEnergizeSpellLog(pVictim, SpellID, Damage, powertype);
// needs to be called after sending spell log
pVictim->ModifyPower(powertype, Damage);
}
void SpellCaster::SendEnergizeSpellLog(Unit const* pVictim, uint32 SpellID, uint32 Damage, Powers powertype) const
{
#if SUPPORTED_CLIENT_BUILD > CLIENT_BUILD_1_9_4
WorldPacket data(SMSG_SPELLENERGIZELOG, (8 + 8 + 4 + 4 + 4 + 1));
data << pVictim->GetPackGUID();
data << GetPackGUID();
data << uint32(SpellID);
data << uint32(powertype);
data << uint32(Damage);
SendMessageToSet(&data, true);
#endif
}
void SpellCaster::SendSpellNonMeleeDamageLog(SpellNonMeleeDamage* log) const
{
WorldPacket data(SMSG_SPELLNONMELEEDAMAGELOG, (16 + 4 + 4 + 1 + 4 + 4 + 1 + 1 + 4 + 4 + 1)); // we guess size
#if SUPPORTED_CLIENT_BUILD > CLIENT_BUILD_1_8_4
data << log->target->GetPackGUID();
data << log->attacker->GetPackGUID();
#else
data << log->target->GetGUID();
data << log->attacker->GetGUID();
#endif
data << uint32(log->SpellID);
data << uint32(log->damage); // damage amount
data << uint8(log->school); // damage school
data << uint32(log->absorb); // AbsorbedDamage
data << int32(log->resist); // resist
data << uint8(log->periodicLog); // if 1, then client show spell name (example: %s's ranged shot hit %s for %u school or %s suffers %u school damage from %s's spell_name
data << uint8(false); // unused
data << uint32(log->blocked); // blocked
data << uint32(log->HitInfo);
data << uint8(0); // flag to use extend data
SendMessageToSet(&data, true);
}
void SpellCaster::SendSpellNonMeleeDamageLog(Unit* target, uint32 spellId, uint32 damage, SpellSchoolMask damageSchoolMask, uint32 absorbedDamage, int32 resist, bool isPeriodic, uint32 blocked, bool criticalHit, bool split)
{
SpellNonMeleeDamage log(this, target, spellId, GetFirstSchoolInMask(damageSchoolMask));
log.damage = damage;
log.damage += (resist < 0 ? uint32(std::abs(resist)) : 0);
log.damage -= (absorbedDamage + (resist > 0 ? uint32(resist) : 0) + blocked);
log.absorb = absorbedDamage;
log.resist = resist;
log.periodicLog = isPeriodic;
log.blocked = blocked;
log.HitInfo = 0;
if (criticalHit)
log.HitInfo |= SPELL_HIT_TYPE_CRIT;
if (split)
log.HitInfo |= SPELL_HIT_TYPE_SPLIT;
SendSpellNonMeleeDamageLog(&log);
}
SpellSchoolMask SpellCaster::GetMeleeDamageSchoolMask() const
{
return SPELL_SCHOOL_MASK_NORMAL;
}
float SpellCaster::CalcArmorReducedDamage(Unit* pVictim, uint32 const damage) const
{
uint32 newdamage = 0;
float armor = (float)pVictim->GetArmor();
Unit const* pUnit = ToUnit();
// Ignore enemy armor by SPELL_AURA_MOD_TARGET_RESISTANCE aura
if (pUnit)
armor += pUnit->GetTotalAuraModifierByMiscMask(SPELL_AURA_MOD_TARGET_RESISTANCE, SPELL_SCHOOL_MASK_NORMAL);
if (armor < 0.0f)
armor = 0.0f;
float tmpvalue = 0.1f * armor / (8.5f * float(GetLevel()) + 40.0f);
tmpvalue = tmpvalue / (1.0f + tmpvalue);
if (tmpvalue < 0.0f)
tmpvalue = 0.0f;
if (tmpvalue > 0.75f)
tmpvalue = 0.75f;
newdamage = damage - (damage * tmpvalue);
return (newdamage > 1) ? newdamage : 1;
}
float SpellCaster::CalculateSpellEffectValue(Unit const* target, SpellEntry const* spellProto, SpellEffectIndex effect_index, int32 const* effBasePoints, Spell* spell) const
{
Unit const* pUnit = ToUnit();
Player const* pPlayer = ToPlayer();
uint8 comboPoints = pPlayer ? pPlayer->GetComboPoints() : 0;
int32 level = GetLevel();
if (level > (int32)spellProto->maxLevel && spellProto->maxLevel > 0)
level = (int32)spellProto->maxLevel;
else if (level < (int32)spellProto->baseLevel)
level = (int32)spellProto->baseLevel;
level -= (int32)spellProto->spellLevel;
int32 baseDice = int32(spellProto->EffectBaseDice[effect_index]);
float basePointsPerLevel = spellProto->EffectRealPointsPerLevel[effect_index];
float randomPointsPerLevel = spellProto->EffectDicePerLevel[effect_index];
float value = effBasePoints
? *effBasePoints - baseDice
: spellProto->EffectBasePoints[effect_index];
value += level * basePointsPerLevel;
int32 randomPoints = int32(spellProto->EffectDieSides[effect_index] + level * randomPointsPerLevel);
float comboDamage = spellProto->EffectPointsPerComboPoint[effect_index];
switch (randomPoints)
{
case 0:
case 1:
value += baseDice;
break; // range 1..1
default:
{
// range can have positive (1..rand) and negative (rand..1) values, so order its for irand
int32 randvalue = baseDice >= randomPoints
? irand(randomPoints, baseDice)
: irand(baseDice, randomPoints);
value += randvalue;
break;
}
}
// random damage
if (comboDamage != 0 && pPlayer && target && (target->GetObjectGuid() == pPlayer->GetComboTargetGuid()))
value += comboDamage * comboPoints;
if (pUnit)
{
if (Player* modOwner = pUnit->GetSpellModOwner())
{
modOwner->ApplySpellMod(spellProto->Id, SPELLMOD_ALL_EFFECTS, value, spell);
// Apply speed aura mods at cast time.
// Fixes Curse of Exhaustion not removing Amplify Curse.
switch (spellProto->EffectApplyAuraName[effect_index])
{
case SPELL_AURA_MOD_INCREASE_SPEED:
case SPELL_AURA_MOD_SPEED_ALWAYS:
case SPELL_AURA_MOD_SPEED_NOT_STACK:
case SPELL_AURA_MOD_DECREASE_SPEED:
modOwner->ApplySpellMod(spellProto->Id, SPELLMOD_SPEED, value, spell);
break;
}
}
}
if (spellProto->HasAttribute(SPELL_ATTR_SCALES_WITH_CREATURE_LEVEL) && spellProto->spellLevel &&
spellProto->Effect[effect_index] != SPELL_EFFECT_WEAPON_PERCENT_DAMAGE &&
spellProto->Effect[effect_index] != SPELL_EFFECT_KNOCK_BACK &&
(spellProto->Effect[effect_index] != SPELL_EFFECT_APPLY_AURA || spellProto->EffectApplyAuraName[effect_index] != SPELL_AURA_MOD_DECREASE_SPEED))
{
CreatureClassLevelStats const* pCLS = sObjectMgr.GetCreatureClassLevelStats(1, GetLevel());
float CLSPowerCreature = pCLS->melee_damage;
CreatureClassLevelStats const* spellCLS = sObjectMgr.GetCreatureClassLevelStats(1, spellProto->spellLevel);
float CLSPowerSpell = spellCLS->melee_damage;
value = value * (CLSPowerCreature / CLSPowerSpell);
}
return value;
}
void SpellCaster::CalculateSpellDamage(SpellNonMeleeDamage* damageInfo, float damage, SpellEntry const* spellInfo, SpellEffectIndex effectIndex, WeaponAttackType attackType, Spell* spell, bool crit)
{
SpellSchoolMask damageSchoolMask = GetSchoolMask(damageInfo->school);
Unit* pVictim = damageInfo->target;
if (!pVictim)
return;
if (damage < 0)
return;
if (!pVictim->IsAlive())
return;
// damage bonus (per damage class)
switch (spellInfo->DmgClass)
{
// Melee and Ranged Spells
case SPELL_DAMAGE_CLASS_RANGED:
case SPELL_DAMAGE_CLASS_MELEE:
{
//Calculate damage bonus
damage = MeleeDamageBonusDone(pVictim, damage, attackType, spellInfo, effectIndex, SPELL_DIRECT_DAMAGE, 1, spell);
damage = pVictim->MeleeDamageBonusTaken(this, damage, attackType, spellInfo, effectIndex, SPELL_DIRECT_DAMAGE, 1, spell);
// if crit add critical bonus
if (crit && !spellInfo->HasAttribute(SPELL_ATTR_EX3_IGNORE_CASTER_MODIFIERS))
{
damageInfo->HitInfo |= SPELL_HIT_TYPE_CRIT;
damage = SpellCriticalDamageBonus(spellInfo, damage, pVictim, spell);
}
break;
}
// Magical Attacks
case SPELL_DAMAGE_CLASS_NONE:
case SPELL_DAMAGE_CLASS_MAGIC:
{
// Calculate damage bonus
damage = SpellDamageBonusDone(pVictim, spellInfo, effectIndex, damage, SPELL_DIRECT_DAMAGE, 1, spell);
damage = pVictim->SpellDamageBonusTaken(this, spellInfo, effectIndex, damage, SPELL_DIRECT_DAMAGE, 1, spell);
// If crit add critical bonus
if (crit && !spellInfo->HasAttribute(SPELL_ATTR_EX3_IGNORE_CASTER_MODIFIERS))
{
damageInfo->HitInfo |= SPELL_HIT_TYPE_CRIT;
damage = SpellCriticalDamageBonus(spellInfo, damage, pVictim, spell);
}
break;