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_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;
273 m_delayAtDamageCount
= 0;
275 m_applyMultiplierMask
= 0;
277 // Get data for type of attack
278 switch (m_spellInfo
->DmgClass
)
280 case SPELL_DAMAGE_CLASS_MELEE
:
281 if (m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_REQ_OFFHAND
)
282 m_attackType
= OFF_ATTACK
;
284 m_attackType
= BASE_ATTACK
;
286 case SPELL_DAMAGE_CLASS_RANGED
:
287 m_attackType
= RANGED_ATTACK
;
291 if (m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_REQ_WAND
)
292 m_attackType
= RANGED_ATTACK
;
294 m_attackType
= BASE_ATTACK
;
298 m_spellSchoolMask
= GetSpellSchoolMask(info
); // Can be override for some spell (wand shoot for example)
300 if(m_attackType
== RANGED_ATTACK
)
303 if((m_caster
->getClassMask() & CLASSMASK_WAND_USERS
) != 0 && m_caster
->GetTypeId()==TYPEID_PLAYER
)
305 if(Item
* pItem
= ((Player
*)m_caster
)->GetWeaponForAttack(RANGED_ATTACK
))
306 m_spellSchoolMask
= SpellSchoolMask(1 << pItem
->GetProto()->Damage
->DamageType
);
310 if(originalCasterGUID
)
311 m_originalCasterGUID
= originalCasterGUID
;
313 m_originalCasterGUID
= m_caster
->GetGUID();
315 if(m_originalCasterGUID
==m_caster
->GetGUID())
316 m_originalCaster
= m_caster
;
319 m_originalCaster
= ObjectAccessor::GetUnit(*m_caster
,m_originalCasterGUID
);
320 if(m_originalCaster
&& !m_originalCaster
->IsInWorld()) m_originalCaster
= NULL
;
323 for(int i
=0; i
<3; ++i
)
324 m_currentBasePoints
[i
] = m_spellInfo
->EffectBasePoints
[i
];
326 m_spellState
= SPELL_STATE_NULL
;
328 m_castPositionX
= m_castPositionY
= m_castPositionZ
= 0;
329 m_TriggerSpells
.clear();
330 m_IsTriggeredSpell
= triggered
;
331 //m_AreaAura = false;
336 gameObjTarget
= NULL
;
339 m_triggeredByAuraSpell
= NULL
;
342 if( m_spellInfo
->AttributesEx2
== 0x000020 && !triggered
)
345 m_autoRepeat
= false;
347 m_powerCost
= 0; // setup to correct value in Spell::prepare, don't must be used before.
348 m_casttime
= 0; // setup to correct value in Spell::prepare, don't must be used before.
349 m_timer
= 0; // will set to castime in preper
351 m_needAliveTargetMask
= 0;
353 // determine reflection
354 m_canReflect
= false;
356 if(m_spellInfo
->DmgClass
== SPELL_DAMAGE_CLASS_MAGIC
&& (m_spellInfo
->AttributesEx2
& 0x4)==0)
360 if (m_spellInfo
->Effect
[j
]==0)
363 if(!IsPositiveTarget(m_spellInfo
->EffectImplicitTargetA
[j
],m_spellInfo
->EffectImplicitTargetB
[j
]))
366 m_canReflect
= (m_spellInfo
->AttributesEx
& (1<<7)) ? true : false;
382 void Spell::FillTargetMap()
384 // TODO: ADD the correct target FILLS!!!!!!
386 for(uint32 i
=0;i
<3;i
++)
388 // not call for empty effect.
389 // Also some spells use not used effect targets for store targets for dummy effect in triggered spells
390 if(m_spellInfo
->Effect
[i
]==0)
393 // targets for TARGET_SCRIPT_COORDINATES (A) and TARGET_SCRIPT filled in Spell::canCast call
394 if( m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SCRIPT_COORDINATES
||
395 m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SCRIPT
||
396 m_spellInfo
->EffectImplicitTargetB
[i
] == TARGET_SCRIPT
&& m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_SELF
)
399 // TODO: find a way so this is not needed?
400 // for area auras always add caster as target (needed for totems for example)
401 if(IsAreaAuraEffect(m_spellInfo
->Effect
[i
]))
402 AddUnitTarget(m_caster
, i
);
404 std::list
<Unit
*> tmpUnitMap
;
406 // TargetA/TargetB dependent from each other, we not switch to full support this dependences
407 // but need it support in some know cases
408 switch(m_spellInfo
->EffectImplicitTargetA
[i
])
410 case TARGET_ALL_AROUND_CASTER
:
411 if( m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_PARTY
||
412 m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_FRIENDLY_UNITS_AROUND_CASTER
||
413 m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_RANDOM_RAID_MEMBER
)
415 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
417 // Note: this hack with search required until GO casting not implemented
418 // environment damage spells already have around enemies targeting but this not help in case not existed GO casting support
419 // currently each enemy selected explicitly and self cast damage
420 else if(m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_ENEMY_IN_AREA
&& m_spellInfo
->Effect
[i
]==SPELL_EFFECT_ENVIRONMENTAL_DAMAGE
)
422 if(m_targets
.getUnitTarget())
423 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
427 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
428 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
431 case TARGET_TABLE_X_Y_Z_COORDINATES
:
432 // Only if target A, for target B (used in teleports) dest select in effect
433 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
436 switch(m_spellInfo
->EffectImplicitTargetB
[i
])
438 case TARGET_SCRIPT_COORDINATES
: // B case filled in canCast but we need fill unit list base at A case
439 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
442 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
443 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
449 if( (m_spellInfo
->EffectImplicitTargetA
[i
]==0 || m_spellInfo
->EffectImplicitTargetA
[i
]==TARGET_EFFECT_SELECT
) &&
450 (m_spellInfo
->EffectImplicitTargetB
[i
]==0 || m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_EFFECT_SELECT
) )
452 // add here custom effects that need default target.
453 // FOR EVERY TARGET TYPE THERE IS A DIFFERENT FILL!!
454 switch(m_spellInfo
->Effect
[i
])
456 case SPELL_EFFECT_DUMMY
:
458 switch(m_spellInfo
->Id
)
460 case 20577: // Cannibalize
462 // non-standard target selection
463 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
464 float max_range
= GetSpellMaxRange(srange
);
466 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
468 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
471 WorldObject
* result
= NULL
;
473 MaNGOS::CannibalizeObjectCheck
u_check(m_caster
, max_range
);
474 MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
> searcher(result
, u_check
);
476 TypeContainerVisitor
<MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
>, GridTypeMapContainer
> grid_searcher(searcher
);
477 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
478 cell_lock
->Visit(cell_lock
, grid_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
482 TypeContainerVisitor
<MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
>, WorldTypeMapContainer
> world_searcher(searcher
);
483 cell_lock
->Visit(cell_lock
, world_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
488 switch(result
->GetTypeId())
492 tmpUnitMap
.push_back((Unit
*)result
);
495 m_targets
.setCorpseTarget((Corpse
*)result
);
496 if(Player
* owner
= ObjectAccessor::FindPlayer(((Corpse
*)result
)->GetOwnerGUID()))
497 tmpUnitMap
.push_back(owner
);
503 // clear cooldown at fail
504 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
506 ((Player
*)m_caster
)->RemoveSpellCooldown(m_spellInfo
->Id
);
508 WorldPacket
data(SMSG_CLEAR_COOLDOWN
, (4+8));
509 data
<< uint32(m_spellInfo
->Id
);
510 data
<< uint64(m_caster
->GetGUID());
511 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
514 SendCastResult(SPELL_FAILED_NO_EDIBLE_CORPSES
);
520 if(m_targets
.getUnitTarget())
521 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
526 case SPELL_EFFECT_RESURRECT
:
527 case SPELL_EFFECT_PARRY
:
528 case SPELL_EFFECT_BLOCK
:
529 case SPELL_EFFECT_CREATE_ITEM
:
530 case SPELL_EFFECT_TRIGGER_SPELL
:
531 case SPELL_EFFECT_TRIGGER_MISSILE
:
532 case SPELL_EFFECT_LEARN_SPELL
:
533 case SPELL_EFFECT_SKILL_STEP
:
534 case SPELL_EFFECT_PROFICIENCY
:
535 case SPELL_EFFECT_SUMMON_POSSESSED
:
536 case SPELL_EFFECT_SUMMON_OBJECT_WILD
:
537 case SPELL_EFFECT_SELF_RESURRECT
:
538 case SPELL_EFFECT_REPUTATION
:
539 if(m_targets
.getUnitTarget())
540 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
542 case SPELL_EFFECT_SUMMON_PLAYER
:
543 if(m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->GetSelection())
545 Player
* target
= objmgr
.GetPlayer(((Player
*)m_caster
)->GetSelection());
547 tmpUnitMap
.push_back(target
);
550 case SPELL_EFFECT_RESURRECT_NEW
:
551 if(m_targets
.getUnitTarget())
552 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
553 if(m_targets
.getCorpseTargetGUID())
555 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
558 Player
* owner
= ObjectAccessor::FindPlayer(corpse
->GetOwnerGUID());
560 tmpUnitMap
.push_back(owner
);
564 case SPELL_EFFECT_SUMMON
:
565 if(m_spellInfo
->EffectMiscValueB
[i
] == SUMMON_TYPE_POSESSED
|| m_spellInfo
->EffectMiscValueB
[i
] == SUMMON_TYPE_POSESSED2
)
567 if(m_targets
.getUnitTarget())
568 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
571 tmpUnitMap
.push_back(m_caster
);
573 case SPELL_EFFECT_SUMMON_CHANGE_ITEM
:
574 case SPELL_EFFECT_SUMMON_WILD
:
575 case SPELL_EFFECT_SUMMON_GUARDIAN
:
576 case SPELL_EFFECT_TRANS_DOOR
:
577 case SPELL_EFFECT_ADD_FARSIGHT
:
578 case SPELL_EFFECT_APPLY_GLYPH
:
579 case SPELL_EFFECT_STUCK
:
580 case SPELL_EFFECT_DESTROY_ALL_TOTEMS
:
581 case SPELL_EFFECT_SUMMON_DEMON
:
582 case SPELL_EFFECT_SKILL
:
583 tmpUnitMap
.push_back(m_caster
);
585 case SPELL_EFFECT_LEARN_PET_SPELL
:
586 if(Pet
* pet
= m_caster
->GetPet())
587 tmpUnitMap
.push_back(pet
);
589 case SPELL_EFFECT_ENCHANT_ITEM
:
590 case SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY
:
591 case SPELL_EFFECT_DISENCHANT
:
592 case SPELL_EFFECT_FEED_PET
:
593 case SPELL_EFFECT_PROSPECTING
:
594 if(m_targets
.getItemTarget())
595 AddItemTarget(m_targets
.getItemTarget(), i
);
597 case SPELL_EFFECT_APPLY_AURA
:
598 switch(m_spellInfo
->EffectApplyAuraName
[i
])
600 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)
601 case SPELL_AURA_ADD_PCT_MODIFIER
:
602 tmpUnitMap
.push_back(m_caster
);
604 default: // apply to target in other case
605 if(m_targets
.getUnitTarget())
606 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
610 case SPELL_EFFECT_APPLY_AREA_AURA_PARTY
:
612 if(m_spellInfo
->Attributes
== 0x9050000 || m_spellInfo
->Attributes
== 0x10000)
613 SetTargetMap(i
,TARGET_AREAEFFECT_PARTY
,tmpUnitMap
);
615 case SPELL_EFFECT_SKIN_PLAYER_CORPSE
:
616 if(m_targets
.getUnitTarget())
618 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
620 else if (m_targets
.getCorpseTargetGUID())
622 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
625 Player
* owner
= ObjectAccessor::FindPlayer(corpse
->GetOwnerGUID());
627 tmpUnitMap
.push_back(owner
);
635 if(IsChanneledSpell(m_spellInfo
) && !tmpUnitMap
.empty())
636 m_needAliveTargetMask
|= (1<<i
);
638 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
640 Player
*me
= (Player
*)m_caster
;
641 for (std::list
<Unit
*>::const_iterator itr
= tmpUnitMap
.begin(); itr
!= tmpUnitMap
.end(); itr
++)
643 Unit
*owner
= (*itr
)->GetOwner();
644 Unit
*u
= owner
? owner
: (*itr
);
645 if(u
!=m_caster
&& u
->IsPvP() && (!me
->duel
|| me
->duel
->opponent
!= u
))
648 me
->RemoveAurasWithInterruptFlags(AURA_INTERRUPT_FLAG_ENTER_PVP_COMBAT
);
654 for (std::list
<Unit
*>::iterator itr
= tmpUnitMap
.begin() ; itr
!= tmpUnitMap
.end();)
656 if(!CheckTarget(*itr
, i
, false ))
658 itr
= tmpUnitMap
.erase(itr
);
665 for(std::list
<Unit
*>::iterator iunit
= tmpUnitMap
.begin();iunit
!= tmpUnitMap
.end();++iunit
)
666 AddUnitTarget((*iunit
), i
);
670 void Spell::CleanupTargetList()
672 m_UniqueTargetInfo
.clear();
673 m_UniqueGOTargetInfo
.clear();
674 m_UniqueItemInfo
.clear();
680 void Spell::AddUnitTarget(Unit
* pVictim
, uint32 effIndex
)
682 if( m_spellInfo
->Effect
[effIndex
]==0 )
685 uint64 targetGUID
= pVictim
->GetGUID();
687 // Lookup target in already in list
688 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
690 if (targetGUID
== ihit
->targetGUID
) // Found in list
692 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
697 // This is new target calculate data for him
699 // Get spell hit result on target
701 target
.targetGUID
= targetGUID
; // Store target GUID
702 target
.effectMask
= 1<<effIndex
; // Store index of effect
703 target
.processed
= false; // Effects not apply on target
705 // Calculate hit result
706 target
.missCondition
= m_caster
->SpellHitResult(pVictim
, m_spellInfo
, m_canReflect
);
707 if (target
.missCondition
== SPELL_MISS_NONE
)
712 // Spell have speed - need calculate incoming time
713 if (m_spellInfo
->speed
> 0.0f
)
715 // calculate spell incoming interval
716 float dist
= m_caster
->GetDistance(pVictim
->GetPositionX(), pVictim
->GetPositionY(), pVictim
->GetPositionZ());
717 if (dist
< 5.0f
) dist
= 5.0f
;
718 target
.timeDelay
= (uint64
) floor(dist
/ m_spellInfo
->speed
* 1000.0f
);
720 // Calculate minimum incoming time
721 if (m_delayMoment
==0 || m_delayMoment
>target
.timeDelay
)
722 m_delayMoment
= target
.timeDelay
;
725 target
.timeDelay
= 0LL;
727 // If target reflect spell back to caster
728 if (target
.missCondition
==SPELL_MISS_REFLECT
)
730 // Calculate reflected spell result on caster
731 target
.reflectResult
= m_caster
->SpellHitResult(m_caster
, m_spellInfo
, m_canReflect
);
733 if (target
.reflectResult
== SPELL_MISS_REFLECT
) // Impossible reflect again, so simply deflect spell
734 target
.reflectResult
= SPELL_MISS_PARRY
;
736 // Increase time interval for reflected spells by 1.5
737 target
.timeDelay
+=target
.timeDelay
>>1;
740 target
.reflectResult
= SPELL_MISS_NONE
;
742 // Add target to list
743 m_UniqueTargetInfo
.push_back(target
);
746 void Spell::AddUnitTarget(uint64 unitGUID
, uint32 effIndex
)
748 Unit
* unit
= m_caster
->GetGUID()==unitGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, unitGUID
);
750 AddUnitTarget(unit
, effIndex
);
753 void Spell::AddGOTarget(GameObject
* pVictim
, uint32 effIndex
)
755 if( m_spellInfo
->Effect
[effIndex
]==0 )
758 uint64 targetGUID
= pVictim
->GetGUID();
760 // Lookup target in already in list
761 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
763 if (targetGUID
== ihit
->targetGUID
) // Found in list
765 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
770 // This is new target calculate data for him
773 target
.targetGUID
= targetGUID
;
774 target
.effectMask
= 1<<effIndex
;
775 target
.processed
= false; // Effects not apply on target
777 // Spell have speed - need calculate incoming time
778 if (m_spellInfo
->speed
> 0.0f
)
780 // calculate spell incoming interval
781 float dist
= m_caster
->GetDistance(pVictim
->GetPositionX(), pVictim
->GetPositionY(), pVictim
->GetPositionZ());
782 if (dist
< 5.0f
) dist
= 5.0f
;
783 target
.timeDelay
= (uint64
) floor(dist
/ m_spellInfo
->speed
* 1000.0f
);
784 if (m_delayMoment
==0 || m_delayMoment
>target
.timeDelay
)
785 m_delayMoment
= target
.timeDelay
;
788 target
.timeDelay
= 0LL;
792 // Add target to list
793 m_UniqueGOTargetInfo
.push_back(target
);
796 void Spell::AddGOTarget(uint64 goGUID
, uint32 effIndex
)
798 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, goGUID
);
800 AddGOTarget(go
, effIndex
);
803 void Spell::AddItemTarget(Item
* pitem
, uint32 effIndex
)
805 if( m_spellInfo
->Effect
[effIndex
]==0 )
808 // Lookup target in already in list
809 for(std::list
<ItemTargetInfo
>::iterator ihit
= m_UniqueItemInfo
.begin();ihit
!= m_UniqueItemInfo
.end();++ihit
)
811 if (pitem
== ihit
->item
) // Found in list
813 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
818 // This is new target add data
820 ItemTargetInfo target
;
822 target
.effectMask
= 1<<effIndex
;
823 m_UniqueItemInfo
.push_back(target
);
826 void Spell::doTriggers(SpellMissInfo missInfo
, uint32 damage
, SpellSchoolMask damageSchoolMask
, uint32 block
, uint32 absorb
, bool crit
)
828 // Do triggers depends from hit result (triggers on hit do in effects)
829 // Set aura states depends from hit result
830 if (missInfo
!=SPELL_MISS_NONE
)
832 // Miss/dodge/parry/block only for melee based spells
833 // Resist only for magic based spells
836 case SPELL_MISS_MISS
:
837 if(m_caster
->GetTypeId()== TYPEID_PLAYER
)
838 ((Player
*)m_caster
)->UpdateWeaponSkill(BASE_ATTACK
);
840 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_MISS
, m_spellInfo
, m_IsTriggeredSpell
);
842 case SPELL_MISS_RESIST
:
843 m_caster
->ProcDamageAndSpell(unitTarget
, PROC_FLAG_TARGET_RESISTS
, PROC_FLAG_RESIST_SPELL
, 0, damageSchoolMask
, m_spellInfo
, m_IsTriggeredSpell
);
845 case SPELL_MISS_DODGE
:
846 if(unitTarget
->GetTypeId() == TYPEID_PLAYER
)
847 ((Player
*)unitTarget
)->UpdateDefense();
850 if (m_caster
->GetTypeId() == TYPEID_PLAYER
&& m_caster
->getClass() == CLASS_WARRIOR
)
852 ((Player
*) m_caster
)->AddComboPoints(unitTarget
, 1);
853 m_caster
->StartReactiveTimer( REACTIVE_OVERPOWER
);
857 if (unitTarget
->getClass() != CLASS_ROGUE
)
859 unitTarget
->ModifyAuraState(AURA_STATE_DEFENSE
, true);
860 unitTarget
->StartReactiveTimer( REACTIVE_DEFENSE
);
863 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_DODGE
, m_spellInfo
, m_IsTriggeredSpell
);
865 case SPELL_MISS_PARRY
:
866 // Update victim defense ?
867 if(unitTarget
->GetTypeId() == TYPEID_PLAYER
)
868 ((Player
*)unitTarget
)->UpdateDefense();
869 // Mongoose bite - set only Counterattack here
870 if (unitTarget
->getClass() == CLASS_HUNTER
)
872 unitTarget
->ModifyAuraState(AURA_STATE_HUNTER_PARRY
,true);
873 unitTarget
->StartReactiveTimer( REACTIVE_HUNTER_PARRY
);
877 unitTarget
->ModifyAuraState(AURA_STATE_DEFENSE
, true);
878 unitTarget
->StartReactiveTimer( REACTIVE_DEFENSE
);
880 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_PARRY
, m_spellInfo
, m_IsTriggeredSpell
);
882 case SPELL_MISS_BLOCK
:
883 unitTarget
->ModifyAuraState(AURA_STATE_DEFENSE
, true);
884 unitTarget
->StartReactiveTimer( REACTIVE_DEFENSE
);
886 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_BLOCK
, m_spellInfo
, m_IsTriggeredSpell
);
888 // Trigger from this events not supported
889 case SPELL_MISS_EVADE
:
890 case SPELL_MISS_IMMUNE
:
891 case SPELL_MISS_IMMUNE2
:
892 case SPELL_MISS_DEFLECT
:
893 case SPELL_MISS_ABSORB
:
894 // Trigger from reflects need do after get reflect result
895 case SPELL_MISS_REFLECT
:
903 void Spell::DoAllEffectOnTarget(TargetInfo
*target
)
905 if (target
->processed
) // Check target
907 target
->processed
= true; // Target checked in apply effects procedure
909 // Get mask of effects for target
910 uint32 mask
= target
->effectMask
;
911 if (mask
== 0) // No effects
914 Unit
* unit
= m_caster
->GetGUID()==target
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
,target
->targetGUID
);
918 SpellMissInfo missInfo
= target
->missCondition
;
919 // Need init unitTarget by default unit (can changed in code on reflect)
920 // Or on missInfo!=SPELL_MISS_NONE unitTarget undefined (but need in trigger subsystem)
923 if (missInfo
==SPELL_MISS_NONE
) // In case spell hit target, do all effect on that target
924 DoSpellHitOnUnit(unit
, mask
);
925 else if (missInfo
== SPELL_MISS_REFLECT
) // In case spell reflect from target, do all effect on caster (if hit)
927 if (target
->reflectResult
== SPELL_MISS_NONE
) // If reflected spell hit caster -> do all effect on him
928 DoSpellHitOnUnit(m_caster
, mask
);
931 // Do triggers only on miss/resist/parry/dodge
932 if (missInfo
!=SPELL_MISS_NONE
)
933 doTriggers(missInfo
);
935 // Call scripted function for AI if this spell is casted upon a creature (except pets)
936 if(IS_CREATURE_GUID(target
->targetGUID
))
938 // cast at creature (or GO) quest objectives update at successful cast finished (+channel finished)
939 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
940 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() && !IsChannelActive() )
941 ((Player
*)m_caster
)->CastedCreatureOrGO(unit
->GetEntry(),unit
->GetGUID(),m_spellInfo
->Id
);
943 if(((Creature
*)unit
)->AI())
944 ((Creature
*)unit
)->AI()->SpellHit(m_caster
,m_spellInfo
);
948 void Spell::DoSpellHitOnUnit(Unit
*unit
, const uint32 effectMask
)
950 if(!unit
|| !effectMask
)
953 // Recheck immune (only for delayed spells)
954 if( m_spellInfo
->speed
&& (
955 unit
->IsImmunedToDamage(GetSpellSchoolMask(m_spellInfo
),true) ||
956 unit
->IsImmunedToSpell(m_spellInfo
,true) ))
958 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_IMMUNE
);
962 if( m_caster
!= unit
)
964 if( !m_caster
->IsFriendlyTo(unit
) )
966 // for delayed spells ignore not visible explicit target
967 if(m_spellInfo
->speed
> 0.0f
&& unit
==m_targets
.getUnitTarget() && !unit
->isVisibleForOrDetect(m_caster
,false))
969 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_EVADE
);
973 // exclude Arcane Missiles Dummy Aura aura for now (attack on hit)
974 // TODO: find way to not need this?
975 if(!(m_spellInfo
->SpellFamilyName
== SPELLFAMILY_MAGE
&&
976 m_spellInfo
->SpellFamilyFlags
& 0x800LL
))
978 unit
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
980 if( !(m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_NO_INITIAL_AGGRO
) )
982 if(!unit
->IsStandState() && !unit
->hasUnitState(UNIT_STAT_STUNNED
))
983 unit
->SetStandState(PLAYER_STATE_NONE
);
985 if(!unit
->isInCombat() && unit
->GetTypeId() != TYPEID_PLAYER
&& ((Creature
*)unit
)->AI())
986 ((Creature
*)unit
)->AI()->AttackStart(m_caster
);
988 unit
->SetInCombatWith(m_caster
);
989 m_caster
->SetInCombatWith(unit
);
991 if(Player
*attackedPlayer
= unit
->GetCharmerOrOwnerPlayerOrPlayerItself())
993 m_caster
->SetContestedPvP(attackedPlayer
);
995 unit
->AddThreat(m_caster
, 0.0f
);
1001 // for delayed spells ignore negative spells (after duel end) for friendly targets
1002 if(m_spellInfo
->speed
> 0.0f
&& !IsPositiveSpell(m_spellInfo
->Id
))
1004 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_EVADE
);
1008 // assisting case, healing and resurrection
1009 if(unit
->hasUnitState(UNIT_STAT_ATTACK_PLAYER
))
1010 m_caster
->SetContestedPvP();
1011 if( unit
->isInCombat() && !(m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_NO_INITIAL_AGGRO
) )
1013 m_caster
->SetInCombatState(unit
->GetCombatTimer() > 0);
1014 unit
->getHostilRefManager().threatAssist(m_caster
, 0.0f
);
1019 // Get Data Needed for Diminishing Returns, some effects may have multiple auras, so this must be done on spell hit, not aura add
1020 m_diminishGroup
= GetDiminishingReturnsGroupForSpell(m_spellInfo
,m_triggeredByAuraSpell
);
1021 m_diminishLevel
= unit
->GetDiminishing(m_diminishGroup
);
1022 // Increase Diminishing on unit, current informations for actually casts will use values above
1023 if((GetDiminishingReturnsGroupType(m_diminishGroup
) == DRTYPE_PLAYER
&& unit
->GetTypeId() == TYPEID_PLAYER
) || GetDiminishingReturnsGroupType(m_diminishGroup
) == DRTYPE_ALL
)
1024 unit
->IncrDiminishing(m_diminishGroup
);
1026 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1028 if (effectMask
& (1<<effectNumber
))
1030 HandleEffects(unit
,NULL
,NULL
,effectNumber
,m_damageMultipliers
[effectNumber
]);
1031 if ( m_applyMultiplierMask
& (1 << effectNumber
) )
1034 float multiplier
= m_spellInfo
->DmgMultiplier
[effectNumber
];
1035 // Apply multiplier mods
1036 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1037 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_EFFECT_PAST_FIRST
, multiplier
,this);
1038 m_damageMultipliers
[effectNumber
] *= multiplier
;
1044 void Spell::DoAllEffectOnTarget(GOTargetInfo
*target
)
1046 if (target
->processed
) // Check target
1048 target
->processed
= true; // Target checked in apply effects procedure
1050 uint32 effectMask
= target
->effectMask
;
1054 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, target
->targetGUID
);
1058 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1059 if (effectMask
& (1<<effectNumber
))
1060 HandleEffects(NULL
,NULL
,go
,effectNumber
);
1062 // cast at creature (or GO) quest objectives update at successful cast finished (+channel finished)
1063 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
1064 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() && !IsChannelActive() )
1065 ((Player
*)m_caster
)->CastedCreatureOrGO(go
->GetEntry(),go
->GetGUID(),m_spellInfo
->Id
);
1068 void Spell::DoAllEffectOnTarget(ItemTargetInfo
*target
)
1070 uint32 effectMask
= target
->effectMask
;
1071 if(!target
->item
|| !effectMask
)
1074 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1075 if (effectMask
& (1<<effectNumber
))
1076 HandleEffects(NULL
, target
->item
, NULL
, effectNumber
);
1079 bool Spell::IsAliveUnitPresentInTargetList()
1081 // Not need check return true
1082 if (m_needAliveTargetMask
== 0)
1085 uint8 needAliveTargetMask
= m_needAliveTargetMask
;
1087 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
1089 if( ihit
->missCondition
== SPELL_MISS_NONE
&& (needAliveTargetMask
& ihit
->effectMask
) )
1091 Unit
*unit
= m_caster
->GetGUID()==ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
1093 if (unit
&& unit
->isAlive())
1094 needAliveTargetMask
&= ~ihit
->effectMask
; // remove from need alive mask effect that have alive target
1098 // is all effects from m_needAliveTargetMask have alive targets
1099 return needAliveTargetMask
==0;
1102 // Helper for Chain Healing
1103 // Spell target first
1104 // Raidmates then descending by injury suffered (MaxHealth - Health)
1105 // Other players/mobs then descending by injury suffered (MaxHealth - Health)
1106 struct ChainHealingOrder
: public std::binary_function
<const Unit
*, const Unit
*, bool>
1108 const Unit
* MainTarget
;
1109 ChainHealingOrder(Unit
const* Target
) : MainTarget(Target
) {};
1110 // functor for operator ">"
1111 bool operator()(Unit
const* _Left
, Unit
const* _Right
) const
1113 return (ChainHealingHash(_Left
) < ChainHealingHash(_Right
));
1115 int32
ChainHealingHash(Unit
const* Target
) const
1117 if (Target
== MainTarget
)
1119 else if (Target
->GetTypeId() == TYPEID_PLAYER
&& MainTarget
->GetTypeId() == TYPEID_PLAYER
&&
1120 ((Player
const*)Target
)->IsInSameRaidWith((Player
const*)MainTarget
))
1122 if (Target
->GetHealth() == Target
->GetMaxHealth())
1125 return 20000 - Target
->GetMaxHealth() + Target
->GetHealth();
1128 return 40000 - Target
->GetMaxHealth() + Target
->GetHealth();
1132 class ChainHealingFullHealth
: std::unary_function
<const Unit
*, bool>
1135 const Unit
* MainTarget
;
1136 ChainHealingFullHealth(const Unit
* Target
) : MainTarget(Target
) {};
1138 bool operator()(const Unit
* Target
)
1140 return (Target
!= MainTarget
&& Target
->GetHealth() == Target
->GetMaxHealth());
1144 // Helper for targets nearest to the spell target
1145 // The spell target is always first unless there is a target at _completely_ the same position (unbelievable case)
1146 struct TargetDistanceOrder
: public std::binary_function
<const Unit
, const Unit
, bool>
1148 const Unit
* MainTarget
;
1149 TargetDistanceOrder(const Unit
* Target
) : MainTarget(Target
) {};
1150 // functor for operator ">"
1151 bool operator()(const Unit
* _Left
, const Unit
* _Right
) const
1153 return (MainTarget
->GetDistance(_Left
) < MainTarget
->GetDistance(_Right
));
1157 void Spell::SetTargetMap(uint32 i
,uint32 cur
,std::list
<Unit
*> &TagUnitMap
)
1160 if (m_spellInfo
->EffectRadiusIndex
[i
])
1161 radius
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[i
]));
1163 radius
= GetSpellMaxRange(sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
));
1165 if(m_originalCaster
)
1166 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1167 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_RADIUS
, radius
,this);
1169 uint32 EffectChainTarget
= m_spellInfo
->EffectChainTarget
[i
];
1170 if(m_originalCaster
)
1171 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1172 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_JUMP_TARGETS
, EffectChainTarget
, this);
1174 uint32 unMaxTargets
= m_spellInfo
->MaxAffectedTargets
;
1177 case TARGET_TOTEM_EARTH
:
1178 case TARGET_TOTEM_WATER
:
1179 case TARGET_TOTEM_AIR
:
1180 case TARGET_TOTEM_FIRE
:
1183 case TARGET_DYNAMIC_OBJECT
:
1184 case TARGET_AREAEFFECT_CUSTOM
:
1185 case TARGET_AREAEFFECT_CUSTOM_2
:
1188 TagUnitMap
.push_back(m_caster
);
1191 case TARGET_RANDOM_ENEMY_CHAIN_IN_AREA
:
1193 m_targets
.m_targetMask
= 0;
1194 unMaxTargets
= EffectChainTarget
;
1195 float max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1197 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1199 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1202 std::list
<Unit
*> tempUnitMap
;
1205 MaNGOS::AnyAoETargetUnitInObjectRangeCheck
u_check(m_caster
, m_caster
, max_range
);
1206 MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
> searcher(tempUnitMap
, u_check
);
1208 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_unit_searcher(searcher
);
1209 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1211 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1212 cell_lock
->Visit(cell_lock
, world_unit_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1213 cell_lock
->Visit(cell_lock
, grid_unit_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1216 if(tempUnitMap
.empty())
1219 tempUnitMap
.sort(TargetDistanceOrder(m_caster
));
1221 //Now to get us a random target that's in the initial range of the spell
1223 std::list
<Unit
*>::iterator itr
= tempUnitMap
.begin();
1224 while(itr
!= tempUnitMap
.end() && (*itr
)->GetDistance(m_caster
) < radius
)
1230 itr
= tempUnitMap
.begin();
1231 std::advance(itr
, rand()%t
);
1232 Unit
*pUnitTarget
= *itr
;
1233 TagUnitMap
.push_back(pUnitTarget
);
1235 tempUnitMap
.erase(itr
);
1237 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1239 t
= unMaxTargets
- 1;
1240 Unit
*prev
= pUnitTarget
;
1241 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1243 while(t
&& next
!= tempUnitMap
.end() )
1245 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1248 if(!prev
->IsWithinLOSInMap(*next
))
1255 TagUnitMap
.push_back(prev
);
1256 tempUnitMap
.erase(next
);
1257 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1258 next
= tempUnitMap
.begin();
1265 Pet
* tmpUnit
= m_caster
->GetPet();
1266 if (!tmpUnit
) break;
1267 TagUnitMap
.push_back(tmpUnit
);
1270 case TARGET_CHAIN_DAMAGE
:
1272 if (EffectChainTarget
<= 1)
1274 Unit
* pUnitTarget
= SelectMagnetTarget();
1276 TagUnitMap
.push_back(pUnitTarget
);
1280 Unit
* pUnitTarget
= m_targets
.getUnitTarget();
1284 unMaxTargets
= EffectChainTarget
;
1287 if(m_spellInfo
->DmgClass
==SPELL_DAMAGE_CLASS_MELEE
)
1288 max_range
= radius
; //
1290 //FIXME: This very like horrible hack and wrong for most spells
1291 max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1293 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1295 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1298 Unit
* originalCaster
= GetOriginalCaster();
1301 std::list
<Unit
*> tempUnitMap
;
1304 MaNGOS::AnyAoETargetUnitInObjectRangeCheck
u_check(pUnitTarget
, originalCaster
, max_range
);
1305 MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
> searcher(tempUnitMap
, u_check
);
1307 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_unit_searcher(searcher
);
1308 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1310 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1311 cell_lock
->Visit(cell_lock
, world_unit_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1312 cell_lock
->Visit(cell_lock
, grid_unit_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1315 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1317 if(tempUnitMap
.empty())
1320 if(*tempUnitMap
.begin() == pUnitTarget
)
1321 tempUnitMap
.erase(tempUnitMap
.begin());
1323 TagUnitMap
.push_back(pUnitTarget
);
1324 uint32 t
= unMaxTargets
- 1;
1325 Unit
*prev
= pUnitTarget
;
1326 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1328 while(t
&& next
!= tempUnitMap
.end() )
1330 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1333 if(!prev
->IsWithinLOSInMap(*next
))
1340 TagUnitMap
.push_back(prev
);
1341 tempUnitMap
.erase(next
);
1342 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1343 next
= tempUnitMap
.begin();
1350 case TARGET_ALL_ENEMY_IN_AREA
:
1353 case TARGET_ALL_ENEMY_IN_AREA_INSTANT
:
1355 // targets the ground, not the units in the area
1356 if (m_spellInfo
->Effect
[i
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
1358 CellPair
p(MaNGOS::ComputeCellPair(m_targets
.m_destX
, m_targets
.m_destY
));
1360 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1363 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1365 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1366 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1368 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1369 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1370 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1372 // exclude caster (this can be important if this not original caster)
1373 TagUnitMap
.remove(m_caster
);
1376 case TARGET_DUELVSPLAYER_COORDINATES
:
1378 if(Unit
* currentTarget
= m_targets
.getUnitTarget())
1380 m_targets
.setDestination(currentTarget
->GetPositionX(), currentTarget
->GetPositionY(), currentTarget
->GetPositionZ());
1381 TagUnitMap
.push_back(currentTarget
);
1384 case TARGET_ALL_PARTY_AROUND_CASTER
:
1385 case TARGET_ALL_PARTY_AROUND_CASTER_2
:
1386 case TARGET_ALL_PARTY
:
1388 Player
*pTarget
= m_caster
->GetCharmerOrOwnerPlayerOrPlayerItself();
1389 Group
*pGroup
= pTarget
? pTarget
->GetGroup() : NULL
;
1393 uint8 subgroup
= pTarget
->GetSubGroup();
1395 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1397 Player
* Target
= itr
->getSource();
1399 // IsHostileTo check duel and controlled by enemy
1400 if( Target
&& Target
->GetSubGroup()==subgroup
&& !m_caster
->IsHostileTo(Target
) )
1402 if( m_caster
->IsWithinDistInMap(Target
, radius
) )
1403 TagUnitMap
.push_back(Target
);
1405 if(Pet
* pet
= Target
->GetPet())
1406 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1407 TagUnitMap
.push_back(pet
);
1413 Unit
* ownerOrSelf
= pTarget
? pTarget
: m_caster
->GetCharmerOrOwnerOrSelf();
1414 if(ownerOrSelf
==m_caster
|| m_caster
->IsWithinDistInMap(ownerOrSelf
, radius
))
1415 TagUnitMap
.push_back(ownerOrSelf
);
1416 if(Pet
* pet
= ownerOrSelf
->GetPet())
1417 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1418 TagUnitMap
.push_back(pet
);
1421 case TARGET_RANDOM_RAID_MEMBER
:
1423 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1424 if(Player
* target
= ((Player
*)m_caster
)->GetNextRandomRaidMember(radius
))
1425 TagUnitMap
.push_back(target
);
1427 case TARGET_SINGLE_FRIEND
:
1428 case TARGET_SINGLE_FRIEND_2
:
1430 if(m_targets
.getUnitTarget())
1431 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1433 case TARGET_NONCOMBAT_PET
:
1435 if(Unit
* target
= m_targets
.getUnitTarget())
1436 if( target
->GetTypeId() == TYPEID_UNIT
&& ((Creature
*)target
)->isPet() && ((Pet
*)target
)->getPetType() == MINI_PET
)
1437 TagUnitMap
.push_back(target
);
1439 case TARGET_ALL_AROUND_CASTER
:
1441 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1443 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1446 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_SELF_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1448 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1449 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1451 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1452 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1453 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1455 case TARGET_ALL_FRIENDLY_UNITS_AROUND_CASTER
:
1457 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1459 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1462 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_SELF_CENTER
,SPELL_TARGETS_FRIENDLY
);
1464 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1465 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1467 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1468 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1469 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1471 case TARGET_ALL_FRIENDLY_UNITS_IN_AREA
:
1473 CellPair
p(MaNGOS::ComputeCellPair(m_targets
.m_destX
, m_targets
.m_destY
));
1475 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1478 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_FRIENDLY
);
1480 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1481 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1483 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1484 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1485 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1487 // TARGET_SINGLE_PARTY means that the spells can only be casted on a party member and not on the caster (some sceals, fire shield from imp, etc..)
1488 case TARGET_SINGLE_PARTY
:
1490 Unit
*target
= m_targets
.getUnitTarget();
1491 // Thoses spells apparently can't be casted on the caster.
1492 if( target
&& target
!= m_caster
)
1494 // Can only be casted on group's members or its pets
1495 Group
*pGroup
= NULL
;
1497 Unit
* owner
= m_caster
->GetCharmerOrOwner();
1498 Unit
*targetOwner
= target
->GetCharmerOrOwner();
1501 if(owner
->GetTypeId() == TYPEID_PLAYER
)
1503 if( target
== owner
)
1505 TagUnitMap
.push_back(target
);
1508 pGroup
= ((Player
*)owner
)->GetGroup();
1511 else if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1513 if( targetOwner
== m_caster
&& target
->GetTypeId()==TYPEID_UNIT
&& ((Creature
*)target
)->isPet())
1515 TagUnitMap
.push_back(target
);
1518 pGroup
= ((Player
*)m_caster
)->GetGroup();
1523 // Our target can also be a player's pet who's grouped with us or our pet. But can't be controlled player
1526 if( targetOwner
->GetTypeId() == TYPEID_PLAYER
&&
1527 target
->GetTypeId()==TYPEID_UNIT
&& (((Creature
*)target
)->isPet()) &&
1528 target
->GetOwnerGUID()==targetOwner
->GetGUID() &&
1529 pGroup
->IsMember(((Player
*)targetOwner
)->GetGUID()))
1531 TagUnitMap
.push_back(target
);
1534 // 1Our target can be a player who is on our group
1535 else if (target
->GetTypeId() == TYPEID_PLAYER
&& pGroup
->IsMember(((Player
*)target
)->GetGUID()))
1537 TagUnitMap
.push_back(target
);
1542 case TARGET_GAMEOBJECT
:
1544 if(m_targets
.getGOTarget())
1545 AddGOTarget(m_targets
.getGOTarget(), i
);
1547 case TARGET_IN_FRONT_OF_CASTER
:
1549 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1551 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1554 bool inFront
= m_spellInfo
->SpellVisual
[0] != 3879;
1555 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, inFront
? PUSH_IN_FRONT
: PUSH_IN_BACK
,SPELL_TARGETS_AOE_DAMAGE
);
1557 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1558 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1560 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1561 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1562 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1564 case TARGET_DUELVSPLAYER
:
1566 Unit
*target
= m_targets
.getUnitTarget();
1569 if(m_caster
->IsFriendlyTo(target
))
1571 TagUnitMap
.push_back(target
);
1575 Unit
* pUnitTarget
= SelectMagnetTarget();
1577 TagUnitMap
.push_back(pUnitTarget
);
1581 case TARGET_GAMEOBJECT_ITEM
:
1583 if(m_targets
.getGOTargetGUID())
1584 AddGOTarget(m_targets
.getGOTarget(), i
);
1585 else if(m_targets
.getItemTarget())
1586 AddItemTarget(m_targets
.getItemTarget(), i
);
1591 if(Unit
* owner
= m_caster
->GetCharmerOrOwner())
1592 TagUnitMap
.push_back(owner
);
1595 case TARGET_ALL_ENEMY_IN_AREA_CHANNELED
:
1597 // targets the ground, not the units in the area
1598 if (m_spellInfo
->Effect
[i
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
1600 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1602 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1605 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1607 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1608 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1610 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1611 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1612 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1617 if(m_spellInfo
->Effect
[i
] != SPELL_EFFECT_DUEL
)
1618 TagUnitMap
.push_back(m_caster
);
1620 case TARGET_SINGLE_ENEMY
:
1622 Unit
* pUnitTarget
= SelectMagnetTarget();
1624 TagUnitMap
.push_back(pUnitTarget
);
1626 case TARGET_AREAEFFECT_PARTY
:
1628 Unit
* owner
= m_caster
->GetCharmerOrOwner();
1629 Player
*pTarget
= NULL
;
1633 TagUnitMap
.push_back(m_caster
);
1634 if(owner
->GetTypeId() == TYPEID_PLAYER
)
1635 pTarget
= (Player
*)owner
;
1637 else if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1639 if(Unit
* target
= m_targets
.getUnitTarget())
1641 if( target
->GetTypeId() != TYPEID_PLAYER
)
1643 if(((Creature
*)target
)->isPet())
1645 Unit
*targetOwner
= target
->GetOwner();
1646 if(targetOwner
->GetTypeId() == TYPEID_PLAYER
)
1647 pTarget
= (Player
*)targetOwner
;
1651 pTarget
= (Player
*)target
;
1655 Group
* pGroup
= pTarget
? pTarget
->GetGroup() : NULL
;
1659 uint8 subgroup
= pTarget
->GetSubGroup();
1661 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1663 Player
* Target
= itr
->getSource();
1665 // IsHostileTo check duel and controlled by enemy
1666 if(Target
&& Target
->GetSubGroup()==subgroup
&& !m_caster
->IsHostileTo(Target
))
1668 if( pTarget
->IsWithinDistInMap(Target
, radius
) )
1669 TagUnitMap
.push_back(Target
);
1671 if(Pet
* pet
= Target
->GetPet())
1672 if( pTarget
->IsWithinDistInMap(pet
, radius
) )
1673 TagUnitMap
.push_back(pet
);
1679 if(m_caster
->IsWithinDistInMap(owner
, radius
))
1680 TagUnitMap
.push_back(owner
);
1684 TagUnitMap
.push_back(pTarget
);
1686 if(Pet
* pet
= pTarget
->GetPet())
1687 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1688 TagUnitMap
.push_back(pet
);
1694 if(m_targets
.getUnitTarget())
1695 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1696 if(m_targets
.getItemTarget())
1697 AddItemTarget(m_targets
.getItemTarget(), i
);
1699 case TARGET_SELF_FISHING
:
1701 TagUnitMap
.push_back(m_caster
);
1703 case TARGET_CHAIN_HEAL
:
1705 Unit
* pUnitTarget
= m_targets
.getUnitTarget();
1709 if (EffectChainTarget
<= 1)
1710 TagUnitMap
.push_back(pUnitTarget
);
1713 unMaxTargets
= EffectChainTarget
;
1714 float max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1716 std::list
<Unit
*> tempUnitMap
;
1719 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1721 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1724 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, tempUnitMap
, max_range
, PUSH_SELF_CENTER
, SPELL_TARGETS_FRIENDLY
);
1726 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1727 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1729 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1730 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1731 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1735 if(m_caster
!= pUnitTarget
&& std::find(tempUnitMap
.begin(),tempUnitMap
.end(),m_caster
) == tempUnitMap
.end() )
1736 tempUnitMap
.push_front(m_caster
);
1738 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1740 if(tempUnitMap
.empty())
1743 if(*tempUnitMap
.begin() == pUnitTarget
)
1744 tempUnitMap
.erase(tempUnitMap
.begin());
1746 TagUnitMap
.push_back(pUnitTarget
);
1747 uint32 t
= unMaxTargets
- 1;
1748 Unit
*prev
= pUnitTarget
;
1749 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1751 while(t
&& next
!= tempUnitMap
.end() )
1753 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1756 if(!prev
->IsWithinLOSInMap(*next
))
1762 if((*next
)->GetHealth() == (*next
)->GetMaxHealth())
1764 next
= tempUnitMap
.erase(next
);
1769 TagUnitMap
.push_back(prev
);
1770 tempUnitMap
.erase(next
);
1771 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1772 next
= tempUnitMap
.begin();
1778 case TARGET_CURRENT_ENEMY_COORDINATES
:
1780 Unit
* currentTarget
= m_targets
.getUnitTarget();
1783 TagUnitMap
.push_back(currentTarget
);
1784 m_targets
.setDestination(currentTarget
->GetPositionX(), currentTarget
->GetPositionY(), currentTarget
->GetPositionZ());
1785 if(m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_ENEMY_IN_AREA_INSTANT
)
1787 CellPair
p(MaNGOS::ComputeCellPair(currentTarget
->GetPositionX(), currentTarget
->GetPositionY()));
1789 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1791 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
,PUSH_TARGET_CENTER
, SPELL_TARGETS_AOE_DAMAGE
);
1792 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_notifier(notifier
);
1793 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_notifier(notifier
);
1794 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1795 cell_lock
->Visit(cell_lock
, world_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1796 cell_lock
->Visit(cell_lock
, grid_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1800 case TARGET_AREAEFFECT_PARTY_AND_CLASS
:
1802 Player
* targetPlayer
= m_targets
.getUnitTarget() && m_targets
.getUnitTarget()->GetTypeId() == TYPEID_PLAYER
1803 ? (Player
*)m_targets
.getUnitTarget() : NULL
;
1805 Group
* pGroup
= targetPlayer
? targetPlayer
->GetGroup() : NULL
;
1808 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1810 Player
* Target
= itr
->getSource();
1812 // IsHostileTo check duel and controlled by enemy
1813 if( Target
&& targetPlayer
->IsWithinDistInMap(Target
, radius
) &&
1814 targetPlayer
->getClass() == Target
->getClass() &&
1815 !m_caster
->IsHostileTo(Target
) )
1817 TagUnitMap
.push_back(Target
);
1821 else if(m_targets
.getUnitTarget())
1822 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1825 case TARGET_TABLE_X_Y_Z_COORDINATES
:
1827 SpellTargetPosition
const* st
= spellmgr
.GetSpellTargetPosition(m_spellInfo
->Id
);
1830 if (st
->target_mapId
== m_caster
->GetMapId())
1831 m_targets
.setDestination(st
->target_X
, st
->target_Y
, st
->target_Z
);
1833 // if B==TARGET_TABLE_X_Y_Z_COORDINATES then A already fill all required targets
1834 if (m_spellInfo
->EffectImplicitTargetB
[i
] && m_spellInfo
->EffectImplicitTargetB
[i
]!=TARGET_TABLE_X_Y_Z_COORDINATES
)
1836 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1838 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1841 SpellTargets targetB
= SPELL_TARGETS_AOE_DAMAGE
;
1842 // Select friendly targets for positive effect
1843 if (IsPositiveEffect(m_spellInfo
->Id
, i
))
1844 targetB
= SPELL_TARGETS_FRIENDLY
;
1846 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
,PUSH_DEST_CENTER
, targetB
);
1848 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_notifier(notifier
);
1849 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_notifier(notifier
);
1851 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1852 cell_lock
->Visit(cell_lock
, world_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1853 cell_lock
->Visit(cell_lock
, grid_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1857 sLog
.outError( "SPELL: unknown target coordinates for spell ID %u\n", m_spellInfo
->Id
);
1859 case TARGET_BEHIND_VICTIM
:
1861 Unit
*pTarget
= m_caster
->getVictim();
1862 if(!pTarget
&& m_caster
->GetTypeId() == TYPEID_PLAYER
)
1863 pTarget
= ObjectAccessor::GetUnit(*m_caster
, ((Player
*)m_caster
)->GetSelection());
1867 float _target_x
, _target_y
, _target_z
;
1868 pTarget
->GetClosePoint(_target_x
, _target_y
, _target_z
, m_caster
->GetObjectSize(), CONTACT_DISTANCE
, M_PI
);
1869 if(pTarget
->IsWithinLOS(_target_x
,_target_y
,_target_z
))
1870 m_targets
.setDestination(_target_x
, _target_y
, _target_z
);
1877 if (unMaxTargets
&& TagUnitMap
.size() > unMaxTargets
)
1879 // make sure one unit is always removed per iteration
1880 uint32 removed_utarget
= 0;
1881 for (std::list
<Unit
*>::iterator itr
= TagUnitMap
.begin(), next
; itr
!= TagUnitMap
.end(); itr
= next
)
1885 if (!*itr
) continue;
1886 if ((*itr
) == m_targets
.getUnitTarget())
1888 TagUnitMap
.erase(itr
);
1889 removed_utarget
= 1;
1893 // remove random units from the map
1894 while (TagUnitMap
.size() > unMaxTargets
- removed_utarget
)
1896 uint32 poz
= urand(0, TagUnitMap
.size()-1);
1897 for (std::list
<Unit
*>::iterator itr
= TagUnitMap
.begin(); itr
!= TagUnitMap
.end(); ++itr
, --poz
)
1899 if (!*itr
) continue;
1902 TagUnitMap
.erase(itr
);
1907 // the player's target will always be added to the map
1908 if (removed_utarget
&& m_targets
.getUnitTarget())
1909 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1913 void Spell::prepare(SpellCastTargets
* targets
, Aura
* triggeredByAura
)
1915 m_targets
= *targets
;
1917 m_spellState
= SPELL_STATE_PREPARING
;
1919 m_castPositionX
= m_caster
->GetPositionX();
1920 m_castPositionY
= m_caster
->GetPositionY();
1921 m_castPositionZ
= m_caster
->GetPositionZ();
1922 m_castOrientation
= m_caster
->GetOrientation();
1925 m_triggeredByAuraSpell
= triggeredByAura
->GetSpellProto();
1927 // create and add update event for this spell
1928 SpellEvent
* Event
= new SpellEvent(this);
1929 m_caster
->m_Events
.AddEvent(Event
, m_caster
->m_Events
.CalculateTime(1));
1931 //Prevent casting at cast another spell (ServerSide check)
1932 if(m_caster
->IsNonMeleeSpellCasted(false, true) && m_cast_count
)
1934 SendCastResult(SPELL_FAILED_SPELL_IN_PROGRESS
);
1940 m_powerCost
= CalculatePowerCost();
1942 uint8 result
= CanCast(true);
1943 if(result
!= 0 && !IsAutoRepeat()) //always cast autorepeat dummy for triggering
1947 SendChannelUpdate(0);
1948 triggeredByAura
->SetAuraDuration(0);
1950 SendCastResult(result
);
1955 // calculate cast time (calculated after first CanCast check to prevent charge counting for first CanCast fail)
1956 m_casttime
= GetSpellCastTime(m_spellInfo
, this);
1958 // set timer base at cast time
1961 // stealth must be removed at cast starting (at show channel bar)
1962 // skip triggered spell (item equip spell casting and other not explicit character casts/item uses)
1963 if ( !m_IsTriggeredSpell
&& isSpellBreakStealth(m_spellInfo
) )
1965 m_caster
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
1966 m_caster
->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH
);
1969 if(m_IsTriggeredSpell
)
1973 m_caster
->SetCurrentCastedSpell( this );
1974 m_selfContainer
= &(m_caster
->m_currentSpells
[GetCurrentContainer()]);
1979 void Spell::cancel()
1981 if(m_spellState
== SPELL_STATE_FINISHED
)
1984 m_autoRepeat
= false;
1985 switch (m_spellState
)
1987 case SPELL_STATE_PREPARING
:
1988 case SPELL_STATE_DELAYED
:
1991 SendCastResult(SPELL_FAILED_INTERRUPTED
);
1994 case SPELL_STATE_CASTING
:
1996 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
1998 if( ihit
->missCondition
== SPELL_MISS_NONE
)
2000 Unit
* unit
= m_caster
->GetGUID()==(*ihit
).targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
2001 if( unit
&& unit
->isAlive() )
2002 unit
->RemoveAurasDueToSpell(m_spellInfo
->Id
);
2006 m_caster
->RemoveAurasDueToSpell(m_spellInfo
->Id
);
2007 SendChannelUpdate(0);
2009 SendCastResult(SPELL_FAILED_INTERRUPTED
);
2018 m_caster
->RemoveDynObject(m_spellInfo
->Id
);
2019 m_caster
->RemoveGameObject(m_spellInfo
->Id
,true);
2022 void Spell::cast(bool skipCheck
)
2024 SetExecutedCurrently(true);
2026 uint8 castResult
= 0;
2028 // update pointers base at GUIDs to prevent access to non-existed already object
2031 // cancel at lost main target unit
2032 if(!m_targets
.getUnitTarget() && m_targets
.getUnitTargetGUID() && m_targets
.getUnitTargetGUID() != m_caster
->GetGUID())
2035 SetExecutedCurrently(false);
2039 if(m_caster
->GetTypeId() != TYPEID_PLAYER
&& m_targets
.getUnitTarget() && m_targets
.getUnitTarget() != m_caster
)
2040 m_caster
->SetInFront(m_targets
.getUnitTarget());
2042 castResult
= CheckPower();
2045 SendCastResult(castResult
);
2047 SetExecutedCurrently(false);
2051 // triggered cast called from Spell::prepare where it was already checked
2054 castResult
= CanCast(false);
2057 SendCastResult(castResult
);
2059 SetExecutedCurrently(false);
2064 // Conflagrate - consumes immolate
2065 if ((m_spellInfo
->TargetAuraState
== AURA_STATE_IMMOLATE
) && m_targets
.getUnitTarget())
2067 // for caster applied auras only
2068 Unit::AuraList
const &mPeriodic
= m_targets
.getUnitTarget()->GetAurasByType(SPELL_AURA_PERIODIC_DAMAGE
);
2069 for(Unit::AuraList::const_iterator i
= mPeriodic
.begin(); i
!= mPeriodic
.end(); ++i
)
2071 if( (*i
)->GetSpellProto()->SpellFamilyName
== SPELLFAMILY_WARLOCK
&& ((*i
)->GetSpellProto()->SpellFamilyFlags
& 4) &&
2072 (*i
)->GetCasterGUID()==m_caster
->GetGUID() )
2074 m_targets
.getUnitTarget()->RemoveAura((*i
)->GetId(), (*i
)->GetEffIndex());
2080 // traded items have trade slot instead of guid in m_itemTargetGUID
2081 // set to real guid to be sent later to the client
2082 m_targets
.updateTradeSlotItem();
2085 SendSpellCooldown();
2088 TakeReagents(); // we must remove reagents before HandleEffects to allow place crafted item in same slot
2091 if(m_spellState
== SPELL_STATE_FINISHED
) // stop cast if spell marked as finish somewhere in Take*/FillTargetMap
2093 SetExecutedCurrently(false);
2097 SendCastResult(castResult
);
2098 SendSpellGo(); // we must send smsg_spell_go packet before m_castItem delete in TakeCastItem()...
2100 // Pass cast spell event to handler (not send triggered by aura spells)
2101 if (m_spellInfo
->DmgClass
!= SPELL_DAMAGE_CLASS_MELEE
&& m_spellInfo
->DmgClass
!= SPELL_DAMAGE_CLASS_RANGED
&& !m_triggeredByAuraSpell
)
2103 m_caster
->ProcDamageAndSpell(m_targets
.getUnitTarget(), PROC_FLAG_CAST_SPELL
, PROC_FLAG_NONE
, 0, SPELL_SCHOOL_MASK_NONE
, m_spellInfo
, m_IsTriggeredSpell
);
2105 // update pointers base at GUIDs to prevent access to non-existed already object
2106 UpdatePointers(); // pointers can be invalidate at triggered spell casting
2109 // Okay, everything is prepared. Now we need to distinguish between immediate and evented delayed spells
2110 if (m_spellInfo
->speed
> 0.0f
)
2113 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2114 // in case delayed spell remove item at cast delay start
2117 // Okay, maps created, now prepare flags
2118 m_immediateHandled
= false;
2119 m_spellState
= SPELL_STATE_DELAYED
;
2124 // Immediate spell, no big deal
2128 SetExecutedCurrently(false);
2131 void Spell::handle_immediate()
2133 // start channeling if applicable
2134 if(IsChanneledSpell(m_spellInfo
))
2136 m_spellState
= SPELL_STATE_CASTING
;
2137 SendChannelStart(GetSpellDuration(m_spellInfo
));
2140 // process immediate effects (items, ground, etc.) also initialize some variables
2141 _handle_immediate_phase();
2143 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2144 DoAllEffectOnTarget(&(*ihit
));
2146 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
2147 DoAllEffectOnTarget(&(*ihit
));
2149 // spell is finished, perform some last features of the spell here
2150 _handle_finish_phase();
2152 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2155 if(m_spellState
!= SPELL_STATE_CASTING
)
2156 finish(true); // successfully finish spell cast (not last in case autorepeat or channel spell)
2159 uint64
Spell::handle_delayed(uint64 t_offset
)
2161 uint64 next_time
= 0;
2163 if (!m_immediateHandled
)
2165 _handle_immediate_phase();
2166 m_immediateHandled
= true;
2169 // 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)
2170 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2172 if (ihit
->processed
== false)
2174 if ( ihit
->timeDelay
<= t_offset
)
2175 DoAllEffectOnTarget(&(*ihit
));
2176 else if( next_time
== 0 || ihit
->timeDelay
< next_time
)
2177 next_time
= ihit
->timeDelay
;
2181 // now recheck gameobject targeting correctness
2182 for(std::list
<GOTargetInfo
>::iterator ighit
= m_UniqueGOTargetInfo
.begin(); ighit
!= m_UniqueGOTargetInfo
.end();++ighit
)
2184 if (ighit
->processed
== false)
2186 if ( ighit
->timeDelay
<= t_offset
)
2187 DoAllEffectOnTarget(&(*ighit
));
2188 else if( next_time
== 0 || ighit
->timeDelay
< next_time
)
2189 next_time
= ighit
->timeDelay
;
2192 // All targets passed - need finish phase
2195 // spell is finished, perform some last features of the spell here
2196 _handle_finish_phase();
2198 finish(true); // successfully finish spell cast
2200 // return zero, spell is finished now
2205 // spell is unfinished, return next execution time
2210 void Spell::_handle_immediate_phase()
2212 // handle some immediate features of the spell here
2213 HandleThreatSpells(m_spellInfo
->Id
);
2215 m_needSpellLog
= IsNeedSendToClient();
2216 for(uint32 j
= 0;j
<3;j
++)
2218 if(m_spellInfo
->Effect
[j
]==0)
2221 // apply Send Event effect to ground in case empty target lists
2222 if( m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SEND_EVENT
&& !HaveTargetsForEffect(j
) )
2224 HandleEffects(NULL
,NULL
,NULL
, j
);
2228 // Don't do spell log, if is school damage spell
2229 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SCHOOL_DAMAGE
|| m_spellInfo
->Effect
[j
] == 0)
2230 m_needSpellLog
= false;
2232 uint32 EffectChainTarget
= m_spellInfo
->EffectChainTarget
[j
];
2233 if(m_originalCaster
)
2234 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
2235 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_JUMP_TARGETS
, EffectChainTarget
, this);
2237 // initialize multipliers
2238 m_damageMultipliers
[j
] = 1.0f
;
2239 if( (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_DAMAGE
|| m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_HEAL
) &&
2240 (EffectChainTarget
> 1) )
2241 m_applyMultiplierMask
|= 1 << j
;
2244 // initialize Diminishing Returns Data
2245 m_diminishLevel
= DIMINISHING_LEVEL_1
;
2246 m_diminishGroup
= DIMINISHING_NONE
;
2249 for(std::list
<ItemTargetInfo
>::iterator ihit
= m_UniqueItemInfo
.begin();ihit
!= m_UniqueItemInfo
.end();++ihit
)
2250 DoAllEffectOnTarget(&(*ihit
));
2253 for(uint32 j
= 0;j
<3;j
++)
2255 // persistent area auras target only the ground
2256 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_PERSISTENT_AREA_AURA
)
2257 HandleEffects(NULL
,NULL
,NULL
, j
);
2261 void Spell::_handle_finish_phase()
2268 void Spell::SendSpellCooldown()
2270 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
2273 Player
* _player
= (Player
*)m_caster
;
2274 // Add cooldown for max (disable spell)
2275 // Cooldown started on SendCooldownEvent call
2276 if (m_spellInfo
->Attributes
& SPELL_ATTR_DISABLED_WHILE_ACTIVE
)
2278 _player
->AddSpellCooldown(m_spellInfo
->Id
, 0, time(NULL
) - 1);
2282 // init cooldown values
2287 // some special item spells without correct cooldown in SpellInfo
2288 // cooldown information stored in item prototype
2289 // This used in same way in WorldSession::HandleItemQuerySingleOpcode data sending to client.
2293 ItemPrototype
const* proto
= m_CastItem
->GetProto();
2296 for(int idx
= 0; idx
< 5; ++idx
)
2298 if(proto
->Spells
[idx
].SpellId
== m_spellInfo
->Id
)
2300 cat
= proto
->Spells
[idx
].SpellCategory
;
2301 rec
= proto
->Spells
[idx
].SpellCooldown
;
2302 catrec
= proto
->Spells
[idx
].SpellCategoryCooldown
;
2309 // if no cooldown found above then base at DBC data
2310 if(rec
< 0 && catrec
< 0)
2312 cat
= m_spellInfo
->Category
;
2313 rec
= m_spellInfo
->RecoveryTime
;
2314 catrec
= m_spellInfo
->CategoryRecoveryTime
;
2317 // shoot spells used equipped item cooldown values already assigned in GetAttackTime(RANGED_ATTACK)
2318 // prevent 0 cooldowns set by another way
2319 if (rec
<= 0 && catrec
<= 0 && (cat
== 76 || cat
== 351))
2320 rec
= _player
->GetAttackTime(RANGED_ATTACK
);
2322 // Now we have cooldown data (if found any), time to apply mods
2324 _player
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COOLDOWN
, rec
, this);
2327 _player
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COOLDOWN
, catrec
, this);
2329 // replace negative cooldowns by 0
2330 if (rec
< 0) rec
= 0;
2331 if (catrec
< 0) catrec
= 0;
2333 // no cooldown after applying spell mods
2334 if( rec
== 0 && catrec
== 0)
2337 time_t curTime
= time(NULL
);
2339 time_t catrecTime
= catrec
? curTime
+catrec
/1000 : 0; // in secs
2340 time_t recTime
= rec
? curTime
+rec
/1000 : catrecTime
;// in secs
2342 // self spell cooldown
2344 _player
->AddSpellCooldown(m_spellInfo
->Id
, m_CastItem
? m_CastItem
->GetEntry() : 0, recTime
);
2349 SpellCategoryStore::const_iterator i_scstore
= sSpellCategoryStore
.find(cat
);
2350 if(i_scstore
!= sSpellCategoryStore
.end())
2352 for(SpellCategorySet::const_iterator i_scset
= i_scstore
->second
.begin(); i_scset
!= i_scstore
->second
.end(); ++i_scset
)
2354 if(*i_scset
== m_spellInfo
->Id
) // skip main spell, already handled above
2357 _player
->AddSpellCooldown(m_spellInfo
->Id
, m_CastItem
? m_CastItem
->GetEntry() : 0, catrecTime
);
2363 void Spell::update(uint32 difftime
)
2365 // update pointers based at it's GUIDs
2368 if(m_targets
.getUnitTargetGUID() && !m_targets
.getUnitTarget())
2374 // check if the player caster has moved before the spell finished
2375 if ((m_caster
->GetTypeId() == TYPEID_PLAYER
&& m_timer
!= 0) &&
2376 (m_castPositionX
!= m_caster
->GetPositionX() || m_castPositionY
!= m_caster
->GetPositionY() || m_castPositionZ
!= m_caster
->GetPositionZ()) &&
2377 (m_spellInfo
->Effect
[0] != SPELL_EFFECT_STUCK
|| !m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_FALLING
)))
2379 // always cancel for channeled spells
2380 if( m_spellState
== SPELL_STATE_CASTING
)
2382 // don't cancel for melee, autorepeat, triggered and instant spells
2383 else if(!IsNextMeleeSwingSpell() && !IsAutoRepeat() && !m_IsTriggeredSpell
&& (m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_MOVEMENT
))
2387 switch(m_spellState
)
2389 case SPELL_STATE_PREPARING
:
2393 if(difftime
>= m_timer
)
2396 m_timer
-= difftime
;
2399 if(m_timer
== 0 && !IsNextMeleeSwingSpell() && !IsAutoRepeat())
2402 case SPELL_STATE_CASTING
:
2406 if( m_caster
->GetTypeId() == TYPEID_PLAYER
)
2408 // check if player has jumped before the channeling finished
2409 if(m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_JUMPING
))
2412 // check for incapacitating player states
2413 if( m_caster
->hasUnitState(UNIT_STAT_STUNNED
| UNIT_STAT_CONFUSED
))
2416 // check if player has turned if flag is set
2417 if( m_spellInfo
->ChannelInterruptFlags
& CHANNEL_FLAG_TURNING
&& m_castOrientation
!= m_caster
->GetOrientation() )
2421 // check if there are alive targets left
2422 if (!IsAliveUnitPresentInTargetList())
2424 SendChannelUpdate(0);
2428 if(difftime
>= m_timer
)
2431 m_timer
-= difftime
;
2436 SendChannelUpdate(0);
2438 // channeled spell processed independently for quest targeting
2439 // cast at creature (or GO) quest objectives update at successful cast channel finished
2440 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
2441 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() )
2443 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2445 TargetInfo
* target
= &*ihit
;
2446 if(!IS_CREATURE_GUID(target
->targetGUID
))
2449 Unit
* unit
= m_caster
->GetGUID()==target
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
,target
->targetGUID
);
2453 ((Player
*)m_caster
)->CastedCreatureOrGO(unit
->GetEntry(),unit
->GetGUID(),m_spellInfo
->Id
);
2456 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
2458 GOTargetInfo
* target
= &*ihit
;
2460 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, target
->targetGUID
);
2464 ((Player
*)m_caster
)->CastedCreatureOrGO(go
->GetEntry(),go
->GetGUID(),m_spellInfo
->Id
);
2477 void Spell::finish(bool ok
)
2482 if(m_spellState
== SPELL_STATE_FINISHED
)
2485 m_spellState
= SPELL_STATE_FINISHED
;
2488 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2489 ((Player
*)m_caster
)->RemoveSpellMods(this);
2491 // other code related only to successfully finished spells
2495 //handle SPELL_AURA_ADD_TARGET_TRIGGER auras
2496 Unit::AuraList
const& targetTriggers
= m_caster
->GetAurasByType(SPELL_AURA_ADD_TARGET_TRIGGER
);
2497 for(Unit::AuraList::const_iterator i
= targetTriggers
.begin(); i
!= targetTriggers
.end(); ++i
)
2499 SpellEntry
const *auraSpellInfo
= (*i
)->GetSpellProto();
2500 uint32 auraSpellIdx
= (*i
)->GetEffIndex();
2501 if (IsAffectedBy(auraSpellInfo
, auraSpellIdx
))
2503 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2504 if( ihit
->effectMask
& (1<<auraSpellIdx
) )
2506 // check m_caster->GetGUID() let load auras at login and speedup most often case
2507 Unit
*unit
= m_caster
->GetGUID()== ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
2508 if (unit
&& unit
->isAlive())
2510 // Calculate chance at that moment (can be depend for example from combo points)
2511 int32 chance
= m_caster
->CalculateSpellDamage(auraSpellInfo
, auraSpellIdx
, (*i
)->GetBasePoints(),unit
);
2513 if(roll_chance_i(chance
))
2514 m_caster
->CastSpell(unit
, auraSpellInfo
->EffectTriggerSpell
[auraSpellIdx
], true, NULL
, (*i
));
2520 if (IsMeleeAttackResetSpell())
2522 m_caster
->resetAttackTimer(BASE_ATTACK
);
2523 if(m_caster
->haveOffhandWeapon())
2524 m_caster
->resetAttackTimer(OFF_ATTACK
);
2527 /*if (IsRangedAttackResetSpell())
2528 m_caster->resetAttackTimer(RANGED_ATTACK);*/
2530 // Clear combo at finish state
2531 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& NeedsComboPoints(m_spellInfo
))
2532 ((Player
*)m_caster
)->ClearComboPoints();
2534 // call triggered spell only at successful cast (after clear combo points -> for add some if need)
2535 if(!m_TriggerSpells
.empty())
2538 // Stop Attack for some spells
2539 if( m_spellInfo
->Attributes
& SPELL_ATTR_STOP_ATTACK_TARGET
)
2540 m_caster
->AttackStop();
2543 void Spell::SendCastResult(uint8 result
)
2545 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2548 if(((Player
*)m_caster
)->GetSession()->PlayerLoading()) // don't send cast results at loading time
2553 WorldPacket
data(SMSG_CAST_FAILED
, (4+1+1));
2554 data
<< uint8(m_cast_count
); // single cast or multi 2.3 (0/1)
2555 data
<< uint32(m_spellInfo
->Id
);
2556 data
<< uint8(result
); // problem
2559 case SPELL_FAILED_REQUIRES_SPELL_FOCUS
:
2560 data
<< uint32(m_spellInfo
->RequiresSpellFocus
);
2562 case SPELL_FAILED_REQUIRES_AREA
:
2563 // hardcode areas limitation case
2564 if( m_spellInfo
->Id
==41618 || m_spellInfo
->Id
==41620 )
2565 data
<< uint32(3842);
2566 else if( m_spellInfo
->Id
==41617 || m_spellInfo
->Id
==41619 )
2567 data
<< uint32(3905);
2570 data
<< uint32(m_spellInfo
->AreaId
);
2572 case SPELL_FAILED_TOTEMS
:
2573 if(m_spellInfo
->Totem
[0])
2574 data
<< uint32(m_spellInfo
->Totem
[0]);
2575 if(m_spellInfo
->Totem
[1])
2576 data
<< uint32(m_spellInfo
->Totem
[1]);
2578 case SPELL_FAILED_TOTEM_CATEGORY
:
2579 if(m_spellInfo
->TotemCategory
[0])
2580 data
<< uint32(m_spellInfo
->TotemCategory
[0]);
2581 if(m_spellInfo
->TotemCategory
[1])
2582 data
<< uint32(m_spellInfo
->TotemCategory
[1]);
2584 case SPELL_FAILED_EQUIPPED_ITEM_CLASS
:
2585 data
<< uint32(m_spellInfo
->EquippedItemClass
);
2586 data
<< uint32(m_spellInfo
->EquippedItemSubClassMask
);
2587 //data << uint32(m_spellInfo->EquippedItemInventoryTypeMask);
2590 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
2594 WorldPacket data(SMSG_CLEAR_EXTRA_AURA_INFO_OBSOLETE, (8+4));
2595 data.append(m_caster->GetPackGUID());
2596 data << uint32(m_spellInfo->Id);
2597 ((Player*)m_caster)->GetSession()->SendPacket(&data);
2601 void Spell::SendSpellStart()
2603 if(!IsNeedSendToClient())
2606 sLog
.outDebug("Sending SMSG_SPELL_START id=%u", m_spellInfo
->Id
);
2608 uint32 castFlags
= CAST_FLAG_UNKNOWN1
;
2610 castFlags
|= CAST_FLAG_AMMO
;
2613 if(!m_targets
.getUnitTarget())
2616 target
= m_targets
.getUnitTarget();
2618 WorldPacket
data(SMSG_SPELL_START
, (8+8+4+4+2));
2620 data
.append(m_CastItem
->GetPackGUID());
2622 data
.append(m_caster
->GetPackGUID());
2624 data
.append(m_caster
->GetPackGUID());
2625 data
<< uint8(m_cast_count
); // pending spell cast?
2626 data
<< uint32(m_spellInfo
->Id
);
2627 data
<< uint32(castFlags
);
2628 data
<< uint32(m_timer
);
2630 m_targets
.write(&data
);
2632 if ( castFlags
& CAST_FLAG_UNKNOWN6
)
2635 if ( castFlags
& CAST_FLAG_UNKNOWN7
)
2641 for(uint8 i
= 0; i
< 6; ++i
)
2647 if ( castFlags
& CAST_FLAG_AMMO
)
2648 WriteAmmoToPacket(&data
);
2650 m_caster
->SendMessageToSet(&data
, true);
2653 void Spell::SendSpellGo()
2655 // not send invisible spell casting
2656 if(!IsNeedSendToClient())
2659 sLog
.outDebug("Sending SMSG_SPELL_GO id=%u",m_spellInfo
->Id
);
2662 if(!m_targets
.getUnitTarget())
2665 target
= m_targets
.getUnitTarget();
2667 uint32 castFlags
= CAST_FLAG_UNKNOWN3
;
2669 castFlags
|= CAST_FLAG_AMMO
;
2671 WorldPacket
data(SMSG_SPELL_GO
, 50); // guess size
2673 data
.append(m_CastItem
->GetPackGUID());
2675 data
.append(m_caster
->GetPackGUID());
2677 data
.append(m_caster
->GetPackGUID());
2678 data
<< uint8(m_cast_count
); // pending spell cast?
2679 data
<< uint32(m_spellInfo
->Id
);
2680 data
<< uint32(castFlags
);
2681 data
<< uint32(getMSTime()); // timestamp
2683 WriteSpellGoTargets(&data
);
2685 m_targets
.write(&data
);
2687 if ( castFlags
& CAST_FLAG_UNKNOWN6
) // unknown wotlk
2690 if ( castFlags
& CAST_FLAG_UNKNOWN7
)
2696 for(uint8 i
= 0; i
< 6; ++i
)
2702 if ( castFlags
& CAST_FLAG_UNKNOWN4
) // unknown wotlk
2708 if ( castFlags
& CAST_FLAG_AMMO
)
2709 WriteAmmoToPacket(&data
);
2711 if ( castFlags
& CAST_FLAG_UNKNOWN5
) // unknown wotlk
2717 if ( m_targets
.m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
2722 m_caster
->SendMessageToSet(&data
, true);
2725 void Spell::WriteAmmoToPacket( WorldPacket
* data
)
2727 uint32 ammoInventoryType
= 0;
2728 uint32 ammoDisplayID
= 0;
2730 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2732 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack( RANGED_ATTACK
);
2735 ammoInventoryType
= pItem
->GetProto()->InventoryType
;
2736 if( ammoInventoryType
== INVTYPE_THROWN
)
2737 ammoDisplayID
= pItem
->GetProto()->DisplayInfoID
;
2740 uint32 ammoID
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
2743 ItemPrototype
const *pProto
= objmgr
.GetItemPrototype( ammoID
);
2746 ammoDisplayID
= pProto
->DisplayInfoID
;
2747 ammoInventoryType
= pProto
->InventoryType
;
2750 else if(m_caster
->GetDummyAura(46699)) // Requires No Ammo
2752 ammoDisplayID
= 5996; // normal arrow
2753 ammoInventoryType
= INVTYPE_AMMO
;
2758 // TODO: implement selection ammo data based at ranged weapon stored in equipmodel/equipinfo/equipslot fields
2760 *data
<< uint32(ammoDisplayID
);
2761 *data
<< uint32(ammoInventoryType
);
2764 void Spell::WriteSpellGoTargets( WorldPacket
* data
)
2766 *data
<< (uint8
)m_countOfHit
;
2767 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2768 if ((*ihit
).missCondition
== SPELL_MISS_NONE
) // Add only hits
2769 *data
<< uint64(ihit
->targetGUID
);
2771 for(std::list
<GOTargetInfo
>::iterator ighit
= m_UniqueGOTargetInfo
.begin();ighit
!= m_UniqueGOTargetInfo
.end();++ighit
)
2772 *data
<< uint64(ighit
->targetGUID
); // Always hits
2774 *data
<< (uint8
)m_countOfMiss
;
2775 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2777 if( ihit
->missCondition
!= SPELL_MISS_NONE
) // Add only miss
2779 *data
<< uint64(ihit
->targetGUID
);
2780 *data
<< uint8(ihit
->missCondition
);
2781 if( ihit
->missCondition
== SPELL_MISS_REFLECT
)
2782 *data
<< uint8(ihit
->reflectResult
);
2787 void Spell::SendLogExecute()
2789 Unit
*target
= m_targets
.getUnitTarget() ? m_targets
.getUnitTarget() : m_caster
;
2791 WorldPacket
data(SMSG_SPELLLOGEXECUTE
, (8+4+4+4+4+8));
2793 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
2794 data
.append(m_caster
->GetPackGUID());
2796 data
.append(target
->GetPackGUID());
2798 data
<< uint32(m_spellInfo
->Id
);
2800 data
<< uint32(count1
); // count1 (effect count?)
2801 for(uint32 i
= 0; i
< count1
; ++i
)
2803 data
<< uint32(m_spellInfo
->Effect
[0]); // spell effect
2805 data
<< uint32(count2
); // count2 (target count?)
2806 for(uint32 j
= 0; j
< count2
; ++j
)
2808 switch(m_spellInfo
->Effect
[0])
2810 case SPELL_EFFECT_POWER_DRAIN
:
2811 if(Unit
*unit
= m_targets
.getUnitTarget())
2812 data
.append(unit
->GetPackGUID());
2819 case SPELL_EFFECT_ADD_EXTRA_ATTACKS
:
2820 if(Unit
*unit
= m_targets
.getUnitTarget())
2821 data
.append(unit
->GetPackGUID());
2824 data
<< uint32(0); // count?
2826 case SPELL_EFFECT_INTERRUPT_CAST
:
2827 if(Unit
*unit
= m_targets
.getUnitTarget())
2828 data
.append(unit
->GetPackGUID());
2831 data
<< uint32(0); // spellid
2833 case SPELL_EFFECT_DURABILITY_DAMAGE
:
2834 if(Unit
*unit
= m_targets
.getUnitTarget())
2835 data
.append(unit
->GetPackGUID());
2841 case SPELL_EFFECT_OPEN_LOCK
:
2842 case SPELL_EFFECT_OPEN_LOCK_ITEM
:
2843 if(Item
*item
= m_targets
.getItemTarget())
2844 data
.append(item
->GetPackGUID());
2848 case SPELL_EFFECT_CREATE_ITEM
:
2849 case SPELL_EFFECT_157
:
2850 data
<< uint32(m_spellInfo
->EffectItemType
[0]);
2852 case SPELL_EFFECT_SUMMON
:
2853 case SPELL_EFFECT_TRANS_DOOR
:
2854 case SPELL_EFFECT_SUMMON_PET
:
2855 case SPELL_EFFECT_SUMMON_OBJECT_WILD
:
2856 case SPELL_EFFECT_CREATE_HOUSE
:
2857 case SPELL_EFFECT_DUEL
:
2858 case SPELL_EFFECT_SUMMON_OBJECT_SLOT1
:
2859 case SPELL_EFFECT_SUMMON_OBJECT_SLOT2
:
2860 case SPELL_EFFECT_SUMMON_OBJECT_SLOT3
:
2861 case SPELL_EFFECT_SUMMON_OBJECT_SLOT4
:
2862 if(Unit
*unit
= m_targets
.getUnitTarget())
2863 data
.append(unit
->GetPackGUID());
2864 else if(m_targets
.getItemTargetGUID())
2865 data
.appendPackGUID(m_targets
.getItemTargetGUID());
2866 else if(GameObject
*go
= m_targets
.getGOTarget())
2867 data
.append(go
->GetPackGUID());
2869 data
<< uint8(0); // guid
2871 case SPELL_EFFECT_FEED_PET
:
2872 data
<< uint32(m_targets
.getItemTargetEntry());
2874 case SPELL_EFFECT_DISMISS_PET
:
2875 if(Unit
*unit
= m_targets
.getUnitTarget())
2876 data
.append(unit
->GetPackGUID());
2880 case SPELL_EFFECT_RESURRECT
:
2881 case SPELL_EFFECT_RESURRECT_NEW
:
2882 if(Unit
*unit
= m_targets
.getUnitTarget())
2883 data
.append(unit
->GetPackGUID());
2893 m_caster
->SendMessageToSet(&data
, true);
2896 void Spell::SendInterrupted(uint8 result
)
2898 WorldPacket
data(SMSG_SPELL_FAILURE
, (8+4+1));
2899 data
.append(m_caster
->GetPackGUID());
2900 data
<< uint8(m_cast_count
);
2901 data
<< uint32(m_spellInfo
->Id
);
2902 data
<< uint8(result
);
2903 m_caster
->SendMessageToSet(&data
, true);
2905 data
.Initialize(SMSG_SPELL_FAILED_OTHER
, (8+4));
2906 data
.append(m_caster
->GetPackGUID());
2907 data
<< uint8(m_cast_count
);
2908 data
<< uint32(m_spellInfo
->Id
);
2909 data
<< uint8(result
);
2910 m_caster
->SendMessageToSet(&data
, true);
2913 void Spell::SendChannelUpdate(uint32 time
)
2917 m_caster
->SetUInt64Value(UNIT_FIELD_CHANNEL_OBJECT
,0);
2918 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
,0);
2921 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2924 WorldPacket
data( MSG_CHANNEL_UPDATE
, 8+4 );
2925 data
.append(m_caster
->GetPackGUID());
2926 data
<< uint32(time
);
2928 ((Player
*)m_caster
)->GetSession()->SendPacket( &data
);
2931 void Spell::SendChannelStart(uint32 duration
)
2933 WorldObject
* target
= NULL
;
2935 // select first not resisted target from target list for _0_ effect
2936 if(!m_UniqueTargetInfo
.empty())
2938 for(std::list
<TargetInfo
>::iterator itr
= m_UniqueTargetInfo
.begin();itr
!= m_UniqueTargetInfo
.end();++itr
)
2940 if( (itr
->effectMask
& (1<<0)) && itr
->reflectResult
==SPELL_MISS_NONE
&& itr
->targetGUID
!= m_caster
->GetGUID())
2942 target
= ObjectAccessor::GetUnit(*m_caster
, itr
->targetGUID
);
2947 else if(!m_UniqueGOTargetInfo
.empty())
2949 for(std::list
<GOTargetInfo
>::iterator itr
= m_UniqueGOTargetInfo
.begin();itr
!= m_UniqueGOTargetInfo
.end();++itr
)
2951 if(itr
->effectMask
& (1<<0) )
2953 target
= ObjectAccessor::GetGameObject(*m_caster
, itr
->targetGUID
);
2959 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2961 WorldPacket
data( MSG_CHANNEL_START
, (8+4+4) );
2962 data
.append(m_caster
->GetPackGUID());
2963 data
<< uint32(m_spellInfo
->Id
);
2964 data
<< uint32(duration
);
2966 ((Player
*)m_caster
)->GetSession()->SendPacket( &data
);
2971 m_caster
->SetUInt64Value(UNIT_FIELD_CHANNEL_OBJECT
, target
->GetGUID());
2972 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
, m_spellInfo
->Id
);
2975 void Spell::SendResurrectRequest(Player
* target
)
2977 WorldPacket
data(SMSG_RESURRECT_REQUEST
, (8+4+2+4));
2978 data
<< uint64(m_caster
->GetGUID());
2980 data
<< uint32(count
); // amount of bytes to read
2982 for(uint32 i
= 0; i
< count
; ++i
)
2987 target
->GetSession()->SendPacket(&data
);
2990 void Spell::SendPlaySpellVisual(uint32 SpellID
)
2992 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2995 WorldPacket
data(SMSG_PLAY_SPELL_VISUAL
, 12);
2996 data
<< uint64(m_caster
->GetGUID());
2997 data
<< uint32(SpellID
); // spell visual id?
2998 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
3001 void Spell::TakeCastItem()
3003 if(!m_CastItem
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
3006 // not remove cast item at triggered spell (equipping, weapon damage, etc)
3007 if(m_IsTriggeredSpell
)
3010 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3014 // This code is to avoid a crash
3015 // I'm not sure, if this is really an error, but I guess every item needs a prototype
3016 sLog
.outError("Cast item has no item prototype highId=%d, lowId=%d",m_CastItem
->GetGUIDHigh(), m_CastItem
->GetGUIDLow());
3020 bool expendable
= false;
3021 bool withoutCharges
= false;
3023 for (int i
= 0; i
<5; i
++)
3025 if (proto
->Spells
[i
].SpellId
)
3027 // item has limited charges
3028 if (proto
->Spells
[i
].SpellCharges
)
3030 if (proto
->Spells
[i
].SpellCharges
< 0)
3033 int32 charges
= m_CastItem
->GetSpellCharges(i
);
3035 // item has charges left
3038 (charges
> 0) ? --charges
: ++charges
; // abs(charges) less at 1 after use
3039 if (proto
->Stackable
< 2)
3040 m_CastItem
->SetSpellCharges(i
, charges
);
3041 m_CastItem
->SetState(ITEM_CHANGED
, (Player
*)m_caster
);
3045 withoutCharges
= (charges
== 0);
3050 if (expendable
&& withoutCharges
)
3053 ((Player
*)m_caster
)->DestroyItemCount(m_CastItem
, count
, true);
3055 // prevent crash at access to deleted m_targets.getItemTarget
3056 if(m_CastItem
==m_targets
.getItemTarget())
3057 m_targets
.setItemTarget(NULL
);
3063 void Spell::TakePower()
3065 if(m_CastItem
|| m_triggeredByAuraSpell
)
3068 // health as power used
3069 if(m_spellInfo
->powerType
== POWER_HEALTH
)
3071 m_caster
->ModifyHealth( -(int32
)m_powerCost
);
3075 if(m_spellInfo
->powerType
>= MAX_POWERS
)
3077 sLog
.outError("Spell::TakePower: Unknown power type '%d'", m_spellInfo
->powerType
);
3081 Powers powerType
= Powers(m_spellInfo
->powerType
);
3083 m_caster
->ModifyPower(powerType
, -(int32
)m_powerCost
);
3085 // Set the five second timer
3086 if (powerType
== POWER_MANA
&& m_powerCost
> 0)
3087 m_caster
->SetLastManaUse(getMSTime());
3090 void Spell::TakeReagents()
3092 if(m_IsTriggeredSpell
) // reagents used in triggered spell removed by original spell or don't must be removed.
3095 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
3098 if (m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_NO_REAGENT_WHILE_PREP
&&
3099 m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PREPARATION
))
3102 Player
* p_caster
= (Player
*)m_caster
;
3104 for(uint32 x
=0;x
<8;x
++)
3106 if(m_spellInfo
->Reagent
[x
] <= 0)
3109 uint32 itemid
= m_spellInfo
->Reagent
[x
];
3110 uint32 itemcount
= m_spellInfo
->ReagentCount
[x
];
3112 // if CastItem is also spell reagent
3115 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3116 if( proto
&& proto
->ItemId
== itemid
)
3118 for(int s
=0;s
<5;s
++)
3120 // CastItem will be used up and does not count as reagent
3121 int32 charges
= m_CastItem
->GetSpellCharges(s
);
3122 if (proto
->Spells
[s
].SpellCharges
< 0 && abs(charges
) < 2)
3133 // if getItemTarget is also spell reagent
3134 if (m_targets
.getItemTargetEntry()==itemid
)
3135 m_targets
.setItemTarget(NULL
);
3137 p_caster
->DestroyItemCount(itemid
, itemcount
, true);
3141 void Spell::HandleThreatSpells(uint32 spellId
)
3143 if(!m_targets
.getUnitTarget() || !spellId
)
3146 if(!m_targets
.getUnitTarget()->CanHaveThreatList())
3149 SpellThreatEntry
const *threatSpell
= sSpellThreatStore
.LookupEntry
<SpellThreatEntry
>(spellId
);
3153 m_targets
.getUnitTarget()->AddThreat(m_caster
, float(threatSpell
->threat
));
3155 DEBUG_LOG("Spell %u, rank %u, added an additional %i threat", spellId
, spellmgr
.GetSpellRank(spellId
), threatSpell
->threat
);
3158 void Spell::HandleEffects(Unit
*pUnitTarget
,Item
*pItemTarget
,GameObject
*pGOTarget
,uint32 i
, float DamageMultiplier
)
3160 unitTarget
= pUnitTarget
;
3161 itemTarget
= pItemTarget
;
3162 gameObjTarget
= pGOTarget
;
3164 uint8 eff
= m_spellInfo
->Effect
[i
];
3165 uint32 mechanic
= m_spellInfo
->EffectMechanic
[i
];
3167 damage
= int32(CalculateDamage((uint8
)i
,unitTarget
)*DamageMultiplier
);
3169 sLog
.outDebug( "Spell: Effect : %u", eff
);
3171 //Simply return. Do not display "immune" in red text on client
3172 if(unitTarget
&& unitTarget
->IsImmunedToSpellEffect(eff
, mechanic
))
3175 if(eff
<TOTAL_SPELL_EFFECTS
)
3177 //sLog.outDebug( "WORLD: Spell FX %d < TOTAL_SPELL_EFFECTS ", eff);
3178 (*this.*SpellEffects
[eff
])(i
);
3183 sLog.outDebug( "WORLD: Spell FX %d > TOTAL_SPELL_EFFECTS ", eff);
3185 EffectEnchantItemTmp(i);
3188 sLog.outError("SPELL: unknown effect %u spell id %u\n",
3189 eff, m_spellInfo->Id);
3195 void Spell::TriggerSpell()
3197 for(TriggerSpells::iterator si
=m_TriggerSpells
.begin(); si
!=m_TriggerSpells
.end(); ++si
)
3199 Spell
* spell
= new Spell(m_caster
, (*si
), true, m_originalCasterGUID
, this->m_selfContainer
);
3200 spell
->prepare(&m_targets
); // use original spell original targets
3204 uint8
Spell::CanCast(bool strict
)
3206 // check cooldowns to prevent cheating
3207 if(m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
3209 if(m_triggeredByAuraSpell
)
3210 return SPELL_FAILED_DONT_REPORT
;
3212 return SPELL_FAILED_NOT_READY
;
3215 // only allow triggered spells if at an ended battleground
3216 if( !m_IsTriggeredSpell
&& m_caster
->GetTypeId() == TYPEID_PLAYER
)
3217 if(BattleGround
* bg
= ((Player
*)m_caster
)->GetBattleGround())
3218 if(bg
->GetStatus() == STATUS_WAIT_LEAVE
)
3219 return SPELL_FAILED_DONT_REPORT
;
3221 // only check at first call, Stealth auras are already removed at second call
3222 // for now, ignore triggered spells
3223 if( strict
&& !m_IsTriggeredSpell
)
3225 // Cannot be used in this stance/form
3226 if(uint8 shapeError
= GetErrorAtShapeshiftedCast(m_spellInfo
, m_caster
->m_form
))
3229 if ((m_spellInfo
->Attributes
& SPELL_ATTR_ONLY_STEALTHED
) && !(m_caster
->HasStealthAura()))
3230 return SPELL_FAILED_ONLY_STEALTHED
;
3233 // caster state requirements
3234 if(m_spellInfo
->CasterAuraState
&& !m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraState
)))
3235 return SPELL_FAILED_CASTER_AURASTATE
;
3236 if(m_spellInfo
->CasterAuraStateNot
&& m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraStateNot
)))
3237 return SPELL_FAILED_CASTER_AURASTATE
;
3239 // cancel autorepeat spells if cast start when moving
3240 // (not wand currently autorepeat cast delayed to moving stop anyway in spell update code)
3241 if( m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->isMoving() )
3243 // skip stuck spell to allow use it in falling case and apply spell limitations at movement
3244 if( (!m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_FALLING
) || m_spellInfo
->Effect
[0] != SPELL_EFFECT_STUCK
) &&
3245 (IsAutoRepeat() || (m_spellInfo
->AuraInterruptFlags
& AURA_INTERRUPT_FLAG_NOT_SEATED
) != 0) )
3246 return SPELL_FAILED_MOVING
;
3249 Unit
*target
= m_targets
.getUnitTarget();
3253 // target state requirements (not allowed state), apply to self also
3254 if(m_spellInfo
->TargetAuraStateNot
&& target
->HasAuraState(AuraState(m_spellInfo
->TargetAuraStateNot
)))
3255 return SPELL_FAILED_TARGET_AURASTATE
;
3257 if(target
!= m_caster
)
3259 // target state requirements (apply to non-self only), to allow cast affects to self like Dirty Deeds
3260 if(m_spellInfo
->TargetAuraState
&& !target
->HasAuraState(AuraState(m_spellInfo
->TargetAuraState
)))
3261 return SPELL_FAILED_TARGET_AURASTATE
;
3263 // Not allow casting on flying player
3264 if (target
->isInFlight())
3265 return SPELL_FAILED_BAD_TARGETS
;
3267 if(VMAP::VMapFactory::checkSpellForLoS(m_spellInfo
->Id
) && !m_caster
->IsWithinLOSInMap(target
))
3268 return SPELL_FAILED_LINE_OF_SIGHT
;
3270 // auto selection spell rank implemented in WorldSession::HandleCastSpellOpcode
3271 // this case can be triggered if rank not found (too low-level target for first rank)
3272 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsPassiveSpell(m_spellInfo
->Id
) && !m_CastItem
)
3274 for(int i
=0;i
<3;i
++)
3276 if(IsPositiveEffect(m_spellInfo
->Id
, i
) && m_spellInfo
->Effect
[i
] == SPELL_EFFECT_APPLY_AURA
)
3277 if(target
->getLevel() + 10 < m_spellInfo
->spellLevel
)
3278 return SPELL_FAILED_LOWLEVEL
;
3283 // check pet presents
3284 for(int j
=0;j
<3;j
++)
3286 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_PET
)
3288 target
= m_caster
->GetPet();
3291 if(m_triggeredByAuraSpell
) // not report pet not existence for triggered spells
3292 return SPELL_FAILED_DONT_REPORT
;
3294 return SPELL_FAILED_NO_PET
;
3300 //check creature type
3301 //ignore self casts (including area casts when caster selected as target)
3302 if(target
!= m_caster
)
3304 if(!CheckTargetCreatureType(target
))
3306 if(target
->GetTypeId()==TYPEID_PLAYER
)
3307 return SPELL_FAILED_TARGET_IS_PLAYER
;
3309 return SPELL_FAILED_BAD_TARGETS
;
3313 // TODO: this check can be applied and for player to prevent cheating when IsPositiveSpell will return always correct result.
3314 // check target for pet/charmed casts (not self targeted), self targeted cast used for area effects and etc
3315 if(m_caster
!= target
&& m_caster
->GetTypeId()==TYPEID_UNIT
&& m_caster
->GetCharmerOrOwnerGUID())
3317 // check correctness positive/negative cast target (pet cast real check and cheating check)
3318 if(IsPositiveSpell(m_spellInfo
->Id
))
3320 if(m_caster
->IsHostileTo(target
))
3321 return SPELL_FAILED_BAD_TARGETS
;
3325 if(m_caster
->IsFriendlyTo(target
))
3326 return SPELL_FAILED_BAD_TARGETS
;
3330 if(IsPositiveSpell(m_spellInfo
->Id
))
3332 if(target
->IsImmunedToSpell(m_spellInfo
,false))
3333 return SPELL_FAILED_TARGET_AURASTATE
;
3336 //Must be behind the target.
3337 if( m_spellInfo
->AttributesEx2
== 0x100000 && (m_spellInfo
->AttributesEx
& 0x200) == 0x200 && target
->HasInArc(M_PI
, m_caster
) )
3340 return SPELL_FAILED_NOT_BEHIND
;
3343 //Target must be facing you.
3344 if((m_spellInfo
->Attributes
== 0x150010) && !target
->HasInArc(M_PI
, m_caster
) )
3347 return SPELL_FAILED_NOT_INFRONT
;
3350 // check if target is in combat
3351 if (target
!= m_caster
&& (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_NOT_IN_COMBAT_TARGET
) && target
->isInCombat())
3353 return SPELL_FAILED_TARGET_AFFECTING_COMBAT
;
3356 // Spell casted only on battleground
3357 if((m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_BATTLEGROUND
) && m_caster
->GetTypeId()==TYPEID_PLAYER
)
3358 if(!((Player
*)m_caster
)->InBattleGround())
3359 return SPELL_FAILED_ONLY_BATTLEGROUNDS
;
3361 // do not allow spells to be cast in arenas
3362 // - with greater than 15 min CD without SPELL_ATTR_EX4_USABLE_IN_ARENA flag
3363 // - with SPELL_ATTR_EX4_NOT_USABLE_IN_ARENA flag
3364 if( (m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_NOT_USABLE_IN_ARENA
) ||
3365 GetSpellRecoveryTime(m_spellInfo
) > 15 * MINUTE
* 1000 && !(m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_USABLE_IN_ARENA
) )
3366 if(MapEntry
const* mapEntry
= sMapStore
.LookupEntry(m_caster
->GetMapId()))
3367 if(mapEntry
->IsBattleArena())
3368 return SPELL_FAILED_NOT_IN_ARENA
;
3371 if(!IsSpellAllowedInLocation(m_spellInfo
,m_caster
->GetMapId(),m_caster
->GetZoneId(),m_caster
->GetAreaId()))
3372 return SPELL_FAILED_REQUIRES_AREA
;
3374 // not let players cast spells at mount (and let do it to creatures)
3375 if( m_caster
->IsMounted() && m_caster
->GetTypeId()==TYPEID_PLAYER
&& !m_IsTriggeredSpell
&&
3376 !IsPassiveSpell(m_spellInfo
->Id
) && !(m_spellInfo
->Attributes
& SPELL_ATTR_CASTABLE_WHILE_MOUNTED
) )
3378 if(m_caster
->isInFlight())
3379 return SPELL_FAILED_NOT_FLYING
;
3381 return SPELL_FAILED_NOT_MOUNTED
;
3384 // always (except passive spells) check items (focus object can be required for any type casts)
3385 if(!IsPassiveSpell(m_spellInfo
->Id
))
3386 if(uint8 castResult
= CheckItems())
3389 //ImpliciteTargetA-B = 38, If fact there is 0 Spell with ImpliciteTargetB=38
3390 if(m_UniqueTargetInfo
.empty()) // skip second canCast apply (for delayed spells for example)
3392 for(uint8 j
= 0; j
< 3; j
++)
3394 if( m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT
||
3395 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT
&& m_spellInfo
->EffectImplicitTargetA
[j
] != TARGET_SELF
||
3396 m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3397 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3401 SpellScriptTarget::const_iterator lower
= spellmgr
.GetBeginSpellScriptTarget(m_spellInfo
->Id
);
3402 SpellScriptTarget::const_iterator upper
= spellmgr
.GetEndSpellScriptTarget(m_spellInfo
->Id
);
3404 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
);
3406 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
3407 float range
= GetSpellMaxRange(srange
);
3409 Creature
* creatureScriptTarget
= NULL
;
3410 GameObject
* goScriptTarget
= NULL
;
3412 for(SpellScriptTarget::const_iterator i_spellST
= lower
; i_spellST
!= upper
; ++i_spellST
)
3414 switch(i_spellST
->second
.type
)
3416 case SPELL_TARGET_TYPE_GAMEOBJECT
:
3418 GameObject
* p_GameObject
= NULL
;
3420 if(i_spellST
->second
.targetEntry
)
3422 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
3424 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
3426 MaNGOS::NearestGameObjectEntryInObjectRangeCheck
go_check(*m_caster
,i_spellST
->second
.targetEntry
,range
);
3427 MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
> checker(p_GameObject
,go_check
);
3429 TypeContainerVisitor
<MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
>, GridTypeMapContainer
> object_checker(checker
);
3430 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
3431 cell_lock
->Visit(cell_lock
, object_checker
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
3435 // remember found target and range, next attempt will find more near target with another entry
3436 creatureScriptTarget
= NULL
;
3437 goScriptTarget
= p_GameObject
;
3438 range
= go_check
.GetLastRange();
3441 else if( focusObject
) //Focus Object
3443 float frange
= m_caster
->GetDistance(focusObject
);
3446 creatureScriptTarget
= NULL
;
3447 goScriptTarget
= focusObject
;
3453 case SPELL_TARGET_TYPE_CREATURE
:
3454 case SPELL_TARGET_TYPE_DEAD
:
3457 Creature
*p_Creature
= NULL
;
3459 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
3461 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
3462 cell
.SetNoCreate(); // Really don't know what is that???
3464 MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
u_check(*m_caster
,i_spellST
->second
.targetEntry
,i_spellST
->second
.type
!=SPELL_TARGET_TYPE_DEAD
,range
);
3465 MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
> searcher(p_Creature
, u_check
);
3467 TypeContainerVisitor
<MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
>, GridTypeMapContainer
> grid_creature_searcher(searcher
);
3469 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
3470 cell_lock
->Visit(cell_lock
, grid_creature_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
3474 creatureScriptTarget
= p_Creature
;
3475 goScriptTarget
= NULL
;
3476 range
= u_check
.GetLastRange();
3483 if(creatureScriptTarget
)
3485 // store coordinates for TARGET_SCRIPT_COORDINATES
3486 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3487 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3489 m_targets
.setDestination(creatureScriptTarget
->GetPositionX(),creatureScriptTarget
->GetPositionY(),creatureScriptTarget
->GetPositionZ());
3491 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->EffectImplicitTargetB
[j
] == 0 && m_spellInfo
->Effect
[j
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
3492 AddUnitTarget(creatureScriptTarget
, j
);
3494 // store explicit target for TARGET_SCRIPT
3496 AddUnitTarget(creatureScriptTarget
, j
);
3498 else if(goScriptTarget
)
3500 // store coordinates for TARGET_SCRIPT_COORDINATES
3501 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3502 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3504 m_targets
.setDestination(goScriptTarget
->GetPositionX(),goScriptTarget
->GetPositionY(),goScriptTarget
->GetPositionZ());
3506 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->EffectImplicitTargetB
[j
] == 0 && m_spellInfo
->Effect
[j
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
3507 AddGOTarget(goScriptTarget
, j
);
3509 // store explicit target for TARGET_SCRIPT
3511 AddGOTarget(goScriptTarget
, j
);
3513 //Missing DB Entry or targets for this spellEffect.
3516 // not report target not existence for triggered spells
3517 if(m_triggeredByAuraSpell
|| m_IsTriggeredSpell
)
3518 return SPELL_FAILED_DONT_REPORT
;
3520 return SPELL_FAILED_BAD_TARGETS
;
3526 if(uint8 castResult
= CheckRange(strict
))
3530 if(uint8 castResult
= CheckPower())
3534 if(!m_triggeredByAuraSpell
) // triggered spell not affected by stun/etc
3535 if(uint8 castResult
= CheckCasterAuras())
3538 for (int i
= 0; i
< 3; i
++)
3540 // for effects of spells that have only one target
3541 switch(m_spellInfo
->Effect
[i
])
3543 case SPELL_EFFECT_DUMMY
:
3545 if(m_spellInfo
->SpellIconID
== 1648) // Execute
3547 if(!m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
3548 return SPELL_FAILED_BAD_TARGETS
;
3550 else if (m_spellInfo
->Id
== 51582) // Rocket Boots Engaged
3552 if(m_caster
->IsInWater())
3553 return SPELL_FAILED_ONLY_ABOVEWATER
;
3555 else if(m_spellInfo
->SpellIconID
==156) // Holy Shock
3557 // spell different for friends and enemies
3558 // hart version required facing
3559 if(m_targets
.getUnitTarget() && !m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()) && !m_caster
->HasInArc( M_PI
, target
))
3560 return SPELL_FAILED_UNIT_NOT_INFRONT
;
3564 case SPELL_EFFECT_SCHOOL_DAMAGE
:
3567 if(m_spellInfo
->SpellVisual
[0] == 7250)
3569 if (!m_targets
.getUnitTarget())
3570 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3572 if(m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
3573 return SPELL_FAILED_BAD_TARGETS
;
3577 case SPELL_EFFECT_TAMECREATURE
:
3579 if (!m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetTypeId() == TYPEID_PLAYER
)
3580 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3582 if (m_targets
.getUnitTarget()->getLevel() > m_caster
->getLevel())
3583 return SPELL_FAILED_HIGHLEVEL
;
3585 // use SMSG_PET_TAME_FAILURE?
3586 if (!((Creature
*)m_targets
.getUnitTarget())->GetCreatureInfo()->isTameable ())
3587 return SPELL_FAILED_BAD_TARGETS
;
3589 if(m_caster
->GetPetGUID())
3590 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3592 if(m_caster
->GetCharmGUID())
3593 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3597 case SPELL_EFFECT_LEARN_SPELL
:
3599 if(m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_PET
)
3602 Pet
* pet
= m_caster
->GetPet();
3605 return SPELL_FAILED_NO_PET
;
3607 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
3609 if(!learn_spellproto
)
3610 return SPELL_FAILED_NOT_KNOWN
;
3612 if(m_spellInfo
->spellLevel
> pet
->getLevel())
3613 return SPELL_FAILED_LOWLEVEL
;
3617 case SPELL_EFFECT_LEARN_PET_SPELL
:
3619 Pet
* pet
= m_caster
->GetPet();
3622 return SPELL_FAILED_NO_PET
;
3624 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
3626 if(!learn_spellproto
)
3627 return SPELL_FAILED_NOT_KNOWN
;
3629 if(m_spellInfo
->spellLevel
> pet
->getLevel())
3630 return SPELL_FAILED_LOWLEVEL
;
3634 case SPELL_EFFECT_FEED_PET
:
3636 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| !m_targets
.getItemTarget() )
3637 return SPELL_FAILED_BAD_TARGETS
;
3639 Pet
* pet
= m_caster
->GetPet();
3642 return SPELL_FAILED_NO_PET
;
3644 if(!pet
->HaveInDiet(m_targets
.getItemTarget()->GetProto()))
3645 return SPELL_FAILED_WRONG_PET_FOOD
;
3647 if(!pet
->GetCurrentFoodBenefitLevel(m_targets
.getItemTarget()->GetProto()->ItemLevel
))
3648 return SPELL_FAILED_FOOD_LOWLEVEL
;
3650 if(m_caster
->isInCombat() || pet
->isInCombat())
3651 return SPELL_FAILED_AFFECTING_COMBAT
;
3655 case SPELL_EFFECT_POWER_BURN
:
3656 case SPELL_EFFECT_POWER_DRAIN
:
3658 // Can be area effect, Check only for players and not check if target - caster (spell can have multiply drain/burn effects)
3659 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
3660 if(Unit
* target
= m_targets
.getUnitTarget())
3661 if(target
!=m_caster
&& target
->getPowerType()!=m_spellInfo
->EffectMiscValue
[i
])
3662 return SPELL_FAILED_BAD_TARGETS
;
3665 case SPELL_EFFECT_CHARGE
:
3667 if (m_caster
->hasUnitState(UNIT_STAT_ROOT
))
3668 return SPELL_FAILED_ROOTED
;
3672 case SPELL_EFFECT_SKINNING
:
3674 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| !m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetTypeId() != TYPEID_UNIT
)
3675 return SPELL_FAILED_BAD_TARGETS
;
3677 if( !(m_targets
.getUnitTarget()->GetUInt32Value(UNIT_FIELD_FLAGS
) & UNIT_FLAG_SKINNABLE
) )
3678 return SPELL_FAILED_TARGET_UNSKINNABLE
;
3680 Creature
* creature
= (Creature
*)m_targets
.getUnitTarget();
3681 if ( creature
->GetCreatureType() != CREATURE_TYPE_CRITTER
&& ( !creature
->lootForBody
|| !creature
->loot
.empty() ) )
3683 return SPELL_FAILED_TARGET_NOT_LOOTED
;
3686 uint32 skill
= creature
->GetCreatureInfo()->GetRequiredLootSkill();
3688 int32 skillValue
= ((Player
*)m_caster
)->GetSkillValue(skill
);
3689 int32 TargetLevel
= m_targets
.getUnitTarget()->getLevel();
3690 int32 ReqValue
= (skillValue
< 100 ? (TargetLevel
-10)*10 : TargetLevel
*5);
3691 if (ReqValue
> skillValue
)
3692 return SPELL_FAILED_LOW_CASTLEVEL
;
3694 // chance for fail at orange skinning attempt
3695 if( (m_selfContainer
&& (*m_selfContainer
) == this) &&
3696 skillValue
< sWorld
.GetConfigMaxSkillValue() &&
3697 (ReqValue
< 0 ? 0 : ReqValue
) > irand(skillValue
-25, skillValue
+37) )
3698 return SPELL_FAILED_TRY_AGAIN
;
3702 case SPELL_EFFECT_OPEN_LOCK_ITEM
:
3703 case SPELL_EFFECT_OPEN_LOCK
:
3705 if( m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT
&&
3706 m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT_ITEM
)
3709 if( m_caster
->GetTypeId() != TYPEID_PLAYER
// only players can open locks, gather etc.
3710 // we need a go target in case of TARGET_GAMEOBJECT
3711 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT
&& !m_targets
.getGOTarget()
3712 // we need a go target, or an openable item target in case of TARGET_GAMEOBJECT_ITEM
3713 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT_ITEM
&& !m_targets
.getGOTarget() &&
3714 (!m_targets
.getItemTarget() || !m_targets
.getItemTarget()->GetProto()->LockID
|| m_targets
.getItemTarget()->GetOwner() != m_caster
) )
3715 return SPELL_FAILED_BAD_TARGETS
;
3717 // In BattleGround players can use only flags and banners
3718 if( ((Player
*)m_caster
)->InBattleGround() &&
3719 !((Player
*)m_caster
)->isAllowUseBattleGroundObject() )
3720 return SPELL_FAILED_TRY_AGAIN
;
3722 // get the lock entry
3723 LockEntry
const *lockInfo
= NULL
;
3724 if (GameObject
* go
=m_targets
.getGOTarget())
3725 lockInfo
= sLockStore
.LookupEntry(go
->GetLockId());
3726 else if(Item
* itm
=m_targets
.getItemTarget())
3727 lockInfo
= sLockStore
.LookupEntry(itm
->GetProto()->LockID
);
3729 // check lock compatibility
3732 // check for lock - key pair (checked by client also, just prevent cheating
3733 bool ok_key
= false;
3734 for(int it
= 0; it
< 8; ++it
)
3736 switch(lockInfo
->Type
[it
])
3742 if(lockInfo
->Index
[it
])
3744 if(m_CastItem
&& m_CastItem
->GetEntry()==lockInfo
->Index
[it
])
3749 case LOCK_KEY_SKILL
:
3751 if(uint32(m_spellInfo
->EffectMiscValue
[i
])!=lockInfo
->Index
[it
])
3754 switch(lockInfo
->Index
[it
])
3756 case LOCKTYPE_HERBALISM
:
3757 if(((Player
*)m_caster
)->HasSkill(SKILL_HERBALISM
))
3760 case LOCKTYPE_MINING
:
3761 if(((Player
*)m_caster
)->HasSkill(SKILL_MINING
))
3775 return SPELL_FAILED_BAD_TARGETS
;
3778 // chance for fail at orange mining/herb/LockPicking gathering attempt
3779 if (!m_selfContainer
|| ((*m_selfContainer
) != this))
3782 // get the skill value of the player
3783 int32 SkillValue
= 0;
3784 bool canFailAtMax
= true;
3785 if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_HERBALISM
)
3787 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_HERBALISM
);
3788 canFailAtMax
= false;
3790 else if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_MINING
)
3792 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_MINING
);
3793 canFailAtMax
= false;
3795 else if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_PICKLOCK
)
3796 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_LOCKPICKING
);
3798 // castitem check: rogue using skeleton keys. the skill values should not be added in this case.
3802 // add the damage modifier from the spell casted (cheat lock / skeleton key etc.) (use m_currentBasePoints, CalculateDamage returns wrong value)
3803 SkillValue
+= m_currentBasePoints
[i
]+1;
3805 // get the required lock value
3809 // check for lock - key pair
3811 for(int it
= 0; it
< 8; ++it
)
3813 if(lockInfo
->Type
[it
]==LOCK_KEY_ITEM
&& lockInfo
->Index
[it
] && m_CastItem
&& m_CastItem
->GetEntry()==lockInfo
->Index
[it
])
3815 // if so, we're good to go
3823 if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_PICKLOCK
)
3824 ReqValue
= lockInfo
->Skill
[1];
3826 ReqValue
= lockInfo
->Skill
[0];
3829 // skill doesn't meet the required value
3830 if (ReqValue
> SkillValue
)
3831 return SPELL_FAILED_LOW_CASTLEVEL
;
3833 // chance for failure in orange gather / lockpick (gathering skill can't fail at maxskill)
3834 if((canFailAtMax
|| SkillValue
< sWorld
.GetConfigMaxSkillValue()) && ReqValue
> irand(SkillValue
-25, SkillValue
+37))
3835 return SPELL_FAILED_TRY_AGAIN
;
3839 case SPELL_EFFECT_SUMMON_DEAD_PET
:
3841 Creature
*pet
= m_caster
->GetPet();
3843 return SPELL_FAILED_NO_PET
;
3846 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3850 // This is generic summon effect now and don't make this check for summon types similar
3851 // SPELL_EFFECT_SUMMON_CRITTER, SPELL_EFFECT_SUMMON_WILD or SPELL_EFFECT_SUMMON_GUARDIAN.
3852 // These won't show up in m_caster->GetPetGUID()
3853 case SPELL_EFFECT_SUMMON
:
3855 switch(m_spellInfo
->EffectMiscValueB
[i
])
3857 case SUMMON_TYPE_POSESSED
:
3858 case SUMMON_TYPE_POSESSED2
:
3859 case SUMMON_TYPE_DEMON
:
3860 case SUMMON_TYPE_SUMMON
:
3862 if(m_caster
->GetPetGUID())
3863 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3865 if(m_caster
->GetCharmGUID())
3866 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3872 // Don't make this check for SPELL_EFFECT_SUMMON_CRITTER, SPELL_EFFECT_SUMMON_WILD or SPELL_EFFECT_SUMMON_GUARDIAN.
3873 // These won't show up in m_caster->GetPetGUID()
3874 case SPELL_EFFECT_SUMMON_POSSESSED
:
3875 case SPELL_EFFECT_SUMMON_PHANTASM
:
3876 case SPELL_EFFECT_SUMMON_DEMON
:
3878 if(m_caster
->GetPetGUID())
3879 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3881 if(m_caster
->GetCharmGUID())
3882 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3886 case SPELL_EFFECT_SUMMON_PET
:
3888 if(m_caster
->GetPetGUID()) //let warlock do a replacement summon
3891 Pet
* pet
= ((Player
*)m_caster
)->GetPet();
3893 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& m_caster
->getClass()==CLASS_WARLOCK
)
3895 if (strict
) //starting cast, trigger pet stun (cast by pet so it doesn't attack player)
3896 pet
->CastSpell(pet
, 32752, true, NULL
, NULL
, pet
->GetGUID());
3899 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3902 if(m_caster
->GetCharmGUID())
3903 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3907 case SPELL_EFFECT_SUMMON_PLAYER
:
3909 if(m_caster
->GetTypeId()!=TYPEID_PLAYER
)
3910 return SPELL_FAILED_BAD_TARGETS
;
3911 if(!((Player
*)m_caster
)->GetSelection())
3912 return SPELL_FAILED_BAD_TARGETS
;
3914 Player
* target
= objmgr
.GetPlayer(((Player
*)m_caster
)->GetSelection());
3915 if( !target
|| ((Player
*)m_caster
)==target
|| !target
->IsInSameRaidWith((Player
*)m_caster
) )
3916 return SPELL_FAILED_BAD_TARGETS
;
3918 // check if our map is dungeon
3919 if( sMapStore
.LookupEntry(m_caster
->GetMapId())->IsDungeon() )
3921 InstanceTemplate
const* instance
= ObjectMgr::GetInstanceTemplate(m_caster
->GetMapId());
3923 return SPELL_FAILED_TARGET_NOT_IN_INSTANCE
;
3924 if ( instance
->levelMin
> target
->getLevel() )
3925 return SPELL_FAILED_LOWLEVEL
;
3926 if ( instance
->levelMax
&& instance
->levelMax
< target
->getLevel() )
3927 return SPELL_FAILED_HIGHLEVEL
;
3931 case SPELL_EFFECT_LEAP
:
3932 case SPELL_EFFECT_TELEPORT_UNITS_FACE_CASTER
:
3934 float dis
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[i
]));
3935 float fx
= m_caster
->GetPositionX() + dis
* cos(m_caster
->GetOrientation());
3936 float fy
= m_caster
->GetPositionY() + dis
* sin(m_caster
->GetOrientation());
3937 // teleport a bit above terrain level to avoid falling below it
3938 float fz
= MapManager::Instance().GetBaseMap(m_caster
->GetMapId())->GetHeight(fx
,fy
,m_caster
->GetPositionZ(),true);
3939 if(fz
<= INVALID_HEIGHT
) // note: this also will prevent use effect in instances without vmaps height enabled
3940 return SPELL_FAILED_TRY_AGAIN
;
3942 float caster_pos_z
= m_caster
->GetPositionZ();
3943 // Control the caster to not climb or drop when +-fz > 8
3944 if(!(fz
<=caster_pos_z
+8 && fz
>=caster_pos_z
-8))
3945 return SPELL_FAILED_TRY_AGAIN
;
3947 // not allow use this effect at battleground until battleground start
3948 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
3949 if(BattleGround
const *bg
= ((Player
*)m_caster
)->GetBattleGround())
3950 if(bg
->GetStatus() != STATUS_IN_PROGRESS
)
3951 return SPELL_FAILED_TRY_AGAIN
;
3954 case SPELL_EFFECT_STEAL_BENEFICIAL_BUFF
:
3956 if (m_targets
.getUnitTarget()==m_caster
)
3957 return SPELL_FAILED_BAD_TARGETS
;
3964 for (int i
= 0; i
< 3; i
++)
3966 switch(m_spellInfo
->EffectApplyAuraName
[i
])
3968 case SPELL_AURA_MOD_POSSESS
:
3969 case SPELL_AURA_MOD_CHARM
:
3971 if(m_caster
->GetPetGUID())
3972 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3974 if(m_caster
->GetCharmGUID())
3975 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3977 if(m_caster
->GetCharmerGUID())
3978 return SPELL_FAILED_CHARMED
;
3980 if(!m_targets
.getUnitTarget())
3981 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3983 if(m_targets
.getUnitTarget()->GetCharmerGUID())
3984 return SPELL_FAILED_CHARMED
;
3986 if(int32(m_targets
.getUnitTarget()->getLevel()) > CalculateDamage(i
,m_targets
.getUnitTarget()))
3987 return SPELL_FAILED_HIGHLEVEL
;
3989 case SPELL_AURA_MOUNTED
:
3991 if (m_caster
->IsInWater())
3992 return SPELL_FAILED_ONLY_ABOVEWATER
;
3994 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->GetTransport())
3995 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
3997 // Ignore map check if spell have AreaId. AreaId already checked and this prevent special mount spells
3998 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& !sMapStore
.LookupEntry(m_caster
->GetMapId())->IsMountAllowed() && !m_IsTriggeredSpell
&& !m_spellInfo
->AreaId
)
3999 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4001 if (m_caster
->GetAreaId()==35)
4002 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4004 ShapeshiftForm form
= m_caster
->m_form
;
4005 if( form
== FORM_CAT
|| form
== FORM_TREE
|| form
== FORM_TRAVEL
||
4006 form
== FORM_AQUA
|| form
== FORM_BEAR
|| form
== FORM_DIREBEAR
||
4007 form
== FORM_CREATUREBEAR
|| form
== FORM_GHOSTWOLF
|| form
== FORM_FLIGHT
||
4008 form
== FORM_FLIGHT_EPIC
|| form
== FORM_MOONKIN
)
4009 return SPELL_FAILED_NOT_SHAPESHIFT
;
4013 case SPELL_AURA_RANGED_ATTACK_POWER_ATTACKER_BONUS
:
4015 if(!m_targets
.getUnitTarget())
4016 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4018 // can be casted at non-friendly unit or own pet/charm
4019 if(m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()))
4020 return SPELL_FAILED_TARGET_FRIENDLY
;
4022 case SPELL_AURA_MOD_INCREASE_FLIGHT_SPEED
:
4023 case SPELL_AURA_FLY
:
4025 // not allow cast fly spells at old maps by players (all spells is self target)
4026 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
4028 if( !((Player
*)m_caster
)->isGameMaster() &&
4029 GetVirtualMapForMapAndZone(m_caster
->GetMapId(),m_caster
->GetZoneId()) != 530)
4030 return SPELL_FAILED_NOT_HERE
;
4033 case SPELL_AURA_PERIODIC_MANA_LEECH
:
4035 if (!m_targets
.getUnitTarget())
4036 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4038 if (m_caster
->GetTypeId()!=TYPEID_PLAYER
|| m_CastItem
)
4041 if(m_targets
.getUnitTarget()->getPowerType()!=POWER_MANA
)
4042 return SPELL_FAILED_BAD_TARGETS
;
4053 int16
Spell::PetCanCast(Unit
* target
)
4055 if(!m_caster
->isAlive())
4056 return SPELL_FAILED_CASTER_DEAD
;
4058 if(m_caster
->IsNonMeleeSpellCasted(false)) //prevent spellcast interuption by another spellcast
4059 return SPELL_FAILED_SPELL_IN_PROGRESS
;
4060 if(m_caster
->isInCombat() && IsNonCombatSpell(m_spellInfo
))
4061 return SPELL_FAILED_AFFECTING_COMBAT
;
4063 if(m_caster
->GetTypeId()==TYPEID_UNIT
&& (((Creature
*)m_caster
)->isPet() || m_caster
->isCharmed()))
4065 //dead owner (pets still alive when owners ressed?)
4066 if(m_caster
->GetCharmerOrOwner() && !m_caster
->GetCharmerOrOwner()->isAlive())
4067 return SPELL_FAILED_CASTER_DEAD
;
4069 if(!target
&& m_targets
.getUnitTarget())
4070 target
= m_targets
.getUnitTarget();
4073 for(uint32 i
= 0;i
<3;i
++)
4075 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
)
4079 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4084 m_targets
.setUnitTarget(target
);
4086 Unit
* _target
= m_targets
.getUnitTarget();
4088 if(_target
) //for target dead/target not valid
4090 if(!_target
->isAlive())
4091 return SPELL_FAILED_BAD_TARGETS
;
4093 if(IsPositiveSpell(m_spellInfo
->Id
))
4095 if(m_caster
->IsHostileTo(_target
))
4096 return SPELL_FAILED_BAD_TARGETS
;
4100 bool duelvsplayertar
= false;
4101 for(int j
=0;j
<3;j
++)
4103 //TARGET_DUELVSPLAYER is positive AND negative
4104 duelvsplayertar
|= (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_DUELVSPLAYER
);
4106 if(m_caster
->IsFriendlyTo(target
) && !duelvsplayertar
)
4108 return SPELL_FAILED_BAD_TARGETS
;
4113 if(((Creature
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
4114 return SPELL_FAILED_NOT_READY
;
4117 uint16 result
= CanCast(true);
4121 return -1; //this allows to check spell fail 0, in combat
4124 uint8
Spell::CheckCasterAuras() const
4126 // Flag drop spells totally immuned to caster auras
4127 // FIXME: find more nice check for all totally immuned spells
4128 // AttributesEx3 & 0x10000000?
4129 if(m_spellInfo
->Id
==23336 || m_spellInfo
->Id
==23334 || m_spellInfo
->Id
==34991)
4132 uint8 school_immune
= 0;
4133 uint32 mechanic_immune
= 0;
4134 uint32 dispel_immune
= 0;
4136 //Check if the spell grants school or mechanic immunity.
4137 //We use bitmasks so the loop is done only once and not on every aura check below.
4138 if ( m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DISPEL_AURAS_ON_IMMUNITY
)
4140 for(int i
= 0;i
< 3; i
++)
4142 if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_SCHOOL_IMMUNITY
)
4143 school_immune
|= uint32(m_spellInfo
->EffectMiscValue
[i
]);
4144 else if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_MECHANIC_IMMUNITY
)
4145 mechanic_immune
|= 1 << uint32(m_spellInfo
->EffectMiscValue
[i
]);
4146 else if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_DISPEL_IMMUNITY
)
4147 dispel_immune
|= GetDispellMask(DispelType(m_spellInfo
->EffectMiscValue
[i
]));
4149 //immune movement impairement and loss of control
4150 if(m_spellInfo
->Id
==(uint32
)42292)
4151 mechanic_immune
= IMMUNE_TO_MOVEMENT_IMPAIRMENT_AND_LOSS_CONTROL_MASK
;
4154 //Check whether the cast should be prevented by any state you might have.
4155 uint8 prevented_reason
= 0;
4156 // Have to check if there is a stun aura. Otherwise will have problems with ghost aura apply while logging out
4157 if(!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
) && m_caster
->HasAuraType(SPELL_AURA_MOD_STUN
))
4158 prevented_reason
= SPELL_FAILED_STUNNED
;
4159 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_CONFUSED
) && !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
4160 prevented_reason
= SPELL_FAILED_CONFUSED
;
4161 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_FLEEING
) && !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
4162 prevented_reason
= SPELL_FAILED_FLEEING
;
4163 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_SILENCED
) && m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_SILENCE
)
4164 prevented_reason
= SPELL_FAILED_SILENCED
;
4165 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PACIFIED
) && m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_PACIFY
)
4166 prevented_reason
= SPELL_FAILED_PACIFIED
;
4168 // Attr must make flag drop spell totally immune from all effects
4169 if(prevented_reason
)
4171 if(school_immune
|| mechanic_immune
|| dispel_immune
)
4173 //Checking auras is needed now, because you are prevented by some state but the spell grants immunity.
4174 Unit::AuraMap
const& auras
= m_caster
->GetAuras();
4175 for(Unit::AuraMap::const_iterator itr
= auras
.begin(); itr
!= auras
.end(); itr
++)
4179 if( GetSpellMechanicMask(itr
->second
->GetSpellProto(), itr
->second
->GetEffIndex()) & mechanic_immune
)
4181 if( GetSpellSchoolMask(itr
->second
->GetSpellProto()) & school_immune
)
4183 if( (1<<(itr
->second
->GetSpellProto()->Dispel
)) & dispel_immune
)
4186 //Make a second check for spell failed so the right SPELL_FAILED message is returned.
4187 //That is needed when your casting is prevented by multiple states and you are only immune to some of them.
4188 switch(itr
->second
->GetModifier()->m_auraname
)
4190 case SPELL_AURA_MOD_STUN
:
4191 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
))
4192 return SPELL_FAILED_STUNNED
;
4194 case SPELL_AURA_MOD_CONFUSE
:
4195 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
4196 return SPELL_FAILED_CONFUSED
;
4198 case SPELL_AURA_MOD_FEAR
:
4199 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
4200 return SPELL_FAILED_FLEEING
;
4202 case SPELL_AURA_MOD_SILENCE
:
4203 case SPELL_AURA_MOD_PACIFY
:
4204 case SPELL_AURA_MOD_PACIFY_SILENCE
:
4205 if( m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_PACIFY
)
4206 return SPELL_FAILED_PACIFIED
;
4207 else if ( m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_SILENCE
)
4208 return SPELL_FAILED_SILENCED
;
4214 //You are prevented from casting and the spell casted does not grant immunity. Return a failed error.
4216 return prevented_reason
;
4221 bool Spell::CanAutoCast(Unit
* target
)
4223 uint64 targetguid
= target
->GetGUID();
4225 for(uint32 j
= 0;j
<3;j
++)
4227 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_APPLY_AURA
)
4229 if( m_spellInfo
->StackAmount
<= 1)
4231 if( target
->HasAura(m_spellInfo
->Id
, j
) )
4236 if( target
->GetAuras().count(Unit::spellEffectPair(m_spellInfo
->Id
, j
)) >= m_spellInfo
->StackAmount
)
4240 else if ( IsAreaAuraEffect( m_spellInfo
->Effect
[j
] ))
4242 if( target
->HasAura(m_spellInfo
->Id
, j
) )
4247 int16 result
= PetCanCast(target
);
4249 if(result
== -1 || result
== SPELL_FAILED_UNIT_NOT_INFRONT
)
4252 //check if among target units, our WANTED target is as well (->only self cast spells return false)
4253 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
4254 if( ihit
->targetGUID
== targetguid
)
4257 return false; //target invalid
4260 uint8
Spell::CheckRange(bool strict
)
4264 // self cast doesn't need range checking -- also for Starshards fix
4265 if (m_spellInfo
->rangeIndex
== 1) return 0;
4267 if (strict
) //add radius of caster
4269 else //add radius of caster and ~5 yds "give"
4272 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
4273 float max_range
= GetSpellMaxRange(srange
) + range_mod
;
4274 float min_range
= GetSpellMinRange(srange
);
4276 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
4277 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_RANGE
, max_range
, this);
4279 Unit
*target
= m_targets
.getUnitTarget();
4281 if(target
&& target
!= m_caster
)
4283 // distance from target center in checks
4284 float dist
= m_caster
->GetDistance(target
->GetPositionX(),target
->GetPositionY(),target
->GetPositionZ());
4285 if(dist
> max_range
)
4286 return SPELL_FAILED_OUT_OF_RANGE
; //0x5A;
4287 if(dist
< min_range
)
4288 return SPELL_FAILED_TOO_CLOSE
;
4289 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&&
4290 (m_spellInfo
->FacingCasterFlags
& SPELL_FACING_FLAG_INFRONT
) && !m_caster
->HasInArc( M_PI
, target
) )
4291 return SPELL_FAILED_UNIT_NOT_INFRONT
;
4294 if(m_targets
.m_targetMask
== TARGET_FLAG_DEST_LOCATION
&& m_targets
.m_destX
!= 0 && m_targets
.m_destY
!= 0 && m_targets
.m_destZ
!= 0)
4296 float dist
= m_caster
->GetDistance(m_targets
.m_destX
, m_targets
.m_destY
, m_targets
.m_destZ
);
4297 if(dist
> max_range
)
4298 return SPELL_FAILED_OUT_OF_RANGE
;
4299 if(dist
< min_range
)
4300 return SPELL_FAILED_TOO_CLOSE
;
4306 int32
Spell::CalculatePowerCost()
4308 // item cast not used power
4312 // Spell drain all exist power on cast (Only paladin lay of Hands)
4313 if (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DRAIN_ALL_POWER
)
4315 // If power type - health drain all
4316 if (m_spellInfo
->powerType
== POWER_HEALTH
)
4317 return m_caster
->GetHealth();
4318 // Else drain all power
4319 if (m_spellInfo
->powerType
< MAX_POWERS
)
4320 return m_caster
->GetPower(Powers(m_spellInfo
->powerType
));
4321 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
4326 int32 powerCost
= m_spellInfo
->manaCost
;
4327 // PCT cost from total amount
4328 if (m_spellInfo
->ManaCostPercentage
)
4330 switch (m_spellInfo
->powerType
)
4332 // health as power used
4334 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateHealth() / 100;
4337 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateMana() / 100;
4342 case POWER_HAPPINESS
:
4343 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetMaxPower(Powers(m_spellInfo
->powerType
)) / 100;
4346 case POWER_RUNIC_POWER
:
4347 sLog
.outDebug("Spell::CalculateManaCost: Not implemented yet!");
4350 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
4354 SpellSchools school
= GetFirstSchoolInMask(m_spellSchoolMask
);
4355 // Flat mod from caster auras by spell school
4356 powerCost
+= m_caster
->GetInt32Value(UNIT_FIELD_POWER_COST_MODIFIER
+ school
);
4357 // Shiv - costs 20 + weaponSpeed*10 energy (apply only to non-triggered spell with energy cost)
4358 if ( m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_SPELL_VS_EXTEND_COST
)
4359 powerCost
+= m_caster
->GetAttackTime(OFF_ATTACK
)/100;
4360 // Apply cost mod by spell
4361 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
4362 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COST
, powerCost
, this);
4364 if(m_spellInfo
->Attributes
& SPELL_ATTR_LEVEL_DAMAGE_CALCULATION
)
4365 powerCost
= int32(powerCost
/ (1.117f
* m_spellInfo
->spellLevel
/ m_caster
->getLevel() -0.1327f
));
4367 // PCT mod from user auras by school
4368 powerCost
= int32(powerCost
* (1.0f
+m_caster
->GetFloatValue(UNIT_FIELD_POWER_COST_MULTIPLIER
+school
)));
4374 uint8
Spell::CheckPower()
4376 // item cast not used power
4380 // health as power used - need check health amount
4381 if(m_spellInfo
->powerType
== POWER_HEALTH
)
4383 if(m_caster
->GetHealth() <= m_powerCost
)
4384 return SPELL_FAILED_CASTER_AURASTATE
;
4387 // Check valid power type
4388 if( m_spellInfo
->powerType
>= MAX_POWERS
)
4390 sLog
.outError("Spell::CheckMana: Unknown power type '%d'", m_spellInfo
->powerType
);
4391 return SPELL_FAILED_UNKNOWN
;
4393 // Check power amount
4394 Powers powerType
= Powers(m_spellInfo
->powerType
);
4395 if(m_caster
->GetPower(powerType
) < m_powerCost
)
4396 return SPELL_FAILED_NO_POWER
;
4401 uint8
Spell::CheckItems()
4403 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
4406 uint32 itemid
, itemcount
;
4407 Player
* p_caster
= (Player
*)m_caster
;
4411 itemid
= m_CastItem
->GetEntry();
4412 if( !p_caster
->HasItemCount(itemid
,1) )
4413 return SPELL_FAILED_ITEM_NOT_READY
;
4416 ItemPrototype
const *proto
= m_CastItem
->GetProto();
4418 return SPELL_FAILED_ITEM_NOT_READY
;
4420 for (int i
= 0; i
<5; i
++)
4422 if (proto
->Spells
[i
].SpellCharges
)
4424 if(m_CastItem
->GetSpellCharges(i
)==0)
4425 return SPELL_FAILED_NO_CHARGES_REMAIN
;
4429 uint32 ItemClass
= proto
->Class
;
4430 if (ItemClass
== ITEM_CLASS_CONSUMABLE
&& m_targets
.getUnitTarget())
4432 for (int i
= 0; i
< 3; i
++)
4434 // skip check, pet not required like checks, and for TARGET_PET m_targets.getUnitTarget() is not the real target but the caster
4435 if (m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_PET
)
4438 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_HEAL
)
4439 if (m_targets
.getUnitTarget()->GetHealth() == m_targets
.getUnitTarget()->GetMaxHealth())
4440 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_HEALTH
;
4442 // Mana Potion, Rage Potion, Thistle Tea(Rogue), ...
4443 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_ENERGIZE
)
4445 if(m_spellInfo
->EffectMiscValue
[i
] < 0 || m_spellInfo
->EffectMiscValue
[i
] >= MAX_POWERS
)
4446 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_POWER
;
4448 Powers power
= Powers(m_spellInfo
->EffectMiscValue
[i
]);
4450 if (m_targets
.getUnitTarget()->GetPower(power
) == m_targets
.getUnitTarget()->GetMaxPower(power
))
4451 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_POWER
;
4458 if(m_targets
.getItemTargetGUID())
4460 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
4461 return SPELL_FAILED_BAD_TARGETS
;
4463 if(!m_targets
.getItemTarget())
4464 return SPELL_FAILED_ITEM_GONE
;
4466 if(!m_targets
.getItemTarget()->IsFitToSpellRequirements(m_spellInfo
))
4467 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
4469 // if not item target then required item must be equipped
4472 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& !((Player
*)m_caster
)->HasItemFitToSpellReqirements(m_spellInfo
))
4473 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
4476 if(m_spellInfo
->RequiresSpellFocus
)
4478 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
4480 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
4482 GameObject
* ok
= NULL
;
4483 MaNGOS::GameObjectFocusCheck
go_check(m_caster
,m_spellInfo
->RequiresSpellFocus
);
4484 MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
> checker(ok
,go_check
);
4486 TypeContainerVisitor
<MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
>, GridTypeMapContainer
> object_checker(checker
);
4487 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
4488 cell_lock
->Visit(cell_lock
, object_checker
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
4491 return (uint8
)SPELL_FAILED_REQUIRES_SPELL_FOCUS
;
4493 focusObject
= ok
; // game object found in range
4496 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_NO_REAGENT_WHILE_PREP
&&
4497 m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PREPARATION
)))
4499 for(uint32 i
=0;i
<8;i
++)
4501 if(m_spellInfo
->Reagent
[i
] <= 0)
4504 itemid
= m_spellInfo
->Reagent
[i
];
4505 itemcount
= m_spellInfo
->ReagentCount
[i
];
4507 // if CastItem is also spell reagent
4508 if( m_CastItem
&& m_CastItem
->GetEntry() == itemid
)
4510 ItemPrototype
const *proto
= m_CastItem
->GetProto();
4512 return SPELL_FAILED_ITEM_NOT_READY
;
4513 for(int s
=0;s
<5;s
++)
4515 // CastItem will be used up and does not count as reagent
4516 int32 charges
= m_CastItem
->GetSpellCharges(s
);
4517 if (proto
->Spells
[s
].SpellCharges
< 0 && abs(charges
) < 2)
4524 if( !p_caster
->HasItemCount(itemid
,itemcount
) )
4525 return (uint8
)SPELL_FAILED_ITEM_NOT_READY
; //0x54
4530 for(int i
=0;i
<2;++i
)
4532 if(m_spellInfo
->Totem
[i
] != 0)
4534 if( p_caster
->HasItemCount(m_spellInfo
->Totem
[i
],1) )
4543 return (uint8
)SPELL_FAILED_TOTEMS
; //0x7C
4545 //Check items for TotemCategory
4546 uint32 TotemCategory
= 2;
4547 for(int i
=0;i
<2;++i
)
4549 if(m_spellInfo
->TotemCategory
[i
] != 0)
4551 if( p_caster
->HasItemTotemCategory(m_spellInfo
->TotemCategory
[i
]) )
4560 if(TotemCategory
!= 0)
4561 return (uint8
)SPELL_FAILED_TOTEM_CATEGORY
; //0x7B
4563 for(int i
= 0; i
< 3; i
++)
4565 switch (m_spellInfo
->Effect
[i
])
4567 case SPELL_EFFECT_CREATE_ITEM
:
4569 if (!m_IsTriggeredSpell
&& m_spellInfo
->EffectItemType
[i
])
4571 ItemPosCountVec dest
;
4572 uint8 msg
= p_caster
->CanStoreNewItem(NULL_BAG
, NULL_SLOT
, dest
, m_spellInfo
->EffectItemType
[i
], 1 );
4573 if (msg
!= EQUIP_ERR_OK
)
4575 p_caster
->SendEquipError( msg
, NULL
, NULL
);
4576 return SPELL_FAILED_DONT_REPORT
;
4581 case SPELL_EFFECT_ENCHANT_ITEM
:
4583 Item
* targetItem
= m_targets
.getItemTarget();
4585 return SPELL_FAILED_ITEM_NOT_FOUND
;
4587 if( targetItem
->GetProto()->ItemLevel
< m_spellInfo
->baseLevel
)
4588 return SPELL_FAILED_LOWLEVEL
;
4589 // Not allow enchant in trade slot for some enchant type
4590 if( targetItem
->GetOwner() != m_caster
)
4592 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
4593 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
4595 return SPELL_FAILED_ERROR
;
4596 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
4597 return SPELL_FAILED_NOT_TRADEABLE
;
4601 case SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY
:
4603 Item
*item
= m_targets
.getItemTarget();
4605 return SPELL_FAILED_ITEM_NOT_FOUND
;
4606 // Not allow enchant in trade slot for some enchant type
4607 if( item
->GetOwner() != m_caster
)
4609 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
4610 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
4612 return SPELL_FAILED_ERROR
;
4613 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
4614 return SPELL_FAILED_NOT_TRADEABLE
;
4618 case SPELL_EFFECT_ENCHANT_HELD_ITEM
:
4619 // check item existence in effect code (not output errors at offhand hold item effect to main hand for example
4621 case SPELL_EFFECT_DISENCHANT
:
4623 if(!m_targets
.getItemTarget())
4624 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4626 // prevent disenchanting in trade slot
4627 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4628 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4630 ItemPrototype
const* itemProto
= m_targets
.getItemTarget()->GetProto();
4632 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4634 uint32 item_quality
= itemProto
->Quality
;
4635 // 2.0.x addon: Check player enchanting level against the item desenchanting requirements
4636 uint32 item_disenchantskilllevel
= itemProto
->RequiredDisenchantSkill
;
4637 if (item_disenchantskilllevel
== uint32(-1))
4638 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4639 if (item_disenchantskilllevel
> p_caster
->GetSkillValue(SKILL_ENCHANTING
))
4640 return SPELL_FAILED_LOW_CASTLEVEL
;
4641 if(item_quality
> 4 || item_quality
< 2)
4642 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4643 if(itemProto
->Class
!= ITEM_CLASS_WEAPON
&& itemProto
->Class
!= ITEM_CLASS_ARMOR
)
4644 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4645 if (!itemProto
->DisenchantID
)
4646 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4649 case SPELL_EFFECT_PROSPECTING
:
4651 if(!m_targets
.getItemTarget())
4652 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4653 //ensure item is a prospectable ore
4654 if(!(m_targets
.getItemTarget()->GetProto()->BagFamily
& BAG_FAMILY_MASK_MINING_SUPP
) || m_targets
.getItemTarget()->GetProto()->Class
!= ITEM_CLASS_TRADE_GOODS
)
4655 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4656 //prevent prospecting in trade slot
4657 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4658 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4659 //Check for enough skill in jewelcrafting
4660 uint32 item_prospectingskilllevel
= m_targets
.getItemTarget()->GetProto()->RequiredSkillRank
;
4661 if(item_prospectingskilllevel
>p_caster
->GetSkillValue(SKILL_JEWELCRAFTING
))
4662 return SPELL_FAILED_LOW_CASTLEVEL
;
4663 //make sure the player has the required ores in inventory
4664 if(m_targets
.getItemTarget()->GetCount() < 5)
4665 return SPELL_FAILED_NEED_MORE_ITEMS
;
4667 if(!LootTemplates_Prospecting
.HaveLootFor(m_targets
.getItemTargetEntry()))
4668 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4672 case SPELL_EFFECT_WEAPON_DAMAGE
:
4673 case SPELL_EFFECT_WEAPON_DAMAGE_NOSCHOOL
:
4675 if(m_caster
->GetTypeId() != TYPEID_PLAYER
) return SPELL_FAILED_TARGET_NOT_PLAYER
;
4676 if( m_attackType
!= RANGED_ATTACK
)
4678 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack(m_attackType
);
4679 if(!pItem
|| pItem
->IsBroken())
4680 return SPELL_FAILED_EQUIPPED_ITEM
;
4682 switch(pItem
->GetProto()->SubClass
)
4684 case ITEM_SUBCLASS_WEAPON_THROWN
:
4686 uint32 ammo
= pItem
->GetEntry();
4687 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
4688 return SPELL_FAILED_NO_AMMO
;
4690 case ITEM_SUBCLASS_WEAPON_GUN
:
4691 case ITEM_SUBCLASS_WEAPON_BOW
:
4692 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
4694 uint32 ammo
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
4698 if(m_caster
->GetDummyAura(46699))
4699 break; // skip other checks
4701 return SPELL_FAILED_NO_AMMO
;
4704 ItemPrototype
const *ammoProto
= objmgr
.GetItemPrototype( ammo
);
4706 return SPELL_FAILED_NO_AMMO
;
4708 if(ammoProto
->Class
!= ITEM_CLASS_PROJECTILE
)
4709 return SPELL_FAILED_NO_AMMO
;
4711 // check ammo ws. weapon compatibility
4712 switch(pItem
->GetProto()->SubClass
)
4714 case ITEM_SUBCLASS_WEAPON_BOW
:
4715 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
4716 if(ammoProto
->SubClass
!=ITEM_SUBCLASS_ARROW
)
4717 return SPELL_FAILED_NO_AMMO
;
4719 case ITEM_SUBCLASS_WEAPON_GUN
:
4720 if(ammoProto
->SubClass
!=ITEM_SUBCLASS_BULLET
)
4721 return SPELL_FAILED_NO_AMMO
;
4724 return SPELL_FAILED_NO_AMMO
;
4727 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
4728 return SPELL_FAILED_NO_AMMO
;
4730 case ITEM_SUBCLASS_WEAPON_WAND
:
4743 void Spell::Delayed()
4745 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
4748 if (m_spellState
== SPELL_STATE_DELAYED
)
4749 return; // spell is active and can't be time-backed
4751 // spells not loosing casting time ( slam, dynamites, bombs.. )
4752 if(!(m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_DAMAGE
))
4755 //check resist chance
4756 int32 resistChance
= 100; //must be initialized to 100 for percent modifiers
4757 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
,SPELLMOD_NOT_LOSE_CASTING_TIME
,resistChance
, this);
4758 resistChance
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_RESIST_PUSHBACK
) - 100;
4759 if (roll_chance_i(resistChance
))
4762 int32 delaytime
= GetNextDelayAtDamageMsTime();
4764 if(int32(m_timer
) + delaytime
> m_casttime
)
4766 delaytime
= m_casttime
- m_timer
;
4767 m_timer
= m_casttime
;
4770 m_timer
+= delaytime
;
4772 sLog
.outDetail("Spell %u partially interrupted for (%d) ms at damage",m_spellInfo
->Id
,delaytime
);
4774 WorldPacket
data(SMSG_SPELL_DELAYED
, 8+4);
4775 data
.append(m_caster
->GetPackGUID());
4776 data
<< uint32(delaytime
);
4778 m_caster
->SendMessageToSet(&data
,true);
4781 void Spell::DelayedChannel()
4783 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
|| getState() != SPELL_STATE_CASTING
)
4786 //check resist chance
4787 int32 resistChance
= 100; //must be initialized to 100 for percent modifiers
4788 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
,SPELLMOD_NOT_LOSE_CASTING_TIME
,resistChance
, this);
4789 resistChance
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_RESIST_PUSHBACK
) - 100;
4790 if (roll_chance_i(resistChance
))
4793 int32 delaytime
= GetNextDelayAtDamageMsTime();
4795 if(int32(m_timer
) < delaytime
)
4797 delaytime
= m_timer
;
4801 m_timer
-= delaytime
;
4803 sLog
.outDebug("Spell %u partially interrupted for %i ms, new duration: %u ms", m_spellInfo
->Id
, delaytime
, m_timer
);
4805 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
4807 if ((*ihit
).missCondition
== SPELL_MISS_NONE
)
4809 Unit
* unit
= m_caster
->GetGUID()==ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
4812 for (int j
=0;j
<3;j
++)
4813 if( ihit
->effectMask
& (1<<j
) )
4814 unit
->DelayAura(m_spellInfo
->Id
, j
, delaytime
);
4820 for(int j
= 0; j
< 3; j
++)
4822 // partially interrupt persistent area auras
4823 DynamicObject
* dynObj
= m_caster
->GetDynObject(m_spellInfo
->Id
, j
);
4825 dynObj
->Delay(delaytime
);
4828 SendChannelUpdate(m_timer
);
4831 void Spell::UpdatePointers()
4833 if(m_originalCasterGUID
==m_caster
->GetGUID())
4834 m_originalCaster
= m_caster
;
4837 m_originalCaster
= ObjectAccessor::GetUnit(*m_caster
,m_originalCasterGUID
);
4838 if(m_originalCaster
&& !m_originalCaster
->IsInWorld()) m_originalCaster
= NULL
;
4841 m_targets
.Update(m_caster
);
4844 bool Spell::IsAffectedBy(SpellEntry
const *spellInfo
, uint32 effectId
)
4846 return spellmgr
.IsAffectedBySpell(m_spellInfo
,spellInfo
->Id
,effectId
,spellInfo
->EffectItemType
[effectId
]);
4849 bool Spell::CheckTargetCreatureType(Unit
* target
) const
4851 uint32 spellCreatureTargetMask
= m_spellInfo
->TargetCreatureType
;
4853 // Curse of Doom : not find another way to fix spell target check :/
4854 if(m_spellInfo
->SpellFamilyName
==SPELLFAMILY_WARLOCK
&& m_spellInfo
->SpellFamilyFlags
== 0x0200000000LL
)
4856 // not allow cast at player
4857 if(target
->GetTypeId()==TYPEID_PLAYER
)
4860 spellCreatureTargetMask
= 0x7FF;
4863 // Dismiss Pet and Taming Lesson skipped
4864 if(m_spellInfo
->Id
== 2641 || m_spellInfo
->Id
== 23356)
4865 spellCreatureTargetMask
= 0;
4867 if (spellCreatureTargetMask
)
4869 uint32 TargetCreatureType
= target
->GetCreatureTypeMask();
4871 return !TargetCreatureType
|| (spellCreatureTargetMask
& TargetCreatureType
);
4876 CurrentSpellTypes
Spell::GetCurrentContainer()
4878 if (IsNextMeleeSwingSpell())
4879 return(CURRENT_MELEE_SPELL
);
4880 else if (IsAutoRepeat())
4881 return(CURRENT_AUTOREPEAT_SPELL
);
4882 else if (IsChanneledSpell(m_spellInfo
))
4883 return(CURRENT_CHANNELED_SPELL
);
4885 return(CURRENT_GENERIC_SPELL
);
4888 bool Spell::CheckTarget( Unit
* target
, uint32 eff
, bool hitPhase
)
4890 // Check targets for creature type mask and remove not appropriate (skip explicit self target case, maybe need other explicit targets)
4891 if(m_spellInfo
->EffectImplicitTargetA
[eff
]!=TARGET_SELF
)
4893 if (!CheckTargetCreatureType(target
))
4897 // Check targets for not_selectable unit flag and remove
4898 // A player can cast spells on his pet (or other controlled unit) though in any state
4899 if (target
!= m_caster
&& target
->GetCharmerOrOwnerGUID() != m_caster
->GetGUID())
4901 // any unattackable target skipped
4902 if (target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NON_ATTACKABLE
))
4905 // unselectable targets skipped in all cases except TARGET_SCRIPT targeting
4906 // in case TARGET_SCRIPT target selected by server always and can't be cheated
4907 if( target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NOT_SELECTABLE
) &&
4908 m_spellInfo
->EffectImplicitTargetA
[eff
] != TARGET_SCRIPT
&&
4909 m_spellInfo
->EffectImplicitTargetB
[eff
] != TARGET_SCRIPT
)
4913 //Check player targets and remove if in GM mode or GM invisibility (for not self casting case)
4914 if( target
!= m_caster
&& target
->GetTypeId()==TYPEID_PLAYER
)
4916 if(((Player
*)target
)->GetVisibility()==VISIBILITY_OFF
)
4919 if(((Player
*)target
)->isGameMaster() && !IsPositiveSpell(m_spellInfo
->Id
))
4923 //Check targets for LOS visibility (except spells without range limitations )
4924 switch(m_spellInfo
->Effect
[eff
])
4926 case SPELL_EFFECT_SUMMON_PLAYER
: // from anywhere
4928 case SPELL_EFFECT_DUMMY
:
4929 if(m_spellInfo
->Id
!=20577) // Cannibalize
4932 case SPELL_EFFECT_RESURRECT_NEW
:
4933 // player far away, maybe his corpse near?
4934 if(target
!=m_caster
&& !target
->IsWithinLOSInMap(m_caster
))
4936 if(!m_targets
.getCorpseTargetGUID())
4939 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
4943 if(target
->GetGUID()!=corpse
->GetOwnerGUID())
4946 if(!corpse
->IsWithinLOSInMap(m_caster
))
4950 // all ok by some way or another, skip normal check
4952 default: // normal case
4953 if(target
!=m_caster
&& !target
->IsWithinLOSInMap(m_caster
))
4961 Unit
* Spell::SelectMagnetTarget()
4963 Unit
* target
= m_targets
.getUnitTarget();
4965 if(target
&& target
->HasAuraType(SPELL_AURA_SPELL_MAGNET
) && !(m_spellInfo
->Attributes
& 0x10))
4967 Unit::AuraList
const& magnetAuras
= target
->GetAurasByType(SPELL_AURA_SPELL_MAGNET
);
4968 for(Unit::AuraList::const_iterator itr
= magnetAuras
.begin(); itr
!= magnetAuras
.end(); ++itr
)
4970 if(Unit
* magnet
= (*itr
)->GetCaster())
4972 if(magnet
->IsWithinLOSInMap(m_caster
))
4975 m_targets
.setUnitTarget(target
);
4985 bool Spell::IsNeedSendToClient() const
4987 return m_spellInfo
->SpellVisual
!=0 || IsChanneledSpell(m_spellInfo
) ||
4988 m_spellInfo
->speed
> 0.0f
|| !m_triggeredByAuraSpell
&& !m_IsTriggeredSpell
;
4991 bool Spell::HaveTargetsForEffect( uint8 effect
) const
4993 for(std::list
<TargetInfo
>::const_iterator itr
= m_UniqueTargetInfo
.begin();itr
!= m_UniqueTargetInfo
.end();++itr
)
4994 if(itr
->effectMask
& (1<<effect
))
4997 for(std::list
<GOTargetInfo
>::const_iterator itr
= m_UniqueGOTargetInfo
.begin();itr
!= m_UniqueGOTargetInfo
.end();++itr
)
4998 if(itr
->effectMask
& (1<<effect
))
5001 for(std::list
<ItemTargetInfo
>::const_iterator itr
= m_UniqueItemInfo
.begin();itr
!= m_UniqueItemInfo
.end();++itr
)
5002 if(itr
->effectMask
& (1<<effect
))
5008 SpellEvent::SpellEvent(Spell
* spell
) : BasicEvent()
5013 SpellEvent::~SpellEvent()
5015 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5018 if (m_Spell
->IsDeletable())
5024 sLog
.outError("~SpellEvent: %s %u tried to delete non-deletable spell %u. Was not deleted, causes memory leak.",
5025 (m_Spell
->GetCaster()->GetTypeId()==TYPEID_PLAYER
?"Player":"Creature"), m_Spell
->GetCaster()->GetGUIDLow(),m_Spell
->m_spellInfo
->Id
);
5029 bool SpellEvent::Execute(uint64 e_time
, uint32 p_time
)
5031 // update spell if it is not finished
5032 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5033 m_Spell
->update(p_time
);
5035 // check spell state to process
5036 switch (m_Spell
->getState())
5038 case SPELL_STATE_FINISHED
:
5040 // spell was finished, check deletable state
5041 if (m_Spell
->IsDeletable())
5043 // check, if we do have unfinished triggered spells
5045 return(true); // spell is deletable, finish event
5047 // event will be re-added automatically at the end of routine)
5050 case SPELL_STATE_CASTING
:
5052 // this spell is in channeled state, process it on the next update
5053 // event will be re-added automatically at the end of routine)
5056 case SPELL_STATE_DELAYED
:
5058 // first, check, if we have just started
5059 if (m_Spell
->GetDelayStart() != 0)
5061 // no, we aren't, do the typical update
5062 // check, if we have channeled spell on our hands
5063 if (IsChanneledSpell(m_Spell
->m_spellInfo
))
5065 // evented channeled spell is processed separately, casted once after delay, and not destroyed till finish
5066 // check, if we have casting anything else except this channeled spell and autorepeat
5067 if (m_Spell
->GetCaster()->IsNonMeleeSpellCasted(false, true, true))
5069 // another non-melee non-delayed spell is casted now, abort
5074 // do the action (pass spell to channeling state)
5075 m_Spell
->handle_immediate();
5077 // event will be re-added automatically at the end of routine)
5081 // run the spell handler and think about what we can do next
5082 uint64 t_offset
= e_time
- m_Spell
->GetDelayStart();
5083 uint64 n_offset
= m_Spell
->handle_delayed(t_offset
);
5086 // re-add us to the queue
5087 m_Spell
->GetCaster()->m_Events
.AddEvent(this, m_Spell
->GetDelayStart() + n_offset
, false);
5088 return(false); // event not complete
5091 // finish update event will be re-added automatically at the end of routine)
5096 // delaying had just started, record the moment
5097 m_Spell
->SetDelayStart(e_time
);
5098 // re-plan the event for the delay moment
5099 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ m_Spell
->GetDelayMoment(), false);
5100 return(false); // event not complete
5107 // event will be re-added automatically at the end of routine)
5111 // spell processing not complete, plan event on the next update interval
5112 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ 1, false);
5113 return(false); // event not complete
5116 void SpellEvent::Abort(uint64
/*e_time*/)
5118 // oops, the spell we try to do is aborted
5119 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5123 bool SpellEvent::IsDeletable() const
5125 return m_Spell
->IsDeletable();