acdream/tests/AcDream.Core.Tests/Physics/Motion/StickyManagerTests.cs
Erik 5bd2b8bc8b fix(#171): R5-V3 — bind sticky melee (StickyManager live) + real arrival radii
Group-melee interpenetration + facing drift: the R5-V1-ported
StickyManager/PositionManager were Core-only — the StickTo/Unstick seams
were unbound and every arrival radius was 0, so ACE's Sticky|UseSpheres
melee chases closed ~one body-radius too deep and froze at stale arrival
poses until the next wire re-arm. Retires TS-39.

Wiring (anchors re-verified against the named decomp this session):
- EntityPhysicsHost owns a PositionManager; HandleUpdateTarget fans
  MoveToManager then PositionManager (CPhysicsObj::HandleUpdateTarget
  0x00512bc0 order).
- Seams bound remote + player: MoveToManager.StickTo (BeginNextNode
  sticky arrival @0x00529d3a), Unstick (PerformMovement head), and
  MotionInterpreter.UnstickFromObject (UM funnel head, 0x0050eaea).
- AdjustOffset at the retail UpdatePositionInternal slot (@0x00512d0e):
  NPC branch composes pre-sweep (steer is swept by ResolveWithTransition);
  remote-player branch chains the combiner offset through the shared
  delta frame (the interp stage) so sticky OVERWRITES when armed
  (0x00555430 assigns m_fOrigin, not accumulates); player inside the
  30 Hz physics quantum before UpdatePhysicsInternal.
- UseTime (the 1 s lease watchdog) at the UpdateObjectInternal tail
  (@0x005159b3): unconditional per remote; player gated on the physics
  tick (retail's MinQuantum gate skips UseTime too).
- Real setup cylsphere radii (CPartArray::GetRadius/GetHeight
  0x005180a0/0x005180b0 = setup radius/height x ObjScale from the spawn
  record): own via EnsureRemoteMotionBindings + player wiring; target via
  RouteServerMoveTo AND the speculative use-walk install (retail resolves
  the target PartArray at EVERY MoveToObject site — ACE PhysicsObj.cs:951).
- Teardown parity: exit_world (0x00514e60) UnStick + ClearTarget before
  the ExitWorld notify; player teleport fires teleport_hook's tail
  (UnStick in SetPosition + EntityPhysicsHost.NotifyTeleported =
  ClearTarget + NotifyVoyeurOfEvent(Teleported) @0x00514f1b) so mobs
  stuck to the player drop their sticks on a recall.
- SERVERVEL arbitration also yields to a stuck entity (same starvation
  class as the #170 fix — sticky owns the between-snap translation).
- StickyManager.UseTime aligned to retail's strict > deadline
  (0x00555626; ACE >): two V1 tests had pinned the >= edge — corrected.

Register: TS-39 deleted; TS-41 narrowed (stickyArmed gate); TS-43 added
(remote teleport_hook gap — self-corrects within the 1 s lease); AP-23
narrowed (real radii at the speculative site; only the use-radius
buckets remain invented).

Conformance: 2 new full-stack sticky scenarios in
RemoteChaseEndToEndHarnessTests (arrive -> stick -> strafing-target
gap+facing track -> lease expiry; unstick-on-rearm -> re-stick).
Full suite 4038 green.

Pre-commit adversarial diff review (3 lenses + per-finding refuters)
confirmed and fixed 4 findings: ObjScale-dead radius read, player
UseTime order inversion, missing teleport voyeur notify, speculative-
site radius asymmetry.

Awaiting the user visual gate: pack melee side-by-side vs retail
(attackers reshuffle + keep facing; some overlap is ACE-server-side).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-04 23:46:17 +02:00

227 lines
8.4 KiB
C#

using System.Collections.Generic;
using System.Numerics;
using AcDream.Core.Physics;
using AcDream.Core.Physics.Motion;
using Xunit;
namespace AcDream.Core.Tests.Physics.Motion;
/// <summary>
/// R5 conformance — <see cref="StickyManager"/> (retail 0x00555400-0x00555866).
/// Lifecycle (StickTo/UnStick/timeout/HandleUpdateTarget) + the decoded
/// <c>adjust_offset</c> steering math (port-plan §2a).
/// </summary>
public sealed class StickyManagerTests
{
private static (R5Host self, R5Host target, StickyManager sticky) Setup(
uint targetId = 20u, float targetRadius = 0.5f)
{
var world = new Dictionary<uint, R5Host>();
var self = new R5Host(10u, world);
var target = new R5Host(targetId, world);
var sticky = new StickyManager(self);
return (self, target, sticky);
}
private static StickyManager StuckAndInitialized(
R5Host self, R5Host target, Vector3 targetOrigin, float targetRadius = 0.5f)
{
var sticky = new StickyManager(self);
sticky.StickTo(target.Id, targetRadius, targetHeight: 1.0f);
target.SetOrigin(targetOrigin);
var tp = new Position(1u, targetOrigin, Quaternion.Identity);
sticky.HandleUpdateTarget(new TargetInfo(target.Id, TargetStatus.Ok, tp, tp));
return sticky;
}
[Fact]
public void StickTo_SetsTargetAndTimeout_AndSubscribesVoyeurOnTarget()
{
var (self, target, sticky) = Setup();
self.CurTime = 100.0;
sticky.StickTo(target.Id, targetRadius: 0.5f, targetHeight: 2.0f);
Assert.Equal(target.Id, sticky.TargetId);
Assert.Equal(0.5f, sticky.TargetRadius);
Assert.False(sticky.Initialized);
Assert.Equal(101.0, sticky.StickyTimeoutTime); // now + StickyTime(1.0)
// set_target(0, target, 0.5, 0.5) → watcher subscribes ON the target.
Assert.NotNull(target.TargetManagerOrNull);
Assert.True(target.TargetManager.VoyeurTable!.ContainsKey(self.Id));
}
[Fact]
public void HandleUpdateTarget_Ok_MarksInitializedAndCachesPosition()
{
var (self, target, sticky) = Setup();
sticky.StickTo(target.Id, 0.5f, 1.0f);
var tp = new Position(1u, new Vector3(3f, 0f, 0f), Quaternion.Identity);
sticky.HandleUpdateTarget(new TargetInfo(target.Id, TargetStatus.Ok, tp, tp));
Assert.True(sticky.Initialized);
Assert.Equal(new Vector3(3f, 0f, 0f), sticky.TargetPosition.Frame.Origin);
}
[Fact]
public void HandleUpdateTarget_ForeignObject_Ignored()
{
var (self, target, sticky) = Setup();
sticky.StickTo(target.Id, 0.5f, 1.0f);
var tp = new Position(1u, Vector3.Zero, Quaternion.Identity);
sticky.HandleUpdateTarget(new TargetInfo(999u, TargetStatus.Ok, tp, tp));
Assert.False(sticky.Initialized);
Assert.Equal(target.Id, sticky.TargetId); // still stuck to the real target
}
[Fact]
public void HandleUpdateTarget_NonOkStatus_TearsDown()
{
var (self, target, sticky) = Setup();
sticky.StickTo(target.Id, 0.5f, 1.0f);
int interruptsBefore = self.InterruptCurrentMovementCalls;
var tp = new Position(1u, Vector3.Zero, Quaternion.Identity);
sticky.HandleUpdateTarget(new TargetInfo(target.Id, TargetStatus.ExitWorld, tp, tp));
Assert.Equal(0u, sticky.TargetId);
Assert.False(sticky.Initialized);
Assert.Equal(interruptsBefore + 1, self.InterruptCurrentMovementCalls);
}
[Fact]
public void UseTime_BeforeDeadline_KeepsStick()
{
var (self, target, sticky) = Setup();
self.CurTime = 100.0;
sticky.StickTo(target.Id, 0.5f, 1.0f); // deadline 101.0
self.CurTime = 100.9;
sticky.UseTime();
Assert.Equal(target.Id, sticky.TargetId);
}
[Fact]
public void UseTime_PastDeadline_UnsticksAndInterrupts()
{
var (self, target, sticky) = Setup();
self.CurTime = 100.0;
sticky.StickTo(target.Id, 0.5f, 1.0f); // deadline 101.0
// AT the deadline the stick survives — retail 0x00555626 tears down
// strictly AFTER it (`test ah,0x41`: C0|C3 = less-or-equal keeps;
// ACE `>` too). The R5-V1 pin of `>=` was the wrong value.
self.CurTime = 101.0;
sticky.UseTime();
Assert.Equal(target.Id, sticky.TargetId);
int interruptsBefore = self.InterruptCurrentMovementCalls;
self.CurTime = 101.001;
sticky.UseTime();
Assert.Equal(0u, sticky.TargetId);
Assert.Equal(interruptsBefore + 1, self.InterruptCurrentMovementCalls);
}
[Fact]
public void ReStick_TearsDownPreviousBeforeSettingNew()
{
var world = new Dictionary<uint, R5Host>();
var self = new R5Host(10u, world);
var a = new R5Host(20u, world);
var b = new R5Host(21u, world);
var sticky = new StickyManager(self);
sticky.StickTo(a.Id, 0.5f, 1.0f);
int interruptsBefore = self.InterruptCurrentMovementCalls;
sticky.StickTo(b.Id, 0.5f, 1.0f);
Assert.Equal(b.Id, sticky.TargetId);
Assert.Equal(interruptsBefore + 1, self.InterruptCurrentMovementCalls); // old stick torn down
}
[Fact]
public void AdjustOffset_NotInitialized_IsNoOp()
{
var (self, target, sticky) = Setup();
sticky.StickTo(target.Id, 0.5f, 1.0f); // no HandleUpdateTarget → not initialized
var frame = new MotionDeltaFrame { Origin = new Vector3(9f, 9f, 9f) };
sticky.AdjustOffset(frame, 0.1);
Assert.Equal(new Vector3(9f, 9f, 9f), frame.Origin); // untouched
}
[Fact]
public void AdjustOffset_SteersTowardTarget_ClampedToStep()
{
var world = new Dictionary<uint, R5Host>();
var self = new R5Host(10u, world) { Radius = 0.5f, MinterpMaxSpeed = 1.0f };
var target = new R5Host(20u, world);
var sticky = StuckAndInitialized(self, target, new Vector3(5f, 0f, 0f));
var frame = new MotionDeltaFrame();
sticky.AdjustOffset(frame, quantum: 0.1);
// dir = +X (east); speed = 1.0 * 5 = 5; delta = 5 * 0.1 = 0.5 (< dist 3.7).
Assert.Equal(0.5f, frame.Origin.X, 3);
Assert.Equal(0f, frame.Origin.Y, 3);
Assert.Equal(0f, frame.Origin.Z, 3); // horizontal-only
// heading toward +X (east) = 90° compass.
Assert.Equal(90f, frame.GetHeading(), 1);
}
[Fact]
public void AdjustOffset_TooClose_BacksOff_SignedDistance()
{
var world = new Dictionary<uint, R5Host>();
var self = new R5Host(10u, world) { Radius = 0.5f, MinterpMaxSpeed = 1.0f };
var target = new R5Host(20u, world);
// centerDist 0.9, cyl = 0.9-0.5-0.5 = -0.1, minus 0.3 → dist = -0.4.
var sticky = StuckAndInitialized(self, target, new Vector3(0.9f, 0f, 0f));
var frame = new MotionDeltaFrame();
sticky.AdjustOffset(frame, quantum: 0.1);
// delta = 5*0.1 = 0.5 >= |dist|=0.4 → delta = dist = -0.4; dir +X * -0.4 → back off (-X).
Assert.Equal(-0.4f, frame.Origin.X, 3);
}
[Fact]
public void AdjustOffset_UsesCachedPosition_WhenTargetUnresolvable()
{
var world = new Dictionary<uint, R5Host>();
var self = new R5Host(10u, world) { Radius = 0.5f, MinterpMaxSpeed = 1.0f };
var target = new R5Host(20u, world);
var sticky = new StickyManager(self);
sticky.StickTo(target.Id, 0.5f, 1.0f);
// Cache a position via HandleUpdateTarget, then make the target vanish.
var tp = new Position(1u, new Vector3(4f, 0f, 0f), Quaternion.Identity);
sticky.HandleUpdateTarget(new TargetInfo(target.Id, TargetStatus.Ok, tp, tp));
target.Resolvable = false; // GetObjectA(target) → null → fall back to cached
var frame = new MotionDeltaFrame();
sticky.AdjustOffset(frame, quantum: 0.1);
Assert.Equal(0.5f, frame.Origin.X, 3); // still steers toward cached (4,0,0)
}
[Fact]
public void AdjustOffset_NoMinterp_UsesFallbackSpeed()
{
var world = new Dictionary<uint, R5Host>();
var self = new R5Host(10u, world) { Radius = 0.5f, MinterpMaxSpeed = null };
var target = new R5Host(20u, world);
var sticky = StuckAndInitialized(self, target, new Vector3(50f, 0f, 0f));
var frame = new MotionDeltaFrame();
sticky.AdjustOffset(frame, quantum: 0.1);
// fallback speed 15 → delta = 15 * 0.1 = 1.5 (dist ≈ 49 → not clamped).
Assert.Equal(1.5f, frame.Origin.X, 3);
}
}