Fixes walk-up aggro latency (up to a full 10 s of obliviousness) and hardens the reacquire gate so no state can silence acquisition, while turning `AcquireOnApproach` into the reaction-time knob for future per-creature intelligence tuning. ### Why `AcquireFocusMob` re-armed the 10 s `ReacquireDelay` **before** scanning, success or failure. A creature that scanned an empty room was blind for 10 s to a player walking up — walk-up aggro latency was uniform in 0..10 s. Waking from sector sleep stacked the AI timer's 0–3 s construction stagger on top. And `NextReacquireTime` is not serialized: on hosts whose tick counter starts negative (GCP pass-through), the 0 default blocked **all** acquisition shard-wide after a restart until the counter crossed zero. ### What **Event-driven reaction — `AcquireOnApproachDelay` (the intelligence gradient)** - The paragon `AcquireOnApproach` bool becomes a `TimeSpan` on every creature: an enemy moving inside `AcquireOnApproachRange` (10 for all creatures — on-screen reactive aggro; the periodic scan keeps the wide `RangePerception` sweep) *clamps* the next scan to at most the delay. Repeated steps cannot shorten it further — one scan per delay period, not per step or think. - `Zero` (paragons) also prods the AI timer: the ranked scan engages within a wheel turn — the old snap, minus the special-cased engage path. The target now comes from the normal FightMode ranking instead of whichever mobile happened to move, and the `Combatant == null` guard stops re-engage spam. - The 2 s default reads as "took a beat to notice you"; larger values are dumber; `ReacquireDelay` alone is the oblivious floor. Mover checks are the approach logic's `IsEnemy` + `CanBeHarmful` (so pets count and hidden movers are excluded via `CanSee`), with `IsEnemy` first to cheaply reject same-team wild creatures wandering past. The check rides the `OnMovement` callback every step already pays for — no polling added. **Gate correctness** - Every scan re-arms the full `ReacquireDelay`, success or failure (classic semantics; reaction time is the approach path, not the poll). - Self-healing by construction: a deadline further out than `ReacquireDelay` is an illegal state and reads as open — no wedged or wrapped value can silence acquisition beyond one delay period. - `NextReacquireTime` is seeded from a live tick on deserialize (the GCP negative-tick blackout). **AI timer wake** - Activation (sector wake, spawn, resurrection) starts within a 0–256 ms spread instead of the 0–3 s construction stagger, which read as lag. - The stagger's real job — keeping same-speed cohorts out of lock-step (the RunUO town artifact) — is now a zero-mean ±period/8 jitter on each **idle** think, so phases random-walk apart within seconds and can never re-lock. Instrumentation showed why a one-shot spread can't do this job: the timer wheel fires within ±1 ms, so with 10 creatures on a 500 ms period some pair collides on nearly the same phase ~75% of the time (birthday paradox) and then steps in the same loop iteration *forever*. Jitter is scoped to passive speed: engaged cadence stays exact, since pursuit timing anchors to real step times. **Debug** - The `AcquireFocusMob` scan message no longer re-arms the shared 5 s debug cooldown, which swallowed every AI's "I have detected X" transition line. **API change** for custom scripts: `AcquireOnApproach` (bool) → `AcquireOnApproachDelay` (TimeSpan). Documented in `content-patterns.md` § Target Acquisition, `runuo-migration-docs/09` + `11`, and the migration skill checklist. ### Tests `AcquisitionTests`: both scan outcomes honor `ReacquireDelay`; a 60 s-wedged gate still acquires; enemy movement clamps the deadline (same-team wild movers and out-of-range movers ignored); repeated movement cannot shorten below the delay; `Zero` opens the gate and prods without a direct engage. Full suite: 755 UOContent green.
211 lines
6.7 KiB
C#
211 lines
6.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Server;
|
|
using Server.Mobiles;
|
|
using Xunit;
|
|
|
|
namespace UOContent.Tests.Mobiles.AI;
|
|
|
|
// Pins the reacquire gate and the AcquireOnApproachDelay gradient: every scan re-arms the
|
|
// full ReacquireDelay; enemy movement clamps the deadline to the approach delay (Zero =
|
|
// prodded scan); an illegal deadline self-heals.
|
|
[Collection("Sequential Pathfinding Tests")]
|
|
public class AcquisitionTests : IDisposable
|
|
{
|
|
private readonly List<Mobile> _created = new();
|
|
|
|
public void Dispose()
|
|
{
|
|
foreach (var m in _created)
|
|
{
|
|
m?.Delete();
|
|
}
|
|
|
|
_created.Clear();
|
|
}
|
|
|
|
private sealed class WildStub : BaseCreature
|
|
{
|
|
public WildStub() : base(AIType.AI_Melee, FightMode.Closest, 16, 1) => Body = 0xC9;
|
|
|
|
public override void GetSpeeds(out double activeSpeed, out double passiveSpeed)
|
|
{
|
|
activeSpeed = 0.3;
|
|
passiveSpeed = 0.6;
|
|
}
|
|
}
|
|
|
|
private sealed class TargetStub : Mobile
|
|
{
|
|
public TargetStub() => Body = 0x190;
|
|
}
|
|
|
|
private WildStub Spawn(Map map, Point3D loc)
|
|
{
|
|
var bc = new WildStub();
|
|
bc.MoveToWorld(loc, map);
|
|
bc.AIObject.AITimer?.Stop();
|
|
_created.Add(bc);
|
|
return bc;
|
|
}
|
|
|
|
[Fact]
|
|
public void EmptyScan_HonorsReacquireDelay()
|
|
{
|
|
var map = Map.Maps[1];
|
|
Assert.NotNull(map);
|
|
map.GetAverageZ(1500, 1600, out _, out var z, out _);
|
|
|
|
var bc = Spawn(map, new Point3D(1500, 1600, (sbyte)z));
|
|
bc.NextReacquireTime = Core.TickCount;
|
|
|
|
Assert.False(bc.AIObject.AcquireFocusMob(bc.RangePerception, FightMode.Closest, false, false, true));
|
|
Assert.InRange(bc.NextReacquireTime - Core.TickCount, 5000, 10000);
|
|
}
|
|
|
|
[Fact]
|
|
public void WedgedGate_SelfHeals()
|
|
{
|
|
var map = Map.Maps[1];
|
|
Assert.NotNull(map);
|
|
map.GetAverageZ(1500, 1600, out _, out var z, out _);
|
|
|
|
var bc = Spawn(map, new Point3D(1500, 1600, (sbyte)z));
|
|
|
|
var target = new TargetStub();
|
|
target.DefaultMobileInit();
|
|
target.MoveToWorld(new Point3D(1497, 1600, (sbyte)z), map);
|
|
_created.Add(target);
|
|
|
|
// Illegal deadline (beyond ReacquireDelay): must read as open, not block forever.
|
|
bc.NextReacquireTime = Core.TickCount + 60000;
|
|
|
|
Assert.True(bc.AIObject.AcquireFocusMob(bc.RangePerception, FightMode.Closest, false, false, true));
|
|
Assert.Equal(target, bc.FocusMob);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false, 5, true)] // an enemy moving inside approach range (10) clamps the deadline
|
|
[InlineData(true, 5, false)] // a same-team wild creature is not an enemy — ignored
|
|
[InlineData(false, 12, false)] // inside RangePerception but outside approach range — poll only
|
|
[InlineData(false, 20, false)] // outside approach range (10) is ignored
|
|
public void MovementClampsScanDeadlineOnlyForEnemiesInRange(bool wildMover, int distance, bool notices)
|
|
{
|
|
var map = Map.Maps[1];
|
|
Assert.NotNull(map);
|
|
map.GetAverageZ(1500, 1600, out _, out var z, out _);
|
|
|
|
var bc = Spawn(map, new Point3D(1500, 1600, (sbyte)z));
|
|
bc.NextReacquireTime = Core.TickCount + 8000;
|
|
|
|
Mobile mover;
|
|
if (wildMover)
|
|
{
|
|
mover = Spawn(map, new Point3D(1500 - distance, 1600, (sbyte)z));
|
|
}
|
|
else
|
|
{
|
|
mover = new TargetStub { Player = true };
|
|
mover.DefaultMobileInit();
|
|
mover.MoveToWorld(new Point3D(1500 - distance, 1600, (sbyte)z), map);
|
|
_created.Add(mover);
|
|
}
|
|
|
|
bc.OnMovement(mover, new Point3D(1400, 1600, (sbyte)z));
|
|
|
|
var remaining = bc.NextReacquireTime - Core.TickCount;
|
|
|
|
if (notices)
|
|
{
|
|
// Clamped to the approach delay (2s), never opened outright.
|
|
Assert.InRange(remaining, 1, (long)bc.AcquireOnApproachDelay.TotalMilliseconds);
|
|
}
|
|
else
|
|
{
|
|
Assert.True(remaining > 5000);
|
|
}
|
|
}
|
|
|
|
private sealed class InstantStub : BaseCreature
|
|
{
|
|
public InstantStub() : base(AIType.AI_Melee, FightMode.Closest, 16, 1) => Body = 0xC9;
|
|
|
|
public override TimeSpan AcquireOnApproachDelay => TimeSpan.Zero;
|
|
|
|
public override void GetSpeeds(out double activeSpeed, out double passiveSpeed)
|
|
{
|
|
activeSpeed = 0.3;
|
|
passiveSpeed = 0.6;
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void ZeroApproachDelay_OpensGateImmediately()
|
|
{
|
|
var map = Map.Maps[1];
|
|
Assert.NotNull(map);
|
|
map.GetAverageZ(1500, 1600, out _, out var z, out _);
|
|
|
|
var bc = new InstantStub();
|
|
bc.MoveToWorld(new Point3D(1500, 1600, (sbyte)z), map);
|
|
bc.AIObject.AITimer?.Stop();
|
|
_created.Add(bc);
|
|
bc.NextReacquireTime = Core.TickCount + 8000;
|
|
|
|
var mover = new TargetStub { Player = true };
|
|
mover.DefaultMobileInit();
|
|
mover.MoveToWorld(new Point3D(1495, 1600, (sbyte)z), map);
|
|
_created.Add(mover);
|
|
|
|
bc.OnMovement(mover, new Point3D(1400, 1600, (sbyte)z));
|
|
|
|
// Zero = the gate opens and the AI is prodded to think now; no direct engage.
|
|
Assert.True(Core.TickCount - bc.NextReacquireTime >= 0);
|
|
Assert.Null(bc.Combatant);
|
|
Assert.True(bc.AIObject.AITimer.Running);
|
|
}
|
|
|
|
[Fact]
|
|
public void RepeatedMovement_DoesNotShortenBelowApproachDelay()
|
|
{
|
|
var map = Map.Maps[1];
|
|
Assert.NotNull(map);
|
|
map.GetAverageZ(1500, 1600, out _, out var z, out _);
|
|
|
|
var bc = Spawn(map, new Point3D(1500, 1600, (sbyte)z));
|
|
bc.NextReacquireTime = Core.TickCount + 8000;
|
|
|
|
var mover = new TargetStub { Player = true };
|
|
mover.DefaultMobileInit();
|
|
mover.MoveToWorld(new Point3D(1495, 1600, (sbyte)z), map);
|
|
_created.Add(mover);
|
|
|
|
bc.OnMovement(mover, new Point3D(1400, 1600, (sbyte)z));
|
|
var afterFirst = bc.NextReacquireTime;
|
|
|
|
bc.OnMovement(mover, new Point3D(1496, 1600, (sbyte)z));
|
|
|
|
Assert.Equal(afterFirst, bc.NextReacquireTime);
|
|
}
|
|
|
|
[Fact]
|
|
public void SuccessfulAcquire_HoldsFullDelay()
|
|
{
|
|
var map = Map.Maps[1];
|
|
Assert.NotNull(map);
|
|
map.GetAverageZ(1500, 1600, out _, out var z, out _);
|
|
|
|
var bc = Spawn(map, new Point3D(1500, 1600, (sbyte)z));
|
|
|
|
var target = new TargetStub();
|
|
target.DefaultMobileInit();
|
|
target.MoveToWorld(new Point3D(1497, 1600, (sbyte)z), map);
|
|
_created.Add(target);
|
|
|
|
bc.NextReacquireTime = Core.TickCount;
|
|
|
|
Assert.True(bc.AIObject.AcquireFocusMob(bc.RangePerception, FightMode.Closest, false, false, true));
|
|
Assert.Equal(target, bc.FocusMob);
|
|
Assert.True(bc.NextReacquireTime - Core.TickCount > 5000);
|
|
}
|
|
}
|