[8381] Some refactoring work in Unit::m_currentSpells code. Author: VladimirMangos

* Restrict access, use enum args, move some repeated code parts to function.
    * Make m_selfContainer set only part of Unit::SetCurrentCastedSpell

--HG--
branch : trunk
This commit is contained in:
megamage
2009-08-19 16:52:51 -05:00
parent 6181620520
commit 012d6fa865
11 changed files with 87 additions and 86 deletions
@@ -486,11 +486,11 @@ struct TRINITY_DLL_DECL boss_essence_of_desireAI : public ScriptedAI
void SpellHit(Unit *caster, const SpellEntry *spell)
{
if (m_creature->m_currentSpells[CURRENT_GENERIC_SPELL])
if (m_creature->GetCurrentSpell(CURRENT_GENERIC_SPELL))
for(uint8 i = 0; i < 3; ++i)
if (spell->Effect[i] == SPELL_EFFECT_INTERRUPT_CAST)
if (m_creature->m_currentSpells[CURRENT_GENERIC_SPELL]->m_spellInfo->Id == SPELL_SOUL_SHOCK
|| m_creature->m_currentSpells[CURRENT_GENERIC_SPELL]->m_spellInfo->Id == SPELL_DEADEN)
if (m_creature->GetCurrentSpell(CURRENT_GENERIC_SPELL)->m_spellInfo->Id == SPELL_SOUL_SHOCK
|| m_creature->GetCurrentSpell(CURRENT_GENERIC_SPELL)->m_spellInfo->Id == SPELL_DEADEN)
m_creature->InterruptSpell(CURRENT_GENERIC_SPELL, false);
}
@@ -686,7 +686,7 @@ struct TRINITY_DLL_DECL mob_greyheart_spellbinderAI : public ScriptedAI
void CastChanneling()
{
if (!m_creature->isInCombat() && !m_creature->m_currentSpells[CURRENT_CHANNELED_SPELL])
if (!m_creature->isInCombat() && !m_creature->GetCurrentSpell(CURRENT_CHANNELED_SPELL))
{
if (leotherasGUID)
{
@@ -745,7 +745,7 @@ struct TRINITY_DLL_DECL mob_greyheart_spellbinderAI : public ScriptedAI
{
bool isCasting = false;
for(uint8 i = 0; i < CURRENT_MAX_SPELL; ++i)
if (i_pl->m_currentSpells[i])
if (i_pl->GetCurrentSpell(i))
isCasting = true;
if (isCasting)
+4 -13
View File
@@ -268,11 +268,7 @@ void GameObject::Update(uint32 /*p_time*/)
Unit* caster = GetOwner();
if(caster && caster->GetTypeId()==TYPEID_PLAYER)
{
if(caster->m_currentSpells[CURRENT_CHANNELED_SPELL])
{
caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish(false);
}
caster->FinishSpell(CURRENT_CHANNELED_SPELL);
WorldPacket data(SMSG_FISH_NOT_HOOKED,0);
((Player*)caster)->GetSession()->SendPacket(&data);
@@ -1150,11 +1146,7 @@ void GameObject::Use(Unit* user)
}
}
if(player->m_currentSpells[CURRENT_CHANNELED_SPELL])
{
player->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
player->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish();
}
player->FinishSpell(CURRENT_CHANNELED_SPELL);
return;
}
@@ -1185,7 +1177,7 @@ void GameObject::Use(Unit* user)
// in case summoning ritual caster is GO creator
spellCaster = caster;
if(!caster->m_currentSpells[CURRENT_CHANNELED_SPELL])
if(!caster->GetCurrentSpell(CURRENT_CHANNELED_SPELL))
return;
spellId = info->summoningRitual.spellId;
@@ -1198,8 +1190,7 @@ void GameObject::Use(Unit* user)
}
// finish spell
caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
caster->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish();
player->FinishSpell(CURRENT_CHANNELED_SPELL);
// can be deleted now
SetLootState(GO_JUST_DEACTIVATED);
+10 -10
View File
@@ -17775,13 +17775,15 @@ bool Player::ActivateTaxiPathTo(std::vector<uint32> const& nodes, Creature* npc
if( m_ShapeShiftFormSpellId && m_form != FORM_BATTLESTANCE && m_form != FORM_BERSERKERSTANCE && m_form != FORM_DEFENSIVESTANCE && m_form != FORM_SHADOW )
RemoveAurasDueToSpell(m_ShapeShiftFormSpellId);
if(m_currentSpells[CURRENT_GENERIC_SPELL] && m_currentSpells[CURRENT_GENERIC_SPELL]->m_spellInfo->Id != spellid)
InterruptSpell(CURRENT_GENERIC_SPELL,false);
if (Spell* spell = GetCurrentSpell(CURRENT_GENERIC_SPELL))
if (spell->m_spellInfo->Id != spellid)
InterruptSpell(CURRENT_GENERIC_SPELL,false);
InterruptSpell(CURRENT_AUTOREPEAT_SPELL,false);
if(m_currentSpells[CURRENT_CHANNELED_SPELL] && m_currentSpells[CURRENT_CHANNELED_SPELL]->m_spellInfo->Id != spellid)
InterruptSpell(CURRENT_CHANNELED_SPELL,true);
if (Spell* spell = GetCurrentSpell(CURRENT_CHANNELED_SPELL))
if (spell->m_spellInfo->Id != spellid)
InterruptSpell(CURRENT_CHANNELED_SPELL,true);
}
uint32 sourcenode = nodes[0];
@@ -19924,12 +19926,10 @@ void Player::RemoveItemDependentAurasAndCasts( Item * pItem )
}
// currently casted spells can be dependent from item
for (uint32 i = 0; i < CURRENT_MAX_SPELL; i++)
{
if( m_currentSpells[i] && m_currentSpells[i]->getState()!=SPELL_STATE_DELAYED &&
!HasItemFitToSpellReqirements(m_currentSpells[i]->m_spellInfo,pItem) )
InterruptSpell(i);
}
for (uint32 i = 0; i < CURRENT_MAX_SPELL; ++i)
if (Spell* spell = GetCurrentSpell(CurrentSpellTypes(i)))
if (spell->getState()!=SPELL_STATE_DELAYED && !HasItemFitToSpellReqirements(spell->m_spellInfo,pItem) )
InterruptSpell(CurrentSpellTypes(i));
}
uint32 Player::GetResurrectionSpellId()
+5 -6
View File
@@ -2124,7 +2124,7 @@ void Spell::SetTargetMap(uint32 i, uint32 cur)
case TARGET_TYPE_CHANNEL:
{
if(!m_originalCaster || !m_originalCaster->m_currentSpells[CURRENT_CHANNELED_SPELL])
if(!m_originalCaster || !m_originalCaster->GetCurrentSpell(CURRENT_CHANNELED_SPELL))
{
sLog.outError( "SPELL: no current channeled spell for spell ID %u", m_spellInfo->Id );
break;
@@ -2133,15 +2133,15 @@ void Spell::SetTargetMap(uint32 i, uint32 cur)
switch(cur)
{
case TARGET_UNIT_CHANNEL:
if(Unit* target = m_originalCaster->m_currentSpells[CURRENT_CHANNELED_SPELL]->m_targets.getUnitTarget())
if(Unit* target = m_originalCaster->GetCurrentSpell(CURRENT_CHANNELED_SPELL)->m_targets.getUnitTarget())
AddUnitTarget(target, i);
else
sLog.outError( "SPELL: cannot find channel spell target for spell ID %u", m_spellInfo->Id );
break;
case TARGET_DEST_CHANNEL:
if(m_originalCaster->m_currentSpells[CURRENT_CHANNELED_SPELL]->m_targets.HasDst())
m_targets = m_originalCaster->m_currentSpells[CURRENT_CHANNELED_SPELL]->m_targets;
else if(Unit* target = m_originalCaster->m_currentSpells[CURRENT_CHANNELED_SPELL]->m_targets.getUnitTarget())
if(m_originalCaster->GetCurrentSpell(CURRENT_CHANNELED_SPELL)->m_targets.HasDst())
m_targets = m_originalCaster->GetCurrentSpell(CURRENT_CHANNELED_SPELL)->m_targets;
else if(Unit* target = m_originalCaster->GetCurrentSpell(CURRENT_CHANNELED_SPELL)->m_targets.getUnitTarget())
m_targets.setDestination(target);
else
sLog.outError( "SPELL: cannot find channel spell destination for spell ID %u", m_spellInfo->Id );
@@ -2704,7 +2704,6 @@ void Spell::prepare(SpellCastTargets const* targets, AuraEffect* triggeredByAura
}
m_caster->SetCurrentCastedSpell( this );
m_selfContainer = &(m_caster->m_currentSpells[GetCurrentContainer()]);
SendSpellStart();
if(!m_casttime && !m_spellInfo->StartRecoveryTime
+1
View File
@@ -243,6 +243,7 @@ enum SpellTargets
class Spell
{
friend struct Trinity::SpellNotifierCreatureAndPlayer;
friend void Unit::SetCurrentCastedSpell( Spell * pSpell );
public:
void EffectNULL(uint32 );
+16 -24
View File
@@ -3841,10 +3841,10 @@ void AuraEffect::HandleFeignDeath(bool apply, bool Real, bool /*changeAmount*/)
for(uint32 i = CURRENT_FIRST_NON_MELEE_SPELL; i < CURRENT_MAX_SPELL; i++)
{
if((*iter)->m_currentSpells[i]
&& (*iter)->m_currentSpells[i]->m_targets.getUnitTargetGUID() == m_target->GetGUID())
if((*iter)->GetCurrentSpell(i)
&& (*iter)->GetCurrentSpell(i)->m_targets.getUnitTargetGUID() == m_target->GetGUID())
{
(*iter)->InterruptSpell(i, false);
(*iter)->InterruptSpell(CurrentSpellTypes(i), false);
}
}
}
@@ -3860,8 +3860,8 @@ void AuraEffect::HandleFeignDeath(bool apply, bool Real, bool /*changeAmount*/)
m_target->RemoveAurasWithInterruptFlags(AURA_INTERRUPT_FLAG_IMMUNE_OR_LOST_SELECTION);
// prevent interrupt message
if(GetCasterGUID()==m_target->GetGUID() && m_target->m_currentSpells[CURRENT_GENERIC_SPELL])
m_target->m_currentSpells[CURRENT_GENERIC_SPELL]->finish();
if(GetCasterGUID()==m_target->GetGUID() && m_target->GetCurrentSpell(CURRENT_GENERIC_SPELL))
m_target->FinishSpell(CURRENT_GENERIC_SPELL, false);
m_target->InterruptNonMeleeSpells(true);
m_target->getHostilRefManager().deleteReferences();
}
@@ -4033,9 +4033,11 @@ void AuraEffect::HandleAuraModSilence(bool apply, bool Real, bool /*changeAmount
{
m_target->SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SILENCED);
// Stop cast only spells vs PreventionType == SPELL_PREVENTION_TYPE_SILENCE
for (uint32 i = CURRENT_MELEE_SPELL; i < CURRENT_MAX_SPELL;i++)
if (m_target->m_currentSpells[i] && m_target->m_currentSpells[i]->m_spellInfo->PreventionType == SPELL_PREVENTION_TYPE_SILENCE)
m_target->InterruptSpell(i,false); // Stop spells on prepare or casting state
for (uint32 i = CURRENT_MELEE_SPELL; i < CURRENT_MAX_SPELL; ++i)
if (Spell* spell = m_target->GetCurrentSpell(CurrentSpellTypes(i)))
if(spell->m_spellInfo->PreventionType == SPELL_PREVENTION_TYPE_SILENCE)
// Stop spells on prepare or casting state
m_target->InterruptSpell(CurrentSpellTypes(i), false);
}
else
{
@@ -5813,13 +5815,10 @@ void AuraEffect::PeriodicTick()
int32 new_damage = pCaster->DealDamage(target, pdamage, &cleanDamage, DOT, GetSpellSchoolMask(spellProto), spellProto, false);
if (!target->isAlive() && pCaster->IsNonMeleeSpellCasted(false))
{
for (uint32 i = CURRENT_FIRST_NON_MELEE_SPELL; i < CURRENT_MAX_SPELL; i++)
{
if (pCaster->m_currentSpells[i] && pCaster->m_currentSpells[i]->m_spellInfo->Id == spellProto->Id)
pCaster->m_currentSpells[i]->cancel();
}
}
for (uint32 i = CURRENT_FIRST_NON_MELEE_SPELL; i < CURRENT_MAX_SPELL; ++i)
if (Spell* spell = pCaster->GetCurrentSpell(CurrentSpellTypes(i)))
if (spell->m_spellInfo->Id == GetId())
spell->cancel();
if(Player *modOwner = pCaster->GetSpellModOwner())
modOwner->ApplySpellMod(spellProto->Id, SPELLMOD_MULTIPLE_VALUE, multiplier);
@@ -5931,15 +5930,8 @@ void AuraEffect::PeriodicTick()
pCaster->RemoveAurasDueToSpell(GetId());
// finish current generic/channeling spells, don't affect autorepeat
if(pCaster->m_currentSpells[CURRENT_GENERIC_SPELL])
{
pCaster->m_currentSpells[CURRENT_GENERIC_SPELL]->finish();
}
if(pCaster->m_currentSpells[CURRENT_CHANNELED_SPELL])
{
pCaster->m_currentSpells[CURRENT_CHANNELED_SPELL]->SendChannelUpdate(0);
pCaster->m_currentSpells[CURRENT_CHANNELED_SPELL]->finish();
}
pCaster->FinishSpell(CURRENT_GENERIC_SPELL);
pCaster->FinishSpell(CURRENT_CHANNELED_SPELL);
}
else
{
+11 -8
View File
@@ -4542,19 +4542,22 @@ void Spell::EffectInterruptCast(uint32 i)
// TODO: not all spells that used this effect apply cooldown at school spells
// also exist case: apply cooldown to interrupted cast only and to all spells
for (uint32 i = CURRENT_FIRST_NON_MELEE_SPELL; i < CURRENT_MAX_SPELL; i++)
for (uint32 i = CURRENT_FIRST_NON_MELEE_SPELL; i < CURRENT_MAX_SPELL; ++i)
{
if (unitTarget->m_currentSpells[i])
if (Spell* spell = unitTarget->GetCurrentSpell(CurrentSpellTypes(i)))
{
SpellEntry const* curSpellInfo = spell->m_spellInfo;
// check if we can interrupt spell
if ( (unitTarget->m_currentSpells[i]->getState() == SPELL_STATE_CASTING || (unitTarget->m_currentSpells[i]->getState() == SPELL_STATE_PREPARING && unitTarget->m_currentSpells[i]->GetCastTime() > 0.0f)) && unitTarget->m_currentSpells[i]->m_spellInfo->InterruptFlags & SPELL_INTERRUPT_FLAG_INTERRUPT && unitTarget->m_currentSpells[i]->m_spellInfo->PreventionType == SPELL_PREVENTION_TYPE_SILENCE )
if ((spell->getState() == SPELL_STATE_CASTING
|| spell->getState() == SPELL_STATE_PREPARING && spell->GetCastTime() > 0.0f)
&& curSpellInfo->InterruptFlags & SPELL_INTERRUPT_FLAG_INTERRUPT && curSpellInfo->PreventionType == SPELL_PREVENTION_TYPE_SILENCE )
{
if(m_originalCaster)
{
int32 duration = m_originalCaster->ModSpellDuration(m_spellInfo, unitTarget, m_originalCaster->CalcSpellDuration(m_spellInfo), false);
unitTarget->ProhibitSpellScholl(GetSpellSchoolMask(unitTarget->m_currentSpells[i]->m_spellInfo), duration/*GetSpellDuration(m_spellInfo)*/);
unitTarget->ProhibitSpellScholl(GetSpellSchoolMask(curSpellInfo), duration/*GetSpellDuration(m_spellInfo)*/);
}
unitTarget->InterruptSpell(i,false);
unitTarget->InterruptSpell(CurrentSpellTypes(i), false);
}
}
}
@@ -5585,10 +5588,10 @@ void Spell::EffectSanctuary(uint32 /*i*/)
for(uint32 i = CURRENT_FIRST_NON_MELEE_SPELL; i < CURRENT_MAX_SPELL; i++)
{
if((*iter)->m_currentSpells[i]
&& (*iter)->m_currentSpells[i]->m_targets.getUnitTargetGUID() == unitTarget->GetGUID())
if((*iter)->GetCurrentSpell(i)
&& (*iter)->GetCurrentSpell(i)->m_targets.getUnitTargetGUID() == unitTarget->GetGUID())
{
(*iter)->InterruptSpell(i, false);
(*iter)->InterruptSpell(CurrentSpellTypes(i), false);
}
}
}
+5 -5
View File
@@ -325,8 +325,8 @@ void WorldSession::HandleCastSpellOpcode(WorldPacket& recvPacket)
// Client is resending autoshot cast opcode when other spell is casted during shoot rotation
// Skip it to prevent "interrupt" message
if (IsAutoRepeatRangedSpell(spellInfo) && _player->m_currentSpells[CURRENT_AUTOREPEAT_SPELL]
&& _player->m_currentSpells[CURRENT_AUTOREPEAT_SPELL]->m_spellInfo == spellInfo)
if (IsAutoRepeatRangedSpell(spellInfo) && _player->GetCurrentSpell(CURRENT_AUTOREPEAT_SPELL)
&& _player->GetCurrentSpell(CURRENT_AUTOREPEAT_SPELL)->m_spellInfo == spellInfo)
return;
// can't use our own spells when we're in possession of another unit,
@@ -381,9 +381,9 @@ void WorldSession::HandleCancelAuraOpcode( WorldPacket& recvPacket)
// channeled spell case (it currently casted then)
if (IsChanneledSpell(spellInfo))
{
if (_player->m_currentSpells[CURRENT_CHANNELED_SPELL] &&
_player->m_currentSpells[CURRENT_CHANNELED_SPELL]->m_spellInfo->Id==spellId)
_player->InterruptSpell(CURRENT_CHANNELED_SPELL);
if (Spell* curSpell = _player->GetCurrentSpell(CURRENT_CHANNELED_SPELL))
if (curSpell->m_spellInfo->Id==spellId)
_player->InterruptSpell(CURRENT_CHANNELED_SPELL);
return;
}
+16 -6
View File
@@ -873,7 +873,7 @@ void Unit::CastStop(uint32 except_spellid)
{
for (uint32 i = CURRENT_FIRST_NON_MELEE_SPELL; i < CURRENT_MAX_SPELL; i++)
if (m_currentSpells[i] && m_currentSpells[i]->m_spellInfo->Id!=except_spellid)
InterruptSpell(i,false);
InterruptSpell(CurrentSpellTypes(i),false);
}
void Unit::CastSpell(Unit* Victim, uint32 spellId, bool triggered, Item *castItem, AuraEffect* triggeredByAura, uint64 originalCaster)
@@ -2904,13 +2904,9 @@ SpellMissInfo Unit::SpellHitResult(Unit *pVictim, SpellEntry const *spell, bool
}
}
if (isNormal || m_currentSpells[CURRENT_MELEE_SPELL])
{
misschance = 5.0f;
}
else
{
misschance = 24.0f;
}
}
// PvP : PvE melee misschances per leveldif > 2
@@ -3314,9 +3310,11 @@ void Unit::SetCurrentCastedSpell( Spell * pSpell )
// set new current spell
m_currentSpells[CSpellType] = pSpell;
pSpell->SetReferencedFromCurrent(true);
pSpell->m_selfContainer = &(m_currentSpells[pSpell->GetCurrentContainer()]);
}
void Unit::InterruptSpell(uint32 spellType, bool withDelayed, bool withInstant)
void Unit::InterruptSpell(CurrentSpellTypes spellType, bool withDelayed, bool withInstant)
{
assert(spellType < CURRENT_MAX_SPELL);
@@ -3345,6 +3343,18 @@ void Unit::InterruptSpell(uint32 spellType, bool withDelayed, bool withInstant)
}
}
void Unit::FinishSpell(CurrentSpellTypes spellType, bool ok /*= true*/)
{
Spell* spell = m_currentSpells[spellType];
if (!spell)
return;
if (spellType == CURRENT_CHANNELED_SPELL)
spell->SendChannelUpdate(0);
spell->finish(ok);
}
bool Unit::IsNonMeleeSpellCasted(bool withDelayed, bool skipChanneled, bool skipAutorepeat, bool isAutoshoot) const
{
// We don't do loop here to explicitly show that melee spell is excluded.
+14 -9
View File
@@ -852,14 +852,16 @@ struct DeclinedName
enum CurrentSpellTypes
{
CURRENT_MELEE_SPELL = 0,
CURRENT_FIRST_NON_MELEE_SPELL = 1, // just counter
CURRENT_GENERIC_SPELL = 1,
CURRENT_AUTOREPEAT_SPELL = 2,
CURRENT_CHANNELED_SPELL = 3,
CURRENT_MAX_SPELL = 4 // just counter
CURRENT_MELEE_SPELL = 0,
CURRENT_GENERIC_SPELL = 1,
CURRENT_AUTOREPEAT_SPELL = 2,
CURRENT_CHANNELED_SPELL = 3
};
#define CURRENT_FIRST_NON_MELEE_SPELL 1
#define CURRENT_MAX_SPELL 4
enum ActiveStates
{
ACT_PASSIVE = 0x01, // 0x01 - passive
@@ -1490,7 +1492,8 @@ class TRINITY_DLL_SPEC Unit : public WorldObject
void SetCurrentCastedSpell(Spell * pSpell);
virtual void ProhibitSpellScholl(SpellSchoolMask /*idSchoolMask*/, uint32 /*unTimeMs*/ ) { }
void InterruptSpell(uint32 spellType, bool withDelayed = true, bool withInstant = true);
void InterruptSpell(CurrentSpellTypes spellType, bool withDelayed = true, bool withInstant = true);
void FinishSpell(CurrentSpellTypes spellType, bool ok = true);
// set withDelayed to true to account delayed spells as casted
// delayed+channeled spells are always accounted as casted
@@ -1501,11 +1504,11 @@ class TRINITY_DLL_SPEC Unit : public WorldObject
// delayed+channeled spells are always interrupted
void InterruptNonMeleeSpells(bool withDelayed, uint32 spellid = 0, bool withInstant = true);
Spell* GetCurrentSpell(CurrentSpellTypes spellType) const { return m_currentSpells[spellType]; }
Spell* GetCurrentSpell(uint32 spellType) const { return m_currentSpells[spellType]; }
Spell* FindCurrentSpellBySpellId(uint32 spell_id) const;
int32 GetCurrentSpellCastTime(uint32 spell_id) const;
Spell* m_currentSpells[CURRENT_MAX_SPELL];
uint32 m_addDmgOnce;
uint64 m_SummonSlot[MAX_SUMMON_SLOT];
uint64 m_ObjectSlot[4];
@@ -1907,6 +1910,8 @@ class TRINITY_DLL_SPEC Unit : public WorldObject
uint32 m_CombatTimer;
uint32 m_lastManaUse; // msecs
Spell* m_currentSpells[CURRENT_MAX_SPELL];
UnitVisibility m_Visibility;
Diminishing m_Diminishing;