Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/game/Object/PetAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void PetAI::AttackStart(Unit* u)
// hope it doesn't start to leak memory without this :-/
// i_pet->Clear();
m_creature->UpdateSpeed(MOVE_RUN, false);
HandleMovementOnAttackStart(u);
// range and action choices handled by UpdateAI
inCombat = true;
}
}
Expand Down Expand Up @@ -227,6 +227,7 @@ void PetAI::UpdateAI(const uint32 diff)
typedef std::vector<std::pair<Unit*, Spell*> > TargetSpellList;
TargetSpellList targetSpellStore;

float maxOutOfRangeDistance = 0.0f; // track spells failing due to range
for (uint8 i = 0; i < m_creature->GetPetAutoSpellSize(); ++i)
{
uint32 spellID = m_creature->GetPetAutoSpellOnPos(i);
Expand Down Expand Up @@ -292,7 +293,7 @@ void PetAI::UpdateAI(const uint32 diff)

Spell* spell = new Spell(m_creature, spellInfo, false);

if (inCombat && !m_creature->hasUnitState(UNIT_STAT_FOLLOW) && spell->CanAutoCast(m_creature->getVictim()))
if (inCombat && spell->CanAutoCast(m_creature->getVictim()))
{
targetSpellStore.push_back(TargetSpellList::value_type(m_creature->getVictim(), spell));
continue;
Expand All @@ -317,6 +318,25 @@ void PetAI::UpdateAI(const uint32 diff)
break;
}
}

//if offensive spell wasn't usable, check WHY
if (!spellUsed && inCombat && m_creature->getVictim() && !IsPositiveSpell(spellInfo->Id))
{
SpellCastResult failReason = spell->CheckPetCast(m_creature->getVictim());
if (failReason == SPELL_FAILED_OUT_OF_RANGE)
{
SpellRangeEntry const* spellRange = sSpellRangeStore.LookupEntry(spellInfo->rangeIndex);
if (spellRange)
{
float spellMaxRange = GetSpellMaxRange(spellRange);
if (spellMaxRange >= 7.0f)
{
maxOutOfRangeDistance = spellMaxRange - 1.0f;
}
}
}
}

if (!spellUsed)
{
delete spell;
Expand Down Expand Up @@ -359,6 +379,21 @@ void PetAI::UpdateAI(const uint32 diff)

spell->prepare(&targets);
}
else if (maxOutOfRangeDistance > 0.0f && inCombat && m_creature->getVictim() && (m_attackDistance != maxOutOfRangeDistance))
{
// spells failed due to range - move closer
m_attackDistance = maxOutOfRangeDistance;
HandleMovementOnAttackStart(m_creature->getVictim());
}
else if (inCombat && m_creature->getVictim())
{
// No castable spells at all - switch to melee
if (m_attackDistance > 0.0f || !m_creature->hasUnitState(UNIT_STAT_CHASE))
{
m_attackDistance = 0.0f;
HandleMovementOnAttackStart(m_creature->getVictim());
}
}

// deleted cached Spell objects
for (TargetSpellList::const_iterator itr = targetSpellStore.begin(); itr != targetSpellStore.end(); ++itr)
Expand Down