2 * Copyright (C) 2005-2008 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"
36 #include "SpellAuras.h"
38 #include "UpdateData.h"
39 #include "MapManager.h"
40 #include "ObjectAccessor.h"
42 #include "Policies/SingletonImp.h"
43 #include "SharedDefines.h"
45 #include "VMapFactory.h"
46 #include "BattleGround.h"
49 #define SPELL_CHANNEL_UPDATE_INTERVAL 1000
51 extern pEffect SpellEffects
[TOTAL_SPELL_EFFECTS
];
53 bool IsQuestTameSpell(uint32 spellId
)
55 SpellEntry
const *spellproto
= sSpellStore
.LookupEntry(spellId
);
56 if (!spellproto
) return false;
58 return spellproto
->Effect
[0] == SPELL_EFFECT_THREAT
59 && spellproto
->Effect
[1] == SPELL_EFFECT_APPLY_AURA
&& spellproto
->EffectApplyAuraName
[1] == SPELL_AURA_DUMMY
;
62 SpellCastTargets::SpellCastTargets()
70 m_CorpseTargetGUID
= 0;
72 m_itemTargetEntry
= 0;
74 m_srcX
= m_srcY
= m_srcZ
= m_destX
= m_destY
= m_destZ
= 0;
79 SpellCastTargets::~SpellCastTargets()
83 void SpellCastTargets::setUnitTarget(Unit
*target
)
88 m_destX
= target
->GetPositionX();
89 m_destY
= target
->GetPositionY();
90 m_destZ
= target
->GetPositionZ();
91 m_unitTarget
= target
;
92 m_unitTargetGUID
= target
->GetGUID();
93 m_targetMask
|= TARGET_FLAG_UNIT
;
96 void SpellCastTargets::setDestination(float x
, float y
, float z
)
101 m_targetMask
|= TARGET_FLAG_DEST_LOCATION
;
104 void SpellCastTargets::setGOTarget(GameObject
*target
)
107 m_GOTargetGUID
= target
->GetGUID();
108 // m_targetMask |= TARGET_FLAG_OBJECT;
111 void SpellCastTargets::setItemTarget(Item
* item
)
117 m_itemTargetGUID
= item
->GetGUID();
118 m_itemTargetEntry
= item
->GetEntry();
119 m_targetMask
|= TARGET_FLAG_ITEM
;
122 void SpellCastTargets::setCorpseTarget(Corpse
* corpse
)
124 m_CorpseTargetGUID
= corpse
->GetGUID();
127 void SpellCastTargets::Update(Unit
* caster
)
129 m_GOTarget
= m_GOTargetGUID
? ObjectAccessor::GetGameObject(*caster
,m_GOTargetGUID
) : NULL
;
130 m_unitTarget
= m_unitTargetGUID
?
131 ( m_unitTargetGUID
==caster
->GetGUID() ? caster
: ObjectAccessor::GetUnit(*caster
, m_unitTargetGUID
) ) :
135 if(caster
->GetTypeId()==TYPEID_PLAYER
)
137 if(m_targetMask
& TARGET_FLAG_ITEM
)
138 m_itemTarget
= ((Player
*)caster
)->GetItemByGuid(m_itemTargetGUID
);
141 Player
* pTrader
= ((Player
*)caster
)->GetTrader();
142 if(pTrader
&& m_itemTargetGUID
< TRADE_SLOT_COUNT
)
143 m_itemTarget
= pTrader
->GetItemByPos(pTrader
->GetItemPosByTradeSlot(m_itemTargetGUID
));
146 m_itemTargetEntry
= m_itemTarget
->GetEntry();
150 bool SpellCastTargets::read ( WorldPacket
* data
, Unit
*caster
)
152 if(data
->rpos()+4 > data
->size())
155 *data
>> m_targetMask
;
157 if(m_targetMask
== TARGET_FLAG_SELF
)
159 m_destX
= caster
->GetPositionX();
160 m_destY
= caster
->GetPositionY();
161 m_destZ
= caster
->GetPositionZ();
162 m_unitTarget
= caster
;
163 m_unitTargetGUID
= caster
->GetGUID();
167 // TARGET_FLAG_UNK2 is used for non-combat pets, maybe other?
168 if( m_targetMask
& ( TARGET_FLAG_UNIT
| TARGET_FLAG_UNK2
))
169 if(!data
->readPackGUID(m_unitTargetGUID
))
172 if( m_targetMask
& ( TARGET_FLAG_OBJECT
| TARGET_FLAG_OBJECT_UNK
))
173 if(!data
->readPackGUID(m_GOTargetGUID
))
176 if(( m_targetMask
& ( TARGET_FLAG_ITEM
| TARGET_FLAG_TRADE_ITEM
)) && caster
->GetTypeId() == TYPEID_PLAYER
)
177 if(!data
->readPackGUID(m_itemTargetGUID
))
180 if( m_targetMask
& TARGET_FLAG_SOURCE_LOCATION
)
182 if(data
->rpos()+4+4+4 > data
->size())
185 *data
>> m_srcX
>> m_srcY
>> m_srcZ
;
186 if(!MaNGOS::IsValidMapCoord(m_srcX
, m_srcY
, m_srcZ
))
190 if( m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
192 if(data
->rpos()+4+4+4 > data
->size())
195 *data
>> m_destX
>> m_destY
>> m_destZ
;
196 if(!MaNGOS::IsValidMapCoord(m_destX
, m_destY
, m_destZ
))
200 if( m_targetMask
& TARGET_FLAG_STRING
)
202 if(data
->rpos()+1 > data
->size())
205 *data
>> m_strTarget
;
208 if( m_targetMask
& (TARGET_FLAG_CORPSE
| TARGET_FLAG_PVP_CORPSE
) )
209 if(!data
->readPackGUID(m_CorpseTargetGUID
))
212 // find real units/GOs
217 void SpellCastTargets::write ( WorldPacket
* data
)
219 *data
<< uint32(m_targetMask
);
221 if( m_targetMask
& ( TARGET_FLAG_UNIT
| TARGET_FLAG_PVP_CORPSE
| TARGET_FLAG_OBJECT
| TARGET_FLAG_OBJECT_UNK
| TARGET_FLAG_CORPSE
| TARGET_FLAG_UNK2
) )
223 if(m_targetMask
& TARGET_FLAG_UNIT
)
226 data
->append(m_unitTarget
->GetPackGUID());
230 else if( m_targetMask
& ( TARGET_FLAG_OBJECT
| TARGET_FLAG_OBJECT_UNK
) )
233 data
->append(m_GOTarget
->GetPackGUID());
237 else if( m_targetMask
& ( TARGET_FLAG_CORPSE
| TARGET_FLAG_PVP_CORPSE
) )
238 data
->appendPackGUID(m_CorpseTargetGUID
);
243 if( m_targetMask
& ( TARGET_FLAG_ITEM
| TARGET_FLAG_TRADE_ITEM
) )
246 data
->append(m_itemTarget
->GetPackGUID());
251 if( m_targetMask
& TARGET_FLAG_SOURCE_LOCATION
)
252 *data
<< m_srcX
<< m_srcY
<< m_srcZ
;
254 if( m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
255 *data
<< m_destX
<< m_destY
<< m_destZ
;
257 if( m_targetMask
& TARGET_FLAG_STRING
)
258 *data
<< m_strTarget
;
261 Spell::Spell( Unit
* Caster
, SpellEntry
const *info
, bool triggered
, uint64 originalCasterGUID
, Spell
** triggeringContainer
)
263 ASSERT( Caster
!= NULL
&& info
!= NULL
);
264 ASSERT( info
== sSpellStore
.LookupEntry( info
->Id
) && "`info` must be pointer to sSpellStore element");
268 m_selfContainer
= NULL
;
269 m_triggeringContainer
= triggeringContainer
;
270 m_referencedFromCurrentSpell
= false;
271 m_executedCurrently
= false;
273 m_delayAtDamageCount
= 0;
275 m_applyMultiplierMask
= 0;
277 // Get data for type of attack
278 switch (m_spellInfo
->DmgClass
)
280 case SPELL_DAMAGE_CLASS_MELEE
:
281 if (m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_REQ_OFFHAND
)
282 m_attackType
= OFF_ATTACK
;
284 m_attackType
= BASE_ATTACK
;
286 case SPELL_DAMAGE_CLASS_RANGED
:
287 m_attackType
= RANGED_ATTACK
;
291 if (m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_REQ_WAND
)
292 m_attackType
= RANGED_ATTACK
;
294 m_attackType
= BASE_ATTACK
;
298 m_spellSchoolMask
= GetSpellSchoolMask(info
); // Can be override for some spell (wand shoot for example)
300 if(m_attackType
== RANGED_ATTACK
)
303 if((m_caster
->getClassMask() & CLASSMASK_WAND_USERS
) != 0 && m_caster
->GetTypeId()==TYPEID_PLAYER
)
305 if(Item
* pItem
= ((Player
*)m_caster
)->GetWeaponForAttack(RANGED_ATTACK
))
306 m_spellSchoolMask
= SpellSchoolMask(1 << pItem
->GetProto()->Damage
->DamageType
);
309 // Set health leech amount to zero
312 if(originalCasterGUID
)
313 m_originalCasterGUID
= originalCasterGUID
;
315 m_originalCasterGUID
= m_caster
->GetGUID();
317 if(m_originalCasterGUID
==m_caster
->GetGUID())
318 m_originalCaster
= m_caster
;
321 m_originalCaster
= ObjectAccessor::GetUnit(*m_caster
,m_originalCasterGUID
);
322 if(m_originalCaster
&& !m_originalCaster
->IsInWorld()) m_originalCaster
= NULL
;
325 for(int i
=0; i
<3; ++i
)
326 m_currentBasePoints
[i
] = m_spellInfo
->EffectBasePoints
[i
];
328 m_spellState
= SPELL_STATE_NULL
;
330 m_castPositionX
= m_castPositionY
= m_castPositionZ
= 0;
331 m_TriggerSpells
.clear();
332 m_IsTriggeredSpell
= triggered
;
333 //m_AreaAura = false;
338 gameObjTarget
= NULL
;
342 m_triggeredByAuraSpell
= NULL
;
345 if( m_spellInfo
->AttributesEx2
== 0x000020 && !triggered
)
348 m_autoRepeat
= false;
351 m_powerCost
= 0; // setup to correct value in Spell::prepare, don't must be used before.
352 m_casttime
= 0; // setup to correct value in Spell::prepare, don't must be used before.
353 m_timer
= 0; // will set to castime in prepare
355 m_needAliveTargetMask
= 0;
357 // determine reflection
358 m_canReflect
= false;
360 if(m_spellInfo
->DmgClass
== SPELL_DAMAGE_CLASS_MAGIC
&& (m_spellInfo
->AttributesEx2
& 0x4)==0)
364 if (m_spellInfo
->Effect
[j
]==0)
367 if(!IsPositiveTarget(m_spellInfo
->EffectImplicitTargetA
[j
],m_spellInfo
->EffectImplicitTargetB
[j
]))
370 m_canReflect
= (m_spellInfo
->AttributesEx
& (1<<7)) ? true : false;
386 void Spell::FillTargetMap()
388 // TODO: ADD the correct target FILLS!!!!!!
390 for(uint32 i
=0;i
<3;i
++)
392 // not call for empty effect.
393 // Also some spells use not used effect targets for store targets for dummy effect in triggered spells
394 if(m_spellInfo
->Effect
[i
]==0)
397 // targets for TARGET_SCRIPT_COORDINATES (A) and TARGET_SCRIPT filled in Spell::canCast call
398 if( m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SCRIPT_COORDINATES
||
399 m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SCRIPT
||
400 m_spellInfo
->EffectImplicitTargetB
[i
] == TARGET_SCRIPT
&& m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_SELF
)
403 // TODO: find a way so this is not needed?
404 // for area auras always add caster as target (needed for totems for example)
405 if(IsAreaAuraEffect(m_spellInfo
->Effect
[i
]))
406 AddUnitTarget(m_caster
, i
);
408 std::list
<Unit
*> tmpUnitMap
;
410 // TargetA/TargetB dependent from each other, we not switch to full support this dependences
411 // but need it support in some know cases
412 switch(m_spellInfo
->EffectImplicitTargetA
[i
])
414 case TARGET_ALL_AROUND_CASTER
:
415 if( m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_PARTY
||
416 m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_FRIENDLY_UNITS_AROUND_CASTER
||
417 m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_RANDOM_RAID_MEMBER
)
419 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
421 // Note: this hack with search required until GO casting not implemented
422 // environment damage spells already have around enemies targeting but this not help in case not existed GO casting support
423 // currently each enemy selected explicitly and self cast damage
424 else if(m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_ENEMY_IN_AREA
&& m_spellInfo
->Effect
[i
]==SPELL_EFFECT_ENVIRONMENTAL_DAMAGE
)
426 if(m_targets
.getUnitTarget())
427 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
431 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
432 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
435 case TARGET_TABLE_X_Y_Z_COORDINATES
:
436 // Only if target A, for target B (used in teleports) dest select in effect
437 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
440 switch(m_spellInfo
->EffectImplicitTargetB
[i
])
442 case TARGET_SCRIPT_COORDINATES
: // B case filled in canCast but we need fill unit list base at A case
443 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
446 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
447 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
453 if( (m_spellInfo
->EffectImplicitTargetA
[i
]==0 || m_spellInfo
->EffectImplicitTargetA
[i
]==TARGET_EFFECT_SELECT
) &&
454 (m_spellInfo
->EffectImplicitTargetB
[i
]==0 || m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_EFFECT_SELECT
) )
456 // add here custom effects that need default target.
457 // FOR EVERY TARGET TYPE THERE IS A DIFFERENT FILL!!
458 switch(m_spellInfo
->Effect
[i
])
460 case SPELL_EFFECT_DUMMY
:
462 switch(m_spellInfo
->Id
)
464 case 20577: // Cannibalize
466 // non-standard target selection
467 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
468 float max_range
= GetSpellMaxRange(srange
);
470 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
472 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
475 WorldObject
* result
= NULL
;
477 MaNGOS::CannibalizeObjectCheck
u_check(m_caster
, max_range
);
478 MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
> searcher(result
, u_check
);
480 TypeContainerVisitor
<MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
>, GridTypeMapContainer
> grid_searcher(searcher
);
481 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
482 cell_lock
->Visit(cell_lock
, grid_searcher
, *m_caster
->GetMap());
486 TypeContainerVisitor
<MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
>, WorldTypeMapContainer
> world_searcher(searcher
);
487 cell_lock
->Visit(cell_lock
, world_searcher
, *m_caster
->GetMap());
492 switch(result
->GetTypeId())
496 tmpUnitMap
.push_back((Unit
*)result
);
499 m_targets
.setCorpseTarget((Corpse
*)result
);
500 if(Player
* owner
= ObjectAccessor::FindPlayer(((Corpse
*)result
)->GetOwnerGUID()))
501 tmpUnitMap
.push_back(owner
);
507 // clear cooldown at fail
508 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
510 ((Player
*)m_caster
)->RemoveSpellCooldown(m_spellInfo
->Id
);
512 WorldPacket
data(SMSG_CLEAR_COOLDOWN
, (4+8));
513 data
<< uint32(m_spellInfo
->Id
);
514 data
<< uint64(m_caster
->GetGUID());
515 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
518 SendCastResult(SPELL_FAILED_NO_EDIBLE_CORPSES
);
524 if(m_targets
.getUnitTarget())
525 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
530 case SPELL_EFFECT_RESURRECT
:
531 case SPELL_EFFECT_PARRY
:
532 case SPELL_EFFECT_BLOCK
:
533 case SPELL_EFFECT_CREATE_ITEM
:
534 case SPELL_EFFECT_TRIGGER_SPELL
:
535 case SPELL_EFFECT_TRIGGER_MISSILE
:
536 case SPELL_EFFECT_LEARN_SPELL
:
537 case SPELL_EFFECT_SKILL_STEP
:
538 case SPELL_EFFECT_PROFICIENCY
:
539 case SPELL_EFFECT_SUMMON_OBJECT_WILD
:
540 case SPELL_EFFECT_SELF_RESURRECT
:
541 case SPELL_EFFECT_REPUTATION
:
542 if(m_targets
.getUnitTarget())
543 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
545 case SPELL_EFFECT_SUMMON_PLAYER
:
546 if(m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->GetSelection())
548 Player
* target
= objmgr
.GetPlayer(((Player
*)m_caster
)->GetSelection());
550 tmpUnitMap
.push_back(target
);
553 case SPELL_EFFECT_RESURRECT_NEW
:
554 if(m_targets
.getUnitTarget())
555 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
556 if(m_targets
.getCorpseTargetGUID())
558 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
561 Player
* owner
= ObjectAccessor::FindPlayer(corpse
->GetOwnerGUID());
563 tmpUnitMap
.push_back(owner
);
567 case SPELL_EFFECT_SUMMON
:
568 if(m_spellInfo
->EffectMiscValueB
[i
] == SUMMON_TYPE_POSESSED
|| m_spellInfo
->EffectMiscValueB
[i
] == SUMMON_TYPE_POSESSED2
)
570 if(m_targets
.getUnitTarget())
571 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
574 tmpUnitMap
.push_back(m_caster
);
576 case SPELL_EFFECT_SUMMON_CHANGE_ITEM
:
577 case SPELL_EFFECT_SUMMON_WILD
:
578 case SPELL_EFFECT_SUMMON_GUARDIAN
:
579 case SPELL_EFFECT_TRANS_DOOR
:
580 case SPELL_EFFECT_ADD_FARSIGHT
:
581 case SPELL_EFFECT_APPLY_GLYPH
:
582 case SPELL_EFFECT_STUCK
:
583 case SPELL_EFFECT_DESTROY_ALL_TOTEMS
:
584 case SPELL_EFFECT_SUMMON_DEMON
:
585 case SPELL_EFFECT_SKILL
:
586 tmpUnitMap
.push_back(m_caster
);
588 case SPELL_EFFECT_LEARN_PET_SPELL
:
589 if(Pet
* pet
= m_caster
->GetPet())
590 tmpUnitMap
.push_back(pet
);
592 case SPELL_EFFECT_ENCHANT_ITEM
:
593 case SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY
:
594 case SPELL_EFFECT_DISENCHANT
:
595 case SPELL_EFFECT_FEED_PET
:
596 case SPELL_EFFECT_PROSPECTING
:
597 case SPELL_EFFECT_MILLING
:
598 if(m_targets
.getItemTarget())
599 AddItemTarget(m_targets
.getItemTarget(), i
);
601 case SPELL_EFFECT_APPLY_AURA
:
602 switch(m_spellInfo
->EffectApplyAuraName
[i
])
604 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)
605 case SPELL_AURA_ADD_PCT_MODIFIER
:
606 tmpUnitMap
.push_back(m_caster
);
608 default: // apply to target in other case
609 if(m_targets
.getUnitTarget())
610 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
614 case SPELL_EFFECT_APPLY_AREA_AURA_PARTY
:
616 if(m_spellInfo
->Attributes
== 0x9050000 || m_spellInfo
->Attributes
== 0x10000)
617 SetTargetMap(i
,TARGET_AREAEFFECT_PARTY
,tmpUnitMap
);
619 case SPELL_EFFECT_SKIN_PLAYER_CORPSE
:
620 if(m_targets
.getUnitTarget())
622 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
624 else if (m_targets
.getCorpseTargetGUID())
626 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
629 Player
* owner
= ObjectAccessor::FindPlayer(corpse
->GetOwnerGUID());
631 tmpUnitMap
.push_back(owner
);
639 if(IsChanneledSpell(m_spellInfo
) && !tmpUnitMap
.empty())
640 m_needAliveTargetMask
|= (1<<i
);
642 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
644 Player
*me
= (Player
*)m_caster
;
645 for (std::list
<Unit
*>::const_iterator itr
= tmpUnitMap
.begin(); itr
!= tmpUnitMap
.end(); ++itr
)
647 Unit
*owner
= (*itr
)->GetOwner();
648 Unit
*u
= owner
? owner
: (*itr
);
649 if(u
!=m_caster
&& u
->IsPvP() && (!me
->duel
|| me
->duel
->opponent
!= u
))
652 me
->RemoveAurasWithInterruptFlags(AURA_INTERRUPT_FLAG_ENTER_PVP_COMBAT
);
658 for (std::list
<Unit
*>::iterator itr
= tmpUnitMap
.begin() ; itr
!= tmpUnitMap
.end();)
660 if (!CheckTarget (*itr
, i
))
662 itr
= tmpUnitMap
.erase(itr
);
669 for(std::list
<Unit
*>::iterator iunit
= tmpUnitMap
.begin();iunit
!= tmpUnitMap
.end();++iunit
)
670 AddUnitTarget((*iunit
), i
);
674 void Spell::prepareDataForTriggerSystem()
676 //==========================================================================================
677 // Now fill data for trigger system, need know:
678 // Ñan spell trigger another or not ( m_canTrigger )
679 // Create base triggers flags for Attacker and Victim ( m_procAttacker and m_procVictim)
680 //==========================================================================================
682 // Fill flag can spell trigger or not
683 if (!m_IsTriggeredSpell
)
684 m_canTrigger
= true; // Normal cast - can trigger
685 else if (!m_triggeredByAuraSpell
)
686 m_canTrigger
= true; // Triggered from SPELL_EFFECT_TRIGGER_SPELL - can trigger
687 else // Exceptions (some periodic triggers)
689 m_canTrigger
= false; // Triggered spells can`t trigger another
690 switch (m_spellInfo
->SpellFamilyName
)
692 case SPELLFAMILY_MAGE
: // Arcane Missles triggers need do it
693 if (m_spellInfo
->SpellFamilyFlags
& 0x0000000000200000LL
) m_canTrigger
= true;
695 case SPELLFAMILY_WARLOCK
: // For Hellfire Effect / Rain of Fire / Seed of Corruption triggers need do it
696 if (m_spellInfo
->SpellFamilyFlags
& 0x0000800000000060LL
) m_canTrigger
= true;
698 case SPELLFAMILY_HUNTER
: // Hunter Explosive Trap Effect/Immolation Trap Effect/Frost Trap Aura/Snake Trap Effect
699 if (m_spellInfo
->SpellFamilyFlags
& 0x0000200000000014LL
) m_canTrigger
= true;
701 case SPELLFAMILY_PALADIN
: // For Holy Shock triggers need do it
702 if (m_spellInfo
->SpellFamilyFlags
& 0x0001000000200000LL
) m_canTrigger
= true;
706 // Do not trigger from item cast spell
708 m_canTrigger
= false;
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
:
718 m_procAttacker
= PROC_FLAG_SUCCESSFUL_RANGED_SPELL_HIT
;
719 m_procVictim
= PROC_FLAG_TAKEN_RANGED_SPELL_HIT
;
722 if (IsPositiveSpell(m_spellInfo
->Id
)) // Check for positive spell
724 m_procAttacker
= PROC_FLAG_SUCCESSFUL_POSITIVE_SPELL
;
725 m_procVictim
= PROC_FLAG_TAKEN_POSITIVE_SPELL
;
727 else if (m_spellInfo
->Id
!= SPELL_ID_AUTOSHOT
) // Wands
729 m_procAttacker
= PROC_FLAG_SUCCESSFUL_RANGED_SPELL_HIT
;
730 m_procVictim
= PROC_FLAG_TAKEN_RANGED_SPELL_HIT
;
734 m_procAttacker
= PROC_FLAG_SUCCESSFUL_NEGATIVE_SPELL_HIT
;
735 m_procVictim
= PROC_FLAG_TAKEN_NEGATIVE_SPELL_HIT
;
739 // Hunter traps spells (for Entrapment trigger)
740 // Gives your Immolation Trap, Frost Trap, Explosive Trap, and Snake Trap ....
741 if (m_spellInfo
->SpellFamilyName
== SPELLFAMILY_HUNTER
&& m_spellInfo
->SpellFamilyFlags
& 0x0000200000000014LL
)
742 m_procAttacker
|= PROC_FLAG_ON_TRAP_ACTIVATION
;
745 void Spell::CleanupTargetList()
747 m_UniqueTargetInfo
.clear();
748 m_UniqueGOTargetInfo
.clear();
749 m_UniqueItemInfo
.clear();
755 void Spell::AddUnitTarget(Unit
* pVictim
, uint32 effIndex
)
757 if( m_spellInfo
->Effect
[effIndex
]==0 )
760 uint64 targetGUID
= pVictim
->GetGUID();
762 // Lookup target in already in list
763 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
765 if (targetGUID
== ihit
->targetGUID
) // Found in list
767 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
772 // This is new target calculate data for him
774 // Get spell hit result on target
776 target
.targetGUID
= targetGUID
; // Store target GUID
777 target
.effectMask
= 1<<effIndex
; // Store index of effect
778 target
.processed
= false; // Effects not apply on target
780 // Calculate hit result
781 target
.missCondition
= m_caster
->SpellHitResult(pVictim
, m_spellInfo
, m_canReflect
);
782 if (target
.missCondition
== SPELL_MISS_NONE
)
787 // Spell have speed - need calculate incoming time
788 if (m_spellInfo
->speed
> 0.0f
)
790 // calculate spell incoming interval
791 float dist
= m_caster
->GetDistance(pVictim
->GetPositionX(), pVictim
->GetPositionY(), pVictim
->GetPositionZ());
792 if (dist
< 5.0f
) dist
= 5.0f
;
793 target
.timeDelay
= (uint64
) floor(dist
/ m_spellInfo
->speed
* 1000.0f
);
795 // Calculate minimum incoming time
796 if (m_delayMoment
==0 || m_delayMoment
>target
.timeDelay
)
797 m_delayMoment
= target
.timeDelay
;
800 target
.timeDelay
= 0LL;
802 // If target reflect spell back to caster
803 if (target
.missCondition
==SPELL_MISS_REFLECT
)
805 // Calculate reflected spell result on caster
806 target
.reflectResult
= m_caster
->SpellHitResult(m_caster
, m_spellInfo
, m_canReflect
);
808 if (target
.reflectResult
== SPELL_MISS_REFLECT
) // Impossible reflect again, so simply deflect spell
809 target
.reflectResult
= SPELL_MISS_PARRY
;
811 // Increase time interval for reflected spells by 1.5
812 target
.timeDelay
+=target
.timeDelay
>>1;
815 target
.reflectResult
= SPELL_MISS_NONE
;
817 // Add target to list
818 m_UniqueTargetInfo
.push_back(target
);
821 void Spell::AddUnitTarget(uint64 unitGUID
, uint32 effIndex
)
823 Unit
* unit
= m_caster
->GetGUID()==unitGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, unitGUID
);
825 AddUnitTarget(unit
, effIndex
);
828 void Spell::AddGOTarget(GameObject
* pVictim
, uint32 effIndex
)
830 if( m_spellInfo
->Effect
[effIndex
]==0 )
833 uint64 targetGUID
= pVictim
->GetGUID();
835 // Lookup target in already in list
836 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
838 if (targetGUID
== ihit
->targetGUID
) // Found in list
840 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
845 // This is new target calculate data for him
848 target
.targetGUID
= targetGUID
;
849 target
.effectMask
= 1<<effIndex
;
850 target
.processed
= false; // Effects not apply on target
852 // Spell have speed - need calculate incoming time
853 if (m_spellInfo
->speed
> 0.0f
)
855 // calculate spell incoming interval
856 float dist
= m_caster
->GetDistance(pVictim
->GetPositionX(), pVictim
->GetPositionY(), pVictim
->GetPositionZ());
857 if (dist
< 5.0f
) dist
= 5.0f
;
858 target
.timeDelay
= (uint64
) floor(dist
/ m_spellInfo
->speed
* 1000.0f
);
859 if (m_delayMoment
==0 || m_delayMoment
>target
.timeDelay
)
860 m_delayMoment
= target
.timeDelay
;
863 target
.timeDelay
= 0LL;
867 // Add target to list
868 m_UniqueGOTargetInfo
.push_back(target
);
871 void Spell::AddGOTarget(uint64 goGUID
, uint32 effIndex
)
873 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, goGUID
);
875 AddGOTarget(go
, effIndex
);
878 void Spell::AddItemTarget(Item
* pitem
, uint32 effIndex
)
880 if( m_spellInfo
->Effect
[effIndex
]==0 )
883 // Lookup target in already in list
884 for(std::list
<ItemTargetInfo
>::iterator ihit
= m_UniqueItemInfo
.begin();ihit
!= m_UniqueItemInfo
.end();++ihit
)
886 if (pitem
== ihit
->item
) // Found in list
888 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
893 // This is new target add data
895 ItemTargetInfo target
;
897 target
.effectMask
= 1<<effIndex
;
898 m_UniqueItemInfo
.push_back(target
);
901 void Spell::DoAllEffectOnTarget(TargetInfo
*target
)
903 if (target
->processed
) // Check target
905 target
->processed
= true; // Target checked in apply effects procedure
907 // Get mask of effects for target
908 uint32 mask
= target
->effectMask
;
909 if (mask
== 0) // No effects
912 Unit
* unit
= m_caster
->GetGUID()==target
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
,target
->targetGUID
);
916 // Get original caster (if exist) and calculate damage/healing from him data
917 Unit
*caster
= m_originalCasterGUID
? m_originalCaster
: m_caster
;
919 // Skip if m_originalCaster not avaiable
923 SpellMissInfo missInfo
= target
->missCondition
;
924 // Need init unitTarget by default unit (can changed in code on reflect)
925 // Or on missInfo!=SPELL_MISS_NONE unitTarget undefined (but need in trigger subsystem)
928 // Reset damage/healing counter
932 // Fill base trigger info
933 uint32 procAttacker
= m_procAttacker
;
934 uint32 procVictim
= m_procVictim
;
935 uint32 procEx
= PROC_EX_NONE
;
937 if (missInfo
==SPELL_MISS_NONE
) // In case spell hit target, do all effect on that target
938 DoSpellHitOnUnit(unit
, mask
);
939 else if (missInfo
== SPELL_MISS_REFLECT
) // In case spell reflect from target, do all effect on caster (if hit)
941 if (target
->reflectResult
== SPELL_MISS_NONE
) // If reflected spell hit caster -> do all effect on him
942 DoSpellHitOnUnit(m_caster
, mask
);
945 // All calculated do it!
946 // Do healing and triggers
949 bool crit
= caster
->isSpellCrit(NULL
, m_spellInfo
, m_spellSchoolMask
);
950 uint32 addhealth
= m_healing
;
953 procEx
|= PROC_EX_CRITICAL_HIT
;
954 addhealth
= caster
->SpellCriticalBonus(m_spellInfo
, addhealth
, NULL
);
957 procEx
|= PROC_EX_NORMAL_HIT
;
959 caster
->SendHealSpellLog(unitTarget
, m_spellInfo
->Id
, addhealth
, crit
);
961 // Do triggers for unit (reflect triggers passed on hit phase for correct drop charge)
962 if (m_canTrigger
&& missInfo
!= SPELL_MISS_REFLECT
)
963 caster
->ProcDamageAndSpell(unitTarget
, procAttacker
, procVictim
, procEx
, addhealth
, m_attackType
, m_spellInfo
);
965 int32 gain
= unitTarget
->ModifyHealth( int32(addhealth
) );
967 unitTarget
->getHostilRefManager().threatAssist(caster
, float(gain
) * 0.5f
, m_spellInfo
);
968 if(caster
->GetTypeId()==TYPEID_PLAYER
)
969 if(BattleGround
*bg
= ((Player
*)caster
)->GetBattleGround())
970 bg
->UpdatePlayerScore(((Player
*)caster
), SCORE_HEALING_DONE
, gain
);
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
);
981 // Send log damage message to client
982 caster
->SendSpellNonMeleeDamageLog(&damageInfo
);
984 procEx
= createProcExtendMask(&damageInfo
, missInfo
);
985 procVictim
|= PROC_FLAG_TAKEN_ANY_DAMAGE
;
987 // Do triggers for unit (reflect triggers passed on hit phase for correct drop charge)
988 if (m_canTrigger
&& missInfo
!= SPELL_MISS_REFLECT
)
989 caster
->ProcDamageAndSpell(unitTarget
, procAttacker
, procVictim
, procEx
, damageInfo
.damage
, m_attackType
, m_spellInfo
);
991 caster
->DealSpellDamage(&damageInfo
, true);
993 // Judgement of Blood
994 if (m_spellInfo
->SpellFamilyName
== SPELLFAMILY_PALADIN
&& m_spellInfo
->SpellFamilyFlags
& 0x0000000800000000LL
&& m_spellInfo
->SpellIconID
==153)
996 int32 damagePoint
= damageInfo
.damage
* 33 / 100;
997 m_caster
->CastCustomSpell(m_caster
, 32220, &damagePoint
, NULL
, NULL
, true);
1000 else if (m_spellInfo
->SpellFamilyName
== SPELLFAMILY_WARRIOR
&& m_spellInfo
->SpellFamilyFlags
& 0x40000000000LL
)
1003 switch(m_spellInfo
->Id
)
1005 case 23881: BTAura
= 23885; break;
1006 case 23892: BTAura
= 23886; break;
1007 case 23893: BTAura
= 23887; break;
1008 case 23894: BTAura
= 23888; break;
1009 case 25251: BTAura
= 25252; break;
1010 case 30335: BTAura
= 30339; break;
1012 sLog
.outError("Spell::EffectSchoolDMG: Spell %u not handled in BTAura",m_spellInfo
->Id
);
1016 m_caster
->CastSpell(m_caster
,BTAura
,true);
1019 // Passive spell hits/misses or active spells only misses (only triggers)
1022 // Fill base damage struct (unitTarget - is real spell target)
1023 SpellNonMeleeDamage
damageInfo(caster
, unitTarget
, m_spellInfo
->Id
, m_spellSchoolMask
);
1024 procEx
= createProcExtendMask(&damageInfo
, missInfo
);
1025 // Do triggers for unit (reflect triggers passed on hit phase for correct drop charge)
1026 if (m_canTrigger
&& missInfo
!= SPELL_MISS_REFLECT
)
1027 caster
->ProcDamageAndSpell(unit
, procAttacker
, procVictim
, procEx
, 0, m_attackType
, m_spellInfo
);
1030 // Call scripted function for AI if this spell is casted upon a creature (except pets)
1031 if(IS_CREATURE_GUID(target
->targetGUID
))
1033 // cast at creature (or GO) quest objectives update at successful cast finished (+channel finished)
1034 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
1035 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() && !IsChannelActive() )
1036 ((Player
*)m_caster
)->CastedCreatureOrGO(unit
->GetEntry(),unit
->GetGUID(),m_spellInfo
->Id
);
1038 if(((Creature
*)unit
)->AI())
1039 ((Creature
*)unit
)->AI()->SpellHit(m_caster
,m_spellInfo
);
1043 void Spell::DoSpellHitOnUnit(Unit
*unit
, const uint32 effectMask
)
1045 if(!unit
|| !effectMask
)
1048 // Recheck immune (only for delayed spells)
1049 if( m_spellInfo
->speed
&& (
1050 unit
->IsImmunedToDamage(GetSpellSchoolMask(m_spellInfo
),true) ||
1051 unit
->IsImmunedToSpell(m_spellInfo
,true) ))
1053 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_IMMUNE
);
1057 if (unit
->GetTypeId() == TYPEID_PLAYER
)
1059 ((Player
*)unit
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_BE_SPELL_TARGET
, m_spellInfo
->Id
);
1060 ((Player
*)unit
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_BE_SPELL_TARGET2
, m_spellInfo
->Id
);
1063 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
1065 ((Player
*)m_caster
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_CAST_SPELL2
, m_spellInfo
->Id
, 0, unit
);
1068 if( m_caster
!= unit
)
1070 if( !m_caster
->IsFriendlyTo(unit
) )
1072 // for delayed spells ignore not visible explicit target
1073 if(m_spellInfo
->speed
> 0.0f
&& unit
==m_targets
.getUnitTarget() && !unit
->isVisibleForOrDetect(m_caster
,false))
1075 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_EVADE
);
1079 // exclude Arcane Missiles Dummy Aura aura for now (attack on hit)
1080 // TODO: find way to not need this?
1081 if(!(m_spellInfo
->SpellFamilyName
== SPELLFAMILY_MAGE
&&
1082 m_spellInfo
->SpellFamilyFlags
& 0x800LL
))
1084 unit
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
1086 if( !(m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_NO_INITIAL_AGGRO
) )
1088 if(!unit
->IsStandState() && !unit
->hasUnitState(UNIT_STAT_STUNNED
))
1089 unit
->SetStandState(PLAYER_STATE_NONE
);
1091 if(!unit
->isInCombat() && unit
->GetTypeId() != TYPEID_PLAYER
&& ((Creature
*)unit
)->AI())
1092 ((Creature
*)unit
)->AI()->AttackStart(m_caster
);
1094 unit
->SetInCombatWith(m_caster
);
1095 m_caster
->SetInCombatWith(unit
);
1097 if(Player
*attackedPlayer
= unit
->GetCharmerOrOwnerPlayerOrPlayerItself())
1099 m_caster
->SetContestedPvP(attackedPlayer
);
1101 unit
->AddThreat(m_caster
, 0.0f
);
1107 // for delayed spells ignore negative spells (after duel end) for friendly targets
1108 if(m_spellInfo
->speed
> 0.0f
&& !IsPositiveSpell(m_spellInfo
->Id
))
1110 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_EVADE
);
1114 // assisting case, healing and resurrection
1115 if(unit
->hasUnitState(UNIT_STAT_ATTACK_PLAYER
))
1116 m_caster
->SetContestedPvP();
1117 if( unit
->isInCombat() && !(m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_NO_INITIAL_AGGRO
) )
1119 m_caster
->SetInCombatState(unit
->GetCombatTimer() > 0);
1120 unit
->getHostilRefManager().threatAssist(m_caster
, 0.0f
);
1125 // Get Data Needed for Diminishing Returns, some effects may have multiple auras, so this must be done on spell hit, not aura add
1126 m_diminishGroup
= GetDiminishingReturnsGroupForSpell(m_spellInfo
,m_triggeredByAuraSpell
);
1127 m_diminishLevel
= unit
->GetDiminishing(m_diminishGroup
);
1128 // Increase Diminishing on unit, current informations for actually casts will use values above
1129 if((GetDiminishingReturnsGroupType(m_diminishGroup
) == DRTYPE_PLAYER
&& unit
->GetTypeId() == TYPEID_PLAYER
) || GetDiminishingReturnsGroupType(m_diminishGroup
) == DRTYPE_ALL
)
1130 unit
->IncrDiminishing(m_diminishGroup
);
1132 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1134 if (effectMask
& (1<<effectNumber
))
1136 HandleEffects(unit
,NULL
,NULL
,effectNumber
,m_damageMultipliers
[effectNumber
]);
1137 if ( m_applyMultiplierMask
& (1 << effectNumber
) )
1140 float multiplier
= m_spellInfo
->DmgMultiplier
[effectNumber
];
1141 // Apply multiplier mods
1142 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1143 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_EFFECT_PAST_FIRST
, multiplier
,this);
1144 m_damageMultipliers
[effectNumber
] *= multiplier
;
1150 void Spell::DoAllEffectOnTarget(GOTargetInfo
*target
)
1152 if (target
->processed
) // Check target
1154 target
->processed
= true; // Target checked in apply effects procedure
1156 uint32 effectMask
= target
->effectMask
;
1160 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, target
->targetGUID
);
1164 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1165 if (effectMask
& (1<<effectNumber
))
1166 HandleEffects(NULL
,NULL
,go
,effectNumber
);
1168 // cast at creature (or GO) quest objectives update at successful cast finished (+channel finished)
1169 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
1170 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() && !IsChannelActive() )
1171 ((Player
*)m_caster
)->CastedCreatureOrGO(go
->GetEntry(),go
->GetGUID(),m_spellInfo
->Id
);
1174 void Spell::DoAllEffectOnTarget(ItemTargetInfo
*target
)
1176 uint32 effectMask
= target
->effectMask
;
1177 if(!target
->item
|| !effectMask
)
1180 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1181 if (effectMask
& (1<<effectNumber
))
1182 HandleEffects(NULL
, target
->item
, NULL
, effectNumber
);
1185 bool Spell::IsAliveUnitPresentInTargetList()
1187 // Not need check return true
1188 if (m_needAliveTargetMask
== 0)
1191 uint8 needAliveTargetMask
= m_needAliveTargetMask
;
1193 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
1195 if( ihit
->missCondition
== SPELL_MISS_NONE
&& (needAliveTargetMask
& ihit
->effectMask
) )
1197 Unit
*unit
= m_caster
->GetGUID()==ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
1199 if (unit
&& unit
->isAlive())
1200 needAliveTargetMask
&= ~ihit
->effectMask
; // remove from need alive mask effect that have alive target
1204 // is all effects from m_needAliveTargetMask have alive targets
1205 return needAliveTargetMask
==0;
1208 // Helper for Chain Healing
1209 // Spell target first
1210 // Raidmates then descending by injury suffered (MaxHealth - Health)
1211 // Other players/mobs then descending by injury suffered (MaxHealth - Health)
1212 struct ChainHealingOrder
: public std::binary_function
<const Unit
*, const Unit
*, bool>
1214 const Unit
* MainTarget
;
1215 ChainHealingOrder(Unit
const* Target
) : MainTarget(Target
) {};
1216 // functor for operator ">"
1217 bool operator()(Unit
const* _Left
, Unit
const* _Right
) const
1219 return (ChainHealingHash(_Left
) < ChainHealingHash(_Right
));
1221 int32
ChainHealingHash(Unit
const* Target
) const
1223 if (Target
== MainTarget
)
1225 else if (Target
->GetTypeId() == TYPEID_PLAYER
&& MainTarget
->GetTypeId() == TYPEID_PLAYER
&&
1226 ((Player
const*)Target
)->IsInSameRaidWith((Player
const*)MainTarget
))
1228 if (Target
->GetHealth() == Target
->GetMaxHealth())
1231 return 20000 - Target
->GetMaxHealth() + Target
->GetHealth();
1234 return 40000 - Target
->GetMaxHealth() + Target
->GetHealth();
1238 class ChainHealingFullHealth
: std::unary_function
<const Unit
*, bool>
1241 const Unit
* MainTarget
;
1242 ChainHealingFullHealth(const Unit
* Target
) : MainTarget(Target
) {};
1244 bool operator()(const Unit
* Target
)
1246 return (Target
!= MainTarget
&& Target
->GetHealth() == Target
->GetMaxHealth());
1250 // Helper for targets nearest to the spell target
1251 // The spell target is always first unless there is a target at _completely_ the same position (unbelievable case)
1252 struct TargetDistanceOrder
: public std::binary_function
<const Unit
, const Unit
, bool>
1254 const Unit
* MainTarget
;
1255 TargetDistanceOrder(const Unit
* Target
) : MainTarget(Target
) {};
1256 // functor for operator ">"
1257 bool operator()(const Unit
* _Left
, const Unit
* _Right
) const
1259 return (MainTarget
->GetDistance(_Left
) < MainTarget
->GetDistance(_Right
));
1263 void Spell::SetTargetMap(uint32 i
,uint32 cur
,std::list
<Unit
*> &TagUnitMap
)
1266 if (m_spellInfo
->EffectRadiusIndex
[i
])
1267 radius
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[i
]));
1269 radius
= GetSpellMaxRange(sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
));
1271 if(m_originalCaster
)
1272 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1273 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_RADIUS
, radius
,this);
1275 uint32 EffectChainTarget
= m_spellInfo
->EffectChainTarget
[i
];
1276 if(m_originalCaster
)
1277 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1278 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_JUMP_TARGETS
, EffectChainTarget
, this);
1280 // Get spell max affected targets
1281 uint32 unMaxTargets
= m_spellInfo
->MaxAffectedTargets
;
1282 Unit::AuraList
const& mod
= m_caster
->GetAurasByType(SPELL_AURA_MOD_MAX_AFFECTED_TARGETS
);
1283 for(Unit::AuraList::const_iterator m
= mod
.begin(); m
!= mod
.end(); ++m
)
1285 if (!(*m
)->isAffectedOnSpell(m_spellInfo
))
1287 unMaxTargets
+=(*m
)->GetModifier()->m_amount
;
1291 case TARGET_TOTEM_EARTH
:
1292 case TARGET_TOTEM_WATER
:
1293 case TARGET_TOTEM_AIR
:
1294 case TARGET_TOTEM_FIRE
:
1297 case TARGET_DYNAMIC_OBJECT
:
1298 case TARGET_AREAEFFECT_CUSTOM
:
1299 case TARGET_AREAEFFECT_CUSTOM_2
:
1302 TagUnitMap
.push_back(m_caster
);
1305 case TARGET_RANDOM_ENEMY_CHAIN_IN_AREA
:
1307 m_targets
.m_targetMask
= 0;
1308 unMaxTargets
= EffectChainTarget
;
1309 float max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1311 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1313 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1316 std::list
<Unit
*> tempUnitMap
;
1319 MaNGOS::AnyAoETargetUnitInObjectRangeCheck
u_check(m_caster
, m_caster
, max_range
);
1320 MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
> searcher(tempUnitMap
, u_check
);
1322 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_unit_searcher(searcher
);
1323 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1325 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1326 cell_lock
->Visit(cell_lock
, world_unit_searcher
, *m_caster
->GetMap());
1327 cell_lock
->Visit(cell_lock
, grid_unit_searcher
, *m_caster
->GetMap());
1330 if(tempUnitMap
.empty())
1333 tempUnitMap
.sort(TargetDistanceOrder(m_caster
));
1335 //Now to get us a random target that's in the initial range of the spell
1337 std::list
<Unit
*>::iterator itr
= tempUnitMap
.begin();
1338 while(itr
!= tempUnitMap
.end() && (*itr
)->GetDistance(m_caster
) < radius
)
1344 itr
= tempUnitMap
.begin();
1345 std::advance(itr
, rand()%t
);
1346 Unit
*pUnitTarget
= *itr
;
1347 TagUnitMap
.push_back(pUnitTarget
);
1349 tempUnitMap
.erase(itr
);
1351 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1353 t
= unMaxTargets
- 1;
1354 Unit
*prev
= pUnitTarget
;
1355 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1357 while(t
&& next
!= tempUnitMap
.end() )
1359 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1362 if(!prev
->IsWithinLOSInMap(*next
))
1369 TagUnitMap
.push_back(prev
);
1370 tempUnitMap
.erase(next
);
1371 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1372 next
= tempUnitMap
.begin();
1379 Pet
* tmpUnit
= m_caster
->GetPet();
1380 if (!tmpUnit
) break;
1381 TagUnitMap
.push_back(tmpUnit
);
1384 case TARGET_CHAIN_DAMAGE
:
1386 if (EffectChainTarget
<= 1)
1388 Unit
* pUnitTarget
= SelectMagnetTarget();
1390 TagUnitMap
.push_back(pUnitTarget
);
1394 Unit
* pUnitTarget
= m_targets
.getUnitTarget();
1398 unMaxTargets
= EffectChainTarget
;
1401 if(m_spellInfo
->DmgClass
==SPELL_DAMAGE_CLASS_MELEE
)
1402 max_range
= radius
; //
1404 //FIXME: This very like horrible hack and wrong for most spells
1405 max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1407 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1409 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1412 Unit
* originalCaster
= GetOriginalCaster();
1415 std::list
<Unit
*> tempUnitMap
;
1418 MaNGOS::AnyAoETargetUnitInObjectRangeCheck
u_check(pUnitTarget
, originalCaster
, max_range
);
1419 MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
> searcher(tempUnitMap
, u_check
);
1421 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_unit_searcher(searcher
);
1422 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1424 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1425 cell_lock
->Visit(cell_lock
, world_unit_searcher
, *m_caster
->GetMap());
1426 cell_lock
->Visit(cell_lock
, grid_unit_searcher
, *m_caster
->GetMap());
1429 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1431 if(tempUnitMap
.empty())
1434 if(*tempUnitMap
.begin() == pUnitTarget
)
1435 tempUnitMap
.erase(tempUnitMap
.begin());
1437 TagUnitMap
.push_back(pUnitTarget
);
1438 uint32 t
= unMaxTargets
- 1;
1439 Unit
*prev
= pUnitTarget
;
1440 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1442 while(t
&& next
!= tempUnitMap
.end() )
1444 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1447 if(!prev
->IsWithinLOSInMap(*next
))
1454 TagUnitMap
.push_back(prev
);
1455 tempUnitMap
.erase(next
);
1456 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1457 next
= tempUnitMap
.begin();
1464 case TARGET_ALL_ENEMY_IN_AREA
:
1467 case TARGET_ALL_ENEMY_IN_AREA_INSTANT
:
1469 // targets the ground, not the units in the area
1470 if (m_spellInfo
->Effect
[i
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
1472 CellPair
p(MaNGOS::ComputeCellPair(m_targets
.m_destX
, m_targets
.m_destY
));
1474 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1477 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1479 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1480 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1482 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1483 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1484 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1486 // exclude caster (this can be important if this not original caster)
1487 TagUnitMap
.remove(m_caster
);
1490 case TARGET_DUELVSPLAYER_COORDINATES
:
1492 if(Unit
* currentTarget
= m_targets
.getUnitTarget())
1494 m_targets
.setDestination(currentTarget
->GetPositionX(), currentTarget
->GetPositionY(), currentTarget
->GetPositionZ());
1495 TagUnitMap
.push_back(currentTarget
);
1498 case TARGET_ALL_PARTY_AROUND_CASTER
:
1499 case TARGET_ALL_PARTY_AROUND_CASTER_2
:
1500 case TARGET_ALL_PARTY
:
1502 Player
*pTarget
= m_caster
->GetCharmerOrOwnerPlayerOrPlayerItself();
1503 Group
*pGroup
= pTarget
? pTarget
->GetGroup() : NULL
;
1507 uint8 subgroup
= pTarget
->GetSubGroup();
1509 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1511 Player
* Target
= itr
->getSource();
1513 // IsHostileTo check duel and controlled by enemy
1514 if( Target
&& Target
->GetSubGroup()==subgroup
&& !m_caster
->IsHostileTo(Target
) )
1516 if( m_caster
->IsWithinDistInMap(Target
, radius
) )
1517 TagUnitMap
.push_back(Target
);
1519 if(Pet
* pet
= Target
->GetPet())
1520 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1521 TagUnitMap
.push_back(pet
);
1527 Unit
* ownerOrSelf
= pTarget
? pTarget
: m_caster
->GetCharmerOrOwnerOrSelf();
1528 if(ownerOrSelf
==m_caster
|| m_caster
->IsWithinDistInMap(ownerOrSelf
, radius
))
1529 TagUnitMap
.push_back(ownerOrSelf
);
1530 if(Pet
* pet
= ownerOrSelf
->GetPet())
1531 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1532 TagUnitMap
.push_back(pet
);
1535 case TARGET_RANDOM_RAID_MEMBER
:
1537 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1538 if(Player
* target
= ((Player
*)m_caster
)->GetNextRandomRaidMember(radius
))
1539 TagUnitMap
.push_back(target
);
1541 case TARGET_SINGLE_FRIEND
:
1542 case TARGET_SINGLE_FRIEND_2
:
1544 if(m_targets
.getUnitTarget())
1545 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1547 case TARGET_NONCOMBAT_PET
:
1549 if(Unit
* target
= m_targets
.getUnitTarget())
1550 if( target
->GetTypeId() == TYPEID_UNIT
&& ((Creature
*)target
)->isPet() && ((Pet
*)target
)->getPetType() == MINI_PET
)
1551 TagUnitMap
.push_back(target
);
1553 case TARGET_ALL_AROUND_CASTER
:
1555 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1557 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1560 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_SELF_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1562 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1563 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1565 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1566 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1567 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1569 case TARGET_ALL_FRIENDLY_UNITS_AROUND_CASTER
:
1571 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1573 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1576 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_SELF_CENTER
,SPELL_TARGETS_FRIENDLY
);
1578 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1579 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1581 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1582 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1583 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1585 case TARGET_ALL_FRIENDLY_UNITS_IN_AREA
:
1587 CellPair
p(MaNGOS::ComputeCellPair(m_targets
.m_destX
, m_targets
.m_destY
));
1589 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1592 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_FRIENDLY
);
1594 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1595 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1597 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1598 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1599 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1601 // 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..)
1602 case TARGET_SINGLE_PARTY
:
1604 Unit
*target
= m_targets
.getUnitTarget();
1605 // Thoses spells apparently can't be casted on the caster.
1606 if( target
&& target
!= m_caster
)
1608 // Can only be casted on group's members or its pets
1609 Group
*pGroup
= NULL
;
1611 Unit
* owner
= m_caster
->GetCharmerOrOwner();
1612 Unit
*targetOwner
= target
->GetCharmerOrOwner();
1615 if(owner
->GetTypeId() == TYPEID_PLAYER
)
1617 if( target
== owner
)
1619 TagUnitMap
.push_back(target
);
1622 pGroup
= ((Player
*)owner
)->GetGroup();
1625 else if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1627 if( targetOwner
== m_caster
&& target
->GetTypeId()==TYPEID_UNIT
&& ((Creature
*)target
)->isPet())
1629 TagUnitMap
.push_back(target
);
1632 pGroup
= ((Player
*)m_caster
)->GetGroup();
1637 // Our target can also be a player's pet who's grouped with us or our pet. But can't be controlled player
1640 if( targetOwner
->GetTypeId() == TYPEID_PLAYER
&&
1641 target
->GetTypeId()==TYPEID_UNIT
&& (((Creature
*)target
)->isPet()) &&
1642 target
->GetOwnerGUID()==targetOwner
->GetGUID() &&
1643 pGroup
->IsMember(((Player
*)targetOwner
)->GetGUID()))
1645 TagUnitMap
.push_back(target
);
1648 // 1Our target can be a player who is on our group
1649 else if (target
->GetTypeId() == TYPEID_PLAYER
&& pGroup
->IsMember(((Player
*)target
)->GetGUID()))
1651 TagUnitMap
.push_back(target
);
1656 case TARGET_GAMEOBJECT
:
1658 if(m_targets
.getGOTarget())
1659 AddGOTarget(m_targets
.getGOTarget(), i
);
1661 case TARGET_IN_FRONT_OF_CASTER
:
1663 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1665 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1668 bool inFront
= m_spellInfo
->SpellVisual
[0] != 3879;
1669 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, inFront
? PUSH_IN_FRONT
: PUSH_IN_BACK
,SPELL_TARGETS_AOE_DAMAGE
);
1671 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1672 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1674 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1675 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1676 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1678 case TARGET_DUELVSPLAYER
:
1680 Unit
*target
= m_targets
.getUnitTarget();
1683 if(m_caster
->IsFriendlyTo(target
))
1685 TagUnitMap
.push_back(target
);
1689 Unit
* pUnitTarget
= SelectMagnetTarget();
1691 TagUnitMap
.push_back(pUnitTarget
);
1695 case TARGET_GAMEOBJECT_ITEM
:
1697 if(m_targets
.getGOTargetGUID())
1698 AddGOTarget(m_targets
.getGOTarget(), i
);
1699 else if(m_targets
.getItemTarget())
1700 AddItemTarget(m_targets
.getItemTarget(), i
);
1705 if(Unit
* owner
= m_caster
->GetCharmerOrOwner())
1706 TagUnitMap
.push_back(owner
);
1709 case TARGET_ALL_ENEMY_IN_AREA_CHANNELED
:
1711 // targets the ground, not the units in the area
1712 if (m_spellInfo
->Effect
[i
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
1714 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1716 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1719 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1721 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1722 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1724 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1725 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1726 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1731 if(m_spellInfo
->Effect
[i
] != SPELL_EFFECT_DUEL
)
1732 TagUnitMap
.push_back(m_caster
);
1734 case TARGET_SINGLE_ENEMY
:
1736 Unit
* pUnitTarget
= SelectMagnetTarget();
1738 TagUnitMap
.push_back(pUnitTarget
);
1740 case TARGET_AREAEFFECT_PARTY
:
1742 Unit
* owner
= m_caster
->GetCharmerOrOwner();
1743 Player
*pTarget
= NULL
;
1747 TagUnitMap
.push_back(m_caster
);
1748 if(owner
->GetTypeId() == TYPEID_PLAYER
)
1749 pTarget
= (Player
*)owner
;
1751 else if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1753 if(Unit
* target
= m_targets
.getUnitTarget())
1755 if( target
->GetTypeId() != TYPEID_PLAYER
)
1757 if(((Creature
*)target
)->isPet())
1759 Unit
*targetOwner
= target
->GetOwner();
1760 if(targetOwner
->GetTypeId() == TYPEID_PLAYER
)
1761 pTarget
= (Player
*)targetOwner
;
1765 pTarget
= (Player
*)target
;
1769 Group
* pGroup
= pTarget
? pTarget
->GetGroup() : NULL
;
1773 uint8 subgroup
= pTarget
->GetSubGroup();
1775 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1777 Player
* Target
= itr
->getSource();
1779 // IsHostileTo check duel and controlled by enemy
1780 if(Target
&& Target
->GetSubGroup()==subgroup
&& !m_caster
->IsHostileTo(Target
))
1782 if( pTarget
->IsWithinDistInMap(Target
, radius
) )
1783 TagUnitMap
.push_back(Target
);
1785 if(Pet
* pet
= Target
->GetPet())
1786 if( pTarget
->IsWithinDistInMap(pet
, radius
) )
1787 TagUnitMap
.push_back(pet
);
1793 if(m_caster
->IsWithinDistInMap(owner
, radius
))
1794 TagUnitMap
.push_back(owner
);
1798 TagUnitMap
.push_back(pTarget
);
1800 if(Pet
* pet
= pTarget
->GetPet())
1801 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1802 TagUnitMap
.push_back(pet
);
1808 if(m_targets
.getUnitTarget())
1809 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1810 if(m_targets
.getItemTarget())
1811 AddItemTarget(m_targets
.getItemTarget(), i
);
1813 case TARGET_SELF_FISHING
:
1815 TagUnitMap
.push_back(m_caster
);
1817 case TARGET_CHAIN_HEAL
:
1819 Unit
* pUnitTarget
= m_targets
.getUnitTarget();
1823 if (EffectChainTarget
<= 1)
1824 TagUnitMap
.push_back(pUnitTarget
);
1827 unMaxTargets
= EffectChainTarget
;
1828 float max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1830 std::list
<Unit
*> tempUnitMap
;
1833 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1835 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1838 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, tempUnitMap
, max_range
, PUSH_SELF_CENTER
, SPELL_TARGETS_FRIENDLY
);
1840 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1841 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1843 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1844 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1845 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1849 if(m_caster
!= pUnitTarget
&& std::find(tempUnitMap
.begin(),tempUnitMap
.end(),m_caster
) == tempUnitMap
.end() )
1850 tempUnitMap
.push_front(m_caster
);
1852 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1854 if(tempUnitMap
.empty())
1857 if(*tempUnitMap
.begin() == pUnitTarget
)
1858 tempUnitMap
.erase(tempUnitMap
.begin());
1860 TagUnitMap
.push_back(pUnitTarget
);
1861 uint32 t
= unMaxTargets
- 1;
1862 Unit
*prev
= pUnitTarget
;
1863 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1865 while(t
&& next
!= tempUnitMap
.end() )
1867 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1870 if(!prev
->IsWithinLOSInMap(*next
))
1876 if((*next
)->GetHealth() == (*next
)->GetMaxHealth())
1878 next
= tempUnitMap
.erase(next
);
1883 TagUnitMap
.push_back(prev
);
1884 tempUnitMap
.erase(next
);
1885 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1886 next
= tempUnitMap
.begin();
1892 case TARGET_CURRENT_ENEMY_COORDINATES
:
1894 Unit
* currentTarget
= m_targets
.getUnitTarget();
1897 TagUnitMap
.push_back(currentTarget
);
1898 m_targets
.setDestination(currentTarget
->GetPositionX(), currentTarget
->GetPositionY(), currentTarget
->GetPositionZ());
1899 if(m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_ENEMY_IN_AREA_INSTANT
)
1901 CellPair
p(MaNGOS::ComputeCellPair(currentTarget
->GetPositionX(), currentTarget
->GetPositionY()));
1903 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1905 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
,PUSH_TARGET_CENTER
, SPELL_TARGETS_AOE_DAMAGE
);
1906 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_notifier(notifier
);
1907 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_notifier(notifier
);
1908 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1909 cell_lock
->Visit(cell_lock
, world_notifier
, *m_caster
->GetMap());
1910 cell_lock
->Visit(cell_lock
, grid_notifier
, *m_caster
->GetMap());
1914 case TARGET_AREAEFFECT_PARTY_AND_CLASS
:
1916 Player
* targetPlayer
= m_targets
.getUnitTarget() && m_targets
.getUnitTarget()->GetTypeId() == TYPEID_PLAYER
1917 ? (Player
*)m_targets
.getUnitTarget() : NULL
;
1919 Group
* pGroup
= targetPlayer
? targetPlayer
->GetGroup() : NULL
;
1922 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1924 Player
* Target
= itr
->getSource();
1926 // IsHostileTo check duel and controlled by enemy
1927 if( Target
&& targetPlayer
->IsWithinDistInMap(Target
, radius
) &&
1928 targetPlayer
->getClass() == Target
->getClass() &&
1929 !m_caster
->IsHostileTo(Target
) )
1931 TagUnitMap
.push_back(Target
);
1935 else if(m_targets
.getUnitTarget())
1936 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1939 case TARGET_TABLE_X_Y_Z_COORDINATES
:
1941 SpellTargetPosition
const* st
= spellmgr
.GetSpellTargetPosition(m_spellInfo
->Id
);
1944 if (st
->target_mapId
== m_caster
->GetMapId())
1945 m_targets
.setDestination(st
->target_X
, st
->target_Y
, st
->target_Z
);
1947 // if B==TARGET_TABLE_X_Y_Z_COORDINATES then A already fill all required targets
1948 if (m_spellInfo
->EffectImplicitTargetB
[i
] && m_spellInfo
->EffectImplicitTargetB
[i
]!=TARGET_TABLE_X_Y_Z_COORDINATES
)
1950 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1952 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1955 SpellTargets targetB
= SPELL_TARGETS_AOE_DAMAGE
;
1956 // Select friendly targets for positive effect
1957 if (IsPositiveEffect(m_spellInfo
->Id
, i
))
1958 targetB
= SPELL_TARGETS_FRIENDLY
;
1960 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
,PUSH_DEST_CENTER
, targetB
);
1962 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_notifier(notifier
);
1963 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_notifier(notifier
);
1965 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1966 cell_lock
->Visit(cell_lock
, world_notifier
, *m_caster
->GetMap());
1967 cell_lock
->Visit(cell_lock
, grid_notifier
, *m_caster
->GetMap());
1971 sLog
.outError( "SPELL: unknown target coordinates for spell ID %u\n", m_spellInfo
->Id
);
1973 case TARGET_BEHIND_VICTIM
:
1975 Unit
*pTarget
= m_caster
->getVictim();
1976 if(!pTarget
&& m_caster
->GetTypeId() == TYPEID_PLAYER
)
1977 pTarget
= ObjectAccessor::GetUnit(*m_caster
, ((Player
*)m_caster
)->GetSelection());
1981 float _target_x
, _target_y
, _target_z
;
1982 pTarget
->GetClosePoint(_target_x
, _target_y
, _target_z
, m_caster
->GetObjectSize(), CONTACT_DISTANCE
, M_PI
);
1983 if(pTarget
->IsWithinLOS(_target_x
,_target_y
,_target_z
))
1984 m_targets
.setDestination(_target_x
, _target_y
, _target_z
);
1991 if (unMaxTargets
&& TagUnitMap
.size() > unMaxTargets
)
1993 // make sure one unit is always removed per iteration
1994 uint32 removed_utarget
= 0;
1995 for (std::list
<Unit
*>::iterator itr
= TagUnitMap
.begin(), next
; itr
!= TagUnitMap
.end(); itr
= next
)
1999 if (!*itr
) continue;
2000 if ((*itr
) == m_targets
.getUnitTarget())
2002 TagUnitMap
.erase(itr
);
2003 removed_utarget
= 1;
2007 // remove random units from the map
2008 while (TagUnitMap
.size() > unMaxTargets
- removed_utarget
)
2010 uint32 poz
= urand(0, TagUnitMap
.size()-1);
2011 for (std::list
<Unit
*>::iterator itr
= TagUnitMap
.begin(); itr
!= TagUnitMap
.end(); ++itr
, --poz
)
2013 if (!*itr
) continue;
2016 TagUnitMap
.erase(itr
);
2021 // the player's target will always be added to the map
2022 if (removed_utarget
&& m_targets
.getUnitTarget())
2023 TagUnitMap
.push_back(m_targets
.getUnitTarget());
2027 void Spell::prepare(SpellCastTargets
* targets
, Aura
* triggeredByAura
)
2029 m_targets
= *targets
;
2031 m_spellState
= SPELL_STATE_PREPARING
;
2033 m_castPositionX
= m_caster
->GetPositionX();
2034 m_castPositionY
= m_caster
->GetPositionY();
2035 m_castPositionZ
= m_caster
->GetPositionZ();
2036 m_castOrientation
= m_caster
->GetOrientation();
2039 m_triggeredByAuraSpell
= triggeredByAura
->GetSpellProto();
2041 // create and add update event for this spell
2042 SpellEvent
* Event
= new SpellEvent(this);
2043 m_caster
->m_Events
.AddEvent(Event
, m_caster
->m_Events
.CalculateTime(1));
2045 //Prevent casting at cast another spell (ServerSide check)
2046 if(m_caster
->IsNonMeleeSpellCasted(false, true) && m_cast_count
)
2048 SendCastResult(SPELL_FAILED_SPELL_IN_PROGRESS
);
2054 m_powerCost
= CalculatePowerCost();
2056 uint8 result
= CanCast(true);
2057 if(result
!= 0 && !IsAutoRepeat()) //always cast autorepeat dummy for triggering
2061 SendChannelUpdate(0);
2062 triggeredByAura
->SetAuraDuration(0);
2064 SendCastResult(result
);
2069 // Prepare data for triggers
2070 prepareDataForTriggerSystem();
2072 // calculate cast time (calculated after first CanCast check to prevent charge counting for first CanCast fail)
2073 m_casttime
= GetSpellCastTime(m_spellInfo
, this);
2075 // set timer base at cast time
2078 // stealth must be removed at cast starting (at show channel bar)
2079 // skip triggered spell (item equip spell casting and other not explicit character casts/item uses)
2080 if ( !m_IsTriggeredSpell
&& isSpellBreakStealth(m_spellInfo
) )
2082 m_caster
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
2083 m_caster
->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH
);
2086 if(m_IsTriggeredSpell
)
2090 m_caster
->SetCurrentCastedSpell( this );
2091 m_selfContainer
= &(m_caster
->m_currentSpells
[GetCurrentContainer()]);
2096 void Spell::cancel()
2098 if(m_spellState
== SPELL_STATE_FINISHED
)
2101 m_autoRepeat
= false;
2102 switch (m_spellState
)
2104 case SPELL_STATE_PREPARING
:
2105 case SPELL_STATE_DELAYED
:
2108 SendCastResult(SPELL_FAILED_INTERRUPTED
);
2111 case SPELL_STATE_CASTING
:
2113 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2115 if( ihit
->missCondition
== SPELL_MISS_NONE
)
2117 Unit
* unit
= m_caster
->GetGUID()==(*ihit
).targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
2118 if( unit
&& unit
->isAlive() )
2119 unit
->RemoveAurasDueToSpell(m_spellInfo
->Id
);
2123 m_caster
->RemoveAurasDueToSpell(m_spellInfo
->Id
);
2124 SendChannelUpdate(0);
2126 SendCastResult(SPELL_FAILED_INTERRUPTED
);
2135 m_caster
->RemoveDynObject(m_spellInfo
->Id
);
2136 m_caster
->RemoveGameObject(m_spellInfo
->Id
,true);
2139 void Spell::cast(bool skipCheck
)
2141 SetExecutedCurrently(true);
2143 uint8 castResult
= 0;
2145 // update pointers base at GUIDs to prevent access to non-existed already object
2148 // cancel at lost main target unit
2149 if(!m_targets
.getUnitTarget() && m_targets
.getUnitTargetGUID() && m_targets
.getUnitTargetGUID() != m_caster
->GetGUID())
2152 SetExecutedCurrently(false);
2156 if(m_caster
->GetTypeId() != TYPEID_PLAYER
&& m_targets
.getUnitTarget() && m_targets
.getUnitTarget() != m_caster
)
2157 m_caster
->SetInFront(m_targets
.getUnitTarget());
2159 castResult
= CheckPower();
2162 SendCastResult(castResult
);
2164 SetExecutedCurrently(false);
2168 // triggered cast called from Spell::prepare where it was already checked
2171 castResult
= CanCast(false);
2174 SendCastResult(castResult
);
2176 SetExecutedCurrently(false);
2181 // Conflagrate - consumes immolate
2182 if ((m_spellInfo
->TargetAuraState
== AURA_STATE_IMMOLATE
) && m_targets
.getUnitTarget())
2184 // for caster applied auras only
2185 Unit::AuraList
const &mPeriodic
= m_targets
.getUnitTarget()->GetAurasByType(SPELL_AURA_PERIODIC_DAMAGE
);
2186 for(Unit::AuraList::const_iterator i
= mPeriodic
.begin(); i
!= mPeriodic
.end(); ++i
)
2188 if( (*i
)->GetSpellProto()->SpellFamilyName
== SPELLFAMILY_WARLOCK
&& ((*i
)->GetSpellProto()->SpellFamilyFlags
& 4) &&
2189 (*i
)->GetCasterGUID()==m_caster
->GetGUID() )
2191 m_targets
.getUnitTarget()->RemoveAura((*i
)->GetId(), (*i
)->GetEffIndex());
2197 // traded items have trade slot instead of guid in m_itemTargetGUID
2198 // set to real guid to be sent later to the client
2199 m_targets
.updateTradeSlotItem();
2201 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2204 ((Player
*)m_caster
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_USE_ITEM
, m_CastItem
->GetEntry());
2206 ((Player
*)m_caster
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_CAST_SPELL
, m_spellInfo
->Id
);
2210 SendSpellCooldown();
2213 TakeReagents(); // we must remove reagents before HandleEffects to allow place crafted item in same slot
2216 if(m_spellState
== SPELL_STATE_FINISHED
) // stop cast if spell marked as finish somewhere in Take*/FillTargetMap
2218 SetExecutedCurrently(false);
2222 SendCastResult(castResult
);
2223 SendSpellGo(); // we must send smsg_spell_go packet before m_castItem delete in TakeCastItem()...
2225 // Okay, everything is prepared. Now we need to distinguish between immediate and evented delayed spells
2226 if (m_spellInfo
->speed
> 0.0f
)
2229 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2230 // in case delayed spell remove item at cast delay start
2233 // Okay, maps created, now prepare flags
2234 m_immediateHandled
= false;
2235 m_spellState
= SPELL_STATE_DELAYED
;
2240 // Immediate spell, no big deal
2244 SetExecutedCurrently(false);
2247 void Spell::handle_immediate()
2249 // start channeling if applicable
2250 if(IsChanneledSpell(m_spellInfo
))
2252 m_spellState
= SPELL_STATE_CASTING
;
2253 SendChannelStart(GetSpellDuration(m_spellInfo
));
2256 // process immediate effects (items, ground, etc.) also initialize some variables
2257 _handle_immediate_phase();
2259 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2260 DoAllEffectOnTarget(&(*ihit
));
2262 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
2263 DoAllEffectOnTarget(&(*ihit
));
2265 // spell is finished, perform some last features of the spell here
2266 _handle_finish_phase();
2268 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2271 if(m_spellState
!= SPELL_STATE_CASTING
)
2272 finish(true); // successfully finish spell cast (not last in case autorepeat or channel spell)
2275 uint64
Spell::handle_delayed(uint64 t_offset
)
2277 uint64 next_time
= 0;
2279 if (!m_immediateHandled
)
2281 _handle_immediate_phase();
2282 m_immediateHandled
= true;
2285 // 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)
2286 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2288 if (ihit
->processed
== false)
2290 if ( ihit
->timeDelay
<= t_offset
)
2291 DoAllEffectOnTarget(&(*ihit
));
2292 else if( next_time
== 0 || ihit
->timeDelay
< next_time
)
2293 next_time
= ihit
->timeDelay
;
2297 // now recheck gameobject targeting correctness
2298 for(std::list
<GOTargetInfo
>::iterator ighit
= m_UniqueGOTargetInfo
.begin(); ighit
!= m_UniqueGOTargetInfo
.end();++ighit
)
2300 if (ighit
->processed
== false)
2302 if ( ighit
->timeDelay
<= t_offset
)
2303 DoAllEffectOnTarget(&(*ighit
));
2304 else if( next_time
== 0 || ighit
->timeDelay
< next_time
)
2305 next_time
= ighit
->timeDelay
;
2308 // All targets passed - need finish phase
2311 // spell is finished, perform some last features of the spell here
2312 _handle_finish_phase();
2314 finish(true); // successfully finish spell cast
2316 // return zero, spell is finished now
2321 // spell is unfinished, return next execution time
2326 void Spell::_handle_immediate_phase()
2328 // handle some immediate features of the spell here
2329 HandleThreatSpells(m_spellInfo
->Id
);
2331 m_needSpellLog
= IsNeedSendToClient();
2332 for(uint32 j
= 0;j
<3;j
++)
2334 if(m_spellInfo
->Effect
[j
]==0)
2337 // apply Send Event effect to ground in case empty target lists
2338 if( m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SEND_EVENT
&& !HaveTargetsForEffect(j
) )
2340 HandleEffects(NULL
,NULL
,NULL
, j
);
2344 // Don't do spell log, if is school damage spell
2345 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SCHOOL_DAMAGE
|| m_spellInfo
->Effect
[j
] == 0)
2346 m_needSpellLog
= false;
2348 uint32 EffectChainTarget
= m_spellInfo
->EffectChainTarget
[j
];
2349 if(m_originalCaster
)
2350 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
2351 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_JUMP_TARGETS
, EffectChainTarget
, this);
2353 // initialize multipliers
2354 m_damageMultipliers
[j
] = 1.0f
;
2355 if( (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_DAMAGE
|| m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_HEAL
) &&
2356 (EffectChainTarget
> 1) )
2357 m_applyMultiplierMask
|= 1 << j
;
2360 // initialize Diminishing Returns Data
2361 m_diminishLevel
= DIMINISHING_LEVEL_1
;
2362 m_diminishGroup
= DIMINISHING_NONE
;
2365 for(std::list
<ItemTargetInfo
>::iterator ihit
= m_UniqueItemInfo
.begin();ihit
!= m_UniqueItemInfo
.end();++ihit
)
2366 DoAllEffectOnTarget(&(*ihit
));
2369 for(uint32 j
= 0;j
<3;j
++)
2371 // persistent area auras target only the ground
2372 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_PERSISTENT_AREA_AURA
)
2373 HandleEffects(NULL
,NULL
,NULL
, j
);
2377 void Spell::_handle_finish_phase()
2384 void Spell::SendSpellCooldown()
2386 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
2389 Player
* _player
= (Player
*)m_caster
;
2390 // Add cooldown for max (disable spell)
2391 // Cooldown started on SendCooldownEvent call
2392 if (m_spellInfo
->Attributes
& SPELL_ATTR_DISABLED_WHILE_ACTIVE
)
2394 _player
->AddSpellCooldown(m_spellInfo
->Id
, 0, time(NULL
) - 1);
2398 // init cooldown values
2403 // some special item spells without correct cooldown in SpellInfo
2404 // cooldown information stored in item prototype
2405 // This used in same way in WorldSession::HandleItemQuerySingleOpcode data sending to client.
2409 ItemPrototype
const* proto
= m_CastItem
->GetProto();
2412 for(int idx
= 0; idx
< 5; ++idx
)
2414 if(proto
->Spells
[idx
].SpellId
== m_spellInfo
->Id
)
2416 cat
= proto
->Spells
[idx
].SpellCategory
;
2417 rec
= proto
->Spells
[idx
].SpellCooldown
;
2418 catrec
= proto
->Spells
[idx
].SpellCategoryCooldown
;
2425 // if no cooldown found above then base at DBC data
2426 if(rec
< 0 && catrec
< 0)
2428 cat
= m_spellInfo
->Category
;
2429 rec
= m_spellInfo
->RecoveryTime
;
2430 catrec
= m_spellInfo
->CategoryRecoveryTime
;
2433 // shoot spells used equipped item cooldown values already assigned in GetAttackTime(RANGED_ATTACK)
2434 // prevent 0 cooldowns set by another way
2435 if (rec
<= 0 && catrec
<= 0 && (cat
== 76 || m_spellInfo
->Id
!= SPELL_ID_AUTOSHOT
))
2436 rec
= _player
->GetAttackTime(RANGED_ATTACK
);
2438 // Now we have cooldown data (if found any), time to apply mods
2440 _player
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COOLDOWN
, rec
, this);
2443 _player
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COOLDOWN
, catrec
, this);
2445 // replace negative cooldowns by 0
2446 if (rec
< 0) rec
= 0;
2447 if (catrec
< 0) catrec
= 0;
2449 // no cooldown after applying spell mods
2450 if( rec
== 0 && catrec
== 0)
2453 time_t curTime
= time(NULL
);
2455 time_t catrecTime
= catrec
? curTime
+catrec
/1000 : 0; // in secs
2456 time_t recTime
= rec
? curTime
+rec
/1000 : catrecTime
;// in secs
2458 // self spell cooldown
2460 _player
->AddSpellCooldown(m_spellInfo
->Id
, m_CastItem
? m_CastItem
->GetEntry() : 0, recTime
);
2465 SpellCategoryStore::const_iterator i_scstore
= sSpellCategoryStore
.find(cat
);
2466 if(i_scstore
!= sSpellCategoryStore
.end())
2468 for(SpellCategorySet::const_iterator i_scset
= i_scstore
->second
.begin(); i_scset
!= i_scstore
->second
.end(); ++i_scset
)
2470 if(*i_scset
== m_spellInfo
->Id
) // skip main spell, already handled above
2473 _player
->AddSpellCooldown(m_spellInfo
->Id
, m_CastItem
? m_CastItem
->GetEntry() : 0, catrecTime
);
2479 void Spell::update(uint32 difftime
)
2481 // update pointers based at it's GUIDs
2484 if(m_targets
.getUnitTargetGUID() && !m_targets
.getUnitTarget())
2490 // check if the player caster has moved before the spell finished
2491 if ((m_caster
->GetTypeId() == TYPEID_PLAYER
&& m_timer
!= 0) &&
2492 (m_castPositionX
!= m_caster
->GetPositionX() || m_castPositionY
!= m_caster
->GetPositionY() || m_castPositionZ
!= m_caster
->GetPositionZ()) &&
2493 (m_spellInfo
->Effect
[0] != SPELL_EFFECT_STUCK
|| !m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_FALLING
)))
2495 // always cancel for channeled spells
2496 if( m_spellState
== SPELL_STATE_CASTING
)
2498 // don't cancel for melee, autorepeat, triggered and instant spells
2499 else if(!IsNextMeleeSwingSpell() && !IsAutoRepeat() && !m_IsTriggeredSpell
&& (m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_MOVEMENT
))
2503 switch(m_spellState
)
2505 case SPELL_STATE_PREPARING
:
2509 if(difftime
>= m_timer
)
2512 m_timer
-= difftime
;
2515 if(m_timer
== 0 && !IsNextMeleeSwingSpell() && !IsAutoRepeat())
2518 case SPELL_STATE_CASTING
:
2522 if( m_caster
->GetTypeId() == TYPEID_PLAYER
)
2524 // check if player has jumped before the channeling finished
2525 if(m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_JUMPING
))
2528 // check for incapacitating player states
2529 if( m_caster
->hasUnitState(UNIT_STAT_STUNNED
| UNIT_STAT_CONFUSED
))
2532 // check if player has turned if flag is set
2533 if( m_spellInfo
->ChannelInterruptFlags
& CHANNEL_FLAG_TURNING
&& m_castOrientation
!= m_caster
->GetOrientation() )
2537 // check if there are alive targets left
2538 if (!IsAliveUnitPresentInTargetList())
2540 SendChannelUpdate(0);
2544 if(difftime
>= m_timer
)
2547 m_timer
-= difftime
;
2552 SendChannelUpdate(0);
2554 // channeled spell processed independently for quest targeting
2555 // cast at creature (or GO) quest objectives update at successful cast channel finished
2556 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
2557 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() )
2559 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2561 TargetInfo
* target
= &*ihit
;
2562 if(!IS_CREATURE_GUID(target
->targetGUID
))
2565 Unit
* unit
= m_caster
->GetGUID()==target
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
,target
->targetGUID
);
2569 ((Player
*)m_caster
)->CastedCreatureOrGO(unit
->GetEntry(),unit
->GetGUID(),m_spellInfo
->Id
);
2572 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
2574 GOTargetInfo
* target
= &*ihit
;
2576 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, target
->targetGUID
);
2580 ((Player
*)m_caster
)->CastedCreatureOrGO(go
->GetEntry(),go
->GetGUID(),m_spellInfo
->Id
);
2593 void Spell::finish(bool ok
)
2598 if(m_spellState
== SPELL_STATE_FINISHED
)
2601 m_spellState
= SPELL_STATE_FINISHED
;
2603 // other code related only to successfully finished spells
2608 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2609 ((Player
*)m_caster
)->RemoveSpellMods(this);
2611 //handle SPELL_AURA_ADD_TARGET_TRIGGER auras
2612 Unit::AuraList
const& targetTriggers
= m_caster
->GetAurasByType(SPELL_AURA_ADD_TARGET_TRIGGER
);
2613 for(Unit::AuraList::const_iterator i
= targetTriggers
.begin(); i
!= targetTriggers
.end(); ++i
)
2615 SpellEntry
const *auraSpellInfo
= (*i
)->GetSpellProto();
2616 uint32 auraSpellIdx
= (*i
)->GetEffIndex();
2617 if (IsAffectedByAura((*i
)))
2619 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2620 if( ihit
->effectMask
& (1<<auraSpellIdx
) )
2622 // check m_caster->GetGUID() let load auras at login and speedup most often case
2623 Unit
*unit
= m_caster
->GetGUID()== ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
2624 if (unit
&& unit
->isAlive())
2626 // Calculate chance at that moment (can be depend for example from combo points)
2627 int32 chance
= m_caster
->CalculateSpellDamage(auraSpellInfo
, auraSpellIdx
, (*i
)->GetBasePoints(),unit
);
2629 if(roll_chance_i(chance
))
2630 m_caster
->CastSpell(unit
, auraSpellInfo
->EffectTriggerSpell
[auraSpellIdx
], true, NULL
, (*i
));
2636 // Heal caster for all health leech from all targets
2639 m_caster
->ModifyHealth(m_healthLeech
);
2640 m_caster
->SendHealSpellLog(m_caster
, m_spellInfo
->Id
, uint32(m_healthLeech
));
2643 if (IsMeleeAttackResetSpell())
2645 m_caster
->resetAttackTimer(BASE_ATTACK
);
2646 if(m_caster
->haveOffhandWeapon())
2647 m_caster
->resetAttackTimer(OFF_ATTACK
);
2650 /*if (IsRangedAttackResetSpell())
2651 m_caster->resetAttackTimer(RANGED_ATTACK);*/
2653 // Clear combo at finish state
2654 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& NeedsComboPoints(m_spellInfo
))
2656 // Not drop combopoints if negative spell and if any miss on enemy exist
2657 bool needDrop
= true;
2658 if (!IsPositiveSpell(m_spellInfo
->Id
))
2659 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2660 if (ihit
->missCondition
!= SPELL_MISS_NONE
&& ihit
->targetGUID
!=m_caster
->GetGUID())
2666 ((Player
*)m_caster
)->ClearComboPoints();
2669 // call triggered spell only at successful cast (after clear combo points -> for add some if need)
2670 if(!m_TriggerSpells
.empty())
2673 // Stop Attack for some spells
2674 if( m_spellInfo
->Attributes
& SPELL_ATTR_STOP_ATTACK_TARGET
)
2675 m_caster
->AttackStop();
2678 void Spell::SendCastResult(uint8 result
)
2680 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2683 if(((Player
*)m_caster
)->GetSession()->PlayerLoading()) // don't send cast results at loading time
2688 WorldPacket
data(SMSG_CAST_FAILED
, (4+1+1));
2689 data
<< uint8(m_cast_count
); // single cast or multi 2.3 (0/1)
2690 data
<< uint32(m_spellInfo
->Id
);
2691 data
<< uint8(result
); // problem
2694 case SPELL_FAILED_REQUIRES_SPELL_FOCUS
:
2695 data
<< uint32(m_spellInfo
->RequiresSpellFocus
);
2697 case SPELL_FAILED_REQUIRES_AREA
:
2698 // hardcode areas limitation case
2699 switch(m_spellInfo
->Id
)
2701 case 41617: // Cenarion Mana Salve
2702 case 41619: // Cenarion Healing Salve
2703 data
<< uint32(3905);
2705 case 41618: // Bottled Nethergon Energy
2706 case 41620: // Bottled Nethergon Vapor
2707 data
<< uint32(3842);
2709 case 45373: // Bloodberry Elixir
2710 data
<< uint32(4075);
2712 default: // default case
2713 data
<< uint32(m_spellInfo
->AreaId
);
2717 case SPELL_FAILED_TOTEMS
:
2718 if(m_spellInfo
->Totem
[0])
2719 data
<< uint32(m_spellInfo
->Totem
[0]);
2720 if(m_spellInfo
->Totem
[1])
2721 data
<< uint32(m_spellInfo
->Totem
[1]);
2723 case SPELL_FAILED_TOTEM_CATEGORY
:
2724 if(m_spellInfo
->TotemCategory
[0])
2725 data
<< uint32(m_spellInfo
->TotemCategory
[0]);
2726 if(m_spellInfo
->TotemCategory
[1])
2727 data
<< uint32(m_spellInfo
->TotemCategory
[1]);
2729 case SPELL_FAILED_EQUIPPED_ITEM_CLASS
:
2730 data
<< uint32(m_spellInfo
->EquippedItemClass
);
2731 data
<< uint32(m_spellInfo
->EquippedItemSubClassMask
);
2732 //data << uint32(m_spellInfo->EquippedItemInventoryTypeMask);
2735 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
2739 void Spell::SendSpellStart()
2741 if(!IsNeedSendToClient())
2744 sLog
.outDebug("Sending SMSG_SPELL_START id=%u", m_spellInfo
->Id
);
2746 uint32 castFlags
= CAST_FLAG_UNKNOWN1
;
2748 castFlags
|= CAST_FLAG_AMMO
;
2750 if(m_spellInfo
->runeCostID
)
2751 castFlags
|= CAST_FLAG_UNKNOWN10
;
2753 Unit
*target
= m_targets
.getUnitTarget() ? m_targets
.getUnitTarget() : m_caster
;
2755 WorldPacket
data(SMSG_SPELL_START
, (8+8+4+4+2));
2757 data
.append(m_CastItem
->GetPackGUID());
2759 data
.append(m_caster
->GetPackGUID());
2761 data
.append(m_caster
->GetPackGUID());
2762 data
<< uint8(m_cast_count
); // pending spell cast?
2763 data
<< uint32(m_spellInfo
->Id
); // spellId
2764 data
<< uint32(castFlags
); // cast flags
2765 data
<< uint32(m_timer
); // delay?
2767 m_targets
.write(&data
);
2769 if ( castFlags
& CAST_FLAG_UNKNOWN6
) // predicted power?
2772 if ( castFlags
& CAST_FLAG_UNKNOWN7
) // rune cooldowns list
2774 uint8 v1
= 0;//m_runesState;
2775 uint8 v2
= 0;//((Player*)m_caster)->GetRunesState();
2776 data
<< uint8(v1
); // runes state before
2777 data
<< uint8(v2
); // runes state after
2778 for(uint8 i
= 0; i
< MAX_RUNES
; ++i
)
2781 if(m
& v1
) // usable before...
2782 if(!(m
& v2
)) // ...but on cooldown now...
2783 data
<< uint8(0); // some unknown byte (time?)
2787 if ( castFlags
& CAST_FLAG_AMMO
)
2788 WriteAmmoToPacket(&data
);
2790 m_caster
->SendMessageToSet(&data
, true);
2793 void Spell::SendSpellGo()
2795 // not send invisible spell casting
2796 if(!IsNeedSendToClient())
2799 sLog
.outDebug("Sending SMSG_SPELL_GO id=%u", m_spellInfo
->Id
);
2801 Unit
*target
= m_targets
.getUnitTarget() ? m_targets
.getUnitTarget() : m_caster
;
2803 uint32 castFlags
= CAST_FLAG_UNKNOWN3
;
2805 castFlags
|= CAST_FLAG_AMMO
; // arrows/bullets visual
2807 if((m_caster
->GetTypeId() == TYPEID_PLAYER
) && (m_caster
->getClass() == CLASS_DEATH_KNIGHT
) && m_spellInfo
->runeCostID
)
2809 castFlags
|= CAST_FLAG_UNKNOWN10
; // same as in SMSG_SPELL_START
2810 castFlags
|= CAST_FLAG_UNKNOWN6
; // makes cooldowns visible
2811 castFlags
|= CAST_FLAG_UNKNOWN7
; // rune cooldowns list
2814 WorldPacket
data(SMSG_SPELL_GO
, 50); // guess size
2816 data
.append(m_CastItem
->GetPackGUID());
2818 data
.append(m_caster
->GetPackGUID());
2820 data
.append(m_caster
->GetPackGUID());
2821 data
<< uint8(m_cast_count
); // pending spell cast?
2822 data
<< uint32(m_spellInfo
->Id
); // spellId
2823 data
<< uint32(castFlags
); // cast flags
2824 data
<< uint32(getMSTime()); // timestamp
2826 WriteSpellGoTargets(&data
);
2828 m_targets
.write(&data
);
2830 if ( castFlags
& CAST_FLAG_UNKNOWN6
) // unknown wotlk, predicted power?
2833 if ( castFlags
& CAST_FLAG_UNKNOWN7
) // rune cooldowns list
2835 uint8 v1
= m_runesState
;
2836 uint8 v2
= ((Player
*)m_caster
)->GetRunesState();
2837 data
<< uint8(v1
); // runes state before
2838 data
<< uint8(v2
); // runes state after
2839 for(uint8 i
= 0; i
< MAX_RUNES
; ++i
)
2842 if(m
& v1
) // usable before...
2843 if(!(m
& v2
)) // ...but on cooldown now...
2844 data
<< uint8(0); // some unknown byte (time?)
2848 if ( castFlags
& CAST_FLAG_UNKNOWN4
) // unknown wotlk
2854 if ( castFlags
& CAST_FLAG_AMMO
)
2855 WriteAmmoToPacket(&data
);
2857 if ( castFlags
& CAST_FLAG_UNKNOWN5
) // unknown wotlk
2863 if ( m_targets
.m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
2868 m_caster
->SendMessageToSet(&data
, true);
2871 void Spell::WriteAmmoToPacket( WorldPacket
* data
)
2873 uint32 ammoInventoryType
= 0;
2874 uint32 ammoDisplayID
= 0;
2876 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2878 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack( RANGED_ATTACK
);
2881 ammoInventoryType
= pItem
->GetProto()->InventoryType
;
2882 if( ammoInventoryType
== INVTYPE_THROWN
)
2883 ammoDisplayID
= pItem
->GetProto()->DisplayInfoID
;
2886 uint32 ammoID
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
2889 ItemPrototype
const *pProto
= objmgr
.GetItemPrototype( ammoID
);
2892 ammoDisplayID
= pProto
->DisplayInfoID
;
2893 ammoInventoryType
= pProto
->InventoryType
;
2896 else if(m_caster
->GetDummyAura(46699)) // Requires No Ammo
2898 ammoDisplayID
= 5996; // normal arrow
2899 ammoInventoryType
= INVTYPE_AMMO
;
2904 // TODO: implement selection ammo data based at ranged weapon stored in equipmodel/equipinfo/equipslot fields
2906 *data
<< uint32(ammoDisplayID
);
2907 *data
<< uint32(ammoInventoryType
);
2910 void Spell::WriteSpellGoTargets( WorldPacket
* data
)
2912 *data
<< (uint8
)m_countOfHit
;
2913 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2914 if ((*ihit
).missCondition
== SPELL_MISS_NONE
) // Add only hits
2915 *data
<< uint64(ihit
->targetGUID
);
2917 for(std::list
<GOTargetInfo
>::iterator ighit
= m_UniqueGOTargetInfo
.begin();ighit
!= m_UniqueGOTargetInfo
.end();++ighit
)
2918 *data
<< uint64(ighit
->targetGUID
); // Always hits
2920 *data
<< (uint8
)m_countOfMiss
;
2921 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2923 if( ihit
->missCondition
!= SPELL_MISS_NONE
) // Add only miss
2925 *data
<< uint64(ihit
->targetGUID
);
2926 *data
<< uint8(ihit
->missCondition
);
2927 if( ihit
->missCondition
== SPELL_MISS_REFLECT
)
2928 *data
<< uint8(ihit
->reflectResult
);
2933 void Spell::SendLogExecute()
2935 Unit
*target
= m_targets
.getUnitTarget() ? m_targets
.getUnitTarget() : m_caster
;
2937 WorldPacket
data(SMSG_SPELLLOGEXECUTE
, (8+4+4+4+4+8));
2939 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
2940 data
.append(m_caster
->GetPackGUID());
2942 data
.append(target
->GetPackGUID());
2944 data
<< uint32(m_spellInfo
->Id
);
2946 data
<< uint32(count1
); // count1 (effect count?)
2947 for(uint32 i
= 0; i
< count1
; ++i
)
2949 data
<< uint32(m_spellInfo
->Effect
[0]); // spell effect
2951 data
<< uint32(count2
); // count2 (target count?)
2952 for(uint32 j
= 0; j
< count2
; ++j
)
2954 switch(m_spellInfo
->Effect
[0])
2956 case SPELL_EFFECT_POWER_DRAIN
:
2957 if(Unit
*unit
= m_targets
.getUnitTarget())
2958 data
.append(unit
->GetPackGUID());
2965 case SPELL_EFFECT_ADD_EXTRA_ATTACKS
:
2966 if(Unit
*unit
= m_targets
.getUnitTarget())
2967 data
.append(unit
->GetPackGUID());
2970 data
<< uint32(0); // count?
2972 case SPELL_EFFECT_INTERRUPT_CAST
:
2973 if(Unit
*unit
= m_targets
.getUnitTarget())
2974 data
.append(unit
->GetPackGUID());
2977 data
<< uint32(0); // spellid
2979 case SPELL_EFFECT_DURABILITY_DAMAGE
:
2980 if(Unit
*unit
= m_targets
.getUnitTarget())
2981 data
.append(unit
->GetPackGUID());
2987 case SPELL_EFFECT_OPEN_LOCK
:
2988 case SPELL_EFFECT_OPEN_LOCK_ITEM
:
2989 if(Item
*item
= m_targets
.getItemTarget())
2990 data
.append(item
->GetPackGUID());
2994 case SPELL_EFFECT_CREATE_ITEM
:
2995 case SPELL_EFFECT_157
:
2996 data
<< uint32(m_spellInfo
->EffectItemType
[0]);
2998 case SPELL_EFFECT_SUMMON
:
2999 case SPELL_EFFECT_TRANS_DOOR
:
3000 case SPELL_EFFECT_SUMMON_PET
:
3001 case SPELL_EFFECT_SUMMON_OBJECT_WILD
:
3002 case SPELL_EFFECT_CREATE_HOUSE
:
3003 case SPELL_EFFECT_DUEL
:
3004 case SPELL_EFFECT_SUMMON_OBJECT_SLOT1
:
3005 case SPELL_EFFECT_SUMMON_OBJECT_SLOT2
:
3006 case SPELL_EFFECT_SUMMON_OBJECT_SLOT3
:
3007 case SPELL_EFFECT_SUMMON_OBJECT_SLOT4
:
3008 if(Unit
*unit
= m_targets
.getUnitTarget())
3009 data
.append(unit
->GetPackGUID());
3010 else if(m_targets
.getItemTargetGUID())
3011 data
.appendPackGUID(m_targets
.getItemTargetGUID());
3012 else if(GameObject
*go
= m_targets
.getGOTarget())
3013 data
.append(go
->GetPackGUID());
3015 data
<< uint8(0); // guid
3017 case SPELL_EFFECT_FEED_PET
:
3018 data
<< uint32(m_targets
.getItemTargetEntry());
3020 case SPELL_EFFECT_DISMISS_PET
:
3021 if(Unit
*unit
= m_targets
.getUnitTarget())
3022 data
.append(unit
->GetPackGUID());
3026 case SPELL_EFFECT_RESURRECT
:
3027 case SPELL_EFFECT_RESURRECT_NEW
:
3028 if(Unit
*unit
= m_targets
.getUnitTarget())
3029 data
.append(unit
->GetPackGUID());
3039 m_caster
->SendMessageToSet(&data
, true);
3042 void Spell::SendInterrupted(uint8 result
)
3044 WorldPacket
data(SMSG_SPELL_FAILURE
, (8+4+1));
3045 data
.append(m_caster
->GetPackGUID());
3046 data
<< uint8(m_cast_count
);
3047 data
<< uint32(m_spellInfo
->Id
);
3048 data
<< uint8(result
);
3049 m_caster
->SendMessageToSet(&data
, true);
3051 data
.Initialize(SMSG_SPELL_FAILED_OTHER
, (8+4));
3052 data
.append(m_caster
->GetPackGUID());
3053 data
<< uint8(m_cast_count
);
3054 data
<< uint32(m_spellInfo
->Id
);
3055 data
<< uint8(result
);
3056 m_caster
->SendMessageToSet(&data
, true);
3059 void Spell::SendChannelUpdate(uint32 time
)
3063 m_caster
->SetUInt64Value(UNIT_FIELD_CHANNEL_OBJECT
,0);
3064 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
,0);
3067 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
3070 WorldPacket
data( MSG_CHANNEL_UPDATE
, 8+4 );
3071 data
.append(m_caster
->GetPackGUID());
3072 data
<< uint32(time
);
3074 ((Player
*)m_caster
)->GetSession()->SendPacket( &data
);
3077 void Spell::SendChannelStart(uint32 duration
)
3079 WorldObject
* target
= NULL
;
3081 // select first not resisted target from target list for _0_ effect
3082 if(!m_UniqueTargetInfo
.empty())
3084 for(std::list
<TargetInfo
>::iterator itr
= m_UniqueTargetInfo
.begin();itr
!= m_UniqueTargetInfo
.end();++itr
)
3086 if( (itr
->effectMask
& (1<<0)) && itr
->reflectResult
==SPELL_MISS_NONE
&& itr
->targetGUID
!= m_caster
->GetGUID())
3088 target
= ObjectAccessor::GetUnit(*m_caster
, itr
->targetGUID
);
3093 else if(!m_UniqueGOTargetInfo
.empty())
3095 for(std::list
<GOTargetInfo
>::iterator itr
= m_UniqueGOTargetInfo
.begin();itr
!= m_UniqueGOTargetInfo
.end();++itr
)
3097 if(itr
->effectMask
& (1<<0) )
3099 target
= ObjectAccessor::GetGameObject(*m_caster
, itr
->targetGUID
);
3105 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
3107 WorldPacket
data( MSG_CHANNEL_START
, (8+4+4) );
3108 data
.append(m_caster
->GetPackGUID());
3109 data
<< uint32(m_spellInfo
->Id
);
3110 data
<< uint32(duration
);
3112 ((Player
*)m_caster
)->GetSession()->SendPacket( &data
);
3117 m_caster
->SetUInt64Value(UNIT_FIELD_CHANNEL_OBJECT
, target
->GetGUID());
3118 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
, m_spellInfo
->Id
);
3121 void Spell::SendResurrectRequest(Player
* target
)
3123 // Both players and NPCs can resurrect using spells - have a look at creature 28487 for example
3124 // However, the packet structure differs slightly
3126 const char* sentName
= m_caster
->GetTypeId()==TYPEID_PLAYER
?"":m_caster
->GetNameForLocaleIdx(target
->GetSession()->GetSessionDbLocaleIndex());
3128 WorldPacket
data(SMSG_RESURRECT_REQUEST
, (8+4+strlen(sentName
)+1+1+1));
3129 data
<< uint64(m_caster
->GetGUID());
3130 data
<< uint32(strlen(sentName
)+1);
3135 data
<< uint8(m_caster
->GetTypeId()==TYPEID_PLAYER
?0:1);
3136 target
->GetSession()->SendPacket(&data
);
3139 void Spell::SendPlaySpellVisual(uint32 SpellID
)
3141 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
3144 WorldPacket
data(SMSG_PLAY_SPELL_VISUAL
, 12);
3145 data
<< uint64(m_caster
->GetGUID());
3146 data
<< uint32(SpellID
); // spell visual id?
3147 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
3150 void Spell::TakeCastItem()
3152 if(!m_CastItem
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
3155 // not remove cast item at triggered spell (equipping, weapon damage, etc)
3156 if(m_IsTriggeredSpell
)
3159 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3163 // This code is to avoid a crash
3164 // I'm not sure, if this is really an error, but I guess every item needs a prototype
3165 sLog
.outError("Cast item has no item prototype highId=%d, lowId=%d",m_CastItem
->GetGUIDHigh(), m_CastItem
->GetGUIDLow());
3169 bool expendable
= false;
3170 bool withoutCharges
= false;
3172 for (int i
= 0; i
<5; i
++)
3174 if (proto
->Spells
[i
].SpellId
)
3176 // item has limited charges
3177 if (proto
->Spells
[i
].SpellCharges
)
3179 if (proto
->Spells
[i
].SpellCharges
< 0)
3182 int32 charges
= m_CastItem
->GetSpellCharges(i
);
3184 // item has charges left
3187 (charges
> 0) ? --charges
: ++charges
; // abs(charges) less at 1 after use
3188 if (proto
->Stackable
< 2)
3189 m_CastItem
->SetSpellCharges(i
, charges
);
3190 m_CastItem
->SetState(ITEM_CHANGED
, (Player
*)m_caster
);
3194 withoutCharges
= (charges
== 0);
3199 if (expendable
&& withoutCharges
)
3202 ((Player
*)m_caster
)->DestroyItemCount(m_CastItem
, count
, true);
3204 // prevent crash at access to deleted m_targets.getItemTarget
3205 if(m_CastItem
==m_targets
.getItemTarget())
3206 m_targets
.setItemTarget(NULL
);
3212 void Spell::TakePower()
3214 if(m_CastItem
|| m_triggeredByAuraSpell
)
3217 // health as power used
3218 if(m_spellInfo
->powerType
== POWER_HEALTH
)
3220 m_caster
->ModifyHealth( -(int32
)m_powerCost
);
3224 if(m_spellInfo
->powerType
>= MAX_POWERS
)
3226 sLog
.outError("Spell::TakePower: Unknown power type '%d'", m_spellInfo
->powerType
);
3230 Powers powerType
= Powers(m_spellInfo
->powerType
);
3232 if(powerType
== POWER_RUNE
)
3238 m_caster
->ModifyPower(powerType
, -(int32
)m_powerCost
);
3240 // Set the five second timer
3241 if (powerType
== POWER_MANA
&& m_powerCost
> 0)
3242 m_caster
->SetLastManaUse(getMSTime());
3245 uint8
Spell::CheckRuneCost(uint32 runeCostID
)
3247 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
3250 Player
*plr
= (Player
*)m_caster
;
3252 if(plr
->getClass() != CLASS_DEATH_KNIGHT
)
3255 SpellRuneCostEntry
const *src
= sSpellRuneCostStore
.LookupEntry(runeCostID
);
3260 if(src
->NoRuneCost())
3263 int32 runeCost
[NUM_RUNE_TYPES
]; // blood, frost, unholy, death
3265 for(uint32 i
= 0; i
< RUNE_DEATH
; ++i
)
3267 runeCost
[i
] = src
->RuneCost
[i
];
3270 runeCost
[RUNE_DEATH
] = 0; // calculated later
3272 for(uint32 i
= 0; i
< MAX_RUNES
; ++i
)
3274 uint8 rune
= plr
->GetCurrentRune(i
);
3275 if((plr
->GetRuneCooldown(i
) == 0) && (runeCost
[rune
] > 0))
3281 for(uint32 i
= 0; i
< RUNE_DEATH
; ++i
)
3285 runeCost
[RUNE_DEATH
] += runeCost
[i
];
3289 if(runeCost
[RUNE_DEATH
] > 0)
3290 return SPELL_FAILED_NO_POWER
; // not sure if result code is correct
3295 void Spell::TakeRunePower()
3297 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
3300 Player
*plr
= (Player
*)m_caster
;
3302 if(plr
->getClass() != CLASS_DEATH_KNIGHT
)
3305 SpellRuneCostEntry
const *src
= sSpellRuneCostStore
.LookupEntry(m_spellInfo
->runeCostID
);
3307 if(!src
|| (src
->NoRuneCost() && src
->NoRunicPowerGain()))
3310 m_runesState
= plr
->GetRunesState(); // store previous state
3312 int32 runeCost
[NUM_RUNE_TYPES
]; // blood, frost, unholy, death
3314 for(uint32 i
= 0; i
< RUNE_DEATH
; ++i
)
3316 runeCost
[i
] = src
->RuneCost
[i
];
3319 runeCost
[RUNE_DEATH
] = 0; // calculated later
3321 for(uint32 i
= 0; i
< MAX_RUNES
; ++i
)
3323 uint8 rune
= plr
->GetCurrentRune(i
);
3324 if((plr
->GetRuneCooldown(i
) == 0) && (runeCost
[rune
] > 0))
3326 plr
->SetRuneCooldown(i
, RUNE_COOLDOWN
); // 5*2=10 sec
3331 runeCost
[RUNE_DEATH
] = runeCost
[RUNE_BLOOD
] + runeCost
[RUNE_UNHOLY
] + runeCost
[RUNE_FROST
];
3333 if(runeCost
[RUNE_DEATH
] > 0)
3335 for(uint32 i
= 0; i
< MAX_RUNES
; ++i
)
3337 uint8 rune
= plr
->GetCurrentRune(i
);
3338 if((plr
->GetRuneCooldown(i
) == 0) && (rune
== RUNE_DEATH
))
3340 plr
->SetRuneCooldown(i
, RUNE_COOLDOWN
); // 5*2=10 sec
3342 plr
->ConvertRune(i
, plr
->GetBaseRune(i
));
3343 if(runeCost
[RUNE_DEATH
] == 0)
3349 // you can gain some runic power when use runes
3350 float rp
= src
->runePowerGain
;;
3351 rp
*= sWorld
.getRate(RATE_POWER_RUNICPOWER_INCOME
);
3352 plr
->ModifyPower(POWER_RUNIC_POWER
, (int32
)rp
);
3355 void Spell::TakeReagents()
3357 if(m_IsTriggeredSpell
) // reagents used in triggered spell removed by original spell or don't must be removed.
3360 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
3363 Player
* p_caster
= (Player
*)m_caster
;
3364 if (p_caster
->CanNoReagentCast(m_spellInfo
))
3367 for(uint32 x
=0;x
<8;x
++)
3369 if(m_spellInfo
->Reagent
[x
] <= 0)
3372 uint32 itemid
= m_spellInfo
->Reagent
[x
];
3373 uint32 itemcount
= m_spellInfo
->ReagentCount
[x
];
3375 // if CastItem is also spell reagent
3378 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3379 if( proto
&& proto
->ItemId
== itemid
)
3381 for(int s
=0;s
<5;s
++)
3383 // CastItem will be used up and does not count as reagent
3384 int32 charges
= m_CastItem
->GetSpellCharges(s
);
3385 if (proto
->Spells
[s
].SpellCharges
< 0 && abs(charges
) < 2)
3396 // if getItemTarget is also spell reagent
3397 if (m_targets
.getItemTargetEntry()==itemid
)
3398 m_targets
.setItemTarget(NULL
);
3400 p_caster
->DestroyItemCount(itemid
, itemcount
, true);
3404 void Spell::HandleThreatSpells(uint32 spellId
)
3406 if(!m_targets
.getUnitTarget() || !spellId
)
3409 if(!m_targets
.getUnitTarget()->CanHaveThreatList())
3412 SpellThreatEntry
const *threatSpell
= sSpellThreatStore
.LookupEntry
<SpellThreatEntry
>(spellId
);
3416 m_targets
.getUnitTarget()->AddThreat(m_caster
, float(threatSpell
->threat
));
3418 DEBUG_LOG("Spell %u, rank %u, added an additional %i threat", spellId
, spellmgr
.GetSpellRank(spellId
), threatSpell
->threat
);
3421 void Spell::HandleEffects(Unit
*pUnitTarget
,Item
*pItemTarget
,GameObject
*pGOTarget
,uint32 i
, float DamageMultiplier
)
3423 unitTarget
= pUnitTarget
;
3424 itemTarget
= pItemTarget
;
3425 gameObjTarget
= pGOTarget
;
3427 uint8 eff
= m_spellInfo
->Effect
[i
];
3428 uint32 mechanic
= m_spellInfo
->EffectMechanic
[i
];
3430 damage
= int32(CalculateDamage((uint8
)i
,unitTarget
)*DamageMultiplier
);
3432 sLog
.outDebug( "Spell: Effect : %u", eff
);
3434 //Simply return. Do not display "immune" in red text on client
3435 if(unitTarget
&& unitTarget
->IsImmunedToSpellEffect(eff
, mechanic
))
3438 if(eff
<TOTAL_SPELL_EFFECTS
)
3440 //sLog.outDebug( "WORLD: Spell FX %d < TOTAL_SPELL_EFFECTS ", eff);
3441 (*this.*SpellEffects
[eff
])(i
);
3446 sLog.outDebug( "WORLD: Spell FX %d > TOTAL_SPELL_EFFECTS ", eff);
3448 EffectEnchantItemTmp(i);
3451 sLog.outError("SPELL: unknown effect %u spell id %u\n",
3452 eff, m_spellInfo->Id);
3458 void Spell::TriggerSpell()
3460 for(TriggerSpells::iterator si
=m_TriggerSpells
.begin(); si
!=m_TriggerSpells
.end(); ++si
)
3462 Spell
* spell
= new Spell(m_caster
, (*si
), true, m_originalCasterGUID
, m_selfContainer
);
3463 spell
->prepare(&m_targets
); // use original spell original targets
3467 uint8
Spell::CanCast(bool strict
)
3469 // check cooldowns to prevent cheating
3470 if(m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
3472 if(m_triggeredByAuraSpell
)
3473 return SPELL_FAILED_DONT_REPORT
;
3475 return SPELL_FAILED_NOT_READY
;
3478 // only allow triggered spells if at an ended battleground
3479 if( !m_IsTriggeredSpell
&& m_caster
->GetTypeId() == TYPEID_PLAYER
)
3480 if(BattleGround
* bg
= ((Player
*)m_caster
)->GetBattleGround())
3481 if(bg
->GetStatus() == STATUS_WAIT_LEAVE
)
3482 return SPELL_FAILED_DONT_REPORT
;
3484 // only check at first call, Stealth auras are already removed at second call
3485 // for now, ignore triggered spells
3486 if( strict
&& !m_IsTriggeredSpell
)
3488 bool checkForm
= true;
3489 // Ignore form req aura
3490 Unit::AuraList
const& ignore
= m_caster
->GetAurasByType(SPELL_AURA_MOD_IGNORE_SHAPESHIFT
);
3491 for(Unit::AuraList::const_iterator i
= ignore
.begin(); i
!= ignore
.end(); ++i
)
3493 if (!(*i
)->isAffectedOnSpell(m_spellInfo
))
3500 // Cannot be used in this stance/form
3501 if(uint8 shapeError
= GetErrorAtShapeshiftedCast(m_spellInfo
, m_caster
->m_form
))
3504 if ((m_spellInfo
->Attributes
& SPELL_ATTR_ONLY_STEALTHED
) && !(m_caster
->HasStealthAura()))
3505 return SPELL_FAILED_ONLY_STEALTHED
;
3509 // caster state requirements
3510 if(m_spellInfo
->CasterAuraState
&& !m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraState
)))
3511 return SPELL_FAILED_CASTER_AURASTATE
;
3512 if(m_spellInfo
->CasterAuraStateNot
&& m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraStateNot
)))
3513 return SPELL_FAILED_CASTER_AURASTATE
;
3515 // cancel autorepeat spells if cast start when moving
3516 // (not wand currently autorepeat cast delayed to moving stop anyway in spell update code)
3517 if( m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->isMoving() )
3519 // skip stuck spell to allow use it in falling case and apply spell limitations at movement
3520 if( (!m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_FALLING
) || m_spellInfo
->Effect
[0] != SPELL_EFFECT_STUCK
) &&
3521 (IsAutoRepeat() || (m_spellInfo
->AuraInterruptFlags
& AURA_INTERRUPT_FLAG_NOT_SEATED
) != 0) )
3522 return SPELL_FAILED_MOVING
;
3525 Unit
*target
= m_targets
.getUnitTarget();
3529 // target state requirements (not allowed state), apply to self also
3530 if(m_spellInfo
->TargetAuraStateNot
&& target
->HasAuraState(AuraState(m_spellInfo
->TargetAuraStateNot
)))
3531 return SPELL_FAILED_TARGET_AURASTATE
;
3533 if(target
!= m_caster
)
3535 // target state requirements (apply to non-self only), to allow cast affects to self like Dirty Deeds
3536 if(m_spellInfo
->TargetAuraState
&& !target
->HasAuraState(AuraState(m_spellInfo
->TargetAuraState
)))
3537 return SPELL_FAILED_TARGET_AURASTATE
;
3539 // Not allow casting on flying player
3540 if (target
->isInFlight())
3541 return SPELL_FAILED_BAD_TARGETS
;
3543 if(!m_IsTriggeredSpell
&& VMAP::VMapFactory::checkSpellForLoS(m_spellInfo
->Id
) && !m_caster
->IsWithinLOSInMap(target
))
3544 return SPELL_FAILED_LINE_OF_SIGHT
;
3546 // auto selection spell rank implemented in WorldSession::HandleCastSpellOpcode
3547 // this case can be triggered if rank not found (too low-level target for first rank)
3548 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsPassiveSpell(m_spellInfo
->Id
) && !m_CastItem
)
3550 for(int i
=0;i
<3;i
++)
3552 if(IsPositiveEffect(m_spellInfo
->Id
, i
) && m_spellInfo
->Effect
[i
] == SPELL_EFFECT_APPLY_AURA
)
3553 if(target
->getLevel() + 10 < m_spellInfo
->spellLevel
)
3554 return SPELL_FAILED_LOWLEVEL
;
3559 // check pet presents
3560 for(int j
=0;j
<3;j
++)
3562 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_PET
)
3564 target
= m_caster
->GetPet();
3567 if(m_triggeredByAuraSpell
) // not report pet not existence for triggered spells
3568 return SPELL_FAILED_DONT_REPORT
;
3570 return SPELL_FAILED_NO_PET
;
3576 //check creature type
3577 //ignore self casts (including area casts when caster selected as target)
3578 if(target
!= m_caster
)
3580 if(!CheckTargetCreatureType(target
))
3582 if(target
->GetTypeId()==TYPEID_PLAYER
)
3583 return SPELL_FAILED_TARGET_IS_PLAYER
;
3585 return SPELL_FAILED_BAD_TARGETS
;
3589 // TODO: this check can be applied and for player to prevent cheating when IsPositiveSpell will return always correct result.
3590 // check target for pet/charmed casts (not self targeted), self targeted cast used for area effects and etc
3591 if(m_caster
!= target
&& m_caster
->GetTypeId()==TYPEID_UNIT
&& m_caster
->GetCharmerOrOwnerGUID())
3593 // check correctness positive/negative cast target (pet cast real check and cheating check)
3594 if(IsPositiveSpell(m_spellInfo
->Id
))
3596 if(m_caster
->IsHostileTo(target
))
3597 return SPELL_FAILED_BAD_TARGETS
;
3601 if(m_caster
->IsFriendlyTo(target
))
3602 return SPELL_FAILED_BAD_TARGETS
;
3606 if(IsPositiveSpell(m_spellInfo
->Id
))
3608 if(target
->IsImmunedToSpell(m_spellInfo
,false))
3609 return SPELL_FAILED_TARGET_AURASTATE
;
3612 //Must be behind the target.
3613 if( m_spellInfo
->AttributesEx2
== 0x100000 && (m_spellInfo
->AttributesEx
& 0x200) == 0x200 && target
->HasInArc(M_PI
, m_caster
) )
3615 //Exclusion for Pounce: Facing Limitation was removed in 2.0.1, but it still uses the same, old Ex-Flags
3616 if( m_spellInfo
->SpellFamilyName
!= SPELLFAMILY_DRUID
|| m_spellInfo
->SpellFamilyFlags
!= 0x0000000000020000LL
)
3619 return SPELL_FAILED_NOT_BEHIND
;
3623 //Target must be facing you.
3624 if((m_spellInfo
->Attributes
== 0x150010) && !target
->HasInArc(M_PI
, m_caster
) )
3627 return SPELL_FAILED_NOT_INFRONT
;
3630 // check if target is in combat
3631 if (target
!= m_caster
&& (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_NOT_IN_COMBAT_TARGET
) && target
->isInCombat())
3633 return SPELL_FAILED_TARGET_AFFECTING_COMBAT
;
3636 // Spell casted only on battleground
3637 if((m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_BATTLEGROUND
) && m_caster
->GetTypeId()==TYPEID_PLAYER
)
3638 if(!((Player
*)m_caster
)->InBattleGround())
3639 return SPELL_FAILED_ONLY_BATTLEGROUNDS
;
3641 // do not allow spells to be cast in arenas
3642 // - with greater than 15 min CD without SPELL_ATTR_EX4_USABLE_IN_ARENA flag
3643 // - with SPELL_ATTR_EX4_NOT_USABLE_IN_ARENA flag
3644 if( (m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_NOT_USABLE_IN_ARENA
) ||
3645 GetSpellRecoveryTime(m_spellInfo
) > 15 * MINUTE
* 1000 && !(m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_USABLE_IN_ARENA
) )
3646 if(MapEntry
const* mapEntry
= sMapStore
.LookupEntry(m_caster
->GetMapId()))
3647 if(mapEntry
->IsBattleArena())
3648 return SPELL_FAILED_NOT_IN_ARENA
;
3651 if(!IsSpellAllowedInLocation(m_spellInfo
,m_caster
->GetMapId(),m_caster
->GetZoneId(),m_caster
->GetAreaId()))
3652 return SPELL_FAILED_REQUIRES_AREA
;
3654 // not let players cast spells at mount (and let do it to creatures)
3655 if( m_caster
->IsMounted() && m_caster
->GetTypeId()==TYPEID_PLAYER
&& !m_IsTriggeredSpell
&&
3656 !IsPassiveSpell(m_spellInfo
->Id
) && !(m_spellInfo
->Attributes
& SPELL_ATTR_CASTABLE_WHILE_MOUNTED
) )
3658 if(m_caster
->isInFlight())
3659 return SPELL_FAILED_NOT_FLYING
;
3661 return SPELL_FAILED_NOT_MOUNTED
;
3664 // always (except passive spells) check items (focus object can be required for any type casts)
3665 if(!IsPassiveSpell(m_spellInfo
->Id
))
3666 if(uint8 castResult
= CheckItems())
3669 //ImpliciteTargetA-B = 38, If fact there is 0 Spell with ImpliciteTargetB=38
3670 if(m_UniqueTargetInfo
.empty()) // skip second canCast apply (for delayed spells for example)
3672 for(uint8 j
= 0; j
< 3; j
++)
3674 if( m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT
||
3675 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT
&& m_spellInfo
->EffectImplicitTargetA
[j
] != TARGET_SELF
||
3676 m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3677 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3679 SpellScriptTarget::const_iterator lower
= spellmgr
.GetBeginSpellScriptTarget(m_spellInfo
->Id
);
3680 SpellScriptTarget::const_iterator upper
= spellmgr
.GetEndSpellScriptTarget(m_spellInfo
->Id
);
3682 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
);
3684 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
3685 float range
= GetSpellMaxRange(srange
);
3687 Creature
* creatureScriptTarget
= NULL
;
3688 GameObject
* goScriptTarget
= NULL
;
3690 for(SpellScriptTarget::const_iterator i_spellST
= lower
; i_spellST
!= upper
; ++i_spellST
)
3692 switch(i_spellST
->second
.type
)
3694 case SPELL_TARGET_TYPE_GAMEOBJECT
:
3696 GameObject
* p_GameObject
= NULL
;
3698 if(i_spellST
->second
.targetEntry
)
3700 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
3702 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
3704 MaNGOS::NearestGameObjectEntryInObjectRangeCheck
go_check(*m_caster
,i_spellST
->second
.targetEntry
,range
);
3705 MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
> checker(p_GameObject
,go_check
);
3707 TypeContainerVisitor
<MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
>, GridTypeMapContainer
> object_checker(checker
);
3708 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
3709 cell_lock
->Visit(cell_lock
, object_checker
, *m_caster
->GetMap());
3713 // remember found target and range, next attempt will find more near target with another entry
3714 creatureScriptTarget
= NULL
;
3715 goScriptTarget
= p_GameObject
;
3716 range
= go_check
.GetLastRange();
3719 else if( focusObject
) //Focus Object
3721 float frange
= m_caster
->GetDistance(focusObject
);
3724 creatureScriptTarget
= NULL
;
3725 goScriptTarget
= focusObject
;
3731 case SPELL_TARGET_TYPE_CREATURE
:
3732 case SPELL_TARGET_TYPE_DEAD
:
3735 Creature
*p_Creature
= NULL
;
3737 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
3739 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
3740 cell
.SetNoCreate(); // Really don't know what is that???
3742 MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
u_check(*m_caster
,i_spellST
->second
.targetEntry
,i_spellST
->second
.type
!=SPELL_TARGET_TYPE_DEAD
,range
);
3743 MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
> searcher(p_Creature
, u_check
);
3745 TypeContainerVisitor
<MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
>, GridTypeMapContainer
> grid_creature_searcher(searcher
);
3747 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
3748 cell_lock
->Visit(cell_lock
, grid_creature_searcher
, *m_caster
->GetMap());
3752 creatureScriptTarget
= p_Creature
;
3753 goScriptTarget
= NULL
;
3754 range
= u_check
.GetLastRange();
3761 if(creatureScriptTarget
)
3763 // store coordinates for TARGET_SCRIPT_COORDINATES
3764 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3765 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3767 m_targets
.setDestination(creatureScriptTarget
->GetPositionX(),creatureScriptTarget
->GetPositionY(),creatureScriptTarget
->GetPositionZ());
3769 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->EffectImplicitTargetB
[j
] == 0 && m_spellInfo
->Effect
[j
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
3770 AddUnitTarget(creatureScriptTarget
, j
);
3772 // store explicit target for TARGET_SCRIPT
3774 AddUnitTarget(creatureScriptTarget
, j
);
3776 else if(goScriptTarget
)
3778 // store coordinates for TARGET_SCRIPT_COORDINATES
3779 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3780 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3782 m_targets
.setDestination(goScriptTarget
->GetPositionX(),goScriptTarget
->GetPositionY(),goScriptTarget
->GetPositionZ());
3784 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->EffectImplicitTargetB
[j
] == 0 && m_spellInfo
->Effect
[j
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
3785 AddGOTarget(goScriptTarget
, j
);
3787 // store explicit target for TARGET_SCRIPT
3789 AddGOTarget(goScriptTarget
, j
);
3791 //Missing DB Entry or targets for this spellEffect.
3794 // not report target not existence for triggered spells
3795 if(m_triggeredByAuraSpell
|| m_IsTriggeredSpell
)
3796 return SPELL_FAILED_DONT_REPORT
;
3798 return SPELL_FAILED_BAD_TARGETS
;
3804 if(!m_triggeredByAuraSpell
)
3805 if(uint8 castResult
= CheckRange(strict
))
3809 if(uint8 castResult
= CheckPower())
3813 if(!m_triggeredByAuraSpell
) // triggered spell not affected by stun/etc
3814 if(uint8 castResult
= CheckCasterAuras())
3817 for (int i
= 0; i
< 3; i
++)
3819 // for effects of spells that have only one target
3820 switch(m_spellInfo
->Effect
[i
])
3822 case SPELL_EFFECT_DUMMY
:
3824 if(m_spellInfo
->SpellIconID
== 1648) // Execute
3826 if(!m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
3827 return SPELL_FAILED_BAD_TARGETS
;
3829 else if (m_spellInfo
->Id
== 51582) // Rocket Boots Engaged
3831 if(m_caster
->IsInWater())
3832 return SPELL_FAILED_ONLY_ABOVEWATER
;
3834 else if(m_spellInfo
->SpellIconID
==156) // Holy Shock
3836 // spell different for friends and enemies
3837 // hart version required facing
3838 if(m_targets
.getUnitTarget() && !m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()) && !m_caster
->HasInArc( M_PI
, target
))
3839 return SPELL_FAILED_UNIT_NOT_INFRONT
;
3843 case SPELL_EFFECT_SCHOOL_DAMAGE
:
3846 if(m_spellInfo
->SpellVisual
[0] == 7250)
3848 if (!m_targets
.getUnitTarget())
3849 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3851 if(m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
3852 return SPELL_FAILED_BAD_TARGETS
;
3856 case SPELL_EFFECT_TAMECREATURE
:
3858 if (!m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetTypeId() == TYPEID_PLAYER
)
3859 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3861 if (m_targets
.getUnitTarget()->getLevel() > m_caster
->getLevel())
3862 return SPELL_FAILED_HIGHLEVEL
;
3864 // use SMSG_PET_TAME_FAILURE?
3865 if (!((Creature
*)m_targets
.getUnitTarget())->GetCreatureInfo()->isTameable ())
3866 return SPELL_FAILED_BAD_TARGETS
;
3868 if(m_caster
->GetPetGUID())
3869 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3871 if(m_caster
->GetCharmGUID())
3872 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3876 case SPELL_EFFECT_LEARN_SPELL
:
3878 if(m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_PET
)
3881 Pet
* pet
= m_caster
->GetPet();
3884 return SPELL_FAILED_NO_PET
;
3886 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
3888 if(!learn_spellproto
)
3889 return SPELL_FAILED_NOT_KNOWN
;
3891 if(m_spellInfo
->spellLevel
> pet
->getLevel())
3892 return SPELL_FAILED_LOWLEVEL
;
3896 case SPELL_EFFECT_LEARN_PET_SPELL
:
3898 Pet
* pet
= m_caster
->GetPet();
3901 return SPELL_FAILED_NO_PET
;
3903 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
3905 if(!learn_spellproto
)
3906 return SPELL_FAILED_NOT_KNOWN
;
3908 if(m_spellInfo
->spellLevel
> pet
->getLevel())
3909 return SPELL_FAILED_LOWLEVEL
;
3913 case SPELL_EFFECT_FEED_PET
:
3915 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| !m_targets
.getItemTarget() )
3916 return SPELL_FAILED_BAD_TARGETS
;
3918 Pet
* pet
= m_caster
->GetPet();
3921 return SPELL_FAILED_NO_PET
;
3923 if(!pet
->HaveInDiet(m_targets
.getItemTarget()->GetProto()))
3924 return SPELL_FAILED_WRONG_PET_FOOD
;
3926 if(!pet
->GetCurrentFoodBenefitLevel(m_targets
.getItemTarget()->GetProto()->ItemLevel
))
3927 return SPELL_FAILED_FOOD_LOWLEVEL
;
3929 if(m_caster
->isInCombat() || pet
->isInCombat())
3930 return SPELL_FAILED_AFFECTING_COMBAT
;
3934 case SPELL_EFFECT_POWER_BURN
:
3935 case SPELL_EFFECT_POWER_DRAIN
:
3937 // Can be area effect, Check only for players and not check if target - caster (spell can have multiply drain/burn effects)
3938 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
3939 if(Unit
* target
= m_targets
.getUnitTarget())
3940 if(target
!=m_caster
&& target
->getPowerType()!=m_spellInfo
->EffectMiscValue
[i
])
3941 return SPELL_FAILED_BAD_TARGETS
;
3944 case SPELL_EFFECT_CHARGE
:
3946 if (m_caster
->hasUnitState(UNIT_STAT_ROOT
))
3947 return SPELL_FAILED_ROOTED
;
3951 case SPELL_EFFECT_SKINNING
:
3953 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| !m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetTypeId() != TYPEID_UNIT
)
3954 return SPELL_FAILED_BAD_TARGETS
;
3956 if( !(m_targets
.getUnitTarget()->GetUInt32Value(UNIT_FIELD_FLAGS
) & UNIT_FLAG_SKINNABLE
) )
3957 return SPELL_FAILED_TARGET_UNSKINNABLE
;
3959 Creature
* creature
= (Creature
*)m_targets
.getUnitTarget();
3960 if ( creature
->GetCreatureType() != CREATURE_TYPE_CRITTER
&& ( !creature
->lootForBody
|| !creature
->loot
.empty() ) )
3962 return SPELL_FAILED_TARGET_NOT_LOOTED
;
3965 uint32 skill
= creature
->GetCreatureInfo()->GetRequiredLootSkill();
3967 int32 skillValue
= ((Player
*)m_caster
)->GetSkillValue(skill
);
3968 int32 TargetLevel
= m_targets
.getUnitTarget()->getLevel();
3969 int32 ReqValue
= (skillValue
< 100 ? (TargetLevel
-10)*10 : TargetLevel
*5);
3970 if (ReqValue
> skillValue
)
3971 return SPELL_FAILED_LOW_CASTLEVEL
;
3973 // chance for fail at orange skinning attempt
3974 if( (m_selfContainer
&& (*m_selfContainer
) == this) &&
3975 skillValue
< sWorld
.GetConfigMaxSkillValue() &&
3976 (ReqValue
< 0 ? 0 : ReqValue
) > irand(skillValue
-25, skillValue
+37) )
3977 return SPELL_FAILED_TRY_AGAIN
;
3981 case SPELL_EFFECT_OPEN_LOCK_ITEM
:
3982 case SPELL_EFFECT_OPEN_LOCK
:
3984 if( m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT
&&
3985 m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT_ITEM
)
3988 if( m_caster
->GetTypeId() != TYPEID_PLAYER
// only players can open locks, gather etc.
3989 // we need a go target in case of TARGET_GAMEOBJECT
3990 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT
&& !m_targets
.getGOTarget()
3991 // we need a go target, or an openable item target in case of TARGET_GAMEOBJECT_ITEM
3992 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT_ITEM
&& !m_targets
.getGOTarget() &&
3993 (!m_targets
.getItemTarget() || !m_targets
.getItemTarget()->GetProto()->LockID
|| m_targets
.getItemTarget()->GetOwner() != m_caster
) )
3994 return SPELL_FAILED_BAD_TARGETS
;
3996 // In BattleGround players can use only flags and banners
3997 if( ((Player
*)m_caster
)->InBattleGround() &&
3998 !((Player
*)m_caster
)->isAllowUseBattleGroundObject() )
3999 return SPELL_FAILED_TRY_AGAIN
;
4001 // get the lock entry
4002 LockEntry
const *lockInfo
= NULL
;
4003 if (GameObject
* go
=m_targets
.getGOTarget())
4004 lockInfo
= sLockStore
.LookupEntry(go
->GetLockId());
4005 else if(Item
* itm
=m_targets
.getItemTarget())
4006 lockInfo
= sLockStore
.LookupEntry(itm
->GetProto()->LockID
);
4008 // check lock compatibility
4011 // check for lock - key pair (checked by client also, just prevent cheating
4012 bool ok_key
= false;
4013 for(int it
= 0; it
< 8; ++it
)
4015 switch(lockInfo
->Type
[it
])
4021 if(lockInfo
->Index
[it
])
4023 if(m_CastItem
&& m_CastItem
->GetEntry()==lockInfo
->Index
[it
])
4028 case LOCK_KEY_SKILL
:
4030 if(uint32(m_spellInfo
->EffectMiscValue
[i
])!=lockInfo
->Index
[it
])
4033 switch(lockInfo
->Index
[it
])
4035 case LOCKTYPE_HERBALISM
:
4036 if(((Player
*)m_caster
)->HasSkill(SKILL_HERBALISM
))
4039 case LOCKTYPE_MINING
:
4040 if(((Player
*)m_caster
)->HasSkill(SKILL_MINING
))
4054 return SPELL_FAILED_BAD_TARGETS
;
4057 // chance for fail at orange mining/herb/LockPicking gathering attempt
4058 if (!m_selfContainer
|| ((*m_selfContainer
) != this))
4061 // get the skill value of the player
4062 int32 SkillValue
= 0;
4063 bool canFailAtMax
= true;
4064 if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_HERBALISM
)
4066 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_HERBALISM
);
4067 canFailAtMax
= false;
4069 else if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_MINING
)
4071 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_MINING
);
4072 canFailAtMax
= false;
4074 else if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_PICKLOCK
)
4075 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_LOCKPICKING
);
4077 // castitem check: rogue using skeleton keys. the skill values should not be added in this case.
4081 // add the damage modifier from the spell casted (cheat lock / skeleton key etc.) (use m_currentBasePoints, CalculateDamage returns wrong value)
4082 SkillValue
+= m_currentBasePoints
[i
]+1;
4084 // get the required lock value
4088 // check for lock - key pair
4090 for(int it
= 0; it
< 8; ++it
)
4092 if(lockInfo
->Type
[it
]==LOCK_KEY_ITEM
&& lockInfo
->Index
[it
] && m_CastItem
&& m_CastItem
->GetEntry()==lockInfo
->Index
[it
])
4094 // if so, we're good to go
4102 if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_PICKLOCK
)
4103 ReqValue
= lockInfo
->Skill
[1];
4105 ReqValue
= lockInfo
->Skill
[0];
4108 // skill doesn't meet the required value
4109 if (ReqValue
> SkillValue
)
4110 return SPELL_FAILED_LOW_CASTLEVEL
;
4112 // chance for failure in orange gather / lockpick (gathering skill can't fail at maxskill)
4113 if((canFailAtMax
|| SkillValue
< sWorld
.GetConfigMaxSkillValue()) && ReqValue
> irand(SkillValue
-25, SkillValue
+37))
4114 return SPELL_FAILED_TRY_AGAIN
;
4118 case SPELL_EFFECT_SUMMON_DEAD_PET
:
4120 Creature
*pet
= m_caster
->GetPet();
4122 return SPELL_FAILED_NO_PET
;
4125 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4129 // This is generic summon effect now and don't make this check for summon types similar
4130 // SPELL_EFFECT_SUMMON_CRITTER, SPELL_EFFECT_SUMMON_WILD or SPELL_EFFECT_SUMMON_GUARDIAN.
4131 // These won't show up in m_caster->GetPetGUID()
4132 case SPELL_EFFECT_SUMMON
:
4134 switch(m_spellInfo
->EffectMiscValueB
[i
])
4136 case SUMMON_TYPE_POSESSED
:
4137 case SUMMON_TYPE_POSESSED2
:
4138 case SUMMON_TYPE_DEMON
:
4139 case SUMMON_TYPE_SUMMON
:
4141 if(m_caster
->GetPetGUID())
4142 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4144 if(m_caster
->GetCharmGUID())
4145 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4151 // Don't make this check for SPELL_EFFECT_SUMMON_CRITTER, SPELL_EFFECT_SUMMON_WILD or SPELL_EFFECT_SUMMON_GUARDIAN.
4152 // These won't show up in m_caster->GetPetGUID()
4153 case SPELL_EFFECT_SUMMON_PHANTASM
:
4154 case SPELL_EFFECT_SUMMON_DEMON
:
4156 if(m_caster
->GetPetGUID())
4157 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4159 if(m_caster
->GetCharmGUID())
4160 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4164 case SPELL_EFFECT_SUMMON_PET
:
4166 if(m_caster
->GetPetGUID()) //let warlock do a replacement summon
4169 Pet
* pet
= ((Player
*)m_caster
)->GetPet();
4171 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& m_caster
->getClass()==CLASS_WARLOCK
)
4173 if (strict
) //starting cast, trigger pet stun (cast by pet so it doesn't attack player)
4174 pet
->CastSpell(pet
, 32752, true, NULL
, NULL
, pet
->GetGUID());
4177 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4180 if(m_caster
->GetCharmGUID())
4181 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4185 case SPELL_EFFECT_SUMMON_PLAYER
:
4187 if(m_caster
->GetTypeId()!=TYPEID_PLAYER
)
4188 return SPELL_FAILED_BAD_TARGETS
;
4189 if(!((Player
*)m_caster
)->GetSelection())
4190 return SPELL_FAILED_BAD_TARGETS
;
4192 Player
* target
= objmgr
.GetPlayer(((Player
*)m_caster
)->GetSelection());
4193 if( !target
|| ((Player
*)m_caster
)==target
|| !target
->IsInSameRaidWith((Player
*)m_caster
) )
4194 return SPELL_FAILED_BAD_TARGETS
;
4196 // check if our map is dungeon
4197 if( sMapStore
.LookupEntry(m_caster
->GetMapId())->IsDungeon() )
4199 InstanceTemplate
const* instance
= ObjectMgr::GetInstanceTemplate(m_caster
->GetMapId());
4201 return SPELL_FAILED_TARGET_NOT_IN_INSTANCE
;
4202 if ( instance
->levelMin
> target
->getLevel() )
4203 return SPELL_FAILED_LOWLEVEL
;
4204 if ( instance
->levelMax
&& instance
->levelMax
< target
->getLevel() )
4205 return SPELL_FAILED_HIGHLEVEL
;
4209 case SPELL_EFFECT_LEAP
:
4210 case SPELL_EFFECT_TELEPORT_UNITS_FACE_CASTER
:
4212 float dis
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[i
]));
4213 float fx
= m_caster
->GetPositionX() + dis
* cos(m_caster
->GetOrientation());
4214 float fy
= m_caster
->GetPositionY() + dis
* sin(m_caster
->GetOrientation());
4215 // teleport a bit above terrain level to avoid falling below it
4216 float fz
= MapManager::Instance().GetBaseMap(m_caster
->GetMapId())->GetHeight(fx
,fy
,m_caster
->GetPositionZ(),true);
4217 if(fz
<= INVALID_HEIGHT
) // note: this also will prevent use effect in instances without vmaps height enabled
4218 return SPELL_FAILED_TRY_AGAIN
;
4220 float caster_pos_z
= m_caster
->GetPositionZ();
4221 // Control the caster to not climb or drop when +-fz > 8
4222 if(!(fz
<=caster_pos_z
+8 && fz
>=caster_pos_z
-8))
4223 return SPELL_FAILED_TRY_AGAIN
;
4225 // not allow use this effect at battleground until battleground start
4226 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
4227 if(BattleGround
const *bg
= ((Player
*)m_caster
)->GetBattleGround())
4228 if(bg
->GetStatus() != STATUS_IN_PROGRESS
)
4229 return SPELL_FAILED_TRY_AGAIN
;
4232 case SPELL_EFFECT_STEAL_BENEFICIAL_BUFF
:
4234 if (m_targets
.getUnitTarget()==m_caster
)
4235 return SPELL_FAILED_BAD_TARGETS
;
4242 for (int i
= 0; i
< 3; i
++)
4244 switch(m_spellInfo
->EffectApplyAuraName
[i
])
4246 case SPELL_AURA_MOD_POSSESS
:
4247 case SPELL_AURA_MOD_CHARM
:
4249 if(m_caster
->GetPetGUID())
4250 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4252 if(m_caster
->GetCharmGUID())
4253 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4255 if(m_caster
->GetCharmerGUID())
4256 return SPELL_FAILED_CHARMED
;
4258 if(!m_targets
.getUnitTarget())
4259 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4261 if(m_targets
.getUnitTarget()->GetCharmerGUID())
4262 return SPELL_FAILED_CHARMED
;
4264 if(int32(m_targets
.getUnitTarget()->getLevel()) > CalculateDamage(i
,m_targets
.getUnitTarget()))
4265 return SPELL_FAILED_HIGHLEVEL
;
4269 case SPELL_AURA_MOUNTED
:
4271 if (m_caster
->IsInWater())
4272 return SPELL_FAILED_ONLY_ABOVEWATER
;
4274 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->GetTransport())
4275 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4277 // Ignore map check if spell have AreaId. AreaId already checked and this prevent special mount spells
4278 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& !sMapStore
.LookupEntry(m_caster
->GetMapId())->IsMountAllowed() && !m_IsTriggeredSpell
&& !m_spellInfo
->AreaId
)
4279 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4281 if (m_caster
->GetAreaId()==35)
4282 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4284 ShapeshiftForm form
= m_caster
->m_form
;
4285 if( form
== FORM_CAT
|| form
== FORM_TREE
|| form
== FORM_TRAVEL
||
4286 form
== FORM_AQUA
|| form
== FORM_BEAR
|| form
== FORM_DIREBEAR
||
4287 form
== FORM_CREATUREBEAR
|| form
== FORM_GHOSTWOLF
|| form
== FORM_FLIGHT
||
4288 form
== FORM_FLIGHT_EPIC
|| form
== FORM_MOONKIN
|| form
== FORM_METAMORPHOSIS
)
4289 return SPELL_FAILED_NOT_SHAPESHIFT
;
4293 case SPELL_AURA_RANGED_ATTACK_POWER_ATTACKER_BONUS
:
4295 if(!m_targets
.getUnitTarget())
4296 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4298 // can be casted at non-friendly unit or own pet/charm
4299 if(m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()))
4300 return SPELL_FAILED_TARGET_FRIENDLY
;
4304 case SPELL_AURA_MOD_INCREASE_FLIGHT_SPEED
:
4305 case SPELL_AURA_FLY
:
4307 // not allow cast fly spells at old maps by players (all spells is self target)
4308 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
4310 uint32 v_map
= GetVirtualMapForMapAndZone(m_caster
->GetMapId(), m_caster
->GetZoneId());
4311 if( !((Player
*)m_caster
)->isGameMaster() && v_map
!= 530 && !(v_map
== 571 && ((Player
*)m_caster
)->HasSpell(54197)))
4312 return SPELL_FAILED_NOT_HERE
;
4317 case SPELL_AURA_PERIODIC_MANA_LEECH
:
4319 if (!m_targets
.getUnitTarget())
4320 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4322 if (m_caster
->GetTypeId()!=TYPEID_PLAYER
|| m_CastItem
)
4325 if(m_targets
.getUnitTarget()->getPowerType()!=POWER_MANA
)
4326 return SPELL_FAILED_BAD_TARGETS
;
4339 int16
Spell::PetCanCast(Unit
* target
)
4341 if(!m_caster
->isAlive())
4342 return SPELL_FAILED_CASTER_DEAD
;
4344 if(m_caster
->IsNonMeleeSpellCasted(false)) //prevent spellcast interruption by another spellcast
4345 return SPELL_FAILED_SPELL_IN_PROGRESS
;
4346 if(m_caster
->isInCombat() && IsNonCombatSpell(m_spellInfo
))
4347 return SPELL_FAILED_AFFECTING_COMBAT
;
4349 if(m_caster
->GetTypeId()==TYPEID_UNIT
&& (((Creature
*)m_caster
)->isPet() || m_caster
->isCharmed()))
4351 //dead owner (pets still alive when owners ressed?)
4352 if(m_caster
->GetCharmerOrOwner() && !m_caster
->GetCharmerOrOwner()->isAlive())
4353 return SPELL_FAILED_CASTER_DEAD
;
4355 if(!target
&& m_targets
.getUnitTarget())
4356 target
= m_targets
.getUnitTarget();
4359 for(uint32 i
= 0;i
<3;i
++)
4361 if(m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_CHAIN_DAMAGE
|| m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SINGLE_FRIEND
|| m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_DUELVSPLAYER
|| m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SINGLE_PARTY
|| m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_CURRENT_ENEMY_COORDINATES
)
4365 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4370 m_targets
.setUnitTarget(target
);
4372 Unit
* _target
= m_targets
.getUnitTarget();
4374 if(_target
) //for target dead/target not valid
4376 if(!_target
->isAlive())
4377 return SPELL_FAILED_BAD_TARGETS
;
4379 if(IsPositiveSpell(m_spellInfo
->Id
))
4381 if(m_caster
->IsHostileTo(_target
))
4382 return SPELL_FAILED_BAD_TARGETS
;
4386 bool duelvsplayertar
= false;
4387 for(int j
=0;j
<3;j
++)
4389 //TARGET_DUELVSPLAYER is positive AND negative
4390 duelvsplayertar
|= (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_DUELVSPLAYER
);
4392 if(m_caster
->IsFriendlyTo(target
) && !duelvsplayertar
)
4394 return SPELL_FAILED_BAD_TARGETS
;
4399 if(((Creature
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
4400 return SPELL_FAILED_NOT_READY
;
4403 uint16 result
= CanCast(true);
4407 return -1; //this allows to check spell fail 0, in combat
4410 uint8
Spell::CheckCasterAuras() const
4412 // Flag drop spells totally immuned to caster auras
4413 // FIXME: find more nice check for all totally immuned spells
4414 // AttributesEx3 & 0x10000000?
4415 if(m_spellInfo
->Id
==23336 || m_spellInfo
->Id
==23334 || m_spellInfo
->Id
==34991)
4418 uint8 school_immune
= 0;
4419 uint32 mechanic_immune
= 0;
4420 uint32 dispel_immune
= 0;
4422 //Check if the spell grants school or mechanic immunity.
4423 //We use bitmasks so the loop is done only once and not on every aura check below.
4424 if ( m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DISPEL_AURAS_ON_IMMUNITY
)
4426 for(int i
= 0;i
< 3; i
++)
4428 if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_SCHOOL_IMMUNITY
)
4429 school_immune
|= uint32(m_spellInfo
->EffectMiscValue
[i
]);
4430 else if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_MECHANIC_IMMUNITY
)
4431 mechanic_immune
|= 1 << uint32(m_spellInfo
->EffectMiscValue
[i
]);
4432 else if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_DISPEL_IMMUNITY
)
4433 dispel_immune
|= GetDispellMask(DispelType(m_spellInfo
->EffectMiscValue
[i
]));
4435 //immune movement impairment and loss of control
4436 if(m_spellInfo
->Id
==(uint32
)42292)
4437 mechanic_immune
= IMMUNE_TO_MOVEMENT_IMPAIRMENT_AND_LOSS_CONTROL_MASK
;
4440 //Check whether the cast should be prevented by any state you might have.
4441 uint8 prevented_reason
= 0;
4442 // Have to check if there is a stun aura. Otherwise will have problems with ghost aura apply while logging out
4443 if(!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
) && m_caster
->HasAuraType(SPELL_AURA_MOD_STUN
))
4444 prevented_reason
= SPELL_FAILED_STUNNED
;
4445 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_CONFUSED
) && !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
4446 prevented_reason
= SPELL_FAILED_CONFUSED
;
4447 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_FLEEING
) && !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
4448 prevented_reason
= SPELL_FAILED_FLEEING
;
4449 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_SILENCED
) && m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_SILENCE
)
4450 prevented_reason
= SPELL_FAILED_SILENCED
;
4451 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PACIFIED
) && m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_PACIFY
)
4452 prevented_reason
= SPELL_FAILED_PACIFIED
;
4454 // Attr must make flag drop spell totally immune from all effects
4455 if(prevented_reason
)
4457 if(school_immune
|| mechanic_immune
|| dispel_immune
)
4459 //Checking auras is needed now, because you are prevented by some state but the spell grants immunity.
4460 Unit::AuraMap
const& auras
= m_caster
->GetAuras();
4461 for(Unit::AuraMap::const_iterator itr
= auras
.begin(); itr
!= auras
.end(); ++itr
)
4465 if( GetSpellMechanicMask(itr
->second
->GetSpellProto(), itr
->second
->GetEffIndex()) & mechanic_immune
)
4467 if( GetSpellSchoolMask(itr
->second
->GetSpellProto()) & school_immune
)
4469 if( (1<<(itr
->second
->GetSpellProto()->Dispel
)) & dispel_immune
)
4472 //Make a second check for spell failed so the right SPELL_FAILED message is returned.
4473 //That is needed when your casting is prevented by multiple states and you are only immune to some of them.
4474 switch(itr
->second
->GetModifier()->m_auraname
)
4476 case SPELL_AURA_MOD_STUN
:
4477 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
))
4478 return SPELL_FAILED_STUNNED
;
4480 case SPELL_AURA_MOD_CONFUSE
:
4481 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
4482 return SPELL_FAILED_CONFUSED
;
4484 case SPELL_AURA_MOD_FEAR
:
4485 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
4486 return SPELL_FAILED_FLEEING
;
4488 case SPELL_AURA_MOD_SILENCE
:
4489 case SPELL_AURA_MOD_PACIFY
:
4490 case SPELL_AURA_MOD_PACIFY_SILENCE
:
4491 if( m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_PACIFY
)
4492 return SPELL_FAILED_PACIFIED
;
4493 else if ( m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_SILENCE
)
4494 return SPELL_FAILED_SILENCED
;
4500 //You are prevented from casting and the spell casted does not grant immunity. Return a failed error.
4502 return prevented_reason
;
4507 bool Spell::CanAutoCast(Unit
* target
)
4509 uint64 targetguid
= target
->GetGUID();
4511 for(uint32 j
= 0;j
<3;j
++)
4513 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_APPLY_AURA
)
4515 if( m_spellInfo
->StackAmount
<= 1)
4517 if( target
->HasAura(m_spellInfo
->Id
, j
) )
4522 if( target
->GetAuras().count(Unit::spellEffectPair(m_spellInfo
->Id
, j
)) >= m_spellInfo
->StackAmount
)
4526 else if ( IsAreaAuraEffect( m_spellInfo
->Effect
[j
] ))
4528 if( target
->HasAura(m_spellInfo
->Id
, j
) )
4533 int16 result
= PetCanCast(target
);
4535 if(result
== -1 || result
== SPELL_FAILED_UNIT_NOT_INFRONT
)
4538 //check if among target units, our WANTED target is as well (->only self cast spells return false)
4539 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
4540 if( ihit
->targetGUID
== targetguid
)
4543 return false; //target invalid
4546 uint8
Spell::CheckRange(bool strict
)
4550 // self cast doesn't need range checking -- also for Starshards fix
4551 if (m_spellInfo
->rangeIndex
== 1) return 0;
4553 if (strict
) //add radius of caster
4555 else //add radius of caster and ~5 yds "give"
4558 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
4559 float max_range
= GetSpellMaxRange(srange
) + range_mod
;
4560 float min_range
= GetSpellMinRange(srange
);
4562 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
4563 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_RANGE
, max_range
, this);
4565 Unit
*target
= m_targets
.getUnitTarget();
4567 if(target
&& target
!= m_caster
)
4569 // distance from target center in checks
4570 float dist
= m_caster
->GetDistance(target
->GetPositionX(),target
->GetPositionY(),target
->GetPositionZ());
4571 if(dist
> max_range
)
4572 return SPELL_FAILED_OUT_OF_RANGE
; //0x5A;
4573 if(dist
< min_range
)
4574 return SPELL_FAILED_TOO_CLOSE
;
4575 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&&
4576 (m_spellInfo
->FacingCasterFlags
& SPELL_FACING_FLAG_INFRONT
) && !m_caster
->HasInArc( M_PI
, target
) )
4577 return SPELL_FAILED_UNIT_NOT_INFRONT
;
4580 if(m_targets
.m_targetMask
== TARGET_FLAG_DEST_LOCATION
&& m_targets
.m_destX
!= 0 && m_targets
.m_destY
!= 0 && m_targets
.m_destZ
!= 0)
4582 float dist
= m_caster
->GetDistance(m_targets
.m_destX
, m_targets
.m_destY
, m_targets
.m_destZ
);
4583 if(dist
> max_range
)
4584 return SPELL_FAILED_OUT_OF_RANGE
;
4585 if(dist
< min_range
)
4586 return SPELL_FAILED_TOO_CLOSE
;
4592 int32
Spell::CalculatePowerCost()
4594 // item cast not used power
4598 // Spell drain all exist power on cast (Only paladin lay of Hands)
4599 if (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DRAIN_ALL_POWER
)
4601 // If power type - health drain all
4602 if (m_spellInfo
->powerType
== POWER_HEALTH
)
4603 return m_caster
->GetHealth();
4604 // Else drain all power
4605 if (m_spellInfo
->powerType
< MAX_POWERS
)
4606 return m_caster
->GetPower(Powers(m_spellInfo
->powerType
));
4607 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
4612 int32 powerCost
= m_spellInfo
->manaCost
;
4613 // PCT cost from total amount
4614 if (m_spellInfo
->ManaCostPercentage
)
4616 switch (m_spellInfo
->powerType
)
4618 // health as power used
4620 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateHealth() / 100;
4623 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateMana() / 100;
4628 case POWER_HAPPINESS
:
4629 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetMaxPower(Powers(m_spellInfo
->powerType
)) / 100;
4632 case POWER_RUNIC_POWER
:
4633 sLog
.outDebug("Spell::CalculateManaCost: Not implemented yet!");
4636 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
4640 SpellSchools school
= GetFirstSchoolInMask(m_spellSchoolMask
);
4641 // Flat mod from caster auras by spell school
4642 powerCost
+= m_caster
->GetInt32Value(UNIT_FIELD_POWER_COST_MODIFIER
+ school
);
4643 // Shiv - costs 20 + weaponSpeed*10 energy (apply only to non-triggered spell with energy cost)
4644 if ( m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_SPELL_VS_EXTEND_COST
)
4645 powerCost
+= m_caster
->GetAttackTime(OFF_ATTACK
)/100;
4646 // Apply cost mod by spell
4647 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
4648 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COST
, powerCost
, this);
4650 if(m_spellInfo
->Attributes
& SPELL_ATTR_LEVEL_DAMAGE_CALCULATION
)
4651 powerCost
= int32(powerCost
/ (1.117f
* m_spellInfo
->spellLevel
/ m_caster
->getLevel() -0.1327f
));
4653 // PCT mod from user auras by school
4654 powerCost
= int32(powerCost
* (1.0f
+m_caster
->GetFloatValue(UNIT_FIELD_POWER_COST_MULTIPLIER
+school
)));
4660 uint8
Spell::CheckPower()
4662 // item cast not used power
4666 // health as power used - need check health amount
4667 if(m_spellInfo
->powerType
== POWER_HEALTH
)
4669 if(m_caster
->GetHealth() <= m_powerCost
)
4670 return SPELL_FAILED_CASTER_AURASTATE
;
4673 // Check valid power type
4674 if( m_spellInfo
->powerType
>= MAX_POWERS
)
4676 sLog
.outError("Spell::CheckMana: Unknown power type '%d'", m_spellInfo
->powerType
);
4677 return SPELL_FAILED_UNKNOWN
;
4680 uint8 failReason
= CheckRuneCost(m_spellInfo
->runeCostID
);
4684 // Check power amount
4685 Powers powerType
= Powers(m_spellInfo
->powerType
);
4686 if(m_caster
->GetPower(powerType
) < m_powerCost
)
4687 return SPELL_FAILED_NO_POWER
;
4692 uint8
Spell::CheckItems()
4694 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
4697 uint32 itemid
, itemcount
;
4698 Player
* p_caster
= (Player
*)m_caster
;
4702 itemid
= m_CastItem
->GetEntry();
4703 if( !p_caster
->HasItemCount(itemid
,1) )
4704 return SPELL_FAILED_ITEM_NOT_READY
;
4707 ItemPrototype
const *proto
= m_CastItem
->GetProto();
4709 return SPELL_FAILED_ITEM_NOT_READY
;
4711 for (int i
= 0; i
<5; i
++)
4713 if (proto
->Spells
[i
].SpellCharges
)
4715 if(m_CastItem
->GetSpellCharges(i
)==0)
4716 return SPELL_FAILED_NO_CHARGES_REMAIN
;
4720 uint32 ItemClass
= proto
->Class
;
4721 if (ItemClass
== ITEM_CLASS_CONSUMABLE
&& m_targets
.getUnitTarget())
4723 // such items should only fail if there is no suitable effect at all - see Rejuvenation Potions for example
4724 uint8 failReason
= 0;
4725 for (int i
= 0; i
< 3; i
++)
4727 // skip check, pet not required like checks, and for TARGET_PET m_targets.getUnitTarget() is not the real target but the caster
4728 if (m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_PET
)
4731 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_HEAL
)
4733 if (m_targets
.getUnitTarget()->GetHealth() == m_targets
.getUnitTarget()->GetMaxHealth())
4735 failReason
= (uint8
)SPELL_FAILED_ALREADY_AT_FULL_HEALTH
;
4745 // Mana Potion, Rage Potion, Thistle Tea(Rogue), ...
4746 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_ENERGIZE
)
4748 if(m_spellInfo
->EffectMiscValue
[i
] < 0 || m_spellInfo
->EffectMiscValue
[i
] >= MAX_POWERS
)
4750 failReason
= (uint8
)SPELL_FAILED_ALREADY_AT_FULL_POWER
;
4754 Powers power
= Powers(m_spellInfo
->EffectMiscValue
[i
]);
4755 if (m_targets
.getUnitTarget()->GetPower(power
) == m_targets
.getUnitTarget()->GetMaxPower(power
))
4757 failReason
= (uint8
)SPELL_FAILED_ALREADY_AT_FULL_POWER
;
4773 if(m_targets
.getItemTargetGUID())
4775 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
4776 return SPELL_FAILED_BAD_TARGETS
;
4778 if(!m_targets
.getItemTarget())
4779 return SPELL_FAILED_ITEM_GONE
;
4781 if(!m_targets
.getItemTarget()->IsFitToSpellRequirements(m_spellInfo
))
4782 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
4784 // if not item target then required item must be equipped
4787 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& !((Player
*)m_caster
)->HasItemFitToSpellReqirements(m_spellInfo
))
4788 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
4791 if(m_spellInfo
->RequiresSpellFocus
)
4793 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
4795 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
4797 GameObject
* ok
= NULL
;
4798 MaNGOS::GameObjectFocusCheck
go_check(m_caster
,m_spellInfo
->RequiresSpellFocus
);
4799 MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
> checker(ok
,go_check
);
4801 TypeContainerVisitor
<MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
>, GridTypeMapContainer
> object_checker(checker
);
4802 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
4803 cell_lock
->Visit(cell_lock
, object_checker
, *m_caster
->GetMap());
4806 return (uint8
)SPELL_FAILED_REQUIRES_SPELL_FOCUS
;
4808 focusObject
= ok
; // game object found in range
4811 if (!p_caster
->CanNoReagentCast(m_spellInfo
))
4813 for(uint32 i
=0;i
<8;i
++)
4815 if(m_spellInfo
->Reagent
[i
] <= 0)
4818 itemid
= m_spellInfo
->Reagent
[i
];
4819 itemcount
= m_spellInfo
->ReagentCount
[i
];
4821 // if CastItem is also spell reagent
4822 if( m_CastItem
&& m_CastItem
->GetEntry() == itemid
)
4824 ItemPrototype
const *proto
= m_CastItem
->GetProto();
4826 return SPELL_FAILED_ITEM_NOT_READY
;
4827 for(int s
=0;s
<5;s
++)
4829 // CastItem will be used up and does not count as reagent
4830 int32 charges
= m_CastItem
->GetSpellCharges(s
);
4831 if (proto
->Spells
[s
].SpellCharges
< 0 && abs(charges
) < 2)
4838 if( !p_caster
->HasItemCount(itemid
,itemcount
) )
4839 return (uint8
)SPELL_FAILED_ITEM_NOT_READY
; //0x54
4844 for(int i
=0;i
<2;++i
)
4846 if(m_spellInfo
->Totem
[i
] != 0)
4848 if( p_caster
->HasItemCount(m_spellInfo
->Totem
[i
],1) )
4857 return (uint8
)SPELL_FAILED_TOTEMS
; //0x7C
4859 //Check items for TotemCategory
4860 uint32 TotemCategory
= 2;
4861 for(int i
=0;i
<2;++i
)
4863 if(m_spellInfo
->TotemCategory
[i
] != 0)
4865 if( p_caster
->HasItemTotemCategory(m_spellInfo
->TotemCategory
[i
]) )
4874 if(TotemCategory
!= 0)
4875 return (uint8
)SPELL_FAILED_TOTEM_CATEGORY
; //0x7B
4877 for(int i
= 0; i
< 3; i
++)
4879 switch (m_spellInfo
->Effect
[i
])
4881 case SPELL_EFFECT_CREATE_ITEM
:
4883 if (!m_IsTriggeredSpell
&& m_spellInfo
->EffectItemType
[i
])
4885 ItemPosCountVec dest
;
4886 uint8 msg
= p_caster
->CanStoreNewItem(NULL_BAG
, NULL_SLOT
, dest
, m_spellInfo
->EffectItemType
[i
], 1 );
4887 if (msg
!= EQUIP_ERR_OK
)
4889 p_caster
->SendEquipError( msg
, NULL
, NULL
);
4890 return SPELL_FAILED_DONT_REPORT
;
4895 case SPELL_EFFECT_ENCHANT_ITEM
:
4897 Item
* targetItem
= m_targets
.getItemTarget();
4899 return SPELL_FAILED_ITEM_NOT_FOUND
;
4901 if( targetItem
->GetProto()->ItemLevel
< m_spellInfo
->baseLevel
)
4902 return SPELL_FAILED_LOWLEVEL
;
4903 // Not allow enchant in trade slot for some enchant type
4904 if( targetItem
->GetOwner() != m_caster
)
4906 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
4907 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
4909 return SPELL_FAILED_ERROR
;
4910 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
4911 return SPELL_FAILED_NOT_TRADEABLE
;
4915 case SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY
:
4917 Item
*item
= m_targets
.getItemTarget();
4919 return SPELL_FAILED_ITEM_NOT_FOUND
;
4920 // Not allow enchant in trade slot for some enchant type
4921 if( item
->GetOwner() != m_caster
)
4923 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
4924 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
4926 return SPELL_FAILED_ERROR
;
4927 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
4928 return SPELL_FAILED_NOT_TRADEABLE
;
4932 case SPELL_EFFECT_ENCHANT_HELD_ITEM
:
4933 // check item existence in effect code (not output errors at offhand hold item effect to main hand for example
4935 case SPELL_EFFECT_DISENCHANT
:
4937 if(!m_targets
.getItemTarget())
4938 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4940 // prevent disenchanting in trade slot
4941 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4942 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4944 ItemPrototype
const* itemProto
= m_targets
.getItemTarget()->GetProto();
4946 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4948 uint32 item_quality
= itemProto
->Quality
;
4949 // 2.0.x addon: Check player enchanting level against the item disenchanting requirements
4950 uint32 item_disenchantskilllevel
= itemProto
->RequiredDisenchantSkill
;
4951 if (item_disenchantskilllevel
== uint32(-1))
4952 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4953 if (item_disenchantskilllevel
> p_caster
->GetSkillValue(SKILL_ENCHANTING
))
4954 return SPELL_FAILED_LOW_CASTLEVEL
;
4955 if(item_quality
> 4 || item_quality
< 2)
4956 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4957 if(itemProto
->Class
!= ITEM_CLASS_WEAPON
&& itemProto
->Class
!= ITEM_CLASS_ARMOR
)
4958 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4959 if (!itemProto
->DisenchantID
)
4960 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4963 case SPELL_EFFECT_PROSPECTING
:
4965 if(!m_targets
.getItemTarget())
4966 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4967 //ensure item is a prospectable ore
4968 if(!(m_targets
.getItemTarget()->GetProto()->BagFamily
& BAG_FAMILY_MASK_MINING_SUPP
) || m_targets
.getItemTarget()->GetProto()->Class
!= ITEM_CLASS_TRADE_GOODS
)
4969 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4970 //prevent prospecting in trade slot
4971 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4972 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4973 //Check for enough skill in jewelcrafting
4974 uint32 item_prospectingskilllevel
= m_targets
.getItemTarget()->GetProto()->RequiredSkillRank
;
4975 if(item_prospectingskilllevel
>p_caster
->GetSkillValue(SKILL_JEWELCRAFTING
))
4976 return SPELL_FAILED_LOW_CASTLEVEL
;
4977 //make sure the player has the required ores in inventory
4978 if(m_targets
.getItemTarget()->GetCount() < 5)
4979 return SPELL_FAILED_NEED_MORE_ITEMS
;
4981 if(!LootTemplates_Prospecting
.HaveLootFor(m_targets
.getItemTargetEntry()))
4982 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4986 case SPELL_EFFECT_MILLING
:
4988 if(!m_targets
.getItemTarget())
4989 return SPELL_FAILED_CANT_BE_MILLED
;
4990 //ensure item is a millable herb
4991 if(!(m_targets
.getItemTarget()->GetProto()->BagFamily
& BAG_FAMILY_MASK_HERBS
) || m_targets
.getItemTarget()->GetProto()->Class
!= ITEM_CLASS_TRADE_GOODS
)
4992 return SPELL_FAILED_CANT_BE_MILLED
;
4993 //prevent milling in trade slot
4994 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4995 return SPELL_FAILED_CANT_BE_MILLED
;
4996 //Check for enough skill in inscription
4997 uint32 item_millingskilllevel
= m_targets
.getItemTarget()->GetProto()->RequiredSkillRank
;
4998 if(item_millingskilllevel
>p_caster
->GetSkillValue(SKILL_INSCRIPTION
))
4999 return SPELL_FAILED_LOW_CASTLEVEL
;
5000 //make sure the player has the required herbs in inventory
5001 if(m_targets
.getItemTarget()->GetCount() < 5)
5002 return SPELL_FAILED_NEED_MORE_ITEMS
;
5004 if(!LootTemplates_Milling
.HaveLootFor(m_targets
.getItemTargetEntry()))
5005 return SPELL_FAILED_CANT_BE_MILLED
;
5009 case SPELL_EFFECT_WEAPON_DAMAGE
:
5010 case SPELL_EFFECT_WEAPON_DAMAGE_NOSCHOOL
:
5012 if(m_caster
->GetTypeId() != TYPEID_PLAYER
) return SPELL_FAILED_TARGET_NOT_PLAYER
;
5013 if( m_attackType
!= RANGED_ATTACK
)
5015 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack(m_attackType
);
5016 if(!pItem
|| pItem
->IsBroken())
5017 return SPELL_FAILED_EQUIPPED_ITEM
;
5019 switch(pItem
->GetProto()->SubClass
)
5021 case ITEM_SUBCLASS_WEAPON_THROWN
:
5023 uint32 ammo
= pItem
->GetEntry();
5024 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
5025 return SPELL_FAILED_NO_AMMO
;
5027 case ITEM_SUBCLASS_WEAPON_GUN
:
5028 case ITEM_SUBCLASS_WEAPON_BOW
:
5029 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
5031 uint32 ammo
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
5035 if(m_caster
->GetDummyAura(46699))
5036 break; // skip other checks
5038 return SPELL_FAILED_NO_AMMO
;
5041 ItemPrototype
const *ammoProto
= objmgr
.GetItemPrototype( ammo
);
5043 return SPELL_FAILED_NO_AMMO
;
5045 if(ammoProto
->Class
!= ITEM_CLASS_PROJECTILE
)
5046 return SPELL_FAILED_NO_AMMO
;
5048 // check ammo ws. weapon compatibility
5049 switch(pItem
->GetProto()->SubClass
)
5051 case ITEM_SUBCLASS_WEAPON_BOW
:
5052 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
5053 if(ammoProto
->SubClass
!=ITEM_SUBCLASS_ARROW
)
5054 return SPELL_FAILED_NO_AMMO
;
5056 case ITEM_SUBCLASS_WEAPON_GUN
:
5057 if(ammoProto
->SubClass
!=ITEM_SUBCLASS_BULLET
)
5058 return SPELL_FAILED_NO_AMMO
;
5061 return SPELL_FAILED_NO_AMMO
;
5064 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
5065 return SPELL_FAILED_NO_AMMO
;
5067 case ITEM_SUBCLASS_WEAPON_WAND
:
5080 void Spell::Delayed()
5082 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
5085 if (m_spellState
== SPELL_STATE_DELAYED
)
5086 return; // spell is active and can't be time-backed
5088 // spells not loosing casting time ( slam, dynamites, bombs.. )
5089 if(!(m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_DAMAGE
))
5092 //check resist chance
5093 int32 resistChance
= 100; //must be initialized to 100 for percent modifiers
5094 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
,SPELLMOD_NOT_LOSE_CASTING_TIME
,resistChance
, this);
5095 resistChance
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_RESIST_PUSHBACK
) - 100;
5096 if (roll_chance_i(resistChance
))
5099 int32 delaytime
= GetNextDelayAtDamageMsTime();
5101 if(int32(m_timer
) + delaytime
> m_casttime
)
5103 delaytime
= m_casttime
- m_timer
;
5104 m_timer
= m_casttime
;
5107 m_timer
+= delaytime
;
5109 sLog
.outDetail("Spell %u partially interrupted for (%d) ms at damage",m_spellInfo
->Id
,delaytime
);
5111 WorldPacket
data(SMSG_SPELL_DELAYED
, 8+4);
5112 data
.append(m_caster
->GetPackGUID());
5113 data
<< uint32(delaytime
);
5115 m_caster
->SendMessageToSet(&data
,true);
5118 void Spell::DelayedChannel()
5120 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
|| getState() != SPELL_STATE_CASTING
)
5123 //check resist chance
5124 int32 resistChance
= 100; //must be initialized to 100 for percent modifiers
5125 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
,SPELLMOD_NOT_LOSE_CASTING_TIME
,resistChance
, this);
5126 resistChance
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_RESIST_PUSHBACK
) - 100;
5127 if (roll_chance_i(resistChance
))
5130 int32 delaytime
= GetNextDelayAtDamageMsTime();
5132 if(int32(m_timer
) < delaytime
)
5134 delaytime
= m_timer
;
5138 m_timer
-= delaytime
;
5140 sLog
.outDebug("Spell %u partially interrupted for %i ms, new duration: %u ms", m_spellInfo
->Id
, delaytime
, m_timer
);
5142 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
5144 if ((*ihit
).missCondition
== SPELL_MISS_NONE
)
5146 Unit
* unit
= m_caster
->GetGUID()==ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
5149 for (int j
=0;j
<3;j
++)
5150 if( ihit
->effectMask
& (1<<j
) )
5151 unit
->DelayAura(m_spellInfo
->Id
, j
, delaytime
);
5157 for(int j
= 0; j
< 3; j
++)
5159 // partially interrupt persistent area auras
5160 DynamicObject
* dynObj
= m_caster
->GetDynObject(m_spellInfo
->Id
, j
);
5162 dynObj
->Delay(delaytime
);
5165 SendChannelUpdate(m_timer
);
5168 void Spell::UpdatePointers()
5170 if(m_originalCasterGUID
==m_caster
->GetGUID())
5171 m_originalCaster
= m_caster
;
5174 m_originalCaster
= ObjectAccessor::GetUnit(*m_caster
,m_originalCasterGUID
);
5175 if(m_originalCaster
&& !m_originalCaster
->IsInWorld()) m_originalCaster
= NULL
;
5178 m_targets
.Update(m_caster
);
5181 bool Spell::IsAffectedByAura(Aura
*aura
)
5183 return spellmgr
.IsAffectedByMod(m_spellInfo
, aura
->getAuraSpellMod());
5186 bool Spell::CheckTargetCreatureType(Unit
* target
) const
5188 uint32 spellCreatureTargetMask
= m_spellInfo
->TargetCreatureType
;
5190 // Curse of Doom : not find another way to fix spell target check :/
5191 if(m_spellInfo
->SpellFamilyName
==SPELLFAMILY_WARLOCK
&& m_spellInfo
->SpellFamilyFlags
== 0x0200000000LL
)
5193 // not allow cast at player
5194 if(target
->GetTypeId()==TYPEID_PLAYER
)
5197 spellCreatureTargetMask
= 0x7FF;
5200 // Dismiss Pet and Taming Lesson skipped
5201 if(m_spellInfo
->Id
== 2641 || m_spellInfo
->Id
== 23356)
5202 spellCreatureTargetMask
= 0;
5204 if (spellCreatureTargetMask
)
5206 uint32 TargetCreatureType
= target
->GetCreatureTypeMask();
5208 return !TargetCreatureType
|| (spellCreatureTargetMask
& TargetCreatureType
);
5213 CurrentSpellTypes
Spell::GetCurrentContainer()
5215 if (IsNextMeleeSwingSpell())
5216 return(CURRENT_MELEE_SPELL
);
5217 else if (IsAutoRepeat())
5218 return(CURRENT_AUTOREPEAT_SPELL
);
5219 else if (IsChanneledSpell(m_spellInfo
))
5220 return(CURRENT_CHANNELED_SPELL
);
5222 return(CURRENT_GENERIC_SPELL
);
5225 bool Spell::CheckTarget( Unit
* target
, uint32 eff
)
5227 // Check targets for creature type mask and remove not appropriate (skip explicit self target case, maybe need other explicit targets)
5228 if(m_spellInfo
->EffectImplicitTargetA
[eff
]!=TARGET_SELF
)
5230 if (!CheckTargetCreatureType(target
))
5234 // Check targets for not_selectable unit flag and remove
5235 // A player can cast spells on his pet (or other controlled unit) though in any state
5236 if (target
!= m_caster
&& target
->GetCharmerOrOwnerGUID() != m_caster
->GetGUID())
5238 // any unattackable target skipped
5239 if (target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NON_ATTACKABLE
))
5242 // unselectable targets skipped in all cases except TARGET_SCRIPT targeting
5243 // in case TARGET_SCRIPT target selected by server always and can't be cheated
5244 if( target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NOT_SELECTABLE
) &&
5245 m_spellInfo
->EffectImplicitTargetA
[eff
] != TARGET_SCRIPT
&&
5246 m_spellInfo
->EffectImplicitTargetB
[eff
] != TARGET_SCRIPT
)
5250 //Check player targets and remove if in GM mode or GM invisibility (for not self casting case)
5251 if( target
!= m_caster
&& target
->GetTypeId()==TYPEID_PLAYER
)
5253 if(((Player
*)target
)->GetVisibility()==VISIBILITY_OFF
)
5256 if(((Player
*)target
)->isGameMaster() && !IsPositiveSpell(m_spellInfo
->Id
))
5260 //Check targets for LOS visibility (except spells without range limitations )
5261 switch(m_spellInfo
->Effect
[eff
])
5263 case SPELL_EFFECT_SUMMON_PLAYER
: // from anywhere
5265 case SPELL_EFFECT_DUMMY
:
5266 if(m_spellInfo
->Id
!=20577) // Cannibalize
5269 case SPELL_EFFECT_RESURRECT_NEW
:
5270 // player far away, maybe his corpse near?
5271 if(target
!=m_caster
&& !target
->IsWithinLOSInMap(m_caster
))
5273 if(!m_targets
.getCorpseTargetGUID())
5276 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
5280 if(target
->GetGUID()!=corpse
->GetOwnerGUID())
5283 if(!corpse
->IsWithinLOSInMap(m_caster
))
5287 // all ok by some way or another, skip normal check
5289 default: // normal case
5290 if(target
!=m_caster
&& !target
->IsWithinLOSInMap(m_caster
))
5298 Unit
* Spell::SelectMagnetTarget()
5300 Unit
* target
= m_targets
.getUnitTarget();
5302 if(target
&& target
->HasAuraType(SPELL_AURA_SPELL_MAGNET
) && !(m_spellInfo
->Attributes
& 0x10))
5304 Unit::AuraList
const& magnetAuras
= target
->GetAurasByType(SPELL_AURA_SPELL_MAGNET
);
5305 for(Unit::AuraList::const_iterator itr
= magnetAuras
.begin(); itr
!= magnetAuras
.end(); ++itr
)
5307 if(Unit
* magnet
= (*itr
)->GetCaster())
5309 if(magnet
->IsWithinLOSInMap(m_caster
))
5312 m_targets
.setUnitTarget(target
);
5322 bool Spell::IsNeedSendToClient() const
5324 return m_spellInfo
->SpellVisual
!=0 || IsChanneledSpell(m_spellInfo
) ||
5325 m_spellInfo
->speed
> 0.0f
|| !m_triggeredByAuraSpell
&& !m_IsTriggeredSpell
;
5328 bool Spell::HaveTargetsForEffect( uint8 effect
) const
5330 for(std::list
<TargetInfo
>::const_iterator itr
= m_UniqueTargetInfo
.begin();itr
!= m_UniqueTargetInfo
.end();++itr
)
5331 if(itr
->effectMask
& (1<<effect
))
5334 for(std::list
<GOTargetInfo
>::const_iterator itr
= m_UniqueGOTargetInfo
.begin();itr
!= m_UniqueGOTargetInfo
.end();++itr
)
5335 if(itr
->effectMask
& (1<<effect
))
5338 for(std::list
<ItemTargetInfo
>::const_iterator itr
= m_UniqueItemInfo
.begin();itr
!= m_UniqueItemInfo
.end();++itr
)
5339 if(itr
->effectMask
& (1<<effect
))
5345 SpellEvent::SpellEvent(Spell
* spell
) : BasicEvent()
5350 SpellEvent::~SpellEvent()
5352 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5355 if (m_Spell
->IsDeletable())
5361 sLog
.outError("~SpellEvent: %s %u tried to delete non-deletable spell %u. Was not deleted, causes memory leak.",
5362 (m_Spell
->GetCaster()->GetTypeId()==TYPEID_PLAYER
?"Player":"Creature"), m_Spell
->GetCaster()->GetGUIDLow(),m_Spell
->m_spellInfo
->Id
);
5366 bool SpellEvent::Execute(uint64 e_time
, uint32 p_time
)
5368 // update spell if it is not finished
5369 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5370 m_Spell
->update(p_time
);
5372 // check spell state to process
5373 switch (m_Spell
->getState())
5375 case SPELL_STATE_FINISHED
:
5377 // spell was finished, check deletable state
5378 if (m_Spell
->IsDeletable())
5380 // check, if we do have unfinished triggered spells
5382 return(true); // spell is deletable, finish event
5384 // event will be re-added automatically at the end of routine)
5387 case SPELL_STATE_CASTING
:
5389 // this spell is in channeled state, process it on the next update
5390 // event will be re-added automatically at the end of routine)
5393 case SPELL_STATE_DELAYED
:
5395 // first, check, if we have just started
5396 if (m_Spell
->GetDelayStart() != 0)
5398 // no, we aren't, do the typical update
5399 // check, if we have channeled spell on our hands
5400 if (IsChanneledSpell(m_Spell
->m_spellInfo
))
5402 // evented channeled spell is processed separately, casted once after delay, and not destroyed till finish
5403 // check, if we have casting anything else except this channeled spell and autorepeat
5404 if (m_Spell
->GetCaster()->IsNonMeleeSpellCasted(false, true, true))
5406 // another non-melee non-delayed spell is casted now, abort
5411 // do the action (pass spell to channeling state)
5412 m_Spell
->handle_immediate();
5414 // event will be re-added automatically at the end of routine)
5418 // run the spell handler and think about what we can do next
5419 uint64 t_offset
= e_time
- m_Spell
->GetDelayStart();
5420 uint64 n_offset
= m_Spell
->handle_delayed(t_offset
);
5423 // re-add us to the queue
5424 m_Spell
->GetCaster()->m_Events
.AddEvent(this, m_Spell
->GetDelayStart() + n_offset
, false);
5425 return(false); // event not complete
5428 // finish update event will be re-added automatically at the end of routine)
5433 // delaying had just started, record the moment
5434 m_Spell
->SetDelayStart(e_time
);
5435 // re-plan the event for the delay moment
5436 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ m_Spell
->GetDelayMoment(), false);
5437 return(false); // event not complete
5444 // event will be re-added automatically at the end of routine)
5448 // spell processing not complete, plan event on the next update interval
5449 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ 1, false);
5450 return(false); // event not complete
5453 void SpellEvent::Abort(uint64
/*e_time*/)
5455 // oops, the spell we try to do is aborted
5456 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5460 bool SpellEvent::IsDeletable() const
5462 return m_Spell
->IsDeletable();