Pet/Guardian AI hook re-organizing

Port From (https://github.com/TrinityCore/TrinityCore/commit/f7a7d02a7f50400cdc2be3c4722afeefe7efce80)
This commit is contained in:
hondacrx
2020-07-22 15:27:19 -04:00
parent 61b6606dce
commit db44970987
5 changed files with 74 additions and 53 deletions
+16 -7
View File
@@ -129,13 +129,24 @@ namespace Game.AI
if (me.GetVictim() != null)
return;
if (me.GetCreatureType() == CreatureType.NonCombatPet)
return;
if (me.HasReactState(ReactStates.Aggressive) && me.CanStartAttack(who, false))
AttackStart(who);
}
void _OnOwnerCombatInteraction(Unit target)
{
if (target == null || !me.IsAlive())
return;
if (!me.HasReactState(ReactStates.Passive) && me.CanStartAttack(target, true))
{
if (me.IsInCombat())
me.AddThreat(target, 0.0f);
else
AttackStart(target);
}
}
// Distract creature, if player gets too close while stealthed/prowling
public void TriggerAlert(Unit who)
{
@@ -433,8 +444,6 @@ namespace Game.AI
// Called when spell hits a target
public virtual void SpellHitTarget(Unit target, SpellInfo spell) {}
// Called when the creature is target of hostile action: swing, hostile spell landed, fear/etc)
public virtual void AttackedBy(Unit attacker) { }
public virtual bool IsEscorted() { return false; }
// Called when creature is spawned or respawned
@@ -455,10 +464,10 @@ namespace Game.AI
public virtual void ReceiveEmote(Player player, TextEmotes emoteId) { }
// Called when owner takes damage
public virtual void OwnerAttackedBy(Unit attacker) {}
public virtual void OwnerAttackedBy(Unit attacker) { _OnOwnerCombatInteraction(attacker); }
// Called when owner attacks something
public virtual void OwnerAttacked(Unit target) {}
public virtual void OwnerAttacked(Unit target) { _OnOwnerCombatInteraction(target); }
// called when the corpse of this creature gets removed
public virtual void CorpseRemoved(long respawnDelay) {}
+18 -23
View File
@@ -115,7 +115,7 @@ namespace Game.AI
// Aggressive - Allow auto select if owner or pet don't have a target
// Stay - Only pick from pet or owner targets / attackers so targets won't run by
// while chasing our owner. Don't do auto select.
// All other cases (ie: defensive) - Targets are assigned by AttackedBy(), OwnerAttackedBy(), OwnerAttacked(), etc.
// All other cases (ie: defensive) - Targets are assigned by DamageTaken(), OwnerAttackedBy(), OwnerAttacked(), etc.
Unit nextTarget = SelectNextTarget(me.HasReactState(ReactStates.Aggressive));
if (nextTarget)
@@ -307,16 +307,26 @@ namespace Game.AI
HandleReturnMovement(); // Return
}
public override void AttackStart(Unit victim)
public override void AttackStart(Unit target)
{
// Overrides Unit.AttackStart to correctly evaluate Pet states
// Overrides Unit.AttackStart to prevent pet from switching off its assigned target
if (target == null || target == me)
return;
if (me.GetVictim() != null && me.GetVictim().IsAlive())
return;
_AttackStart(target);
}
public void _AttackStart(Unit target)
{
// Check all pet states to decide if we can attack this target
if (!CanAIAttack(victim))
if (!CanAIAttack(target))
return;
// Only chase if not commanded to stay or if stay but commanded to attack
DoAttack(victim, (!me.GetCharmInfo().HasCommandState(CommandStates.Stay) || me.GetCharmInfo().IsCommandAttack()));
DoAttack(target, (!me.GetCharmInfo().HasCommandState(CommandStates.Stay) || me.GetCharmInfo().IsCommandAttack()));
}
public override void OwnerAttackedBy(Unit attacker)
@@ -324,7 +334,7 @@ namespace Game.AI
// Called when owner takes damage. This function helps keep pets from running off
// simply due to owner gaining aggro.
if (!attacker)
if (attacker == null || !me.IsAlive())
return;
// Passive pets don't do anything
@@ -345,7 +355,7 @@ namespace Game.AI
// that they need to assist
// Target might be null if called from spell with invalid cast targets
if (!target)
if (target == null || !me.IsAlive())
return;
// Passive pets don't do anything
@@ -620,23 +630,8 @@ namespace Game.AI
}
}
public override void AttackedBy(Unit attacker)
public override void DamageTaken(Unit attacker, ref uint damage)
{
// Called when pet takes damage. This function helps keep pets from running off
// simply due to gaining aggro.
if (!attacker)
return;
// Passive pets don't do anything
if (me.HasReactState( ReactStates.Passive))
return;
// Prevent pet from disengaging from current target
if (me.GetVictim() && me.GetVictim().IsAlive())
return;
// Continue to evaluate and attack if necessary
AttackStart(attacker);
}