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"
46 #include "VMapFactory.h"
47 #include "BattleGround.h"
50 #define SPELL_CHANNEL_UPDATE_INTERVAL 1000
52 extern pEffect SpellEffects
[TOTAL_SPELL_EFFECTS
];
54 bool IsQuestTameSpell(uint32 spellId
)
56 SpellEntry
const *spellproto
= sSpellStore
.LookupEntry(spellId
);
57 if (!spellproto
) return false;
59 return spellproto
->Effect
[0] == SPELL_EFFECT_THREAT
60 && spellproto
->Effect
[1] == SPELL_EFFECT_APPLY_AURA
&& spellproto
->EffectApplyAuraName
[1] == SPELL_AURA_DUMMY
;
63 SpellCastTargets::SpellCastTargets()
71 m_CorpseTargetGUID
= 0;
73 m_itemTargetEntry
= 0;
75 m_srcX
= m_srcY
= m_srcZ
= m_destX
= m_destY
= m_destZ
= 0;
80 SpellCastTargets::~SpellCastTargets()
84 void SpellCastTargets::setUnitTarget(Unit
*target
)
89 m_destX
= target
->GetPositionX();
90 m_destY
= target
->GetPositionY();
91 m_destZ
= target
->GetPositionZ();
92 m_unitTarget
= target
;
93 m_unitTargetGUID
= target
->GetGUID();
94 m_targetMask
|= TARGET_FLAG_UNIT
;
97 void SpellCastTargets::setDestination(float x
, float y
, float z
)
102 m_targetMask
|= TARGET_FLAG_DEST_LOCATION
;
105 void SpellCastTargets::setGOTarget(GameObject
*target
)
108 m_GOTargetGUID
= target
->GetGUID();
109 // m_targetMask |= TARGET_FLAG_OBJECT;
112 void SpellCastTargets::setItemTarget(Item
* item
)
118 m_itemTargetGUID
= item
->GetGUID();
119 m_itemTargetEntry
= item
->GetEntry();
120 m_targetMask
|= TARGET_FLAG_ITEM
;
123 void SpellCastTargets::setCorpseTarget(Corpse
* corpse
)
125 m_CorpseTargetGUID
= corpse
->GetGUID();
128 void SpellCastTargets::Update(Unit
* caster
)
130 m_GOTarget
= m_GOTargetGUID
? ObjectAccessor::GetGameObject(*caster
,m_GOTargetGUID
) : NULL
;
131 m_unitTarget
= m_unitTargetGUID
?
132 ( m_unitTargetGUID
==caster
->GetGUID() ? caster
: ObjectAccessor::GetUnit(*caster
, m_unitTargetGUID
) ) :
136 if(caster
->GetTypeId()==TYPEID_PLAYER
)
138 if(m_targetMask
& TARGET_FLAG_ITEM
)
139 m_itemTarget
= ((Player
*)caster
)->GetItemByGuid(m_itemTargetGUID
);
142 Player
* pTrader
= ((Player
*)caster
)->GetTrader();
143 if(pTrader
&& m_itemTargetGUID
< TRADE_SLOT_COUNT
)
144 m_itemTarget
= pTrader
->GetItemByPos(pTrader
->GetItemPosByTradeSlot(m_itemTargetGUID
));
147 m_itemTargetEntry
= m_itemTarget
->GetEntry();
151 bool SpellCastTargets::read ( WorldPacket
* data
, Unit
*caster
)
153 if(data
->rpos()+4 > data
->size())
156 *data
>> m_targetMask
;
158 if(m_targetMask
== TARGET_FLAG_SELF
)
160 m_destX
= caster
->GetPositionX();
161 m_destY
= caster
->GetPositionY();
162 m_destZ
= caster
->GetPositionZ();
163 m_unitTarget
= caster
;
164 m_unitTargetGUID
= caster
->GetGUID();
168 // TARGET_FLAG_UNK2 is used for non-combat pets, maybe other?
169 if( m_targetMask
& ( TARGET_FLAG_UNIT
| TARGET_FLAG_UNK2
))
170 if(!readGUID(*data
, m_unitTargetGUID
))
173 if( m_targetMask
& ( TARGET_FLAG_OBJECT
| TARGET_FLAG_OBJECT_UNK
))
174 if(!readGUID(*data
, m_GOTargetGUID
))
177 if(( m_targetMask
& ( TARGET_FLAG_ITEM
| TARGET_FLAG_TRADE_ITEM
)) && caster
->GetTypeId() == TYPEID_PLAYER
)
178 if(!readGUID(*data
, m_itemTargetGUID
))
181 if( m_targetMask
& TARGET_FLAG_SOURCE_LOCATION
)
183 if(data
->rpos()+4+4+4 > data
->size())
186 *data
>> m_srcX
>> m_srcY
>> m_srcZ
;
187 if(!MaNGOS::IsValidMapCoord(m_srcX
, m_srcY
, m_srcZ
))
191 if( m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
193 if(data
->rpos()+4+4+4 > data
->size())
196 *data
>> m_destX
>> m_destY
>> m_destZ
;
197 if(!MaNGOS::IsValidMapCoord(m_destX
, m_destY
, m_destZ
))
201 if( m_targetMask
& TARGET_FLAG_STRING
)
203 if(data
->rpos()+1 > data
->size())
206 *data
>> m_strTarget
;
209 if( m_targetMask
& (TARGET_FLAG_CORPSE
| TARGET_FLAG_PVP_CORPSE
) )
210 if(!readGUID(*data
, m_CorpseTargetGUID
))
213 // find real units/GOs
218 void SpellCastTargets::write ( WorldPacket
* data
)
220 *data
<< uint32(m_targetMask
);
222 if( m_targetMask
& ( TARGET_FLAG_UNIT
| TARGET_FLAG_PVP_CORPSE
| TARGET_FLAG_OBJECT
| TARGET_FLAG_OBJECT_UNK
| TARGET_FLAG_CORPSE
| TARGET_FLAG_UNK2
) )
224 if(m_targetMask
& TARGET_FLAG_UNIT
)
227 data
->append(m_unitTarget
->GetPackGUID());
231 else if( m_targetMask
& ( TARGET_FLAG_OBJECT
| TARGET_FLAG_OBJECT_UNK
) )
234 data
->append(m_GOTarget
->GetPackGUID());
238 else if( m_targetMask
& ( TARGET_FLAG_CORPSE
| TARGET_FLAG_PVP_CORPSE
) )
239 data
->appendPackGUID(m_CorpseTargetGUID
);
244 if( m_targetMask
& ( TARGET_FLAG_ITEM
| TARGET_FLAG_TRADE_ITEM
) )
247 data
->append(m_itemTarget
->GetPackGUID());
252 if( m_targetMask
& TARGET_FLAG_SOURCE_LOCATION
)
253 *data
<< m_srcX
<< m_srcY
<< m_srcZ
;
255 if( m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
256 *data
<< m_destX
<< m_destY
<< m_destZ
;
258 if( m_targetMask
& TARGET_FLAG_STRING
)
259 *data
<< m_strTarget
;
262 Spell::Spell( Unit
* Caster
, SpellEntry
const *info
, bool triggered
, uint64 originalCasterGUID
, Spell
** triggeringContainer
)
264 ASSERT( Caster
!= NULL
&& info
!= NULL
);
265 ASSERT( info
== sSpellStore
.LookupEntry( info
->Id
) && "`info` must be pointer to sSpellStore element");
269 m_selfContainer
= NULL
;
270 m_triggeringContainer
= triggeringContainer
;
271 m_referencedFromCurrentSpell
= false;
272 m_executedCurrently
= false;
274 m_delayAtDamageCount
= 0;
276 m_applyMultiplierMask
= 0;
278 // Get data for type of attack
279 switch (m_spellInfo
->DmgClass
)
281 case SPELL_DAMAGE_CLASS_MELEE
:
282 if (m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_REQ_OFFHAND
)
283 m_attackType
= OFF_ATTACK
;
285 m_attackType
= BASE_ATTACK
;
287 case SPELL_DAMAGE_CLASS_RANGED
:
288 m_attackType
= RANGED_ATTACK
;
292 if (m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_REQ_WAND
)
293 m_attackType
= RANGED_ATTACK
;
295 m_attackType
= BASE_ATTACK
;
299 m_spellSchoolMask
= GetSpellSchoolMask(info
); // Can be override for some spell (wand shoot for example)
301 if(m_attackType
== RANGED_ATTACK
)
304 if((m_caster
->getClassMask() & CLASSMASK_WAND_USERS
) != 0 && m_caster
->GetTypeId()==TYPEID_PLAYER
)
306 if(Item
* pItem
= ((Player
*)m_caster
)->GetWeaponForAttack(RANGED_ATTACK
))
307 m_spellSchoolMask
= SpellSchoolMask(1 << pItem
->GetProto()->Damage
->DamageType
);
311 if(originalCasterGUID
)
312 m_originalCasterGUID
= originalCasterGUID
;
314 m_originalCasterGUID
= m_caster
->GetGUID();
316 if(m_originalCasterGUID
==m_caster
->GetGUID())
317 m_originalCaster
= m_caster
;
320 m_originalCaster
= ObjectAccessor::GetUnit(*m_caster
,m_originalCasterGUID
);
321 if(m_originalCaster
&& !m_originalCaster
->IsInWorld()) m_originalCaster
= NULL
;
324 for(int i
=0; i
<3; ++i
)
325 m_currentBasePoints
[i
] = m_spellInfo
->EffectBasePoints
[i
];
327 m_spellState
= SPELL_STATE_NULL
;
329 m_castPositionX
= m_castPositionY
= m_castPositionZ
= 0;
330 m_TriggerSpells
.clear();
331 m_IsTriggeredSpell
= triggered
;
332 //m_AreaAura = false;
337 gameObjTarget
= NULL
;
341 m_triggeredByAuraSpell
= NULL
;
344 if( m_spellInfo
->AttributesEx2
== 0x000020 && !triggered
)
347 m_autoRepeat
= false;
350 m_powerCost
= 0; // setup to correct value in Spell::prepare, don't must be used before.
351 m_casttime
= 0; // setup to correct value in Spell::prepare, don't must be used before.
352 m_timer
= 0; // will set to castime in prepare
354 m_needAliveTargetMask
= 0;
356 // determine reflection
357 m_canReflect
= false;
359 if(m_spellInfo
->DmgClass
== SPELL_DAMAGE_CLASS_MAGIC
&& (m_spellInfo
->AttributesEx2
& 0x4)==0)
363 if (m_spellInfo
->Effect
[j
]==0)
366 if(!IsPositiveTarget(m_spellInfo
->EffectImplicitTargetA
[j
],m_spellInfo
->EffectImplicitTargetB
[j
]))
369 m_canReflect
= (m_spellInfo
->AttributesEx
& (1<<7)) ? true : false;
385 void Spell::FillTargetMap()
387 // TODO: ADD the correct target FILLS!!!!!!
389 for(uint32 i
=0;i
<3;i
++)
391 // not call for empty effect.
392 // Also some spells use not used effect targets for store targets for dummy effect in triggered spells
393 if(m_spellInfo
->Effect
[i
]==0)
396 // targets for TARGET_SCRIPT_COORDINATES (A) and TARGET_SCRIPT filled in Spell::canCast call
397 if( m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SCRIPT_COORDINATES
||
398 m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SCRIPT
||
399 m_spellInfo
->EffectImplicitTargetB
[i
] == TARGET_SCRIPT
&& m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_SELF
)
402 // TODO: find a way so this is not needed?
403 // for area auras always add caster as target (needed for totems for example)
404 if(IsAreaAuraEffect(m_spellInfo
->Effect
[i
]))
405 AddUnitTarget(m_caster
, i
);
407 std::list
<Unit
*> tmpUnitMap
;
409 // TargetA/TargetB dependent from each other, we not switch to full support this dependences
410 // but need it support in some know cases
411 switch(m_spellInfo
->EffectImplicitTargetA
[i
])
413 case TARGET_ALL_AROUND_CASTER
:
414 if( m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_PARTY
||
415 m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_FRIENDLY_UNITS_AROUND_CASTER
||
416 m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_RANDOM_RAID_MEMBER
)
418 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
420 // Note: this hack with search required until GO casting not implemented
421 // environment damage spells already have around enemies targeting but this not help in case not existed GO casting support
422 // currently each enemy selected explicitly and self cast damage
423 else if(m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_ENEMY_IN_AREA
&& m_spellInfo
->Effect
[i
]==SPELL_EFFECT_ENVIRONMENTAL_DAMAGE
)
425 if(m_targets
.getUnitTarget())
426 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
430 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
431 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
434 case TARGET_TABLE_X_Y_Z_COORDINATES
:
435 // Only if target A, for target B (used in teleports) dest select in effect
436 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
439 switch(m_spellInfo
->EffectImplicitTargetB
[i
])
441 case TARGET_SCRIPT_COORDINATES
: // B case filled in canCast but we need fill unit list base at A case
442 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
445 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
446 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
452 if( (m_spellInfo
->EffectImplicitTargetA
[i
]==0 || m_spellInfo
->EffectImplicitTargetA
[i
]==TARGET_EFFECT_SELECT
) &&
453 (m_spellInfo
->EffectImplicitTargetB
[i
]==0 || m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_EFFECT_SELECT
) )
455 // add here custom effects that need default target.
456 // FOR EVERY TARGET TYPE THERE IS A DIFFERENT FILL!!
457 switch(m_spellInfo
->Effect
[i
])
459 case SPELL_EFFECT_DUMMY
:
461 switch(m_spellInfo
->Id
)
463 case 20577: // Cannibalize
465 // non-standard target selection
466 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
467 float max_range
= GetSpellMaxRange(srange
);
469 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
471 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
474 WorldObject
* result
= NULL
;
476 MaNGOS::CannibalizeObjectCheck
u_check(m_caster
, max_range
);
477 MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
> searcher(result
, u_check
);
479 TypeContainerVisitor
<MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
>, GridTypeMapContainer
> grid_searcher(searcher
);
480 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
481 cell_lock
->Visit(cell_lock
, grid_searcher
, *m_caster
->GetMap());
485 TypeContainerVisitor
<MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
>, WorldTypeMapContainer
> world_searcher(searcher
);
486 cell_lock
->Visit(cell_lock
, world_searcher
, *m_caster
->GetMap());
491 switch(result
->GetTypeId())
495 tmpUnitMap
.push_back((Unit
*)result
);
498 m_targets
.setCorpseTarget((Corpse
*)result
);
499 if(Player
* owner
= ObjectAccessor::FindPlayer(((Corpse
*)result
)->GetOwnerGUID()))
500 tmpUnitMap
.push_back(owner
);
506 // clear cooldown at fail
507 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
509 ((Player
*)m_caster
)->RemoveSpellCooldown(m_spellInfo
->Id
);
511 WorldPacket
data(SMSG_CLEAR_COOLDOWN
, (4+8));
512 data
<< uint32(m_spellInfo
->Id
);
513 data
<< uint64(m_caster
->GetGUID());
514 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
517 SendCastResult(SPELL_FAILED_NO_EDIBLE_CORPSES
);
523 if(m_targets
.getUnitTarget())
524 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
529 case SPELL_EFFECT_RESURRECT
:
530 case SPELL_EFFECT_PARRY
:
531 case SPELL_EFFECT_BLOCK
:
532 case SPELL_EFFECT_CREATE_ITEM
:
533 case SPELL_EFFECT_TRIGGER_SPELL
:
534 case SPELL_EFFECT_TRIGGER_MISSILE
:
535 case SPELL_EFFECT_LEARN_SPELL
:
536 case SPELL_EFFECT_SKILL_STEP
:
537 case SPELL_EFFECT_PROFICIENCY
:
538 case SPELL_EFFECT_SUMMON_POSSESSED
:
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::CleanupTargetList()
676 m_UniqueTargetInfo
.clear();
677 m_UniqueGOTargetInfo
.clear();
678 m_UniqueItemInfo
.clear();
684 void Spell::AddUnitTarget(Unit
* pVictim
, uint32 effIndex
)
686 if( m_spellInfo
->Effect
[effIndex
]==0 )
689 uint64 targetGUID
= pVictim
->GetGUID();
691 // Lookup target in already in list
692 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
694 if (targetGUID
== ihit
->targetGUID
) // Found in list
696 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
701 // This is new target calculate data for him
703 // Get spell hit result on target
705 target
.targetGUID
= targetGUID
; // Store target GUID
706 target
.effectMask
= 1<<effIndex
; // Store index of effect
707 target
.processed
= false; // Effects not apply on target
709 // Calculate hit result
710 target
.missCondition
= m_caster
->SpellHitResult(pVictim
, m_spellInfo
, m_canReflect
);
711 if (target
.missCondition
== SPELL_MISS_NONE
)
716 // Spell have speed - need calculate incoming time
717 if (m_spellInfo
->speed
> 0.0f
)
719 // calculate spell incoming interval
720 float dist
= m_caster
->GetDistance(pVictim
->GetPositionX(), pVictim
->GetPositionY(), pVictim
->GetPositionZ());
721 if (dist
< 5.0f
) dist
= 5.0f
;
722 target
.timeDelay
= (uint64
) floor(dist
/ m_spellInfo
->speed
* 1000.0f
);
724 // Calculate minimum incoming time
725 if (m_delayMoment
==0 || m_delayMoment
>target
.timeDelay
)
726 m_delayMoment
= target
.timeDelay
;
729 target
.timeDelay
= 0LL;
731 // If target reflect spell back to caster
732 if (target
.missCondition
==SPELL_MISS_REFLECT
)
734 // Calculate reflected spell result on caster
735 target
.reflectResult
= m_caster
->SpellHitResult(m_caster
, m_spellInfo
, m_canReflect
);
737 if (target
.reflectResult
== SPELL_MISS_REFLECT
) // Impossible reflect again, so simply deflect spell
738 target
.reflectResult
= SPELL_MISS_PARRY
;
740 // Increase time interval for reflected spells by 1.5
741 target
.timeDelay
+=target
.timeDelay
>>1;
744 target
.reflectResult
= SPELL_MISS_NONE
;
746 // Add target to list
747 m_UniqueTargetInfo
.push_back(target
);
750 void Spell::AddUnitTarget(uint64 unitGUID
, uint32 effIndex
)
752 Unit
* unit
= m_caster
->GetGUID()==unitGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, unitGUID
);
754 AddUnitTarget(unit
, effIndex
);
757 void Spell::AddGOTarget(GameObject
* pVictim
, uint32 effIndex
)
759 if( m_spellInfo
->Effect
[effIndex
]==0 )
762 uint64 targetGUID
= pVictim
->GetGUID();
764 // Lookup target in already in list
765 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
767 if (targetGUID
== ihit
->targetGUID
) // Found in list
769 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
774 // This is new target calculate data for him
777 target
.targetGUID
= targetGUID
;
778 target
.effectMask
= 1<<effIndex
;
779 target
.processed
= false; // Effects not apply on target
781 // Spell have speed - need calculate incoming time
782 if (m_spellInfo
->speed
> 0.0f
)
784 // calculate spell incoming interval
785 float dist
= m_caster
->GetDistance(pVictim
->GetPositionX(), pVictim
->GetPositionY(), pVictim
->GetPositionZ());
786 if (dist
< 5.0f
) dist
= 5.0f
;
787 target
.timeDelay
= (uint64
) floor(dist
/ m_spellInfo
->speed
* 1000.0f
);
788 if (m_delayMoment
==0 || m_delayMoment
>target
.timeDelay
)
789 m_delayMoment
= target
.timeDelay
;
792 target
.timeDelay
= 0LL;
796 // Add target to list
797 m_UniqueGOTargetInfo
.push_back(target
);
800 void Spell::AddGOTarget(uint64 goGUID
, uint32 effIndex
)
802 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, goGUID
);
804 AddGOTarget(go
, effIndex
);
807 void Spell::AddItemTarget(Item
* pitem
, uint32 effIndex
)
809 if( m_spellInfo
->Effect
[effIndex
]==0 )
812 // Lookup target in already in list
813 for(std::list
<ItemTargetInfo
>::iterator ihit
= m_UniqueItemInfo
.begin();ihit
!= m_UniqueItemInfo
.end();++ihit
)
815 if (pitem
== ihit
->item
) // Found in list
817 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
822 // This is new target add data
824 ItemTargetInfo target
;
826 target
.effectMask
= 1<<effIndex
;
827 m_UniqueItemInfo
.push_back(target
);
830 void Spell::doTriggers(SpellMissInfo missInfo
, uint32 damage
, SpellSchoolMask damageSchoolMask
, uint32 block
, uint32 absorb
, bool crit
)
832 // Do triggers depends from hit result (triggers on hit do in effects)
833 // Set aura states depends from hit result
834 if (missInfo
!=SPELL_MISS_NONE
)
836 // Miss/dodge/parry/block only for melee based spells
837 // Resist only for magic based spells
840 case SPELL_MISS_MISS
:
841 if(m_caster
->GetTypeId()== TYPEID_PLAYER
)
842 ((Player
*)m_caster
)->UpdateWeaponSkill(BASE_ATTACK
);
844 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_MISS
, m_spellInfo
, m_IsTriggeredSpell
);
846 case SPELL_MISS_RESIST
:
847 m_caster
->ProcDamageAndSpell(unitTarget
, PROC_FLAG_TARGET_RESISTS
, PROC_FLAG_RESIST_SPELL
, 0, damageSchoolMask
, m_spellInfo
, m_IsTriggeredSpell
);
849 case SPELL_MISS_DODGE
:
850 if(unitTarget
->GetTypeId() == TYPEID_PLAYER
)
851 ((Player
*)unitTarget
)->UpdateDefense();
854 if (m_caster
->GetTypeId() == TYPEID_PLAYER
&& m_caster
->getClass() == CLASS_WARRIOR
)
856 ((Player
*) m_caster
)->AddComboPoints(unitTarget
, 1);
857 m_caster
->StartReactiveTimer( REACTIVE_OVERPOWER
);
861 if (unitTarget
->getClass() != CLASS_ROGUE
)
863 unitTarget
->ModifyAuraState(AURA_STATE_DEFENSE
, true);
864 unitTarget
->StartReactiveTimer( REACTIVE_DEFENSE
);
867 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_DODGE
, m_spellInfo
, m_IsTriggeredSpell
);
869 case SPELL_MISS_PARRY
:
870 // Update victim defense ?
871 if(unitTarget
->GetTypeId() == TYPEID_PLAYER
)
872 ((Player
*)unitTarget
)->UpdateDefense();
873 // Mongoose bite - set only Counterattack here
874 if (unitTarget
->getClass() == CLASS_HUNTER
)
876 unitTarget
->ModifyAuraState(AURA_STATE_HUNTER_PARRY
,true);
877 unitTarget
->StartReactiveTimer( REACTIVE_HUNTER_PARRY
);
881 unitTarget
->ModifyAuraState(AURA_STATE_DEFENSE
, true);
882 unitTarget
->StartReactiveTimer( REACTIVE_DEFENSE
);
884 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_PARRY
, m_spellInfo
, m_IsTriggeredSpell
);
886 case SPELL_MISS_BLOCK
:
887 unitTarget
->ModifyAuraState(AURA_STATE_DEFENSE
, true);
888 unitTarget
->StartReactiveTimer( REACTIVE_DEFENSE
);
890 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_BLOCK
, m_spellInfo
, m_IsTriggeredSpell
);
892 // Trigger from this events not supported
893 case SPELL_MISS_EVADE
:
894 case SPELL_MISS_IMMUNE
:
895 case SPELL_MISS_IMMUNE2
:
896 case SPELL_MISS_DEFLECT
:
897 case SPELL_MISS_ABSORB
:
898 // Trigger from reflects need do after get reflect result
899 case SPELL_MISS_REFLECT
:
907 void Spell::DoAllEffectOnTarget(TargetInfo
*target
)
909 if (target
->processed
) // Check target
911 target
->processed
= true; // Target checked in apply effects procedure
913 // Get mask of effects for target
914 uint32 mask
= target
->effectMask
;
915 if (mask
== 0) // No effects
918 Unit
* unit
= m_caster
->GetGUID()==target
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
,target
->targetGUID
);
922 SpellMissInfo missInfo
= target
->missCondition
;
923 // Need init unitTarget by default unit (can changed in code on reflect)
924 // Or on missInfo!=SPELL_MISS_NONE unitTarget undefined (but need in trigger subsystem)
927 if (missInfo
==SPELL_MISS_NONE
) // In case spell hit target, do all effect on that target
928 DoSpellHitOnUnit(unit
, mask
);
929 else if (missInfo
== SPELL_MISS_REFLECT
) // In case spell reflect from target, do all effect on caster (if hit)
931 if (target
->reflectResult
== SPELL_MISS_NONE
) // If reflected spell hit caster -> do all effect on him
932 DoSpellHitOnUnit(m_caster
, mask
);
935 // Do triggers only on miss/resist/parry/dodge
936 if (missInfo
!=SPELL_MISS_NONE
)
937 doTriggers(missInfo
);
939 // Call scripted function for AI if this spell is casted upon a creature (except pets)
940 if(IS_CREATURE_GUID(target
->targetGUID
))
942 // cast at creature (or GO) quest objectives update at successful cast finished (+channel finished)
943 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
944 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() && !IsChannelActive() )
945 ((Player
*)m_caster
)->CastedCreatureOrGO(unit
->GetEntry(),unit
->GetGUID(),m_spellInfo
->Id
);
947 if(((Creature
*)unit
)->AI())
948 ((Creature
*)unit
)->AI()->SpellHit(m_caster
,m_spellInfo
);
952 void Spell::DoSpellHitOnUnit(Unit
*unit
, const uint32 effectMask
)
954 if(!unit
|| !effectMask
)
957 // Recheck immune (only for delayed spells)
958 if( m_spellInfo
->speed
&& (
959 unit
->IsImmunedToDamage(GetSpellSchoolMask(m_spellInfo
),true) ||
960 unit
->IsImmunedToSpell(m_spellInfo
,true) ))
962 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_IMMUNE
);
966 if (unit
->GetTypeId() == TYPEID_PLAYER
)
968 ((Player
*)unit
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_BE_SPELL_TARGET
, m_spellInfo
->Id
);
969 ((Player
*)unit
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_BE_SPELL_TARGET2
, m_spellInfo
->Id
);
972 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
974 ((Player
*)m_caster
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_CAST_SPELL2
, m_spellInfo
->Id
, 0, unit
);
977 if( m_caster
!= unit
)
979 if( !m_caster
->IsFriendlyTo(unit
) )
981 // for delayed spells ignore not visible explicit target
982 if(m_spellInfo
->speed
> 0.0f
&& unit
==m_targets
.getUnitTarget() && !unit
->isVisibleForOrDetect(m_caster
,false))
984 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_EVADE
);
988 // exclude Arcane Missiles Dummy Aura aura for now (attack on hit)
989 // TODO: find way to not need this?
990 if(!(m_spellInfo
->SpellFamilyName
== SPELLFAMILY_MAGE
&&
991 m_spellInfo
->SpellFamilyFlags
& 0x800LL
))
993 unit
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
995 if( !(m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_NO_INITIAL_AGGRO
) )
997 if(!unit
->IsStandState() && !unit
->hasUnitState(UNIT_STAT_STUNNED
))
998 unit
->SetStandState(PLAYER_STATE_NONE
);
1000 if(!unit
->isInCombat() && unit
->GetTypeId() != TYPEID_PLAYER
&& ((Creature
*)unit
)->AI())
1001 ((Creature
*)unit
)->AI()->AttackStart(m_caster
);
1003 unit
->SetInCombatWith(m_caster
);
1004 m_caster
->SetInCombatWith(unit
);
1006 if(Player
*attackedPlayer
= unit
->GetCharmerOrOwnerPlayerOrPlayerItself())
1008 m_caster
->SetContestedPvP(attackedPlayer
);
1010 unit
->AddThreat(m_caster
, 0.0f
);
1016 // for delayed spells ignore negative spells (after duel end) for friendly targets
1017 if(m_spellInfo
->speed
> 0.0f
&& !IsPositiveSpell(m_spellInfo
->Id
))
1019 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_EVADE
);
1023 // assisting case, healing and resurrection
1024 if(unit
->hasUnitState(UNIT_STAT_ATTACK_PLAYER
))
1025 m_caster
->SetContestedPvP();
1026 if( unit
->isInCombat() && !(m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_NO_INITIAL_AGGRO
) )
1028 m_caster
->SetInCombatState(unit
->GetCombatTimer() > 0);
1029 unit
->getHostilRefManager().threatAssist(m_caster
, 0.0f
);
1034 // Get Data Needed for Diminishing Returns, some effects may have multiple auras, so this must be done on spell hit, not aura add
1035 m_diminishGroup
= GetDiminishingReturnsGroupForSpell(m_spellInfo
,m_triggeredByAuraSpell
);
1036 m_diminishLevel
= unit
->GetDiminishing(m_diminishGroup
);
1037 // Increase Diminishing on unit, current informations for actually casts will use values above
1038 if((GetDiminishingReturnsGroupType(m_diminishGroup
) == DRTYPE_PLAYER
&& unit
->GetTypeId() == TYPEID_PLAYER
) || GetDiminishingReturnsGroupType(m_diminishGroup
) == DRTYPE_ALL
)
1039 unit
->IncrDiminishing(m_diminishGroup
);
1041 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1043 if (effectMask
& (1<<effectNumber
))
1045 HandleEffects(unit
,NULL
,NULL
,effectNumber
,m_damageMultipliers
[effectNumber
]);
1046 if ( m_applyMultiplierMask
& (1 << effectNumber
) )
1049 float multiplier
= m_spellInfo
->DmgMultiplier
[effectNumber
];
1050 // Apply multiplier mods
1051 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1052 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_EFFECT_PAST_FIRST
, multiplier
,this);
1053 m_damageMultipliers
[effectNumber
] *= multiplier
;
1059 void Spell::DoAllEffectOnTarget(GOTargetInfo
*target
)
1061 if (target
->processed
) // Check target
1063 target
->processed
= true; // Target checked in apply effects procedure
1065 uint32 effectMask
= target
->effectMask
;
1069 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, target
->targetGUID
);
1073 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1074 if (effectMask
& (1<<effectNumber
))
1075 HandleEffects(NULL
,NULL
,go
,effectNumber
);
1077 // cast at creature (or GO) quest objectives update at successful cast finished (+channel finished)
1078 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
1079 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() && !IsChannelActive() )
1080 ((Player
*)m_caster
)->CastedCreatureOrGO(go
->GetEntry(),go
->GetGUID(),m_spellInfo
->Id
);
1083 void Spell::DoAllEffectOnTarget(ItemTargetInfo
*target
)
1085 uint32 effectMask
= target
->effectMask
;
1086 if(!target
->item
|| !effectMask
)
1089 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1090 if (effectMask
& (1<<effectNumber
))
1091 HandleEffects(NULL
, target
->item
, NULL
, effectNumber
);
1094 bool Spell::IsAliveUnitPresentInTargetList()
1096 // Not need check return true
1097 if (m_needAliveTargetMask
== 0)
1100 uint8 needAliveTargetMask
= m_needAliveTargetMask
;
1102 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
1104 if( ihit
->missCondition
== SPELL_MISS_NONE
&& (needAliveTargetMask
& ihit
->effectMask
) )
1106 Unit
*unit
= m_caster
->GetGUID()==ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
1108 if (unit
&& unit
->isAlive())
1109 needAliveTargetMask
&= ~ihit
->effectMask
; // remove from need alive mask effect that have alive target
1113 // is all effects from m_needAliveTargetMask have alive targets
1114 return needAliveTargetMask
==0;
1117 // Helper for Chain Healing
1118 // Spell target first
1119 // Raidmates then descending by injury suffered (MaxHealth - Health)
1120 // Other players/mobs then descending by injury suffered (MaxHealth - Health)
1121 struct ChainHealingOrder
: public std::binary_function
<const Unit
*, const Unit
*, bool>
1123 const Unit
* MainTarget
;
1124 ChainHealingOrder(Unit
const* Target
) : MainTarget(Target
) {};
1125 // functor for operator ">"
1126 bool operator()(Unit
const* _Left
, Unit
const* _Right
) const
1128 return (ChainHealingHash(_Left
) < ChainHealingHash(_Right
));
1130 int32
ChainHealingHash(Unit
const* Target
) const
1132 if (Target
== MainTarget
)
1134 else if (Target
->GetTypeId() == TYPEID_PLAYER
&& MainTarget
->GetTypeId() == TYPEID_PLAYER
&&
1135 ((Player
const*)Target
)->IsInSameRaidWith((Player
const*)MainTarget
))
1137 if (Target
->GetHealth() == Target
->GetMaxHealth())
1140 return 20000 - Target
->GetMaxHealth() + Target
->GetHealth();
1143 return 40000 - Target
->GetMaxHealth() + Target
->GetHealth();
1147 class ChainHealingFullHealth
: std::unary_function
<const Unit
*, bool>
1150 const Unit
* MainTarget
;
1151 ChainHealingFullHealth(const Unit
* Target
) : MainTarget(Target
) {};
1153 bool operator()(const Unit
* Target
)
1155 return (Target
!= MainTarget
&& Target
->GetHealth() == Target
->GetMaxHealth());
1159 // Helper for targets nearest to the spell target
1160 // The spell target is always first unless there is a target at _completely_ the same position (unbelievable case)
1161 struct TargetDistanceOrder
: public std::binary_function
<const Unit
, const Unit
, bool>
1163 const Unit
* MainTarget
;
1164 TargetDistanceOrder(const Unit
* Target
) : MainTarget(Target
) {};
1165 // functor for operator ">"
1166 bool operator()(const Unit
* _Left
, const Unit
* _Right
) const
1168 return (MainTarget
->GetDistance(_Left
) < MainTarget
->GetDistance(_Right
));
1172 void Spell::SetTargetMap(uint32 i
,uint32 cur
,std::list
<Unit
*> &TagUnitMap
)
1175 if (m_spellInfo
->EffectRadiusIndex
[i
])
1176 radius
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[i
]));
1178 radius
= GetSpellMaxRange(sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
));
1180 if(m_originalCaster
)
1181 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1182 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_RADIUS
, radius
,this);
1184 uint32 EffectChainTarget
= m_spellInfo
->EffectChainTarget
[i
];
1185 if(m_originalCaster
)
1186 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1187 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_JUMP_TARGETS
, EffectChainTarget
, this);
1189 uint32 unMaxTargets
= m_spellInfo
->MaxAffectedTargets
;
1192 case TARGET_TOTEM_EARTH
:
1193 case TARGET_TOTEM_WATER
:
1194 case TARGET_TOTEM_AIR
:
1195 case TARGET_TOTEM_FIRE
:
1198 case TARGET_DYNAMIC_OBJECT
:
1199 case TARGET_AREAEFFECT_CUSTOM
:
1200 case TARGET_AREAEFFECT_CUSTOM_2
:
1203 TagUnitMap
.push_back(m_caster
);
1206 case TARGET_RANDOM_ENEMY_CHAIN_IN_AREA
:
1208 m_targets
.m_targetMask
= 0;
1209 unMaxTargets
= EffectChainTarget
;
1210 float max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1212 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1214 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1217 std::list
<Unit
*> tempUnitMap
;
1220 MaNGOS::AnyAoETargetUnitInObjectRangeCheck
u_check(m_caster
, m_caster
, max_range
);
1221 MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
> searcher(tempUnitMap
, u_check
);
1223 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_unit_searcher(searcher
);
1224 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1226 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1227 cell_lock
->Visit(cell_lock
, world_unit_searcher
, *m_caster
->GetMap());
1228 cell_lock
->Visit(cell_lock
, grid_unit_searcher
, *m_caster
->GetMap());
1231 if(tempUnitMap
.empty())
1234 tempUnitMap
.sort(TargetDistanceOrder(m_caster
));
1236 //Now to get us a random target that's in the initial range of the spell
1238 std::list
<Unit
*>::iterator itr
= tempUnitMap
.begin();
1239 while(itr
!= tempUnitMap
.end() && (*itr
)->GetDistance(m_caster
) < radius
)
1245 itr
= tempUnitMap
.begin();
1246 std::advance(itr
, rand()%t
);
1247 Unit
*pUnitTarget
= *itr
;
1248 TagUnitMap
.push_back(pUnitTarget
);
1250 tempUnitMap
.erase(itr
);
1252 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1254 t
= unMaxTargets
- 1;
1255 Unit
*prev
= pUnitTarget
;
1256 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1258 while(t
&& next
!= tempUnitMap
.end() )
1260 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1263 if(!prev
->IsWithinLOSInMap(*next
))
1270 TagUnitMap
.push_back(prev
);
1271 tempUnitMap
.erase(next
);
1272 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1273 next
= tempUnitMap
.begin();
1280 Pet
* tmpUnit
= m_caster
->GetPet();
1281 if (!tmpUnit
) break;
1282 TagUnitMap
.push_back(tmpUnit
);
1285 case TARGET_CHAIN_DAMAGE
:
1287 if (EffectChainTarget
<= 1)
1289 Unit
* pUnitTarget
= SelectMagnetTarget();
1291 TagUnitMap
.push_back(pUnitTarget
);
1295 Unit
* pUnitTarget
= m_targets
.getUnitTarget();
1299 unMaxTargets
= EffectChainTarget
;
1302 if(m_spellInfo
->DmgClass
==SPELL_DAMAGE_CLASS_MELEE
)
1303 max_range
= radius
; //
1305 //FIXME: This very like horrible hack and wrong for most spells
1306 max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1308 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1310 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1313 Unit
* originalCaster
= GetOriginalCaster();
1316 std::list
<Unit
*> tempUnitMap
;
1319 MaNGOS::AnyAoETargetUnitInObjectRangeCheck
u_check(pUnitTarget
, originalCaster
, 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 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1332 if(tempUnitMap
.empty())
1335 if(*tempUnitMap
.begin() == pUnitTarget
)
1336 tempUnitMap
.erase(tempUnitMap
.begin());
1338 TagUnitMap
.push_back(pUnitTarget
);
1339 uint32 t
= unMaxTargets
- 1;
1340 Unit
*prev
= pUnitTarget
;
1341 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1343 while(t
&& next
!= tempUnitMap
.end() )
1345 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1348 if(!prev
->IsWithinLOSInMap(*next
))
1355 TagUnitMap
.push_back(prev
);
1356 tempUnitMap
.erase(next
);
1357 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1358 next
= tempUnitMap
.begin();
1365 case TARGET_ALL_ENEMY_IN_AREA
:
1368 case TARGET_ALL_ENEMY_IN_AREA_INSTANT
:
1370 // targets the ground, not the units in the area
1371 if (m_spellInfo
->Effect
[i
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
1373 CellPair
p(MaNGOS::ComputeCellPair(m_targets
.m_destX
, m_targets
.m_destY
));
1375 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1378 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1380 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1381 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1383 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1384 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1385 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1387 // exclude caster (this can be important if this not original caster)
1388 TagUnitMap
.remove(m_caster
);
1391 case TARGET_DUELVSPLAYER_COORDINATES
:
1393 if(Unit
* currentTarget
= m_targets
.getUnitTarget())
1395 m_targets
.setDestination(currentTarget
->GetPositionX(), currentTarget
->GetPositionY(), currentTarget
->GetPositionZ());
1396 TagUnitMap
.push_back(currentTarget
);
1399 case TARGET_ALL_PARTY_AROUND_CASTER
:
1400 case TARGET_ALL_PARTY_AROUND_CASTER_2
:
1401 case TARGET_ALL_PARTY
:
1403 Player
*pTarget
= m_caster
->GetCharmerOrOwnerPlayerOrPlayerItself();
1404 Group
*pGroup
= pTarget
? pTarget
->GetGroup() : NULL
;
1408 uint8 subgroup
= pTarget
->GetSubGroup();
1410 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1412 Player
* Target
= itr
->getSource();
1414 // IsHostileTo check duel and controlled by enemy
1415 if( Target
&& Target
->GetSubGroup()==subgroup
&& !m_caster
->IsHostileTo(Target
) )
1417 if( m_caster
->IsWithinDistInMap(Target
, radius
) )
1418 TagUnitMap
.push_back(Target
);
1420 if(Pet
* pet
= Target
->GetPet())
1421 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1422 TagUnitMap
.push_back(pet
);
1428 Unit
* ownerOrSelf
= pTarget
? pTarget
: m_caster
->GetCharmerOrOwnerOrSelf();
1429 if(ownerOrSelf
==m_caster
|| m_caster
->IsWithinDistInMap(ownerOrSelf
, radius
))
1430 TagUnitMap
.push_back(ownerOrSelf
);
1431 if(Pet
* pet
= ownerOrSelf
->GetPet())
1432 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1433 TagUnitMap
.push_back(pet
);
1436 case TARGET_RANDOM_RAID_MEMBER
:
1438 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1439 if(Player
* target
= ((Player
*)m_caster
)->GetNextRandomRaidMember(radius
))
1440 TagUnitMap
.push_back(target
);
1442 case TARGET_SINGLE_FRIEND
:
1443 case TARGET_SINGLE_FRIEND_2
:
1445 if(m_targets
.getUnitTarget())
1446 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1448 case TARGET_NONCOMBAT_PET
:
1450 if(Unit
* target
= m_targets
.getUnitTarget())
1451 if( target
->GetTypeId() == TYPEID_UNIT
&& ((Creature
*)target
)->isPet() && ((Pet
*)target
)->getPetType() == MINI_PET
)
1452 TagUnitMap
.push_back(target
);
1454 case TARGET_ALL_AROUND_CASTER
:
1456 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1458 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1461 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_SELF_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1463 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1464 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1466 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1467 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1468 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1470 case TARGET_ALL_FRIENDLY_UNITS_AROUND_CASTER
:
1472 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1474 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1477 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_SELF_CENTER
,SPELL_TARGETS_FRIENDLY
);
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 case TARGET_ALL_FRIENDLY_UNITS_IN_AREA
:
1488 CellPair
p(MaNGOS::ComputeCellPair(m_targets
.m_destX
, m_targets
.m_destY
));
1490 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1493 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_FRIENDLY
);
1495 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1496 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1498 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1499 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1500 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1502 // 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..)
1503 case TARGET_SINGLE_PARTY
:
1505 Unit
*target
= m_targets
.getUnitTarget();
1506 // Thoses spells apparently can't be casted on the caster.
1507 if( target
&& target
!= m_caster
)
1509 // Can only be casted on group's members or its pets
1510 Group
*pGroup
= NULL
;
1512 Unit
* owner
= m_caster
->GetCharmerOrOwner();
1513 Unit
*targetOwner
= target
->GetCharmerOrOwner();
1516 if(owner
->GetTypeId() == TYPEID_PLAYER
)
1518 if( target
== owner
)
1520 TagUnitMap
.push_back(target
);
1523 pGroup
= ((Player
*)owner
)->GetGroup();
1526 else if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1528 if( targetOwner
== m_caster
&& target
->GetTypeId()==TYPEID_UNIT
&& ((Creature
*)target
)->isPet())
1530 TagUnitMap
.push_back(target
);
1533 pGroup
= ((Player
*)m_caster
)->GetGroup();
1538 // Our target can also be a player's pet who's grouped with us or our pet. But can't be controlled player
1541 if( targetOwner
->GetTypeId() == TYPEID_PLAYER
&&
1542 target
->GetTypeId()==TYPEID_UNIT
&& (((Creature
*)target
)->isPet()) &&
1543 target
->GetOwnerGUID()==targetOwner
->GetGUID() &&
1544 pGroup
->IsMember(((Player
*)targetOwner
)->GetGUID()))
1546 TagUnitMap
.push_back(target
);
1549 // 1Our target can be a player who is on our group
1550 else if (target
->GetTypeId() == TYPEID_PLAYER
&& pGroup
->IsMember(((Player
*)target
)->GetGUID()))
1552 TagUnitMap
.push_back(target
);
1557 case TARGET_GAMEOBJECT
:
1559 if(m_targets
.getGOTarget())
1560 AddGOTarget(m_targets
.getGOTarget(), i
);
1562 case TARGET_IN_FRONT_OF_CASTER
:
1564 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1566 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1569 bool inFront
= m_spellInfo
->SpellVisual
[0] != 3879;
1570 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, inFront
? PUSH_IN_FRONT
: PUSH_IN_BACK
,SPELL_TARGETS_AOE_DAMAGE
);
1572 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1573 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1575 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1576 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1577 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1579 case TARGET_DUELVSPLAYER
:
1581 Unit
*target
= m_targets
.getUnitTarget();
1584 if(m_caster
->IsFriendlyTo(target
))
1586 TagUnitMap
.push_back(target
);
1590 Unit
* pUnitTarget
= SelectMagnetTarget();
1592 TagUnitMap
.push_back(pUnitTarget
);
1596 case TARGET_GAMEOBJECT_ITEM
:
1598 if(m_targets
.getGOTargetGUID())
1599 AddGOTarget(m_targets
.getGOTarget(), i
);
1600 else if(m_targets
.getItemTarget())
1601 AddItemTarget(m_targets
.getItemTarget(), i
);
1606 if(Unit
* owner
= m_caster
->GetCharmerOrOwner())
1607 TagUnitMap
.push_back(owner
);
1610 case TARGET_ALL_ENEMY_IN_AREA_CHANNELED
:
1612 // targets the ground, not the units in the area
1613 if (m_spellInfo
->Effect
[i
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
1615 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1617 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1620 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1622 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1623 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1625 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1626 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1627 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1632 if(m_spellInfo
->Effect
[i
] != SPELL_EFFECT_DUEL
)
1633 TagUnitMap
.push_back(m_caster
);
1635 case TARGET_SINGLE_ENEMY
:
1637 Unit
* pUnitTarget
= SelectMagnetTarget();
1639 TagUnitMap
.push_back(pUnitTarget
);
1641 case TARGET_AREAEFFECT_PARTY
:
1643 Unit
* owner
= m_caster
->GetCharmerOrOwner();
1644 Player
*pTarget
= NULL
;
1648 TagUnitMap
.push_back(m_caster
);
1649 if(owner
->GetTypeId() == TYPEID_PLAYER
)
1650 pTarget
= (Player
*)owner
;
1652 else if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1654 if(Unit
* target
= m_targets
.getUnitTarget())
1656 if( target
->GetTypeId() != TYPEID_PLAYER
)
1658 if(((Creature
*)target
)->isPet())
1660 Unit
*targetOwner
= target
->GetOwner();
1661 if(targetOwner
->GetTypeId() == TYPEID_PLAYER
)
1662 pTarget
= (Player
*)targetOwner
;
1666 pTarget
= (Player
*)target
;
1670 Group
* pGroup
= pTarget
? pTarget
->GetGroup() : NULL
;
1674 uint8 subgroup
= pTarget
->GetSubGroup();
1676 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1678 Player
* Target
= itr
->getSource();
1680 // IsHostileTo check duel and controlled by enemy
1681 if(Target
&& Target
->GetSubGroup()==subgroup
&& !m_caster
->IsHostileTo(Target
))
1683 if( pTarget
->IsWithinDistInMap(Target
, radius
) )
1684 TagUnitMap
.push_back(Target
);
1686 if(Pet
* pet
= Target
->GetPet())
1687 if( pTarget
->IsWithinDistInMap(pet
, radius
) )
1688 TagUnitMap
.push_back(pet
);
1694 if(m_caster
->IsWithinDistInMap(owner
, radius
))
1695 TagUnitMap
.push_back(owner
);
1699 TagUnitMap
.push_back(pTarget
);
1701 if(Pet
* pet
= pTarget
->GetPet())
1702 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1703 TagUnitMap
.push_back(pet
);
1709 if(m_targets
.getUnitTarget())
1710 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1711 if(m_targets
.getItemTarget())
1712 AddItemTarget(m_targets
.getItemTarget(), i
);
1714 case TARGET_SELF_FISHING
:
1716 TagUnitMap
.push_back(m_caster
);
1718 case TARGET_CHAIN_HEAL
:
1720 Unit
* pUnitTarget
= m_targets
.getUnitTarget();
1724 if (EffectChainTarget
<= 1)
1725 TagUnitMap
.push_back(pUnitTarget
);
1728 unMaxTargets
= EffectChainTarget
;
1729 float max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1731 std::list
<Unit
*> tempUnitMap
;
1734 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1736 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1739 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, tempUnitMap
, max_range
, PUSH_SELF_CENTER
, SPELL_TARGETS_FRIENDLY
);
1741 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1742 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1744 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1745 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1746 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1750 if(m_caster
!= pUnitTarget
&& std::find(tempUnitMap
.begin(),tempUnitMap
.end(),m_caster
) == tempUnitMap
.end() )
1751 tempUnitMap
.push_front(m_caster
);
1753 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1755 if(tempUnitMap
.empty())
1758 if(*tempUnitMap
.begin() == pUnitTarget
)
1759 tempUnitMap
.erase(tempUnitMap
.begin());
1761 TagUnitMap
.push_back(pUnitTarget
);
1762 uint32 t
= unMaxTargets
- 1;
1763 Unit
*prev
= pUnitTarget
;
1764 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1766 while(t
&& next
!= tempUnitMap
.end() )
1768 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1771 if(!prev
->IsWithinLOSInMap(*next
))
1777 if((*next
)->GetHealth() == (*next
)->GetMaxHealth())
1779 next
= tempUnitMap
.erase(next
);
1784 TagUnitMap
.push_back(prev
);
1785 tempUnitMap
.erase(next
);
1786 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1787 next
= tempUnitMap
.begin();
1793 case TARGET_CURRENT_ENEMY_COORDINATES
:
1795 Unit
* currentTarget
= m_targets
.getUnitTarget();
1798 TagUnitMap
.push_back(currentTarget
);
1799 m_targets
.setDestination(currentTarget
->GetPositionX(), currentTarget
->GetPositionY(), currentTarget
->GetPositionZ());
1800 if(m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_ENEMY_IN_AREA_INSTANT
)
1802 CellPair
p(MaNGOS::ComputeCellPair(currentTarget
->GetPositionX(), currentTarget
->GetPositionY()));
1804 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1806 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
,PUSH_TARGET_CENTER
, SPELL_TARGETS_AOE_DAMAGE
);
1807 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_notifier(notifier
);
1808 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_notifier(notifier
);
1809 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1810 cell_lock
->Visit(cell_lock
, world_notifier
, *m_caster
->GetMap());
1811 cell_lock
->Visit(cell_lock
, grid_notifier
, *m_caster
->GetMap());
1815 case TARGET_AREAEFFECT_PARTY_AND_CLASS
:
1817 Player
* targetPlayer
= m_targets
.getUnitTarget() && m_targets
.getUnitTarget()->GetTypeId() == TYPEID_PLAYER
1818 ? (Player
*)m_targets
.getUnitTarget() : NULL
;
1820 Group
* pGroup
= targetPlayer
? targetPlayer
->GetGroup() : NULL
;
1823 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1825 Player
* Target
= itr
->getSource();
1827 // IsHostileTo check duel and controlled by enemy
1828 if( Target
&& targetPlayer
->IsWithinDistInMap(Target
, radius
) &&
1829 targetPlayer
->getClass() == Target
->getClass() &&
1830 !m_caster
->IsHostileTo(Target
) )
1832 TagUnitMap
.push_back(Target
);
1836 else if(m_targets
.getUnitTarget())
1837 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1840 case TARGET_TABLE_X_Y_Z_COORDINATES
:
1842 SpellTargetPosition
const* st
= spellmgr
.GetSpellTargetPosition(m_spellInfo
->Id
);
1845 if (st
->target_mapId
== m_caster
->GetMapId())
1846 m_targets
.setDestination(st
->target_X
, st
->target_Y
, st
->target_Z
);
1848 // if B==TARGET_TABLE_X_Y_Z_COORDINATES then A already fill all required targets
1849 if (m_spellInfo
->EffectImplicitTargetB
[i
] && m_spellInfo
->EffectImplicitTargetB
[i
]!=TARGET_TABLE_X_Y_Z_COORDINATES
)
1851 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1853 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1856 SpellTargets targetB
= SPELL_TARGETS_AOE_DAMAGE
;
1857 // Select friendly targets for positive effect
1858 if (IsPositiveEffect(m_spellInfo
->Id
, i
))
1859 targetB
= SPELL_TARGETS_FRIENDLY
;
1861 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
,PUSH_DEST_CENTER
, targetB
);
1863 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_notifier(notifier
);
1864 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_notifier(notifier
);
1866 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1867 cell_lock
->Visit(cell_lock
, world_notifier
, *m_caster
->GetMap());
1868 cell_lock
->Visit(cell_lock
, grid_notifier
, *m_caster
->GetMap());
1872 sLog
.outError( "SPELL: unknown target coordinates for spell ID %u\n", m_spellInfo
->Id
);
1874 case TARGET_BEHIND_VICTIM
:
1876 Unit
*pTarget
= m_caster
->getVictim();
1877 if(!pTarget
&& m_caster
->GetTypeId() == TYPEID_PLAYER
)
1878 pTarget
= ObjectAccessor::GetUnit(*m_caster
, ((Player
*)m_caster
)->GetSelection());
1882 float _target_x
, _target_y
, _target_z
;
1883 pTarget
->GetClosePoint(_target_x
, _target_y
, _target_z
, m_caster
->GetObjectSize(), CONTACT_DISTANCE
, M_PI
);
1884 if(pTarget
->IsWithinLOS(_target_x
,_target_y
,_target_z
))
1885 m_targets
.setDestination(_target_x
, _target_y
, _target_z
);
1892 if (unMaxTargets
&& TagUnitMap
.size() > unMaxTargets
)
1894 // make sure one unit is always removed per iteration
1895 uint32 removed_utarget
= 0;
1896 for (std::list
<Unit
*>::iterator itr
= TagUnitMap
.begin(), next
; itr
!= TagUnitMap
.end(); itr
= next
)
1900 if (!*itr
) continue;
1901 if ((*itr
) == m_targets
.getUnitTarget())
1903 TagUnitMap
.erase(itr
);
1904 removed_utarget
= 1;
1908 // remove random units from the map
1909 while (TagUnitMap
.size() > unMaxTargets
- removed_utarget
)
1911 uint32 poz
= urand(0, TagUnitMap
.size()-1);
1912 for (std::list
<Unit
*>::iterator itr
= TagUnitMap
.begin(); itr
!= TagUnitMap
.end(); ++itr
, --poz
)
1914 if (!*itr
) continue;
1917 TagUnitMap
.erase(itr
);
1922 // the player's target will always be added to the map
1923 if (removed_utarget
&& m_targets
.getUnitTarget())
1924 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1928 void Spell::prepare(SpellCastTargets
* targets
, Aura
* triggeredByAura
)
1930 m_targets
= *targets
;
1932 m_spellState
= SPELL_STATE_PREPARING
;
1934 m_castPositionX
= m_caster
->GetPositionX();
1935 m_castPositionY
= m_caster
->GetPositionY();
1936 m_castPositionZ
= m_caster
->GetPositionZ();
1937 m_castOrientation
= m_caster
->GetOrientation();
1940 m_triggeredByAuraSpell
= triggeredByAura
->GetSpellProto();
1942 // create and add update event for this spell
1943 SpellEvent
* Event
= new SpellEvent(this);
1944 m_caster
->m_Events
.AddEvent(Event
, m_caster
->m_Events
.CalculateTime(1));
1946 //Prevent casting at cast another spell (ServerSide check)
1947 if(m_caster
->IsNonMeleeSpellCasted(false, true) && m_cast_count
)
1949 SendCastResult(SPELL_FAILED_SPELL_IN_PROGRESS
);
1955 m_powerCost
= CalculatePowerCost();
1957 uint8 result
= CanCast(true);
1958 if(result
!= 0 && !IsAutoRepeat()) //always cast autorepeat dummy for triggering
1962 SendChannelUpdate(0);
1963 triggeredByAura
->SetAuraDuration(0);
1965 SendCastResult(result
);
1970 // calculate cast time (calculated after first CanCast check to prevent charge counting for first CanCast fail)
1971 m_casttime
= GetSpellCastTime(m_spellInfo
, this);
1973 // set timer base at cast time
1976 // stealth must be removed at cast starting (at show channel bar)
1977 // skip triggered spell (item equip spell casting and other not explicit character casts/item uses)
1978 if ( !m_IsTriggeredSpell
&& isSpellBreakStealth(m_spellInfo
) )
1980 m_caster
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
1981 m_caster
->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH
);
1984 if(m_IsTriggeredSpell
)
1988 m_caster
->SetCurrentCastedSpell( this );
1989 m_selfContainer
= &(m_caster
->m_currentSpells
[GetCurrentContainer()]);
1994 void Spell::cancel()
1996 if(m_spellState
== SPELL_STATE_FINISHED
)
1999 m_autoRepeat
= false;
2000 switch (m_spellState
)
2002 case SPELL_STATE_PREPARING
:
2003 case SPELL_STATE_DELAYED
:
2006 SendCastResult(SPELL_FAILED_INTERRUPTED
);
2009 case SPELL_STATE_CASTING
:
2011 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2013 if( ihit
->missCondition
== SPELL_MISS_NONE
)
2015 Unit
* unit
= m_caster
->GetGUID()==(*ihit
).targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
2016 if( unit
&& unit
->isAlive() )
2017 unit
->RemoveAurasDueToSpell(m_spellInfo
->Id
);
2021 m_caster
->RemoveAurasDueToSpell(m_spellInfo
->Id
);
2022 SendChannelUpdate(0);
2024 SendCastResult(SPELL_FAILED_INTERRUPTED
);
2033 m_caster
->RemoveDynObject(m_spellInfo
->Id
);
2034 m_caster
->RemoveGameObject(m_spellInfo
->Id
,true);
2037 void Spell::cast(bool skipCheck
)
2039 SetExecutedCurrently(true);
2041 uint8 castResult
= 0;
2043 // update pointers base at GUIDs to prevent access to non-existed already object
2046 // cancel at lost main target unit
2047 if(!m_targets
.getUnitTarget() && m_targets
.getUnitTargetGUID() && m_targets
.getUnitTargetGUID() != m_caster
->GetGUID())
2050 SetExecutedCurrently(false);
2054 if(m_caster
->GetTypeId() != TYPEID_PLAYER
&& m_targets
.getUnitTarget() && m_targets
.getUnitTarget() != m_caster
)
2055 m_caster
->SetInFront(m_targets
.getUnitTarget());
2057 castResult
= CheckPower();
2060 SendCastResult(castResult
);
2062 SetExecutedCurrently(false);
2066 // triggered cast called from Spell::prepare where it was already checked
2069 castResult
= CanCast(false);
2072 SendCastResult(castResult
);
2074 SetExecutedCurrently(false);
2079 // Conflagrate - consumes immolate
2080 if ((m_spellInfo
->TargetAuraState
== AURA_STATE_IMMOLATE
) && m_targets
.getUnitTarget())
2082 // for caster applied auras only
2083 Unit::AuraList
const &mPeriodic
= m_targets
.getUnitTarget()->GetAurasByType(SPELL_AURA_PERIODIC_DAMAGE
);
2084 for(Unit::AuraList::const_iterator i
= mPeriodic
.begin(); i
!= mPeriodic
.end(); ++i
)
2086 if( (*i
)->GetSpellProto()->SpellFamilyName
== SPELLFAMILY_WARLOCK
&& ((*i
)->GetSpellProto()->SpellFamilyFlags
& 4) &&
2087 (*i
)->GetCasterGUID()==m_caster
->GetGUID() )
2089 m_targets
.getUnitTarget()->RemoveAura((*i
)->GetId(), (*i
)->GetEffIndex());
2095 // traded items have trade slot instead of guid in m_itemTargetGUID
2096 // set to real guid to be sent later to the client
2097 m_targets
.updateTradeSlotItem();
2099 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2102 ((Player
*)m_caster
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_USE_ITEM
, m_CastItem
->GetEntry());
2104 ((Player
*)m_caster
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_CAST_SPELL
, m_spellInfo
->Id
);
2108 SendSpellCooldown();
2111 TakeReagents(); // we must remove reagents before HandleEffects to allow place crafted item in same slot
2114 if(m_spellState
== SPELL_STATE_FINISHED
) // stop cast if spell marked as finish somewhere in Take*/FillTargetMap
2116 SetExecutedCurrently(false);
2120 SendCastResult(castResult
);
2121 SendSpellGo(); // we must send smsg_spell_go packet before m_castItem delete in TakeCastItem()...
2123 // Pass cast spell event to handler (not send triggered by aura spells)
2124 if (m_spellInfo
->DmgClass
!= SPELL_DAMAGE_CLASS_MELEE
&& m_spellInfo
->DmgClass
!= SPELL_DAMAGE_CLASS_RANGED
&& !m_triggeredByAuraSpell
)
2126 m_caster
->ProcDamageAndSpell(m_targets
.getUnitTarget(), PROC_FLAG_CAST_SPELL
, PROC_FLAG_NONE
, 0, SPELL_SCHOOL_MASK_NONE
, m_spellInfo
, m_IsTriggeredSpell
);
2128 // update pointers base at GUIDs to prevent access to non-existed already object
2129 UpdatePointers(); // pointers can be invalidate at triggered spell casting
2132 // Okay, everything is prepared. Now we need to distinguish between immediate and evented delayed spells
2133 if (m_spellInfo
->speed
> 0.0f
)
2136 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2137 // in case delayed spell remove item at cast delay start
2140 // Okay, maps created, now prepare flags
2141 m_immediateHandled
= false;
2142 m_spellState
= SPELL_STATE_DELAYED
;
2147 // Immediate spell, no big deal
2151 SetExecutedCurrently(false);
2154 void Spell::handle_immediate()
2156 // start channeling if applicable
2157 if(IsChanneledSpell(m_spellInfo
))
2159 m_spellState
= SPELL_STATE_CASTING
;
2160 SendChannelStart(GetSpellDuration(m_spellInfo
));
2163 // process immediate effects (items, ground, etc.) also initialize some variables
2164 _handle_immediate_phase();
2166 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2167 DoAllEffectOnTarget(&(*ihit
));
2169 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
2170 DoAllEffectOnTarget(&(*ihit
));
2172 // spell is finished, perform some last features of the spell here
2173 _handle_finish_phase();
2175 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2178 if(m_spellState
!= SPELL_STATE_CASTING
)
2179 finish(true); // successfully finish spell cast (not last in case autorepeat or channel spell)
2182 uint64
Spell::handle_delayed(uint64 t_offset
)
2184 uint64 next_time
= 0;
2186 if (!m_immediateHandled
)
2188 _handle_immediate_phase();
2189 m_immediateHandled
= true;
2192 // 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)
2193 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2195 if (ihit
->processed
== false)
2197 if ( ihit
->timeDelay
<= t_offset
)
2198 DoAllEffectOnTarget(&(*ihit
));
2199 else if( next_time
== 0 || ihit
->timeDelay
< next_time
)
2200 next_time
= ihit
->timeDelay
;
2204 // now recheck gameobject targeting correctness
2205 for(std::list
<GOTargetInfo
>::iterator ighit
= m_UniqueGOTargetInfo
.begin(); ighit
!= m_UniqueGOTargetInfo
.end();++ighit
)
2207 if (ighit
->processed
== false)
2209 if ( ighit
->timeDelay
<= t_offset
)
2210 DoAllEffectOnTarget(&(*ighit
));
2211 else if( next_time
== 0 || ighit
->timeDelay
< next_time
)
2212 next_time
= ighit
->timeDelay
;
2215 // All targets passed - need finish phase
2218 // spell is finished, perform some last features of the spell here
2219 _handle_finish_phase();
2221 finish(true); // successfully finish spell cast
2223 // return zero, spell is finished now
2228 // spell is unfinished, return next execution time
2233 void Spell::_handle_immediate_phase()
2235 // handle some immediate features of the spell here
2236 HandleThreatSpells(m_spellInfo
->Id
);
2238 m_needSpellLog
= IsNeedSendToClient();
2239 for(uint32 j
= 0;j
<3;j
++)
2241 if(m_spellInfo
->Effect
[j
]==0)
2244 // apply Send Event effect to ground in case empty target lists
2245 if( m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SEND_EVENT
&& !HaveTargetsForEffect(j
) )
2247 HandleEffects(NULL
,NULL
,NULL
, j
);
2251 // Don't do spell log, if is school damage spell
2252 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SCHOOL_DAMAGE
|| m_spellInfo
->Effect
[j
] == 0)
2253 m_needSpellLog
= false;
2255 uint32 EffectChainTarget
= m_spellInfo
->EffectChainTarget
[j
];
2256 if(m_originalCaster
)
2257 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
2258 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_JUMP_TARGETS
, EffectChainTarget
, this);
2260 // initialize multipliers
2261 m_damageMultipliers
[j
] = 1.0f
;
2262 if( (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_DAMAGE
|| m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_HEAL
) &&
2263 (EffectChainTarget
> 1) )
2264 m_applyMultiplierMask
|= 1 << j
;
2267 // initialize Diminishing Returns Data
2268 m_diminishLevel
= DIMINISHING_LEVEL_1
;
2269 m_diminishGroup
= DIMINISHING_NONE
;
2272 for(std::list
<ItemTargetInfo
>::iterator ihit
= m_UniqueItemInfo
.begin();ihit
!= m_UniqueItemInfo
.end();++ihit
)
2273 DoAllEffectOnTarget(&(*ihit
));
2276 for(uint32 j
= 0;j
<3;j
++)
2278 // persistent area auras target only the ground
2279 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_PERSISTENT_AREA_AURA
)
2280 HandleEffects(NULL
,NULL
,NULL
, j
);
2284 void Spell::_handle_finish_phase()
2291 void Spell::SendSpellCooldown()
2293 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
2296 Player
* _player
= (Player
*)m_caster
;
2297 // Add cooldown for max (disable spell)
2298 // Cooldown started on SendCooldownEvent call
2299 if (m_spellInfo
->Attributes
& SPELL_ATTR_DISABLED_WHILE_ACTIVE
)
2301 _player
->AddSpellCooldown(m_spellInfo
->Id
, 0, time(NULL
) - 1);
2305 // init cooldown values
2310 // some special item spells without correct cooldown in SpellInfo
2311 // cooldown information stored in item prototype
2312 // This used in same way in WorldSession::HandleItemQuerySingleOpcode data sending to client.
2316 ItemPrototype
const* proto
= m_CastItem
->GetProto();
2319 for(int idx
= 0; idx
< 5; ++idx
)
2321 if(proto
->Spells
[idx
].SpellId
== m_spellInfo
->Id
)
2323 cat
= proto
->Spells
[idx
].SpellCategory
;
2324 rec
= proto
->Spells
[idx
].SpellCooldown
;
2325 catrec
= proto
->Spells
[idx
].SpellCategoryCooldown
;
2332 // if no cooldown found above then base at DBC data
2333 if(rec
< 0 && catrec
< 0)
2335 cat
= m_spellInfo
->Category
;
2336 rec
= m_spellInfo
->RecoveryTime
;
2337 catrec
= m_spellInfo
->CategoryRecoveryTime
;
2340 // shoot spells used equipped item cooldown values already assigned in GetAttackTime(RANGED_ATTACK)
2341 // prevent 0 cooldowns set by another way
2342 if (rec
<= 0 && catrec
<= 0 && (cat
== 76 || cat
== 351))
2343 rec
= _player
->GetAttackTime(RANGED_ATTACK
);
2345 // Now we have cooldown data (if found any), time to apply mods
2347 _player
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COOLDOWN
, rec
, this);
2350 _player
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COOLDOWN
, catrec
, this);
2352 // replace negative cooldowns by 0
2353 if (rec
< 0) rec
= 0;
2354 if (catrec
< 0) catrec
= 0;
2356 // no cooldown after applying spell mods
2357 if( rec
== 0 && catrec
== 0)
2360 time_t curTime
= time(NULL
);
2362 time_t catrecTime
= catrec
? curTime
+catrec
/1000 : 0; // in secs
2363 time_t recTime
= rec
? curTime
+rec
/1000 : catrecTime
;// in secs
2365 // self spell cooldown
2367 _player
->AddSpellCooldown(m_spellInfo
->Id
, m_CastItem
? m_CastItem
->GetEntry() : 0, recTime
);
2372 SpellCategoryStore::const_iterator i_scstore
= sSpellCategoryStore
.find(cat
);
2373 if(i_scstore
!= sSpellCategoryStore
.end())
2375 for(SpellCategorySet::const_iterator i_scset
= i_scstore
->second
.begin(); i_scset
!= i_scstore
->second
.end(); ++i_scset
)
2377 if(*i_scset
== m_spellInfo
->Id
) // skip main spell, already handled above
2380 _player
->AddSpellCooldown(m_spellInfo
->Id
, m_CastItem
? m_CastItem
->GetEntry() : 0, catrecTime
);
2386 void Spell::update(uint32 difftime
)
2388 // update pointers based at it's GUIDs
2391 if(m_targets
.getUnitTargetGUID() && !m_targets
.getUnitTarget())
2397 // check if the player caster has moved before the spell finished
2398 if ((m_caster
->GetTypeId() == TYPEID_PLAYER
&& m_timer
!= 0) &&
2399 (m_castPositionX
!= m_caster
->GetPositionX() || m_castPositionY
!= m_caster
->GetPositionY() || m_castPositionZ
!= m_caster
->GetPositionZ()) &&
2400 (m_spellInfo
->Effect
[0] != SPELL_EFFECT_STUCK
|| !m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_FALLING
)))
2402 // always cancel for channeled spells
2403 if( m_spellState
== SPELL_STATE_CASTING
)
2405 // don't cancel for melee, autorepeat, triggered and instant spells
2406 else if(!IsNextMeleeSwingSpell() && !IsAutoRepeat() && !m_IsTriggeredSpell
&& (m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_MOVEMENT
))
2410 switch(m_spellState
)
2412 case SPELL_STATE_PREPARING
:
2416 if(difftime
>= m_timer
)
2419 m_timer
-= difftime
;
2422 if(m_timer
== 0 && !IsNextMeleeSwingSpell() && !IsAutoRepeat())
2425 case SPELL_STATE_CASTING
:
2429 if( m_caster
->GetTypeId() == TYPEID_PLAYER
)
2431 // check if player has jumped before the channeling finished
2432 if(m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_JUMPING
))
2435 // check for incapacitating player states
2436 if( m_caster
->hasUnitState(UNIT_STAT_STUNNED
| UNIT_STAT_CONFUSED
))
2439 // check if player has turned if flag is set
2440 if( m_spellInfo
->ChannelInterruptFlags
& CHANNEL_FLAG_TURNING
&& m_castOrientation
!= m_caster
->GetOrientation() )
2444 // check if there are alive targets left
2445 if (!IsAliveUnitPresentInTargetList())
2447 SendChannelUpdate(0);
2451 if(difftime
>= m_timer
)
2454 m_timer
-= difftime
;
2459 SendChannelUpdate(0);
2461 // channeled spell processed independently for quest targeting
2462 // cast at creature (or GO) quest objectives update at successful cast channel finished
2463 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
2464 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() )
2466 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2468 TargetInfo
* target
= &*ihit
;
2469 if(!IS_CREATURE_GUID(target
->targetGUID
))
2472 Unit
* unit
= m_caster
->GetGUID()==target
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
,target
->targetGUID
);
2476 ((Player
*)m_caster
)->CastedCreatureOrGO(unit
->GetEntry(),unit
->GetGUID(),m_spellInfo
->Id
);
2479 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
2481 GOTargetInfo
* target
= &*ihit
;
2483 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, target
->targetGUID
);
2487 ((Player
*)m_caster
)->CastedCreatureOrGO(go
->GetEntry(),go
->GetGUID(),m_spellInfo
->Id
);
2500 void Spell::finish(bool ok
)
2505 if(m_spellState
== SPELL_STATE_FINISHED
)
2508 m_spellState
= SPELL_STATE_FINISHED
;
2511 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2512 ((Player
*)m_caster
)->RemoveSpellMods(this);
2514 // other code related only to successfully finished spells
2518 //handle SPELL_AURA_ADD_TARGET_TRIGGER auras
2519 Unit::AuraList
const& targetTriggers
= m_caster
->GetAurasByType(SPELL_AURA_ADD_TARGET_TRIGGER
);
2520 for(Unit::AuraList::const_iterator i
= targetTriggers
.begin(); i
!= targetTriggers
.end(); ++i
)
2522 SpellEntry
const *auraSpellInfo
= (*i
)->GetSpellProto();
2523 uint32 auraSpellIdx
= (*i
)->GetEffIndex();
2524 if (IsAffectedBy(auraSpellInfo
, auraSpellIdx
))
2526 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2527 if( ihit
->effectMask
& (1<<auraSpellIdx
) )
2529 // check m_caster->GetGUID() let load auras at login and speedup most often case
2530 Unit
*unit
= m_caster
->GetGUID()== ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
2531 if (unit
&& unit
->isAlive())
2533 // Calculate chance at that moment (can be depend for example from combo points)
2534 int32 chance
= m_caster
->CalculateSpellDamage(auraSpellInfo
, auraSpellIdx
, (*i
)->GetBasePoints(),unit
);
2536 if(roll_chance_i(chance
))
2537 m_caster
->CastSpell(unit
, auraSpellInfo
->EffectTriggerSpell
[auraSpellIdx
], true, NULL
, (*i
));
2543 if (IsMeleeAttackResetSpell())
2545 m_caster
->resetAttackTimer(BASE_ATTACK
);
2546 if(m_caster
->haveOffhandWeapon())
2547 m_caster
->resetAttackTimer(OFF_ATTACK
);
2550 /*if (IsRangedAttackResetSpell())
2551 m_caster->resetAttackTimer(RANGED_ATTACK);*/
2553 // Clear combo at finish state
2554 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& NeedsComboPoints(m_spellInfo
))
2556 // Not drop combopoints if any miss exist
2557 bool needDrop
= true;
2558 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2559 if (ihit
->missCondition
!= SPELL_MISS_NONE
)
2565 ((Player
*)m_caster
)->ClearComboPoints();
2568 // call triggered spell only at successful cast (after clear combo points -> for add some if need)
2569 if(!m_TriggerSpells
.empty())
2572 // Stop Attack for some spells
2573 if( m_spellInfo
->Attributes
& SPELL_ATTR_STOP_ATTACK_TARGET
)
2574 m_caster
->AttackStop();
2577 void Spell::SendCastResult(uint8 result
)
2579 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2582 if(((Player
*)m_caster
)->GetSession()->PlayerLoading()) // don't send cast results at loading time
2587 WorldPacket
data(SMSG_CAST_FAILED
, (4+1+1));
2588 data
<< uint8(m_cast_count
); // single cast or multi 2.3 (0/1)
2589 data
<< uint32(m_spellInfo
->Id
);
2590 data
<< uint8(result
); // problem
2593 case SPELL_FAILED_REQUIRES_SPELL_FOCUS
:
2594 data
<< uint32(m_spellInfo
->RequiresSpellFocus
);
2596 case SPELL_FAILED_REQUIRES_AREA
:
2597 // hardcode areas limitation case
2598 switch(m_spellInfo
->Id
)
2600 case 41617: // Cenarion Mana Salve
2601 case 41619: // Cenarion Healing Salve
2602 data
<< uint32(3905);
2604 case 41618: // Bottled Nethergon Energy
2605 case 41620: // Bottled Nethergon Vapor
2606 data
<< uint32(3842);
2608 case 45373: // Bloodberry Elixir
2609 data
<< uint32(4075);
2611 default: // default case
2612 data
<< uint32(m_spellInfo
->AreaId
);
2616 case SPELL_FAILED_TOTEMS
:
2617 if(m_spellInfo
->Totem
[0])
2618 data
<< uint32(m_spellInfo
->Totem
[0]);
2619 if(m_spellInfo
->Totem
[1])
2620 data
<< uint32(m_spellInfo
->Totem
[1]);
2622 case SPELL_FAILED_TOTEM_CATEGORY
:
2623 if(m_spellInfo
->TotemCategory
[0])
2624 data
<< uint32(m_spellInfo
->TotemCategory
[0]);
2625 if(m_spellInfo
->TotemCategory
[1])
2626 data
<< uint32(m_spellInfo
->TotemCategory
[1]);
2628 case SPELL_FAILED_EQUIPPED_ITEM_CLASS
:
2629 data
<< uint32(m_spellInfo
->EquippedItemClass
);
2630 data
<< uint32(m_spellInfo
->EquippedItemSubClassMask
);
2631 //data << uint32(m_spellInfo->EquippedItemInventoryTypeMask);
2634 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
2638 void Spell::SendSpellStart()
2640 if(!IsNeedSendToClient())
2643 sLog
.outDebug("Sending SMSG_SPELL_START id=%u", m_spellInfo
->Id
);
2645 uint32 castFlags
= CAST_FLAG_UNKNOWN1
;
2647 castFlags
|= CAST_FLAG_AMMO
;
2649 if(m_spellInfo
->runeCostID
)
2650 castFlags
|= CAST_FLAG_UNKNOWN10
;
2652 Unit
*target
= m_targets
.getUnitTarget() ? m_targets
.getUnitTarget() : m_caster
;
2654 WorldPacket
data(SMSG_SPELL_START
, (8+8+4+4+2));
2656 data
.append(m_CastItem
->GetPackGUID());
2658 data
.append(m_caster
->GetPackGUID());
2660 data
.append(m_caster
->GetPackGUID());
2661 data
<< uint8(m_cast_count
); // pending spell cast?
2662 data
<< uint32(m_spellInfo
->Id
); // spellId
2663 data
<< uint32(castFlags
); // cast flags
2664 data
<< uint32(m_timer
); // delay?
2666 m_targets
.write(&data
);
2668 if ( castFlags
& CAST_FLAG_UNKNOWN6
) // predicted power?
2671 if ( castFlags
& CAST_FLAG_UNKNOWN7
) // rune cooldowns list
2673 uint8 v1
= 0;//m_runesState;
2674 uint8 v2
= 0;//((Player*)m_caster)->GetRunesState();
2675 data
<< uint8(v1
); // runes state before
2676 data
<< uint8(v2
); // runes state after
2677 for(uint8 i
= 0; i
< MAX_RUNES
; ++i
)
2680 if(m
& v1
) // usable before...
2681 if(!(m
& v2
)) // ...but on cooldown now...
2682 data
<< uint8(0); // some unknown byte (time?)
2686 if ( castFlags
& CAST_FLAG_AMMO
)
2687 WriteAmmoToPacket(&data
);
2689 m_caster
->SendMessageToSet(&data
, true);
2692 void Spell::SendSpellGo()
2694 // not send invisible spell casting
2695 if(!IsNeedSendToClient())
2698 sLog
.outDebug("Sending SMSG_SPELL_GO id=%u", m_spellInfo
->Id
);
2700 Unit
*target
= m_targets
.getUnitTarget() ? m_targets
.getUnitTarget() : m_caster
;
2702 uint32 castFlags
= CAST_FLAG_UNKNOWN3
;
2704 castFlags
|= CAST_FLAG_AMMO
; // arrows/bullets visual
2706 if((m_caster
->GetTypeId() == TYPEID_PLAYER
) && (m_caster
->getClass() == CLASS_DEATH_KNIGHT
) && m_spellInfo
->runeCostID
)
2708 castFlags
|= CAST_FLAG_UNKNOWN10
; // same as in SMSG_SPELL_START
2709 castFlags
|= CAST_FLAG_UNKNOWN6
; // makes cooldowns visible
2710 castFlags
|= CAST_FLAG_UNKNOWN7
; // rune cooldowns list
2713 WorldPacket
data(SMSG_SPELL_GO
, 50); // guess size
2715 data
.append(m_CastItem
->GetPackGUID());
2717 data
.append(m_caster
->GetPackGUID());
2719 data
.append(m_caster
->GetPackGUID());
2720 data
<< uint8(m_cast_count
); // pending spell cast?
2721 data
<< uint32(m_spellInfo
->Id
); // spellId
2722 data
<< uint32(castFlags
); // cast flags
2723 data
<< uint32(getMSTime()); // timestamp
2725 WriteSpellGoTargets(&data
);
2727 m_targets
.write(&data
);
2729 if ( castFlags
& CAST_FLAG_UNKNOWN6
) // unknown wotlk, predicted power?
2732 if ( castFlags
& CAST_FLAG_UNKNOWN7
) // rune cooldowns list
2734 uint8 v1
= m_runesState
;
2735 uint8 v2
= ((Player
*)m_caster
)->GetRunesState();
2736 data
<< uint8(v1
); // runes state before
2737 data
<< uint8(v2
); // runes state after
2738 for(uint8 i
= 0; i
< MAX_RUNES
; ++i
)
2741 if(m
& v1
) // usable before...
2742 if(!(m
& v2
)) // ...but on cooldown now...
2743 data
<< uint8(0); // some unknown byte (time?)
2747 if ( castFlags
& CAST_FLAG_UNKNOWN4
) // unknown wotlk
2753 if ( castFlags
& CAST_FLAG_AMMO
)
2754 WriteAmmoToPacket(&data
);
2756 if ( castFlags
& CAST_FLAG_UNKNOWN5
) // unknown wotlk
2762 if ( m_targets
.m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
2767 m_caster
->SendMessageToSet(&data
, true);
2770 void Spell::WriteAmmoToPacket( WorldPacket
* data
)
2772 uint32 ammoInventoryType
= 0;
2773 uint32 ammoDisplayID
= 0;
2775 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2777 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack( RANGED_ATTACK
);
2780 ammoInventoryType
= pItem
->GetProto()->InventoryType
;
2781 if( ammoInventoryType
== INVTYPE_THROWN
)
2782 ammoDisplayID
= pItem
->GetProto()->DisplayInfoID
;
2785 uint32 ammoID
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
2788 ItemPrototype
const *pProto
= objmgr
.GetItemPrototype( ammoID
);
2791 ammoDisplayID
= pProto
->DisplayInfoID
;
2792 ammoInventoryType
= pProto
->InventoryType
;
2795 else if(m_caster
->GetDummyAura(46699)) // Requires No Ammo
2797 ammoDisplayID
= 5996; // normal arrow
2798 ammoInventoryType
= INVTYPE_AMMO
;
2803 // TODO: implement selection ammo data based at ranged weapon stored in equipmodel/equipinfo/equipslot fields
2805 *data
<< uint32(ammoDisplayID
);
2806 *data
<< uint32(ammoInventoryType
);
2809 void Spell::WriteSpellGoTargets( WorldPacket
* data
)
2811 *data
<< (uint8
)m_countOfHit
;
2812 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2813 if ((*ihit
).missCondition
== SPELL_MISS_NONE
) // Add only hits
2814 *data
<< uint64(ihit
->targetGUID
);
2816 for(std::list
<GOTargetInfo
>::iterator ighit
= m_UniqueGOTargetInfo
.begin();ighit
!= m_UniqueGOTargetInfo
.end();++ighit
)
2817 *data
<< uint64(ighit
->targetGUID
); // Always hits
2819 *data
<< (uint8
)m_countOfMiss
;
2820 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2822 if( ihit
->missCondition
!= SPELL_MISS_NONE
) // Add only miss
2824 *data
<< uint64(ihit
->targetGUID
);
2825 *data
<< uint8(ihit
->missCondition
);
2826 if( ihit
->missCondition
== SPELL_MISS_REFLECT
)
2827 *data
<< uint8(ihit
->reflectResult
);
2832 void Spell::SendLogExecute()
2834 Unit
*target
= m_targets
.getUnitTarget() ? m_targets
.getUnitTarget() : m_caster
;
2836 WorldPacket
data(SMSG_SPELLLOGEXECUTE
, (8+4+4+4+4+8));
2838 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
2839 data
.append(m_caster
->GetPackGUID());
2841 data
.append(target
->GetPackGUID());
2843 data
<< uint32(m_spellInfo
->Id
);
2845 data
<< uint32(count1
); // count1 (effect count?)
2846 for(uint32 i
= 0; i
< count1
; ++i
)
2848 data
<< uint32(m_spellInfo
->Effect
[0]); // spell effect
2850 data
<< uint32(count2
); // count2 (target count?)
2851 for(uint32 j
= 0; j
< count2
; ++j
)
2853 switch(m_spellInfo
->Effect
[0])
2855 case SPELL_EFFECT_POWER_DRAIN
:
2856 if(Unit
*unit
= m_targets
.getUnitTarget())
2857 data
.append(unit
->GetPackGUID());
2864 case SPELL_EFFECT_ADD_EXTRA_ATTACKS
:
2865 if(Unit
*unit
= m_targets
.getUnitTarget())
2866 data
.append(unit
->GetPackGUID());
2869 data
<< uint32(0); // count?
2871 case SPELL_EFFECT_INTERRUPT_CAST
:
2872 if(Unit
*unit
= m_targets
.getUnitTarget())
2873 data
.append(unit
->GetPackGUID());
2876 data
<< uint32(0); // spellid
2878 case SPELL_EFFECT_DURABILITY_DAMAGE
:
2879 if(Unit
*unit
= m_targets
.getUnitTarget())
2880 data
.append(unit
->GetPackGUID());
2886 case SPELL_EFFECT_OPEN_LOCK
:
2887 case SPELL_EFFECT_OPEN_LOCK_ITEM
:
2888 if(Item
*item
= m_targets
.getItemTarget())
2889 data
.append(item
->GetPackGUID());
2893 case SPELL_EFFECT_CREATE_ITEM
:
2894 case SPELL_EFFECT_157
:
2895 data
<< uint32(m_spellInfo
->EffectItemType
[0]);
2897 case SPELL_EFFECT_SUMMON
:
2898 case SPELL_EFFECT_TRANS_DOOR
:
2899 case SPELL_EFFECT_SUMMON_PET
:
2900 case SPELL_EFFECT_SUMMON_OBJECT_WILD
:
2901 case SPELL_EFFECT_CREATE_HOUSE
:
2902 case SPELL_EFFECT_DUEL
:
2903 case SPELL_EFFECT_SUMMON_OBJECT_SLOT1
:
2904 case SPELL_EFFECT_SUMMON_OBJECT_SLOT2
:
2905 case SPELL_EFFECT_SUMMON_OBJECT_SLOT3
:
2906 case SPELL_EFFECT_SUMMON_OBJECT_SLOT4
:
2907 if(Unit
*unit
= m_targets
.getUnitTarget())
2908 data
.append(unit
->GetPackGUID());
2909 else if(m_targets
.getItemTargetGUID())
2910 data
.appendPackGUID(m_targets
.getItemTargetGUID());
2911 else if(GameObject
*go
= m_targets
.getGOTarget())
2912 data
.append(go
->GetPackGUID());
2914 data
<< uint8(0); // guid
2916 case SPELL_EFFECT_FEED_PET
:
2917 data
<< uint32(m_targets
.getItemTargetEntry());
2919 case SPELL_EFFECT_DISMISS_PET
:
2920 if(Unit
*unit
= m_targets
.getUnitTarget())
2921 data
.append(unit
->GetPackGUID());
2925 case SPELL_EFFECT_RESURRECT
:
2926 case SPELL_EFFECT_RESURRECT_NEW
:
2927 if(Unit
*unit
= m_targets
.getUnitTarget())
2928 data
.append(unit
->GetPackGUID());
2938 m_caster
->SendMessageToSet(&data
, true);
2941 void Spell::SendInterrupted(uint8 result
)
2943 WorldPacket
data(SMSG_SPELL_FAILURE
, (8+4+1));
2944 data
.append(m_caster
->GetPackGUID());
2945 data
<< uint8(m_cast_count
);
2946 data
<< uint32(m_spellInfo
->Id
);
2947 data
<< uint8(result
);
2948 m_caster
->SendMessageToSet(&data
, true);
2950 data
.Initialize(SMSG_SPELL_FAILED_OTHER
, (8+4));
2951 data
.append(m_caster
->GetPackGUID());
2952 data
<< uint8(m_cast_count
);
2953 data
<< uint32(m_spellInfo
->Id
);
2954 data
<< uint8(result
);
2955 m_caster
->SendMessageToSet(&data
, true);
2958 void Spell::SendChannelUpdate(uint32 time
)
2962 m_caster
->SetUInt64Value(UNIT_FIELD_CHANNEL_OBJECT
,0);
2963 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
,0);
2966 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2969 WorldPacket
data( MSG_CHANNEL_UPDATE
, 8+4 );
2970 data
.append(m_caster
->GetPackGUID());
2971 data
<< uint32(time
);
2973 ((Player
*)m_caster
)->GetSession()->SendPacket( &data
);
2976 void Spell::SendChannelStart(uint32 duration
)
2978 WorldObject
* target
= NULL
;
2980 // select first not resisted target from target list for _0_ effect
2981 if(!m_UniqueTargetInfo
.empty())
2983 for(std::list
<TargetInfo
>::iterator itr
= m_UniqueTargetInfo
.begin();itr
!= m_UniqueTargetInfo
.end();++itr
)
2985 if( (itr
->effectMask
& (1<<0)) && itr
->reflectResult
==SPELL_MISS_NONE
&& itr
->targetGUID
!= m_caster
->GetGUID())
2987 target
= ObjectAccessor::GetUnit(*m_caster
, itr
->targetGUID
);
2992 else if(!m_UniqueGOTargetInfo
.empty())
2994 for(std::list
<GOTargetInfo
>::iterator itr
= m_UniqueGOTargetInfo
.begin();itr
!= m_UniqueGOTargetInfo
.end();++itr
)
2996 if(itr
->effectMask
& (1<<0) )
2998 target
= ObjectAccessor::GetGameObject(*m_caster
, itr
->targetGUID
);
3004 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
3006 WorldPacket
data( MSG_CHANNEL_START
, (8+4+4) );
3007 data
.append(m_caster
->GetPackGUID());
3008 data
<< uint32(m_spellInfo
->Id
);
3009 data
<< uint32(duration
);
3011 ((Player
*)m_caster
)->GetSession()->SendPacket( &data
);
3016 m_caster
->SetUInt64Value(UNIT_FIELD_CHANNEL_OBJECT
, target
->GetGUID());
3017 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
, m_spellInfo
->Id
);
3020 void Spell::SendResurrectRequest(Player
* target
)
3022 // Both players and NPCs can resurrect using spells - have a look at creature 28487 for example
3023 // However, the packet structure differs slightly
3025 const char* sentName
= m_caster
->GetTypeId()==TYPEID_PLAYER
?"":m_caster
->GetNameForLocaleIdx(target
->GetSession()->GetSessionDbLocaleIndex());
3027 WorldPacket
data(SMSG_RESURRECT_REQUEST
, (8+4+strlen(sentName
)+1+1+1));
3028 data
<< uint64(m_caster
->GetGUID());
3029 data
<< uint32(strlen(sentName
)+1);
3034 data
<< uint8(m_caster
->GetTypeId()==TYPEID_PLAYER
?0:1);
3035 target
->GetSession()->SendPacket(&data
);
3038 void Spell::SendPlaySpellVisual(uint32 SpellID
)
3040 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
3043 WorldPacket
data(SMSG_PLAY_SPELL_VISUAL
, 12);
3044 data
<< uint64(m_caster
->GetGUID());
3045 data
<< uint32(SpellID
); // spell visual id?
3046 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
3049 void Spell::TakeCastItem()
3051 if(!m_CastItem
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
3054 // not remove cast item at triggered spell (equipping, weapon damage, etc)
3055 if(m_IsTriggeredSpell
)
3058 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3062 // This code is to avoid a crash
3063 // I'm not sure, if this is really an error, but I guess every item needs a prototype
3064 sLog
.outError("Cast item has no item prototype highId=%d, lowId=%d",m_CastItem
->GetGUIDHigh(), m_CastItem
->GetGUIDLow());
3068 bool expendable
= false;
3069 bool withoutCharges
= false;
3071 for (int i
= 0; i
<5; i
++)
3073 if (proto
->Spells
[i
].SpellId
)
3075 // item has limited charges
3076 if (proto
->Spells
[i
].SpellCharges
)
3078 if (proto
->Spells
[i
].SpellCharges
< 0)
3081 int32 charges
= m_CastItem
->GetSpellCharges(i
);
3083 // item has charges left
3086 (charges
> 0) ? --charges
: ++charges
; // abs(charges) less at 1 after use
3087 if (proto
->Stackable
< 2)
3088 m_CastItem
->SetSpellCharges(i
, charges
);
3089 m_CastItem
->SetState(ITEM_CHANGED
, (Player
*)m_caster
);
3093 withoutCharges
= (charges
== 0);
3098 if (expendable
&& withoutCharges
)
3101 ((Player
*)m_caster
)->DestroyItemCount(m_CastItem
, count
, true);
3103 // prevent crash at access to deleted m_targets.getItemTarget
3104 if(m_CastItem
==m_targets
.getItemTarget())
3105 m_targets
.setItemTarget(NULL
);
3111 void Spell::TakePower()
3113 if(m_CastItem
|| m_triggeredByAuraSpell
)
3116 // health as power used
3117 if(m_spellInfo
->powerType
== POWER_HEALTH
)
3119 m_caster
->ModifyHealth( -(int32
)m_powerCost
);
3123 if(m_spellInfo
->powerType
>= MAX_POWERS
)
3125 sLog
.outError("Spell::TakePower: Unknown power type '%d'", m_spellInfo
->powerType
);
3129 Powers powerType
= Powers(m_spellInfo
->powerType
);
3131 if(powerType
== POWER_RUNE
)
3137 m_caster
->ModifyPower(powerType
, -(int32
)m_powerCost
);
3139 // Set the five second timer
3140 if (powerType
== POWER_MANA
&& m_powerCost
> 0)
3141 m_caster
->SetLastManaUse(getMSTime());
3144 uint8
Spell::CheckRuneCost(uint32 runeCostID
)
3146 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
3149 Player
*plr
= (Player
*)m_caster
;
3151 if(plr
->getClass() != CLASS_DEATH_KNIGHT
)
3154 SpellRuneCostEntry
const *src
= sSpellRuneCostStore
.LookupEntry(runeCostID
);
3159 if(src
->NoRuneCost())
3162 int32 runeCost
[NUM_RUNE_TYPES
]; // blood, frost, unholy, death
3164 for(uint32 i
= 0; i
< RUNE_DEATH
; ++i
)
3166 runeCost
[i
] = src
->RuneCost
[i
];
3169 runeCost
[RUNE_DEATH
] = 0; // calculated later
3171 for(uint32 i
= 0; i
< MAX_RUNES
; ++i
)
3173 uint8 rune
= plr
->GetCurrentRune(i
);
3174 if((plr
->GetRuneCooldown(i
) == 0) && (runeCost
[rune
] > 0))
3180 for(uint32 i
= 0; i
< RUNE_DEATH
; ++i
)
3184 runeCost
[RUNE_DEATH
] += runeCost
[i
];
3188 if(runeCost
[RUNE_DEATH
] > 0)
3189 return SPELL_FAILED_NO_POWER
; // not sure if result code is correct
3194 void Spell::TakeRunePower()
3196 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
3199 Player
*plr
= (Player
*)m_caster
;
3201 if(plr
->getClass() != CLASS_DEATH_KNIGHT
)
3204 SpellRuneCostEntry
const *src
= sSpellRuneCostStore
.LookupEntry(m_spellInfo
->runeCostID
);
3206 if(!src
|| (src
->NoRuneCost() && src
->NoRunicPowerGain()))
3209 m_runesState
= plr
->GetRunesState(); // store previous state
3211 int32 runeCost
[NUM_RUNE_TYPES
]; // blood, frost, unholy, death
3213 for(uint32 i
= 0; i
< RUNE_DEATH
; ++i
)
3215 runeCost
[i
] = src
->RuneCost
[i
];
3218 runeCost
[RUNE_DEATH
] = 0; // calculated later
3220 for(uint32 i
= 0; i
< MAX_RUNES
; ++i
)
3222 uint8 rune
= plr
->GetCurrentRune(i
);
3223 if((plr
->GetRuneCooldown(i
) == 0) && (runeCost
[rune
] > 0))
3225 plr
->SetRuneCooldown(i
, RUNE_COOLDOWN
); // 5*2=10 sec
3230 runeCost
[RUNE_DEATH
] = runeCost
[RUNE_BLOOD
] + runeCost
[RUNE_UNHOLY
] + runeCost
[RUNE_FROST
];
3232 if(runeCost
[RUNE_DEATH
] > 0)
3234 for(uint32 i
= 0; i
< MAX_RUNES
; ++i
)
3236 uint8 rune
= plr
->GetCurrentRune(i
);
3237 if((plr
->GetRuneCooldown(i
) == 0) && (rune
== RUNE_DEATH
))
3239 plr
->SetRuneCooldown(i
, RUNE_COOLDOWN
); // 5*2=10 sec
3241 plr
->ConvertRune(i
, plr
->GetBaseRune(i
));
3242 if(runeCost
[RUNE_DEATH
] == 0)
3248 // you can gain some runic power when use runes
3249 float rp
= src
->runePowerGain
;;
3250 rp
*= sWorld
.getRate(RATE_POWER_RUNICPOWER_INCOME
);
3251 plr
->ModifyPower(POWER_RUNIC_POWER
, (int32
)rp
);
3254 void Spell::TakeReagents()
3256 if(m_IsTriggeredSpell
) // reagents used in triggered spell removed by original spell or don't must be removed.
3259 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
3262 if (m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_NO_REAGENT_WHILE_PREP
&&
3263 m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PREPARATION
))
3266 Player
* p_caster
= (Player
*)m_caster
;
3268 for(uint32 x
=0;x
<8;x
++)
3270 if(m_spellInfo
->Reagent
[x
] <= 0)
3273 uint32 itemid
= m_spellInfo
->Reagent
[x
];
3274 uint32 itemcount
= m_spellInfo
->ReagentCount
[x
];
3276 // if CastItem is also spell reagent
3279 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3280 if( proto
&& proto
->ItemId
== itemid
)
3282 for(int s
=0;s
<5;s
++)
3284 // CastItem will be used up and does not count as reagent
3285 int32 charges
= m_CastItem
->GetSpellCharges(s
);
3286 if (proto
->Spells
[s
].SpellCharges
< 0 && abs(charges
) < 2)
3297 // if getItemTarget is also spell reagent
3298 if (m_targets
.getItemTargetEntry()==itemid
)
3299 m_targets
.setItemTarget(NULL
);
3301 p_caster
->DestroyItemCount(itemid
, itemcount
, true);
3305 void Spell::HandleThreatSpells(uint32 spellId
)
3307 if(!m_targets
.getUnitTarget() || !spellId
)
3310 if(!m_targets
.getUnitTarget()->CanHaveThreatList())
3313 SpellThreatEntry
const *threatSpell
= sSpellThreatStore
.LookupEntry
<SpellThreatEntry
>(spellId
);
3317 m_targets
.getUnitTarget()->AddThreat(m_caster
, float(threatSpell
->threat
));
3319 DEBUG_LOG("Spell %u, rank %u, added an additional %i threat", spellId
, spellmgr
.GetSpellRank(spellId
), threatSpell
->threat
);
3322 void Spell::HandleEffects(Unit
*pUnitTarget
,Item
*pItemTarget
,GameObject
*pGOTarget
,uint32 i
, float DamageMultiplier
)
3324 unitTarget
= pUnitTarget
;
3325 itemTarget
= pItemTarget
;
3326 gameObjTarget
= pGOTarget
;
3328 uint8 eff
= m_spellInfo
->Effect
[i
];
3329 uint32 mechanic
= m_spellInfo
->EffectMechanic
[i
];
3331 damage
= int32(CalculateDamage((uint8
)i
,unitTarget
)*DamageMultiplier
);
3333 sLog
.outDebug( "Spell: Effect : %u", eff
);
3335 //Simply return. Do not display "immune" in red text on client
3336 if(unitTarget
&& unitTarget
->IsImmunedToSpellEffect(eff
, mechanic
))
3339 if(eff
<TOTAL_SPELL_EFFECTS
)
3341 //sLog.outDebug( "WORLD: Spell FX %d < TOTAL_SPELL_EFFECTS ", eff);
3342 (*this.*SpellEffects
[eff
])(i
);
3347 sLog.outDebug( "WORLD: Spell FX %d > TOTAL_SPELL_EFFECTS ", eff);
3349 EffectEnchantItemTmp(i);
3352 sLog.outError("SPELL: unknown effect %u spell id %u\n",
3353 eff, m_spellInfo->Id);
3359 void Spell::TriggerSpell()
3361 for(TriggerSpells::iterator si
=m_TriggerSpells
.begin(); si
!=m_TriggerSpells
.end(); ++si
)
3363 Spell
* spell
= new Spell(m_caster
, (*si
), true, m_originalCasterGUID
, m_selfContainer
);
3364 spell
->prepare(&m_targets
); // use original spell original targets
3368 uint8
Spell::CanCast(bool strict
)
3370 // check cooldowns to prevent cheating
3371 if(m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
3373 if(m_triggeredByAuraSpell
)
3374 return SPELL_FAILED_DONT_REPORT
;
3376 return SPELL_FAILED_NOT_READY
;
3379 // only allow triggered spells if at an ended battleground
3380 if( !m_IsTriggeredSpell
&& m_caster
->GetTypeId() == TYPEID_PLAYER
)
3381 if(BattleGround
* bg
= ((Player
*)m_caster
)->GetBattleGround())
3382 if(bg
->GetStatus() == STATUS_WAIT_LEAVE
)
3383 return SPELL_FAILED_DONT_REPORT
;
3385 // only check at first call, Stealth auras are already removed at second call
3386 // for now, ignore triggered spells
3387 if( strict
&& !m_IsTriggeredSpell
)
3389 // Cannot be used in this stance/form
3390 if(uint8 shapeError
= GetErrorAtShapeshiftedCast(m_spellInfo
, m_caster
->m_form
))
3393 if ((m_spellInfo
->Attributes
& SPELL_ATTR_ONLY_STEALTHED
) && !(m_caster
->HasStealthAura()))
3394 return SPELL_FAILED_ONLY_STEALTHED
;
3397 // caster state requirements
3398 if(m_spellInfo
->CasterAuraState
&& !m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraState
)))
3399 return SPELL_FAILED_CASTER_AURASTATE
;
3400 if(m_spellInfo
->CasterAuraStateNot
&& m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraStateNot
)))
3401 return SPELL_FAILED_CASTER_AURASTATE
;
3403 // cancel autorepeat spells if cast start when moving
3404 // (not wand currently autorepeat cast delayed to moving stop anyway in spell update code)
3405 if( m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->isMoving() )
3407 // skip stuck spell to allow use it in falling case and apply spell limitations at movement
3408 if( (!m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_FALLING
) || m_spellInfo
->Effect
[0] != SPELL_EFFECT_STUCK
) &&
3409 (IsAutoRepeat() || (m_spellInfo
->AuraInterruptFlags
& AURA_INTERRUPT_FLAG_NOT_SEATED
) != 0) )
3410 return SPELL_FAILED_MOVING
;
3413 Unit
*target
= m_targets
.getUnitTarget();
3417 // target state requirements (not allowed state), apply to self also
3418 if(m_spellInfo
->TargetAuraStateNot
&& target
->HasAuraState(AuraState(m_spellInfo
->TargetAuraStateNot
)))
3419 return SPELL_FAILED_TARGET_AURASTATE
;
3421 if(target
!= m_caster
)
3423 // target state requirements (apply to non-self only), to allow cast affects to self like Dirty Deeds
3424 if(m_spellInfo
->TargetAuraState
&& !target
->HasAuraState(AuraState(m_spellInfo
->TargetAuraState
)))
3425 return SPELL_FAILED_TARGET_AURASTATE
;
3427 // Not allow casting on flying player
3428 if (target
->isInFlight())
3429 return SPELL_FAILED_BAD_TARGETS
;
3431 if(!m_IsTriggeredSpell
&& VMAP::VMapFactory::checkSpellForLoS(m_spellInfo
->Id
) && !m_caster
->IsWithinLOSInMap(target
))
3432 return SPELL_FAILED_LINE_OF_SIGHT
;
3434 // auto selection spell rank implemented in WorldSession::HandleCastSpellOpcode
3435 // this case can be triggered if rank not found (too low-level target for first rank)
3436 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsPassiveSpell(m_spellInfo
->Id
) && !m_CastItem
)
3438 for(int i
=0;i
<3;i
++)
3440 if(IsPositiveEffect(m_spellInfo
->Id
, i
) && m_spellInfo
->Effect
[i
] == SPELL_EFFECT_APPLY_AURA
)
3441 if(target
->getLevel() + 10 < m_spellInfo
->spellLevel
)
3442 return SPELL_FAILED_LOWLEVEL
;
3447 // check pet presents
3448 for(int j
=0;j
<3;j
++)
3450 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_PET
)
3452 target
= m_caster
->GetPet();
3455 if(m_triggeredByAuraSpell
) // not report pet not existence for triggered spells
3456 return SPELL_FAILED_DONT_REPORT
;
3458 return SPELL_FAILED_NO_PET
;
3464 //check creature type
3465 //ignore self casts (including area casts when caster selected as target)
3466 if(target
!= m_caster
)
3468 if(!CheckTargetCreatureType(target
))
3470 if(target
->GetTypeId()==TYPEID_PLAYER
)
3471 return SPELL_FAILED_TARGET_IS_PLAYER
;
3473 return SPELL_FAILED_BAD_TARGETS
;
3477 // TODO: this check can be applied and for player to prevent cheating when IsPositiveSpell will return always correct result.
3478 // check target for pet/charmed casts (not self targeted), self targeted cast used for area effects and etc
3479 if(m_caster
!= target
&& m_caster
->GetTypeId()==TYPEID_UNIT
&& m_caster
->GetCharmerOrOwnerGUID())
3481 // check correctness positive/negative cast target (pet cast real check and cheating check)
3482 if(IsPositiveSpell(m_spellInfo
->Id
))
3484 if(m_caster
->IsHostileTo(target
))
3485 return SPELL_FAILED_BAD_TARGETS
;
3489 if(m_caster
->IsFriendlyTo(target
))
3490 return SPELL_FAILED_BAD_TARGETS
;
3494 if(IsPositiveSpell(m_spellInfo
->Id
))
3496 if(target
->IsImmunedToSpell(m_spellInfo
,false))
3497 return SPELL_FAILED_TARGET_AURASTATE
;
3500 //Must be behind the target.
3501 if( m_spellInfo
->AttributesEx2
== 0x100000 && (m_spellInfo
->AttributesEx
& 0x200) == 0x200 && target
->HasInArc(M_PI
, m_caster
) )
3504 return SPELL_FAILED_NOT_BEHIND
;
3507 //Target must be facing you.
3508 if((m_spellInfo
->Attributes
== 0x150010) && !target
->HasInArc(M_PI
, m_caster
) )
3511 return SPELL_FAILED_NOT_INFRONT
;
3514 // check if target is in combat
3515 if (target
!= m_caster
&& (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_NOT_IN_COMBAT_TARGET
) && target
->isInCombat())
3517 return SPELL_FAILED_TARGET_AFFECTING_COMBAT
;
3520 // Spell casted only on battleground
3521 if((m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_BATTLEGROUND
) && m_caster
->GetTypeId()==TYPEID_PLAYER
)
3522 if(!((Player
*)m_caster
)->InBattleGround())
3523 return SPELL_FAILED_ONLY_BATTLEGROUNDS
;
3525 // do not allow spells to be cast in arenas
3526 // - with greater than 15 min CD without SPELL_ATTR_EX4_USABLE_IN_ARENA flag
3527 // - with SPELL_ATTR_EX4_NOT_USABLE_IN_ARENA flag
3528 if( (m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_NOT_USABLE_IN_ARENA
) ||
3529 GetSpellRecoveryTime(m_spellInfo
) > 15 * MINUTE
* 1000 && !(m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_USABLE_IN_ARENA
) )
3530 if(MapEntry
const* mapEntry
= sMapStore
.LookupEntry(m_caster
->GetMapId()))
3531 if(mapEntry
->IsBattleArena())
3532 return SPELL_FAILED_NOT_IN_ARENA
;
3535 if(!IsSpellAllowedInLocation(m_spellInfo
,m_caster
->GetMapId(),m_caster
->GetZoneId(),m_caster
->GetAreaId()))
3536 return SPELL_FAILED_REQUIRES_AREA
;
3538 // not let players cast spells at mount (and let do it to creatures)
3539 if( m_caster
->IsMounted() && m_caster
->GetTypeId()==TYPEID_PLAYER
&& !m_IsTriggeredSpell
&&
3540 !IsPassiveSpell(m_spellInfo
->Id
) && !(m_spellInfo
->Attributes
& SPELL_ATTR_CASTABLE_WHILE_MOUNTED
) )
3542 if(m_caster
->isInFlight())
3543 return SPELL_FAILED_NOT_FLYING
;
3545 return SPELL_FAILED_NOT_MOUNTED
;
3548 // always (except passive spells) check items (focus object can be required for any type casts)
3549 if(!IsPassiveSpell(m_spellInfo
->Id
))
3550 if(uint8 castResult
= CheckItems())
3553 //ImpliciteTargetA-B = 38, If fact there is 0 Spell with ImpliciteTargetB=38
3554 if(m_UniqueTargetInfo
.empty()) // skip second canCast apply (for delayed spells for example)
3556 for(uint8 j
= 0; j
< 3; j
++)
3558 if( m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT
||
3559 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT
&& m_spellInfo
->EffectImplicitTargetA
[j
] != TARGET_SELF
||
3560 m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3561 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3563 SpellScriptTarget::const_iterator lower
= spellmgr
.GetBeginSpellScriptTarget(m_spellInfo
->Id
);
3564 SpellScriptTarget::const_iterator upper
= spellmgr
.GetEndSpellScriptTarget(m_spellInfo
->Id
);
3566 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
);
3568 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
3569 float range
= GetSpellMaxRange(srange
);
3571 Creature
* creatureScriptTarget
= NULL
;
3572 GameObject
* goScriptTarget
= NULL
;
3574 for(SpellScriptTarget::const_iterator i_spellST
= lower
; i_spellST
!= upper
; ++i_spellST
)
3576 switch(i_spellST
->second
.type
)
3578 case SPELL_TARGET_TYPE_GAMEOBJECT
:
3580 GameObject
* p_GameObject
= NULL
;
3582 if(i_spellST
->second
.targetEntry
)
3584 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
3586 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
3588 MaNGOS::NearestGameObjectEntryInObjectRangeCheck
go_check(*m_caster
,i_spellST
->second
.targetEntry
,range
);
3589 MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
> checker(p_GameObject
,go_check
);
3591 TypeContainerVisitor
<MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
>, GridTypeMapContainer
> object_checker(checker
);
3592 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
3593 cell_lock
->Visit(cell_lock
, object_checker
, *m_caster
->GetMap());
3597 // remember found target and range, next attempt will find more near target with another entry
3598 creatureScriptTarget
= NULL
;
3599 goScriptTarget
= p_GameObject
;
3600 range
= go_check
.GetLastRange();
3603 else if( focusObject
) //Focus Object
3605 float frange
= m_caster
->GetDistance(focusObject
);
3608 creatureScriptTarget
= NULL
;
3609 goScriptTarget
= focusObject
;
3615 case SPELL_TARGET_TYPE_CREATURE
:
3616 case SPELL_TARGET_TYPE_DEAD
:
3619 Creature
*p_Creature
= NULL
;
3621 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
3623 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
3624 cell
.SetNoCreate(); // Really don't know what is that???
3626 MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
u_check(*m_caster
,i_spellST
->second
.targetEntry
,i_spellST
->second
.type
!=SPELL_TARGET_TYPE_DEAD
,range
);
3627 MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
> searcher(p_Creature
, u_check
);
3629 TypeContainerVisitor
<MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
>, GridTypeMapContainer
> grid_creature_searcher(searcher
);
3631 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
3632 cell_lock
->Visit(cell_lock
, grid_creature_searcher
, *m_caster
->GetMap());
3636 creatureScriptTarget
= p_Creature
;
3637 goScriptTarget
= NULL
;
3638 range
= u_check
.GetLastRange();
3645 if(creatureScriptTarget
)
3647 // store coordinates for TARGET_SCRIPT_COORDINATES
3648 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3649 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3651 m_targets
.setDestination(creatureScriptTarget
->GetPositionX(),creatureScriptTarget
->GetPositionY(),creatureScriptTarget
->GetPositionZ());
3653 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->EffectImplicitTargetB
[j
] == 0 && m_spellInfo
->Effect
[j
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
3654 AddUnitTarget(creatureScriptTarget
, j
);
3656 // store explicit target for TARGET_SCRIPT
3658 AddUnitTarget(creatureScriptTarget
, j
);
3660 else if(goScriptTarget
)
3662 // store coordinates for TARGET_SCRIPT_COORDINATES
3663 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3664 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3666 m_targets
.setDestination(goScriptTarget
->GetPositionX(),goScriptTarget
->GetPositionY(),goScriptTarget
->GetPositionZ());
3668 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->EffectImplicitTargetB
[j
] == 0 && m_spellInfo
->Effect
[j
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
3669 AddGOTarget(goScriptTarget
, j
);
3671 // store explicit target for TARGET_SCRIPT
3673 AddGOTarget(goScriptTarget
, j
);
3675 //Missing DB Entry or targets for this spellEffect.
3678 // not report target not existence for triggered spells
3679 if(m_triggeredByAuraSpell
|| m_IsTriggeredSpell
)
3680 return SPELL_FAILED_DONT_REPORT
;
3682 return SPELL_FAILED_BAD_TARGETS
;
3688 if(uint8 castResult
= CheckRange(strict
))
3692 if(uint8 castResult
= CheckPower())
3696 if(!m_triggeredByAuraSpell
) // triggered spell not affected by stun/etc
3697 if(uint8 castResult
= CheckCasterAuras())
3700 for (int i
= 0; i
< 3; i
++)
3702 // for effects of spells that have only one target
3703 switch(m_spellInfo
->Effect
[i
])
3705 case SPELL_EFFECT_DUMMY
:
3707 if(m_spellInfo
->SpellIconID
== 1648) // Execute
3709 if(!m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
3710 return SPELL_FAILED_BAD_TARGETS
;
3712 else if (m_spellInfo
->Id
== 51582) // Rocket Boots Engaged
3714 if(m_caster
->IsInWater())
3715 return SPELL_FAILED_ONLY_ABOVEWATER
;
3717 else if(m_spellInfo
->SpellIconID
==156) // Holy Shock
3719 // spell different for friends and enemies
3720 // hart version required facing
3721 if(m_targets
.getUnitTarget() && !m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()) && !m_caster
->HasInArc( M_PI
, target
))
3722 return SPELL_FAILED_UNIT_NOT_INFRONT
;
3726 case SPELL_EFFECT_SCHOOL_DAMAGE
:
3729 if(m_spellInfo
->SpellVisual
[0] == 7250)
3731 if (!m_targets
.getUnitTarget())
3732 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3734 if(m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
3735 return SPELL_FAILED_BAD_TARGETS
;
3739 case SPELL_EFFECT_TAMECREATURE
:
3741 if (!m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetTypeId() == TYPEID_PLAYER
)
3742 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3744 if (m_targets
.getUnitTarget()->getLevel() > m_caster
->getLevel())
3745 return SPELL_FAILED_HIGHLEVEL
;
3747 // use SMSG_PET_TAME_FAILURE?
3748 if (!((Creature
*)m_targets
.getUnitTarget())->GetCreatureInfo()->isTameable ())
3749 return SPELL_FAILED_BAD_TARGETS
;
3751 if(m_caster
->GetPetGUID())
3752 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3754 if(m_caster
->GetCharmGUID())
3755 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3759 case SPELL_EFFECT_LEARN_SPELL
:
3761 if(m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_PET
)
3764 Pet
* pet
= m_caster
->GetPet();
3767 return SPELL_FAILED_NO_PET
;
3769 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
3771 if(!learn_spellproto
)
3772 return SPELL_FAILED_NOT_KNOWN
;
3774 if(m_spellInfo
->spellLevel
> pet
->getLevel())
3775 return SPELL_FAILED_LOWLEVEL
;
3779 case SPELL_EFFECT_LEARN_PET_SPELL
:
3781 Pet
* pet
= m_caster
->GetPet();
3784 return SPELL_FAILED_NO_PET
;
3786 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
3788 if(!learn_spellproto
)
3789 return SPELL_FAILED_NOT_KNOWN
;
3791 if(m_spellInfo
->spellLevel
> pet
->getLevel())
3792 return SPELL_FAILED_LOWLEVEL
;
3796 case SPELL_EFFECT_FEED_PET
:
3798 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| !m_targets
.getItemTarget() )
3799 return SPELL_FAILED_BAD_TARGETS
;
3801 Pet
* pet
= m_caster
->GetPet();
3804 return SPELL_FAILED_NO_PET
;
3806 if(!pet
->HaveInDiet(m_targets
.getItemTarget()->GetProto()))
3807 return SPELL_FAILED_WRONG_PET_FOOD
;
3809 if(!pet
->GetCurrentFoodBenefitLevel(m_targets
.getItemTarget()->GetProto()->ItemLevel
))
3810 return SPELL_FAILED_FOOD_LOWLEVEL
;
3812 if(m_caster
->isInCombat() || pet
->isInCombat())
3813 return SPELL_FAILED_AFFECTING_COMBAT
;
3817 case SPELL_EFFECT_POWER_BURN
:
3818 case SPELL_EFFECT_POWER_DRAIN
:
3820 // Can be area effect, Check only for players and not check if target - caster (spell can have multiply drain/burn effects)
3821 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
3822 if(Unit
* target
= m_targets
.getUnitTarget())
3823 if(target
!=m_caster
&& target
->getPowerType()!=m_spellInfo
->EffectMiscValue
[i
])
3824 return SPELL_FAILED_BAD_TARGETS
;
3827 case SPELL_EFFECT_CHARGE
:
3829 if (m_caster
->hasUnitState(UNIT_STAT_ROOT
))
3830 return SPELL_FAILED_ROOTED
;
3834 case SPELL_EFFECT_SKINNING
:
3836 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| !m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetTypeId() != TYPEID_UNIT
)
3837 return SPELL_FAILED_BAD_TARGETS
;
3839 if( !(m_targets
.getUnitTarget()->GetUInt32Value(UNIT_FIELD_FLAGS
) & UNIT_FLAG_SKINNABLE
) )
3840 return SPELL_FAILED_TARGET_UNSKINNABLE
;
3842 Creature
* creature
= (Creature
*)m_targets
.getUnitTarget();
3843 if ( creature
->GetCreatureType() != CREATURE_TYPE_CRITTER
&& ( !creature
->lootForBody
|| !creature
->loot
.empty() ) )
3845 return SPELL_FAILED_TARGET_NOT_LOOTED
;
3848 uint32 skill
= creature
->GetCreatureInfo()->GetRequiredLootSkill();
3850 int32 skillValue
= ((Player
*)m_caster
)->GetSkillValue(skill
);
3851 int32 TargetLevel
= m_targets
.getUnitTarget()->getLevel();
3852 int32 ReqValue
= (skillValue
< 100 ? (TargetLevel
-10)*10 : TargetLevel
*5);
3853 if (ReqValue
> skillValue
)
3854 return SPELL_FAILED_LOW_CASTLEVEL
;
3856 // chance for fail at orange skinning attempt
3857 if( (m_selfContainer
&& (*m_selfContainer
) == this) &&
3858 skillValue
< sWorld
.GetConfigMaxSkillValue() &&
3859 (ReqValue
< 0 ? 0 : ReqValue
) > irand(skillValue
-25, skillValue
+37) )
3860 return SPELL_FAILED_TRY_AGAIN
;
3864 case SPELL_EFFECT_OPEN_LOCK_ITEM
:
3865 case SPELL_EFFECT_OPEN_LOCK
:
3867 if( m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT
&&
3868 m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT_ITEM
)
3871 if( m_caster
->GetTypeId() != TYPEID_PLAYER
// only players can open locks, gather etc.
3872 // we need a go target in case of TARGET_GAMEOBJECT
3873 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT
&& !m_targets
.getGOTarget()
3874 // we need a go target, or an openable item target in case of TARGET_GAMEOBJECT_ITEM
3875 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT_ITEM
&& !m_targets
.getGOTarget() &&
3876 (!m_targets
.getItemTarget() || !m_targets
.getItemTarget()->GetProto()->LockID
|| m_targets
.getItemTarget()->GetOwner() != m_caster
) )
3877 return SPELL_FAILED_BAD_TARGETS
;
3879 // In BattleGround players can use only flags and banners
3880 if( ((Player
*)m_caster
)->InBattleGround() &&
3881 !((Player
*)m_caster
)->isAllowUseBattleGroundObject() )
3882 return SPELL_FAILED_TRY_AGAIN
;
3884 // get the lock entry
3885 LockEntry
const *lockInfo
= NULL
;
3886 if (GameObject
* go
=m_targets
.getGOTarget())
3887 lockInfo
= sLockStore
.LookupEntry(go
->GetLockId());
3888 else if(Item
* itm
=m_targets
.getItemTarget())
3889 lockInfo
= sLockStore
.LookupEntry(itm
->GetProto()->LockID
);
3891 // check lock compatibility
3894 // check for lock - key pair (checked by client also, just prevent cheating
3895 bool ok_key
= false;
3896 for(int it
= 0; it
< 8; ++it
)
3898 switch(lockInfo
->Type
[it
])
3904 if(lockInfo
->Index
[it
])
3906 if(m_CastItem
&& m_CastItem
->GetEntry()==lockInfo
->Index
[it
])
3911 case LOCK_KEY_SKILL
:
3913 if(uint32(m_spellInfo
->EffectMiscValue
[i
])!=lockInfo
->Index
[it
])
3916 switch(lockInfo
->Index
[it
])
3918 case LOCKTYPE_HERBALISM
:
3919 if(((Player
*)m_caster
)->HasSkill(SKILL_HERBALISM
))
3922 case LOCKTYPE_MINING
:
3923 if(((Player
*)m_caster
)->HasSkill(SKILL_MINING
))
3937 return SPELL_FAILED_BAD_TARGETS
;
3940 // chance for fail at orange mining/herb/LockPicking gathering attempt
3941 if (!m_selfContainer
|| ((*m_selfContainer
) != this))
3944 // get the skill value of the player
3945 int32 SkillValue
= 0;
3946 bool canFailAtMax
= true;
3947 if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_HERBALISM
)
3949 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_HERBALISM
);
3950 canFailAtMax
= false;
3952 else if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_MINING
)
3954 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_MINING
);
3955 canFailAtMax
= false;
3957 else if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_PICKLOCK
)
3958 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_LOCKPICKING
);
3960 // castitem check: rogue using skeleton keys. the skill values should not be added in this case.
3964 // add the damage modifier from the spell casted (cheat lock / skeleton key etc.) (use m_currentBasePoints, CalculateDamage returns wrong value)
3965 SkillValue
+= m_currentBasePoints
[i
]+1;
3967 // get the required lock value
3971 // check for lock - key pair
3973 for(int it
= 0; it
< 8; ++it
)
3975 if(lockInfo
->Type
[it
]==LOCK_KEY_ITEM
&& lockInfo
->Index
[it
] && m_CastItem
&& m_CastItem
->GetEntry()==lockInfo
->Index
[it
])
3977 // if so, we're good to go
3985 if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_PICKLOCK
)
3986 ReqValue
= lockInfo
->Skill
[1];
3988 ReqValue
= lockInfo
->Skill
[0];
3991 // skill doesn't meet the required value
3992 if (ReqValue
> SkillValue
)
3993 return SPELL_FAILED_LOW_CASTLEVEL
;
3995 // chance for failure in orange gather / lockpick (gathering skill can't fail at maxskill)
3996 if((canFailAtMax
|| SkillValue
< sWorld
.GetConfigMaxSkillValue()) && ReqValue
> irand(SkillValue
-25, SkillValue
+37))
3997 return SPELL_FAILED_TRY_AGAIN
;
4001 case SPELL_EFFECT_SUMMON_DEAD_PET
:
4003 Creature
*pet
= m_caster
->GetPet();
4005 return SPELL_FAILED_NO_PET
;
4008 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4012 // This is generic summon effect now and don't make this check for summon types similar
4013 // SPELL_EFFECT_SUMMON_CRITTER, SPELL_EFFECT_SUMMON_WILD or SPELL_EFFECT_SUMMON_GUARDIAN.
4014 // These won't show up in m_caster->GetPetGUID()
4015 case SPELL_EFFECT_SUMMON
:
4017 switch(m_spellInfo
->EffectMiscValueB
[i
])
4019 case SUMMON_TYPE_POSESSED
:
4020 case SUMMON_TYPE_POSESSED2
:
4021 case SUMMON_TYPE_DEMON
:
4022 case SUMMON_TYPE_SUMMON
:
4024 if(m_caster
->GetPetGUID())
4025 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4027 if(m_caster
->GetCharmGUID())
4028 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4034 // Don't make this check for SPELL_EFFECT_SUMMON_CRITTER, SPELL_EFFECT_SUMMON_WILD or SPELL_EFFECT_SUMMON_GUARDIAN.
4035 // These won't show up in m_caster->GetPetGUID()
4036 case SPELL_EFFECT_SUMMON_POSSESSED
:
4037 case SPELL_EFFECT_SUMMON_PHANTASM
:
4038 case SPELL_EFFECT_SUMMON_DEMON
:
4040 if(m_caster
->GetPetGUID())
4041 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4043 if(m_caster
->GetCharmGUID())
4044 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4048 case SPELL_EFFECT_SUMMON_PET
:
4050 if(m_caster
->GetPetGUID()) //let warlock do a replacement summon
4053 Pet
* pet
= ((Player
*)m_caster
)->GetPet();
4055 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& m_caster
->getClass()==CLASS_WARLOCK
)
4057 if (strict
) //starting cast, trigger pet stun (cast by pet so it doesn't attack player)
4058 pet
->CastSpell(pet
, 32752, true, NULL
, NULL
, pet
->GetGUID());
4061 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4064 if(m_caster
->GetCharmGUID())
4065 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4069 case SPELL_EFFECT_SUMMON_PLAYER
:
4071 if(m_caster
->GetTypeId()!=TYPEID_PLAYER
)
4072 return SPELL_FAILED_BAD_TARGETS
;
4073 if(!((Player
*)m_caster
)->GetSelection())
4074 return SPELL_FAILED_BAD_TARGETS
;
4076 Player
* target
= objmgr
.GetPlayer(((Player
*)m_caster
)->GetSelection());
4077 if( !target
|| ((Player
*)m_caster
)==target
|| !target
->IsInSameRaidWith((Player
*)m_caster
) )
4078 return SPELL_FAILED_BAD_TARGETS
;
4080 // check if our map is dungeon
4081 if( sMapStore
.LookupEntry(m_caster
->GetMapId())->IsDungeon() )
4083 InstanceTemplate
const* instance
= ObjectMgr::GetInstanceTemplate(m_caster
->GetMapId());
4085 return SPELL_FAILED_TARGET_NOT_IN_INSTANCE
;
4086 if ( instance
->levelMin
> target
->getLevel() )
4087 return SPELL_FAILED_LOWLEVEL
;
4088 if ( instance
->levelMax
&& instance
->levelMax
< target
->getLevel() )
4089 return SPELL_FAILED_HIGHLEVEL
;
4093 case SPELL_EFFECT_LEAP
:
4094 case SPELL_EFFECT_TELEPORT_UNITS_FACE_CASTER
:
4096 float dis
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[i
]));
4097 float fx
= m_caster
->GetPositionX() + dis
* cos(m_caster
->GetOrientation());
4098 float fy
= m_caster
->GetPositionY() + dis
* sin(m_caster
->GetOrientation());
4099 // teleport a bit above terrain level to avoid falling below it
4100 float fz
= MapManager::Instance().GetBaseMap(m_caster
->GetMapId())->GetHeight(fx
,fy
,m_caster
->GetPositionZ(),true);
4101 if(fz
<= INVALID_HEIGHT
) // note: this also will prevent use effect in instances without vmaps height enabled
4102 return SPELL_FAILED_TRY_AGAIN
;
4104 float caster_pos_z
= m_caster
->GetPositionZ();
4105 // Control the caster to not climb or drop when +-fz > 8
4106 if(!(fz
<=caster_pos_z
+8 && fz
>=caster_pos_z
-8))
4107 return SPELL_FAILED_TRY_AGAIN
;
4109 // not allow use this effect at battleground until battleground start
4110 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
4111 if(BattleGround
const *bg
= ((Player
*)m_caster
)->GetBattleGround())
4112 if(bg
->GetStatus() != STATUS_IN_PROGRESS
)
4113 return SPELL_FAILED_TRY_AGAIN
;
4116 case SPELL_EFFECT_STEAL_BENEFICIAL_BUFF
:
4118 if (m_targets
.getUnitTarget()==m_caster
)
4119 return SPELL_FAILED_BAD_TARGETS
;
4126 for (int i
= 0; i
< 3; i
++)
4128 switch(m_spellInfo
->EffectApplyAuraName
[i
])
4130 case SPELL_AURA_MOD_POSSESS
:
4131 case SPELL_AURA_MOD_CHARM
:
4133 if(m_caster
->GetPetGUID())
4134 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4136 if(m_caster
->GetCharmGUID())
4137 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4139 if(m_caster
->GetCharmerGUID())
4140 return SPELL_FAILED_CHARMED
;
4142 if(!m_targets
.getUnitTarget())
4143 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4145 if(m_targets
.getUnitTarget()->GetCharmerGUID())
4146 return SPELL_FAILED_CHARMED
;
4148 if(int32(m_targets
.getUnitTarget()->getLevel()) > CalculateDamage(i
,m_targets
.getUnitTarget()))
4149 return SPELL_FAILED_HIGHLEVEL
;
4153 case SPELL_AURA_MOUNTED
:
4155 if (m_caster
->IsInWater())
4156 return SPELL_FAILED_ONLY_ABOVEWATER
;
4158 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->GetTransport())
4159 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4161 // Ignore map check if spell have AreaId. AreaId already checked and this prevent special mount spells
4162 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& !sMapStore
.LookupEntry(m_caster
->GetMapId())->IsMountAllowed() && !m_IsTriggeredSpell
&& !m_spellInfo
->AreaId
)
4163 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4165 if (m_caster
->GetAreaId()==35)
4166 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4168 ShapeshiftForm form
= m_caster
->m_form
;
4169 if( form
== FORM_CAT
|| form
== FORM_TREE
|| form
== FORM_TRAVEL
||
4170 form
== FORM_AQUA
|| form
== FORM_BEAR
|| form
== FORM_DIREBEAR
||
4171 form
== FORM_CREATUREBEAR
|| form
== FORM_GHOSTWOLF
|| form
== FORM_FLIGHT
||
4172 form
== FORM_FLIGHT_EPIC
|| form
== FORM_MOONKIN
|| form
== FORM_METAMORPHOSIS
)
4173 return SPELL_FAILED_NOT_SHAPESHIFT
;
4177 case SPELL_AURA_RANGED_ATTACK_POWER_ATTACKER_BONUS
:
4179 if(!m_targets
.getUnitTarget())
4180 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4182 // can be casted at non-friendly unit or own pet/charm
4183 if(m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()))
4184 return SPELL_FAILED_TARGET_FRIENDLY
;
4188 case SPELL_AURA_MOD_INCREASE_FLIGHT_SPEED
:
4189 case SPELL_AURA_FLY
:
4191 // not allow cast fly spells at old maps by players (all spells is self target)
4192 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
4194 uint32 v_map
= GetVirtualMapForMapAndZone(m_caster
->GetMapId(), m_caster
->GetZoneId());
4195 if( !((Player
*)m_caster
)->isGameMaster() && v_map
!= 530 && !(v_map
== 571 && ((Player
*)m_caster
)->HasSpell(54197)))
4196 return SPELL_FAILED_NOT_HERE
;
4201 case SPELL_AURA_PERIODIC_MANA_LEECH
:
4203 if (!m_targets
.getUnitTarget())
4204 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4206 if (m_caster
->GetTypeId()!=TYPEID_PLAYER
|| m_CastItem
)
4209 if(m_targets
.getUnitTarget()->getPowerType()!=POWER_MANA
)
4210 return SPELL_FAILED_BAD_TARGETS
;
4223 int16
Spell::PetCanCast(Unit
* target
)
4225 if(!m_caster
->isAlive())
4226 return SPELL_FAILED_CASTER_DEAD
;
4228 if(m_caster
->IsNonMeleeSpellCasted(false)) //prevent spellcast interruption by another spellcast
4229 return SPELL_FAILED_SPELL_IN_PROGRESS
;
4230 if(m_caster
->isInCombat() && IsNonCombatSpell(m_spellInfo
))
4231 return SPELL_FAILED_AFFECTING_COMBAT
;
4233 if(m_caster
->GetTypeId()==TYPEID_UNIT
&& (((Creature
*)m_caster
)->isPet() || m_caster
->isCharmed()))
4235 //dead owner (pets still alive when owners ressed?)
4236 if(m_caster
->GetCharmerOrOwner() && !m_caster
->GetCharmerOrOwner()->isAlive())
4237 return SPELL_FAILED_CASTER_DEAD
;
4239 if(!target
&& m_targets
.getUnitTarget())
4240 target
= m_targets
.getUnitTarget();
4243 for(uint32 i
= 0;i
<3;i
++)
4245 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
)
4249 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4254 m_targets
.setUnitTarget(target
);
4256 Unit
* _target
= m_targets
.getUnitTarget();
4258 if(_target
) //for target dead/target not valid
4260 if(!_target
->isAlive())
4261 return SPELL_FAILED_BAD_TARGETS
;
4263 if(IsPositiveSpell(m_spellInfo
->Id
))
4265 if(m_caster
->IsHostileTo(_target
))
4266 return SPELL_FAILED_BAD_TARGETS
;
4270 bool duelvsplayertar
= false;
4271 for(int j
=0;j
<3;j
++)
4273 //TARGET_DUELVSPLAYER is positive AND negative
4274 duelvsplayertar
|= (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_DUELVSPLAYER
);
4276 if(m_caster
->IsFriendlyTo(target
) && !duelvsplayertar
)
4278 return SPELL_FAILED_BAD_TARGETS
;
4283 if(((Creature
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
4284 return SPELL_FAILED_NOT_READY
;
4287 uint16 result
= CanCast(true);
4291 return -1; //this allows to check spell fail 0, in combat
4294 uint8
Spell::CheckCasterAuras() const
4296 // Flag drop spells totally immuned to caster auras
4297 // FIXME: find more nice check for all totally immuned spells
4298 // AttributesEx3 & 0x10000000?
4299 if(m_spellInfo
->Id
==23336 || m_spellInfo
->Id
==23334 || m_spellInfo
->Id
==34991)
4302 uint8 school_immune
= 0;
4303 uint32 mechanic_immune
= 0;
4304 uint32 dispel_immune
= 0;
4306 //Check if the spell grants school or mechanic immunity.
4307 //We use bitmasks so the loop is done only once and not on every aura check below.
4308 if ( m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DISPEL_AURAS_ON_IMMUNITY
)
4310 for(int i
= 0;i
< 3; i
++)
4312 if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_SCHOOL_IMMUNITY
)
4313 school_immune
|= uint32(m_spellInfo
->EffectMiscValue
[i
]);
4314 else if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_MECHANIC_IMMUNITY
)
4315 mechanic_immune
|= 1 << uint32(m_spellInfo
->EffectMiscValue
[i
]);
4316 else if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_DISPEL_IMMUNITY
)
4317 dispel_immune
|= GetDispellMask(DispelType(m_spellInfo
->EffectMiscValue
[i
]));
4319 //immune movement impairment and loss of control
4320 if(m_spellInfo
->Id
==(uint32
)42292)
4321 mechanic_immune
= IMMUNE_TO_MOVEMENT_IMPAIRMENT_AND_LOSS_CONTROL_MASK
;
4324 //Check whether the cast should be prevented by any state you might have.
4325 uint8 prevented_reason
= 0;
4326 // Have to check if there is a stun aura. Otherwise will have problems with ghost aura apply while logging out
4327 if(!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
) && m_caster
->HasAuraType(SPELL_AURA_MOD_STUN
))
4328 prevented_reason
= SPELL_FAILED_STUNNED
;
4329 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_CONFUSED
) && !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
4330 prevented_reason
= SPELL_FAILED_CONFUSED
;
4331 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_FLEEING
) && !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
4332 prevented_reason
= SPELL_FAILED_FLEEING
;
4333 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_SILENCED
) && m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_SILENCE
)
4334 prevented_reason
= SPELL_FAILED_SILENCED
;
4335 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PACIFIED
) && m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_PACIFY
)
4336 prevented_reason
= SPELL_FAILED_PACIFIED
;
4338 // Attr must make flag drop spell totally immune from all effects
4339 if(prevented_reason
)
4341 if(school_immune
|| mechanic_immune
|| dispel_immune
)
4343 //Checking auras is needed now, because you are prevented by some state but the spell grants immunity.
4344 Unit::AuraMap
const& auras
= m_caster
->GetAuras();
4345 for(Unit::AuraMap::const_iterator itr
= auras
.begin(); itr
!= auras
.end(); itr
++)
4349 if( GetSpellMechanicMask(itr
->second
->GetSpellProto(), itr
->second
->GetEffIndex()) & mechanic_immune
)
4351 if( GetSpellSchoolMask(itr
->second
->GetSpellProto()) & school_immune
)
4353 if( (1<<(itr
->second
->GetSpellProto()->Dispel
)) & dispel_immune
)
4356 //Make a second check for spell failed so the right SPELL_FAILED message is returned.
4357 //That is needed when your casting is prevented by multiple states and you are only immune to some of them.
4358 switch(itr
->second
->GetModifier()->m_auraname
)
4360 case SPELL_AURA_MOD_STUN
:
4361 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
))
4362 return SPELL_FAILED_STUNNED
;
4364 case SPELL_AURA_MOD_CONFUSE
:
4365 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
4366 return SPELL_FAILED_CONFUSED
;
4368 case SPELL_AURA_MOD_FEAR
:
4369 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
4370 return SPELL_FAILED_FLEEING
;
4372 case SPELL_AURA_MOD_SILENCE
:
4373 case SPELL_AURA_MOD_PACIFY
:
4374 case SPELL_AURA_MOD_PACIFY_SILENCE
:
4375 if( m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_PACIFY
)
4376 return SPELL_FAILED_PACIFIED
;
4377 else if ( m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_SILENCE
)
4378 return SPELL_FAILED_SILENCED
;
4384 //You are prevented from casting and the spell casted does not grant immunity. Return a failed error.
4386 return prevented_reason
;
4391 bool Spell::CanAutoCast(Unit
* target
)
4393 uint64 targetguid
= target
->GetGUID();
4395 for(uint32 j
= 0;j
<3;j
++)
4397 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_APPLY_AURA
)
4399 if( m_spellInfo
->StackAmount
<= 1)
4401 if( target
->HasAura(m_spellInfo
->Id
, j
) )
4406 if( target
->GetAuras().count(Unit::spellEffectPair(m_spellInfo
->Id
, j
)) >= m_spellInfo
->StackAmount
)
4410 else if ( IsAreaAuraEffect( m_spellInfo
->Effect
[j
] ))
4412 if( target
->HasAura(m_spellInfo
->Id
, j
) )
4417 int16 result
= PetCanCast(target
);
4419 if(result
== -1 || result
== SPELL_FAILED_UNIT_NOT_INFRONT
)
4422 //check if among target units, our WANTED target is as well (->only self cast spells return false)
4423 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
4424 if( ihit
->targetGUID
== targetguid
)
4427 return false; //target invalid
4430 uint8
Spell::CheckRange(bool strict
)
4434 // self cast doesn't need range checking -- also for Starshards fix
4435 if (m_spellInfo
->rangeIndex
== 1) return 0;
4437 if (strict
) //add radius of caster
4439 else //add radius of caster and ~5 yds "give"
4442 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
4443 float max_range
= GetSpellMaxRange(srange
) + range_mod
;
4444 float min_range
= GetSpellMinRange(srange
);
4446 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
4447 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_RANGE
, max_range
, this);
4449 Unit
*target
= m_targets
.getUnitTarget();
4451 if(target
&& target
!= m_caster
)
4453 // distance from target center in checks
4454 float dist
= m_caster
->GetDistance(target
->GetPositionX(),target
->GetPositionY(),target
->GetPositionZ());
4455 if(dist
> max_range
)
4456 return SPELL_FAILED_OUT_OF_RANGE
; //0x5A;
4457 if(dist
< min_range
)
4458 return SPELL_FAILED_TOO_CLOSE
;
4459 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&&
4460 (m_spellInfo
->FacingCasterFlags
& SPELL_FACING_FLAG_INFRONT
) && !m_caster
->HasInArc( M_PI
, target
) )
4461 return SPELL_FAILED_UNIT_NOT_INFRONT
;
4464 if(m_targets
.m_targetMask
== TARGET_FLAG_DEST_LOCATION
&& m_targets
.m_destX
!= 0 && m_targets
.m_destY
!= 0 && m_targets
.m_destZ
!= 0)
4466 float dist
= m_caster
->GetDistance(m_targets
.m_destX
, m_targets
.m_destY
, m_targets
.m_destZ
);
4467 if(dist
> max_range
)
4468 return SPELL_FAILED_OUT_OF_RANGE
;
4469 if(dist
< min_range
)
4470 return SPELL_FAILED_TOO_CLOSE
;
4476 int32
Spell::CalculatePowerCost()
4478 // item cast not used power
4482 // Spell drain all exist power on cast (Only paladin lay of Hands)
4483 if (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DRAIN_ALL_POWER
)
4485 // If power type - health drain all
4486 if (m_spellInfo
->powerType
== POWER_HEALTH
)
4487 return m_caster
->GetHealth();
4488 // Else drain all power
4489 if (m_spellInfo
->powerType
< MAX_POWERS
)
4490 return m_caster
->GetPower(Powers(m_spellInfo
->powerType
));
4491 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
4496 int32 powerCost
= m_spellInfo
->manaCost
;
4497 // PCT cost from total amount
4498 if (m_spellInfo
->ManaCostPercentage
)
4500 switch (m_spellInfo
->powerType
)
4502 // health as power used
4504 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateHealth() / 100;
4507 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateMana() / 100;
4512 case POWER_HAPPINESS
:
4513 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetMaxPower(Powers(m_spellInfo
->powerType
)) / 100;
4516 case POWER_RUNIC_POWER
:
4517 sLog
.outDebug("Spell::CalculateManaCost: Not implemented yet!");
4520 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
4524 SpellSchools school
= GetFirstSchoolInMask(m_spellSchoolMask
);
4525 // Flat mod from caster auras by spell school
4526 powerCost
+= m_caster
->GetInt32Value(UNIT_FIELD_POWER_COST_MODIFIER
+ school
);
4527 // Shiv - costs 20 + weaponSpeed*10 energy (apply only to non-triggered spell with energy cost)
4528 if ( m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_SPELL_VS_EXTEND_COST
)
4529 powerCost
+= m_caster
->GetAttackTime(OFF_ATTACK
)/100;
4530 // Apply cost mod by spell
4531 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
4532 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COST
, powerCost
, this);
4534 if(m_spellInfo
->Attributes
& SPELL_ATTR_LEVEL_DAMAGE_CALCULATION
)
4535 powerCost
= int32(powerCost
/ (1.117f
* m_spellInfo
->spellLevel
/ m_caster
->getLevel() -0.1327f
));
4537 // PCT mod from user auras by school
4538 powerCost
= int32(powerCost
* (1.0f
+m_caster
->GetFloatValue(UNIT_FIELD_POWER_COST_MULTIPLIER
+school
)));
4544 uint8
Spell::CheckPower()
4546 // item cast not used power
4550 // health as power used - need check health amount
4551 if(m_spellInfo
->powerType
== POWER_HEALTH
)
4553 if(m_caster
->GetHealth() <= m_powerCost
)
4554 return SPELL_FAILED_CASTER_AURASTATE
;
4557 // Check valid power type
4558 if( m_spellInfo
->powerType
>= MAX_POWERS
)
4560 sLog
.outError("Spell::CheckMana: Unknown power type '%d'", m_spellInfo
->powerType
);
4561 return SPELL_FAILED_UNKNOWN
;
4564 uint8 failReason
= CheckRuneCost(m_spellInfo
->runeCostID
);
4568 // Check power amount
4569 Powers powerType
= Powers(m_spellInfo
->powerType
);
4570 if(m_caster
->GetPower(powerType
) < m_powerCost
)
4571 return SPELL_FAILED_NO_POWER
;
4576 uint8
Spell::CheckItems()
4578 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
4581 uint32 itemid
, itemcount
;
4582 Player
* p_caster
= (Player
*)m_caster
;
4586 itemid
= m_CastItem
->GetEntry();
4587 if( !p_caster
->HasItemCount(itemid
,1) )
4588 return SPELL_FAILED_ITEM_NOT_READY
;
4591 ItemPrototype
const *proto
= m_CastItem
->GetProto();
4593 return SPELL_FAILED_ITEM_NOT_READY
;
4595 for (int i
= 0; i
<5; i
++)
4597 if (proto
->Spells
[i
].SpellCharges
)
4599 if(m_CastItem
->GetSpellCharges(i
)==0)
4600 return SPELL_FAILED_NO_CHARGES_REMAIN
;
4604 uint32 ItemClass
= proto
->Class
;
4605 if (ItemClass
== ITEM_CLASS_CONSUMABLE
&& m_targets
.getUnitTarget())
4607 for (int i
= 0; i
< 3; i
++)
4609 // skip check, pet not required like checks, and for TARGET_PET m_targets.getUnitTarget() is not the real target but the caster
4610 if (m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_PET
)
4613 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_HEAL
)
4614 if (m_targets
.getUnitTarget()->GetHealth() == m_targets
.getUnitTarget()->GetMaxHealth())
4615 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_HEALTH
;
4617 // Mana Potion, Rage Potion, Thistle Tea(Rogue), ...
4618 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_ENERGIZE
)
4620 if(m_spellInfo
->EffectMiscValue
[i
] < 0 || m_spellInfo
->EffectMiscValue
[i
] >= MAX_POWERS
)
4621 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_POWER
;
4623 Powers power
= Powers(m_spellInfo
->EffectMiscValue
[i
]);
4625 if (m_targets
.getUnitTarget()->GetPower(power
) == m_targets
.getUnitTarget()->GetMaxPower(power
))
4626 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_POWER
;
4633 if(m_targets
.getItemTargetGUID())
4635 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
4636 return SPELL_FAILED_BAD_TARGETS
;
4638 if(!m_targets
.getItemTarget())
4639 return SPELL_FAILED_ITEM_GONE
;
4641 if(!m_targets
.getItemTarget()->IsFitToSpellRequirements(m_spellInfo
))
4642 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
4644 // if not item target then required item must be equipped
4647 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& !((Player
*)m_caster
)->HasItemFitToSpellReqirements(m_spellInfo
))
4648 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
4651 if(m_spellInfo
->RequiresSpellFocus
)
4653 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
4655 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
4657 GameObject
* ok
= NULL
;
4658 MaNGOS::GameObjectFocusCheck
go_check(m_caster
,m_spellInfo
->RequiresSpellFocus
);
4659 MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
> checker(ok
,go_check
);
4661 TypeContainerVisitor
<MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
>, GridTypeMapContainer
> object_checker(checker
);
4662 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
4663 cell_lock
->Visit(cell_lock
, object_checker
, *m_caster
->GetMap());
4666 return (uint8
)SPELL_FAILED_REQUIRES_SPELL_FOCUS
;
4668 focusObject
= ok
; // game object found in range
4671 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_NO_REAGENT_WHILE_PREP
&&
4672 m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PREPARATION
)))
4674 for(uint32 i
=0;i
<8;i
++)
4676 if(m_spellInfo
->Reagent
[i
] <= 0)
4679 itemid
= m_spellInfo
->Reagent
[i
];
4680 itemcount
= m_spellInfo
->ReagentCount
[i
];
4682 // if CastItem is also spell reagent
4683 if( m_CastItem
&& m_CastItem
->GetEntry() == itemid
)
4685 ItemPrototype
const *proto
= m_CastItem
->GetProto();
4687 return SPELL_FAILED_ITEM_NOT_READY
;
4688 for(int s
=0;s
<5;s
++)
4690 // CastItem will be used up and does not count as reagent
4691 int32 charges
= m_CastItem
->GetSpellCharges(s
);
4692 if (proto
->Spells
[s
].SpellCharges
< 0 && abs(charges
) < 2)
4699 if( !p_caster
->HasItemCount(itemid
,itemcount
) )
4700 return (uint8
)SPELL_FAILED_ITEM_NOT_READY
; //0x54
4705 for(int i
=0;i
<2;++i
)
4707 if(m_spellInfo
->Totem
[i
] != 0)
4709 if( p_caster
->HasItemCount(m_spellInfo
->Totem
[i
],1) )
4718 return (uint8
)SPELL_FAILED_TOTEMS
; //0x7C
4720 //Check items for TotemCategory
4721 uint32 TotemCategory
= 2;
4722 for(int i
=0;i
<2;++i
)
4724 if(m_spellInfo
->TotemCategory
[i
] != 0)
4726 if( p_caster
->HasItemTotemCategory(m_spellInfo
->TotemCategory
[i
]) )
4735 if(TotemCategory
!= 0)
4736 return (uint8
)SPELL_FAILED_TOTEM_CATEGORY
; //0x7B
4738 for(int i
= 0; i
< 3; i
++)
4740 switch (m_spellInfo
->Effect
[i
])
4742 case SPELL_EFFECT_CREATE_ITEM
:
4744 if (!m_IsTriggeredSpell
&& m_spellInfo
->EffectItemType
[i
])
4746 ItemPosCountVec dest
;
4747 uint8 msg
= p_caster
->CanStoreNewItem(NULL_BAG
, NULL_SLOT
, dest
, m_spellInfo
->EffectItemType
[i
], 1 );
4748 if (msg
!= EQUIP_ERR_OK
)
4750 p_caster
->SendEquipError( msg
, NULL
, NULL
);
4751 return SPELL_FAILED_DONT_REPORT
;
4756 case SPELL_EFFECT_ENCHANT_ITEM
:
4758 Item
* targetItem
= m_targets
.getItemTarget();
4760 return SPELL_FAILED_ITEM_NOT_FOUND
;
4762 if( targetItem
->GetProto()->ItemLevel
< m_spellInfo
->baseLevel
)
4763 return SPELL_FAILED_LOWLEVEL
;
4764 // Not allow enchant in trade slot for some enchant type
4765 if( targetItem
->GetOwner() != m_caster
)
4767 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
4768 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
4770 return SPELL_FAILED_ERROR
;
4771 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
4772 return SPELL_FAILED_NOT_TRADEABLE
;
4776 case SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY
:
4778 Item
*item
= m_targets
.getItemTarget();
4780 return SPELL_FAILED_ITEM_NOT_FOUND
;
4781 // Not allow enchant in trade slot for some enchant type
4782 if( item
->GetOwner() != m_caster
)
4784 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
4785 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
4787 return SPELL_FAILED_ERROR
;
4788 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
4789 return SPELL_FAILED_NOT_TRADEABLE
;
4793 case SPELL_EFFECT_ENCHANT_HELD_ITEM
:
4794 // check item existence in effect code (not output errors at offhand hold item effect to main hand for example
4796 case SPELL_EFFECT_DISENCHANT
:
4798 if(!m_targets
.getItemTarget())
4799 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4801 // prevent disenchanting in trade slot
4802 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4803 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4805 ItemPrototype
const* itemProto
= m_targets
.getItemTarget()->GetProto();
4807 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4809 uint32 item_quality
= itemProto
->Quality
;
4810 // 2.0.x addon: Check player enchanting level against the item disenchanting requirements
4811 uint32 item_disenchantskilllevel
= itemProto
->RequiredDisenchantSkill
;
4812 if (item_disenchantskilllevel
== uint32(-1))
4813 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4814 if (item_disenchantskilllevel
> p_caster
->GetSkillValue(SKILL_ENCHANTING
))
4815 return SPELL_FAILED_LOW_CASTLEVEL
;
4816 if(item_quality
> 4 || item_quality
< 2)
4817 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4818 if(itemProto
->Class
!= ITEM_CLASS_WEAPON
&& itemProto
->Class
!= ITEM_CLASS_ARMOR
)
4819 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4820 if (!itemProto
->DisenchantID
)
4821 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4824 case SPELL_EFFECT_PROSPECTING
:
4826 if(!m_targets
.getItemTarget())
4827 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4828 //ensure item is a prospectable ore
4829 if(!(m_targets
.getItemTarget()->GetProto()->BagFamily
& BAG_FAMILY_MASK_MINING_SUPP
) || m_targets
.getItemTarget()->GetProto()->Class
!= ITEM_CLASS_TRADE_GOODS
)
4830 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4831 //prevent prospecting in trade slot
4832 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4833 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4834 //Check for enough skill in jewelcrafting
4835 uint32 item_prospectingskilllevel
= m_targets
.getItemTarget()->GetProto()->RequiredSkillRank
;
4836 if(item_prospectingskilllevel
>p_caster
->GetSkillValue(SKILL_JEWELCRAFTING
))
4837 return SPELL_FAILED_LOW_CASTLEVEL
;
4838 //make sure the player has the required ores in inventory
4839 if(m_targets
.getItemTarget()->GetCount() < 5)
4840 return SPELL_FAILED_NEED_MORE_ITEMS
;
4842 if(!LootTemplates_Prospecting
.HaveLootFor(m_targets
.getItemTargetEntry()))
4843 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4847 case SPELL_EFFECT_MILLING
:
4849 if(!m_targets
.getItemTarget())
4850 return SPELL_FAILED_CANT_BE_MILLED
;
4851 //ensure item is a millable herb
4852 if(!(m_targets
.getItemTarget()->GetProto()->BagFamily
& BAG_FAMILY_MASK_HERBS
) || m_targets
.getItemTarget()->GetProto()->Class
!= ITEM_CLASS_TRADE_GOODS
)
4853 return SPELL_FAILED_CANT_BE_MILLED
;
4854 //prevent milling in trade slot
4855 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4856 return SPELL_FAILED_CANT_BE_MILLED
;
4857 //Check for enough skill in inscription
4858 uint32 item_millingskilllevel
= m_targets
.getItemTarget()->GetProto()->RequiredSkillRank
;
4859 if(item_millingskilllevel
>p_caster
->GetSkillValue(SKILL_INSCRIPTION
))
4860 return SPELL_FAILED_LOW_CASTLEVEL
;
4861 //make sure the player has the required herbs in inventory
4862 if(m_targets
.getItemTarget()->GetCount() < 5)
4863 return SPELL_FAILED_NEED_MORE_ITEMS
;
4865 if(!LootTemplates_Milling
.HaveLootFor(m_targets
.getItemTargetEntry()))
4866 return SPELL_FAILED_CANT_BE_MILLED
;
4870 case SPELL_EFFECT_WEAPON_DAMAGE
:
4871 case SPELL_EFFECT_WEAPON_DAMAGE_NOSCHOOL
:
4873 if(m_caster
->GetTypeId() != TYPEID_PLAYER
) return SPELL_FAILED_TARGET_NOT_PLAYER
;
4874 if( m_attackType
!= RANGED_ATTACK
)
4876 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack(m_attackType
);
4877 if(!pItem
|| pItem
->IsBroken())
4878 return SPELL_FAILED_EQUIPPED_ITEM
;
4880 switch(pItem
->GetProto()->SubClass
)
4882 case ITEM_SUBCLASS_WEAPON_THROWN
:
4884 uint32 ammo
= pItem
->GetEntry();
4885 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
4886 return SPELL_FAILED_NO_AMMO
;
4888 case ITEM_SUBCLASS_WEAPON_GUN
:
4889 case ITEM_SUBCLASS_WEAPON_BOW
:
4890 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
4892 uint32 ammo
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
4896 if(m_caster
->GetDummyAura(46699))
4897 break; // skip other checks
4899 return SPELL_FAILED_NO_AMMO
;
4902 ItemPrototype
const *ammoProto
= objmgr
.GetItemPrototype( ammo
);
4904 return SPELL_FAILED_NO_AMMO
;
4906 if(ammoProto
->Class
!= ITEM_CLASS_PROJECTILE
)
4907 return SPELL_FAILED_NO_AMMO
;
4909 // check ammo ws. weapon compatibility
4910 switch(pItem
->GetProto()->SubClass
)
4912 case ITEM_SUBCLASS_WEAPON_BOW
:
4913 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
4914 if(ammoProto
->SubClass
!=ITEM_SUBCLASS_ARROW
)
4915 return SPELL_FAILED_NO_AMMO
;
4917 case ITEM_SUBCLASS_WEAPON_GUN
:
4918 if(ammoProto
->SubClass
!=ITEM_SUBCLASS_BULLET
)
4919 return SPELL_FAILED_NO_AMMO
;
4922 return SPELL_FAILED_NO_AMMO
;
4925 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
4926 return SPELL_FAILED_NO_AMMO
;
4928 case ITEM_SUBCLASS_WEAPON_WAND
:
4941 void Spell::Delayed()
4943 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
4946 if (m_spellState
== SPELL_STATE_DELAYED
)
4947 return; // spell is active and can't be time-backed
4949 // spells not loosing casting time ( slam, dynamites, bombs.. )
4950 if(!(m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_DAMAGE
))
4953 //check resist chance
4954 int32 resistChance
= 100; //must be initialized to 100 for percent modifiers
4955 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
,SPELLMOD_NOT_LOSE_CASTING_TIME
,resistChance
, this);
4956 resistChance
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_RESIST_PUSHBACK
) - 100;
4957 if (roll_chance_i(resistChance
))
4960 int32 delaytime
= GetNextDelayAtDamageMsTime();
4962 if(int32(m_timer
) + delaytime
> m_casttime
)
4964 delaytime
= m_casttime
- m_timer
;
4965 m_timer
= m_casttime
;
4968 m_timer
+= delaytime
;
4970 sLog
.outDetail("Spell %u partially interrupted for (%d) ms at damage",m_spellInfo
->Id
,delaytime
);
4972 WorldPacket
data(SMSG_SPELL_DELAYED
, 8+4);
4973 data
.append(m_caster
->GetPackGUID());
4974 data
<< uint32(delaytime
);
4976 m_caster
->SendMessageToSet(&data
,true);
4979 void Spell::DelayedChannel()
4981 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
|| getState() != SPELL_STATE_CASTING
)
4984 //check resist chance
4985 int32 resistChance
= 100; //must be initialized to 100 for percent modifiers
4986 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
,SPELLMOD_NOT_LOSE_CASTING_TIME
,resistChance
, this);
4987 resistChance
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_RESIST_PUSHBACK
) - 100;
4988 if (roll_chance_i(resistChance
))
4991 int32 delaytime
= GetNextDelayAtDamageMsTime();
4993 if(int32(m_timer
) < delaytime
)
4995 delaytime
= m_timer
;
4999 m_timer
-= delaytime
;
5001 sLog
.outDebug("Spell %u partially interrupted for %i ms, new duration: %u ms", m_spellInfo
->Id
, delaytime
, m_timer
);
5003 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
5005 if ((*ihit
).missCondition
== SPELL_MISS_NONE
)
5007 Unit
* unit
= m_caster
->GetGUID()==ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
5010 for (int j
=0;j
<3;j
++)
5011 if( ihit
->effectMask
& (1<<j
) )
5012 unit
->DelayAura(m_spellInfo
->Id
, j
, delaytime
);
5018 for(int j
= 0; j
< 3; j
++)
5020 // partially interrupt persistent area auras
5021 DynamicObject
* dynObj
= m_caster
->GetDynObject(m_spellInfo
->Id
, j
);
5023 dynObj
->Delay(delaytime
);
5026 SendChannelUpdate(m_timer
);
5029 void Spell::UpdatePointers()
5031 if(m_originalCasterGUID
==m_caster
->GetGUID())
5032 m_originalCaster
= m_caster
;
5035 m_originalCaster
= ObjectAccessor::GetUnit(*m_caster
,m_originalCasterGUID
);
5036 if(m_originalCaster
&& !m_originalCaster
->IsInWorld()) m_originalCaster
= NULL
;
5039 m_targets
.Update(m_caster
);
5042 bool Spell::IsAffectedBy(SpellEntry
const *spellInfo
, uint32 effectId
)
5044 return spellmgr
.IsAffectedBySpell(m_spellInfo
,spellInfo
->Id
,effectId
,spellInfo
->EffectItemType
[effectId
]);
5047 bool Spell::CheckTargetCreatureType(Unit
* target
) const
5049 uint32 spellCreatureTargetMask
= m_spellInfo
->TargetCreatureType
;
5051 // Curse of Doom : not find another way to fix spell target check :/
5052 if(m_spellInfo
->SpellFamilyName
==SPELLFAMILY_WARLOCK
&& m_spellInfo
->SpellFamilyFlags
== 0x0200000000LL
)
5054 // not allow cast at player
5055 if(target
->GetTypeId()==TYPEID_PLAYER
)
5058 spellCreatureTargetMask
= 0x7FF;
5061 // Dismiss Pet and Taming Lesson skipped
5062 if(m_spellInfo
->Id
== 2641 || m_spellInfo
->Id
== 23356)
5063 spellCreatureTargetMask
= 0;
5065 if (spellCreatureTargetMask
)
5067 uint32 TargetCreatureType
= target
->GetCreatureTypeMask();
5069 return !TargetCreatureType
|| (spellCreatureTargetMask
& TargetCreatureType
);
5074 CurrentSpellTypes
Spell::GetCurrentContainer()
5076 if (IsNextMeleeSwingSpell())
5077 return(CURRENT_MELEE_SPELL
);
5078 else if (IsAutoRepeat())
5079 return(CURRENT_AUTOREPEAT_SPELL
);
5080 else if (IsChanneledSpell(m_spellInfo
))
5081 return(CURRENT_CHANNELED_SPELL
);
5083 return(CURRENT_GENERIC_SPELL
);
5086 bool Spell::CheckTarget( Unit
* target
, uint32 eff
)
5088 // Check targets for creature type mask and remove not appropriate (skip explicit self target case, maybe need other explicit targets)
5089 if(m_spellInfo
->EffectImplicitTargetA
[eff
]!=TARGET_SELF
)
5091 if (!CheckTargetCreatureType(target
))
5095 // Check targets for not_selectable unit flag and remove
5096 // A player can cast spells on his pet (or other controlled unit) though in any state
5097 if (target
!= m_caster
&& target
->GetCharmerOrOwnerGUID() != m_caster
->GetGUID())
5099 // any unattackable target skipped
5100 if (target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NON_ATTACKABLE
))
5103 // unselectable targets skipped in all cases except TARGET_SCRIPT targeting
5104 // in case TARGET_SCRIPT target selected by server always and can't be cheated
5105 if( target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NOT_SELECTABLE
) &&
5106 m_spellInfo
->EffectImplicitTargetA
[eff
] != TARGET_SCRIPT
&&
5107 m_spellInfo
->EffectImplicitTargetB
[eff
] != TARGET_SCRIPT
)
5111 //Check player targets and remove if in GM mode or GM invisibility (for not self casting case)
5112 if( target
!= m_caster
&& target
->GetTypeId()==TYPEID_PLAYER
)
5114 if(((Player
*)target
)->GetVisibility()==VISIBILITY_OFF
)
5117 if(((Player
*)target
)->isGameMaster() && !IsPositiveSpell(m_spellInfo
->Id
))
5121 //Check targets for LOS visibility (except spells without range limitations )
5122 switch(m_spellInfo
->Effect
[eff
])
5124 case SPELL_EFFECT_SUMMON_PLAYER
: // from anywhere
5126 case SPELL_EFFECT_DUMMY
:
5127 if(m_spellInfo
->Id
!=20577) // Cannibalize
5130 case SPELL_EFFECT_RESURRECT_NEW
:
5131 // player far away, maybe his corpse near?
5132 if(target
!=m_caster
&& !target
->IsWithinLOSInMap(m_caster
))
5134 if(!m_targets
.getCorpseTargetGUID())
5137 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
5141 if(target
->GetGUID()!=corpse
->GetOwnerGUID())
5144 if(!corpse
->IsWithinLOSInMap(m_caster
))
5148 // all ok by some way or another, skip normal check
5150 default: // normal case
5151 if(target
!=m_caster
&& !target
->IsWithinLOSInMap(m_caster
))
5159 Unit
* Spell::SelectMagnetTarget()
5161 Unit
* target
= m_targets
.getUnitTarget();
5163 if(target
&& target
->HasAuraType(SPELL_AURA_SPELL_MAGNET
) && !(m_spellInfo
->Attributes
& 0x10))
5165 Unit::AuraList
const& magnetAuras
= target
->GetAurasByType(SPELL_AURA_SPELL_MAGNET
);
5166 for(Unit::AuraList::const_iterator itr
= magnetAuras
.begin(); itr
!= magnetAuras
.end(); ++itr
)
5168 if(Unit
* magnet
= (*itr
)->GetCaster())
5170 if(magnet
->IsWithinLOSInMap(m_caster
))
5173 m_targets
.setUnitTarget(target
);
5183 bool Spell::IsNeedSendToClient() const
5185 return m_spellInfo
->SpellVisual
!=0 || IsChanneledSpell(m_spellInfo
) ||
5186 m_spellInfo
->speed
> 0.0f
|| !m_triggeredByAuraSpell
&& !m_IsTriggeredSpell
;
5189 bool Spell::HaveTargetsForEffect( uint8 effect
) const
5191 for(std::list
<TargetInfo
>::const_iterator itr
= m_UniqueTargetInfo
.begin();itr
!= m_UniqueTargetInfo
.end();++itr
)
5192 if(itr
->effectMask
& (1<<effect
))
5195 for(std::list
<GOTargetInfo
>::const_iterator itr
= m_UniqueGOTargetInfo
.begin();itr
!= m_UniqueGOTargetInfo
.end();++itr
)
5196 if(itr
->effectMask
& (1<<effect
))
5199 for(std::list
<ItemTargetInfo
>::const_iterator itr
= m_UniqueItemInfo
.begin();itr
!= m_UniqueItemInfo
.end();++itr
)
5200 if(itr
->effectMask
& (1<<effect
))
5206 SpellEvent::SpellEvent(Spell
* spell
) : BasicEvent()
5211 SpellEvent::~SpellEvent()
5213 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5216 if (m_Spell
->IsDeletable())
5222 sLog
.outError("~SpellEvent: %s %u tried to delete non-deletable spell %u. Was not deleted, causes memory leak.",
5223 (m_Spell
->GetCaster()->GetTypeId()==TYPEID_PLAYER
?"Player":"Creature"), m_Spell
->GetCaster()->GetGUIDLow(),m_Spell
->m_spellInfo
->Id
);
5227 bool SpellEvent::Execute(uint64 e_time
, uint32 p_time
)
5229 // update spell if it is not finished
5230 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5231 m_Spell
->update(p_time
);
5233 // check spell state to process
5234 switch (m_Spell
->getState())
5236 case SPELL_STATE_FINISHED
:
5238 // spell was finished, check deletable state
5239 if (m_Spell
->IsDeletable())
5241 // check, if we do have unfinished triggered spells
5243 return(true); // spell is deletable, finish event
5245 // event will be re-added automatically at the end of routine)
5248 case SPELL_STATE_CASTING
:
5250 // this spell is in channeled state, process it on the next update
5251 // event will be re-added automatically at the end of routine)
5254 case SPELL_STATE_DELAYED
:
5256 // first, check, if we have just started
5257 if (m_Spell
->GetDelayStart() != 0)
5259 // no, we aren't, do the typical update
5260 // check, if we have channeled spell on our hands
5261 if (IsChanneledSpell(m_Spell
->m_spellInfo
))
5263 // evented channeled spell is processed separately, casted once after delay, and not destroyed till finish
5264 // check, if we have casting anything else except this channeled spell and autorepeat
5265 if (m_Spell
->GetCaster()->IsNonMeleeSpellCasted(false, true, true))
5267 // another non-melee non-delayed spell is casted now, abort
5272 // do the action (pass spell to channeling state)
5273 m_Spell
->handle_immediate();
5275 // event will be re-added automatically at the end of routine)
5279 // run the spell handler and think about what we can do next
5280 uint64 t_offset
= e_time
- m_Spell
->GetDelayStart();
5281 uint64 n_offset
= m_Spell
->handle_delayed(t_offset
);
5284 // re-add us to the queue
5285 m_Spell
->GetCaster()->m_Events
.AddEvent(this, m_Spell
->GetDelayStart() + n_offset
, false);
5286 return(false); // event not complete
5289 // finish update event will be re-added automatically at the end of routine)
5294 // delaying had just started, record the moment
5295 m_Spell
->SetDelayStart(e_time
);
5296 // re-plan the event for the delay moment
5297 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ m_Spell
->GetDelayMoment(), false);
5298 return(false); // event not complete
5305 // event will be re-added automatically at the end of routine)
5309 // spell processing not complete, plan event on the next update interval
5310 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ 1, false);
5311 return(false); // event not complete
5314 void SpellEvent::Abort(uint64
/*e_time*/)
5316 // oops, the spell we try to do is aborted
5317 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5321 bool SpellEvent::IsDeletable() const
5323 return m_Spell
->IsDeletable();