ModernUO/Projects/UOContent/Mobiles/AI/PredatorAI.cs
Kamron Batman d4282367ba
fix: Fixes crash with combatant and mobs facing each other (#1644)
### Summary
- Fixes crash bugs with combatant checks in AI
- Fixes mobs not facing each other while in combat. (Old RunUO bug)
- Fixes predators not actually going into guard when their combatant dies.
- Adds missing checks for combatant in various AI.

Note: Much easier to understand changes if whitespace is off - https://github.com/modernuo/ModernUO/pull/1644/files?diff=split&w=1
2023-12-20 14:48:24 -08:00

113 lines
2.9 KiB
C#

namespace Server.Mobiles;
public class PredatorAI : BaseAI
{
public PredatorAI(BaseCreature m) : base(m)
{
}
public override bool DoActionWander()
{
if (m_Mobile.Combatant != null)
{
if (m_Mobile.Debug)
{
m_Mobile.DebugSay("I am hurt or being attacked, I kill him");
}
Action = ActionType.Combat;
}
else if (AcquireFocusMob(m_Mobile.RangePerception, m_Mobile.FightMode, true, false, true))
{
if (m_Mobile.Debug)
{
m_Mobile.DebugSay("There is something near, I go away");
}
Action = ActionType.Backoff;
}
else
{
base.DoActionWander();
}
return true;
}
public override bool DoActionCombat()
{
var combatant = m_Mobile.Combatant;
if (combatant == null || combatant.Deleted || combatant.Map != m_Mobile.Map || !combatant.Alive ||
combatant.IsDeadBondedPet)
{
if (m_Mobile.Debug)
{
m_Mobile.DebugSay("My combatant is gone, so my guard is up");
}
Action = ActionType.Guard;
return true;
}
if (!WalkMobileRange(combatant, 1, true, m_Mobile.RangeFight, m_Mobile.RangeFight))
{
if (m_Mobile.GetDistanceToSqrt(combatant) > m_Mobile.RangePerception + 1)
{
if (m_Mobile.Debug)
{
m_Mobile.DebugSay($"I cannot find {combatant.Name}");
}
Action = ActionType.Wander;
return true;
}
if (m_Mobile.Debug)
{
m_Mobile.DebugSay($"I should be closer to {combatant.Name}");
}
}
if (m_Mobile.TriggerAbility(MonsterAbilityTrigger.CombatAction, combatant))
{
if (m_Mobile.Debug)
{
m_Mobile.DebugSay($"I used my abilities on {combatant.Name}!");
}
}
return true;
}
public override bool DoActionBackoff()
{
if (m_Mobile.IsHurt() || m_Mobile.Combatant != null)
{
Action = ActionType.Combat;
}
else if (AcquireFocusMob(m_Mobile.RangePerception * 2, FightMode.Closest, true, false, true))
{
if (WalkMobileRange(m_Mobile.FocusMob, 1, false, m_Mobile.RangePerception, m_Mobile.RangePerception * 2))
{
if (m_Mobile.Debug)
{
m_Mobile.DebugSay("Well, here I am safe");
}
Action = ActionType.Wander;
}
}
else
{
if (m_Mobile.Debug)
{
m_Mobile.DebugSay("I have lost my focus, lets relax");
}
Action = ActionType.Wander;
}
return true;
}
}