2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "Database/DatabaseEnv.h"
21 #include "WorldPacket.h"
22 #include "WorldSession.h"
23 #include "GridNotifiers.h"
24 #include "GridNotifiersImpl.h"
27 #include "UpdateMask.h"
29 #include "ObjectMgr.h"
35 #include "DynamicObject.h"
36 #include "SpellAuras.h"
38 #include "UpdateData.h"
39 #include "MapManager.h"
40 #include "ObjectAccessor.h"
42 #include "Policies/SingletonImp.h"
43 #include "SharedDefines.h"
46 #include "VMapFactory.h"
47 #include "BattleGround.h"
50 #define SPELL_CHANNEL_UPDATE_INTERVAL 1000
52 extern pEffect SpellEffects
[TOTAL_SPELL_EFFECTS
];
54 bool IsQuestTameSpell(uint32 spellId
)
56 SpellEntry
const *spellproto
= sSpellStore
.LookupEntry(spellId
);
57 if (!spellproto
) return false;
59 return spellproto
->Effect
[0] == SPELL_EFFECT_THREAT
60 && spellproto
->Effect
[1] == SPELL_EFFECT_APPLY_AURA
&& spellproto
->EffectApplyAuraName
[1] == SPELL_AURA_DUMMY
;
63 SpellCastTargets::SpellCastTargets()
71 m_CorpseTargetGUID
= 0;
73 m_itemTargetEntry
= 0;
75 m_srcX
= m_srcY
= m_srcZ
= m_destX
= m_destY
= m_destZ
= 0;
80 SpellCastTargets::~SpellCastTargets()
84 void SpellCastTargets::setUnitTarget(Unit
*target
)
89 m_destX
= target
->GetPositionX();
90 m_destY
= target
->GetPositionY();
91 m_destZ
= target
->GetPositionZ();
92 m_unitTarget
= target
;
93 m_unitTargetGUID
= target
->GetGUID();
94 m_targetMask
|= TARGET_FLAG_UNIT
;
97 void SpellCastTargets::setDestination(float x
, float y
, float z
)
102 m_targetMask
|= TARGET_FLAG_DEST_LOCATION
;
105 void SpellCastTargets::setGOTarget(GameObject
*target
)
108 m_GOTargetGUID
= target
->GetGUID();
109 // m_targetMask |= TARGET_FLAG_OBJECT;
112 void SpellCastTargets::setItemTarget(Item
* item
)
118 m_itemTargetGUID
= item
->GetGUID();
119 m_itemTargetEntry
= item
->GetEntry();
120 m_targetMask
|= TARGET_FLAG_ITEM
;
123 void SpellCastTargets::setCorpseTarget(Corpse
* corpse
)
125 m_CorpseTargetGUID
= corpse
->GetGUID();
128 void SpellCastTargets::Update(Unit
* caster
)
130 m_GOTarget
= m_GOTargetGUID
? ObjectAccessor::GetGameObject(*caster
,m_GOTargetGUID
) : NULL
;
131 m_unitTarget
= m_unitTargetGUID
?
132 ( m_unitTargetGUID
==caster
->GetGUID() ? caster
: ObjectAccessor::GetUnit(*caster
, m_unitTargetGUID
) ) :
136 if(caster
->GetTypeId()==TYPEID_PLAYER
)
138 if(m_targetMask
& TARGET_FLAG_ITEM
)
139 m_itemTarget
= ((Player
*)caster
)->GetItemByGuid(m_itemTargetGUID
);
142 Player
* pTrader
= ((Player
*)caster
)->GetTrader();
143 if(pTrader
&& m_itemTargetGUID
< TRADE_SLOT_COUNT
)
144 m_itemTarget
= pTrader
->GetItemByPos(pTrader
->GetItemPosByTradeSlot(m_itemTargetGUID
));
147 m_itemTargetEntry
= m_itemTarget
->GetEntry();
151 bool SpellCastTargets::read ( WorldPacket
* data
, Unit
*caster
)
153 if(data
->rpos()+4 > data
->size())
156 *data
>> m_targetMask
;
158 if(m_targetMask
== TARGET_FLAG_SELF
)
160 m_destX
= caster
->GetPositionX();
161 m_destY
= caster
->GetPositionY();
162 m_destZ
= caster
->GetPositionZ();
163 m_unitTarget
= caster
;
164 m_unitTargetGUID
= caster
->GetGUID();
168 // TARGET_FLAG_UNK2 is used for non-combat pets, maybe other?
169 if( m_targetMask
& ( TARGET_FLAG_UNIT
| TARGET_FLAG_UNK2
))
170 if(!readGUID(*data
, m_unitTargetGUID
))
173 if( m_targetMask
& ( TARGET_FLAG_OBJECT
| TARGET_FLAG_OBJECT_UNK
))
174 if(!readGUID(*data
, m_GOTargetGUID
))
177 if(( m_targetMask
& ( TARGET_FLAG_ITEM
| TARGET_FLAG_TRADE_ITEM
)) && caster
->GetTypeId() == TYPEID_PLAYER
)
178 if(!readGUID(*data
, m_itemTargetGUID
))
181 if( m_targetMask
& TARGET_FLAG_SOURCE_LOCATION
)
183 if(data
->rpos()+4+4+4 > data
->size())
186 *data
>> m_srcX
>> m_srcY
>> m_srcZ
;
187 if(!MaNGOS::IsValidMapCoord(m_srcX
, m_srcY
, m_srcZ
))
191 if( m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
193 if(data
->rpos()+4+4+4 > data
->size())
196 *data
>> m_destX
>> m_destY
>> m_destZ
;
197 if(!MaNGOS::IsValidMapCoord(m_destX
, m_destY
, m_destZ
))
201 if( m_targetMask
& TARGET_FLAG_STRING
)
203 if(data
->rpos()+1 > data
->size())
206 *data
>> m_strTarget
;
209 if( m_targetMask
& (TARGET_FLAG_CORPSE
| TARGET_FLAG_PVP_CORPSE
) )
210 if(!readGUID(*data
, m_CorpseTargetGUID
))
213 // find real units/GOs
218 void SpellCastTargets::write ( WorldPacket
* data
)
220 *data
<< uint32(m_targetMask
);
222 if( m_targetMask
& ( TARGET_FLAG_UNIT
| TARGET_FLAG_PVP_CORPSE
| TARGET_FLAG_OBJECT
| TARGET_FLAG_CORPSE
| TARGET_FLAG_UNK2
) )
224 if(m_targetMask
& TARGET_FLAG_UNIT
)
227 data
->append(m_unitTarget
->GetPackGUID());
231 else if( m_targetMask
& ( TARGET_FLAG_OBJECT
| TARGET_FLAG_OBJECT_UNK
) )
234 data
->append(m_GOTarget
->GetPackGUID());
238 else if( m_targetMask
& ( TARGET_FLAG_CORPSE
| TARGET_FLAG_PVP_CORPSE
) )
239 data
->appendPackGUID(m_CorpseTargetGUID
);
244 if( m_targetMask
& ( TARGET_FLAG_ITEM
| TARGET_FLAG_TRADE_ITEM
) )
247 data
->append(m_itemTarget
->GetPackGUID());
252 if( m_targetMask
& TARGET_FLAG_SOURCE_LOCATION
)
253 *data
<< m_srcX
<< m_srcY
<< m_srcZ
;
255 if( m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
256 *data
<< m_destX
<< m_destY
<< m_destZ
;
258 if( m_targetMask
& TARGET_FLAG_STRING
)
259 *data
<< m_strTarget
;
262 Spell::Spell( Unit
* Caster
, SpellEntry
const *info
, bool triggered
, uint64 originalCasterGUID
, Spell
** triggeringContainer
)
264 ASSERT( Caster
!= NULL
&& info
!= NULL
);
265 ASSERT( info
== sSpellStore
.LookupEntry( info
->Id
) && "`info` must be pointer to sSpellStore element");
269 m_selfContainer
= NULL
;
270 m_triggeringContainer
= triggeringContainer
;
271 m_referencedFromCurrentSpell
= false;
272 m_executedCurrently
= false;
273 m_delayAtDamageCount
= 0;
275 m_applyMultiplierMask
= 0;
277 // Get data for type of attack
278 switch (m_spellInfo
->DmgClass
)
280 case SPELL_DAMAGE_CLASS_MELEE
:
281 if (m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_REQ_OFFHAND
)
282 m_attackType
= OFF_ATTACK
;
284 m_attackType
= BASE_ATTACK
;
286 case SPELL_DAMAGE_CLASS_RANGED
:
287 m_attackType
= RANGED_ATTACK
;
291 if (m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_REQ_WAND
)
292 m_attackType
= RANGED_ATTACK
;
294 m_attackType
= BASE_ATTACK
;
298 m_spellSchoolMask
= GetSpellSchoolMask(info
); // Can be override for some spell (wand shoot for example)
300 if(m_attackType
== RANGED_ATTACK
)
303 if((m_caster
->getClassMask() & CLASSMASK_WAND_USERS
) != 0 && m_caster
->GetTypeId()==TYPEID_PLAYER
)
305 if(Item
* pItem
= ((Player
*)m_caster
)->GetWeaponForAttack(RANGED_ATTACK
))
306 m_spellSchoolMask
= SpellSchoolMask(1 << pItem
->GetProto()->Damage
->DamageType
);
310 if(originalCasterGUID
)
311 m_originalCasterGUID
= originalCasterGUID
;
313 m_originalCasterGUID
= m_caster
->GetGUID();
315 if(m_originalCasterGUID
==m_caster
->GetGUID())
316 m_originalCaster
= m_caster
;
319 m_originalCaster
= ObjectAccessor::GetUnit(*m_caster
,m_originalCasterGUID
);
320 if(m_originalCaster
&& !m_originalCaster
->IsInWorld()) m_originalCaster
= NULL
;
323 for(int i
=0; i
<3; ++i
)
324 m_currentBasePoints
[i
] = m_spellInfo
->EffectBasePoints
[i
];
326 m_spellState
= SPELL_STATE_NULL
;
328 m_castPositionX
= m_castPositionY
= m_castPositionZ
= 0;
329 m_TriggerSpells
.clear();
330 m_IsTriggeredSpell
= triggered
;
331 //m_AreaAura = false;
336 gameObjTarget
= NULL
;
339 m_triggeredByAuraSpell
= NULL
;
342 if( m_spellInfo
->AttributesEx2
== 0x000020 && !triggered
)
345 m_autoRepeat
= false;
347 m_powerCost
= 0; // setup to correct value in Spell::prepare, don't must be used before.
348 m_casttime
= 0; // setup to correct value in Spell::prepare, don't must be used before.
349 m_timer
= 0; // will set to castime in preper
351 m_needAliveTargetMask
= 0;
353 // determine reflection
354 m_canReflect
= false;
356 if(m_spellInfo
->DmgClass
== SPELL_DAMAGE_CLASS_MAGIC
&& (m_spellInfo
->AttributesEx2
& 0x4)==0)
360 if (m_spellInfo
->Effect
[j
]==0)
363 if(!IsPositiveTarget(m_spellInfo
->EffectImplicitTargetA
[j
],m_spellInfo
->EffectImplicitTargetB
[j
]))
366 m_canReflect
= (m_spellInfo
->AttributesEx
& (1<<7)) ? true : false;
382 void Spell::FillTargetMap()
384 // TODO: ADD the correct target FILLS!!!!!!
386 for(uint32 i
=0;i
<3;i
++)
388 // not call for empty effect.
389 // Also some spells use not used effect targets for store targets for dummy effect in triggered spells
390 if(m_spellInfo
->Effect
[i
]==0)
393 // targets for TARGET_SCRIPT_COORDINATES (A) and TARGET_SCRIPT filled in Spell::canCast call
394 if( m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SCRIPT_COORDINATES
||
395 m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SCRIPT
||
396 m_spellInfo
->EffectImplicitTargetB
[i
] == TARGET_SCRIPT
&& m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_SELF
)
399 // TODO: find a way so this is not needed?
400 // for area auras always add caster as target (needed for totems for example)
401 if(IsAreaAuraEffect(m_spellInfo
->Effect
[i
]))
402 AddUnitTarget(m_caster
, i
);
404 std::list
<Unit
*> tmpUnitMap
;
406 // TargetA/TargetB dependent from each other, we not switch to full support this dependences
407 // but need it support in some know cases
408 switch(m_spellInfo
->EffectImplicitTargetA
[i
])
410 case TARGET_ALL_AROUND_CASTER
:
411 if( m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_PARTY
||
412 m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_FRIENDLY_UNITS_AROUND_CASTER
||
413 m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_RANDOM_RAID_MEMBER
)
415 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
417 // Note: this hack with search required until GO casting not implemented
418 // environment damage spells already have around enemies targeting but this not help in case not existed GO casting support
419 // currently each enemy selected explicitly and self cast damage
420 else if(m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_ENEMY_IN_AREA
&& m_spellInfo
->Effect
[i
]==SPELL_EFFECT_ENVIRONMENTAL_DAMAGE
)
422 if(m_targets
.getUnitTarget())
423 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
427 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
428 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
431 case TARGET_TABLE_X_Y_Z_COORDINATES
:
432 // Only if target A, for target B (used in teleports) dest select in effect
433 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
436 switch(m_spellInfo
->EffectImplicitTargetB
[i
])
438 case TARGET_SCRIPT_COORDINATES
: // B case filled in canCast but we need fill unit list base at A case
439 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
442 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
443 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
449 if( (m_spellInfo
->EffectImplicitTargetA
[i
]==0 || m_spellInfo
->EffectImplicitTargetA
[i
]==TARGET_EFFECT_SELECT
) &&
450 (m_spellInfo
->EffectImplicitTargetB
[i
]==0 || m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_EFFECT_SELECT
) )
452 // add here custom effects that need default target.
453 // FOR EVERY TARGET TYPE THERE IS A DIFFERENT FILL!!
454 switch(m_spellInfo
->Effect
[i
])
456 case SPELL_EFFECT_DUMMY
:
458 switch(m_spellInfo
->Id
)
460 case 20577: // Cannibalize
462 // non-standard target selection
463 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
464 float max_range
= GetSpellMaxRange(srange
);
466 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
468 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
471 WorldObject
* result
= NULL
;
473 MaNGOS::CannibalizeObjectCheck
u_check(m_caster
, max_range
);
474 MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
> searcher(result
, u_check
);
476 TypeContainerVisitor
<MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
>, GridTypeMapContainer
> grid_searcher(searcher
);
477 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
478 cell_lock
->Visit(cell_lock
, grid_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
482 TypeContainerVisitor
<MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
>, WorldTypeMapContainer
> world_searcher(searcher
);
483 cell_lock
->Visit(cell_lock
, world_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
488 switch(result
->GetTypeId())
492 tmpUnitMap
.push_back((Unit
*)result
);
495 m_targets
.setCorpseTarget((Corpse
*)result
);
496 if(Player
* owner
= ObjectAccessor::FindPlayer(((Corpse
*)result
)->GetOwnerGUID()))
497 tmpUnitMap
.push_back(owner
);
503 // clear cooldown at fail
504 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
506 ((Player
*)m_caster
)->RemoveSpellCooldown(m_spellInfo
->Id
);
508 WorldPacket
data(SMSG_CLEAR_COOLDOWN
, (4+8));
509 data
<< uint32(m_spellInfo
->Id
);
510 data
<< uint64(m_caster
->GetGUID());
511 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
514 SendCastResult(SPELL_FAILED_NO_EDIBLE_CORPSES
);
520 if(m_targets
.getUnitTarget())
521 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
526 case SPELL_EFFECT_RESURRECT
:
527 case SPELL_EFFECT_PARRY
:
528 case SPELL_EFFECT_BLOCK
:
529 case SPELL_EFFECT_CREATE_ITEM
:
530 case SPELL_EFFECT_TRIGGER_SPELL
:
531 case SPELL_EFFECT_TRIGGER_MISSILE
:
532 case SPELL_EFFECT_LEARN_SPELL
:
533 case SPELL_EFFECT_SKILL_STEP
:
534 case SPELL_EFFECT_PROFICIENCY
:
535 case SPELL_EFFECT_SUMMON_POSSESSED
:
536 case SPELL_EFFECT_SUMMON_OBJECT_WILD
:
537 case SPELL_EFFECT_SELF_RESURRECT
:
538 case SPELL_EFFECT_REPUTATION
:
539 if(m_targets
.getUnitTarget())
540 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
542 case SPELL_EFFECT_SUMMON_PLAYER
:
543 if(m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->GetSelection())
545 Player
* target
= objmgr
.GetPlayer(((Player
*)m_caster
)->GetSelection());
547 tmpUnitMap
.push_back(target
);
550 case SPELL_EFFECT_RESURRECT_NEW
:
551 if(m_targets
.getUnitTarget())
552 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
553 if(m_targets
.getCorpseTargetGUID())
555 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
558 Player
* owner
= ObjectAccessor::FindPlayer(corpse
->GetOwnerGUID());
560 tmpUnitMap
.push_back(owner
);
564 case SPELL_EFFECT_SUMMON
:
565 if(m_spellInfo
->EffectMiscValueB
[i
] == SUMMON_TYPE_POSESSED
|| m_spellInfo
->EffectMiscValueB
[i
] == SUMMON_TYPE_POSESSED2
)
567 if(m_targets
.getUnitTarget())
568 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
571 tmpUnitMap
.push_back(m_caster
);
573 case SPELL_EFFECT_SUMMON_CHANGE_ITEM
:
574 case SPELL_EFFECT_SUMMON_WILD
:
575 case SPELL_EFFECT_SUMMON_GUARDIAN
:
576 case SPELL_EFFECT_TRANS_DOOR
:
577 case SPELL_EFFECT_ADD_FARSIGHT
:
578 case SPELL_EFFECT_APPLY_GLYPH
:
579 case SPELL_EFFECT_STUCK
:
580 case SPELL_EFFECT_DESTROY_ALL_TOTEMS
:
581 case SPELL_EFFECT_SUMMON_DEMON
:
582 case SPELL_EFFECT_SKILL
:
583 tmpUnitMap
.push_back(m_caster
);
585 case SPELL_EFFECT_LEARN_PET_SPELL
:
586 if(Pet
* pet
= m_caster
->GetPet())
587 tmpUnitMap
.push_back(pet
);
589 case SPELL_EFFECT_ENCHANT_ITEM
:
590 case SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY
:
591 case SPELL_EFFECT_DISENCHANT
:
592 case SPELL_EFFECT_FEED_PET
:
593 case SPELL_EFFECT_PROSPECTING
:
594 if(m_targets
.getItemTarget())
595 AddItemTarget(m_targets
.getItemTarget(), i
);
597 case SPELL_EFFECT_APPLY_AURA
:
598 switch(m_spellInfo
->EffectApplyAuraName
[i
])
600 case SPELL_AURA_ADD_FLAT_MODIFIER
: // some spell mods auras have 0 target modes instead expected TARGET_SELF(1) (and present for other ranks for same spell for example)
601 case SPELL_AURA_ADD_PCT_MODIFIER
:
602 tmpUnitMap
.push_back(m_caster
);
604 default: // apply to target in other case
605 if(m_targets
.getUnitTarget())
606 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
610 case SPELL_EFFECT_APPLY_AREA_AURA_PARTY
:
612 if(m_spellInfo
->Attributes
== 0x9050000 || m_spellInfo
->Attributes
== 0x10000)
613 SetTargetMap(i
,TARGET_AREAEFFECT_PARTY
,tmpUnitMap
);
615 case SPELL_EFFECT_SKIN_PLAYER_CORPSE
:
616 if(m_targets
.getUnitTarget())
618 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
620 else if (m_targets
.getCorpseTargetGUID())
622 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
625 Player
* owner
= ObjectAccessor::FindPlayer(corpse
->GetOwnerGUID());
627 tmpUnitMap
.push_back(owner
);
635 if(IsChanneledSpell(m_spellInfo
) && !tmpUnitMap
.empty())
636 m_needAliveTargetMask
|= (1<<i
);
638 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
640 Player
*me
= (Player
*)m_caster
;
641 for (std::list
<Unit
*>::const_iterator itr
= tmpUnitMap
.begin(); itr
!= tmpUnitMap
.end(); itr
++)
643 Unit
*owner
= (*itr
)->GetOwner();
644 Unit
*u
= owner
? owner
: (*itr
);
645 if(u
!=m_caster
&& u
->IsPvP() && (!me
->duel
|| me
->duel
->opponent
!= u
))
648 me
->RemoveAurasWithInterruptFlags(AURA_INTERRUPT_FLAG_ENTER_PVP_COMBAT
);
654 for (std::list
<Unit
*>::iterator itr
= tmpUnitMap
.begin() ; itr
!= tmpUnitMap
.end();)
656 if(!CheckTarget(*itr
, i
, false ))
658 itr
= tmpUnitMap
.erase(itr
);
665 for(std::list
<Unit
*>::iterator iunit
= tmpUnitMap
.begin();iunit
!= tmpUnitMap
.end();++iunit
)
666 AddUnitTarget((*iunit
), i
);
670 void Spell::CleanupTargetList()
672 m_UniqueTargetInfo
.clear();
673 m_UniqueGOTargetInfo
.clear();
674 m_UniqueItemInfo
.clear();
680 void Spell::AddUnitTarget(Unit
* pVictim
, uint32 effIndex
)
682 if( m_spellInfo
->Effect
[effIndex
]==0 )
685 uint64 targetGUID
= pVictim
->GetGUID();
687 // Lookup target in already in list
688 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
690 if (targetGUID
== ihit
->targetGUID
) // Found in list
692 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
697 // This is new target calculate data for him
699 // Get spell hit result on target
701 target
.targetGUID
= targetGUID
; // Store target GUID
702 target
.effectMask
= 1<<effIndex
; // Store index of effect
703 target
.processed
= false; // Effects not apply on target
705 // Calculate hit result
706 target
.missCondition
= m_caster
->SpellHitResult(pVictim
, m_spellInfo
, m_canReflect
);
707 if (target
.missCondition
== SPELL_MISS_NONE
)
712 // Spell have speed - need calculate incoming time
713 if (m_spellInfo
->speed
> 0.0f
)
715 // calculate spell incoming interval
716 float dist
= m_caster
->GetDistance(pVictim
->GetPositionX(), pVictim
->GetPositionY(), pVictim
->GetPositionZ());
717 if (dist
< 5.0f
) dist
= 5.0f
;
718 target
.timeDelay
= (uint64
) floor(dist
/ m_spellInfo
->speed
* 1000.0f
);
720 // Calculate minimum incoming time
721 if (m_delayMoment
==0 || m_delayMoment
>target
.timeDelay
)
722 m_delayMoment
= target
.timeDelay
;
725 target
.timeDelay
= 0LL;
727 // If target reflect spell back to caster
728 if (target
.missCondition
==SPELL_MISS_REFLECT
)
730 // Calculate reflected spell result on caster
731 target
.reflectResult
= m_caster
->SpellHitResult(m_caster
, m_spellInfo
, m_canReflect
);
733 if (target
.reflectResult
== SPELL_MISS_REFLECT
) // Impossible reflect again, so simply deflect spell
734 target
.reflectResult
= SPELL_MISS_PARRY
;
736 // Increase time interval for reflected spells by 1.5
737 target
.timeDelay
+=target
.timeDelay
>>1;
740 target
.reflectResult
= SPELL_MISS_NONE
;
742 // Add target to list
743 m_UniqueTargetInfo
.push_back(target
);
746 void Spell::AddUnitTarget(uint64 unitGUID
, uint32 effIndex
)
748 Unit
* unit
= m_caster
->GetGUID()==unitGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, unitGUID
);
750 AddUnitTarget(unit
, effIndex
);
753 void Spell::AddGOTarget(GameObject
* pVictim
, uint32 effIndex
)
755 if( m_spellInfo
->Effect
[effIndex
]==0 )
758 uint64 targetGUID
= pVictim
->GetGUID();
760 // Lookup target in already in list
761 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
763 if (targetGUID
== ihit
->targetGUID
) // Found in list
765 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
770 // This is new target calculate data for him
773 target
.targetGUID
= targetGUID
;
774 target
.effectMask
= 1<<effIndex
;
775 target
.processed
= false; // Effects not apply on target
777 // Spell have speed - need calculate incoming time
778 if (m_spellInfo
->speed
> 0.0f
)
780 // calculate spell incoming interval
781 float dist
= m_caster
->GetDistance(pVictim
->GetPositionX(), pVictim
->GetPositionY(), pVictim
->GetPositionZ());
782 if (dist
< 5.0f
) dist
= 5.0f
;
783 target
.timeDelay
= (uint64
) floor(dist
/ m_spellInfo
->speed
* 1000.0f
);
784 if (m_delayMoment
==0 || m_delayMoment
>target
.timeDelay
)
785 m_delayMoment
= target
.timeDelay
;
788 target
.timeDelay
= 0LL;
792 // Add target to list
793 m_UniqueGOTargetInfo
.push_back(target
);
796 void Spell::AddGOTarget(uint64 goGUID
, uint32 effIndex
)
798 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, goGUID
);
800 AddGOTarget(go
, effIndex
);
803 void Spell::AddItemTarget(Item
* pitem
, uint32 effIndex
)
805 if( m_spellInfo
->Effect
[effIndex
]==0 )
808 // Lookup target in already in list
809 for(std::list
<ItemTargetInfo
>::iterator ihit
= m_UniqueItemInfo
.begin();ihit
!= m_UniqueItemInfo
.end();++ihit
)
811 if (pitem
== ihit
->item
) // Found in list
813 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
818 // This is new target add data
820 ItemTargetInfo target
;
822 target
.effectMask
= 1<<effIndex
;
823 m_UniqueItemInfo
.push_back(target
);
826 void Spell::doTriggers(SpellMissInfo missInfo
, uint32 damage
, SpellSchoolMask damageSchoolMask
, uint32 block
, uint32 absorb
, bool crit
)
828 // Do triggers depends from hit result (triggers on hit do in effects)
829 // Set aura states depends from hit result
830 if (missInfo
!=SPELL_MISS_NONE
)
832 // Miss/dodge/parry/block only for melee based spells
833 // Resist only for magic based spells
836 case SPELL_MISS_MISS
:
837 if(m_caster
->GetTypeId()== TYPEID_PLAYER
)
838 ((Player
*)m_caster
)->UpdateWeaponSkill(BASE_ATTACK
);
840 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_MISS
, m_spellInfo
, m_IsTriggeredSpell
);
842 case SPELL_MISS_RESIST
:
843 m_caster
->ProcDamageAndSpell(unitTarget
, PROC_FLAG_TARGET_RESISTS
, PROC_FLAG_RESIST_SPELL
, 0, damageSchoolMask
, m_spellInfo
, m_IsTriggeredSpell
);
845 case SPELL_MISS_DODGE
:
846 if(unitTarget
->GetTypeId() == TYPEID_PLAYER
)
847 ((Player
*)unitTarget
)->UpdateDefense();
850 if (m_caster
->GetTypeId() == TYPEID_PLAYER
&& m_caster
->getClass() == CLASS_WARRIOR
)
852 ((Player
*) m_caster
)->AddComboPoints(unitTarget
, 1);
853 m_caster
->StartReactiveTimer( REACTIVE_OVERPOWER
);
857 if (unitTarget
->getClass() != CLASS_ROGUE
)
859 unitTarget
->ModifyAuraState(AURA_STATE_DEFENSE
, true);
860 unitTarget
->StartReactiveTimer( REACTIVE_DEFENSE
);
863 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_DODGE
, m_spellInfo
, m_IsTriggeredSpell
);
865 case SPELL_MISS_PARRY
:
866 // Update victim defense ?
867 if(unitTarget
->GetTypeId() == TYPEID_PLAYER
)
868 ((Player
*)unitTarget
)->UpdateDefense();
869 // Mongoose bite - set only Counterattack here
870 if (unitTarget
->getClass() == CLASS_HUNTER
)
872 unitTarget
->ModifyAuraState(AURA_STATE_HUNTER_PARRY
,true);
873 unitTarget
->StartReactiveTimer( REACTIVE_HUNTER_PARRY
);
877 unitTarget
->ModifyAuraState(AURA_STATE_DEFENSE
, true);
878 unitTarget
->StartReactiveTimer( REACTIVE_DEFENSE
);
880 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_PARRY
, m_spellInfo
, m_IsTriggeredSpell
);
882 case SPELL_MISS_BLOCK
:
883 unitTarget
->ModifyAuraState(AURA_STATE_DEFENSE
, true);
884 unitTarget
->StartReactiveTimer( REACTIVE_DEFENSE
);
886 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_BLOCK
, m_spellInfo
, m_IsTriggeredSpell
);
888 // Trigger from this events not supported
889 case SPELL_MISS_EVADE
:
890 case SPELL_MISS_IMMUNE
:
891 case SPELL_MISS_IMMUNE2
:
892 case SPELL_MISS_DEFLECT
:
893 case SPELL_MISS_ABSORB
:
894 // Trigger from reflects need do after get reflect result
895 case SPELL_MISS_REFLECT
:
903 void Spell::DoAllEffectOnTarget(TargetInfo
*target
)
905 if (target
->processed
) // Check target
907 target
->processed
= true; // Target checked in apply effects procedure
909 // Get mask of effects for target
910 uint32 mask
= target
->effectMask
;
911 if (mask
== 0) // No effects
914 Unit
* unit
= m_caster
->GetGUID()==target
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
,target
->targetGUID
);
918 SpellMissInfo missInfo
= target
->missCondition
;
919 // Need init unitTarget by default unit (can changed in code on reflect)
920 // Or on missInfo!=SPELL_MISS_NONE unitTarget undefined (but need in trigger subsystem)
923 if (missInfo
==SPELL_MISS_NONE
) // In case spell hit target, do all effect on that target
924 DoSpellHitOnUnit(unit
, mask
);
925 else if (missInfo
== SPELL_MISS_REFLECT
) // In case spell reflect from target, do all effect on caster (if hit)
927 if (target
->reflectResult
== SPELL_MISS_NONE
) // If reflected spell hit caster -> do all effect on him
928 DoSpellHitOnUnit(m_caster
, mask
);
931 // Do triggers only on miss/resist/parry/dodge
932 if (missInfo
!=SPELL_MISS_NONE
)
933 doTriggers(missInfo
);
935 // Call scripted function for AI if this spell is casted upon a creature (except pets)
936 if(IS_CREATURE_GUID(target
->targetGUID
))
938 // cast at creature (or GO) quest objectives update at successful cast finished (+channel finished)
939 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
940 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() && !IsChannelActive() )
941 ((Player
*)m_caster
)->CastedCreatureOrGO(unit
->GetEntry(),unit
->GetGUID(),m_spellInfo
->Id
);
943 if(((Creature
*)unit
)->AI())
944 ((Creature
*)unit
)->AI()->SpellHit(m_caster
,m_spellInfo
);
948 void Spell::DoSpellHitOnUnit(Unit
*unit
, const uint32 effectMask
)
950 if(!unit
|| !effectMask
)
953 // Recheck immune (only for delayed spells)
954 if( m_spellInfo
->speed
&& (
955 unit
->IsImmunedToDamage(GetSpellSchoolMask(m_spellInfo
),true) ||
956 unit
->IsImmunedToSpell(m_spellInfo
,true) ))
958 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_IMMUNE
);
962 if( m_caster
!= unit
)
964 if( !m_caster
->IsFriendlyTo(unit
) )
966 // for delayed spells ignore not visible explicit target
967 if(m_spellInfo
->speed
> 0.0f
&& unit
==m_targets
.getUnitTarget() && !unit
->isVisibleForOrDetect(m_caster
,false))
969 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_EVADE
);
973 // exclude Arcane Missiles Dummy Aura aura for now (attack on hit)
974 // TODO: find way to not need this?
975 if(!(m_spellInfo
->SpellFamilyName
== SPELLFAMILY_MAGE
&&
976 m_spellInfo
->SpellFamilyFlags
& 0x800LL
))
978 unit
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
980 if( !(m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_NO_INITIAL_AGGRO
) )
982 if(!unit
->IsStandState() && !unit
->hasUnitState(UNIT_STAT_STUNNED
))
983 unit
->SetStandState(PLAYER_STATE_NONE
);
985 if(!unit
->isInCombat() && unit
->GetTypeId() != TYPEID_PLAYER
&& ((Creature
*)unit
)->AI())
986 ((Creature
*)unit
)->AI()->AttackStart(m_caster
);
988 unit
->SetInCombatWith(m_caster
);
989 m_caster
->SetInCombatWith(unit
);
991 if(Player
*attackedPlayer
= unit
->GetCharmerOrOwnerPlayerOrPlayerItself())
993 m_caster
->SetContestedPvP(attackedPlayer
);
995 unit
->AddThreat(m_caster
, 0.0f
);
1001 // for delayed spells ignore negative spells (after duel end) for friendly targets
1002 if(m_spellInfo
->speed
> 0.0f
&& !IsPositiveSpell(m_spellInfo
->Id
))
1004 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_EVADE
);
1008 // assisting case, healing and resurrection
1009 if(unit
->hasUnitState(UNIT_STAT_ATTACK_PLAYER
))
1010 m_caster
->SetContestedPvP();
1011 if( unit
->isInCombat() && !(m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_NO_INITIAL_AGGRO
) )
1013 m_caster
->SetInCombatState(unit
->GetCombatTimer() > 0);
1014 unit
->getHostilRefManager().threatAssist(m_caster
, 0.0f
);
1019 // Get Data Needed for Diminishing Returns, some effects may have multiple auras, so this must be done on spell hit, not aura add
1020 m_diminishGroup
= GetDiminishingReturnsGroupForSpell(m_spellInfo
,m_triggeredByAuraSpell
);
1021 m_diminishLevel
= unit
->GetDiminishing(m_diminishGroup
);
1022 // Increase Diminishing on unit, current informations for actually casts will use values above
1023 if((GetDiminishingReturnsGroupType(m_diminishGroup
) == DRTYPE_PLAYER
&& unit
->GetTypeId() == TYPEID_PLAYER
) || GetDiminishingReturnsGroupType(m_diminishGroup
) == DRTYPE_ALL
)
1024 unit
->IncrDiminishing(m_diminishGroup
);
1026 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1028 if (effectMask
& (1<<effectNumber
))
1030 HandleEffects(unit
,NULL
,NULL
,effectNumber
,m_damageMultipliers
[effectNumber
]);
1031 if ( m_applyMultiplierMask
& (1 << effectNumber
) )
1034 float multiplier
= m_spellInfo
->DmgMultiplier
[effectNumber
];
1035 // Apply multiplier mods
1036 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1037 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_EFFECT_PAST_FIRST
, multiplier
,this);
1038 m_damageMultipliers
[effectNumber
] *= multiplier
;
1044 void Spell::DoAllEffectOnTarget(GOTargetInfo
*target
)
1046 if (target
->processed
) // Check target
1048 target
->processed
= true; // Target checked in apply effects procedure
1050 uint32 effectMask
= target
->effectMask
;
1054 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, target
->targetGUID
);
1058 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1059 if (effectMask
& (1<<effectNumber
))
1060 HandleEffects(NULL
,NULL
,go
,effectNumber
);
1062 // cast at creature (or GO) quest objectives update at successful cast finished (+channel finished)
1063 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
1064 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() && !IsChannelActive() )
1065 ((Player
*)m_caster
)->CastedCreatureOrGO(go
->GetEntry(),go
->GetGUID(),m_spellInfo
->Id
);
1068 void Spell::DoAllEffectOnTarget(ItemTargetInfo
*target
)
1070 uint32 effectMask
= target
->effectMask
;
1071 if(!target
->item
|| !effectMask
)
1074 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1075 if (effectMask
& (1<<effectNumber
))
1076 HandleEffects(NULL
, target
->item
, NULL
, effectNumber
);
1079 bool Spell::IsAliveUnitPresentInTargetList()
1081 // Not need check return true
1082 if (m_needAliveTargetMask
== 0)
1085 uint8 needAliveTargetMask
= m_needAliveTargetMask
;
1087 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
1089 if( ihit
->missCondition
== SPELL_MISS_NONE
&& (needAliveTargetMask
& ihit
->effectMask
) )
1091 Unit
*unit
= m_caster
->GetGUID()==ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
1093 if (unit
&& unit
->isAlive())
1094 needAliveTargetMask
&= ~ihit
->effectMask
; // remove from need alive mask effect that have alive target
1098 // is all effects from m_needAliveTargetMask have alive targets
1099 return needAliveTargetMask
==0;
1102 // Helper for Chain Healing
1103 // Spell target first
1104 // Raidmates then descending by injury suffered (MaxHealth - Health)
1105 // Other players/mobs then descending by injury suffered (MaxHealth - Health)
1106 struct ChainHealingOrder
: public std::binary_function
<const Unit
*, const Unit
*, bool>
1108 const Unit
* MainTarget
;
1109 ChainHealingOrder(Unit
const* Target
) : MainTarget(Target
) {};
1110 // functor for operator ">"
1111 bool operator()(Unit
const* _Left
, Unit
const* _Right
) const
1113 return (ChainHealingHash(_Left
) < ChainHealingHash(_Right
));
1115 int32
ChainHealingHash(Unit
const* Target
) const
1117 if (Target
== MainTarget
)
1119 else if (Target
->GetTypeId() == TYPEID_PLAYER
&& MainTarget
->GetTypeId() == TYPEID_PLAYER
&&
1120 ((Player
const*)Target
)->IsInSameRaidWith((Player
const*)MainTarget
))
1122 if (Target
->GetHealth() == Target
->GetMaxHealth())
1125 return 20000 - Target
->GetMaxHealth() + Target
->GetHealth();
1128 return 40000 - Target
->GetMaxHealth() + Target
->GetHealth();
1132 class ChainHealingFullHealth
: std::unary_function
<const Unit
*, bool>
1135 const Unit
* MainTarget
;
1136 ChainHealingFullHealth(const Unit
* Target
) : MainTarget(Target
) {};
1138 bool operator()(const Unit
* Target
)
1140 return (Target
!= MainTarget
&& Target
->GetHealth() == Target
->GetMaxHealth());
1144 // Helper for targets nearest to the spell target
1145 // The spell target is always first unless there is a target at _completely_ the same position (unbelievable case)
1146 struct TargetDistanceOrder
: public std::binary_function
<const Unit
, const Unit
, bool>
1148 const Unit
* MainTarget
;
1149 TargetDistanceOrder(const Unit
* Target
) : MainTarget(Target
) {};
1150 // functor for operator ">"
1151 bool operator()(const Unit
* _Left
, const Unit
* _Right
) const
1153 return (MainTarget
->GetDistance(_Left
) < MainTarget
->GetDistance(_Right
));
1157 void Spell::SetTargetMap(uint32 i
,uint32 cur
,std::list
<Unit
*> &TagUnitMap
)
1160 if (m_spellInfo
->EffectRadiusIndex
[i
])
1161 radius
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[i
]));
1163 radius
= GetSpellMaxRange(sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
));
1165 if(m_originalCaster
)
1166 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1167 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_RADIUS
, radius
,this);
1169 uint32 EffectChainTarget
= m_spellInfo
->EffectChainTarget
[i
];
1170 if(m_originalCaster
)
1171 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1172 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_JUMP_TARGETS
, EffectChainTarget
, this);
1174 uint32 unMaxTargets
= m_spellInfo
->MaxAffectedTargets
;
1177 case TARGET_TOTEM_EARTH
:
1178 case TARGET_TOTEM_WATER
:
1179 case TARGET_TOTEM_AIR
:
1180 case TARGET_TOTEM_FIRE
:
1183 case TARGET_DYNAMIC_OBJECT
:
1184 case TARGET_AREAEFFECT_CUSTOM
:
1185 case TARGET_AREAEFFECT_CUSTOM_2
:
1188 TagUnitMap
.push_back(m_caster
);
1191 case TARGET_RANDOM_ENEMY_CHAIN_IN_AREA
:
1193 m_targets
.m_targetMask
= 0;
1194 unMaxTargets
= EffectChainTarget
;
1195 float max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1197 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1199 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1202 std::list
<Unit
*> tempUnitMap
;
1205 MaNGOS::AnyAoETargetUnitInObjectRangeCheck
u_check(m_caster
, m_caster
, max_range
);
1206 MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
> searcher(tempUnitMap
, u_check
);
1208 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_unit_searcher(searcher
);
1209 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1211 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1212 cell_lock
->Visit(cell_lock
, world_unit_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1213 cell_lock
->Visit(cell_lock
, grid_unit_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1216 if(tempUnitMap
.empty())
1219 tempUnitMap
.sort(TargetDistanceOrder(m_caster
));
1221 //Now to get us a random target that's in the initial range of the spell
1223 std::list
<Unit
*>::iterator itr
= tempUnitMap
.begin();
1224 while(itr
!= tempUnitMap
.end() && (*itr
)->GetDistance(m_caster
) < radius
)
1230 itr
= tempUnitMap
.begin();
1231 std::advance(itr
, rand()%t
);
1232 Unit
*pUnitTarget
= *itr
;
1233 TagUnitMap
.push_back(pUnitTarget
);
1235 tempUnitMap
.erase(itr
);
1237 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1239 t
= unMaxTargets
- 1;
1240 Unit
*prev
= pUnitTarget
;
1241 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1243 while(t
&& next
!= tempUnitMap
.end() )
1245 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1248 if(!prev
->IsWithinLOSInMap(*next
))
1255 TagUnitMap
.push_back(prev
);
1256 tempUnitMap
.erase(next
);
1257 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1258 next
= tempUnitMap
.begin();
1265 Pet
* tmpUnit
= m_caster
->GetPet();
1266 if (!tmpUnit
) break;
1267 TagUnitMap
.push_back(tmpUnit
);
1270 case TARGET_CHAIN_DAMAGE
:
1272 if (EffectChainTarget
<= 1)
1274 Unit
* pUnitTarget
= SelectMagnetTarget();
1276 TagUnitMap
.push_back(pUnitTarget
);
1280 Unit
* pUnitTarget
= m_targets
.getUnitTarget();
1284 unMaxTargets
= EffectChainTarget
;
1287 if(m_spellInfo
->DmgClass
==SPELL_DAMAGE_CLASS_MELEE
)
1288 max_range
= radius
; //
1290 //FIXME: This very like horrible hack and wrong for most spells
1291 max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1293 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1295 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1298 Unit
* originalCaster
= GetOriginalCaster();
1301 std::list
<Unit
*> tempUnitMap
;
1304 MaNGOS::AnyAoETargetUnitInObjectRangeCheck
u_check(pUnitTarget
, originalCaster
, max_range
);
1305 MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
> searcher(tempUnitMap
, u_check
);
1307 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_unit_searcher(searcher
);
1308 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1310 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1311 cell_lock
->Visit(cell_lock
, world_unit_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1312 cell_lock
->Visit(cell_lock
, grid_unit_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1315 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1317 if(tempUnitMap
.empty())
1320 if(*tempUnitMap
.begin() == pUnitTarget
)
1321 tempUnitMap
.erase(tempUnitMap
.begin());
1323 TagUnitMap
.push_back(pUnitTarget
);
1324 uint32 t
= unMaxTargets
- 1;
1325 Unit
*prev
= pUnitTarget
;
1326 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1328 while(t
&& next
!= tempUnitMap
.end() )
1330 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1333 if(!prev
->IsWithinLOSInMap(*next
))
1340 TagUnitMap
.push_back(prev
);
1341 tempUnitMap
.erase(next
);
1342 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1343 next
= tempUnitMap
.begin();
1350 case TARGET_ALL_ENEMY_IN_AREA
:
1353 case TARGET_ALL_ENEMY_IN_AREA_INSTANT
:
1355 // targets the ground, not the units in the area
1356 if (m_spellInfo
->Effect
[i
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
1358 CellPair
p(MaNGOS::ComputeCellPair(m_targets
.m_destX
, m_targets
.m_destY
));
1360 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1363 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1365 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1366 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1368 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1369 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1370 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1372 // exclude caster (this can be important if this not original caster)
1373 TagUnitMap
.remove(m_caster
);
1376 case TARGET_DUELVSPLAYER_COORDINATES
:
1378 if(Unit
* currentTarget
= m_targets
.getUnitTarget())
1380 m_targets
.setDestination(currentTarget
->GetPositionX(), currentTarget
->GetPositionY(), currentTarget
->GetPositionZ());
1381 TagUnitMap
.push_back(currentTarget
);
1384 case TARGET_ALL_PARTY_AROUND_CASTER
:
1385 case TARGET_ALL_PARTY_AROUND_CASTER_2
:
1386 case TARGET_ALL_PARTY
:
1388 Player
*pTarget
= m_caster
->GetCharmerOrOwnerPlayerOrPlayerItself();
1389 Group
*pGroup
= pTarget
? pTarget
->GetGroup() : NULL
;
1393 uint8 subgroup
= pTarget
->GetSubGroup();
1395 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1397 Player
* Target
= itr
->getSource();
1399 // IsHostileTo check duel and controlled by enemy
1400 if( Target
&& Target
->GetSubGroup()==subgroup
&& !m_caster
->IsHostileTo(Target
) )
1402 if( m_caster
->IsWithinDistInMap(Target
, radius
) )
1403 TagUnitMap
.push_back(Target
);
1405 if(Pet
* pet
= Target
->GetPet())
1406 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1407 TagUnitMap
.push_back(pet
);
1413 Unit
* ownerOrSelf
= pTarget
? pTarget
: m_caster
->GetCharmerOrOwnerOrSelf();
1414 if(ownerOrSelf
==m_caster
|| m_caster
->IsWithinDistInMap(ownerOrSelf
, radius
))
1415 TagUnitMap
.push_back(ownerOrSelf
);
1416 if(Pet
* pet
= ownerOrSelf
->GetPet())
1417 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1418 TagUnitMap
.push_back(pet
);
1421 case TARGET_RANDOM_RAID_MEMBER
:
1423 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1424 if(Player
* target
= ((Player
*)m_caster
)->GetNextRandomRaidMember(radius
))
1425 TagUnitMap
.push_back(target
);
1427 case TARGET_SINGLE_FRIEND
:
1428 case TARGET_SINGLE_FRIEND_2
:
1430 if(m_targets
.getUnitTarget())
1431 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1433 case TARGET_NONCOMBAT_PET
:
1435 if(Unit
* target
= m_targets
.getUnitTarget())
1436 if( target
->GetTypeId() == TYPEID_UNIT
&& ((Creature
*)target
)->isPet() && ((Pet
*)target
)->getPetType() == MINI_PET
)
1437 TagUnitMap
.push_back(target
);
1439 case TARGET_ALL_AROUND_CASTER
:
1441 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1443 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1446 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_SELF_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1448 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1449 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1451 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1452 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1453 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1455 case TARGET_ALL_FRIENDLY_UNITS_AROUND_CASTER
:
1457 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1459 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1462 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_SELF_CENTER
,SPELL_TARGETS_FRIENDLY
);
1464 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1465 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1467 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1468 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1469 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1471 case TARGET_ALL_FRIENDLY_UNITS_IN_AREA
:
1473 CellPair
p(MaNGOS::ComputeCellPair(m_targets
.m_destX
, m_targets
.m_destY
));
1475 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1478 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_FRIENDLY
);
1480 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1481 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1483 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1484 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1485 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1487 // TARGET_SINGLE_PARTY means that the spells can only be casted on a party member and not on the caster (some sceals, fire shield from imp, etc..)
1488 case TARGET_SINGLE_PARTY
:
1490 Unit
*target
= m_targets
.getUnitTarget();
1491 // Thoses spells apparently can't be casted on the caster.
1492 if( target
&& target
!= m_caster
)
1494 // Can only be casted on group's members or its pets
1495 Group
*pGroup
= NULL
;
1497 Unit
* owner
= m_caster
->GetCharmerOrOwner();
1498 Unit
*targetOwner
= target
->GetCharmerOrOwner();
1501 if(owner
->GetTypeId() == TYPEID_PLAYER
)
1503 if( target
== owner
)
1505 TagUnitMap
.push_back(target
);
1508 pGroup
= ((Player
*)owner
)->GetGroup();
1511 else if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1513 if( targetOwner
== m_caster
&& target
->GetTypeId()==TYPEID_UNIT
&& ((Creature
*)target
)->isPet())
1515 TagUnitMap
.push_back(target
);
1518 pGroup
= ((Player
*)m_caster
)->GetGroup();
1523 // Our target can also be a player's pet who's grouped with us or our pet. But can't be controlled player
1526 if( targetOwner
->GetTypeId() == TYPEID_PLAYER
&&
1527 target
->GetTypeId()==TYPEID_UNIT
&& (((Creature
*)target
)->isPet()) &&
1528 target
->GetOwnerGUID()==targetOwner
->GetGUID() &&
1529 pGroup
->IsMember(((Player
*)targetOwner
)->GetGUID()))
1531 TagUnitMap
.push_back(target
);
1534 // 1Our target can be a player who is on our group
1535 else if (target
->GetTypeId() == TYPEID_PLAYER
&& pGroup
->IsMember(((Player
*)target
)->GetGUID()))
1537 TagUnitMap
.push_back(target
);
1542 case TARGET_GAMEOBJECT
:
1544 if(m_targets
.getGOTarget())
1545 AddGOTarget(m_targets
.getGOTarget(), i
);
1547 case TARGET_IN_FRONT_OF_CASTER
:
1549 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1551 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1554 bool inFront
= m_spellInfo
->SpellVisual
[0] != 3879;
1555 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, inFront
? PUSH_IN_FRONT
: PUSH_IN_BACK
,SPELL_TARGETS_AOE_DAMAGE
);
1557 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1558 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1560 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1561 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1562 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1564 case TARGET_DUELVSPLAYER
:
1566 Unit
*target
= m_targets
.getUnitTarget();
1569 if(m_caster
->IsFriendlyTo(target
))
1571 TagUnitMap
.push_back(target
);
1575 Unit
* pUnitTarget
= SelectMagnetTarget();
1577 TagUnitMap
.push_back(pUnitTarget
);
1581 case TARGET_GAMEOBJECT_ITEM
:
1583 if(m_targets
.getGOTargetGUID())
1584 AddGOTarget(m_targets
.getGOTarget(), i
);
1585 else if(m_targets
.getItemTarget())
1586 AddItemTarget(m_targets
.getItemTarget(), i
);
1591 if(Unit
* owner
= m_caster
->GetCharmerOrOwner())
1592 TagUnitMap
.push_back(owner
);
1595 case TARGET_ALL_ENEMY_IN_AREA_CHANNELED
:
1597 // targets the ground, not the units in the area
1598 if (m_spellInfo
->Effect
[i
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
1600 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1602 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1605 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1607 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1608 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1610 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1611 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1612 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1617 if(m_spellInfo
->Effect
[i
] != SPELL_EFFECT_DUEL
)
1618 TagUnitMap
.push_back(m_caster
);
1620 case TARGET_SINGLE_ENEMY
:
1622 Unit
* pUnitTarget
= SelectMagnetTarget();
1624 TagUnitMap
.push_back(pUnitTarget
);
1626 case TARGET_AREAEFFECT_PARTY
:
1628 Unit
* owner
= m_caster
->GetCharmerOrOwner();
1629 Player
*pTarget
= NULL
;
1633 TagUnitMap
.push_back(m_caster
);
1634 if(owner
->GetTypeId() == TYPEID_PLAYER
)
1635 pTarget
= (Player
*)owner
;
1637 else if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1639 if(Unit
* target
= m_targets
.getUnitTarget())
1641 if( target
->GetTypeId() != TYPEID_PLAYER
)
1643 if(((Creature
*)target
)->isPet())
1645 Unit
*targetOwner
= target
->GetOwner();
1646 if(targetOwner
->GetTypeId() == TYPEID_PLAYER
)
1647 pTarget
= (Player
*)targetOwner
;
1651 pTarget
= (Player
*)target
;
1655 Group
* pGroup
= pTarget
? pTarget
->GetGroup() : NULL
;
1659 uint8 subgroup
= pTarget
->GetSubGroup();
1661 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1663 Player
* Target
= itr
->getSource();
1665 // IsHostileTo check duel and controlled by enemy
1666 if(Target
&& Target
->GetSubGroup()==subgroup
&& !m_caster
->IsHostileTo(Target
))
1668 if( pTarget
->IsWithinDistInMap(Target
, radius
) )
1669 TagUnitMap
.push_back(Target
);
1671 if(Pet
* pet
= Target
->GetPet())
1672 if( pTarget
->IsWithinDistInMap(pet
, radius
) )
1673 TagUnitMap
.push_back(pet
);
1679 if(m_caster
->IsWithinDistInMap(owner
, radius
))
1680 TagUnitMap
.push_back(owner
);
1684 TagUnitMap
.push_back(pTarget
);
1686 if(Pet
* pet
= pTarget
->GetPet())
1687 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1688 TagUnitMap
.push_back(pet
);
1694 if(m_targets
.getUnitTarget())
1695 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1696 if(m_targets
.getItemTarget())
1697 AddItemTarget(m_targets
.getItemTarget(), i
);
1699 case TARGET_SELF_FISHING
:
1701 TagUnitMap
.push_back(m_caster
);
1703 case TARGET_CHAIN_HEAL
:
1705 Unit
* pUnitTarget
= m_targets
.getUnitTarget();
1709 if (EffectChainTarget
<= 1)
1710 TagUnitMap
.push_back(pUnitTarget
);
1713 unMaxTargets
= EffectChainTarget
;
1714 float max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1716 std::list
<Unit
*> tempUnitMap
;
1719 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1721 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1724 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, tempUnitMap
, max_range
, PUSH_SELF_CENTER
, SPELL_TARGETS_FRIENDLY
);
1726 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1727 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1729 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1730 cell_lock
->Visit(cell_lock
, world_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1731 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1735 if(m_caster
!= pUnitTarget
&& std::find(tempUnitMap
.begin(),tempUnitMap
.end(),m_caster
) == tempUnitMap
.end() )
1736 tempUnitMap
.push_front(m_caster
);
1738 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1740 if(tempUnitMap
.empty())
1743 if(*tempUnitMap
.begin() == pUnitTarget
)
1744 tempUnitMap
.erase(tempUnitMap
.begin());
1746 TagUnitMap
.push_back(pUnitTarget
);
1747 uint32 t
= unMaxTargets
- 1;
1748 Unit
*prev
= pUnitTarget
;
1749 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1751 while(t
&& next
!= tempUnitMap
.end() )
1753 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1756 if(!prev
->IsWithinLOSInMap(*next
))
1762 if((*next
)->GetHealth() == (*next
)->GetMaxHealth())
1764 next
= tempUnitMap
.erase(next
);
1769 TagUnitMap
.push_back(prev
);
1770 tempUnitMap
.erase(next
);
1771 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1772 next
= tempUnitMap
.begin();
1778 case TARGET_CURRENT_ENEMY_COORDINATES
:
1780 Unit
* currentTarget
= m_targets
.getUnitTarget();
1783 TagUnitMap
.push_back(currentTarget
);
1784 m_targets
.setDestination(currentTarget
->GetPositionX(), currentTarget
->GetPositionY(), currentTarget
->GetPositionZ());
1785 if(m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_ENEMY_IN_AREA_INSTANT
)
1787 CellPair
p(MaNGOS::ComputeCellPair(currentTarget
->GetPositionX(), currentTarget
->GetPositionY()));
1789 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1791 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
,PUSH_TARGET_CENTER
, SPELL_TARGETS_AOE_DAMAGE
);
1792 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_notifier(notifier
);
1793 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_notifier(notifier
);
1794 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1795 cell_lock
->Visit(cell_lock
, world_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1796 cell_lock
->Visit(cell_lock
, grid_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1800 case TARGET_AREAEFFECT_PARTY_AND_CLASS
:
1802 Player
* targetPlayer
= m_targets
.getUnitTarget() && m_targets
.getUnitTarget()->GetTypeId() == TYPEID_PLAYER
1803 ? (Player
*)m_targets
.getUnitTarget() : NULL
;
1805 Group
* pGroup
= targetPlayer
? targetPlayer
->GetGroup() : NULL
;
1808 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1810 Player
* Target
= itr
->getSource();
1812 // IsHostileTo check duel and controlled by enemy
1813 if( Target
&& targetPlayer
->IsWithinDistInMap(Target
, radius
) &&
1814 targetPlayer
->getClass() == Target
->getClass() &&
1815 !m_caster
->IsHostileTo(Target
) )
1817 TagUnitMap
.push_back(Target
);
1821 else if(m_targets
.getUnitTarget())
1822 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1825 case TARGET_TABLE_X_Y_Z_COORDINATES
:
1827 SpellTargetPosition
const* st
= spellmgr
.GetSpellTargetPosition(m_spellInfo
->Id
);
1830 if (st
->target_mapId
== m_caster
->GetMapId())
1831 m_targets
.setDestination(st
->target_X
, st
->target_Y
, st
->target_Z
);
1833 // if B==TARGET_TABLE_X_Y_Z_COORDINATES then A already fill all required targets
1834 if (m_spellInfo
->EffectImplicitTargetB
[i
] && m_spellInfo
->EffectImplicitTargetB
[i
]!=TARGET_TABLE_X_Y_Z_COORDINATES
)
1836 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1838 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1841 SpellTargets targetB
= SPELL_TARGETS_AOE_DAMAGE
;
1842 // Select friendly targets for positive effect
1843 if (IsPositiveEffect(m_spellInfo
->Id
, i
))
1844 targetB
= SPELL_TARGETS_FRIENDLY
;
1846 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
,PUSH_DEST_CENTER
, targetB
);
1848 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_notifier(notifier
);
1849 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_notifier(notifier
);
1851 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1852 cell_lock
->Visit(cell_lock
, world_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1853 cell_lock
->Visit(cell_lock
, grid_notifier
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
1857 sLog
.outError( "SPELL: unknown target coordinates for spell ID %u\n", m_spellInfo
->Id
);
1859 case TARGET_BEHIND_VICTIM
:
1861 Unit
*pTarget
= m_caster
->getVictim();
1862 if(!pTarget
&& m_caster
->GetTypeId() == TYPEID_PLAYER
)
1863 pTarget
= ObjectAccessor::GetUnit(*m_caster
, ((Player
*)m_caster
)->GetSelection());
1867 float _target_x
, _target_y
, _target_z
;
1868 pTarget
->GetClosePoint(_target_x
, _target_y
, _target_z
, m_caster
->GetObjectSize(), CONTACT_DISTANCE
, M_PI
);
1869 if(pTarget
->IsWithinLOS(_target_x
,_target_y
,_target_z
))
1870 m_targets
.setDestination(_target_x
, _target_y
, _target_z
);
1877 if (unMaxTargets
&& TagUnitMap
.size() > unMaxTargets
)
1879 // make sure one unit is always removed per iteration
1880 uint32 removed_utarget
= 0;
1881 for (std::list
<Unit
*>::iterator itr
= TagUnitMap
.begin(), next
; itr
!= TagUnitMap
.end(); itr
= next
)
1885 if (!*itr
) continue;
1886 if ((*itr
) == m_targets
.getUnitTarget())
1888 TagUnitMap
.erase(itr
);
1889 removed_utarget
= 1;
1893 // remove random units from the map
1894 while (TagUnitMap
.size() > unMaxTargets
- removed_utarget
)
1896 uint32 poz
= urand(0, TagUnitMap
.size()-1);
1897 for (std::list
<Unit
*>::iterator itr
= TagUnitMap
.begin(); itr
!= TagUnitMap
.end(); ++itr
, --poz
)
1899 if (!*itr
) continue;
1902 TagUnitMap
.erase(itr
);
1907 // the player's target will always be added to the map
1908 if (removed_utarget
&& m_targets
.getUnitTarget())
1909 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1913 void Spell::prepare(SpellCastTargets
* targets
, Aura
* triggeredByAura
)
1915 m_targets
= *targets
;
1917 m_spellState
= SPELL_STATE_PREPARING
;
1919 m_castPositionX
= m_caster
->GetPositionX();
1920 m_castPositionY
= m_caster
->GetPositionY();
1921 m_castPositionZ
= m_caster
->GetPositionZ();
1922 m_castOrientation
= m_caster
->GetOrientation();
1925 m_triggeredByAuraSpell
= triggeredByAura
->GetSpellProto();
1927 // create and add update event for this spell
1928 SpellEvent
* Event
= new SpellEvent(this);
1929 m_caster
->m_Events
.AddEvent(Event
, m_caster
->m_Events
.CalculateTime(1));
1931 //Prevent casting at cast another spell (ServerSide check)
1932 if(m_caster
->IsNonMeleeSpellCasted(false, true) && m_cast_count
)
1934 SendCastResult(SPELL_FAILED_SPELL_IN_PROGRESS
);
1940 m_powerCost
= CalculatePowerCost();
1942 uint8 result
= CanCast(true);
1943 if(result
!= 0 && !IsAutoRepeat()) //always cast autorepeat dummy for triggering
1947 SendChannelUpdate(0);
1948 triggeredByAura
->SetAuraDuration(0);
1950 SendCastResult(result
);
1955 // calculate cast time (calculated after first CanCast check to prevent charge counting for first CanCast fail)
1956 m_casttime
= GetSpellCastTime(m_spellInfo
, this);
1958 // set timer base at cast time
1961 // stealth must be removed at cast starting (at show channel bar)
1962 // skip triggered spell (item equip spell casting and other not explicit character casts/item uses)
1963 if ( !m_IsTriggeredSpell
&& isSpellBreakStealth(m_spellInfo
) )
1965 m_caster
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
1966 m_caster
->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH
);
1969 if(m_IsTriggeredSpell
)
1973 m_caster
->SetCurrentCastedSpell( this );
1974 m_selfContainer
= &(m_caster
->m_currentSpells
[GetCurrentContainer()]);
1979 void Spell::cancel()
1981 if(m_spellState
== SPELL_STATE_FINISHED
)
1984 m_autoRepeat
= false;
1985 switch (m_spellState
)
1987 case SPELL_STATE_PREPARING
:
1988 case SPELL_STATE_DELAYED
:
1991 SendCastResult(SPELL_FAILED_INTERRUPTED
);
1994 case SPELL_STATE_CASTING
:
1996 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
1998 if( ihit
->missCondition
== SPELL_MISS_NONE
)
2000 Unit
* unit
= m_caster
->GetGUID()==(*ihit
).targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
2001 if( unit
&& unit
->isAlive() )
2002 unit
->RemoveAurasDueToSpell(m_spellInfo
->Id
);
2006 m_caster
->RemoveAurasDueToSpell(m_spellInfo
->Id
);
2007 SendChannelUpdate(0);
2009 SendCastResult(SPELL_FAILED_INTERRUPTED
);
2018 m_caster
->RemoveDynObject(m_spellInfo
->Id
);
2019 m_caster
->RemoveGameObject(m_spellInfo
->Id
,true);
2022 void Spell::cast(bool skipCheck
)
2024 SetExecutedCurrently(true);
2026 uint8 castResult
= 0;
2028 // update pointers base at GUIDs to prevent access to non-existed already object
2031 // cancel at lost main target unit
2032 if(!m_targets
.getUnitTarget() && m_targets
.getUnitTargetGUID() && m_targets
.getUnitTargetGUID() != m_caster
->GetGUID())
2035 SetExecutedCurrently(false);
2039 if(m_caster
->GetTypeId() != TYPEID_PLAYER
&& m_targets
.getUnitTarget() && m_targets
.getUnitTarget() != m_caster
)
2040 m_caster
->SetInFront(m_targets
.getUnitTarget());
2042 castResult
= CheckPower();
2045 SendCastResult(castResult
);
2047 SetExecutedCurrently(false);
2051 // triggered cast called from Spell::prepare where it was already checked
2054 castResult
= CanCast(false);
2057 SendCastResult(castResult
);
2059 SetExecutedCurrently(false);
2064 // Conflagrate - consumes immolate
2065 if ((m_spellInfo
->TargetAuraState
== AURA_STATE_IMMOLATE
) && m_targets
.getUnitTarget())
2067 // for caster applied auras only
2068 Unit::AuraList
const &mPeriodic
= m_targets
.getUnitTarget()->GetAurasByType(SPELL_AURA_PERIODIC_DAMAGE
);
2069 for(Unit::AuraList::const_iterator i
= mPeriodic
.begin(); i
!= mPeriodic
.end(); ++i
)
2071 if( (*i
)->GetSpellProto()->SpellFamilyName
== SPELLFAMILY_WARLOCK
&& ((*i
)->GetSpellProto()->SpellFamilyFlags
& 4) &&
2072 (*i
)->GetCasterGUID()==m_caster
->GetGUID() )
2074 m_targets
.getUnitTarget()->RemoveAura((*i
)->GetId(), (*i
)->GetEffIndex());
2080 // traded items have trade slot instead of guid in m_itemTargetGUID
2081 // set to real guid to be sent later to the client
2082 m_targets
.updateTradeSlotItem();
2085 SendSpellCooldown();
2088 TakeReagents(); // we must remove reagents before HandleEffects to allow place crafted item in same slot
2091 if(m_spellState
== SPELL_STATE_FINISHED
) // stop cast if spell marked as finish somewhere in Take*/FillTargetMap
2093 SetExecutedCurrently(false);
2097 SendCastResult(castResult
);
2098 SendSpellGo(); // we must send smsg_spell_go packet before m_castItem delete in TakeCastItem()...
2100 // Pass cast spell event to handler (not send triggered by aura spells)
2101 if (m_spellInfo
->DmgClass
!= SPELL_DAMAGE_CLASS_MELEE
&& m_spellInfo
->DmgClass
!= SPELL_DAMAGE_CLASS_RANGED
&& !m_triggeredByAuraSpell
)
2103 m_caster
->ProcDamageAndSpell(m_targets
.getUnitTarget(), PROC_FLAG_CAST_SPELL
, PROC_FLAG_NONE
, 0, SPELL_SCHOOL_MASK_NONE
, m_spellInfo
, m_IsTriggeredSpell
);
2105 // update pointers base at GUIDs to prevent access to non-existed already object
2106 UpdatePointers(); // pointers can be invalidate at triggered spell casting
2109 // Okay, everything is prepared. Now we need to distinguish between immediate and evented delayed spells
2110 if (m_spellInfo
->speed
> 0.0f
)
2113 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2114 // in case delayed spell remove item at cast delay start
2117 // Okay, maps created, now prepare flags
2118 m_immediateHandled
= false;
2119 m_spellState
= SPELL_STATE_DELAYED
;
2124 // Immediate spell, no big deal
2128 SetExecutedCurrently(false);
2131 void Spell::handle_immediate()
2133 // start channeling if applicable
2134 if(IsChanneledSpell(m_spellInfo
))
2136 m_spellState
= SPELL_STATE_CASTING
;
2137 SendChannelStart(GetSpellDuration(m_spellInfo
));
2140 // process immediate effects (items, ground, etc.) also initialize some variables
2141 _handle_immediate_phase();
2143 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2144 DoAllEffectOnTarget(&(*ihit
));
2146 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
2147 DoAllEffectOnTarget(&(*ihit
));
2149 // spell is finished, perform some last features of the spell here
2150 _handle_finish_phase();
2152 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2155 if(m_spellState
!= SPELL_STATE_CASTING
)
2156 finish(true); // successfully finish spell cast (not last in case autorepeat or channel spell)
2159 uint64
Spell::handle_delayed(uint64 t_offset
)
2161 uint64 next_time
= 0;
2163 if (!m_immediateHandled
)
2165 _handle_immediate_phase();
2166 m_immediateHandled
= true;
2169 // now recheck units targeting correctness (need before any effects apply to prevent adding immunity at first effect not allow apply second spell effect and similar cases)
2170 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2172 if (ihit
->processed
== false)
2174 if ( ihit
->timeDelay
<= t_offset
)
2175 DoAllEffectOnTarget(&(*ihit
));
2176 else if( next_time
== 0 || ihit
->timeDelay
< next_time
)
2177 next_time
= ihit
->timeDelay
;
2181 // now recheck gameobject targeting correctness
2182 for(std::list
<GOTargetInfo
>::iterator ighit
= m_UniqueGOTargetInfo
.begin(); ighit
!= m_UniqueGOTargetInfo
.end();++ighit
)
2184 if (ighit
->processed
== false)
2186 if ( ighit
->timeDelay
<= t_offset
)
2187 DoAllEffectOnTarget(&(*ighit
));
2188 else if( next_time
== 0 || ighit
->timeDelay
< next_time
)
2189 next_time
= ighit
->timeDelay
;
2192 // All targets passed - need finish phase
2195 // spell is finished, perform some last features of the spell here
2196 _handle_finish_phase();
2198 finish(true); // successfully finish spell cast
2200 // return zero, spell is finished now
2205 // spell is unfinished, return next execution time
2210 void Spell::_handle_immediate_phase()
2212 // handle some immediate features of the spell here
2213 HandleThreatSpells(m_spellInfo
->Id
);
2215 m_needSpellLog
= IsNeedSendToClient();
2216 for(uint32 j
= 0;j
<3;j
++)
2218 if(m_spellInfo
->Effect
[j
]==0)
2221 // apply Send Event effect to ground in case empty target lists
2222 if( m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SEND_EVENT
&& !HaveTargetsForEffect(j
) )
2224 HandleEffects(NULL
,NULL
,NULL
, j
);
2228 // Don't do spell log, if is school damage spell
2229 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SCHOOL_DAMAGE
|| m_spellInfo
->Effect
[j
] == 0)
2230 m_needSpellLog
= false;
2232 uint32 EffectChainTarget
= m_spellInfo
->EffectChainTarget
[j
];
2233 if(m_originalCaster
)
2234 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
2235 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_JUMP_TARGETS
, EffectChainTarget
, this);
2237 // initialize multipliers
2238 m_damageMultipliers
[j
] = 1.0f
;
2239 if( (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_DAMAGE
|| m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_HEAL
) &&
2240 (EffectChainTarget
> 1) )
2241 m_applyMultiplierMask
|= 1 << j
;
2244 // initialize Diminishing Returns Data
2245 m_diminishLevel
= DIMINISHING_LEVEL_1
;
2246 m_diminishGroup
= DIMINISHING_NONE
;
2249 for(std::list
<ItemTargetInfo
>::iterator ihit
= m_UniqueItemInfo
.begin();ihit
!= m_UniqueItemInfo
.end();++ihit
)
2250 DoAllEffectOnTarget(&(*ihit
));
2253 for(uint32 j
= 0;j
<3;j
++)
2255 // persistent area auras target only the ground
2256 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_PERSISTENT_AREA_AURA
)
2257 HandleEffects(NULL
,NULL
,NULL
, j
);
2261 void Spell::_handle_finish_phase()
2268 void Spell::SendSpellCooldown()
2270 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
2273 Player
* _player
= (Player
*)m_caster
;
2274 // Add cooldown for max (disable spell)
2275 // Cooldown started on SendCooldownEvent call
2276 if (m_spellInfo
->Attributes
& SPELL_ATTR_DISABLED_WHILE_ACTIVE
)
2278 _player
->AddSpellCooldown(m_spellInfo
->Id
, 0, time(NULL
) - 1);
2282 // init cooldown values
2287 // some special item spells without correct cooldown in SpellInfo
2288 // cooldown information stored in item prototype
2289 // This used in same way in WorldSession::HandleItemQuerySingleOpcode data sending to client.
2293 ItemPrototype
const* proto
= m_CastItem
->GetProto();
2296 for(int idx
= 0; idx
< 5; ++idx
)
2298 if(proto
->Spells
[idx
].SpellId
== m_spellInfo
->Id
)
2300 cat
= proto
->Spells
[idx
].SpellCategory
;
2301 rec
= proto
->Spells
[idx
].SpellCooldown
;
2302 catrec
= proto
->Spells
[idx
].SpellCategoryCooldown
;
2309 // if no cooldown found above then base at DBC data
2310 if(rec
< 0 && catrec
< 0)
2312 cat
= m_spellInfo
->Category
;
2313 rec
= m_spellInfo
->RecoveryTime
;
2314 catrec
= m_spellInfo
->CategoryRecoveryTime
;
2317 // shoot spells used equipped item cooldown values already assigned in GetAttackTime(RANGED_ATTACK)
2318 // prevent 0 cooldowns set by another way
2319 if (rec
<= 0 && catrec
<= 0 && (cat
== 76 || cat
== 351))
2320 rec
= _player
->GetAttackTime(RANGED_ATTACK
);
2322 // Now we have cooldown data (if found any), time to apply mods
2324 _player
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COOLDOWN
, rec
, this);
2327 _player
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COOLDOWN
, catrec
, this);
2329 // replace negative cooldowns by 0
2330 if (rec
< 0) rec
= 0;
2331 if (catrec
< 0) catrec
= 0;
2333 // no cooldown after applying spell mods
2334 if( rec
== 0 && catrec
== 0)
2337 time_t curTime
= time(NULL
);
2339 time_t catrecTime
= catrec
? curTime
+catrec
/1000 : 0; // in secs
2340 time_t recTime
= rec
? curTime
+rec
/1000 : catrecTime
;// in secs
2342 // self spell cooldown
2344 _player
->AddSpellCooldown(m_spellInfo
->Id
, m_CastItem
? m_CastItem
->GetEntry() : 0, recTime
);
2349 SpellCategoryStore::const_iterator i_scstore
= sSpellCategoryStore
.find(cat
);
2350 if(i_scstore
!= sSpellCategoryStore
.end())
2352 for(SpellCategorySet::const_iterator i_scset
= i_scstore
->second
.begin(); i_scset
!= i_scstore
->second
.end(); ++i_scset
)
2354 if(*i_scset
== m_spellInfo
->Id
) // skip main spell, already handled above
2357 _player
->AddSpellCooldown(m_spellInfo
->Id
, m_CastItem
? m_CastItem
->GetEntry() : 0, catrecTime
);
2363 void Spell::update(uint32 difftime
)
2365 // update pointers based at it's GUIDs
2368 if(m_targets
.getUnitTargetGUID() && !m_targets
.getUnitTarget())
2374 // check if the player caster has moved before the spell finished
2375 if ((m_caster
->GetTypeId() == TYPEID_PLAYER
&& m_timer
!= 0) &&
2376 (m_castPositionX
!= m_caster
->GetPositionX() || m_castPositionY
!= m_caster
->GetPositionY() || m_castPositionZ
!= m_caster
->GetPositionZ()) &&
2377 (m_spellInfo
->Effect
[0] != SPELL_EFFECT_STUCK
|| !m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_FALLING
)))
2379 // always cancel for channeled spells
2380 if( m_spellState
== SPELL_STATE_CASTING
)
2382 // don't cancel for melee, autorepeat, triggered and instant spells
2383 else if(!IsNextMeleeSwingSpell() && !IsAutoRepeat() && !m_IsTriggeredSpell
&& (m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_MOVEMENT
))
2387 switch(m_spellState
)
2389 case SPELL_STATE_PREPARING
:
2393 if(difftime
>= m_timer
)
2396 m_timer
-= difftime
;
2399 if(m_timer
== 0 && !IsNextMeleeSwingSpell() && !IsAutoRepeat())
2402 case SPELL_STATE_CASTING
:
2406 if( m_caster
->GetTypeId() == TYPEID_PLAYER
)
2408 // check if player has jumped before the channeling finished
2409 if(m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_JUMPING
))
2412 // check for incapacitating player states
2413 if( m_caster
->hasUnitState(UNIT_STAT_STUNNED
| UNIT_STAT_CONFUSED
))
2416 // check if player has turned if flag is set
2417 if( m_spellInfo
->ChannelInterruptFlags
& CHANNEL_FLAG_TURNING
&& m_castOrientation
!= m_caster
->GetOrientation() )
2421 // check if there are alive targets left
2422 if (!IsAliveUnitPresentInTargetList())
2424 SendChannelUpdate(0);
2428 if(difftime
>= m_timer
)
2431 m_timer
-= difftime
;
2436 SendChannelUpdate(0);
2438 // channeled spell processed independently for quest targeting
2439 // cast at creature (or GO) quest objectives update at successful cast channel finished
2440 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
2441 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() )
2443 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2445 TargetInfo
* target
= &*ihit
;
2446 if(!IS_CREATURE_GUID(target
->targetGUID
))
2449 Unit
* unit
= m_caster
->GetGUID()==target
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
,target
->targetGUID
);
2453 ((Player
*)m_caster
)->CastedCreatureOrGO(unit
->GetEntry(),unit
->GetGUID(),m_spellInfo
->Id
);
2456 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
2458 GOTargetInfo
* target
= &*ihit
;
2460 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, target
->targetGUID
);
2464 ((Player
*)m_caster
)->CastedCreatureOrGO(go
->GetEntry(),go
->GetGUID(),m_spellInfo
->Id
);
2477 void Spell::finish(bool ok
)
2482 if(m_spellState
== SPELL_STATE_FINISHED
)
2485 m_spellState
= SPELL_STATE_FINISHED
;
2488 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2489 ((Player
*)m_caster
)->RemoveSpellMods(this);
2491 // other code related only to successfully finished spells
2495 //handle SPELL_AURA_ADD_TARGET_TRIGGER auras
2496 Unit::AuraList
const& targetTriggers
= m_caster
->GetAurasByType(SPELL_AURA_ADD_TARGET_TRIGGER
);
2497 for(Unit::AuraList::const_iterator i
= targetTriggers
.begin(); i
!= targetTriggers
.end(); ++i
)
2499 SpellEntry
const *auraSpellInfo
= (*i
)->GetSpellProto();
2500 uint32 auraSpellIdx
= (*i
)->GetEffIndex();
2501 if (IsAffectedBy(auraSpellInfo
, auraSpellIdx
))
2503 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2504 if( ihit
->effectMask
& (1<<auraSpellIdx
) )
2506 // check m_caster->GetGUID() let load auras at login and speedup most often case
2507 Unit
*unit
= m_caster
->GetGUID()== ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
2508 if (unit
&& unit
->isAlive())
2510 // Calculate chance at that moment (can be depend for example from combo points)
2511 int32 chance
= m_caster
->CalculateSpellDamage(auraSpellInfo
, auraSpellIdx
, (*i
)->GetBasePoints(),unit
);
2513 if(roll_chance_i(chance
))
2514 m_caster
->CastSpell(unit
, auraSpellInfo
->EffectTriggerSpell
[auraSpellIdx
], true, NULL
, (*i
));
2520 if (IsMeleeAttackResetSpell())
2522 m_caster
->resetAttackTimer(BASE_ATTACK
);
2523 if(m_caster
->haveOffhandWeapon())
2524 m_caster
->resetAttackTimer(OFF_ATTACK
);
2527 /*if (IsRangedAttackResetSpell())
2528 m_caster->resetAttackTimer(RANGED_ATTACK);*/
2530 // Clear combo at finish state
2531 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& NeedsComboPoints(m_spellInfo
))
2532 ((Player
*)m_caster
)->ClearComboPoints();
2534 // call triggered spell only at successful cast (after clear combo points -> for add some if need)
2535 if(!m_TriggerSpells
.empty())
2538 // Stop Attack for some spells
2539 if( m_spellInfo
->Attributes
& SPELL_ATTR_STOP_ATTACK_TARGET
)
2540 m_caster
->AttackStop();
2543 void Spell::SendCastResult(uint8 result
)
2545 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2548 if(((Player
*)m_caster
)->GetSession()->PlayerLoading()) // don't send cast results at loading time
2553 WorldPacket
data(SMSG_CAST_FAILED
, (4+1+1));
2554 data
<< uint8(m_cast_count
); // single cast or multi 2.3 (0/1)
2555 data
<< uint32(m_spellInfo
->Id
);
2556 data
<< uint8(result
); // problem
2559 case SPELL_FAILED_REQUIRES_SPELL_FOCUS
:
2560 data
<< uint32(m_spellInfo
->RequiresSpellFocus
);
2562 case SPELL_FAILED_REQUIRES_AREA
:
2563 // hardcode areas limitation case
2564 if( m_spellInfo
->Id
==41618 || m_spellInfo
->Id
==41620 )
2565 data
<< uint32(3842);
2566 else if( m_spellInfo
->Id
==41617 || m_spellInfo
->Id
==41619 )
2567 data
<< uint32(3905);
2570 data
<< uint32(m_spellInfo
->AreaId
);
2572 case SPELL_FAILED_TOTEMS
:
2573 if(m_spellInfo
->Totem
[0])
2574 data
<< uint32(m_spellInfo
->Totem
[0]);
2575 if(m_spellInfo
->Totem
[1])
2576 data
<< uint32(m_spellInfo
->Totem
[1]);
2578 case SPELL_FAILED_TOTEM_CATEGORY
:
2579 if(m_spellInfo
->TotemCategory
[0])
2580 data
<< uint32(m_spellInfo
->TotemCategory
[0]);
2581 if(m_spellInfo
->TotemCategory
[1])
2582 data
<< uint32(m_spellInfo
->TotemCategory
[1]);
2584 case SPELL_FAILED_EQUIPPED_ITEM_CLASS
:
2585 data
<< uint32(m_spellInfo
->EquippedItemClass
);
2586 data
<< uint32(m_spellInfo
->EquippedItemSubClassMask
);
2587 data
<< uint32(m_spellInfo
->EquippedItemInventoryTypeMask
);
2590 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
2594 WorldPacket
data(SMSG_CLEAR_EXTRA_AURA_INFO_OBSOLETE
, (8+4));
2595 data
.append(m_caster
->GetPackGUID());
2596 data
<< uint32(m_spellInfo
->Id
);
2597 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
2601 void Spell::SendSpellStart()
2603 if(!IsNeedSendToClient())
2606 sLog
.outDebug("Sending SMSG_SPELL_START id=%u",m_spellInfo
->Id
);
2608 uint32 castFlags
= CAST_FLAG_UNKNOWN1
;
2610 castFlags
|= CAST_FLAG_AMMO
;
2613 if(!m_targets
.getUnitTarget())
2616 target
= m_targets
.getUnitTarget();
2618 WorldPacket
data(SMSG_SPELL_START
, (8+8+4+4+2));
2620 data
.append(m_CastItem
->GetPackGUID());
2622 data
.append(m_caster
->GetPackGUID());
2624 data
.append(m_caster
->GetPackGUID());
2625 data
<< uint8(m_cast_count
); // pending spell cast?
2626 data
<< uint32(m_spellInfo
->Id
);
2627 data
<< uint32(castFlags
);
2628 data
<< uint32(m_timer
);
2630 m_targets
.write(&data
);
2632 if ( castFlags
& CAST_FLAG_UNKNOWN6
)
2635 if ( castFlags
& CAST_FLAG_UNKNOWN7
)
2641 for(uint8 i
= 0; i
< 6; ++i
)
2647 if ( castFlags
& CAST_FLAG_AMMO
)
2648 WriteAmmoToPacket(&data
);
2650 m_caster
->SendMessageToSet(&data
, true);
2653 void Spell::SendSpellGo()
2655 // not send invisible spell casting
2656 if(!IsNeedSendToClient())
2659 sLog
.outDebug("Sending SMSG_SPELL_GO id=%u",m_spellInfo
->Id
);
2662 if(!m_targets
.getUnitTarget())
2665 target
= m_targets
.getUnitTarget();
2667 uint32 castFlags
= CAST_FLAG_UNKNOWN3
;
2669 castFlags
|= CAST_FLAG_AMMO
;
2671 WorldPacket
data(SMSG_SPELL_GO
, 50); // guess size
2673 data
.append(m_CastItem
->GetPackGUID());
2675 data
.append(m_caster
->GetPackGUID());
2677 data
.append(m_caster
->GetPackGUID());
2678 data
<< uint8(m_cast_count
); // pending spell cast?
2679 data
<< uint32(m_spellInfo
->Id
);
2680 data
<< uint32(castFlags
);
2681 data
<< uint32(getMSTime()); // timestamp
2683 WriteSpellGoTargets(&data
);
2685 m_targets
.write(&data
);
2687 if ( castFlags
& CAST_FLAG_UNKNOWN6
) // unknown wotlk
2690 if ( castFlags
& CAST_FLAG_UNKNOWN7
)
2696 for(uint8 i
= 0; i
< 6; ++i
)
2702 if ( castFlags
& CAST_FLAG_UNKNOWN4
) // unknown wotlk
2708 if ( castFlags
& CAST_FLAG_AMMO
)
2709 WriteAmmoToPacket(&data
);
2711 if ( castFlags
& CAST_FLAG_UNKNOWN5
) // unknown wotlk
2717 if ( m_targets
.m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
2722 m_caster
->SendMessageToSet(&data
, true);
2725 void Spell::WriteAmmoToPacket( WorldPacket
* data
)
2727 uint32 ammoInventoryType
= 0;
2728 uint32 ammoDisplayID
= 0;
2730 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2732 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack( RANGED_ATTACK
);
2735 ammoInventoryType
= pItem
->GetProto()->InventoryType
;
2736 if( ammoInventoryType
== INVTYPE_THROWN
)
2737 ammoDisplayID
= pItem
->GetProto()->DisplayInfoID
;
2740 uint32 ammoID
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
2743 ItemPrototype
const *pProto
= objmgr
.GetItemPrototype( ammoID
);
2746 ammoDisplayID
= pProto
->DisplayInfoID
;
2747 ammoInventoryType
= pProto
->InventoryType
;
2750 else if(m_caster
->GetDummyAura(46699)) // Requires No Ammo
2752 ammoDisplayID
= 5996; // normal arrow
2753 ammoInventoryType
= INVTYPE_AMMO
;
2758 // TODO: implement selection ammo data based at ranged weapon stored in equipmodel/equipinfo/equipslot fields
2760 *data
<< uint32(ammoDisplayID
);
2761 *data
<< uint32(ammoInventoryType
);
2764 void Spell::WriteSpellGoTargets( WorldPacket
* data
)
2766 *data
<< (uint8
)m_countOfHit
;
2767 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2768 if ((*ihit
).missCondition
== SPELL_MISS_NONE
) // Add only hits
2769 *data
<< uint64(ihit
->targetGUID
);
2771 for(std::list
<GOTargetInfo
>::iterator ighit
= m_UniqueGOTargetInfo
.begin();ighit
!= m_UniqueGOTargetInfo
.end();++ighit
)
2772 *data
<< uint64(ighit
->targetGUID
); // Always hits
2774 *data
<< (uint8
)m_countOfMiss
;
2775 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2777 if( ihit
->missCondition
!= SPELL_MISS_NONE
) // Add only miss
2779 *data
<< uint64(ihit
->targetGUID
);
2780 *data
<< uint8(ihit
->missCondition
);
2781 if( ihit
->missCondition
== SPELL_MISS_REFLECT
)
2782 *data
<< uint8(ihit
->reflectResult
);
2787 void Spell::SendLogExecute()
2789 Unit
*target
= m_targets
.getUnitTarget() ? m_targets
.getUnitTarget() : m_caster
;
2791 WorldPacket
data(SMSG_SPELLLOGEXECUTE
, (8+4+4+4+4+8));
2793 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
2794 data
.append(m_caster
->GetPackGUID());
2796 data
.append(target
->GetPackGUID());
2798 data
<< uint32(m_spellInfo
->Id
);
2800 data
<< uint32(count1
); // count1 (effect count?)
2801 for(uint32 i
= 0; i
< count1
; ++i
)
2803 data
<< uint32(m_spellInfo
->Effect
[0]); // spell effect?
2805 data
<< uint32(count2
); // count2 (target count?)
2806 for(uint32 j
= 0; j
< count2
; ++j
)
2808 switch(m_spellInfo
->Effect
[0])
2810 case SPELL_EFFECT_POWER_DRAIN
:
2811 if(Unit
*unit
= m_targets
.getUnitTarget())
2812 data
.append(unit
->GetPackGUID());
2819 case SPELL_EFFECT_ADD_EXTRA_ATTACKS
:
2820 if(Unit
*unit
= m_targets
.getUnitTarget())
2821 data
.append(unit
->GetPackGUID());
2824 data
<< uint32(0); // count?
2826 case SPELL_EFFECT_INTERRUPT_CAST
:
2827 if(Unit
*unit
= m_targets
.getUnitTarget())
2828 data
.append(unit
->GetPackGUID());
2831 data
<< uint32(0); // spellid
2833 case SPELL_EFFECT_DURABILITY_DAMAGE
:
2834 if(Unit
*unit
= m_targets
.getUnitTarget())
2835 data
.append(unit
->GetPackGUID());
2841 case SPELL_EFFECT_OPEN_LOCK
:
2842 case SPELL_EFFECT_OPEN_LOCK_ITEM
:
2843 if(Item
*item
= m_targets
.getItemTarget())
2844 data
.append(item
->GetPackGUID());
2848 case SPELL_EFFECT_CREATE_ITEM
:
2849 data
<< uint32(m_spellInfo
->EffectItemType
[0]);
2851 case SPELL_EFFECT_SUMMON
:
2852 case SPELL_EFFECT_SUMMON_WILD
:
2853 case SPELL_EFFECT_SUMMON_GUARDIAN
:
2854 case SPELL_EFFECT_TRANS_DOOR
:
2855 case SPELL_EFFECT_SUMMON_PET
:
2856 case SPELL_EFFECT_SUMMON_POSSESSED
:
2857 //case SPELL_EFFECT_SUMMON_TOTEM:
2858 case SPELL_EFFECT_SUMMON_OBJECT_WILD
:
2859 case SPELL_EFFECT_CREATE_HOUSE
:
2860 case SPELL_EFFECT_DUEL
:
2861 case SPELL_EFFECT_SUMMON_TOTEM_SLOT1
:
2862 case SPELL_EFFECT_SUMMON_TOTEM_SLOT2
:
2863 case SPELL_EFFECT_SUMMON_TOTEM_SLOT3
:
2864 case SPELL_EFFECT_SUMMON_TOTEM_SLOT4
:
2865 case SPELL_EFFECT_SUMMON_PHANTASM
:
2866 case SPELL_EFFECT_SUMMON_CRITTER
:
2867 case SPELL_EFFECT_SUMMON_OBJECT_SLOT1
:
2868 case SPELL_EFFECT_SUMMON_OBJECT_SLOT2
:
2869 case SPELL_EFFECT_SUMMON_OBJECT_SLOT3
:
2870 case SPELL_EFFECT_SUMMON_OBJECT_SLOT4
:
2871 case SPELL_EFFECT_SUMMON_DEMON
:
2872 case SPELL_EFFECT_150
:
2873 if(Unit
*unit
= m_targets
.getUnitTarget())
2874 data
.append(unit
->GetPackGUID());
2875 else if(m_targets
.getItemTargetGUID())
2876 data
.appendPackGUID(m_targets
.getItemTargetGUID());
2877 else if(GameObject
*go
= m_targets
.getGOTarget())
2878 data
.append(go
->GetPackGUID());
2880 data
<< uint8(0); // guid
2882 case SPELL_EFFECT_FEED_PET
:
2883 data
<< uint32(m_targets
.getItemTargetEntry());
2885 case SPELL_EFFECT_DISMISS_PET
:
2886 if(Unit
*unit
= m_targets
.getUnitTarget())
2887 data
.append(unit
->GetPackGUID());
2897 m_caster
->SendMessageToSet(&data
, true);
2900 void Spell::SendInterrupted(uint8 result
)
2902 WorldPacket
data(SMSG_SPELL_FAILURE
, (8+4+1));
2903 data
.append(m_caster
->GetPackGUID());
2904 data
<< uint8(m_cast_count
);
2905 data
<< uint32(m_spellInfo
->Id
);
2906 data
<< uint8(result
);
2907 m_caster
->SendMessageToSet(&data
, true);
2909 data
.Initialize(SMSG_SPELL_FAILED_OTHER
, (8+4));
2910 data
.append(m_caster
->GetPackGUID());
2911 data
<< uint8(m_cast_count
);
2912 data
<< uint32(m_spellInfo
->Id
);
2913 data
<< uint8(result
);
2914 m_caster
->SendMessageToSet(&data
, true);
2917 void Spell::SendChannelUpdate(uint32 time
)
2921 m_caster
->SetUInt64Value(UNIT_FIELD_CHANNEL_OBJECT
,0);
2922 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
,0);
2925 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2928 WorldPacket
data( MSG_CHANNEL_UPDATE
, 8+4 );
2929 data
.append(m_caster
->GetPackGUID());
2932 ((Player
*)m_caster
)->GetSession()->SendPacket( &data
);
2935 void Spell::SendChannelStart(uint32 duration
)
2937 WorldObject
* target
= NULL
;
2939 // select first not resisted target from target list for _0_ effect
2940 if(!m_UniqueTargetInfo
.empty())
2942 for(std::list
<TargetInfo
>::iterator itr
= m_UniqueTargetInfo
.begin();itr
!= m_UniqueTargetInfo
.end();++itr
)
2944 if( (itr
->effectMask
& (1<<0)) && itr
->reflectResult
==SPELL_MISS_NONE
&& itr
->targetGUID
!= m_caster
->GetGUID())
2946 target
= ObjectAccessor::GetUnit(*m_caster
, itr
->targetGUID
);
2951 else if(!m_UniqueGOTargetInfo
.empty())
2953 for(std::list
<GOTargetInfo
>::iterator itr
= m_UniqueGOTargetInfo
.begin();itr
!= m_UniqueGOTargetInfo
.end();++itr
)
2955 if(itr
->effectMask
& (1<<0) )
2957 target
= ObjectAccessor::GetGameObject(*m_caster
, itr
->targetGUID
);
2963 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2965 WorldPacket
data( MSG_CHANNEL_START
, (8+4+4) );
2966 data
.append(m_caster
->GetPackGUID());
2967 data
<< m_spellInfo
->Id
;
2970 ((Player
*)m_caster
)->GetSession()->SendPacket( &data
);
2975 m_caster
->SetUInt64Value(UNIT_FIELD_CHANNEL_OBJECT
, target
->GetGUID());
2976 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
, m_spellInfo
->Id
);
2979 void Spell::SendResurrectRequest(Player
* target
)
2981 WorldPacket
data(SMSG_RESURRECT_REQUEST
, (8+4+2+4));
2982 data
<< m_caster
->GetGUID();
2983 data
<< uint32(1) << uint16(0) << uint32(1);
2985 target
->GetSession()->SendPacket(&data
);
2988 void Spell::SendPlaySpellVisual(uint32 SpellID
)
2990 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2993 WorldPacket
data(SMSG_PLAY_SPELL_VISUAL
, 12);
2994 data
<< m_caster
->GetGUID();
2996 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
2999 void Spell::TakeCastItem()
3001 if(!m_CastItem
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
3004 // not remove cast item at triggered spell (equipping, weapon damage, etc)
3005 if(m_IsTriggeredSpell
)
3008 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3012 // This code is to avoid a crash
3013 // I'm not sure, if this is really an error, but I guess every item needs a prototype
3014 sLog
.outError("Cast item has no item prototype highId=%d, lowId=%d",m_CastItem
->GetGUIDHigh(), m_CastItem
->GetGUIDLow());
3018 bool expendable
= false;
3019 bool withoutCharges
= false;
3021 for (int i
= 0; i
<5; i
++)
3023 if (proto
->Spells
[i
].SpellId
)
3025 // item has limited charges
3026 if (proto
->Spells
[i
].SpellCharges
)
3028 if (proto
->Spells
[i
].SpellCharges
< 0)
3031 int32 charges
= m_CastItem
->GetSpellCharges(i
);
3033 // item has charges left
3036 (charges
> 0) ? --charges
: ++charges
; // abs(charges) less at 1 after use
3037 if (proto
->Stackable
< 2)
3038 m_CastItem
->SetSpellCharges(i
, charges
);
3039 m_CastItem
->SetState(ITEM_CHANGED
, (Player
*)m_caster
);
3043 withoutCharges
= (charges
== 0);
3048 if (expendable
&& withoutCharges
)
3051 ((Player
*)m_caster
)->DestroyItemCount(m_CastItem
, count
, true);
3053 // prevent crash at access to deleted m_targets.getItemTarget
3054 if(m_CastItem
==m_targets
.getItemTarget())
3055 m_targets
.setItemTarget(NULL
);
3061 void Spell::TakePower()
3063 if(m_CastItem
|| m_triggeredByAuraSpell
)
3066 // health as power used
3067 if(m_spellInfo
->powerType
== POWER_HEALTH
)
3069 m_caster
->ModifyHealth( -(int32
)m_powerCost
);
3073 if(m_spellInfo
->powerType
>= MAX_POWERS
)
3075 sLog
.outError("Spell::TakePower: Unknown power type '%d'", m_spellInfo
->powerType
);
3079 Powers powerType
= Powers(m_spellInfo
->powerType
);
3081 m_caster
->ModifyPower(powerType
, -(int32
)m_powerCost
);
3083 // Set the five second timer
3084 if (powerType
== POWER_MANA
&& m_powerCost
> 0)
3085 m_caster
->SetLastManaUse(getMSTime());
3088 void Spell::TakeReagents()
3090 if(m_IsTriggeredSpell
) // reagents used in triggered spell removed by original spell or don't must be removed.
3093 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
3096 if (m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_NO_REAGENT_WHILE_PREP
&&
3097 m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PREPARATION
))
3100 Player
* p_caster
= (Player
*)m_caster
;
3102 for(uint32 x
=0;x
<8;x
++)
3104 if(m_spellInfo
->Reagent
[x
] <= 0)
3107 uint32 itemid
= m_spellInfo
->Reagent
[x
];
3108 uint32 itemcount
= m_spellInfo
->ReagentCount
[x
];
3110 // if CastItem is also spell reagent
3113 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3114 if( proto
&& proto
->ItemId
== itemid
)
3116 for(int s
=0;s
<5;s
++)
3118 // CastItem will be used up and does not count as reagent
3119 int32 charges
= m_CastItem
->GetSpellCharges(s
);
3120 if (proto
->Spells
[s
].SpellCharges
< 0 && abs(charges
) < 2)
3131 // if getItemTarget is also spell reagent
3132 if (m_targets
.getItemTargetEntry()==itemid
)
3133 m_targets
.setItemTarget(NULL
);
3135 p_caster
->DestroyItemCount(itemid
, itemcount
, true);
3139 void Spell::HandleThreatSpells(uint32 spellId
)
3141 if(!m_targets
.getUnitTarget() || !spellId
)
3144 if(!m_targets
.getUnitTarget()->CanHaveThreatList())
3147 SpellThreatEntry
const *threatSpell
= sSpellThreatStore
.LookupEntry
<SpellThreatEntry
>(spellId
);
3151 m_targets
.getUnitTarget()->AddThreat(m_caster
, float(threatSpell
->threat
));
3153 DEBUG_LOG("Spell %u, rank %u, added an additional %i threat", spellId
, spellmgr
.GetSpellRank(spellId
), threatSpell
->threat
);
3156 void Spell::HandleEffects(Unit
*pUnitTarget
,Item
*pItemTarget
,GameObject
*pGOTarget
,uint32 i
, float DamageMultiplier
)
3158 unitTarget
= pUnitTarget
;
3159 itemTarget
= pItemTarget
;
3160 gameObjTarget
= pGOTarget
;
3162 uint8 eff
= m_spellInfo
->Effect
[i
];
3163 uint32 mechanic
= m_spellInfo
->EffectMechanic
[i
];
3165 damage
= int32(CalculateDamage((uint8
)i
,unitTarget
)*DamageMultiplier
);
3167 sLog
.outDebug( "Spell: Effect : %u", eff
);
3169 //Simply return. Do not display "immune" in red text on client
3170 if(unitTarget
&& unitTarget
->IsImmunedToSpellEffect(eff
, mechanic
))
3173 if(eff
<TOTAL_SPELL_EFFECTS
)
3175 //sLog.outDebug( "WORLD: Spell FX %d < TOTAL_SPELL_EFFECTS ", eff);
3176 (*this.*SpellEffects
[eff
])(i
);
3181 sLog.outDebug( "WORLD: Spell FX %d > TOTAL_SPELL_EFFECTS ", eff);
3183 EffectEnchantItemTmp(i);
3186 sLog.outError("SPELL: unknown effect %u spell id %u\n",
3187 eff, m_spellInfo->Id);
3193 void Spell::TriggerSpell()
3195 for(TriggerSpells::iterator si
=m_TriggerSpells
.begin(); si
!=m_TriggerSpells
.end(); ++si
)
3197 Spell
* spell
= new Spell(m_caster
, (*si
), true, m_originalCasterGUID
, this->m_selfContainer
);
3198 spell
->prepare(&m_targets
); // use original spell original targets
3202 uint8
Spell::CanCast(bool strict
)
3204 // check cooldowns to prevent cheating
3205 if(m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
3207 if(m_triggeredByAuraSpell
)
3208 return SPELL_FAILED_DONT_REPORT
;
3210 return SPELL_FAILED_NOT_READY
;
3213 // only allow triggered spells if at an ended battleground
3214 if( !m_IsTriggeredSpell
&& m_caster
->GetTypeId() == TYPEID_PLAYER
)
3215 if(BattleGround
* bg
= ((Player
*)m_caster
)->GetBattleGround())
3216 if(bg
->GetStatus() == STATUS_WAIT_LEAVE
)
3217 return SPELL_FAILED_DONT_REPORT
;
3219 // only check at first call, Stealth auras are already removed at second call
3220 // for now, ignore triggered spells
3221 if( strict
&& !m_IsTriggeredSpell
)
3223 // Cannot be used in this stance/form
3224 if(uint8 shapeError
= GetErrorAtShapeshiftedCast(m_spellInfo
, m_caster
->m_form
))
3227 if ((m_spellInfo
->Attributes
& SPELL_ATTR_ONLY_STEALTHED
) && !(m_caster
->HasStealthAura()))
3228 return SPELL_FAILED_ONLY_STEALTHED
;
3231 // caster state requirements
3232 if(m_spellInfo
->CasterAuraState
&& !m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraState
)))
3233 return SPELL_FAILED_CASTER_AURASTATE
;
3234 if(m_spellInfo
->CasterAuraStateNot
&& m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraStateNot
)))
3235 return SPELL_FAILED_CASTER_AURASTATE
;
3237 // cancel autorepeat spells if cast start when moving
3238 // (not wand currently autorepeat cast delayed to moving stop anyway in spell update code)
3239 if( m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->isMoving() )
3241 // skip stuck spell to allow use it in falling case and apply spell limitations at movement
3242 if( (!m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_FALLING
) || m_spellInfo
->Effect
[0] != SPELL_EFFECT_STUCK
) &&
3243 (IsAutoRepeat() || (m_spellInfo
->AuraInterruptFlags
& AURA_INTERRUPT_FLAG_NOT_SEATED
) != 0) )
3244 return SPELL_FAILED_MOVING
;
3247 Unit
*target
= m_targets
.getUnitTarget();
3251 // target state requirements (not allowed state), apply to self also
3252 if(m_spellInfo
->TargetAuraStateNot
&& target
->HasAuraState(AuraState(m_spellInfo
->TargetAuraStateNot
)))
3253 return SPELL_FAILED_TARGET_AURASTATE
;
3255 if(target
!= m_caster
)
3257 // target state requirements (apply to non-self only), to allow cast affects to self like Dirty Deeds
3258 if(m_spellInfo
->TargetAuraState
&& !target
->HasAuraState(AuraState(m_spellInfo
->TargetAuraState
)))
3259 return SPELL_FAILED_TARGET_AURASTATE
;
3261 // Not allow casting on flying player
3262 if (target
->isInFlight())
3263 return SPELL_FAILED_BAD_TARGETS
;
3265 if(VMAP::VMapFactory::checkSpellForLoS(m_spellInfo
->Id
) && !m_caster
->IsWithinLOSInMap(target
))
3266 return SPELL_FAILED_LINE_OF_SIGHT
;
3268 // auto selection spell rank implemented in WorldSession::HandleCastSpellOpcode
3269 // this case can be triggered if rank not found (too low-level target for first rank)
3270 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsPassiveSpell(m_spellInfo
->Id
) && !m_CastItem
)
3272 for(int i
=0;i
<3;i
++)
3274 if(IsPositiveEffect(m_spellInfo
->Id
, i
) && m_spellInfo
->Effect
[i
] == SPELL_EFFECT_APPLY_AURA
)
3275 if(target
->getLevel() + 10 < m_spellInfo
->spellLevel
)
3276 return SPELL_FAILED_LOWLEVEL
;
3281 // check pet presents
3282 for(int j
=0;j
<3;j
++)
3284 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_PET
)
3286 target
= m_caster
->GetPet();
3289 if(m_triggeredByAuraSpell
) // not report pet not existence for triggered spells
3290 return SPELL_FAILED_DONT_REPORT
;
3292 return SPELL_FAILED_NO_PET
;
3298 //check creature type
3299 //ignore self casts (including area casts when caster selected as target)
3300 if(target
!= m_caster
)
3302 if(!CheckTargetCreatureType(target
))
3304 if(target
->GetTypeId()==TYPEID_PLAYER
)
3305 return SPELL_FAILED_TARGET_IS_PLAYER
;
3307 return SPELL_FAILED_BAD_TARGETS
;
3311 // TODO: this check can be applied and for player to prevent cheating when IsPositiveSpell will return always correct result.
3312 // check target for pet/charmed casts (not self targeted), self targeted cast used for area effects and etc
3313 if(m_caster
!= target
&& m_caster
->GetTypeId()==TYPEID_UNIT
&& m_caster
->GetCharmerOrOwnerGUID())
3315 // check correctness positive/negative cast target (pet cast real check and cheating check)
3316 if(IsPositiveSpell(m_spellInfo
->Id
))
3318 if(m_caster
->IsHostileTo(target
))
3319 return SPELL_FAILED_BAD_TARGETS
;
3323 if(m_caster
->IsFriendlyTo(target
))
3324 return SPELL_FAILED_BAD_TARGETS
;
3328 if(IsPositiveSpell(m_spellInfo
->Id
))
3330 if(target
->IsImmunedToSpell(m_spellInfo
,false))
3331 return SPELL_FAILED_TARGET_AURASTATE
;
3334 //Must be behind the target.
3335 if( m_spellInfo
->AttributesEx2
== 0x100000 && (m_spellInfo
->AttributesEx
& 0x200) == 0x200 && target
->HasInArc(M_PI
, m_caster
) )
3338 return SPELL_FAILED_NOT_BEHIND
;
3341 //Target must be facing you.
3342 if((m_spellInfo
->Attributes
== 0x150010) && !target
->HasInArc(M_PI
, m_caster
) )
3345 return SPELL_FAILED_NOT_INFRONT
;
3348 // check if target is in combat
3349 if (target
!= m_caster
&& (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_NOT_IN_COMBAT_TARGET
) && target
->isInCombat())
3351 return SPELL_FAILED_TARGET_AFFECTING_COMBAT
;
3354 // Spell casted only on battleground
3355 if((m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_BATTLEGROUND
) && m_caster
->GetTypeId()==TYPEID_PLAYER
)
3356 if(!((Player
*)m_caster
)->InBattleGround())
3357 return SPELL_FAILED_ONLY_BATTLEGROUNDS
;
3359 // do not allow spells to be cast in arenas
3360 // - with greater than 15 min CD without SPELL_ATTR_EX4_USABLE_IN_ARENA flag
3361 // - with SPELL_ATTR_EX4_NOT_USABLE_IN_ARENA flag
3362 if( (m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_NOT_USABLE_IN_ARENA
) ||
3363 GetSpellRecoveryTime(m_spellInfo
) > 15 * MINUTE
* 1000 && !(m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_USABLE_IN_ARENA
) )
3364 if(MapEntry
const* mapEntry
= sMapStore
.LookupEntry(m_caster
->GetMapId()))
3365 if(mapEntry
->IsBattleArena())
3366 return SPELL_FAILED_NOT_IN_ARENA
;
3369 if(!IsSpellAllowedInLocation(m_spellInfo
,m_caster
->GetMapId(),m_caster
->GetZoneId(),m_caster
->GetAreaId()))
3370 return SPELL_FAILED_REQUIRES_AREA
;
3372 // not let players cast spells at mount (and let do it to creatures)
3373 if( m_caster
->IsMounted() && m_caster
->GetTypeId()==TYPEID_PLAYER
&& !m_IsTriggeredSpell
&&
3374 !IsPassiveSpell(m_spellInfo
->Id
) && !(m_spellInfo
->Attributes
& SPELL_ATTR_CASTABLE_WHILE_MOUNTED
) )
3376 if(m_caster
->isInFlight())
3377 return SPELL_FAILED_NOT_FLYING
;
3379 return SPELL_FAILED_NOT_MOUNTED
;
3382 // always (except passive spells) check items (focus object can be required for any type casts)
3383 if(!IsPassiveSpell(m_spellInfo
->Id
))
3384 if(uint8 castResult
= CheckItems())
3387 //ImpliciteTargetA-B = 38, If fact there is 0 Spell with ImpliciteTargetB=38
3388 if(m_UniqueTargetInfo
.empty()) // skip second canCast apply (for delayed spells for example)
3390 for(uint8 j
= 0; j
< 3; j
++)
3392 if( m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT
||
3393 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT
&& m_spellInfo
->EffectImplicitTargetA
[j
] != TARGET_SELF
||
3394 m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3395 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3399 SpellScriptTarget::const_iterator lower
= spellmgr
.GetBeginSpellScriptTarget(m_spellInfo
->Id
);
3400 SpellScriptTarget::const_iterator upper
= spellmgr
.GetEndSpellScriptTarget(m_spellInfo
->Id
);
3402 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
);
3404 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
3405 float range
= GetSpellMaxRange(srange
);
3407 Creature
* creatureScriptTarget
= NULL
;
3408 GameObject
* goScriptTarget
= NULL
;
3410 for(SpellScriptTarget::const_iterator i_spellST
= lower
; i_spellST
!= upper
; ++i_spellST
)
3412 switch(i_spellST
->second
.type
)
3414 case SPELL_TARGET_TYPE_GAMEOBJECT
:
3416 GameObject
* p_GameObject
= NULL
;
3418 if(i_spellST
->second
.targetEntry
)
3420 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
3422 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
3424 MaNGOS::NearestGameObjectEntryInObjectRangeCheck
go_check(*m_caster
,i_spellST
->second
.targetEntry
,range
);
3425 MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
> checker(p_GameObject
,go_check
);
3427 TypeContainerVisitor
<MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
>, GridTypeMapContainer
> object_checker(checker
);
3428 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
3429 cell_lock
->Visit(cell_lock
, object_checker
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
3433 // remember found target and range, next attempt will find more near target with another entry
3434 creatureScriptTarget
= NULL
;
3435 goScriptTarget
= p_GameObject
;
3436 range
= go_check
.GetLastRange();
3439 else if( focusObject
) //Focus Object
3441 float frange
= m_caster
->GetDistance(focusObject
);
3444 creatureScriptTarget
= NULL
;
3445 goScriptTarget
= focusObject
;
3451 case SPELL_TARGET_TYPE_CREATURE
:
3452 case SPELL_TARGET_TYPE_DEAD
:
3455 Creature
*p_Creature
= NULL
;
3457 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
3459 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
3460 cell
.SetNoCreate(); // Really don't know what is that???
3462 MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
u_check(*m_caster
,i_spellST
->second
.targetEntry
,i_spellST
->second
.type
!=SPELL_TARGET_TYPE_DEAD
,range
);
3463 MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
> searcher(p_Creature
, u_check
);
3465 TypeContainerVisitor
<MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
>, GridTypeMapContainer
> grid_creature_searcher(searcher
);
3467 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
3468 cell_lock
->Visit(cell_lock
, grid_creature_searcher
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
3472 creatureScriptTarget
= p_Creature
;
3473 goScriptTarget
= NULL
;
3474 range
= u_check
.GetLastRange();
3481 if(creatureScriptTarget
)
3483 // store coordinates for TARGET_SCRIPT_COORDINATES
3484 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3485 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3487 m_targets
.setDestination(creatureScriptTarget
->GetPositionX(),creatureScriptTarget
->GetPositionY(),creatureScriptTarget
->GetPositionZ());
3489 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->EffectImplicitTargetB
[j
] == 0 && m_spellInfo
->Effect
[j
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
3490 AddUnitTarget(creatureScriptTarget
, j
);
3492 // store explicit target for TARGET_SCRIPT
3494 AddUnitTarget(creatureScriptTarget
, j
);
3496 else if(goScriptTarget
)
3498 // store coordinates for TARGET_SCRIPT_COORDINATES
3499 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3500 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3502 m_targets
.setDestination(goScriptTarget
->GetPositionX(),goScriptTarget
->GetPositionY(),goScriptTarget
->GetPositionZ());
3504 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->EffectImplicitTargetB
[j
] == 0 && m_spellInfo
->Effect
[j
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
3505 AddGOTarget(goScriptTarget
, j
);
3507 // store explicit target for TARGET_SCRIPT
3509 AddGOTarget(goScriptTarget
, j
);
3511 //Missing DB Entry or targets for this spellEffect.
3514 // not report target not existence for triggered spells
3515 if(m_triggeredByAuraSpell
|| m_IsTriggeredSpell
)
3516 return SPELL_FAILED_DONT_REPORT
;
3518 return SPELL_FAILED_BAD_TARGETS
;
3524 if(uint8 castResult
= CheckRange(strict
))
3528 if(uint8 castResult
= CheckPower())
3532 if(!m_triggeredByAuraSpell
) // triggered spell not affected by stun/etc
3533 if(uint8 castResult
= CheckCasterAuras())
3536 for (int i
= 0; i
< 3; i
++)
3538 // for effects of spells that have only one target
3539 switch(m_spellInfo
->Effect
[i
])
3541 case SPELL_EFFECT_DUMMY
:
3543 if(m_spellInfo
->SpellIconID
== 1648) // Execute
3545 if(!m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
3546 return SPELL_FAILED_BAD_TARGETS
;
3548 else if (m_spellInfo
->Id
== 51582) // Rocket Boots Engaged
3550 if(m_caster
->IsInWater())
3551 return SPELL_FAILED_ONLY_ABOVEWATER
;
3553 else if(m_spellInfo
->SpellIconID
==156) // Holy Shock
3555 // spell different for friends and enemies
3556 // hart version required facing
3557 if(m_targets
.getUnitTarget() && !m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()) && !m_caster
->HasInArc( M_PI
, target
))
3558 return SPELL_FAILED_UNIT_NOT_INFRONT
;
3562 case SPELL_EFFECT_SCHOOL_DAMAGE
:
3565 if(m_spellInfo
->SpellVisual
[0] == 7250)
3567 if (!m_targets
.getUnitTarget())
3568 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3570 if(m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
3571 return SPELL_FAILED_BAD_TARGETS
;
3575 case SPELL_EFFECT_TAMECREATURE
:
3577 if (!m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetTypeId() == TYPEID_PLAYER
)
3578 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3580 if (m_targets
.getUnitTarget()->getLevel() > m_caster
->getLevel())
3581 return SPELL_FAILED_HIGHLEVEL
;
3583 // use SMSG_PET_TAME_FAILURE?
3584 if (!((Creature
*)m_targets
.getUnitTarget())->GetCreatureInfo()->isTameable ())
3585 return SPELL_FAILED_BAD_TARGETS
;
3587 if(m_caster
->GetPetGUID())
3588 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3590 if(m_caster
->GetCharmGUID())
3591 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3595 case SPELL_EFFECT_LEARN_SPELL
:
3597 if(m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_PET
)
3600 Pet
* pet
= m_caster
->GetPet();
3603 return SPELL_FAILED_NO_PET
;
3605 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
3607 if(!learn_spellproto
)
3608 return SPELL_FAILED_NOT_KNOWN
;
3610 if(m_spellInfo
->spellLevel
> pet
->getLevel())
3611 return SPELL_FAILED_LOWLEVEL
;
3615 case SPELL_EFFECT_LEARN_PET_SPELL
:
3617 Pet
* pet
= m_caster
->GetPet();
3620 return SPELL_FAILED_NO_PET
;
3622 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
3624 if(!learn_spellproto
)
3625 return SPELL_FAILED_NOT_KNOWN
;
3627 if(m_spellInfo
->spellLevel
> pet
->getLevel())
3628 return SPELL_FAILED_LOWLEVEL
;
3632 case SPELL_EFFECT_FEED_PET
:
3634 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| !m_targets
.getItemTarget() )
3635 return SPELL_FAILED_BAD_TARGETS
;
3637 Pet
* pet
= m_caster
->GetPet();
3640 return SPELL_FAILED_NO_PET
;
3642 if(!pet
->HaveInDiet(m_targets
.getItemTarget()->GetProto()))
3643 return SPELL_FAILED_WRONG_PET_FOOD
;
3645 if(!pet
->GetCurrentFoodBenefitLevel(m_targets
.getItemTarget()->GetProto()->ItemLevel
))
3646 return SPELL_FAILED_FOOD_LOWLEVEL
;
3648 if(m_caster
->isInCombat() || pet
->isInCombat())
3649 return SPELL_FAILED_AFFECTING_COMBAT
;
3653 case SPELL_EFFECT_POWER_BURN
:
3654 case SPELL_EFFECT_POWER_DRAIN
:
3656 // Can be area effect, Check only for players and not check if target - caster (spell can have multiply drain/burn effects)
3657 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
3658 if(Unit
* target
= m_targets
.getUnitTarget())
3659 if(target
!=m_caster
&& target
->getPowerType()!=m_spellInfo
->EffectMiscValue
[i
])
3660 return SPELL_FAILED_BAD_TARGETS
;
3663 case SPELL_EFFECT_CHARGE
:
3665 if (m_caster
->hasUnitState(UNIT_STAT_ROOT
))
3666 return SPELL_FAILED_ROOTED
;
3670 case SPELL_EFFECT_SKINNING
:
3672 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| !m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetTypeId() != TYPEID_UNIT
)
3673 return SPELL_FAILED_BAD_TARGETS
;
3675 if( !(m_targets
.getUnitTarget()->GetUInt32Value(UNIT_FIELD_FLAGS
) & UNIT_FLAG_SKINNABLE
) )
3676 return SPELL_FAILED_TARGET_UNSKINNABLE
;
3678 Creature
* creature
= (Creature
*)m_targets
.getUnitTarget();
3679 if ( creature
->GetCreatureType() != CREATURE_TYPE_CRITTER
&& ( !creature
->lootForBody
|| !creature
->loot
.empty() ) )
3681 return SPELL_FAILED_TARGET_NOT_LOOTED
;
3684 uint32 skill
= creature
->GetCreatureInfo()->GetRequiredLootSkill();
3686 int32 skillValue
= ((Player
*)m_caster
)->GetSkillValue(skill
);
3687 int32 TargetLevel
= m_targets
.getUnitTarget()->getLevel();
3688 int32 ReqValue
= (skillValue
< 100 ? (TargetLevel
-10)*10 : TargetLevel
*5);
3689 if (ReqValue
> skillValue
)
3690 return SPELL_FAILED_LOW_CASTLEVEL
;
3692 // chance for fail at orange skinning attempt
3693 if( (m_selfContainer
&& (*m_selfContainer
) == this) &&
3694 skillValue
< sWorld
.GetConfigMaxSkillValue() &&
3695 (ReqValue
< 0 ? 0 : ReqValue
) > irand(skillValue
-25, skillValue
+37) )
3696 return SPELL_FAILED_TRY_AGAIN
;
3700 case SPELL_EFFECT_OPEN_LOCK_ITEM
:
3701 case SPELL_EFFECT_OPEN_LOCK
:
3703 if( m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT
&&
3704 m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT_ITEM
)
3707 if( m_caster
->GetTypeId() != TYPEID_PLAYER
// only players can open locks, gather etc.
3708 // we need a go target in case of TARGET_GAMEOBJECT
3709 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT
&& !m_targets
.getGOTarget()
3710 // we need a go target, or an openable item target in case of TARGET_GAMEOBJECT_ITEM
3711 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT_ITEM
&& !m_targets
.getGOTarget() &&
3712 (!m_targets
.getItemTarget() || !m_targets
.getItemTarget()->GetProto()->LockID
|| m_targets
.getItemTarget()->GetOwner() != m_caster
) )
3713 return SPELL_FAILED_BAD_TARGETS
;
3715 // In BattleGround players can use only flags and banners
3716 if( ((Player
*)m_caster
)->InBattleGround() &&
3717 !((Player
*)m_caster
)->isAllowUseBattleGroundObject() )
3718 return SPELL_FAILED_TRY_AGAIN
;
3720 // get the lock entry
3721 LockEntry
const *lockInfo
= NULL
;
3722 if (GameObject
* go
=m_targets
.getGOTarget())
3723 lockInfo
= sLockStore
.LookupEntry(go
->GetLockId());
3724 else if(Item
* itm
=m_targets
.getItemTarget())
3725 lockInfo
= sLockStore
.LookupEntry(itm
->GetProto()->LockID
);
3727 // check lock compatibility
3730 // check for lock - key pair (checked by client also, just prevent cheating
3731 bool ok_key
= false;
3732 for(int it
= 0; it
< 8; ++it
)
3734 switch(lockInfo
->Type
[it
])
3740 if(lockInfo
->Index
[it
])
3742 if(m_CastItem
&& m_CastItem
->GetEntry()==lockInfo
->Index
[it
])
3747 case LOCK_KEY_SKILL
:
3749 if(uint32(m_spellInfo
->EffectMiscValue
[i
])!=lockInfo
->Index
[it
])
3752 switch(lockInfo
->Index
[it
])
3754 case LOCKTYPE_HERBALISM
:
3755 if(((Player
*)m_caster
)->HasSkill(SKILL_HERBALISM
))
3758 case LOCKTYPE_MINING
:
3759 if(((Player
*)m_caster
)->HasSkill(SKILL_MINING
))
3773 return SPELL_FAILED_BAD_TARGETS
;
3776 // chance for fail at orange mining/herb/LockPicking gathering attempt
3777 if (!m_selfContainer
|| ((*m_selfContainer
) != this))
3780 // get the skill value of the player
3781 int32 SkillValue
= 0;
3782 bool canFailAtMax
= true;
3783 if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_HERBALISM
)
3785 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_HERBALISM
);
3786 canFailAtMax
= false;
3788 else if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_MINING
)
3790 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_MINING
);
3791 canFailAtMax
= false;
3793 else if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_PICKLOCK
)
3794 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_LOCKPICKING
);
3796 // castitem check: rogue using skeleton keys. the skill values should not be added in this case.
3800 // add the damage modifier from the spell casted (cheat lock / skeleton key etc.) (use m_currentBasePoints, CalculateDamage returns wrong value)
3801 SkillValue
+= m_currentBasePoints
[i
]+1;
3803 // get the required lock value
3807 // check for lock - key pair
3809 for(int it
= 0; it
< 8; ++it
)
3811 if(lockInfo
->Type
[it
]==LOCK_KEY_ITEM
&& lockInfo
->Index
[it
] && m_CastItem
&& m_CastItem
->GetEntry()==lockInfo
->Index
[it
])
3813 // if so, we're good to go
3821 if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_PICKLOCK
)
3822 ReqValue
= lockInfo
->Skill
[1];
3824 ReqValue
= lockInfo
->Skill
[0];
3827 // skill doesn't meet the required value
3828 if (ReqValue
> SkillValue
)
3829 return SPELL_FAILED_LOW_CASTLEVEL
;
3831 // chance for failure in orange gather / lockpick (gathering skill can't fail at maxskill)
3832 if((canFailAtMax
|| SkillValue
< sWorld
.GetConfigMaxSkillValue()) && ReqValue
> irand(SkillValue
-25, SkillValue
+37))
3833 return SPELL_FAILED_TRY_AGAIN
;
3837 case SPELL_EFFECT_SUMMON_DEAD_PET
:
3839 Creature
*pet
= m_caster
->GetPet();
3841 return SPELL_FAILED_NO_PET
;
3844 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3848 // This is generic summon effect now and don't make this check for summon types similar
3849 // SPELL_EFFECT_SUMMON_CRITTER, SPELL_EFFECT_SUMMON_WILD or SPELL_EFFECT_SUMMON_GUARDIAN.
3850 // These won't show up in m_caster->GetPetGUID()
3851 case SPELL_EFFECT_SUMMON
:
3853 switch(m_spellInfo
->EffectMiscValueB
[i
])
3855 case SUMMON_TYPE_POSESSED
:
3856 case SUMMON_TYPE_POSESSED2
:
3857 case SUMMON_TYPE_DEMON
:
3858 case SUMMON_TYPE_SUMMON
:
3860 if(m_caster
->GetPetGUID())
3861 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3863 if(m_caster
->GetCharmGUID())
3864 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3870 // Don't make this check for SPELL_EFFECT_SUMMON_CRITTER, SPELL_EFFECT_SUMMON_WILD or SPELL_EFFECT_SUMMON_GUARDIAN.
3871 // These won't show up in m_caster->GetPetGUID()
3872 case SPELL_EFFECT_SUMMON_POSSESSED
:
3873 case SPELL_EFFECT_SUMMON_PHANTASM
:
3874 case SPELL_EFFECT_SUMMON_DEMON
:
3876 if(m_caster
->GetPetGUID())
3877 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3879 if(m_caster
->GetCharmGUID())
3880 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3884 case SPELL_EFFECT_SUMMON_PET
:
3886 if(m_caster
->GetPetGUID()) //let warlock do a replacement summon
3889 Pet
* pet
= ((Player
*)m_caster
)->GetPet();
3891 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& m_caster
->getClass()==CLASS_WARLOCK
)
3893 if (strict
) //starting cast, trigger pet stun (cast by pet so it doesn't attack player)
3894 pet
->CastSpell(pet
, 32752, true, NULL
, NULL
, pet
->GetGUID());
3897 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3900 if(m_caster
->GetCharmGUID())
3901 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3905 case SPELL_EFFECT_SUMMON_PLAYER
:
3907 if(m_caster
->GetTypeId()!=TYPEID_PLAYER
)
3908 return SPELL_FAILED_BAD_TARGETS
;
3909 if(!((Player
*)m_caster
)->GetSelection())
3910 return SPELL_FAILED_BAD_TARGETS
;
3912 Player
* target
= objmgr
.GetPlayer(((Player
*)m_caster
)->GetSelection());
3913 if( !target
|| ((Player
*)m_caster
)==target
|| !target
->IsInSameRaidWith((Player
*)m_caster
) )
3914 return SPELL_FAILED_BAD_TARGETS
;
3916 // check if our map is dungeon
3917 if( sMapStore
.LookupEntry(m_caster
->GetMapId())->IsDungeon() )
3919 InstanceTemplate
const* instance
= ObjectMgr::GetInstanceTemplate(m_caster
->GetMapId());
3921 return SPELL_FAILED_TARGET_NOT_IN_INSTANCE
;
3922 if ( instance
->levelMin
> target
->getLevel() )
3923 return SPELL_FAILED_LOWLEVEL
;
3924 if ( instance
->levelMax
&& instance
->levelMax
< target
->getLevel() )
3925 return SPELL_FAILED_HIGHLEVEL
;
3929 case SPELL_EFFECT_LEAP
:
3930 case SPELL_EFFECT_TELEPORT_UNITS_FACE_CASTER
:
3932 float dis
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[i
]));
3933 float fx
= m_caster
->GetPositionX() + dis
* cos(m_caster
->GetOrientation());
3934 float fy
= m_caster
->GetPositionY() + dis
* sin(m_caster
->GetOrientation());
3935 // teleport a bit above terrain level to avoid falling below it
3936 float fz
= MapManager::Instance().GetBaseMap(m_caster
->GetMapId())->GetHeight(fx
,fy
,m_caster
->GetPositionZ(),true);
3937 if(fz
<= INVALID_HEIGHT
) // note: this also will prevent use effect in instances without vmaps height enabled
3938 return SPELL_FAILED_TRY_AGAIN
;
3940 float caster_pos_z
= m_caster
->GetPositionZ();
3941 // Control the caster to not climb or drop when +-fz > 8
3942 if(!(fz
<=caster_pos_z
+8 && fz
>=caster_pos_z
-8))
3943 return SPELL_FAILED_TRY_AGAIN
;
3945 // not allow use this effect at battleground until battleground start
3946 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
3947 if(BattleGround
const *bg
= ((Player
*)m_caster
)->GetBattleGround())
3948 if(bg
->GetStatus() != STATUS_IN_PROGRESS
)
3949 return SPELL_FAILED_TRY_AGAIN
;
3952 case SPELL_EFFECT_STEAL_BENEFICIAL_BUFF
:
3954 if (m_targets
.getUnitTarget()==m_caster
)
3955 return SPELL_FAILED_BAD_TARGETS
;
3962 for (int i
= 0; i
< 3; i
++)
3964 switch(m_spellInfo
->EffectApplyAuraName
[i
])
3966 case SPELL_AURA_MOD_POSSESS
:
3967 case SPELL_AURA_MOD_CHARM
:
3969 if(m_caster
->GetPetGUID())
3970 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3972 if(m_caster
->GetCharmGUID())
3973 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3975 if(m_caster
->GetCharmerGUID())
3976 return SPELL_FAILED_CHARMED
;
3978 if(!m_targets
.getUnitTarget())
3979 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3981 if(m_targets
.getUnitTarget()->GetCharmerGUID())
3982 return SPELL_FAILED_CHARMED
;
3984 if(int32(m_targets
.getUnitTarget()->getLevel()) > CalculateDamage(i
,m_targets
.getUnitTarget()))
3985 return SPELL_FAILED_HIGHLEVEL
;
3987 case SPELL_AURA_MOUNTED
:
3989 if (m_caster
->IsInWater())
3990 return SPELL_FAILED_ONLY_ABOVEWATER
;
3992 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->GetTransport())
3993 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
3995 // Ignore map check if spell have AreaId. AreaId already checked and this prevent special mount spells
3996 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& !sMapStore
.LookupEntry(m_caster
->GetMapId())->IsMountAllowed() && !m_IsTriggeredSpell
&& !m_spellInfo
->AreaId
)
3997 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
3999 if (m_caster
->GetAreaId()==35)
4000 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4002 ShapeshiftForm form
= m_caster
->m_form
;
4003 if( form
== FORM_CAT
|| form
== FORM_TREE
|| form
== FORM_TRAVEL
||
4004 form
== FORM_AQUA
|| form
== FORM_BEAR
|| form
== FORM_DIREBEAR
||
4005 form
== FORM_CREATUREBEAR
|| form
== FORM_GHOSTWOLF
|| form
== FORM_FLIGHT
||
4006 form
== FORM_FLIGHT_EPIC
|| form
== FORM_MOONKIN
)
4007 return SPELL_FAILED_NOT_SHAPESHIFT
;
4011 case SPELL_AURA_RANGED_ATTACK_POWER_ATTACKER_BONUS
:
4013 if(!m_targets
.getUnitTarget())
4014 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4016 // can be casted at non-friendly unit or own pet/charm
4017 if(m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()))
4018 return SPELL_FAILED_TARGET_FRIENDLY
;
4020 case SPELL_AURA_MOD_INCREASE_FLIGHT_SPEED
:
4021 case SPELL_AURA_FLY
:
4023 // not allow cast fly spells at old maps by players (all spells is self target)
4024 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
4026 if( !((Player
*)m_caster
)->isGameMaster() &&
4027 GetVirtualMapForMapAndZone(m_caster
->GetMapId(),m_caster
->GetZoneId()) != 530)
4028 return SPELL_FAILED_NOT_HERE
;
4031 case SPELL_AURA_PERIODIC_MANA_LEECH
:
4033 if (!m_targets
.getUnitTarget())
4034 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4036 if (m_caster
->GetTypeId()!=TYPEID_PLAYER
|| m_CastItem
)
4039 if(m_targets
.getUnitTarget()->getPowerType()!=POWER_MANA
)
4040 return SPELL_FAILED_BAD_TARGETS
;
4051 int16
Spell::PetCanCast(Unit
* target
)
4053 if(!m_caster
->isAlive())
4054 return SPELL_FAILED_CASTER_DEAD
;
4056 if(m_caster
->IsNonMeleeSpellCasted(false)) //prevent spellcast interuption by another spellcast
4057 return SPELL_FAILED_SPELL_IN_PROGRESS
;
4058 if(m_caster
->isInCombat() && IsNonCombatSpell(m_spellInfo
))
4059 return SPELL_FAILED_AFFECTING_COMBAT
;
4061 if(m_caster
->GetTypeId()==TYPEID_UNIT
&& (((Creature
*)m_caster
)->isPet() || m_caster
->isCharmed()))
4063 //dead owner (pets still alive when owners ressed?)
4064 if(m_caster
->GetCharmerOrOwner() && !m_caster
->GetCharmerOrOwner()->isAlive())
4065 return SPELL_FAILED_CASTER_DEAD
;
4067 if(!target
&& m_targets
.getUnitTarget())
4068 target
= m_targets
.getUnitTarget();
4071 for(uint32 i
= 0;i
<3;i
++)
4073 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
)
4077 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4082 m_targets
.setUnitTarget(target
);
4084 Unit
* _target
= m_targets
.getUnitTarget();
4086 if(_target
) //for target dead/target not valid
4088 if(!_target
->isAlive())
4089 return SPELL_FAILED_BAD_TARGETS
;
4091 if(IsPositiveSpell(m_spellInfo
->Id
))
4093 if(m_caster
->IsHostileTo(_target
))
4094 return SPELL_FAILED_BAD_TARGETS
;
4098 bool duelvsplayertar
= false;
4099 for(int j
=0;j
<3;j
++)
4101 //TARGET_DUELVSPLAYER is positive AND negative
4102 duelvsplayertar
|= (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_DUELVSPLAYER
);
4104 if(m_caster
->IsFriendlyTo(target
) && !duelvsplayertar
)
4106 return SPELL_FAILED_BAD_TARGETS
;
4111 if(((Creature
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
4112 return SPELL_FAILED_NOT_READY
;
4115 uint16 result
= CanCast(true);
4119 return -1; //this allows to check spell fail 0, in combat
4122 uint8
Spell::CheckCasterAuras() const
4124 // Flag drop spells totally immuned to caster auras
4125 // FIXME: find more nice check for all totally immuned spells
4126 // AttributesEx3 & 0x10000000?
4127 if(m_spellInfo
->Id
==23336 || m_spellInfo
->Id
==23334 || m_spellInfo
->Id
==34991)
4130 uint8 school_immune
= 0;
4131 uint32 mechanic_immune
= 0;
4132 uint32 dispel_immune
= 0;
4134 //Check if the spell grants school or mechanic immunity.
4135 //We use bitmasks so the loop is done only once and not on every aura check below.
4136 if ( m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DISPEL_AURAS_ON_IMMUNITY
)
4138 for(int i
= 0;i
< 3; i
++)
4140 if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_SCHOOL_IMMUNITY
)
4141 school_immune
|= uint32(m_spellInfo
->EffectMiscValue
[i
]);
4142 else if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_MECHANIC_IMMUNITY
)
4143 mechanic_immune
|= 1 << uint32(m_spellInfo
->EffectMiscValue
[i
]);
4144 else if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_DISPEL_IMMUNITY
)
4145 dispel_immune
|= GetDispellMask(DispelType(m_spellInfo
->EffectMiscValue
[i
]));
4147 //immune movement impairement and loss of control
4148 if(m_spellInfo
->Id
==(uint32
)42292)
4149 mechanic_immune
= IMMUNE_TO_MOVEMENT_IMPAIRMENT_AND_LOSS_CONTROL_MASK
;
4152 //Check whether the cast should be prevented by any state you might have.
4153 uint8 prevented_reason
= 0;
4154 // Have to check if there is a stun aura. Otherwise will have problems with ghost aura apply while logging out
4155 if(!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
) && m_caster
->HasAuraType(SPELL_AURA_MOD_STUN
))
4156 prevented_reason
= SPELL_FAILED_STUNNED
;
4157 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_CONFUSED
) && !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
4158 prevented_reason
= SPELL_FAILED_CONFUSED
;
4159 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_FLEEING
) && !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
4160 prevented_reason
= SPELL_FAILED_FLEEING
;
4161 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_SILENCED
) && m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_SILENCE
)
4162 prevented_reason
= SPELL_FAILED_SILENCED
;
4163 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PACIFIED
) && m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_PACIFY
)
4164 prevented_reason
= SPELL_FAILED_PACIFIED
;
4166 // Attr must make flag drop spell totally immuned from all effects
4167 if(prevented_reason
)
4169 if(school_immune
|| mechanic_immune
|| dispel_immune
)
4171 //Checking auras is needed now, because you are prevented by some state but the spell grants immunity.
4172 Unit::AuraMap
const& auras
= m_caster
->GetAuras();
4173 for(Unit::AuraMap::const_iterator itr
= auras
.begin(); itr
!= auras
.end(); itr
++)
4177 if( GetSpellMechanicMask(itr
->second
->GetSpellProto(), itr
->second
->GetEffIndex()) & mechanic_immune
)
4179 if( GetSpellSchoolMask(itr
->second
->GetSpellProto()) & school_immune
)
4181 if( (1<<(itr
->second
->GetSpellProto()->Dispel
)) & dispel_immune
)
4184 //Make a second check for spell failed so the right SPELL_FAILED message is returned.
4185 //That is needed when your casting is prevented by multiple states and you are only immune to some of them.
4186 switch(itr
->second
->GetModifier()->m_auraname
)
4188 case SPELL_AURA_MOD_STUN
:
4189 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
))
4190 return SPELL_FAILED_STUNNED
;
4192 case SPELL_AURA_MOD_CONFUSE
:
4193 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
4194 return SPELL_FAILED_CONFUSED
;
4196 case SPELL_AURA_MOD_FEAR
:
4197 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
4198 return SPELL_FAILED_FLEEING
;
4200 case SPELL_AURA_MOD_SILENCE
:
4201 case SPELL_AURA_MOD_PACIFY
:
4202 case SPELL_AURA_MOD_PACIFY_SILENCE
:
4203 if( m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_PACIFY
)
4204 return SPELL_FAILED_PACIFIED
;
4205 else if ( m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_SILENCE
)
4206 return SPELL_FAILED_SILENCED
;
4212 //You are prevented from casting and the spell casted does not grant immunity. Return a failed error.
4214 return prevented_reason
;
4219 bool Spell::CanAutoCast(Unit
* target
)
4221 uint64 targetguid
= target
->GetGUID();
4223 for(uint32 j
= 0;j
<3;j
++)
4225 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_APPLY_AURA
)
4227 if( m_spellInfo
->StackAmount
<= 1)
4229 if( target
->HasAura(m_spellInfo
->Id
, j
) )
4234 if( target
->GetAuras().count(Unit::spellEffectPair(m_spellInfo
->Id
, j
)) >= m_spellInfo
->StackAmount
)
4238 else if ( IsAreaAuraEffect( m_spellInfo
->Effect
[j
] ))
4240 if( target
->HasAura(m_spellInfo
->Id
, j
) )
4245 int16 result
= PetCanCast(target
);
4247 if(result
== -1 || result
== SPELL_FAILED_UNIT_NOT_INFRONT
)
4250 //check if among target units, our WANTED target is as well (->only self cast spells return false)
4251 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
4252 if( ihit
->targetGUID
== targetguid
)
4255 return false; //target invalid
4258 uint8
Spell::CheckRange(bool strict
)
4262 // self cast doesn't need range checking -- also for Starshards fix
4263 if (m_spellInfo
->rangeIndex
== 1) return 0;
4265 if (strict
) //add radius of caster
4267 else //add radius of caster and ~5 yds "give"
4270 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
4271 float max_range
= GetSpellMaxRange(srange
) + range_mod
;
4272 float min_range
= GetSpellMinRange(srange
);
4274 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
4275 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_RANGE
, max_range
, this);
4277 Unit
*target
= m_targets
.getUnitTarget();
4279 if(target
&& target
!= m_caster
)
4281 // distance from target center in checks
4282 float dist
= m_caster
->GetDistance(target
->GetPositionX(),target
->GetPositionY(),target
->GetPositionZ());
4283 if(dist
> max_range
)
4284 return SPELL_FAILED_OUT_OF_RANGE
; //0x5A;
4285 if(dist
< min_range
)
4286 return SPELL_FAILED_TOO_CLOSE
;
4287 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&&
4288 (m_spellInfo
->FacingCasterFlags
& SPELL_FACING_FLAG_INFRONT
) && !m_caster
->HasInArc( M_PI
, target
) )
4289 return SPELL_FAILED_UNIT_NOT_INFRONT
;
4292 if(m_targets
.m_targetMask
== TARGET_FLAG_DEST_LOCATION
&& m_targets
.m_destX
!= 0 && m_targets
.m_destY
!= 0 && m_targets
.m_destZ
!= 0)
4294 float dist
= m_caster
->GetDistance(m_targets
.m_destX
, m_targets
.m_destY
, m_targets
.m_destZ
);
4295 if(dist
> max_range
)
4296 return SPELL_FAILED_OUT_OF_RANGE
;
4297 if(dist
< min_range
)
4298 return SPELL_FAILED_TOO_CLOSE
;
4304 int32
Spell::CalculatePowerCost()
4306 // item cast not used power
4310 // Spell drain all exist power on cast (Only paladin lay of Hands)
4311 if (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DRAIN_ALL_POWER
)
4313 // If power type - health drain all
4314 if (m_spellInfo
->powerType
== POWER_HEALTH
)
4315 return m_caster
->GetHealth();
4316 // Else drain all power
4317 if (m_spellInfo
->powerType
< MAX_POWERS
)
4318 return m_caster
->GetPower(Powers(m_spellInfo
->powerType
));
4319 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
4324 int32 powerCost
= m_spellInfo
->manaCost
;
4325 // PCT cost from total amount
4326 if (m_spellInfo
->ManaCostPercentage
)
4328 switch (m_spellInfo
->powerType
)
4330 // health as power used
4332 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateHealth() / 100;
4335 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateMana() / 100;
4340 case POWER_HAPPINESS
:
4341 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetMaxPower(Powers(m_spellInfo
->powerType
)) / 100;
4344 case POWER_RUNIC_POWER
:
4345 sLog
.outDebug("Spell::CalculateManaCost: Not implemented yet!");
4348 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
4352 SpellSchools school
= GetFirstSchoolInMask(m_spellSchoolMask
);
4353 // Flat mod from caster auras by spell school
4354 powerCost
+= m_caster
->GetInt32Value(UNIT_FIELD_POWER_COST_MODIFIER
+ school
);
4355 // Shiv - costs 20 + weaponSpeed*10 energy (apply only to non-triggered spell with energy cost)
4356 if ( m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_SPELL_VS_EXTEND_COST
)
4357 powerCost
+= m_caster
->GetAttackTime(OFF_ATTACK
)/100;
4358 // Apply cost mod by spell
4359 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
4360 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COST
, powerCost
, this);
4362 if(m_spellInfo
->Attributes
& SPELL_ATTR_LEVEL_DAMAGE_CALCULATION
)
4363 powerCost
= int32(powerCost
/ (1.117f
* m_spellInfo
->spellLevel
/ m_caster
->getLevel() -0.1327f
));
4365 // PCT mod from user auras by school
4366 powerCost
= int32(powerCost
* (1.0f
+m_caster
->GetFloatValue(UNIT_FIELD_POWER_COST_MULTIPLIER
+school
)));
4372 uint8
Spell::CheckPower()
4374 // item cast not used power
4378 // health as power used - need check health amount
4379 if(m_spellInfo
->powerType
== POWER_HEALTH
)
4381 if(m_caster
->GetHealth() <= m_powerCost
)
4382 return SPELL_FAILED_CASTER_AURASTATE
;
4385 // Check valid power type
4386 if( m_spellInfo
->powerType
>= MAX_POWERS
)
4388 sLog
.outError("Spell::CheckMana: Unknown power type '%d'", m_spellInfo
->powerType
);
4389 return SPELL_FAILED_UNKNOWN
;
4391 // Check power amount
4392 Powers powerType
= Powers(m_spellInfo
->powerType
);
4393 if(m_caster
->GetPower(powerType
) < m_powerCost
)
4394 return SPELL_FAILED_NO_POWER
;
4399 uint8
Spell::CheckItems()
4401 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
4404 uint32 itemid
, itemcount
;
4405 Player
* p_caster
= (Player
*)m_caster
;
4409 itemid
= m_CastItem
->GetEntry();
4410 if( !p_caster
->HasItemCount(itemid
,1) )
4411 return SPELL_FAILED_ITEM_NOT_READY
;
4414 ItemPrototype
const *proto
= m_CastItem
->GetProto();
4416 return SPELL_FAILED_ITEM_NOT_READY
;
4418 for (int i
= 0; i
<5; i
++)
4420 if (proto
->Spells
[i
].SpellCharges
)
4422 if(m_CastItem
->GetSpellCharges(i
)==0)
4423 return SPELL_FAILED_NO_CHARGES_REMAIN
;
4427 uint32 ItemClass
= proto
->Class
;
4428 if (ItemClass
== ITEM_CLASS_CONSUMABLE
&& m_targets
.getUnitTarget())
4430 for (int i
= 0; i
< 3; i
++)
4432 // skip check, pet not required like checks, and for TARGET_PET m_targets.getUnitTarget() is not the real target but the caster
4433 if (m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_PET
)
4436 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_HEAL
)
4437 if (m_targets
.getUnitTarget()->GetHealth() == m_targets
.getUnitTarget()->GetMaxHealth())
4438 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_HEALTH
;
4440 // Mana Potion, Rage Potion, Thistle Tea(Rogue), ...
4441 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_ENERGIZE
)
4443 if(m_spellInfo
->EffectMiscValue
[i
] < 0 || m_spellInfo
->EffectMiscValue
[i
] >= MAX_POWERS
)
4444 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_POWER
;
4446 Powers power
= Powers(m_spellInfo
->EffectMiscValue
[i
]);
4448 if (m_targets
.getUnitTarget()->GetPower(power
) == m_targets
.getUnitTarget()->GetMaxPower(power
))
4449 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_POWER
;
4456 if(m_targets
.getItemTargetGUID())
4458 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
4459 return SPELL_FAILED_BAD_TARGETS
;
4461 if(!m_targets
.getItemTarget())
4462 return SPELL_FAILED_ITEM_GONE
;
4464 if(!m_targets
.getItemTarget()->IsFitToSpellRequirements(m_spellInfo
))
4465 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
4467 // if not item target then required item must be equipped
4470 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& !((Player
*)m_caster
)->HasItemFitToSpellReqirements(m_spellInfo
))
4471 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
4474 if(m_spellInfo
->RequiresSpellFocus
)
4476 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
4478 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
4480 GameObject
* ok
= NULL
;
4481 MaNGOS::GameObjectFocusCheck
go_check(m_caster
,m_spellInfo
->RequiresSpellFocus
);
4482 MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
> checker(ok
,go_check
);
4484 TypeContainerVisitor
<MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
>, GridTypeMapContainer
> object_checker(checker
);
4485 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
4486 cell_lock
->Visit(cell_lock
, object_checker
, *MapManager::Instance().GetMap(m_caster
->GetMapId(), m_caster
));
4489 return (uint8
)SPELL_FAILED_REQUIRES_SPELL_FOCUS
;
4491 focusObject
= ok
; // game object found in range
4494 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_NO_REAGENT_WHILE_PREP
&&
4495 m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PREPARATION
)))
4497 for(uint32 i
=0;i
<8;i
++)
4499 if(m_spellInfo
->Reagent
[i
] <= 0)
4502 itemid
= m_spellInfo
->Reagent
[i
];
4503 itemcount
= m_spellInfo
->ReagentCount
[i
];
4505 // if CastItem is also spell reagent
4506 if( m_CastItem
&& m_CastItem
->GetEntry() == itemid
)
4508 ItemPrototype
const *proto
= m_CastItem
->GetProto();
4510 return SPELL_FAILED_ITEM_NOT_READY
;
4511 for(int s
=0;s
<5;s
++)
4513 // CastItem will be used up and does not count as reagent
4514 int32 charges
= m_CastItem
->GetSpellCharges(s
);
4515 if (proto
->Spells
[s
].SpellCharges
< 0 && abs(charges
) < 2)
4522 if( !p_caster
->HasItemCount(itemid
,itemcount
) )
4523 return (uint8
)SPELL_FAILED_ITEM_NOT_READY
; //0x54
4528 for(int i
=0;i
<2;++i
)
4530 if(m_spellInfo
->Totem
[i
] != 0)
4532 if( p_caster
->HasItemCount(m_spellInfo
->Totem
[i
],1) )
4541 return (uint8
)SPELL_FAILED_TOTEMS
; //0x7C
4543 //Check items for TotemCategory
4544 uint32 TotemCategory
= 2;
4545 for(int i
=0;i
<2;++i
)
4547 if(m_spellInfo
->TotemCategory
[i
] != 0)
4549 if( p_caster
->HasItemTotemCategory(m_spellInfo
->TotemCategory
[i
]) )
4558 if(TotemCategory
!= 0)
4559 return (uint8
)SPELL_FAILED_TOTEM_CATEGORY
; //0x7B
4561 for(int i
= 0; i
< 3; i
++)
4563 switch (m_spellInfo
->Effect
[i
])
4565 case SPELL_EFFECT_CREATE_ITEM
:
4567 if (!m_IsTriggeredSpell
&& m_spellInfo
->EffectItemType
[i
])
4569 ItemPosCountVec dest
;
4570 uint8 msg
= p_caster
->CanStoreNewItem(NULL_BAG
, NULL_SLOT
, dest
, m_spellInfo
->EffectItemType
[i
], 1 );
4571 if (msg
!= EQUIP_ERR_OK
)
4573 p_caster
->SendEquipError( msg
, NULL
, NULL
);
4574 return SPELL_FAILED_DONT_REPORT
;
4579 case SPELL_EFFECT_ENCHANT_ITEM
:
4581 Item
* targetItem
= m_targets
.getItemTarget();
4583 return SPELL_FAILED_ITEM_NOT_FOUND
;
4585 if( targetItem
->GetProto()->ItemLevel
< m_spellInfo
->baseLevel
)
4586 return SPELL_FAILED_LOWLEVEL
;
4587 // Not allow enchant in trade slot for some enchant type
4588 if( targetItem
->GetOwner() != m_caster
)
4590 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
4591 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
4593 return SPELL_FAILED_ERROR
;
4594 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
4595 return SPELL_FAILED_NOT_TRADEABLE
;
4599 case SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY
:
4601 Item
*item
= m_targets
.getItemTarget();
4603 return SPELL_FAILED_ITEM_NOT_FOUND
;
4604 // Not allow enchant in trade slot for some enchant type
4605 if( item
->GetOwner() != m_caster
)
4607 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
4608 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
4610 return SPELL_FAILED_ERROR
;
4611 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
4612 return SPELL_FAILED_NOT_TRADEABLE
;
4616 case SPELL_EFFECT_ENCHANT_HELD_ITEM
:
4617 // check item existence in effect code (not output errors at offhand hold item effect to main hand for example
4619 case SPELL_EFFECT_DISENCHANT
:
4621 if(!m_targets
.getItemTarget())
4622 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4624 // prevent disenchanting in trade slot
4625 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4626 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4628 ItemPrototype
const* itemProto
= m_targets
.getItemTarget()->GetProto();
4630 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4632 uint32 item_quality
= itemProto
->Quality
;
4633 // 2.0.x addon: Check player enchanting level agains the item desenchanting requirements
4634 uint32 item_disenchantskilllevel
= itemProto
->RequiredDisenchantSkill
;
4635 if (item_disenchantskilllevel
== uint32(-1))
4636 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4637 if (item_disenchantskilllevel
> p_caster
->GetSkillValue(SKILL_ENCHANTING
))
4638 return SPELL_FAILED_LOW_CASTLEVEL
;
4639 if(item_quality
> 4 || item_quality
< 2)
4640 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4641 if(itemProto
->Class
!= ITEM_CLASS_WEAPON
&& itemProto
->Class
!= ITEM_CLASS_ARMOR
)
4642 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4643 if (!itemProto
->DisenchantID
)
4644 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4647 case SPELL_EFFECT_PROSPECTING
:
4649 if(!m_targets
.getItemTarget())
4650 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4651 //ensure item is a prospectable ore
4652 if(!(m_targets
.getItemTarget()->GetProto()->BagFamily
& BAG_FAMILY_MASK_MINING_SUPP
) || m_targets
.getItemTarget()->GetProto()->Class
!= ITEM_CLASS_TRADE_GOODS
)
4653 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4654 //prevent prospecting in trade slot
4655 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4656 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4657 //Check for enough skill in jewelcrafting
4658 uint32 item_prospectingskilllevel
= m_targets
.getItemTarget()->GetProto()->RequiredSkillRank
;
4659 if(item_prospectingskilllevel
>p_caster
->GetSkillValue(SKILL_JEWELCRAFTING
))
4660 return SPELL_FAILED_LOW_CASTLEVEL
;
4661 //make sure the player has the required ores in inventory
4662 if(m_targets
.getItemTarget()->GetCount() < 5)
4663 return SPELL_FAILED_NEED_MORE_ITEMS
;
4665 if(!LootTemplates_Prospecting
.HaveLootFor(m_targets
.getItemTargetEntry()))
4666 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4670 case SPELL_EFFECT_WEAPON_DAMAGE
:
4671 case SPELL_EFFECT_WEAPON_DAMAGE_NOSCHOOL
:
4673 if(m_caster
->GetTypeId() != TYPEID_PLAYER
) return SPELL_FAILED_TARGET_NOT_PLAYER
;
4674 if( m_attackType
!= RANGED_ATTACK
)
4676 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack(m_attackType
);
4677 if(!pItem
|| pItem
->IsBroken())
4678 return SPELL_FAILED_EQUIPPED_ITEM
;
4680 switch(pItem
->GetProto()->SubClass
)
4682 case ITEM_SUBCLASS_WEAPON_THROWN
:
4684 uint32 ammo
= pItem
->GetEntry();
4685 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
4686 return SPELL_FAILED_NO_AMMO
;
4688 case ITEM_SUBCLASS_WEAPON_GUN
:
4689 case ITEM_SUBCLASS_WEAPON_BOW
:
4690 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
4692 uint32 ammo
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
4696 if(m_caster
->GetDummyAura(46699))
4697 break; // skip other checks
4699 return SPELL_FAILED_NO_AMMO
;
4702 ItemPrototype
const *ammoProto
= objmgr
.GetItemPrototype( ammo
);
4704 return SPELL_FAILED_NO_AMMO
;
4706 if(ammoProto
->Class
!= ITEM_CLASS_PROJECTILE
)
4707 return SPELL_FAILED_NO_AMMO
;
4709 // check ammo ws. weapon compatibility
4710 switch(pItem
->GetProto()->SubClass
)
4712 case ITEM_SUBCLASS_WEAPON_BOW
:
4713 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
4714 if(ammoProto
->SubClass
!=ITEM_SUBCLASS_ARROW
)
4715 return SPELL_FAILED_NO_AMMO
;
4717 case ITEM_SUBCLASS_WEAPON_GUN
:
4718 if(ammoProto
->SubClass
!=ITEM_SUBCLASS_BULLET
)
4719 return SPELL_FAILED_NO_AMMO
;
4722 return SPELL_FAILED_NO_AMMO
;
4725 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
4726 return SPELL_FAILED_NO_AMMO
;
4728 case ITEM_SUBCLASS_WEAPON_WAND
:
4741 void Spell::Delayed()
4743 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
4746 if (m_spellState
== SPELL_STATE_DELAYED
)
4747 return; // spell is active and can't be time-backed
4749 // spells not loosing casting time ( slam, dynamites, bombs.. )
4750 if(!(m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_DAMAGE
))
4753 //check resist chance
4754 int32 resistChance
= 100; //must be initialized to 100 for percent modifiers
4755 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
,SPELLMOD_NOT_LOSE_CASTING_TIME
,resistChance
, this);
4756 resistChance
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_RESIST_PUSHBACK
) - 100;
4757 if (roll_chance_i(resistChance
))
4760 int32 delaytime
= GetNextDelayAtDamageMsTime();
4762 if(int32(m_timer
) + delaytime
> m_casttime
)
4764 delaytime
= m_casttime
- m_timer
;
4765 m_timer
= m_casttime
;
4768 m_timer
+= delaytime
;
4770 sLog
.outDetail("Spell %u partially interrupted for (%d) ms at damage",m_spellInfo
->Id
,delaytime
);
4772 WorldPacket
data(SMSG_SPELL_DELAYED
, 8+4);
4773 data
.append(m_caster
->GetPackGUID());
4774 data
<< uint32(delaytime
);
4776 m_caster
->SendMessageToSet(&data
,true);
4779 void Spell::DelayedChannel()
4781 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
|| getState() != SPELL_STATE_CASTING
)
4784 //check resist chance
4785 int32 resistChance
= 100; //must be initialized to 100 for percent modifiers
4786 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
,SPELLMOD_NOT_LOSE_CASTING_TIME
,resistChance
, this);
4787 resistChance
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_RESIST_PUSHBACK
) - 100;
4788 if (roll_chance_i(resistChance
))
4791 int32 delaytime
= GetNextDelayAtDamageMsTime();
4793 if(int32(m_timer
) < delaytime
)
4795 delaytime
= m_timer
;
4799 m_timer
-= delaytime
;
4801 sLog
.outDebug("Spell %u partially interrupted for %i ms, new duration: %u ms", m_spellInfo
->Id
, delaytime
, m_timer
);
4803 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
4805 if ((*ihit
).missCondition
== SPELL_MISS_NONE
)
4807 Unit
* unit
= m_caster
->GetGUID()==ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
4810 for (int j
=0;j
<3;j
++)
4811 if( ihit
->effectMask
& (1<<j
) )
4812 unit
->DelayAura(m_spellInfo
->Id
, j
, delaytime
);
4818 for(int j
= 0; j
< 3; j
++)
4820 // partially interrupt persistent area auras
4821 DynamicObject
* dynObj
= m_caster
->GetDynObject(m_spellInfo
->Id
, j
);
4823 dynObj
->Delay(delaytime
);
4826 SendChannelUpdate(m_timer
);
4829 void Spell::UpdatePointers()
4831 if(m_originalCasterGUID
==m_caster
->GetGUID())
4832 m_originalCaster
= m_caster
;
4835 m_originalCaster
= ObjectAccessor::GetUnit(*m_caster
,m_originalCasterGUID
);
4836 if(m_originalCaster
&& !m_originalCaster
->IsInWorld()) m_originalCaster
= NULL
;
4839 m_targets
.Update(m_caster
);
4842 bool Spell::IsAffectedBy(SpellEntry
const *spellInfo
, uint32 effectId
)
4844 return spellmgr
.IsAffectedBySpell(m_spellInfo
,spellInfo
->Id
,effectId
,spellInfo
->EffectItemType
[effectId
]);
4847 bool Spell::CheckTargetCreatureType(Unit
* target
) const
4849 uint32 spellCreatureTargetMask
= m_spellInfo
->TargetCreatureType
;
4851 // Curse of Doom : not find another way to fix spell target check :/
4852 if(m_spellInfo
->SpellFamilyName
==SPELLFAMILY_WARLOCK
&& m_spellInfo
->SpellFamilyFlags
== 0x0200000000LL
)
4854 // not allow cast at player
4855 if(target
->GetTypeId()==TYPEID_PLAYER
)
4858 spellCreatureTargetMask
= 0x7FF;
4861 // Dismiss Pet and Taming Lesson skipped
4862 if(m_spellInfo
->Id
== 2641 || m_spellInfo
->Id
== 23356)
4863 spellCreatureTargetMask
= 0;
4865 if (spellCreatureTargetMask
)
4867 uint32 TargetCreatureType
= target
->GetCreatureTypeMask();
4869 return !TargetCreatureType
|| (spellCreatureTargetMask
& TargetCreatureType
);
4874 CurrentSpellTypes
Spell::GetCurrentContainer()
4876 if (IsNextMeleeSwingSpell())
4877 return(CURRENT_MELEE_SPELL
);
4878 else if (IsAutoRepeat())
4879 return(CURRENT_AUTOREPEAT_SPELL
);
4880 else if (IsChanneledSpell(m_spellInfo
))
4881 return(CURRENT_CHANNELED_SPELL
);
4883 return(CURRENT_GENERIC_SPELL
);
4886 bool Spell::CheckTarget( Unit
* target
, uint32 eff
, bool hitPhase
)
4888 // Check targets for creature type mask and remove not appropriate (skip explicit self target case, maybe need other explicit targets)
4889 if(m_spellInfo
->EffectImplicitTargetA
[eff
]!=TARGET_SELF
)
4891 if (!CheckTargetCreatureType(target
))
4895 // Check targets for not_selectable unit flag and remove
4896 // A player can cast spells on his pet (or other controlled unit) though in any state
4897 if (target
!= m_caster
&& target
->GetCharmerOrOwnerGUID() != m_caster
->GetGUID())
4899 // any unattackable target skipped
4900 if (target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NON_ATTACKABLE
))
4903 // unselectable targets skipped in all cases except TARGET_SCRIPT targeting
4904 // in case TARGET_SCRIPT target selected by server always and can't be cheated
4905 if( target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NOT_SELECTABLE
) &&
4906 m_spellInfo
->EffectImplicitTargetA
[eff
] != TARGET_SCRIPT
&&
4907 m_spellInfo
->EffectImplicitTargetB
[eff
] != TARGET_SCRIPT
)
4911 //Check player targets and remove if in GM mode or GM invisibility (for not self casting case)
4912 if( target
!= m_caster
&& target
->GetTypeId()==TYPEID_PLAYER
)
4914 if(((Player
*)target
)->GetVisibility()==VISIBILITY_OFF
)
4917 if(((Player
*)target
)->isGameMaster() && !IsPositiveSpell(m_spellInfo
->Id
))
4921 //Check targets for LOS visibility (except spells without range limitations )
4922 switch(m_spellInfo
->Effect
[eff
])
4924 case SPELL_EFFECT_SUMMON_PLAYER
: // from anywhere
4926 case SPELL_EFFECT_DUMMY
:
4927 if(m_spellInfo
->Id
!=20577) // Cannibalize
4930 case SPELL_EFFECT_RESURRECT_NEW
:
4931 // player far away, maybe his corpse near?
4932 if(target
!=m_caster
&& !target
->IsWithinLOSInMap(m_caster
))
4934 if(!m_targets
.getCorpseTargetGUID())
4937 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
4941 if(target
->GetGUID()!=corpse
->GetOwnerGUID())
4944 if(!corpse
->IsWithinLOSInMap(m_caster
))
4948 // all ok by some way or another, skip normal check
4950 default: // normal case
4951 if(target
!=m_caster
&& !target
->IsWithinLOSInMap(m_caster
))
4959 Unit
* Spell::SelectMagnetTarget()
4961 Unit
* target
= m_targets
.getUnitTarget();
4963 if(target
&& target
->HasAuraType(SPELL_AURA_SPELL_MAGNET
) && !(m_spellInfo
->Attributes
& 0x10))
4965 Unit::AuraList
const& magnetAuras
= target
->GetAurasByType(SPELL_AURA_SPELL_MAGNET
);
4966 for(Unit::AuraList::const_iterator itr
= magnetAuras
.begin(); itr
!= magnetAuras
.end(); ++itr
)
4968 if(Unit
* magnet
= (*itr
)->GetCaster())
4970 if(magnet
->IsWithinLOSInMap(m_caster
))
4973 m_targets
.setUnitTarget(target
);
4983 bool Spell::IsNeedSendToClient() const
4985 return m_spellInfo
->SpellVisual
!=0 || IsChanneledSpell(m_spellInfo
) ||
4986 m_spellInfo
->speed
> 0.0f
|| !m_triggeredByAuraSpell
&& !m_IsTriggeredSpell
;
4989 bool Spell::HaveTargetsForEffect( uint8 effect
) const
4991 for(std::list
<TargetInfo
>::const_iterator itr
= m_UniqueTargetInfo
.begin();itr
!= m_UniqueTargetInfo
.end();++itr
)
4992 if(itr
->effectMask
& (1<<effect
))
4995 for(std::list
<GOTargetInfo
>::const_iterator itr
= m_UniqueGOTargetInfo
.begin();itr
!= m_UniqueGOTargetInfo
.end();++itr
)
4996 if(itr
->effectMask
& (1<<effect
))
4999 for(std::list
<ItemTargetInfo
>::const_iterator itr
= m_UniqueItemInfo
.begin();itr
!= m_UniqueItemInfo
.end();++itr
)
5000 if(itr
->effectMask
& (1<<effect
))
5006 SpellEvent::SpellEvent(Spell
* spell
) : BasicEvent()
5011 SpellEvent::~SpellEvent()
5013 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5016 if (m_Spell
->IsDeletable())
5022 sLog
.outError("~SpellEvent: %s %u tried to delete non-deletable spell %u. Was not deleted, causes memory leak.",
5023 (m_Spell
->GetCaster()->GetTypeId()==TYPEID_PLAYER
?"Player":"Creature"), m_Spell
->GetCaster()->GetGUIDLow(),m_Spell
->m_spellInfo
->Id
);
5027 bool SpellEvent::Execute(uint64 e_time
, uint32 p_time
)
5029 // update spell if it is not finished
5030 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5031 m_Spell
->update(p_time
);
5033 // check spell state to process
5034 switch (m_Spell
->getState())
5036 case SPELL_STATE_FINISHED
:
5038 // spell was finished, check deletable state
5039 if (m_Spell
->IsDeletable())
5041 // check, if we do have unfinished triggered spells
5043 return(true); // spell is deletable, finish event
5045 // event will be re-added automatically at the end of routine)
5048 case SPELL_STATE_CASTING
:
5050 // this spell is in channeled state, process it on the next update
5051 // event will be re-added automatically at the end of routine)
5054 case SPELL_STATE_DELAYED
:
5056 // first, check, if we have just started
5057 if (m_Spell
->GetDelayStart() != 0)
5059 // no, we aren't, do the typical update
5060 // check, if we have channeled spell on our hands
5061 if (IsChanneledSpell(m_Spell
->m_spellInfo
))
5063 // evented channeled spell is processed separately, casted once after delay, and not destroyed till finish
5064 // check, if we have casting anything else except this channeled spell and autorepeat
5065 if (m_Spell
->GetCaster()->IsNonMeleeSpellCasted(false, true, true))
5067 // another non-melee non-delayed spell is casted now, abort
5072 // do the action (pass spell to channeling state)
5073 m_Spell
->handle_immediate();
5075 // event will be re-added automatically at the end of routine)
5079 // run the spell handler and think about what we can do next
5080 uint64 t_offset
= e_time
- m_Spell
->GetDelayStart();
5081 uint64 n_offset
= m_Spell
->handle_delayed(t_offset
);
5084 // re-add us to the queue
5085 m_Spell
->GetCaster()->m_Events
.AddEvent(this, m_Spell
->GetDelayStart() + n_offset
, false);
5086 return(false); // event not complete
5089 // finish update event will be re-added automatically at the end of routine)
5094 // delaying had just started, record the moment
5095 m_Spell
->SetDelayStart(e_time
);
5096 // re-plan the event for the delay moment
5097 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ m_Spell
->GetDelayMoment(), false);
5098 return(false); // event not complete
5105 // event will be re-added automatically at the end of routine)
5109 // spell processing not complete, plan event on the next update interval
5110 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ 1, false);
5111 return(false); // event not complete
5114 void SpellEvent::Abort(uint64
/*e_time*/)
5116 // oops, the spell we try to do is aborted
5117 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5121 bool SpellEvent::IsDeletable() const
5123 return m_Spell
->IsDeletable();