acdream/tests/AcDream.Core.Tests/Physics/Motion/PositionManagerFacadeTests.cs
Erik 3d89446d98 feat(physics): R5-V1 — port PositionManager/Sticky/Constraint + TargetManager (Core, unwired)
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>
2026-07-03 19:34:49 +02:00

112 lines
3.5 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="PositionManager"/> facade (retail
/// 0x00555160-0x005553d0). Lazy sub-manager creation + fan-out. (Test class is
/// suffixed "Facade" to read distinctly from the renamed
/// <c>RemoteMotionCombiner</c> combiner tests.)
/// </summary>
public sealed class PositionManagerFacadeTests
{
private static (R5Host self, R5Host target, PositionManager pm) Setup()
{
var world = new Dictionary<uint, R5Host>();
var self = new R5Host(10u, world);
var target = new R5Host(20u, world);
return (self, target, new PositionManager(self));
}
[Fact]
public void UnStick_BeforeAnyStick_IsNoOp()
{
var (_, _, pm) = Setup();
pm.UnStick(); // no sticky manager yet — must not throw
Assert.Null(pm.Sticky);
Assert.Equal(0u, pm.GetStickyObjectId());
}
[Fact]
public void StickTo_LazilyCreatesSticky_AndForwards()
{
var (self, target, pm) = Setup();
pm.StickTo(target.Id, radius: 0.5f, height: 1.0f);
Assert.NotNull(pm.Sticky);
Assert.Equal(target.Id, pm.GetStickyObjectId());
}
[Fact]
public void IsFullyConstrained_FalseWhenNoConstraintManager()
{
var (_, _, pm) = Setup();
Assert.False(pm.IsFullyConstrained());
Assert.Null(pm.Constraint);
}
[Fact]
public void ConstrainTo_LazilyCreatesConstraint()
{
var (self, _, pm) = Setup();
self.SetOrigin(Vector3.Zero);
pm.ConstrainTo(new Position(1u, new Vector3(5f, 0f, 0f), Quaternion.Identity), 2f, 10f);
Assert.NotNull(pm.Constraint);
Assert.True(pm.Constraint!.IsConstrained);
}
[Fact]
public void HandleUpdateTarget_ForwardsToSticky()
{
var (self, target, pm) = Setup();
pm.StickTo(target.Id, 0.5f, 1.0f);
var tp = new Position(1u, new Vector3(2f, 0f, 0f), Quaternion.Identity);
pm.HandleUpdateTarget(new TargetInfo(target.Id, TargetStatus.Ok, tp, tp));
Assert.True(pm.Sticky!.Initialized);
}
[Fact]
public void AdjustOffset_ChainsStickyThenConstraint()
{
var (self, target, pm) = Setup();
self.Radius = 0.5f;
self.MinterpMaxSpeed = 1.0f;
self.InContact = true;
// Sticky toward +X: produces an offset the constraint then tapers.
pm.StickTo(target.Id, 0.5f, 1.0f);
target.SetOrigin(new Vector3(5f, 0f, 0f));
var tp = new Position(1u, new Vector3(5f, 0f, 0f), Quaternion.Identity);
pm.HandleUpdateTarget(new TargetInfo(target.Id, TargetStatus.Ok, tp, tp));
// Arm a constraint whose band tapers by 0.5.
self.SetOrigin(Vector3.Zero);
pm.ConstrainTo(new Position(1u, new Vector3(6f, 0f, 0f), Quaternion.Identity),
startDistance: 2f, maxDistance: 10f); // offset 6 → taper (10-6)/(10-2)=0.5
var frame = new MotionDeltaFrame();
pm.AdjustOffset(frame, quantum: 0.1);
// Sticky writes +0.5 X; constraint tapers by 0.5 → 0.25.
Assert.Equal(0.25f, frame.Origin.X, 3);
}
[Fact]
public void UseTime_DrivesStickyTimeout()
{
var (self, target, pm) = Setup();
self.CurTime = 100.0;
pm.StickTo(target.Id, 0.5f, 1.0f); // deadline 101
self.CurTime = 101.0;
pm.UseTime();
Assert.Equal(0u, pm.GetStickyObjectId()); // unstuck
}
}