ModernUO/Projects/UOContent/Mobiles/AI/AnimalAI.cs
Kamron Batman 3332fabc39
fix: Reverts to RunUO speeds, fixes direction glitching, adds movement speed interpolation (#1695)
> [!IMPORTANT]  
> Please read through these changes, as they changed certain expectations for how the internal AI thinking/movement work.
> Note that some mobs still don't have smooth movement on ClassicUO due to how the client handles animations/movement.

### Summary
- **Important Change**: Mobs will now move at a more regular pace. If the OnThink results in a move, but the cooldown would otherwise prevent the move, then the movement is scheduled on another timer.
- Added a 400ms delay to mobs turning to face a player to attack in order to avoid glitching between moving and turning.
- Reverted AI thinking speeds back to RunUO specific speeds.
- Reverted the AI thinking to moving conversion delays back to RunUO.
- For thinking speeds that are not exactly the predefined speeds from RunUO, there is a new calculation to determine the correct conversion to movement speed. This stops speeds like `0.35` from being faster than `0.3`

> [!NOTE]  
> **Developer Note**
> BaseAI.CheckMove() no longer contains the check for whether or not a mob is on movement cooldown. This function can now be overwritten without causing issues to figuring out that cooldown.
2024-03-18 14:13:41 -07:00

158 lines
4.3 KiB
C#

// Ideas
// When you run on animals the panic
// When if (distance < 8 && Utility.RandomDouble() * Math.Sqrt( (8 - distance) / 6 ) >= incoming.Skills.AnimalTaming.Value)
// More your close, the more it can panic
/*
* AnimalHunterAI, AnimalHidingAI, AnimalDomesticAI...
*
*/
namespace Server.Mobiles;
public class AnimalAI : BaseAI
{
public AnimalAI(BaseCreature m) : base(m)
{
}
public override bool DoActionWander()
{
// New, only flee @ 10%
var hitPercent = (double)m_Mobile.Hits / m_Mobile.HitsMax;
if (!m_Mobile.Summoned && !m_Mobile.Controlled && hitPercent < 0.1 && m_Mobile.CanFlee) // Less than 10% health
{
if (m_Mobile.Debug)
{
m_Mobile.DebugSay("I am low on health!");
}
Action = ActionType.Flee;
}
else if (AcquireFocusMob(m_Mobile.RangePerception, m_Mobile.FightMode, false, false, true))
{
if (m_Mobile.Debug)
{
m_Mobile.DebugSay($"I have detected {m_Mobile.FocusMob.Name}, attacking");
}
m_Mobile.Combatant = m_Mobile.FocusMob;
Action = ActionType.Combat;
}
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!");
}
Action = ActionType.Wander;
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}");
}
}
else if (Core.TickCount - m_Mobile.LastMoveTime > 400)
{
m_Mobile.Direction = m_Mobile.GetDirectionTo(combatant);
}
if (!m_Mobile.Controlled && !m_Mobile.Summoned && m_Mobile.CanFlee)
{
var hitPercent = (double)m_Mobile.Hits / m_Mobile.HitsMax;
if (hitPercent <= 0.1)
{
if (m_Mobile.Debug)
{
m_Mobile.DebugSay("I am low on health!");
}
Action = ActionType.Flee;
return true;
}
}
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()
{
var hitPercent = (double)m_Mobile.Hits / m_Mobile.HitsMax;
if (!m_Mobile.Summoned && !m_Mobile.Controlled && hitPercent < 0.1 && m_Mobile.CanFlee) // Less than 10% health
{
Action = ActionType.Flee;
}
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;
}
public override bool DoActionFlee()
{
AcquireFocusMob(m_Mobile.RangePerception * 2, m_Mobile.FightMode, true, false, true);
m_Mobile.FocusMob ??= m_Mobile.Combatant;
return base.DoActionFlee();
}
}