The retail movement-manager family the R4 MoveToManager port left as do-not-invent seams (decomp §9f/§9g). Faithful C# ports of retail's PositionManager facade + StickyManager + ConstraintManager + the TargetManager voyeur system, with full conformance tests. NO wiring yet — purely additive, no behavior change. Wiring (retiring TS-39 sticky + AP-79 target adapter) is R5-V2/V3. New Core classes (src/AcDream.Core/Physics/Motion/): - StickyManager (0x00555400): follow-a-target steering. adjust_offset's dense x87 mush decoded via ACE (StickyRadius 0.3, StickyTime 1.0, follow speed ×5 / fallback 15) — speed-clamped signed-distance steer + bounded turn-to-face; 1 s watchdog; Ok→initialized / non-Ok→teardown. - ConstraintManager (0x00556090): the server-position rubber-band leash. 90% IsFullyConstrained jump gate + grounded linear brake taper. Structural only — acdream never ARMS it (retail arms from SmartBox::HandleReceivedPosition, which acdream lacks, with two x87 constants BN elided). IsFullyConstrained stays false = TS-35 behavior; leash-arming + the unknown constants are a deferred issue. - PositionManager facade (0x00555160): lazy Sticky/Constraint + fan-out. - TargetManager (0x0051a370) + TargettedVoyeurInfo: the peer-to-peer voyeur subscription system (0.5 s throttle, 10 s staleness, send-on-drift-past-radius, dead-reckon GetInterpolatedPosition). A faithful superset of the AP-79 adapter — SetTarget subscribes ON the target; the target's HandleTargetting pushes updates back. - IPhysicsObjHost: the CPhysicsObj back-pointer seam (position/velocity/ radius/contact/GetObjectA + target-tracking fan-out) the App wires per entity in V2/V3. MotionDeltaFrame: mutable retail-Frame delta accumulator. Supporting: - TargetInfo extended to the full retail 10-field struct (additive defaults keep the R4 4-arg call sites compiling). - MoveToMath: signed CylinderDistanceNoZ, NormalizeCheckSmall, GlobalToLocalVec. - Rename: the misnamed AcDream.Core.Physics.PositionManager (a remote anim+interp per-frame combiner, NOT the retail facade) → RemoteMotion Combiner, freeing the name and removing the ambiguity that breaks every file importing both Physics + Physics.Motion (GameWindow will in V2/V3). Tests: 42 new conformance cases (Sticky/Constraint/Position facade + TargetManager incl. the full cross-entity voyeur round-trip). Full suite 4006 green (+2 skipped), no regressions. Decomp + ACE cross-ref + port plan: docs/research/2026-07-03-r5-managers/. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
220 lines
8.1 KiB
C#
220 lines
8.1 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_AtDeadline_UnsticksAndInterrupts()
|
|
{
|
|
var (self, target, sticky) = Setup();
|
|
self.CurTime = 100.0;
|
|
sticky.StickTo(target.Id, 0.5f, 1.0f); // deadline 101.0
|
|
int interruptsBefore = self.InterruptCurrentMovementCalls;
|
|
|
|
self.CurTime = 101.0;
|
|
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);
|
|
}
|
|
}
|