/
Spell.cpp
8500 lines (7447 loc) · 360 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 "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.h"
#include "Chat/Chat.h"
#include "Entities/Vehicle.h"
#include "Entities/TemporarySpawn.h"
#include "Server/SQLStorages.h"
#include "MotionGenerators/PathFinder.h"
#include "Entities/ObjectGuid.h"
extern pEffect SpellEffects[TOTAL_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->GetHealth() * 100 / unit->GetMaxHealth();
}
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_itemTargetEntry = 0;
m_srcX = m_srcY = m_srcZ = m_destX = m_destY = m_destZ = 0.0f;
m_strTarget.clear();
m_targetMask = 0;
m_elevation = 0.0f;
m_speed = 0.0f;
}
SpellCastTargets::~SpellCastTargets()
{
}
void SpellCastTargets::setUnitTarget(Unit* target)
{
if (!target)
return;
m_destX = target->GetPositionX();
m_destY = target->GetPositionY();
m_destZ = target->GetPositionZ();
m_unitTarget = target;
m_unitTargetGUID = target->GetObjectGuid();
m_targetMask |= TARGET_FLAG_UNIT;
}
void SpellCastTargets::setDestination(float x, float y, float z)
{
m_destX = x;
m_destY = y;
m_destZ = z;
m_targetMask |= TARGET_FLAG_DEST_LOCATION;
}
void SpellCastTargets::setSource(float x, float y, float z)
{
m_srcX = x;
m_srcY = y;
m_srcZ = z;
m_targetMask |= TARGET_FLAG_SOURCE_LOCATION;
}
void SpellCastTargets::setGOTarget(GameObject* target)
{
m_GOTarget = target;
m_GOTargetGUID = target->GetObjectGuid();
// m_targetMask |= TARGET_FLAG_OBJECT;
}
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::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_CorpseTargetGUID = corpse->GetObjectGuid();
}
void SpellCastTargets::Update(Unit* caster)
{
m_GOTarget = m_GOTargetGUID ? caster->GetMap()->GetGameObject(m_GOTargetGUID) : nullptr;
m_unitTarget = m_unitTargetGUID ?
(m_unitTargetGUID == caster->GetObjectGuid() ? caster : ObjectAccessor::GetUnit(*caster, m_unitTargetGUID)) :
nullptr;
m_itemTarget = nullptr;
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();
}
}
void SpellCastTargets::read(ByteBuffer& data, Unit* caster)
{
data >> m_targetMask;
if (m_targetMask == TARGET_FLAG_SELF)
{
m_destX = caster->GetPositionX();
m_destY = caster->GetPositionY();
m_destZ = caster->GetPositionZ();
m_unitTarget = caster;
m_unitTargetGUID = caster->GetObjectGuid();
return;
}
// TARGET_FLAG_UNK2 is used for non-combat pets, maybe other?
if (m_targetMask & (TARGET_FLAG_UNIT | TARGET_FLAG_UNK2))
data >> m_unitTargetGUID.ReadAsPacked();
if (m_targetMask & (TARGET_FLAG_OBJECT | TARGET_FLAG_GAMEOBJECT_ITEM))
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_PVP_CORPSE))
data >> m_CorpseTargetGUID.ReadAsPacked();
if (m_targetMask & TARGET_FLAG_SOURCE_LOCATION)
{
data >> m_srcTransportGUID.ReadAsPacked();
data >> m_srcX >> m_srcY >> m_srcZ;
if (!MaNGOS::IsValidMapCoord(m_srcX, m_srcY, m_srcZ))
throw ByteBufferException(false, data.rpos(), 0, data.size());
}
if (m_targetMask & TARGET_FLAG_DEST_LOCATION)
{
data >> m_destTransportGUID.ReadAsPacked();
data >> m_destX >> m_destY >> m_destZ;
if (!MaNGOS::IsValidMapCoord(m_destX, m_destY, m_destZ))
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_PVP_CORPSE | TARGET_FLAG_OBJECT | TARGET_FLAG_CORPSE_ALLY | TARGET_FLAG_UNK2))
{
if (m_targetMask & TARGET_FLAG_UNIT)
{
if (m_unitTarget)
data << m_unitTarget->GetPackGUID();
else
data << uint8(0);
}
else if (m_targetMask & TARGET_FLAG_OBJECT)
{
if (m_GOTarget)
data << m_GOTarget->GetPackGUID();
else
data << uint8(0);
}
else if (m_targetMask & (TARGET_FLAG_CORPSE_ALLY | TARGET_FLAG_PVP_CORPSE))
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_srcX << m_srcY << m_srcZ;
}
if (m_targetMask & TARGET_FLAG_DEST_LOCATION)
{
data << m_destTransportGUID.WriteAsPacked();
data << m_destX << m_destY << m_destZ;
}
if (m_targetMask & TARGET_FLAG_STRING)
data << m_strTarget;
}
void SpellCastTargets::ReadAdditionalSpellData(ByteBuffer& data, uint8 castFlags)
{
if (castFlags & 0x02) // Trajectory data
{
data >> m_elevation;
data >> m_speed;
uint8 moveFlag;
data >> moveFlag;
if (moveFlag)
{
ObjectGuid guid; // unk guid (possible - active mover) - unused
MovementInfo movementInfo; // MovementInfo
data >> Unused<uint32>(); // >> MSG_MOVE_STOP
data >> guid.ReadAsPacked();
data >> movementInfo;
setSource(movementInfo.GetPos()->x, movementInfo.GetPos()->y, movementInfo.GetPos()->z);
}
}
// Note: no other additional spell data can be found in 3.3.5a. However newer versions have additional information (4.x has flag 0x08 for archeology)
}
// SpellLog class
// **************
void SpellLog::Initialize()
{
m_spellLogData.Initialize(SMSG_SPELLLOGEXECUTE);
//m_spellLogData << m_spell->GetCaster()->GetPackGUID();
m_spellLogData.appendPackGUID(m_spell->GetCaster()->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(TOTAL_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(TOTAL_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->GetCaster()->SendMessageToSet(m_spellLogData, true);
// make it ready for another log if need
Initialize();
}
// Spell class
// ***********
Spell::Spell(Unit* caster, SpellEntry const* info, uint32 triggeredFlags, ObjectGuid originalCasterGUID, SpellEntry const* triggeredBy) :
m_spellLog(this)
{
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 = caster;
m_referencedFromCurrentSpell = false;
m_executedCurrently = false;
m_delayStart = 0;
m_delayAtDamageCount = 0;
m_applyMultiplierMask = 0;
// 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)
if (m_attackType == RANGED_ATTACK)
{
// wand case
if ((m_caster->getClassMask() & CLASSMASK_WAND_USERS) != 0 && m_caster->GetTypeId() == TYPEID_PLAYER)
{
if (Item* pItem = ((Player*)m_caster)->GetWeaponForAttack(RANGED_ATTACK))
m_spellSchoolMask = SpellSchoolMask(1 << pItem->GetProto()->Damage[0].DamageType);
}
}
// Set health leech amount to zero
m_healthLeech = 0;
m_originalCasterGUID = originalCasterGUID ? originalCasterGUID : m_caster->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;
unitTarget = nullptr;
itemTarget = nullptr;
gameObjTarget = nullptr;
focusObject = 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_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_duration = 0;
m_maxRange = 0.f;
m_needAliveTargetMask = 0;
m_ignoreHitResult = !!(triggeredFlags & TRIGGERED_IGNORE_HIT_CALCULATION);
m_ignoreUnselectableTarget = (m_IsTriggeredSpell | !!(triggeredFlags & TRIGGERED_IGNORE_UNSELECTABLE_FLAG));
m_ignoreUnattackableTarget = !!(triggeredFlags & TRIGGERED_IGNORE_UNATTACKABLE_FLAG);
m_triggerAutorepeat = !!(triggeredFlags & TRIGGERED_AUTOREPEAT);
m_doNotProc = triggeredFlags & TRIGGERED_DO_NOT_PROC;
m_petCast = !!(triggeredFlags & TRIGGERED_PET_CAST);
m_reflectable = IsReflectableSpell(m_spellInfo);
m_spellFlags = SPELL_FLAG_NORMAL;
m_affectedTargetCount = GetAffectedTargets(m_spellInfo, caster);
m_scriptValue = 0;
CleanupTargetList();
m_spellLog.Initialize();
m_needSpellLog = (m_spellInfo->Attributes & (SPELL_ATTR_HIDE_IN_COMBAT_LOG | SPELL_ATTR_HIDDEN_CLIENTSIDE)) == 0;
}
Spell::~Spell()
{
}
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()
{
// TODO: ADD the correct target FILLS!!!!!!
UnitList tmpUnitLists[MAX_EFFECT_INDEX]; // Stores the temporary Target Lists for each effect
uint8 effToIndex[MAX_EFFECT_INDEX] = {0, 1, 2}; // Helper array, to link to another tmpUnitList, if the targets for both effects match
for (int 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;
// targets for TARGET_SCRIPT_COORDINATES (A) and TARGET_SCRIPT
// for TARGET_FOCUS_OR_SCRIPTED_GAMEOBJECT (A) all is checked in Spell::CheckCast and in Spell::CheckItem
// filled in Spell::CheckCast call
if (m_spellInfo->EffectImplicitTargetA[i] == TARGET_SCRIPT_COORDINATES ||
m_spellInfo->EffectImplicitTargetA[i] == TARGET_FOCUS_OR_SCRIPTED_GAMEOBJECT ||
(m_spellInfo->EffectImplicitTargetA[i] == TARGET_SCRIPT && m_spellInfo->EffectImplicitTargetB[i] != TARGET_SELF) ||
(m_spellInfo->EffectImplicitTargetB[i] == TARGET_SCRIPT && m_spellInfo->EffectImplicitTargetA[i] != TARGET_SELF))
continue;
// TODO: find a way so this is not needed?
// for area auras always add caster as target (needed for totems for example)
if (IsAreaAuraEffect(m_spellInfo->Effect[i]))
AddUnitTarget(m_caster, SpellEffectIndex(i));
// no double fill for same targets
for (int j = 0; j < i; ++j)
{
// Check if same target, but handle i.e. AreaAuras different
if (m_spellInfo->EffectImplicitTargetA[i] == m_spellInfo->EffectImplicitTargetA[j] && m_spellInfo->EffectImplicitTargetB[i] == m_spellInfo->EffectImplicitTargetB[j]
&& m_spellInfo->Effect[j] != SPELL_EFFECT_NONE
&& !IsAreaAuraEffect(m_spellInfo->Effect[i]) && !IsAreaAuraEffect(m_spellInfo->Effect[j]))
// Add further conditions here if required
{
effToIndex[i] = j; // effect i has same targeting list as effect j
break;
}
}
if (effToIndex[i] == i) // New target combination
{
// TargetA/TargetB dependent from each other, we not switch to full support this dependences
// but need it support in some know cases
switch (m_spellInfo->EffectImplicitTargetA[i])
{
case TARGET_NONE:
switch (m_spellInfo->EffectImplicitTargetB[i])
{
case TARGET_NONE:
// TODO: TARGETING: move this to default target selection
// TODO: Investigate TARGET_FLAG relationship with TARGET_NONE
if (m_spellInfo->Targets)
{
if (m_spellInfo->Targets & (TARGET_FLAG_UNIT_TARGET | TARGET_FLAG_UNIT))
{
if (m_targets.getUnitTarget())
tmpUnitLists[i].push_back(m_targets.getUnitTarget());
}
else if (m_spellInfo->Targets & (TARGET_FLAG_PVP_CORPSE | TARGET_FLAG_CORPSE_ALLY))
{
if (m_targets.getUnitTarget())
tmpUnitLists[i].push_back(m_targets.getUnitTarget());
else if (m_targets.getCorpseTargetGuid())
{
if (Corpse* corpse = m_caster->GetMap()->GetCorpse(m_targets.getCorpseTargetGuid()))
if (Player* owner = ObjectAccessor::FindPlayer(corpse->GetOwnerGuid()))
tmpUnitLists[i].push_back(owner);
}
}
else if (m_spellInfo->Targets & (TARGET_FLAG_ITEM | TARGET_FLAG_TRADE_ITEM))
{
if (m_targets.getItemTarget())
AddItemTarget(m_targets.getItemTarget(), SpellEffectIndex(i));
}
else if (m_spellInfo->Targets & (TARGET_FLAG_DEST_LOCATION))
{
if (!(m_targets.m_targetMask & TARGET_FLAG_DEST_LOCATION))
{
sLog.outError("No destination for spell with flag TARGET_FLAG_DEST_LOCATION, spell ID: %u", m_spellInfo->Id); // should never occur
if (WorldObject* caster = GetCastingObject())
m_targets.setDestination(caster->GetPositionX(), caster->GetPositionY(), caster->GetPositionZ());
}
tmpUnitLists[i].push_back(m_caster); // add caster so that we can cast dest only effects
}
break; // uses flag for target
}
switch (m_spellInfo->Effect[i])
{
case SPELL_EFFECT_SCHOOL_DAMAGE: // Spell 34451 suggests this
SetTargetMap(SpellEffectIndex(i), TARGET_CHAIN_DAMAGE, tmpUnitLists[i /*==effToIndex[i]*/]);
break;
case SPELL_EFFECT_BIND: // guessed based on 3286
case SPELL_EFFECT_CREATE_ITEM:
case SPELL_EFFECT_LEARN_SPELL: // Sniffs suggest supplied target
case SPELL_EFFECT_SKILL_STEP:
case SPELL_EFFECT_SKILL:
case SPELL_EFFECT_BREAK_PLAYER_TARGETING:
case SPELL_EFFECT_APPLY_GLYPH:
case SPELL_EFFECT_RESURRECT: // no spell guesswork
case SPELL_EFFECT_RESURRECT_NEW: // no spell guesswork
case SPELL_EFFECT_SKIN_PLAYER_CORPSE: // no spell guesswork
case SPELL_EFFECT_APPLY_AURA:
case SPELL_EFFECT_SUMMON_PLAYER: // guessed based on 7720 sniff data
SetTargetMap(SpellEffectIndex(i), TARGET_DUELVSPLAYER, tmpUnitLists[i /*==effToIndex[i]*/]);
break;
case SPELL_EFFECT_LEARN_PET_SPELL: // Always targets pet supplied from client
SetTargetMap(SpellEffectIndex(i), TARGET_PET, tmpUnitLists[i /*==effToIndex[i]*/]); // No spell like this exists, guesswork
break;
case SPELL_EFFECT_DUMMY:
{
bool end = true;
switch (m_spellInfo->Id) // place for enabling scriptability for dummy targeting
{
case 20577: // Cannibalize
{
WorldObject* result = FindCorpseUsing<MaNGOS::CannibalizeObjectCheck>();
if (result)
{
switch (result->GetTypeId())
{
case TYPEID_UNIT:
case TYPEID_PLAYER:
tmpUnitLists[i].push_back((Unit*)result);
break;
case TYPEID_CORPSE:
m_targets.setCorpseTarget((Corpse*)result);
if (Player* owner = ObjectAccessor::FindPlayer(((Corpse*)result)->GetOwnerGuid()))
tmpUnitLists[i].push_back(owner);
break;
}
}
else
{
// clear cooldown at fail
if (m_caster->GetTypeId() == TYPEID_PLAYER)
m_caster->RemoveSpellCooldown(*m_spellInfo, true);
SendCastResult(SPELL_FAILED_NO_EDIBLE_CORPSES);
finish(false);
}
break;
}
default:
end = false; // continue to TARGET_DUELVSPLAYER
break;
}
if (end)
break;
}
case SPELL_EFFECT_STUCK: // guessed based on 7355
case SPELL_EFFECT_TRIGGER_SPELL: // guessed based on 37851
SetTargetMap(SpellEffectIndex(i), TARGET_SELF, tmpUnitLists[i /*==effToIndex[i]*/]);
break;
case SPELL_EFFECT_SUMMON: // no spell guesswork - dest only effect
case SPELL_EFFECT_TRANS_DOOR:
SetTargetMap(SpellEffectIndex(i), TARGET_EFFECT_SELECT, tmpUnitLists[i /*==effToIndex[i]*/]);
break;
case SPELL_EFFECT_ENCHANT_ITEM:
case SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY:
case SPELL_EFFECT_ENCHANT_ITEM_PRISMATIC:
case SPELL_EFFECT_DISENCHANT:
case SPELL_EFFECT_FEED_PET: // no spell guesswork
case SPELL_EFFECT_PROSPECTING: // no spell guesswork
case SPELL_EFFECT_MILLING:
case SPELL_EFFECT_ADD_FARSIGHT:
case SPELL_EFFECT_TELEPORT_UNITS:
sLog.outError("Reached invalid default target for spell: %d.", m_spellInfo->Id); // should never occur
break;
case SPELL_EFFECT_SUMMON_CHANGE_ITEM: // no targeting needed, all data is in m_castItem - only works when item cast
case SPELL_EFFECT_SUMMON_ALL_TOTEMS:
case SPELL_EFFECT_DESTROY_ALL_TOTEMS: // no targeting needed, no data is needed - iteration through totem slots
SetTargetMap(SpellEffectIndex(i), TARGET_SELF, tmpUnitLists[i /*==effToIndex[i]*/]); // workaround for item only effects
break;
default: // old Legacy code
if (m_caster->GetObjectGuid().IsPet())
SetTargetMap(SpellEffectIndex(i), TARGET_SELF, tmpUnitLists[i /*==effToIndex[i]*/]);
else
SetTargetMap(SpellEffectIndex(i), TARGET_EFFECT_SELECT, tmpUnitLists[i /*==effToIndex[i]*/]);
break;
}
break;
default:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
}
break;
case TARGET_SELF:
switch (m_spellInfo->EffectImplicitTargetB[i])
{
case TARGET_NONE: // Fill Target based on A only
case TARGET_EFFECT_SELECT:
case TARGET_SCRIPT: // B-target only used with CheckCast here
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
case TARGET_AREAEFFECT_INSTANT: // use B case that not dependent from from A in fact
if ((m_targets.m_targetMask & TARGET_FLAG_DEST_LOCATION) == 0)
m_targets.setDestination(m_caster->GetPositionX(), m_caster->GetPositionY(), m_caster->GetPositionZ());
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
case TARGET_BEHIND_VICTIM: // use B case that not dependent from from A in fact
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
default:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
}
break;
case TARGET_EFFECT_SELECT:
switch (m_spellInfo->EffectImplicitTargetB[i])
{
case TARGET_NONE:
case TARGET_EFFECT_SELECT:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
// dest point setup required
case TARGET_AREAEFFECT_INSTANT:
case TARGET_AREAEFFECT_CUSTOM:
case TARGET_ALL_ENEMY_IN_AREA:
case TARGET_ALL_ENEMY_IN_AREA_INSTANT:
case TARGET_ALL_ENEMY_IN_AREA_CHANNELED:
case TARGET_ALL_FRIENDLY_UNITS_IN_AREA:
case TARGET_AREAEFFECT_GO_AROUND_DEST:
case TARGET_RANDOM_NEARBY_DEST:
// triggered spells get dest point from default target set, ignore it
if (!(m_targets.m_targetMask & TARGET_FLAG_DEST_LOCATION) || m_IsTriggeredSpell)
if (WorldObject* castObject = GetCastingObject())
m_targets.setDestination(castObject->GetPositionX(), castObject->GetPositionY(), castObject->GetPositionZ());
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
// target pre-selection required
case TARGET_INNKEEPER_COORDINATES:
case TARGET_TABLE_X_Y_Z_COORDINATES:
case TARGET_CASTER_COORDINATES:
case TARGET_SCRIPT_COORDINATES:
case TARGET_CURRENT_ENEMY_COORDINATES:
case TARGET_DUELVSPLAYER_COORDINATES:
case TARGET_DYNAMIC_OBJECT_COORDINATES:
case TARGET_POINT_AT_NORTH:
case TARGET_POINT_AT_SOUTH:
case TARGET_POINT_AT_EAST:
case TARGET_POINT_AT_WEST:
case TARGET_POINT_AT_NE:
case TARGET_POINT_AT_NW:
case TARGET_POINT_AT_SE:
case TARGET_POINT_AT_SW:
// need some target for processing
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
default:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
}
break;
case TARGET_CASTER_COORDINATES:
switch (m_spellInfo->EffectImplicitTargetB[i])
{
case TARGET_ALL_ENEMY_IN_AREA:
// Note: this hack with search required until GO casting not implemented
// environment damage spells already have around enemies targeting but this not help in case nonexistent GO casting support
// currently each enemy selected explicitly and self cast damage
if (m_spellInfo->Effect[i] == SPELL_EFFECT_ENVIRONMENTAL_DAMAGE)
{
if (m_targets.getUnitTarget())
tmpUnitLists[i /*==effToIndex[i]*/].push_back(m_targets.getUnitTarget());
}
else
{
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
}
break;
case TARGET_NONE:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
tmpUnitLists[i /*==effToIndex[i]*/].push_back(m_caster);
break;
default:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
}
break;
case TARGET_TABLE_X_Y_Z_COORDINATES:
switch (m_spellInfo->EffectImplicitTargetB[i])
{
case TARGET_NONE:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
// need some target for processing
SetTargetMap(SpellEffectIndex(i), TARGET_EFFECT_SELECT, tmpUnitLists[i /*==effToIndex[i]*/]);
break;
case TARGET_AREAEFFECT_INSTANT: // All 17/7 pairs used for dest teleportation, A processed in effect code
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
default:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
}
break;
case TARGET_SELF2:
switch (m_spellInfo->EffectImplicitTargetB[i])
{
case TARGET_NONE:
case TARGET_EFFECT_SELECT:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
case TARGET_AREAEFFECT_CUSTOM:
// triggered spells get dest point from default target set, ignore it
if (!(m_targets.m_targetMask & TARGET_FLAG_DEST_LOCATION) || m_IsTriggeredSpell)
if (WorldObject* castObject = GetCastingObject())
m_targets.setDestination(castObject->GetPositionX(), castObject->GetPositionY(), castObject->GetPositionZ());
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
// most A/B target pairs is self->negative and not expect adding caster to target list
default:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
}
break;
case TARGET_DUELVSPLAYER_COORDINATES:
switch (m_spellInfo->EffectImplicitTargetB[i])
{
case TARGET_NONE:
case TARGET_EFFECT_SELECT:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
if (Unit* currentTarget = m_targets.getUnitTarget())
tmpUnitLists[i /*==effToIndex[i]*/].push_back(currentTarget);
break;
default:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
}
break;
case TARGET_SCRIPT:
switch (m_spellInfo->EffectImplicitTargetB[i])
{
case TARGET_SELF:
// Fill target based on B only, A is only used with CheckCast here.
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
default:
break;
}
break;
default:
switch (m_spellInfo->EffectImplicitTargetB[i])
{
case TARGET_NONE:
case TARGET_EFFECT_SELECT:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
case TARGET_SCRIPT_COORDINATES: // B case filled in CheckCast but we need fill unit list base at A case
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
default:
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetA[i], tmpUnitLists[i /*==effToIndex[i]*/]);
SetTargetMap(SpellEffectIndex(i), m_spellInfo->EffectImplicitTargetB[i], tmpUnitLists[i /*==effToIndex[i]*/]);
break;
}
break;
}
}
if (m_caster->GetTypeId() == TYPEID_PLAYER)
{
Player* me = (Player*)m_caster;
for (UnitList::const_iterator itr = tmpUnitLists[effToIndex[i]].begin(); itr != tmpUnitLists[effToIndex[i]].end(); ++itr)
{
Player* targetOwner = (*itr)->GetBeneficiaryPlayer();
if (targetOwner && targetOwner != me && targetOwner->IsPvP() && !me->IsInDuelWith(targetOwner))
{
me->UpdatePvP(true);
me->RemoveAurasWithInterruptFlags(AURA_INTERRUPT_FLAG_ENTER_PVP_COMBAT);
break;
}
}
}
for (UnitList::iterator itr = tmpUnitLists[effToIndex[i]].begin(); itr != tmpUnitLists[effToIndex[i]].end();)
{
if (!CheckTarget(*itr, SpellEffectIndex(i)))
{
itr = tmpUnitLists[effToIndex[i]].erase(itr);
continue;
}
else
++itr;
}
if (m_affectedTargetCount && tmpUnitLists[effToIndex[i]].size() > m_affectedTargetCount)
{
// remove random units from the map
while (tmpUnitLists[effToIndex[i]].size() > m_affectedTargetCount)
{
uint32 poz = urand(0, tmpUnitLists[effToIndex[i]].size() - 1);
for (UnitList::iterator itr = tmpUnitLists[effToIndex[i]].begin(); itr != tmpUnitLists[effToIndex[i]].end(); ++itr, --poz)
{
if (!*itr) continue;
if (!poz)
{
itr = tmpUnitLists[effToIndex[i]].erase(itr);
break;
}
}
}
}
for (UnitList::const_iterator iunit = tmpUnitLists[effToIndex[i]].begin(); iunit != tmpUnitLists[effToIndex[i]].end(); ++iunit)
AddUnitTarget((*iunit), SpellEffectIndex(i));
}
}
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 = false;
if ((m_CastItem && m_spellInfo->SpellFamilyFlags == 0) || m_spellInfo->HasAttribute(SPELL_ATTR_EX3_CANT_TRIGGER_PROC) || m_doNotProc)
m_canTrigger = false; // Do not trigger from item cast spell
else if (!m_IsTriggeredSpell)
m_canTrigger = true; // Normal cast - can trigger
else if (!m_triggeredByAuraSpell)
m_canTrigger = true; // Triggered from SPELL_EFFECT_TRIGGER_SPELL - can trigger
else if (m_spellInfo->HasAttribute(SPELL_ATTR_EX2_TRIGGERED_CAN_TRIGGER_PROC) || m_spellInfo->HasAttribute(SPELL_ATTR_EX3_TRIGGERED_CAN_TRIGGER_SPECIAL))
m_canTrigger = true; // Spells with these special attributes can trigger even if triggeredByAuraSpell
if (!m_canTrigger) // Exceptions (some periodic triggers)
{
switch (m_spellInfo->SpellFamilyName)
{
case SPELLFAMILY_MAGE:
// Arcane Missiles / Blizzard triggers need do it
if (m_spellInfo->IsFitToFamilyMask(uint64(0x0000000000200080)))
m_canTrigger = true;
// Clearcasting trigger need do it
else if (m_spellInfo->IsFitToFamilyMask(uint64(0x0000000200000000)))
m_canTrigger = true;
// Replenish Mana, item spell with triggered cases (Mana Agate, etc mana gems)
else if (m_spellInfo->IsFitToFamilyMask(uint64(0x0000010000000000)))
m_canTrigger = true;
break;
case SPELLFAMILY_WARLOCK:
// For Hellfire Effect / Rain of Fire / Seed of Corruption triggers need do it
if (m_spellInfo->IsFitToFamilyMask(uint64(0x0000800000000060)))
m_canTrigger = true;
break;
case SPELLFAMILY_PRIEST:
// For Penance,Mind Sear,Mind Flay heal/damage triggers need do it
if (m_spellInfo->IsFitToFamilyMask(uint64(0x0001800000800000), 0x00000040))
m_canTrigger = true;
break;
case SPELLFAMILY_ROGUE:
// For poisons need do it
if (m_spellInfo->IsFitToFamilyMask(uint64(0x000000101001E000)))
m_canTrigger = true;
break;
case SPELLFAMILY_HUNTER:
// Hunter Rapid Killing/Explosive Trap Effect/Immolation Trap Effect/Frost Trap Aura/Snake Trap Effect/Explosive Shot