refactor(runtime): own local movement and outbound cadence
Move the canonical local movement controller, body/motion managers, object clock, movement wire data, and MTS/jump/AP sender into AcDream.Runtime. Replace process skill defaults with typed Runtime character options, make graphical and direct commands borrow one autorun owner, retain the construction-time PartArray seam, and include movement in terminal ownership convergence. Preserve the accepted pre-inbound movement/jump and post-inbound autonomous-position order while moving the exact packet/cadence fixtures into Runtime tests. Add graphical/direct parity, two-instance isolation, teardown, allocation, architecture, and divergence-path coverage. Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
parent
3456dff038
commit
aa3f4a60f8
36 changed files with 878 additions and 276 deletions
|
|
@ -0,0 +1,114 @@
|
|||
using System.Buffers.Binary;
|
||||
using System.Net;
|
||||
using System.Numerics;
|
||||
using AcDream.Core.Net;
|
||||
using AcDream.Core.Net.Messages;
|
||||
using AcDream.Core.Physics;
|
||||
using AcDream.Runtime.Gameplay;
|
||||
|
||||
namespace AcDream.Runtime.Tests.Gameplay;
|
||||
|
||||
public sealed class LocalPlayerImmediatePositionTests
|
||||
{
|
||||
[Fact]
|
||||
public void ForcePositionAcknowledgement_SendsOneExactAutonomousPositionAndStampsIt()
|
||||
{
|
||||
using var session = new WorldSession(
|
||||
new IPEndPoint(IPAddress.Loopback, 9000));
|
||||
session.PublishAcceptedLocalPhysicsTimestamps(
|
||||
instance: 11,
|
||||
serverControlledMove: 12,
|
||||
teleport: 13,
|
||||
forcePosition: 14);
|
||||
var sent = new List<byte[]>();
|
||||
session.GameActionCapture = sent.Add;
|
||||
|
||||
PlayerMovementController player = GroundedPlayer();
|
||||
Position preTurnCanonical = player.CellPosition;
|
||||
Quaternion rotation = Quaternion.Normalize(
|
||||
Quaternion.CreateFromAxisAngle(Vector3.UnitZ, 0.7f));
|
||||
player.SetBodyOrientation(rotation);
|
||||
Assert.NotEqual(rotation, preTurnCanonical.Frame.Orientation);
|
||||
Assert.True(player.TryGetOutboundPosition(out Position outboundPosition));
|
||||
var outbound = new LocalPlayerOutboundController((_, _, _, _, _, _) => { });
|
||||
|
||||
outbound.SendImmediatePosition(session, player);
|
||||
|
||||
byte[] body = Assert.Single(sent);
|
||||
Assert.Equal(56, body.Length);
|
||||
Assert.Equal(AutonomousPosition.GameActionOpcode, U32(body, 0));
|
||||
Assert.Equal(1u, U32(body, 4));
|
||||
Assert.Equal(AutonomousPosition.AutonomousPositionAction, U32(body, 8));
|
||||
Assert.Equal(outboundPosition.ObjCellId, U32(body, 12));
|
||||
Assert.Equal(outboundPosition.Frame.Origin.X, F32(body, 16));
|
||||
Assert.Equal(outboundPosition.Frame.Origin.Y, F32(body, 20));
|
||||
Assert.Equal(outboundPosition.Frame.Origin.Z, F32(body, 24));
|
||||
Assert.Equal(rotation.W, F32(body, 28), precision: 6);
|
||||
Assert.Equal(rotation.X, F32(body, 32), precision: 6);
|
||||
Assert.Equal(rotation.Y, F32(body, 36), precision: 6);
|
||||
Assert.Equal(rotation.Z, F32(body, 40), precision: 6);
|
||||
Assert.Equal((ushort)11, U16(body, 44));
|
||||
Assert.Equal((ushort)12, U16(body, 46));
|
||||
Assert.Equal((ushort)13, U16(body, 48));
|
||||
Assert.Equal((ushort)14, U16(body, 50));
|
||||
Assert.Equal((byte)1, body[52]);
|
||||
Assert.True(player.TryGetOutboundPosition(out Position currentOutbound));
|
||||
Assert.False(player.ShouldSendPositionEvent(
|
||||
currentOutbound,
|
||||
player.ContactPlane,
|
||||
player.SimTimeSeconds));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ImmediateAcknowledgement_RequiresSessionPlayerAndGroundContact()
|
||||
{
|
||||
using var session = new WorldSession(
|
||||
new IPEndPoint(IPAddress.Loopback, 9000));
|
||||
var sent = new List<byte[]>();
|
||||
session.GameActionCapture = sent.Add;
|
||||
var outbound = new LocalPlayerOutboundController((_, _, _, _, _, _) => { });
|
||||
PlayerMovementController grounded = GroundedPlayer();
|
||||
|
||||
outbound.SendImmediatePosition(null, grounded);
|
||||
outbound.SendImmediatePosition(session, null);
|
||||
outbound.SendImmediatePosition(
|
||||
session,
|
||||
new PlayerMovementController(new PhysicsEngine()));
|
||||
|
||||
Assert.Empty(sent);
|
||||
}
|
||||
|
||||
private static PlayerMovementController GroundedPlayer()
|
||||
{
|
||||
var engine = new PhysicsEngine();
|
||||
var heights = new byte[81];
|
||||
Array.Fill(heights, (byte)50);
|
||||
var heightTable = new float[256];
|
||||
for (int i = 0; i < heightTable.Length; i++)
|
||||
heightTable[i] = i;
|
||||
engine.AddLandblock(
|
||||
0xA9B4FFFFu,
|
||||
new TerrainSurface(heights, heightTable),
|
||||
Array.Empty<CellSurface>(),
|
||||
Array.Empty<PortalPlane>(),
|
||||
worldOffsetX: 0f,
|
||||
worldOffsetY: 0f);
|
||||
var player = new PlayerMovementController(engine);
|
||||
player.SetPosition(
|
||||
new Vector3(96f, 97f, 50f),
|
||||
0xA9B40001u,
|
||||
new Vector3(96f, 97f, 50f));
|
||||
Assert.True(player.CanSendPositionEvent);
|
||||
return player;
|
||||
}
|
||||
|
||||
private static uint U32(byte[] body, int offset) =>
|
||||
BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(offset, 4));
|
||||
|
||||
private static ushort U16(byte[] body, int offset) =>
|
||||
BinaryPrimitives.ReadUInt16LittleEndian(body.AsSpan(offset, 2));
|
||||
|
||||
private static float F32(byte[] body, int offset) =>
|
||||
BitConverter.Int32BitsToSingle(
|
||||
BinaryPrimitives.ReadInt32LittleEndian(body.AsSpan(offset, 4)));
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
using System.Buffers.Binary;
|
||||
using AcDream.Core.Net.Messages;
|
||||
using AcDream.Core.Net.Packets;
|
||||
using AcDream.Core.Physics;
|
||||
using AcDream.Runtime.Gameplay;
|
||||
|
||||
namespace AcDream.Runtime.Tests.Gameplay;
|
||||
|
||||
public sealed class LocalPlayerOutboundCombatStyleTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(0x8000003Cu)] // HandCombat
|
||||
[InlineData(0x8000003Eu)] // Magic
|
||||
[InlineData(0x8000003Fu)] // BowCombat
|
||||
[InlineData(0x80000041u)] // CrossbowCombat
|
||||
public void CaptureAndBuild_PreservesCanonicalRawCombatStyle(uint combatStyle)
|
||||
{
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
controller.Motion.RawState.CurrentStyle = combatStyle;
|
||||
|
||||
MovementResult movement = controller.CaptureMovementResult(
|
||||
mouseLookEvent: false);
|
||||
RawMotionState outbound = LocalPlayerOutboundController.BuildRawMotionState(
|
||||
movement);
|
||||
|
||||
Assert.Equal(combatStyle, movement.CurrentStyle);
|
||||
Assert.Equal(combatStyle, outbound.CurrentStyle);
|
||||
|
||||
var writer = new PacketWriter(16);
|
||||
RawMotionStatePacker.Pack(writer, outbound);
|
||||
byte[] bytes = writer.ToArray();
|
||||
|
||||
uint flags = BinaryPrimitives.ReadUInt32LittleEndian(bytes);
|
||||
Assert.NotEqual(0u, flags & 0x2u);
|
||||
Assert.Equal(
|
||||
combatStyle,
|
||||
BinaryPrimitives.ReadUInt32LittleEndian(bytes.AsSpan(4)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CaptureAndBuild_NonCombatRetainsRetailDefault()
|
||||
{
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
|
||||
RawMotionState outbound = LocalPlayerOutboundController.BuildRawMotionState(
|
||||
controller.CaptureMovementResult(mouseLookEvent: false));
|
||||
|
||||
Assert.Equal(RawMotionState.Default.CurrentStyle, outbound.CurrentStyle);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,455 @@
|
|||
using System.Numerics;
|
||||
using AcDream.Core.Physics;
|
||||
using AcDream.Core.Physics.Motion;
|
||||
using AcDream.Runtime.Gameplay;
|
||||
|
||||
namespace AcDream.Runtime.Tests.Gameplay;
|
||||
|
||||
public sealed class PlayerMouseLookMovementTests
|
||||
{
|
||||
[Fact]
|
||||
public void KeyboardMotionEdges_StillRequestImmediateMovementEvents()
|
||||
{
|
||||
var controller = CreateController();
|
||||
|
||||
MovementResult pressed = controller.Update(
|
||||
1f / 60f,
|
||||
new MovementInput(Forward: true));
|
||||
MovementResult held = controller.Update(
|
||||
1f / 60f,
|
||||
new MovementInput(Forward: true));
|
||||
MovementResult released = controller.Update(
|
||||
1f / 60f,
|
||||
new MovementInput());
|
||||
|
||||
Assert.True(pressed.ShouldSendMovementEvent);
|
||||
Assert.False(held.ShouldSendMovementEvent);
|
||||
Assert.True(released.ShouldSendMovementEvent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseAdjustment_UsesTurnMotionInsteadOfDirectYawMutation()
|
||||
{
|
||||
var controller = CreateController();
|
||||
Assert.True(controller.BeginMouseLook(new MovementInput()));
|
||||
controller.NoteMovementSent(controller.SimTimeSeconds, mouseLookEvent: false);
|
||||
|
||||
float yawBefore = controller.Yaw;
|
||||
controller.SubmitMouseTurnAdjustment(-0.4f, new MovementInput());
|
||||
// Rotation is authored/applied on an admitted retail object quantum,
|
||||
// not directly by the mouse sample. Stay below the outbound mouse
|
||||
// cadence while crossing the strict physics minimum.
|
||||
MovementResult turning = controller.Update(
|
||||
PhysicsBody.MinQuantum + 0.001f,
|
||||
new MovementInput());
|
||||
|
||||
Assert.Equal(MotionCommand.TurnRight, turning.TurnCommand);
|
||||
Assert.Equal(0.8f, turning.TurnSpeed);
|
||||
Assert.True(controller.Yaw < yawBefore);
|
||||
Assert.False(turning.ShouldSendMovementEvent);
|
||||
Assert.True(turning.TurnUsesRunHold);
|
||||
|
||||
MovementResult held = controller.Update(1f / 60f, new MovementInput());
|
||||
Assert.Equal(MotionCommand.TurnRight, held.TurnCommand);
|
||||
|
||||
controller.StopMouseDrift(new MovementInput());
|
||||
MovementResult stopped = controller.Update(1f / 60f, new MovementInput());
|
||||
Assert.Null(stopped.TurnCommand);
|
||||
Assert.False(stopped.ShouldSendMovementEvent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLook_ReportsStartCadenceAndStopWithoutPerSampleFlooding()
|
||||
{
|
||||
var controller = CreateController();
|
||||
Assert.True(controller.BeginMouseLook(new MovementInput()));
|
||||
MovementResult begin = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
Assert.True(begin.ShouldSendMovementEvent);
|
||||
Assert.False(begin.IsMouseLookMovementEvent);
|
||||
controller.NoteMovementSent(controller.SimTimeSeconds, mouseLookEvent: false);
|
||||
|
||||
controller.SubmitMouseTurnAdjustment(0.5f, new MovementInput());
|
||||
MovementResult sample = controller.Update(0.01f, new MovementInput());
|
||||
Assert.Equal(MotionCommand.TurnLeft, sample.TurnCommand);
|
||||
Assert.False(sample.ShouldSendMovementEvent);
|
||||
|
||||
controller.Update(
|
||||
PlayerMovementController.MouseMovementEventInterval,
|
||||
new MovementInput());
|
||||
controller.SubmitMouseTurnAdjustment(0.5f, new MovementInput());
|
||||
MovementResult cadence = controller.Update(
|
||||
0.001f,
|
||||
new MovementInput());
|
||||
Assert.True(cadence.ShouldSendMovementEvent);
|
||||
Assert.True(cadence.IsMouseLookMovementEvent);
|
||||
controller.NoteMovementSent(
|
||||
controller.SimTimeSeconds,
|
||||
mouseLookEvent: true);
|
||||
|
||||
Assert.True(controller.EndMouseLook(new MovementInput()));
|
||||
MovementResult ended = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
Assert.True(ended.ShouldSendMovementEvent);
|
||||
Assert.False(ended.IsMouseLookMovementEvent);
|
||||
Assert.Null(ended.TurnCommand);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseMovementEvent_IsRetriedUntilSuccessfulSendIsAcknowledged()
|
||||
{
|
||||
var controller = CreateController();
|
||||
controller.BeginMouseLook(new MovementInput());
|
||||
controller.Update(
|
||||
PlayerMovementController.MouseMovementEventInterval + 0.001f,
|
||||
new MovementInput());
|
||||
controller.SubmitMouseTurnAdjustment(0.4f, new MovementInput());
|
||||
|
||||
MovementResult first = controller.Update(0.01f, new MovementInput(Run: true));
|
||||
MovementResult retry = controller.Update(0.01f, new MovementInput(Run: true));
|
||||
|
||||
Assert.True(first.ShouldSendMovementEvent);
|
||||
Assert.True(first.IsMouseLookMovementEvent);
|
||||
Assert.True(retry.ShouldSendMovementEvent);
|
||||
|
||||
controller.NoteMovementSent(
|
||||
controller.SimTimeSeconds,
|
||||
mouseLookEvent: true);
|
||||
MovementResult acknowledged = controller.Update(
|
||||
0.01f,
|
||||
new MovementInput(Run: true));
|
||||
Assert.False(acknowledged.ShouldSendMovementEvent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLook_RapidPressReleaseCanBeCapturedSynchronously()
|
||||
{
|
||||
var controller = CreateController();
|
||||
var input = new MovementInput(Run: true);
|
||||
|
||||
Assert.True(controller.BeginMouseLook(input));
|
||||
MovementResult press = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
Assert.True(controller.EndMouseLook(input));
|
||||
MovementResult release = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
|
||||
Assert.True(press.ShouldSendMovementEvent);
|
||||
Assert.False(press.IsMouseLookMovementEvent);
|
||||
Assert.True(release.ShouldSendMovementEvent);
|
||||
Assert.False(release.IsMouseLookMovementEvent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLook_CadenceUsesStrictHalfSecondBoundary()
|
||||
{
|
||||
var controller = CreateController();
|
||||
controller.BeginMouseLook(new MovementInput());
|
||||
controller.NoteMovementSent(nowSeconds: 0f, mouseLookEvent: true);
|
||||
|
||||
controller.SubmitMouseTurnAdjustment(0.4f, new MovementInput());
|
||||
MovementResult boundary = controller.Update(
|
||||
PlayerMovementController.MouseMovementEventInterval,
|
||||
new MovementInput());
|
||||
Assert.False(boundary.ShouldSendMovementEvent);
|
||||
|
||||
controller.SubmitMouseTurnAdjustment(0.4f, new MovementInput());
|
||||
MovementResult justOver = controller.Update(
|
||||
0.0001f,
|
||||
new MovementInput());
|
||||
Assert.True(justOver.ShouldSendMovementEvent);
|
||||
Assert.True(justOver.IsMouseLookMovementEvent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseTurn_UsesPerAxisRunHoldEvenInWalkMode()
|
||||
{
|
||||
var controller = CreateController();
|
||||
controller.BeginMouseLook(new MovementInput());
|
||||
controller.NoteMovementSent(nowSeconds: 0f, mouseLookEvent: true);
|
||||
controller.SubmitMouseTurnAdjustment(-0.4f, new MovementInput());
|
||||
|
||||
MovementResult turning = controller.Update(
|
||||
0.01f,
|
||||
new MovementInput(Run: false));
|
||||
|
||||
Assert.Equal(MotionCommand.TurnRight, turning.TurnCommand);
|
||||
Assert.False(turning.IsRunning);
|
||||
Assert.True(turning.TurnUsesRunHold);
|
||||
Assert.Equal(HoldKey.Run, controller.Motion.RawState.TurnHoldKey);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLookRelease_SynchronouslyRestoresHeldKeyboardTurn()
|
||||
{
|
||||
var controller = CreateController();
|
||||
controller.Update(
|
||||
0.01f,
|
||||
new MovementInput(TurnLeft: true, Run: true));
|
||||
var held = new MovementInput(TurnLeft: true, Run: true);
|
||||
controller.BeginMouseLook(held);
|
||||
controller.NoteMovementSent(controller.SimTimeSeconds, mouseLookEvent: false);
|
||||
controller.SubmitMouseTurnAdjustment(-0.4f, held);
|
||||
controller.Update(0.01f, held);
|
||||
|
||||
controller.EndMouseLook(held);
|
||||
MovementResult release = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
|
||||
Assert.Equal(MotionCommand.TurnLeft, release.TurnCommand);
|
||||
Assert.Equal(1f, release.TurnSpeed);
|
||||
Assert.False(release.TurnUsesRunHold);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLook_RemapMovesHeldKeyboardTurnToSidestepAndBack()
|
||||
{
|
||||
var controller = CreateController();
|
||||
var held = new MovementInput(TurnLeft: true, Run: true);
|
||||
controller.Update(0.01f, held);
|
||||
|
||||
Assert.True(controller.BeginMouseLook(held));
|
||||
MovementResult press = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
|
||||
Assert.Null(press.TurnCommand);
|
||||
Assert.Equal(MotionCommand.SideStepLeft, press.SidestepCommand);
|
||||
Assert.Equal(HoldKey.Run, controller.Motion.RawState.SidestepHoldKey);
|
||||
|
||||
Assert.True(controller.EndMouseLook(held));
|
||||
MovementResult release = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
|
||||
Assert.Null(release.SidestepCommand);
|
||||
Assert.Equal(MotionCommand.TurnLeft, release.TurnCommand);
|
||||
Assert.False(release.TurnUsesRunHold);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLookEntry_PreservesHeldForwardAndRunAfterTakingControl()
|
||||
{
|
||||
var controller = CreateController();
|
||||
var held = new MovementInput(Forward: true, Run: true);
|
||||
|
||||
Assert.True(controller.BeginMouseLook(held));
|
||||
MovementResult press = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
|
||||
Assert.Equal(MotionCommand.WalkForward, press.ForwardCommand);
|
||||
Assert.True(press.IsRunning);
|
||||
Assert.Equal(HoldKey.Run, controller.Motion.RawState.CurrentHoldKey);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLookEntry_AlreadyAutonomousSameFrameForwardEdgeIsNotLost()
|
||||
{
|
||||
var controller = CreateController();
|
||||
controller.Update(0.01f, new MovementInput(Forward: true));
|
||||
controller.Update(0.01f, new MovementInput());
|
||||
var justPressed = new MovementInput(Forward: true, Run: true);
|
||||
|
||||
controller.BeginMouseLook(justPressed);
|
||||
MovementResult press = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
|
||||
Assert.Equal(MotionCommand.WalkForward, press.ForwardCommand);
|
||||
Assert.True(press.IsRunning);
|
||||
Assert.Equal(HoldKey.Run, controller.Motion.RawState.CurrentHoldKey);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLookEntry_AlreadyAutonomousSameFrameRunChangeIsNotLost()
|
||||
{
|
||||
var controller = CreateController();
|
||||
controller.Update(0.01f, new MovementInput(Forward: true));
|
||||
controller.Update(0.01f, new MovementInput());
|
||||
|
||||
controller.BeginMouseLook(new MovementInput(Run: true));
|
||||
MovementResult press = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
RawMotionState wire = LocalPlayerOutboundController.BuildRawMotionState(press);
|
||||
|
||||
Assert.Equal(HoldKey.Run, controller.Motion.RawState.CurrentHoldKey);
|
||||
Assert.True(press.IsRunning);
|
||||
Assert.Equal(HoldKey.Run, wire.CurrentHoldKey);
|
||||
Assert.Equal(HoldKey.Invalid, wire.ForwardHoldKey);
|
||||
Assert.Equal(HoldKey.Invalid, wire.SidestepHoldKey);
|
||||
Assert.Equal(HoldKey.Invalid, wire.TurnHoldKey);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLookEntry_ServerControlledHeldTurnNeverQueuesWrongAxis()
|
||||
{
|
||||
var controller = CreateController();
|
||||
|
||||
controller.BeginMouseLook(new MovementInput(TurnLeft: true));
|
||||
|
||||
Assert.DoesNotContain(
|
||||
controller.Motion.PendingMotions,
|
||||
node => node.Motion == MotionCommand.TurnLeft);
|
||||
Assert.Equal(MotionCommand.SideStepLeft,
|
||||
controller.Motion.RawState.SidestepCommand);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLookExit_ServerControlledHeldTurnNeverQueuesRemappedAxis()
|
||||
{
|
||||
var controller = CreateController();
|
||||
var held = new MovementInput(TurnLeft: true);
|
||||
controller.BeginMouseLook(held);
|
||||
DrainPendingMotions(controller);
|
||||
controller.SetLastMoveWasAutonomous(false);
|
||||
|
||||
controller.EndMouseLook(held);
|
||||
|
||||
Assert.DoesNotContain(
|
||||
controller.Motion.PendingMotions,
|
||||
node => node.Motion == MotionCommand.SideStepLeft);
|
||||
Assert.Equal(MotionCommand.TurnLeft, controller.Motion.RawState.TurnCommand);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLook_RemapPressedAfterEntryAppearsInOutboundResult()
|
||||
{
|
||||
var controller = CreateController();
|
||||
controller.BeginMouseLook(new MovementInput());
|
||||
|
||||
MovementResult result = controller.Update(
|
||||
0.01f,
|
||||
new MovementInput(TurnLeft: true));
|
||||
|
||||
Assert.Equal(MotionCommand.SideStepLeft, result.SidestepCommand);
|
||||
Assert.Null(result.TurnCommand);
|
||||
Assert.True(result.SidestepUsesRunHold);
|
||||
Assert.False(result.IsRunning);
|
||||
Assert.True(result.ShouldSendMovementEvent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLook_WalkModeRemapCarriesPerAxisRunHold()
|
||||
{
|
||||
var controller = CreateController();
|
||||
var held = new MovementInput(TurnRight: true, Run: false);
|
||||
|
||||
controller.BeginMouseLook(held);
|
||||
MovementResult press = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
RawMotionState wire = LocalPlayerOutboundController.BuildRawMotionState(press);
|
||||
|
||||
Assert.Equal(MotionCommand.SideStepRight, press.SidestepCommand);
|
||||
Assert.True(press.SidestepUsesRunHold);
|
||||
Assert.False(press.IsRunning);
|
||||
Assert.Equal(HoldKey.Run, controller.Motion.RawState.SidestepHoldKey);
|
||||
Assert.Equal(HoldKey.None, wire.CurrentHoldKey);
|
||||
Assert.Equal(HoldKey.Run, wire.SidestepHoldKey);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLook_MouseHandlerRetakesServerControlWithoutDroppingHeldInput()
|
||||
{
|
||||
var controller = CreateController();
|
||||
var held = new MovementInput(Forward: true, TurnLeft: true, Run: true);
|
||||
controller.BeginMouseLook(held);
|
||||
controller.SetLastMoveWasAutonomous(false);
|
||||
|
||||
controller.SubmitMouseTurnAdjustment(-0.4f, held);
|
||||
MovementResult result = controller.Update(0.01f, held);
|
||||
|
||||
Assert.Equal(MotionCommand.WalkForward, result.ForwardCommand);
|
||||
Assert.Equal(MotionCommand.SideStepLeft, result.SidestepCommand);
|
||||
Assert.Equal(MotionCommand.TurnRight, result.TurnCommand);
|
||||
Assert.True(result.IsRunning);
|
||||
Assert.True(result.SidestepUsesRunHold);
|
||||
Assert.True(result.TurnUsesRunHold);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLookRelease_UsesCurrentInputSnapshotInsteadOfPreviousFrame()
|
||||
{
|
||||
var controller = CreateController();
|
||||
var held = new MovementInput(Forward: true, TurnLeft: true, Run: true);
|
||||
controller.Update(0.01f, held);
|
||||
controller.BeginMouseLook(held);
|
||||
|
||||
Assert.True(controller.EndMouseLook(new MovementInput()));
|
||||
MovementResult release = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
|
||||
Assert.Null(release.ForwardCommand);
|
||||
Assert.Null(release.SidestepCommand);
|
||||
Assert.Null(release.TurnCommand);
|
||||
Assert.False(release.IsRunning);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MouseLook_CannotBeginInPortalSpace()
|
||||
{
|
||||
var controller = CreateController();
|
||||
controller.State = PlayerState.PortalSpace;
|
||||
|
||||
Assert.False(controller.BeginMouseLook(new MovementInput(Forward: true)));
|
||||
|
||||
MovementResult result = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
Assert.Null(result.ForwardCommand);
|
||||
Assert.Null(result.SidestepCommand);
|
||||
Assert.Null(result.TurnCommand);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AttackPreparation_StopsMouseTurnAndPublishesFinalHeading()
|
||||
{
|
||||
var controller = CreateController();
|
||||
controller.BeginMouseLook(new MovementInput());
|
||||
controller.Update(0.01f, new MovementInput());
|
||||
controller.SubmitMouseTurnAdjustment(-0.5f, new MovementInput());
|
||||
MovementResult turning = controller.Update(1f / 60f, new MovementInput());
|
||||
Assert.Equal(MotionCommand.TurnRight, turning.TurnCommand);
|
||||
|
||||
Assert.True(controller.PrepareForAttackRequest());
|
||||
float finalYaw = controller.Yaw;
|
||||
MovementResult stopped = controller.CaptureMovementResult(mouseLookEvent: false);
|
||||
|
||||
Assert.Equal(finalYaw, controller.Yaw);
|
||||
Assert.Null(stopped.TurnCommand);
|
||||
Assert.True(stopped.ShouldSendMovementEvent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AttackPreparation_DoesNotInterruptServerControlledMovement()
|
||||
{
|
||||
var controller = CreateController();
|
||||
controller.SetLastMoveWasAutonomous(false);
|
||||
|
||||
Assert.False(controller.PrepareForAttackRequest());
|
||||
MovementResult result = controller.Update(
|
||||
0.01f,
|
||||
new MovementInput(Run: true));
|
||||
|
||||
Assert.False(result.ShouldSendMovementEvent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AttackPreparation_DefaultServerControlledStateIsNoOp()
|
||||
{
|
||||
var controller = CreateController();
|
||||
|
||||
Assert.False(controller.PrepareForAttackRequest());
|
||||
}
|
||||
|
||||
private static PlayerMovementController CreateController()
|
||||
{
|
||||
var engine = new PhysicsEngine();
|
||||
var heights = new byte[81];
|
||||
Array.Fill(heights, (byte)50);
|
||||
var heightTable = new float[256];
|
||||
for (int i = 0; i < heightTable.Length; i++)
|
||||
heightTable[i] = i;
|
||||
|
||||
engine.AddLandblock(
|
||||
0xA9B4FFFFu,
|
||||
new TerrainSurface(heights, heightTable),
|
||||
Array.Empty<CellSurface>(),
|
||||
Array.Empty<PortalPlane>(),
|
||||
worldOffsetX: 0f,
|
||||
worldOffsetY: 0f);
|
||||
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001u);
|
||||
return controller;
|
||||
}
|
||||
|
||||
private static void DrainPendingMotions(PlayerMovementController controller)
|
||||
{
|
||||
int remaining = controller.Motion.PendingMotions.Count();
|
||||
for (int i = 0; i < remaining; i++)
|
||||
controller.Motion.MotionDone(0u, success: true);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,829 @@
|
|||
using System;
|
||||
using System.Numerics;
|
||||
using AcDream.Core.Physics;
|
||||
using AcDream.Runtime.Gameplay;
|
||||
|
||||
namespace AcDream.Runtime.Tests.Gameplay;
|
||||
|
||||
public class PlayerMovementControllerTests
|
||||
{
|
||||
private static float ObjectTick => PhysicsBody.MinQuantum + 0.001f;
|
||||
|
||||
private static PhysicsEngine MakeFlatEngine()
|
||||
{
|
||||
var engine = new PhysicsEngine();
|
||||
var heights = new byte[81];
|
||||
Array.Fill(heights, (byte)50);
|
||||
var heightTable = new float[256];
|
||||
for (int i = 0; i < 256; i++) heightTable[i] = i * 1f;
|
||||
var terrain = new TerrainSurface(heights, heightTable);
|
||||
engine.AddLandblock(0xA9B4FFFFu, terrain, Array.Empty<CellSurface>(),
|
||||
Array.Empty<PortalPlane>(), worldOffsetX: 0f, worldOffsetY: 0f);
|
||||
return engine;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PerformMovement_ReactivatesCanonicalClock_ExceptForStaticObject()
|
||||
{
|
||||
var clock = new RetailObjectQuantumClock();
|
||||
var controller = new PlayerMovementController(MakeFlatEngine(), clock);
|
||||
clock.Deactivate();
|
||||
controller.ApplyPhysicsState(PhysicsStateFlags.None);
|
||||
|
||||
controller.Movement.PerformMovement(new MovementStruct
|
||||
{
|
||||
Type = (MovementType)99,
|
||||
});
|
||||
Assert.True(clock.IsActive);
|
||||
|
||||
clock.Deactivate();
|
||||
controller.ApplyPhysicsState(PhysicsStateFlags.Static);
|
||||
controller.Movement.PerformMovement(new MovementStruct
|
||||
{
|
||||
Type = (MovementType)99,
|
||||
});
|
||||
Assert.False(clock.IsActive);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_NoInput_PositionUnchanged()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
|
||||
var result = controller.Update(0.016f, new MovementInput());
|
||||
|
||||
Assert.Equal(96f, result.Position.X, precision: 1);
|
||||
Assert.Equal(96f, result.Position.Y, precision: 1);
|
||||
}
|
||||
|
||||
// ── Indoor-flap root cause: resting-body bit-stability ────────────────────
|
||||
//
|
||||
// The indoor render "flap" (textures battling at the cottage doorway) is
|
||||
// portal-flood membership instability. PortalVisibilityBuilder.Build is a
|
||||
// proven-deterministic pure function, so the membership can only flip if its
|
||||
// INPUT (the camera eye, derived from the player RenderPosition) varies.
|
||||
// Live 6-dp capture (pvinput.log:54) shows the player RenderPosition carries
|
||||
// a perpetual ~1-ULP flicker at rest (Z 94.000000 <-> 93.999992 — exactly one
|
||||
// float mantissa step). ComputeRenderPosition is Vector3.Lerp(_prevPhysicsPos,
|
||||
// _currPhysicsPos, alpha), and Lerp(a, a, t) == a exactly, so a jittering
|
||||
// RenderPosition at rest means the physics body's resting Position is NOT
|
||||
// bit-stable between ticks. Retail's authoritative local position is bit-stable
|
||||
// at rest (validate_transition -> kill_velocity on every grounded contact), so
|
||||
// retail never flaps.
|
||||
//
|
||||
// This test pins the physics-side invariant: a grounded body with no input
|
||||
// must hold a byte-identical position across many frames. It PASSES — which
|
||||
// is itself the evidence: the physics resting position is bit-stable, so the
|
||||
// doorway flap is NOT a physics-rest jitter. See
|
||||
// docs/research/2026-06-08-flap-physics-diagnosis-REFUTED-its-render-membership.md
|
||||
// (the flap is render-side portal-flood membership instability at the grazing
|
||||
// doorway portal under a sweeping camera eye). Kept as a regression guard.
|
||||
[Fact]
|
||||
public void Update_AtRestNoInput_RenderPositionBitStableAcrossManyFrames()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
var rest = new Vector3(96f, 96f, 50f);
|
||||
controller.SetPosition(rest, 0x0001);
|
||||
|
||||
// Settle one frame so the resolver establishes its rest state, then
|
||||
// capture the baseline the body must hold.
|
||||
var settled = controller.Update(1f / 60f, new MovementInput());
|
||||
Vector3 baselineRender = settled.RenderPosition;
|
||||
Vector3 baselinePhysics = settled.Position;
|
||||
|
||||
// Hold still for ~10 s of 60 Hz frames (crosses MinQuantum every ~2
|
||||
// frames, so the 30 Hz physics tick fires throughout — same cadence as
|
||||
// live). Any deviation, even one ULP, is the flap's root cause.
|
||||
float maxRenderDev = 0f;
|
||||
float maxPhysicsDev = 0f;
|
||||
for (int i = 0; i < 600; i++)
|
||||
{
|
||||
var r = controller.Update(1f / 60f, new MovementInput());
|
||||
maxRenderDev = MathF.Max(maxRenderDev, (r.RenderPosition - baselineRender).Length());
|
||||
maxPhysicsDev = MathF.Max(maxPhysicsDev, (r.Position - baselinePhysics).Length());
|
||||
}
|
||||
|
||||
Assert.True(
|
||||
maxRenderDev == 0f && maxPhysicsDev == 0f,
|
||||
$"resting body drifted: render={maxRenderDev * 1e6f:F3} µm, " +
|
||||
$"physics={maxPhysicsDev * 1e6f:F3} µm; expected byte-identical rest");
|
||||
}
|
||||
|
||||
// After walking then releasing input, the body must SETTLE to a
|
||||
// byte-identical resting position — not keep blipping a residual velocity.
|
||||
// This models the live flap: the player walks to the cottage doorway and
|
||||
// stops, and the eye then carries a ~1-ULP jitter that flips portal-flood
|
||||
// membership. Flat-terrain variant: if even this drifts, the residual-after-
|
||||
// motion path is the root and it is not indoor-specific.
|
||||
[Fact]
|
||||
public void Update_WalkThenStop_SettlesToBitStableRest()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
controller.Yaw = 0f;
|
||||
|
||||
// Walk forward ~0.5 s, then release.
|
||||
for (int i = 0; i < 30; i++)
|
||||
controller.Update(1f / 60f, new MovementInput(Forward: true));
|
||||
// Let velocity decay / state settle.
|
||||
for (int i = 0; i < 30; i++)
|
||||
controller.Update(1f / 60f, new MovementInput());
|
||||
|
||||
var settled = controller.Update(1f / 60f, new MovementInput());
|
||||
Vector3 basePos = settled.Position;
|
||||
Vector3 baseRender = settled.RenderPosition;
|
||||
|
||||
float maxPos = 0f, maxRender = 0f;
|
||||
for (int i = 0; i < 600; i++)
|
||||
{
|
||||
var r = controller.Update(1f / 60f, new MovementInput());
|
||||
maxPos = MathF.Max(maxPos, (r.Position - basePos).Length());
|
||||
maxRender = MathF.Max(maxRender, (r.RenderPosition - baseRender).Length());
|
||||
}
|
||||
|
||||
Assert.True(maxPos == 0f && maxRender == 0f,
|
||||
$"post-walk rest drifted: pos={maxPos * 1e6f:F3} µm, render={maxRender * 1e6f:F3} µm");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_ForwardInput_MovesInFacingDirection()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
controller.Yaw = 0f; // facing +X
|
||||
|
||||
// L.5 physics-tick gate (235de33, 2026-04-30): Update() integrates
|
||||
// only one MinQuantum (~0.033s) per MaxQuantum (~0.1s) tick, matching
|
||||
// retail's 30Hz physics. A single Update(1.0f) only advances one
|
||||
// MaxQuantum step (~0.312m at walk speed 3.12 m/s). Drive the
|
||||
// controller one MaxQuantum at a time for ~1s to accumulate real
|
||||
// forward motion (8 × 0.1s = 0.8s × 3.12 m/s ≈ 2.5m).
|
||||
var input = new MovementInput { Forward = true };
|
||||
MovementResult result = default;
|
||||
int ticks = (int)MathF.Ceiling(1.0f / PhysicsBody.MaxQuantum) + 1; // ~11 ticks
|
||||
for (int i = 0; i < ticks; i++)
|
||||
result = controller.Update(PhysicsBody.MaxQuantum, input);
|
||||
|
||||
// Should have moved >2 units in +X (walk speed over ~1s).
|
||||
Assert.True(result.Position.X > 96f + 2f, $"X={result.Position.X} should have moved forward");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_AttachedAnimationWithZeroRootDelta_DoesNotGlideOnForwardEdge()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
var start = new Vector3(96f, 96f, 50f);
|
||||
controller.SetPosition(start, 0x0001);
|
||||
controller.Yaw = 0f;
|
||||
controller.AttachAnimationRootMotionSource((_, _) => { });
|
||||
|
||||
MovementResult result = controller.Update(
|
||||
ObjectTick,
|
||||
new MovementInput(Forward: true));
|
||||
|
||||
Assert.Equal(start, result.Position);
|
||||
Assert.Equal(0f, controller.BodyVelocity.X);
|
||||
Assert.Equal(0f, controller.BodyVelocity.Y);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_AttachedAnimationRootDelta_DrivesGroundedBodyAtObjectScale()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
var start = new Vector3(96f, 96f, 50f);
|
||||
controller.SetPosition(start, 0x0001);
|
||||
controller.Yaw = 0f;
|
||||
controller.ObjectScale = 2f;
|
||||
controller.AttachAnimationRootMotionSource((_, frame) =>
|
||||
frame.Origin = new Vector3(0f, 0.1f, 0f));
|
||||
|
||||
MovementResult result = controller.Update(
|
||||
ObjectTick,
|
||||
new MovementInput(Forward: true));
|
||||
|
||||
// Local +Y is forward. Yaw 0 maps it to world +X; m_scale doubles
|
||||
// the animation-authored 0.1 m displacement to 0.2 m.
|
||||
Assert.Equal(start.X + 0.2f, result.Position.X, precision: 3);
|
||||
Assert.Equal(start.Y, result.Position.Y, precision: 3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_SubQuantumFrames_AdvanceAnimationOnceAtObjectThreshold()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
var start = new Vector3(96f, 96f, 50f);
|
||||
controller.SetPosition(start, 0x0001);
|
||||
controller.Yaw = 0f;
|
||||
int advances = 0;
|
||||
controller.AttachAnimationRootMotionSource((_, frame) =>
|
||||
{
|
||||
advances++;
|
||||
frame.Origin = new Vector3(0f, 0.1f, 0f);
|
||||
});
|
||||
|
||||
MovementResult first = controller.Update(
|
||||
ObjectTick * 0.5f,
|
||||
new MovementInput(Forward: true));
|
||||
MovementResult second = controller.Update(
|
||||
ObjectTick * 0.5f,
|
||||
new MovementInput(Forward: true));
|
||||
|
||||
Assert.Equal(start, first.Position);
|
||||
Assert.Equal(start.X + 0.1f, second.Position.X, precision: 3);
|
||||
Assert.Equal(start.Y, second.Position.Y, precision: 3);
|
||||
Assert.Equal(1, advances);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_AttachedAnimationFrame_ComposesTranslationBeforeTurn()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
var start = new Vector3(96f, 96f, 50f);
|
||||
controller.SetPosition(start, 0x0001);
|
||||
controller.Yaw = 0f; // body local +Y faces world +X
|
||||
controller.AttachAnimationRootMotionSource((_, frame) =>
|
||||
{
|
||||
frame.Origin = new Vector3(0f, 0.1f, 0f);
|
||||
frame.Orientation = Quaternion.CreateFromAxisAngle(
|
||||
Vector3.UnitZ,
|
||||
MathF.PI / 2f);
|
||||
});
|
||||
|
||||
MovementResult result = controller.Update(
|
||||
ObjectTick,
|
||||
new MovementInput());
|
||||
|
||||
// Retail Frame::combine transforms the delta origin by the OLD body
|
||||
// orientation, then composes the delta orientation. The body therefore
|
||||
// moves east before finishing the quarter-turn to north.
|
||||
Assert.Equal(start.X + 0.1f, result.Position.X, precision: 3);
|
||||
Assert.Equal(start.Y, result.Position.Y, precision: 3);
|
||||
Assert.Equal(MathF.PI / 2f, controller.Yaw, precision: 3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_AttachedAnimationFrame_PreservesCompleteNonCommutingOrientation()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
Quaternion initial = Quaternion.CreateFromAxisAngle(Vector3.UnitX, 1.1f);
|
||||
Quaternion delta = Quaternion.CreateFromAxisAngle(Vector3.UnitY, -0.9f);
|
||||
controller.SetBodyOrientation(initial);
|
||||
controller.AttachAnimationRootMotionSource((_, frame) =>
|
||||
frame.Orientation = delta);
|
||||
|
||||
controller.Update(ObjectTick, new MovementInput());
|
||||
|
||||
Quaternion expected = Quaternion.Normalize(initial * delta);
|
||||
float alignment = MathF.Abs(Quaternion.Dot(
|
||||
expected,
|
||||
Quaternion.Normalize(controller.BodyOrientation)));
|
||||
Assert.InRange(alignment, 0.99999f, 1.00001f);
|
||||
Assert.True(MathF.Abs(Quaternion.Dot(
|
||||
Quaternion.Normalize(delta * initial),
|
||||
Quaternion.Normalize(controller.BodyOrientation))) < 0.999f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_MultiQuantumAnimationFrames_ComposeInTemporalOrder()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
var start = new Vector3(96f, 96f, 50f);
|
||||
controller.SetPosition(start, 0x0001);
|
||||
controller.Yaw = 0f;
|
||||
int sample = 0;
|
||||
controller.AttachAnimationRootMotionSource((_, frame) =>
|
||||
{
|
||||
if (sample++ == 0)
|
||||
{
|
||||
frame.Orientation = Quaternion.CreateFromAxisAngle(
|
||||
Vector3.UnitZ,
|
||||
MathF.PI / 2f);
|
||||
}
|
||||
else
|
||||
{
|
||||
frame.Origin = new Vector3(0f, 0.1f, 0f);
|
||||
}
|
||||
});
|
||||
|
||||
MovementResult result = controller.Update(
|
||||
PhysicsBody.MaxQuantum * 2f,
|
||||
new MovementInput());
|
||||
|
||||
// The second local-forward displacement occurs after the first
|
||||
// quarter-turn, so it moves north. Adding Origins as bare vectors
|
||||
// would incorrectly move east.
|
||||
Assert.Equal(start.X, result.Position.X, precision: 3);
|
||||
Assert.Equal(start.Y + 0.1f, result.Position.Y, precision: 3);
|
||||
Assert.Equal(MathF.PI / 2f, controller.Yaw, precision: 3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_ExactMinQuantum_RetainsTimeWithoutAdvancingObject()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
var start = new Vector3(96f, 96f, 50f);
|
||||
controller.SetPosition(start, 0x0001);
|
||||
int advances = 0;
|
||||
controller.AttachAnimationRootMotionSource((_, frame) =>
|
||||
{
|
||||
advances++;
|
||||
frame.Origin = new Vector3(0f, 1f, 0f);
|
||||
});
|
||||
|
||||
MovementResult atThreshold = controller.Update(
|
||||
PhysicsBody.MinQuantum,
|
||||
new MovementInput());
|
||||
|
||||
Assert.Equal(start, atThreshold.Position);
|
||||
Assert.Equal(0, advances);
|
||||
|
||||
controller.Update(0.001f, new MovementInput());
|
||||
Assert.Equal(1, advances);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_LargeFrame_MatchesSeparateRetailObjectQuanta()
|
||||
{
|
||||
var combined = new PlayerMovementController(MakeFlatEngine());
|
||||
var split = new PlayerMovementController(MakeFlatEngine());
|
||||
var start = new Vector3(96f, 96f, 50f);
|
||||
combined.SetPosition(start, 0x0001);
|
||||
split.SetPosition(start, 0x0001);
|
||||
int combinedHooks = 0;
|
||||
int splitHooks = 0;
|
||||
|
||||
static void Advance(float dt, AcDream.Core.Physics.Motion.MotionDeltaFrame frame)
|
||||
{
|
||||
frame.Origin = new Vector3(0f, dt * 2f, 0f);
|
||||
frame.Orientation = Quaternion.CreateFromAxisAngle(
|
||||
Vector3.UnitZ,
|
||||
dt * 0.7f);
|
||||
}
|
||||
|
||||
combined.AttachAnimationRootMotionSource(Advance, () => combinedHooks++);
|
||||
split.AttachAnimationRootMotionSource(Advance, () => splitHooks++);
|
||||
|
||||
combined.Update(PhysicsBody.MaxQuantum * 2f, new MovementInput());
|
||||
split.Update(PhysicsBody.MaxQuantum, new MovementInput());
|
||||
split.Update(PhysicsBody.MaxQuantum, new MovementInput());
|
||||
|
||||
Assert.Equal(split.Position.X, combined.Position.X, precision: 5);
|
||||
Assert.Equal(split.Position.Y, combined.Position.Y, precision: 5);
|
||||
Assert.Equal(split.Position.Z, combined.Position.Z, precision: 5);
|
||||
float alignment = MathF.Abs(Quaternion.Dot(
|
||||
Quaternion.Normalize(split.BodyOrientation),
|
||||
Quaternion.Normalize(combined.BodyOrientation)));
|
||||
Assert.InRange(alignment, 0.99999f, 1.00001f);
|
||||
Assert.Equal(2, combinedHooks);
|
||||
Assert.Equal(2, splitHooks);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TickHidden_DoesNotAdvancePartArrayButStillProcessesHooks()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
int advances = 0;
|
||||
int hookPasses = 0;
|
||||
controller.AttachAnimationRootMotionSource(
|
||||
(_, frame) =>
|
||||
{
|
||||
advances++;
|
||||
frame.Origin = new Vector3(0f, 1f, 0f);
|
||||
},
|
||||
() => hookPasses++);
|
||||
|
||||
controller.TickHidden(ObjectTick);
|
||||
|
||||
Assert.Equal(0, advances);
|
||||
Assert.Equal(1, hookPasses);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InvalidElapsed_VisibleFrameIsPurePresentationRead()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
float initialTime = controller.SimTimeSeconds;
|
||||
float initialYaw = controller.Yaw;
|
||||
Vector3 initialPosition = controller.Position;
|
||||
RawMotionState initialMotion = controller.Motion.RawState;
|
||||
var hostileInput = new MovementInput(
|
||||
Forward: true,
|
||||
TurnLeft: true,
|
||||
Jump: true,
|
||||
Run: true);
|
||||
|
||||
foreach (float elapsed in new[]
|
||||
{
|
||||
float.NaN,
|
||||
float.PositiveInfinity,
|
||||
float.NegativeInfinity,
|
||||
-0.1f,
|
||||
0f,
|
||||
})
|
||||
{
|
||||
MovementResult result = controller.Update(elapsed, hostileInput);
|
||||
Assert.False(result.ShouldSendMovementEvent);
|
||||
}
|
||||
|
||||
Assert.Equal(initialTime, controller.SimTimeSeconds);
|
||||
Assert.Equal(initialYaw, controller.Yaw);
|
||||
Assert.Equal(initialPosition, controller.Position);
|
||||
Assert.Equal(initialMotion, controller.Motion.RawState);
|
||||
|
||||
controller.Update(ObjectTick, new MovementInput());
|
||||
controller.Update(ObjectTick, new MovementInput());
|
||||
Assert.True(controller.AdvancedObjectQuantumLastTick);
|
||||
controller.Update(float.NaN, hostileInput);
|
||||
Assert.False(controller.AdvancedObjectQuantumLastTick);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InvalidElapsed_HiddenFrameDoesNotAdvanceClockOrManagerTail()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
float initialTime = controller.SimTimeSeconds;
|
||||
int targetPasses = 0;
|
||||
|
||||
controller.TickHidden(float.NaN, () => targetPasses++);
|
||||
controller.TickHidden(float.PositiveInfinity, () => targetPasses++);
|
||||
controller.TickHidden(-0.1f, () => targetPasses++);
|
||||
|
||||
Assert.Equal(initialTime, controller.SimTimeSeconds);
|
||||
Assert.Equal(0, targetPasses);
|
||||
Assert.False(controller.AdvancedObjectQuantumLastTick);
|
||||
|
||||
controller.TickHidden(ObjectTick);
|
||||
controller.TickHidden(ObjectTick);
|
||||
Assert.True(controller.AdvancedObjectQuantumLastTick);
|
||||
controller.TickHidden(float.PositiveInfinity);
|
||||
Assert.False(controller.AdvancedObjectQuantumLastTick);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_AirbornePartArrayFrame_SuppressesOriginButPreservesOrientation()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
controller.Update(1f, new MovementInput(Jump: true));
|
||||
|
||||
Vector3 beforeRelease = controller.Position;
|
||||
Quaternion beforeOrientation = controller.BodyOrientation;
|
||||
Quaternion delta = Quaternion.CreateFromAxisAngle(Vector3.UnitY, 0.4f);
|
||||
int advances = 0;
|
||||
controller.AttachAnimationRootMotionSource((_, frame) =>
|
||||
{
|
||||
advances++;
|
||||
frame.Origin = new Vector3(0f, 10f, 0f);
|
||||
frame.Orientation = delta;
|
||||
});
|
||||
|
||||
controller.Update(ObjectTick, new MovementInput(Jump: false));
|
||||
|
||||
Assert.True(controller.IsAirborne);
|
||||
Assert.Equal(beforeRelease.X, controller.Position.X, precision: 5);
|
||||
Assert.Equal(beforeRelease.Y, controller.Position.Y, precision: 5);
|
||||
Quaternion expected = Quaternion.Normalize(beforeOrientation * delta);
|
||||
float alignment = MathF.Abs(Quaternion.Dot(
|
||||
expected,
|
||||
Quaternion.Normalize(controller.BodyOrientation)));
|
||||
Assert.InRange(alignment, 0.99999f, 1.00001f);
|
||||
Assert.Equal(1, advances);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_AttachedAnimationTurn_IsNotAppliedByASecondYawIntegrator()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
controller.Yaw = 0f;
|
||||
controller.AttachAnimationRootMotionSource((dt, frame) =>
|
||||
{
|
||||
frame.Orientation = Quaternion.CreateFromAxisAngle(
|
||||
Vector3.UnitZ,
|
||||
-(MathF.PI / 2f) * dt);
|
||||
});
|
||||
|
||||
controller.Update(
|
||||
ObjectTick,
|
||||
new MovementInput(TurnRight: true));
|
||||
|
||||
Assert.Equal(
|
||||
-(MathF.PI / 2f) * ObjectTick,
|
||||
controller.Yaw,
|
||||
precision: 4);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_SubQuantumFrame_InterpolatesRenderPositionWithoutAdvancingPhysicsPosition()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
var start = new Vector3(96f, 96f, 50f);
|
||||
controller.SetPosition(start, 0x0001);
|
||||
controller.Yaw = 0f;
|
||||
|
||||
var firstTick = controller.Update(ObjectTick, new MovementInput(Forward: true));
|
||||
Assert.True(firstTick.Position.X > start.X, "Physics tick should advance the authoritative body position");
|
||||
Assert.Equal(start.X, firstTick.RenderPosition.X, precision: 4);
|
||||
|
||||
var halfFrame = controller.Update(PhysicsBody.MinQuantum * 0.5f, new MovementInput(Forward: true));
|
||||
|
||||
Assert.Equal(firstTick.Position.X, halfFrame.Position.X, precision: 4);
|
||||
Assert.True(halfFrame.RenderPosition.X > start.X, "Render position should move between physics ticks");
|
||||
Assert.True(halfFrame.RenderPosition.X < firstTick.Position.X,
|
||||
$"Render X={halfFrame.RenderPosition.X} should stay between {start.X} and {firstTick.Position.X}");
|
||||
|
||||
float expectedMidpoint = start.X + ((firstTick.Position.X - start.X) * 0.5f);
|
||||
Assert.Equal(expectedMidpoint, halfFrame.RenderPosition.X, precision: 3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetPosition_ResnapsRenderInterpolationEndpoints()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
controller.Yaw = 0f;
|
||||
|
||||
controller.Update(ObjectTick, new MovementInput(Forward: true));
|
||||
controller.Update(PhysicsBody.MinQuantum * 0.5f, new MovementInput(Forward: true));
|
||||
|
||||
var snapped = new Vector3(120f, 80f, 50f);
|
||||
controller.SetPosition(snapped, 0x0001);
|
||||
var result = controller.Update(PhysicsBody.MinQuantum * 0.5f, new MovementInput());
|
||||
|
||||
Assert.Equal(snapped, result.Position);
|
||||
Assert.Equal(snapped, result.RenderPosition);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BlipPosition_ResnapsPoseWithoutStoppingActiveMotion()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
controller.Yaw = 0f;
|
||||
controller.Update(ObjectTick, new MovementInput(Forward: true));
|
||||
Vector3 velocity = controller.BodyVelocity;
|
||||
Assert.True(velocity.LengthSquared() > 0f);
|
||||
|
||||
var corrected = new Vector3(100f, 98f, 50f);
|
||||
controller.BlipPosition(corrected, 0x0001, corrected);
|
||||
|
||||
Assert.Equal(corrected, controller.Position);
|
||||
Assert.Equal(corrected, controller.RenderPosition);
|
||||
Assert.Equal(velocity, controller.BodyVelocity);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BlipPosition_PublishesCanonicalOutdoorCellAndLocalFrame()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
var world = new Vector3(150f, 193f, 50f);
|
||||
var wireLocal = new Vector3(150f, 193f, 50f);
|
||||
|
||||
controller.BlipPosition(world, 0xA9B30038u, wireLocal);
|
||||
|
||||
Assert.Equal(0xA9B40031u, controller.CellId);
|
||||
Assert.Equal(controller.CellId, controller.CellPosition.ObjCellId);
|
||||
Assert.Equal(new Vector3(150f, 1f, 50f), controller.CellPosition.Frame.Origin);
|
||||
Assert.Equal(world, controller.Position);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TeleportPosition_PublishesCanonicalOutdoorCellAndLocalFrame()
|
||||
{
|
||||
var controller = new PlayerMovementController(MakeFlatEngine());
|
||||
var world = new Vector3(12f, 12f, 50f);
|
||||
var wireLocal = new Vector3(12f, 12f, 50f);
|
||||
|
||||
controller.SetPosition(world, 0xA9B40031u, wireLocal);
|
||||
|
||||
Assert.Equal(0xA9B40001u, controller.CellId);
|
||||
Assert.Equal(controller.CellId, controller.CellPosition.ObjCellId);
|
||||
Assert.Equal(wireLocal, controller.CellPosition.Frame.Origin);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_HugeQuantumDiscard_ResnapsRenderInterpolationEndpoints()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
controller.Yaw = 0f;
|
||||
int animationAdvances = 0;
|
||||
int hookPasses = 0;
|
||||
controller.AttachAnimationRootMotionSource(
|
||||
(_, frame) =>
|
||||
{
|
||||
animationAdvances++;
|
||||
frame.Origin = new Vector3(0f, 0.1f, 0f);
|
||||
},
|
||||
() => hookPasses++);
|
||||
|
||||
var moved = controller.Update(ObjectTick, new MovementInput(Forward: true));
|
||||
int advancesBeforeDiscard = animationAdvances;
|
||||
int hooksBeforeDiscard = hookPasses;
|
||||
var stale = controller.Update(PhysicsBody.HugeQuantum + 0.1f, new MovementInput(Forward: true));
|
||||
|
||||
Assert.Equal(moved.Position.X, stale.Position.X, precision: 4);
|
||||
Assert.Equal(stale.Position, stale.RenderPosition);
|
||||
Assert.Equal(advancesBeforeDiscard, animationAdvances);
|
||||
Assert.Equal(hooksBeforeDiscard, hookPasses);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_LeftoverAboveMinQuantum_ClampsRenderAlphaToCurrentPhysicsPosition()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
controller.Yaw = 0f;
|
||||
|
||||
var result = controller.Update(
|
||||
PhysicsBody.MaxQuantum + PhysicsBody.MinQuantum,
|
||||
new MovementInput(Forward: true));
|
||||
|
||||
Assert.Equal(result.Position.X, result.RenderPosition.X, precision: 4);
|
||||
Assert.Equal(result.Position.Y, result.RenderPosition.Y, precision: 4);
|
||||
Assert.Equal(result.Position.Z, result.RenderPosition.Z, precision: 4);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_RunForward_MoveFasterThanWalk()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
controller.Yaw = 0f;
|
||||
|
||||
var walkInput = new MovementInput { Forward = true };
|
||||
var walkResult = controller.Update(1.0f, walkInput);
|
||||
float walkDist = walkResult.Position.X - 96f;
|
||||
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
|
||||
var runInput = new MovementInput { Forward = true, Run = true };
|
||||
var runResult = controller.Update(1.0f, runInput);
|
||||
float runDist = runResult.Position.X - 96f;
|
||||
|
||||
Assert.True(runDist > walkDist, $"Run ({runDist}) should be faster than walk ({walkDist})");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_TurnInput_ChangesYaw()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
float initialYaw = controller.Yaw;
|
||||
|
||||
var input = new MovementInput { TurnRight = true };
|
||||
controller.Update(0.5f, input);
|
||||
|
||||
Assert.NotEqual(initialYaw, controller.Yaw);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MotionStateChanged_WhenStartingToWalk()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
|
||||
// First frame: idle (no input).
|
||||
controller.Update(0.016f, new MovementInput());
|
||||
|
||||
// Second frame: start walking.
|
||||
var input = new MovementInput { Forward = true };
|
||||
var result = controller.Update(0.016f, input);
|
||||
|
||||
Assert.True(result.MotionStateChanged);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_JumpOnFlatTerrain_BecomesAirborne()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
|
||||
// Charged jump: hold for a full charge (1s dt), then release to fire.
|
||||
// A full charge gives enough Vz that the player clears the 0.05-unit
|
||||
// ground-snap threshold within the same integration frame.
|
||||
controller.Update(1.0f, new MovementInput(Jump: true)); // full charge
|
||||
controller.Update(0.016f, new MovementInput(Jump: false)); // release → jump fires
|
||||
|
||||
Assert.True(controller.IsAirborne);
|
||||
Assert.True(controller.VerticalVelocity > 0f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PositionEventGateRequiresContactAndWalkable()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
|
||||
Assert.True(controller.CanSendPositionEvent);
|
||||
|
||||
controller.Update(1.0f, new MovementInput(Jump: true));
|
||||
controller.Update(0.016f, new MovementInput(Jump: false));
|
||||
|
||||
Assert.True(controller.IsAirborne);
|
||||
Assert.False(controller.CanSendPositionEvent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JumpChargeSnapshot_TracksHeldChargeAndResetsOnRelease()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
|
||||
Assert.Equal(default, controller.JumpCharge);
|
||||
|
||||
controller.Update(0.25f, new MovementInput(Jump: true));
|
||||
Assert.True(controller.JumpCharge.IsCharging);
|
||||
Assert.Equal(0.25f, controller.JumpCharge.Power, precision: 3);
|
||||
|
||||
controller.Update(0.016f, new MovementInput(Jump: false));
|
||||
Assert.False(controller.JumpCharge.IsCharging);
|
||||
Assert.Equal(0f, controller.JumpCharge.Power);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_AirborneFrames_ZRiseThenFalls()
|
||||
{
|
||||
var engine = MakeFlatEngine();
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(96f, 96f, 50f), 0x0001);
|
||||
|
||||
// Charged jump: hold for a full charge, then release.
|
||||
controller.Update(1.0f, new MovementInput(Jump: true)); // full charge
|
||||
controller.Update(0.016f, new MovementInput(Jump: false)); // release → jump fires
|
||||
float z1 = controller.Position.Z;
|
||||
|
||||
// A few frames of rising
|
||||
controller.Update(0.1f, new MovementInput());
|
||||
float z2 = controller.Position.Z;
|
||||
Assert.True(z2 > z1, "Should be rising");
|
||||
|
||||
// Many frames — should come back down.
|
||||
// DefaultJumpVz = 10 m/s → full flight time ≈ 2.04s, so run 50 × 50ms = 2.5s
|
||||
// to ensure the player has definitely landed.
|
||||
for (int i = 0; i < 50; i++)
|
||||
controller.Update(0.05f, new MovementInput());
|
||||
|
||||
Assert.False(controller.IsAirborne, "Should have landed");
|
||||
Assert.Equal(50f, controller.Position.Z, precision: 1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_WalkOffLedge_BecomesFalling()
|
||||
{
|
||||
// Build terrain with a sharp cliff: grid x<5 = Z50, grid x>=5 = Z20.
|
||||
// heights[x*9+y] is indexed x-major; heightTable[i]=i*1f so
|
||||
// byte value == Z value directly.
|
||||
var heights = new byte[81];
|
||||
for (int x = 0; x < 9; x++)
|
||||
for (int y = 0; y < 9; y++)
|
||||
heights[x * 9 + y] = (byte)(x < 5 ? 50 : 20);
|
||||
|
||||
var heightTable = new float[256];
|
||||
for (int i = 0; i < 256; i++) heightTable[i] = i * 1f;
|
||||
|
||||
var engine = new PhysicsEngine();
|
||||
var terrain = new TerrainSurface(heights, heightTable);
|
||||
engine.AddLandblock(0xA9B4FFFFu, terrain, Array.Empty<CellSurface>(),
|
||||
Array.Empty<PortalPlane>(), worldOffsetX: 0f, worldOffsetY: 0f);
|
||||
|
||||
// Position the player just before the cliff edge (localX=118 ≈ grid x=4.92).
|
||||
// At this point terrain Z is ~51.7 (bilinear interpolation near the high side).
|
||||
// One step at walk speed will cross into the low region where terrain drops
|
||||
// ~28 units — more than StepUpHeight=5, triggering the ledge-fall.
|
||||
var controller = new PlayerMovementController(engine);
|
||||
controller.SetPosition(new Vector3(118f, 96f, 50f), 0x0001);
|
||||
controller.Yaw = 0f; // facing +X
|
||||
|
||||
// Single step — should trigger airborne state because terrain drops sharply.
|
||||
controller.Update(0.05f, new MovementInput(Forward: true));
|
||||
|
||||
Assert.True(controller.IsAirborne, "Player should be airborne after stepping off the cliff");
|
||||
|
||||
// Simulate enough frames to fall and land on the Z=20 floor.
|
||||
for (int i = 0; i < 60; i++)
|
||||
controller.Update(0.05f, new MovementInput(Forward: true));
|
||||
|
||||
Assert.False(controller.IsAirborne, "Player should have landed");
|
||||
Assert.Equal(20f, controller.Position.Z, precision: 1);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,214 @@
|
|||
using System.Numerics;
|
||||
using AcDream.Core.Physics;
|
||||
using AcDream.Runtime.Gameplay;
|
||||
|
||||
namespace AcDream.Runtime.Tests.Gameplay;
|
||||
|
||||
public sealed class PlayerOutboundPositionTests
|
||||
{
|
||||
private static readonly Plane GroundPlane =
|
||||
new(Vector3.UnitZ, 0f);
|
||||
|
||||
[Fact]
|
||||
public void CrossLandblockWorldFrame_SendsCarriedCellLocalPosition()
|
||||
{
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
|
||||
// The render origin remains A9B1, so a point 22.56 m into A9B2 is
|
||||
// represented at world Y=214.56. The wire frame must remain the
|
||||
// carried A9B2-local 22.56, never the render-space 214.56.
|
||||
controller.SetPosition(
|
||||
new Vector3(116.07f, 214.56f, 83.76f),
|
||||
0xA9B20021u,
|
||||
new Vector3(116.07f, 22.56f, 83.76f));
|
||||
|
||||
Assert.True(controller.TryGetOutboundPosition(out Position outbound));
|
||||
Assert.Equal(0xA9B20021u, outbound.ObjCellId);
|
||||
Assert.Equal(
|
||||
new Vector3(116.07f, 22.56f, 83.76f),
|
||||
outbound.Frame.Origin);
|
||||
Assert.NotEqual(controller.Position, outbound.Frame.Origin);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnplacedBody_DoesNotProduceWirePosition()
|
||||
{
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
|
||||
Assert.False(controller.TryGetOutboundPosition(out _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OutboundPosition_PreservesCompleteAuthoritativeQuaternion()
|
||||
{
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
controller.SetPosition(
|
||||
new Vector3(116.07f, 214.56f, 83.76f),
|
||||
0xA9B20021u,
|
||||
new Vector3(116.07f, 22.56f, 83.76f));
|
||||
Quaternion complete = Quaternion.Normalize(
|
||||
Quaternion.CreateFromAxisAngle(Vector3.UnitX, 0.7f)
|
||||
* Quaternion.CreateFromAxisAngle(Vector3.UnitY, -0.4f));
|
||||
controller.SetBodyOrientation(complete);
|
||||
|
||||
Assert.True(controller.TryGetOutboundPosition(out Position outbound));
|
||||
|
||||
Assert.InRange(
|
||||
MathF.Abs(Quaternion.Dot(
|
||||
complete,
|
||||
Quaternion.Normalize(outbound.Frame.Orientation))),
|
||||
0.99999f,
|
||||
1.00001f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PositionEvent_FirstFrameIsDue()
|
||||
{
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
var current = PositionAt(Quaternion.Identity);
|
||||
|
||||
Assert.True(controller.ShouldSendPositionEvent(current, GroundPlane, 0f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PositionEvent_StationaryHeadingChangeIsDueAfterRetailInterval()
|
||||
{
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
var sent = PositionAt(Quaternion.Identity);
|
||||
var turned = PositionAt(Quaternion.CreateFromAxisAngle(
|
||||
Vector3.UnitZ,
|
||||
MathF.PI / 2f));
|
||||
controller.NotePositionSent(sent, GroundPlane, nowSeconds: 0f);
|
||||
|
||||
Assert.False(controller.ShouldSendPositionEvent(
|
||||
turned,
|
||||
GroundPlane,
|
||||
PlayerMovementController.HeartbeatInterval));
|
||||
Assert.True(controller.ShouldSendPositionEvent(
|
||||
turned,
|
||||
GroundPlane,
|
||||
PlayerMovementController.HeartbeatInterval + 0.01f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PositionEvent_UnchangedCompleteFrameIsNotIdleHeartbeat()
|
||||
{
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
var sent = PositionAt(Quaternion.Identity);
|
||||
controller.NotePositionSent(sent, GroundPlane, nowSeconds: 0f);
|
||||
|
||||
Assert.False(controller.ShouldSendPositionEvent(
|
||||
sent,
|
||||
GroundPlane,
|
||||
PlayerMovementController.HeartbeatInterval + 0.01f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MovementEvent_StampsOnlyTime_NotTheRememberedFrame()
|
||||
{
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
var sent = PositionAt(Quaternion.Identity);
|
||||
var turned = PositionAt(Quaternion.CreateFromAxisAngle(
|
||||
Vector3.UnitZ,
|
||||
MathF.PI / 2f));
|
||||
controller.NotePositionSent(sent, GroundPlane, nowSeconds: 0f);
|
||||
controller.NoteMovementSent(nowSeconds: 0.9f);
|
||||
|
||||
Assert.False(controller.ShouldSendPositionEvent(
|
||||
turned,
|
||||
GroundPlane,
|
||||
nowSeconds: 1.1f));
|
||||
Assert.True(controller.ShouldSendPositionEvent(
|
||||
turned,
|
||||
GroundPlane,
|
||||
nowSeconds: 1.91f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PositionEvent_CellOrContactPlaneChangeIsDueInsideInterval()
|
||||
{
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
var sent = PositionAt(Quaternion.Identity);
|
||||
controller.NotePositionSent(sent, GroundPlane, nowSeconds: 0f);
|
||||
|
||||
var otherCell = sent with { ObjCellId = 0xA9B20022u };
|
||||
var otherPlane = new Plane(Vector3.Normalize(new Vector3(0.01f, 0f, 1f)), 0f);
|
||||
|
||||
Assert.True(controller.ShouldSendPositionEvent(otherCell, GroundPlane, 0.1f));
|
||||
Assert.True(controller.ShouldSendPositionEvent(sent, otherPlane, 0.1f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PositionEvent_QuaternionSignIsStoredFrameDataLikeRetail()
|
||||
{
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
var sent = PositionAt(Quaternion.Identity);
|
||||
var oppositeStoredQuaternion = PositionAt(new Quaternion(0f, 0f, 0f, -1f));
|
||||
controller.NotePositionSent(sent, GroundPlane, nowSeconds: 0f);
|
||||
|
||||
Assert.True(controller.ShouldSendPositionEvent(
|
||||
oppositeStoredQuaternion,
|
||||
GroundPlane,
|
||||
PlayerMovementController.HeartbeatInterval + 0.01f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PositionEvent_RetailEpsilonUsesDifferentOriginAndQuaternionBoundaries()
|
||||
{
|
||||
const float epsilon = 0.000199999995f;
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
var sent = new Position(0xA9B20021u, Vector3.Zero, Quaternion.Identity);
|
||||
controller.NotePositionSent(sent, GroundPlane, nowSeconds: 0f);
|
||||
|
||||
var exactOriginBoundary = sent with
|
||||
{
|
||||
Frame = sent.Frame with { Origin = new Vector3(epsilon, 0f, 0f) },
|
||||
};
|
||||
var exactQuaternionBoundary = sent with
|
||||
{
|
||||
Frame = sent.Frame with
|
||||
{
|
||||
Orientation = new Quaternion(epsilon, 0f, 0f, 1f),
|
||||
},
|
||||
};
|
||||
float elapsed = PlayerMovementController.HeartbeatInterval + 0.01f;
|
||||
|
||||
Assert.False(controller.ShouldSendPositionEvent(
|
||||
exactOriginBoundary,
|
||||
GroundPlane,
|
||||
elapsed));
|
||||
Assert.True(controller.ShouldSendPositionEvent(
|
||||
exactQuaternionBoundary,
|
||||
GroundPlane,
|
||||
elapsed));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PositionEvent_RetailPlaneDistanceBoundaryIsStrict()
|
||||
{
|
||||
const float epsilon = 0.000199999995f;
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
var sent = PositionAt(Quaternion.Identity);
|
||||
controller.NotePositionSent(sent, GroundPlane, nowSeconds: 0f);
|
||||
|
||||
var exactNormalBoundary = new Plane(
|
||||
new Vector3(epsilon, 0f, 1f),
|
||||
0f);
|
||||
var exactDistanceBoundary = new Plane(Vector3.UnitZ, epsilon);
|
||||
|
||||
Assert.False(controller.ShouldSendPositionEvent(
|
||||
sent,
|
||||
exactNormalBoundary,
|
||||
nowSeconds: 0.1f));
|
||||
Assert.True(controller.ShouldSendPositionEvent(
|
||||
sent,
|
||||
exactDistanceBoundary,
|
||||
nowSeconds: 0.1f));
|
||||
}
|
||||
|
||||
private static Position PositionAt(Quaternion orientation)
|
||||
=> new(
|
||||
0xA9B20021u,
|
||||
new Vector3(116.07f, 22.56f, 83.76f),
|
||||
orientation);
|
||||
}
|
||||
|
|
@ -9,13 +9,15 @@ namespace AcDream.Runtime.Tests.Gameplay;
|
|||
public sealed class RuntimeGameplayOwnershipTests
|
||||
{
|
||||
[Fact]
|
||||
public void CombinedLedgerConvergesEveryJ4OwnerAndSubscription()
|
||||
public void CombinedLedgerConvergesEveryGameplayOwnerAndSubscription()
|
||||
{
|
||||
using var entities = new RuntimeEntityObjectLifetime();
|
||||
var inventory = new RuntimeInventoryState(entities);
|
||||
var character = new RuntimeCharacterState();
|
||||
var communication = new RuntimeCommunicationState();
|
||||
var actions = RuntimeActionTestFactory.Create(inventory.Transactions);
|
||||
var movement = new RuntimeLocalPlayerMovementState();
|
||||
movement.Execute(RuntimeMovementCommand.ToggleRunLock);
|
||||
inventory.Shortcuts.Changed += static () => { };
|
||||
inventory.Shortcuts.Load([new ShortcutEntry(1, 2u, 3u)]);
|
||||
inventory.ItemMana.OnQueryItemManaResponse(2u, 0.5f, true);
|
||||
|
|
@ -73,7 +75,8 @@ public sealed class RuntimeGameplayOwnershipTests
|
|||
inventory,
|
||||
character,
|
||||
communication,
|
||||
actions);
|
||||
actions,
|
||||
movement);
|
||||
|
||||
Assert.False(populated.IsConverged);
|
||||
Assert.Equal(1, populated.Inventory.ShortcutCount);
|
||||
|
|
@ -86,7 +89,9 @@ public sealed class RuntimeGameplayOwnershipTests
|
|||
AcDream.Core.Combat.CombatMode.Melee,
|
||||
populated.Actions.CombatMode);
|
||||
Assert.Equal(1, populated.Actions.TrackedTargetHealthCount);
|
||||
Assert.True(populated.Movement.AutoRunActive);
|
||||
|
||||
movement.Dispose();
|
||||
actions.Dispose();
|
||||
communication.Dispose();
|
||||
character.Dispose();
|
||||
|
|
@ -98,7 +103,8 @@ public sealed class RuntimeGameplayOwnershipTests
|
|||
inventory,
|
||||
character,
|
||||
communication,
|
||||
actions);
|
||||
actions,
|
||||
movement);
|
||||
|
||||
Assert.True(retired.IsConverged);
|
||||
Assert.Equal(0, retired.Inventory.ShortcutSubscriberCount);
|
||||
|
|
@ -108,13 +114,14 @@ public sealed class RuntimeGameplayOwnershipTests
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void BorrowerFailuresCannotStrandAnyJ4OwnerDuringTerminalDisposal()
|
||||
public void BorrowerFailuresCannotStrandAnyGameplayOwnerDuringTerminalDisposal()
|
||||
{
|
||||
using var entities = new RuntimeEntityObjectLifetime();
|
||||
var inventory = new RuntimeInventoryState(entities);
|
||||
var character = new RuntimeCharacterState();
|
||||
var communication = new RuntimeCommunicationState();
|
||||
var actions = RuntimeActionTestFactory.Create(inventory.Transactions);
|
||||
var movement = new RuntimeLocalPlayerMovementState();
|
||||
|
||||
inventory.ExternalContainers.RequestOpen(0x70000001u);
|
||||
inventory.ExternalContainers.ApplyViewContents(0x70000001u);
|
||||
|
|
@ -132,6 +139,7 @@ public sealed class RuntimeGameplayOwnershipTests
|
|||
communication.Chat.OnSystemMessage("failure-isolated event", 0u);
|
||||
Assert.Equal(1, communication.DispatchFailureCount);
|
||||
|
||||
movement.Dispose();
|
||||
actions.Dispose();
|
||||
Assert.Throws<AggregateException>(inventory.Dispose);
|
||||
Assert.Throws<AggregateException>(character.Dispose);
|
||||
|
|
@ -142,7 +150,8 @@ public sealed class RuntimeGameplayOwnershipTests
|
|||
inventory,
|
||||
character,
|
||||
communication,
|
||||
actions);
|
||||
actions,
|
||||
movement);
|
||||
|
||||
Assert.True(retired.IsConverged);
|
||||
Assert.Equal(1, retired.Communication.DispatchFailureCount);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,174 @@
|
|||
using System.Numerics;
|
||||
using AcDream.Core.Physics;
|
||||
using AcDream.Runtime.Gameplay;
|
||||
|
||||
namespace AcDream.Runtime.Tests.Gameplay;
|
||||
|
||||
public sealed class RuntimeLocalPlayerMovementStateTests
|
||||
{
|
||||
[Fact]
|
||||
public void ViewProjectsTheExactCanonicalControllerAndAutorunOwner()
|
||||
{
|
||||
var controller = new PlayerMovementController(new PhysicsEngine())
|
||||
{
|
||||
LocalEntityId = 0x50000001u,
|
||||
};
|
||||
controller.SetPosition(
|
||||
new Vector3(11f, 12f, 13f),
|
||||
0xA9B40001u,
|
||||
new Vector3(11f, 12f, 13f));
|
||||
using var movement = new RuntimeLocalPlayerMovementState
|
||||
{
|
||||
Controller = controller,
|
||||
};
|
||||
|
||||
Assert.Same(movement, movement.View);
|
||||
Assert.True(movement.Execute(RuntimeMovementCommand.ToggleRunLock));
|
||||
|
||||
RuntimeMovementSnapshot snapshot = movement.View.Snapshot;
|
||||
Assert.True(snapshot.HasController);
|
||||
Assert.Equal(0x50000001u, snapshot.LocalEntityId);
|
||||
Assert.Equal(controller.CellPosition, snapshot.Position);
|
||||
Assert.Equal(controller.BodyVelocity, snapshot.Velocity);
|
||||
Assert.Equal(controller.IsAirborne, snapshot.IsAirborne);
|
||||
Assert.Equal(controller.SimTimeSeconds, snapshot.SimulationTimeSeconds);
|
||||
Assert.True(snapshot.AutoRunActive);
|
||||
Assert.Equal(movement.Revision, snapshot.Revision);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GraphicalAndDirectCommandsMutateOneAutorunLatch()
|
||||
{
|
||||
using var movement = new RuntimeLocalPlayerMovementState();
|
||||
|
||||
Assert.True(movement.Execute(RuntimeMovementCommand.ToggleRunLock));
|
||||
Assert.True(movement.AutoRunActive);
|
||||
Assert.True(movement.View.Snapshot.AutoRunActive);
|
||||
|
||||
Assert.True(movement.Execute(RuntimeMovementCommand.Stop));
|
||||
Assert.False(movement.AutoRunActive);
|
||||
Assert.False(movement.View.Snapshot.AutoRunActive);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConcurrentRuntimeInstancesHaveIndependentMovementState()
|
||||
{
|
||||
using var first = new RuntimeLocalPlayerMovementState();
|
||||
using var second = new RuntimeLocalPlayerMovementState();
|
||||
var firstController = new PlayerMovementController(new PhysicsEngine())
|
||||
{
|
||||
LocalEntityId = 1u,
|
||||
};
|
||||
var secondController = new PlayerMovementController(new PhysicsEngine())
|
||||
{
|
||||
LocalEntityId = 2u,
|
||||
};
|
||||
first.Controller = firstController;
|
||||
second.Controller = secondController;
|
||||
|
||||
first.Execute(RuntimeMovementCommand.ToggleRunLock);
|
||||
firstController.SetPosition(Vector3.One, 0xA9B40001u, Vector3.One);
|
||||
secondController.SetPosition(
|
||||
new Vector3(2f),
|
||||
0xA9B50001u,
|
||||
new Vector3(2f));
|
||||
|
||||
Assert.True(first.Snapshot.AutoRunActive);
|
||||
Assert.False(second.Snapshot.AutoRunActive);
|
||||
Assert.Equal(1u, first.Snapshot.LocalEntityId);
|
||||
Assert.Equal(2u, second.Snapshot.LocalEntityId);
|
||||
Assert.NotEqual(first.Snapshot.Position, second.Snapshot.Position);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MotionPreparationPublishesOnlyTheConstructionSeam()
|
||||
{
|
||||
using var movement = new RuntimeLocalPlayerMovementState();
|
||||
IRuntimeLocalPlayerMotionSource source = movement;
|
||||
var committed = new PlayerMovementController(new PhysicsEngine());
|
||||
var candidate = new PlayerMovementController(new PhysicsEngine());
|
||||
movement.Controller = committed;
|
||||
bool drained = false;
|
||||
|
||||
using (movement.BeginMotionPreparation(
|
||||
candidate,
|
||||
() =>
|
||||
{
|
||||
drained = true;
|
||||
Assert.Same(committed.Motion, source.Motion);
|
||||
}))
|
||||
{
|
||||
Assert.True(drained);
|
||||
Assert.Same(committed, movement.Controller);
|
||||
Assert.Same(candidate.Motion, source.Motion);
|
||||
}
|
||||
|
||||
Assert.Same(committed.Motion, source.Motion);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TerminalDisposalConvergesWhenPreparationLeaseUnwindsLater()
|
||||
{
|
||||
var movement = new RuntimeLocalPlayerMovementState();
|
||||
var controller = new PlayerMovementController(new PhysicsEngine());
|
||||
IDisposable preparation = movement.BeginMotionPreparation(controller);
|
||||
movement.Controller = controller;
|
||||
movement.Execute(RuntimeMovementCommand.ToggleRunLock);
|
||||
|
||||
movement.Dispose();
|
||||
preparation.Dispose();
|
||||
|
||||
RuntimeLocalMovementOwnershipSnapshot ownership =
|
||||
movement.CaptureOwnership();
|
||||
Assert.True(ownership.IsConverged);
|
||||
Assert.Throws<ObjectDisposedException>(
|
||||
() => movement.Execute(RuntimeMovementCommand.ToggleRunLock));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TypedSkillOptionsPreserveCompleteValuesAndFallbackPerField()
|
||||
{
|
||||
Assert.Equal(
|
||||
new PlayerMovementConstructionOptions(321, 654),
|
||||
PlayerMovementConstructionOptions.From(
|
||||
new RuntimeMovementSkillSnapshot(321, 654, 1)));
|
||||
Assert.Equal(
|
||||
new PlayerMovementConstructionOptions(
|
||||
PlayerMovementConstructionOptions.FallbackRunSkill,
|
||||
654),
|
||||
PlayerMovementConstructionOptions.From(
|
||||
new RuntimeMovementSkillSnapshot(-1, 654, 1)));
|
||||
Assert.Equal(
|
||||
new PlayerMovementConstructionOptions(
|
||||
321,
|
||||
PlayerMovementConstructionOptions.FallbackJumpSkill),
|
||||
PlayerMovementConstructionOptions.From(
|
||||
new RuntimeMovementSkillSnapshot(321, -1, 1)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmMovementSnapshotsAndOwnershipCaptureAllocateNothing()
|
||||
{
|
||||
using var movement = new RuntimeLocalPlayerMovementState();
|
||||
movement.Controller = new PlayerMovementController(new PhysicsEngine())
|
||||
{
|
||||
LocalEntityId = 7u,
|
||||
};
|
||||
|
||||
for (int i = 0; i < 1_000; i++)
|
||||
{
|
||||
_ = movement.Snapshot;
|
||||
_ = movement.CaptureOwnership();
|
||||
}
|
||||
|
||||
long before = GC.GetAllocatedBytesForCurrentThread();
|
||||
for (int i = 0; i < 100_000; i++)
|
||||
{
|
||||
_ = movement.Snapshot;
|
||||
_ = movement.CaptureOwnership();
|
||||
}
|
||||
long allocated = GC.GetAllocatedBytesForCurrentThread() - before;
|
||||
|
||||
Assert.Equal(0L, allocated);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue