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;
274 m_delayAtDamageCount
= 0;
276 m_applyMultiplierMask
= 0;
278 // Get data for type of attack
279 switch (m_spellInfo
->DmgClass
)
281 case SPELL_DAMAGE_CLASS_MELEE
:
282 if (m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_REQ_OFFHAND
)
283 m_attackType
= OFF_ATTACK
;
285 m_attackType
= BASE_ATTACK
;
287 case SPELL_DAMAGE_CLASS_RANGED
:
288 m_attackType
= RANGED_ATTACK
;
292 if (m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_REQ_WAND
)
293 m_attackType
= RANGED_ATTACK
;
295 m_attackType
= BASE_ATTACK
;
299 m_spellSchoolMask
= GetSpellSchoolMask(info
); // Can be override for some spell (wand shoot for example)
301 if(m_attackType
== RANGED_ATTACK
)
304 if((m_caster
->getClassMask() & CLASSMASK_WAND_USERS
) != 0 && m_caster
->GetTypeId()==TYPEID_PLAYER
)
306 if(Item
* pItem
= ((Player
*)m_caster
)->GetWeaponForAttack(RANGED_ATTACK
))
307 m_spellSchoolMask
= SpellSchoolMask(1 << pItem
->GetProto()->Damage
->DamageType
);
311 if(originalCasterGUID
)
312 m_originalCasterGUID
= originalCasterGUID
;
314 m_originalCasterGUID
= m_caster
->GetGUID();
316 if(m_originalCasterGUID
==m_caster
->GetGUID())
317 m_originalCaster
= m_caster
;
320 m_originalCaster
= ObjectAccessor::GetUnit(*m_caster
,m_originalCasterGUID
);
321 if(m_originalCaster
&& !m_originalCaster
->IsInWorld()) m_originalCaster
= NULL
;
324 for(int i
=0; i
<3; ++i
)
325 m_currentBasePoints
[i
] = m_spellInfo
->EffectBasePoints
[i
];
327 m_spellState
= SPELL_STATE_NULL
;
329 m_castPositionX
= m_castPositionY
= m_castPositionZ
= 0;
330 m_TriggerSpells
.clear();
331 m_IsTriggeredSpell
= triggered
;
332 //m_AreaAura = false;
337 gameObjTarget
= NULL
;
341 m_triggeredByAuraSpell
= NULL
;
344 if( m_spellInfo
->AttributesEx2
== 0x000020 && !triggered
)
347 m_autoRepeat
= false;
349 m_powerCost
= 0; // setup to correct value in Spell::prepare, don't must be used before.
350 m_casttime
= 0; // setup to correct value in Spell::prepare, don't must be used before.
351 m_timer
= 0; // will set to castime in prepare
353 m_needAliveTargetMask
= 0;
355 // determine reflection
356 m_canReflect
= false;
358 if(m_spellInfo
->DmgClass
== SPELL_DAMAGE_CLASS_MAGIC
&& (m_spellInfo
->AttributesEx2
& 0x4)==0)
362 if (m_spellInfo
->Effect
[j
]==0)
365 if(!IsPositiveTarget(m_spellInfo
->EffectImplicitTargetA
[j
],m_spellInfo
->EffectImplicitTargetB
[j
]))
368 m_canReflect
= (m_spellInfo
->AttributesEx
& (1<<7)) ? true : false;
384 void Spell::FillTargetMap()
386 // TODO: ADD the correct target FILLS!!!!!!
388 for(uint32 i
=0;i
<3;i
++)
390 // not call for empty effect.
391 // Also some spells use not used effect targets for store targets for dummy effect in triggered spells
392 if(m_spellInfo
->Effect
[i
]==0)
395 // targets for TARGET_SCRIPT_COORDINATES (A) and TARGET_SCRIPT filled in Spell::canCast call
396 if( m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SCRIPT_COORDINATES
||
397 m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_SCRIPT
||
398 m_spellInfo
->EffectImplicitTargetB
[i
] == TARGET_SCRIPT
&& m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_SELF
)
401 // TODO: find a way so this is not needed?
402 // for area auras always add caster as target (needed for totems for example)
403 if(IsAreaAuraEffect(m_spellInfo
->Effect
[i
]))
404 AddUnitTarget(m_caster
, i
);
406 std::list
<Unit
*> tmpUnitMap
;
408 // TargetA/TargetB dependent from each other, we not switch to full support this dependences
409 // but need it support in some know cases
410 switch(m_spellInfo
->EffectImplicitTargetA
[i
])
412 case TARGET_ALL_AROUND_CASTER
:
413 if( m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_PARTY
||
414 m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_FRIENDLY_UNITS_AROUND_CASTER
||
415 m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_RANDOM_RAID_MEMBER
)
417 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
419 // Note: this hack with search required until GO casting not implemented
420 // environment damage spells already have around enemies targeting but this not help in case not existed GO casting support
421 // currently each enemy selected explicitly and self cast damage
422 else if(m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_ENEMY_IN_AREA
&& m_spellInfo
->Effect
[i
]==SPELL_EFFECT_ENVIRONMENTAL_DAMAGE
)
424 if(m_targets
.getUnitTarget())
425 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
429 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
430 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
433 case TARGET_TABLE_X_Y_Z_COORDINATES
:
434 // Only if target A, for target B (used in teleports) dest select in effect
435 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
438 switch(m_spellInfo
->EffectImplicitTargetB
[i
])
440 case TARGET_SCRIPT_COORDINATES
: // B case filled in canCast but we need fill unit list base at A case
441 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
444 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetA
[i
],tmpUnitMap
);
445 SetTargetMap(i
,m_spellInfo
->EffectImplicitTargetB
[i
],tmpUnitMap
);
451 if( (m_spellInfo
->EffectImplicitTargetA
[i
]==0 || m_spellInfo
->EffectImplicitTargetA
[i
]==TARGET_EFFECT_SELECT
) &&
452 (m_spellInfo
->EffectImplicitTargetB
[i
]==0 || m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_EFFECT_SELECT
) )
454 // add here custom effects that need default target.
455 // FOR EVERY TARGET TYPE THERE IS A DIFFERENT FILL!!
456 switch(m_spellInfo
->Effect
[i
])
458 case SPELL_EFFECT_DUMMY
:
460 switch(m_spellInfo
->Id
)
462 case 20577: // Cannibalize
464 // non-standard target selection
465 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
466 float max_range
= GetSpellMaxRange(srange
);
468 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
470 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
473 WorldObject
* result
= NULL
;
475 MaNGOS::CannibalizeObjectCheck
u_check(m_caster
, max_range
);
476 MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
> searcher(result
, u_check
);
478 TypeContainerVisitor
<MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
>, GridTypeMapContainer
> grid_searcher(searcher
);
479 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
480 cell_lock
->Visit(cell_lock
, grid_searcher
, *m_caster
->GetMap());
484 TypeContainerVisitor
<MaNGOS::WorldObjectSearcher
<MaNGOS::CannibalizeObjectCheck
>, WorldTypeMapContainer
> world_searcher(searcher
);
485 cell_lock
->Visit(cell_lock
, world_searcher
, *m_caster
->GetMap());
490 switch(result
->GetTypeId())
494 tmpUnitMap
.push_back((Unit
*)result
);
497 m_targets
.setCorpseTarget((Corpse
*)result
);
498 if(Player
* owner
= ObjectAccessor::FindPlayer(((Corpse
*)result
)->GetOwnerGUID()))
499 tmpUnitMap
.push_back(owner
);
505 // clear cooldown at fail
506 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
508 ((Player
*)m_caster
)->RemoveSpellCooldown(m_spellInfo
->Id
);
510 WorldPacket
data(SMSG_CLEAR_COOLDOWN
, (4+8));
511 data
<< uint32(m_spellInfo
->Id
);
512 data
<< uint64(m_caster
->GetGUID());
513 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
516 SendCastResult(SPELL_FAILED_NO_EDIBLE_CORPSES
);
522 if(m_targets
.getUnitTarget())
523 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
528 case SPELL_EFFECT_RESURRECT
:
529 case SPELL_EFFECT_PARRY
:
530 case SPELL_EFFECT_BLOCK
:
531 case SPELL_EFFECT_CREATE_ITEM
:
532 case SPELL_EFFECT_TRIGGER_SPELL
:
533 case SPELL_EFFECT_TRIGGER_MISSILE
:
534 case SPELL_EFFECT_LEARN_SPELL
:
535 case SPELL_EFFECT_SKILL_STEP
:
536 case SPELL_EFFECT_PROFICIENCY
:
537 case SPELL_EFFECT_SUMMON_POSSESSED
:
538 case SPELL_EFFECT_SUMMON_OBJECT_WILD
:
539 case SPELL_EFFECT_SELF_RESURRECT
:
540 case SPELL_EFFECT_REPUTATION
:
541 if(m_targets
.getUnitTarget())
542 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
544 case SPELL_EFFECT_SUMMON_PLAYER
:
545 if(m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->GetSelection())
547 Player
* target
= objmgr
.GetPlayer(((Player
*)m_caster
)->GetSelection());
549 tmpUnitMap
.push_back(target
);
552 case SPELL_EFFECT_RESURRECT_NEW
:
553 if(m_targets
.getUnitTarget())
554 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
555 if(m_targets
.getCorpseTargetGUID())
557 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
560 Player
* owner
= ObjectAccessor::FindPlayer(corpse
->GetOwnerGUID());
562 tmpUnitMap
.push_back(owner
);
566 case SPELL_EFFECT_SUMMON
:
567 if(m_spellInfo
->EffectMiscValueB
[i
] == SUMMON_TYPE_POSESSED
|| m_spellInfo
->EffectMiscValueB
[i
] == SUMMON_TYPE_POSESSED2
)
569 if(m_targets
.getUnitTarget())
570 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
573 tmpUnitMap
.push_back(m_caster
);
575 case SPELL_EFFECT_SUMMON_CHANGE_ITEM
:
576 case SPELL_EFFECT_SUMMON_WILD
:
577 case SPELL_EFFECT_SUMMON_GUARDIAN
:
578 case SPELL_EFFECT_TRANS_DOOR
:
579 case SPELL_EFFECT_ADD_FARSIGHT
:
580 case SPELL_EFFECT_APPLY_GLYPH
:
581 case SPELL_EFFECT_STUCK
:
582 case SPELL_EFFECT_DESTROY_ALL_TOTEMS
:
583 case SPELL_EFFECT_SUMMON_DEMON
:
584 case SPELL_EFFECT_SKILL
:
585 tmpUnitMap
.push_back(m_caster
);
587 case SPELL_EFFECT_LEARN_PET_SPELL
:
588 if(Pet
* pet
= m_caster
->GetPet())
589 tmpUnitMap
.push_back(pet
);
591 case SPELL_EFFECT_ENCHANT_ITEM
:
592 case SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY
:
593 case SPELL_EFFECT_DISENCHANT
:
594 case SPELL_EFFECT_FEED_PET
:
595 case SPELL_EFFECT_PROSPECTING
:
596 case SPELL_EFFECT_MILLING
:
597 if(m_targets
.getItemTarget())
598 AddItemTarget(m_targets
.getItemTarget(), i
);
600 case SPELL_EFFECT_APPLY_AURA
:
601 switch(m_spellInfo
->EffectApplyAuraName
[i
])
603 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)
604 case SPELL_AURA_ADD_PCT_MODIFIER
:
605 tmpUnitMap
.push_back(m_caster
);
607 default: // apply to target in other case
608 if(m_targets
.getUnitTarget())
609 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
613 case SPELL_EFFECT_APPLY_AREA_AURA_PARTY
:
615 if(m_spellInfo
->Attributes
== 0x9050000 || m_spellInfo
->Attributes
== 0x10000)
616 SetTargetMap(i
,TARGET_AREAEFFECT_PARTY
,tmpUnitMap
);
618 case SPELL_EFFECT_SKIN_PLAYER_CORPSE
:
619 if(m_targets
.getUnitTarget())
621 tmpUnitMap
.push_back(m_targets
.getUnitTarget());
623 else if (m_targets
.getCorpseTargetGUID())
625 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
628 Player
* owner
= ObjectAccessor::FindPlayer(corpse
->GetOwnerGUID());
630 tmpUnitMap
.push_back(owner
);
638 if(IsChanneledSpell(m_spellInfo
) && !tmpUnitMap
.empty())
639 m_needAliveTargetMask
|= (1<<i
);
641 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
643 Player
*me
= (Player
*)m_caster
;
644 for (std::list
<Unit
*>::const_iterator itr
= tmpUnitMap
.begin(); itr
!= tmpUnitMap
.end(); itr
++)
646 Unit
*owner
= (*itr
)->GetOwner();
647 Unit
*u
= owner
? owner
: (*itr
);
648 if(u
!=m_caster
&& u
->IsPvP() && (!me
->duel
|| me
->duel
->opponent
!= u
))
651 me
->RemoveAurasWithInterruptFlags(AURA_INTERRUPT_FLAG_ENTER_PVP_COMBAT
);
657 for (std::list
<Unit
*>::iterator itr
= tmpUnitMap
.begin() ; itr
!= tmpUnitMap
.end();)
659 if (!CheckTarget (*itr
, i
))
661 itr
= tmpUnitMap
.erase(itr
);
668 for(std::list
<Unit
*>::iterator iunit
= tmpUnitMap
.begin();iunit
!= tmpUnitMap
.end();++iunit
)
669 AddUnitTarget((*iunit
), i
);
673 void Spell::CleanupTargetList()
675 m_UniqueTargetInfo
.clear();
676 m_UniqueGOTargetInfo
.clear();
677 m_UniqueItemInfo
.clear();
683 void Spell::AddUnitTarget(Unit
* pVictim
, uint32 effIndex
)
685 if( m_spellInfo
->Effect
[effIndex
]==0 )
688 uint64 targetGUID
= pVictim
->GetGUID();
690 // Lookup target in already in list
691 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
693 if (targetGUID
== ihit
->targetGUID
) // Found in list
695 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
700 // This is new target calculate data for him
702 // Get spell hit result on target
704 target
.targetGUID
= targetGUID
; // Store target GUID
705 target
.effectMask
= 1<<effIndex
; // Store index of effect
706 target
.processed
= false; // Effects not apply on target
708 // Calculate hit result
709 target
.missCondition
= m_caster
->SpellHitResult(pVictim
, m_spellInfo
, m_canReflect
);
710 if (target
.missCondition
== SPELL_MISS_NONE
)
715 // Spell have speed - need calculate incoming time
716 if (m_spellInfo
->speed
> 0.0f
)
718 // calculate spell incoming interval
719 float dist
= m_caster
->GetDistance(pVictim
->GetPositionX(), pVictim
->GetPositionY(), pVictim
->GetPositionZ());
720 if (dist
< 5.0f
) dist
= 5.0f
;
721 target
.timeDelay
= (uint64
) floor(dist
/ m_spellInfo
->speed
* 1000.0f
);
723 // Calculate minimum incoming time
724 if (m_delayMoment
==0 || m_delayMoment
>target
.timeDelay
)
725 m_delayMoment
= target
.timeDelay
;
728 target
.timeDelay
= 0LL;
730 // If target reflect spell back to caster
731 if (target
.missCondition
==SPELL_MISS_REFLECT
)
733 // Calculate reflected spell result on caster
734 target
.reflectResult
= m_caster
->SpellHitResult(m_caster
, m_spellInfo
, m_canReflect
);
736 if (target
.reflectResult
== SPELL_MISS_REFLECT
) // Impossible reflect again, so simply deflect spell
737 target
.reflectResult
= SPELL_MISS_PARRY
;
739 // Increase time interval for reflected spells by 1.5
740 target
.timeDelay
+=target
.timeDelay
>>1;
743 target
.reflectResult
= SPELL_MISS_NONE
;
745 // Add target to list
746 m_UniqueTargetInfo
.push_back(target
);
749 void Spell::AddUnitTarget(uint64 unitGUID
, uint32 effIndex
)
751 Unit
* unit
= m_caster
->GetGUID()==unitGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, unitGUID
);
753 AddUnitTarget(unit
, effIndex
);
756 void Spell::AddGOTarget(GameObject
* pVictim
, uint32 effIndex
)
758 if( m_spellInfo
->Effect
[effIndex
]==0 )
761 uint64 targetGUID
= pVictim
->GetGUID();
763 // Lookup target in already in list
764 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
766 if (targetGUID
== ihit
->targetGUID
) // Found in list
768 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
773 // This is new target calculate data for him
776 target
.targetGUID
= targetGUID
;
777 target
.effectMask
= 1<<effIndex
;
778 target
.processed
= false; // Effects not apply on target
780 // Spell have speed - need calculate incoming time
781 if (m_spellInfo
->speed
> 0.0f
)
783 // calculate spell incoming interval
784 float dist
= m_caster
->GetDistance(pVictim
->GetPositionX(), pVictim
->GetPositionY(), pVictim
->GetPositionZ());
785 if (dist
< 5.0f
) dist
= 5.0f
;
786 target
.timeDelay
= (uint64
) floor(dist
/ m_spellInfo
->speed
* 1000.0f
);
787 if (m_delayMoment
==0 || m_delayMoment
>target
.timeDelay
)
788 m_delayMoment
= target
.timeDelay
;
791 target
.timeDelay
= 0LL;
795 // Add target to list
796 m_UniqueGOTargetInfo
.push_back(target
);
799 void Spell::AddGOTarget(uint64 goGUID
, uint32 effIndex
)
801 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, goGUID
);
803 AddGOTarget(go
, effIndex
);
806 void Spell::AddItemTarget(Item
* pitem
, uint32 effIndex
)
808 if( m_spellInfo
->Effect
[effIndex
]==0 )
811 // Lookup target in already in list
812 for(std::list
<ItemTargetInfo
>::iterator ihit
= m_UniqueItemInfo
.begin();ihit
!= m_UniqueItemInfo
.end();++ihit
)
814 if (pitem
== ihit
->item
) // Found in list
816 ihit
->effectMask
|= 1<<effIndex
; // Add only effect mask
821 // This is new target add data
823 ItemTargetInfo target
;
825 target
.effectMask
= 1<<effIndex
;
826 m_UniqueItemInfo
.push_back(target
);
829 void Spell::doTriggers(SpellMissInfo missInfo
, uint32 damage
, SpellSchoolMask damageSchoolMask
, uint32 block
, uint32 absorb
, bool crit
)
831 // Do triggers depends from hit result (triggers on hit do in effects)
832 // Set aura states depends from hit result
833 if (missInfo
!=SPELL_MISS_NONE
)
835 // Miss/dodge/parry/block only for melee based spells
836 // Resist only for magic based spells
839 case SPELL_MISS_MISS
:
840 if(m_caster
->GetTypeId()== TYPEID_PLAYER
)
841 ((Player
*)m_caster
)->UpdateWeaponSkill(BASE_ATTACK
);
843 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_MISS
, m_spellInfo
, m_IsTriggeredSpell
);
845 case SPELL_MISS_RESIST
:
846 m_caster
->ProcDamageAndSpell(unitTarget
, PROC_FLAG_TARGET_RESISTS
, PROC_FLAG_RESIST_SPELL
, 0, damageSchoolMask
, m_spellInfo
, m_IsTriggeredSpell
);
848 case SPELL_MISS_DODGE
:
849 if(unitTarget
->GetTypeId() == TYPEID_PLAYER
)
850 ((Player
*)unitTarget
)->UpdateDefense();
853 if (m_caster
->GetTypeId() == TYPEID_PLAYER
&& m_caster
->getClass() == CLASS_WARRIOR
)
855 ((Player
*) m_caster
)->AddComboPoints(unitTarget
, 1);
856 m_caster
->StartReactiveTimer( REACTIVE_OVERPOWER
);
860 if (unitTarget
->getClass() != CLASS_ROGUE
)
862 unitTarget
->ModifyAuraState(AURA_STATE_DEFENSE
, true);
863 unitTarget
->StartReactiveTimer( REACTIVE_DEFENSE
);
866 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_DODGE
, m_spellInfo
, m_IsTriggeredSpell
);
868 case SPELL_MISS_PARRY
:
869 // Update victim defense ?
870 if(unitTarget
->GetTypeId() == TYPEID_PLAYER
)
871 ((Player
*)unitTarget
)->UpdateDefense();
872 // Mongoose bite - set only Counterattack here
873 if (unitTarget
->getClass() == CLASS_HUNTER
)
875 unitTarget
->ModifyAuraState(AURA_STATE_HUNTER_PARRY
,true);
876 unitTarget
->StartReactiveTimer( REACTIVE_HUNTER_PARRY
);
880 unitTarget
->ModifyAuraState(AURA_STATE_DEFENSE
, true);
881 unitTarget
->StartReactiveTimer( REACTIVE_DEFENSE
);
883 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_PARRY
, m_spellInfo
, m_IsTriggeredSpell
);
885 case SPELL_MISS_BLOCK
:
886 unitTarget
->ModifyAuraState(AURA_STATE_DEFENSE
, true);
887 unitTarget
->StartReactiveTimer( REACTIVE_DEFENSE
);
889 m_caster
->CastMeleeProcDamageAndSpell(unitTarget
, 0, damageSchoolMask
, m_attackType
, MELEE_HIT_BLOCK
, m_spellInfo
, m_IsTriggeredSpell
);
891 // Trigger from this events not supported
892 case SPELL_MISS_EVADE
:
893 case SPELL_MISS_IMMUNE
:
894 case SPELL_MISS_IMMUNE2
:
895 case SPELL_MISS_DEFLECT
:
896 case SPELL_MISS_ABSORB
:
897 // Trigger from reflects need do after get reflect result
898 case SPELL_MISS_REFLECT
:
906 void Spell::DoAllEffectOnTarget(TargetInfo
*target
)
908 if (target
->processed
) // Check target
910 target
->processed
= true; // Target checked in apply effects procedure
912 // Get mask of effects for target
913 uint32 mask
= target
->effectMask
;
914 if (mask
== 0) // No effects
917 Unit
* unit
= m_caster
->GetGUID()==target
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
,target
->targetGUID
);
921 SpellMissInfo missInfo
= target
->missCondition
;
922 // Need init unitTarget by default unit (can changed in code on reflect)
923 // Or on missInfo!=SPELL_MISS_NONE unitTarget undefined (but need in trigger subsystem)
926 if (missInfo
==SPELL_MISS_NONE
) // In case spell hit target, do all effect on that target
927 DoSpellHitOnUnit(unit
, mask
);
928 else if (missInfo
== SPELL_MISS_REFLECT
) // In case spell reflect from target, do all effect on caster (if hit)
930 if (target
->reflectResult
== SPELL_MISS_NONE
) // If reflected spell hit caster -> do all effect on him
931 DoSpellHitOnUnit(m_caster
, mask
);
934 // Do triggers only on miss/resist/parry/dodge
935 if (missInfo
!=SPELL_MISS_NONE
)
936 doTriggers(missInfo
);
938 // Call scripted function for AI if this spell is casted upon a creature (except pets)
939 if(IS_CREATURE_GUID(target
->targetGUID
))
941 // cast at creature (or GO) quest objectives update at successful cast finished (+channel finished)
942 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
943 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() && !IsChannelActive() )
944 ((Player
*)m_caster
)->CastedCreatureOrGO(unit
->GetEntry(),unit
->GetGUID(),m_spellInfo
->Id
);
946 if(((Creature
*)unit
)->AI())
947 ((Creature
*)unit
)->AI()->SpellHit(m_caster
,m_spellInfo
);
951 void Spell::DoSpellHitOnUnit(Unit
*unit
, const uint32 effectMask
)
953 if(!unit
|| !effectMask
)
956 // Recheck immune (only for delayed spells)
957 if( m_spellInfo
->speed
&& (
958 unit
->IsImmunedToDamage(GetSpellSchoolMask(m_spellInfo
),true) ||
959 unit
->IsImmunedToSpell(m_spellInfo
,true) ))
961 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_IMMUNE
);
965 if (unit
->GetTypeId() == TYPEID_PLAYER
)
967 ((Player
*)unit
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_BE_SPELL_TARGET
, m_spellInfo
->Id
);
968 ((Player
*)unit
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_BE_SPELL_TARGET2
, m_spellInfo
->Id
);
971 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
973 ((Player
*)m_caster
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_CAST_SPELL2
, m_spellInfo
->Id
, 0, unit
);
976 if( m_caster
!= unit
)
978 if( !m_caster
->IsFriendlyTo(unit
) )
980 // for delayed spells ignore not visible explicit target
981 if(m_spellInfo
->speed
> 0.0f
&& unit
==m_targets
.getUnitTarget() && !unit
->isVisibleForOrDetect(m_caster
,false))
983 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_EVADE
);
987 // exclude Arcane Missiles Dummy Aura aura for now (attack on hit)
988 // TODO: find way to not need this?
989 if(!(m_spellInfo
->SpellFamilyName
== SPELLFAMILY_MAGE
&&
990 m_spellInfo
->SpellFamilyFlags
& 0x800LL
))
992 unit
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
994 if( !(m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_NO_INITIAL_AGGRO
) )
996 if(!unit
->IsStandState() && !unit
->hasUnitState(UNIT_STAT_STUNNED
))
997 unit
->SetStandState(PLAYER_STATE_NONE
);
999 if(!unit
->isInCombat() && unit
->GetTypeId() != TYPEID_PLAYER
&& ((Creature
*)unit
)->AI())
1000 ((Creature
*)unit
)->AI()->AttackStart(m_caster
);
1002 unit
->SetInCombatWith(m_caster
);
1003 m_caster
->SetInCombatWith(unit
);
1005 if(Player
*attackedPlayer
= unit
->GetCharmerOrOwnerPlayerOrPlayerItself())
1007 m_caster
->SetContestedPvP(attackedPlayer
);
1009 unit
->AddThreat(m_caster
, 0.0f
);
1015 // for delayed spells ignore negative spells (after duel end) for friendly targets
1016 if(m_spellInfo
->speed
> 0.0f
&& !IsPositiveSpell(m_spellInfo
->Id
))
1018 m_caster
->SendSpellMiss(unit
, m_spellInfo
->Id
, SPELL_MISS_EVADE
);
1022 // assisting case, healing and resurrection
1023 if(unit
->hasUnitState(UNIT_STAT_ATTACK_PLAYER
))
1024 m_caster
->SetContestedPvP();
1025 if( unit
->isInCombat() && !(m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_NO_INITIAL_AGGRO
) )
1027 m_caster
->SetInCombatState(unit
->GetCombatTimer() > 0);
1028 unit
->getHostilRefManager().threatAssist(m_caster
, 0.0f
);
1033 // Get Data Needed for Diminishing Returns, some effects may have multiple auras, so this must be done on spell hit, not aura add
1034 m_diminishGroup
= GetDiminishingReturnsGroupForSpell(m_spellInfo
,m_triggeredByAuraSpell
);
1035 m_diminishLevel
= unit
->GetDiminishing(m_diminishGroup
);
1036 // Increase Diminishing on unit, current informations for actually casts will use values above
1037 if((GetDiminishingReturnsGroupType(m_diminishGroup
) == DRTYPE_PLAYER
&& unit
->GetTypeId() == TYPEID_PLAYER
) || GetDiminishingReturnsGroupType(m_diminishGroup
) == DRTYPE_ALL
)
1038 unit
->IncrDiminishing(m_diminishGroup
);
1040 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1042 if (effectMask
& (1<<effectNumber
))
1044 HandleEffects(unit
,NULL
,NULL
,effectNumber
,m_damageMultipliers
[effectNumber
]);
1045 if ( m_applyMultiplierMask
& (1 << effectNumber
) )
1048 float multiplier
= m_spellInfo
->DmgMultiplier
[effectNumber
];
1049 // Apply multiplier mods
1050 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1051 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_EFFECT_PAST_FIRST
, multiplier
,this);
1052 m_damageMultipliers
[effectNumber
] *= multiplier
;
1058 void Spell::DoAllEffectOnTarget(GOTargetInfo
*target
)
1060 if (target
->processed
) // Check target
1062 target
->processed
= true; // Target checked in apply effects procedure
1064 uint32 effectMask
= target
->effectMask
;
1068 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, target
->targetGUID
);
1072 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1073 if (effectMask
& (1<<effectNumber
))
1074 HandleEffects(NULL
,NULL
,go
,effectNumber
);
1076 // cast at creature (or GO) quest objectives update at successful cast finished (+channel finished)
1077 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
1078 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() && !IsChannelActive() )
1079 ((Player
*)m_caster
)->CastedCreatureOrGO(go
->GetEntry(),go
->GetGUID(),m_spellInfo
->Id
);
1082 void Spell::DoAllEffectOnTarget(ItemTargetInfo
*target
)
1084 uint32 effectMask
= target
->effectMask
;
1085 if(!target
->item
|| !effectMask
)
1088 for(uint32 effectNumber
=0;effectNumber
<3;effectNumber
++)
1089 if (effectMask
& (1<<effectNumber
))
1090 HandleEffects(NULL
, target
->item
, NULL
, effectNumber
);
1093 bool Spell::IsAliveUnitPresentInTargetList()
1095 // Not need check return true
1096 if (m_needAliveTargetMask
== 0)
1099 uint8 needAliveTargetMask
= m_needAliveTargetMask
;
1101 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
1103 if( ihit
->missCondition
== SPELL_MISS_NONE
&& (needAliveTargetMask
& ihit
->effectMask
) )
1105 Unit
*unit
= m_caster
->GetGUID()==ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
1107 if (unit
&& unit
->isAlive())
1108 needAliveTargetMask
&= ~ihit
->effectMask
; // remove from need alive mask effect that have alive target
1112 // is all effects from m_needAliveTargetMask have alive targets
1113 return needAliveTargetMask
==0;
1116 // Helper for Chain Healing
1117 // Spell target first
1118 // Raidmates then descending by injury suffered (MaxHealth - Health)
1119 // Other players/mobs then descending by injury suffered (MaxHealth - Health)
1120 struct ChainHealingOrder
: public std::binary_function
<const Unit
*, const Unit
*, bool>
1122 const Unit
* MainTarget
;
1123 ChainHealingOrder(Unit
const* Target
) : MainTarget(Target
) {};
1124 // functor for operator ">"
1125 bool operator()(Unit
const* _Left
, Unit
const* _Right
) const
1127 return (ChainHealingHash(_Left
) < ChainHealingHash(_Right
));
1129 int32
ChainHealingHash(Unit
const* Target
) const
1131 if (Target
== MainTarget
)
1133 else if (Target
->GetTypeId() == TYPEID_PLAYER
&& MainTarget
->GetTypeId() == TYPEID_PLAYER
&&
1134 ((Player
const*)Target
)->IsInSameRaidWith((Player
const*)MainTarget
))
1136 if (Target
->GetHealth() == Target
->GetMaxHealth())
1139 return 20000 - Target
->GetMaxHealth() + Target
->GetHealth();
1142 return 40000 - Target
->GetMaxHealth() + Target
->GetHealth();
1146 class ChainHealingFullHealth
: std::unary_function
<const Unit
*, bool>
1149 const Unit
* MainTarget
;
1150 ChainHealingFullHealth(const Unit
* Target
) : MainTarget(Target
) {};
1152 bool operator()(const Unit
* Target
)
1154 return (Target
!= MainTarget
&& Target
->GetHealth() == Target
->GetMaxHealth());
1158 // Helper for targets nearest to the spell target
1159 // The spell target is always first unless there is a target at _completely_ the same position (unbelievable case)
1160 struct TargetDistanceOrder
: public std::binary_function
<const Unit
, const Unit
, bool>
1162 const Unit
* MainTarget
;
1163 TargetDistanceOrder(const Unit
* Target
) : MainTarget(Target
) {};
1164 // functor for operator ">"
1165 bool operator()(const Unit
* _Left
, const Unit
* _Right
) const
1167 return (MainTarget
->GetDistance(_Left
) < MainTarget
->GetDistance(_Right
));
1171 void Spell::SetTargetMap(uint32 i
,uint32 cur
,std::list
<Unit
*> &TagUnitMap
)
1174 if (m_spellInfo
->EffectRadiusIndex
[i
])
1175 radius
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[i
]));
1177 radius
= GetSpellMaxRange(sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
));
1179 if(m_originalCaster
)
1180 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1181 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_RADIUS
, radius
,this);
1183 uint32 EffectChainTarget
= m_spellInfo
->EffectChainTarget
[i
];
1184 if(m_originalCaster
)
1185 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
1186 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_JUMP_TARGETS
, EffectChainTarget
, this);
1188 uint32 unMaxTargets
= m_spellInfo
->MaxAffectedTargets
;
1191 case TARGET_TOTEM_EARTH
:
1192 case TARGET_TOTEM_WATER
:
1193 case TARGET_TOTEM_AIR
:
1194 case TARGET_TOTEM_FIRE
:
1197 case TARGET_DYNAMIC_OBJECT
:
1198 case TARGET_AREAEFFECT_CUSTOM
:
1199 case TARGET_AREAEFFECT_CUSTOM_2
:
1202 TagUnitMap
.push_back(m_caster
);
1205 case TARGET_RANDOM_ENEMY_CHAIN_IN_AREA
:
1207 m_targets
.m_targetMask
= 0;
1208 unMaxTargets
= EffectChainTarget
;
1209 float max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1211 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1213 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1216 std::list
<Unit
*> tempUnitMap
;
1219 MaNGOS::AnyAoETargetUnitInObjectRangeCheck
u_check(m_caster
, m_caster
, max_range
);
1220 MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
> searcher(tempUnitMap
, u_check
);
1222 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_unit_searcher(searcher
);
1223 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1225 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1226 cell_lock
->Visit(cell_lock
, world_unit_searcher
, *m_caster
->GetMap());
1227 cell_lock
->Visit(cell_lock
, grid_unit_searcher
, *m_caster
->GetMap());
1230 if(tempUnitMap
.empty())
1233 tempUnitMap
.sort(TargetDistanceOrder(m_caster
));
1235 //Now to get us a random target that's in the initial range of the spell
1237 std::list
<Unit
*>::iterator itr
= tempUnitMap
.begin();
1238 while(itr
!= tempUnitMap
.end() && (*itr
)->GetDistance(m_caster
) < radius
)
1244 itr
= tempUnitMap
.begin();
1245 std::advance(itr
, rand()%t
);
1246 Unit
*pUnitTarget
= *itr
;
1247 TagUnitMap
.push_back(pUnitTarget
);
1249 tempUnitMap
.erase(itr
);
1251 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1253 t
= unMaxTargets
- 1;
1254 Unit
*prev
= pUnitTarget
;
1255 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1257 while(t
&& next
!= tempUnitMap
.end() )
1259 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1262 if(!prev
->IsWithinLOSInMap(*next
))
1269 TagUnitMap
.push_back(prev
);
1270 tempUnitMap
.erase(next
);
1271 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1272 next
= tempUnitMap
.begin();
1279 Pet
* tmpUnit
= m_caster
->GetPet();
1280 if (!tmpUnit
) break;
1281 TagUnitMap
.push_back(tmpUnit
);
1284 case TARGET_CHAIN_DAMAGE
:
1286 if (EffectChainTarget
<= 1)
1288 Unit
* pUnitTarget
= SelectMagnetTarget();
1290 TagUnitMap
.push_back(pUnitTarget
);
1294 Unit
* pUnitTarget
= m_targets
.getUnitTarget();
1298 unMaxTargets
= EffectChainTarget
;
1301 if(m_spellInfo
->DmgClass
==SPELL_DAMAGE_CLASS_MELEE
)
1302 max_range
= radius
; //
1304 //FIXME: This very like horrible hack and wrong for most spells
1305 max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1307 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1309 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1312 Unit
* originalCaster
= GetOriginalCaster();
1315 std::list
<Unit
*> tempUnitMap
;
1318 MaNGOS::AnyAoETargetUnitInObjectRangeCheck
u_check(pUnitTarget
, originalCaster
, max_range
);
1319 MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
> searcher(tempUnitMap
, u_check
);
1321 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, WorldTypeMapContainer
> world_unit_searcher(searcher
);
1322 TypeContainerVisitor
<MaNGOS::UnitListSearcher
<MaNGOS::AnyAoETargetUnitInObjectRangeCheck
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1324 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1325 cell_lock
->Visit(cell_lock
, world_unit_searcher
, *m_caster
->GetMap());
1326 cell_lock
->Visit(cell_lock
, grid_unit_searcher
, *m_caster
->GetMap());
1329 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1331 if(tempUnitMap
.empty())
1334 if(*tempUnitMap
.begin() == pUnitTarget
)
1335 tempUnitMap
.erase(tempUnitMap
.begin());
1337 TagUnitMap
.push_back(pUnitTarget
);
1338 uint32 t
= unMaxTargets
- 1;
1339 Unit
*prev
= pUnitTarget
;
1340 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1342 while(t
&& next
!= tempUnitMap
.end() )
1344 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1347 if(!prev
->IsWithinLOSInMap(*next
))
1354 TagUnitMap
.push_back(prev
);
1355 tempUnitMap
.erase(next
);
1356 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1357 next
= tempUnitMap
.begin();
1364 case TARGET_ALL_ENEMY_IN_AREA
:
1367 case TARGET_ALL_ENEMY_IN_AREA_INSTANT
:
1369 // targets the ground, not the units in the area
1370 if (m_spellInfo
->Effect
[i
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
1372 CellPair
p(MaNGOS::ComputeCellPair(m_targets
.m_destX
, m_targets
.m_destY
));
1374 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1377 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1379 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1380 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1382 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1383 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1384 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1386 // exclude caster (this can be important if this not original caster)
1387 TagUnitMap
.remove(m_caster
);
1390 case TARGET_DUELVSPLAYER_COORDINATES
:
1392 if(Unit
* currentTarget
= m_targets
.getUnitTarget())
1394 m_targets
.setDestination(currentTarget
->GetPositionX(), currentTarget
->GetPositionY(), currentTarget
->GetPositionZ());
1395 TagUnitMap
.push_back(currentTarget
);
1398 case TARGET_ALL_PARTY_AROUND_CASTER
:
1399 case TARGET_ALL_PARTY_AROUND_CASTER_2
:
1400 case TARGET_ALL_PARTY
:
1402 Player
*pTarget
= m_caster
->GetCharmerOrOwnerPlayerOrPlayerItself();
1403 Group
*pGroup
= pTarget
? pTarget
->GetGroup() : NULL
;
1407 uint8 subgroup
= pTarget
->GetSubGroup();
1409 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1411 Player
* Target
= itr
->getSource();
1413 // IsHostileTo check duel and controlled by enemy
1414 if( Target
&& Target
->GetSubGroup()==subgroup
&& !m_caster
->IsHostileTo(Target
) )
1416 if( m_caster
->IsWithinDistInMap(Target
, radius
) )
1417 TagUnitMap
.push_back(Target
);
1419 if(Pet
* pet
= Target
->GetPet())
1420 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1421 TagUnitMap
.push_back(pet
);
1427 Unit
* ownerOrSelf
= pTarget
? pTarget
: m_caster
->GetCharmerOrOwnerOrSelf();
1428 if(ownerOrSelf
==m_caster
|| m_caster
->IsWithinDistInMap(ownerOrSelf
, radius
))
1429 TagUnitMap
.push_back(ownerOrSelf
);
1430 if(Pet
* pet
= ownerOrSelf
->GetPet())
1431 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1432 TagUnitMap
.push_back(pet
);
1435 case TARGET_RANDOM_RAID_MEMBER
:
1437 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1438 if(Player
* target
= ((Player
*)m_caster
)->GetNextRandomRaidMember(radius
))
1439 TagUnitMap
.push_back(target
);
1441 case TARGET_SINGLE_FRIEND
:
1442 case TARGET_SINGLE_FRIEND_2
:
1444 if(m_targets
.getUnitTarget())
1445 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1447 case TARGET_NONCOMBAT_PET
:
1449 if(Unit
* target
= m_targets
.getUnitTarget())
1450 if( target
->GetTypeId() == TYPEID_UNIT
&& ((Creature
*)target
)->isPet() && ((Pet
*)target
)->getPetType() == MINI_PET
)
1451 TagUnitMap
.push_back(target
);
1453 case TARGET_ALL_AROUND_CASTER
:
1455 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1457 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1460 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_SELF_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1462 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1463 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1465 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1466 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1467 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1469 case TARGET_ALL_FRIENDLY_UNITS_AROUND_CASTER
:
1471 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1473 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1476 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_SELF_CENTER
,SPELL_TARGETS_FRIENDLY
);
1478 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1479 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1481 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1482 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1483 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1485 case TARGET_ALL_FRIENDLY_UNITS_IN_AREA
:
1487 CellPair
p(MaNGOS::ComputeCellPair(m_targets
.m_destX
, m_targets
.m_destY
));
1489 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1492 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_FRIENDLY
);
1494 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1495 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1497 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1498 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1499 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1501 // TARGET_SINGLE_PARTY means that the spells can only be casted on a party member and not on the caster (some seals, fire shield from imp, etc..)
1502 case TARGET_SINGLE_PARTY
:
1504 Unit
*target
= m_targets
.getUnitTarget();
1505 // Thoses spells apparently can't be casted on the caster.
1506 if( target
&& target
!= m_caster
)
1508 // Can only be casted on group's members or its pets
1509 Group
*pGroup
= NULL
;
1511 Unit
* owner
= m_caster
->GetCharmerOrOwner();
1512 Unit
*targetOwner
= target
->GetCharmerOrOwner();
1515 if(owner
->GetTypeId() == TYPEID_PLAYER
)
1517 if( target
== owner
)
1519 TagUnitMap
.push_back(target
);
1522 pGroup
= ((Player
*)owner
)->GetGroup();
1525 else if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1527 if( targetOwner
== m_caster
&& target
->GetTypeId()==TYPEID_UNIT
&& ((Creature
*)target
)->isPet())
1529 TagUnitMap
.push_back(target
);
1532 pGroup
= ((Player
*)m_caster
)->GetGroup();
1537 // Our target can also be a player's pet who's grouped with us or our pet. But can't be controlled player
1540 if( targetOwner
->GetTypeId() == TYPEID_PLAYER
&&
1541 target
->GetTypeId()==TYPEID_UNIT
&& (((Creature
*)target
)->isPet()) &&
1542 target
->GetOwnerGUID()==targetOwner
->GetGUID() &&
1543 pGroup
->IsMember(((Player
*)targetOwner
)->GetGUID()))
1545 TagUnitMap
.push_back(target
);
1548 // 1Our target can be a player who is on our group
1549 else if (target
->GetTypeId() == TYPEID_PLAYER
&& pGroup
->IsMember(((Player
*)target
)->GetGUID()))
1551 TagUnitMap
.push_back(target
);
1556 case TARGET_GAMEOBJECT
:
1558 if(m_targets
.getGOTarget())
1559 AddGOTarget(m_targets
.getGOTarget(), i
);
1561 case TARGET_IN_FRONT_OF_CASTER
:
1563 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1565 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1568 bool inFront
= m_spellInfo
->SpellVisual
[0] != 3879;
1569 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, inFront
? PUSH_IN_FRONT
: PUSH_IN_BACK
,SPELL_TARGETS_AOE_DAMAGE
);
1571 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1572 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1574 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1575 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1576 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1578 case TARGET_DUELVSPLAYER
:
1580 Unit
*target
= m_targets
.getUnitTarget();
1583 if(m_caster
->IsFriendlyTo(target
))
1585 TagUnitMap
.push_back(target
);
1589 Unit
* pUnitTarget
= SelectMagnetTarget();
1591 TagUnitMap
.push_back(pUnitTarget
);
1595 case TARGET_GAMEOBJECT_ITEM
:
1597 if(m_targets
.getGOTargetGUID())
1598 AddGOTarget(m_targets
.getGOTarget(), i
);
1599 else if(m_targets
.getItemTarget())
1600 AddItemTarget(m_targets
.getItemTarget(), i
);
1605 if(Unit
* owner
= m_caster
->GetCharmerOrOwner())
1606 TagUnitMap
.push_back(owner
);
1609 case TARGET_ALL_ENEMY_IN_AREA_CHANNELED
:
1611 // targets the ground, not the units in the area
1612 if (m_spellInfo
->Effect
[i
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
1614 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1616 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1619 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
, PUSH_DEST_CENTER
,SPELL_TARGETS_AOE_DAMAGE
);
1621 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1622 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1624 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1625 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1626 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1631 if(m_spellInfo
->Effect
[i
] != SPELL_EFFECT_DUEL
)
1632 TagUnitMap
.push_back(m_caster
);
1634 case TARGET_SINGLE_ENEMY
:
1636 Unit
* pUnitTarget
= SelectMagnetTarget();
1638 TagUnitMap
.push_back(pUnitTarget
);
1640 case TARGET_AREAEFFECT_PARTY
:
1642 Unit
* owner
= m_caster
->GetCharmerOrOwner();
1643 Player
*pTarget
= NULL
;
1647 TagUnitMap
.push_back(m_caster
);
1648 if(owner
->GetTypeId() == TYPEID_PLAYER
)
1649 pTarget
= (Player
*)owner
;
1651 else if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
1653 if(Unit
* target
= m_targets
.getUnitTarget())
1655 if( target
->GetTypeId() != TYPEID_PLAYER
)
1657 if(((Creature
*)target
)->isPet())
1659 Unit
*targetOwner
= target
->GetOwner();
1660 if(targetOwner
->GetTypeId() == TYPEID_PLAYER
)
1661 pTarget
= (Player
*)targetOwner
;
1665 pTarget
= (Player
*)target
;
1669 Group
* pGroup
= pTarget
? pTarget
->GetGroup() : NULL
;
1673 uint8 subgroup
= pTarget
->GetSubGroup();
1675 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1677 Player
* Target
= itr
->getSource();
1679 // IsHostileTo check duel and controlled by enemy
1680 if(Target
&& Target
->GetSubGroup()==subgroup
&& !m_caster
->IsHostileTo(Target
))
1682 if( pTarget
->IsWithinDistInMap(Target
, radius
) )
1683 TagUnitMap
.push_back(Target
);
1685 if(Pet
* pet
= Target
->GetPet())
1686 if( pTarget
->IsWithinDistInMap(pet
, radius
) )
1687 TagUnitMap
.push_back(pet
);
1693 if(m_caster
->IsWithinDistInMap(owner
, radius
))
1694 TagUnitMap
.push_back(owner
);
1698 TagUnitMap
.push_back(pTarget
);
1700 if(Pet
* pet
= pTarget
->GetPet())
1701 if( m_caster
->IsWithinDistInMap(pet
, radius
) )
1702 TagUnitMap
.push_back(pet
);
1708 if(m_targets
.getUnitTarget())
1709 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1710 if(m_targets
.getItemTarget())
1711 AddItemTarget(m_targets
.getItemTarget(), i
);
1713 case TARGET_SELF_FISHING
:
1715 TagUnitMap
.push_back(m_caster
);
1717 case TARGET_CHAIN_HEAL
:
1719 Unit
* pUnitTarget
= m_targets
.getUnitTarget();
1723 if (EffectChainTarget
<= 1)
1724 TagUnitMap
.push_back(pUnitTarget
);
1727 unMaxTargets
= EffectChainTarget
;
1728 float max_range
= radius
+ unMaxTargets
* CHAIN_SPELL_JUMP_RADIUS
;
1730 std::list
<Unit
*> tempUnitMap
;
1733 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1735 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1738 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, tempUnitMap
, max_range
, PUSH_SELF_CENTER
, SPELL_TARGETS_FRIENDLY
);
1740 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_object_notifier(notifier
);
1741 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_object_notifier(notifier
);
1743 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1744 cell_lock
->Visit(cell_lock
, world_object_notifier
, *m_caster
->GetMap());
1745 cell_lock
->Visit(cell_lock
, grid_object_notifier
, *m_caster
->GetMap());
1749 if(m_caster
!= pUnitTarget
&& std::find(tempUnitMap
.begin(),tempUnitMap
.end(),m_caster
) == tempUnitMap
.end() )
1750 tempUnitMap
.push_front(m_caster
);
1752 tempUnitMap
.sort(TargetDistanceOrder(pUnitTarget
));
1754 if(tempUnitMap
.empty())
1757 if(*tempUnitMap
.begin() == pUnitTarget
)
1758 tempUnitMap
.erase(tempUnitMap
.begin());
1760 TagUnitMap
.push_back(pUnitTarget
);
1761 uint32 t
= unMaxTargets
- 1;
1762 Unit
*prev
= pUnitTarget
;
1763 std::list
<Unit
*>::iterator next
= tempUnitMap
.begin();
1765 while(t
&& next
!= tempUnitMap
.end() )
1767 if(prev
->GetDistance(*next
) > CHAIN_SPELL_JUMP_RADIUS
)
1770 if(!prev
->IsWithinLOSInMap(*next
))
1776 if((*next
)->GetHealth() == (*next
)->GetMaxHealth())
1778 next
= tempUnitMap
.erase(next
);
1783 TagUnitMap
.push_back(prev
);
1784 tempUnitMap
.erase(next
);
1785 tempUnitMap
.sort(TargetDistanceOrder(prev
));
1786 next
= tempUnitMap
.begin();
1792 case TARGET_CURRENT_ENEMY_COORDINATES
:
1794 Unit
* currentTarget
= m_targets
.getUnitTarget();
1797 TagUnitMap
.push_back(currentTarget
);
1798 m_targets
.setDestination(currentTarget
->GetPositionX(), currentTarget
->GetPositionY(), currentTarget
->GetPositionZ());
1799 if(m_spellInfo
->EffectImplicitTargetB
[i
]==TARGET_ALL_ENEMY_IN_AREA_INSTANT
)
1801 CellPair
p(MaNGOS::ComputeCellPair(currentTarget
->GetPositionX(), currentTarget
->GetPositionY()));
1803 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1805 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
,PUSH_TARGET_CENTER
, SPELL_TARGETS_AOE_DAMAGE
);
1806 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_notifier(notifier
);
1807 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_notifier(notifier
);
1808 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1809 cell_lock
->Visit(cell_lock
, world_notifier
, *m_caster
->GetMap());
1810 cell_lock
->Visit(cell_lock
, grid_notifier
, *m_caster
->GetMap());
1814 case TARGET_AREAEFFECT_PARTY_AND_CLASS
:
1816 Player
* targetPlayer
= m_targets
.getUnitTarget() && m_targets
.getUnitTarget()->GetTypeId() == TYPEID_PLAYER
1817 ? (Player
*)m_targets
.getUnitTarget() : NULL
;
1819 Group
* pGroup
= targetPlayer
? targetPlayer
->GetGroup() : NULL
;
1822 for(GroupReference
*itr
= pGroup
->GetFirstMember(); itr
!= NULL
; itr
= itr
->next())
1824 Player
* Target
= itr
->getSource();
1826 // IsHostileTo check duel and controlled by enemy
1827 if( Target
&& targetPlayer
->IsWithinDistInMap(Target
, radius
) &&
1828 targetPlayer
->getClass() == Target
->getClass() &&
1829 !m_caster
->IsHostileTo(Target
) )
1831 TagUnitMap
.push_back(Target
);
1835 else if(m_targets
.getUnitTarget())
1836 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1839 case TARGET_TABLE_X_Y_Z_COORDINATES
:
1841 SpellTargetPosition
const* st
= spellmgr
.GetSpellTargetPosition(m_spellInfo
->Id
);
1844 if (st
->target_mapId
== m_caster
->GetMapId())
1845 m_targets
.setDestination(st
->target_X
, st
->target_Y
, st
->target_Z
);
1847 // if B==TARGET_TABLE_X_Y_Z_COORDINATES then A already fill all required targets
1848 if (m_spellInfo
->EffectImplicitTargetB
[i
] && m_spellInfo
->EffectImplicitTargetB
[i
]!=TARGET_TABLE_X_Y_Z_COORDINATES
)
1850 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
1852 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1855 SpellTargets targetB
= SPELL_TARGETS_AOE_DAMAGE
;
1856 // Select friendly targets for positive effect
1857 if (IsPositiveEffect(m_spellInfo
->Id
, i
))
1858 targetB
= SPELL_TARGETS_FRIENDLY
;
1860 MaNGOS::SpellNotifierCreatureAndPlayer
notifier(*this, TagUnitMap
, radius
,PUSH_DEST_CENTER
, targetB
);
1862 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, WorldTypeMapContainer
> world_notifier(notifier
);
1863 TypeContainerVisitor
<MaNGOS::SpellNotifierCreatureAndPlayer
, GridTypeMapContainer
> grid_notifier(notifier
);
1865 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1866 cell_lock
->Visit(cell_lock
, world_notifier
, *m_caster
->GetMap());
1867 cell_lock
->Visit(cell_lock
, grid_notifier
, *m_caster
->GetMap());
1871 sLog
.outError( "SPELL: unknown target coordinates for spell ID %u\n", m_spellInfo
->Id
);
1873 case TARGET_BEHIND_VICTIM
:
1875 Unit
*pTarget
= m_caster
->getVictim();
1876 if(!pTarget
&& m_caster
->GetTypeId() == TYPEID_PLAYER
)
1877 pTarget
= ObjectAccessor::GetUnit(*m_caster
, ((Player
*)m_caster
)->GetSelection());
1881 float _target_x
, _target_y
, _target_z
;
1882 pTarget
->GetClosePoint(_target_x
, _target_y
, _target_z
, m_caster
->GetObjectSize(), CONTACT_DISTANCE
, M_PI
);
1883 if(pTarget
->IsWithinLOS(_target_x
,_target_y
,_target_z
))
1884 m_targets
.setDestination(_target_x
, _target_y
, _target_z
);
1891 if (unMaxTargets
&& TagUnitMap
.size() > unMaxTargets
)
1893 // make sure one unit is always removed per iteration
1894 uint32 removed_utarget
= 0;
1895 for (std::list
<Unit
*>::iterator itr
= TagUnitMap
.begin(), next
; itr
!= TagUnitMap
.end(); itr
= next
)
1899 if (!*itr
) continue;
1900 if ((*itr
) == m_targets
.getUnitTarget())
1902 TagUnitMap
.erase(itr
);
1903 removed_utarget
= 1;
1907 // remove random units from the map
1908 while (TagUnitMap
.size() > unMaxTargets
- removed_utarget
)
1910 uint32 poz
= urand(0, TagUnitMap
.size()-1);
1911 for (std::list
<Unit
*>::iterator itr
= TagUnitMap
.begin(); itr
!= TagUnitMap
.end(); ++itr
, --poz
)
1913 if (!*itr
) continue;
1916 TagUnitMap
.erase(itr
);
1921 // the player's target will always be added to the map
1922 if (removed_utarget
&& m_targets
.getUnitTarget())
1923 TagUnitMap
.push_back(m_targets
.getUnitTarget());
1927 void Spell::prepare(SpellCastTargets
* targets
, Aura
* triggeredByAura
)
1929 m_targets
= *targets
;
1931 m_spellState
= SPELL_STATE_PREPARING
;
1933 m_castPositionX
= m_caster
->GetPositionX();
1934 m_castPositionY
= m_caster
->GetPositionY();
1935 m_castPositionZ
= m_caster
->GetPositionZ();
1936 m_castOrientation
= m_caster
->GetOrientation();
1939 m_triggeredByAuraSpell
= triggeredByAura
->GetSpellProto();
1941 // create and add update event for this spell
1942 SpellEvent
* Event
= new SpellEvent(this);
1943 m_caster
->m_Events
.AddEvent(Event
, m_caster
->m_Events
.CalculateTime(1));
1945 //Prevent casting at cast another spell (ServerSide check)
1946 if(m_caster
->IsNonMeleeSpellCasted(false, true) && m_cast_count
)
1948 SendCastResult(SPELL_FAILED_SPELL_IN_PROGRESS
);
1954 m_powerCost
= CalculatePowerCost();
1956 uint8 result
= CanCast(true);
1957 if(result
!= 0 && !IsAutoRepeat()) //always cast autorepeat dummy for triggering
1961 SendChannelUpdate(0);
1962 triggeredByAura
->SetAuraDuration(0);
1964 SendCastResult(result
);
1969 // calculate cast time (calculated after first CanCast check to prevent charge counting for first CanCast fail)
1970 m_casttime
= GetSpellCastTime(m_spellInfo
, this);
1972 // set timer base at cast time
1975 // stealth must be removed at cast starting (at show channel bar)
1976 // skip triggered spell (item equip spell casting and other not explicit character casts/item uses)
1977 if ( !m_IsTriggeredSpell
&& isSpellBreakStealth(m_spellInfo
) )
1979 m_caster
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
1980 m_caster
->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH
);
1983 if(m_IsTriggeredSpell
)
1987 m_caster
->SetCurrentCastedSpell( this );
1988 m_selfContainer
= &(m_caster
->m_currentSpells
[GetCurrentContainer()]);
1993 void Spell::cancel()
1995 if(m_spellState
== SPELL_STATE_FINISHED
)
1998 m_autoRepeat
= false;
1999 switch (m_spellState
)
2001 case SPELL_STATE_PREPARING
:
2002 case SPELL_STATE_DELAYED
:
2005 SendCastResult(SPELL_FAILED_INTERRUPTED
);
2008 case SPELL_STATE_CASTING
:
2010 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2012 if( ihit
->missCondition
== SPELL_MISS_NONE
)
2014 Unit
* unit
= m_caster
->GetGUID()==(*ihit
).targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
2015 if( unit
&& unit
->isAlive() )
2016 unit
->RemoveAurasDueToSpell(m_spellInfo
->Id
);
2020 m_caster
->RemoveAurasDueToSpell(m_spellInfo
->Id
);
2021 SendChannelUpdate(0);
2023 SendCastResult(SPELL_FAILED_INTERRUPTED
);
2032 m_caster
->RemoveDynObject(m_spellInfo
->Id
);
2033 m_caster
->RemoveGameObject(m_spellInfo
->Id
,true);
2036 void Spell::cast(bool skipCheck
)
2038 SetExecutedCurrently(true);
2040 uint8 castResult
= 0;
2042 // update pointers base at GUIDs to prevent access to non-existed already object
2045 // cancel at lost main target unit
2046 if(!m_targets
.getUnitTarget() && m_targets
.getUnitTargetGUID() && m_targets
.getUnitTargetGUID() != m_caster
->GetGUID())
2049 SetExecutedCurrently(false);
2053 if(m_caster
->GetTypeId() != TYPEID_PLAYER
&& m_targets
.getUnitTarget() && m_targets
.getUnitTarget() != m_caster
)
2054 m_caster
->SetInFront(m_targets
.getUnitTarget());
2056 castResult
= CheckPower();
2059 SendCastResult(castResult
);
2061 SetExecutedCurrently(false);
2065 // triggered cast called from Spell::prepare where it was already checked
2068 castResult
= CanCast(false);
2071 SendCastResult(castResult
);
2073 SetExecutedCurrently(false);
2078 // Conflagrate - consumes immolate
2079 if ((m_spellInfo
->TargetAuraState
== AURA_STATE_IMMOLATE
) && m_targets
.getUnitTarget())
2081 // for caster applied auras only
2082 Unit::AuraList
const &mPeriodic
= m_targets
.getUnitTarget()->GetAurasByType(SPELL_AURA_PERIODIC_DAMAGE
);
2083 for(Unit::AuraList::const_iterator i
= mPeriodic
.begin(); i
!= mPeriodic
.end(); ++i
)
2085 if( (*i
)->GetSpellProto()->SpellFamilyName
== SPELLFAMILY_WARLOCK
&& ((*i
)->GetSpellProto()->SpellFamilyFlags
& 4) &&
2086 (*i
)->GetCasterGUID()==m_caster
->GetGUID() )
2088 m_targets
.getUnitTarget()->RemoveAura((*i
)->GetId(), (*i
)->GetEffIndex());
2094 // traded items have trade slot instead of guid in m_itemTargetGUID
2095 // set to real guid to be sent later to the client
2096 m_targets
.updateTradeSlotItem();
2098 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2101 ((Player
*)m_caster
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_USE_ITEM
, m_CastItem
->GetEntry());
2103 ((Player
*)m_caster
)->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_CAST_SPELL
, m_spellInfo
->Id
);
2107 SendSpellCooldown();
2110 TakeReagents(); // we must remove reagents before HandleEffects to allow place crafted item in same slot
2113 if(m_spellState
== SPELL_STATE_FINISHED
) // stop cast if spell marked as finish somewhere in Take*/FillTargetMap
2115 SetExecutedCurrently(false);
2119 SendCastResult(castResult
);
2120 SendSpellGo(); // we must send smsg_spell_go packet before m_castItem delete in TakeCastItem()...
2122 // Pass cast spell event to handler (not send triggered by aura spells)
2123 if (m_spellInfo
->DmgClass
!= SPELL_DAMAGE_CLASS_MELEE
&& m_spellInfo
->DmgClass
!= SPELL_DAMAGE_CLASS_RANGED
&& !m_triggeredByAuraSpell
)
2125 m_caster
->ProcDamageAndSpell(m_targets
.getUnitTarget(), PROC_FLAG_CAST_SPELL
, PROC_FLAG_NONE
, 0, SPELL_SCHOOL_MASK_NONE
, m_spellInfo
, m_IsTriggeredSpell
);
2127 // update pointers base at GUIDs to prevent access to non-existed already object
2128 UpdatePointers(); // pointers can be invalidate at triggered spell casting
2131 // Okay, everything is prepared. Now we need to distinguish between immediate and evented delayed spells
2132 if (m_spellInfo
->speed
> 0.0f
)
2135 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2136 // in case delayed spell remove item at cast delay start
2139 // Okay, maps created, now prepare flags
2140 m_immediateHandled
= false;
2141 m_spellState
= SPELL_STATE_DELAYED
;
2146 // Immediate spell, no big deal
2150 SetExecutedCurrently(false);
2153 void Spell::handle_immediate()
2155 // start channeling if applicable
2156 if(IsChanneledSpell(m_spellInfo
))
2158 m_spellState
= SPELL_STATE_CASTING
;
2159 SendChannelStart(GetSpellDuration(m_spellInfo
));
2162 // process immediate effects (items, ground, etc.) also initialize some variables
2163 _handle_immediate_phase();
2165 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2166 DoAllEffectOnTarget(&(*ihit
));
2168 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
2169 DoAllEffectOnTarget(&(*ihit
));
2171 // spell is finished, perform some last features of the spell here
2172 _handle_finish_phase();
2174 // Remove used for cast item if need (it can be already NULL after TakeReagents call
2177 if(m_spellState
!= SPELL_STATE_CASTING
)
2178 finish(true); // successfully finish spell cast (not last in case autorepeat or channel spell)
2181 uint64
Spell::handle_delayed(uint64 t_offset
)
2183 uint64 next_time
= 0;
2185 if (!m_immediateHandled
)
2187 _handle_immediate_phase();
2188 m_immediateHandled
= true;
2191 // 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)
2192 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin(); ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2194 if (ihit
->processed
== false)
2196 if ( ihit
->timeDelay
<= t_offset
)
2197 DoAllEffectOnTarget(&(*ihit
));
2198 else if( next_time
== 0 || ihit
->timeDelay
< next_time
)
2199 next_time
= ihit
->timeDelay
;
2203 // now recheck gameobject targeting correctness
2204 for(std::list
<GOTargetInfo
>::iterator ighit
= m_UniqueGOTargetInfo
.begin(); ighit
!= m_UniqueGOTargetInfo
.end();++ighit
)
2206 if (ighit
->processed
== false)
2208 if ( ighit
->timeDelay
<= t_offset
)
2209 DoAllEffectOnTarget(&(*ighit
));
2210 else if( next_time
== 0 || ighit
->timeDelay
< next_time
)
2211 next_time
= ighit
->timeDelay
;
2214 // All targets passed - need finish phase
2217 // spell is finished, perform some last features of the spell here
2218 _handle_finish_phase();
2220 finish(true); // successfully finish spell cast
2222 // return zero, spell is finished now
2227 // spell is unfinished, return next execution time
2232 void Spell::_handle_immediate_phase()
2234 // handle some immediate features of the spell here
2235 HandleThreatSpells(m_spellInfo
->Id
);
2237 m_needSpellLog
= IsNeedSendToClient();
2238 for(uint32 j
= 0;j
<3;j
++)
2240 if(m_spellInfo
->Effect
[j
]==0)
2243 // apply Send Event effect to ground in case empty target lists
2244 if( m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SEND_EVENT
&& !HaveTargetsForEffect(j
) )
2246 HandleEffects(NULL
,NULL
,NULL
, j
);
2250 // Don't do spell log, if is school damage spell
2251 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_SCHOOL_DAMAGE
|| m_spellInfo
->Effect
[j
] == 0)
2252 m_needSpellLog
= false;
2254 uint32 EffectChainTarget
= m_spellInfo
->EffectChainTarget
[j
];
2255 if(m_originalCaster
)
2256 if(Player
* modOwner
= m_originalCaster
->GetSpellModOwner())
2257 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_JUMP_TARGETS
, EffectChainTarget
, this);
2259 // initialize multipliers
2260 m_damageMultipliers
[j
] = 1.0f
;
2261 if( (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_DAMAGE
|| m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_CHAIN_HEAL
) &&
2262 (EffectChainTarget
> 1) )
2263 m_applyMultiplierMask
|= 1 << j
;
2266 // initialize Diminishing Returns Data
2267 m_diminishLevel
= DIMINISHING_LEVEL_1
;
2268 m_diminishGroup
= DIMINISHING_NONE
;
2271 for(std::list
<ItemTargetInfo
>::iterator ihit
= m_UniqueItemInfo
.begin();ihit
!= m_UniqueItemInfo
.end();++ihit
)
2272 DoAllEffectOnTarget(&(*ihit
));
2275 for(uint32 j
= 0;j
<3;j
++)
2277 // persistent area auras target only the ground
2278 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_PERSISTENT_AREA_AURA
)
2279 HandleEffects(NULL
,NULL
,NULL
, j
);
2283 void Spell::_handle_finish_phase()
2290 void Spell::SendSpellCooldown()
2292 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
2295 Player
* _player
= (Player
*)m_caster
;
2296 // Add cooldown for max (disable spell)
2297 // Cooldown started on SendCooldownEvent call
2298 if (m_spellInfo
->Attributes
& SPELL_ATTR_DISABLED_WHILE_ACTIVE
)
2300 _player
->AddSpellCooldown(m_spellInfo
->Id
, 0, time(NULL
) - 1);
2304 // init cooldown values
2309 // some special item spells without correct cooldown in SpellInfo
2310 // cooldown information stored in item prototype
2311 // This used in same way in WorldSession::HandleItemQuerySingleOpcode data sending to client.
2315 ItemPrototype
const* proto
= m_CastItem
->GetProto();
2318 for(int idx
= 0; idx
< 5; ++idx
)
2320 if(proto
->Spells
[idx
].SpellId
== m_spellInfo
->Id
)
2322 cat
= proto
->Spells
[idx
].SpellCategory
;
2323 rec
= proto
->Spells
[idx
].SpellCooldown
;
2324 catrec
= proto
->Spells
[idx
].SpellCategoryCooldown
;
2331 // if no cooldown found above then base at DBC data
2332 if(rec
< 0 && catrec
< 0)
2334 cat
= m_spellInfo
->Category
;
2335 rec
= m_spellInfo
->RecoveryTime
;
2336 catrec
= m_spellInfo
->CategoryRecoveryTime
;
2339 // shoot spells used equipped item cooldown values already assigned in GetAttackTime(RANGED_ATTACK)
2340 // prevent 0 cooldowns set by another way
2341 if (rec
<= 0 && catrec
<= 0 && (cat
== 76 || cat
== 351))
2342 rec
= _player
->GetAttackTime(RANGED_ATTACK
);
2344 // Now we have cooldown data (if found any), time to apply mods
2346 _player
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COOLDOWN
, rec
, this);
2349 _player
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COOLDOWN
, catrec
, this);
2351 // replace negative cooldowns by 0
2352 if (rec
< 0) rec
= 0;
2353 if (catrec
< 0) catrec
= 0;
2355 // no cooldown after applying spell mods
2356 if( rec
== 0 && catrec
== 0)
2359 time_t curTime
= time(NULL
);
2361 time_t catrecTime
= catrec
? curTime
+catrec
/1000 : 0; // in secs
2362 time_t recTime
= rec
? curTime
+rec
/1000 : catrecTime
;// in secs
2364 // self spell cooldown
2366 _player
->AddSpellCooldown(m_spellInfo
->Id
, m_CastItem
? m_CastItem
->GetEntry() : 0, recTime
);
2371 SpellCategoryStore::const_iterator i_scstore
= sSpellCategoryStore
.find(cat
);
2372 if(i_scstore
!= sSpellCategoryStore
.end())
2374 for(SpellCategorySet::const_iterator i_scset
= i_scstore
->second
.begin(); i_scset
!= i_scstore
->second
.end(); ++i_scset
)
2376 if(*i_scset
== m_spellInfo
->Id
) // skip main spell, already handled above
2379 _player
->AddSpellCooldown(m_spellInfo
->Id
, m_CastItem
? m_CastItem
->GetEntry() : 0, catrecTime
);
2385 void Spell::update(uint32 difftime
)
2387 // update pointers based at it's GUIDs
2390 if(m_targets
.getUnitTargetGUID() && !m_targets
.getUnitTarget())
2396 // check if the player caster has moved before the spell finished
2397 if ((m_caster
->GetTypeId() == TYPEID_PLAYER
&& m_timer
!= 0) &&
2398 (m_castPositionX
!= m_caster
->GetPositionX() || m_castPositionY
!= m_caster
->GetPositionY() || m_castPositionZ
!= m_caster
->GetPositionZ()) &&
2399 (m_spellInfo
->Effect
[0] != SPELL_EFFECT_STUCK
|| !m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_FALLING
)))
2401 // always cancel for channeled spells
2402 if( m_spellState
== SPELL_STATE_CASTING
)
2404 // don't cancel for melee, autorepeat, triggered and instant spells
2405 else if(!IsNextMeleeSwingSpell() && !IsAutoRepeat() && !m_IsTriggeredSpell
&& (m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_MOVEMENT
))
2409 switch(m_spellState
)
2411 case SPELL_STATE_PREPARING
:
2415 if(difftime
>= m_timer
)
2418 m_timer
-= difftime
;
2421 if(m_timer
== 0 && !IsNextMeleeSwingSpell() && !IsAutoRepeat())
2424 case SPELL_STATE_CASTING
:
2428 if( m_caster
->GetTypeId() == TYPEID_PLAYER
)
2430 // check if player has jumped before the channeling finished
2431 if(m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_JUMPING
))
2434 // check for incapacitating player states
2435 if( m_caster
->hasUnitState(UNIT_STAT_STUNNED
| UNIT_STAT_CONFUSED
))
2438 // check if player has turned if flag is set
2439 if( m_spellInfo
->ChannelInterruptFlags
& CHANNEL_FLAG_TURNING
&& m_castOrientation
!= m_caster
->GetOrientation() )
2443 // check if there are alive targets left
2444 if (!IsAliveUnitPresentInTargetList())
2446 SendChannelUpdate(0);
2450 if(difftime
>= m_timer
)
2453 m_timer
-= difftime
;
2458 SendChannelUpdate(0);
2460 // channeled spell processed independently for quest targeting
2461 // cast at creature (or GO) quest objectives update at successful cast channel finished
2462 // ignore autorepeat/melee casts for speed (not exist quest for spells (hm... )
2463 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsAutoRepeat() && !IsNextMeleeSwingSpell() )
2465 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2467 TargetInfo
* target
= &*ihit
;
2468 if(!IS_CREATURE_GUID(target
->targetGUID
))
2471 Unit
* unit
= m_caster
->GetGUID()==target
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
,target
->targetGUID
);
2475 ((Player
*)m_caster
)->CastedCreatureOrGO(unit
->GetEntry(),unit
->GetGUID(),m_spellInfo
->Id
);
2478 for(std::list
<GOTargetInfo
>::iterator ihit
= m_UniqueGOTargetInfo
.begin();ihit
!= m_UniqueGOTargetInfo
.end();++ihit
)
2480 GOTargetInfo
* target
= &*ihit
;
2482 GameObject
* go
= ObjectAccessor::GetGameObject(*m_caster
, target
->targetGUID
);
2486 ((Player
*)m_caster
)->CastedCreatureOrGO(go
->GetEntry(),go
->GetGUID(),m_spellInfo
->Id
);
2499 void Spell::finish(bool ok
)
2504 if(m_spellState
== SPELL_STATE_FINISHED
)
2507 m_spellState
= SPELL_STATE_FINISHED
;
2510 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2511 ((Player
*)m_caster
)->RemoveSpellMods(this);
2513 // other code related only to successfully finished spells
2517 //handle SPELL_AURA_ADD_TARGET_TRIGGER auras
2518 Unit::AuraList
const& targetTriggers
= m_caster
->GetAurasByType(SPELL_AURA_ADD_TARGET_TRIGGER
);
2519 for(Unit::AuraList::const_iterator i
= targetTriggers
.begin(); i
!= targetTriggers
.end(); ++i
)
2521 SpellEntry
const *auraSpellInfo
= (*i
)->GetSpellProto();
2522 uint32 auraSpellIdx
= (*i
)->GetEffIndex();
2523 if (IsAffectedBy(auraSpellInfo
, auraSpellIdx
))
2525 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2526 if( ihit
->effectMask
& (1<<auraSpellIdx
) )
2528 // check m_caster->GetGUID() let load auras at login and speedup most often case
2529 Unit
*unit
= m_caster
->GetGUID()== ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
2530 if (unit
&& unit
->isAlive())
2532 // Calculate chance at that moment (can be depend for example from combo points)
2533 int32 chance
= m_caster
->CalculateSpellDamage(auraSpellInfo
, auraSpellIdx
, (*i
)->GetBasePoints(),unit
);
2535 if(roll_chance_i(chance
))
2536 m_caster
->CastSpell(unit
, auraSpellInfo
->EffectTriggerSpell
[auraSpellIdx
], true, NULL
, (*i
));
2542 if (IsMeleeAttackResetSpell())
2544 m_caster
->resetAttackTimer(BASE_ATTACK
);
2545 if(m_caster
->haveOffhandWeapon())
2546 m_caster
->resetAttackTimer(OFF_ATTACK
);
2549 /*if (IsRangedAttackResetSpell())
2550 m_caster->resetAttackTimer(RANGED_ATTACK);*/
2552 // Clear combo at finish state
2553 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& NeedsComboPoints(m_spellInfo
))
2555 // Not drop combopoints if any miss exist
2556 bool needDrop
= true;
2557 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2558 if (ihit
->missCondition
!= SPELL_MISS_NONE
)
2564 ((Player
*)m_caster
)->ClearComboPoints();
2567 // call triggered spell only at successful cast (after clear combo points -> for add some if need)
2568 if(!m_TriggerSpells
.empty())
2571 // Stop Attack for some spells
2572 if( m_spellInfo
->Attributes
& SPELL_ATTR_STOP_ATTACK_TARGET
)
2573 m_caster
->AttackStop();
2576 void Spell::SendCastResult(uint8 result
)
2578 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2581 if(((Player
*)m_caster
)->GetSession()->PlayerLoading()) // don't send cast results at loading time
2586 WorldPacket
data(SMSG_CAST_FAILED
, (4+1+1));
2587 data
<< uint8(m_cast_count
); // single cast or multi 2.3 (0/1)
2588 data
<< uint32(m_spellInfo
->Id
);
2589 data
<< uint8(result
); // problem
2592 case SPELL_FAILED_REQUIRES_SPELL_FOCUS
:
2593 data
<< uint32(m_spellInfo
->RequiresSpellFocus
);
2595 case SPELL_FAILED_REQUIRES_AREA
:
2596 // hardcode areas limitation case
2597 switch(m_spellInfo
->Id
)
2599 case 41617: // Cenarion Mana Salve
2600 case 41619: // Cenarion Healing Salve
2601 data
<< uint32(3905);
2603 case 41618: // Bottled Nethergon Energy
2604 case 41620: // Bottled Nethergon Vapor
2605 data
<< uint32(3842);
2607 case 45373: // Bloodberry Elixir
2608 data
<< uint32(4075);
2610 default: // default case
2611 data
<< uint32(m_spellInfo
->AreaId
);
2615 case SPELL_FAILED_TOTEMS
:
2616 if(m_spellInfo
->Totem
[0])
2617 data
<< uint32(m_spellInfo
->Totem
[0]);
2618 if(m_spellInfo
->Totem
[1])
2619 data
<< uint32(m_spellInfo
->Totem
[1]);
2621 case SPELL_FAILED_TOTEM_CATEGORY
:
2622 if(m_spellInfo
->TotemCategory
[0])
2623 data
<< uint32(m_spellInfo
->TotemCategory
[0]);
2624 if(m_spellInfo
->TotemCategory
[1])
2625 data
<< uint32(m_spellInfo
->TotemCategory
[1]);
2627 case SPELL_FAILED_EQUIPPED_ITEM_CLASS
:
2628 data
<< uint32(m_spellInfo
->EquippedItemClass
);
2629 data
<< uint32(m_spellInfo
->EquippedItemSubClassMask
);
2630 //data << uint32(m_spellInfo->EquippedItemInventoryTypeMask);
2633 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
2637 WorldPacket data(SMSG_CLEAR_EXTRA_AURA_INFO_OBSOLETE, (8+4));
2638 data.append(m_caster->GetPackGUID());
2639 data << uint32(m_spellInfo->Id);
2640 ((Player*)m_caster)->GetSession()->SendPacket(&data);
2644 void Spell::SendSpellStart()
2646 if(!IsNeedSendToClient())
2649 sLog
.outDebug("Sending SMSG_SPELL_START id=%u", m_spellInfo
->Id
);
2651 uint32 castFlags
= CAST_FLAG_UNKNOWN1
;
2653 castFlags
|= CAST_FLAG_AMMO
;
2656 if(!m_targets
.getUnitTarget())
2659 target
= m_targets
.getUnitTarget();
2661 WorldPacket
data(SMSG_SPELL_START
, (8+8+4+4+2));
2663 data
.append(m_CastItem
->GetPackGUID());
2665 data
.append(m_caster
->GetPackGUID());
2667 data
.append(m_caster
->GetPackGUID());
2668 data
<< uint8(m_cast_count
); // pending spell cast?
2669 data
<< uint32(m_spellInfo
->Id
);
2670 data
<< uint32(castFlags
);
2671 data
<< uint32(m_timer
);
2673 m_targets
.write(&data
);
2675 if ( castFlags
& CAST_FLAG_UNKNOWN6
)
2678 if ( castFlags
& CAST_FLAG_UNKNOWN7
)
2684 for(uint8 i
= 0; i
< 6; ++i
)
2690 if ( castFlags
& CAST_FLAG_AMMO
)
2691 WriteAmmoToPacket(&data
);
2693 m_caster
->SendMessageToSet(&data
, true);
2696 void Spell::SendSpellGo()
2698 // not send invisible spell casting
2699 if(!IsNeedSendToClient())
2702 sLog
.outDebug("Sending SMSG_SPELL_GO id=%u",m_spellInfo
->Id
);
2705 if(!m_targets
.getUnitTarget())
2708 target
= m_targets
.getUnitTarget();
2710 uint32 castFlags
= CAST_FLAG_UNKNOWN3
;
2712 castFlags
|= CAST_FLAG_AMMO
;
2714 WorldPacket
data(SMSG_SPELL_GO
, 50); // guess size
2716 data
.append(m_CastItem
->GetPackGUID());
2718 data
.append(m_caster
->GetPackGUID());
2720 data
.append(m_caster
->GetPackGUID());
2721 data
<< uint8(m_cast_count
); // pending spell cast?
2722 data
<< uint32(m_spellInfo
->Id
);
2723 data
<< uint32(castFlags
);
2724 data
<< uint32(getMSTime()); // timestamp
2726 WriteSpellGoTargets(&data
);
2728 m_targets
.write(&data
);
2730 if ( castFlags
& CAST_FLAG_UNKNOWN6
) // unknown wotlk
2733 if ( castFlags
& CAST_FLAG_UNKNOWN7
)
2739 for(uint8 i
= 0; i
< 6; ++i
)
2745 if ( castFlags
& CAST_FLAG_UNKNOWN4
) // unknown wotlk
2751 if ( castFlags
& CAST_FLAG_AMMO
)
2752 WriteAmmoToPacket(&data
);
2754 if ( castFlags
& CAST_FLAG_UNKNOWN5
) // unknown wotlk
2760 if ( m_targets
.m_targetMask
& TARGET_FLAG_DEST_LOCATION
)
2765 m_caster
->SendMessageToSet(&data
, true);
2768 void Spell::WriteAmmoToPacket( WorldPacket
* data
)
2770 uint32 ammoInventoryType
= 0;
2771 uint32 ammoDisplayID
= 0;
2773 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
2775 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack( RANGED_ATTACK
);
2778 ammoInventoryType
= pItem
->GetProto()->InventoryType
;
2779 if( ammoInventoryType
== INVTYPE_THROWN
)
2780 ammoDisplayID
= pItem
->GetProto()->DisplayInfoID
;
2783 uint32 ammoID
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
2786 ItemPrototype
const *pProto
= objmgr
.GetItemPrototype( ammoID
);
2789 ammoDisplayID
= pProto
->DisplayInfoID
;
2790 ammoInventoryType
= pProto
->InventoryType
;
2793 else if(m_caster
->GetDummyAura(46699)) // Requires No Ammo
2795 ammoDisplayID
= 5996; // normal arrow
2796 ammoInventoryType
= INVTYPE_AMMO
;
2801 // TODO: implement selection ammo data based at ranged weapon stored in equipmodel/equipinfo/equipslot fields
2803 *data
<< uint32(ammoDisplayID
);
2804 *data
<< uint32(ammoInventoryType
);
2807 void Spell::WriteSpellGoTargets( WorldPacket
* data
)
2809 *data
<< (uint8
)m_countOfHit
;
2810 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2811 if ((*ihit
).missCondition
== SPELL_MISS_NONE
) // Add only hits
2812 *data
<< uint64(ihit
->targetGUID
);
2814 for(std::list
<GOTargetInfo
>::iterator ighit
= m_UniqueGOTargetInfo
.begin();ighit
!= m_UniqueGOTargetInfo
.end();++ighit
)
2815 *data
<< uint64(ighit
->targetGUID
); // Always hits
2817 *data
<< (uint8
)m_countOfMiss
;
2818 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
2820 if( ihit
->missCondition
!= SPELL_MISS_NONE
) // Add only miss
2822 *data
<< uint64(ihit
->targetGUID
);
2823 *data
<< uint8(ihit
->missCondition
);
2824 if( ihit
->missCondition
== SPELL_MISS_REFLECT
)
2825 *data
<< uint8(ihit
->reflectResult
);
2830 void Spell::SendLogExecute()
2832 Unit
*target
= m_targets
.getUnitTarget() ? m_targets
.getUnitTarget() : m_caster
;
2834 WorldPacket
data(SMSG_SPELLLOGEXECUTE
, (8+4+4+4+4+8));
2836 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
2837 data
.append(m_caster
->GetPackGUID());
2839 data
.append(target
->GetPackGUID());
2841 data
<< uint32(m_spellInfo
->Id
);
2843 data
<< uint32(count1
); // count1 (effect count?)
2844 for(uint32 i
= 0; i
< count1
; ++i
)
2846 data
<< uint32(m_spellInfo
->Effect
[0]); // spell effect
2848 data
<< uint32(count2
); // count2 (target count?)
2849 for(uint32 j
= 0; j
< count2
; ++j
)
2851 switch(m_spellInfo
->Effect
[0])
2853 case SPELL_EFFECT_POWER_DRAIN
:
2854 if(Unit
*unit
= m_targets
.getUnitTarget())
2855 data
.append(unit
->GetPackGUID());
2862 case SPELL_EFFECT_ADD_EXTRA_ATTACKS
:
2863 if(Unit
*unit
= m_targets
.getUnitTarget())
2864 data
.append(unit
->GetPackGUID());
2867 data
<< uint32(0); // count?
2869 case SPELL_EFFECT_INTERRUPT_CAST
:
2870 if(Unit
*unit
= m_targets
.getUnitTarget())
2871 data
.append(unit
->GetPackGUID());
2874 data
<< uint32(0); // spellid
2876 case SPELL_EFFECT_DURABILITY_DAMAGE
:
2877 if(Unit
*unit
= m_targets
.getUnitTarget())
2878 data
.append(unit
->GetPackGUID());
2884 case SPELL_EFFECT_OPEN_LOCK
:
2885 case SPELL_EFFECT_OPEN_LOCK_ITEM
:
2886 if(Item
*item
= m_targets
.getItemTarget())
2887 data
.append(item
->GetPackGUID());
2891 case SPELL_EFFECT_CREATE_ITEM
:
2892 case SPELL_EFFECT_157
:
2893 data
<< uint32(m_spellInfo
->EffectItemType
[0]);
2895 case SPELL_EFFECT_SUMMON
:
2896 case SPELL_EFFECT_TRANS_DOOR
:
2897 case SPELL_EFFECT_SUMMON_PET
:
2898 case SPELL_EFFECT_SUMMON_OBJECT_WILD
:
2899 case SPELL_EFFECT_CREATE_HOUSE
:
2900 case SPELL_EFFECT_DUEL
:
2901 case SPELL_EFFECT_SUMMON_OBJECT_SLOT1
:
2902 case SPELL_EFFECT_SUMMON_OBJECT_SLOT2
:
2903 case SPELL_EFFECT_SUMMON_OBJECT_SLOT3
:
2904 case SPELL_EFFECT_SUMMON_OBJECT_SLOT4
:
2905 if(Unit
*unit
= m_targets
.getUnitTarget())
2906 data
.append(unit
->GetPackGUID());
2907 else if(m_targets
.getItemTargetGUID())
2908 data
.appendPackGUID(m_targets
.getItemTargetGUID());
2909 else if(GameObject
*go
= m_targets
.getGOTarget())
2910 data
.append(go
->GetPackGUID());
2912 data
<< uint8(0); // guid
2914 case SPELL_EFFECT_FEED_PET
:
2915 data
<< uint32(m_targets
.getItemTargetEntry());
2917 case SPELL_EFFECT_DISMISS_PET
:
2918 if(Unit
*unit
= m_targets
.getUnitTarget())
2919 data
.append(unit
->GetPackGUID());
2923 case SPELL_EFFECT_RESURRECT
:
2924 case SPELL_EFFECT_RESURRECT_NEW
:
2925 if(Unit
*unit
= m_targets
.getUnitTarget())
2926 data
.append(unit
->GetPackGUID());
2936 m_caster
->SendMessageToSet(&data
, true);
2939 void Spell::SendInterrupted(uint8 result
)
2941 WorldPacket
data(SMSG_SPELL_FAILURE
, (8+4+1));
2942 data
.append(m_caster
->GetPackGUID());
2943 data
<< uint8(m_cast_count
);
2944 data
<< uint32(m_spellInfo
->Id
);
2945 data
<< uint8(result
);
2946 m_caster
->SendMessageToSet(&data
, true);
2948 data
.Initialize(SMSG_SPELL_FAILED_OTHER
, (8+4));
2949 data
.append(m_caster
->GetPackGUID());
2950 data
<< uint8(m_cast_count
);
2951 data
<< uint32(m_spellInfo
->Id
);
2952 data
<< uint8(result
);
2953 m_caster
->SendMessageToSet(&data
, true);
2956 void Spell::SendChannelUpdate(uint32 time
)
2960 m_caster
->SetUInt64Value(UNIT_FIELD_CHANNEL_OBJECT
,0);
2961 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
,0);
2964 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
2967 WorldPacket
data( MSG_CHANNEL_UPDATE
, 8+4 );
2968 data
.append(m_caster
->GetPackGUID());
2969 data
<< uint32(time
);
2971 ((Player
*)m_caster
)->GetSession()->SendPacket( &data
);
2974 void Spell::SendChannelStart(uint32 duration
)
2976 WorldObject
* target
= NULL
;
2978 // select first not resisted target from target list for _0_ effect
2979 if(!m_UniqueTargetInfo
.empty())
2981 for(std::list
<TargetInfo
>::iterator itr
= m_UniqueTargetInfo
.begin();itr
!= m_UniqueTargetInfo
.end();++itr
)
2983 if( (itr
->effectMask
& (1<<0)) && itr
->reflectResult
==SPELL_MISS_NONE
&& itr
->targetGUID
!= m_caster
->GetGUID())
2985 target
= ObjectAccessor::GetUnit(*m_caster
, itr
->targetGUID
);
2990 else if(!m_UniqueGOTargetInfo
.empty())
2992 for(std::list
<GOTargetInfo
>::iterator itr
= m_UniqueGOTargetInfo
.begin();itr
!= m_UniqueGOTargetInfo
.end();++itr
)
2994 if(itr
->effectMask
& (1<<0) )
2996 target
= ObjectAccessor::GetGameObject(*m_caster
, itr
->targetGUID
);
3002 if (m_caster
->GetTypeId() == TYPEID_PLAYER
)
3004 WorldPacket
data( MSG_CHANNEL_START
, (8+4+4) );
3005 data
.append(m_caster
->GetPackGUID());
3006 data
<< uint32(m_spellInfo
->Id
);
3007 data
<< uint32(duration
);
3009 ((Player
*)m_caster
)->GetSession()->SendPacket( &data
);
3014 m_caster
->SetUInt64Value(UNIT_FIELD_CHANNEL_OBJECT
, target
->GetGUID());
3015 m_caster
->SetUInt32Value(UNIT_CHANNEL_SPELL
, m_spellInfo
->Id
);
3018 void Spell::SendResurrectRequest(Player
* target
)
3020 // Both players and NPCs can resurrect using spells - have a look at creature 28487 for example
3021 // However, the packet structure differs slightly
3023 const char* sentName
= m_caster
->GetTypeId()==TYPEID_PLAYER
?"":m_caster
->GetNameForLocaleIdx(target
->GetSession()->GetSessionDbLocaleIndex());
3025 WorldPacket
data(SMSG_RESURRECT_REQUEST
, (8+4+strlen(sentName
)+1+1+1));
3026 data
<< uint64(m_caster
->GetGUID());
3027 data
<< uint32(strlen(sentName
)+1);
3032 data
<< uint8(m_caster
->GetTypeId()==TYPEID_PLAYER
?0:1);
3033 target
->GetSession()->SendPacket(&data
);
3036 void Spell::SendPlaySpellVisual(uint32 SpellID
)
3038 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
3041 WorldPacket
data(SMSG_PLAY_SPELL_VISUAL
, 12);
3042 data
<< uint64(m_caster
->GetGUID());
3043 data
<< uint32(SpellID
); // spell visual id?
3044 ((Player
*)m_caster
)->GetSession()->SendPacket(&data
);
3047 void Spell::TakeCastItem()
3049 if(!m_CastItem
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
3052 // not remove cast item at triggered spell (equipping, weapon damage, etc)
3053 if(m_IsTriggeredSpell
)
3056 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3060 // This code is to avoid a crash
3061 // I'm not sure, if this is really an error, but I guess every item needs a prototype
3062 sLog
.outError("Cast item has no item prototype highId=%d, lowId=%d",m_CastItem
->GetGUIDHigh(), m_CastItem
->GetGUIDLow());
3066 bool expendable
= false;
3067 bool withoutCharges
= false;
3069 for (int i
= 0; i
<5; i
++)
3071 if (proto
->Spells
[i
].SpellId
)
3073 // item has limited charges
3074 if (proto
->Spells
[i
].SpellCharges
)
3076 if (proto
->Spells
[i
].SpellCharges
< 0)
3079 int32 charges
= m_CastItem
->GetSpellCharges(i
);
3081 // item has charges left
3084 (charges
> 0) ? --charges
: ++charges
; // abs(charges) less at 1 after use
3085 if (proto
->Stackable
< 2)
3086 m_CastItem
->SetSpellCharges(i
, charges
);
3087 m_CastItem
->SetState(ITEM_CHANGED
, (Player
*)m_caster
);
3091 withoutCharges
= (charges
== 0);
3096 if (expendable
&& withoutCharges
)
3099 ((Player
*)m_caster
)->DestroyItemCount(m_CastItem
, count
, true);
3101 // prevent crash at access to deleted m_targets.getItemTarget
3102 if(m_CastItem
==m_targets
.getItemTarget())
3103 m_targets
.setItemTarget(NULL
);
3109 void Spell::TakePower()
3111 if(m_CastItem
|| m_triggeredByAuraSpell
)
3114 // health as power used
3115 if(m_spellInfo
->powerType
== POWER_HEALTH
)
3117 m_caster
->ModifyHealth( -(int32
)m_powerCost
);
3121 if(m_spellInfo
->powerType
>= MAX_POWERS
)
3123 sLog
.outError("Spell::TakePower: Unknown power type '%d'", m_spellInfo
->powerType
);
3127 Powers powerType
= Powers(m_spellInfo
->powerType
);
3129 if(powerType
== POWER_RUNE
)
3135 m_caster
->ModifyPower(powerType
, -(int32
)m_powerCost
);
3137 // Set the five second timer
3138 if (powerType
== POWER_MANA
&& m_powerCost
> 0)
3139 m_caster
->SetLastManaUse(getMSTime());
3142 uint8
Spell::CheckRuneCost(uint32 runeCostID
)
3144 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
3147 Player
*plr
= (Player
*)m_caster
;
3149 if(plr
->getClass() != CLASS_DEATH_KNIGHT
)
3152 SpellRuneCostEntry
const *src
= sSpellRuneCostStore
.LookupEntry(runeCostID
);
3157 if(src
->NoRuneCost())
3160 int32 runeCost
[NUM_RUNE_TYPES
]; // blood, frost, unholy, death
3162 for(uint32 i
= 0; i
< RUNE_DEATH
; ++i
)
3163 runeCost
[i
] = src
->RuneCost
[i
];
3165 runeCost
[RUNE_DEATH
] = 0; // calculated later
3167 for(uint32 i
= 0; i
< MAX_RUNES
; ++i
)
3168 if(!plr
->GetRuneCooldown(i
))
3169 runeCost
[plr
->GetCurrentRune(i
)]--;
3171 for(uint32 i
= 0; i
< RUNE_DEATH
; ++i
)
3173 runeCost
[RUNE_DEATH
] += runeCost
[i
];
3175 if(runeCost
[RUNE_DEATH
] > 0)
3176 return SPELL_FAILED_REAGENTS
; // not sure if result code is correct
3181 void Spell::TakeRunePower()
3183 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
3186 Player
*plr
= (Player
*)m_caster
;
3188 if(plr
->getClass() != CLASS_DEATH_KNIGHT
)
3191 SpellRuneCostEntry
const *src
= sSpellRuneCostStore
.LookupEntry(m_spellInfo
->runeCostID
);
3193 if(!src
|| (src
->NoRuneCost() && src
->NoRunicPowerGain()))
3196 int32 runeCost
[NUM_RUNE_TYPES
]; // blood, frost, unholy, death
3198 for(uint32 i
= 0; i
< RUNE_DEATH
; ++i
)
3199 runeCost
[i
] = src
->RuneCost
[i
];
3201 runeCost
[RUNE_DEATH
] = 0; // calculated later
3203 for(uint32 i
= 0; i
< MAX_RUNES
; ++i
)
3205 if(!plr
->GetRuneCooldown(i
))
3207 uint8 rune
= plr
->GetCurrentRune(i
);
3208 if(runeCost
[rune
] > 0)
3210 plr
->SetRuneCooldown(i
, RUNE_COOLDOWN
); // 5*2=10 sec
3216 runeCost
[RUNE_DEATH
] = runeCost
[RUNE_BLOOD
] + runeCost
[RUNE_FROST
] + runeCost
[RUNE_UNHOLY
];
3218 if(runeCost
[RUNE_DEATH
] > 0)
3220 for(uint32 i
= 0; i
< MAX_RUNES
; ++i
)
3222 if(!plr
->GetRuneCooldown(i
) && plr
->GetCurrentRune(i
) == RUNE_DEATH
)
3224 plr
->SetRuneCooldown(i
, RUNE_COOLDOWN
); // 5*2=10 sec
3225 runeCost
[plr
->GetCurrentRune(i
)]--;
3226 uint8 base
= plr
->GetBaseRune(i
);
3227 plr
->SetCurrentRune(i
, base
);
3228 plr
->ConvertRune(i
, base
);
3229 if(runeCost
[RUNE_DEATH
] == 0)
3235 // you can gain some runic power when use runes
3236 float rp
= src
->runePowerGain
;;
3237 rp
*= sWorld
.getRate(RATE_POWER_RUNICPOWER_INCOME
);
3238 plr
->ModifyPower(POWER_RUNIC_POWER
, (int32
)rp
);
3241 void Spell::TakeReagents()
3243 if(m_IsTriggeredSpell
) // reagents used in triggered spell removed by original spell or don't must be removed.
3246 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
3249 if (m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_NO_REAGENT_WHILE_PREP
&&
3250 m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PREPARATION
))
3253 Player
* p_caster
= (Player
*)m_caster
;
3255 for(uint32 x
=0;x
<8;x
++)
3257 if(m_spellInfo
->Reagent
[x
] <= 0)
3260 uint32 itemid
= m_spellInfo
->Reagent
[x
];
3261 uint32 itemcount
= m_spellInfo
->ReagentCount
[x
];
3263 // if CastItem is also spell reagent
3266 ItemPrototype
const *proto
= m_CastItem
->GetProto();
3267 if( proto
&& proto
->ItemId
== itemid
)
3269 for(int s
=0;s
<5;s
++)
3271 // CastItem will be used up and does not count as reagent
3272 int32 charges
= m_CastItem
->GetSpellCharges(s
);
3273 if (proto
->Spells
[s
].SpellCharges
< 0 && abs(charges
) < 2)
3284 // if getItemTarget is also spell reagent
3285 if (m_targets
.getItemTargetEntry()==itemid
)
3286 m_targets
.setItemTarget(NULL
);
3288 p_caster
->DestroyItemCount(itemid
, itemcount
, true);
3292 void Spell::HandleThreatSpells(uint32 spellId
)
3294 if(!m_targets
.getUnitTarget() || !spellId
)
3297 if(!m_targets
.getUnitTarget()->CanHaveThreatList())
3300 SpellThreatEntry
const *threatSpell
= sSpellThreatStore
.LookupEntry
<SpellThreatEntry
>(spellId
);
3304 m_targets
.getUnitTarget()->AddThreat(m_caster
, float(threatSpell
->threat
));
3306 DEBUG_LOG("Spell %u, rank %u, added an additional %i threat", spellId
, spellmgr
.GetSpellRank(spellId
), threatSpell
->threat
);
3309 void Spell::HandleEffects(Unit
*pUnitTarget
,Item
*pItemTarget
,GameObject
*pGOTarget
,uint32 i
, float DamageMultiplier
)
3311 unitTarget
= pUnitTarget
;
3312 itemTarget
= pItemTarget
;
3313 gameObjTarget
= pGOTarget
;
3315 uint8 eff
= m_spellInfo
->Effect
[i
];
3316 uint32 mechanic
= m_spellInfo
->EffectMechanic
[i
];
3318 damage
= int32(CalculateDamage((uint8
)i
,unitTarget
)*DamageMultiplier
);
3320 sLog
.outDebug( "Spell: Effect : %u", eff
);
3322 //Simply return. Do not display "immune" in red text on client
3323 if(unitTarget
&& unitTarget
->IsImmunedToSpellEffect(eff
, mechanic
))
3326 if(eff
<TOTAL_SPELL_EFFECTS
)
3328 //sLog.outDebug( "WORLD: Spell FX %d < TOTAL_SPELL_EFFECTS ", eff);
3329 (*this.*SpellEffects
[eff
])(i
);
3334 sLog.outDebug( "WORLD: Spell FX %d > TOTAL_SPELL_EFFECTS ", eff);
3336 EffectEnchantItemTmp(i);
3339 sLog.outError("SPELL: unknown effect %u spell id %u\n",
3340 eff, m_spellInfo->Id);
3346 void Spell::TriggerSpell()
3348 for(TriggerSpells::iterator si
=m_TriggerSpells
.begin(); si
!=m_TriggerSpells
.end(); ++si
)
3350 Spell
* spell
= new Spell(m_caster
, (*si
), true, m_originalCasterGUID
, m_selfContainer
);
3351 spell
->prepare(&m_targets
); // use original spell original targets
3355 uint8
Spell::CanCast(bool strict
)
3357 // check cooldowns to prevent cheating
3358 if(m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
3360 if(m_triggeredByAuraSpell
)
3361 return SPELL_FAILED_DONT_REPORT
;
3363 return SPELL_FAILED_NOT_READY
;
3366 // only allow triggered spells if at an ended battleground
3367 if( !m_IsTriggeredSpell
&& m_caster
->GetTypeId() == TYPEID_PLAYER
)
3368 if(BattleGround
* bg
= ((Player
*)m_caster
)->GetBattleGround())
3369 if(bg
->GetStatus() == STATUS_WAIT_LEAVE
)
3370 return SPELL_FAILED_DONT_REPORT
;
3372 // only check at first call, Stealth auras are already removed at second call
3373 // for now, ignore triggered spells
3374 if( strict
&& !m_IsTriggeredSpell
)
3376 // Cannot be used in this stance/form
3377 if(uint8 shapeError
= GetErrorAtShapeshiftedCast(m_spellInfo
, m_caster
->m_form
))
3380 if ((m_spellInfo
->Attributes
& SPELL_ATTR_ONLY_STEALTHED
) && !(m_caster
->HasStealthAura()))
3381 return SPELL_FAILED_ONLY_STEALTHED
;
3384 // caster state requirements
3385 if(m_spellInfo
->CasterAuraState
&& !m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraState
)))
3386 return SPELL_FAILED_CASTER_AURASTATE
;
3387 if(m_spellInfo
->CasterAuraStateNot
&& m_caster
->HasAuraState(AuraState(m_spellInfo
->CasterAuraStateNot
)))
3388 return SPELL_FAILED_CASTER_AURASTATE
;
3390 // cancel autorepeat spells if cast start when moving
3391 // (not wand currently autorepeat cast delayed to moving stop anyway in spell update code)
3392 if( m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->isMoving() )
3394 // skip stuck spell to allow use it in falling case and apply spell limitations at movement
3395 if( (!m_caster
->HasUnitMovementFlag(MOVEMENTFLAG_FALLING
) || m_spellInfo
->Effect
[0] != SPELL_EFFECT_STUCK
) &&
3396 (IsAutoRepeat() || (m_spellInfo
->AuraInterruptFlags
& AURA_INTERRUPT_FLAG_NOT_SEATED
) != 0) )
3397 return SPELL_FAILED_MOVING
;
3400 Unit
*target
= m_targets
.getUnitTarget();
3404 // target state requirements (not allowed state), apply to self also
3405 if(m_spellInfo
->TargetAuraStateNot
&& target
->HasAuraState(AuraState(m_spellInfo
->TargetAuraStateNot
)))
3406 return SPELL_FAILED_TARGET_AURASTATE
;
3408 if(target
!= m_caster
)
3410 // target state requirements (apply to non-self only), to allow cast affects to self like Dirty Deeds
3411 if(m_spellInfo
->TargetAuraState
&& !target
->HasAuraState(AuraState(m_spellInfo
->TargetAuraState
)))
3412 return SPELL_FAILED_TARGET_AURASTATE
;
3414 // Not allow casting on flying player
3415 if (target
->isInFlight())
3416 return SPELL_FAILED_BAD_TARGETS
;
3418 if(!m_IsTriggeredSpell
&& VMAP::VMapFactory::checkSpellForLoS(m_spellInfo
->Id
) && !m_caster
->IsWithinLOSInMap(target
))
3419 return SPELL_FAILED_LINE_OF_SIGHT
;
3421 // auto selection spell rank implemented in WorldSession::HandleCastSpellOpcode
3422 // this case can be triggered if rank not found (too low-level target for first rank)
3423 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& !IsPassiveSpell(m_spellInfo
->Id
) && !m_CastItem
)
3425 for(int i
=0;i
<3;i
++)
3427 if(IsPositiveEffect(m_spellInfo
->Id
, i
) && m_spellInfo
->Effect
[i
] == SPELL_EFFECT_APPLY_AURA
)
3428 if(target
->getLevel() + 10 < m_spellInfo
->spellLevel
)
3429 return SPELL_FAILED_LOWLEVEL
;
3434 // check pet presents
3435 for(int j
=0;j
<3;j
++)
3437 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_PET
)
3439 target
= m_caster
->GetPet();
3442 if(m_triggeredByAuraSpell
) // not report pet not existence for triggered spells
3443 return SPELL_FAILED_DONT_REPORT
;
3445 return SPELL_FAILED_NO_PET
;
3451 //check creature type
3452 //ignore self casts (including area casts when caster selected as target)
3453 if(target
!= m_caster
)
3455 if(!CheckTargetCreatureType(target
))
3457 if(target
->GetTypeId()==TYPEID_PLAYER
)
3458 return SPELL_FAILED_TARGET_IS_PLAYER
;
3460 return SPELL_FAILED_BAD_TARGETS
;
3464 // TODO: this check can be applied and for player to prevent cheating when IsPositiveSpell will return always correct result.
3465 // check target for pet/charmed casts (not self targeted), self targeted cast used for area effects and etc
3466 if(m_caster
!= target
&& m_caster
->GetTypeId()==TYPEID_UNIT
&& m_caster
->GetCharmerOrOwnerGUID())
3468 // check correctness positive/negative cast target (pet cast real check and cheating check)
3469 if(IsPositiveSpell(m_spellInfo
->Id
))
3471 if(m_caster
->IsHostileTo(target
))
3472 return SPELL_FAILED_BAD_TARGETS
;
3476 if(m_caster
->IsFriendlyTo(target
))
3477 return SPELL_FAILED_BAD_TARGETS
;
3481 if(IsPositiveSpell(m_spellInfo
->Id
))
3483 if(target
->IsImmunedToSpell(m_spellInfo
,false))
3484 return SPELL_FAILED_TARGET_AURASTATE
;
3487 //Must be behind the target.
3488 if( m_spellInfo
->AttributesEx2
== 0x100000 && (m_spellInfo
->AttributesEx
& 0x200) == 0x200 && target
->HasInArc(M_PI
, m_caster
) )
3491 return SPELL_FAILED_NOT_BEHIND
;
3494 //Target must be facing you.
3495 if((m_spellInfo
->Attributes
== 0x150010) && !target
->HasInArc(M_PI
, m_caster
) )
3498 return SPELL_FAILED_NOT_INFRONT
;
3501 // check if target is in combat
3502 if (target
!= m_caster
&& (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_NOT_IN_COMBAT_TARGET
) && target
->isInCombat())
3504 return SPELL_FAILED_TARGET_AFFECTING_COMBAT
;
3507 // Spell casted only on battleground
3508 if((m_spellInfo
->AttributesEx3
& SPELL_ATTR_EX3_BATTLEGROUND
) && m_caster
->GetTypeId()==TYPEID_PLAYER
)
3509 if(!((Player
*)m_caster
)->InBattleGround())
3510 return SPELL_FAILED_ONLY_BATTLEGROUNDS
;
3512 // do not allow spells to be cast in arenas
3513 // - with greater than 15 min CD without SPELL_ATTR_EX4_USABLE_IN_ARENA flag
3514 // - with SPELL_ATTR_EX4_NOT_USABLE_IN_ARENA flag
3515 if( (m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_NOT_USABLE_IN_ARENA
) ||
3516 GetSpellRecoveryTime(m_spellInfo
) > 15 * MINUTE
* 1000 && !(m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_USABLE_IN_ARENA
) )
3517 if(MapEntry
const* mapEntry
= sMapStore
.LookupEntry(m_caster
->GetMapId()))
3518 if(mapEntry
->IsBattleArena())
3519 return SPELL_FAILED_NOT_IN_ARENA
;
3522 if(!IsSpellAllowedInLocation(m_spellInfo
,m_caster
->GetMapId(),m_caster
->GetZoneId(),m_caster
->GetAreaId()))
3523 return SPELL_FAILED_REQUIRES_AREA
;
3525 // not let players cast spells at mount (and let do it to creatures)
3526 if( m_caster
->IsMounted() && m_caster
->GetTypeId()==TYPEID_PLAYER
&& !m_IsTriggeredSpell
&&
3527 !IsPassiveSpell(m_spellInfo
->Id
) && !(m_spellInfo
->Attributes
& SPELL_ATTR_CASTABLE_WHILE_MOUNTED
) )
3529 if(m_caster
->isInFlight())
3530 return SPELL_FAILED_NOT_FLYING
;
3532 return SPELL_FAILED_NOT_MOUNTED
;
3535 // always (except passive spells) check items (focus object can be required for any type casts)
3536 if(!IsPassiveSpell(m_spellInfo
->Id
))
3537 if(uint8 castResult
= CheckItems())
3540 //ImpliciteTargetA-B = 38, If fact there is 0 Spell with ImpliciteTargetB=38
3541 if(m_UniqueTargetInfo
.empty()) // skip second canCast apply (for delayed spells for example)
3543 for(uint8 j
= 0; j
< 3; j
++)
3545 if( m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT
||
3546 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT
&& m_spellInfo
->EffectImplicitTargetA
[j
] != TARGET_SELF
||
3547 m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3548 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3550 SpellScriptTarget::const_iterator lower
= spellmgr
.GetBeginSpellScriptTarget(m_spellInfo
->Id
);
3551 SpellScriptTarget::const_iterator upper
= spellmgr
.GetEndSpellScriptTarget(m_spellInfo
->Id
);
3553 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
);
3555 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
3556 float range
= GetSpellMaxRange(srange
);
3558 Creature
* creatureScriptTarget
= NULL
;
3559 GameObject
* goScriptTarget
= NULL
;
3561 for(SpellScriptTarget::const_iterator i_spellST
= lower
; i_spellST
!= upper
; ++i_spellST
)
3563 switch(i_spellST
->second
.type
)
3565 case SPELL_TARGET_TYPE_GAMEOBJECT
:
3567 GameObject
* p_GameObject
= NULL
;
3569 if(i_spellST
->second
.targetEntry
)
3571 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
3573 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
3575 MaNGOS::NearestGameObjectEntryInObjectRangeCheck
go_check(*m_caster
,i_spellST
->second
.targetEntry
,range
);
3576 MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
> checker(p_GameObject
,go_check
);
3578 TypeContainerVisitor
<MaNGOS::GameObjectLastSearcher
<MaNGOS::NearestGameObjectEntryInObjectRangeCheck
>, GridTypeMapContainer
> object_checker(checker
);
3579 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
3580 cell_lock
->Visit(cell_lock
, object_checker
, *m_caster
->GetMap());
3584 // remember found target and range, next attempt will find more near target with another entry
3585 creatureScriptTarget
= NULL
;
3586 goScriptTarget
= p_GameObject
;
3587 range
= go_check
.GetLastRange();
3590 else if( focusObject
) //Focus Object
3592 float frange
= m_caster
->GetDistance(focusObject
);
3595 creatureScriptTarget
= NULL
;
3596 goScriptTarget
= focusObject
;
3602 case SPELL_TARGET_TYPE_CREATURE
:
3603 case SPELL_TARGET_TYPE_DEAD
:
3606 Creature
*p_Creature
= NULL
;
3608 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
3610 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
3611 cell
.SetNoCreate(); // Really don't know what is that???
3613 MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
u_check(*m_caster
,i_spellST
->second
.targetEntry
,i_spellST
->second
.type
!=SPELL_TARGET_TYPE_DEAD
,range
);
3614 MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
> searcher(p_Creature
, u_check
);
3616 TypeContainerVisitor
<MaNGOS::CreatureLastSearcher
<MaNGOS::NearestCreatureEntryWithLiveStateInObjectRangeCheck
>, GridTypeMapContainer
> grid_creature_searcher(searcher
);
3618 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
3619 cell_lock
->Visit(cell_lock
, grid_creature_searcher
, *m_caster
->GetMap());
3623 creatureScriptTarget
= p_Creature
;
3624 goScriptTarget
= NULL
;
3625 range
= u_check
.GetLastRange();
3632 if(creatureScriptTarget
)
3634 // store coordinates for TARGET_SCRIPT_COORDINATES
3635 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3636 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3638 m_targets
.setDestination(creatureScriptTarget
->GetPositionX(),creatureScriptTarget
->GetPositionY(),creatureScriptTarget
->GetPositionZ());
3640 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->EffectImplicitTargetB
[j
] == 0 && m_spellInfo
->Effect
[j
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
3641 AddUnitTarget(creatureScriptTarget
, j
);
3643 // store explicit target for TARGET_SCRIPT
3645 AddUnitTarget(creatureScriptTarget
, j
);
3647 else if(goScriptTarget
)
3649 // store coordinates for TARGET_SCRIPT_COORDINATES
3650 if (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
||
3651 m_spellInfo
->EffectImplicitTargetB
[j
] == TARGET_SCRIPT_COORDINATES
)
3653 m_targets
.setDestination(goScriptTarget
->GetPositionX(),goScriptTarget
->GetPositionY(),goScriptTarget
->GetPositionZ());
3655 if(m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_SCRIPT_COORDINATES
&& m_spellInfo
->EffectImplicitTargetB
[j
] == 0 && m_spellInfo
->Effect
[j
]!=SPELL_EFFECT_PERSISTENT_AREA_AURA
)
3656 AddGOTarget(goScriptTarget
, j
);
3658 // store explicit target for TARGET_SCRIPT
3660 AddGOTarget(goScriptTarget
, j
);
3662 //Missing DB Entry or targets for this spellEffect.
3665 // not report target not existence for triggered spells
3666 if(m_triggeredByAuraSpell
|| m_IsTriggeredSpell
)
3667 return SPELL_FAILED_DONT_REPORT
;
3669 return SPELL_FAILED_BAD_TARGETS
;
3675 if(uint8 castResult
= CheckRange(strict
))
3679 if(uint8 castResult
= CheckPower())
3683 if(!m_triggeredByAuraSpell
) // triggered spell not affected by stun/etc
3684 if(uint8 castResult
= CheckCasterAuras())
3687 for (int i
= 0; i
< 3; i
++)
3689 // for effects of spells that have only one target
3690 switch(m_spellInfo
->Effect
[i
])
3692 case SPELL_EFFECT_DUMMY
:
3694 if(m_spellInfo
->SpellIconID
== 1648) // Execute
3696 if(!m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
3697 return SPELL_FAILED_BAD_TARGETS
;
3699 else if (m_spellInfo
->Id
== 51582) // Rocket Boots Engaged
3701 if(m_caster
->IsInWater())
3702 return SPELL_FAILED_ONLY_ABOVEWATER
;
3704 else if(m_spellInfo
->SpellIconID
==156) // Holy Shock
3706 // spell different for friends and enemies
3707 // hart version required facing
3708 if(m_targets
.getUnitTarget() && !m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()) && !m_caster
->HasInArc( M_PI
, target
))
3709 return SPELL_FAILED_UNIT_NOT_INFRONT
;
3713 case SPELL_EFFECT_SCHOOL_DAMAGE
:
3716 if(m_spellInfo
->SpellVisual
[0] == 7250)
3718 if (!m_targets
.getUnitTarget())
3719 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3721 if(m_targets
.getUnitTarget()->GetHealth() > m_targets
.getUnitTarget()->GetMaxHealth()*0.2)
3722 return SPELL_FAILED_BAD_TARGETS
;
3726 case SPELL_EFFECT_TAMECREATURE
:
3728 if (!m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetTypeId() == TYPEID_PLAYER
)
3729 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
3731 if (m_targets
.getUnitTarget()->getLevel() > m_caster
->getLevel())
3732 return SPELL_FAILED_HIGHLEVEL
;
3734 // use SMSG_PET_TAME_FAILURE?
3735 if (!((Creature
*)m_targets
.getUnitTarget())->GetCreatureInfo()->isTameable ())
3736 return SPELL_FAILED_BAD_TARGETS
;
3738 if(m_caster
->GetPetGUID())
3739 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3741 if(m_caster
->GetCharmGUID())
3742 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
3746 case SPELL_EFFECT_LEARN_SPELL
:
3748 if(m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_PET
)
3751 Pet
* pet
= m_caster
->GetPet();
3754 return SPELL_FAILED_NO_PET
;
3756 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
3758 if(!learn_spellproto
)
3759 return SPELL_FAILED_NOT_KNOWN
;
3761 if(m_spellInfo
->spellLevel
> pet
->getLevel())
3762 return SPELL_FAILED_LOWLEVEL
;
3766 case SPELL_EFFECT_LEARN_PET_SPELL
:
3768 Pet
* pet
= m_caster
->GetPet();
3771 return SPELL_FAILED_NO_PET
;
3773 SpellEntry
const *learn_spellproto
= sSpellStore
.LookupEntry(m_spellInfo
->EffectTriggerSpell
[i
]);
3775 if(!learn_spellproto
)
3776 return SPELL_FAILED_NOT_KNOWN
;
3778 if(m_spellInfo
->spellLevel
> pet
->getLevel())
3779 return SPELL_FAILED_LOWLEVEL
;
3783 case SPELL_EFFECT_FEED_PET
:
3785 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| !m_targets
.getItemTarget() )
3786 return SPELL_FAILED_BAD_TARGETS
;
3788 Pet
* pet
= m_caster
->GetPet();
3791 return SPELL_FAILED_NO_PET
;
3793 if(!pet
->HaveInDiet(m_targets
.getItemTarget()->GetProto()))
3794 return SPELL_FAILED_WRONG_PET_FOOD
;
3796 if(!pet
->GetCurrentFoodBenefitLevel(m_targets
.getItemTarget()->GetProto()->ItemLevel
))
3797 return SPELL_FAILED_FOOD_LOWLEVEL
;
3799 if(m_caster
->isInCombat() || pet
->isInCombat())
3800 return SPELL_FAILED_AFFECTING_COMBAT
;
3804 case SPELL_EFFECT_POWER_BURN
:
3805 case SPELL_EFFECT_POWER_DRAIN
:
3807 // Can be area effect, Check only for players and not check if target - caster (spell can have multiply drain/burn effects)
3808 if(m_caster
->GetTypeId() == TYPEID_PLAYER
)
3809 if(Unit
* target
= m_targets
.getUnitTarget())
3810 if(target
!=m_caster
&& target
->getPowerType()!=m_spellInfo
->EffectMiscValue
[i
])
3811 return SPELL_FAILED_BAD_TARGETS
;
3814 case SPELL_EFFECT_CHARGE
:
3816 if (m_caster
->hasUnitState(UNIT_STAT_ROOT
))
3817 return SPELL_FAILED_ROOTED
;
3821 case SPELL_EFFECT_SKINNING
:
3823 if (m_caster
->GetTypeId() != TYPEID_PLAYER
|| !m_targets
.getUnitTarget() || m_targets
.getUnitTarget()->GetTypeId() != TYPEID_UNIT
)
3824 return SPELL_FAILED_BAD_TARGETS
;
3826 if( !(m_targets
.getUnitTarget()->GetUInt32Value(UNIT_FIELD_FLAGS
) & UNIT_FLAG_SKINNABLE
) )
3827 return SPELL_FAILED_TARGET_UNSKINNABLE
;
3829 Creature
* creature
= (Creature
*)m_targets
.getUnitTarget();
3830 if ( creature
->GetCreatureType() != CREATURE_TYPE_CRITTER
&& ( !creature
->lootForBody
|| !creature
->loot
.empty() ) )
3832 return SPELL_FAILED_TARGET_NOT_LOOTED
;
3835 uint32 skill
= creature
->GetCreatureInfo()->GetRequiredLootSkill();
3837 int32 skillValue
= ((Player
*)m_caster
)->GetSkillValue(skill
);
3838 int32 TargetLevel
= m_targets
.getUnitTarget()->getLevel();
3839 int32 ReqValue
= (skillValue
< 100 ? (TargetLevel
-10)*10 : TargetLevel
*5);
3840 if (ReqValue
> skillValue
)
3841 return SPELL_FAILED_LOW_CASTLEVEL
;
3843 // chance for fail at orange skinning attempt
3844 if( (m_selfContainer
&& (*m_selfContainer
) == this) &&
3845 skillValue
< sWorld
.GetConfigMaxSkillValue() &&
3846 (ReqValue
< 0 ? 0 : ReqValue
) > irand(skillValue
-25, skillValue
+37) )
3847 return SPELL_FAILED_TRY_AGAIN
;
3851 case SPELL_EFFECT_OPEN_LOCK_ITEM
:
3852 case SPELL_EFFECT_OPEN_LOCK
:
3854 if( m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT
&&
3855 m_spellInfo
->EffectImplicitTargetA
[i
] != TARGET_GAMEOBJECT_ITEM
)
3858 if( m_caster
->GetTypeId() != TYPEID_PLAYER
// only players can open locks, gather etc.
3859 // we need a go target in case of TARGET_GAMEOBJECT
3860 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT
&& !m_targets
.getGOTarget()
3861 // we need a go target, or an openable item target in case of TARGET_GAMEOBJECT_ITEM
3862 || m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_GAMEOBJECT_ITEM
&& !m_targets
.getGOTarget() &&
3863 (!m_targets
.getItemTarget() || !m_targets
.getItemTarget()->GetProto()->LockID
|| m_targets
.getItemTarget()->GetOwner() != m_caster
) )
3864 return SPELL_FAILED_BAD_TARGETS
;
3866 // In BattleGround players can use only flags and banners
3867 if( ((Player
*)m_caster
)->InBattleGround() &&
3868 !((Player
*)m_caster
)->isAllowUseBattleGroundObject() )
3869 return SPELL_FAILED_TRY_AGAIN
;
3871 // get the lock entry
3872 LockEntry
const *lockInfo
= NULL
;
3873 if (GameObject
* go
=m_targets
.getGOTarget())
3874 lockInfo
= sLockStore
.LookupEntry(go
->GetLockId());
3875 else if(Item
* itm
=m_targets
.getItemTarget())
3876 lockInfo
= sLockStore
.LookupEntry(itm
->GetProto()->LockID
);
3878 // check lock compatibility
3881 // check for lock - key pair (checked by client also, just prevent cheating
3882 bool ok_key
= false;
3883 for(int it
= 0; it
< 8; ++it
)
3885 switch(lockInfo
->Type
[it
])
3891 if(lockInfo
->Index
[it
])
3893 if(m_CastItem
&& m_CastItem
->GetEntry()==lockInfo
->Index
[it
])
3898 case LOCK_KEY_SKILL
:
3900 if(uint32(m_spellInfo
->EffectMiscValue
[i
])!=lockInfo
->Index
[it
])
3903 switch(lockInfo
->Index
[it
])
3905 case LOCKTYPE_HERBALISM
:
3906 if(((Player
*)m_caster
)->HasSkill(SKILL_HERBALISM
))
3909 case LOCKTYPE_MINING
:
3910 if(((Player
*)m_caster
)->HasSkill(SKILL_MINING
))
3924 return SPELL_FAILED_BAD_TARGETS
;
3927 // chance for fail at orange mining/herb/LockPicking gathering attempt
3928 if (!m_selfContainer
|| ((*m_selfContainer
) != this))
3931 // get the skill value of the player
3932 int32 SkillValue
= 0;
3933 bool canFailAtMax
= true;
3934 if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_HERBALISM
)
3936 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_HERBALISM
);
3937 canFailAtMax
= false;
3939 else if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_MINING
)
3941 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_MINING
);
3942 canFailAtMax
= false;
3944 else if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_PICKLOCK
)
3945 SkillValue
= ((Player
*)m_caster
)->GetSkillValue(SKILL_LOCKPICKING
);
3947 // castitem check: rogue using skeleton keys. the skill values should not be added in this case.
3951 // add the damage modifier from the spell casted (cheat lock / skeleton key etc.) (use m_currentBasePoints, CalculateDamage returns wrong value)
3952 SkillValue
+= m_currentBasePoints
[i
]+1;
3954 // get the required lock value
3958 // check for lock - key pair
3960 for(int it
= 0; it
< 8; ++it
)
3962 if(lockInfo
->Type
[it
]==LOCK_KEY_ITEM
&& lockInfo
->Index
[it
] && m_CastItem
&& m_CastItem
->GetEntry()==lockInfo
->Index
[it
])
3964 // if so, we're good to go
3972 if (m_spellInfo
->EffectMiscValue
[i
] == LOCKTYPE_PICKLOCK
)
3973 ReqValue
= lockInfo
->Skill
[1];
3975 ReqValue
= lockInfo
->Skill
[0];
3978 // skill doesn't meet the required value
3979 if (ReqValue
> SkillValue
)
3980 return SPELL_FAILED_LOW_CASTLEVEL
;
3982 // chance for failure in orange gather / lockpick (gathering skill can't fail at maxskill)
3983 if((canFailAtMax
|| SkillValue
< sWorld
.GetConfigMaxSkillValue()) && ReqValue
> irand(SkillValue
-25, SkillValue
+37))
3984 return SPELL_FAILED_TRY_AGAIN
;
3988 case SPELL_EFFECT_SUMMON_DEAD_PET
:
3990 Creature
*pet
= m_caster
->GetPet();
3992 return SPELL_FAILED_NO_PET
;
3995 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
3999 // This is generic summon effect now and don't make this check for summon types similar
4000 // SPELL_EFFECT_SUMMON_CRITTER, SPELL_EFFECT_SUMMON_WILD or SPELL_EFFECT_SUMMON_GUARDIAN.
4001 // These won't show up in m_caster->GetPetGUID()
4002 case SPELL_EFFECT_SUMMON
:
4004 switch(m_spellInfo
->EffectMiscValueB
[i
])
4006 case SUMMON_TYPE_POSESSED
:
4007 case SUMMON_TYPE_POSESSED2
:
4008 case SUMMON_TYPE_DEMON
:
4009 case SUMMON_TYPE_SUMMON
:
4011 if(m_caster
->GetPetGUID())
4012 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4014 if(m_caster
->GetCharmGUID())
4015 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4021 // Don't make this check for SPELL_EFFECT_SUMMON_CRITTER, SPELL_EFFECT_SUMMON_WILD or SPELL_EFFECT_SUMMON_GUARDIAN.
4022 // These won't show up in m_caster->GetPetGUID()
4023 case SPELL_EFFECT_SUMMON_POSSESSED
:
4024 case SPELL_EFFECT_SUMMON_PHANTASM
:
4025 case SPELL_EFFECT_SUMMON_DEMON
:
4027 if(m_caster
->GetPetGUID())
4028 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4030 if(m_caster
->GetCharmGUID())
4031 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4035 case SPELL_EFFECT_SUMMON_PET
:
4037 if(m_caster
->GetPetGUID()) //let warlock do a replacement summon
4040 Pet
* pet
= ((Player
*)m_caster
)->GetPet();
4042 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& m_caster
->getClass()==CLASS_WARLOCK
)
4044 if (strict
) //starting cast, trigger pet stun (cast by pet so it doesn't attack player)
4045 pet
->CastSpell(pet
, 32752, true, NULL
, NULL
, pet
->GetGUID());
4048 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4051 if(m_caster
->GetCharmGUID())
4052 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4056 case SPELL_EFFECT_SUMMON_PLAYER
:
4058 if(m_caster
->GetTypeId()!=TYPEID_PLAYER
)
4059 return SPELL_FAILED_BAD_TARGETS
;
4060 if(!((Player
*)m_caster
)->GetSelection())
4061 return SPELL_FAILED_BAD_TARGETS
;
4063 Player
* target
= objmgr
.GetPlayer(((Player
*)m_caster
)->GetSelection());
4064 if( !target
|| ((Player
*)m_caster
)==target
|| !target
->IsInSameRaidWith((Player
*)m_caster
) )
4065 return SPELL_FAILED_BAD_TARGETS
;
4067 // check if our map is dungeon
4068 if( sMapStore
.LookupEntry(m_caster
->GetMapId())->IsDungeon() )
4070 InstanceTemplate
const* instance
= ObjectMgr::GetInstanceTemplate(m_caster
->GetMapId());
4072 return SPELL_FAILED_TARGET_NOT_IN_INSTANCE
;
4073 if ( instance
->levelMin
> target
->getLevel() )
4074 return SPELL_FAILED_LOWLEVEL
;
4075 if ( instance
->levelMax
&& instance
->levelMax
< target
->getLevel() )
4076 return SPELL_FAILED_HIGHLEVEL
;
4080 case SPELL_EFFECT_LEAP
:
4081 case SPELL_EFFECT_TELEPORT_UNITS_FACE_CASTER
:
4083 float dis
= GetSpellRadius(sSpellRadiusStore
.LookupEntry(m_spellInfo
->EffectRadiusIndex
[i
]));
4084 float fx
= m_caster
->GetPositionX() + dis
* cos(m_caster
->GetOrientation());
4085 float fy
= m_caster
->GetPositionY() + dis
* sin(m_caster
->GetOrientation());
4086 // teleport a bit above terrain level to avoid falling below it
4087 float fz
= MapManager::Instance().GetBaseMap(m_caster
->GetMapId())->GetHeight(fx
,fy
,m_caster
->GetPositionZ(),true);
4088 if(fz
<= INVALID_HEIGHT
) // note: this also will prevent use effect in instances without vmaps height enabled
4089 return SPELL_FAILED_TRY_AGAIN
;
4091 float caster_pos_z
= m_caster
->GetPositionZ();
4092 // Control the caster to not climb or drop when +-fz > 8
4093 if(!(fz
<=caster_pos_z
+8 && fz
>=caster_pos_z
-8))
4094 return SPELL_FAILED_TRY_AGAIN
;
4096 // not allow use this effect at battleground until battleground start
4097 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
4098 if(BattleGround
const *bg
= ((Player
*)m_caster
)->GetBattleGround())
4099 if(bg
->GetStatus() != STATUS_IN_PROGRESS
)
4100 return SPELL_FAILED_TRY_AGAIN
;
4103 case SPELL_EFFECT_STEAL_BENEFICIAL_BUFF
:
4105 if (m_targets
.getUnitTarget()==m_caster
)
4106 return SPELL_FAILED_BAD_TARGETS
;
4113 for (int i
= 0; i
< 3; i
++)
4115 switch(m_spellInfo
->EffectApplyAuraName
[i
])
4117 case SPELL_AURA_MOD_POSSESS
:
4118 case SPELL_AURA_MOD_CHARM
:
4120 if(m_caster
->GetPetGUID())
4121 return SPELL_FAILED_ALREADY_HAVE_SUMMON
;
4123 if(m_caster
->GetCharmGUID())
4124 return SPELL_FAILED_ALREADY_HAVE_CHARM
;
4126 if(m_caster
->GetCharmerGUID())
4127 return SPELL_FAILED_CHARMED
;
4129 if(!m_targets
.getUnitTarget())
4130 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4132 if(m_targets
.getUnitTarget()->GetCharmerGUID())
4133 return SPELL_FAILED_CHARMED
;
4135 if(int32(m_targets
.getUnitTarget()->getLevel()) > CalculateDamage(i
,m_targets
.getUnitTarget()))
4136 return SPELL_FAILED_HIGHLEVEL
;
4140 case SPELL_AURA_MOUNTED
:
4142 if (m_caster
->IsInWater())
4143 return SPELL_FAILED_ONLY_ABOVEWATER
;
4145 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& ((Player
*)m_caster
)->GetTransport())
4146 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4148 // Ignore map check if spell have AreaId. AreaId already checked and this prevent special mount spells
4149 if (m_caster
->GetTypeId()==TYPEID_PLAYER
&& !sMapStore
.LookupEntry(m_caster
->GetMapId())->IsMountAllowed() && !m_IsTriggeredSpell
&& !m_spellInfo
->AreaId
)
4150 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4152 if (m_caster
->GetAreaId()==35)
4153 return SPELL_FAILED_NO_MOUNTS_ALLOWED
;
4155 ShapeshiftForm form
= m_caster
->m_form
;
4156 if( form
== FORM_CAT
|| form
== FORM_TREE
|| form
== FORM_TRAVEL
||
4157 form
== FORM_AQUA
|| form
== FORM_BEAR
|| form
== FORM_DIREBEAR
||
4158 form
== FORM_CREATUREBEAR
|| form
== FORM_GHOSTWOLF
|| form
== FORM_FLIGHT
||
4159 form
== FORM_FLIGHT_EPIC
|| form
== FORM_MOONKIN
|| form
== FORM_METAMORPHOSIS
)
4160 return SPELL_FAILED_NOT_SHAPESHIFT
;
4164 case SPELL_AURA_RANGED_ATTACK_POWER_ATTACKER_BONUS
:
4166 if(!m_targets
.getUnitTarget())
4167 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4169 // can be casted at non-friendly unit or own pet/charm
4170 if(m_caster
->IsFriendlyTo(m_targets
.getUnitTarget()))
4171 return SPELL_FAILED_TARGET_FRIENDLY
;
4175 case SPELL_AURA_MOD_INCREASE_FLIGHT_SPEED
:
4176 case SPELL_AURA_FLY
:
4178 // not allow cast fly spells at old maps by players (all spells is self target)
4179 if(m_caster
->GetTypeId()==TYPEID_PLAYER
)
4181 uint32 v_map
= GetVirtualMapForMapAndZone(m_caster
->GetMapId(), m_caster
->GetZoneId());
4182 if( !((Player
*)m_caster
)->isGameMaster() && v_map
!= 530 && !(v_map
== 571 && ((Player
*)m_caster
)->HasSpell(54197)))
4183 return SPELL_FAILED_NOT_HERE
;
4188 case SPELL_AURA_PERIODIC_MANA_LEECH
:
4190 if (!m_targets
.getUnitTarget())
4191 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4193 if (m_caster
->GetTypeId()!=TYPEID_PLAYER
|| m_CastItem
)
4196 if(m_targets
.getUnitTarget()->getPowerType()!=POWER_MANA
)
4197 return SPELL_FAILED_BAD_TARGETS
;
4210 int16
Spell::PetCanCast(Unit
* target
)
4212 if(!m_caster
->isAlive())
4213 return SPELL_FAILED_CASTER_DEAD
;
4215 if(m_caster
->IsNonMeleeSpellCasted(false)) //prevent spellcast interruption by another spellcast
4216 return SPELL_FAILED_SPELL_IN_PROGRESS
;
4217 if(m_caster
->isInCombat() && IsNonCombatSpell(m_spellInfo
))
4218 return SPELL_FAILED_AFFECTING_COMBAT
;
4220 if(m_caster
->GetTypeId()==TYPEID_UNIT
&& (((Creature
*)m_caster
)->isPet() || m_caster
->isCharmed()))
4222 //dead owner (pets still alive when owners ressed?)
4223 if(m_caster
->GetCharmerOrOwner() && !m_caster
->GetCharmerOrOwner()->isAlive())
4224 return SPELL_FAILED_CASTER_DEAD
;
4226 if(!target
&& m_targets
.getUnitTarget())
4227 target
= m_targets
.getUnitTarget();
4230 for(uint32 i
= 0;i
<3;i
++)
4232 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
)
4236 return SPELL_FAILED_BAD_IMPLICIT_TARGETS
;
4241 m_targets
.setUnitTarget(target
);
4243 Unit
* _target
= m_targets
.getUnitTarget();
4245 if(_target
) //for target dead/target not valid
4247 if(!_target
->isAlive())
4248 return SPELL_FAILED_BAD_TARGETS
;
4250 if(IsPositiveSpell(m_spellInfo
->Id
))
4252 if(m_caster
->IsHostileTo(_target
))
4253 return SPELL_FAILED_BAD_TARGETS
;
4257 bool duelvsplayertar
= false;
4258 for(int j
=0;j
<3;j
++)
4260 //TARGET_DUELVSPLAYER is positive AND negative
4261 duelvsplayertar
|= (m_spellInfo
->EffectImplicitTargetA
[j
] == TARGET_DUELVSPLAYER
);
4263 if(m_caster
->IsFriendlyTo(target
) && !duelvsplayertar
)
4265 return SPELL_FAILED_BAD_TARGETS
;
4270 if(((Creature
*)m_caster
)->HasSpellCooldown(m_spellInfo
->Id
))
4271 return SPELL_FAILED_NOT_READY
;
4274 uint16 result
= CanCast(true);
4278 return -1; //this allows to check spell fail 0, in combat
4281 uint8
Spell::CheckCasterAuras() const
4283 // Flag drop spells totally immuned to caster auras
4284 // FIXME: find more nice check for all totally immuned spells
4285 // AttributesEx3 & 0x10000000?
4286 if(m_spellInfo
->Id
==23336 || m_spellInfo
->Id
==23334 || m_spellInfo
->Id
==34991)
4289 uint8 school_immune
= 0;
4290 uint32 mechanic_immune
= 0;
4291 uint32 dispel_immune
= 0;
4293 //Check if the spell grants school or mechanic immunity.
4294 //We use bitmasks so the loop is done only once and not on every aura check below.
4295 if ( m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DISPEL_AURAS_ON_IMMUNITY
)
4297 for(int i
= 0;i
< 3; i
++)
4299 if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_SCHOOL_IMMUNITY
)
4300 school_immune
|= uint32(m_spellInfo
->EffectMiscValue
[i
]);
4301 else if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_MECHANIC_IMMUNITY
)
4302 mechanic_immune
|= 1 << uint32(m_spellInfo
->EffectMiscValue
[i
]);
4303 else if(m_spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_DISPEL_IMMUNITY
)
4304 dispel_immune
|= GetDispellMask(DispelType(m_spellInfo
->EffectMiscValue
[i
]));
4306 //immune movement impairment and loss of control
4307 if(m_spellInfo
->Id
==(uint32
)42292)
4308 mechanic_immune
= IMMUNE_TO_MOVEMENT_IMPAIRMENT_AND_LOSS_CONTROL_MASK
;
4311 //Check whether the cast should be prevented by any state you might have.
4312 uint8 prevented_reason
= 0;
4313 // Have to check if there is a stun aura. Otherwise will have problems with ghost aura apply while logging out
4314 if(!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
) && m_caster
->HasAuraType(SPELL_AURA_MOD_STUN
))
4315 prevented_reason
= SPELL_FAILED_STUNNED
;
4316 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_CONFUSED
) && !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
4317 prevented_reason
= SPELL_FAILED_CONFUSED
;
4318 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_FLEEING
) && !(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
4319 prevented_reason
= SPELL_FAILED_FLEEING
;
4320 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_SILENCED
) && m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_SILENCE
)
4321 prevented_reason
= SPELL_FAILED_SILENCED
;
4322 else if(m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PACIFIED
) && m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_PACIFY
)
4323 prevented_reason
= SPELL_FAILED_PACIFIED
;
4325 // Attr must make flag drop spell totally immune from all effects
4326 if(prevented_reason
)
4328 if(school_immune
|| mechanic_immune
|| dispel_immune
)
4330 //Checking auras is needed now, because you are prevented by some state but the spell grants immunity.
4331 Unit::AuraMap
const& auras
= m_caster
->GetAuras();
4332 for(Unit::AuraMap::const_iterator itr
= auras
.begin(); itr
!= auras
.end(); itr
++)
4336 if( GetSpellMechanicMask(itr
->second
->GetSpellProto(), itr
->second
->GetEffIndex()) & mechanic_immune
)
4338 if( GetSpellSchoolMask(itr
->second
->GetSpellProto()) & school_immune
)
4340 if( (1<<(itr
->second
->GetSpellProto()->Dispel
)) & dispel_immune
)
4343 //Make a second check for spell failed so the right SPELL_FAILED message is returned.
4344 //That is needed when your casting is prevented by multiple states and you are only immune to some of them.
4345 switch(itr
->second
->GetModifier()->m_auraname
)
4347 case SPELL_AURA_MOD_STUN
:
4348 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_STUNNED
))
4349 return SPELL_FAILED_STUNNED
;
4351 case SPELL_AURA_MOD_CONFUSE
:
4352 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_CONFUSED
))
4353 return SPELL_FAILED_CONFUSED
;
4355 case SPELL_AURA_MOD_FEAR
:
4356 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_USABLE_WHILE_FEARED
))
4357 return SPELL_FAILED_FLEEING
;
4359 case SPELL_AURA_MOD_SILENCE
:
4360 case SPELL_AURA_MOD_PACIFY
:
4361 case SPELL_AURA_MOD_PACIFY_SILENCE
:
4362 if( m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_PACIFY
)
4363 return SPELL_FAILED_PACIFIED
;
4364 else if ( m_spellInfo
->PreventionType
==SPELL_PREVENTION_TYPE_SILENCE
)
4365 return SPELL_FAILED_SILENCED
;
4371 //You are prevented from casting and the spell casted does not grant immunity. Return a failed error.
4373 return prevented_reason
;
4378 bool Spell::CanAutoCast(Unit
* target
)
4380 uint64 targetguid
= target
->GetGUID();
4382 for(uint32 j
= 0;j
<3;j
++)
4384 if(m_spellInfo
->Effect
[j
] == SPELL_EFFECT_APPLY_AURA
)
4386 if( m_spellInfo
->StackAmount
<= 1)
4388 if( target
->HasAura(m_spellInfo
->Id
, j
) )
4393 if( target
->GetAuras().count(Unit::spellEffectPair(m_spellInfo
->Id
, j
)) >= m_spellInfo
->StackAmount
)
4397 else if ( IsAreaAuraEffect( m_spellInfo
->Effect
[j
] ))
4399 if( target
->HasAura(m_spellInfo
->Id
, j
) )
4404 int16 result
= PetCanCast(target
);
4406 if(result
== -1 || result
== SPELL_FAILED_UNIT_NOT_INFRONT
)
4409 //check if among target units, our WANTED target is as well (->only self cast spells return false)
4410 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
4411 if( ihit
->targetGUID
== targetguid
)
4414 return false; //target invalid
4417 uint8
Spell::CheckRange(bool strict
)
4421 // self cast doesn't need range checking -- also for Starshards fix
4422 if (m_spellInfo
->rangeIndex
== 1) return 0;
4424 if (strict
) //add radius of caster
4426 else //add radius of caster and ~5 yds "give"
4429 SpellRangeEntry
const* srange
= sSpellRangeStore
.LookupEntry(m_spellInfo
->rangeIndex
);
4430 float max_range
= GetSpellMaxRange(srange
) + range_mod
;
4431 float min_range
= GetSpellMinRange(srange
);
4433 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
4434 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_RANGE
, max_range
, this);
4436 Unit
*target
= m_targets
.getUnitTarget();
4438 if(target
&& target
!= m_caster
)
4440 // distance from target center in checks
4441 float dist
= m_caster
->GetDistance(target
->GetPositionX(),target
->GetPositionY(),target
->GetPositionZ());
4442 if(dist
> max_range
)
4443 return SPELL_FAILED_OUT_OF_RANGE
; //0x5A;
4444 if(dist
< min_range
)
4445 return SPELL_FAILED_TOO_CLOSE
;
4446 if( m_caster
->GetTypeId() == TYPEID_PLAYER
&&
4447 (m_spellInfo
->FacingCasterFlags
& SPELL_FACING_FLAG_INFRONT
) && !m_caster
->HasInArc( M_PI
, target
) )
4448 return SPELL_FAILED_UNIT_NOT_INFRONT
;
4451 if(m_targets
.m_targetMask
== TARGET_FLAG_DEST_LOCATION
&& m_targets
.m_destX
!= 0 && m_targets
.m_destY
!= 0 && m_targets
.m_destZ
!= 0)
4453 float dist
= m_caster
->GetDistance(m_targets
.m_destX
, m_targets
.m_destY
, m_targets
.m_destZ
);
4454 if(dist
> max_range
)
4455 return SPELL_FAILED_OUT_OF_RANGE
;
4456 if(dist
< min_range
)
4457 return SPELL_FAILED_TOO_CLOSE
;
4463 int32
Spell::CalculatePowerCost()
4465 // item cast not used power
4469 // Spell drain all exist power on cast (Only paladin lay of Hands)
4470 if (m_spellInfo
->AttributesEx
& SPELL_ATTR_EX_DRAIN_ALL_POWER
)
4472 // If power type - health drain all
4473 if (m_spellInfo
->powerType
== POWER_HEALTH
)
4474 return m_caster
->GetHealth();
4475 // Else drain all power
4476 if (m_spellInfo
->powerType
< MAX_POWERS
)
4477 return m_caster
->GetPower(Powers(m_spellInfo
->powerType
));
4478 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
4483 int32 powerCost
= m_spellInfo
->manaCost
;
4484 // PCT cost from total amount
4485 if (m_spellInfo
->ManaCostPercentage
)
4487 switch (m_spellInfo
->powerType
)
4489 // health as power used
4491 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateHealth() / 100;
4494 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetCreateMana() / 100;
4499 case POWER_HAPPINESS
:
4500 powerCost
+= m_spellInfo
->ManaCostPercentage
* m_caster
->GetMaxPower(Powers(m_spellInfo
->powerType
)) / 100;
4503 case POWER_RUNIC_POWER
:
4504 sLog
.outDebug("Spell::CalculateManaCost: Not implemented yet!");
4507 sLog
.outError("Spell::CalculateManaCost: Unknown power type '%d' in spell %d", m_spellInfo
->powerType
, m_spellInfo
->Id
);
4511 SpellSchools school
= GetFirstSchoolInMask(m_spellSchoolMask
);
4512 // Flat mod from caster auras by spell school
4513 powerCost
+= m_caster
->GetInt32Value(UNIT_FIELD_POWER_COST_MODIFIER
+ school
);
4514 // Shiv - costs 20 + weaponSpeed*10 energy (apply only to non-triggered spell with energy cost)
4515 if ( m_spellInfo
->AttributesEx4
& SPELL_ATTR_EX4_SPELL_VS_EXTEND_COST
)
4516 powerCost
+= m_caster
->GetAttackTime(OFF_ATTACK
)/100;
4517 // Apply cost mod by spell
4518 if(Player
* modOwner
= m_caster
->GetSpellModOwner())
4519 modOwner
->ApplySpellMod(m_spellInfo
->Id
, SPELLMOD_COST
, powerCost
, this);
4521 if(m_spellInfo
->Attributes
& SPELL_ATTR_LEVEL_DAMAGE_CALCULATION
)
4522 powerCost
= int32(powerCost
/ (1.117f
* m_spellInfo
->spellLevel
/ m_caster
->getLevel() -0.1327f
));
4524 // PCT mod from user auras by school
4525 powerCost
= int32(powerCost
* (1.0f
+m_caster
->GetFloatValue(UNIT_FIELD_POWER_COST_MULTIPLIER
+school
)));
4531 uint8
Spell::CheckPower()
4533 // item cast not used power
4537 // health as power used - need check health amount
4538 if(m_spellInfo
->powerType
== POWER_HEALTH
)
4540 if(m_caster
->GetHealth() <= m_powerCost
)
4541 return SPELL_FAILED_CASTER_AURASTATE
;
4544 // Check valid power type
4545 if( m_spellInfo
->powerType
>= MAX_POWERS
)
4547 sLog
.outError("Spell::CheckMana: Unknown power type '%d'", m_spellInfo
->powerType
);
4548 return SPELL_FAILED_UNKNOWN
;
4551 uint8 failReason
= CheckRuneCost(m_spellInfo
->runeCostID
);
4555 // Check power amount
4556 Powers powerType
= Powers(m_spellInfo
->powerType
);
4557 if(m_caster
->GetPower(powerType
) < m_powerCost
)
4558 return SPELL_FAILED_NO_POWER
;
4563 uint8
Spell::CheckItems()
4565 if (m_caster
->GetTypeId() != TYPEID_PLAYER
)
4568 uint32 itemid
, itemcount
;
4569 Player
* p_caster
= (Player
*)m_caster
;
4573 itemid
= m_CastItem
->GetEntry();
4574 if( !p_caster
->HasItemCount(itemid
,1) )
4575 return SPELL_FAILED_ITEM_NOT_READY
;
4578 ItemPrototype
const *proto
= m_CastItem
->GetProto();
4580 return SPELL_FAILED_ITEM_NOT_READY
;
4582 for (int i
= 0; i
<5; i
++)
4584 if (proto
->Spells
[i
].SpellCharges
)
4586 if(m_CastItem
->GetSpellCharges(i
)==0)
4587 return SPELL_FAILED_NO_CHARGES_REMAIN
;
4591 uint32 ItemClass
= proto
->Class
;
4592 if (ItemClass
== ITEM_CLASS_CONSUMABLE
&& m_targets
.getUnitTarget())
4594 for (int i
= 0; i
< 3; i
++)
4596 // skip check, pet not required like checks, and for TARGET_PET m_targets.getUnitTarget() is not the real target but the caster
4597 if (m_spellInfo
->EffectImplicitTargetA
[i
] == TARGET_PET
)
4600 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_HEAL
)
4601 if (m_targets
.getUnitTarget()->GetHealth() == m_targets
.getUnitTarget()->GetMaxHealth())
4602 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_HEALTH
;
4604 // Mana Potion, Rage Potion, Thistle Tea(Rogue), ...
4605 if (m_spellInfo
->Effect
[i
] == SPELL_EFFECT_ENERGIZE
)
4607 if(m_spellInfo
->EffectMiscValue
[i
] < 0 || m_spellInfo
->EffectMiscValue
[i
] >= MAX_POWERS
)
4608 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_POWER
;
4610 Powers power
= Powers(m_spellInfo
->EffectMiscValue
[i
]);
4612 if (m_targets
.getUnitTarget()->GetPower(power
) == m_targets
.getUnitTarget()->GetMaxPower(power
))
4613 return (uint8
)SPELL_FAILED_ALREADY_AT_FULL_POWER
;
4620 if(m_targets
.getItemTargetGUID())
4622 if(m_caster
->GetTypeId() != TYPEID_PLAYER
)
4623 return SPELL_FAILED_BAD_TARGETS
;
4625 if(!m_targets
.getItemTarget())
4626 return SPELL_FAILED_ITEM_GONE
;
4628 if(!m_targets
.getItemTarget()->IsFitToSpellRequirements(m_spellInfo
))
4629 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
4631 // if not item target then required item must be equipped
4634 if(m_caster
->GetTypeId() == TYPEID_PLAYER
&& !((Player
*)m_caster
)->HasItemFitToSpellReqirements(m_spellInfo
))
4635 return SPELL_FAILED_EQUIPPED_ITEM_CLASS
;
4638 if(m_spellInfo
->RequiresSpellFocus
)
4640 CellPair
p(MaNGOS::ComputeCellPair(m_caster
->GetPositionX(), m_caster
->GetPositionY()));
4642 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
4644 GameObject
* ok
= NULL
;
4645 MaNGOS::GameObjectFocusCheck
go_check(m_caster
,m_spellInfo
->RequiresSpellFocus
);
4646 MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
> checker(ok
,go_check
);
4648 TypeContainerVisitor
<MaNGOS::GameObjectSearcher
<MaNGOS::GameObjectFocusCheck
>, GridTypeMapContainer
> object_checker(checker
);
4649 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
4650 cell_lock
->Visit(cell_lock
, object_checker
, *m_caster
->GetMap());
4653 return (uint8
)SPELL_FAILED_REQUIRES_SPELL_FOCUS
;
4655 focusObject
= ok
; // game object found in range
4658 if (!(m_spellInfo
->AttributesEx5
& SPELL_ATTR_EX5_NO_REAGENT_WHILE_PREP
&&
4659 m_caster
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_PREPARATION
)))
4661 for(uint32 i
=0;i
<8;i
++)
4663 if(m_spellInfo
->Reagent
[i
] <= 0)
4666 itemid
= m_spellInfo
->Reagent
[i
];
4667 itemcount
= m_spellInfo
->ReagentCount
[i
];
4669 // if CastItem is also spell reagent
4670 if( m_CastItem
&& m_CastItem
->GetEntry() == itemid
)
4672 ItemPrototype
const *proto
= m_CastItem
->GetProto();
4674 return SPELL_FAILED_ITEM_NOT_READY
;
4675 for(int s
=0;s
<5;s
++)
4677 // CastItem will be used up and does not count as reagent
4678 int32 charges
= m_CastItem
->GetSpellCharges(s
);
4679 if (proto
->Spells
[s
].SpellCharges
< 0 && abs(charges
) < 2)
4686 if( !p_caster
->HasItemCount(itemid
,itemcount
) )
4687 return (uint8
)SPELL_FAILED_ITEM_NOT_READY
; //0x54
4692 for(int i
=0;i
<2;++i
)
4694 if(m_spellInfo
->Totem
[i
] != 0)
4696 if( p_caster
->HasItemCount(m_spellInfo
->Totem
[i
],1) )
4705 return (uint8
)SPELL_FAILED_TOTEMS
; //0x7C
4707 //Check items for TotemCategory
4708 uint32 TotemCategory
= 2;
4709 for(int i
=0;i
<2;++i
)
4711 if(m_spellInfo
->TotemCategory
[i
] != 0)
4713 if( p_caster
->HasItemTotemCategory(m_spellInfo
->TotemCategory
[i
]) )
4722 if(TotemCategory
!= 0)
4723 return (uint8
)SPELL_FAILED_TOTEM_CATEGORY
; //0x7B
4725 for(int i
= 0; i
< 3; i
++)
4727 switch (m_spellInfo
->Effect
[i
])
4729 case SPELL_EFFECT_CREATE_ITEM
:
4731 if (!m_IsTriggeredSpell
&& m_spellInfo
->EffectItemType
[i
])
4733 ItemPosCountVec dest
;
4734 uint8 msg
= p_caster
->CanStoreNewItem(NULL_BAG
, NULL_SLOT
, dest
, m_spellInfo
->EffectItemType
[i
], 1 );
4735 if (msg
!= EQUIP_ERR_OK
)
4737 p_caster
->SendEquipError( msg
, NULL
, NULL
);
4738 return SPELL_FAILED_DONT_REPORT
;
4743 case SPELL_EFFECT_ENCHANT_ITEM
:
4745 Item
* targetItem
= m_targets
.getItemTarget();
4747 return SPELL_FAILED_ITEM_NOT_FOUND
;
4749 if( targetItem
->GetProto()->ItemLevel
< m_spellInfo
->baseLevel
)
4750 return SPELL_FAILED_LOWLEVEL
;
4751 // Not allow enchant in trade slot for some enchant type
4752 if( targetItem
->GetOwner() != m_caster
)
4754 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
4755 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
4757 return SPELL_FAILED_ERROR
;
4758 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
4759 return SPELL_FAILED_NOT_TRADEABLE
;
4763 case SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY
:
4765 Item
*item
= m_targets
.getItemTarget();
4767 return SPELL_FAILED_ITEM_NOT_FOUND
;
4768 // Not allow enchant in trade slot for some enchant type
4769 if( item
->GetOwner() != m_caster
)
4771 uint32 enchant_id
= m_spellInfo
->EffectMiscValue
[i
];
4772 SpellItemEnchantmentEntry
const *pEnchant
= sSpellItemEnchantmentStore
.LookupEntry(enchant_id
);
4774 return SPELL_FAILED_ERROR
;
4775 if (pEnchant
->slot
& ENCHANTMENT_CAN_SOULBOUND
)
4776 return SPELL_FAILED_NOT_TRADEABLE
;
4780 case SPELL_EFFECT_ENCHANT_HELD_ITEM
:
4781 // check item existence in effect code (not output errors at offhand hold item effect to main hand for example
4783 case SPELL_EFFECT_DISENCHANT
:
4785 if(!m_targets
.getItemTarget())
4786 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4788 // prevent disenchanting in trade slot
4789 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4790 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4792 ItemPrototype
const* itemProto
= m_targets
.getItemTarget()->GetProto();
4794 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4796 uint32 item_quality
= itemProto
->Quality
;
4797 // 2.0.x addon: Check player enchanting level against the item disenchanting requirements
4798 uint32 item_disenchantskilllevel
= itemProto
->RequiredDisenchantSkill
;
4799 if (item_disenchantskilllevel
== uint32(-1))
4800 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4801 if (item_disenchantskilllevel
> p_caster
->GetSkillValue(SKILL_ENCHANTING
))
4802 return SPELL_FAILED_LOW_CASTLEVEL
;
4803 if(item_quality
> 4 || item_quality
< 2)
4804 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4805 if(itemProto
->Class
!= ITEM_CLASS_WEAPON
&& itemProto
->Class
!= ITEM_CLASS_ARMOR
)
4806 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4807 if (!itemProto
->DisenchantID
)
4808 return SPELL_FAILED_CANT_BE_DISENCHANTED
;
4811 case SPELL_EFFECT_PROSPECTING
:
4813 if(!m_targets
.getItemTarget())
4814 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4815 //ensure item is a prospectable ore
4816 if(!(m_targets
.getItemTarget()->GetProto()->BagFamily
& BAG_FAMILY_MASK_MINING_SUPP
) || m_targets
.getItemTarget()->GetProto()->Class
!= ITEM_CLASS_TRADE_GOODS
)
4817 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4818 //prevent prospecting in trade slot
4819 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4820 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4821 //Check for enough skill in jewelcrafting
4822 uint32 item_prospectingskilllevel
= m_targets
.getItemTarget()->GetProto()->RequiredSkillRank
;
4823 if(item_prospectingskilllevel
>p_caster
->GetSkillValue(SKILL_JEWELCRAFTING
))
4824 return SPELL_FAILED_LOW_CASTLEVEL
;
4825 //make sure the player has the required ores in inventory
4826 if(m_targets
.getItemTarget()->GetCount() < 5)
4827 return SPELL_FAILED_NEED_MORE_ITEMS
;
4829 if(!LootTemplates_Prospecting
.HaveLootFor(m_targets
.getItemTargetEntry()))
4830 return SPELL_FAILED_CANT_BE_PROSPECTED
;
4834 case SPELL_EFFECT_MILLING
:
4836 if(!m_targets
.getItemTarget())
4837 return SPELL_FAILED_CANT_BE_MILLED
;
4838 //ensure item is a millable herb
4839 if(!(m_targets
.getItemTarget()->GetProto()->BagFamily
& BAG_FAMILY_MASK_HERBS
) || m_targets
.getItemTarget()->GetProto()->Class
!= ITEM_CLASS_TRADE_GOODS
)
4840 return SPELL_FAILED_CANT_BE_MILLED
;
4841 //prevent milling in trade slot
4842 if( m_targets
.getItemTarget()->GetOwnerGUID() != m_caster
->GetGUID() )
4843 return SPELL_FAILED_CANT_BE_MILLED
;
4844 //Check for enough skill in inscription
4845 uint32 item_millingskilllevel
= m_targets
.getItemTarget()->GetProto()->RequiredSkillRank
;
4846 if(item_millingskilllevel
>p_caster
->GetSkillValue(SKILL_INSCRIPTION
))
4847 return SPELL_FAILED_LOW_CASTLEVEL
;
4848 //make sure the player has the required herbs in inventory
4849 if(m_targets
.getItemTarget()->GetCount() < 5)
4850 return SPELL_FAILED_NEED_MORE_ITEMS
;
4852 if(!LootTemplates_Milling
.HaveLootFor(m_targets
.getItemTargetEntry()))
4853 return SPELL_FAILED_CANT_BE_MILLED
;
4857 case SPELL_EFFECT_WEAPON_DAMAGE
:
4858 case SPELL_EFFECT_WEAPON_DAMAGE_NOSCHOOL
:
4860 if(m_caster
->GetTypeId() != TYPEID_PLAYER
) return SPELL_FAILED_TARGET_NOT_PLAYER
;
4861 if( m_attackType
!= RANGED_ATTACK
)
4863 Item
*pItem
= ((Player
*)m_caster
)->GetWeaponForAttack(m_attackType
);
4864 if(!pItem
|| pItem
->IsBroken())
4865 return SPELL_FAILED_EQUIPPED_ITEM
;
4867 switch(pItem
->GetProto()->SubClass
)
4869 case ITEM_SUBCLASS_WEAPON_THROWN
:
4871 uint32 ammo
= pItem
->GetEntry();
4872 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
4873 return SPELL_FAILED_NO_AMMO
;
4875 case ITEM_SUBCLASS_WEAPON_GUN
:
4876 case ITEM_SUBCLASS_WEAPON_BOW
:
4877 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
4879 uint32 ammo
= ((Player
*)m_caster
)->GetUInt32Value(PLAYER_AMMO_ID
);
4883 if(m_caster
->GetDummyAura(46699))
4884 break; // skip other checks
4886 return SPELL_FAILED_NO_AMMO
;
4889 ItemPrototype
const *ammoProto
= objmgr
.GetItemPrototype( ammo
);
4891 return SPELL_FAILED_NO_AMMO
;
4893 if(ammoProto
->Class
!= ITEM_CLASS_PROJECTILE
)
4894 return SPELL_FAILED_NO_AMMO
;
4896 // check ammo ws. weapon compatibility
4897 switch(pItem
->GetProto()->SubClass
)
4899 case ITEM_SUBCLASS_WEAPON_BOW
:
4900 case ITEM_SUBCLASS_WEAPON_CROSSBOW
:
4901 if(ammoProto
->SubClass
!=ITEM_SUBCLASS_ARROW
)
4902 return SPELL_FAILED_NO_AMMO
;
4904 case ITEM_SUBCLASS_WEAPON_GUN
:
4905 if(ammoProto
->SubClass
!=ITEM_SUBCLASS_BULLET
)
4906 return SPELL_FAILED_NO_AMMO
;
4909 return SPELL_FAILED_NO_AMMO
;
4912 if( !((Player
*)m_caster
)->HasItemCount( ammo
, 1 ) )
4913 return SPELL_FAILED_NO_AMMO
;
4915 case ITEM_SUBCLASS_WEAPON_WAND
:
4928 void Spell::Delayed()
4930 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
)
4933 if (m_spellState
== SPELL_STATE_DELAYED
)
4934 return; // spell is active and can't be time-backed
4936 // spells not loosing casting time ( slam, dynamites, bombs.. )
4937 if(!(m_spellInfo
->InterruptFlags
& SPELL_INTERRUPT_FLAG_DAMAGE
))
4940 //check resist chance
4941 int32 resistChance
= 100; //must be initialized to 100 for percent modifiers
4942 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
,SPELLMOD_NOT_LOSE_CASTING_TIME
,resistChance
, this);
4943 resistChance
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_RESIST_PUSHBACK
) - 100;
4944 if (roll_chance_i(resistChance
))
4947 int32 delaytime
= GetNextDelayAtDamageMsTime();
4949 if(int32(m_timer
) + delaytime
> m_casttime
)
4951 delaytime
= m_casttime
- m_timer
;
4952 m_timer
= m_casttime
;
4955 m_timer
+= delaytime
;
4957 sLog
.outDetail("Spell %u partially interrupted for (%d) ms at damage",m_spellInfo
->Id
,delaytime
);
4959 WorldPacket
data(SMSG_SPELL_DELAYED
, 8+4);
4960 data
.append(m_caster
->GetPackGUID());
4961 data
<< uint32(delaytime
);
4963 m_caster
->SendMessageToSet(&data
,true);
4966 void Spell::DelayedChannel()
4968 if(!m_caster
|| m_caster
->GetTypeId() != TYPEID_PLAYER
|| getState() != SPELL_STATE_CASTING
)
4971 //check resist chance
4972 int32 resistChance
= 100; //must be initialized to 100 for percent modifiers
4973 ((Player
*)m_caster
)->ApplySpellMod(m_spellInfo
->Id
,SPELLMOD_NOT_LOSE_CASTING_TIME
,resistChance
, this);
4974 resistChance
+= m_caster
->GetTotalAuraModifier(SPELL_AURA_RESIST_PUSHBACK
) - 100;
4975 if (roll_chance_i(resistChance
))
4978 int32 delaytime
= GetNextDelayAtDamageMsTime();
4980 if(int32(m_timer
) < delaytime
)
4982 delaytime
= m_timer
;
4986 m_timer
-= delaytime
;
4988 sLog
.outDebug("Spell %u partially interrupted for %i ms, new duration: %u ms", m_spellInfo
->Id
, delaytime
, m_timer
);
4990 for(std::list
<TargetInfo
>::iterator ihit
= m_UniqueTargetInfo
.begin();ihit
!= m_UniqueTargetInfo
.end();++ihit
)
4992 if ((*ihit
).missCondition
== SPELL_MISS_NONE
)
4994 Unit
* unit
= m_caster
->GetGUID()==ihit
->targetGUID
? m_caster
: ObjectAccessor::GetUnit(*m_caster
, ihit
->targetGUID
);
4997 for (int j
=0;j
<3;j
++)
4998 if( ihit
->effectMask
& (1<<j
) )
4999 unit
->DelayAura(m_spellInfo
->Id
, j
, delaytime
);
5005 for(int j
= 0; j
< 3; j
++)
5007 // partially interrupt persistent area auras
5008 DynamicObject
* dynObj
= m_caster
->GetDynObject(m_spellInfo
->Id
, j
);
5010 dynObj
->Delay(delaytime
);
5013 SendChannelUpdate(m_timer
);
5016 void Spell::UpdatePointers()
5018 if(m_originalCasterGUID
==m_caster
->GetGUID())
5019 m_originalCaster
= m_caster
;
5022 m_originalCaster
= ObjectAccessor::GetUnit(*m_caster
,m_originalCasterGUID
);
5023 if(m_originalCaster
&& !m_originalCaster
->IsInWorld()) m_originalCaster
= NULL
;
5026 m_targets
.Update(m_caster
);
5029 bool Spell::IsAffectedBy(SpellEntry
const *spellInfo
, uint32 effectId
)
5031 return spellmgr
.IsAffectedBySpell(m_spellInfo
,spellInfo
->Id
,effectId
,spellInfo
->EffectItemType
[effectId
]);
5034 bool Spell::CheckTargetCreatureType(Unit
* target
) const
5036 uint32 spellCreatureTargetMask
= m_spellInfo
->TargetCreatureType
;
5038 // Curse of Doom : not find another way to fix spell target check :/
5039 if(m_spellInfo
->SpellFamilyName
==SPELLFAMILY_WARLOCK
&& m_spellInfo
->SpellFamilyFlags
== 0x0200000000LL
)
5041 // not allow cast at player
5042 if(target
->GetTypeId()==TYPEID_PLAYER
)
5045 spellCreatureTargetMask
= 0x7FF;
5048 // Dismiss Pet and Taming Lesson skipped
5049 if(m_spellInfo
->Id
== 2641 || m_spellInfo
->Id
== 23356)
5050 spellCreatureTargetMask
= 0;
5052 if (spellCreatureTargetMask
)
5054 uint32 TargetCreatureType
= target
->GetCreatureTypeMask();
5056 return !TargetCreatureType
|| (spellCreatureTargetMask
& TargetCreatureType
);
5061 CurrentSpellTypes
Spell::GetCurrentContainer()
5063 if (IsNextMeleeSwingSpell())
5064 return(CURRENT_MELEE_SPELL
);
5065 else if (IsAutoRepeat())
5066 return(CURRENT_AUTOREPEAT_SPELL
);
5067 else if (IsChanneledSpell(m_spellInfo
))
5068 return(CURRENT_CHANNELED_SPELL
);
5070 return(CURRENT_GENERIC_SPELL
);
5073 bool Spell::CheckTarget( Unit
* target
, uint32 eff
)
5075 // Check targets for creature type mask and remove not appropriate (skip explicit self target case, maybe need other explicit targets)
5076 if(m_spellInfo
->EffectImplicitTargetA
[eff
]!=TARGET_SELF
)
5078 if (!CheckTargetCreatureType(target
))
5082 // Check targets for not_selectable unit flag and remove
5083 // A player can cast spells on his pet (or other controlled unit) though in any state
5084 if (target
!= m_caster
&& target
->GetCharmerOrOwnerGUID() != m_caster
->GetGUID())
5086 // any unattackable target skipped
5087 if (target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NON_ATTACKABLE
))
5090 // unselectable targets skipped in all cases except TARGET_SCRIPT targeting
5091 // in case TARGET_SCRIPT target selected by server always and can't be cheated
5092 if( target
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NOT_SELECTABLE
) &&
5093 m_spellInfo
->EffectImplicitTargetA
[eff
] != TARGET_SCRIPT
&&
5094 m_spellInfo
->EffectImplicitTargetB
[eff
] != TARGET_SCRIPT
)
5098 //Check player targets and remove if in GM mode or GM invisibility (for not self casting case)
5099 if( target
!= m_caster
&& target
->GetTypeId()==TYPEID_PLAYER
)
5101 if(((Player
*)target
)->GetVisibility()==VISIBILITY_OFF
)
5104 if(((Player
*)target
)->isGameMaster() && !IsPositiveSpell(m_spellInfo
->Id
))
5108 //Check targets for LOS visibility (except spells without range limitations )
5109 switch(m_spellInfo
->Effect
[eff
])
5111 case SPELL_EFFECT_SUMMON_PLAYER
: // from anywhere
5113 case SPELL_EFFECT_DUMMY
:
5114 if(m_spellInfo
->Id
!=20577) // Cannibalize
5117 case SPELL_EFFECT_RESURRECT_NEW
:
5118 // player far away, maybe his corpse near?
5119 if(target
!=m_caster
&& !target
->IsWithinLOSInMap(m_caster
))
5121 if(!m_targets
.getCorpseTargetGUID())
5124 Corpse
*corpse
= ObjectAccessor::GetCorpse(*m_caster
,m_targets
.getCorpseTargetGUID());
5128 if(target
->GetGUID()!=corpse
->GetOwnerGUID())
5131 if(!corpse
->IsWithinLOSInMap(m_caster
))
5135 // all ok by some way or another, skip normal check
5137 default: // normal case
5138 if(target
!=m_caster
&& !target
->IsWithinLOSInMap(m_caster
))
5146 Unit
* Spell::SelectMagnetTarget()
5148 Unit
* target
= m_targets
.getUnitTarget();
5150 if(target
&& target
->HasAuraType(SPELL_AURA_SPELL_MAGNET
) && !(m_spellInfo
->Attributes
& 0x10))
5152 Unit::AuraList
const& magnetAuras
= target
->GetAurasByType(SPELL_AURA_SPELL_MAGNET
);
5153 for(Unit::AuraList::const_iterator itr
= magnetAuras
.begin(); itr
!= magnetAuras
.end(); ++itr
)
5155 if(Unit
* magnet
= (*itr
)->GetCaster())
5157 if(magnet
->IsWithinLOSInMap(m_caster
))
5160 m_targets
.setUnitTarget(target
);
5170 bool Spell::IsNeedSendToClient() const
5172 return m_spellInfo
->SpellVisual
!=0 || IsChanneledSpell(m_spellInfo
) ||
5173 m_spellInfo
->speed
> 0.0f
|| !m_triggeredByAuraSpell
&& !m_IsTriggeredSpell
;
5176 bool Spell::HaveTargetsForEffect( uint8 effect
) const
5178 for(std::list
<TargetInfo
>::const_iterator itr
= m_UniqueTargetInfo
.begin();itr
!= m_UniqueTargetInfo
.end();++itr
)
5179 if(itr
->effectMask
& (1<<effect
))
5182 for(std::list
<GOTargetInfo
>::const_iterator itr
= m_UniqueGOTargetInfo
.begin();itr
!= m_UniqueGOTargetInfo
.end();++itr
)
5183 if(itr
->effectMask
& (1<<effect
))
5186 for(std::list
<ItemTargetInfo
>::const_iterator itr
= m_UniqueItemInfo
.begin();itr
!= m_UniqueItemInfo
.end();++itr
)
5187 if(itr
->effectMask
& (1<<effect
))
5193 SpellEvent::SpellEvent(Spell
* spell
) : BasicEvent()
5198 SpellEvent::~SpellEvent()
5200 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5203 if (m_Spell
->IsDeletable())
5209 sLog
.outError("~SpellEvent: %s %u tried to delete non-deletable spell %u. Was not deleted, causes memory leak.",
5210 (m_Spell
->GetCaster()->GetTypeId()==TYPEID_PLAYER
?"Player":"Creature"), m_Spell
->GetCaster()->GetGUIDLow(),m_Spell
->m_spellInfo
->Id
);
5214 bool SpellEvent::Execute(uint64 e_time
, uint32 p_time
)
5216 // update spell if it is not finished
5217 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5218 m_Spell
->update(p_time
);
5220 // check spell state to process
5221 switch (m_Spell
->getState())
5223 case SPELL_STATE_FINISHED
:
5225 // spell was finished, check deletable state
5226 if (m_Spell
->IsDeletable())
5228 // check, if we do have unfinished triggered spells
5230 return(true); // spell is deletable, finish event
5232 // event will be re-added automatically at the end of routine)
5235 case SPELL_STATE_CASTING
:
5237 // this spell is in channeled state, process it on the next update
5238 // event will be re-added automatically at the end of routine)
5241 case SPELL_STATE_DELAYED
:
5243 // first, check, if we have just started
5244 if (m_Spell
->GetDelayStart() != 0)
5246 // no, we aren't, do the typical update
5247 // check, if we have channeled spell on our hands
5248 if (IsChanneledSpell(m_Spell
->m_spellInfo
))
5250 // evented channeled spell is processed separately, casted once after delay, and not destroyed till finish
5251 // check, if we have casting anything else except this channeled spell and autorepeat
5252 if (m_Spell
->GetCaster()->IsNonMeleeSpellCasted(false, true, true))
5254 // another non-melee non-delayed spell is casted now, abort
5259 // do the action (pass spell to channeling state)
5260 m_Spell
->handle_immediate();
5262 // event will be re-added automatically at the end of routine)
5266 // run the spell handler and think about what we can do next
5267 uint64 t_offset
= e_time
- m_Spell
->GetDelayStart();
5268 uint64 n_offset
= m_Spell
->handle_delayed(t_offset
);
5271 // re-add us to the queue
5272 m_Spell
->GetCaster()->m_Events
.AddEvent(this, m_Spell
->GetDelayStart() + n_offset
, false);
5273 return(false); // event not complete
5276 // finish update event will be re-added automatically at the end of routine)
5281 // delaying had just started, record the moment
5282 m_Spell
->SetDelayStart(e_time
);
5283 // re-plan the event for the delay moment
5284 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ m_Spell
->GetDelayMoment(), false);
5285 return(false); // event not complete
5292 // event will be re-added automatically at the end of routine)
5296 // spell processing not complete, plan event on the next update interval
5297 m_Spell
->GetCaster()->m_Events
.AddEvent(this, e_time
+ 1, false);
5298 return(false); // event not complete
5301 void SpellEvent::Abort(uint64
/*e_time*/)
5303 // oops, the spell we try to do is aborted
5304 if (m_Spell
->getState() != SPELL_STATE_FINISHED
)
5308 bool SpellEvent::IsDeletable() const
5310 return m_Spell
->IsDeletable();