-
Notifications
You must be signed in to change notification settings - Fork 293
/
Unit.cpp
11700 lines (9999 loc) · 426 KB
/
Unit.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 file is part of the CMaNGOS Project. See AUTHORS file for Copyright information
*
* 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 "Unit.h"
#include "Log.h"
#include "Opcodes.h"
#include "WorldPacket.h"
#include "WorldSession.h"
#include "World.h"
#include "ObjectMgr.h"
#include "ObjectGuid.h"
#include "SpellMgr.h"
#include "Player.h"
#include "Creature.h"
#include "Spell.h"
#include "Group.h"
#include "SpellAuras.h"
#include "ObjectAccessor.h"
#include "AI/CreatureAI.h"
#include "TemporarySummon.h"
#include "Pet.h"
#include "Util.h"
#include "Totem.h"
#include "Vehicle.h"
#include "BattleGround/BattleGround.h"
#include "InstanceData.h"
#include "OutdoorPvP/OutdoorPvP.h"
#include "MapPersistentStateMgr.h"
#include "GridNotifiersImpl.h"
#include "CellImpl.h"
#include "MovementGenerator.h"
#include "movement/MoveSplineInit.h"
#include "movement/MoveSpline.h"
#include "CreatureLinkingMgr.h"
#include <math.h>
float baseMoveSpeed[MAX_MOVE_TYPE] =
{
2.5f, // MOVE_WALK
7.0f, // MOVE_RUN
4.5f, // MOVE_RUN_BACK
4.722222f, // MOVE_SWIM
2.5f, // MOVE_SWIM_BACK
3.141594f, // MOVE_TURN_RATE
7.0f, // MOVE_FLIGHT
4.5f, // MOVE_FLIGHT_BACK
3.14f // MOVE_PITCH_RATE
};
////////////////////////////////////////////////////////////
// Methods of class MovementInfo
void MovementInfo::Read(ByteBuffer& data)
{
data >> moveFlags;
data >> moveFlags2;
data >> time;
data >> pos.x;
data >> pos.y;
data >> pos.z;
data >> pos.o;
if (HasMovementFlag(MOVEFLAG_ONTRANSPORT))
{
data >> t_guid.ReadAsPacked();
data >> t_pos.x;
data >> t_pos.y;
data >> t_pos.z;
data >> t_pos.o;
data >> t_time;
data >> t_seat;
if (moveFlags2 & MOVEFLAG2_INTERP_MOVEMENT)
data >> t_time2;
}
if ((HasMovementFlag(MovementFlags(MOVEFLAG_SWIMMING | MOVEFLAG_FLYING))) || (moveFlags2 & MOVEFLAG2_ALLOW_PITCHING))
{
data >> s_pitch;
}
data >> fallTime;
if (HasMovementFlag(MOVEFLAG_FALLING))
{
data >> jump.velocity;
data >> jump.sinAngle;
data >> jump.cosAngle;
data >> jump.xyspeed;
}
if (HasMovementFlag(MOVEFLAG_SPLINE_ELEVATION))
{
data >> u_unk1;
}
}
void MovementInfo::Write(ByteBuffer& data) const
{
data << moveFlags;
data << moveFlags2;
data << time;
data << pos.x;
data << pos.y;
data << pos.z;
data << pos.o;
if (HasMovementFlag(MOVEFLAG_ONTRANSPORT))
{
data << t_guid.WriteAsPacked();
data << t_pos.x;
data << t_pos.y;
data << t_pos.z;
data << t_pos.o;
data << t_time;
data << t_seat;
if (moveFlags2 & MOVEFLAG2_INTERP_MOVEMENT)
data << t_time2;
}
if ((HasMovementFlag(MovementFlags(MOVEFLAG_SWIMMING | MOVEFLAG_FLYING))) || (moveFlags2 & MOVEFLAG2_ALLOW_PITCHING))
{
data << s_pitch;
}
data << fallTime;
if (HasMovementFlag(MOVEFLAG_FALLING))
{
data << jump.velocity;
data << jump.sinAngle;
data << jump.cosAngle;
data << jump.xyspeed;
}
if (HasMovementFlag(MOVEFLAG_SPLINE_ELEVATION))
{
data << u_unk1;
}
}
////////////////////////////////////////////////////////////
// Methods of class GlobalCooldownMgr
bool GlobalCooldownMgr::HasGlobalCooldown(SpellEntry const* spellInfo) const
{
GlobalCooldownList::const_iterator itr = m_GlobalCooldowns.find(spellInfo->StartRecoveryCategory);
return itr != m_GlobalCooldowns.end() && itr->second.duration && WorldTimer::getMSTimeDiff(itr->second.cast_time, WorldTimer::getMSTime()) < itr->second.duration;
}
void GlobalCooldownMgr::AddGlobalCooldown(SpellEntry const* spellInfo, uint32 gcd)
{
m_GlobalCooldowns[spellInfo->StartRecoveryCategory] = GlobalCooldown(gcd, WorldTimer::getMSTime());
}
void GlobalCooldownMgr::CancelGlobalCooldown(SpellEntry const* spellInfo)
{
m_GlobalCooldowns[spellInfo->StartRecoveryCategory].duration = 0;
}
////////////////////////////////////////////////////////////
// Methods of class Unit
Unit::Unit() :
movespline(new Movement::MoveSpline()),
m_charmInfo(nullptr),
i_motionMaster(this),
m_regenTimer(0),
m_vehicleInfo(nullptr),
m_combatData(new CombatData(this))
{
m_objectType |= TYPEMASK_UNIT;
m_objectTypeId = TYPEID_UNIT;
m_updateFlag = (UPDATEFLAG_HIGHGUID | UPDATEFLAG_LIVING | UPDATEFLAG_HAS_POSITION);
m_attackTimer[BASE_ATTACK] = 0;
m_attackTimer[OFF_ATTACK] = 0;
m_attackTimer[RANGED_ATTACK] = 0;
m_modAttackSpeedPct[BASE_ATTACK] = 1.0f;
m_modAttackSpeedPct[OFF_ATTACK] = 1.0f;
m_modAttackSpeedPct[RANGED_ATTACK] = 1.0f;
m_extraAttacks = 0;
m_state = 0;
m_deathState = ALIVE;
for (uint32 i = 0; i < CURRENT_MAX_SPELL; ++i)
m_currentSpells[i] = nullptr;
m_castCounter = 0;
// m_Aura = nullptr;
// m_AurasCheck = 2000;
// m_removeAuraTimer = 4;
m_spellAuraHoldersUpdateIterator = m_spellAuraHolders.end();
m_AuraFlags = 0;
m_Visibility = VISIBILITY_ON;
m_AINotifyScheduled = false;
m_detectInvisibilityMask = 0;
m_invisibilityMask = 0;
m_transform = 0;
m_canModifyStats = false;
for (int i = 0; i < MAX_SPELL_IMMUNITY; ++i)
m_spellImmune[i].clear();
for (int i = 0; i < UNIT_MOD_END; ++i)
{
m_auraModifiersGroup[i][BASE_VALUE] = 0.0f;
m_auraModifiersGroup[i][BASE_EXCLUSIVE] = 0.0f;
m_auraModifiersGroup[i][BASE_PCT] = 1.0f;
m_auraModifiersGroup[i][TOTAL_VALUE] = 0.0f;
m_auraModifiersGroup[i][TOTAL_PCT] = 1.0f;
}
// implement 50% base damage from offhand
m_auraModifiersGroup[UNIT_MOD_DAMAGE_OFFHAND][TOTAL_PCT] = 0.5f;
for (int i = 0; i < MAX_ATTACK; ++i)
{
m_weaponDamage[i][MINDAMAGE] = BASE_MINDAMAGE;
m_weaponDamage[i][MAXDAMAGE] = BASE_MAXDAMAGE;
}
for (int i = 0; i < MAX_STATS; ++i)
m_createStats[i] = 0.0f;
m_attacking = nullptr;
m_modMeleeHitChance = 0.0f;
m_modRangedHitChance = 0.0f;
m_modSpellHitChance = 0.0f;
m_baseSpellCritChance = 5;
m_CombatTimer = 0;
m_lastManaUseTimer = 0;
// m_victimThreat = 0.0f;
for (int i = 0; i < MAX_SPELL_SCHOOL; ++i)
m_threatModifier[i] = 1.0f;
m_isSorted = true;
for (int i = 0; i < MAX_MOVE_TYPE; ++i)
m_speed_rate[i] = 1.0f;
// remove aurastates allowing special moves
for (int i = 0; i < MAX_REACTIVE; ++i)
m_reactiveTimer[i] = 0;
m_isCreatureLinkingTrigger = false;
m_isSpawningLinked = false;
}
Unit::~Unit()
{
// set current spells as deletable
for (uint32 i = 0; i < CURRENT_MAX_SPELL; ++i)
{
if (m_currentSpells[i])
{
m_currentSpells[i]->SetReferencedFromCurrent(false);
m_currentSpells[i] = nullptr;
}
}
delete m_charmInfo;
delete m_vehicleInfo;
delete movespline;
delete m_combatData;
// those should be already removed at "RemoveFromWorld()" call
MANGOS_ASSERT(m_gameObj.size() == 0);
MANGOS_ASSERT(m_dynObjGUIDs.size() == 0);
MANGOS_ASSERT(m_deletedAuras.size() == 0);
MANGOS_ASSERT(m_deletedHolders.size() == 0);
}
void Unit::Update(uint32 update_diff, uint32 p_time)
{
if (!IsInWorld())
return;
/*if(p_time > m_AurasCheck)
{
m_AurasCheck = 2000;
_UpdateAura();
}else
m_AurasCheck -= p_time;*/
// WARNING! Order of execution here is important, do not change.
// Spells must be processed with event system BEFORE they go to _UpdateSpells.
// Or else we may have some SPELL_STATE_FINISHED spells stalled in pointers, that is bad.
m_Events.Update(update_diff);
_UpdateSpells(update_diff);
CleanupDeletedAuras();
if (m_lastManaUseTimer)
{
if (update_diff >= m_lastManaUseTimer)
m_lastManaUseTimer = 0;
else
m_lastManaUseTimer -= update_diff;
}
if (CanHaveThreatList())
getThreatManager().UpdateForClient(update_diff);
else if (isInCombat())
{
// update combat timer only for players and pets (they have no threat list)
// Check UNIT_STAT_MELEE_ATTACKING or UNIT_STAT_CHASE (without UNIT_STAT_FOLLOW in this case) so pets can reach far away
// targets without stopping half way there and running off.
// These flags are reset after target dies or another command is given.
if (getHostileRefManager().isEmpty())
{
// m_CombatTimer set at aura start and it will be freeze until aura removing
if (m_CombatTimer <= update_diff)
CombatStop();
else
m_CombatTimer -= update_diff;
}
}
if (uint32 base_att = getAttackTimer(BASE_ATTACK))
{
setAttackTimer(BASE_ATTACK, (update_diff >= base_att ? 0 : base_att - update_diff));
}
if (uint32 base_att = getAttackTimer(OFF_ATTACK))
{
setAttackTimer(OFF_ATTACK, (update_diff >= base_att ? 0 : base_att - update_diff));
}
if (IsVehicle())
{
// Initialize vehicle if not done
if (isAlive() && !m_vehicleInfo->IsInitialized())
m_vehicleInfo->Initialize();
// Update passenger positions if we are the first vehicle
if (!IsBoarded())
m_vehicleInfo->Update(update_diff);
}
// update abilities available only for fraction of time
UpdateReactives(update_diff);
if (isAlive())
{
ModifyAuraState(AURA_STATE_HEALTHLESS_20_PERCENT, GetHealth() < GetMaxHealth() * 0.20f);
ModifyAuraState(AURA_STATE_HEALTHLESS_35_PERCENT, GetHealth() < GetMaxHealth() * 0.35f);
ModifyAuraState(AURA_STATE_HEALTH_ABOVE_75_PERCENT, GetHealth() > GetMaxHealth() * 0.75f);
}
UpdateSplineMovement(p_time);
i_motionMaster.UpdateMotion(p_time);
}
bool Unit::UpdateMeleeAttackingState()
{
Unit* victim = getVictim();
if (!victim || IsNonMeleeSpellCasted(false))
return false;
if (!isAttackReady(BASE_ATTACK) && !(isAttackReady(OFF_ATTACK) && haveOffhandWeapon()))
return false;
uint8 swingError = 0;
if (!CanReachWithMeleeAttack(victim))
{
setAttackTimer(BASE_ATTACK, 100);
setAttackTimer(OFF_ATTACK, 100);
swingError = 1;
}
// 120 degrees of radiant range
else if (!HasInArc(2 * M_PI_F / 3, victim))
{
setAttackTimer(BASE_ATTACK, 100);
setAttackTimer(OFF_ATTACK, 100);
swingError = 2;
}
else
{
if (isAttackReady(BASE_ATTACK))
{
// prevent base and off attack in same time, delay attack at 0.2 sec
if (haveOffhandWeapon())
{
if (getAttackTimer(OFF_ATTACK) < ATTACK_DISPLAY_DELAY)
setAttackTimer(OFF_ATTACK, ATTACK_DISPLAY_DELAY);
}
AttackerStateUpdate(victim, BASE_ATTACK);
resetAttackTimer(BASE_ATTACK);
}
if (haveOffhandWeapon() && isAttackReady(OFF_ATTACK))
{
// prevent base and off attack in same time, delay attack at 0.2 sec
uint32 base_att = getAttackTimer(BASE_ATTACK);
if (base_att < ATTACK_DISPLAY_DELAY)
setAttackTimer(BASE_ATTACK, ATTACK_DISPLAY_DELAY);
// do attack
AttackerStateUpdate(victim, OFF_ATTACK);
resetAttackTimer(OFF_ATTACK);
}
}
Player* player = (GetTypeId() == TYPEID_PLAYER ? (Player*)this : nullptr);
if (player && swingError != player->LastSwingErrorMsg())
{
if (swingError == 1)
player->SendAttackSwingNotInRange();
else if (swingError == 2)
player->SendAttackSwingBadFacingAttack();
player->SwingErrorMsg(swingError);
}
return swingError;
}
bool Unit::haveOffhandWeapon() const
{
if (!CanUseEquippedWeapon(OFF_ATTACK))
return false;
if (GetTypeId() == TYPEID_PLAYER)
return !!((Player*)this)->GetWeaponForAttack(OFF_ATTACK, true, true);
else
{
uint32 ItemId = GetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_ID + 1);
ItemEntry const* itemInfo = sItemStore.LookupEntry(ItemId);
if (itemInfo && itemInfo->Class == ITEM_CLASS_WEAPON)
return true;
return false;
}
}
void Unit::SendHeartBeat()
{
m_movementInfo.UpdateTime(WorldTimer::getMSTime());
WorldPacket data(MSG_MOVE_HEARTBEAT, 64);
data << GetPackGUID();
data << m_movementInfo;
SendMessageToSet(&data, true);
}
void Unit::resetAttackTimer(WeaponAttackType type)
{
m_attackTimer[type] = uint32(GetAttackTime(type) * m_modAttackSpeedPct[type]);
}
float Unit::GetCombatReach(Unit const* pVictim, bool forMeleeRange /*=true*/, float flat_mod /*=0.0f*/) const
{
// The measured values show BASE_MELEE_OFFSET in (1.3224, 1.342)
float reach = GetFloatValue(UNIT_FIELD_COMBATREACH) + pVictim->GetFloatValue(UNIT_FIELD_COMBATREACH) +
BASE_MELEERANGE_OFFSET + flat_mod;
if (forMeleeRange && reach < ATTACK_DISTANCE)
reach = ATTACK_DISTANCE;
return reach;
}
float Unit::GetCombatDistance(Unit const* target, bool forMeleeRange) const
{
float radius = GetCombatReach(target, forMeleeRange);
float dx = GetPositionX() - target->GetPositionX();
float dy = GetPositionY() - target->GetPositionY();
float dz = GetPositionZ() - target->GetPositionZ();
float dist = sqrt((dx * dx) + (dy * dy) + (dz * dz)) - radius;
return (dist > 0.0f ? dist : 0.0f);
}
bool Unit::CanReachWithMeleeAttack(Unit const* pVictim, float flat_mod /*= 0.0f*/) const
{
MANGOS_ASSERT(pVictim);
float reach = GetCombatReach(pVictim, true, flat_mod);
// This check is not related to bounding radius
float dx = GetPositionX() - pVictim->GetPositionX();
float dy = GetPositionY() - pVictim->GetPositionY();
float dz = GetPositionZ() - pVictim->GetPositionZ();
return dx * dx + dy * dy + dz * dz < reach * reach;
}
void Unit::RemoveSpellsCausingAura(AuraType auraType)
{
for (AuraList::const_iterator iter = m_modAuras[auraType].begin(); iter != m_modAuras[auraType].end();)
{
RemoveAurasDueToSpell((*iter)->GetId());
iter = m_modAuras[auraType].begin();
}
}
void Unit::RemoveSpellsCausingAura(AuraType auraType, SpellAuraHolder* except)
{
for (AuraList::const_iterator iter = m_modAuras[auraType].begin(); iter != m_modAuras[auraType].end();)
{
// skip `except` aura
if ((*iter)->GetHolder() == except)
{
++iter;
continue;
}
RemoveAurasDueToSpell((*iter)->GetId(), except);
iter = m_modAuras[auraType].begin();
}
}
void Unit::RemoveSpellsCausingAura(AuraType auraType, ObjectGuid casterGuid)
{
for (AuraList::const_iterator iter = m_modAuras[auraType].begin(); iter != m_modAuras[auraType].end();)
{
if ((*iter)->GetCasterGuid() == casterGuid)
{
RemoveSpellAuraHolder((*iter)->GetHolder());
iter = m_modAuras[auraType].begin();
}
else
++iter;
}
}
void Unit::DealDamageMods(Unit* pVictim, uint32& damage, uint32* absorb)
{
if (!pVictim->isAlive() || pVictim->IsTaxiFlying() || (pVictim->GetTypeId() == TYPEID_UNIT && ((Creature*)pVictim)->IsInEvadeMode()))
{
if (absorb)
*absorb += damage;
damage = 0;
return;
}
// You don't lose health from damage taken from another player while in a sanctuary
// You still see it in the combat log though
if (!IsAllowedDamageInArea(pVictim))
{
if (absorb)
*absorb += damage;
damage = 0;
}
uint32 originalDamage = damage;
// Script Event damage Deal
if (GetTypeId() == TYPEID_UNIT && ((Creature*)this)->AI())
((Creature*)this)->AI()->DamageDeal(pVictim, damage);
// Script Event damage taken
if (pVictim->GetTypeId() == TYPEID_UNIT && ((Creature*)pVictim)->AI())
((Creature*)pVictim)->AI()->DamageTaken(this, damage);
if (absorb && originalDamage > damage)
*absorb += (originalDamage - damage);
}
uint32 Unit::DealDamage(Unit* pVictim, uint32 damage, CleanDamage const* cleanDamage, DamageEffectType damagetype, SpellSchoolMask damageSchoolMask, SpellEntry const* spellProto, bool durabilityLoss)
{
// remove affects from attacker at any non-DoT damage (including 0 damage)
if (damagetype != DOT)
{
if (damagetype != SELF_DAMAGE_ROGUE_FALL)
RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
if (pVictim->GetTypeId() == TYPEID_PLAYER && !pVictim->IsStandState() && !pVictim->hasUnitState(UNIT_STAT_STUNNED))
pVictim->SetStandState(UNIT_STAND_STATE_STAND);
}
if (!damage)
{
// Rage from physical damage received .
if (cleanDamage && cleanDamage->damage && (damageSchoolMask & SPELL_SCHOOL_MASK_NORMAL) && pVictim->GetTypeId() == TYPEID_PLAYER && (pVictim->GetPowerType() == POWER_RAGE))
((Player*)pVictim)->RewardRage(cleanDamage->damage, 0, false);
return 0;
}
DEBUG_FILTER_LOG(LOG_FILTER_DAMAGE, "DealDamageStart");
uint32 health = pVictim->GetHealth();
DEBUG_FILTER_LOG(LOG_FILTER_DAMAGE, "deal dmg:%d to health:%d ", damage, health);
// Rage from Damage made (only from direct weapon damage)
if (cleanDamage && damagetype == DIRECT_DAMAGE && this != pVictim && GetTypeId() == TYPEID_PLAYER && (GetPowerType() == POWER_RAGE))
{
uint32 weaponSpeedHitFactor;
switch (cleanDamage->attackType)
{
case BASE_ATTACK:
{
if (cleanDamage->hitOutCome == MELEE_HIT_CRIT)
weaponSpeedHitFactor = uint32(GetAttackTime(cleanDamage->attackType) / 1000.0f * 7);
else
weaponSpeedHitFactor = uint32(GetAttackTime(cleanDamage->attackType) / 1000.0f * 3.5f);
((Player*)this)->RewardRage(damage, weaponSpeedHitFactor, true);
break;
}
case OFF_ATTACK:
{
if (cleanDamage->hitOutCome == MELEE_HIT_CRIT)
weaponSpeedHitFactor = uint32(GetAttackTime(cleanDamage->attackType) / 1000.0f * 3.5f);
else
weaponSpeedHitFactor = uint32(GetAttackTime(cleanDamage->attackType) / 1000.0f * 1.75f);
((Player*)this)->RewardRage(damage, weaponSpeedHitFactor, true);
break;
}
case RANGED_ATTACK:
break;
}
}
// no xp,health if type 8 /critters/
if (pVictim->GetTypeId() == TYPEID_UNIT && pVictim->GetCreatureType() == CREATURE_TYPE_CRITTER)
{
// TODO: fix this part
// Critter may not die of damage taken, instead expect it to run away (no fighting back)
// If (this) is TYPEID_PLAYER, (this) will enter combat w/victim, but after some time, automatically leave combat.
// It is unclear how it should work for other cases.
DEBUG_FILTER_LOG(LOG_FILTER_DAMAGE, "DealDamage critter, critter dies");
((Creature*)pVictim)->SetLootRecipient(this);
JustKilledCreature((Creature*)pVictim, nullptr);
pVictim->SetHealth(0);
return damage;
}
// share damage by auras
AuraList const& vShareDamageAuras = pVictim->GetAurasByType(SPELL_AURA_SHARE_DAMAGE_PCT);
for (AuraList::const_iterator itr = vShareDamageAuras.begin(); itr != vShareDamageAuras.end(); ++itr)
{
// if damage is done by another shared aura, then skip to avoid circular reference (aura 300 is only applied on effect_idx_0
if (spellProto && spellProto->Effect[EFFECT_INDEX_0] == SPELL_EFFECT_APPLY_AURA &&
spellProto->EffectApplyAuraName[EFFECT_INDEX_0] == SPELL_AURA_SHARE_DAMAGE_PCT)
break;
if (Unit* shareTarget = (*itr)->GetCaster())
{
if (shareTarget != pVictim && ((*itr)->GetMiscValue() & damageSchoolMask))
{
SpellEntry const* shareSpell = (*itr)->GetSpellProto();
uint32 shareDamage = uint32(damage*(*itr)->GetModifier()->m_amount / 100.0f);
DealDamageMods(shareTarget, shareDamage, nullptr);
DealDamage(shareTarget, shareDamage, nullptr, damagetype, GetSpellSchoolMask(shareSpell), shareSpell, false);
}
}
}
// duel ends when player has 1 or less hp
bool duel_hasEnded = false;
if (pVictim->GetTypeId() == TYPEID_PLAYER && ((Player*)pVictim)->duel && damage >= (health - 1))
{
// prevent kill only if killed in duel and killed by opponent or opponent controlled creature
if (((Player*)pVictim)->duel->opponent == this || ((Player*)pVictim)->duel->opponent->GetObjectGuid() == GetOwnerGuid())
damage = health - 1;
duel_hasEnded = true;
}
// Get in CombatState
if (pVictim != this && damagetype != DOT && (!spellProto || !spellProto->HasAttribute(SPELL_ATTR_EX3_NO_INITIAL_AGGRO)))
{
SetInCombatWith(pVictim);
pVictim->SetInCombatWith(this);
if (Player* attackedPlayer = pVictim->GetCharmerOrOwnerPlayerOrPlayerItself())
SetContestedPvP(attackedPlayer);
}
if (GetTypeId() == TYPEID_PLAYER && this != pVictim)
{
Player* killer = ((Player*)this);
// in bg, count dmg if victim is also a player
if (pVictim->GetTypeId() == TYPEID_PLAYER)
{
if (BattleGround* bg = killer->GetBattleGround())
{
// FIXME: kept by compatibility. don't know in BG if the restriction apply.
bg->UpdatePlayerScore(killer, SCORE_DAMAGE_DONE, damage);
}
}
killer->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_DAMAGE_DONE, damage, 0, pVictim);
killer->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_HIGHEST_HIT_DEALT, damage);
}
if (pVictim->GetTypeId() == TYPEID_PLAYER)
((Player*)pVictim)->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_HIGHEST_HIT_RECEIVED, damage);
if (pVictim->GetTypeId() == TYPEID_UNIT && !((Creature*)pVictim)->IsPet() && !((Creature*)pVictim)->HasLootRecipient())
((Creature*)pVictim)->SetLootRecipient(this);
if (health <= damage)
{
DEBUG_FILTER_LOG(LOG_FILTER_DAMAGE, "DealDamage %s Killed %s", GetGuidStr().c_str(), pVictim->GetGuidStr().c_str());
/*
* Preparation: Who gets credit for killing whom, invoke SpiritOfRedemtion?
*/
// for loot will be used only if group_tap == nullptr
Player* player_tap = GetCharmerOrOwnerPlayerOrPlayerItself();
Group* group_tap = nullptr;
// in creature kill case group/player tap stored for creature
if (pVictim->GetTypeId() == TYPEID_UNIT)
{
group_tap = ((Creature*)pVictim)->GetGroupLootRecipient();
if (Player* recipient = ((Creature*)pVictim)->GetOriginalLootRecipient())
player_tap = recipient;
}
// in player kill case group tap selected by player_tap (killer-player itself, or charmer, or owner, etc)
else
{
if (player_tap)
group_tap = player_tap->GetGroup();
}
// Spirit of Redemtion Talent
bool damageFromSpiritOfRedemtionTalent = spellProto && spellProto->Id == 27795;
// if talent known but not triggered (check priest class for speedup check)
Aura* spiritOfRedemtionTalentReady = nullptr;
if (!damageFromSpiritOfRedemtionTalent && // not called from SPELL_AURA_SPIRIT_OF_REDEMPTION
pVictim->GetTypeId() == TYPEID_PLAYER && pVictim->getClass() == CLASS_PRIEST)
{
AuraList const& vDummyAuras = pVictim->GetAurasByType(SPELL_AURA_DUMMY);
for (AuraList::const_iterator itr = vDummyAuras.begin(); itr != vDummyAuras.end(); ++itr)
{
if ((*itr)->GetSpellProto()->SpellIconID == 1654)
{
spiritOfRedemtionTalentReady = *itr;
break;
}
}
}
/*
* Generic Actions (ProcEvents, Combat-Log, Kill Rewards, Stop Combat)
*/
// call kill spell proc event (before real die and combat stop to triggering auras removed at death/combat stop)
if (player_tap && player_tap != pVictim)
{
player_tap->ProcDamageAndSpell(pVictim, PROC_FLAG_KILL, PROC_FLAG_KILLED, PROC_EX_NONE, 0);
WorldPacket data(SMSG_PARTYKILLLOG, (8 + 8)); // send event PARTY_KILL
data << player_tap->GetObjectGuid(); // player with killing blow
data << pVictim->GetObjectGuid(); // victim
if (group_tap)
group_tap->BroadcastPacket(&data, false, group_tap->GetMemberGroup(player_tap->GetObjectGuid()), player_tap->GetObjectGuid());
player_tap->SendDirectMessage(&data);
}
else if (GetTypeId() == TYPEID_UNIT && this != pVictim)
ProcDamageAndSpell(pVictim, PROC_FLAG_KILL, PROC_FLAG_KILLED, PROC_EX_NONE, 0);
// Reward player, his pets, and group/raid members
if (player_tap != pVictim)
{
if (group_tap)
group_tap->RewardGroupAtKill(pVictim, player_tap);
else if (player_tap)
player_tap->RewardSinglePlayerAtKill(pVictim);
}
/*
* Actions for the killer
*/
if (spiritOfRedemtionTalentReady)
{
DEBUG_FILTER_LOG(LOG_FILTER_DAMAGE, "DealDamage: Spirit of Redemtion ready");
// save value before aura remove
uint32 ressSpellId = pVictim->GetUInt32Value(PLAYER_SELF_RES_SPELL);
if (!ressSpellId)
ressSpellId = ((Player*)pVictim)->GetResurrectionSpellId();
// Remove all expected to remove at death auras (most important negative case like DoT or periodic triggers)
pVictim->RemoveAllAurasOnDeath();
// restore for use at real death
pVictim->SetUInt32Value(PLAYER_SELF_RES_SPELL, ressSpellId);
// FORM_SPIRITOFREDEMPTION and related auras
pVictim->CastSpell(pVictim, 27827, true, nullptr, spiritOfRedemtionTalentReady);
}
else
pVictim->SetHealth(0);
// Call KilledUnit for creatures
if (GetTypeId() == TYPEID_UNIT && ((Creature*)this)->AI())
((Creature*)this)->AI()->KilledUnit(pVictim);
// Call AI OwnerKilledUnit (for any current summoned minipet/guardian/protector)
PetOwnerKilledUnit(pVictim);
/*
* Actions for the victim
*/
if (pVictim->GetTypeId() == TYPEID_PLAYER) // Killed player
{
Player* playerVictim = (Player*)pVictim;
// remember victim PvP death for corpse type and corpse reclaim delay
// at original death (not at SpiritOfRedemtionTalent timeout)
if (!damageFromSpiritOfRedemtionTalent)
playerVictim->SetPvPDeath(player_tap != nullptr);
// achievement stuff
playerVictim->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_TOTAL_DAMAGE_RECEIVED, health);
if (player_tap)
player_tap->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_SPECIAL_PVP_KILL, 1, 0, pVictim);
if (GetTypeId() == TYPEID_UNIT)
playerVictim->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_KILLED_BY_CREATURE, GetEntry());
else if (GetTypeId() == TYPEID_PLAYER && pVictim != this)
playerVictim->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_KILLED_BY_PLAYER, 1, playerVictim->GetTeam());
// 10% durability loss on death
// only if not player and not controlled by player pet. And not at BG
if (durabilityLoss && !player_tap && !playerVictim->InBattleGround())
{
DEBUG_LOG("DealDamage: Killed %s, looing 10 percents durability", pVictim->GetGuidStr().c_str());
playerVictim->DurabilityLossAll(0.10f, false);
// durability lost message
WorldPacket data(SMSG_DURABILITY_DAMAGE_DEATH, 0);
playerVictim->GetSession()->SendPacket(&data);
}
if (!spiritOfRedemtionTalentReady) // Before informing Battleground
{
DEBUG_FILTER_LOG(LOG_FILTER_DAMAGE, "SET JUST_DIED");
pVictim->SetDeathState(JUST_DIED);
}
// playerVictim was in duel, duel must be interrupted
// last damage from non duel opponent or non opponent controlled creature
if (duel_hasEnded)
{
playerVictim->duel->opponent->CombatStopWithPets(true);
playerVictim->CombatStopWithPets(true);
playerVictim->DuelComplete(DUEL_INTERRUPTED);
}
if (player_tap) // PvP kill
{
if (BattleGround* bg = playerVictim->GetBattleGround())
{
bg->HandleKillPlayer(playerVictim, player_tap);
}
else if (pVictim != this)
{
// selfkills are not handled in outdoor pvp scripts
if (OutdoorPvP* outdoorPvP = sOutdoorPvPMgr.GetScript(playerVictim->GetCachedZoneId()))
outdoorPvP->HandlePlayerKill(player_tap, playerVictim);
}
}
}
else // Killed creature
JustKilledCreature((Creature*)pVictim, player_tap);
// stop combat
DEBUG_FILTER_LOG(LOG_FILTER_DAMAGE, "DealDamageAttackStop");
pVictim->CombatStop();
pVictim->getHostileRefManager().deleteReferences();
}
else // if (health <= damage)
{
DEBUG_FILTER_LOG(LOG_FILTER_DAMAGE, "DealDamageAlive");
if (pVictim->GetTypeId() == TYPEID_PLAYER)
((Player*)pVictim)->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_TOTAL_DAMAGE_RECEIVED, damage);
pVictim->ModifyHealth(- (int32)damage);
if (damagetype != DOT)
{
if (!getVictim())
{
// if not have main target then attack state with target (including AI call)
// start melee attacks only after melee hit
Attack(pVictim, (damagetype == DIRECT_DAMAGE));
}
// if damage pVictim call AI reaction
pVictim->AttackedBy(this);
}
if (pVictim->GetTypeId() != TYPEID_PLAYER)
{
float threat = damage * sSpellMgr.GetSpellThreatMultiplier(spellProto);
pVictim->AddThreat(this, threat, (cleanDamage && cleanDamage->hitOutCome == MELEE_HIT_CRIT), damageSchoolMask, spellProto);
}
else // victim is a player
{
// Rage from damage received
if (this != pVictim && pVictim->GetPowerType() == POWER_RAGE)
{
uint32 rage_damage = damage + (cleanDamage ? cleanDamage->damage : 0);
((Player*)pVictim)->RewardRage(rage_damage, 0, false);
}
// random durability for items (HIT TAKEN)
if (roll_chance_f(sWorld.getConfig(CONFIG_FLOAT_RATE_DURABILITY_LOSS_DAMAGE)))
{
EquipmentSlots slot = EquipmentSlots(urand(0, EQUIPMENT_SLOT_END - 1));
((Player*)pVictim)->DurabilityPointLossForEquipSlot(slot);
}
}
if (GetTypeId() == TYPEID_PLAYER)
{
// random durability for items (HIT DONE)
if (roll_chance_f(sWorld.getConfig(CONFIG_FLOAT_RATE_DURABILITY_LOSS_DAMAGE)))
{
EquipmentSlots slot = EquipmentSlots(urand(0, EQUIPMENT_SLOT_END - 1));
((Player*)this)->DurabilityPointLossForEquipSlot(slot);
}
}
if (damagetype != NODAMAGE && damage && pVictim->GetTypeId() == TYPEID_PLAYER)
{
if (damagetype != DOT)
{
for (uint32 i = CURRENT_FIRST_NON_MELEE_SPELL; i < CURRENT_MAX_SPELL; ++i)
{
// skip channeled spell (processed differently below)
if (i == CURRENT_CHANNELED_SPELL)
continue;
if (Spell* spell = pVictim->GetCurrentSpell(CurrentSpellTypes(i)))
{
if (spell->getState() == SPELL_STATE_PREPARING)
{
if (spell->m_spellInfo->InterruptFlags & SPELL_INTERRUPT_FLAG_ABORT_ON_DMG)
pVictim->InterruptSpell(CurrentSpellTypes(i));
else
spell->Delayed();
}
}
}
}
if (Spell* spell = pVictim->m_currentSpells[CURRENT_CHANNELED_SPELL])
{
if (spell->getState() == SPELL_STATE_CASTING)
{
uint32 channelInterruptFlags = spell->m_spellInfo->ChannelInterruptFlags;
if (channelInterruptFlags & CHANNEL_FLAG_DELAY)
{
if (pVictim != this) // don't shorten the duration of channeling if you damage yourself
spell->DelayedChannel();
}
else if ((channelInterruptFlags & (CHANNEL_FLAG_DAMAGE | CHANNEL_FLAG_DAMAGE2)))
{
DETAIL_LOG("Spell %u canceled at damage!", spell->m_spellInfo->Id);
pVictim->InterruptSpell(CURRENT_CHANNELED_SPELL);
}
}
else if (spell->getState() == SPELL_STATE_DELAYED)
// break channeled spell in delayed state on damage
{
DETAIL_LOG("Spell %u canceled at damage!", spell->m_spellInfo->Id);
pVictim->InterruptSpell(CURRENT_CHANNELED_SPELL);
}
}
}
// last damage from duel opponent
if (duel_hasEnded)