Core/AI: PetAI method reordering

(cherry picked from commit acbfac7b13)
This commit is contained in:
ccrs
2019-05-17 00:18:09 +02:00
committed by Shauren
parent cb9a1c38f1
commit 86b4aa01bb
2 changed files with 74 additions and 73 deletions

View File

@@ -48,40 +48,10 @@ PetAI::PetAI(Creature* creature, uint32 scriptId) : CreatureAI(creature, scriptI
{
if (!me->GetCharmInfo())
throw InvalidAIException("Creature doesn't have a valid charm info");
UpdateAllies();
}
bool PetAI::NeedToStop()
{
// This is needed for charmed creatures, as once their target was reset other effects can trigger threat
if (me->IsCharmed() && me->GetVictim() == me->GetCharmer())
return true;
// dont allow pets to follow targets far away from owner
if (Unit* owner = me->GetCharmerOrOwner())
if (owner->GetExactDist(me) >= (owner->GetVisibilityRange()-10.0f))
return true;
return !me->IsValidAttackTarget(me->GetVictim());
}
void PetAI::StopAttack()
{
if (!me->IsAlive())
{
me->GetMotionMaster()->Clear();
me->GetMotionMaster()->MoveIdle();
me->CombatStop();
return;
}
me->AttackStop();
me->InterruptNonMeleeSpells(false);
me->GetCharmInfo()->SetIsCommandAttack(false);
ClearCharmInfoFlags();
HandleReturnMovement();
}
void PetAI::UpdateAI(uint32 diff)
{
if (!me->IsAlive() || !me->GetCharmInfo())
@@ -260,46 +230,6 @@ void PetAI::UpdateAI(uint32 diff)
}
void PetAI::UpdateAllies()
{
_updateAlliesTimer = 10 * IN_MILLISECONDS; // update friendly targets every 10 seconds, lesser checks increase performance
Unit* owner = me->GetCharmerOrOwner();
if (!owner)
return;
Group* group = nullptr;
if (Player* player = owner->ToPlayer())
group = player->GetGroup();
// only pet and owner/not in group->ok
if (_allySet.size() == 2 && !group)
return;
// owner is in group; group members filled in already (no raid -> subgroupcount = whole count)
if (group && !group->isRaidGroup() && _allySet.size() == (group->GetMembersCount() + 2))
return;
_allySet.clear();
_allySet.insert(me->GetGUID());
if (group) // add group
{
for (GroupReference* itr = group->GetFirstMember(); itr != nullptr; itr = itr->next())
{
Player* Target = itr->GetSource();
if (!Target || !Target->IsInMap(owner) || !group->SameSubGroup(owner->ToPlayer(), Target))
continue;
if (Target->GetGUID() == owner->GetGUID())
continue;
_allySet.insert(Target->GetGUID());
}
}
else // remove group
_allySet.insert(owner->GetGUID());
}
void PetAI::KilledUnit(Unit* victim)
{
// Called from Unit::Kill() in case where pet or owner kills something
@@ -624,6 +554,77 @@ void PetAI::ReceiveEmote(Player* player, uint32 emote)
}
}
bool PetAI::NeedToStop()
{
// This is needed for charmed creatures, as once their target was reset other effects can trigger threat
if (me->IsCharmed() && me->GetVictim() == me->GetCharmer())
return true;
// dont allow pets to follow targets far away from owner
if (Unit* owner = me->GetCharmerOrOwner())
if (owner->GetExactDist(me) >= (owner->GetVisibilityRange() - 10.0f))
return true;
return !me->IsValidAttackTarget(me->GetVictim());
}
void PetAI::StopAttack()
{
if (!me->IsAlive())
{
me->GetMotionMaster()->Clear();
me->GetMotionMaster()->MoveIdle();
me->CombatStop();
return;
}
me->AttackStop();
me->InterruptNonMeleeSpells(false);
me->GetCharmInfo()->SetIsCommandAttack(false);
ClearCharmInfoFlags();
HandleReturnMovement();
}
void PetAI::UpdateAllies()
{
_updateAlliesTimer = 10 * IN_MILLISECONDS; // update friendly targets every 10 seconds, lesser checks increase performance
Unit* owner = me->GetCharmerOrOwner();
if (!owner)
return;
Group* group = nullptr;
if (Player* player = owner->ToPlayer())
group = player->GetGroup();
// only pet and owner/not in group->ok
if (_allySet.size() == 2 && !group)
return;
// owner is in group; group members filled in already (no raid -> subgroupcount = whole count)
if (group && !group->isRaidGroup() && _allySet.size() == (group->GetMembersCount() + 2))
return;
_allySet.clear();
_allySet.insert(me->GetGUID());
if (group) // add group
{
for (GroupReference* itr = group->GetFirstMember(); itr != nullptr; itr = itr->next())
{
Player* Target = itr->GetSource();
if (!Target || !Target->IsInMap(owner) || !group->SameSubGroup(owner->ToPlayer(), Target))
continue;
if (Target->GetGUID() == owner->GetGUID())
continue;
_allySet.insert(Target->GetGUID());
}
}
else // remove group
_allySet.insert(owner->GetGUID());
}
void PetAI::ClearCharmInfoFlags()
{
CharmInfo* ci = me->GetCharmInfo();

View File

@@ -29,11 +29,11 @@ typedef std::vector<std::pair<Unit*, Spell*>> TargetSpellList;
class TC_GAME_API PetAI : public CreatureAI
{
public:
static int32 Permissible(Creature const* creature);
explicit PetAI(Creature* creature, uint32 scriptId = {});
void UpdateAI(uint32) override;
static int32 Permissible(Creature const* creature);
void KilledUnit(Unit* /*victim*/) override;
// only start attacking if not attacking something else already
void AttackStart(Unit* target) override;