/
SpellAuras.cpp
2665 lines (2269 loc) · 95 KB
/
SpellAuras.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 TrinityCore 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, see <http://www.gnu.org/licenses/>.
*/
#include "Common.h"
#include "CellImpl.h"
#include "Containers.h"
#include "DynamicObject.h"
#include "GridNotifiersImpl.h"
#include "Item.h"
#include "ListUtils.h"
#include "Log.h"
#include "ObjectAccessor.h"
#include "ObjectMgr.h"
#include "PhasingHandler.h"
#include "Player.h"
#include "ScriptMgr.h"
#include "Spell.h"
#include "SpellAuraEffects.h"
#include "SpellHistory.h"
#include "SpellMgr.h"
#include "SpellPackets.h"
#include "SpellScript.h"
#include "Unit.h"
#include "Util.h"
#include "Vehicle.h"
#include "World.h"
#include <sstream>
class ChargeDropEvent : public BasicEvent
{
public:
ChargeDropEvent(Aura* base, AuraRemoveMode mode) : _base(base), _mode(mode) { }
bool Execute(uint64 /*e_time*/, uint32 /*p_time*/) override
{
// _base is always valid (look in Aura::_Remove())
_base->ModChargesDelayed(-1, _mode);
return true;
}
private:
Aura* _base;
AuraRemoveMode _mode;
};
AuraCreateInfo::AuraCreateInfo(ObjectGuid castId, SpellInfo const* spellInfo, Difficulty castDifficulty, uint32 auraEffMask, WorldObject* owner) :
_castId(castId), _spellInfo(spellInfo), _castDifficulty(castDifficulty), _auraEffectMask(auraEffMask), _owner(owner)
{
ASSERT(spellInfo);
ASSERT(auraEffMask);
ASSERT(owner);
ASSERT(auraEffMask <= MAX_EFFECT_MASK);
}
AuraApplication::AuraApplication(Unit* target, Unit* caster, Aura* aura, uint32 effMask) :
_target(target), _base(aura), _removeMode(AURA_REMOVE_NONE), _slot(MAX_AURAS),
_flags(AFLAG_NONE), _effectsToApply(effMask), _needClientUpdate(false), _effectMask(0)
{
ASSERT(GetTarget() && GetBase());
// Try find slot for aura
uint8 slot = 0;
// lookup for free slots in units visibleAuras
for (AuraApplication* visibleAura : GetTarget()->GetVisibleAuras())
{
if (slot < visibleAura->GetSlot())
break;
++slot;
}
// Register Visible Aura
if (slot < MAX_AURAS)
{
_slot = slot;
GetTarget()->SetVisibleAura(this);
_needClientUpdate = true;
TC_LOG_DEBUG("spells", "Aura: {} Effect: {} put to unit visible auras slot: {}", GetBase()->GetId(), GetEffectMask(), slot);
}
else
TC_LOG_ERROR("spells", "Aura: {} Effect: {} could not find empty unit visible slot", GetBase()->GetId(), GetEffectMask());
_InitFlags(caster, effMask);
}
void AuraApplication::_Remove()
{
// update for out of range group members
if (GetSlot() < MAX_AURAS)
{
GetTarget()->RemoveVisibleAura(this);
ClientUpdate(true);
}
}
void AuraApplication::_InitFlags(Unit* caster, uint32 effMask)
{
// mark as selfcast if needed
_flags |= (GetBase()->GetCasterGUID() == GetTarget()->GetGUID()) ? AFLAG_NOCASTER : AFLAG_NONE;
// aura is cast by self or an enemy
// one negative effect and we know aura is negative
if (IsSelfcast() || !caster || !caster->IsFriendlyTo(GetTarget()))
{
bool negativeFound = false;
for (uint8 i = 0; i < GetBase()->GetSpellInfo()->GetEffects().size(); ++i)
{
if (((1 << i) & effMask) && !GetBase()->GetSpellInfo()->IsPositiveEffect(i))
{
negativeFound = true;
break;
}
}
_flags |= negativeFound ? AFLAG_NEGATIVE : AFLAG_POSITIVE;
}
// aura is cast by friend
// one positive effect and we know aura is positive
else
{
bool positiveFound = false;
for (uint8 i = 0; i < GetBase()->GetSpellInfo()->GetEffects().size(); ++i)
{
if (((1 << i) & effMask) && GetBase()->GetSpellInfo()->IsPositiveEffect(i))
{
positiveFound = true;
break;
}
}
_flags |= positiveFound ? AFLAG_POSITIVE : AFLAG_NEGATIVE;
}
auto effectNeedsAmount = [this](AuraEffect const* effect)
{
return effect && (GetEffectsToApply() & (1 << effect->GetEffIndex())) && Aura::EffectTypeNeedsSendingAmount(effect->GetAuraType());
};
if (GetBase()->GetSpellInfo()->HasAttribute(SPELL_ATTR8_AURA_POINTS_ON_CLIENT)
|| std::find_if(GetBase()->GetAuraEffects().begin(), GetBase()->GetAuraEffects().end(), std::cref(effectNeedsAmount)) != GetBase()->GetAuraEffects().end())
_flags |= AFLAG_SCALABLE;
if ((_flags & AFLAG_POSITIVE))
if (!GetBase()->GetSpellInfo()->IsPassive() && !GetBase()->GetSpellInfo()->HasAttribute(SPELL_ATTR1_NO_AURA_ICON) && GetBase()->GetOwner()->GetGUID() == GetTarget()->GetGUID())
_flags |= AFLAG_CANCELABLE;
if (GetBase()->GetSpellInfo()->IsPassive())
_flags |= AFLAG_PASSIVE;
}
void AuraApplication::_HandleEffect(uint8 effIndex, bool apply)
{
AuraEffect* aurEff = GetBase()->GetEffect(effIndex);
if (!aurEff)
{
TC_LOG_ERROR("spells", "Aura {} has no effect at effectIndex {} but _HandleEffect was called", GetBase()->GetSpellInfo()->Id, uint32(effIndex));
return;
}
ASSERT(aurEff);
ASSERT(HasEffect(effIndex) == (!apply));
ASSERT((1<<effIndex) & _effectsToApply);
TC_LOG_DEBUG("spells", "AuraApplication::_HandleEffect: {}, apply: {}: amount: {}", aurEff->GetAuraType(), apply, aurEff->GetAmount());
if (apply)
{
ASSERT(!(_effectMask & (1<<effIndex)));
_effectMask |= 1<<effIndex;
aurEff->HandleEffect(this, AURA_EFFECT_HANDLE_REAL, true);
}
else
{
ASSERT(_effectMask & (1<<effIndex));
_effectMask &= ~(1<<effIndex);
aurEff->HandleEffect(this, AURA_EFFECT_HANDLE_REAL, false);
}
SetNeedClientUpdate();
}
void AuraApplication::UpdateApplyEffectMask(uint32 newEffMask, bool canHandleNewEffects)
{
if (_effectsToApply == newEffMask)
return;
uint32 removeEffMask = (_effectsToApply ^ newEffMask) & (~newEffMask);
uint32 addEffMask = (_effectsToApply ^ newEffMask) & (~_effectsToApply);
// quick check, removes application completely
if (removeEffMask == _effectsToApply && !addEffMask)
{
_target->_UnapplyAura(this, AURA_REMOVE_BY_DEFAULT);
return;
}
// update real effects only if they were applied already
for (uint8 i = 0; i < MAX_SPELL_EFFECTS; ++i)
if (HasEffect(i) && (removeEffMask & (1 << i)))
_HandleEffect(i, false);
_effectsToApply = newEffMask;
if (canHandleNewEffects)
for (uint8 i = 0; i < MAX_SPELL_EFFECTS; ++i)
if (addEffMask & (1 << i))
_HandleEffect(i, true);
}
void AuraApplication::SetNeedClientUpdate()
{
if (_needClientUpdate || GetRemoveMode() != AURA_REMOVE_NONE)
return;
_needClientUpdate = true;
_target->SetVisibleAuraUpdate(this);
}
void AuraApplication::BuildUpdatePacket(WorldPackets::Spells::AuraInfo& auraInfo, bool remove)
{
ASSERT(_target->HasVisibleAura(this) != remove);
auraInfo.Slot = GetSlot();
if (remove)
return;
Aura const* aura = GetBase();
WorldPackets::Spells::AuraDataInfo& auraData = auraInfo.AuraData.emplace();
auraData.CastID = aura->GetCastId();
auraData.SpellID = aura->GetId();
auraData.Visual = aura->GetSpellVisual();
auraData.Flags = GetFlags();
if (aura->GetType() != DYNOBJ_AURA_TYPE && aura->GetMaxDuration() > 0 && !aura->GetSpellInfo()->HasAttribute(SPELL_ATTR5_DO_NOT_DISPLAY_DURATION))
auraData.Flags |= AFLAG_DURATION;
auraData.ActiveFlags = GetEffectMask();
if (!aura->GetSpellInfo()->HasAttribute(SPELL_ATTR11_SCALES_WITH_ITEM_LEVEL))
auraData.CastLevel = aura->GetCasterLevel();
else
auraData.CastLevel = uint16(aura->GetCastItemLevel());
// send stack amount for aura which could be stacked (never 0 - causes incorrect display) or charges
// stack amount has priority over charges (checked on retail with spell 50262)
auraData.Applications = aura->IsUsingStacks() ? aura->GetStackAmount() : aura->GetCharges();
if (!aura->GetCasterGUID().IsUnit())
auraData.CastUnit = ObjectGuid::Empty; // optional data is filled in, but cast unit contains empty guid in packet
else if (!(auraData.Flags & AFLAG_NOCASTER))
auraData.CastUnit = aura->GetCasterGUID();
if (auraData.Flags & AFLAG_DURATION)
{
auraData.Duration = aura->GetMaxDuration();
auraData.Remaining = aura->GetDuration();
}
if (auraData.Flags & AFLAG_SCALABLE)
{
auraData.Points.reserve(aura->GetAuraEffects().size());
bool hasEstimatedAmounts = false;
for (AuraEffect const* effect : GetBase()->GetAuraEffects())
{
if (effect && HasEffect(effect->GetEffIndex())) // Not all of aura's effects have to be applied on every target
{
Trinity::Containers::EnsureWritableVectorIndex(auraData.Points, effect->GetEffIndex()) = float(effect->GetAmount());
if (effect->GetEstimatedAmount())
hasEstimatedAmounts = true;
}
}
if (hasEstimatedAmounts)
{
// When sending EstimatedPoints all effects (at least up to the last one that uses GetEstimatedAmount) must have proper value in packet
auraData.EstimatedPoints.resize(auraData.Points.size());
for (AuraEffect const* effect : GetBase()->GetAuraEffects())
if (effect && HasEffect(effect->GetEffIndex())) // Not all of aura's effects have to be applied on every target
auraData.EstimatedPoints[effect->GetEffIndex()] = effect->GetEstimatedAmount().value_or(effect->GetAmount());
}
}
}
void AuraApplication::ClientUpdate(bool remove)
{
_needClientUpdate = false;
WorldPackets::Spells::AuraUpdate update;
update.UpdateAll = false;
update.UnitGUID = GetTarget()->GetGUID();
WorldPackets::Spells::AuraInfo auraInfo;
BuildUpdatePacket(auraInfo, remove);
update.Auras.push_back(auraInfo);
_target->SendMessageToSet(update.Write(), true);
}
std::string AuraApplication::GetDebugInfo() const
{
std::stringstream sstr;
sstr << "Base: " << (GetBase() ? GetBase()->GetDebugInfo() : "NULL")
<< "\nTarget: " << (GetTarget() ? GetTarget()->GetDebugInfo() : "NULL");
return sstr.str();
}
uint32 Aura::BuildEffectMaskForOwner(SpellInfo const* spellProto, uint32 availableEffectMask, WorldObject* owner)
{
ASSERT_NODEBUGINFO(spellProto);
ASSERT_NODEBUGINFO(owner);
uint32 effMask = 0;
switch (owner->GetTypeId())
{
case TYPEID_UNIT:
case TYPEID_PLAYER:
for (SpellEffectInfo const& spellEffectInfo : spellProto->GetEffects())
{
if (spellEffectInfo.IsUnitOwnedAuraEffect())
effMask |= 1 << spellEffectInfo.EffectIndex;
}
break;
case TYPEID_DYNAMICOBJECT:
for (SpellEffectInfo const& spellEffectInfo : spellProto->GetEffects())
{
if (spellEffectInfo.IsEffect(SPELL_EFFECT_PERSISTENT_AREA_AURA))
effMask |= 1 << spellEffectInfo.EffectIndex;
}
break;
default:
ABORT();
break;
}
return effMask & availableEffectMask;
}
Aura* Aura::TryRefreshStackOrCreate(AuraCreateInfo& createInfo, bool updateEffectMask)
{
ASSERT_NODEBUGINFO(createInfo.Caster || !createInfo.CasterGUID.IsEmpty());
if (createInfo.IsRefresh)
*createInfo.IsRefresh = false;
createInfo._auraEffectMask = Aura::BuildEffectMaskForOwner(createInfo._spellInfo, createInfo._auraEffectMask, createInfo._owner);
createInfo._targetEffectMask &= createInfo._auraEffectMask;
uint32 effMask = createInfo._auraEffectMask;
if (createInfo._targetEffectMask)
effMask = createInfo._targetEffectMask;
if (!effMask)
return nullptr;
if (Aura* foundAura = createInfo._owner->ToUnit()->_TryStackingOrRefreshingExistingAura(createInfo))
{
// we've here aura, which script triggered removal after modding stack amount
// check the state here, so we won't create new Aura object
if (foundAura->IsRemoved())
return nullptr;
if (createInfo.IsRefresh)
*createInfo.IsRefresh = true;
// add owner
Unit* unit = createInfo._owner->ToUnit();
// check effmask on owner application (if existing)
if (updateEffectMask)
if (AuraApplication* aurApp = foundAura->GetApplicationOfTarget(unit->GetGUID()))
aurApp->UpdateApplyEffectMask(effMask, false);
return foundAura;
}
else
return Create(createInfo);
}
Aura* Aura::TryCreate(AuraCreateInfo& createInfo)
{
uint32 effMask = createInfo._auraEffectMask;
if (createInfo._targetEffectMask)
effMask = createInfo._targetEffectMask;
effMask = Aura::BuildEffectMaskForOwner(createInfo._spellInfo, effMask, createInfo._owner);
if (!effMask)
return nullptr;
return Create(createInfo);
}
Aura* Aura::Create(AuraCreateInfo& createInfo)
{
// try to get caster of aura
if (!createInfo.CasterGUID.IsEmpty())
{
if (createInfo.CasterGUID.IsUnit())
{
if (createInfo._owner->GetGUID() == createInfo.CasterGUID)
createInfo.Caster = createInfo._owner->ToUnit();
else
createInfo.Caster = ObjectAccessor::GetUnit(*createInfo._owner, createInfo.CasterGUID);
}
}
else if (createInfo.Caster)
createInfo.CasterGUID = createInfo.Caster->GetGUID();
// check if aura can be owned by owner
if (createInfo._owner->isType(TYPEMASK_UNIT))
if (!createInfo._owner->IsInWorld() || createInfo._owner->ToUnit()->IsDuringRemoveFromWorld())
// owner not in world so don't allow to own not self cast single target auras
if (createInfo.CasterGUID != createInfo._owner->GetGUID() && createInfo._spellInfo->IsSingleTarget())
return nullptr;
Aura* aura = nullptr;
switch (createInfo._owner->GetTypeId())
{
case TYPEID_UNIT:
case TYPEID_PLAYER:
{
aura = new UnitAura(createInfo);
// aura can be removed in Unit::_AddAura call
if (aura->IsRemoved())
return nullptr;
// add owner
uint32 effMask = createInfo._auraEffectMask;
if (createInfo._targetEffectMask)
effMask = createInfo._targetEffectMask;
effMask = Aura::BuildEffectMaskForOwner(createInfo._spellInfo, effMask, createInfo._owner);
ASSERT_NODEBUGINFO(effMask);
Unit* unit = createInfo._owner->ToUnit();
aura->ToUnitAura()->AddStaticApplication(unit, effMask);
break;
}
case TYPEID_DYNAMICOBJECT:
createInfo._auraEffectMask = Aura::BuildEffectMaskForOwner(createInfo._spellInfo, createInfo._auraEffectMask, createInfo._owner);
ASSERT_NODEBUGINFO(createInfo._auraEffectMask);
aura = new DynObjAura(createInfo);
break;
default:
ABORT();
return nullptr;
}
// scripts, etc.
if (aura->IsRemoved())
return nullptr;
return aura;
}
Aura::Aura(AuraCreateInfo const& createInfo) :
m_spellInfo(createInfo._spellInfo), m_castDifficulty(createInfo._castDifficulty), m_castId(createInfo._castId), m_casterGuid(createInfo.CasterGUID),
m_castItemGuid(createInfo.CastItemGUID), m_castItemId(createInfo.CastItemId),
m_castItemLevel(createInfo.CastItemLevel), m_spellVisual({ createInfo.Caster ? createInfo.Caster->GetCastSpellXSpellVisualId(createInfo._spellInfo) : createInfo._spellInfo->GetSpellXSpellVisualId() }),
m_applyTime(GameTime::GetGameTime()), m_owner(createInfo._owner), m_timeCla(0), m_updateTargetMapInterval(0),
m_casterLevel(createInfo.Caster ? createInfo.Caster->GetLevel() : m_spellInfo->SpellLevel), m_procCharges(0), m_stackAmount(1),
m_isRemoved(false), m_isSingleTarget(false), m_isUsingCharges(false), m_dropEvent(nullptr),
m_procCooldown(TimePoint::min()),
m_lastProcAttemptTime(GameTime::Now() - Seconds(10)), m_lastProcSuccessTime(GameTime::Now() - Seconds(120))
{
for (SpellPowerEntry const* power : m_spellInfo->PowerCosts)
if (power && (power->ManaPerSecond != 0 || power->PowerPctPerSecond > 0.0f))
m_periodicCosts.push_back(power);
if (!m_periodicCosts.empty())
m_timeCla = 1 * IN_MILLISECONDS;
m_maxDuration = CalcMaxDuration(createInfo.Caster);
m_duration = m_maxDuration;
m_procCharges = CalcMaxCharges(createInfo.Caster);
m_isUsingCharges = m_procCharges != 0;
// m_casterLevel = cast item level/caster level, caster level should be saved to db, confirmed with sniffs
}
AuraScript* Aura::GetScriptByType(std::type_info const& type) const
{
for (AuraScript* script : m_loadedScripts)
if (typeid(*script) == type)
return script;
return nullptr;
}
void Aura::_InitEffects(uint32 effMask, Unit* caster, int32 const* baseAmount)
{
// shouldn't be in constructor - functions in AuraEffect::AuraEffect use polymorphism
_effects.resize(GetSpellInfo()->GetEffects().size());
for (SpellEffectInfo const& spellEffectInfo : GetSpellInfo()->GetEffects())
if (effMask & (1 << spellEffectInfo.EffectIndex))
_effects[spellEffectInfo.EffectIndex] = new AuraEffect(this, spellEffectInfo, baseAmount ? baseAmount + spellEffectInfo.EffectIndex : nullptr, caster);
}
Aura::~Aura()
{
// unload scripts
for (AuraScript* script : m_loadedScripts)
{
script->_Unload();
delete script;
}
for (AuraEffect* effect : _effects)
delete effect;
ASSERT(m_applications.empty());
_DeleteRemovedApplications();
}
Unit* Aura::GetCaster() const
{
if (GetOwner()->GetGUID() == GetCasterGUID())
return GetUnitOwner();
return ObjectAccessor::GetUnit(*GetOwner(), GetCasterGUID());
}
WorldObject* Aura::GetWorldObjectCaster() const
{
if (GetCasterGUID().IsUnit())
return GetCaster();
return ObjectAccessor::GetWorldObject(*GetOwner(), GetCasterGUID());
}
AuraEffect* Aura::GetEffect(uint32 index) const
{
if (index >= _effects.size())
return nullptr;
return _effects[index];
}
AuraObjectType Aura::GetType() const
{
return (m_owner->GetTypeId() == TYPEID_DYNAMICOBJECT) ? DYNOBJ_AURA_TYPE : UNIT_AURA_TYPE;
}
void Aura::_ApplyForTarget(Unit* target, Unit* caster, AuraApplication* auraApp)
{
ASSERT(target);
ASSERT(auraApp);
// aura mustn't be already applied on target
ASSERT (!IsAppliedOnTarget(target->GetGUID()) && "Aura::_ApplyForTarget: aura musn't be already applied on target");
m_applications[target->GetGUID()] = auraApp;
// set infinity cooldown state for spells
if (caster && caster->GetTypeId() == TYPEID_PLAYER)
{
if (m_spellInfo->IsCooldownStartedOnEvent())
{
Item* castItem = !m_castItemGuid.IsEmpty() ? caster->ToPlayer()->GetItemByGuid(m_castItemGuid) : nullptr;
caster->GetSpellHistory()->StartCooldown(m_spellInfo, castItem ? castItem->GetEntry() : 0, nullptr, true);
}
}
}
void Aura::_UnapplyForTarget(Unit* target, Unit* caster, AuraApplication* auraApp)
{
ASSERT(target);
ASSERT(auraApp->GetRemoveMode());
ASSERT(auraApp);
ApplicationMap::iterator itr = m_applications.find(target->GetGUID());
/// @todo Figure out why this happens
if (itr == m_applications.end())
{
TC_LOG_ERROR("spells", "Aura::_UnapplyForTarget, target: {}, caster: {}, spell:{} was not found in owners application map!",
target->GetGUID().ToString(), caster ? caster->GetGUID().ToString().c_str() : "Empty", auraApp->GetBase()->GetSpellInfo()->Id);
ABORT();
}
// aura has to be already applied
ASSERT(itr->second == auraApp);
m_applications.erase(itr);
_removedApplications.push_back(auraApp);
// reset cooldown state for spells
if (caster && GetSpellInfo()->IsCooldownStartedOnEvent())
// note: item based cooldowns and cooldown spell mods with charges ignored (unknown existed cases)
caster->GetSpellHistory()->SendCooldownEvent(GetSpellInfo());
}
// removes aura from all targets
// and marks aura as removed
void Aura::_Remove(AuraRemoveMode removeMode)
{
ASSERT (!m_isRemoved);
m_isRemoved = true;
ApplicationMap::iterator appItr = m_applications.begin();
for (appItr = m_applications.begin(); appItr != m_applications.end();)
{
AuraApplication * aurApp = appItr->second;
Unit* target = aurApp->GetTarget();
target->_UnapplyAura(aurApp, removeMode);
appItr = m_applications.begin();
}
if (m_dropEvent)
{
m_dropEvent->ScheduleAbort();
m_dropEvent = nullptr;
}
}
void Aura::UpdateTargetMap(Unit* caster, bool apply)
{
if (IsRemoved())
return;
m_updateTargetMapInterval = UPDATE_TARGET_MAP_INTERVAL;
// fill up to date target list
// target, effMask
std::unordered_map<Unit*, uint32> targets;
FillTargetMap(targets, caster);
std::vector<Unit*> targetsToRemove;
// mark all auras as ready to remove
for (auto const& applicationPair : m_applications)
{
auto itr = targets.find(applicationPair.second->GetTarget());
// not found in current area - remove the aura
if (itr == targets.end())
targetsToRemove.push_back(applicationPair.second->GetTarget());
else
{
// needs readding - remove now, will be applied in next update cycle
// (dbcs do not have auras which apply on same type of targets but have different radius, so this is not really needed)
if (itr->first->IsImmunedToSpell(GetSpellInfo(), caster, true) || !CanBeAppliedOn(itr->first))
{
targetsToRemove.push_back(applicationPair.second->GetTarget());
continue;
}
// check target immunities (for existing targets)
for (SpellEffectInfo const& spellEffectInfo : GetSpellInfo()->GetEffects())
if (itr->first->IsImmunedToSpellEffect(GetSpellInfo(), spellEffectInfo, caster, true))
itr->second &= ~(1 << spellEffectInfo.EffectIndex);
// needs to add/remove effects from application, don't remove from map so it gets updated
if (applicationPair.second->GetEffectMask() != itr->second)
continue;
// nothing to do - aura already applied
// remove from auras to register list
targets.erase(itr);
}
}
// register auras for units
for (auto itr = targets.begin(); itr != targets.end();)
{
bool addUnit = true;
AuraApplication* aurApp = GetApplicationOfTarget(itr->first->GetGUID());
if (!aurApp)
{
// check target immunities (for new targets)
for (SpellEffectInfo const& spellEffectInfo : GetSpellInfo()->GetEffects())
if (itr->first->IsImmunedToSpellEffect(GetSpellInfo(), spellEffectInfo, caster))
itr->second &= ~(1 << spellEffectInfo.EffectIndex);
if (!itr->second || itr->first->IsImmunedToSpell(GetSpellInfo(), caster) || !CanBeAppliedOn(itr->first))
addUnit = false;
}
if (addUnit && !itr->first->IsHighestExclusiveAura(this, true))
addUnit = false;
// Dynobj auras don't hit flying targets
if (GetType() == DYNOBJ_AURA_TYPE && itr->first->IsInFlight())
addUnit = false;
// Do not apply aura if it cannot stack with existing auras
if (addUnit)
{
// Allow to remove by stack when aura is going to be applied on owner
if (itr->first != GetOwner())
{
// check if not stacking aura already on target
// this one prevents unwanted usefull buff loss because of stacking and prevents overriding auras periodicaly by 2 near area aura owners
for (Unit::AuraApplicationMap::iterator iter = itr->first->GetAppliedAuras().begin(); iter != itr->first->GetAppliedAuras().end(); ++iter)
{
Aura const* aura = iter->second->GetBase();
if (!CanStackWith(aura))
{
addUnit = false;
break;
}
}
}
}
if (!addUnit)
itr = targets.erase(itr);
else
{
// owner has to be in world, or effect has to be applied to self
if (!GetOwner()->IsSelfOrInSameMap(itr->first))
{
/// @todo There is a crash caused by shadowfiend load addon
TC_LOG_FATAL("spells", "Aura {}: Owner {} (map {}) is not in the same map as target {} (map {}).", GetSpellInfo()->Id,
GetOwner()->GetName(), GetOwner()->IsInWorld() ? GetOwner()->GetMap()->GetId() : uint32(-1),
itr->first->GetName(), itr->first->IsInWorld() ? itr->first->GetMap()->GetId() : uint32(-1));
ABORT();
}
if (aurApp)
{
aurApp->UpdateApplyEffectMask(itr->second, true); // aura is already applied, this means we need to update effects of current application
itr = targets.erase(itr);
}
else
{
itr->first->_CreateAuraApplication(this, itr->second);
++itr;
}
}
}
// remove auras from units no longer needing them
for (Unit* unit : targetsToRemove)
if (AuraApplication* aurApp = GetApplicationOfTarget(unit->GetGUID()))
unit->_UnapplyAura(aurApp, AURA_REMOVE_BY_DEFAULT);
if (!apply)
return;
// apply aura effects for units
for (auto itr = targets.begin(); itr!= targets.end(); ++itr)
{
if (AuraApplication* aurApp = GetApplicationOfTarget(itr->first->GetGUID()))
{
// owner has to be in world, or effect has to be applied to self
ASSERT((!GetOwner()->IsInWorld() && GetOwner() == itr->first) || GetOwner()->IsInMap(itr->first));
itr->first->_ApplyAura(aurApp, itr->second);
}
}
}
// targets have to be registered and not have effect applied yet to use this function
void Aura::_ApplyEffectForTargets(uint8 effIndex)
{
// prepare list of aura targets
UnitList targetList;
for (ApplicationMap::iterator appIter = m_applications.begin(); appIter != m_applications.end(); ++appIter)
{
if ((appIter->second->GetEffectsToApply() & (1 << effIndex)) && !appIter->second->HasEffect(effIndex))
targetList.push_back(appIter->second->GetTarget());
}
// apply effect to targets
for (UnitList::iterator itr = targetList.begin(); itr != targetList.end(); ++itr)
{
if (GetApplicationOfTarget((*itr)->GetGUID()))
{
// owner has to be in world, or effect has to be applied to self
ASSERT((!GetOwner()->IsInWorld() && GetOwner() == *itr) || GetOwner()->IsInMap(*itr));
(*itr)->_ApplyAuraEffect(this, effIndex);
}
}
}
void Aura::UpdateOwner(uint32 diff, WorldObject* owner)
{
ASSERT(owner == m_owner);
Unit* caster = GetCaster();
// Apply spellmods for channeled auras
// used for example when triggered spell of spell:10 is modded
Spell* modSpell = nullptr;
Player* modOwner = nullptr;
if (caster)
{
modOwner = caster->GetSpellModOwner();
if (modOwner)
{
modSpell = modOwner->FindCurrentSpellBySpellId(GetId());
if (modSpell)
modOwner->SetSpellModTakingSpell(modSpell, true);
}
}
Update(diff, caster);
if (m_updateTargetMapInterval <= int32(diff))
UpdateTargetMap(caster);
else
m_updateTargetMapInterval -= diff;
// update aura effects
for (AuraEffect* effect : GetAuraEffects())
if (effect)
effect->Update(diff, caster);
// remove spellmods after effects update
if (modSpell)
modOwner->SetSpellModTakingSpell(modSpell, false);
_DeleteRemovedApplications();
}
void Aura::Update(uint32 diff, Unit* caster)
{
if (m_duration > 0)
{
m_duration -= diff;
if (m_duration < 0)
m_duration = 0;
// handle manaPerSecond/manaPerSecondPerLevel
if (m_timeCla)
{
if (m_timeCla > int32(diff))
m_timeCla -= diff;
else if (caster && (caster == GetOwner() || !GetSpellInfo()->HasAttribute(SPELL_ATTR2_NO_TARGET_PER_SECOND_COSTS)))
{
if (!m_periodicCosts.empty())
{
m_timeCla += 1000 - diff;
for (SpellPowerEntry const* power : m_periodicCosts)
{
if (power->RequiredAuraSpellID && !caster->HasAura(power->RequiredAuraSpellID))
continue;
int32 manaPerSecond = power->ManaPerSecond;
Powers powertype = Powers(power->PowerType);
if (powertype != POWER_HEALTH)
manaPerSecond += int32(CalculatePct(caster->GetMaxPower(powertype), power->PowerPctPerSecond));
else
manaPerSecond += int32(CalculatePct(caster->GetMaxHealth(), power->PowerPctPerSecond));
if (manaPerSecond)
{
if (powertype == POWER_HEALTH)
{
if (int32(caster->GetHealth()) > manaPerSecond)
caster->ModifyHealth(-manaPerSecond);
else
Remove();
}
else if (int32(caster->GetPower(powertype)) >= manaPerSecond)
caster->ModifyPower(powertype, -manaPerSecond);
else
Remove();
}
}
}
}
}
}
}
int32 Aura::CalcMaxDuration(Unit* caster) const
{
return Aura::CalcMaxDuration(GetSpellInfo(), caster, nullptr);
}
/*static*/ int32 Aura::CalcMaxDuration(SpellInfo const* spellInfo, WorldObject const* caster, std::vector<SpellPowerCost> const* powerCosts)
{
Player* modOwner = nullptr;
int32 maxDuration;
if (caster)
{
modOwner = caster->GetSpellModOwner();
maxDuration = caster->CalcSpellDuration(spellInfo, powerCosts);
}
else
maxDuration = spellInfo->GetDuration();
if (spellInfo->IsPassive() && !spellInfo->DurationEntry)
maxDuration = -1;
// IsPermanent() checks max duration (which we are supposed to calculate here)
if (maxDuration != -1 && modOwner)
modOwner->ApplySpellMod(spellInfo, SpellModOp::Duration, maxDuration);
return maxDuration;
}
void Aura::SetDuration(int32 duration, bool withMods)
{
if (withMods)
if (Unit* caster = GetCaster())
if (Player* modOwner = caster->GetSpellModOwner())
modOwner->ApplySpellMod(GetSpellInfo(), SpellModOp::Duration, duration);
m_duration = duration;
SetNeedClientUpdateForTargets();
}
void Aura::RefreshDuration(bool withMods)
{
Unit* caster = GetCaster();
if (withMods && caster)
{
int32 duration = m_spellInfo->GetMaxDuration();
// Calculate duration of periodics affected by haste.
if (m_spellInfo->HasAttribute(SPELL_ATTR8_HASTE_AFFECTS_DURATION))
duration = int32(duration * caster->m_unitData->ModCastingSpeed);
SetMaxDuration(duration);
SetDuration(duration);
}
else
SetDuration(GetMaxDuration());
if (!m_periodicCosts.empty())
m_timeCla = 1 * IN_MILLISECONDS;
// also reset periodic counters
for (uint8 i = 0; i < MAX_SPELL_EFFECTS; ++i)
if (AuraEffect* aurEff = GetEffect(i))
aurEff->ResetTicks();
}
void Aura::RefreshTimers(bool resetPeriodicTimer)
{
m_maxDuration = CalcMaxDuration();
// Pandemic Mechanic
if (m_spellInfo->HasAttribute(SPELL_ATTR13_PERIODIC_REFRESH_EXTENDS_DURATION))
{
// Pandemic doesn't reset periodic timer
resetPeriodicTimer = false;
int32 pandemicDuration = CalculatePct(m_maxDuration, 30.f);
m_maxDuration = std::max(GetDuration(), std::min(pandemicDuration, GetDuration()) + m_maxDuration);
}
RefreshDuration();
Unit* caster = GetCaster();
for (uint8 i = 0; i < MAX_SPELL_EFFECTS; ++i)
if (AuraEffect* aurEff = GetEffect(i))
aurEff->CalculatePeriodic(caster, resetPeriodicTimer, false);
}
void Aura::SetCharges(uint8 charges)
{
if (m_procCharges == charges)
return;
m_procCharges = charges;
m_isUsingCharges = m_procCharges != 0;
SetNeedClientUpdateForTargets();
}
uint8 Aura::CalcMaxCharges(Unit* caster) const
{
uint32 maxProcCharges = m_spellInfo->ProcCharges;
if (SpellProcEntry const* procEntry = sSpellMgr->GetSpellProcEntry(GetSpellInfo()))
maxProcCharges = procEntry->Charges;
if (caster)
if (Player* modOwner = caster->GetSpellModOwner())
modOwner->ApplySpellMod(GetSpellInfo(), SpellModOp::ProcCharges, maxProcCharges);
return uint8(maxProcCharges);
}
bool Aura::ModCharges(int32 num, AuraRemoveMode removeMode)
{
if (IsUsingCharges())
{
int32 charges = m_procCharges + num;
int32 maxCharges = CalcMaxCharges();
// limit charges (only on charges increase, charges may be changed manually)
if ((num > 0) && (charges > int32(maxCharges)))
charges = maxCharges;
// we're out of charges, remove
else if (charges <= 0)
{
Remove(removeMode);
return true;
}
SetCharges(charges);
}
return false;
}