2 * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "Database/DatabaseEnv.h"
21 #include "WorldPacket.h"
22 #include "WorldSession.h"
23 #include "GridNotifiers.h"
24 #include "GridNotifiersImpl.h"
27 #include "UpdateMask.h"
29 #include "ObjectMgr.h"
35 #include "DynamicObject.h"
37 #include "UpdateData.h"
38 #include "MapManager.h"
39 #include "ObjectAccessor.h"
41 #include "Policies/SingletonImp.h"
42 #include "SharedDefines.h"
44 #include "VMapFactory.h"
45 #include "BattleGround.h"
48 #define SPELL_CHANNEL_UPDATE_INTERVAL (1 * IN_MILISECONDS)
50 extern pEffect SpellEffects
[TOTAL_SPELL_EFFECTS
];
52 class PrioritizeManaUnitWraper
55 explicit PrioritizeManaUnitWraper(Unit
* unit
) : i_unit(unit
)
57 uint32 maxmana
= unit
->GetMaxPower(POWER_MANA
);
58 i_percent
= maxmana
? unit
->GetPower(POWER_MANA
) * 100 / maxmana
: 101;
60 Unit
* getUnit() const { return i_unit
; }
61 uint32
getPercent() const { return i_percent
; }
69 int operator()( PrioritizeManaUnitWraper
const& x
, PrioritizeManaUnitWraper
const& y
) const
71 return x
.getPercent() < y
.getPercent();
75 typedef std::priority_queue
<PrioritizeManaUnitWraper
, std::vector
<PrioritizeManaUnitWraper
>, PrioritizeMana
> PrioritizeManaUnitQueue
;
77 class PrioritizeHealthUnitWraper
80 explicit PrioritizeHealthUnitWraper(Unit
* unit
) : i_unit(unit
)
82 i_percent
= unit
->GetHealth() * 100 / unit
->GetMaxHealth();
84 Unit
* getUnit() const { return i_unit
; }
85 uint32
getPercent() const { return i_percent
; }
91 struct PrioritizeHealth
93 int operator()( PrioritizeHealthUnitWraper
const& x
, PrioritizeHealthUnitWraper
const& y
) const
95 return x
.getPercent() < y
.getPercent();
99 typedef std::priority_queue
<PrioritizeHealthUnitWraper
, std::vector
<PrioritizeHealthUnitWraper
>, PrioritizeHealth
> PrioritizeHealthUnitQueue
;
101 bool IsQuestTameSpell(uint32 spellId
)
103 SpellEntry
const *spellproto
= sSpellStore
.LookupEntry(spellId
);
107 return spellproto
->Effect
[EFFECT_INDEX_0
] == SPELL_EFFECT_THREAT
108 && spellproto
->Effect
[EFFECT_INDEX_1
] == SPELL_EFFECT_APPLY_AURA
&& spellproto
->EffectApplyAuraName
[EFFECT_INDEX_1
] == SPELL_AURA_DUMMY
;
111 SpellCastTargets::SpellCastTargets()
117 m_unitTargetGUID
= 0;
119 m_CorpseTargetGUID
= 0;
120 m_itemTargetGUID
= 0;
121 m_itemTargetEntry
= 0;
123 m_srcX
= m_srcY
= m_srcZ
= m_destX
= m_destY
= m_destZ
= 0.0f
;
128 SpellCastTargets::~SpellCastTargets()
132 void SpellCastTargets::setUnitTarget(Unit
*target
)
137 m_destX
= target
->GetPositionX();
138 m_destY
= target
->GetPositionY();
139 m_destZ
= target
->GetPositionZ();
140 m_unitTarget
= target
;
141 m_unitTargetGUID
= target
->GetGUID();
142 m_targetMask
|= TARGET_FLAG_UNIT
;
145 void SpellCastTargets::setDestination(float x
, float y
, float z
)
150 m_targetMask
|= TARGET_FLAG_DEST_LOCATION
;
153 void SpellCastTargets::setSource(float x
, float y
, float z
)
158 m_targetMask
|= TARGET_FLAG_SOURCE_LOCATION
;
161 void SpellCastTargets::setGOTarget(GameObject
*target
)
164 m_GOTargetGUID
= target
->GetGUID();
165 // m_targetMask |= TARGET_FLAG_OBJECT;
168 void SpellCastTargets::setItemTarget(Item
* item
)
174 m_itemTargetGUID
= item
->GetGUID();
175 m_itemTargetEntry
= item
->GetEntry();
176 m_targetMask
|= TARGET_FLAG_ITEM
;
179 void SpellCastTargets::setCorpseTarget(Corpse
* corpse
)
181 m_CorpseTargetGUID
= corpse
->GetGUID();
184 void SpellCastTargets::Update(Unit
* caster
)
186 m_GOTarget
= !m_GOTargetGUID
.IsEmpty() ? caster
->GetMap()->GetGameObject(m_GOTargetGUID
) : NULL
;
187 m_unitTarget
= !m_unitTargetGUID
.IsEmpty() ?
188 ( m_unitTargetGUID
== caster
->GetObjectGuid() ? caster
: ObjectAccessor::GetUnit(*caster
, m_unitTargetGUID
) ) :
192 if(caster
->GetTypeId() == TYPEID_PLAYER
)
194 if(m_targetMask
& TARGET_FLAG_ITEM
)
195 m_itemTarget
= ((Player
*)caster
)->GetItemByGuid(m_itemTargetGUID
);
196 else if(m_targetMask
& TARGET_FLAG_TRADE_ITEM
)
198 Player
* pTrader
= ((Player
*)caster
)->GetTrader();
199 if(pTrader
&& m_itemTargetGUID
.GetRawValue() < TRADE_SLOT_COUNT
)
200 m_itemTarget
= pTrader
->GetItemByPos(pTrader
->GetItemPosByTradeSlot(uint32(m_itemTargetGUID
.GetRawValue())));
203 m_itemTargetEntry
= m_itemTarget
->GetEntry();
207 void SpellCastTargets::read( ByteBuffer
& data
, Unit
*caster
)
209 data
>> m_targetMask
;
211 if(m_targetMask
== TARGET_FLAG_SELF
)
213 m_destX
= caster
->GetPositionX();
214 m_destY
= caster
->GetPositionY();
215 m_destZ
= caster
->GetPositionZ();
216 m_unitTarget
= caster
;
217 m_unitTargetGUID
= caster
->GetGUID();
221 // TARGET_FLAG_UNK2 is used for non-combat pets, maybe other?
222 if( m_targetMask
& ( TARGET_FLAG_UNIT
| TARGET_FLAG_UNK2
))
223 data
>> m_unitTargetGUID
.ReadAsPacked();
225 if( m_targetMask
& ( TARGET_FLAG_OBJECT
))
226 data
>> m_GOTargetGUID
.ReadAsPacked();
228 if(( m_targetMask
& ( TARGET_FLAG_ITEM
| TARGET_FLAG_TRADE_ITEM
)) && caster
->GetTypeId() == TYPEID_PLAYER
)
229 data
>> m_itemTargetGUID
.ReadAsPacked();
231 if( m_targetMask
& (TARGET_FLAG_CORPSE
| TARGET_FLAG_PVP_CORPSE
) )
232 data
>> m_CorpseTargetGUID
.ReadAsPacked();
234 if( m_targetMask
& TARGET_FLAG_SOURCE_LOCATION
)
236 data
>> m_unitTargetGUID
.ReadAsPacked();
237 data
>> m_srcX
>> m_srcY
>> m_srcZ
;
238 if(!MaNGOS::IsValidMapCoord(m_srcX
, m_srcY
, m_srcZ
))
239 throw ByteBufferException(false, data
.rpos(), 0, data
.size());
242 if( m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
244 data
>> m_unitTargetGUID
.ReadAsPacked();
245 data
>> m_destX
>> m_destY
>> m_destZ
;
246 if(!MaNGOS::IsValidMapCoord(m_destX
, m_destY
, m_destZ
))
247 throw ByteBufferException(false, data
.rpos(), 0, data
.size());
250 if( m_targetMask
& TARGET_FLAG_STRING
)
253 // find real units/GOs
257 void SpellCastTargets::write( ByteBuffer
& data
) const
259 data
<< uint32(m_targetMask
);
261 if( m_targetMask
& ( TARGET_FLAG_UNIT
| TARGET_FLAG_PVP_CORPSE
| TARGET_FLAG_OBJECT
| TARGET_FLAG_CORPSE
| TARGET_FLAG_UNK2
) )
263 if(m_targetMask
& TARGET_FLAG_UNIT
)
266 data
<< m_unitTarget
->GetPackGUID();
270 else if( m_targetMask
& TARGET_FLAG_OBJECT
)
273 data
<< m_GOTarget
->GetPackGUID();
277 else if( m_targetMask
& ( TARGET_FLAG_CORPSE
| TARGET_FLAG_PVP_CORPSE
) )
278 data
<< m_CorpseTargetGUID
.WriteAsPacked();
283 if( m_targetMask
& ( TARGET_FLAG_ITEM
| TARGET_FLAG_TRADE_ITEM
) )
286 data
<< m_itemTarget
->GetPackGUID();
291 if( m_targetMask
& TARGET_FLAG_SOURCE_LOCATION
)
294 data
<< m_unitTarget
->GetPackGUID();
298 data
<< m_srcX
<< m_srcY
<< m_srcZ
;
301 if( m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
304 data
<< m_unitTarget
->GetPackGUID();
308 data
<< m_destX
<< m_destY
<< m_destZ
;
311 if( m_targetMask
& TARGET_FLAG_STRING
)
315 Spell::Spell( Unit
* Caster
, SpellEntry
const *info
, bool triggered
, ObjectGuid originalCasterGUID
, Spell
** triggeringContainer
)
317 ASSERT( Caster
!= NULL
&& info
!= NULL
);
318 ASSERT( info
== sSpellStore
.LookupEntry( info
->Id
) && "`info` must be pointer to sSpellStore element");
322 m_selfContainer
= NULL
;
323 m_triggeringContainer
= triggeringContainer
;
324 m_referencedFromCurrentSpell
= false;
325 m_executedCurrently
= false;
327 m_delayAtDamageCount
= 0;
329 m_applyMultiplierMask
= 0;
331 // Get data for type of attack
332 m_attackType
= GetWeaponAttackType(m_spellInfo
);
334 m_spellSchoolMask
= GetSpellSchoolMask(info
); // Can be override for some spell (wand shoot for example)
336 if(m_attackType
== RANGED_ATTACK
)
339 if((m_caster
->getClassMask() & CLASSMASK_WAND_USERS
) != 0 && m_caster
->GetTypeId() == TYPEID_PLAYER
)
341 if(Item
* pItem
= ((Player
*)m_caster
)->GetWeaponForAttack(RANGED_ATTACK
))
342 m_spellSchoolMask
= SpellSchoolMask(1 << pItem
->GetProto()->Damage
[0].DamageType
);
345 // Set health leech amount to zero
348 if(!originalCasterGUID
.IsEmpty())
349 m_originalCasterGUID
= originalCasterGUID
;
351 m_originalCasterGUID
= m_caster
->GetObjectGuid();
353 UpdateOriginalCasterPointer();
355 for(int i
= 0; i
< MAX_EFFECT_INDEX
; ++i
)
356 m_currentBasePoints
[i
] = m_spellInfo
->EffectBasePoints
[i
];
358 m_spellState
= SPELL_STATE_NULL
;
360 m_castPositionX
= m_castPositionY
= m_castPositionZ
= 0;
361 m_TriggerSpells
.clear();
362 m_preCastSpells
.clear();
363 m_IsTriggeredSpell
= triggered
;
364 //m_AreaAura = false;
369 gameObjTarget
= NULL
;
373 m_triggeredByAuraSpell
= NULL
;
375 //Auto Shot & Shoot (wand)
376 m_autoRepeat
= IsAutoRepeatRangedSpell(m_spellInfo
);
379 m_powerCost
= 0; // setup to correct value in Spell::prepare, don't must be used before.
380 m_casttime
= 0; // setup to correct value in Spell::prepare, don't must be used before.
381 m_timer
= 0; // will set to cast time in prepare
383 m_needAliveTargetMask
= 0;
385 // determine reflection
386 m_canReflect
= false;
388 if(m_spellInfo
->DmgClass
== SPELL_DAMAGE_CLASS_MAGIC
&& !(m_spellInfo
->AttributesEx2
& SPELL_ATTR_EX2_CANT_REFLECTED
))
390 for(int j
= 0; j
< MAX_EFFECT_INDEX
; ++j
)
392 if (m_spellInfo
->Effect
[j
] == 0)
395 if(!IsPositiveTarget(m_spellInfo
->EffectImplicitTargetA
[j
], m_spellInfo
->EffectImplicitTargetB
[j
]))
398 m_canReflect
= (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_NEGATIVE
) ? true : false;
415 WorldObject
* Spell::FindCorpseUsing()
417 // non-standard target selection
418 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
419 float max_range
= GetSpellMaxRange(srange
);
421 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
423 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
426 WorldObject
* result
= NULL
;
428 T
u_check(m_caster
, max_range
);
429 MaNGOS::WorldObjectSearcher
<T
> searcher(m_caster
, result
, u_check
);
431 TypeContainerVisitor
<MaNGOS::WorldObjectSearcher
<T
>, GridTypeMapContainer
> grid_searcher(searcher
);
432 cell
.Visit(p
, grid_searcher
, *m_caster
->GetMap(), *m_caster
, max_range
);
436 TypeContainerVisitor
<MaNGOS::WorldObjectSearcher
<T
>, WorldTypeMapContainer
> world_searcher(searcher
);
437 cell
.Visit(p
, world_searcher
, *m_caster
->GetMap(), *m_caster
, max_range
);
443 void Spell::FillTargetMap()
445 // TODO: ADD the correct target FILLS!!!!!!
447 for(int i
= 0; i
< MAX_EFFECT_INDEX
; ++i
)
449 // not call for empty effect.
450 // Also some spells use not used effect targets for store targets for dummy effect in triggered spells
451 if(m_spellInfo
->Effect
[i
] == 0)
454 // targets for TARGET_SCRIPT_COORDINATES (A) and TARGET_SCRIPT
455 // for TARGET_FOCUS_OR_SCRIPTED_GAMEOBJECT (A) all is checked in Spell::CheckCast and in Spell::CheckItem
456 // filled in Spell::CheckCast call
457 if(m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SCRIPT_COORDINATES
||
458 m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SCRIPT
||
459 m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_FOCUS_OR_SCRIPTED_GAMEOBJECT
||
460 (m_spellInfo
->EffectImplicitTargetB
[i
] == TARGET_SCRIPT
&& m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_SELF
))
463 // TODO: find a way so this is not needed?
464 // for area auras always add caster as target (needed for totems for example)
465 if(IsAreaAuraEffect(m_spellInfo
->Effect
[i
]))
466 AddUnitTarget(m_caster
, SpellEffectIndex(i
));
468 std::list
<Unit
*> tmpUnitMap
;
470 // TargetA/TargetB dependent from each other, we not switch to full support this dependences
471 // but need it support in some know cases
472 switch(m_spellInfo
->EffectImplicitTargetA
[i
])
475 switch(m_spellInfo
->EffectImplicitTargetB
[i
])
478 SetTargetMap(SpellEffectIndex(i
), TARGET_EFFECT_SELECT
, tmpUnitMap
);
481 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetB
[i
], tmpUnitMap
);
486 switch(m_spellInfo
->EffectImplicitTargetB
[i
])
489 case TARGET_EFFECT_SELECT
:
490 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetA
[i
], tmpUnitMap
);
492 case TARGET_AREAEFFECT_INSTANT
: // use B case that not dependent from from A in fact
493 if((m_targets
.m_targetMask
& TARGET_FLAG_DEST_LOCATION
) == 0)
494 m_targets
.setDestination(m_caster
->GetPositionX(), m_caster
->GetPositionY(), m_caster
->GetPositionZ());
495 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetB
[i
], tmpUnitMap
);
497 case TARGET_BEHIND_VICTIM
: // use B case that not dependent from from A in fact
498 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetB
[i
], tmpUnitMap
);
501 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetA
[i
], tmpUnitMap
);
502 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetB
[i
], tmpUnitMap
);
506 case TARGET_EFFECT_SELECT
:
507 switch(m_spellInfo
->EffectImplicitTargetB
[i
])
510 case TARGET_EFFECT_SELECT
:
511 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetA
[i
], tmpUnitMap
);
513 case TARGET_INNKEEPER_COORDINATES
:
514 case TARGET_TABLE_X_Y_Z_COORDINATES
:
515 case TARGET_CASTER_COORDINATES
:
516 case TARGET_SCRIPT_COORDINATES
:
517 case TARGET_CURRENT_ENEMY_COORDINATES
:
518 case TARGET_DUELVSPLAYER_COORDINATES
:
519 case TARGET_DYNAMIC_OBJECT_COORDINATES
:
520 case TARGET_POINT_AT_NORTH
:
521 case TARGET_POINT_AT_SOUTH
:
522 case TARGET_POINT_AT_EAST
:
523 case TARGET_POINT_AT_WEST
:
524 case TARGET_POINT_AT_NE
:
525 case TARGET_POINT_AT_NW
:
526 case TARGET_POINT_AT_SE
:
527 case TARGET_POINT_AT_SW
:
528 // need some target for processing
529 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetA
[i
], tmpUnitMap
);
530 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetB
[i
], tmpUnitMap
);
533 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetB
[i
], tmpUnitMap
);
537 case TARGET_CASTER_COORDINATES
:
538 switch(m_spellInfo
->EffectImplicitTargetB
[i
])
540 case TARGET_ALL_ENEMY_IN_AREA
:
541 // Note: this hack with search required until GO casting not implemented
542 // environment damage spells already have around enemies targeting but this not help in case not existed GO casting support
543 // currently each enemy selected explicitly and self cast damage
544 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_ENVIRONMENTAL_DAMAGE
)
546 if(m_targets
.getUnitTarget())
547 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
551 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetA
[i
], tmpUnitMap
);
552 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetB
[i
], tmpUnitMap
);
556 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetA
[i
], tmpUnitMap
);
557 tmpUnitMap
.push_back(m_caster
);
560 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetA
[i
], tmpUnitMap
);
561 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetB
[i
], tmpUnitMap
);
565 case TARGET_TABLE_X_Y_Z_COORDINATES
:
566 switch(m_spellInfo
->EffectImplicitTargetB
[i
])
569 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetA
[i
], tmpUnitMap
);
571 // need some target for processing
572 SetTargetMap(SpellEffectIndex(i
), TARGET_EFFECT_SELECT
, tmpUnitMap
);
574 case TARGET_AREAEFFECT_INSTANT
: // All 17/7 pairs used for dest teleportation, A processed in effect code
575 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetB
[i
], tmpUnitMap
);
578 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetA
[i
], tmpUnitMap
);
579 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetB
[i
], tmpUnitMap
);
584 switch(m_spellInfo
->EffectImplicitTargetB
[i
])
587 case TARGET_EFFECT_SELECT
:
588 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetA
[i
], tmpUnitMap
);
590 // most A/B target pairs is self->negative and not expect adding caster to target list
592 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetB
[i
], tmpUnitMap
);
597 switch(m_spellInfo
->EffectImplicitTargetB
[i
])
600 case TARGET_EFFECT_SELECT
:
601 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetA
[i
], tmpUnitMap
);
603 case TARGET_SCRIPT_COORDINATES
: // B case filled in CheckCast but we need fill unit list base at A case
604 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetA
[i
], tmpUnitMap
);
607 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetA
[i
], tmpUnitMap
);
608 SetTargetMap(SpellEffectIndex(i
), m_spellInfo
->EffectImplicitTargetB
[i
], tmpUnitMap
);
614 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
616 Player
*me
= (Player
*)m_caster
;
617 for (std::list
<Unit
*>::const_iterator itr
= tmpUnitMap
.begin(); itr
!= tmpUnitMap
.end(); ++itr
)
619 Unit
*owner
= (*itr
)->GetOwner();
620 Unit
*u
= owner
? owner
: (*itr
);
621 if(u
!=m_caster
&& u
->IsPvP() && (!me
->duel
|| me
->duel
->opponent
!= u
))
624 me
->RemoveAurasWithInterruptFlags(AURA_INTERRUPT_FLAG_ENTER_PVP_COMBAT
);
630 for (std::list
<Unit
*>::iterator itr
= tmpUnitMap
.begin(); itr
!= tmpUnitMap
.end();)
632 if (!CheckTarget (*itr
, SpellEffectIndex(i
)))
634 itr
= tmpUnitMap
.erase(itr
);
641 for(std::list
<Unit
*>::const_iterator iunit
= tmpUnitMap
.begin(); iunit
!= tmpUnitMap
.end(); ++iunit
)
642 AddUnitTarget((*iunit
), SpellEffectIndex(i
));
646 void Spell::prepareDataForTriggerSystem()
648 //==========================================================================================
649 // Now fill data for trigger system, need know:
650 // an spell trigger another or not ( m_canTrigger )
651 // Create base triggers flags for Attacker and Victim ( m_procAttacker and m_procVictim)
652 //==========================================================================================
653 // Fill flag can spell trigger or not
654 // TODO: possible exist spell attribute for this
655 m_canTrigger
= false;
658 m_canTrigger
= false; // Do not trigger from item cast spell
659 else if (!m_IsTriggeredSpell
)
660 m_canTrigger
= true; // Normal cast - can trigger
661 else if (!m_triggeredByAuraSpell
)
662 m_canTrigger
= true; // Triggered from SPELL_EFFECT_TRIGGER_SPELL - can trigger
664 if (!m_canTrigger
) // Exceptions (some periodic triggers)
666 switch (m_spellInfo
->SpellFamilyName
)
668 case SPELLFAMILY_MAGE
:
669 // Arcane Missles / Blizzard triggers need do it
670 if (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x0000000000200080))
672 // Clearcasting trigger need do it
673 else if (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x0000000200000000) && m_spellInfo
->SpellFamilyFlags2
& 0x8)
675 // Replenish Mana, item spell with triggered cases (Mana Agate, etc mana gems)
676 else if (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x0000010000000000))
679 case SPELLFAMILY_WARLOCK
:
680 // For Hellfire Effect / Rain of Fire / Seed of Corruption triggers need do it
681 if (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x0000800000000060))
684 case SPELLFAMILY_PRIEST
:
685 // For Penance,Mind Sear,Mind Flay heal/damage triggers need do it
686 if (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x0001800000800000) || (m_spellInfo
->SpellFamilyFlags2
& 0x00000040))
689 case SPELLFAMILY_ROGUE
:
690 // For poisons need do it
691 if (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x000000101001E000))
694 case SPELLFAMILY_HUNTER
:
695 // Hunter Rapid Killing/Explosive Trap Effect/Immolation Trap Effect/Frost Trap Aura/Snake Trap Effect/Explosive Shot
696 if ((m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x0100200000000214)) ||
697 m_spellInfo
->SpellFamilyFlags2
& 0x200)
700 case SPELLFAMILY_PALADIN
:
701 // For Judgements (all) / Holy Shock triggers need do it
702 if (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x0001000900B80400))
710 // Get data for type of attack and fill base info for trigger
711 switch (m_spellInfo
->DmgClass
)
713 case SPELL_DAMAGE_CLASS_MELEE
:
714 m_procAttacker
= PROC_FLAG_SUCCESSFUL_MELEE_SPELL_HIT
;
715 m_procVictim
= PROC_FLAG_TAKEN_MELEE_SPELL_HIT
;
717 case SPELL_DAMAGE_CLASS_RANGED
:
719 if (m_spellInfo
->AttributesEx2
& SPELL_ATTR_EX2_AUTOREPEAT_FLAG
)
721 m_procAttacker
= PROC_FLAG_SUCCESSFUL_RANGED_HIT
;
722 m_procVictim
= PROC_FLAG_TAKEN_RANGED_HIT
;
724 else // Ranged spell attack
726 m_procAttacker
= PROC_FLAG_SUCCESSFUL_RANGED_SPELL_HIT
;
727 m_procVictim
= PROC_FLAG_TAKEN_RANGED_SPELL_HIT
;
731 if (IsPositiveSpell(m_spellInfo
->Id
)) // Check for positive spell
733 m_procAttacker
= PROC_FLAG_SUCCESSFUL_POSITIVE_SPELL
;
734 m_procVictim
= PROC_FLAG_TAKEN_POSITIVE_SPELL
;
736 else if (m_spellInfo
->AttributesEx2
& SPELL_ATTR_EX2_AUTOREPEAT_FLAG
) // Wands auto attack
738 m_procAttacker
= PROC_FLAG_SUCCESSFUL_RANGED_HIT
;
739 m_procVictim
= PROC_FLAG_TAKEN_RANGED_HIT
;
741 else // Negative spell
743 m_procAttacker
= PROC_FLAG_SUCCESSFUL_NEGATIVE_SPELL_HIT
;
744 m_procVictim
= PROC_FLAG_TAKEN_NEGATIVE_SPELL_HIT
;
748 // Hunter traps spells (for Entrapment trigger)
749 // Gives your Immolation Trap, Frost Trap, Explosive Trap, and Snake Trap ....
750 if (m_spellInfo
->SpellFamilyName
== SPELLFAMILY_HUNTER
&& (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x000020000000001C)))
751 m_procAttacker
|= PROC_FLAG_ON_TRAP_ACTIVATION
;
754 void Spell::CleanupTargetList()
756 m_UniqueTargetInfo
.clear();
757 m_UniqueGOTargetInfo
.clear();
758 m_UniqueItemInfo
.clear();
762 void Spell::AddUnitTarget(Unit
* pVictim
, SpellEffectIndex effIndex
)
764 if( m_spellInfo
->Effect
[effIndex
] == 0 )
767 // Check for effect immune skip if immuned
768 bool immuned
= pVictim
->IsImmunedToSpellEffect(m_spellInfo
, effIndex
);
770 ObjectGuid targetGUID
= pVictim
->GetObjectGuid();
772 // Lookup target in already in list
773 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end(); ++ihit
)
775 if (targetGUID
== ihit
->targetGUID
) // Found in list
778 ihit
->effectMask
|= (1 << effIndex
); // Add only effect mask if not immuned
783 // This is new target calculate data for him
785 // Get spell hit result on target
787 target
.targetGUID
= targetGUID
; // Store target GUID
788 target
.effectMask
= immuned
? 0 : (1 << effIndex
); // Store index of effect if not immuned
789 target
.processed
= false; // Effects not apply on target
791 // Calculate hit result
792 target
.missCondition
= m_caster
->SpellHitResult(pVictim
, m_spellInfo
, m_canReflect
);
794 // Spell have speed - need calculate incoming time
795 if (m_spellInfo
->speed
> 0.0f
)
797 // calculate spell incoming interval
798 float dist
= m_caster
->GetDistance(pVictim
->GetPositionX(), pVictim
->GetPositionY(), pVictim
->GetPositionZ());
799 if (dist
< 5.0f
) dist
= 5.0f
;
800 target
.timeDelay
= (uint64
) floor(dist
/ m_spellInfo
->speed
* 1000.0f
);
802 // Calculate minimum incoming time
803 if (m_delayMoment
== 0 || m_delayMoment
>target
.timeDelay
)
804 m_delayMoment
= target
.timeDelay
;
807 target
.timeDelay
= UI64LIT(0);
809 // If target reflect spell back to caster
810 if (target
.missCondition
== SPELL_MISS_REFLECT
)
812 // Calculate reflected spell result on caster
813 target
.reflectResult
= m_caster
->SpellHitResult(m_caster
, m_spellInfo
, m_canReflect
);
815 if (target
.reflectResult
== SPELL_MISS_REFLECT
) // Impossible reflect again, so simply deflect spell
816 target
.reflectResult
= SPELL_MISS_PARRY
;
818 // Increase time interval for reflected spells by 1.5
819 target
.timeDelay
+= target
.timeDelay
>> 1;
822 target
.reflectResult
= SPELL_MISS_NONE
;
824 // Add target to list
825 m_UniqueTargetInfo
.push_back(target
);
828 void Spell::AddUnitTarget(uint64 unitGUID
, SpellEffectIndex effIndex
)
830 Unit
* unit
= m_caster
->GetGUID() == unitGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, unitGUID
);
832 AddUnitTarget(unit
, effIndex
);
835 void Spell::AddGOTarget(GameObject
* pVictim
, SpellEffectIndex effIndex
)
837 if( m_spellInfo
->Effect
[effIndex
] == 0 )
840 ObjectGuid targetGUID
= pVictim
->GetObjectGuid();
842 // Lookup target in already in list
843 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin(); ihit
!= m_UniqueGOTargetInfo
.end(); ++ihit
)
845 if (targetGUID
== ihit
->targetGUID
) // Found in list
847 ihit
->effectMask
|= (1 << effIndex
); // Add only effect mask
852 // This is new target calculate data for him
855 target
.targetGUID
= targetGUID
;
856 target
.effectMask
= (1 << effIndex
);
857 target
.processed
= false; // Effects not apply on target
859 // Spell have speed - need calculate incoming time
860 if (m_spellInfo
->speed
> 0.0f
)
862 // calculate spell incoming interval
863 float dist
= m_caster
->GetDistance(pVictim
->GetPositionX(), pVictim
->GetPositionY(), pVictim
->GetPositionZ());
866 target
.timeDelay
= (uint64
) floor(dist
/ m_spellInfo
->speed
* 1000.0f
);
867 if (m_delayMoment
== 0 || m_delayMoment
> target
.timeDelay
)
868 m_delayMoment
= target
.timeDelay
;
871 target
.timeDelay
= UI64LIT(0);
873 // Add target to list
874 m_UniqueGOTargetInfo
.push_back(target
);
877 void Spell::AddGOTarget(uint64 goGUID
, SpellEffectIndex effIndex
)
879 GameObject
* go
= m_caster
->GetMap()->GetGameObject(goGUID
);
881 AddGOTarget(go
, effIndex
);
884 void Spell::AddItemTarget(Item
* pitem
, SpellEffectIndex effIndex
)
886 if( m_spellInfo
->Effect
[effIndex
] == 0 )
889 // Lookup target in already in list
890 for(std::list
<ItemTargetInfo
>::iterator ihit
= m_UniqueItemInfo
.begin(); ihit
!= m_UniqueItemInfo
.end(); ++ihit
)
892 if (pitem
== ihit
->item
) // Found in list
894 ihit
->effectMask
|= (1 << effIndex
); // Add only effect mask
899 // This is new target add data
901 ItemTargetInfo target
;
903 target
.effectMask
= (1 << effIndex
);
904 m_UniqueItemInfo
.push_back(target
);
907 void Spell::DoAllEffectOnTarget(TargetInfo
*target
)
909 if (target
->processed
) // Check target
911 target
->processed
= true; // Target checked in apply effects procedure
913 // Get mask of effects for target
914 uint32 mask
= target
->effectMask
;
916 Unit
* unit
= m_caster
->GetObjectGuid() == target
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, target
->targetGUID
);
920 // Get original caster (if exist) and calculate damage/healing from him data
921 Unit
*real_caster
= GetAffectiveCaster();
922 // FIXME: in case wild GO heal/damage spells will be used target bonuses
923 Unit
*caster
= real_caster
? real_caster
: m_caster
;
925 SpellMissInfo missInfo
= target
->missCondition
;
926 // Need init unitTarget by default unit (can changed in code on reflect)
927 // Or on missInfo!=SPELL_MISS_NONE unitTarget undefined (but need in trigger subsystem)
930 // Reset damage/healing counter
934 // Fill base trigger info
935 uint32 procAttacker
= m_procAttacker
;
936 uint32 procVictim
= m_procVictim
;
937 uint32 procEx
= PROC_EX_NONE
;
939 if (missInfo
==SPELL_MISS_NONE
) // In case spell hit target, do all effect on that target
940 DoSpellHitOnUnit(unit
, mask
);
941 else if (missInfo
== SPELL_MISS_REFLECT
) // In case spell reflect from target, do all effect on caster (if hit)
943 if (target
->reflectResult
== SPELL_MISS_NONE
) // If reflected spell hit caster -> do all effect on him
944 DoSpellHitOnUnit(m_caster
, mask
);
947 // All calculated do it!
948 // Do healing and triggers
951 bool crit
= real_caster
&& real_caster
->isSpellCrit(unitTarget
, m_spellInfo
, m_spellSchoolMask
);
952 uint32 addhealth
= m_healing
;
955 procEx
|= PROC_EX_CRITICAL_HIT
;
956 addhealth
= caster
->SpellCriticalHealingBonus(m_spellInfo
, addhealth
, NULL
);
959 procEx
|= PROC_EX_NORMAL_HIT
;
961 // Do triggers for unit (reflect triggers passed on hit phase for correct drop charge)
962 if (m_canTrigger
&& missInfo
!= SPELL_MISS_REFLECT
)
964 caster
->ProcDamageAndSpell(unitTarget
, real_caster
? procAttacker
: PROC_FLAG_NONE
, procVictim
, procEx
, addhealth
, m_attackType
, m_spellInfo
);
967 int32 gain
= caster
->DealHeal(unitTarget
, addhealth
, m_spellInfo
, crit
);
970 unitTarget
->getHostileRefManager().threatAssist(real_caster
, float(gain
) * 0.5f
, m_spellInfo
);
972 // Do damage and triggers
975 // Fill base damage struct (unitTarget - is real spell target)
976 SpellNonMeleeDamage
damageInfo(caster
, unitTarget
, m_spellInfo
->Id
, m_spellSchoolMask
);
978 // Add bonuses and fill damageInfo struct
979 caster
->CalculateSpellDamage(&damageInfo
, m_damage
, m_spellInfo
, m_attackType
);
981 caster
->DealDamageMods(damageInfo
.target
, damageInfo
.damage
, &damageInfo
.absorb
);
983 // Send log damage message to client
984 caster
->SendSpellNonMeleeDamageLog(&damageInfo
);
986 procEx
= createProcExtendMask(&damageInfo
, missInfo
);
987 procVictim
|= PROC_FLAG_TAKEN_ANY_DAMAGE
;
989 // Do triggers for unit (reflect triggers passed on hit phase for correct drop charge)
990 if (m_canTrigger
&& missInfo
!= SPELL_MISS_REFLECT
)
991 caster
->ProcDamageAndSpell(unitTarget
, real_caster
? procAttacker
: PROC_FLAG_NONE
, procVictim
, procEx
, damageInfo
.damage
, m_attackType
, m_spellInfo
);
993 // Haunt (NOTE: for avoid use additional field damage stored in dummy value (replace unused 100%)
994 // apply before deal damage because aura can be removed at target kill
995 if (m_spellInfo
->SpellFamilyName
== SPELLFAMILY_WARLOCK
&& m_spellInfo
->SpellIconID
== 3172 &&
996 (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x0004000000000000)))
997 if(Aura
* dummy
= unitTarget
->GetDummyAura(m_spellInfo
->Id
))
998 dummy
->GetModifier()->m_amount
= damageInfo
.damage
;
1000 caster
->DealSpellDamage(&damageInfo
, true);
1002 // Passive spell hits/misses or active spells only misses (only triggers)
1005 // Fill base damage struct (unitTarget - is real spell target)
1006 SpellNonMeleeDamage
damageInfo(caster
, unitTarget
, m_spellInfo
->Id
, m_spellSchoolMask
);
1007 procEx
= createProcExtendMask(&damageInfo
, missInfo
);
1008 // Do triggers for unit (reflect triggers passed on hit phase for correct drop charge)
1009 if (m_canTrigger
&& missInfo
!= SPELL_MISS_REFLECT
)
1010 caster
->ProcDamageAndSpell(unit
, real_caster
? procAttacker
: PROC_FLAG_NONE
, procVictim
, procEx
, 0, m_attackType
, m_spellInfo
);
1013 // Call scripted function for AI if this spell is casted upon a creature
1014 if (unit
->GetTypeId() == TYPEID_UNIT
)
1016 // cast at creature (or GO) quest objectives update at successful cast finished (+channel finished)
1017 // ignore pets or autorepeat/melee casts for speed (not exist quest for spells (hm... )
1018 if (real_caster
&& !((Creature
*)unit
)->isPet() && !IsAutoRepeat() && !IsNextMeleeSwingSpell() && !IsChannelActive())
1019 if (Player
* p
= real_caster
->GetCharmerOrOwnerPlayerOrPlayerItself())
1020 p
->CastedCreatureOrGO(unit
->GetEntry(), unit
->GetObjectGuid(), m_spellInfo
->Id
);
1022 if(((Creature
*)unit
)->AI())
1023 ((Creature
*)unit
)->AI()->SpellHit(m_caster
, m_spellInfo
);
1026 // Call scripted function for AI if this spell is casted by a creature
1027 if (m_caster
->GetTypeId() == TYPEID_UNIT
&& ((Creature
*)m_caster
)->AI())
1028 ((Creature
*)m_caster
)->AI()->SpellHitTarget(unit
, m_spellInfo
);
1031 void Spell::DoSpellHitOnUnit(Unit
*unit
, const uint32 effectMask
)
1033 if (!unit
|| !effectMask
)
1036 Unit
* realCaster
= GetAffectiveCaster();
1038 // Recheck immune (only for delayed spells)
1039 if (m_spellInfo
->speed
&& (
1040 unit
->IsImmunedToDamage(GetSpellSchoolMask(m_spellInfo
)) ||
1041 unit
->IsImmunedToSpell(m_spellInfo
)))
1044 realCaster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_IMMUNE
);
1048 if (unit
->GetTypeId() == TYPEID_PLAYER
)
1050 ((Player
*)unit
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_BE_SPELL_TARGET
, m_spellInfo
->Id
);
1051 ((Player
*)unit
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_BE_SPELL_TARGET2
, m_spellInfo
->Id
);
1054 if (realCaster
&& realCaster
->GetTypeId() == TYPEID_PLAYER
)
1055 ((Player
*)realCaster
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_CAST_SPELL2
, m_spellInfo
->Id
, 0, unit
);
1057 if (realCaster
&& realCaster
!= unit
)
1059 // Recheck UNIT_FLAG_NON_ATTACKABLE for delayed spells
1060 if (m_spellInfo
->speed
> 0.0f
&&
1061 unit
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NON_ATTACKABLE
) &&
1062 unit
->GetCharmerOrOwnerGUID() != m_caster
->GetGUID())
1064 realCaster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_EVADE
);
1068 if (!realCaster
->IsFriendlyTo(unit
))
1070 // for delayed spells ignore not visible explicit target
1071 if (m_spellInfo
->speed
> 0.0f
&& unit
== m_targets
.getUnitTarget() &&
1072 !unit
->isVisibleForOrDetect(m_caster
, m_caster
, false))
1074 realCaster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_EVADE
);
1078 // not break stealth by cast targeting
1079 if (!(m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_NOT_BREAK_STEALTH
))
1080 unit
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
1082 // can cause back attack (if detected), stealth removed at Spell::cast if spell break it
1083 if (!(m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_NO_INITIAL_AGGRO
) && !IsPositiveSpell(m_spellInfo
->Id
) &&
1084 m_caster
->isVisibleForOrDetect(unit
, unit
, false))
1086 // use speedup check to avoid re-remove after above lines
1087 if (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_NOT_BREAK_STEALTH
)
1088 unit
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
1090 // caster can be detected but have stealth aura
1091 m_caster
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
1093 if (!unit
->IsStandState() && !unit
->hasUnitState(UNIT_STAT_STUNNED
))
1094 unit
->SetStandState(UNIT_STAND_STATE_STAND
);
1096 if (!unit
->isInCombat() && unit
->GetTypeId() != TYPEID_PLAYER
&& ((Creature
*)unit
)->AI())
1097 ((Creature
*)unit
)->AI()->AttackedBy(realCaster
);
1099 unit
->AddThreat(realCaster
);
1100 unit
->SetInCombatWith(realCaster
);
1101 realCaster
->SetInCombatWith(unit
);
1103 if (Player
*attackedPlayer
= unit
->GetCharmerOrOwnerPlayerOrPlayerItself())
1104 realCaster
->SetContestedPvP(attackedPlayer
);
1109 // for delayed spells ignore negative spells (after duel end) for friendly targets
1110 if (m_spellInfo
->speed
> 0.0f
&& !IsPositiveSpell(m_spellInfo
->Id
))
1112 realCaster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_EVADE
);
1116 // assisting case, healing and resurrection
1117 if (unit
->hasUnitState(UNIT_STAT_ATTACK_PLAYER
))
1118 realCaster
->SetContestedPvP();
1120 if (unit
->isInCombat() && !(m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_NO_INITIAL_AGGRO
))
1122 realCaster
->SetInCombatState(unit
->GetCombatTimer() > 0);
1123 unit
->getHostileRefManager().threatAssist(realCaster
, 0.0f
, m_spellInfo
);
1128 // Get Data Needed for Diminishing Returns, some effects may have multiple auras, so this must be done on spell hit, not aura add
1129 m_diminishGroup
= GetDiminishingReturnsGroupForSpell(m_spellInfo
,m_triggeredByAuraSpell
);
1130 m_diminishLevel
= unit
->GetDiminishing(m_diminishGroup
);
1131 // Increase Diminishing on unit, current informations for actually casts will use values above
1132 if ((GetDiminishingReturnsGroupType(m_diminishGroup
) == DRTYPE_PLAYER
&& unit
->GetTypeId() == TYPEID_PLAYER
) ||
1133 GetDiminishingReturnsGroupType(m_diminishGroup
) == DRTYPE_ALL
)
1134 unit
->IncrDiminishing(m_diminishGroup
);
1136 // Apply additional spell effects to target
1137 CastPreCastSpells(unit
);
1139 for(int effectNumber
= 0; effectNumber
< MAX_EFFECT_INDEX
; ++effectNumber
)
1141 if (effectMask
& (1 << effectNumber
))
1143 HandleEffects(unit
, NULL
, NULL
, SpellEffectIndex(effectNumber
), m_damageMultipliers
[effectNumber
]);
1144 if ( m_applyMultiplierMask
& (1 << effectNumber
) )
1147 float multiplier
= m_spellInfo
->DmgMultiplier
[effectNumber
];
1148 // Apply multiplier mods
1150 if(Player
* modOwner
= realCaster
->GetSpellModOwner())
1151 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_EFFECT_PAST_FIRST
, multiplier
, this);
1152 m_damageMultipliers
[effectNumber
] *= multiplier
;
1158 void Spell::DoAllEffectOnTarget(GOTargetInfo
*target
)
1160 if (target
->processed
) // Check target
1162 target
->processed
= true; // Target checked in apply effects procedure
1164 uint32 effectMask
= target
->effectMask
;
1168 GameObject
* go
= m_caster
->GetMap()->GetGameObject(target
->targetGUID
);
1172 for(int effectNumber
= 0; effectNumber
< MAX_EFFECT_INDEX
; ++effectNumber
)
1173 if (effectMask
& (1 << effectNumber
))
1174 HandleEffects(NULL
, NULL
, go
, SpellEffectIndex(effectNumber
));
1176 // cast at creature (or GO) quest objectives update at successful cast finished (+channel finished)
1177 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
1178 if( !IsAutoRepeat() && !IsNextMeleeSwingSpell() && !IsChannelActive() )
1180 if ( Player
* p
= m_caster
->GetCharmerOrOwnerPlayerOrPlayerItself() )
1181 p
->CastedCreatureOrGO(go
->GetEntry(), go
->GetObjectGuid(), m_spellInfo
->Id
);
1185 void Spell::DoAllEffectOnTarget(ItemTargetInfo
*target
)
1187 uint32 effectMask
= target
->effectMask
;
1188 if(!target
->item
|| !effectMask
)
1191 for(int effectNumber
= 0; effectNumber
< MAX_EFFECT_INDEX
; ++effectNumber
)
1192 if (effectMask
& (1 << effectNumber
))
1193 HandleEffects(NULL
, target
->item
, NULL
, SpellEffectIndex(effectNumber
));
1196 bool Spell::IsAliveUnitPresentInTargetList()
1198 // Not need check return true
1199 if (m_needAliveTargetMask
== 0)
1202 uint8 needAliveTargetMask
= m_needAliveTargetMask
;
1204 for(std::list
<TargetInfo
>::const_iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end(); ++ihit
)
1206 if( ihit
->missCondition
== SPELL_MISS_NONE
&& (needAliveTargetMask
& ihit
->effectMask
) )
1208 Unit
*unit
= m_caster
->GetObjectGuid() == ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
1210 // either unit is alive and normal spell, or unit dead and deathonly-spell
1211 if (unit
&& (unit
->isAlive() != IsDeathOnlySpell(m_spellInfo
)))
1212 needAliveTargetMask
&= ~ihit
->effectMask
; // remove from need alive mask effect that have alive target
1216 // is all effects from m_needAliveTargetMask have alive targets
1217 return needAliveTargetMask
== 0;
1220 // Helper for Chain Healing
1221 // Spell target first
1222 // Raidmates then descending by injury suffered (MaxHealth - Health)
1223 // Other players/mobs then descending by injury suffered (MaxHealth - Health)
1224 struct ChainHealingOrder
: public std::binary_function
<const Unit
*, const Unit
*, bool>
1226 const Unit
* MainTarget
;
1227 ChainHealingOrder(Unit
const* Target
) : MainTarget(Target
) {};
1228 // functor for operator ">"
1229 bool operator()(Unit
const* _Left
, Unit
const* _Right
) const
1231 return (ChainHealingHash(_Left
) < ChainHealingHash(_Right
));
1233 int32
ChainHealingHash(Unit
const* Target
) const
1235 if (Target
== MainTarget
)
1237 else if (Target
->GetTypeId() == TYPEID_PLAYER
&& MainTarget
->GetTypeId() == TYPEID_PLAYER
&&
1238 ((Player
const*)Target
)->IsInSameRaidWith((Player
const*)MainTarget
))
1240 if (Target
->GetHealth() == Target
->GetMaxHealth())
1243 return 20000 - Target
->GetMaxHealth() + Target
->GetHealth();
1246 return 40000 - Target
->GetMaxHealth() + Target
->GetHealth();
1250 class ChainHealingFullHealth
: std::unary_function
<const Unit
*, bool>
1253 const Unit
* MainTarget
;
1254 ChainHealingFullHealth(const Unit
* Target
) : MainTarget(Target
) {};
1256 bool operator()(const Unit
* Target
)
1258 return (Target
!= MainTarget
&& Target
->GetHealth() == Target
->GetMaxHealth());
1262 // Helper for targets nearest to the spell target
1263 // The spell target is always first unless there is a target at _completely_ the same position (unbelievable case)
1264 struct TargetDistanceOrder
: public std::binary_function
<const Unit
, const Unit
, bool>
1266 const Unit
* MainTarget
;
1267 TargetDistanceOrder(const Unit
* Target
) : MainTarget(Target
) {};
1268 // functor for operator ">"
1269 bool operator()(const Unit
* _Left
, const Unit
* _Right
) const
1271 return MainTarget
->GetDistanceOrder(_Left
, _Right
);
1275 void Spell::SetTargetMap(SpellEffectIndex effIndex
, uint32 targetMode
, UnitList
& targetUnitMap
)
1278 if (m_spellInfo
->EffectRadiusIndex
[effIndex
])
1279 radius
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[effIndex
]));
1281 radius
= GetSpellMaxRange(sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
));
1283 uint32 EffectChainTarget
= m_spellInfo
->EffectChainTarget
[effIndex
];
1285 if (Unit
* realCaster
= GetAffectiveCaster())
1287 if(Player
* modOwner
= realCaster
->GetSpellModOwner())
1289 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_RADIUS
, radius
, this);
1290 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_JUMP_TARGETS
, EffectChainTarget
, this);
1294 // Get spell max affected targets
1295 uint32 unMaxTargets
= m_spellInfo
->MaxAffectedTargets
;
1297 // custom target amount cases
1298 switch(m_spellInfo
->SpellFamilyName
)
1300 case SPELLFAMILY_GENERIC
:
1302 switch(m_spellInfo
->Id
)
1304 case 31347: // Doom TODO: exclude top threat target from target selection
1305 case 33711: // Murmur's Touch
1306 case 38794: // Murmur's Touch (h)
1309 case 28542: // Life Drain
1310 case 66013: // Penetrating Cold (10 man)
1311 case 68509: // Penetrating Cold (10 man heroic)
1314 case 28796: // Poison Bolt Volley
1315 case 29213: // Curse of the Plaguebringer
1316 case 31298: // Sleep
1319 case 30843: // Enfeeble TODO: exclude top threat target from target selection
1320 case 42005: // Bloodboil TODO: need to be 5 targets(players) furthest away from caster
1321 case 55665: // Life Drain (h)
1322 case 67700: // Penetrating Cold (25 man)
1323 case 68510: // Penetrating Cold (25 man, heroic)
1326 case 54098: // Poison Bolt Volley (h)
1327 case 54835: // Curse of the Plaguebringer (h)
1330 case 25991: // Poison Bolt Volley (Pincess Huhuran)
1336 case SPELLFAMILY_PALADIN
:
1337 if (m_spellInfo
->Id
== 20424) // Seal of Command (2 more target for single targeted spell)
1339 // overwrite EffectChainTarget for non single target spell
1340 if (Spell
* currSpell
= m_caster
->GetCurrentSpell(CURRENT_GENERIC_SPELL
))
1341 if (currSpell
->m_spellInfo
->MaxAffectedTargets
> 0 ||
1342 currSpell
->m_spellInfo
->EffectChainTarget
[EFFECT_INDEX_0
] > 0 ||
1343 currSpell
->m_spellInfo
->EffectChainTarget
[EFFECT_INDEX_1
] > 0 ||
1344 currSpell
->m_spellInfo
->EffectChainTarget
[EFFECT_INDEX_2
] > 0)
1345 EffectChainTarget
= 0; // no chain targets
1348 case SPELLFAMILY_DRUID
:
1350 if (m_spellInfo
->SpellFamilyFlags2
& 0x00000100)// Starfall
1358 Unit::AuraList
const& mod
= m_caster
->GetAurasByType(SPELL_AURA_MOD_MAX_AFFECTED_TARGETS
);
1359 for(Unit::AuraList::const_iterator m
= mod
.begin(); m
!= mod
.end(); ++m
)
1361 if (!(*m
)->isAffectedOnSpell(m_spellInfo
))
1363 unMaxTargets
+= (*m
)->GetModifier()->m_amount
;
1368 case TARGET_RANDOM_NEARBY_LOC
:
1369 radius
*= sqrtf(rand_norm_f()); // Get a random point in circle. Use sqrt(rand) to correct distribution when converting polar to Cartesian coordinates.
1370 // no 'break' expected since we use code in case TARGET_RANDOM_CIRCUMFERENCE_POINT!!!
1371 case TARGET_RANDOM_CIRCUMFERENCE_POINT
:
1373 float angle
= 2.0f
* M_PI_F
* rand_norm_f();
1374 float dest_x
, dest_y
, dest_z
;
1375 m_caster
->GetClosePoint(dest_x
, dest_y
, dest_z
, 0.0f
, radius
, angle
);
1376 m_targets
.setDestination(dest_x
, dest_y
, dest_z
);
1378 targetUnitMap
.push_back(m_caster
);
1381 case TARGET_RANDOM_NEARBY_DEST
:
1383 radius
*= sqrtf(rand_norm_f()); // Get a random point in circle. Use sqrt(rand) to correct distribution when converting polar to Cartesian coordinates.
1384 float angle
= 2.0f
* M_PI_F
* rand_norm_f();
1385 float dest_x
= m_targets
.m_destX
+ cos(angle
) * radius
;
1386 float dest_y
= m_targets
.m_destY
+ sin(angle
) * radius
;
1387 float dest_z
= m_caster
->GetPositionZ();
1388 m_caster
->UpdateGroundPositionZ(dest_x
, dest_y
, dest_z
);
1389 m_targets
.setDestination(dest_x
, dest_y
, dest_z
);
1393 // caster included here?
1394 FillAreaTargets(targetUnitMap
, dest_x
, dest_y
, radius
, PUSH_DEST_CENTER
, SPELL_TARGETS_AOE_DAMAGE
);
1397 targetUnitMap
.push_back(m_caster
);
1401 case TARGET_TOTEM_EARTH
:
1402 case TARGET_TOTEM_WATER
:
1403 case TARGET_TOTEM_AIR
:
1404 case TARGET_TOTEM_FIRE
:
1407 case TARGET_AREAEFFECT_CUSTOM
:
1408 case TARGET_AREAEFFECT_CUSTOM_2
:
1409 targetUnitMap
.push_back(m_caster
);
1411 case TARGET_RANDOM_ENEMY_CHAIN_IN_AREA
:
1413 m_targets
.m_targetMask
= 0;
1414 unMaxTargets
= EffectChainTarget
;
1415 float max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1417 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1419 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1422 std::list
<Unit
*> tempTargetUnitMap
;
1425 MaNGOS::AnyAoETargetUnitInObjectRangeCheck
u_check(m_caster
, max_range
);
1426 MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
> searcher(m_caster
, tempTargetUnitMap
, u_check
);
1428 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_unit_searcher(searcher
);
1429 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1431 cell
.Visit(p
, world_unit_searcher
, *m_caster
->GetMap(), *m_caster
, max_range
);
1432 cell
.Visit(p
, grid_unit_searcher
, *m_caster
->GetMap(), *m_caster
, max_range
);
1435 if(tempTargetUnitMap
.empty())
1438 tempTargetUnitMap
.sort(TargetDistanceOrder(m_caster
));
1440 //Now to get us a random target that's in the initial range of the spell
1442 std::list
<Unit
*>::iterator itr
= tempTargetUnitMap
.begin();
1443 while(itr
!= tempTargetUnitMap
.end() && (*itr
)->IsWithinDist(m_caster
, radius
))
1449 itr
= tempTargetUnitMap
.begin();
1450 std::advance(itr
, rand() % t
);
1451 Unit
*pUnitTarget
= *itr
;
1452 targetUnitMap
.push_back(pUnitTarget
);
1454 tempTargetUnitMap
.erase(itr
);
1456 tempTargetUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1458 t
= unMaxTargets
- 1;
1459 Unit
*prev
= pUnitTarget
;
1460 std::list
<Unit
*>::iterator next
= tempTargetUnitMap
.begin();
1462 while(t
&& next
!= tempTargetUnitMap
.end())
1464 if(!prev
->IsWithinDist(*next
, CHAIN_SPELL_JUMP_RADIUS
))
1467 if(!prev
->IsWithinLOSInMap(*next
))
1474 targetUnitMap
.push_back(prev
);
1475 tempTargetUnitMap
.erase(next
);
1476 tempTargetUnitMap
.sort(TargetDistanceOrder(prev
));
1477 next
= tempTargetUnitMap
.begin();
1483 case TARGET_RANDOM_FRIEND_CHAIN_IN_AREA
:
1485 m_targets
.m_targetMask
= 0;
1486 unMaxTargets
= EffectChainTarget
;
1487 float max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1488 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1490 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1492 std::list
<Unit
*> tempTargetUnitMap
;
1494 MaNGOS::AnyFriendlyUnitInObjectRangeCheck
u_check(m_caster
, max_range
);
1495 MaNGOS::UnitListSearcher
<MaNGOS::AnyFriendlyUnitInObjectRangeCheck
> searcher(m_caster
, tempTargetUnitMap
, u_check
);
1497 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyFriendlyUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_unit_searcher(searcher
);
1498 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyFriendlyUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1500 cell
.Visit(p
, world_unit_searcher
, *m_caster
->GetMap(), *m_caster
, max_range
);
1501 cell
.Visit(p
, grid_unit_searcher
, *m_caster
->GetMap(), *m_caster
, max_range
);
1504 if(tempTargetUnitMap
.empty())
1507 tempTargetUnitMap
.sort(TargetDistanceOrder(m_caster
));
1509 //Now to get us a random target that's in the initial range of the spell
1511 std::list
<Unit
*>::iterator itr
= tempTargetUnitMap
.begin();
1512 while(itr
!= tempTargetUnitMap
.end() && (*itr
)->IsWithinDist(m_caster
, radius
))
1518 itr
= tempTargetUnitMap
.begin();
1519 std::advance(itr
, rand() % t
);
1520 Unit
*pUnitTarget
= *itr
;
1521 targetUnitMap
.push_back(pUnitTarget
);
1523 tempTargetUnitMap
.erase(itr
);
1525 tempTargetUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1527 t
= unMaxTargets
- 1;
1528 Unit
*prev
= pUnitTarget
;
1529 std::list
<Unit
*>::iterator next
= tempTargetUnitMap
.begin();
1531 while(t
&& next
!= tempTargetUnitMap
.end())
1533 if(!prev
->IsWithinDist(*next
, CHAIN_SPELL_JUMP_RADIUS
))
1536 if(!prev
->IsWithinLOSInMap(*next
))
1542 targetUnitMap
.push_back(prev
);
1543 tempTargetUnitMap
.erase(next
);
1544 tempTargetUnitMap
.sort(TargetDistanceOrder(prev
));
1545 next
= tempTargetUnitMap
.begin();
1552 Pet
* tmpUnit
= m_caster
->GetPet();
1553 if (!tmpUnit
) break;
1554 targetUnitMap
.push_back(tmpUnit
);
1557 case TARGET_CHAIN_DAMAGE
:
1559 if (EffectChainTarget
<= 1)
1561 if(Unit
* pUnitTarget
= m_caster
->SelectMagnetTarget(m_targets
.getUnitTarget(), m_spellInfo
))
1563 m_targets
.setUnitTarget(pUnitTarget
);
1564 targetUnitMap
.push_back(pUnitTarget
);
1569 Unit
* pUnitTarget
= m_targets
.getUnitTarget();
1570 Unit
* originalCaster
= GetAffectiveCaster();
1571 if(!pUnitTarget
|| !originalCaster
)
1574 unMaxTargets
= EffectChainTarget
;
1577 if(m_spellInfo
->DmgClass
== SPELL_DAMAGE_CLASS_MELEE
)
1580 //FIXME: This very like horrible hack and wrong for most spells
1581 max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1583 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1585 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1588 std::list
<Unit
*> tempTargetUnitMap
;
1590 MaNGOS::AnyAoEVisibleTargetUnitInObjectRangeCheck
u_check(pUnitTarget
, originalCaster
, max_range
);
1591 MaNGOS::UnitListSearcher
<MaNGOS::AnyAoEVisibleTargetUnitInObjectRangeCheck
> searcher(m_caster
, tempTargetUnitMap
, u_check
);
1592 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoEVisibleTargetUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_unit_searcher(searcher
);
1593 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoEVisibleTargetUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1595 cell
.Visit(p
, world_unit_searcher
, *m_caster
->GetMap(), *pUnitTarget
, max_range
);
1596 cell
.Visit(p
, grid_unit_searcher
, *m_caster
->GetMap(), *pUnitTarget
, max_range
);
1598 if (tempTargetUnitMap
.empty())
1601 tempTargetUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1603 if (*tempTargetUnitMap
.begin() == pUnitTarget
)
1604 tempTargetUnitMap
.erase(tempTargetUnitMap
.begin());
1606 targetUnitMap
.push_back(pUnitTarget
);
1607 uint32 t
= unMaxTargets
- 1;
1608 Unit
*prev
= pUnitTarget
;
1609 std::list
<Unit
*>::iterator next
= tempTargetUnitMap
.begin();
1611 while (t
&& next
!= tempTargetUnitMap
.end())
1613 if (!prev
->IsWithinDist (*next
,CHAIN_SPELL_JUMP_RADIUS
))
1615 if (!prev
->IsWithinLOSInMap (*next
))
1621 targetUnitMap
.push_back(prev
);
1622 tempTargetUnitMap
.erase(next
);
1623 tempTargetUnitMap
.sort(TargetDistanceOrder(prev
));
1624 next
= tempTargetUnitMap
.begin();
1630 case TARGET_ALL_ENEMY_IN_AREA
:
1631 FillAreaTargets(targetUnitMap
, m_targets
.m_destX
, m_targets
.m_destY
, radius
, PUSH_DEST_CENTER
, SPELL_TARGETS_AOE_DAMAGE
);
1633 case TARGET_AREAEFFECT_INSTANT
:
1635 SpellTargets targetB
= SPELL_TARGETS_AOE_DAMAGE
;
1636 // Select friendly targets for positive effect
1637 if (IsPositiveEffect(m_spellInfo
->Id
, effIndex
))
1638 targetB
= SPELL_TARGETS_FRIENDLY
;
1640 FillAreaTargets(targetUnitMap
, m_caster
->GetPositionX(), m_caster
->GetPositionY(), radius
, PUSH_DEST_CENTER
, targetB
);
1643 targetUnitMap
.remove(m_caster
);
1646 case TARGET_ALL_ENEMY_IN_AREA_INSTANT
:
1648 // targets the ground, not the units in the area
1649 switch(m_spellInfo
->Effect
[effIndex
])
1651 case SPELL_EFFECT_PERSISTENT_AREA_AURA
:
1653 case SPELL_EFFECT_SUMMON
:
1654 targetUnitMap
.push_back(m_caster
);
1657 FillAreaTargets(targetUnitMap
, m_targets
.m_destX
, m_targets
.m_destY
, radius
, PUSH_DEST_CENTER
, SPELL_TARGETS_AOE_DAMAGE
);
1662 case TARGET_DUELVSPLAYER_COORDINATES
:
1664 if(Unit
* currentTarget
= m_targets
.getUnitTarget())
1666 m_targets
.setDestination(currentTarget
->GetPositionX(), currentTarget
->GetPositionY(), currentTarget
->GetPositionZ());
1667 targetUnitMap
.push_back(currentTarget
);
1671 case TARGET_ALL_PARTY_AROUND_CASTER
:
1672 case TARGET_ALL_PARTY_AROUND_CASTER_2
:
1673 case TARGET_ALL_PARTY
:
1674 FillRaidOrPartyTargets(targetUnitMap
, m_caster
, m_caster
, radius
, false, true, true);
1676 case TARGET_ALL_RAID_AROUND_CASTER
:
1678 if(m_spellInfo
->Id
== 57669) // Replenishment (special target selection)
1679 FillRaidOrPartyManaPriorityTargets(targetUnitMap
, m_caster
, m_caster
, radius
, 10, true, false, true);
1680 else if (m_spellInfo
->Id
==52759) // Ancestral Awakening (special target selection)
1681 FillRaidOrPartyHealthPriorityTargets(targetUnitMap
, m_caster
, m_caster
, radius
, 1, true, false, true);
1683 FillRaidOrPartyTargets(targetUnitMap
, m_caster
, m_caster
, radius
, true, true, true);
1686 case TARGET_SINGLE_FRIEND
:
1687 case TARGET_SINGLE_FRIEND_2
:
1688 if(m_targets
.getUnitTarget())
1689 targetUnitMap
.push_back(m_targets
.getUnitTarget());
1691 case TARGET_NONCOMBAT_PET
:
1692 if(Unit
* target
= m_targets
.getUnitTarget())
1693 if( target
->GetTypeId() == TYPEID_UNIT
&& ((Creature
*)target
)->isPet() && ((Pet
*)target
)->getPetType() == MINI_PET
)
1694 targetUnitMap
.push_back(target
);
1696 case TARGET_CASTER_COORDINATES
:
1698 // Check original caster is GO - set its coordinates as dst cast
1699 if (WorldObject
*caster
= GetCastingObject())
1700 m_targets
.setDestination(caster
->GetPositionX(), caster
->GetPositionY(), caster
->GetPositionZ());
1703 case TARGET_ALL_HOSTILE_UNITS_AROUND_CASTER
:
1704 FillAreaTargets(targetUnitMap
, m_targets
.m_destX
, m_targets
.m_destY
, radius
, PUSH_SELF_CENTER
, SPELL_TARGETS_HOSTILE
);
1706 case TARGET_ALL_FRIENDLY_UNITS_AROUND_CASTER
:
1707 switch (m_spellInfo
->Id
)
1709 case 56153: // Guardian Aura - Ahn'Kahet
1710 FillAreaTargets(targetUnitMap
, m_targets
.m_destX
, m_targets
.m_destY
, radius
, PUSH_SELF_CENTER
, SPELL_TARGETS_FRIENDLY
);
1711 targetUnitMap
.remove(m_caster
);
1713 case 64844: // Divine Hymn
1714 // target amount stored in parent spell dummy effect but hard to access
1715 FillRaidOrPartyHealthPriorityTargets(targetUnitMap
, m_caster
, m_caster
, radius
, 3, true, false, false);
1717 case 64904: // Hymn of Hope
1718 // target amount stored in parent spell dummy effect but hard to access
1719 FillRaidOrPartyManaPriorityTargets(targetUnitMap
, m_caster
, m_caster
, radius
, 3, true, false, false);
1722 FillAreaTargets(targetUnitMap
, m_targets
.m_destX
, m_targets
.m_destY
, radius
, PUSH_SELF_CENTER
, SPELL_TARGETS_FRIENDLY
);
1726 case TARGET_ALL_FRIENDLY_UNITS_IN_AREA
:
1727 // Death Pact (in fact selection by player selection)
1728 if (m_spellInfo
->Id
== 48743)
1730 // checked in Spell::CheckCast
1731 if (m_caster
->GetTypeId()==TYPEID_PLAYER
)
1732 if (Unit
* target
= m_caster
->GetMap()->GetPet(((Player
*)m_caster
)->GetSelection()))
1733 targetUnitMap
.push_back(target
);
1735 // Circle of Healing
1736 else if (m_spellInfo
->SpellFamilyName
== SPELLFAMILY_PRIEST
&& m_spellInfo
->SpellVisual
[0] == 8253)
1738 Unit
* target
= m_targets
.getUnitTarget();
1743 // Glyph of Circle of Healing
1744 if(Aura
const* glyph
= m_caster
->GetDummyAura(55675))
1745 count
+= glyph
->GetModifier()->m_amount
;
1747 FillRaidOrPartyHealthPriorityTargets(targetUnitMap
, m_caster
, target
, radius
, count
, true, false, true);
1750 else if (m_spellInfo
->SpellFamilyName
== SPELLFAMILY_DRUID
&& m_spellInfo
->SpellIconID
== 2864)
1752 Unit
* target
= m_targets
.getUnitTarget();
1755 uint32 count
= CalculateDamage(EFFECT_INDEX_2
,m_caster
); // stored in dummy effect, affected by mods
1757 FillRaidOrPartyHealthPriorityTargets(targetUnitMap
, m_caster
, target
, radius
, count
, true, false, true);
1760 FillAreaTargets(targetUnitMap
, m_targets
.m_destX
, m_targets
.m_destY
, radius
, PUSH_DEST_CENTER
, SPELL_TARGETS_FRIENDLY
);
1762 // TARGET_SINGLE_PARTY means that the spells can only be casted on a party member and not on the caster (some seals, fire shield from imp, etc..)
1763 case TARGET_SINGLE_PARTY
:
1765 Unit
*target
= m_targets
.getUnitTarget();
1766 // Those spells apparently can't be casted on the caster.
1767 if( target
&& target
!= m_caster
)
1769 // Can only be casted on group's members or its pets
1770 Group
*pGroup
= NULL
;
1772 Unit
* owner
= m_caster
->GetCharmerOrOwner();
1773 Unit
*targetOwner
= target
->GetCharmerOrOwner();
1776 if(owner
->GetTypeId() == TYPEID_PLAYER
)
1778 if( target
== owner
)
1780 targetUnitMap
.push_back(target
);
1783 pGroup
= ((Player
*)owner
)->GetGroup();
1786 else if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1788 if( targetOwner
== m_caster
&& target
->GetTypeId() == TYPEID_UNIT
&& ((Creature
*)target
)->isPet())
1790 targetUnitMap
.push_back(target
);
1793 pGroup
= ((Player
*)m_caster
)->GetGroup();
1798 // Our target can also be a player's pet who's grouped with us or our pet. But can't be controlled player
1801 if( targetOwner
->GetTypeId() == TYPEID_PLAYER
&&
1802 target
->GetTypeId() == TYPEID_UNIT
&& (((Creature
*)target
)->isPet()) &&
1803 target
->GetOwnerGUID() == targetOwner
->GetGUID() &&
1804 pGroup
->IsMember(((Player
*)targetOwner
)->GetGUID()))
1806 targetUnitMap
.push_back(target
);
1809 // 1Our target can be a player who is on our group
1810 else if (target
->GetTypeId() == TYPEID_PLAYER
&& pGroup
->IsMember(((Player
*)target
)->GetGUID()))
1812 targetUnitMap
.push_back(target
);
1818 case TARGET_GAMEOBJECT
:
1819 if(m_targets
.getGOTarget())
1820 AddGOTarget(m_targets
.getGOTarget(), effIndex
);
1822 case TARGET_IN_FRONT_OF_CASTER
:
1824 bool inFront
= m_spellInfo
->SpellVisual
[0] != 3879;
1825 FillAreaTargets(targetUnitMap
, m_caster
->GetPositionX(), m_caster
->GetPositionY(), radius
, inFront
? PUSH_IN_FRONT
: PUSH_IN_BACK
, SPELL_TARGETS_AOE_DAMAGE
);
1828 case TARGET_LARGE_FRONTAL_CONE
:
1829 FillAreaTargets(targetUnitMap
, m_caster
->GetPositionX(), m_caster
->GetPositionY(), radius
, PUSH_IN_FRONT_90
, SPELL_TARGETS_AOE_DAMAGE
);
1831 case TARGET_NARROW_FRONTAL_CONE
:
1832 FillAreaTargets(targetUnitMap
, m_caster
->GetPositionX(), m_caster
->GetPositionY(), radius
, PUSH_IN_FRONT_15
, SPELL_TARGETS_AOE_DAMAGE
);
1834 case TARGET_IN_FRONT_OF_CASTER_30
:
1835 FillAreaTargets(targetUnitMap
, m_caster
->GetPositionX(), m_caster
->GetPositionY(), radius
, PUSH_IN_FRONT_30
, SPELL_TARGETS_AOE_DAMAGE
);
1837 case TARGET_DUELVSPLAYER
:
1839 Unit
*target
= m_targets
.getUnitTarget();
1842 if(m_caster
->IsFriendlyTo(target
))
1844 targetUnitMap
.push_back(target
);
1848 if(Unit
* pUnitTarget
= m_caster
->SelectMagnetTarget(m_targets
.getUnitTarget(), m_spellInfo
))
1850 m_targets
.setUnitTarget(pUnitTarget
);
1851 targetUnitMap
.push_back(pUnitTarget
);
1857 case TARGET_GAMEOBJECT_ITEM
:
1858 if(m_targets
.getGOTargetGUID())
1859 AddGOTarget(m_targets
.getGOTarget(), effIndex
);
1860 else if(m_targets
.getItemTarget())
1861 AddItemTarget(m_targets
.getItemTarget(), effIndex
);
1864 if(Unit
* owner
= m_caster
->GetCharmerOrOwner())
1865 targetUnitMap
.push_back(owner
);
1867 case TARGET_ALL_ENEMY_IN_AREA_CHANNELED
:
1868 // targets the ground, not the units in the area
1869 if (m_spellInfo
->Effect
[effIndex
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
1870 FillAreaTargets(targetUnitMap
, m_targets
.m_destX
, m_targets
.m_destY
, radius
, PUSH_DEST_CENTER
, SPELL_TARGETS_AOE_DAMAGE
);
1873 if(m_spellInfo
->Effect
[effIndex
] != SPELL_EFFECT_DUEL
)
1874 targetUnitMap
.push_back(m_caster
);
1876 case TARGET_SINGLE_ENEMY
:
1878 if(Unit
* pUnitTarget
= m_caster
->SelectMagnetTarget(m_targets
.getUnitTarget(), m_spellInfo
))
1880 m_targets
.setUnitTarget(pUnitTarget
);
1881 targetUnitMap
.push_back(pUnitTarget
);
1885 case TARGET_AREAEFFECT_PARTY
:
1887 Unit
* owner
= m_caster
->GetCharmerOrOwner();
1888 Player
*pTarget
= NULL
;
1892 targetUnitMap
.push_back(m_caster
);
1893 if(owner
->GetTypeId() == TYPEID_PLAYER
)
1894 pTarget
= (Player
*)owner
;
1896 else if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1898 if(Unit
* target
= m_targets
.getUnitTarget())
1900 if( target
->GetTypeId() != TYPEID_PLAYER
)
1902 if(((Creature
*)target
)->isPet())
1904 Unit
*targetOwner
= target
->GetOwner();
1905 if(targetOwner
->GetTypeId() == TYPEID_PLAYER
)
1906 pTarget
= (Player
*)targetOwner
;
1910 pTarget
= (Player
*)target
;
1914 Group
* pGroup
= pTarget
? pTarget
->GetGroup() : NULL
;
1918 uint8 subgroup
= pTarget
->GetSubGroup();
1920 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1922 Player
* Target
= itr
->getSource();
1924 // IsHostileTo check duel and controlled by enemy
1925 if(Target
&& Target
->GetSubGroup() == subgroup
&& !m_caster
->IsHostileTo(Target
))
1927 if( pTarget
->IsWithinDistInMap(Target
, radius
) )
1928 targetUnitMap
.push_back(Target
);
1930 if(Pet
* pet
= Target
->GetPet())
1931 if( pTarget
->IsWithinDistInMap(pet
, radius
) )
1932 targetUnitMap
.push_back(pet
);
1938 if(m_caster
->IsWithinDistInMap(owner
, radius
))
1939 targetUnitMap
.push_back(owner
);
1943 targetUnitMap
.push_back(pTarget
);
1945 if(Pet
* pet
= pTarget
->GetPet())
1946 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1947 targetUnitMap
.push_back(pet
);
1953 if(m_targets
.getUnitTarget())
1954 targetUnitMap
.push_back(m_targets
.getUnitTarget());
1955 if(m_targets
.getItemTarget())
1956 AddItemTarget(m_targets
.getItemTarget(), effIndex
);
1959 case TARGET_SELF_FISHING
:
1960 targetUnitMap
.push_back(m_caster
);
1962 case TARGET_CHAIN_HEAL
:
1964 Unit
* pUnitTarget
= m_targets
.getUnitTarget();
1968 if (EffectChainTarget
<= 1)
1969 targetUnitMap
.push_back(pUnitTarget
);
1972 unMaxTargets
= EffectChainTarget
;
1973 float max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1975 UnitList tempTargetUnitMap
;
1977 FillAreaTargets(tempTargetUnitMap
, m_caster
->GetPositionX(), m_caster
->GetPositionY(), max_range
, PUSH_SELF_CENTER
, SPELL_TARGETS_FRIENDLY
);
1979 if (m_caster
!= pUnitTarget
&& std::find(tempTargetUnitMap
.begin(), tempTargetUnitMap
.end(), m_caster
) == tempTargetUnitMap
.end())
1980 tempTargetUnitMap
.push_front(m_caster
);
1982 tempTargetUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1984 if (tempTargetUnitMap
.empty())
1987 if (*tempTargetUnitMap
.begin() == pUnitTarget
)
1988 tempTargetUnitMap
.erase(tempTargetUnitMap
.begin());
1990 targetUnitMap
.push_back(pUnitTarget
);
1991 uint32 t
= unMaxTargets
- 1;
1992 Unit
*prev
= pUnitTarget
;
1993 std::list
<Unit
*>::iterator next
= tempTargetUnitMap
.begin();
1995 while(t
&& next
!= tempTargetUnitMap
.end())
1997 if(!prev
->IsWithinDist(*next
, CHAIN_SPELL_JUMP_RADIUS
))
2000 if(!prev
->IsWithinLOSInMap(*next
))
2006 if((*next
)->GetHealth() == (*next
)->GetMaxHealth())
2008 next
= tempTargetUnitMap
.erase(next
);
2013 targetUnitMap
.push_back(prev
);
2014 tempTargetUnitMap
.erase(next
);
2015 tempTargetUnitMap
.sort(TargetDistanceOrder(prev
));
2016 next
= tempTargetUnitMap
.begin();
2023 case TARGET_CURRENT_ENEMY_COORDINATES
:
2025 Unit
* currentTarget
= m_targets
.getUnitTarget();
2028 targetUnitMap
.push_back(currentTarget
);
2029 m_targets
.setDestination(currentTarget
->GetPositionX(), currentTarget
->GetPositionY(), currentTarget
->GetPositionZ());
2030 if(m_spellInfo
->EffectImplicitTargetB
[effIndex
] == TARGET_ALL_ENEMY_IN_AREA_INSTANT
)
2031 FillAreaTargets(targetUnitMap
, currentTarget
->GetPositionX(), currentTarget
->GetPositionY(), radius
, PUSH_TARGET_CENTER
, SPELL_TARGETS_AOE_DAMAGE
);
2035 case TARGET_AREAEFFECT_PARTY_AND_CLASS
:
2037 Player
* targetPlayer
= m_targets
.getUnitTarget() && m_targets
.getUnitTarget()->GetTypeId() == TYPEID_PLAYER
2038 ? (Player
*)m_targets
.getUnitTarget() : NULL
;
2040 Group
* pGroup
= targetPlayer
? targetPlayer
->GetGroup() : NULL
;
2043 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
2045 Player
* Target
= itr
->getSource();
2047 // IsHostileTo check duel and controlled by enemy
2048 if( Target
&& targetPlayer
->IsWithinDistInMap(Target
, radius
) &&
2049 targetPlayer
->getClass() == Target
->getClass() &&
2050 !m_caster
->IsHostileTo(Target
) )
2052 targetUnitMap
.push_back(Target
);
2056 else if(m_targets
.getUnitTarget())
2057 targetUnitMap
.push_back(m_targets
.getUnitTarget());
2060 case TARGET_TABLE_X_Y_Z_COORDINATES
:
2062 SpellTargetPosition
const* st
= sSpellMgr
.GetSpellTargetPosition(m_spellInfo
->Id
);
2065 // teleport spells are handled in another way
2066 if (m_spellInfo
->Effect
[effIndex
] == SPELL_EFFECT_TELEPORT_UNITS
)
2068 if (st
->target_mapId
== m_caster
->GetMapId())
2069 m_targets
.setDestination(st
->target_X
, st
->target_Y
, st
->target_Z
);
2071 sLog
.outError( "SPELL: wrong map (%u instead %u) target coordinates for spell ID %u", st
->target_mapId
, m_caster
->GetMapId(), m_spellInfo
->Id
);
2074 sLog
.outError( "SPELL: unknown target coordinates for spell ID %u", m_spellInfo
->Id
);
2077 case TARGET_INFRONT_OF_VICTIM
:
2078 case TARGET_BEHIND_VICTIM
:
2079 case TARGET_RIGHT_FROM_VICTIM
:
2080 case TARGET_LEFT_FROM_VICTIM
:
2082 Unit
*pTarget
= NULL
;
2084 // explicit cast data from client or server-side cast
2085 // some spell at client send caster
2086 if(m_targets
.getUnitTarget() && m_targets
.getUnitTarget()!=m_caster
)
2087 pTarget
= m_targets
.getUnitTarget();
2088 else if(m_caster
->getVictim())
2089 pTarget
= m_caster
->getVictim();
2090 else if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
2091 pTarget
= ObjectAccessor::GetUnit(*m_caster
, ((Player
*)m_caster
)->GetSelection());
2096 float dist
= (radius
&& targetMode
!= TARGET_BEHIND_VICTIM
) ? radius
: CONTACT_DISTANCE
;
2100 case TARGET_INFRONT_OF_VICTIM
: break;
2101 case TARGET_BEHIND_VICTIM
: angle
= M_PI_F
; break;
2102 case TARGET_RIGHT_FROM_VICTIM
: angle
= -M_PI_F
/ 2; break;
2103 case TARGET_LEFT_FROM_VICTIM
: angle
= M_PI_F
/ 2; break;
2106 float _target_x
, _target_y
, _target_z
;
2107 pTarget
->GetClosePoint(_target_x
, _target_y
, _target_z
, pTarget
->GetObjectSize(), dist
, angle
);
2108 if(pTarget
->IsWithinLOS(_target_x
, _target_y
, _target_z
))
2110 targetUnitMap
.push_back(m_caster
);
2111 m_targets
.setDestination(_target_x
, _target_y
, _target_z
);
2116 case TARGET_DYNAMIC_OBJECT_COORDINATES
:
2117 // if parent spell create dynamic object extract area from it
2118 if(DynamicObject
* dynObj
= m_caster
->GetDynObject(m_triggeredByAuraSpell
? m_triggeredByAuraSpell
->Id
: m_spellInfo
->Id
))
2119 m_targets
.setDestination(dynObj
->GetPositionX(), dynObj
->GetPositionY(), dynObj
->GetPositionZ());
2122 case TARGET_DYNAMIC_OBJECT_FRONT
:
2123 case TARGET_DYNAMIC_OBJECT_BEHIND
:
2124 case TARGET_DYNAMIC_OBJECT_LEFT_SIDE
:
2125 case TARGET_DYNAMIC_OBJECT_RIGHT_SIDE
:
2127 if (!(m_targets
.m_targetMask
& TARGET_FLAG_DEST_LOCATION
))
2129 float angle
= m_caster
->GetOrientation();
2132 case TARGET_DYNAMIC_OBJECT_FRONT
: break;
2133 case TARGET_DYNAMIC_OBJECT_BEHIND
: angle
+= M_PI_F
; break;
2134 case TARGET_DYNAMIC_OBJECT_LEFT_SIDE
: angle
+= M_PI_F
/ 2; break;
2135 case TARGET_DYNAMIC_OBJECT_RIGHT_SIDE
: angle
-= M_PI_F
/ 2; break;
2139 m_caster
->GetNearPoint2D(x
, y
, radius
, angle
);
2140 m_targets
.setDestination(x
, y
, m_caster
->GetPositionZ());
2143 targetUnitMap
.push_back(m_caster
);
2146 case TARGET_POINT_AT_NORTH
:
2147 case TARGET_POINT_AT_SOUTH
:
2148 case TARGET_POINT_AT_EAST
:
2149 case TARGET_POINT_AT_WEST
:
2150 case TARGET_POINT_AT_NE
:
2151 case TARGET_POINT_AT_NW
:
2152 case TARGET_POINT_AT_SE
:
2153 case TARGET_POINT_AT_SW
:
2156 if (!(m_targets
.m_targetMask
& TARGET_FLAG_DEST_LOCATION
))
2158 Unit
* currentTarget
= m_targets
.getUnitTarget() ? m_targets
.getUnitTarget() : m_caster
;
2159 float angle
= currentTarget
!= m_caster
? currentTarget
->GetAngle(m_caster
) : m_caster
->GetOrientation();
2163 case TARGET_POINT_AT_NORTH
: break;
2164 case TARGET_POINT_AT_SOUTH
: angle
+= M_PI_F
; break;
2165 case TARGET_POINT_AT_EAST
: angle
-= M_PI_F
/ 2; break;
2166 case TARGET_POINT_AT_WEST
: angle
+= M_PI_F
/ 2; break;
2167 case TARGET_POINT_AT_NE
: angle
-= M_PI_F
/ 4; break;
2168 case TARGET_POINT_AT_NW
: angle
+= M_PI_F
/ 4; break;
2169 case TARGET_POINT_AT_SE
: angle
-= 3*M_PI_F
/ 4; break;
2170 case TARGET_POINT_AT_SW
: angle
+= 3*M_PI_F
/ 4; break;
2174 currentTarget
->GetNearPoint2D(x
, y
, radius
, angle
);
2175 m_targets
.setDestination(x
, y
, currentTarget
->GetPositionZ());
2179 case TARGET_DIRECTLY_FORWARD
:
2181 if (!(m_targets
.m_targetMask
& TARGET_FLAG_DEST_LOCATION
))
2183 SpellRangeEntry
const* rEntry
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
2184 float minRange
= GetSpellMinRange(rEntry
);
2185 float maxRange
= GetSpellMaxRange(rEntry
);
2186 float dist
= minRange
+ rand_norm_f()*(maxRange
-minRange
);
2188 float _target_x
, _target_y
, _target_z
;
2189 m_caster
->GetClosePoint(_target_x
, _target_y
, _target_z
, m_caster
->GetObjectSize(), dist
);
2190 m_targets
.setDestination(_target_x
, _target_y
, _target_z
);
2193 targetUnitMap
.push_back(m_caster
);
2196 case TARGET_EFFECT_SELECT
:
2198 // add here custom effects that need default target.
2199 // FOR EVERY TARGET TYPE THERE IS A DIFFERENT FILL!!
2200 switch(m_spellInfo
->Effect
[effIndex
])
2202 case SPELL_EFFECT_DUMMY
:
2204 switch(m_spellInfo
->Id
)
2206 case 20577: // Cannibalize
2208 WorldObject
* result
= FindCorpseUsing
<MaNGOS::CannibalizeObjectCheck
> ();
2212 switch(result
->GetTypeId())
2216 targetUnitMap
.push_back((Unit
*)result
);
2219 m_targets
.setCorpseTarget((Corpse
*)result
);
2220 if (Player
* owner
= ObjectAccessor::FindPlayer(((Corpse
*)result
)->GetOwnerGUID()))
2221 targetUnitMap
.push_back(owner
);
2227 // clear cooldown at fail
2228 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2229 ((Player
*)m_caster
)->RemoveSpellCooldown(m_spellInfo
->Id
, true);
2230 SendCastResult(SPELL_FAILED_NO_EDIBLE_CORPSES
);
2236 if (m_targets
.getUnitTarget())
2237 targetUnitMap
.push_back(m_targets
.getUnitTarget());
2242 case SPELL_EFFECT_BIND
:
2243 case SPELL_EFFECT_RESURRECT
:
2244 case SPELL_EFFECT_PARRY
:
2245 case SPELL_EFFECT_BLOCK
:
2246 case SPELL_EFFECT_CREATE_ITEM
:
2247 case SPELL_EFFECT_TRIGGER_SPELL
:
2248 case SPELL_EFFECT_TRIGGER_MISSILE
:
2249 case SPELL_EFFECT_LEARN_SPELL
:
2250 case SPELL_EFFECT_SKILL_STEP
:
2251 case SPELL_EFFECT_PROFICIENCY
:
2252 case SPELL_EFFECT_SUMMON_OBJECT_WILD
:
2253 case SPELL_EFFECT_SELF_RESURRECT
:
2254 case SPELL_EFFECT_REPUTATION
:
2255 case SPELL_EFFECT_SEND_TAXI
:
2256 if (m_targets
.getUnitTarget())
2257 targetUnitMap
.push_back(m_targets
.getUnitTarget());
2258 // Triggered spells have additional spell targets - cast them even if no explicit unit target is given (required for spell 50516 for example)
2259 else if (m_spellInfo
->Effect
[effIndex
] == SPELL_EFFECT_TRIGGER_SPELL
)
2260 targetUnitMap
.push_back(m_caster
);
2262 case SPELL_EFFECT_SUMMON_PLAYER
:
2263 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->GetSelection())
2265 Player
* target
= sObjectMgr
.GetPlayer(((Player
*)m_caster
)->GetSelection());
2267 targetUnitMap
.push_back(target
);
2270 case SPELL_EFFECT_RESURRECT_NEW
:
2271 if (m_targets
.getUnitTarget())
2272 targetUnitMap
.push_back(m_targets
.getUnitTarget());
2273 if (m_targets
.getCorpseTargetGUID())
2275 Corpse
*corpse
= m_caster
->GetMap()->GetCorpse(m_targets
.getCorpseTargetGUID());
2278 Player
* owner
= ObjectAccessor::FindPlayer(corpse
->GetOwnerGUID());
2280 targetUnitMap
.push_back(owner
);
2284 case SPELL_EFFECT_SUMMON
:
2285 case SPELL_EFFECT_SUMMON_CHANGE_ITEM
:
2286 case SPELL_EFFECT_TRANS_DOOR
:
2287 case SPELL_EFFECT_ADD_FARSIGHT
:
2288 case SPELL_EFFECT_APPLY_GLYPH
:
2289 case SPELL_EFFECT_STUCK
:
2290 case SPELL_EFFECT_SUMMON_ALL_TOTEMS
:
2291 case SPELL_EFFECT_FEED_PET
:
2292 case SPELL_EFFECT_DESTROY_ALL_TOTEMS
:
2293 case SPELL_EFFECT_SKILL
:
2294 targetUnitMap
.push_back(m_caster
);
2296 case SPELL_EFFECT_LEARN_PET_SPELL
:
2297 if (Pet
* pet
= m_caster
->GetPet())
2298 targetUnitMap
.push_back(pet
);
2300 case SPELL_EFFECT_ENCHANT_ITEM
:
2301 case SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY
:
2302 case SPELL_EFFECT_ENCHANT_ITEM_PRISMATIC
:
2303 case SPELL_EFFECT_DISENCHANT
:
2304 case SPELL_EFFECT_PROSPECTING
:
2305 case SPELL_EFFECT_MILLING
:
2306 if (m_targets
.getItemTarget())
2307 AddItemTarget(m_targets
.getItemTarget(), effIndex
);
2309 case SPELL_EFFECT_APPLY_AURA
:
2310 switch(m_spellInfo
->EffectApplyAuraName
[effIndex
])
2312 case SPELL_AURA_ADD_FLAT_MODIFIER
: // some spell mods auras have 0 target modes instead expected TARGET_SELF(1) (and present for other ranks for same spell for example)
2313 case SPELL_AURA_ADD_PCT_MODIFIER
:
2314 targetUnitMap
.push_back(m_caster
);
2316 default: // apply to target in other case
2317 if (m_targets
.getUnitTarget())
2318 targetUnitMap
.push_back(m_targets
.getUnitTarget());
2322 case SPELL_EFFECT_APPLY_AREA_AURA_PARTY
:
2324 if(m_spellInfo
->Attributes
== 0x9050000 || m_spellInfo
->Attributes
== 0x10000)
2325 SetTargetMap(effIndex
, TARGET_AREAEFFECT_PARTY
, targetUnitMap
);
2327 case SPELL_EFFECT_SKIN_PLAYER_CORPSE
:
2328 if (m_targets
.getUnitTarget())
2329 targetUnitMap
.push_back(m_targets
.getUnitTarget());
2330 else if (m_targets
.getCorpseTargetGUID())
2332 if (Corpse
*corpse
= m_caster
->GetMap()->GetCorpse(m_targets
.getCorpseTargetGUID()))
2333 if (Player
* owner
= ObjectAccessor::FindPlayer(corpse
->GetOwnerGUID()))
2334 targetUnitMap
.push_back(owner
);
2344 //sLog.outError( "SPELL: Unknown implicit target (%u) for spell ID %u", targetMode, m_spellInfo->Id );
2348 if (unMaxTargets
&& targetUnitMap
.size() > unMaxTargets
)
2350 // make sure one unit is always removed per iteration
2351 uint32 removed_utarget
= 0;
2352 for (UnitList::iterator itr
= targetUnitMap
.begin(), next
; itr
!= targetUnitMap
.end(); itr
= next
)
2356 if (!*itr
) continue;
2357 if ((*itr
) == m_targets
.getUnitTarget())
2359 targetUnitMap
.erase(itr
);
2360 removed_utarget
= 1;
2364 // remove random units from the map
2365 while (targetUnitMap
.size() > unMaxTargets
- removed_utarget
)
2367 uint32 poz
= urand(0, targetUnitMap
.size()-1);
2368 for (UnitList::iterator itr
= targetUnitMap
.begin(); itr
!= targetUnitMap
.end(); ++itr
, --poz
)
2370 if (!*itr
) continue;
2374 targetUnitMap
.erase(itr
);
2379 // the player's target will always be added to the map
2380 if (removed_utarget
&& m_targets
.getUnitTarget())
2381 targetUnitMap
.push_back(m_targets
.getUnitTarget());
2385 void Spell::prepare(SpellCastTargets
const* targets
, Aura
* triggeredByAura
)
2387 m_targets
= *targets
;
2389 m_spellState
= SPELL_STATE_PREPARING
;
2391 m_castPositionX
= m_caster
->GetPositionX();
2392 m_castPositionY
= m_caster
->GetPositionY();
2393 m_castPositionZ
= m_caster
->GetPositionZ();
2394 m_castOrientation
= m_caster
->GetOrientation();
2397 m_triggeredByAuraSpell
= triggeredByAura
->GetSpellProto();
2399 // create and add update event for this spell
2400 SpellEvent
* Event
= new SpellEvent(this);
2401 m_caster
->m_Events
.AddEvent(Event
, m_caster
->m_Events
.CalculateTime(1));
2403 //Prevent casting at cast another spell (ServerSide check)
2404 if(m_caster
->IsNonMeleeSpellCasted(false, true, true) && m_cast_count
)
2406 SendCastResult(SPELL_FAILED_SPELL_IN_PROGRESS
);
2412 m_powerCost
= CalculatePowerCost();
2414 SpellCastResult result
= CheckCast(true);
2415 if(result
!= SPELL_CAST_OK
&& !IsAutoRepeat()) //always cast autorepeat dummy for triggering
2419 SendChannelUpdate(0);
2420 triggeredByAura
->SetAuraDuration(0);
2422 SendCastResult(result
);
2427 // Prepare data for triggers
2428 prepareDataForTriggerSystem();
2430 // calculate cast time (calculated after first CheckCast check to prevent charge counting for first CheckCast fail)
2431 m_casttime
= GetSpellCastTime(m_spellInfo
, this);
2433 // set timer base at cast time
2436 // stealth must be removed at cast starting (at show channel bar)
2437 // skip triggered spell (item equip spell casting and other not explicit character casts/item uses)
2438 if ( !m_IsTriggeredSpell
&& isSpellBreakStealth(m_spellInfo
) )
2440 m_caster
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
2441 m_caster
->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH
);
2444 // add non-triggered (with cast time and without)
2445 if (!m_IsTriggeredSpell
)
2447 // add to cast type slot
2448 m_caster
->SetCurrentCastedSpell( this );
2450 // will show cast bar
2453 // execute triggered without cast time explicitly in call point
2454 else if(m_timer
== 0)
2456 // else triggered with cast time will execute execute at next tick or later
2457 // without adding to cast type slot
2458 // will not show cast bar but will show effects at casting time etc
2461 void Spell::cancel()
2463 if(m_spellState
== SPELL_STATE_FINISHED
)
2466 m_autoRepeat
= false;
2467 switch (m_spellState
)
2469 case SPELL_STATE_PREPARING
:
2470 case SPELL_STATE_DELAYED
:
2473 SendCastResult(SPELL_FAILED_INTERRUPTED
);
2476 case SPELL_STATE_CASTING
:
2478 for(std::list
<TargetInfo
>::const_iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end(); ++ihit
)
2480 if( ihit
->missCondition
== SPELL_MISS_NONE
)
2482 Unit
* unit
= m_caster
->GetObjectGuid() == (*ihit
).targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
2483 if( unit
&& unit
->isAlive() )
2484 unit
->RemoveAurasByCasterSpell(m_spellInfo
->Id
, m_caster
->GetGUID());
2488 SendChannelUpdate(0);
2490 SendCastResult(SPELL_FAILED_INTERRUPTED
);
2499 m_caster
->RemoveDynObject(m_spellInfo
->Id
);
2500 m_caster
->RemoveGameObject(m_spellInfo
->Id
, true);
2503 void Spell::cast(bool skipCheck
)
2505 SetExecutedCurrently(true);
2507 if (!m_caster
->CheckAndIncreaseCastCounter())
2509 if (m_triggeredByAuraSpell
)
2510 sLog
.outError("Spell %u triggered by aura spell %u too deep in cast chain for cast. Cast not allowed for prevent overflow stack crash.", m_spellInfo
->Id
, m_triggeredByAuraSpell
->Id
);
2512 sLog
.outError("Spell %u too deep in cast chain for cast. Cast not allowed for prevent overflow stack crash.", m_spellInfo
->Id
);
2514 SendCastResult(SPELL_FAILED_ERROR
);
2516 SetExecutedCurrently(false);
2520 // update pointers base at GUIDs to prevent access to non-existed already object
2523 // cancel at lost main target unit
2524 if(!m_targets
.getUnitTarget() && m_targets
.getUnitTargetGUID() && m_targets
.getUnitTargetGUID() != m_caster
->GetGUID())
2527 m_caster
->DecreaseCastCounter();
2528 SetExecutedCurrently(false);
2532 if(m_caster
->GetTypeId() != TYPEID_PLAYER
&& m_targets
.getUnitTarget() && m_targets
.getUnitTarget() != m_caster
)
2533 m_caster
->SetInFront(m_targets
.getUnitTarget());
2535 SpellCastResult castResult
= CheckPower();
2536 if(castResult
!= SPELL_CAST_OK
)
2538 SendCastResult(castResult
);
2540 m_caster
->DecreaseCastCounter();
2541 SetExecutedCurrently(false);
2545 // triggered cast called from Spell::prepare where it was already checked
2548 castResult
= CheckCast(false);
2549 if(castResult
!= SPELL_CAST_OK
)
2551 SendCastResult(castResult
);
2553 m_caster
->DecreaseCastCounter();
2554 SetExecutedCurrently(false);
2559 // different triggred (for caster) and precast (casted before apply effect to target) cases
2560 switch(m_spellInfo
->SpellFamilyName
)
2562 case SPELLFAMILY_GENERIC
:
2564 if (m_spellInfo
->Mechanic
== MECHANIC_BANDAGE
) // Bandages
2565 AddPrecastSpell(11196); // Recently Bandaged
2566 else if(m_spellInfo
->SpellIconID
== 1662 && m_spellInfo
->AttributesEx
& 0x20)
2567 // Blood Fury (Racial)
2568 AddPrecastSpell(23230); // Blood Fury - Healing Reduction
2569 else if(m_spellInfo
->Id
== 20594) // Stoneskin
2570 AddTriggeredSpell(65116); // Stoneskin - armor 10% for 8 sec
2573 case SPELLFAMILY_MAGE
:
2576 if (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x0000008000000000))
2577 AddPrecastSpell(41425); // Hypothermia
2580 case SPELLFAMILY_PRIEST
:
2582 // Power Word: Shield
2583 if (m_spellInfo
->Mechanic
== MECHANIC_SHIELD
&&
2584 (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x0000000000000001)))
2585 AddPrecastSpell(6788); // Weakened Soul
2586 // Prayer of Mending (jump animation), we need formal caster instead original for correct animation
2587 else if (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x0000002000000000))
2588 AddTriggeredSpell(41637);
2590 switch(m_spellInfo
->Id
)
2592 case 15237: AddTriggeredSpell(23455); break;// Holy Nova, rank 1
2593 case 15430: AddTriggeredSpell(23458); break;// Holy Nova, rank 2
2594 case 15431: AddTriggeredSpell(23459); break;// Holy Nova, rank 3
2595 case 27799: AddTriggeredSpell(27803); break;// Holy Nova, rank 4
2596 case 27800: AddTriggeredSpell(27804); break;// Holy Nova, rank 5
2597 case 27801: AddTriggeredSpell(27805); break;// Holy Nova, rank 6
2598 case 25331: AddTriggeredSpell(25329); break;// Holy Nova, rank 7
2599 case 48077: AddTriggeredSpell(48075); break;// Holy Nova, rank 8
2600 case 48078: AddTriggeredSpell(48076); break;// Holy Nova, rank 9
2605 case SPELLFAMILY_DRUID
:
2607 // Faerie Fire (Feral)
2608 if (m_spellInfo
->Id
== 16857 && m_caster
->m_form
!= FORM_CAT
)
2609 AddTriggeredSpell(60089);
2612 case SPELLFAMILY_ROGUE
:
2613 // Fan of Knives (main hand)
2614 if (m_spellInfo
->Id
== 51723 && m_caster
->GetTypeId() == TYPEID_PLAYER
&&
2615 ((Player
*)m_caster
)->haveOffhandWeapon())
2617 AddTriggeredSpell(52874); // Fan of Knives (offhand)
2620 case SPELLFAMILY_PALADIN
:
2622 // Hand of Reckoning
2623 if (m_spellInfo
->Id
== 62124)
2625 if (m_targets
.getUnitTarget() && m_targets
.getUnitTarget()->getVictim() != m_caster
)
2626 AddPrecastSpell(67485); // Hand of Rekoning (no typos in name ;) )
2628 // Divine Shield, Divine Protection or Hand of Protection
2629 else if (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x0000000000400080))
2631 AddPrecastSpell(25771); // Forbearance
2632 AddPrecastSpell(61987); // Avenging Wrath Marker
2634 else if (m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x200000000000))
2635 AddPrecastSpell(61987); // Avenging Wrath Marker
2638 case SPELLFAMILY_SHAMAN
:
2641 if (m_spellInfo
->Id
== 2825)
2642 AddPrecastSpell(57724); // Sated
2644 else if (m_spellInfo
->Id
== 32182)
2645 AddPrecastSpell(57723); // Exhaustion
2646 else if (m_spellInfo
->Effect
[EFFECT_INDEX_0
]==SPELL_EFFECT_APPLY_AREA_AURA_RAID
&& m_spellInfo
->SpellFamilyFlags
& UI64LIT(0x0000000004000000))
2647 // only for main totem spell cast
2648 AddTriggeredSpell(30708); // Totem of Wrath
2655 // traded items have trade slot instead of guid in m_itemTargetGUID
2656 // set to real guid to be sent later to the client
2657 m_targets
.updateTradeSlotItem();
2659 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2661 if (!m_IsTriggeredSpell
&& m_CastItem
)
2662 ((Player
*)m_caster
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_USE_ITEM
, m_CastItem
->GetEntry());
2664 ((Player
*)m_caster
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_CAST_SPELL
, m_spellInfo
->Id
);
2669 if(m_spellState
== SPELL_STATE_FINISHED
) // stop cast if spell marked as finish somewhere in FillTargetMap
2671 m_caster
->DecreaseCastCounter();
2672 SetExecutedCurrently(false);
2677 SendSpellCooldown();
2680 TakeReagents(); // we must remove reagents before HandleEffects to allow place crafted item in same slot
2682 SendCastResult(castResult
);
2683 SendSpellGo(); // we must send smsg_spell_go packet before m_castItem delete in TakeCastItem()...
2685 // Okay, everything is prepared. Now we need to distinguish between immediate and evented delayed spells
2686 if (m_spellInfo
->speed
> 0.0f
)
2689 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2690 // in case delayed spell remove item at cast delay start
2693 // Okay, maps created, now prepare flags
2694 m_immediateHandled
= false;
2695 m_spellState
= SPELL_STATE_DELAYED
;
2700 // Immediate spell, no big deal
2704 m_caster
->DecreaseCastCounter();
2705 SetExecutedCurrently(false);
2708 void Spell::handle_immediate()
2710 // start channeling if applicable
2711 if(IsChanneledSpell(m_spellInfo
))
2713 int32 duration
= GetSpellDuration(m_spellInfo
);
2716 // Apply duration mod
2717 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
2718 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_DURATION
, duration
);
2719 m_spellState
= SPELL_STATE_CASTING
;
2720 SendChannelStart(duration
);
2724 // process immediate effects (items, ground, etc.) also initialize some variables
2725 _handle_immediate_phase();
2727 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end(); ++ihit
)
2728 DoAllEffectOnTarget(&(*ihit
));
2730 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin(); ihit
!= m_UniqueGOTargetInfo
.end(); ++ihit
)
2731 DoAllEffectOnTarget(&(*ihit
));
2733 // spell is finished, perform some last features of the spell here
2734 _handle_finish_phase();
2736 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2739 if(m_spellState
!= SPELL_STATE_CASTING
)
2740 finish(true); // successfully finish spell cast (not last in case autorepeat or channel spell)
2743 uint64
Spell::handle_delayed(uint64 t_offset
)
2745 uint64 next_time
= 0;
2747 if (!m_immediateHandled
)
2749 _handle_immediate_phase();
2750 m_immediateHandled
= true;
2753 // now recheck units targeting correctness (need before any effects apply to prevent adding immunity at first effect not allow apply second spell effect and similar cases)
2754 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end(); ++ihit
)
2756 if (ihit
->processed
== false)
2758 if ( ihit
->timeDelay
<= t_offset
)
2759 DoAllEffectOnTarget(&(*ihit
));
2760 else if( next_time
== 0 || ihit
->timeDelay
< next_time
)
2761 next_time
= ihit
->timeDelay
;
2765 // now recheck gameobject targeting correctness
2766 for(std::list
<GOTargetInfo
>::iterator ighit
= m_UniqueGOTargetInfo
.begin(); ighit
!= m_UniqueGOTargetInfo
.end(); ++ighit
)
2768 if (ighit
->processed
== false)
2770 if ( ighit
->timeDelay
<= t_offset
)
2771 DoAllEffectOnTarget(&(*ighit
));
2772 else if( next_time
== 0 || ighit
->timeDelay
< next_time
)
2773 next_time
= ighit
->timeDelay
;
2776 // All targets passed - need finish phase
2779 // spell is finished, perform some last features of the spell here
2780 _handle_finish_phase();
2782 finish(true); // successfully finish spell cast
2784 // return zero, spell is finished now
2789 // spell is unfinished, return next execution time
2794 void Spell::_handle_immediate_phase()
2796 // handle some immediate features of the spell here
2797 HandleThreatSpells(m_spellInfo
->Id
);
2799 m_needSpellLog
= IsNeedSendToClient();
2800 for(int j
= 0; j
< MAX_EFFECT_INDEX
; ++j
)
2802 if(m_spellInfo
->Effect
[j
] == 0)
2805 // apply Send Event effect to ground in case empty target lists
2806 if( m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SEND_EVENT
&& !HaveTargetsForEffect(SpellEffectIndex(j
)) )
2808 HandleEffects(NULL
, NULL
, NULL
, SpellEffectIndex(j
));
2812 // Don't do spell log, if is school damage spell
2813 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SCHOOL_DAMAGE
|| m_spellInfo
->Effect
[j
] == 0)
2814 m_needSpellLog
= false;
2816 uint32 EffectChainTarget
= m_spellInfo
->EffectChainTarget
[j
];
2817 if (Unit
* realCaster
= GetAffectiveCaster())
2818 if(Player
* modOwner
= realCaster
->GetSpellModOwner())
2819 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_JUMP_TARGETS
, EffectChainTarget
, this);
2821 // initialize multipliers
2822 m_damageMultipliers
[j
] = 1.0f
;
2823 if( (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_DAMAGE
|| m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_HEAL
) &&
2824 (EffectChainTarget
> 1) )
2825 m_applyMultiplierMask
|= (1 << j
);
2828 // initialize Diminishing Returns Data
2829 m_diminishLevel
= DIMINISHING_LEVEL_1
;
2830 m_diminishGroup
= DIMINISHING_NONE
;
2833 for(std::list
<ItemTargetInfo
>::iterator ihit
= m_UniqueItemInfo
.begin(); ihit
!= m_UniqueItemInfo
.end(); ++ihit
)
2834 DoAllEffectOnTarget(&(*ihit
));
2837 for(int j
= 0; j
< MAX_EFFECT_INDEX
; ++j
)
2839 // persistent area auras target only the ground
2840 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_PERSISTENT_AREA_AURA
)
2841 HandleEffects(NULL
, NULL
, NULL
, SpellEffectIndex(j
));
2845 void Spell::_handle_finish_phase()
2852 void Spell::SendSpellCooldown()
2854 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
2857 Player
* _player
= (Player
*)m_caster
;
2859 // mana/health/etc potions, disabled by client (until combat out as declarate)
2860 if (m_CastItem
&& m_CastItem
->IsPotion())
2862 // need in some way provided data for Spell::finish SendCooldownEvent
2863 _player
->SetLastPotionId(m_CastItem
->GetEntry());
2867 // (1) have infinity cooldown but set at aura apply, (2) passive cooldown at triggering
2868 if(m_spellInfo
->Attributes
& (SPELL_ATTR_DISABLED_WHILE_ACTIVE
| SPELL_ATTR_PASSIVE
))
2871 _player
->AddSpellAndCategoryCooldowns(m_spellInfo
, m_CastItem
? m_CastItem
->GetEntry() : 0, this);
2874 void Spell::update(uint32 difftime
)
2876 // update pointers based at it's GUIDs
2879 if(m_targets
.getUnitTargetGUID() && !m_targets
.getUnitTarget())
2885 // check if the player caster has moved before the spell finished
2886 if ((m_caster
->GetTypeId() == TYPEID_PLAYER
&& m_timer
!= 0) &&
2887 (m_castPositionX
!= m_caster
->GetPositionX() || m_castPositionY
!= m_caster
->GetPositionY() || m_castPositionZ
!= m_caster
->GetPositionZ()) &&
2888 (m_spellInfo
->Effect
[EFFECT_INDEX_0
] != SPELL_EFFECT_STUCK
|| !((Player
*)m_caster
)->m_movementInfo
.HasMovementFlag(MOVEFLAG_FALLINGFAR
)))
2890 // always cancel for channeled spells
2891 if( m_spellState
== SPELL_STATE_CASTING
)
2893 // don't cancel for melee, autorepeat, triggered and instant spells
2894 else if(!IsNextMeleeSwingSpell() && !IsAutoRepeat() && !m_IsTriggeredSpell
&& (m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_MOVEMENT
))
2898 switch(m_spellState
)
2900 case SPELL_STATE_PREPARING
:
2904 if(difftime
>= m_timer
)
2907 m_timer
-= difftime
;
2910 if(m_timer
== 0 && !IsNextMeleeSwingSpell() && !IsAutoRepeat())
2913 case SPELL_STATE_CASTING
:
2917 if( m_caster
->GetTypeId() == TYPEID_PLAYER
)
2919 // check if player has jumped before the channeling finished
2920 if(((Player
*)m_caster
)->m_movementInfo
.HasMovementFlag(MOVEFLAG_FALLING
))
2923 // check for incapacitating player states
2924 if( m_caster
->hasUnitState(UNIT_STAT_CAN_NOT_REACT
))
2927 // check if player has turned if flag is set
2928 if( m_spellInfo
->ChannelInterruptFlags
& CHANNEL_FLAG_TURNING
&& m_castOrientation
!= m_caster
->GetOrientation() )
2932 // check if there are alive targets left
2933 if (!IsAliveUnitPresentInTargetList())
2935 SendChannelUpdate(0);
2939 if(difftime
>= m_timer
)
2942 m_timer
-= difftime
;
2947 SendChannelUpdate(0);
2949 // channeled spell processed independently for quest targeting
2950 // cast at creature (or GO) quest objectives update at successful cast channel finished
2951 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
2952 if( !IsAutoRepeat() && !IsNextMeleeSwingSpell() )
2954 if ( Player
* p
= m_caster
->GetCharmerOrOwnerPlayerOrPlayerItself() )
2956 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end(); ++ihit
)
2958 TargetInfo
* target
= &*ihit
;
2959 if(!target
->targetGUID
.IsCreature())
2962 Unit
* unit
= m_caster
->GetObjectGuid() == target
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, target
->targetGUID
);
2966 p
->CastedCreatureOrGO(unit
->GetEntry(), unit
->GetObjectGuid(), m_spellInfo
->Id
);
2969 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin(); ihit
!= m_UniqueGOTargetInfo
.end(); ++ihit
)
2971 GOTargetInfo
* target
= &*ihit
;
2973 GameObject
* go
= m_caster
->GetMap()->GetGameObject(target
->targetGUID
);
2977 p
->CastedCreatureOrGO(go
->GetEntry(), go
->GetObjectGuid(), m_spellInfo
->Id
);
2991 void Spell::finish(bool ok
)
2996 if(m_spellState
== SPELL_STATE_FINISHED
)
2999 m_spellState
= SPELL_STATE_FINISHED
;
3001 // other code related only to successfully finished spells
3005 // remove spell mods
3006 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
3007 ((Player
*)m_caster
)->RemoveSpellMods(this);
3009 // handle SPELL_AURA_ADD_TARGET_TRIGGER auras
3010 Unit::AuraList
const& targetTriggers
= m_caster
->GetAurasByType(SPELL_AURA_ADD_TARGET_TRIGGER
);
3011 for(Unit::AuraList::const_iterator i
= targetTriggers
.begin(); i
!= targetTriggers
.end(); ++i
)
3013 if (!(*i
)->isAffectedOnSpell(m_spellInfo
))
3015 for(std::list
<TargetInfo
>::const_iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end(); ++ihit
)
3017 if( ihit
->missCondition
== SPELL_MISS_NONE
)
3019 // check m_caster->GetGUID() let load auras at login and speedup most often case
3020 Unit
*unit
= m_caster
->GetObjectGuid() == ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
3021 if (unit
&& unit
->isAlive())
3023 SpellEntry
const *auraSpellInfo
= (*i
)->GetSpellProto();
3024 SpellEffectIndex auraSpellIdx
= (*i
)->GetEffIndex();
3025 // Calculate chance at that moment (can be depend for example from combo points)
3026 int32 chance
= m_caster
->CalculateSpellDamage(auraSpellInfo
, auraSpellIdx
, (*i
)->GetBasePoints(),unit
);
3027 if(roll_chance_i(chance
))
3028 m_caster
->CastSpell(unit
, auraSpellInfo
->EffectTriggerSpell
[auraSpellIdx
], true, NULL
, (*i
));
3034 // Heal caster for all health leech from all targets
3036 m_caster
->DealHeal(m_caster
, uint32(m_healthLeech
), m_spellInfo
);
3038 if (IsMeleeAttackResetSpell())
3040 m_caster
->resetAttackTimer(BASE_ATTACK
);
3041 if(m_caster
->haveOffhandWeapon())
3042 m_caster
->resetAttackTimer(OFF_ATTACK
);
3045 /*if (IsRangedAttackResetSpell())
3046 m_caster->resetAttackTimer(RANGED_ATTACK);*/
3048 // Clear combo at finish state
3049 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& NeedsComboPoints(m_spellInfo
))
3051 // Not drop combopoints if negative spell and if any miss on enemy exist
3052 bool needDrop
= true;
3053 if (!IsPositiveSpell(m_spellInfo
->Id
))
3055 for(std::list
<TargetInfo
>::const_iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end(); ++ihit
)
3057 if (ihit
->missCondition
!= SPELL_MISS_NONE
&& ihit
->targetGUID
!= m_caster
->GetObjectGuid())
3065 ((Player
*)m_caster
)->ClearComboPoints();
3068 // potions disabled by client, send event "not in combat" if need
3069 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
3070 ((Player
*)m_caster
)->UpdatePotionCooldown(this);
3072 // call triggered spell only at successful cast (after clear combo points -> for add some if need)
3073 if(!m_TriggerSpells
.empty())
3074 CastTriggerSpells();
3076 // Stop Attack for some spells
3077 if( m_spellInfo
->Attributes
& SPELL_ATTR_STOP_ATTACK_TARGET
)
3078 m_caster
->AttackStop();
3081 void Spell::SendCastResult(SpellCastResult result
)
3083 if(result
== SPELL_CAST_OK
)
3086 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
3089 if(((Player
*)m_caster
)->GetSession()->PlayerLoading()) // don't send cast results at loading time
3092 SendCastResult((Player
*)m_caster
, m_spellInfo
, m_cast_count
, result
);
3095 void Spell::SendCastResult(Player
* caster
, SpellEntry
const* spellInfo
, uint8 cast_count
, SpellCastResult result
)
3097 if(result
== SPELL_CAST_OK
)
3100 WorldPacket
data(SMSG_CAST_FAILED
, (4+1+1));
3101 data
<< uint8(cast_count
); // single cast or multi 2.3 (0/1)
3102 data
<< uint32(spellInfo
->Id
);
3103 data
<< uint8(result
); // problem
3106 case SPELL_FAILED_REQUIRES_SPELL_FOCUS
:
3107 data
<< uint32(spellInfo
->RequiresSpellFocus
);
3109 case SPELL_FAILED_REQUIRES_AREA
:
3110 // hardcode areas limitation case
3111 switch(spellInfo
->Id
)
3113 case 41617: // Cenarion Mana Salve
3114 case 41619: // Cenarion Healing Salve
3115 data
<< uint32(3905);
3117 case 41618: // Bottled Nethergon Energy
3118 case 41620: // Bottled Nethergon Vapor
3119 data
<< uint32(3842);
3121 case 45373: // Bloodberry Elixir
3122 data
<< uint32(4075);
3124 default: // default case (don't must be)
3129 case SPELL_FAILED_TOTEMS
:
3130 if(spellInfo
->Totem
[0])
3131 data
<< uint32(spellInfo
->Totem
[0]);
3132 if(spellInfo
->Totem
[1])
3133 data
<< uint32(spellInfo
->Totem
[1]);
3135 case SPELL_FAILED_TOTEM_CATEGORY
:
3136 if(spellInfo
->TotemCategory
[0])
3137 data
<< uint32(spellInfo
->TotemCategory
[0]);
3138 if(spellInfo
->TotemCategory
[1])
3139 data
<< uint32(spellInfo
->TotemCategory
[1]);
3141 case SPELL_FAILED_EQUIPPED_ITEM_CLASS
:
3142 data
<< uint32(spellInfo
->EquippedItemClass
);
3143 data
<< uint32(spellInfo
->EquippedItemSubClassMask
);
3144 //data << uint32(spellInfo->EquippedItemInventoryTypeMask);
3149 caster
->GetSession()->SendPacket(&data
);
3152 void Spell::SendSpellStart()
3154 if (!IsNeedSendToClient())
3157 sLog
.outDebug("Sending SMSG_SPELL_START id=%u", m_spellInfo
->Id
);
3159 uint32 castFlags
= CAST_FLAG_UNKNOWN1
;
3160 if (IsRangedSpell())
3161 castFlags
|= CAST_FLAG_AMMO
;
3163 if (m_spellInfo
->runeCostID
)
3164 castFlags
|= CAST_FLAG_UNKNOWN10
;
3166 WorldPacket
data(SMSG_SPELL_START
, (8+8+4+4+2));
3168 data
<< m_CastItem
->GetPackGUID();
3170 data
<< m_caster
->GetPackGUID();
3172 data
<< m_caster
->GetPackGUID();
3173 data
<< uint8(m_cast_count
); // pending spell cast?
3174 data
<< uint32(m_spellInfo
->Id
); // spellId
3175 data
<< uint32(castFlags
); // cast flags
3176 data
<< uint32(m_timer
); // delay?
3179 if ( castFlags
& CAST_FLAG_UNKNOWN6
) // predicted power?
3182 if ( castFlags
& CAST_FLAG_UNKNOWN7
) // rune cooldowns list
3184 uint8 v1
= 0;//m_runesState;
3185 uint8 v2
= 0;//((Player*)m_caster)->GetRunesState();
3186 data
<< uint8(v1
); // runes state before
3187 data
<< uint8(v2
); // runes state after
3188 for(uint8 i
= 0; i
< MAX_RUNES
; ++i
)
3191 if(m
& v1
) // usable before...
3192 if(!(m
& v2
)) // ...but on cooldown now...
3193 data
<< uint8(0); // some unknown byte (time?)
3197 if ( castFlags
& CAST_FLAG_AMMO
)
3198 WriteAmmoToPacket(&data
);
3200 if ( castFlags
& CAST_FLAG_UNKNOWN21
)
3206 m_caster
->SendMessageToSet(&data
, true);
3209 void Spell::SendSpellGo()
3211 // not send invisible spell casting
3212 if(!IsNeedSendToClient())
3215 sLog
.outDebug("Sending SMSG_SPELL_GO id=%u", m_spellInfo
->Id
);
3217 uint32 castFlags
= CAST_FLAG_UNKNOWN3
;
3219 castFlags
|= CAST_FLAG_AMMO
; // arrows/bullets visual
3221 if((m_caster
->GetTypeId() == TYPEID_PLAYER
) && (m_caster
->getClass() == CLASS_DEATH_KNIGHT
) && m_spellInfo
->runeCostID
)
3223 castFlags
|= CAST_FLAG_UNKNOWN10
; // same as in SMSG_SPELL_START
3224 castFlags
|= CAST_FLAG_UNKNOWN6
; // makes cooldowns visible
3225 castFlags
|= CAST_FLAG_UNKNOWN7
; // rune cooldowns list
3228 WorldPacket
data(SMSG_SPELL_GO
, 50); // guess size
3231 data
<< m_CastItem
->GetPackGUID();
3233 data
<< m_caster
->GetPackGUID();
3235 data
<< m_caster
->GetPackGUID();
3236 data
<< uint8(m_cast_count
); // pending spell cast?
3237 data
<< uint32(m_spellInfo
->Id
); // spellId
3238 data
<< uint32(castFlags
); // cast flags
3239 data
<< uint32(getMSTime()); // timestamp
3241 WriteSpellGoTargets(&data
);
3245 if ( castFlags
& CAST_FLAG_UNKNOWN6
) // unknown wotlk, predicted power?
3248 if ( castFlags
& CAST_FLAG_UNKNOWN7
) // rune cooldowns list
3250 uint8 v1
= m_runesState
;
3251 uint8 v2
= m_caster
->getClass() == CLASS_DEATH_KNIGHT
? ((Player
*)m_caster
)->GetRunesState() : 0;
3252 data
<< uint8(v1
); // runes state before
3253 data
<< uint8(v2
); // runes state after
3254 for(uint8 i
= 0; i
< MAX_RUNES
; ++i
)
3257 if(m
& v1
) // usable before...
3258 if(!(m
& v2
)) // ...but on cooldown now...
3259 data
<< uint8(0); // some unknown byte (time?)
3263 if ( castFlags
& CAST_FLAG_UNKNOWN4
) // unknown wotlk
3269 if ( castFlags
& CAST_FLAG_AMMO
)
3270 WriteAmmoToPacket(&data
);
3272 if ( castFlags
& CAST_FLAG_UNKNOWN5
) // unknown wotlk
3278 if ( m_targets
.m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
3283 m_caster
->SendMessageToSet(&data
, true);
3286 void Spell::WriteAmmoToPacket( WorldPacket
* data
)
3288 uint32 ammoInventoryType
= 0;
3289 uint32 ammoDisplayID
= 0;
3291 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
3293 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack( RANGED_ATTACK
);
3296 ammoInventoryType
= pItem
->GetProto()->InventoryType
;
3297 if( ammoInventoryType
== INVTYPE_THROWN
)
3298 ammoDisplayID
= pItem
->GetProto()->DisplayInfoID
;
3301 uint32 ammoID
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
3304 ItemPrototype
const *pProto
= ObjectMgr::GetItemPrototype( ammoID
);
3307 ammoDisplayID
= pProto
->DisplayInfoID
;
3308 ammoInventoryType
= pProto
->InventoryType
;
3311 else if(m_caster
->GetDummyAura(46699)) // Requires No Ammo
3313 ammoDisplayID
= 5996; // normal arrow
3314 ammoInventoryType
= INVTYPE_AMMO
;
3321 for (uint8 i
= 0; i
< 3; ++i
)
3323 if(uint32 item_id
= m_caster
->GetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_ID
+ i
))
3325 if(ItemEntry
const * itemEntry
= sItemStore
.LookupEntry(item_id
))
3327 if(itemEntry
->Class
== ITEM_CLASS_WEAPON
)
3329 switch(itemEntry
->SubClass
)
3331 case ITEM_SUBCLASS_WEAPON_THROWN
:
3332 ammoDisplayID
= itemEntry
->DisplayId
;
3333 ammoInventoryType
= itemEntry
->InventoryType
;
3335 case ITEM_SUBCLASS_WEAPON_BOW
:
3336 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
3337 ammoDisplayID
= 5996; // is this need fixing?
3338 ammoInventoryType
= INVTYPE_AMMO
;
3340 case ITEM_SUBCLASS_WEAPON_GUN
:
3341 ammoDisplayID
= 5998; // is this need fixing?
3342 ammoInventoryType
= INVTYPE_AMMO
;
3354 *data
<< uint32(ammoDisplayID
);
3355 *data
<< uint32(ammoInventoryType
);
3358 void Spell::WriteSpellGoTargets( WorldPacket
* data
)
3360 // This function also fill data for channeled spells:
3361 // m_needAliveTargetMask req for stop channeling if one target die
3362 uint32 hit
= m_UniqueGOTargetInfo
.size(); // Always hits on GO
3364 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end(); ++ihit
)
3366 if ((*ihit
).effectMask
== 0) // No effect apply - all immuned add state
3368 // possibly SPELL_MISS_IMMUNE2 for this??
3369 ihit
->missCondition
= SPELL_MISS_IMMUNE2
;
3372 else if ((*ihit
).missCondition
== SPELL_MISS_NONE
)
3378 *data
<< (uint8
)hit
;
3379 for(std::list
<TargetInfo
>::const_iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end(); ++ihit
)
3381 if ((*ihit
).missCondition
== SPELL_MISS_NONE
) // Add only hits
3383 *data
<< ihit
->targetGUID
;
3384 m_needAliveTargetMask
|=ihit
->effectMask
;
3388 for(std::list
<GOTargetInfo
>::const_iterator ighit
= m_UniqueGOTargetInfo
.begin(); ighit
!= m_UniqueGOTargetInfo
.end(); ++ighit
)
3389 *data
<< ighit
->targetGUID
; // Always hits
3391 *data
<< (uint8
)miss
;
3392 for(std::list
<TargetInfo
>::const_iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end(); ++ihit
)
3394 if( ihit
->missCondition
!= SPELL_MISS_NONE
) // Add only miss
3396 *data
<< ihit
->targetGUID
;
3397 *data
<< uint8(ihit
->missCondition
);
3398 if( ihit
->missCondition
== SPELL_MISS_REFLECT
)
3399 *data
<< uint8(ihit
->reflectResult
);
3402 // Reset m_needAliveTargetMask for non channeled spell
3403 if(!IsChanneledSpell(m_spellInfo
))
3404 m_needAliveTargetMask
= 0;
3407 void Spell::SendLogExecute()
3409 Unit
*target
= m_targets
.getUnitTarget() ? m_targets
.getUnitTarget() : m_caster
;
3411 WorldPacket
data(SMSG_SPELLLOGEXECUTE
, (8+4+4+4+4+8));
3413 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
3414 data
<< m_caster
->GetPackGUID();
3416 data
<< target
->GetPackGUID();
3418 data
<< uint32(m_spellInfo
->Id
);
3420 data
<< uint32(count1
); // count1 (effect count?)
3421 for(uint32 i
= 0; i
< count1
; ++i
)
3423 data
<< uint32(m_spellInfo
->Effect
[EFFECT_INDEX_0
]);// spell effect
3425 data
<< uint32(count2
); // count2 (target count?)
3426 for(uint32 j
= 0; j
< count2
; ++j
)
3428 switch(m_spellInfo
->Effect
[EFFECT_INDEX_0
])
3430 case SPELL_EFFECT_POWER_DRAIN
:
3431 if(Unit
*unit
= m_targets
.getUnitTarget())
3432 data
<< unit
->GetPackGUID();
3439 case SPELL_EFFECT_ADD_EXTRA_ATTACKS
:
3440 if(Unit
*unit
= m_targets
.getUnitTarget())
3441 data
<< unit
->GetPackGUID();
3444 data
<< uint32(0); // count?
3446 case SPELL_EFFECT_INTERRUPT_CAST
:
3447 if(Unit
*unit
= m_targets
.getUnitTarget())
3448 data
<< unit
->GetPackGUID();
3451 data
<< uint32(0); // spellid
3453 case SPELL_EFFECT_DURABILITY_DAMAGE
:
3454 if(Unit
*unit
= m_targets
.getUnitTarget())
3455 data
<< unit
->GetPackGUID();
3461 case SPELL_EFFECT_OPEN_LOCK
:
3462 if(Item
*item
= m_targets
.getItemTarget())
3463 data
<< item
->GetPackGUID();
3467 case SPELL_EFFECT_CREATE_ITEM
:
3468 case SPELL_EFFECT_CREATE_ITEM_2
:
3469 data
<< uint32(m_spellInfo
->EffectItemType
[EFFECT_INDEX_0
]);
3471 case SPELL_EFFECT_SUMMON
:
3472 case SPELL_EFFECT_TRANS_DOOR
:
3473 case SPELL_EFFECT_SUMMON_PET
:
3474 case SPELL_EFFECT_SUMMON_OBJECT_WILD
:
3475 case SPELL_EFFECT_CREATE_HOUSE
:
3476 case SPELL_EFFECT_DUEL
:
3477 case SPELL_EFFECT_SUMMON_OBJECT_SLOT1
:
3478 case SPELL_EFFECT_SUMMON_OBJECT_SLOT2
:
3479 case SPELL_EFFECT_SUMMON_OBJECT_SLOT3
:
3480 case SPELL_EFFECT_SUMMON_OBJECT_SLOT4
:
3481 if(Unit
*unit
= m_targets
.getUnitTarget())
3482 data
<< unit
->GetPackGUID();
3483 else if(m_targets
.getItemTargetGUID())
3484 data
.appendPackGUID(m_targets
.getItemTargetGUID());
3485 else if(GameObject
*go
= m_targets
.getGOTarget())
3486 data
<< go
->GetPackGUID();
3488 data
<< uint8(0); // guid
3490 case SPELL_EFFECT_FEED_PET
:
3491 data
<< uint32(m_targets
.getItemTargetEntry());
3493 case SPELL_EFFECT_DISMISS_PET
:
3494 if(Unit
*unit
= m_targets
.getUnitTarget())
3495 data
<< unit
->GetPackGUID();
3499 case SPELL_EFFECT_RESURRECT
:
3500 case SPELL_EFFECT_RESURRECT_NEW
:
3501 if(Unit
*unit
= m_targets
.getUnitTarget())
3502 data
<< unit
->GetPackGUID();
3512 m_caster
->SendMessageToSet(&data
, true);
3515 void Spell::SendInterrupted(uint8 result
)
3517 WorldPacket
data(SMSG_SPELL_FAILURE
, (8+4+1));
3518 data
<< m_caster
->GetPackGUID();
3519 data
<< uint8(m_cast_count
);
3520 data
<< uint32(m_spellInfo
->Id
);
3521 data
<< uint8(result
);
3522 m_caster
->SendMessageToSet(&data
, true);
3524 data
.Initialize(SMSG_SPELL_FAILED_OTHER
, (8+4));
3525 data
<< m_caster
->GetPackGUID();
3526 data
<< uint8(m_cast_count
);
3527 data
<< uint32(m_spellInfo
->Id
);
3528 data
<< uint8(result
);
3529 m_caster
->SendMessageToSet(&data
, true);
3532 void Spell::SendChannelUpdate(uint32 time
)
3536 m_caster
->RemoveAurasByCasterSpell(m_spellInfo
->Id
, m_caster
->GetGUID());
3538 ObjectGuid target_guid
= m_caster
->GetChannelObjectGUID();
3539 if (target_guid
!= m_caster
->GetObjectGuid() && target_guid
.IsUnit())
3540 if (Unit
* target
= ObjectAccessor::GetUnit(*m_caster
, target_guid
))
3541 target
->RemoveAurasByCasterSpell(m_spellInfo
->Id
, m_caster
->GetGUID());
3543 m_caster
->SetChannelObjectGUID(0);
3544 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
, 0);
3547 WorldPacket
data( MSG_CHANNEL_UPDATE
, 8+4 );
3548 data
<< m_caster
->GetPackGUID();
3549 data
<< uint32(time
);
3550 m_caster
->SendMessageToSet(&data
, true);
3553 void Spell::SendChannelStart(uint32 duration
)
3555 WorldObject
* target
= NULL
;
3557 // select first not resisted target from target list for _0_ effect
3558 if(!m_UniqueTargetInfo
.empty())
3560 for(std::list
<TargetInfo
>::const_iterator itr
= m_UniqueTargetInfo
.begin(); itr
!= m_UniqueTargetInfo
.end(); ++itr
)
3562 if( (itr
->effectMask
& (1 << 0)) && itr
->reflectResult
== SPELL_MISS_NONE
&& itr
->targetGUID
!= m_caster
->GetObjectGuid())
3564 target
= ObjectAccessor::GetUnit(*m_caster
, itr
->targetGUID
);
3569 else if(!m_UniqueGOTargetInfo
.empty())
3571 for(std::list
<GOTargetInfo
>::const_iterator itr
= m_UniqueGOTargetInfo
.begin(); itr
!= m_UniqueGOTargetInfo
.end(); ++itr
)
3573 if(itr
->effectMask
& (1 << 0) )
3575 target
= m_caster
->GetMap()->GetGameObject(itr
->targetGUID
);
3581 WorldPacket
data( MSG_CHANNEL_START
, (8+4+4) );
3582 data
<< m_caster
->GetPackGUID();
3583 data
<< uint32(m_spellInfo
->Id
);
3584 data
<< uint32(duration
);
3585 m_caster
->SendMessageToSet(&data
, true);
3590 m_caster
->SetChannelObjectGUID(target
->GetGUID());
3592 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
, m_spellInfo
->Id
);
3595 void Spell::SendResurrectRequest(Player
* target
)
3597 // Both players and NPCs can resurrect using spells - have a look at creature 28487 for example
3598 // However, the packet structure differs slightly
3600 const char* sentName
= m_caster
->GetTypeId() == TYPEID_PLAYER
? "" : m_caster
->GetNameForLocaleIdx(target
->GetSession()->GetSessionDbLocaleIndex());
3602 WorldPacket
data(SMSG_RESURRECT_REQUEST
, (8+4+strlen(sentName
)+1+1+1));
3603 data
<< uint64(m_caster
->GetGUID());
3604 data
<< uint32(strlen(sentName
) + 1);
3609 data
<< uint8(m_caster
->GetTypeId() == TYPEID_PLAYER
? 0 : 1);
3610 target
->GetSession()->SendPacket(&data
);
3613 void Spell::SendPlaySpellVisual(uint32 SpellID
)
3615 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
3618 WorldPacket
data(SMSG_PLAY_SPELL_VISUAL
, 8 + 4);
3619 data
<< uint64(m_caster
->GetGUID());
3620 data
<< uint32(SpellID
); // spell visual id?
3621 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
3624 void Spell::TakeCastItem()
3626 if(!m_CastItem
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
3629 // not remove cast item at triggered spell (equipping, weapon damage, etc)
3630 if(m_IsTriggeredSpell
)
3633 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3637 // This code is to avoid a crash
3638 // I'm not sure, if this is really an error, but I guess every item needs a prototype
3639 sLog
.outError("Cast item (%s) has no item prototype", m_CastItem
->GetObjectGuid().GetString().c_str());
3643 bool expendable
= false;
3644 bool withoutCharges
= false;
3646 for (int i
= 0; i
< MAX_ITEM_PROTO_SPELLS
; ++i
)
3648 if (proto
->Spells
[i
].SpellId
)
3650 // item has limited charges
3651 if (proto
->Spells
[i
].SpellCharges
)
3653 if (proto
->Spells
[i
].SpellCharges
< 0 && !(proto
->ExtraFlags
& ITEM_EXTRA_NON_CONSUMABLE
))
3656 int32 charges
= m_CastItem
->GetSpellCharges(i
);
3658 // item has charges left
3661 (charges
> 0) ? --charges
: ++charges
; // abs(charges) less at 1 after use
3662 if (proto
->Stackable
== 1)
3663 m_CastItem
->SetSpellCharges(i
, charges
);
3664 m_CastItem
->SetState(ITEM_CHANGED
, (Player
*)m_caster
);
3668 withoutCharges
= (charges
== 0);
3673 if (expendable
&& withoutCharges
)
3676 ((Player
*)m_caster
)->DestroyItemCount(m_CastItem
, count
, true);
3678 // prevent crash at access to deleted m_targets.getItemTarget
3679 if(m_CastItem
==m_targets
.getItemTarget())
3680 m_targets
.setItemTarget(NULL
);
3686 void Spell::TakePower()
3688 if(m_CastItem
|| m_triggeredByAuraSpell
)
3691 // health as power used
3692 if(m_spellInfo
->powerType
== POWER_HEALTH
)
3694 m_caster
->ModifyHealth( -(int32
)m_powerCost
);
3698 if(m_spellInfo
->powerType
>= MAX_POWERS
)
3700 sLog
.outError("Spell::TakePower: Unknown power type '%d'", m_spellInfo
->powerType
);
3704 Powers powerType
= Powers(m_spellInfo
->powerType
);
3706 if(powerType
== POWER_RUNE
)
3708 CheckOrTakeRunePower(true);
3712 m_caster
->ModifyPower(powerType
, -(int32
)m_powerCost
);
3714 // Set the five second timer
3715 if (powerType
== POWER_MANA
&& m_powerCost
> 0)
3716 m_caster
->SetLastManaUse();
3719 SpellCastResult
Spell::CheckOrTakeRunePower(bool take
)
3721 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
3722 return SPELL_CAST_OK
;
3724 Player
*plr
= (Player
*)m_caster
;
3726 if(plr
->getClass() != CLASS_DEATH_KNIGHT
)
3727 return SPELL_CAST_OK
;
3729 SpellRuneCostEntry
const *src
= sSpellRuneCostStore
.LookupEntry(m_spellInfo
->runeCostID
);
3732 return SPELL_CAST_OK
;
3734 if(src
->NoRuneCost() && (!take
|| src
->NoRunicPowerGain()))
3735 return SPELL_CAST_OK
;
3738 m_runesState
= plr
->GetRunesState(); // store previous state
3740 // at this moment for rune cost exist only no cost mods, and no percent mods
3741 int32 runeCostMod
= 10000;
3742 if(Player
* modOwner
= plr
->GetSpellModOwner())
3743 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COST
, runeCostMod
, this);
3745 if (runeCostMod
> 0)
3747 int32 runeCost
[NUM_RUNE_TYPES
]; // blood, frost, unholy, death
3749 // init cost data and apply mods
3750 for(uint32 i
= 0; i
< RUNE_DEATH
; ++i
)
3751 runeCost
[i
] = runeCostMod
> 0 ? src
->RuneCost
[i
] : 0;
3753 runeCost
[RUNE_DEATH
] = 0; // calculated later
3755 // scan non-death runes (death rune not used explicitly in rune costs)
3756 for(uint32 i
= 0; i
< MAX_RUNES
; ++i
)
3758 RuneType rune
= plr
->GetCurrentRune(i
);
3759 if (runeCost
[rune
] <= 0)
3763 if(plr
->GetRuneCooldown(i
) != 0)
3767 plr
->SetRuneCooldown(i
, RUNE_COOLDOWN
); // 5*2=10 sec
3772 // collect all not counted rune costs to death runes cost
3773 for(uint32 i
= 0; i
< RUNE_DEATH
; ++i
)
3775 runeCost
[RUNE_DEATH
] += runeCost
[i
];
3778 if(runeCost
[RUNE_DEATH
] > 0)
3780 for(uint32 i
= 0; i
< MAX_RUNES
&& runeCost
[RUNE_DEATH
]; ++i
)
3782 RuneType rune
= plr
->GetCurrentRune(i
);
3783 if (rune
!= RUNE_DEATH
)
3787 if(plr
->GetRuneCooldown(i
) != 0)
3791 plr
->SetRuneCooldown(i
, RUNE_COOLDOWN
); // 5*2=10 sec
3796 plr
->ConvertRune(i
, plr
->GetBaseRune(i
));
3800 if(!take
&& runeCost
[RUNE_DEATH
] > 0)
3801 return SPELL_FAILED_NO_POWER
; // not sure if result code is correct
3806 // you can gain some runic power when use runes
3807 float rp
= float(src
->runePowerGain
);
3808 rp
*= sWorld
.getConfig(CONFIG_FLOAT_RATE_POWER_RUNICPOWER_INCOME
);
3809 plr
->ModifyPower(POWER_RUNIC_POWER
, (int32
)rp
);
3812 return SPELL_CAST_OK
;
3815 void Spell::TakeReagents()
3817 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
3820 if (IgnoreItemRequirements()) // reagents used in triggered spell removed by original spell or don't must be removed.
3823 Player
* p_caster
= (Player
*)m_caster
;
3824 if (p_caster
->CanNoReagentCast(m_spellInfo
) )
3827 for(uint32 x
= 0; x
< 8; ++x
)
3829 if(m_spellInfo
->Reagent
[x
] <= 0)
3832 uint32 itemid
= m_spellInfo
->Reagent
[x
];
3833 uint32 itemcount
= m_spellInfo
->ReagentCount
[x
];
3835 // if CastItem is also spell reagent
3838 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3839 if( proto
&& proto
->ItemId
== itemid
)
3841 for(int s
= 0; s
< MAX_ITEM_PROTO_SPELLS
; ++s
)
3843 // CastItem will be used up and does not count as reagent
3844 int32 charges
= m_CastItem
->GetSpellCharges(s
);
3845 if (proto
->Spells
[s
].SpellCharges
< 0 && abs(charges
) < 2)
3856 // if getItemTarget is also spell reagent
3857 if (m_targets
.getItemTargetEntry() == itemid
)
3858 m_targets
.setItemTarget(NULL
);
3860 p_caster
->DestroyItemCount(itemid
, itemcount
, true);
3864 void Spell::HandleThreatSpells(uint32 spellId
)
3866 if(!m_targets
.getUnitTarget() || !spellId
)
3869 if(!m_targets
.getUnitTarget()->CanHaveThreatList())
3872 uint16 threat
= sSpellMgr
.GetSpellThreat(spellId
);
3877 m_targets
.getUnitTarget()->AddThreat(m_caster
, float(threat
), false, GetSpellSchoolMask(m_spellInfo
), m_spellInfo
);
3879 DEBUG_LOG("Spell %u, rank %u, added an additional %i threat", spellId
, sSpellMgr
.GetSpellRank(spellId
), threat
);
3882 void Spell::HandleEffects(Unit
*pUnitTarget
,Item
*pItemTarget
,GameObject
*pGOTarget
,SpellEffectIndex i
, float DamageMultiplier
)
3884 unitTarget
= pUnitTarget
;
3885 itemTarget
= pItemTarget
;
3886 gameObjTarget
= pGOTarget
;
3888 uint8 eff
= m_spellInfo
->Effect
[i
];
3890 damage
= int32(CalculateDamage(i
, unitTarget
) * DamageMultiplier
);
3892 sLog
.outDebug( "Spell %u Effect%d : %u", m_spellInfo
->Id
, i
, eff
);
3894 if(eff
<TOTAL_SPELL_EFFECTS
)
3896 //sLog.outDebug( "WORLD: Spell FX %d < TOTAL_SPELL_EFFECTS ", eff);
3897 (*this.*SpellEffects
[eff
])(i
);
3902 sLog.outDebug( "WORLD: Spell FX %d > TOTAL_SPELL_EFFECTS ", eff);
3904 EffectEnchantItemTmp(i);
3907 sLog.outError("SPELL: unknown effect %u spell id %u",
3908 eff, m_spellInfo->Id);
3915 void Spell::AddTriggeredSpell( uint32 spellId
)
3917 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(spellId
);
3921 sLog
.outError("Spell::AddTriggeredSpell: unknown spell id %u used as triggred spell for spell %u)", spellId
, m_spellInfo
->Id
);
3925 m_TriggerSpells
.push_back(spellInfo
);
3928 void Spell::AddPrecastSpell( uint32 spellId
)
3930 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(spellId
);
3934 sLog
.outError("Spell::AddPrecastSpell: unknown spell id %u used as pre-cast spell for spell %u)", spellId
, m_spellInfo
->Id
);
3938 m_preCastSpells
.push_back(spellInfo
);
3941 void Spell::CastTriggerSpells()
3943 for(SpellInfoList::const_iterator si
= m_TriggerSpells
.begin(); si
!= m_TriggerSpells
.end(); ++si
)
3945 Spell
* spell
= new Spell(m_caster
, (*si
), true, m_originalCasterGUID
, m_selfContainer
);
3946 spell
->prepare(&m_targets
); // use original spell original targets
3950 void Spell::CastPreCastSpells(Unit
* target
)
3952 for(SpellInfoList::const_iterator si
= m_preCastSpells
.begin(); si
!= m_preCastSpells
.end(); ++si
)
3953 m_caster
->CastSpell(target
, (*si
), true, m_CastItem
);
3956 SpellCastResult
Spell::CheckCast(bool strict
)
3958 // check cooldowns to prevent cheating (ignore passive spells, that client side visual only)
3959 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& !(m_spellInfo
->Attributes
& SPELL_ATTR_PASSIVE
) &&
3960 ((Player
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
3962 if(m_triggeredByAuraSpell
)
3963 return SPELL_FAILED_DONT_REPORT
;
3965 return SPELL_FAILED_NOT_READY
;
3968 // only allow triggered spells if at an ended battleground
3969 if( !m_IsTriggeredSpell
&& m_caster
->GetTypeId() == TYPEID_PLAYER
)
3970 if(BattleGround
* bg
= ((Player
*)m_caster
)->GetBattleGround())
3971 if(bg
->GetStatus() == STATUS_WAIT_LEAVE
)
3972 return SPELL_FAILED_DONT_REPORT
;
3974 // only check at first call, Stealth auras are already removed at second call
3975 // for now, ignore triggered spells
3976 if( strict
&& !m_IsTriggeredSpell
)
3978 bool checkForm
= true;
3979 // Ignore form req aura
3980 Unit::AuraList
const& ignore
= m_caster
->GetAurasByType(SPELL_AURA_MOD_IGNORE_SHAPESHIFT
);
3981 for(Unit::AuraList::const_iterator i
= ignore
.begin(); i
!= ignore
.end(); ++i
)
3983 if (!(*i
)->isAffectedOnSpell(m_spellInfo
))
3990 // Cannot be used in this stance/form
3991 SpellCastResult shapeError
= GetErrorAtShapeshiftedCast(m_spellInfo
, m_caster
->m_form
);
3992 if(shapeError
!= SPELL_CAST_OK
)
3995 if ((m_spellInfo
->Attributes
& SPELL_ATTR_ONLY_STEALTHED
) && !(m_caster
->HasStealthAura()))
3996 return SPELL_FAILED_ONLY_STEALTHED
;
4000 // caster state requirements
4001 if(m_spellInfo
->CasterAuraState
&& !m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraState
)))
4002 return SPELL_FAILED_CASTER_AURASTATE
;
4003 if(m_spellInfo
->CasterAuraStateNot
&& m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraStateNot
)))
4004 return SPELL_FAILED_CASTER_AURASTATE
;
4006 // Caster aura req check if need
4007 if(m_spellInfo
->casterAuraSpell
&& !m_caster
->HasAura(m_spellInfo
->casterAuraSpell
))
4008 return SPELL_FAILED_CASTER_AURASTATE
;
4009 if(m_spellInfo
->excludeCasterAuraSpell
)
4011 // Special cases of non existing auras handling
4012 if(m_spellInfo
->excludeCasterAuraSpell
== 61988)
4014 // Avenging Wrath Marker
4015 if(m_caster
->HasAura(61987))
4016 return SPELL_FAILED_CASTER_AURASTATE
;
4018 else if(m_caster
->HasAura(m_spellInfo
->excludeCasterAuraSpell
))
4019 return SPELL_FAILED_CASTER_AURASTATE
;
4022 // cancel autorepeat spells if cast start when moving
4023 // (not wand currently autorepeat cast delayed to moving stop anyway in spell update code)
4024 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& ((Player
*)m_caster
)->isMoving() )
4026 // skip stuck spell to allow use it in falling case and apply spell limitations at movement
4027 if( (!((Player
*)m_caster
)->m_movementInfo
.HasMovementFlag(MOVEFLAG_FALLINGFAR
) || m_spellInfo
->Effect
[EFFECT_INDEX_0
] != SPELL_EFFECT_STUCK
) &&
4028 (IsAutoRepeat() || (m_spellInfo
->AuraInterruptFlags
& AURA_INTERRUPT_FLAG_NOT_SEATED
) != 0) )
4029 return SPELL_FAILED_MOVING
;
4032 if(Unit
*target
= m_targets
.getUnitTarget())
4034 // target state requirements (not allowed state), apply to self also
4035 if(m_spellInfo
->TargetAuraStateNot
&& target
->HasAuraState(AuraState(m_spellInfo
->TargetAuraStateNot
)))
4036 return SPELL_FAILED_TARGET_AURASTATE
;
4038 if (!m_IsTriggeredSpell
&& IsDeathOnlySpell(m_spellInfo
) && target
->isAlive())
4039 return SPELL_FAILED_TARGET_NOT_DEAD
;
4041 // Target aura req check if need
4042 if(m_spellInfo
->targetAuraSpell
&& !target
->HasAura(m_spellInfo
->targetAuraSpell
))
4043 return SPELL_FAILED_CASTER_AURASTATE
;
4044 if(m_spellInfo
->excludeTargetAuraSpell
)
4046 // Special cases of non existing auras handling
4047 if (m_spellInfo
->excludeTargetAuraSpell
== 61988)
4049 // Avenging Wrath Marker
4050 if (target
->HasAura(61987))
4051 return SPELL_FAILED_CASTER_AURASTATE
;
4054 else if (target
->HasAura(m_spellInfo
->excludeTargetAuraSpell
))
4055 return SPELL_FAILED_CASTER_AURASTATE
;
4058 bool non_caster_target
= target
!= m_caster
&& !IsSpellWithCasterSourceTargetsOnly(m_spellInfo
);
4060 if(non_caster_target
)
4062 // target state requirements (apply to non-self only), to allow cast affects to self like Dirty Deeds
4063 if(m_spellInfo
->TargetAuraState
&& !target
->HasAuraStateForCaster(AuraState(m_spellInfo
->TargetAuraState
),m_caster
->GetGUID()))
4064 return SPELL_FAILED_TARGET_AURASTATE
;
4066 // Not allow casting on flying player
4067 if (target
->isInFlight())
4068 return SPELL_FAILED_BAD_TARGETS
;
4070 if(!m_IsTriggeredSpell
&& VMAP::VMapFactory::checkSpellForLoS(m_spellInfo
->Id
) && !m_caster
->IsWithinLOSInMap(target
))
4071 return SPELL_FAILED_LINE_OF_SIGHT
;
4073 // auto selection spell rank implemented in WorldSession::HandleCastSpellOpcode
4074 // this case can be triggered if rank not found (too low-level target for first rank)
4075 if (m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsPassiveSpell(m_spellInfo
->Id
) && !m_CastItem
)
4077 for(int i
= 0; i
< MAX_EFFECT_INDEX
; ++i
)
4079 // check only spell that apply positive auras
4080 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_APPLY_AURA
&&
4081 IsPositiveEffect(m_spellInfo
->Id
, SpellEffectIndex(i
)) &&
4082 // at not self target
4083 !IsCasterSourceTarget(m_spellInfo
->EffectImplicitTargetA
[i
]) &&
4084 // and target low level
4085 target
->getLevel() + 10 < m_spellInfo
->spellLevel
)
4087 return SPELL_FAILED_LOWLEVEL
;
4092 else if (m_caster
== target
)
4095 if (m_caster
->GetTypeId() == TYPEID_PLAYER
) // Target - is player caster
4097 // Additional check for some spells
4098 // If 0 spell effect empty - client not send target data (need use selection)
4099 // TODO: check it on next client version
4100 if (m_targets
.m_targetMask
== TARGET_FLAG_SELF
&&
4101 m_spellInfo
->EffectImplicitTargetA
[EFFECT_INDEX_1
] == TARGET_CHAIN_DAMAGE
)
4103 if (target
= m_caster
->GetUnit(*m_caster
, ((Player
*)m_caster
)->GetSelection()))
4104 m_targets
.setUnitTarget(target
);
4106 return SPELL_FAILED_BAD_TARGETS
;
4110 // Some special spells with non-caster only mode
4113 if (m_spellInfo
->SpellFamilyName
== SPELLFAMILY_WARLOCK
&&
4114 m_spellInfo
->SpellIconID
== 16)
4115 return SPELL_FAILED_BAD_TARGETS
;
4117 // Focus Magic (main spell)
4118 if (m_spellInfo
->Id
== 54646)
4119 return SPELL_FAILED_BAD_TARGETS
;
4122 // check pet presents
4123 for(int j
= 0; j
< MAX_EFFECT_INDEX
; ++j
)
4125 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_PET
)
4127 target
= m_caster
->GetPet();
4130 if(m_triggeredByAuraSpell
) // not report pet not existence for triggered spells
4131 return SPELL_FAILED_DONT_REPORT
;
4133 return SPELL_FAILED_NO_PET
;
4139 //check creature type
4140 //ignore self casts (including area casts when caster selected as target)
4141 if(non_caster_target
)
4143 if(!CheckTargetCreatureType(target
))
4145 if(target
->GetTypeId() == TYPEID_PLAYER
)
4146 return SPELL_FAILED_TARGET_IS_PLAYER
;
4148 return SPELL_FAILED_BAD_TARGETS
;
4152 if(non_caster_target
)
4155 bool explicit_target_mode
= false;
4156 bool target_hostile
= false;
4157 bool target_hostile_checked
= false;
4158 bool target_friendly
= false;
4159 bool target_friendly_checked
= false;
4160 for(int k
= 0; k
< MAX_EFFECT_INDEX
; ++k
)
4162 if (IsExplicitPositiveTarget(m_spellInfo
->EffectImplicitTargetA
[k
]))
4164 if (!target_hostile_checked
)
4166 target_hostile_checked
= true;
4167 target_hostile
= m_caster
->IsHostileTo(target
);
4171 return SPELL_FAILED_BAD_TARGETS
;
4173 explicit_target_mode
= true;
4175 else if (IsExplicitNegativeTarget(m_spellInfo
->EffectImplicitTargetA
[k
]))
4177 if (!target_friendly_checked
)
4179 target_friendly_checked
= true;
4180 target_friendly
= m_caster
->IsFriendlyTo(target
);
4184 return SPELL_FAILED_BAD_TARGETS
;
4186 explicit_target_mode
= true;
4189 // TODO: this check can be applied and for player to prevent cheating when IsPositiveSpell will return always correct result.
4190 // check target for pet/charmed casts (not self targeted), self targeted cast used for area effects and etc
4191 if (!explicit_target_mode
&& m_caster
->GetTypeId() == TYPEID_UNIT
&& m_caster
->GetCharmerOrOwnerGUID())
4193 // check correctness positive/negative cast target (pet cast real check and cheating check)
4194 if(IsPositiveSpell(m_spellInfo
->Id
))
4196 if (!target_hostile_checked
)
4198 target_hostile_checked
= true;
4199 target_hostile
= m_caster
->IsHostileTo(target
);
4203 return SPELL_FAILED_BAD_TARGETS
;
4207 if (!target_friendly_checked
)
4209 target_friendly_checked
= true;
4210 target_friendly
= m_caster
->IsFriendlyTo(target
);
4214 return SPELL_FAILED_BAD_TARGETS
;
4219 if(IsPositiveSpell(m_spellInfo
->Id
))
4220 if(target
->IsImmunedToSpell(m_spellInfo
))
4221 return SPELL_FAILED_TARGET_AURASTATE
;
4223 //Must be behind the target.
4224 if( m_spellInfo
->AttributesEx2
== 0x100000 && (m_spellInfo
->AttributesEx
& 0x200) == 0x200 && target
->HasInArc(M_PI_F
, m_caster
) )
4226 // Exclusion for Pounce: Facing Limitation was removed in 2.0.1, but it still uses the same, old Ex-Flags
4227 // Exclusion for Mutilate:Facing Limitation was removed in 2.0.1 and 3.0.3, but they still use the same, old Ex-Flags
4228 // Exclusion for Throw: Facing limitation was added in 3.2.x, but that shouldn't be
4229 if ((m_spellInfo
->SpellFamilyName
!= SPELLFAMILY_DRUID
|| (m_spellInfo
->SpellFamilyFlags
!= UI64LIT(0x0000000000020000))) &&
4230 (m_spellInfo
->SpellFamilyName
!= SPELLFAMILY_ROGUE
|| (m_spellInfo
->SpellFamilyFlags
!= UI64LIT(0x0020000000000000))) &&
4231 m_spellInfo
->Id
!= 2764)
4234 return SPELL_FAILED_NOT_BEHIND
;
4238 //Target must be facing you.
4239 if((m_spellInfo
->Attributes
== 0x150010) && !target
->HasInArc(M_PI_F
, m_caster
) )
4242 return SPELL_FAILED_NOT_INFRONT
;
4245 // check if target is in combat
4246 if (non_caster_target
&& (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_NOT_IN_COMBAT_TARGET
) && target
->isInCombat())
4247 return SPELL_FAILED_TARGET_AFFECTING_COMBAT
;
4251 m_caster
->GetZoneAndAreaId(zone
, area
);
4253 SpellCastResult locRes
= sSpellMgr
.GetSpellAllowedInLocationError(m_spellInfo
, m_caster
->GetMapId(), zone
, area
,
4254 m_caster
->GetCharmerOrOwnerPlayerOrPlayerItself());
4255 if (locRes
!= SPELL_CAST_OK
)
4258 // not let players cast spells at mount (and let do it to creatures)
4259 if (m_caster
->IsMounted() && m_caster
->GetTypeId()==TYPEID_PLAYER
&& !m_IsTriggeredSpell
&&
4260 !IsPassiveSpell(m_spellInfo
->Id
) && !(m_spellInfo
->Attributes
& SPELL_ATTR_CASTABLE_WHILE_MOUNTED
))
4262 if (m_caster
->isInFlight())
4263 return SPELL_FAILED_NOT_ON_TAXI
;
4265 return SPELL_FAILED_NOT_MOUNTED
;
4268 // always (except passive spells) check items (focus object can be required for any type casts)
4269 if (!IsPassiveSpell(m_spellInfo
->Id
))
4271 SpellCastResult castResult
= CheckItems();
4272 if(castResult
!= SPELL_CAST_OK
)
4276 // Database based targets from spell_target_script
4277 if (m_UniqueTargetInfo
.empty()) // skip second CheckCast apply (for delayed spells for example)
4279 for(int j
= 0; j
< MAX_EFFECT_INDEX
; ++j
)
4281 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT
||
4282 (m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT
&& m_spellInfo
->EffectImplicitTargetA
[j
] != TARGET_SELF
) ||
4283 m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
4284 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
||
4285 m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_FOCUS_OR_SCRIPTED_GAMEOBJECT
)
4288 SpellScriptTargetBounds bounds
= sSpellMgr
.GetSpellScriptTargetBounds(m_spellInfo
->Id
);
4290 if (bounds
.first
== bounds
.second
)
4291 sLog
.outErrorDb("Spell (ID: %u) has effect EffectImplicitTargetA/EffectImplicitTargetB = TARGET_SCRIPT or TARGET_SCRIPT_COORDINATES, but does not have record in `spell_script_target`", m_spellInfo
->Id
);
4293 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
4294 float range
= GetSpellMaxRange(srange
);
4296 Creature
* creatureScriptTarget
= NULL
;
4297 GameObject
* goScriptTarget
= NULL
;
4299 for(SpellScriptTarget::const_iterator i_spellST
= bounds
.first
; i_spellST
!= bounds
.second
; ++i_spellST
)
4301 switch(i_spellST
->second
.type
)
4303 case SPELL_TARGET_TYPE_GAMEOBJECT
:
4305 GameObject
* p_GameObject
= NULL
;
4307 if (i_spellST
->second
.targetEntry
)
4309 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
4311 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
4313 MaNGOS::NearestGameObjectEntryInObjectRangeCheck
go_check(*m_caster
, i_spellST
->second
.targetEntry
, range
);
4314 MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
> checker(m_caster
, p_GameObject
, go_check
);
4316 TypeContainerVisitor
<MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
>, GridTypeMapContainer
> object_checker(checker
);
4317 cell
.Visit(p
, object_checker
, *m_caster
->GetMap(), *m_caster
, range
);
4321 // remember found target and range, next attempt will find more near target with another entry
4322 creatureScriptTarget
= NULL
;
4323 goScriptTarget
= p_GameObject
;
4324 range
= go_check
.GetLastRange();
4327 else if (focusObject
) // Focus Object
4329 float frange
= m_caster
->GetDistance(focusObject
);
4330 if (range
>= frange
)
4332 creatureScriptTarget
= NULL
;
4333 goScriptTarget
= focusObject
;
4339 case SPELL_TARGET_TYPE_CREATURE
:
4340 case SPELL_TARGET_TYPE_DEAD
:
4343 Creature
*p_Creature
= NULL
;
4345 // check if explicit target is provided and check it up against database valid target entry/state
4346 if (Unit
* pTarget
= m_targets
.getUnitTarget())
4348 if (pTarget
->GetTypeId() == TYPEID_UNIT
&& pTarget
->GetEntry() == i_spellST
->second
.targetEntry
)
4350 if (i_spellST
->second
.type
== SPELL_TARGET_TYPE_DEAD
&& pTarget
->isDead())
4352 if (pTarget
->IsWithinDistInMap(m_caster
, range
))
4353 p_Creature
= (Creature
*)pTarget
;
4355 else if (i_spellST
->second
.type
== SPELL_TARGET_TYPE_CREATURE
&& pTarget
->isAlive())
4357 if (pTarget
->IsWithinDistInMap(m_caster
, range
))
4358 p_Creature
= (Creature
*)pTarget
;
4363 // no target provided or it was not valid, so use closest in range
4366 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
4368 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
4371 MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
u_check(*m_caster
, i_spellST
->second
.targetEntry
, i_spellST
->second
.type
!= SPELL_TARGET_TYPE_DEAD
, range
);
4372 MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
> searcher(m_caster
, p_Creature
, u_check
);
4374 TypeContainerVisitor
<MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
>, GridTypeMapContainer
> grid_creature_searcher(searcher
);
4376 cell
.Visit(p
, grid_creature_searcher
, *m_caster
->GetMap(), *m_caster
, range
);
4378 range
= u_check
.GetLastRange();
4383 creatureScriptTarget
= p_Creature
;
4384 goScriptTarget
= NULL
;
4392 if (creatureScriptTarget
)
4394 // store coordinates for TARGET_SCRIPT_COORDINATES
4395 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
4396 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
4398 m_targets
.setDestination(creatureScriptTarget
->GetPositionX(),creatureScriptTarget
->GetPositionY(),creatureScriptTarget
->GetPositionZ());
4400 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->Effect
[j
] != SPELL_EFFECT_PERSISTENT_AREA_AURA
)
4401 AddUnitTarget(creatureScriptTarget
, SpellEffectIndex(j
));
4403 // store explicit target for TARGET_SCRIPT
4406 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT
||
4407 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT
)
4408 AddUnitTarget(creatureScriptTarget
, SpellEffectIndex(j
));
4411 else if (goScriptTarget
)
4413 // store coordinates for TARGET_SCRIPT_COORDINATES
4414 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
4415 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
4417 m_targets
.setDestination(goScriptTarget
->GetPositionX(),goScriptTarget
->GetPositionY(),goScriptTarget
->GetPositionZ());
4419 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->Effect
[j
] != SPELL_EFFECT_PERSISTENT_AREA_AURA
)
4420 AddGOTarget(goScriptTarget
, SpellEffectIndex(j
));
4422 // store explicit target for TARGET_FOCUS_OR_SCRIPTED_GAMEOBJECT
4425 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_FOCUS_OR_SCRIPTED_GAMEOBJECT
||
4426 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_FOCUS_OR_SCRIPTED_GAMEOBJECT
)
4427 AddGOTarget(goScriptTarget
, SpellEffectIndex(j
));
4430 //Missing DB Entry or targets for this spellEffect.
4433 /* For TARGET_FOCUS_OR_SCRIPTED_GAMEOBJECT makes DB targets optional not required for now
4434 * TODO: Makes more research for this target type
4436 if (m_spellInfo
->EffectImplicitTargetA
[j
] != TARGET_FOCUS_OR_SCRIPTED_GAMEOBJECT
)
4438 // not report target not existence for triggered spells
4439 if (m_triggeredByAuraSpell
|| m_IsTriggeredSpell
)
4440 return SPELL_FAILED_DONT_REPORT
;
4442 return SPELL_FAILED_BAD_TARGETS
;
4449 if(!m_IsTriggeredSpell
)
4451 SpellCastResult castResult
= CheckRange(strict
);
4452 if(castResult
!= SPELL_CAST_OK
)
4457 SpellCastResult castResult
= CheckPower();
4458 if(castResult
!= SPELL_CAST_OK
)
4462 if(!m_IsTriggeredSpell
) // triggered spell not affected by stun/etc
4464 SpellCastResult castResult
= CheckCasterAuras();
4465 if(castResult
!= SPELL_CAST_OK
)
4469 for (int i
= 0; i
< MAX_EFFECT_INDEX
; ++i
)
4471 // for effects of spells that have only one target
4472 switch(m_spellInfo
->Effect
[i
])
4474 case SPELL_EFFECT_INSTAKILL
:
4476 if(m_spellInfo
->Id
== 48743)
4478 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
4479 return SPELL_FAILED_ERROR
;
4481 if (!((Player
*)m_caster
)->GetSelection())
4482 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4483 Pet
* target
= m_caster
->GetMap()->GetPet(((Player
*)m_caster
)->GetSelection());
4486 if (!target
|| target
->isDead())
4487 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4489 if (target
->GetCreatureType() != CREATURE_TYPE_UNDEAD
)
4490 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4492 if (target
->GetOwnerGUID() != m_caster
->GetGUID())
4493 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4495 float dist
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[i
]));
4496 if (!target
->IsWithinDistInMap(m_caster
,dist
))
4497 return SPELL_FAILED_OUT_OF_RANGE
;
4499 // will set in target selection code
4502 case SPELL_EFFECT_DUMMY
:
4504 if(m_spellInfo
->SpellIconID
== 1648) // Execute
4506 if(!m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
4507 return SPELL_FAILED_BAD_TARGETS
;
4509 else if (m_spellInfo
->Id
== 51582) // Rocket Boots Engaged
4511 if(m_caster
->IsInWater())
4512 return SPELL_FAILED_ONLY_ABOVEWATER
;
4514 else if(m_spellInfo
->SpellIconID
== 156) // Holy Shock
4516 // spell different for friends and enemies
4517 // hart version required facing
4518 if(m_targets
.getUnitTarget() && !m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()) && !m_caster
->HasInArc( M_PI_F
, m_targets
.getUnitTarget() ))
4519 return SPELL_FAILED_UNIT_NOT_INFRONT
;
4522 if (m_spellInfo
->SpellFamilyName
== SPELLFAMILY_SHAMAN
&& m_spellInfo
->SpellIconID
== 33)
4525 if (!m_caster
->GetTotemGUID(TOTEM_SLOT_FIRE
))
4526 return SPELL_FAILED_TOTEMS
;
4530 case SPELL_EFFECT_SCHOOL_DAMAGE
:
4533 if(m_spellInfo
->SpellVisual
[0] == 7250)
4535 if (!m_targets
.getUnitTarget())
4536 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4538 if(m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
4539 return SPELL_FAILED_BAD_TARGETS
;
4543 case SPELL_EFFECT_TAMECREATURE
:
4545 // Spell can be triggered, we need to check original caster prior to caster
4546 Unit
* caster
= GetAffectiveCaster();
4547 if (!caster
|| caster
->GetTypeId() != TYPEID_PLAYER
||
4548 !m_targets
.getUnitTarget() ||
4549 m_targets
.getUnitTarget()->GetTypeId() == TYPEID_PLAYER
)
4550 return SPELL_FAILED_BAD_TARGETS
;
4552 Player
* plrCaster
= (Player
*)caster
;
4554 if(plrCaster
->getClass() != CLASS_HUNTER
)
4556 plrCaster
->SendPetTameFailure(PETTAME_UNITSCANTTAME
);
4557 return SPELL_FAILED_DONT_REPORT
;
4560 Creature
* target
= (Creature
*)m_targets
.getUnitTarget();
4562 if(target
->isPet() || target
->isCharmed())
4564 plrCaster
->SendPetTameFailure(PETTAME_CREATUREALREADYOWNED
);
4565 return SPELL_FAILED_DONT_REPORT
;
4568 if (target
->getLevel() > plrCaster
->getLevel())
4570 plrCaster
->SendPetTameFailure(PETTAME_TOOHIGHLEVEL
);
4571 return SPELL_FAILED_DONT_REPORT
;
4574 if (target
->GetCreatureInfo()->IsExotic() && !plrCaster
->CanTameExoticPets())
4576 plrCaster
->SendPetTameFailure(PETTAME_CANTCONTROLEXOTIC
);
4577 return SPELL_FAILED_DONT_REPORT
;
4580 if (!target
->GetCreatureInfo()->isTameable(plrCaster
->CanTameExoticPets()))
4582 plrCaster
->SendPetTameFailure(PETTAME_NOTTAMEABLE
);
4583 return SPELL_FAILED_DONT_REPORT
;
4586 if(plrCaster
->GetPetGUID() || plrCaster
->GetCharmGUID())
4588 plrCaster
->SendPetTameFailure(PETTAME_ANOTHERSUMMONACTIVE
);
4589 return SPELL_FAILED_DONT_REPORT
;
4594 case SPELL_EFFECT_LEARN_SPELL
:
4596 if(m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_PET
)
4599 Pet
* pet
= m_caster
->GetPet();
4602 return SPELL_FAILED_NO_PET
;
4604 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
4606 if(!learn_spellproto
)
4607 return SPELL_FAILED_NOT_KNOWN
;
4609 if(m_spellInfo
->spellLevel
> pet
->getLevel())
4610 return SPELL_FAILED_LOWLEVEL
;
4614 case SPELL_EFFECT_LEARN_PET_SPELL
:
4616 Pet
* pet
= m_caster
->GetPet();
4619 return SPELL_FAILED_NO_PET
;
4621 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
4623 if(!learn_spellproto
)
4624 return SPELL_FAILED_NOT_KNOWN
;
4626 if(m_spellInfo
->spellLevel
> pet
->getLevel())
4627 return SPELL_FAILED_LOWLEVEL
;
4631 case SPELL_EFFECT_APPLY_GLYPH
:
4633 uint32 glyphId
= m_spellInfo
->EffectMiscValue
[i
];
4634 if(GlyphPropertiesEntry
const *gp
= sGlyphPropertiesStore
.LookupEntry(glyphId
))
4635 if(m_caster
->HasAura(gp
->SpellId
))
4636 return SPELL_FAILED_UNIQUE_GLYPH
;
4639 case SPELL_EFFECT_FEED_PET
:
4641 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
4642 return SPELL_FAILED_BAD_TARGETS
;
4644 Item
* foodItem
= m_targets
.getItemTarget();
4646 return SPELL_FAILED_BAD_TARGETS
;
4648 Pet
* pet
= m_caster
->GetPet();
4651 return SPELL_FAILED_NO_PET
;
4653 if(!pet
->HaveInDiet(foodItem
->GetProto()))
4654 return SPELL_FAILED_WRONG_PET_FOOD
;
4656 if(!pet
->GetCurrentFoodBenefitLevel(foodItem
->GetProto()->ItemLevel
))
4657 return SPELL_FAILED_FOOD_LOWLEVEL
;
4659 if(m_caster
->isInCombat() || pet
->isInCombat())
4660 return SPELL_FAILED_AFFECTING_COMBAT
;
4664 case SPELL_EFFECT_POWER_BURN
:
4665 case SPELL_EFFECT_POWER_DRAIN
:
4667 // Can be area effect, Check only for players and not check if target - caster (spell can have multiply drain/burn effects)
4668 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
4669 if(Unit
* target
= m_targets
.getUnitTarget())
4670 if(target
!= m_caster
&& target
->getPowerType() != m_spellInfo
->EffectMiscValue
[i
])
4671 return SPELL_FAILED_BAD_TARGETS
;
4674 case SPELL_EFFECT_CHARGE
:
4676 if (m_caster
->hasUnitState(UNIT_STAT_ROOT
))
4677 return SPELL_FAILED_ROOTED
;
4681 case SPELL_EFFECT_SKINNING
:
4683 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| !m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetTypeId() != TYPEID_UNIT
)
4684 return SPELL_FAILED_BAD_TARGETS
;
4686 if( !(m_targets
.getUnitTarget()->GetUInt32Value(UNIT_FIELD_FLAGS
) & UNIT_FLAG_SKINNABLE
) )
4687 return SPELL_FAILED_TARGET_UNSKINNABLE
;
4689 Creature
* creature
= (Creature
*)m_targets
.getUnitTarget();
4690 if ( creature
->GetCreatureType() != CREATURE_TYPE_CRITTER
&& ( !creature
->lootForBody
|| !creature
->loot
.empty() ) )
4692 return SPELL_FAILED_TARGET_NOT_LOOTED
;
4695 uint32 skill
= creature
->GetCreatureInfo()->GetRequiredLootSkill();
4697 int32 skillValue
= ((Player
*)m_caster
)->GetSkillValue(skill
);
4698 int32 TargetLevel
= m_targets
.getUnitTarget()->getLevel();
4699 int32 ReqValue
= (skillValue
< 100 ? (TargetLevel
-10) * 10 : TargetLevel
* 5);
4700 if (ReqValue
> skillValue
)
4701 return SPELL_FAILED_LOW_CASTLEVEL
;
4703 // chance for fail at orange skinning attempt
4704 if( (m_selfContainer
&& (*m_selfContainer
) == this) &&
4705 skillValue
< sWorld
.GetConfigMaxSkillValue() &&
4706 (ReqValue
< 0 ? 0 : ReqValue
) > irand(skillValue
- 25, skillValue
+ 37) )
4707 return SPELL_FAILED_TRY_AGAIN
;
4711 case SPELL_EFFECT_OPEN_LOCK
:
4713 if( m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT
&&
4714 m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT_ITEM
)
4717 if( m_caster
->GetTypeId() != TYPEID_PLAYER
// only players can open locks, gather etc.
4718 // we need a go target in case of TARGET_GAMEOBJECT
4719 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT
&& !m_targets
.getGOTarget()
4720 // we need a go target, or an openable item target in case of TARGET_GAMEOBJECT_ITEM
4721 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT_ITEM
&& !m_targets
.getGOTarget() &&
4722 (!m_targets
.getItemTarget() || !m_targets
.getItemTarget()->GetProto()->LockID
|| m_targets
.getItemTarget()->GetOwner() != m_caster
) )
4723 return SPELL_FAILED_BAD_TARGETS
;
4725 // In BattleGround players can use only flags and banners
4726 if( ((Player
*)m_caster
)->InBattleGround() &&
4727 !((Player
*)m_caster
)->CanUseBattleGroundObject() )
4728 return SPELL_FAILED_TRY_AGAIN
;
4730 // get the lock entry
4732 if (GameObject
* go
= m_targets
.getGOTarget())
4734 lockId
= go
->GetGOInfo()->GetLockId();
4736 return SPELL_FAILED_BAD_TARGETS
;
4738 else if(Item
* itm
= m_targets
.getItemTarget())
4739 lockId
= itm
->GetProto()->LockID
;
4741 SkillType skillId
= SKILL_NONE
;
4742 int32 reqSkillValue
= 0;
4743 int32 skillValue
= 0;
4745 // check lock compatibility
4746 SpellCastResult res
= CanOpenLock(SpellEffectIndex(i
), lockId
, skillId
, reqSkillValue
, skillValue
);
4747 if(res
!= SPELL_CAST_OK
)
4750 // chance for fail at orange mining/herb/LockPicking gathering attempt
4751 // second check prevent fail at rechecks
4752 if(skillId
!= SKILL_NONE
&& (!m_selfContainer
|| ((*m_selfContainer
) != this)))
4754 bool canFailAtMax
= skillId
!= SKILL_HERBALISM
&& skillId
!= SKILL_MINING
;
4756 // chance for failure in orange gather / lockpick (gathering skill can't fail at maxskill)
4757 if((canFailAtMax
|| skillValue
< sWorld
.GetConfigMaxSkillValue()) && reqSkillValue
> irand(skillValue
- 25, skillValue
+ 37))
4758 return SPELL_FAILED_TRY_AGAIN
;
4762 case SPELL_EFFECT_SUMMON_DEAD_PET
:
4764 Creature
*pet
= m_caster
->GetPet();
4766 return SPELL_FAILED_NO_PET
;
4769 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4773 // This is generic summon effect
4774 case SPELL_EFFECT_SUMMON
:
4776 if(SummonPropertiesEntry
const *summon_prop
= sSummonPropertiesStore
.LookupEntry(m_spellInfo
->EffectMiscValueB
[i
]))
4778 if(summon_prop
->Group
== SUMMON_PROP_GROUP_PETS
)
4780 if(m_caster
->GetPetGUID())
4781 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4783 if(m_caster
->GetCharmGUID())
4784 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4790 case SPELL_EFFECT_SUMMON_PET
:
4792 if(m_caster
->GetPetGUID()) //let warlock do a replacement summon
4795 Pet
* pet
= ((Player
*)m_caster
)->GetPet();
4797 if (m_caster
->GetTypeId() == TYPEID_PLAYER
&& m_caster
->getClass() == CLASS_WARLOCK
)
4799 if (strict
) //starting cast, trigger pet stun (cast by pet so it doesn't attack player)
4800 pet
->CastSpell(pet
, 32752, true, NULL
, NULL
, pet
->GetGUID());
4803 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4806 if(m_caster
->GetCharmGUID())
4807 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4811 case SPELL_EFFECT_SUMMON_PLAYER
:
4813 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
4814 return SPELL_FAILED_BAD_TARGETS
;
4815 if(!((Player
*)m_caster
)->GetSelection())
4816 return SPELL_FAILED_BAD_TARGETS
;
4818 Player
* target
= sObjectMgr
.GetPlayer(((Player
*)m_caster
)->GetSelection());
4819 if( !target
|| ((Player
*)m_caster
) == target
|| !target
->IsInSameRaidWith((Player
*)m_caster
) )
4820 return SPELL_FAILED_BAD_TARGETS
;
4822 // check if our map is dungeon
4823 if( sMapStore
.LookupEntry(m_caster
->GetMapId())->IsDungeon() )
4825 InstanceTemplate
const* instance
= ObjectMgr::GetInstanceTemplate(m_caster
->GetMapId());
4827 return SPELL_FAILED_TARGET_NOT_IN_INSTANCE
;
4828 if ( instance
->levelMin
> target
->getLevel() )
4829 return SPELL_FAILED_LOWLEVEL
;
4830 if ( instance
->levelMax
&& instance
->levelMax
< target
->getLevel() )
4831 return SPELL_FAILED_HIGHLEVEL
;
4835 case SPELL_EFFECT_LEAP
:
4836 case SPELL_EFFECT_TELEPORT_UNITS_FACE_CASTER
:
4838 float dis
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[i
]));
4839 float fx
= m_caster
->GetPositionX() + dis
* cos(m_caster
->GetOrientation());
4840 float fy
= m_caster
->GetPositionY() + dis
* sin(m_caster
->GetOrientation());
4841 // teleport a bit above terrain level to avoid falling below it
4842 float fz
= m_caster
->GetBaseMap()->GetHeight(fx
, fy
, m_caster
->GetPositionZ(), true);
4843 if(fz
<= INVALID_HEIGHT
) // note: this also will prevent use effect in instances without vmaps height enabled
4844 return SPELL_FAILED_TRY_AGAIN
;
4846 float caster_pos_z
= m_caster
->GetPositionZ();
4847 // Control the caster to not climb or drop when +-fz > 8
4848 if(!(fz
<= caster_pos_z
+ 8 && fz
>= caster_pos_z
- 8))
4849 return SPELL_FAILED_TRY_AGAIN
;
4851 // not allow use this effect at battleground until battleground start
4852 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
4853 if(BattleGround
const *bg
= ((Player
*)m_caster
)->GetBattleGround())
4854 if(bg
->GetStatus() != STATUS_IN_PROGRESS
)
4855 return SPELL_FAILED_TRY_AGAIN
;
4858 case SPELL_EFFECT_STEAL_BENEFICIAL_BUFF
:
4860 if (m_targets
.getUnitTarget() == m_caster
)
4861 return SPELL_FAILED_BAD_TARGETS
;
4868 for (int i
= 0; i
< MAX_EFFECT_INDEX
; ++i
)
4870 switch(m_spellInfo
->EffectApplyAuraName
[i
])
4872 case SPELL_AURA_DUMMY
:
4875 switch(m_spellInfo
->Id
)
4878 if(m_caster
->GetTypeId() != TYPEID_PLAYER
|| !((Player
*)m_caster
)->IsInFeralForm())
4879 return SPELL_FAILED_ONLY_SHAPESHIFT
;
4886 case SPELL_AURA_MOD_POSSESS
:
4888 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
4889 return SPELL_FAILED_UNKNOWN
;
4891 if(m_targets
.getUnitTarget() == m_caster
)
4892 return SPELL_FAILED_BAD_TARGETS
;
4894 if(m_caster
->GetPetGUID())
4895 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4897 if(m_caster
->GetCharmGUID())
4898 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4900 if(m_caster
->GetCharmerGUID())
4901 return SPELL_FAILED_CHARMED
;
4903 if(!m_targets
.getUnitTarget())
4904 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4906 if(m_targets
.getUnitTarget()->GetCharmerGUID())
4907 return SPELL_FAILED_CHARMED
;
4909 if(int32(m_targets
.getUnitTarget()->getLevel()) > CalculateDamage(SpellEffectIndex(i
),m_targets
.getUnitTarget()))
4910 return SPELL_FAILED_HIGHLEVEL
;
4914 case SPELL_AURA_MOD_CHARM
:
4916 if(m_targets
.getUnitTarget() == m_caster
)
4917 return SPELL_FAILED_BAD_TARGETS
;
4919 if(m_caster
->GetPetGUID())
4920 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4922 if(m_caster
->GetCharmGUID())
4923 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4925 if(m_caster
->GetCharmerGUID())
4926 return SPELL_FAILED_CHARMED
;
4928 if(!m_targets
.getUnitTarget())
4929 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4931 if(m_targets
.getUnitTarget()->GetCharmerGUID())
4932 return SPELL_FAILED_CHARMED
;
4934 if(int32(m_targets
.getUnitTarget()->getLevel()) > CalculateDamage(SpellEffectIndex(i
),m_targets
.getUnitTarget()))
4935 return SPELL_FAILED_HIGHLEVEL
;
4939 case SPELL_AURA_MOD_POSSESS_PET
:
4941 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
4942 return SPELL_FAILED_UNKNOWN
;
4944 if(m_caster
->GetCharmGUID())
4945 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4947 if(m_caster
->GetCharmerGUID())
4948 return SPELL_FAILED_CHARMED
;
4950 Pet
* pet
= m_caster
->GetPet();
4952 return SPELL_FAILED_NO_PET
;
4954 if(pet
->GetCharmerGUID())
4955 return SPELL_FAILED_CHARMED
;
4959 case SPELL_AURA_MOUNTED
:
4961 if (m_caster
->IsInWater())
4962 return SPELL_FAILED_ONLY_ABOVEWATER
;
4964 if (m_caster
->GetTypeId() == TYPEID_PLAYER
&& ((Player
*)m_caster
)->GetTransport())
4965 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4967 // Ignore map check if spell have AreaId. AreaId already checked and this prevent special mount spells
4968 if (m_caster
->GetTypeId() == TYPEID_PLAYER
&& !sMapStore
.LookupEntry(m_caster
->GetMapId())->IsMountAllowed() && !m_IsTriggeredSpell
&& !m_spellInfo
->AreaGroupId
)
4969 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4971 ShapeshiftForm form
= m_caster
->m_form
;
4972 if( form
== FORM_CAT
|| form
== FORM_TREE
|| form
== FORM_TRAVEL
||
4973 form
== FORM_AQUA
|| form
== FORM_BEAR
|| form
== FORM_DIREBEAR
||
4974 form
== FORM_CREATUREBEAR
|| form
== FORM_GHOSTWOLF
|| form
== FORM_FLIGHT
||
4975 form
== FORM_FLIGHT_EPIC
|| form
== FORM_MOONKIN
|| form
== FORM_METAMORPHOSIS
)
4976 return SPELL_FAILED_NOT_SHAPESHIFT
;
4980 case SPELL_AURA_RANGED_ATTACK_POWER_ATTACKER_BONUS
:
4982 if(!m_targets
.getUnitTarget())
4983 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4985 // can be casted at non-friendly unit or own pet/charm
4986 if(m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()))
4987 return SPELL_FAILED_TARGET_FRIENDLY
;
4991 case SPELL_AURA_FLY
:
4992 case SPELL_AURA_MOD_INCREASE_FLIGHT_SPEED
:
4994 // not allow cast fly spells if not have req. skills (all spells is self target)
4995 // allow always ghost flight spells
4996 if (m_caster
->GetTypeId() == TYPEID_PLAYER
&& m_caster
->isAlive())
4998 if (!((Player
*)m_caster
)->IsKnowHowFlyIn(m_caster
->GetMapId(),zone
))
4999 return m_IsTriggeredSpell
? SPELL_FAILED_DONT_REPORT
: SPELL_FAILED_NOT_HERE
;
5003 case SPELL_AURA_PERIODIC_MANA_LEECH
:
5005 if (!m_targets
.getUnitTarget())
5006 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
5008 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| m_CastItem
)
5011 if(m_targets
.getUnitTarget()->getPowerType() != POWER_MANA
)
5012 return SPELL_FAILED_BAD_TARGETS
;
5022 return SPELL_CAST_OK
;
5025 SpellCastResult
Spell::CheckPetCast(Unit
* target
)
5027 if(!m_caster
->isAlive())
5028 return SPELL_FAILED_CASTER_DEAD
;
5030 if(m_caster
->IsNonMeleeSpellCasted(false)) //prevent spellcast interruption by another spellcast
5031 return SPELL_FAILED_SPELL_IN_PROGRESS
;
5032 if(m_caster
->isInCombat() && IsNonCombatSpell(m_spellInfo
))
5033 return SPELL_FAILED_AFFECTING_COMBAT
;
5035 if(m_caster
->GetTypeId()==TYPEID_UNIT
&& (((Creature
*)m_caster
)->isPet() || m_caster
->isCharmed()))
5037 //dead owner (pets still alive when owners ressed?)
5038 if(m_caster
->GetCharmerOrOwner() && !m_caster
->GetCharmerOrOwner()->isAlive())
5039 return SPELL_FAILED_CASTER_DEAD
;
5041 if(!target
&& m_targets
.getUnitTarget())
5042 target
= m_targets
.getUnitTarget();
5045 for(int i
= 0; i
< MAX_EFFECT_INDEX
; ++i
)
5047 if (m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_CHAIN_DAMAGE
||
5048 m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SINGLE_FRIEND
||
5049 m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SINGLE_FRIEND_2
||
5050 m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_DUELVSPLAYER
||
5051 m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SINGLE_PARTY
||
5052 m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_CURRENT_ENEMY_COORDINATES
)
5056 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
5061 m_targets
.setUnitTarget(target
);
5063 Unit
* _target
= m_targets
.getUnitTarget();
5065 if(_target
) //for target dead/target not valid
5067 if(!_target
->isAlive())
5068 return SPELL_FAILED_BAD_TARGETS
;
5070 if(IsPositiveSpell(m_spellInfo
->Id
))
5072 if(m_caster
->IsHostileTo(_target
))
5073 return SPELL_FAILED_BAD_TARGETS
;
5077 bool duelvsplayertar
= false;
5078 for(int j
= 0; j
< MAX_EFFECT_INDEX
; ++j
)
5080 //TARGET_DUELVSPLAYER is positive AND negative
5081 duelvsplayertar
|= (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_DUELVSPLAYER
);
5083 if(m_caster
->IsFriendlyTo(target
) && !duelvsplayertar
)
5085 return SPELL_FAILED_BAD_TARGETS
;
5090 if(((Creature
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
5091 return SPELL_FAILED_NOT_READY
;
5094 return CheckCast(true);
5097 SpellCastResult
Spell::CheckCasterAuras() const
5099 // Flag drop spells totally immuned to caster auras
5100 // FIXME: find more nice check for all totally immuned spells
5101 // AttributesEx3 & 0x10000000?
5102 if(m_spellInfo
->Id
== 23336 || m_spellInfo
->Id
== 23334 || m_spellInfo
->Id
== 34991)
5103 return SPELL_CAST_OK
;
5105 uint8 school_immune
= 0;
5106 uint32 mechanic_immune
= 0;
5107 uint32 dispel_immune
= 0;
5109 // Check if the spell grants school or mechanic immunity.
5110 // We use bitmasks so the loop is done only once and not on every aura check below.
5111 if ( m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DISPEL_AURAS_ON_IMMUNITY
)
5113 for(int i
= 0; i
< MAX_EFFECT_INDEX
; ++i
)
5115 if (m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_SCHOOL_IMMUNITY
)
5116 school_immune
|= uint32(m_spellInfo
->EffectMiscValue
[i
]);
5117 else if (m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_MECHANIC_IMMUNITY
)
5118 mechanic_immune
|= 1 << uint32(m_spellInfo
->EffectMiscValue
[i
]-1);
5119 else if (m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_MECHANIC_IMMUNITY_MASK
)
5120 mechanic_immune
|= uint32(m_spellInfo
->EffectMiscValue
[i
]);
5121 else if (m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_DISPEL_IMMUNITY
)
5122 dispel_immune
|= GetDispellMask(DispelType(m_spellInfo
->EffectMiscValue
[i
]));
5124 // immune movement impairment and loss of control
5125 if (m_spellInfo
->Id
== 42292)
5126 mechanic_immune
= IMMUNE_TO_MOVEMENT_IMPAIRMENT_AND_LOSS_CONTROL_MASK
;
5129 // Check whether the cast should be prevented by any state you might have.
5130 SpellCastResult prevented_reason
= SPELL_CAST_OK
;
5131 // Have to check if there is a stun aura. Otherwise will have problems with ghost aura apply while logging out
5132 uint32 unitflag
= m_caster
->GetUInt32Value(UNIT_FIELD_FLAGS
); // Get unit state
5133 if (unitflag
& UNIT_FLAG_STUNNED
&& !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
))
5134 prevented_reason
= SPELL_FAILED_STUNNED
;
5135 else if (unitflag
& UNIT_FLAG_CONFUSED
&& !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
5136 prevented_reason
= SPELL_FAILED_CONFUSED
;
5137 else if (unitflag
& UNIT_FLAG_FLEEING
&& !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
5138 prevented_reason
= SPELL_FAILED_FLEEING
;
5139 else if (unitflag
& UNIT_FLAG_SILENCED
&& m_spellInfo
->PreventionType
== SPELL_PREVENTION_TYPE_SILENCE
)
5140 prevented_reason
= SPELL_FAILED_SILENCED
;
5141 else if (unitflag
& UNIT_FLAG_PACIFIED
&& m_spellInfo
->PreventionType
== SPELL_PREVENTION_TYPE_PACIFY
)
5142 prevented_reason
= SPELL_FAILED_PACIFIED
;
5143 else if(m_caster
->HasAuraType(SPELL_AURA_ALLOW_ONLY_ABILITY
))
5145 Unit::AuraList
const& casingLimit
= m_caster
->GetAurasByType(SPELL_AURA_ALLOW_ONLY_ABILITY
);
5146 for(Unit::AuraList::const_iterator itr
= casingLimit
.begin(); itr
!= casingLimit
.end(); ++itr
)
5148 if(!(*itr
)->isAffectedOnSpell(m_spellInfo
))
5150 prevented_reason
= SPELL_FAILED_CASTER_AURASTATE
;
5156 // Attr must make flag drop spell totally immune from all effects
5157 if (prevented_reason
!= SPELL_CAST_OK
)
5159 if (school_immune
|| mechanic_immune
|| dispel_immune
)
5161 //Checking auras is needed now, because you are prevented by some state but the spell grants immunity.
5162 Unit::AuraMap
const& auras
= m_caster
->GetAuras();
5163 for(Unit::AuraMap::const_iterator itr
= auras
.begin(); itr
!= auras
.end(); ++itr
)
5167 if (GetSpellMechanicMask(itr
->second
->GetSpellProto(), itr
->second
->GetEffIndex()) & mechanic_immune
)
5169 if (GetSpellSchoolMask(itr
->second
->GetSpellProto()) & school_immune
)
5171 if ((1<<(itr
->second
->GetSpellProto()->Dispel
)) & dispel_immune
)
5174 // Make a second check for spell failed so the right SPELL_FAILED message is returned.
5175 // That is needed when your casting is prevented by multiple states and you are only immune to some of them.
5176 switch(itr
->second
->GetModifier()->m_auraname
)
5178 case SPELL_AURA_MOD_STUN
:
5179 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
))
5180 return SPELL_FAILED_STUNNED
;
5182 case SPELL_AURA_MOD_CONFUSE
:
5183 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
5184 return SPELL_FAILED_CONFUSED
;
5186 case SPELL_AURA_MOD_FEAR
:
5187 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
5188 return SPELL_FAILED_FLEEING
;
5190 case SPELL_AURA_MOD_SILENCE
:
5191 case SPELL_AURA_MOD_PACIFY
:
5192 case SPELL_AURA_MOD_PACIFY_SILENCE
:
5193 if( m_spellInfo
->PreventionType
== SPELL_PREVENTION_TYPE_PACIFY
)
5194 return SPELL_FAILED_PACIFIED
;
5195 else if ( m_spellInfo
->PreventionType
== SPELL_PREVENTION_TYPE_SILENCE
)
5196 return SPELL_FAILED_SILENCED
;
5203 // You are prevented from casting and the spell casted does not grant immunity. Return a failed error.
5205 return prevented_reason
;
5207 return SPELL_CAST_OK
;
5210 bool Spell::CanAutoCast(Unit
* target
)
5212 ObjectGuid targetguid
= target
->GetObjectGuid();
5214 for(int j
= 0; j
< MAX_EFFECT_INDEX
; ++j
)
5216 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_APPLY_AURA
)
5218 if( m_spellInfo
->StackAmount
<= 1)
5220 if( target
->HasAura(m_spellInfo
->Id
, SpellEffectIndex(j
)) )
5225 if(Aura
* aura
= target
->GetAura(m_spellInfo
->Id
, SpellEffectIndex(j
)))
5226 if(aura
->GetStackAmount() >= m_spellInfo
->StackAmount
)
5230 else if ( IsAreaAuraEffect( m_spellInfo
->Effect
[j
] ))
5232 if( target
->HasAura(m_spellInfo
->Id
, SpellEffectIndex(j
)) )
5237 SpellCastResult result
= CheckPetCast(target
);
5239 if(result
== SPELL_CAST_OK
|| result
== SPELL_FAILED_UNIT_NOT_INFRONT
)
5242 //check if among target units, our WANTED target is as well (->only self cast spells return false)
5243 for(std::list
<TargetInfo
>::const_iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end(); ++ihit
)
5244 if( ihit
->targetGUID
== targetguid
)
5247 return false; //target invalid
5250 SpellCastResult
Spell::CheckRange(bool strict
)
5254 // self cast doesn't need range checking -- also for Starshards fix
5255 if (m_spellInfo
->rangeIndex
== 1)
5256 return SPELL_CAST_OK
;
5258 if (strict
) //add radius of caster
5260 else //add radius of caster and ~5 yds "give"
5263 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
5264 Unit
*target
= m_targets
.getUnitTarget();
5265 bool friendly
= target
? target
->IsFriendlyTo(m_caster
) : false;
5266 float max_range
= GetSpellMaxRange(srange
, friendly
) + range_mod
;
5267 float min_range
= GetSpellMinRange(srange
, friendly
);
5269 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
5270 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_RANGE
, max_range
, this);
5272 if(target
&& target
!= m_caster
)
5274 // distance from target in checks
5275 float dist
= m_caster
->GetCombatDistance(target
);
5277 if(dist
> max_range
)
5278 return SPELL_FAILED_OUT_OF_RANGE
;
5279 if(min_range
&& dist
< min_range
)
5280 return SPELL_FAILED_TOO_CLOSE
;
5281 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&&
5282 (m_spellInfo
->FacingCasterFlags
& SPELL_FACING_FLAG_INFRONT
) && !m_caster
->HasInArc( M_PI_F
, target
) )
5283 return SPELL_FAILED_UNIT_NOT_INFRONT
;
5286 if(m_targets
.m_targetMask
== TARGET_FLAG_DEST_LOCATION
&& m_targets
.m_destX
!= 0 && m_targets
.m_destY
!= 0 && m_targets
.m_destZ
!= 0)
5288 if(!m_caster
->IsWithinDist3d(m_targets
.m_destX
, m_targets
.m_destY
, m_targets
.m_destZ
, max_range
))
5289 return SPELL_FAILED_OUT_OF_RANGE
;
5290 if(min_range
&& m_caster
->IsWithinDist3d(m_targets
.m_destX
, m_targets
.m_destY
, m_targets
.m_destZ
, min_range
))
5291 return SPELL_FAILED_TOO_CLOSE
;
5294 return SPELL_CAST_OK
;
5297 int32
Spell::CalculatePowerCost()
5299 // item cast not used power
5303 // Spell drain all exist power on cast (Only paladin lay of Hands)
5304 if (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DRAIN_ALL_POWER
)
5306 // If power type - health drain all
5307 if (m_spellInfo
->powerType
== POWER_HEALTH
)
5308 return m_caster
->GetHealth();
5309 // Else drain all power
5310 if (m_spellInfo
->powerType
< MAX_POWERS
)
5311 return m_caster
->GetPower(Powers(m_spellInfo
->powerType
));
5312 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
5317 int32 powerCost
= m_spellInfo
->manaCost
;
5318 // PCT cost from total amount
5319 if (m_spellInfo
->ManaCostPercentage
)
5321 switch (m_spellInfo
->powerType
)
5323 // health as power used
5325 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateHealth() / 100;
5328 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateMana() / 100;
5333 case POWER_HAPPINESS
:
5334 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetMaxPower(Powers(m_spellInfo
->powerType
)) / 100;
5337 case POWER_RUNIC_POWER
:
5338 sLog
.outDebug("Spell::CalculateManaCost: Not implemented yet!");
5341 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
5345 SpellSchools school
= GetFirstSchoolInMask(m_spellSchoolMask
);
5346 // Flat mod from caster auras by spell school
5347 powerCost
+= m_caster
->GetInt32Value(UNIT_FIELD_POWER_COST_MODIFIER
+ school
);
5348 // Shiv - costs 20 + weaponSpeed*10 energy (apply only to non-triggered spell with energy cost)
5349 if ( m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_SPELL_VS_EXTEND_COST
)
5350 powerCost
+= m_caster
->GetAttackTime(OFF_ATTACK
) / 100;
5351 // Apply cost mod by spell
5352 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
5353 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COST
, powerCost
, this);
5355 if(m_spellInfo
->Attributes
& SPELL_ATTR_LEVEL_DAMAGE_CALCULATION
)
5356 powerCost
= int32(powerCost
/ (1.117f
* m_spellInfo
->spellLevel
/ m_caster
->getLevel() -0.1327f
));
5358 // PCT mod from user auras by school
5359 powerCost
= int32(powerCost
* (1.0f
+ m_caster
->GetFloatValue(UNIT_FIELD_POWER_COST_MULTIPLIER
+ school
)));
5365 SpellCastResult
Spell::CheckPower()
5367 // item cast not used power
5369 return SPELL_CAST_OK
;
5371 // Do precise power regen on spell cast
5372 if (m_powerCost
> 0 && m_caster
->GetTypeId() == TYPEID_PLAYER
)
5374 Player
* playerCaster
= (Player
*)m_caster
;
5375 uint32 diff
= REGEN_TIME_FULL
- m_caster
->GetRegenTimer();
5376 if (diff
>= REGEN_TIME_PRECISE
)
5377 playerCaster
->RegenerateAll(diff
);
5380 // health as power used - need check health amount
5381 if(m_spellInfo
->powerType
== POWER_HEALTH
)
5383 if((int32
)m_caster
->GetHealth() <= m_powerCost
)
5384 return SPELL_FAILED_CASTER_AURASTATE
;
5385 return SPELL_CAST_OK
;
5387 // Check valid power type
5388 if( m_spellInfo
->powerType
>= MAX_POWERS
)
5390 sLog
.outError("Spell::CheckMana: Unknown power type '%d'", m_spellInfo
->powerType
);
5391 return SPELL_FAILED_UNKNOWN
;
5394 //check rune cost only if a spell has PowerType == POWER_RUNE
5395 if(m_spellInfo
->powerType
== POWER_RUNE
)
5397 SpellCastResult failReason
= CheckOrTakeRunePower(false);
5398 if(failReason
!= SPELL_CAST_OK
)
5402 // Check power amount
5403 Powers powerType
= Powers(m_spellInfo
->powerType
);
5404 if((int32
)m_caster
->GetPower(powerType
) < m_powerCost
)
5405 return SPELL_FAILED_NO_POWER
;
5407 return SPELL_CAST_OK
;
5410 bool Spell::IgnoreItemRequirements() const
5412 if (m_IsTriggeredSpell
)
5415 /// Check if it's an enchant scroll. These have no required reagents even though their spell does.
5416 if (m_CastItem
&& m_CastItem
->HasFlag(ITEM_FIELD_FLAGS
, ITEM_FLAGS_ENCHANT_SCROLL
))
5422 SpellCastResult
Spell::CheckItems()
5424 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
5425 return SPELL_CAST_OK
;
5427 Player
* p_caster
= (Player
*)m_caster
;
5432 uint32 itemid
= m_CastItem
->GetEntry();
5433 if( !p_caster
->HasItemCount(itemid
, 1) )
5434 return SPELL_FAILED_ITEM_NOT_FOUND
;
5436 ItemPrototype
const *proto
= m_CastItem
->GetProto();
5438 return SPELL_FAILED_ITEM_NOT_FOUND
;
5440 for (int i
= 0; i
< 5; ++i
)
5441 if (proto
->Spells
[i
].SpellCharges
)
5442 if(m_CastItem
->GetSpellCharges(i
) == 0)
5443 return SPELL_FAILED_NO_CHARGES_REMAIN
;
5445 // consumable cast item checks
5446 if (proto
->Class
== ITEM_CLASS_CONSUMABLE
&& m_targets
.getUnitTarget())
5448 // such items should only fail if there is no suitable effect at all - see Rejuvenation Potions for example
5449 SpellCastResult failReason
= SPELL_CAST_OK
;
5450 for (int i
= 0; i
< MAX_EFFECT_INDEX
; ++i
)
5452 // skip check, pet not required like checks, and for TARGET_PET m_targets.getUnitTarget() is not the real target but the caster
5453 if (m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_PET
)
5456 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_HEAL
)
5458 if (m_targets
.getUnitTarget()->GetHealth() == m_targets
.getUnitTarget()->GetMaxHealth())
5460 failReason
= SPELL_FAILED_ALREADY_AT_FULL_HEALTH
;
5465 failReason
= SPELL_CAST_OK
;
5470 // Mana Potion, Rage Potion, Thistle Tea(Rogue), ...
5471 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_ENERGIZE
)
5473 if(m_spellInfo
->EffectMiscValue
[i
] < 0 || m_spellInfo
->EffectMiscValue
[i
] >= MAX_POWERS
)
5475 failReason
= SPELL_FAILED_ALREADY_AT_FULL_POWER
;
5479 Powers power
= Powers(m_spellInfo
->EffectMiscValue
[i
]);
5480 if (m_targets
.getUnitTarget()->GetPower(power
) == m_targets
.getUnitTarget()->GetMaxPower(power
))
5482 failReason
= SPELL_FAILED_ALREADY_AT_FULL_POWER
;
5487 failReason
= SPELL_CAST_OK
;
5492 if (failReason
!= SPELL_CAST_OK
)
5497 // check target item
5498 if(m_targets
.getItemTargetGUID())
5500 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
5501 return SPELL_FAILED_BAD_TARGETS
;
5503 if(!m_targets
.getItemTarget())
5504 return SPELL_FAILED_ITEM_GONE
;
5506 if(!m_targets
.getItemTarget()->IsFitToSpellRequirements(m_spellInfo
))
5507 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
5509 // if not item target then required item must be equipped
5512 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& !((Player
*)m_caster
)->HasItemFitToSpellReqirements(m_spellInfo
))
5513 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
5516 // check spell focus object
5517 if(m_spellInfo
->RequiresSpellFocus
)
5519 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
5521 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
5523 GameObject
* ok
= NULL
;
5524 MaNGOS::GameObjectFocusCheck
go_check(m_caster
,m_spellInfo
->RequiresSpellFocus
);
5525 MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
> checker(m_caster
, ok
, go_check
);
5527 TypeContainerVisitor
<MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
>, GridTypeMapContainer
> object_checker(checker
);
5528 Map
& map
= *m_caster
->GetMap();
5529 cell
.Visit(p
, object_checker
, map
, *m_caster
, map
.GetVisibilityDistance());
5532 return SPELL_FAILED_REQUIRES_SPELL_FOCUS
;
5534 focusObject
= ok
; // game object found in range
5537 // check reagents (ignore triggered spells with reagents processed by original spell) and special reagent ignore case.
5538 if (!IgnoreItemRequirements())
5540 if (!p_caster
->CanNoReagentCast(m_spellInfo
))
5542 for(uint32 i
= 0; i
< 8; ++i
)
5544 if(m_spellInfo
->Reagent
[i
] <= 0)
5547 uint32 itemid
= m_spellInfo
->Reagent
[i
];
5548 uint32 itemcount
= m_spellInfo
->ReagentCount
[i
];
5550 // if CastItem is also spell reagent
5551 if (m_CastItem
&& m_CastItem
->GetEntry() == itemid
)
5553 ItemPrototype
const *proto
= m_CastItem
->GetProto();
5555 return SPELL_FAILED_REAGENTS
;
5556 for(int s
= 0; s
< MAX_ITEM_PROTO_SPELLS
; ++s
)
5558 // CastItem will be used up and does not count as reagent
5559 int32 charges
= m_CastItem
->GetSpellCharges(s
);
5560 if (proto
->Spells
[s
].SpellCharges
< 0 && !(proto
->ExtraFlags
& ITEM_EXTRA_NON_CONSUMABLE
) && abs(charges
) < 2)
5568 if (!p_caster
->HasItemCount(itemid
, itemcount
))
5569 return SPELL_FAILED_REAGENTS
;
5573 // check totem-item requirements (items presence in inventory)
5575 for(int i
= 0; i
< 2 ; ++i
)
5577 if (m_spellInfo
->Totem
[i
] != 0)
5579 if (p_caster
->HasItemCount(m_spellInfo
->Totem
[i
], 1))
5590 return SPELL_FAILED_TOTEMS
;
5592 // Check items for TotemCategory (items presence in inventory)
5593 uint32 TotemCategory
= 2;
5594 for(int i
= 0; i
< 2; ++i
)
5596 if (m_spellInfo
->TotemCategory
[i
] != 0)
5598 if (p_caster
->HasItemTotemCategory(m_spellInfo
->TotemCategory
[i
]))
5608 if (TotemCategory
!= 0)
5609 return SPELL_FAILED_TOTEM_CATEGORY
;
5612 // special checks for spell effects
5613 for(int i
= 0; i
< MAX_EFFECT_INDEX
; ++i
)
5615 switch (m_spellInfo
->Effect
[i
])
5617 case SPELL_EFFECT_CREATE_ITEM
:
5619 if (!m_IsTriggeredSpell
&& m_spellInfo
->EffectItemType
[i
])
5621 // Conjure Mana Gem (skip same or low level ranks for later recharge)
5622 if (i
== EFFECT_INDEX_0
&& m_spellInfo
->Effect
[EFFECT_INDEX_1
] == SPELL_EFFECT_DUMMY
)
5624 if (ItemPrototype
const* itemProto
= ObjectMgr::GetItemPrototype(m_spellInfo
->EffectItemType
[i
]))
5626 if (Item
* item
= p_caster
->GetItemByLimitedCategory(itemProto
->ItemLimitCategory
))
5628 if (item
->GetProto()->ItemLevel
<= itemProto
->ItemLevel
)
5630 if (item
->HasMaxCharges())
5631 return SPELL_FAILED_ITEM_AT_MAX_CHARGES
;
5633 // will recharge in next effect
5640 ItemPosCountVec dest
;
5641 uint8 msg
= p_caster
->CanStoreNewItem(NULL_BAG
, NULL_SLOT
, dest
, m_spellInfo
->EffectItemType
[i
], 1 );
5642 if (msg
!= EQUIP_ERR_OK
)
5644 p_caster
->SendEquipError( msg
, NULL
, NULL
, m_spellInfo
->EffectItemType
[i
] );
5645 return SPELL_FAILED_DONT_REPORT
;
5650 case SPELL_EFFECT_RESTORE_ITEM_CHARGES
:
5652 if (Item
* item
= p_caster
->GetItemByEntry(m_spellInfo
->EffectItemType
[i
]))
5653 if (item
->HasMaxCharges())
5654 return SPELL_FAILED_ITEM_AT_MAX_CHARGES
;
5658 case SPELL_EFFECT_ENCHANT_ITEM
:
5659 case SPELL_EFFECT_ENCHANT_ITEM_PRISMATIC
:
5661 Item
* targetItem
= m_targets
.getItemTarget();
5663 return SPELL_FAILED_ITEM_NOT_FOUND
;
5665 if( targetItem
->GetProto()->ItemLevel
< m_spellInfo
->baseLevel
)
5666 return SPELL_FAILED_LOWLEVEL
;
5667 // Not allow enchant in trade slot for some enchant type
5668 if( targetItem
->GetOwner() != m_caster
)
5670 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
5671 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
5673 return SPELL_FAILED_ERROR
;
5674 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
5675 return SPELL_FAILED_NOT_TRADEABLE
;
5679 case SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY
:
5681 Item
*item
= m_targets
.getItemTarget();
5683 return SPELL_FAILED_ITEM_NOT_FOUND
;
5684 // Not allow enchant in trade slot for some enchant type
5685 if( item
->GetOwner() != m_caster
)
5687 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
5688 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
5690 return SPELL_FAILED_ERROR
;
5691 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
5692 return SPELL_FAILED_NOT_TRADEABLE
;
5696 case SPELL_EFFECT_ENCHANT_HELD_ITEM
:
5697 // check item existence in effect code (not output errors at offhand hold item effect to main hand for example
5699 case SPELL_EFFECT_DISENCHANT
:
5701 if(!m_targets
.getItemTarget())
5702 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
5704 // prevent disenchanting in trade slot
5705 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
5706 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
5708 ItemPrototype
const* itemProto
= m_targets
.getItemTarget()->GetProto();
5710 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
5712 uint32 item_quality
= itemProto
->Quality
;
5713 // 2.0.x addon: Check player enchanting level against the item disenchanting requirements
5714 uint32 item_disenchantskilllevel
= itemProto
->RequiredDisenchantSkill
;
5715 if (item_disenchantskilllevel
== uint32(-1))
5716 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
5717 if (item_disenchantskilllevel
> p_caster
->GetSkillValue(SKILL_ENCHANTING
))
5718 return SPELL_FAILED_LOW_CASTLEVEL
;
5719 if(item_quality
> 4 || item_quality
< 2)
5720 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
5721 if(itemProto
->Class
!= ITEM_CLASS_WEAPON
&& itemProto
->Class
!= ITEM_CLASS_ARMOR
)
5722 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
5723 if (!itemProto
->DisenchantID
)
5724 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
5727 case SPELL_EFFECT_PROSPECTING
:
5729 if(!m_targets
.getItemTarget())
5730 return SPELL_FAILED_CANT_BE_PROSPECTED
;
5731 // ensure item is a prospectable ore
5732 if(!(m_targets
.getItemTarget()->GetProto()->BagFamily
& BAG_FAMILY_MASK_MINING_SUPP
) || m_targets
.getItemTarget()->GetProto()->Class
!= ITEM_CLASS_TRADE_GOODS
)
5733 return SPELL_FAILED_CANT_BE_PROSPECTED
;
5734 // prevent prospecting in trade slot
5735 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
5736 return SPELL_FAILED_CANT_BE_PROSPECTED
;
5737 // Check for enough skill in jewelcrafting
5738 uint32 item_prospectingskilllevel
= m_targets
.getItemTarget()->GetProto()->RequiredSkillRank
;
5739 if(item_prospectingskilllevel
>p_caster
->GetSkillValue(SKILL_JEWELCRAFTING
))
5740 return SPELL_FAILED_LOW_CASTLEVEL
;
5741 // make sure the player has the required ores in inventory
5742 if(m_targets
.getItemTarget()->GetCount() < 5)
5743 return SPELL_FAILED_NEED_MORE_ITEMS
;
5745 if(!LootTemplates_Prospecting
.HaveLootFor(m_targets
.getItemTargetEntry()))
5746 return SPELL_FAILED_CANT_BE_PROSPECTED
;
5750 case SPELL_EFFECT_MILLING
:
5752 if(!m_targets
.getItemTarget())
5753 return SPELL_FAILED_CANT_BE_MILLED
;
5754 // ensure item is a millable herb
5755 if(!(m_targets
.getItemTarget()->GetProto()->BagFamily
& BAG_FAMILY_MASK_HERBS
) || m_targets
.getItemTarget()->GetProto()->Class
!= ITEM_CLASS_TRADE_GOODS
)
5756 return SPELL_FAILED_CANT_BE_MILLED
;
5757 // prevent milling in trade slot
5758 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
5759 return SPELL_FAILED_CANT_BE_MILLED
;
5760 // Check for enough skill in inscription
5761 uint32 item_millingskilllevel
= m_targets
.getItemTarget()->GetProto()->RequiredSkillRank
;
5762 if(item_millingskilllevel
>p_caster
->GetSkillValue(SKILL_INSCRIPTION
))
5763 return SPELL_FAILED_LOW_CASTLEVEL
;
5764 // make sure the player has the required herbs in inventory
5765 if(m_targets
.getItemTarget()->GetCount() < 5)
5766 return SPELL_FAILED_NEED_MORE_ITEMS
;
5768 if(!LootTemplates_Milling
.HaveLootFor(m_targets
.getItemTargetEntry()))
5769 return SPELL_FAILED_CANT_BE_MILLED
;
5773 case SPELL_EFFECT_WEAPON_DAMAGE
:
5774 case SPELL_EFFECT_WEAPON_DAMAGE_NOSCHOOL
:
5776 if(m_caster
->GetTypeId() != TYPEID_PLAYER
) return SPELL_FAILED_TARGET_NOT_PLAYER
;
5777 if( m_attackType
!= RANGED_ATTACK
)
5779 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack(m_attackType
,true,false);
5781 return SPELL_FAILED_EQUIPPED_ITEM
;
5783 switch(pItem
->GetProto()->SubClass
)
5785 case ITEM_SUBCLASS_WEAPON_THROWN
:
5787 uint32 ammo
= pItem
->GetEntry();
5788 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
5789 return SPELL_FAILED_NO_AMMO
;
5791 case ITEM_SUBCLASS_WEAPON_GUN
:
5792 case ITEM_SUBCLASS_WEAPON_BOW
:
5793 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
5795 uint32 ammo
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
5799 if(m_caster
->GetDummyAura(46699))
5800 break; // skip other checks
5802 return SPELL_FAILED_NO_AMMO
;
5805 ItemPrototype
const *ammoProto
= ObjectMgr::GetItemPrototype( ammo
);
5807 return SPELL_FAILED_NO_AMMO
;
5809 if(ammoProto
->Class
!= ITEM_CLASS_PROJECTILE
)
5810 return SPELL_FAILED_NO_AMMO
;
5812 // check ammo ws. weapon compatibility
5813 switch(pItem
->GetProto()->SubClass
)
5815 case ITEM_SUBCLASS_WEAPON_BOW
:
5816 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
5817 if(ammoProto
->SubClass
!= ITEM_SUBCLASS_ARROW
)
5818 return SPELL_FAILED_NO_AMMO
;
5820 case ITEM_SUBCLASS_WEAPON_GUN
:
5821 if(ammoProto
->SubClass
!= ITEM_SUBCLASS_BULLET
)
5822 return SPELL_FAILED_NO_AMMO
;
5825 return SPELL_FAILED_NO_AMMO
;
5828 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
5829 return SPELL_FAILED_NO_AMMO
;
5831 case ITEM_SUBCLASS_WEAPON_WAND
:
5842 return SPELL_CAST_OK
;
5845 void Spell::Delayed()
5847 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
5850 if (m_spellState
== SPELL_STATE_DELAYED
)
5851 return; // spell is active and can't be time-backed
5853 if(isDelayableNoMore()) // Spells may only be delayed twice
5856 // spells not loosing casting time ( slam, dynamites, bombs.. )
5857 if(!(m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_DAMAGE
))
5860 // check pushback reduce
5861 int32 delaytime
= 500; // spellcasting delay is normally 500ms
5862 int32 delayReduce
= 100; // must be initialized to 100 for percent modifiers
5863 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_NOT_LOSE_CASTING_TIME
, delayReduce
, this);
5864 delayReduce
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_REDUCE_PUSHBACK
) - 100;
5865 if(delayReduce
>= 100)
5868 delaytime
= delaytime
* (100 - delayReduce
) / 100;
5870 if(int32(m_timer
) + delaytime
> m_casttime
)
5872 delaytime
= m_casttime
- m_timer
;
5873 m_timer
= m_casttime
;
5876 m_timer
+= delaytime
;
5878 sLog
.outDetail("Spell %u partially interrupted for (%d) ms at damage", m_spellInfo
->Id
, delaytime
);
5880 WorldPacket
data(SMSG_SPELL_DELAYED
, 8+4);
5881 data
<< m_caster
->GetPackGUID();
5882 data
<< uint32(delaytime
);
5884 m_caster
->SendMessageToSet(&data
, true);
5887 void Spell::DelayedChannel()
5889 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
|| getState() != SPELL_STATE_CASTING
)
5892 if(isDelayableNoMore()) // Spells may only be delayed twice
5895 // check pushback reduce
5896 int32 delaytime
= GetSpellDuration(m_spellInfo
) * 25 / 100;// channeling delay is normally 25% of its time per hit
5897 int32 delayReduce
= 100; // must be initialized to 100 for percent modifiers
5898 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_NOT_LOSE_CASTING_TIME
, delayReduce
, this);
5899 delayReduce
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_REDUCE_PUSHBACK
) - 100;
5900 if(delayReduce
>= 100)
5903 delaytime
= delaytime
* (100 - delayReduce
) / 100;
5905 if(int32(m_timer
) < delaytime
)
5907 delaytime
= m_timer
;
5911 m_timer
-= delaytime
;
5913 sLog
.outDebug("Spell %u partially interrupted for %i ms, new duration: %u ms", m_spellInfo
->Id
, delaytime
, m_timer
);
5915 for(std::list
<TargetInfo
>::const_iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end(); ++ihit
)
5917 if ((*ihit
).missCondition
== SPELL_MISS_NONE
)
5919 if (Unit
* unit
= m_caster
->GetObjectGuid() == ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
))
5920 for (int j
= 0; j
< MAX_EFFECT_INDEX
; ++j
)
5921 if (ihit
->effectMask
& (1 << j
))
5922 unit
->DelayAura(m_spellInfo
->Id
, SpellEffectIndex(j
), delaytime
);
5927 for(int j
= 0; j
< MAX_EFFECT_INDEX
; ++j
)
5929 // partially interrupt persistent area auras
5930 if (DynamicObject
* dynObj
= m_caster
->GetDynObject(m_spellInfo
->Id
, SpellEffectIndex(j
)))
5931 dynObj
->Delay(delaytime
);
5934 SendChannelUpdate(m_timer
);
5937 void Spell::UpdateOriginalCasterPointer()
5939 if(m_originalCasterGUID
== m_caster
->GetObjectGuid())
5940 m_originalCaster
= m_caster
;
5941 else if (m_originalCasterGUID
.IsGameobject())
5943 GameObject
* go
= m_caster
->IsInWorld() ? m_caster
->GetMap()->GetGameObject(m_originalCasterGUID
) : NULL
;
5944 m_originalCaster
= go
? go
->GetOwner() : NULL
;
5948 Unit
* unit
= ObjectAccessor::GetUnit(*m_caster
, m_originalCasterGUID
);
5949 m_originalCaster
= unit
&& unit
->IsInWorld() ? unit
: NULL
;
5953 void Spell::UpdatePointers()
5955 UpdateOriginalCasterPointer();
5957 m_targets
.Update(m_caster
);
5960 bool Spell::CheckTargetCreatureType(Unit
* target
) const
5962 uint32 spellCreatureTargetMask
= m_spellInfo
->TargetCreatureType
;
5964 // Curse of Doom & Exorcism: not find another way to fix spell target check :/
5965 if (m_spellInfo
->SpellFamilyName
== SPELLFAMILY_WARLOCK
&& m_spellInfo
->Category
== 1179)
5967 // not allow cast at player
5968 if(target
->GetTypeId() == TYPEID_PLAYER
)
5971 spellCreatureTargetMask
= 0x7FF;
5974 // Dismiss Pet and Taming Lesson skipped
5975 if(m_spellInfo
->Id
== 2641 || m_spellInfo
->Id
== 23356)
5976 spellCreatureTargetMask
= 0;
5978 if (spellCreatureTargetMask
)
5980 uint32 TargetCreatureType
= target
->GetCreatureTypeMask();
5982 return !TargetCreatureType
|| (spellCreatureTargetMask
& TargetCreatureType
);
5987 CurrentSpellTypes
Spell::GetCurrentContainer()
5989 if (IsNextMeleeSwingSpell())
5990 return(CURRENT_MELEE_SPELL
);
5991 else if (IsAutoRepeat())
5992 return(CURRENT_AUTOREPEAT_SPELL
);
5993 else if (IsChanneledSpell(m_spellInfo
))
5994 return(CURRENT_CHANNELED_SPELL
);
5996 return(CURRENT_GENERIC_SPELL
);
5999 bool Spell::CheckTarget( Unit
* target
, SpellEffectIndex eff
)
6001 // Check targets for creature type mask and remove not appropriate (skip explicit self target case, maybe need other explicit targets)
6002 if(m_spellInfo
->EffectImplicitTargetA
[eff
] != TARGET_SELF
)
6004 if (!CheckTargetCreatureType(target
))
6008 // Check Aura spell req (need for AoE spells)
6009 if(m_spellInfo
->targetAuraSpell
&& !target
->HasAura(m_spellInfo
->targetAuraSpell
))
6011 if (m_spellInfo
->excludeTargetAuraSpell
&& target
->HasAura(m_spellInfo
->excludeTargetAuraSpell
))
6014 // Check targets for not_selectable unit flag and remove
6015 // A player can cast spells on his pet (or other controlled unit) though in any state
6016 if (target
!= m_caster
&& target
->GetCharmerOrOwnerGUID() != m_caster
->GetGUID())
6018 // any unattackable target skipped
6019 if (target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NON_ATTACKABLE
))
6022 // unselectable targets skipped in all cases except TARGET_SCRIPT targeting
6023 // in case TARGET_SCRIPT target selected by server always and can't be cheated
6024 if ((!m_IsTriggeredSpell
|| target
!= m_targets
.getUnitTarget()) &&
6025 target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NOT_SELECTABLE
) &&
6026 m_spellInfo
->EffectImplicitTargetA
[eff
] != TARGET_SCRIPT
&&
6027 m_spellInfo
->EffectImplicitTargetB
[eff
] != TARGET_SCRIPT
)
6031 // Check player targets and remove if in GM mode or GM invisibility (for not self casting case)
6032 if( target
!= m_caster
&& target
->GetTypeId() == TYPEID_PLAYER
)
6034 if(((Player
*)target
)->GetVisibility() == VISIBILITY_OFF
)
6037 if(((Player
*)target
)->isGameMaster() && !IsPositiveSpell(m_spellInfo
->Id
))
6041 // Check targets for LOS visibility (except spells without range limitations )
6042 switch(m_spellInfo
->Effect
[eff
])
6044 case SPELL_EFFECT_SUMMON_PLAYER
: // from anywhere
6046 case SPELL_EFFECT_DUMMY
:
6047 if(m_spellInfo
->Id
!= 20577) // Cannibalize
6050 case SPELL_EFFECT_RESURRECT_NEW
:
6051 // player far away, maybe his corpse near?
6052 if(target
!= m_caster
&& !target
->IsWithinLOSInMap(m_caster
))
6054 if(!m_targets
.getCorpseTargetGUID())
6057 Corpse
*corpse
= m_caster
->GetMap()->GetCorpse(m_targets
.getCorpseTargetGUID());
6061 if(target
->GetGUID() != corpse
->GetOwnerGUID())
6064 if(!corpse
->IsWithinLOSInMap(m_caster
))
6068 // all ok by some way or another, skip normal check
6070 default: // normal case
6071 // Get GO cast coordinates if original caster -> GO
6072 if (target
!= m_caster
)
6073 if (WorldObject
*caster
= GetCastingObject())
6074 if (!target
->IsWithinLOSInMap(caster
))
6082 bool Spell::IsNeedSendToClient() const
6084 return m_spellInfo
->SpellVisual
[0] || m_spellInfo
->SpellVisual
[1] || IsChanneledSpell(m_spellInfo
) ||
6085 m_spellInfo
->speed
> 0.0f
|| !m_triggeredByAuraSpell
&& !m_IsTriggeredSpell
;
6089 bool Spell::IsTriggeredSpellWithRedundentData() const
6091 return m_IsTriggeredSpell
&& (m_spellInfo
->manaCost
|| m_spellInfo
->ManaCostPercentage
);
6094 bool Spell::HaveTargetsForEffect(SpellEffectIndex effect
) const
6096 for(std::list
<TargetInfo
>::const_iterator itr
= m_UniqueTargetInfo
.begin(); itr
!= m_UniqueTargetInfo
.end(); ++itr
)
6097 if(itr
->effectMask
& (1 << effect
))
6100 for(std::list
<GOTargetInfo
>::const_iterator itr
= m_UniqueGOTargetInfo
.begin(); itr
!= m_UniqueGOTargetInfo
.end(); ++itr
)
6101 if(itr
->effectMask
& (1 << effect
))
6104 for(std::list
<ItemTargetInfo
>::const_iterator itr
= m_UniqueItemInfo
.begin(); itr
!= m_UniqueItemInfo
.end(); ++itr
)
6105 if(itr
->effectMask
& (1 << effect
))
6111 SpellEvent::SpellEvent(Spell
* spell
) : BasicEvent()
6116 SpellEvent::~SpellEvent()
6118 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
6121 if (m_Spell
->IsDeletable())
6127 sLog
.outError("~SpellEvent: %s %u tried to delete non-deletable spell %u. Was not deleted, causes memory leak.",
6128 (m_Spell
->GetCaster()->GetTypeId() == TYPEID_PLAYER
? "Player" : "Creature"), m_Spell
->GetCaster()->GetGUIDLow(), m_Spell
->m_spellInfo
->Id
);
6132 bool SpellEvent::Execute(uint64 e_time
, uint32 p_time
)
6134 // update spell if it is not finished
6135 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
6136 m_Spell
->update(p_time
);
6138 // check spell state to process
6139 switch (m_Spell
->getState())
6141 case SPELL_STATE_FINISHED
:
6143 // spell was finished, check deletable state
6144 if (m_Spell
->IsDeletable())
6146 // check, if we do have unfinished triggered spells
6147 return true; // spell is deletable, finish event
6149 // event will be re-added automatically at the end of routine)
6152 case SPELL_STATE_CASTING
:
6154 // this spell is in channeled state, process it on the next update
6155 // event will be re-added automatically at the end of routine)
6158 case SPELL_STATE_DELAYED
:
6160 // first, check, if we have just started
6161 if (m_Spell
->GetDelayStart() != 0)
6163 // no, we aren't, do the typical update
6164 // check, if we have channeled spell on our hands
6165 if (IsChanneledSpell(m_Spell
->m_spellInfo
))
6167 // evented channeled spell is processed separately, casted once after delay, and not destroyed till finish
6168 // check, if we have casting anything else except this channeled spell and autorepeat
6169 if (m_Spell
->GetCaster()->IsNonMeleeSpellCasted(false, true, true))
6171 // another non-melee non-delayed spell is casted now, abort
6176 // do the action (pass spell to channeling state)
6177 m_Spell
->handle_immediate();
6179 // event will be re-added automatically at the end of routine)
6183 // run the spell handler and think about what we can do next
6184 uint64 t_offset
= e_time
- m_Spell
->GetDelayStart();
6185 uint64 n_offset
= m_Spell
->handle_delayed(t_offset
);
6188 // re-add us to the queue
6189 m_Spell
->GetCaster()->m_Events
.AddEvent(this, m_Spell
->GetDelayStart() + n_offset
, false);
6190 return false; // event not complete
6193 // finish update event will be re-added automatically at the end of routine)
6198 // delaying had just started, record the moment
6199 m_Spell
->SetDelayStart(e_time
);
6200 // re-plan the event for the delay moment
6201 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ m_Spell
->GetDelayMoment(), false);
6202 return false; // event not complete
6209 // event will be re-added automatically at the end of routine)
6213 // spell processing not complete, plan event on the next update interval
6214 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ 1, false);
6215 return false; // event not complete
6218 void SpellEvent::Abort(uint64
/*e_time*/)
6220 // oops, the spell we try to do is aborted
6221 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
6225 bool SpellEvent::IsDeletable() const
6227 return m_Spell
->IsDeletable();
6230 SpellCastResult
Spell::CanOpenLock(SpellEffectIndex effIndex
, uint32 lockId
, SkillType
& skillId
, int32
& reqSkillValue
, int32
& skillValue
)
6232 if(!lockId
) // possible case for GO and maybe for items.
6233 return SPELL_CAST_OK
;
6236 LockEntry
const *lockInfo
= sLockStore
.LookupEntry(lockId
);
6239 return SPELL_FAILED_BAD_TARGETS
;
6241 bool reqKey
= false; // some locks not have reqs
6243 for(int j
= 0; j
< 8; ++j
)
6245 switch(lockInfo
->Type
[j
])
6247 // check key item (many fit cases can be)
6249 if(lockInfo
->Index
[j
] && m_CastItem
&& m_CastItem
->GetEntry()==lockInfo
->Index
[j
])
6250 return SPELL_CAST_OK
;
6253 // check key skill (only single first fit case can be)
6254 case LOCK_KEY_SKILL
:
6258 // wrong locktype, skip
6259 if(uint32(m_spellInfo
->EffectMiscValue
[effIndex
]) != lockInfo
->Index
[j
])
6262 skillId
= SkillByLockType(LockType(lockInfo
->Index
[j
]));
6264 if ( skillId
!= SKILL_NONE
)
6266 // skill bonus provided by casting spell (mostly item spells)
6267 // add the damage modifier from the spell casted (cheat lock / skeleton key etc.) (use m_currentBasePoints, CalculateDamage returns wrong value)
6268 uint32 spellSkillBonus
= uint32(m_currentBasePoints
[effIndex
]+1);
6269 reqSkillValue
= lockInfo
->Skill
[j
];
6271 // castitem check: rogue using skeleton keys. the skill values should not be added in this case.
6272 skillValue
= m_CastItem
|| m_caster
->GetTypeId()!= TYPEID_PLAYER
?
6273 0 : ((Player
*)m_caster
)->GetSkillValue(skillId
);
6275 skillValue
+= spellSkillBonus
;
6277 if (skillValue
< reqSkillValue
)
6278 return SPELL_FAILED_LOW_CASTLEVEL
;
6281 return SPELL_CAST_OK
;
6287 return SPELL_FAILED_BAD_TARGETS
;
6289 return SPELL_CAST_OK
;
6292 void Spell::FillAreaTargets(UnitList
&targetUnitMap
, float x
, float y
, float radius
, SpellNotifyPushType pushType
, SpellTargets spellTargets
)
6294 CellPair
p(MaNGOS::ComputeCellPair(x
, y
));
6296 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
6298 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, targetUnitMap
, radius
, pushType
, spellTargets
);
6299 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_notifier(notifier
);
6300 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_notifier(notifier
);
6301 cell
.Visit(p
, world_notifier
, *m_caster
->GetMap(), *m_caster
, radius
);
6302 cell
.Visit(p
, grid_notifier
, *m_caster
->GetMap(), *m_caster
, radius
);
6305 void Spell::FillRaidOrPartyTargets(UnitList
&targetUnitMap
, Unit
* member
, Unit
* center
, float radius
, bool raid
, bool withPets
, bool withcaster
)
6307 Player
*pMember
= member
->GetCharmerOrOwnerPlayerOrPlayerItself();
6308 Group
*pGroup
= pMember
? pMember
->GetGroup() : NULL
;
6312 uint8 subgroup
= pMember
->GetSubGroup();
6314 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
6316 Player
* Target
= itr
->getSource();
6318 // IsHostileTo check duel and controlled by enemy
6319 if (Target
&& (raid
|| subgroup
==Target
->GetSubGroup())
6320 && !m_caster
->IsHostileTo(Target
))
6322 if ((Target
== center
|| center
->IsWithinDistInMap(Target
, radius
)) &&
6323 (withcaster
|| Target
!= m_caster
))
6324 targetUnitMap
.push_back(Target
);
6327 if (Pet
* pet
= Target
->GetPet())
6328 if ((pet
== center
|| center
->IsWithinDistInMap(pet
, radius
)) &&
6329 (withcaster
|| pet
!= m_caster
))
6330 targetUnitMap
.push_back(pet
);
6336 Unit
* ownerOrSelf
= pMember
? pMember
: member
->GetCharmerOrOwnerOrSelf();
6337 if ((ownerOrSelf
== center
|| center
->IsWithinDistInMap(ownerOrSelf
, radius
)) &&
6338 (withcaster
|| ownerOrSelf
!= m_caster
))
6339 targetUnitMap
.push_back(ownerOrSelf
);
6342 if (Pet
* pet
= ownerOrSelf
->GetPet())
6343 if ((pet
== center
|| center
->IsWithinDistInMap(pet
, radius
)) &&
6344 (withcaster
|| pet
!= m_caster
))
6345 targetUnitMap
.push_back(pet
);
6349 void Spell::FillRaidOrPartyManaPriorityTargets(UnitList
&targetUnitMap
, Unit
* member
, Unit
* center
, float radius
, uint32 count
, bool raid
, bool withPets
, bool withCaster
)
6351 FillRaidOrPartyTargets(targetUnitMap
, member
, center
, radius
, raid
, withPets
, withCaster
);
6353 PrioritizeManaUnitQueue manaUsers
;
6354 for(UnitList::const_iterator itr
= targetUnitMap
.begin(); itr
!= targetUnitMap
.end() && manaUsers
.size() < count
; ++itr
)
6355 if ((*itr
)->getPowerType() == POWER_MANA
&& !(*itr
)->isDead())
6356 manaUsers
.push(PrioritizeManaUnitWraper(*itr
));
6358 targetUnitMap
.clear();
6359 while(!manaUsers
.empty())
6361 targetUnitMap
.push_back(manaUsers
.top().getUnit());
6366 void Spell::FillRaidOrPartyHealthPriorityTargets(UnitList
&targetUnitMap
, Unit
* member
, Unit
* center
, float radius
, uint32 count
, bool raid
, bool withPets
, bool withCaster
)
6368 FillRaidOrPartyTargets(targetUnitMap
, member
, center
, radius
, raid
, withPets
, withCaster
);
6370 PrioritizeHealthUnitQueue healthQueue
;
6371 for(UnitList::const_iterator itr
= targetUnitMap
.begin(); itr
!= targetUnitMap
.end() && healthQueue
.size() < count
; ++itr
)
6372 if (!(*itr
)->isDead())
6373 healthQueue
.push(PrioritizeHealthUnitWraper(*itr
));
6375 targetUnitMap
.clear();
6376 while(!healthQueue
.empty())
6378 targetUnitMap
.push_back(healthQueue
.top().getUnit());
6383 WorldObject
* Spell::GetCastingObject() const
6385 if (m_originalCasterGUID
.IsGameobject())
6386 return m_caster
->IsInWorld() ? m_caster
->GetMap()->GetGameObject(m_originalCasterGUID
) : NULL
;