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 prepare
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
))
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 seals, 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();
2084 if(m_CastItem
&& m_caster
->GetTypeId() == TYPEID_PLAYER
)
2085 ((Player
*)m_caster
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_USE_ITEM
, m_CastItem
->GetEntry());
2088 SendSpellCooldown();
2091 TakeReagents(); // we must remove reagents before HandleEffects to allow place crafted item in same slot
2094 if(m_spellState
== SPELL_STATE_FINISHED
) // stop cast if spell marked as finish somewhere in Take*/FillTargetMap
2096 SetExecutedCurrently(false);
2100 SendCastResult(castResult
);
2101 SendSpellGo(); // we must send smsg_spell_go packet before m_castItem delete in TakeCastItem()...
2103 // Pass cast spell event to handler (not send triggered by aura spells)
2104 if (m_spellInfo
->DmgClass
!= SPELL_DAMAGE_CLASS_MELEE
&& m_spellInfo
->DmgClass
!= SPELL_DAMAGE_CLASS_RANGED
&& !m_triggeredByAuraSpell
)
2106 m_caster
->ProcDamageAndSpell(m_targets
.getUnitTarget(), PROC_FLAG_CAST_SPELL
, PROC_FLAG_NONE
, 0, SPELL_SCHOOL_MASK_NONE
, m_spellInfo
, m_IsTriggeredSpell
);
2108 // update pointers base at GUIDs to prevent access to non-existed already object
2109 UpdatePointers(); // pointers can be invalidate at triggered spell casting
2112 // Okay, everything is prepared. Now we need to distinguish between immediate and evented delayed spells
2113 if (m_spellInfo
->speed
> 0.0f
)
2116 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2117 // in case delayed spell remove item at cast delay start
2120 // Okay, maps created, now prepare flags
2121 m_immediateHandled
= false;
2122 m_spellState
= SPELL_STATE_DELAYED
;
2127 // Immediate spell, no big deal
2131 SetExecutedCurrently(false);
2134 void Spell::handle_immediate()
2136 // start channeling if applicable
2137 if(IsChanneledSpell(m_spellInfo
))
2139 m_spellState
= SPELL_STATE_CASTING
;
2140 SendChannelStart(GetSpellDuration(m_spellInfo
));
2143 // process immediate effects (items, ground, etc.) also initialize some variables
2144 _handle_immediate_phase();
2146 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2147 DoAllEffectOnTarget(&(*ihit
));
2149 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
2150 DoAllEffectOnTarget(&(*ihit
));
2152 // spell is finished, perform some last features of the spell here
2153 _handle_finish_phase();
2155 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2158 if(m_spellState
!= SPELL_STATE_CASTING
)
2159 finish(true); // successfully finish spell cast (not last in case autorepeat or channel spell)
2162 uint64
Spell::handle_delayed(uint64 t_offset
)
2164 uint64 next_time
= 0;
2166 if (!m_immediateHandled
)
2168 _handle_immediate_phase();
2169 m_immediateHandled
= true;
2172 // 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)
2173 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2175 if (ihit
->processed
== false)
2177 if ( ihit
->timeDelay
<= t_offset
)
2178 DoAllEffectOnTarget(&(*ihit
));
2179 else if( next_time
== 0 || ihit
->timeDelay
< next_time
)
2180 next_time
= ihit
->timeDelay
;
2184 // now recheck gameobject targeting correctness
2185 for(std::list
<GOTargetInfo
>::iterator ighit
= m_UniqueGOTargetInfo
.begin(); ighit
!= m_UniqueGOTargetInfo
.end();++ighit
)
2187 if (ighit
->processed
== false)
2189 if ( ighit
->timeDelay
<= t_offset
)
2190 DoAllEffectOnTarget(&(*ighit
));
2191 else if( next_time
== 0 || ighit
->timeDelay
< next_time
)
2192 next_time
= ighit
->timeDelay
;
2195 // All targets passed - need finish phase
2198 // spell is finished, perform some last features of the spell here
2199 _handle_finish_phase();
2201 finish(true); // successfully finish spell cast
2203 // return zero, spell is finished now
2208 // spell is unfinished, return next execution time
2213 void Spell::_handle_immediate_phase()
2215 // handle some immediate features of the spell here
2216 HandleThreatSpells(m_spellInfo
->Id
);
2218 m_needSpellLog
= IsNeedSendToClient();
2219 for(uint32 j
= 0;j
<3;j
++)
2221 if(m_spellInfo
->Effect
[j
]==0)
2224 // apply Send Event effect to ground in case empty target lists
2225 if( m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SEND_EVENT
&& !HaveTargetsForEffect(j
) )
2227 HandleEffects(NULL
,NULL
,NULL
, j
);
2231 // Don't do spell log, if is school damage spell
2232 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SCHOOL_DAMAGE
|| m_spellInfo
->Effect
[j
] == 0)
2233 m_needSpellLog
= false;
2235 uint32 EffectChainTarget
= m_spellInfo
->EffectChainTarget
[j
];
2236 if(m_originalCaster
)
2237 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
2238 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_JUMP_TARGETS
, EffectChainTarget
, this);
2240 // initialize multipliers
2241 m_damageMultipliers
[j
] = 1.0f
;
2242 if( (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_DAMAGE
|| m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_HEAL
) &&
2243 (EffectChainTarget
> 1) )
2244 m_applyMultiplierMask
|= 1 << j
;
2247 // initialize Diminishing Returns Data
2248 m_diminishLevel
= DIMINISHING_LEVEL_1
;
2249 m_diminishGroup
= DIMINISHING_NONE
;
2252 for(std::list
<ItemTargetInfo
>::iterator ihit
= m_UniqueItemInfo
.begin();ihit
!= m_UniqueItemInfo
.end();++ihit
)
2253 DoAllEffectOnTarget(&(*ihit
));
2256 for(uint32 j
= 0;j
<3;j
++)
2258 // persistent area auras target only the ground
2259 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_PERSISTENT_AREA_AURA
)
2260 HandleEffects(NULL
,NULL
,NULL
, j
);
2264 void Spell::_handle_finish_phase()
2271 void Spell::SendSpellCooldown()
2273 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
2276 Player
* _player
= (Player
*)m_caster
;
2277 // Add cooldown for max (disable spell)
2278 // Cooldown started on SendCooldownEvent call
2279 if (m_spellInfo
->Attributes
& SPELL_ATTR_DISABLED_WHILE_ACTIVE
)
2281 _player
->AddSpellCooldown(m_spellInfo
->Id
, 0, time(NULL
) - 1);
2285 // init cooldown values
2290 // some special item spells without correct cooldown in SpellInfo
2291 // cooldown information stored in item prototype
2292 // This used in same way in WorldSession::HandleItemQuerySingleOpcode data sending to client.
2296 ItemPrototype
const* proto
= m_CastItem
->GetProto();
2299 for(int idx
= 0; idx
< 5; ++idx
)
2301 if(proto
->Spells
[idx
].SpellId
== m_spellInfo
->Id
)
2303 cat
= proto
->Spells
[idx
].SpellCategory
;
2304 rec
= proto
->Spells
[idx
].SpellCooldown
;
2305 catrec
= proto
->Spells
[idx
].SpellCategoryCooldown
;
2312 // if no cooldown found above then base at DBC data
2313 if(rec
< 0 && catrec
< 0)
2315 cat
= m_spellInfo
->Category
;
2316 rec
= m_spellInfo
->RecoveryTime
;
2317 catrec
= m_spellInfo
->CategoryRecoveryTime
;
2320 // shoot spells used equipped item cooldown values already assigned in GetAttackTime(RANGED_ATTACK)
2321 // prevent 0 cooldowns set by another way
2322 if (rec
<= 0 && catrec
<= 0 && (cat
== 76 || cat
== 351))
2323 rec
= _player
->GetAttackTime(RANGED_ATTACK
);
2325 // Now we have cooldown data (if found any), time to apply mods
2327 _player
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COOLDOWN
, rec
, this);
2330 _player
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COOLDOWN
, catrec
, this);
2332 // replace negative cooldowns by 0
2333 if (rec
< 0) rec
= 0;
2334 if (catrec
< 0) catrec
= 0;
2336 // no cooldown after applying spell mods
2337 if( rec
== 0 && catrec
== 0)
2340 time_t curTime
= time(NULL
);
2342 time_t catrecTime
= catrec
? curTime
+catrec
/1000 : 0; // in secs
2343 time_t recTime
= rec
? curTime
+rec
/1000 : catrecTime
;// in secs
2345 // self spell cooldown
2347 _player
->AddSpellCooldown(m_spellInfo
->Id
, m_CastItem
? m_CastItem
->GetEntry() : 0, recTime
);
2352 SpellCategoryStore::const_iterator i_scstore
= sSpellCategoryStore
.find(cat
);
2353 if(i_scstore
!= sSpellCategoryStore
.end())
2355 for(SpellCategorySet::const_iterator i_scset
= i_scstore
->second
.begin(); i_scset
!= i_scstore
->second
.end(); ++i_scset
)
2357 if(*i_scset
== m_spellInfo
->Id
) // skip main spell, already handled above
2360 _player
->AddSpellCooldown(m_spellInfo
->Id
, m_CastItem
? m_CastItem
->GetEntry() : 0, catrecTime
);
2366 void Spell::update(uint32 difftime
)
2368 // update pointers based at it's GUIDs
2371 if(m_targets
.getUnitTargetGUID() && !m_targets
.getUnitTarget())
2377 // check if the player caster has moved before the spell finished
2378 if ((m_caster
->GetTypeId() == TYPEID_PLAYER
&& m_timer
!= 0) &&
2379 (m_castPositionX
!= m_caster
->GetPositionX() || m_castPositionY
!= m_caster
->GetPositionY() || m_castPositionZ
!= m_caster
->GetPositionZ()) &&
2380 (m_spellInfo
->Effect
[0] != SPELL_EFFECT_STUCK
|| !m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_FALLING
)))
2382 // always cancel for channeled spells
2383 if( m_spellState
== SPELL_STATE_CASTING
)
2385 // don't cancel for melee, autorepeat, triggered and instant spells
2386 else if(!IsNextMeleeSwingSpell() && !IsAutoRepeat() && !m_IsTriggeredSpell
&& (m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_MOVEMENT
))
2390 switch(m_spellState
)
2392 case SPELL_STATE_PREPARING
:
2396 if(difftime
>= m_timer
)
2399 m_timer
-= difftime
;
2402 if(m_timer
== 0 && !IsNextMeleeSwingSpell() && !IsAutoRepeat())
2405 case SPELL_STATE_CASTING
:
2409 if( m_caster
->GetTypeId() == TYPEID_PLAYER
)
2411 // check if player has jumped before the channeling finished
2412 if(m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_JUMPING
))
2415 // check for incapacitating player states
2416 if( m_caster
->hasUnitState(UNIT_STAT_STUNNED
| UNIT_STAT_CONFUSED
))
2419 // check if player has turned if flag is set
2420 if( m_spellInfo
->ChannelInterruptFlags
& CHANNEL_FLAG_TURNING
&& m_castOrientation
!= m_caster
->GetOrientation() )
2424 // check if there are alive targets left
2425 if (!IsAliveUnitPresentInTargetList())
2427 SendChannelUpdate(0);
2431 if(difftime
>= m_timer
)
2434 m_timer
-= difftime
;
2439 SendChannelUpdate(0);
2441 // channeled spell processed independently for quest targeting
2442 // cast at creature (or GO) quest objectives update at successful cast channel finished
2443 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
2444 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() )
2446 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2448 TargetInfo
* target
= &*ihit
;
2449 if(!IS_CREATURE_GUID(target
->targetGUID
))
2452 Unit
* unit
= m_caster
->GetGUID()==target
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
,target
->targetGUID
);
2456 ((Player
*)m_caster
)->CastedCreatureOrGO(unit
->GetEntry(),unit
->GetGUID(),m_spellInfo
->Id
);
2459 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
2461 GOTargetInfo
* target
= &*ihit
;
2463 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, target
->targetGUID
);
2467 ((Player
*)m_caster
)->CastedCreatureOrGO(go
->GetEntry(),go
->GetGUID(),m_spellInfo
->Id
);
2480 void Spell::finish(bool ok
)
2485 if(m_spellState
== SPELL_STATE_FINISHED
)
2488 m_spellState
= SPELL_STATE_FINISHED
;
2491 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2492 ((Player
*)m_caster
)->RemoveSpellMods(this);
2494 // other code related only to successfully finished spells
2498 //handle SPELL_AURA_ADD_TARGET_TRIGGER auras
2499 Unit::AuraList
const& targetTriggers
= m_caster
->GetAurasByType(SPELL_AURA_ADD_TARGET_TRIGGER
);
2500 for(Unit::AuraList::const_iterator i
= targetTriggers
.begin(); i
!= targetTriggers
.end(); ++i
)
2502 SpellEntry
const *auraSpellInfo
= (*i
)->GetSpellProto();
2503 uint32 auraSpellIdx
= (*i
)->GetEffIndex();
2504 if (IsAffectedBy(auraSpellInfo
, auraSpellIdx
))
2506 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2507 if( ihit
->effectMask
& (1<<auraSpellIdx
) )
2509 // check m_caster->GetGUID() let load auras at login and speedup most often case
2510 Unit
*unit
= m_caster
->GetGUID()== ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
2511 if (unit
&& unit
->isAlive())
2513 // Calculate chance at that moment (can be depend for example from combo points)
2514 int32 chance
= m_caster
->CalculateSpellDamage(auraSpellInfo
, auraSpellIdx
, (*i
)->GetBasePoints(),unit
);
2516 if(roll_chance_i(chance
))
2517 m_caster
->CastSpell(unit
, auraSpellInfo
->EffectTriggerSpell
[auraSpellIdx
], true, NULL
, (*i
));
2523 if (IsMeleeAttackResetSpell())
2525 m_caster
->resetAttackTimer(BASE_ATTACK
);
2526 if(m_caster
->haveOffhandWeapon())
2527 m_caster
->resetAttackTimer(OFF_ATTACK
);
2530 /*if (IsRangedAttackResetSpell())
2531 m_caster->resetAttackTimer(RANGED_ATTACK);*/
2533 // Clear combo at finish state
2534 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& NeedsComboPoints(m_spellInfo
))
2535 ((Player
*)m_caster
)->ClearComboPoints();
2537 // call triggered spell only at successful cast (after clear combo points -> for add some if need)
2538 if(!m_TriggerSpells
.empty())
2541 // Stop Attack for some spells
2542 if( m_spellInfo
->Attributes
& SPELL_ATTR_STOP_ATTACK_TARGET
)
2543 m_caster
->AttackStop();
2546 void Spell::SendCastResult(uint8 result
)
2548 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2551 if(((Player
*)m_caster
)->GetSession()->PlayerLoading()) // don't send cast results at loading time
2556 WorldPacket
data(SMSG_CAST_FAILED
, (4+1+1));
2557 data
<< uint8(m_cast_count
); // single cast or multi 2.3 (0/1)
2558 data
<< uint32(m_spellInfo
->Id
);
2559 data
<< uint8(result
); // problem
2562 case SPELL_FAILED_REQUIRES_SPELL_FOCUS
:
2563 data
<< uint32(m_spellInfo
->RequiresSpellFocus
);
2565 case SPELL_FAILED_REQUIRES_AREA
:
2566 // hardcode areas limitation case
2567 if( m_spellInfo
->Id
==41618 || m_spellInfo
->Id
==41620 )
2568 data
<< uint32(3842);
2569 else if( m_spellInfo
->Id
==41617 || m_spellInfo
->Id
==41619 )
2570 data
<< uint32(3905);
2573 data
<< uint32(m_spellInfo
->AreaId
);
2575 case SPELL_FAILED_TOTEMS
:
2576 if(m_spellInfo
->Totem
[0])
2577 data
<< uint32(m_spellInfo
->Totem
[0]);
2578 if(m_spellInfo
->Totem
[1])
2579 data
<< uint32(m_spellInfo
->Totem
[1]);
2581 case SPELL_FAILED_TOTEM_CATEGORY
:
2582 if(m_spellInfo
->TotemCategory
[0])
2583 data
<< uint32(m_spellInfo
->TotemCategory
[0]);
2584 if(m_spellInfo
->TotemCategory
[1])
2585 data
<< uint32(m_spellInfo
->TotemCategory
[1]);
2587 case SPELL_FAILED_EQUIPPED_ITEM_CLASS
:
2588 data
<< uint32(m_spellInfo
->EquippedItemClass
);
2589 data
<< uint32(m_spellInfo
->EquippedItemSubClassMask
);
2590 //data << uint32(m_spellInfo->EquippedItemInventoryTypeMask);
2593 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
2597 WorldPacket data(SMSG_CLEAR_EXTRA_AURA_INFO_OBSOLETE, (8+4));
2598 data.append(m_caster->GetPackGUID());
2599 data << uint32(m_spellInfo->Id);
2600 ((Player*)m_caster)->GetSession()->SendPacket(&data);
2604 void Spell::SendSpellStart()
2606 if(!IsNeedSendToClient())
2609 sLog
.outDebug("Sending SMSG_SPELL_START id=%u", m_spellInfo
->Id
);
2611 uint32 castFlags
= CAST_FLAG_UNKNOWN1
;
2613 castFlags
|= CAST_FLAG_AMMO
;
2616 if(!m_targets
.getUnitTarget())
2619 target
= m_targets
.getUnitTarget();
2621 WorldPacket
data(SMSG_SPELL_START
, (8+8+4+4+2));
2623 data
.append(m_CastItem
->GetPackGUID());
2625 data
.append(m_caster
->GetPackGUID());
2627 data
.append(m_caster
->GetPackGUID());
2628 data
<< uint8(m_cast_count
); // pending spell cast?
2629 data
<< uint32(m_spellInfo
->Id
);
2630 data
<< uint32(castFlags
);
2631 data
<< uint32(m_timer
);
2633 m_targets
.write(&data
);
2635 if ( castFlags
& CAST_FLAG_UNKNOWN6
)
2638 if ( castFlags
& CAST_FLAG_UNKNOWN7
)
2644 for(uint8 i
= 0; i
< 6; ++i
)
2650 if ( castFlags
& CAST_FLAG_AMMO
)
2651 WriteAmmoToPacket(&data
);
2653 m_caster
->SendMessageToSet(&data
, true);
2656 void Spell::SendSpellGo()
2658 // not send invisible spell casting
2659 if(!IsNeedSendToClient())
2662 sLog
.outDebug("Sending SMSG_SPELL_GO id=%u",m_spellInfo
->Id
);
2665 if(!m_targets
.getUnitTarget())
2668 target
= m_targets
.getUnitTarget();
2670 uint32 castFlags
= CAST_FLAG_UNKNOWN3
;
2672 castFlags
|= CAST_FLAG_AMMO
;
2674 WorldPacket
data(SMSG_SPELL_GO
, 50); // guess size
2676 data
.append(m_CastItem
->GetPackGUID());
2678 data
.append(m_caster
->GetPackGUID());
2680 data
.append(m_caster
->GetPackGUID());
2681 data
<< uint8(m_cast_count
); // pending spell cast?
2682 data
<< uint32(m_spellInfo
->Id
);
2683 data
<< uint32(castFlags
);
2684 data
<< uint32(getMSTime()); // timestamp
2686 WriteSpellGoTargets(&data
);
2688 m_targets
.write(&data
);
2690 if ( castFlags
& CAST_FLAG_UNKNOWN6
) // unknown wotlk
2693 if ( castFlags
& CAST_FLAG_UNKNOWN7
)
2699 for(uint8 i
= 0; i
< 6; ++i
)
2705 if ( castFlags
& CAST_FLAG_UNKNOWN4
) // unknown wotlk
2711 if ( castFlags
& CAST_FLAG_AMMO
)
2712 WriteAmmoToPacket(&data
);
2714 if ( castFlags
& CAST_FLAG_UNKNOWN5
) // unknown wotlk
2720 if ( m_targets
.m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
2725 m_caster
->SendMessageToSet(&data
, true);
2728 void Spell::WriteAmmoToPacket( WorldPacket
* data
)
2730 uint32 ammoInventoryType
= 0;
2731 uint32 ammoDisplayID
= 0;
2733 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2735 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack( RANGED_ATTACK
);
2738 ammoInventoryType
= pItem
->GetProto()->InventoryType
;
2739 if( ammoInventoryType
== INVTYPE_THROWN
)
2740 ammoDisplayID
= pItem
->GetProto()->DisplayInfoID
;
2743 uint32 ammoID
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
2746 ItemPrototype
const *pProto
= objmgr
.GetItemPrototype( ammoID
);
2749 ammoDisplayID
= pProto
->DisplayInfoID
;
2750 ammoInventoryType
= pProto
->InventoryType
;
2753 else if(m_caster
->GetDummyAura(46699)) // Requires No Ammo
2755 ammoDisplayID
= 5996; // normal arrow
2756 ammoInventoryType
= INVTYPE_AMMO
;
2761 // TODO: implement selection ammo data based at ranged weapon stored in equipmodel/equipinfo/equipslot fields
2763 *data
<< uint32(ammoDisplayID
);
2764 *data
<< uint32(ammoInventoryType
);
2767 void Spell::WriteSpellGoTargets( WorldPacket
* data
)
2769 *data
<< (uint8
)m_countOfHit
;
2770 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2771 if ((*ihit
).missCondition
== SPELL_MISS_NONE
) // Add only hits
2772 *data
<< uint64(ihit
->targetGUID
);
2774 for(std::list
<GOTargetInfo
>::iterator ighit
= m_UniqueGOTargetInfo
.begin();ighit
!= m_UniqueGOTargetInfo
.end();++ighit
)
2775 *data
<< uint64(ighit
->targetGUID
); // Always hits
2777 *data
<< (uint8
)m_countOfMiss
;
2778 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2780 if( ihit
->missCondition
!= SPELL_MISS_NONE
) // Add only miss
2782 *data
<< uint64(ihit
->targetGUID
);
2783 *data
<< uint8(ihit
->missCondition
);
2784 if( ihit
->missCondition
== SPELL_MISS_REFLECT
)
2785 *data
<< uint8(ihit
->reflectResult
);
2790 void Spell::SendLogExecute()
2792 Unit
*target
= m_targets
.getUnitTarget() ? m_targets
.getUnitTarget() : m_caster
;
2794 WorldPacket
data(SMSG_SPELLLOGEXECUTE
, (8+4+4+4+4+8));
2796 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
2797 data
.append(m_caster
->GetPackGUID());
2799 data
.append(target
->GetPackGUID());
2801 data
<< uint32(m_spellInfo
->Id
);
2803 data
<< uint32(count1
); // count1 (effect count?)
2804 for(uint32 i
= 0; i
< count1
; ++i
)
2806 data
<< uint32(m_spellInfo
->Effect
[0]); // spell effect
2808 data
<< uint32(count2
); // count2 (target count?)
2809 for(uint32 j
= 0; j
< count2
; ++j
)
2811 switch(m_spellInfo
->Effect
[0])
2813 case SPELL_EFFECT_POWER_DRAIN
:
2814 if(Unit
*unit
= m_targets
.getUnitTarget())
2815 data
.append(unit
->GetPackGUID());
2822 case SPELL_EFFECT_ADD_EXTRA_ATTACKS
:
2823 if(Unit
*unit
= m_targets
.getUnitTarget())
2824 data
.append(unit
->GetPackGUID());
2827 data
<< uint32(0); // count?
2829 case SPELL_EFFECT_INTERRUPT_CAST
:
2830 if(Unit
*unit
= m_targets
.getUnitTarget())
2831 data
.append(unit
->GetPackGUID());
2834 data
<< uint32(0); // spellid
2836 case SPELL_EFFECT_DURABILITY_DAMAGE
:
2837 if(Unit
*unit
= m_targets
.getUnitTarget())
2838 data
.append(unit
->GetPackGUID());
2844 case SPELL_EFFECT_OPEN_LOCK
:
2845 case SPELL_EFFECT_OPEN_LOCK_ITEM
:
2846 if(Item
*item
= m_targets
.getItemTarget())
2847 data
.append(item
->GetPackGUID());
2851 case SPELL_EFFECT_CREATE_ITEM
:
2852 case SPELL_EFFECT_157
:
2853 data
<< uint32(m_spellInfo
->EffectItemType
[0]);
2855 case SPELL_EFFECT_SUMMON
:
2856 case SPELL_EFFECT_TRANS_DOOR
:
2857 case SPELL_EFFECT_SUMMON_PET
:
2858 case SPELL_EFFECT_SUMMON_OBJECT_WILD
:
2859 case SPELL_EFFECT_CREATE_HOUSE
:
2860 case SPELL_EFFECT_DUEL
:
2861 case SPELL_EFFECT_SUMMON_OBJECT_SLOT1
:
2862 case SPELL_EFFECT_SUMMON_OBJECT_SLOT2
:
2863 case SPELL_EFFECT_SUMMON_OBJECT_SLOT3
:
2864 case SPELL_EFFECT_SUMMON_OBJECT_SLOT4
:
2865 if(Unit
*unit
= m_targets
.getUnitTarget())
2866 data
.append(unit
->GetPackGUID());
2867 else if(m_targets
.getItemTargetGUID())
2868 data
.appendPackGUID(m_targets
.getItemTargetGUID());
2869 else if(GameObject
*go
= m_targets
.getGOTarget())
2870 data
.append(go
->GetPackGUID());
2872 data
<< uint8(0); // guid
2874 case SPELL_EFFECT_FEED_PET
:
2875 data
<< uint32(m_targets
.getItemTargetEntry());
2877 case SPELL_EFFECT_DISMISS_PET
:
2878 if(Unit
*unit
= m_targets
.getUnitTarget())
2879 data
.append(unit
->GetPackGUID());
2883 case SPELL_EFFECT_RESURRECT
:
2884 case SPELL_EFFECT_RESURRECT_NEW
:
2885 if(Unit
*unit
= m_targets
.getUnitTarget())
2886 data
.append(unit
->GetPackGUID());
2896 m_caster
->SendMessageToSet(&data
, true);
2899 void Spell::SendInterrupted(uint8 result
)
2901 WorldPacket
data(SMSG_SPELL_FAILURE
, (8+4+1));
2902 data
.append(m_caster
->GetPackGUID());
2903 data
<< uint8(m_cast_count
);
2904 data
<< uint32(m_spellInfo
->Id
);
2905 data
<< uint8(result
);
2906 m_caster
->SendMessageToSet(&data
, true);
2908 data
.Initialize(SMSG_SPELL_FAILED_OTHER
, (8+4));
2909 data
.append(m_caster
->GetPackGUID());
2910 data
<< uint8(m_cast_count
);
2911 data
<< uint32(m_spellInfo
->Id
);
2912 data
<< uint8(result
);
2913 m_caster
->SendMessageToSet(&data
, true);
2916 void Spell::SendChannelUpdate(uint32 time
)
2920 m_caster
->SetUInt64Value(UNIT_FIELD_CHANNEL_OBJECT
,0);
2921 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
,0);
2924 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2927 WorldPacket
data( MSG_CHANNEL_UPDATE
, 8+4 );
2928 data
.append(m_caster
->GetPackGUID());
2929 data
<< uint32(time
);
2931 ((Player
*)m_caster
)->GetSession()->SendPacket( &data
);
2934 void Spell::SendChannelStart(uint32 duration
)
2936 WorldObject
* target
= NULL
;
2938 // select first not resisted target from target list for _0_ effect
2939 if(!m_UniqueTargetInfo
.empty())
2941 for(std::list
<TargetInfo
>::iterator itr
= m_UniqueTargetInfo
.begin();itr
!= m_UniqueTargetInfo
.end();++itr
)
2943 if( (itr
->effectMask
& (1<<0)) && itr
->reflectResult
==SPELL_MISS_NONE
&& itr
->targetGUID
!= m_caster
->GetGUID())
2945 target
= ObjectAccessor::GetUnit(*m_caster
, itr
->targetGUID
);
2950 else if(!m_UniqueGOTargetInfo
.empty())
2952 for(std::list
<GOTargetInfo
>::iterator itr
= m_UniqueGOTargetInfo
.begin();itr
!= m_UniqueGOTargetInfo
.end();++itr
)
2954 if(itr
->effectMask
& (1<<0) )
2956 target
= ObjectAccessor::GetGameObject(*m_caster
, itr
->targetGUID
);
2962 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2964 WorldPacket
data( MSG_CHANNEL_START
, (8+4+4) );
2965 data
.append(m_caster
->GetPackGUID());
2966 data
<< uint32(m_spellInfo
->Id
);
2967 data
<< uint32(duration
);
2969 ((Player
*)m_caster
)->GetSession()->SendPacket( &data
);
2974 m_caster
->SetUInt64Value(UNIT_FIELD_CHANNEL_OBJECT
, target
->GetGUID());
2975 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
, m_spellInfo
->Id
);
2978 void Spell::SendResurrectRequest(Player
* target
)
2980 WorldPacket
data(SMSG_RESURRECT_REQUEST
, (8+4+2+4));
2981 data
<< uint64(m_caster
->GetGUID());
2983 data
<< uint32(count
); // amount of bytes to read
2985 for(uint32 i
= 0; i
< count
; ++i
)
2990 target
->GetSession()->SendPacket(&data
);
2993 void Spell::SendPlaySpellVisual(uint32 SpellID
)
2995 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2998 WorldPacket
data(SMSG_PLAY_SPELL_VISUAL
, 12);
2999 data
<< uint64(m_caster
->GetGUID());
3000 data
<< uint32(SpellID
); // spell visual id?
3001 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
3004 void Spell::TakeCastItem()
3006 if(!m_CastItem
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
3009 // not remove cast item at triggered spell (equipping, weapon damage, etc)
3010 if(m_IsTriggeredSpell
)
3013 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3017 // This code is to avoid a crash
3018 // I'm not sure, if this is really an error, but I guess every item needs a prototype
3019 sLog
.outError("Cast item has no item prototype highId=%d, lowId=%d",m_CastItem
->GetGUIDHigh(), m_CastItem
->GetGUIDLow());
3023 bool expendable
= false;
3024 bool withoutCharges
= false;
3026 for (int i
= 0; i
<5; i
++)
3028 if (proto
->Spells
[i
].SpellId
)
3030 // item has limited charges
3031 if (proto
->Spells
[i
].SpellCharges
)
3033 if (proto
->Spells
[i
].SpellCharges
< 0)
3036 int32 charges
= m_CastItem
->GetSpellCharges(i
);
3038 // item has charges left
3041 (charges
> 0) ? --charges
: ++charges
; // abs(charges) less at 1 after use
3042 if (proto
->Stackable
< 2)
3043 m_CastItem
->SetSpellCharges(i
, charges
);
3044 m_CastItem
->SetState(ITEM_CHANGED
, (Player
*)m_caster
);
3048 withoutCharges
= (charges
== 0);
3053 if (expendable
&& withoutCharges
)
3056 ((Player
*)m_caster
)->DestroyItemCount(m_CastItem
, count
, true);
3058 // prevent crash at access to deleted m_targets.getItemTarget
3059 if(m_CastItem
==m_targets
.getItemTarget())
3060 m_targets
.setItemTarget(NULL
);
3066 void Spell::TakePower()
3068 if(m_CastItem
|| m_triggeredByAuraSpell
)
3071 // health as power used
3072 if(m_spellInfo
->powerType
== POWER_HEALTH
)
3074 m_caster
->ModifyHealth( -(int32
)m_powerCost
);
3078 if(m_spellInfo
->powerType
>= MAX_POWERS
)
3080 sLog
.outError("Spell::TakePower: Unknown power type '%d'", m_spellInfo
->powerType
);
3084 Powers powerType
= Powers(m_spellInfo
->powerType
);
3086 m_caster
->ModifyPower(powerType
, -(int32
)m_powerCost
);
3088 // Set the five second timer
3089 if (powerType
== POWER_MANA
&& m_powerCost
> 0)
3090 m_caster
->SetLastManaUse(getMSTime());
3093 void Spell::TakeReagents()
3095 if(m_IsTriggeredSpell
) // reagents used in triggered spell removed by original spell or don't must be removed.
3098 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
3101 if (m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_NO_REAGENT_WHILE_PREP
&&
3102 m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PREPARATION
))
3105 Player
* p_caster
= (Player
*)m_caster
;
3107 for(uint32 x
=0;x
<8;x
++)
3109 if(m_spellInfo
->Reagent
[x
] <= 0)
3112 uint32 itemid
= m_spellInfo
->Reagent
[x
];
3113 uint32 itemcount
= m_spellInfo
->ReagentCount
[x
];
3115 // if CastItem is also spell reagent
3118 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3119 if( proto
&& proto
->ItemId
== itemid
)
3121 for(int s
=0;s
<5;s
++)
3123 // CastItem will be used up and does not count as reagent
3124 int32 charges
= m_CastItem
->GetSpellCharges(s
);
3125 if (proto
->Spells
[s
].SpellCharges
< 0 && abs(charges
) < 2)
3136 // if getItemTarget is also spell reagent
3137 if (m_targets
.getItemTargetEntry()==itemid
)
3138 m_targets
.setItemTarget(NULL
);
3140 p_caster
->DestroyItemCount(itemid
, itemcount
, true);
3144 void Spell::HandleThreatSpells(uint32 spellId
)
3146 if(!m_targets
.getUnitTarget() || !spellId
)
3149 if(!m_targets
.getUnitTarget()->CanHaveThreatList())
3152 SpellThreatEntry
const *threatSpell
= sSpellThreatStore
.LookupEntry
<SpellThreatEntry
>(spellId
);
3156 m_targets
.getUnitTarget()->AddThreat(m_caster
, float(threatSpell
->threat
));
3158 DEBUG_LOG("Spell %u, rank %u, added an additional %i threat", spellId
, spellmgr
.GetSpellRank(spellId
), threatSpell
->threat
);
3161 void Spell::HandleEffects(Unit
*pUnitTarget
,Item
*pItemTarget
,GameObject
*pGOTarget
,uint32 i
, float DamageMultiplier
)
3163 unitTarget
= pUnitTarget
;
3164 itemTarget
= pItemTarget
;
3165 gameObjTarget
= pGOTarget
;
3167 uint8 eff
= m_spellInfo
->Effect
[i
];
3168 uint32 mechanic
= m_spellInfo
->EffectMechanic
[i
];
3170 damage
= int32(CalculateDamage((uint8
)i
,unitTarget
)*DamageMultiplier
);
3172 sLog
.outDebug( "Spell: Effect : %u", eff
);
3174 //Simply return. Do not display "immune" in red text on client
3175 if(unitTarget
&& unitTarget
->IsImmunedToSpellEffect(eff
, mechanic
))
3178 if(eff
<TOTAL_SPELL_EFFECTS
)
3180 //sLog.outDebug( "WORLD: Spell FX %d < TOTAL_SPELL_EFFECTS ", eff);
3181 (*this.*SpellEffects
[eff
])(i
);
3186 sLog.outDebug( "WORLD: Spell FX %d > TOTAL_SPELL_EFFECTS ", eff);
3188 EffectEnchantItemTmp(i);
3191 sLog.outError("SPELL: unknown effect %u spell id %u\n",
3192 eff, m_spellInfo->Id);
3198 void Spell::TriggerSpell()
3200 for(TriggerSpells::iterator si
=m_TriggerSpells
.begin(); si
!=m_TriggerSpells
.end(); ++si
)
3202 Spell
* spell
= new Spell(m_caster
, (*si
), true, m_originalCasterGUID
, m_selfContainer
);
3203 spell
->prepare(&m_targets
); // use original spell original targets
3207 uint8
Spell::CanCast(bool strict
)
3209 // check cooldowns to prevent cheating
3210 if(m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
3212 if(m_triggeredByAuraSpell
)
3213 return SPELL_FAILED_DONT_REPORT
;
3215 return SPELL_FAILED_NOT_READY
;
3218 // only allow triggered spells if at an ended battleground
3219 if( !m_IsTriggeredSpell
&& m_caster
->GetTypeId() == TYPEID_PLAYER
)
3220 if(BattleGround
* bg
= ((Player
*)m_caster
)->GetBattleGround())
3221 if(bg
->GetStatus() == STATUS_WAIT_LEAVE
)
3222 return SPELL_FAILED_DONT_REPORT
;
3224 // only check at first call, Stealth auras are already removed at second call
3225 // for now, ignore triggered spells
3226 if( strict
&& !m_IsTriggeredSpell
)
3228 // Cannot be used in this stance/form
3229 if(uint8 shapeError
= GetErrorAtShapeshiftedCast(m_spellInfo
, m_caster
->m_form
))
3232 if ((m_spellInfo
->Attributes
& SPELL_ATTR_ONLY_STEALTHED
) && !(m_caster
->HasStealthAura()))
3233 return SPELL_FAILED_ONLY_STEALTHED
;
3236 // caster state requirements
3237 if(m_spellInfo
->CasterAuraState
&& !m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraState
)))
3238 return SPELL_FAILED_CASTER_AURASTATE
;
3239 if(m_spellInfo
->CasterAuraStateNot
&& m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraStateNot
)))
3240 return SPELL_FAILED_CASTER_AURASTATE
;
3242 // cancel autorepeat spells if cast start when moving
3243 // (not wand currently autorepeat cast delayed to moving stop anyway in spell update code)
3244 if( m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->isMoving() )
3246 // skip stuck spell to allow use it in falling case and apply spell limitations at movement
3247 if( (!m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_FALLING
) || m_spellInfo
->Effect
[0] != SPELL_EFFECT_STUCK
) &&
3248 (IsAutoRepeat() || (m_spellInfo
->AuraInterruptFlags
& AURA_INTERRUPT_FLAG_NOT_SEATED
) != 0) )
3249 return SPELL_FAILED_MOVING
;
3252 Unit
*target
= m_targets
.getUnitTarget();
3256 // target state requirements (not allowed state), apply to self also
3257 if(m_spellInfo
->TargetAuraStateNot
&& target
->HasAuraState(AuraState(m_spellInfo
->TargetAuraStateNot
)))
3258 return SPELL_FAILED_TARGET_AURASTATE
;
3260 if(target
!= m_caster
)
3262 // target state requirements (apply to non-self only), to allow cast affects to self like Dirty Deeds
3263 if(m_spellInfo
->TargetAuraState
&& !target
->HasAuraState(AuraState(m_spellInfo
->TargetAuraState
)))
3264 return SPELL_FAILED_TARGET_AURASTATE
;
3266 // Not allow casting on flying player
3267 if (target
->isInFlight())
3268 return SPELL_FAILED_BAD_TARGETS
;
3270 if(!m_IsTriggeredSpell
&& VMAP::VMapFactory::checkSpellForLoS(m_spellInfo
->Id
) && !m_caster
->IsWithinLOSInMap(target
))
3271 return SPELL_FAILED_LINE_OF_SIGHT
;
3273 // auto selection spell rank implemented in WorldSession::HandleCastSpellOpcode
3274 // this case can be triggered if rank not found (too low-level target for first rank)
3275 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsPassiveSpell(m_spellInfo
->Id
) && !m_CastItem
)
3277 for(int i
=0;i
<3;i
++)
3279 if(IsPositiveEffect(m_spellInfo
->Id
, i
) && m_spellInfo
->Effect
[i
] == SPELL_EFFECT_APPLY_AURA
)
3280 if(target
->getLevel() + 10 < m_spellInfo
->spellLevel
)
3281 return SPELL_FAILED_LOWLEVEL
;
3286 // check pet presents
3287 for(int j
=0;j
<3;j
++)
3289 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_PET
)
3291 target
= m_caster
->GetPet();
3294 if(m_triggeredByAuraSpell
) // not report pet not existence for triggered spells
3295 return SPELL_FAILED_DONT_REPORT
;
3297 return SPELL_FAILED_NO_PET
;
3303 //check creature type
3304 //ignore self casts (including area casts when caster selected as target)
3305 if(target
!= m_caster
)
3307 if(!CheckTargetCreatureType(target
))
3309 if(target
->GetTypeId()==TYPEID_PLAYER
)
3310 return SPELL_FAILED_TARGET_IS_PLAYER
;
3312 return SPELL_FAILED_BAD_TARGETS
;
3316 // TODO: this check can be applied and for player to prevent cheating when IsPositiveSpell will return always correct result.
3317 // check target for pet/charmed casts (not self targeted), self targeted cast used for area effects and etc
3318 if(m_caster
!= target
&& m_caster
->GetTypeId()==TYPEID_UNIT
&& m_caster
->GetCharmerOrOwnerGUID())
3320 // check correctness positive/negative cast target (pet cast real check and cheating check)
3321 if(IsPositiveSpell(m_spellInfo
->Id
))
3323 if(m_caster
->IsHostileTo(target
))
3324 return SPELL_FAILED_BAD_TARGETS
;
3328 if(m_caster
->IsFriendlyTo(target
))
3329 return SPELL_FAILED_BAD_TARGETS
;
3333 if(IsPositiveSpell(m_spellInfo
->Id
))
3335 if(target
->IsImmunedToSpell(m_spellInfo
,false))
3336 return SPELL_FAILED_TARGET_AURASTATE
;
3339 //Must be behind the target.
3340 if( m_spellInfo
->AttributesEx2
== 0x100000 && (m_spellInfo
->AttributesEx
& 0x200) == 0x200 && target
->HasInArc(M_PI
, m_caster
) )
3343 return SPELL_FAILED_NOT_BEHIND
;
3346 //Target must be facing you.
3347 if((m_spellInfo
->Attributes
== 0x150010) && !target
->HasInArc(M_PI
, m_caster
) )
3350 return SPELL_FAILED_NOT_INFRONT
;
3353 // check if target is in combat
3354 if (target
!= m_caster
&& (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_NOT_IN_COMBAT_TARGET
) && target
->isInCombat())
3356 return SPELL_FAILED_TARGET_AFFECTING_COMBAT
;
3359 // Spell casted only on battleground
3360 if((m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_BATTLEGROUND
) && m_caster
->GetTypeId()==TYPEID_PLAYER
)
3361 if(!((Player
*)m_caster
)->InBattleGround())
3362 return SPELL_FAILED_ONLY_BATTLEGROUNDS
;
3364 // do not allow spells to be cast in arenas
3365 // - with greater than 15 min CD without SPELL_ATTR_EX4_USABLE_IN_ARENA flag
3366 // - with SPELL_ATTR_EX4_NOT_USABLE_IN_ARENA flag
3367 if( (m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_NOT_USABLE_IN_ARENA
) ||
3368 GetSpellRecoveryTime(m_spellInfo
) > 15 * MINUTE
* 1000 && !(m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_USABLE_IN_ARENA
) )
3369 if(MapEntry
const* mapEntry
= sMapStore
.LookupEntry(m_caster
->GetMapId()))
3370 if(mapEntry
->IsBattleArena())
3371 return SPELL_FAILED_NOT_IN_ARENA
;
3374 if(!IsSpellAllowedInLocation(m_spellInfo
,m_caster
->GetMapId(),m_caster
->GetZoneId(),m_caster
->GetAreaId()))
3375 return SPELL_FAILED_REQUIRES_AREA
;
3377 // not let players cast spells at mount (and let do it to creatures)
3378 if( m_caster
->IsMounted() && m_caster
->GetTypeId()==TYPEID_PLAYER
&& !m_IsTriggeredSpell
&&
3379 !IsPassiveSpell(m_spellInfo
->Id
) && !(m_spellInfo
->Attributes
& SPELL_ATTR_CASTABLE_WHILE_MOUNTED
) )
3381 if(m_caster
->isInFlight())
3382 return SPELL_FAILED_NOT_FLYING
;
3384 return SPELL_FAILED_NOT_MOUNTED
;
3387 // always (except passive spells) check items (focus object can be required for any type casts)
3388 if(!IsPassiveSpell(m_spellInfo
->Id
))
3389 if(uint8 castResult
= CheckItems())
3392 //ImpliciteTargetA-B = 38, If fact there is 0 Spell with ImpliciteTargetB=38
3393 if(m_UniqueTargetInfo
.empty()) // skip second canCast apply (for delayed spells for example)
3395 for(uint8 j
= 0; j
< 3; j
++)
3397 if( m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT
||
3398 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT
&& m_spellInfo
->EffectImplicitTargetA
[j
] != TARGET_SELF
||
3399 m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3400 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3402 SpellScriptTarget::const_iterator lower
= spellmgr
.GetBeginSpellScriptTarget(m_spellInfo
->Id
);
3403 SpellScriptTarget::const_iterator upper
= spellmgr
.GetEndSpellScriptTarget(m_spellInfo
->Id
);
3405 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
);
3407 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
3408 float range
= GetSpellMaxRange(srange
);
3410 Creature
* creatureScriptTarget
= NULL
;
3411 GameObject
* goScriptTarget
= NULL
;
3413 for(SpellScriptTarget::const_iterator i_spellST
= lower
; i_spellST
!= upper
; ++i_spellST
)
3415 switch(i_spellST
->second
.type
)
3417 case SPELL_TARGET_TYPE_GAMEOBJECT
:
3419 GameObject
* p_GameObject
= NULL
;
3421 if(i_spellST
->second
.targetEntry
)
3423 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
3425 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
3427 MaNGOS::NearestGameObjectEntryInObjectRangeCheck
go_check(*m_caster
,i_spellST
->second
.targetEntry
,range
);
3428 MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
> checker(p_GameObject
,go_check
);
3430 TypeContainerVisitor
<MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
>, GridTypeMapContainer
> object_checker(checker
);
3431 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
3432 cell_lock
->Visit(cell_lock
, object_checker
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
3436 // remember found target and range, next attempt will find more near target with another entry
3437 creatureScriptTarget
= NULL
;
3438 goScriptTarget
= p_GameObject
;
3439 range
= go_check
.GetLastRange();
3442 else if( focusObject
) //Focus Object
3444 float frange
= m_caster
->GetDistance(focusObject
);
3447 creatureScriptTarget
= NULL
;
3448 goScriptTarget
= focusObject
;
3454 case SPELL_TARGET_TYPE_CREATURE
:
3455 case SPELL_TARGET_TYPE_DEAD
:
3458 Creature
*p_Creature
= NULL
;
3460 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
3462 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
3463 cell
.SetNoCreate(); // Really don't know what is that???
3465 MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
u_check(*m_caster
,i_spellST
->second
.targetEntry
,i_spellST
->second
.type
!=SPELL_TARGET_TYPE_DEAD
,range
);
3466 MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
> searcher(p_Creature
, u_check
);
3468 TypeContainerVisitor
<MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
>, GridTypeMapContainer
> grid_creature_searcher(searcher
);
3470 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
3471 cell_lock
->Visit(cell_lock
, grid_creature_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
3475 creatureScriptTarget
= p_Creature
;
3476 goScriptTarget
= NULL
;
3477 range
= u_check
.GetLastRange();
3484 if(creatureScriptTarget
)
3486 // store coordinates for TARGET_SCRIPT_COORDINATES
3487 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3488 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3490 m_targets
.setDestination(creatureScriptTarget
->GetPositionX(),creatureScriptTarget
->GetPositionY(),creatureScriptTarget
->GetPositionZ());
3492 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->EffectImplicitTargetB
[j
] == 0 && m_spellInfo
->Effect
[j
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
3493 AddUnitTarget(creatureScriptTarget
, j
);
3495 // store explicit target for TARGET_SCRIPT
3497 AddUnitTarget(creatureScriptTarget
, j
);
3499 else if(goScriptTarget
)
3501 // store coordinates for TARGET_SCRIPT_COORDINATES
3502 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3503 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3505 m_targets
.setDestination(goScriptTarget
->GetPositionX(),goScriptTarget
->GetPositionY(),goScriptTarget
->GetPositionZ());
3507 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->EffectImplicitTargetB
[j
] == 0 && m_spellInfo
->Effect
[j
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
3508 AddGOTarget(goScriptTarget
, j
);
3510 // store explicit target for TARGET_SCRIPT
3512 AddGOTarget(goScriptTarget
, j
);
3514 //Missing DB Entry or targets for this spellEffect.
3517 // not report target not existence for triggered spells
3518 if(m_triggeredByAuraSpell
|| m_IsTriggeredSpell
)
3519 return SPELL_FAILED_DONT_REPORT
;
3521 return SPELL_FAILED_BAD_TARGETS
;
3527 if(uint8 castResult
= CheckRange(strict
))
3531 if(uint8 castResult
= CheckPower())
3535 if(!m_triggeredByAuraSpell
) // triggered spell not affected by stun/etc
3536 if(uint8 castResult
= CheckCasterAuras())
3539 for (int i
= 0; i
< 3; i
++)
3541 // for effects of spells that have only one target
3542 switch(m_spellInfo
->Effect
[i
])
3544 case SPELL_EFFECT_DUMMY
:
3546 if(m_spellInfo
->SpellIconID
== 1648) // Execute
3548 if(!m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
3549 return SPELL_FAILED_BAD_TARGETS
;
3551 else if (m_spellInfo
->Id
== 51582) // Rocket Boots Engaged
3553 if(m_caster
->IsInWater())
3554 return SPELL_FAILED_ONLY_ABOVEWATER
;
3556 else if(m_spellInfo
->SpellIconID
==156) // Holy Shock
3558 // spell different for friends and enemies
3559 // hart version required facing
3560 if(m_targets
.getUnitTarget() && !m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()) && !m_caster
->HasInArc( M_PI
, target
))
3561 return SPELL_FAILED_UNIT_NOT_INFRONT
;
3565 case SPELL_EFFECT_SCHOOL_DAMAGE
:
3568 if(m_spellInfo
->SpellVisual
[0] == 7250)
3570 if (!m_targets
.getUnitTarget())
3571 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3573 if(m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
3574 return SPELL_FAILED_BAD_TARGETS
;
3578 case SPELL_EFFECT_TAMECREATURE
:
3580 if (!m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetTypeId() == TYPEID_PLAYER
)
3581 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3583 if (m_targets
.getUnitTarget()->getLevel() > m_caster
->getLevel())
3584 return SPELL_FAILED_HIGHLEVEL
;
3586 // use SMSG_PET_TAME_FAILURE?
3587 if (!((Creature
*)m_targets
.getUnitTarget())->GetCreatureInfo()->isTameable ())
3588 return SPELL_FAILED_BAD_TARGETS
;
3590 if(m_caster
->GetPetGUID())
3591 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3593 if(m_caster
->GetCharmGUID())
3594 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3598 case SPELL_EFFECT_LEARN_SPELL
:
3600 if(m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_PET
)
3603 Pet
* pet
= m_caster
->GetPet();
3606 return SPELL_FAILED_NO_PET
;
3608 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
3610 if(!learn_spellproto
)
3611 return SPELL_FAILED_NOT_KNOWN
;
3613 if(m_spellInfo
->spellLevel
> pet
->getLevel())
3614 return SPELL_FAILED_LOWLEVEL
;
3618 case SPELL_EFFECT_LEARN_PET_SPELL
:
3620 Pet
* pet
= m_caster
->GetPet();
3623 return SPELL_FAILED_NO_PET
;
3625 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
3627 if(!learn_spellproto
)
3628 return SPELL_FAILED_NOT_KNOWN
;
3630 if(m_spellInfo
->spellLevel
> pet
->getLevel())
3631 return SPELL_FAILED_LOWLEVEL
;
3635 case SPELL_EFFECT_FEED_PET
:
3637 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| !m_targets
.getItemTarget() )
3638 return SPELL_FAILED_BAD_TARGETS
;
3640 Pet
* pet
= m_caster
->GetPet();
3643 return SPELL_FAILED_NO_PET
;
3645 if(!pet
->HaveInDiet(m_targets
.getItemTarget()->GetProto()))
3646 return SPELL_FAILED_WRONG_PET_FOOD
;
3648 if(!pet
->GetCurrentFoodBenefitLevel(m_targets
.getItemTarget()->GetProto()->ItemLevel
))
3649 return SPELL_FAILED_FOOD_LOWLEVEL
;
3651 if(m_caster
->isInCombat() || pet
->isInCombat())
3652 return SPELL_FAILED_AFFECTING_COMBAT
;
3656 case SPELL_EFFECT_POWER_BURN
:
3657 case SPELL_EFFECT_POWER_DRAIN
:
3659 // Can be area effect, Check only for players and not check if target - caster (spell can have multiply drain/burn effects)
3660 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
3661 if(Unit
* target
= m_targets
.getUnitTarget())
3662 if(target
!=m_caster
&& target
->getPowerType()!=m_spellInfo
->EffectMiscValue
[i
])
3663 return SPELL_FAILED_BAD_TARGETS
;
3666 case SPELL_EFFECT_CHARGE
:
3668 if (m_caster
->hasUnitState(UNIT_STAT_ROOT
))
3669 return SPELL_FAILED_ROOTED
;
3673 case SPELL_EFFECT_SKINNING
:
3675 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| !m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetTypeId() != TYPEID_UNIT
)
3676 return SPELL_FAILED_BAD_TARGETS
;
3678 if( !(m_targets
.getUnitTarget()->GetUInt32Value(UNIT_FIELD_FLAGS
) & UNIT_FLAG_SKINNABLE
) )
3679 return SPELL_FAILED_TARGET_UNSKINNABLE
;
3681 Creature
* creature
= (Creature
*)m_targets
.getUnitTarget();
3682 if ( creature
->GetCreatureType() != CREATURE_TYPE_CRITTER
&& ( !creature
->lootForBody
|| !creature
->loot
.empty() ) )
3684 return SPELL_FAILED_TARGET_NOT_LOOTED
;
3687 uint32 skill
= creature
->GetCreatureInfo()->GetRequiredLootSkill();
3689 int32 skillValue
= ((Player
*)m_caster
)->GetSkillValue(skill
);
3690 int32 TargetLevel
= m_targets
.getUnitTarget()->getLevel();
3691 int32 ReqValue
= (skillValue
< 100 ? (TargetLevel
-10)*10 : TargetLevel
*5);
3692 if (ReqValue
> skillValue
)
3693 return SPELL_FAILED_LOW_CASTLEVEL
;
3695 // chance for fail at orange skinning attempt
3696 if( (m_selfContainer
&& (*m_selfContainer
) == this) &&
3697 skillValue
< sWorld
.GetConfigMaxSkillValue() &&
3698 (ReqValue
< 0 ? 0 : ReqValue
) > irand(skillValue
-25, skillValue
+37) )
3699 return SPELL_FAILED_TRY_AGAIN
;
3703 case SPELL_EFFECT_OPEN_LOCK_ITEM
:
3704 case SPELL_EFFECT_OPEN_LOCK
:
3706 if( m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT
&&
3707 m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT_ITEM
)
3710 if( m_caster
->GetTypeId() != TYPEID_PLAYER
// only players can open locks, gather etc.
3711 // we need a go target in case of TARGET_GAMEOBJECT
3712 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT
&& !m_targets
.getGOTarget()
3713 // we need a go target, or an openable item target in case of TARGET_GAMEOBJECT_ITEM
3714 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT_ITEM
&& !m_targets
.getGOTarget() &&
3715 (!m_targets
.getItemTarget() || !m_targets
.getItemTarget()->GetProto()->LockID
|| m_targets
.getItemTarget()->GetOwner() != m_caster
) )
3716 return SPELL_FAILED_BAD_TARGETS
;
3718 // In BattleGround players can use only flags and banners
3719 if( ((Player
*)m_caster
)->InBattleGround() &&
3720 !((Player
*)m_caster
)->isAllowUseBattleGroundObject() )
3721 return SPELL_FAILED_TRY_AGAIN
;
3723 // get the lock entry
3724 LockEntry
const *lockInfo
= NULL
;
3725 if (GameObject
* go
=m_targets
.getGOTarget())
3726 lockInfo
= sLockStore
.LookupEntry(go
->GetLockId());
3727 else if(Item
* itm
=m_targets
.getItemTarget())
3728 lockInfo
= sLockStore
.LookupEntry(itm
->GetProto()->LockID
);
3730 // check lock compatibility
3733 // check for lock - key pair (checked by client also, just prevent cheating
3734 bool ok_key
= false;
3735 for(int it
= 0; it
< 8; ++it
)
3737 switch(lockInfo
->Type
[it
])
3743 if(lockInfo
->Index
[it
])
3745 if(m_CastItem
&& m_CastItem
->GetEntry()==lockInfo
->Index
[it
])
3750 case LOCK_KEY_SKILL
:
3752 if(uint32(m_spellInfo
->EffectMiscValue
[i
])!=lockInfo
->Index
[it
])
3755 switch(lockInfo
->Index
[it
])
3757 case LOCKTYPE_HERBALISM
:
3758 if(((Player
*)m_caster
)->HasSkill(SKILL_HERBALISM
))
3761 case LOCKTYPE_MINING
:
3762 if(((Player
*)m_caster
)->HasSkill(SKILL_MINING
))
3776 return SPELL_FAILED_BAD_TARGETS
;
3779 // chance for fail at orange mining/herb/LockPicking gathering attempt
3780 if (!m_selfContainer
|| ((*m_selfContainer
) != this))
3783 // get the skill value of the player
3784 int32 SkillValue
= 0;
3785 bool canFailAtMax
= true;
3786 if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_HERBALISM
)
3788 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_HERBALISM
);
3789 canFailAtMax
= false;
3791 else if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_MINING
)
3793 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_MINING
);
3794 canFailAtMax
= false;
3796 else if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_PICKLOCK
)
3797 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_LOCKPICKING
);
3799 // castitem check: rogue using skeleton keys. the skill values should not be added in this case.
3803 // add the damage modifier from the spell casted (cheat lock / skeleton key etc.) (use m_currentBasePoints, CalculateDamage returns wrong value)
3804 SkillValue
+= m_currentBasePoints
[i
]+1;
3806 // get the required lock value
3810 // check for lock - key pair
3812 for(int it
= 0; it
< 8; ++it
)
3814 if(lockInfo
->Type
[it
]==LOCK_KEY_ITEM
&& lockInfo
->Index
[it
] && m_CastItem
&& m_CastItem
->GetEntry()==lockInfo
->Index
[it
])
3816 // if so, we're good to go
3824 if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_PICKLOCK
)
3825 ReqValue
= lockInfo
->Skill
[1];
3827 ReqValue
= lockInfo
->Skill
[0];
3830 // skill doesn't meet the required value
3831 if (ReqValue
> SkillValue
)
3832 return SPELL_FAILED_LOW_CASTLEVEL
;
3834 // chance for failure in orange gather / lockpick (gathering skill can't fail at maxskill)
3835 if((canFailAtMax
|| SkillValue
< sWorld
.GetConfigMaxSkillValue()) && ReqValue
> irand(SkillValue
-25, SkillValue
+37))
3836 return SPELL_FAILED_TRY_AGAIN
;
3840 case SPELL_EFFECT_SUMMON_DEAD_PET
:
3842 Creature
*pet
= m_caster
->GetPet();
3844 return SPELL_FAILED_NO_PET
;
3847 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3851 // This is generic summon effect now and don't make this check for summon types similar
3852 // SPELL_EFFECT_SUMMON_CRITTER, SPELL_EFFECT_SUMMON_WILD or SPELL_EFFECT_SUMMON_GUARDIAN.
3853 // These won't show up in m_caster->GetPetGUID()
3854 case SPELL_EFFECT_SUMMON
:
3856 switch(m_spellInfo
->EffectMiscValueB
[i
])
3858 case SUMMON_TYPE_POSESSED
:
3859 case SUMMON_TYPE_POSESSED2
:
3860 case SUMMON_TYPE_DEMON
:
3861 case SUMMON_TYPE_SUMMON
:
3863 if(m_caster
->GetPetGUID())
3864 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3866 if(m_caster
->GetCharmGUID())
3867 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3873 // Don't make this check for SPELL_EFFECT_SUMMON_CRITTER, SPELL_EFFECT_SUMMON_WILD or SPELL_EFFECT_SUMMON_GUARDIAN.
3874 // These won't show up in m_caster->GetPetGUID()
3875 case SPELL_EFFECT_SUMMON_POSSESSED
:
3876 case SPELL_EFFECT_SUMMON_PHANTASM
:
3877 case SPELL_EFFECT_SUMMON_DEMON
:
3879 if(m_caster
->GetPetGUID())
3880 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3882 if(m_caster
->GetCharmGUID())
3883 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3887 case SPELL_EFFECT_SUMMON_PET
:
3889 if(m_caster
->GetPetGUID()) //let warlock do a replacement summon
3892 Pet
* pet
= ((Player
*)m_caster
)->GetPet();
3894 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& m_caster
->getClass()==CLASS_WARLOCK
)
3896 if (strict
) //starting cast, trigger pet stun (cast by pet so it doesn't attack player)
3897 pet
->CastSpell(pet
, 32752, true, NULL
, NULL
, pet
->GetGUID());
3900 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3903 if(m_caster
->GetCharmGUID())
3904 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3908 case SPELL_EFFECT_SUMMON_PLAYER
:
3910 if(m_caster
->GetTypeId()!=TYPEID_PLAYER
)
3911 return SPELL_FAILED_BAD_TARGETS
;
3912 if(!((Player
*)m_caster
)->GetSelection())
3913 return SPELL_FAILED_BAD_TARGETS
;
3915 Player
* target
= objmgr
.GetPlayer(((Player
*)m_caster
)->GetSelection());
3916 if( !target
|| ((Player
*)m_caster
)==target
|| !target
->IsInSameRaidWith((Player
*)m_caster
) )
3917 return SPELL_FAILED_BAD_TARGETS
;
3919 // check if our map is dungeon
3920 if( sMapStore
.LookupEntry(m_caster
->GetMapId())->IsDungeon() )
3922 InstanceTemplate
const* instance
= ObjectMgr::GetInstanceTemplate(m_caster
->GetMapId());
3924 return SPELL_FAILED_TARGET_NOT_IN_INSTANCE
;
3925 if ( instance
->levelMin
> target
->getLevel() )
3926 return SPELL_FAILED_LOWLEVEL
;
3927 if ( instance
->levelMax
&& instance
->levelMax
< target
->getLevel() )
3928 return SPELL_FAILED_HIGHLEVEL
;
3932 case SPELL_EFFECT_LEAP
:
3933 case SPELL_EFFECT_TELEPORT_UNITS_FACE_CASTER
:
3935 float dis
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[i
]));
3936 float fx
= m_caster
->GetPositionX() + dis
* cos(m_caster
->GetOrientation());
3937 float fy
= m_caster
->GetPositionY() + dis
* sin(m_caster
->GetOrientation());
3938 // teleport a bit above terrain level to avoid falling below it
3939 float fz
= MapManager::Instance().GetBaseMap(m_caster
->GetMapId())->GetHeight(fx
,fy
,m_caster
->GetPositionZ(),true);
3940 if(fz
<= INVALID_HEIGHT
) // note: this also will prevent use effect in instances without vmaps height enabled
3941 return SPELL_FAILED_TRY_AGAIN
;
3943 float caster_pos_z
= m_caster
->GetPositionZ();
3944 // Control the caster to not climb or drop when +-fz > 8
3945 if(!(fz
<=caster_pos_z
+8 && fz
>=caster_pos_z
-8))
3946 return SPELL_FAILED_TRY_AGAIN
;
3948 // not allow use this effect at battleground until battleground start
3949 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
3950 if(BattleGround
const *bg
= ((Player
*)m_caster
)->GetBattleGround())
3951 if(bg
->GetStatus() != STATUS_IN_PROGRESS
)
3952 return SPELL_FAILED_TRY_AGAIN
;
3955 case SPELL_EFFECT_STEAL_BENEFICIAL_BUFF
:
3957 if (m_targets
.getUnitTarget()==m_caster
)
3958 return SPELL_FAILED_BAD_TARGETS
;
3965 for (int i
= 0; i
< 3; i
++)
3967 switch(m_spellInfo
->EffectApplyAuraName
[i
])
3969 case SPELL_AURA_MOD_POSSESS
:
3970 case SPELL_AURA_MOD_CHARM
:
3972 if(m_caster
->GetPetGUID())
3973 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3975 if(m_caster
->GetCharmGUID())
3976 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3978 if(m_caster
->GetCharmerGUID())
3979 return SPELL_FAILED_CHARMED
;
3981 if(!m_targets
.getUnitTarget())
3982 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3984 if(m_targets
.getUnitTarget()->GetCharmerGUID())
3985 return SPELL_FAILED_CHARMED
;
3987 if(int32(m_targets
.getUnitTarget()->getLevel()) > CalculateDamage(i
,m_targets
.getUnitTarget()))
3988 return SPELL_FAILED_HIGHLEVEL
;
3990 case SPELL_AURA_MOUNTED
:
3992 if (m_caster
->IsInWater())
3993 return SPELL_FAILED_ONLY_ABOVEWATER
;
3995 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->GetTransport())
3996 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
3998 // Ignore map check if spell have AreaId. AreaId already checked and this prevent special mount spells
3999 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& !sMapStore
.LookupEntry(m_caster
->GetMapId())->IsMountAllowed() && !m_IsTriggeredSpell
&& !m_spellInfo
->AreaId
)
4000 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4002 if (m_caster
->GetAreaId()==35)
4003 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4005 ShapeshiftForm form
= m_caster
->m_form
;
4006 if( form
== FORM_CAT
|| form
== FORM_TREE
|| form
== FORM_TRAVEL
||
4007 form
== FORM_AQUA
|| form
== FORM_BEAR
|| form
== FORM_DIREBEAR
||
4008 form
== FORM_CREATUREBEAR
|| form
== FORM_GHOSTWOLF
|| form
== FORM_FLIGHT
||
4009 form
== FORM_FLIGHT_EPIC
|| form
== FORM_MOONKIN
)
4010 return SPELL_FAILED_NOT_SHAPESHIFT
;
4014 case SPELL_AURA_RANGED_ATTACK_POWER_ATTACKER_BONUS
:
4016 if(!m_targets
.getUnitTarget())
4017 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4019 // can be casted at non-friendly unit or own pet/charm
4020 if(m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()))
4021 return SPELL_FAILED_TARGET_FRIENDLY
;
4023 case SPELL_AURA_MOD_INCREASE_FLIGHT_SPEED
:
4024 case SPELL_AURA_FLY
:
4026 // not allow cast fly spells at old maps by players (all spells is self target)
4027 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
4029 if( !((Player
*)m_caster
)->isGameMaster() &&
4030 GetVirtualMapForMapAndZone(m_caster
->GetMapId(),m_caster
->GetZoneId()) != 530)
4031 return SPELL_FAILED_NOT_HERE
;
4034 case SPELL_AURA_PERIODIC_MANA_LEECH
:
4036 if (!m_targets
.getUnitTarget())
4037 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4039 if (m_caster
->GetTypeId()!=TYPEID_PLAYER
|| m_CastItem
)
4042 if(m_targets
.getUnitTarget()->getPowerType()!=POWER_MANA
)
4043 return SPELL_FAILED_BAD_TARGETS
;
4054 int16
Spell::PetCanCast(Unit
* target
)
4056 if(!m_caster
->isAlive())
4057 return SPELL_FAILED_CASTER_DEAD
;
4059 if(m_caster
->IsNonMeleeSpellCasted(false)) //prevent spellcast interruption by another spellcast
4060 return SPELL_FAILED_SPELL_IN_PROGRESS
;
4061 if(m_caster
->isInCombat() && IsNonCombatSpell(m_spellInfo
))
4062 return SPELL_FAILED_AFFECTING_COMBAT
;
4064 if(m_caster
->GetTypeId()==TYPEID_UNIT
&& (((Creature
*)m_caster
)->isPet() || m_caster
->isCharmed()))
4066 //dead owner (pets still alive when owners ressed?)
4067 if(m_caster
->GetCharmerOrOwner() && !m_caster
->GetCharmerOrOwner()->isAlive())
4068 return SPELL_FAILED_CASTER_DEAD
;
4070 if(!target
&& m_targets
.getUnitTarget())
4071 target
= m_targets
.getUnitTarget();
4074 for(uint32 i
= 0;i
<3;i
++)
4076 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
)
4080 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4085 m_targets
.setUnitTarget(target
);
4087 Unit
* _target
= m_targets
.getUnitTarget();
4089 if(_target
) //for target dead/target not valid
4091 if(!_target
->isAlive())
4092 return SPELL_FAILED_BAD_TARGETS
;
4094 if(IsPositiveSpell(m_spellInfo
->Id
))
4096 if(m_caster
->IsHostileTo(_target
))
4097 return SPELL_FAILED_BAD_TARGETS
;
4101 bool duelvsplayertar
= false;
4102 for(int j
=0;j
<3;j
++)
4104 //TARGET_DUELVSPLAYER is positive AND negative
4105 duelvsplayertar
|= (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_DUELVSPLAYER
);
4107 if(m_caster
->IsFriendlyTo(target
) && !duelvsplayertar
)
4109 return SPELL_FAILED_BAD_TARGETS
;
4114 if(((Creature
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
4115 return SPELL_FAILED_NOT_READY
;
4118 uint16 result
= CanCast(true);
4122 return -1; //this allows to check spell fail 0, in combat
4125 uint8
Spell::CheckCasterAuras() const
4127 // Flag drop spells totally immuned to caster auras
4128 // FIXME: find more nice check for all totally immuned spells
4129 // AttributesEx3 & 0x10000000?
4130 if(m_spellInfo
->Id
==23336 || m_spellInfo
->Id
==23334 || m_spellInfo
->Id
==34991)
4133 uint8 school_immune
= 0;
4134 uint32 mechanic_immune
= 0;
4135 uint32 dispel_immune
= 0;
4137 //Check if the spell grants school or mechanic immunity.
4138 //We use bitmasks so the loop is done only once and not on every aura check below.
4139 if ( m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DISPEL_AURAS_ON_IMMUNITY
)
4141 for(int i
= 0;i
< 3; i
++)
4143 if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_SCHOOL_IMMUNITY
)
4144 school_immune
|= uint32(m_spellInfo
->EffectMiscValue
[i
]);
4145 else if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_MECHANIC_IMMUNITY
)
4146 mechanic_immune
|= 1 << uint32(m_spellInfo
->EffectMiscValue
[i
]);
4147 else if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_DISPEL_IMMUNITY
)
4148 dispel_immune
|= GetDispellMask(DispelType(m_spellInfo
->EffectMiscValue
[i
]));
4150 //immune movement impairment and loss of control
4151 if(m_spellInfo
->Id
==(uint32
)42292)
4152 mechanic_immune
= IMMUNE_TO_MOVEMENT_IMPAIRMENT_AND_LOSS_CONTROL_MASK
;
4155 //Check whether the cast should be prevented by any state you might have.
4156 uint8 prevented_reason
= 0;
4157 // Have to check if there is a stun aura. Otherwise will have problems with ghost aura apply while logging out
4158 if(!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
) && m_caster
->HasAuraType(SPELL_AURA_MOD_STUN
))
4159 prevented_reason
= SPELL_FAILED_STUNNED
;
4160 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_CONFUSED
) && !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
4161 prevented_reason
= SPELL_FAILED_CONFUSED
;
4162 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_FLEEING
) && !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
4163 prevented_reason
= SPELL_FAILED_FLEEING
;
4164 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_SILENCED
) && m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_SILENCE
)
4165 prevented_reason
= SPELL_FAILED_SILENCED
;
4166 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PACIFIED
) && m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_PACIFY
)
4167 prevented_reason
= SPELL_FAILED_PACIFIED
;
4169 // Attr must make flag drop spell totally immune from all effects
4170 if(prevented_reason
)
4172 if(school_immune
|| mechanic_immune
|| dispel_immune
)
4174 //Checking auras is needed now, because you are prevented by some state but the spell grants immunity.
4175 Unit::AuraMap
const& auras
= m_caster
->GetAuras();
4176 for(Unit::AuraMap::const_iterator itr
= auras
.begin(); itr
!= auras
.end(); itr
++)
4180 if( GetSpellMechanicMask(itr
->second
->GetSpellProto(), itr
->second
->GetEffIndex()) & mechanic_immune
)
4182 if( GetSpellSchoolMask(itr
->second
->GetSpellProto()) & school_immune
)
4184 if( (1<<(itr
->second
->GetSpellProto()->Dispel
)) & dispel_immune
)
4187 //Make a second check for spell failed so the right SPELL_FAILED message is returned.
4188 //That is needed when your casting is prevented by multiple states and you are only immune to some of them.
4189 switch(itr
->second
->GetModifier()->m_auraname
)
4191 case SPELL_AURA_MOD_STUN
:
4192 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
))
4193 return SPELL_FAILED_STUNNED
;
4195 case SPELL_AURA_MOD_CONFUSE
:
4196 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
4197 return SPELL_FAILED_CONFUSED
;
4199 case SPELL_AURA_MOD_FEAR
:
4200 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
4201 return SPELL_FAILED_FLEEING
;
4203 case SPELL_AURA_MOD_SILENCE
:
4204 case SPELL_AURA_MOD_PACIFY
:
4205 case SPELL_AURA_MOD_PACIFY_SILENCE
:
4206 if( m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_PACIFY
)
4207 return SPELL_FAILED_PACIFIED
;
4208 else if ( m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_SILENCE
)
4209 return SPELL_FAILED_SILENCED
;
4215 //You are prevented from casting and the spell casted does not grant immunity. Return a failed error.
4217 return prevented_reason
;
4222 bool Spell::CanAutoCast(Unit
* target
)
4224 uint64 targetguid
= target
->GetGUID();
4226 for(uint32 j
= 0;j
<3;j
++)
4228 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_APPLY_AURA
)
4230 if( m_spellInfo
->StackAmount
<= 1)
4232 if( target
->HasAura(m_spellInfo
->Id
, j
) )
4237 if( target
->GetAuras().count(Unit::spellEffectPair(m_spellInfo
->Id
, j
)) >= m_spellInfo
->StackAmount
)
4241 else if ( IsAreaAuraEffect( m_spellInfo
->Effect
[j
] ))
4243 if( target
->HasAura(m_spellInfo
->Id
, j
) )
4248 int16 result
= PetCanCast(target
);
4250 if(result
== -1 || result
== SPELL_FAILED_UNIT_NOT_INFRONT
)
4253 //check if among target units, our WANTED target is as well (->only self cast spells return false)
4254 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
4255 if( ihit
->targetGUID
== targetguid
)
4258 return false; //target invalid
4261 uint8
Spell::CheckRange(bool strict
)
4265 // self cast doesn't need range checking -- also for Starshards fix
4266 if (m_spellInfo
->rangeIndex
== 1) return 0;
4268 if (strict
) //add radius of caster
4270 else //add radius of caster and ~5 yds "give"
4273 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
4274 float max_range
= GetSpellMaxRange(srange
) + range_mod
;
4275 float min_range
= GetSpellMinRange(srange
);
4277 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
4278 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_RANGE
, max_range
, this);
4280 Unit
*target
= m_targets
.getUnitTarget();
4282 if(target
&& target
!= m_caster
)
4284 // distance from target center in checks
4285 float dist
= m_caster
->GetDistance(target
->GetPositionX(),target
->GetPositionY(),target
->GetPositionZ());
4286 if(dist
> max_range
)
4287 return SPELL_FAILED_OUT_OF_RANGE
; //0x5A;
4288 if(dist
< min_range
)
4289 return SPELL_FAILED_TOO_CLOSE
;
4290 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&&
4291 (m_spellInfo
->FacingCasterFlags
& SPELL_FACING_FLAG_INFRONT
) && !m_caster
->HasInArc( M_PI
, target
) )
4292 return SPELL_FAILED_UNIT_NOT_INFRONT
;
4295 if(m_targets
.m_targetMask
== TARGET_FLAG_DEST_LOCATION
&& m_targets
.m_destX
!= 0 && m_targets
.m_destY
!= 0 && m_targets
.m_destZ
!= 0)
4297 float dist
= m_caster
->GetDistance(m_targets
.m_destX
, m_targets
.m_destY
, m_targets
.m_destZ
);
4298 if(dist
> max_range
)
4299 return SPELL_FAILED_OUT_OF_RANGE
;
4300 if(dist
< min_range
)
4301 return SPELL_FAILED_TOO_CLOSE
;
4307 int32
Spell::CalculatePowerCost()
4309 // item cast not used power
4313 // Spell drain all exist power on cast (Only paladin lay of Hands)
4314 if (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DRAIN_ALL_POWER
)
4316 // If power type - health drain all
4317 if (m_spellInfo
->powerType
== POWER_HEALTH
)
4318 return m_caster
->GetHealth();
4319 // Else drain all power
4320 if (m_spellInfo
->powerType
< MAX_POWERS
)
4321 return m_caster
->GetPower(Powers(m_spellInfo
->powerType
));
4322 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
4327 int32 powerCost
= m_spellInfo
->manaCost
;
4328 // PCT cost from total amount
4329 if (m_spellInfo
->ManaCostPercentage
)
4331 switch (m_spellInfo
->powerType
)
4333 // health as power used
4335 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateHealth() / 100;
4338 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateMana() / 100;
4343 case POWER_HAPPINESS
:
4344 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetMaxPower(Powers(m_spellInfo
->powerType
)) / 100;
4347 case POWER_RUNIC_POWER
:
4348 sLog
.outDebug("Spell::CalculateManaCost: Not implemented yet!");
4351 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
4355 SpellSchools school
= GetFirstSchoolInMask(m_spellSchoolMask
);
4356 // Flat mod from caster auras by spell school
4357 powerCost
+= m_caster
->GetInt32Value(UNIT_FIELD_POWER_COST_MODIFIER
+ school
);
4358 // Shiv - costs 20 + weaponSpeed*10 energy (apply only to non-triggered spell with energy cost)
4359 if ( m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_SPELL_VS_EXTEND_COST
)
4360 powerCost
+= m_caster
->GetAttackTime(OFF_ATTACK
)/100;
4361 // Apply cost mod by spell
4362 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
4363 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COST
, powerCost
, this);
4365 if(m_spellInfo
->Attributes
& SPELL_ATTR_LEVEL_DAMAGE_CALCULATION
)
4366 powerCost
= int32(powerCost
/ (1.117f
* m_spellInfo
->spellLevel
/ m_caster
->getLevel() -0.1327f
));
4368 // PCT mod from user auras by school
4369 powerCost
= int32(powerCost
* (1.0f
+m_caster
->GetFloatValue(UNIT_FIELD_POWER_COST_MULTIPLIER
+school
)));
4375 uint8
Spell::CheckPower()
4377 // item cast not used power
4381 // health as power used - need check health amount
4382 if(m_spellInfo
->powerType
== POWER_HEALTH
)
4384 if(m_caster
->GetHealth() <= m_powerCost
)
4385 return SPELL_FAILED_CASTER_AURASTATE
;
4388 // Check valid power type
4389 if( m_spellInfo
->powerType
>= MAX_POWERS
)
4391 sLog
.outError("Spell::CheckMana: Unknown power type '%d'", m_spellInfo
->powerType
);
4392 return SPELL_FAILED_UNKNOWN
;
4394 // Check power amount
4395 Powers powerType
= Powers(m_spellInfo
->powerType
);
4396 if(m_caster
->GetPower(powerType
) < m_powerCost
)
4397 return SPELL_FAILED_NO_POWER
;
4402 uint8
Spell::CheckItems()
4404 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
4407 uint32 itemid
, itemcount
;
4408 Player
* p_caster
= (Player
*)m_caster
;
4412 itemid
= m_CastItem
->GetEntry();
4413 if( !p_caster
->HasItemCount(itemid
,1) )
4414 return SPELL_FAILED_ITEM_NOT_READY
;
4417 ItemPrototype
const *proto
= m_CastItem
->GetProto();
4419 return SPELL_FAILED_ITEM_NOT_READY
;
4421 for (int i
= 0; i
<5; i
++)
4423 if (proto
->Spells
[i
].SpellCharges
)
4425 if(m_CastItem
->GetSpellCharges(i
)==0)
4426 return SPELL_FAILED_NO_CHARGES_REMAIN
;
4430 uint32 ItemClass
= proto
->Class
;
4431 if (ItemClass
== ITEM_CLASS_CONSUMABLE
&& m_targets
.getUnitTarget())
4433 for (int i
= 0; i
< 3; i
++)
4435 // skip check, pet not required like checks, and for TARGET_PET m_targets.getUnitTarget() is not the real target but the caster
4436 if (m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_PET
)
4439 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_HEAL
)
4440 if (m_targets
.getUnitTarget()->GetHealth() == m_targets
.getUnitTarget()->GetMaxHealth())
4441 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_HEALTH
;
4443 // Mana Potion, Rage Potion, Thistle Tea(Rogue), ...
4444 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_ENERGIZE
)
4446 if(m_spellInfo
->EffectMiscValue
[i
] < 0 || m_spellInfo
->EffectMiscValue
[i
] >= MAX_POWERS
)
4447 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_POWER
;
4449 Powers power
= Powers(m_spellInfo
->EffectMiscValue
[i
]);
4451 if (m_targets
.getUnitTarget()->GetPower(power
) == m_targets
.getUnitTarget()->GetMaxPower(power
))
4452 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_POWER
;
4459 if(m_targets
.getItemTargetGUID())
4461 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
4462 return SPELL_FAILED_BAD_TARGETS
;
4464 if(!m_targets
.getItemTarget())
4465 return SPELL_FAILED_ITEM_GONE
;
4467 if(!m_targets
.getItemTarget()->IsFitToSpellRequirements(m_spellInfo
))
4468 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
4470 // if not item target then required item must be equipped
4473 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& !((Player
*)m_caster
)->HasItemFitToSpellReqirements(m_spellInfo
))
4474 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
4477 if(m_spellInfo
->RequiresSpellFocus
)
4479 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
4481 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
4483 GameObject
* ok
= NULL
;
4484 MaNGOS::GameObjectFocusCheck
go_check(m_caster
,m_spellInfo
->RequiresSpellFocus
);
4485 MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
> checker(ok
,go_check
);
4487 TypeContainerVisitor
<MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
>, GridTypeMapContainer
> object_checker(checker
);
4488 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
4489 cell_lock
->Visit(cell_lock
, object_checker
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
4492 return (uint8
)SPELL_FAILED_REQUIRES_SPELL_FOCUS
;
4494 focusObject
= ok
; // game object found in range
4497 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_NO_REAGENT_WHILE_PREP
&&
4498 m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PREPARATION
)))
4500 for(uint32 i
=0;i
<8;i
++)
4502 if(m_spellInfo
->Reagent
[i
] <= 0)
4505 itemid
= m_spellInfo
->Reagent
[i
];
4506 itemcount
= m_spellInfo
->ReagentCount
[i
];
4508 // if CastItem is also spell reagent
4509 if( m_CastItem
&& m_CastItem
->GetEntry() == itemid
)
4511 ItemPrototype
const *proto
= m_CastItem
->GetProto();
4513 return SPELL_FAILED_ITEM_NOT_READY
;
4514 for(int s
=0;s
<5;s
++)
4516 // CastItem will be used up and does not count as reagent
4517 int32 charges
= m_CastItem
->GetSpellCharges(s
);
4518 if (proto
->Spells
[s
].SpellCharges
< 0 && abs(charges
) < 2)
4525 if( !p_caster
->HasItemCount(itemid
,itemcount
) )
4526 return (uint8
)SPELL_FAILED_ITEM_NOT_READY
; //0x54
4531 for(int i
=0;i
<2;++i
)
4533 if(m_spellInfo
->Totem
[i
] != 0)
4535 if( p_caster
->HasItemCount(m_spellInfo
->Totem
[i
],1) )
4544 return (uint8
)SPELL_FAILED_TOTEMS
; //0x7C
4546 //Check items for TotemCategory
4547 uint32 TotemCategory
= 2;
4548 for(int i
=0;i
<2;++i
)
4550 if(m_spellInfo
->TotemCategory
[i
] != 0)
4552 if( p_caster
->HasItemTotemCategory(m_spellInfo
->TotemCategory
[i
]) )
4561 if(TotemCategory
!= 0)
4562 return (uint8
)SPELL_FAILED_TOTEM_CATEGORY
; //0x7B
4564 for(int i
= 0; i
< 3; i
++)
4566 switch (m_spellInfo
->Effect
[i
])
4568 case SPELL_EFFECT_CREATE_ITEM
:
4570 if (!m_IsTriggeredSpell
&& m_spellInfo
->EffectItemType
[i
])
4572 ItemPosCountVec dest
;
4573 uint8 msg
= p_caster
->CanStoreNewItem(NULL_BAG
, NULL_SLOT
, dest
, m_spellInfo
->EffectItemType
[i
], 1 );
4574 if (msg
!= EQUIP_ERR_OK
)
4576 p_caster
->SendEquipError( msg
, NULL
, NULL
);
4577 return SPELL_FAILED_DONT_REPORT
;
4582 case SPELL_EFFECT_ENCHANT_ITEM
:
4584 Item
* targetItem
= m_targets
.getItemTarget();
4586 return SPELL_FAILED_ITEM_NOT_FOUND
;
4588 if( targetItem
->GetProto()->ItemLevel
< m_spellInfo
->baseLevel
)
4589 return SPELL_FAILED_LOWLEVEL
;
4590 // Not allow enchant in trade slot for some enchant type
4591 if( targetItem
->GetOwner() != m_caster
)
4593 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
4594 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
4596 return SPELL_FAILED_ERROR
;
4597 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
4598 return SPELL_FAILED_NOT_TRADEABLE
;
4602 case SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY
:
4604 Item
*item
= m_targets
.getItemTarget();
4606 return SPELL_FAILED_ITEM_NOT_FOUND
;
4607 // Not allow enchant in trade slot for some enchant type
4608 if( item
->GetOwner() != m_caster
)
4610 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
4611 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
4613 return SPELL_FAILED_ERROR
;
4614 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
4615 return SPELL_FAILED_NOT_TRADEABLE
;
4619 case SPELL_EFFECT_ENCHANT_HELD_ITEM
:
4620 // check item existence in effect code (not output errors at offhand hold item effect to main hand for example
4622 case SPELL_EFFECT_DISENCHANT
:
4624 if(!m_targets
.getItemTarget())
4625 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4627 // prevent disenchanting in trade slot
4628 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4629 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4631 ItemPrototype
const* itemProto
= m_targets
.getItemTarget()->GetProto();
4633 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4635 uint32 item_quality
= itemProto
->Quality
;
4636 // 2.0.x addon: Check player enchanting level against the item disenchanting requirements
4637 uint32 item_disenchantskilllevel
= itemProto
->RequiredDisenchantSkill
;
4638 if (item_disenchantskilllevel
== uint32(-1))
4639 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4640 if (item_disenchantskilllevel
> p_caster
->GetSkillValue(SKILL_ENCHANTING
))
4641 return SPELL_FAILED_LOW_CASTLEVEL
;
4642 if(item_quality
> 4 || item_quality
< 2)
4643 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4644 if(itemProto
->Class
!= ITEM_CLASS_WEAPON
&& itemProto
->Class
!= ITEM_CLASS_ARMOR
)
4645 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4646 if (!itemProto
->DisenchantID
)
4647 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4650 case SPELL_EFFECT_PROSPECTING
:
4652 if(!m_targets
.getItemTarget())
4653 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4654 //ensure item is a prospectable ore
4655 if(!(m_targets
.getItemTarget()->GetProto()->BagFamily
& BAG_FAMILY_MASK_MINING_SUPP
) || m_targets
.getItemTarget()->GetProto()->Class
!= ITEM_CLASS_TRADE_GOODS
)
4656 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4657 //prevent prospecting in trade slot
4658 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4659 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4660 //Check for enough skill in jewelcrafting
4661 uint32 item_prospectingskilllevel
= m_targets
.getItemTarget()->GetProto()->RequiredSkillRank
;
4662 if(item_prospectingskilllevel
>p_caster
->GetSkillValue(SKILL_JEWELCRAFTING
))
4663 return SPELL_FAILED_LOW_CASTLEVEL
;
4664 //make sure the player has the required ores in inventory
4665 if(m_targets
.getItemTarget()->GetCount() < 5)
4666 return SPELL_FAILED_NEED_MORE_ITEMS
;
4668 if(!LootTemplates_Prospecting
.HaveLootFor(m_targets
.getItemTargetEntry()))
4669 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4673 case SPELL_EFFECT_WEAPON_DAMAGE
:
4674 case SPELL_EFFECT_WEAPON_DAMAGE_NOSCHOOL
:
4676 if(m_caster
->GetTypeId() != TYPEID_PLAYER
) return SPELL_FAILED_TARGET_NOT_PLAYER
;
4677 if( m_attackType
!= RANGED_ATTACK
)
4679 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack(m_attackType
);
4680 if(!pItem
|| pItem
->IsBroken())
4681 return SPELL_FAILED_EQUIPPED_ITEM
;
4683 switch(pItem
->GetProto()->SubClass
)
4685 case ITEM_SUBCLASS_WEAPON_THROWN
:
4687 uint32 ammo
= pItem
->GetEntry();
4688 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
4689 return SPELL_FAILED_NO_AMMO
;
4691 case ITEM_SUBCLASS_WEAPON_GUN
:
4692 case ITEM_SUBCLASS_WEAPON_BOW
:
4693 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
4695 uint32 ammo
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
4699 if(m_caster
->GetDummyAura(46699))
4700 break; // skip other checks
4702 return SPELL_FAILED_NO_AMMO
;
4705 ItemPrototype
const *ammoProto
= objmgr
.GetItemPrototype( ammo
);
4707 return SPELL_FAILED_NO_AMMO
;
4709 if(ammoProto
->Class
!= ITEM_CLASS_PROJECTILE
)
4710 return SPELL_FAILED_NO_AMMO
;
4712 // check ammo ws. weapon compatibility
4713 switch(pItem
->GetProto()->SubClass
)
4715 case ITEM_SUBCLASS_WEAPON_BOW
:
4716 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
4717 if(ammoProto
->SubClass
!=ITEM_SUBCLASS_ARROW
)
4718 return SPELL_FAILED_NO_AMMO
;
4720 case ITEM_SUBCLASS_WEAPON_GUN
:
4721 if(ammoProto
->SubClass
!=ITEM_SUBCLASS_BULLET
)
4722 return SPELL_FAILED_NO_AMMO
;
4725 return SPELL_FAILED_NO_AMMO
;
4728 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
4729 return SPELL_FAILED_NO_AMMO
;
4731 case ITEM_SUBCLASS_WEAPON_WAND
:
4744 void Spell::Delayed()
4746 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
4749 if (m_spellState
== SPELL_STATE_DELAYED
)
4750 return; // spell is active and can't be time-backed
4752 // spells not loosing casting time ( slam, dynamites, bombs.. )
4753 if(!(m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_DAMAGE
))
4756 //check resist chance
4757 int32 resistChance
= 100; //must be initialized to 100 for percent modifiers
4758 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
,SPELLMOD_NOT_LOSE_CASTING_TIME
,resistChance
, this);
4759 resistChance
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_RESIST_PUSHBACK
) - 100;
4760 if (roll_chance_i(resistChance
))
4763 int32 delaytime
= GetNextDelayAtDamageMsTime();
4765 if(int32(m_timer
) + delaytime
> m_casttime
)
4767 delaytime
= m_casttime
- m_timer
;
4768 m_timer
= m_casttime
;
4771 m_timer
+= delaytime
;
4773 sLog
.outDetail("Spell %u partially interrupted for (%d) ms at damage",m_spellInfo
->Id
,delaytime
);
4775 WorldPacket
data(SMSG_SPELL_DELAYED
, 8+4);
4776 data
.append(m_caster
->GetPackGUID());
4777 data
<< uint32(delaytime
);
4779 m_caster
->SendMessageToSet(&data
,true);
4782 void Spell::DelayedChannel()
4784 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
|| getState() != SPELL_STATE_CASTING
)
4787 //check resist chance
4788 int32 resistChance
= 100; //must be initialized to 100 for percent modifiers
4789 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
,SPELLMOD_NOT_LOSE_CASTING_TIME
,resistChance
, this);
4790 resistChance
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_RESIST_PUSHBACK
) - 100;
4791 if (roll_chance_i(resistChance
))
4794 int32 delaytime
= GetNextDelayAtDamageMsTime();
4796 if(int32(m_timer
) < delaytime
)
4798 delaytime
= m_timer
;
4802 m_timer
-= delaytime
;
4804 sLog
.outDebug("Spell %u partially interrupted for %i ms, new duration: %u ms", m_spellInfo
->Id
, delaytime
, m_timer
);
4806 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
4808 if ((*ihit
).missCondition
== SPELL_MISS_NONE
)
4810 Unit
* unit
= m_caster
->GetGUID()==ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
4813 for (int j
=0;j
<3;j
++)
4814 if( ihit
->effectMask
& (1<<j
) )
4815 unit
->DelayAura(m_spellInfo
->Id
, j
, delaytime
);
4821 for(int j
= 0; j
< 3; j
++)
4823 // partially interrupt persistent area auras
4824 DynamicObject
* dynObj
= m_caster
->GetDynObject(m_spellInfo
->Id
, j
);
4826 dynObj
->Delay(delaytime
);
4829 SendChannelUpdate(m_timer
);
4832 void Spell::UpdatePointers()
4834 if(m_originalCasterGUID
==m_caster
->GetGUID())
4835 m_originalCaster
= m_caster
;
4838 m_originalCaster
= ObjectAccessor::GetUnit(*m_caster
,m_originalCasterGUID
);
4839 if(m_originalCaster
&& !m_originalCaster
->IsInWorld()) m_originalCaster
= NULL
;
4842 m_targets
.Update(m_caster
);
4845 bool Spell::IsAffectedBy(SpellEntry
const *spellInfo
, uint32 effectId
)
4847 return spellmgr
.IsAffectedBySpell(m_spellInfo
,spellInfo
->Id
,effectId
,spellInfo
->EffectItemType
[effectId
]);
4850 bool Spell::CheckTargetCreatureType(Unit
* target
) const
4852 uint32 spellCreatureTargetMask
= m_spellInfo
->TargetCreatureType
;
4854 // Curse of Doom : not find another way to fix spell target check :/
4855 if(m_spellInfo
->SpellFamilyName
==SPELLFAMILY_WARLOCK
&& m_spellInfo
->SpellFamilyFlags
== 0x0200000000LL
)
4857 // not allow cast at player
4858 if(target
->GetTypeId()==TYPEID_PLAYER
)
4861 spellCreatureTargetMask
= 0x7FF;
4864 // Dismiss Pet and Taming Lesson skipped
4865 if(m_spellInfo
->Id
== 2641 || m_spellInfo
->Id
== 23356)
4866 spellCreatureTargetMask
= 0;
4868 if (spellCreatureTargetMask
)
4870 uint32 TargetCreatureType
= target
->GetCreatureTypeMask();
4872 return !TargetCreatureType
|| (spellCreatureTargetMask
& TargetCreatureType
);
4877 CurrentSpellTypes
Spell::GetCurrentContainer()
4879 if (IsNextMeleeSwingSpell())
4880 return(CURRENT_MELEE_SPELL
);
4881 else if (IsAutoRepeat())
4882 return(CURRENT_AUTOREPEAT_SPELL
);
4883 else if (IsChanneledSpell(m_spellInfo
))
4884 return(CURRENT_CHANNELED_SPELL
);
4886 return(CURRENT_GENERIC_SPELL
);
4889 bool Spell::CheckTarget( Unit
* target
, uint32 eff
)
4891 // Check targets for creature type mask and remove not appropriate (skip explicit self target case, maybe need other explicit targets)
4892 if(m_spellInfo
->EffectImplicitTargetA
[eff
]!=TARGET_SELF
)
4894 if (!CheckTargetCreatureType(target
))
4898 // Check targets for not_selectable unit flag and remove
4899 // A player can cast spells on his pet (or other controlled unit) though in any state
4900 if (target
!= m_caster
&& target
->GetCharmerOrOwnerGUID() != m_caster
->GetGUID())
4902 // any unattackable target skipped
4903 if (target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NON_ATTACKABLE
))
4906 // unselectable targets skipped in all cases except TARGET_SCRIPT targeting
4907 // in case TARGET_SCRIPT target selected by server always and can't be cheated
4908 if( target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NOT_SELECTABLE
) &&
4909 m_spellInfo
->EffectImplicitTargetA
[eff
] != TARGET_SCRIPT
&&
4910 m_spellInfo
->EffectImplicitTargetB
[eff
] != TARGET_SCRIPT
)
4914 //Check player targets and remove if in GM mode or GM invisibility (for not self casting case)
4915 if( target
!= m_caster
&& target
->GetTypeId()==TYPEID_PLAYER
)
4917 if(((Player
*)target
)->GetVisibility()==VISIBILITY_OFF
)
4920 if(((Player
*)target
)->isGameMaster() && !IsPositiveSpell(m_spellInfo
->Id
))
4924 //Check targets for LOS visibility (except spells without range limitations )
4925 switch(m_spellInfo
->Effect
[eff
])
4927 case SPELL_EFFECT_SUMMON_PLAYER
: // from anywhere
4929 case SPELL_EFFECT_DUMMY
:
4930 if(m_spellInfo
->Id
!=20577) // Cannibalize
4933 case SPELL_EFFECT_RESURRECT_NEW
:
4934 // player far away, maybe his corpse near?
4935 if(target
!=m_caster
&& !target
->IsWithinLOSInMap(m_caster
))
4937 if(!m_targets
.getCorpseTargetGUID())
4940 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
4944 if(target
->GetGUID()!=corpse
->GetOwnerGUID())
4947 if(!corpse
->IsWithinLOSInMap(m_caster
))
4951 // all ok by some way or another, skip normal check
4953 default: // normal case
4954 if(target
!=m_caster
&& !target
->IsWithinLOSInMap(m_caster
))
4962 Unit
* Spell::SelectMagnetTarget()
4964 Unit
* target
= m_targets
.getUnitTarget();
4966 if(target
&& target
->HasAuraType(SPELL_AURA_SPELL_MAGNET
) && !(m_spellInfo
->Attributes
& 0x10))
4968 Unit::AuraList
const& magnetAuras
= target
->GetAurasByType(SPELL_AURA_SPELL_MAGNET
);
4969 for(Unit::AuraList::const_iterator itr
= magnetAuras
.begin(); itr
!= magnetAuras
.end(); ++itr
)
4971 if(Unit
* magnet
= (*itr
)->GetCaster())
4973 if(magnet
->IsWithinLOSInMap(m_caster
))
4976 m_targets
.setUnitTarget(target
);
4986 bool Spell::IsNeedSendToClient() const
4988 return m_spellInfo
->SpellVisual
!=0 || IsChanneledSpell(m_spellInfo
) ||
4989 m_spellInfo
->speed
> 0.0f
|| !m_triggeredByAuraSpell
&& !m_IsTriggeredSpell
;
4992 bool Spell::HaveTargetsForEffect( uint8 effect
) const
4994 for(std::list
<TargetInfo
>::const_iterator itr
= m_UniqueTargetInfo
.begin();itr
!= m_UniqueTargetInfo
.end();++itr
)
4995 if(itr
->effectMask
& (1<<effect
))
4998 for(std::list
<GOTargetInfo
>::const_iterator itr
= m_UniqueGOTargetInfo
.begin();itr
!= m_UniqueGOTargetInfo
.end();++itr
)
4999 if(itr
->effectMask
& (1<<effect
))
5002 for(std::list
<ItemTargetInfo
>::const_iterator itr
= m_UniqueItemInfo
.begin();itr
!= m_UniqueItemInfo
.end();++itr
)
5003 if(itr
->effectMask
& (1<<effect
))
5009 SpellEvent::SpellEvent(Spell
* spell
) : BasicEvent()
5014 SpellEvent::~SpellEvent()
5016 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5019 if (m_Spell
->IsDeletable())
5025 sLog
.outError("~SpellEvent: %s %u tried to delete non-deletable spell %u. Was not deleted, causes memory leak.",
5026 (m_Spell
->GetCaster()->GetTypeId()==TYPEID_PLAYER
?"Player":"Creature"), m_Spell
->GetCaster()->GetGUIDLow(),m_Spell
->m_spellInfo
->Id
);
5030 bool SpellEvent::Execute(uint64 e_time
, uint32 p_time
)
5032 // update spell if it is not finished
5033 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5034 m_Spell
->update(p_time
);
5036 // check spell state to process
5037 switch (m_Spell
->getState())
5039 case SPELL_STATE_FINISHED
:
5041 // spell was finished, check deletable state
5042 if (m_Spell
->IsDeletable())
5044 // check, if we do have unfinished triggered spells
5046 return(true); // spell is deletable, finish event
5048 // event will be re-added automatically at the end of routine)
5051 case SPELL_STATE_CASTING
:
5053 // this spell is in channeled state, process it on the next update
5054 // event will be re-added automatically at the end of routine)
5057 case SPELL_STATE_DELAYED
:
5059 // first, check, if we have just started
5060 if (m_Spell
->GetDelayStart() != 0)
5062 // no, we aren't, do the typical update
5063 // check, if we have channeled spell on our hands
5064 if (IsChanneledSpell(m_Spell
->m_spellInfo
))
5066 // evented channeled spell is processed separately, casted once after delay, and not destroyed till finish
5067 // check, if we have casting anything else except this channeled spell and autorepeat
5068 if (m_Spell
->GetCaster()->IsNonMeleeSpellCasted(false, true, true))
5070 // another non-melee non-delayed spell is casted now, abort
5075 // do the action (pass spell to channeling state)
5076 m_Spell
->handle_immediate();
5078 // event will be re-added automatically at the end of routine)
5082 // run the spell handler and think about what we can do next
5083 uint64 t_offset
= e_time
- m_Spell
->GetDelayStart();
5084 uint64 n_offset
= m_Spell
->handle_delayed(t_offset
);
5087 // re-add us to the queue
5088 m_Spell
->GetCaster()->m_Events
.AddEvent(this, m_Spell
->GetDelayStart() + n_offset
, false);
5089 return(false); // event not complete
5092 // finish update event will be re-added automatically at the end of routine)
5097 // delaying had just started, record the moment
5098 m_Spell
->SetDelayStart(e_time
);
5099 // re-plan the event for the delay moment
5100 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ m_Spell
->GetDelayMoment(), false);
5101 return(false); // event not complete
5108 // event will be re-added automatically at the end of routine)
5112 // spell processing not complete, plan event on the next update interval
5113 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ 1, false);
5114 return(false); // event not complete
5117 void SpellEvent::Abort(uint64
/*e_time*/)
5119 // oops, the spell we try to do is aborted
5120 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5124 bool SpellEvent::IsDeletable() const
5126 return m_Spell
->IsDeletable();