refactor(runtime): own teleport destination correlation

This commit is contained in:
Erik 2026-07-26 17:52:34 +02:00
parent 38b3773cb9
commit 6a063a27d4
23 changed files with 1209 additions and 487 deletions

View file

@ -1,3 +1,5 @@
using System.Numerics;
using AcDream.Core.Physics;
using AcDream.Runtime.World;
namespace AcDream.Runtime.Tests.World;
@ -13,7 +15,7 @@ public sealed class RuntimeWorldTransitStateTests
var state = new RuntimeWorldTransitState();
long generation =
state.BeginReveal(RuntimePortalKind.Login, OutdoorCell);
state.BeginLoginReveal(OutdoorCell);
Assert.Equal(1, generation);
Assert.Equal(generation, state.Snapshot.Generation);
@ -29,7 +31,7 @@ public sealed class RuntimeWorldTransitStateTests
List<string> log = [];
var state = new RuntimeWorldTransitState(log.Add);
long generation =
state.BeginReveal(RuntimePortalKind.Portal, OutdoorCell);
BeginPortal(state, OutdoorCell);
Assert.False(state.AcknowledgeDestinationReadiness(
Ready(generation + 1, OutdoorCell)));
@ -56,12 +58,13 @@ public sealed class RuntimeWorldTransitStateTests
{
var state = new RuntimeWorldTransitState();
long generation =
state.BeginReveal(RuntimePortalKind.Portal, OutdoorCell);
BeginPortal(state, OutdoorCell);
Assert.True(state.AcknowledgeDestinationReadiness(
Ready(generation, OutdoorCell)));
Assert.True(state.AcknowledgeMaterialized(
Assert.True(state.AcknowledgePortalMaterialized(
generation,
1,
OutdoorCell));
Assert.True(state.IsWorldSimulationAvailable);
@ -69,8 +72,9 @@ public sealed class RuntimeWorldTransitStateTests
Assert.False(state.Snapshot.WorldViewportObserved);
Assert.False(state.Snapshot.Completed);
Assert.Equal(1, state.Snapshot.PortalMaterializationCount);
Assert.False(state.AcknowledgeMaterialized(
Assert.False(state.AcknowledgePortalMaterialized(
generation,
1,
OutdoorCell));
Assert.True(state.AcknowledgeWorldViewportVisible(generation));
@ -85,10 +89,11 @@ public sealed class RuntimeWorldTransitStateTests
{
var state = new RuntimeWorldTransitState();
long generation =
state.BeginReveal(RuntimePortalKind.Portal, OutdoorCell);
BeginPortal(state, OutdoorCell);
Assert.False(state.AcknowledgeMaterialized(
Assert.False(state.AcknowledgePortalMaterialized(
generation,
1,
OutdoorCell));
Assert.False(state.IsWorldSimulationAvailable);
@ -102,7 +107,7 @@ public sealed class RuntimeWorldTransitStateTests
{
var state = new RuntimeWorldTransitState();
long generation =
state.BeginReveal(RuntimePortalKind.Login, OutdoorCell);
state.BeginLoginReveal(OutdoorCell);
state.AcknowledgeDestinationReadiness(
Ready(generation, OutdoorCell));
@ -120,7 +125,7 @@ public sealed class RuntimeWorldTransitStateTests
{
var state = new RuntimeWorldTransitState();
long generation =
state.BeginReveal(RuntimePortalKind.Login, OutdoorCell);
state.BeginLoginReveal(OutdoorCell);
Assert.False(state.AcknowledgeWorldViewportVisible(generation));
@ -134,7 +139,7 @@ public sealed class RuntimeWorldTransitStateTests
{
var state = new RuntimeWorldTransitState();
long generation =
state.BeginReveal(RuntimePortalKind.Login, OutdoorCell);
state.BeginLoginReveal(OutdoorCell);
RuntimeDestinationReadiness invalid =
Ready(generation, OutdoorCell) with
{
@ -153,7 +158,7 @@ public sealed class RuntimeWorldTransitStateTests
{
var state = new RuntimeWorldTransitState();
long generation =
state.BeginReveal(RuntimePortalKind.Portal, OutdoorCell);
BeginPortal(state, OutdoorCell);
RuntimeDestinationReadiness accepted =
Ready(generation, OutdoorCell);
Assert.True(state.AcknowledgeDestinationReadiness(accepted));
@ -174,13 +179,13 @@ public sealed class RuntimeWorldTransitStateTests
{
var state = new RuntimeWorldTransitState();
long login =
state.BeginReveal(RuntimePortalKind.Login, OutdoorCell);
state.BeginLoginReveal(OutdoorCell);
Assert.False(state.Complete(login));
Assert.False(state.Snapshot.Completed);
long portal =
state.BeginReveal(RuntimePortalKind.Portal, OtherCell);
BeginPortal(state, OtherCell, sequence: 2);
Assert.True(state.AcknowledgeDestinationReadiness(
Ready(portal, OtherCell)));
Assert.False(state.Complete(portal));
@ -193,14 +198,17 @@ public sealed class RuntimeWorldTransitStateTests
{
var state = new RuntimeWorldTransitState();
long first =
state.BeginReveal(RuntimePortalKind.Login, OutdoorCell);
state.BeginLoginReveal(OutdoorCell);
long second =
state.BeginReveal(RuntimePortalKind.Portal, OtherCell);
BeginPortal(state, OtherCell);
Assert.True(second > first);
Assert.False(state.AcknowledgeDestinationReadiness(
Ready(first, OutdoorCell)));
Assert.False(state.AcknowledgeMaterialized(first, OutdoorCell));
Assert.False(state.AcknowledgePortalMaterialized(
first,
1,
OutdoorCell));
Assert.Equal(second, state.Snapshot.Generation);
Assert.Equal(OtherCell, state.Snapshot.DestinationCell);
@ -214,7 +222,7 @@ public sealed class RuntimeWorldTransitStateTests
{
var state = new RuntimeWorldTransitState();
long generation =
state.BeginReveal(RuntimePortalKind.Portal, OutdoorCell);
BeginPortal(state, OutdoorCell);
Assert.True(state.Cancel(generation));
Assert.True(state.IsWorldSimulationAvailable);
@ -222,8 +230,9 @@ public sealed class RuntimeWorldTransitStateTests
Assert.False(state.AcknowledgeDestinationReadiness(
Ready(generation, OutdoorCell)));
Assert.False(state.AcknowledgeMaterialized(
Assert.False(state.AcknowledgePortalMaterialized(
generation,
1,
OutdoorCell));
Assert.False(state.Complete(generation));
Assert.False(state.Snapshot.Materialized);
@ -235,10 +244,13 @@ public sealed class RuntimeWorldTransitStateTests
{
var state = new RuntimeWorldTransitState();
long generation =
state.BeginReveal(RuntimePortalKind.Portal, OutdoorCell);
BeginPortal(state, OutdoorCell);
state.AcknowledgeDestinationReadiness(
Ready(generation, OutdoorCell));
state.AcknowledgeMaterialized(generation, OutdoorCell);
state.AcknowledgePortalMaterialized(
generation,
1,
OutdoorCell);
Assert.True(state.Cancel(generation));
Assert.True(state.IsWorldSimulationAvailable);
@ -254,7 +266,7 @@ public sealed class RuntimeWorldTransitStateTests
{
var state = new RuntimeWorldTransitState();
long generation =
state.BeginReveal(RuntimePortalKind.Portal, OutdoorCell);
BeginPortal(state, OutdoorCell);
Assert.False(state.ObserveWait(
generation,
@ -278,7 +290,7 @@ public sealed class RuntimeWorldTransitStateTests
{
var state = new RuntimeWorldTransitState();
long generation =
state.BeginReveal(RuntimePortalKind.Portal, OutdoorCell);
BeginPortal(state, OutdoorCell);
var acknowledgement = new RuntimeDestinationReadiness(
generation,
OutdoorCell,
@ -291,8 +303,9 @@ public sealed class RuntimeWorldTransitStateTests
Assert.True(state.AcknowledgeDestinationReadiness(acknowledgement));
Assert.True(state.Snapshot.IsReady);
Assert.True(state.AcknowledgeMaterialized(
Assert.True(state.AcknowledgePortalMaterialized(
generation,
1,
OutdoorCell));
}
@ -301,14 +314,14 @@ public sealed class RuntimeWorldTransitStateTests
{
var state = new RuntimeWorldTransitState();
long first =
state.BeginReveal(RuntimePortalKind.Portal, OutdoorCell);
BeginPortal(state, OutdoorCell);
state.ResetSession();
Assert.Equal(RuntimePortalSnapshot.Idle, state.Snapshot);
Assert.True(state.IsWorldSimulationAvailable);
long second =
state.BeginReveal(RuntimePortalKind.Login, OtherCell);
state.BeginLoginReveal(OtherCell);
Assert.True(second > first);
Assert.False(state.AcknowledgeDestinationReadiness(
Ready(first, OutdoorCell)));
@ -322,12 +335,15 @@ public sealed class RuntimeWorldTransitStateTests
var first = new RuntimeWorldTransitState();
var second = new RuntimeWorldTransitState();
long firstGeneration =
first.BeginReveal(RuntimePortalKind.Portal, OutdoorCell);
BeginPortal(first, OutdoorCell);
long secondGeneration =
second.BeginReveal(RuntimePortalKind.Login, OtherCell);
second.BeginLoginReveal(OtherCell);
first.AcknowledgeDestinationReadiness(
Ready(firstGeneration, OutdoorCell));
first.AcknowledgeMaterialized(firstGeneration, OutdoorCell);
first.AcknowledgePortalMaterialized(
firstGeneration,
1,
OutdoorCell);
Assert.Equal(1, first.Snapshot.PortalMaterializationCount);
Assert.Equal(0, second.Snapshot.PortalMaterializationCount);
@ -344,11 +360,12 @@ public sealed class RuntimeWorldTransitStateTests
_ => throw new InvalidOperationException("diagnostic failed"));
long generation =
state.BeginReveal(RuntimePortalKind.Portal, OutdoorCell);
BeginPortal(state, OutdoorCell);
Assert.True(state.AcknowledgeDestinationReadiness(
Ready(generation, OutdoorCell)));
Assert.True(state.AcknowledgeMaterialized(
Assert.True(state.AcknowledgePortalMaterialized(
generation,
1,
OutdoorCell));
Assert.True(state.Complete(generation));
@ -359,6 +376,278 @@ public sealed class RuntimeWorldTransitStateTests
state.LastDiagnosticFailure);
}
[Fact]
public void ReplacementRejectsDelayedPriorDestinationAndAcceptsCurrentAdvance()
{
var state = new RuntimeWorldTransitState();
Assert.True(state.TryQueueTeleportStart(10));
Assert.True(state.ActivateQueuedTeleport());
state.EndTeleport();
Assert.True(state.TryQueueTeleportStart(11));
Assert.True(state.ActivateQueuedTeleport());
Assert.False(state.OfferTeleportDestination(
Destination(OutdoorCell, 10, x: 10f),
teleportTimestampAdvanced: true));
Assert.True(state.OfferTeleportDestination(
Destination(OtherCell, 11, x: 11f),
teleportTimestampAdvanced: true));
Assert.True(state.TryGetAcceptedTeleportDestination(
out RuntimeTeleportDestination accepted));
Assert.Equal(11f, accepted.Position.Frame.Origin.X);
Assert.Equal(OtherCell, accepted.CellId);
}
[Fact]
public void PositionBeforeF751IsBufferedAndEqualFollowupCannotReplaceIt()
{
var state = new RuntimeWorldTransitState();
Assert.False(state.OfferTeleportDestination(
Destination(OutdoorCell, 11, x: 110f),
teleportTimestampAdvanced: true));
Assert.Equal(1, state.BufferedTeleportDestinationCount);
Assert.True(state.TryQueueTeleportStart(11));
Assert.True(state.ActivateQueuedTeleport());
Assert.True(state.TryGetAcceptedTeleportDestination(
out RuntimeTeleportDestination buffered));
Assert.Equal(110f, buffered.Position.Frame.Origin.X);
Assert.False(state.OfferTeleportDestination(
Destination(OtherCell, 11, x: 111f),
teleportTimestampAdvanced: false));
Assert.True(state.TryGetAcceptedTeleportDestination(out buffered));
Assert.Equal(OutdoorCell, buffered.CellId);
}
[Fact]
public void InactiveOrdinaryPositionIsNeverBuffered()
{
var state = new RuntimeWorldTransitState();
Assert.False(state.OfferTeleportDestination(
Destination(OutdoorCell, 11),
teleportTimestampAdvanced: false));
Assert.Equal(0, state.BufferedTeleportDestinationCount);
Assert.True(state.TryQueueTeleportStart(11));
Assert.True(state.ActivateQueuedTeleport());
Assert.False(state.HasAcceptedTeleportDestination);
}
[Fact]
public void DuplicateStartIsRejectedButSequenceWrapIsFresh()
{
var state = new RuntimeWorldTransitState();
Assert.True(state.TryQueueTeleportStart(ushort.MaxValue));
Assert.True(state.ActivateQueuedTeleport());
state.EndTeleport();
Assert.False(state.CanQueueTeleportStart(ushort.MaxValue));
Assert.False(state.TryQueueTeleportStart(ushort.MaxValue));
Assert.True(state.CanQueueTeleportStart(0));
Assert.True(state.TryQueueTeleportStart(0));
Assert.True(state.HasPendingTeleportStart);
}
[Fact]
public void ExactHalfRangeUsesRetailLowerStampTieBreak()
{
var state = new RuntimeWorldTransitState();
Assert.True(state.TryQueueTeleportStart(0x8000));
Assert.True(state.ActivateQueuedTeleport());
state.EndTeleport();
Assert.True(state.CanQueueTeleportStart(0x0000));
Assert.True(state.TryQueueTeleportStart(0x0000));
}
[Fact]
public void PendingStartAcceptsEqualPositionUntilHostCanActivate()
{
var state = new RuntimeWorldTransitState();
Assert.True(state.TryQueueTeleportStart(11));
Assert.False(state.OfferTeleportDestination(
Destination(OutdoorCell, 11, x: 110f),
teleportTimestampAdvanced: false));
Assert.True(state.ActivateQueuedTeleport());
Assert.True(state.TryGetAcceptedTeleportDestination(
out RuntimeTeleportDestination buffered));
Assert.Equal(110f, buffered.Position.Frame.Origin.X);
Assert.True(state.IsTeleportActive);
}
[Fact]
public void AdvancingPositionPrunesSkippedDestination()
{
var state = new RuntimeWorldTransitState();
Assert.False(state.OfferTeleportDestination(
Destination(OutdoorCell, 11),
teleportTimestampAdvanced: true));
Assert.False(state.OfferTeleportDestination(
Destination(OtherCell, 12),
teleportTimestampAdvanced: true));
Assert.Equal(1, state.BufferedTeleportDestinationCount);
Assert.True(state.TryQueueTeleportStart(12));
Assert.True(state.ActivateQueuedTeleport());
Assert.True(state.TryGetAcceptedTeleportDestination(
out RuntimeTeleportDestination buffered));
Assert.Equal(OtherCell, buffered.CellId);
}
[Fact]
public void ObsoleteDestinationCannotSurviveSequenceWrap()
{
var state = new RuntimeWorldTransitState();
Assert.False(state.OfferTeleportDestination(
Destination(OutdoorCell, ushort.MaxValue),
teleportTimestampAdvanced: true));
Assert.False(state.OfferTeleportDestination(
Destination(OtherCell, 0),
teleportTimestampAdvanced: true));
Assert.True(state.TryQueueTeleportStart(0));
Assert.True(state.ActivateQueuedTeleport());
Assert.True(state.TryGetAcceptedTeleportDestination(
out RuntimeTeleportDestination wrapped));
Assert.Equal(OtherCell, wrapped.CellId);
state.EndTeleport();
Assert.True(state.TryQueueTeleportStart(0x7FFF));
Assert.True(state.ActivateQueuedTeleport());
state.EndTeleport();
Assert.True(state.TryQueueTeleportStart(0xFFFE));
Assert.True(state.ActivateQueuedTeleport());
state.EndTeleport();
Assert.True(state.TryQueueTeleportStart(ushort.MaxValue));
Assert.True(state.ActivateQueuedTeleport());
Assert.False(state.HasAcceptedTeleportDestination);
}
[Fact]
public void PortalRevealClaimsOnlyExactAcceptedSequenceAndCell()
{
var state = new RuntimeWorldTransitState();
Assert.True(state.TryQueueTeleportStart(7));
Assert.True(state.ActivateQueuedTeleport());
Assert.True(state.OfferTeleportDestination(
Destination(OutdoorCell, 7),
teleportTimestampAdvanced: true));
Assert.False(state.TryBeginPortalReveal(
8,
OutdoorCell,
out _));
Assert.False(state.TryBeginPortalReveal(
7,
OtherCell,
out _));
Assert.Equal(RuntimePortalSnapshot.Idle.Generation, state.Snapshot.Generation);
Assert.True(state.HasAcceptedTeleportDestination);
Assert.True(state.TryBeginPortalReveal(
7,
OutdoorCell,
out long generation));
Assert.True(generation > 0);
Assert.False(state.HasAcceptedTeleportDestination);
Assert.True(state.CanPlacePortalDestination(
generation,
7,
OutdoorCell));
Assert.False(state.CanPlacePortalDestination(
generation,
8,
OutdoorCell));
Assert.False(state.CanPlacePortalDestination(
generation,
7,
OtherCell));
}
[Fact]
public void MaterializationRequiresExactGenerationSequenceAndCell()
{
var state = new RuntimeWorldTransitState();
long generation = BeginPortal(state, OutdoorCell, sequence: 17);
Assert.True(state.AcknowledgeDestinationReadiness(
Ready(generation, OutdoorCell)));
Assert.False(state.AcknowledgePortalMaterialized(
generation + 1,
17,
OutdoorCell));
Assert.False(state.AcknowledgePortalMaterialized(
generation,
18,
OutdoorCell));
Assert.False(state.AcknowledgePortalMaterialized(
generation,
17,
OtherCell));
Assert.False(state.Snapshot.Materialized);
Assert.True(state.AcknowledgePortalMaterialized(
generation,
17,
OutdoorCell));
Assert.True(state.Snapshot.Materialized);
}
[Fact]
public void ResetSessionClearsCorrelationHistoryAndBufferedDestinations()
{
var state = new RuntimeWorldTransitState();
Assert.False(state.OfferTeleportDestination(
Destination(OutdoorCell, 7),
teleportTimestampAdvanced: true));
Assert.True(state.TryQueueTeleportStart(8));
state.ResetSession();
Assert.False(state.IsTeleportActive);
Assert.False(state.HasPendingTeleportStart);
Assert.False(state.HasAcceptedTeleportDestination);
Assert.Equal(0, state.BufferedTeleportDestinationCount);
Assert.True(state.TryQueueTeleportStart(8));
}
private static long BeginPortal(
RuntimeWorldTransitState state,
uint cell,
ushort sequence = 1)
{
Assert.True(state.TryQueueTeleportStart(sequence));
Assert.True(state.ActivateQueuedTeleport());
Assert.True(state.OfferTeleportDestination(
Destination(cell, sequence),
teleportTimestampAdvanced: true));
Assert.True(state.TryBeginPortalReveal(
sequence,
cell,
out long generation));
return generation;
}
private static RuntimeTeleportDestination Destination(
uint cell,
ushort sequence,
float x = 1f) =>
new(
EntityGuid: 0x50000001u,
InstanceSequence: 1,
PositionSequence: 1,
TeleportSequence: sequence,
ForcePositionSequence: 1,
Position: new Position(
cell,
new Vector3(x, 2f, 3f),
Quaternion.Identity));
private static RuntimeDestinationReadiness Ready(
long generation,
uint cell) =>