-
Notifications
You must be signed in to change notification settings - Fork 294
/
Spell.cpp
9989 lines (8782 loc) · 409 KB
/
Spell.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 "Spells/Spell.h"
#include "Database/DatabaseEnv.h"
#include "Server/WorldPacket.h"
#include "Server/WorldSession.h"
#include "Grids/GridNotifiers.h"
#include "Grids/GridNotifiersImpl.h"
#include "Server/Opcodes.h"
#include "Log.h"
#include "World/World.h"
#include "Globals/ObjectMgr.h"
#include "Spells/SpellMgr.h"
#include "Entities/Player.h"
#include "Entities/Pet.h"
#include "Entities/Unit.h"
#include "Entities/DynamicObject.h"
#include "Groups/Group.h"
#include "Entities/UpdateData.h"
#include "Globals/ObjectAccessor.h"
#include "Grids/CellImpl.h"
#include "Policies/Singleton.h"
#include "Globals/SharedDefines.h"
#include "Loot/LootMgr.h"
#include "Vmap/VMapFactory.h"
#include "BattleGround/BattleGround.h"
#include "Util/Util.h"
#include "Chat/Chat.h"
#include "Entities/Vehicle.h"
#include "Entities/TemporarySpawn.h"
#include "Server/SQLStorages.h"
#include "MotionGenerators/PathFinder.h"
#include "Spells/Scripts/SpellScript.h"
#include "Entities/ObjectGuid.h"
#include "Entities/Transports.h"
extern pEffect SpellEffects[MAX_SPELL_EFFECTS];
class PrioritizeManaUnitWraper
{
public:
explicit PrioritizeManaUnitWraper(Unit* unit) : i_unit(unit)
{
uint32 maxmana = unit->GetMaxPower(POWER_MANA);
i_percent = maxmana ? unit->GetPower(POWER_MANA) * 100 / maxmana : 101;
}
Unit* getUnit() const { return i_unit; }
uint32 getPercent() const { return i_percent; }
private:
Unit* i_unit;
uint32 i_percent;
};
struct PrioritizeMana
{
int operator()(PrioritizeManaUnitWraper const& x, PrioritizeManaUnitWraper const& y) const
{
return x.getPercent() > y.getPercent();
}
};
typedef std::priority_queue<PrioritizeManaUnitWraper, std::vector<PrioritizeManaUnitWraper>, PrioritizeMana> PrioritizeManaUnitQueue;
class PrioritizeHealthUnitWraper
{
public:
explicit PrioritizeHealthUnitWraper(Unit* unit) : i_unit(unit)
{
i_percent = unit->GetHealthPercent();
}
Unit* getUnit() const { return i_unit; }
uint32 getPercent() const { return i_percent; }
private:
Unit* i_unit;
uint32 i_percent;
};
struct PrioritizeHealth
{
int operator()(PrioritizeHealthUnitWraper const& x, PrioritizeHealthUnitWraper const& y) const
{
return x.getPercent() > y.getPercent();
}
};
typedef std::priority_queue<PrioritizeHealthUnitWraper, std::vector<PrioritizeHealthUnitWraper>, PrioritizeHealth> PrioritizeHealthUnitQueue;
bool IsQuestTameSpell(uint32 spellId)
{
SpellEntry const* spellproto = sSpellTemplate.LookupEntry<SpellEntry>(spellId);
if (!spellproto)
return false;
return spellproto->Effect[EFFECT_INDEX_0] == SPELL_EFFECT_THREAT
&& spellproto->Effect[EFFECT_INDEX_1] == SPELL_EFFECT_APPLY_AURA && spellproto->EffectApplyAuraName[EFFECT_INDEX_1] == SPELL_AURA_DUMMY;
}
// SpellCastTargets class
// **********************
SpellCastTargets::SpellCastTargets()
{
m_unitTarget = nullptr;
m_itemTarget = nullptr;
m_GOTarget = nullptr;
m_CorpseTarget = nullptr;
m_itemTargetEntry = 0;
m_strTarget.clear();
m_targetMask = 0;
m_elevation = 0.0f;
m_speed = 0.0f;
m_mapId = UINT32_MAX;
}
SpellCastTargets::~SpellCastTargets()
{
}
void SpellCastTargets::setUnitTarget(Unit* target)
{
if (!target)
return;
m_destPos = target->GetPosition();
m_unitTarget = target;
m_unitTargetGUID = target->GetObjectGuid();
m_targetMask |= TARGET_FLAG_UNIT;
}
void SpellCastTargets::setDestination(float x, float y, float z)
{
m_destPos.x = x;
m_destPos.y = y;
m_destPos.z = z;
m_targetMask |= TARGET_FLAG_DEST_LOCATION;
}
void SpellCastTargets::setDestination(Position position)
{
m_destPos = position;
m_targetMask |= TARGET_FLAG_DEST_LOCATION;
}
void SpellCastTargets::setSource(float x, float y, float z)
{
m_srcPos.x = x;
m_srcPos.y = y;
m_srcPos.z = z;
m_targetMask |= TARGET_FLAG_SOURCE_LOCATION;
}
void SpellCastTargets::setGOTarget(GameObject* target)
{
m_GOTarget = target;
m_GOTargetGUID = target->GetObjectGuid();
// m_targetMask |= TARGET_FLAG_GAMEOBJECT;
}
void SpellCastTargets::setItemTarget(Item* item)
{
if (!item)
return;
m_itemTarget = item;
m_itemTargetGUID = item->GetObjectGuid();
m_itemTargetEntry = item->GetEntry();
m_targetMask |= TARGET_FLAG_ITEM;
}
void SpellCastTargets::clearItemPointer()
{
// keep the other information so its clear how the spell was targeted
m_itemTarget = nullptr;
}
void SpellCastTargets::setTradeItemTarget(Player* caster)
{
m_itemTargetGUID = ObjectGuid(uint64(TRADE_SLOT_NONTRADED));
m_itemTargetEntry = 0;
m_targetMask |= TARGET_FLAG_TRADE_ITEM;
Update(caster);
}
void SpellCastTargets::setCorpseTarget(Corpse* corpse)
{
m_CorpseTarget = corpse;
m_CorpseTargetGUID = corpse->GetObjectGuid();
}
void SpellCastTargets::Update(WorldObject* caster)
{
m_GOTarget = m_GOTargetGUID ? caster->GetMap()->GetGameObject(m_GOTargetGUID) : nullptr;
m_unitTarget = m_unitTargetGUID ?
(m_unitTargetGUID == caster->GetObjectGuid() ? static_cast<Unit*>(caster) : ObjectAccessor::GetUnit(*caster, m_unitTargetGUID)) :
nullptr;
m_itemTarget = nullptr;
m_CorpseTarget = ObjectAccessor::GetCorpseInMap(m_CorpseTargetGUID, caster->GetMapId());
if (caster->GetTypeId() == TYPEID_PLAYER)
{
Player* player = ((Player*)caster);
if (m_targetMask & TARGET_FLAG_ITEM)
m_itemTarget = player->GetItemByGuid(m_itemTargetGUID);
else if (m_targetMask & TARGET_FLAG_TRADE_ITEM)
{
if (TradeData* pTrade = player->GetTradeData())
if (m_itemTargetGUID.GetRawValue() < TRADE_SLOT_COUNT)
m_itemTarget = pTrade->GetTraderData()->GetItem(TradeSlots(m_itemTargetGUID.GetRawValue()));
}
if (m_itemTarget)
m_itemTargetEntry = m_itemTarget->GetEntry();
}
if (m_srcTransportGUID)
{
if (GenericTransport* transport = caster->GetMap()->GetTransport(m_srcTransportGUID))
{
Position temp = m_srcTransportPos;
transport->CalculatePassengerPosition(temp.x, temp.y, temp.z, &temp.o);
m_srcPos = temp;
}
}
if (m_destTransportGUID)
{
if (GenericTransport* transport = caster->GetMap()->GetTransport(m_destTransportGUID))
{
Position temp = m_destTransportPos;
transport->CalculatePassengerPosition(temp.x, temp.y, temp.z, &temp.o);
m_destPos = temp;
}
}
}
void SpellCastTargets::read(ByteBuffer& data, Unit* caster)
{
data >> m_targetMask;
if (m_targetMask == TARGET_FLAG_SELF)
return;
// TARGET_FLAG_UNIT_MINIPET is used for non-combat pets, maybe other?
if (m_targetMask & (TARGET_FLAG_UNIT | TARGET_FLAG_UNIT_MINIPET | TARGET_FLAG_UNIT_ENEMY))
data >> m_unitTargetGUID.ReadAsPacked();
if (m_targetMask & (TARGET_FLAG_GAMEOBJECT | TARGET_FLAG_LOCKED))
data >> m_GOTargetGUID.ReadAsPacked();
if ((m_targetMask & (TARGET_FLAG_ITEM | TARGET_FLAG_TRADE_ITEM)) && caster->GetTypeId() == TYPEID_PLAYER)
data >> m_itemTargetGUID.ReadAsPacked();
if (m_targetMask & (TARGET_FLAG_CORPSE_ALLY | TARGET_FLAG_CORPSE_ENEMY))
data >> m_CorpseTargetGUID.ReadAsPacked();
if (m_targetMask & TARGET_FLAG_SOURCE_LOCATION)
{
data >> m_srcTransportGUID.ReadAsPacked();
if (m_srcTransportGUID)
{
data >> m_srcTransportPos.x >> m_srcTransportPos.y >> m_srcTransportPos.z;
m_srcPos = m_srcTransportPos;
if (GenericTransport* transport = caster->GetMap()->GetTransport(m_srcTransportGUID))
transport->CalculatePassengerPosition(m_srcPos.x, m_srcPos.y, m_srcPos.z, &m_srcPos.o);
}
else
data >> m_srcPos.x >> m_srcPos.y >> m_srcPos.z;
if (!MaNGOS::IsValidMapCoord(m_srcPos.x, m_srcPos.y, m_srcPos.z))
throw ByteBufferException(false, data.rpos(), 0, data.size());
}
if (m_targetMask & TARGET_FLAG_DEST_LOCATION)
{
data >> m_destTransportGUID.ReadAsPacked();
if (m_destTransportGUID)
{
data >> m_destTransportPos.x >> m_destTransportPos.y >> m_destTransportPos.z;
m_destPos = m_destTransportPos;
if (GenericTransport* transport = caster->GetMap()->GetTransport(m_destTransportGUID))
transport->CalculatePassengerPosition(m_destPos.x, m_destPos.y, m_destPos.z, &m_destPos.o);
}
else
data >> m_destPos.x >> m_destPos.y >> m_destPos.z;
if (!MaNGOS::IsValidMapCoord(m_destPos.x, m_destPos.y, m_destPos.z))
throw ByteBufferException(false, data.rpos(), 0, data.size());
}
if (m_targetMask & TARGET_FLAG_STRING)
data >> m_strTarget;
// find real units/GOs
Update(caster);
}
void SpellCastTargets::write(ByteBuffer& data) const
{
data << uint32(m_targetMask);
if (m_targetMask & (TARGET_FLAG_UNIT | TARGET_FLAG_CORPSE_ENEMY | TARGET_FLAG_GAMEOBJECT | TARGET_FLAG_CORPSE_ALLY | TARGET_FLAG_UNIT_MINIPET))
{
if (m_targetMask & TARGET_FLAG_UNIT)
{
if (m_unitTarget)
data << m_unitTarget->GetPackGUID();
else
data << uint8(0);
}
else if (m_targetMask & TARGET_FLAG_GAMEOBJECT)
{
if (m_GOTarget)
data << m_GOTarget->GetPackGUID();
else
data << uint8(0);
}
else if (m_targetMask & (TARGET_FLAG_CORPSE_ALLY | TARGET_FLAG_CORPSE_ENEMY))
data << m_CorpseTargetGUID.WriteAsPacked();
else
data << uint8(0);
}
if (m_targetMask & (TARGET_FLAG_ITEM | TARGET_FLAG_TRADE_ITEM))
{
if (m_itemTarget)
data << m_itemTarget->GetPackGUID();
else
data << uint8(0);
}
if (m_targetMask & TARGET_FLAG_SOURCE_LOCATION)
{
data << m_srcTransportGUID.WriteAsPacked();
data << m_srcPos.x << m_srcPos.y << m_srcPos.z;
}
if (m_targetMask & TARGET_FLAG_DEST_LOCATION)
{
data << m_destTransportGUID.WriteAsPacked();
data << m_destPos.x << m_destPos.y << m_destPos.z;
}
if (m_targetMask & TARGET_FLAG_STRING)
data << m_strTarget;
}
// SpellLog class
// **************
void SpellLog::Initialize()
{
m_spellLogData.Initialize(SMSG_SPELLLOGEXECUTE);
//m_spellLogData << m_spell->GetCaster()->GetPackGUID();
m_spellLogData.appendPackGUID(m_spell->GetTrueCaster()->GetObjectGuid().GetRawValue());
m_spellLogData << uint32(m_spell->m_spellInfo->Id);
m_spellLogDataEffectsCounterPos = m_spellLogData.wpos();
m_spellLogData << uint32(0); //placeholder
m_spellLogDataEffectsCounter = 0;
}
void SpellLog::FinalizePrevious()
{
if (m_currentEffect < uint32(MAX_SPELL_EFFECTS))
{
// not first effect processed so we have to finalize the previous one by putting correct amount of targets
m_spellLogData.put<uint32>(m_spellLogDataTargetsCounterPos, m_spellLogDataTargetsCounter);
m_currentEffect = uint32(MAX_SPELL_EFFECTS);
}
}
void SpellLog::SetCurrentEffect(uint32 effect)
{
// check if its same effect so we don't have to put header
if (effect == m_currentEffect)
{
++m_spellLogDataTargetsCounter;
return;
}
FinalizePrevious();
m_spellLogData << effect;
m_spellLogDataTargetsCounterPos = m_spellLogData.wpos();
m_spellLogData << uint32(0); // placeholder for target counter
m_currentEffect = effect;
m_spellLogDataTargetsCounter = 1;
}
void SpellLog::SendToSet()
{
if (!m_spellLogDataEffectsCounter)
return;
// check if one of previous target is not finalized
FinalizePrevious();
// put total effect counter in packet
m_spellLogData.put<uint32>(m_spellLogDataEffectsCounterPos, m_spellLogDataEffectsCounter);
m_spell->GetTrueCaster()->SendMessageToSet(m_spellLogData, true);
// make it ready for another log if need
Initialize();
}
// Spell class
// ***********
Spell::Spell(WorldObject* caster, SpellEntry const* info, uint32 triggeredFlags, ObjectGuid originalCasterGUID, SpellEntry const* triggeredBy) :
m_partialApplicationMask(0), m_spellScript(SpellScriptMgr::GetSpellScript(info->Id)), m_auraScript(SpellScriptMgr::GetAuraScript(info->Id)),
m_effectSkipMask(0),
m_spellEvent(nullptr), m_spellLog(this), m_param1(0), m_param2(0), m_trueCaster(caster)
{
MANGOS_ASSERT(caster != nullptr && info != nullptr);
MANGOS_ASSERT(info == sSpellTemplate.LookupEntry<SpellEntry>(info->Id) && "`info` must be pointer to sSpellTemplate element");
if (info->SpellDifficultyId && caster->IsInWorld() && caster->GetMap()->IsDungeon())
{
if (SpellEntry const* spellEntry = GetSpellEntryByDifficulty(info->SpellDifficultyId, caster->GetMap()->GetDifficulty(), caster->GetMap()->IsRaid()))
m_spellInfo = spellEntry;
else
m_spellInfo = info;
}
else
m_spellInfo = info;
m_triggeredBySpellInfo = triggeredBy;
m_caster = dynamic_cast<Unit*>(caster);
m_referencedFromCurrentSpell = false;
m_executedCurrently = false;
m_delayStart = 0;
m_delayAtDamageCount = 0;
m_applyMultiplierMask = 0;
for (float& value : m_damageDoneMultiplier)
value = 1.f;
// Get data for type of attack
m_attackType = GetWeaponAttackType(m_spellInfo);
m_spellSchoolMask = GetSpellSchoolMask(info); // Can be override for some spell (wand shoot for example)
// wand case
if (m_attackType == RANGED_ATTACK && m_caster)
if (!!(m_caster->getClassMask() & CLASSMASK_WAND_USERS) && m_caster->GetTypeId() == TYPEID_PLAYER)
m_spellSchoolMask = m_caster->GetRangedDamageSchoolMask();
// Set health leech amount to zero
m_healthLeech = 0;
m_originalCasterGUID = originalCasterGUID ? originalCasterGUID : m_trueCaster->GetObjectGuid();
UpdateOriginalCasterPointer();
for (int i = 0; i < MAX_EFFECT_INDEX; ++i)
m_currentBasePoints[i] = m_spellInfo->CalculateSimpleValue(SpellEffectIndex(i));
m_spellState = SPELL_STATE_CREATED;
m_castPositionX = m_castPositionY = m_castPositionZ = 0;
m_TriggerSpells.clear();
m_preCastSpells.clear();
m_IsTriggeredSpell = triggeredFlags & TRIGGERED_OLD_TRIGGERED;
// m_AreaAura = false;
m_CastItem = nullptr;
m_itemCastSpell = false;
unitTarget = nullptr;
itemTarget = nullptr;
gameObjTarget = nullptr;
corpseTarget = nullptr;
m_eventTarget = nullptr;
m_cast_count = 0;
m_glyphIndex = 0;
m_triggeredByAuraSpell = nullptr;
m_spellAuraHolder = nullptr;
// Auto Shot & Shoot (wand)
m_autoRepeat = IsAutoRepeatRangedSpell(m_spellInfo);
m_runesState = 0;
m_runesStateAfterCast = 0;
m_powerCost = 0; // setup to correct value in Spell::prepare, don't must be used before.
m_casttime = 0; // setup to correct value in Spell::prepare, don't must be used before.
m_timer = 0; // will set to cast time in prepare
m_creationTime = m_trueCaster->GetMap()->GetCurrentMSTime();
m_updated = false;
m_duration = 0;
m_maxRange = 0.f;
m_guaranteedCrit = false;
m_needAliveTargetMask = 0;
m_ignoreHitResult = (triggeredFlags & TRIGGERED_IGNORE_HIT_CALCULATION) != 0;
m_ignoreUnselectableTarget = m_IsTriggeredSpell || ((triggeredFlags & TRIGGERED_IGNORE_UNSELECTABLE_FLAG) != 0);
m_ignoreCastTime = (triggeredFlags & TRIGGERED_INSTANT_CAST) != 0;
m_ignoreUnattackableTarget = (triggeredFlags & TRIGGERED_IGNORE_UNATTACKABLE_FLAG) != 0;
m_triggerAutorepeat = (triggeredFlags & TRIGGERED_AUTOREPEAT) != 0;
m_doNotProc = (triggeredFlags & TRIGGERED_DO_NOT_PROC) != 0;
m_petCast = (triggeredFlags & TRIGGERED_PET_CAST) != 0;
m_notifyAI = (triggeredFlags & TRIGGERED_NORMAL_COMBAT_CAST) != 0;
m_ignoreGCD = m_IsTriggeredSpell || ((triggeredFlags & TRIGGERED_IGNORE_GCD) != 0);
m_ignoreCosts = m_IsTriggeredSpell || ((triggeredFlags & TRIGGERED_IGNORE_COSTS) != 0);
m_ignoreCooldowns = m_IsTriggeredSpell || ((triggeredFlags & TRIGGERED_IGNORE_COOLDOWNS) != 0);
m_ignoreConcurrentCasts = m_IsTriggeredSpell || ((triggeredFlags & TRIGGERED_IGNORE_CURRENT_CASTED_SPELL) != 0) || m_spellInfo->HasAttribute(SPELL_ATTR_EX4_ALLOW_CAST_WHILE_CASTING);
m_ignoreCasterAuraState = m_IsTriggeredSpell || ((triggeredFlags & TRIGGERED_IGNORE_CASTER_AURA_STATE) != 0 || m_spellInfo->HasAttribute(SPELL_ATTR_EX5_IGNORE_CASTER_REQUIREMENTS));
m_hideInCombatLog = (m_IsTriggeredSpell && !IsAutoRepeatRangedSpell(m_spellInfo)) || ((triggeredFlags & TRIGGERED_HIDE_CAST_IN_COMBAT_LOG) != 0);
m_resetLeash = (triggeredFlags & TRIGGERED_DO_NOT_RESET_LEASH) == 0;
m_channelOnly = (triggeredFlags & TRIGGERED_CHANNEL_ONLY) != 0;
m_clientCast = false;
m_reflectable = IsReflectableSpell(m_spellInfo);
m_spellFlags = SPELL_FLAG_NORMAL;
m_affectedTargetCount = m_spellInfo->MaxAffectedTargets;
for (uint32 i = 0; i < MAX_EFFECT_INDEX; ++i)
m_chainTargetCount[i] = m_spellInfo->EffectChainTarget[SpellEffectIndex(i)];
m_jumpRadius = SpellTargetMgr::GetJumpRadius(m_spellInfo->Id);
memcpy(m_filteringScheme, SpellTargetMgr::GetSpellTargetingData(m_spellInfo->Id).filteringScheme, sizeof(m_filteringScheme));
m_scriptValue = 0;
memset(m_effectTriggerChance, -1, sizeof(m_effectTriggerChance));
memset(damagePerEffect, 0, sizeof(damagePerEffect));
CleanupTargetList();
m_spellLog.Initialize();
m_needSpellLog = (m_spellInfo->Attributes & (SPELL_ATTR_DO_NOT_LOG | SPELL_ATTR_DO_NOT_DISPLAY)) == 0;
m_travellingStart = UINT32_MAX;
m_targetlessMask = 0;
m_overrideSpeed = false;
m_ignoreRoot = IsIgnoreRootSpell(m_spellInfo);
m_ignoreOwnerLevel = false;
if (m_caster)
{
if (m_spellInfo->HasAttribute(SPELL_ATTR_EX6_ORIGINATE_FROM_CONTROLLER))
{
if (Player* controllingPlayer = const_cast<Player*>(m_caster->GetControllingPlayer()))
{
m_caster = controllingPlayer;
m_trueCaster = controllingPlayer;
}
}
}
OnInit();
}
Spell::~Spell()
{
if (!m_IsTriggeredSpell && m_CastItem)
m_CastItem->SetUsedInSpell(false);
}
template<typename T>
WorldObject* Spell::FindCorpseUsing()
{
// non-standard target selection
SpellRangeEntry const* srange = sSpellRangeStore.LookupEntry(m_spellInfo->rangeIndex);
float max_range = GetSpellMaxRange(srange);
WorldObject* result = nullptr;
T u_check(m_caster, max_range);
MaNGOS::WorldObjectSearcher<T> searcher(result, u_check);
Cell::VisitGridObjects(m_caster, searcher, max_range);
if (!result)
Cell::VisitWorldObjects(m_caster, searcher, max_range);
return result;
}
void Spell::FillTargetMap()
{
TempTargetingData targetingData;
uint8 effToIndex[MAX_EFFECT_INDEX] = {0, 1, 2}; // Helper array, to link to another tmpUnitList, if the targets for both effects match
if (m_trueCaster->IsUnit())
{
auto const& mod = m_caster->GetAurasByType(SPELL_AURA_MOD_MAX_AFFECTED_TARGETS);
for (Aura* aura : mod)
{
if (!aura->isAffectedOnSpell(m_spellInfo))
continue;
m_affectedTargetCount += aura->GetModifier()->m_amount;
}
}
for (uint32 i = 0; i < MAX_EFFECT_INDEX; ++i)
{
// not call for empty effect.
// Also some spells use not used effect targets for store targets for dummy effect in triggered spells
if (m_spellInfo->Effect[i] == SPELL_EFFECT_NONE)
continue;
auto& data = SpellTargetMgr::GetSpellTargetingData(m_spellInfo->Id);
SpellTargetImplicitType effectTargetType = data.implicitType[i]; // prefilled data on load
auto& targetMask = data.targetMask[i];
auto& ignoredTargets = data.ignoredTargets[i];
auto& filterScheme = m_filteringScheme[i];
targetingData.chainTargetCount[i] = m_chainTargetCount[i];
if (effectTargetType == TARGET_TYPE_SPECIAL_UNIT) // area auras need custom handling
{
uint32 targetA = m_spellInfo->EffectImplicitTargetA[i];
uint32 targetB = m_spellInfo->EffectImplicitTargetB[i];
bool hadTarget = false;
// need to pick a single unit target if existant and use it for area aura owner
if (targetA && !ignoredTargets.first)
{
if (SpellTargetInfoTable[targetA].type == TARGET_TYPE_UNIT && SpellTargetInfoTable[targetA].enumerator == TARGET_ENUMERATOR_SINGLE)
hadTarget = true;
SetTargetMap(SpellEffectIndex(i), targetA, false, targetingData);
}
if (targetB && !ignoredTargets.second)
{
if (SpellTargetInfoTable[targetB].type == TARGET_TYPE_UNIT && SpellTargetInfoTable[targetB].enumerator == TARGET_ENUMERATOR_SINGLE)
hadTarget = true;
if (SpellTargetInfoTable[targetB].enumerator != TARGET_ENUMERATOR_AOE)
SetTargetMap(SpellEffectIndex(i), targetB, true, targetingData);
}
if (!hadTarget && m_caster) // if no single targeting available use caster as default
targetingData.data[i].tmpUnitList[false].push_back(m_caster);
}
else
{
uint32 targetA = m_spellInfo->EffectImplicitTargetA[i];
uint32 targetB = m_spellInfo->EffectImplicitTargetB[i];
if (targetA == TARGET_NONE && targetB == TARGET_NONE)
{
// if no targeting available, attempt to use entry mask
if (m_spellInfo->Targets && SpellTargetMgr::CanEffectBeFilledWithMask(m_spellInfo->Id, i, m_spellInfo->Targets))
FillFromTargetFlags(targetingData, SpellEffectIndex(i));
else if (uint32 defaultTarget = SpellEffectInfoTable[m_spellInfo->Effect[i]].defaultTarget) // else resort to default effect type if it exists
SetTargetMap(SpellEffectIndex(i), defaultTarget, false, targetingData);
}
else // normal case, use existing spell data
{
if (targetA && !ignoredTargets.first)
SetTargetMap(SpellEffectIndex(i), targetA, false, targetingData);
if (targetB && !ignoredTargets.second)
SetTargetMap(SpellEffectIndex(i), targetB, true, targetingData);
if (effectTargetType == TARGET_TYPE_UNIT_DEST) // special case - no unit target, but need to check for valid units
if (SpellTargetInfoTable[targetA].type != TARGET_TYPE_UNIT && SpellTargetInfoTable[targetB].type != TARGET_TYPE_UNIT) // no fill for unit out of targets
FillFromTargetFlags(targetingData, SpellEffectIndex(i)); // inefficient call, very rare, less code duplicity
if (effectTargetType == TARGET_TYPE_LOCATION_TRAJ && (m_targets.m_targetMask & TARGET_FLAG_SOURCE_LOCATION) == 0)
if (WorldObject* caster = GetCastingObject())
m_targets.setSource(caster->GetPositionX(), caster->GetPositionY(), caster->GetPositionZ());
}
}
bool processedUnits = false; // TODO: Review area aura packet fills for area auras with AOE targets
bool processedGOs = false; // likely purpose - animations
switch (effectTargetType)
{
case TARGET_TYPE_NONE:
m_targetlessMask |= (1 << i);
break;
case TARGET_TYPE_LOCATION_DEST:
case TARGET_TYPE_SPECIAL_DEST:
case TARGET_TYPE_LOCATION_TRAJ:
OnDestTarget();
AddDestExecution(SpellEffectIndex(i));
if (effectTargetType == TARGET_TYPE_SPECIAL_DEST)
if (!FillUnitTargets(targetingData, data, i))
return;
break;
case TARGET_TYPE_CORPSE: // can be unit and corpse
if (!targetingData.data[i].tempCorpseList.empty())
{
CorpseList& corpseTargetList = targetingData.data[i].tempCorpseList;
uint8 effectMask = targetMask[0] | targetMask[1];
for (Corpse* corpse : corpseTargetList)
AddCorpseTarget(corpse, effectMask);
}
case TARGET_TYPE_UNIT:
case TARGET_TYPE_UNIT_DEST:
case TARGET_TYPE_PLAYER: // for now player handled here
case TARGET_TYPE_SPECIAL_UNIT:
if (effectTargetType == TARGET_TYPE_UNIT_DEST)
OnDestTarget();
processedUnits = true;
if (!FillUnitTargets(targetingData, data, i))
return;
break;
case TARGET_TYPE_LOCK:
case TARGET_TYPE_ITEM:
if (targetingData.data[i].tempItemList.size() > 0) // Item case
for (Item* item : targetingData.data[i].tempItemList)
AddItemTarget(item, targetMask[0]); // no spell in all of vanilla through wotlk has item target in B
if (effectTargetType == TARGET_TYPE_ITEM)
break;
// [[fallthrough]]
case TARGET_TYPE_GAMEOBJECT:
processedGOs = true;
for (uint8 rightTarget = 0; rightTarget < 2; ++rightTarget) // need to process target A and B separately due to effect masks
{
bool ignored = rightTarget ? ignoredTargets.second : ignoredTargets.first;
if (ignored)
continue;
GameObjectList& goTargetList = targetingData.data[i].tmpGOList[rightTarget];
uint8 effectMask = targetMask[rightTarget];
if (!goTargetList.empty()) // GO case
{
for (auto iter = goTargetList.begin(); iter != goTargetList.end();)
{
if (OnCheckTarget(*iter, SpellEffectIndex(i)))
++iter;
else
iter = goTargetList.erase(iter);
}
if (m_affectedTargetCount && goTargetList.size() > m_affectedTargetCount)
{
// remove random units from the map
while (goTargetList.size() > m_affectedTargetCount)
{
uint32 poz = urand(0, goTargetList.size() - 1);
for (auto itr = goTargetList.begin(); itr != goTargetList.end(); ++itr, --poz)
{
if (!*itr) continue;
if (!poz)
{
goTargetList.erase(itr);
break;
}
}
}
}
for (GameObject* go : goTargetList)
AddGOTarget(go, effectMask);
}
}
break;
default:
break;
}
}
if (m_spellInfo->HasAttribute(SPELL_ATTR_EX2_FAIL_ON_ALL_TARGETS_IMMUNE))
{
bool allImmune = true;
for (auto& ihit : m_UniqueTargetInfo)
{
if (ihit.missCondition != SPELL_MISS_IMMUNE && ihit.missCondition != SPELL_MISS_IMMUNE2)
{
allImmune = false;
break;
}
}
if (allImmune)
{
SendCastResult(SPELL_FAILED_IMMUNE); // guessed error
finish(false);
}
}
if (m_spellInfo->HasAttribute(SPELL_ATTR_EX_REQUIRE_ALL_TARGETS))
{
for (auto& ihit : m_UniqueTargetInfo)
{
if (ihit.targetGUID == m_targets.getUnitTargetGuid() && ihit.missCondition != SPELL_MISS_NONE)
{
for (auto& ihit : m_UniqueTargetInfo)
{
ihit.effectHitMask = 0;
ihit.effectMask = 0;
}
return;
}
}
}
}
bool Spell::FillUnitTargets(TempTargetingData& targetingData, SpellTargetingData& data, uint32 i)
{
auto& targetMask = data.targetMask[i];
auto& ignoredTargets = data.ignoredTargets[i];
auto& filterScheme = m_filteringScheme[i];
for (uint8 rightTarget = 0; rightTarget < 2; ++rightTarget) // need to process target A and B separately due to effect masks
{
bool ignored = rightTarget ? ignoredTargets.second : ignoredTargets.first;
if (ignored)
continue;
UnitList& unitTargetList = targetingData.data[i].tmpUnitList[rightTarget];
uint8 effectMask = targetMask[rightTarget];
SpellTargetFilterScheme scheme = filterScheme[rightTarget];
uint32 target = rightTarget ? m_spellInfo->EffectImplicitTargetB[i] : m_spellInfo->EffectImplicitTargetA[i];
SpellTargetImplicitType type = SpellTargetInfoTable[target].type;
if (!unitTargetList.empty()) // Unit case
{
for (auto itr = unitTargetList.begin(); itr != unitTargetList.end();)
{
if (!CheckTarget(*itr, SpellEffectIndex(i), bool(rightTarget), CheckException(targetingData.magnet)))
itr = unitTargetList.erase(itr);
else
++itr;
}
// Special target filter before adding targets to list
FilterTargetMap(unitTargetList, scheme, targetingData.chainTargetCount[i]);
if (m_trueCaster->IsPlayer())
{
Player* me = static_cast<Player*>(m_trueCaster);
for (auto itr = unitTargetList.begin(); itr != unitTargetList.end(); ++itr)
{
Player* targetOwner = (*itr)->GetBeneficiaryPlayer();
if (targetOwner && targetOwner != me && targetOwner->IsPvP() && !me->IsInDuelWith(targetOwner))
{
me->UpdatePvP(true);
me->RemoveAurasWithInterruptFlags(AURA_INTERRUPT_FLAG_PVP_ACTIVE_CANCELS);
break;
}
}
}
}
if (m_spellInfo->HasAttribute(SPELL_ATTR_EX_REQUIRE_ALL_TARGETS) &&
(type == TARGET_TYPE_UNIT || type == TARGET_TYPE_PLAYER))
{
// spells which should only be cast if a target was found
if (unitTargetList.size() <= 0)
{
SendCastResult(m_spellInfo->Id == 51690 ? SPELL_FAILED_OUT_OF_RANGE : SPELL_FAILED_BAD_TARGETS);
finish(false);
return false;
}
}
for (Unit* unit : unitTargetList)
AddUnitTarget(unit, effectMask);
}
return true;
}
void Spell::prepareDataForTriggerSystem()
{
//==========================================================================================
// Now fill data for trigger system, need know:
// an spell trigger another or not ( m_canTrigger )
// Create base triggers flags for Attacker and Victim ( m_procAttacker and m_procVictim)
//==========================================================================================
// Fill flag can spell trigger or not
// TODO: possible exist spell attribute for this
m_canTrigger = true;
if ((m_CastItem && m_spellInfo->SpellFamilyFlags == nullptr) || m_doNotProc)
m_canTrigger = false; // Do not trigger from item cast spell
// some negative spells have positive effects to another or same targets
// avoid triggering negative hit for only positive targets
m_negativeEffectMask = 0x0;
for (int i = 0; i < MAX_EFFECT_INDEX; ++i)
if (!IsPositiveEffect(m_spellInfo, SpellEffectIndex(i)))
m_negativeEffectMask |= (1 << i);
}
void Spell::PrepareMasksForProcSystem(uint8 effectMask, uint32& procAttacker, uint32& procVictim, WorldObject* caster, WorldObject* target)
{
// Get data for type of attack and fill base info for trigger
switch (m_spellInfo->DmgClass)
{
case SPELL_DAMAGE_CLASS_MELEE:
procAttacker = PROC_FLAG_DEAL_MELEE_ABILITY;
if (m_attackType == BASE_ATTACK)
procAttacker |= PROC_FLAG_MAIN_HAND_WEAPON_SWING;
if (m_attackType == OFF_ATTACK)
procAttacker |= PROC_FLAG_OFF_HAND_WEAPON_SWING;
procVictim = PROC_FLAG_TAKE_MELEE_ABILITY;
break;
case SPELL_DAMAGE_CLASS_RANGED:
// Auto attack
if (m_spellInfo->HasAttribute(SPELL_ATTR_EX2_AUTO_REPEAT))
{
procAttacker = PROC_FLAG_DEAL_RANGED_ATTACK;
procVictim = PROC_FLAG_TAKE_RANGED_ATTACK;
}
else // Ranged spell attack
{
procAttacker = PROC_FLAG_DEAL_RANGED_ABILITY;
procVictim = PROC_FLAG_TAKE_RANGED_ABILITY;
}
break;
default:
if (IsPositiveEffectMask(m_spellInfo, effectMask, caster, target)) // Check for positive spell
{
if (m_spellInfo->HasAttribute(SPELL_ATTR_IS_ABILITY))
{
procAttacker = PROC_FLAG_DEAL_HELPFUL_ABILITY;
procVictim = PROC_FLAG_TAKE_HELPFUL_ABILITY;
}
else
{
procAttacker = PROC_FLAG_DEAL_HELPFUL_SPELL;
procVictim = PROC_FLAG_TAKE_HELPFUL_SPELL;
}
}
else if (m_spellInfo->HasAttribute(SPELL_ATTR_EX2_AUTO_REPEAT)) // Wands auto attack
{
procAttacker = PROC_FLAG_DEAL_RANGED_ATTACK;
procVictim = PROC_FLAG_TAKE_RANGED_ATTACK;
}
else // Negative spell
{
if (m_spellInfo->HasAttribute(SPELL_ATTR_IS_ABILITY))
{
procAttacker = PROC_FLAG_DEAL_HARMFUL_ABILITY;
procVictim = PROC_FLAG_TAKE_HARMFUL_ABILITY;
}
else
{
procAttacker = PROC_FLAG_DEAL_HARMFUL_SPELL;
procVictim = PROC_FLAG_TAKE_HARMFUL_SPELL;
}
}
break;
}
// Hunter traps spells (for Entrapment trigger)
// Gives your Immolation Trap, Frost Trap, Explosive Trap, and Snake Trap ....
if (m_spellInfo->SpellFamilyName == SPELLFAMILY_HUNTER && m_spellInfo->IsFitToFamilyMask(uint64(0x000020000000001C), uint32(0x00040000)))
procAttacker |= PROC_FLAG_ON_TRAP_ACTIVATION;
if (IsNextMeleeSwingSpell(m_spellInfo))
{
procAttacker |= PROC_FLAG_DEAL_MELEE_SWING;
procVictim |= PROC_FLAG_TAKE_MELEE_SWING;
}
if (m_spellInfo->HasAttribute(SPELL_ATTR_EX3_TREAT_AS_PERIODIC))
{
procAttacker = PROC_FLAG_DEAL_HARMFUL_PERIODIC;
procVictim = PROC_FLAG_TAKE_HARMFUL_PERIODIC;
}
if (m_spellInfo->HasAttribute(SPELL_ATTR_EX3_SUPPRESS_CASTER_PROCS))
procAttacker = 0;
// in wotlk SPELL_ATTR_EX3_SUPPRESS_TARGET_PROCS must be done in proc system due to SPELL_ATTR_EX7_CAN_PROC_FROM_SUPPRESSED_TARGET_PROCS
//if (m_spellInfo->HasAttribute(SPELL_ATTR_EX3_SUPPRESS_TARGET_PROCS))
// procVictim = 0;
}
void Spell::CleanupTargetList()
{
m_UniqueTargetInfo.clear();
m_UniqueGOTargetInfo.clear();
m_UniqueItemInfo.clear();
m_delayMoment = 0;
}
void Spell::AddUnitTarget(Unit* target, uint8 effectMask, CheckException exception)
{
if (m_trueCaster != target)
m_targets.m_targetMask |= TARGET_FLAG_UNIT; // all spells with unit target must have this flag
effectMask &= (~m_partialApplicationMask); // remove partially immuned out effects
uint8 executionlessMask = 0;
auto& data = SpellTargetMgr::GetSpellTargetingData(m_spellInfo->Id);
for (uint32 i = 0; i < MAX_EFFECT_INDEX; ++i)
if (data.implicitType[i] == TARGET_TYPE_SPECIAL_DEST)
executionlessMask |= (1 << i);
// Check for effect immune skip if immuned
uint8 notImmunedMask = 0;
for (uint8 effIndex = 0; effIndex < MAX_EFFECT_INDEX; ++effIndex)
if ((effectMask & (1 << effIndex)) != 0)
if (!target->IsImmuneToSpellEffect(m_spellInfo, SpellEffectIndex(effIndex), target == m_trueCaster))