feat(runtime): commit dormant SetPosition activation

This commit is contained in:
Erik 2026-08-01 14:25:02 +02:00
parent 99f867f053
commit 5785a07b3e
13 changed files with 4674 additions and 111 deletions

View file

@ -11,6 +11,17 @@ internal enum RuntimeCollisionReportKind
EnvironmentCollision,
}
internal enum SetPositionCollisionBatchDispatchStatus : byte
{
RejectedReceipt,
Displaced,
Completed,
}
internal readonly record struct SetPositionCollisionBatchDispatchResult(
SetPositionCollisionBatchDispatchStatus Status,
bool Reported);
/// <summary>
/// Immutable presentation-free projection of one retail weenie collision
/// callback. Runtime commits the callback before an observer can re-enter.
@ -38,6 +49,7 @@ internal readonly record struct RuntimeCollisionReportingOwnershipSnapshot(
int PendingReportCount,
int LeavingOwnerCount,
int AdmissionBlockedOwnerCount,
int PendingSetPositionDispatchCount,
bool IsDispatching,
long DispatchFailureCount,
bool IsDisposed)
@ -51,6 +63,7 @@ internal readonly record struct RuntimeCollisionReportingOwnershipSnapshot(
&& PendingReportCount == 0
&& LeavingOwnerCount == 0
&& AdmissionBlockedOwnerCount == 0
&& PendingSetPositionDispatchCount == 0
&& !IsDispatching;
}
@ -78,6 +91,10 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
private ulong _nextSequence;
private ulong _dispatchEpoch = 1UL;
private long _dispatchFailureCount;
private ulong _mutationRevision;
private ulong _nextPreparedBatchId;
private ulong _lastInstalledBatchId;
private readonly HashSet<ulong> _pendingSetPositionDispatches = [];
private bool _dispatching;
private bool _disposed;
@ -102,6 +119,7 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
_pendingReports.Count,
_leaving.Count,
_admissionBlocked.Count,
_pendingSetPositionDispatches.Count,
_dispatching,
_dispatchFailureCount,
_disposed);
@ -125,6 +143,574 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
return new Subscription(this, observer);
}
internal enum StagedReportEligibility : byte
{
Environment,
Object,
}
private sealed record FrozenCollisionSubject(
uint LocalEntityId,
bool IsStatic,
RuntimeEntityRecord? Record,
PhysicsBody? Body,
RuntimeEntityKey Key);
internal sealed record StagedReportAction(
StagedReportEligibility Eligibility,
RuntimeEntityRecord Recipient,
PhysicsBody RecipientBody,
RuntimeEntityKey RecipientKey,
RuntimeEntityRecord? Other,
PhysicsBody? OtherBody,
RuntimeEntityKey? OtherKey,
bool RecipientContact,
bool ExactDormantRecipient,
ulong RecipientPositionAuthorityVersion);
internal sealed class PreparedSetPositionCollisionBatch
{
internal required ulong BatchId { get; init; }
internal required ulong ExpectedMutationRevision { get; init; }
internal required ulong InstalledMutationRevision { get; init; }
internal required ulong SessionLifetimeVersion { get; init; }
internal required RuntimeEntityRecord Owner { get; init; }
internal required PhysicsBody OwnerBody { get; init; }
internal required RuntimeEntityKey OwnerKey { get; init; }
internal required ulong OwnerPositionAuthorityVersion { get; init; }
internal required OwnerState? OwnerState { get; init; }
internal required bool PreviousContact { get; init; }
internal required bool FinalCollidedWithEnvironment { get; init; }
internal required bool FinalGroundEdge { get; init; }
internal required double PhysicsTime { get; init; }
internal required StagedReportAction[] Actions { get; init; }
}
internal readonly record struct SetPositionCollisionBatchReceipt(
ulong BatchId,
RuntimeEntityRecord Owner,
PhysicsBody OwnerBody,
RuntimeEntityKey OwnerKey,
ulong OwnerPositionAuthorityVersion,
bool PreviousContact,
bool FinalCollidedWithEnvironment,
bool FinalGroundEdge,
double PhysicsTime,
StagedReportAction[] Actions)
{
internal bool IsValid => BatchId != 0UL;
}
/// <summary>
/// Freezes retail COLLISIONINFO subjects and prepares the owner-table,
/// environment-latch, and callback subjects without mutating Runtime.
/// Dynamic tracking and reverse-index writes occur during ordered dispatch
/// after each subject's live behavior flags are revalidated. Callback
/// eligibility is evaluated after the dormant
/// frame/contact/ground prephase and before physical response, shadow
/// publication, and enter-world activation, matching retail SetPosition.
/// </summary>
internal bool TryPrepareSetPositionBatch(
RuntimeEntityRecord owner,
PhysicsBody ownerBody,
double physicsTime,
bool previousContact,
bool previousOnWalkable,
bool finalOnWalkable,
bool collidedWithEnvironment,
ImmutableArray<uint> collidedObjectIds,
out PreparedSetPositionCollisionBatch? prepared)
{
EnsureNotDisposed();
ArgumentNullException.ThrowIfNull(owner);
ArgumentNullException.ThrowIfNull(ownerBody);
prepared = null;
RuntimeEntityKey ownerKey = owner.Key ?? default;
if (!double.IsFinite(physicsTime)
|| ownerKey == default
|| !IsKnownParticipant(owner, ownerBody, ownerKey)
|| _leaving.Contains(ownerKey)
|| _admissionBlocked.Contains(ownerKey))
{
return false;
}
ulong expectedMutation = _mutationRevision;
ulong installedMutation = checked(expectedMutation + 1UL);
ulong sessionLifetime = _entities.SessionLifetimeVersion;
if (collidedObjectIds.IsDefault)
collidedObjectIds = ImmutableArray<uint>.Empty;
var subjects = new List<FrozenCollisionSubject>(
collidedObjectIds.Length);
for (int index = 0; index < collidedObjectIds.Length; index++)
{
uint localId = collidedObjectIds[index];
if (localId == 0u || localId == ownerKey.LocalEntityId)
continue;
if (!_shadows.TryGetCollisionOwner(
localId,
out uint shadowState,
out bool isStatic))
{
continue;
}
if (isStatic)
{
subjects.Add(new FrozenCollisionSubject(
localId,
IsStatic: true,
Record: null,
Body: null,
Key: default));
continue;
}
if (!_entities.TryGetByLocalId(localId, out RuntimeEntityRecord target)
|| target.PhysicsBody is not { } targetBody
|| !_entities.IsCurrent(target)
|| target.Key is not { } targetKey)
{
continue;
}
subjects.Add(new FrozenCollisionSubject(
localId,
IsStatic: false,
target,
targetBody,
targetKey));
_ = shadowState;
}
OwnerState staged = CloneOwnerState(TryGetOwner(ownerKey));
var actions = new List<StagedReportAction>(subjects.Count);
void StageEnvironment()
{
actions.Add(new StagedReportAction(
StagedReportEligibility.Environment,
owner,
ownerBody,
ownerKey,
Other: null,
OtherBody: null,
OtherKey: null,
RecipientContact: previousContact,
ExactDormantRecipient: !ownerBody.InWorld,
RecipientPositionAuthorityVersion:
owner.PositionAuthorityVersion));
}
for (int index = 0; index < subjects.Count; index++)
{
FrozenCollisionSubject subject = subjects[index];
if (subject.IsStatic)
{
StageEnvironment();
continue;
}
RuntimeEntityRecord target = subject.Record!;
PhysicsBody targetBody = subject.Body!;
RuntimeEntityKey targetKey = subject.Key;
// Dynamic classification and behavior flags are evaluated after
// the ground edge. Retain the old record unchanged in the
// installed batch; the ordered tracking action below performs
// retail's timestamp/Ethereal clobber or Static conversion.
actions.Add(new StagedReportAction(
StagedReportEligibility.Object,
owner,
ownerBody,
ownerKey,
target,
targetBody,
targetKey,
RecipientContact: previousContact,
ExactDormantRecipient: !ownerBody.InWorld,
RecipientPositionAuthorityVersion:
owner.PositionAuthorityVersion));
}
_owners.EnsureCapacity(_owners.Count + 1);
_pendingSetPositionDispatches.EnsureCapacity(
_pendingSetPositionDispatches.Count + 1);
prepared = new PreparedSetPositionCollisionBatch
{
BatchId = checked(++_nextPreparedBatchId),
ExpectedMutationRevision = expectedMutation,
InstalledMutationRevision = installedMutation,
SessionLifetimeVersion = sessionLifetime,
Owner = owner,
OwnerBody = ownerBody,
OwnerKey = ownerKey,
OwnerPositionAuthorityVersion = owner.PositionAuthorityVersion,
OwnerState = staged.Records.Count == 0
&& !staged.CollidingWithEnvironment
? null
: staged,
PreviousContact = previousContact,
FinalCollidedWithEnvironment = collidedWithEnvironment,
FinalGroundEdge = !previousOnWalkable && finalOnWalkable,
PhysicsTime = physicsTime,
Actions = actions.ToArray(),
};
_ = previousContact;
return _mutationRevision == expectedMutation
&& _entities.SessionLifetimeVersion == sessionLifetime
&& IsKnownParticipant(owner, ownerBody, ownerKey);
}
internal bool TryInstallSetPositionBatch(
PreparedSetPositionCollisionBatch prepared,
out SetPositionCollisionBatchReceipt receipt)
{
EnsureNotDisposed();
ArgumentNullException.ThrowIfNull(prepared);
receipt = default;
if (prepared.BatchId <= _lastInstalledBatchId
|| _mutationRevision != prepared.ExpectedMutationRevision
|| _entities.SessionLifetimeVersion
!= prepared.SessionLifetimeVersion
|| !IsKnownParticipant(
prepared.Owner,
prepared.OwnerBody,
prepared.OwnerKey))
{
return false;
}
if (!IsKnownParticipant(
prepared.Owner,
prepared.OwnerBody,
prepared.OwnerKey))
return false;
if (prepared.OwnerState is null)
_owners.Remove(prepared.OwnerKey);
else
{
prepared.OwnerState.SetPositionBatchId = prepared.BatchId;
_owners[prepared.OwnerKey] = prepared.OwnerState;
}
_mutationRevision = prepared.InstalledMutationRevision;
_lastInstalledBatchId = prepared.BatchId;
_pendingSetPositionDispatches.Add(prepared.BatchId);
receipt = new SetPositionCollisionBatchReceipt(
prepared.BatchId,
prepared.Owner,
prepared.OwnerBody,
prepared.OwnerKey,
prepared.OwnerPositionAuthorityVersion,
prepared.PreviousContact,
prepared.FinalCollidedWithEnvironment,
prepared.FinalGroundEdge,
prepared.PhysicsTime,
prepared.Actions);
return true;
}
internal bool IsPreparedSetPositionBatchCurrent(
PreparedSetPositionCollisionBatch prepared)
{
ArgumentNullException.ThrowIfNull(prepared);
return !_disposed
&& prepared.BatchId > _lastInstalledBatchId
&& _mutationRevision == prepared.ExpectedMutationRevision
&& _entities.SessionLifetimeVersion
== prepared.SessionLifetimeVersion
&& IsKnownParticipant(
prepared.Owner,
prepared.OwnerBody,
prepared.OwnerKey);
}
internal bool DispatchSetPositionBatch(
in SetPositionCollisionBatchReceipt receipt) =>
DispatchSetPositionBatchResult(receipt).Reported;
internal SetPositionCollisionBatchDispatchResult
DispatchSetPositionBatchResult(
in SetPositionCollisionBatchReceipt receipt)
{
if (!receipt.IsValid
|| _disposed
|| receipt.BatchId > _lastInstalledBatchId
|| !_pendingSetPositionDispatches.Remove(receipt.BatchId))
{
return new(
SetPositionCollisionBatchDispatchStatus.RejectedReceipt,
Reported: false);
}
bool reported = false;
for (int index = 0; index < receipt.Actions.Length; index++)
{
if (receipt.Owner.PositionAuthorityVersion
!= receipt.OwnerPositionAuthorityVersion
|| _owners.TryGetValue(
receipt.OwnerKey, out OwnerState? currentOwner)
&& currentOwner.SetPositionBatchId != receipt.BatchId)
{
return new(
SetPositionCollisionBatchDispatchStatus.Displaced,
reported);
}
StagedReportAction action = receipt.Actions[index];
if (action.Eligibility is StagedReportEligibility.Environment)
{
reported |= DispatchEnvironmentAction(
action.Recipient,
action.RecipientBody,
action.RecipientKey,
action.RecipientContact,
receipt.BatchId);
continue;
}
reported |= DispatchTrackingAction(
action, receipt.PhysicsTime, receipt.BatchId);
}
if (receipt.Owner.PositionAuthorityVersion
!= receipt.OwnerPositionAuthorityVersion
|| _owners.TryGetValue(
receipt.OwnerKey, out OwnerState? suffixOwner)
&& suffixOwner.SetPositionBatchId != receipt.BatchId)
{
return new(
SetPositionCollisionBatchDispatchStatus.Displaced,
reported);
}
// Retail chooses the expired set only after every current collision
// has either refreshed its live Ethereal bit/timestamp or converted
// to environment. EndExpiredObjectCollisions predeletes the complete
// selected suffix before its first callback.
EndExpiredObjectCollisions(
receipt.Owner,
receipt.OwnerBody,
receipt.OwnerKey,
receipt.PhysicsTime,
force: false,
receipt.BatchId,
receipt.OwnerPositionAuthorityVersion);
if (!IsSetPositionBatchOwnerCurrent(receipt))
{
return new(
SetPositionCollisionBatchDispatchStatus.Displaced,
reported);
}
reported |= DispatchEnvironmentSuffix(receipt);
return new(
SetPositionCollisionBatchDispatchStatus.Completed,
reported);
}
internal bool DiscardSetPositionBatch(
in SetPositionCollisionBatchReceipt receipt) =>
receipt.IsValid
&& _pendingSetPositionDispatches.Remove(receipt.BatchId);
internal void RetireSetPositionBatchOwner(
in SetPositionCollisionBatchReceipt receipt)
{
if (!receipt.IsValid || _disposed)
return;
_pendingSetPositionDispatches.Remove(receipt.BatchId);
if (!IsKnownParticipant(
receipt.Owner,
receipt.OwnerBody,
receipt.OwnerKey)
|| !_owners.TryGetValue(
receipt.OwnerKey, out OwnerState? owner)
|| owner.SetPositionBatchId != receipt.BatchId)
{
return;
}
_mutationRevision = checked(_mutationRevision + 1UL);
if (!_admissionBlocked.Add(receipt.OwnerKey))
return;
try
{
ForceEnd(receipt.Owner, receipt.OwnerKey);
if (_owners.TryGetValue(receipt.OwnerKey, out OwnerState? current)
&& ReferenceEquals(current, owner)
&& current.SetPositionBatchId == receipt.BatchId)
{
current.CollidingWithEnvironment = false;
_owners.Remove(receipt.OwnerKey);
}
}
finally
{
_admissionBlocked.Remove(receipt.OwnerKey);
}
}
private bool DispatchTrackingAction(
StagedReportAction action,
double physicsTime,
ulong batchId)
{
if (action.Other is null
|| action.OtherBody is null
|| action.OtherKey is not { } targetKey
|| !IsKnownParticipant(
action.Recipient,
action.RecipientBody,
action.RecipientKey)
|| !IsKnownParticipant(
action.Other,
action.OtherBody,
targetKey))
{
return false;
}
PhysicsStateFlags targetState = action.OtherBody.State;
if ((targetState & PhysicsStateFlags.Static) != 0)
{
// track_object_collision is skipped entirely on this path. Any
// older record therefore retains its old timestamp and remains
// eligible for the immediately-following expiry pass.
return DispatchEnvironmentAction(
action.Recipient,
action.RecipientBody,
action.RecipientKey,
action.RecipientContact,
batchId);
}
OwnerState state = GetOrCreateOwner(action.RecipientKey, batchId);
bool isNew = !state.Records.ContainsKey(targetKey);
state.Records[targetKey] = new CollisionRecord(
physicsTime,
(targetState & PhysicsStateFlags.Ethereal) != 0,
action.Other.ServerGuid);
if (!isNew)
{
_mutationRevision = checked(_mutationRevision + 1UL);
return false;
}
state.Order.Add(targetKey);
AddReverseOwner(targetKey, action.RecipientKey);
_mutationRevision = checked(_mutationRevision + 1UL);
return ReportObject(
action.Recipient,
action.RecipientBody,
action.RecipientKey,
action.Other,
action.OtherBody,
targetKey,
targetState,
action.RecipientContact,
action.ExactDormantRecipient,
action.RecipientPositionAuthorityVersion,
batchId);
}
private bool DispatchEnvironmentAction(
RuntimeEntityRecord owner,
PhysicsBody ownerBody,
RuntimeEntityKey ownerKey,
bool previousContact,
ulong batchId)
{
if (!IsKnownParticipant(owner, ownerBody, ownerKey))
return false;
OwnerState state = GetOrCreateOwner(ownerKey, batchId);
if (state.CollidingWithEnvironment)
return false;
state.CollidingWithEnvironment = true;
_mutationRevision = checked(_mutationRevision + 1UL);
bool reported = (ownerBody.State
& PhysicsStateFlags.ReportCollisions) != 0;
if (reported)
{
Publish(new RuntimeCollisionReport(
NextSequence(),
RuntimeCollisionReportKind.EnvironmentCollision,
ownerKey,
owner.ServerGuid,
Other: null,
OtherServerGuid: null,
previousContact,
OtherWasInContact: false));
}
// Environment collision tests Missile after the callback returns.
StopMissileForStagedOwner(
owner,
ownerBody,
ownerKey,
requireCurrentMissile: true);
return reported;
}
private bool DispatchEnvironmentSuffix(
in SetPositionCollisionBatchReceipt receipt)
{
if (!IsSetPositionBatchOwnerCurrent(receipt)
|| !IsKnownParticipant(
receipt.Owner,
receipt.OwnerBody,
receipt.OwnerKey))
{
return false;
}
OwnerState? state = TryGetOwner(receipt.OwnerKey);
if (state?.CollidingWithEnvironment == true)
{
if (state.CollidingWithEnvironment
!= receipt.FinalCollidedWithEnvironment)
{
state.CollidingWithEnvironment =
receipt.FinalCollidedWithEnvironment;
_mutationRevision = checked(_mutationRevision + 1UL);
}
}
else if (receipt.FinalCollidedWithEnvironment
|| receipt.FinalGroundEdge)
{
bool reported = DispatchEnvironmentAction(
receipt.Owner,
receipt.OwnerBody,
receipt.OwnerKey,
receipt.PreviousContact,
receipt.BatchId);
TrimEmptyOwner(receipt.OwnerKey);
return reported;
}
TrimEmptyOwner(receipt.OwnerKey);
return false;
}
private void StopMissileForStagedOwner(
RuntimeEntityRecord owner,
PhysicsBody ownerBody,
RuntimeEntityKey ownerKey,
bool requireCurrentMissile)
{
if (!IsKnownParticipant(owner, ownerBody, ownerKey)
|| !_entities.StopMissileAfterCollision(
owner,
requireCurrentMissile))
{
return;
}
_shadows.UpdatePhysicsState(
ownerKey.LocalEntityId,
(uint)owner.FinalPhysicsState);
}
private static OwnerState CloneOwnerState(OwnerState? source)
{
var clone = new OwnerState();
if (source is null)
return clone;
foreach ((RuntimeEntityKey key, CollisionRecord record)
in source.Records)
clone.Records.Add(key, record);
clone.Order.AddRange(source.Order);
clone.CollidingWithEnvironment = source.CollidingWithEnvironment;
return clone;
}
/// <summary>
/// Ports the reporting/tracking portion of retail
/// <c>CPhysicsObj::handle_all_collisions</c> (0x00514780). The return is
@ -148,6 +734,7 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
{
return false;
}
_mutationRevision = checked(_mutationRevision + 1UL);
bool reported = false;
if (collidedObjectIds.IsDefault)
@ -266,6 +853,7 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
ArgumentNullException.ThrowIfNull(record);
if (record.Key is not { } key)
return;
_mutationRevision = checked(_mutationRevision + 1UL);
if (!_admissionBlocked.Add(key))
return;
try
@ -288,6 +876,7 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
{
EnsureNotDisposed();
ArgumentNullException.ThrowIfNull(records);
_mutationRevision = checked(_mutationRevision + 1UL);
var blocked = new List<(RuntimeEntityRecord Record, RuntimeEntityKey Key)>(
records.Count);
for (int index = 0; index < records.Count; index++)
@ -322,6 +911,7 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
ArgumentNullException.ThrowIfNull(record);
if (record.Key is not { } key)
return;
_mutationRevision = checked(_mutationRevision + 1UL);
// A session-clear batch blocks every owner before publishing the
// first force-end callback. A callback may synchronously accept the
// deletion of a later, already-blocked owner. That owner must still
@ -342,11 +932,13 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
internal void ResetSession()
{
EnsureNotDisposed();
_mutationRevision = checked(_mutationRevision + 1UL);
_owners.Clear();
_ownersByPeer.Clear();
_pendingReports.Clear();
_leaving.Clear();
_admissionBlocked.Clear();
_pendingSetPositionDispatches.Clear();
_dispatchEpoch = checked(_dispatchEpoch + 1UL);
}
@ -359,6 +951,7 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
_pendingReports.Clear();
_leaving.Clear();
_admissionBlocked.Clear();
_pendingSetPositionDispatches.Clear();
_observers = [];
_dispatchEpoch = checked(_dispatchEpoch + 1UL);
_disposed = true;
@ -372,7 +965,10 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
PhysicsBody targetBody,
RuntimeEntityKey targetKey,
PhysicsStateFlags targetState,
bool previousContact)
bool previousContact,
bool exactDormantOwner = false,
ulong expectedOwnerPositionAuthorityVersion = 0UL,
ulong setPositionBatchId = 0UL)
{
if ((targetState & PhysicsStateFlags.ReportAsEnvironment) != 0)
{
@ -380,7 +976,8 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
owner,
ownerBody,
ownerKey,
previousContact);
previousContact,
setPositionBatchId);
}
PhysicsStateFlags ownerState = ownerBody.State;
@ -416,7 +1013,13 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
// reentrant state update can therefore suppress this second report.
bool targetReported = IsCurrentParticipant(target, targetBody, targetKey)
&& (targetBody.State & PhysicsStateFlags.ReportCollisions) != 0
&& IsCurrentParticipant(owner, ownerBody, ownerKey)
&& (IsCurrentParticipant(owner, ownerBody, ownerKey)
|| exactDormantOwner
&& IsExactDormantParticipant(
owner,
ownerBody,
ownerKey,
expectedOwnerPositionAuthorityVersion))
&& (ownerBody.State & PhysicsStateFlags.IgnoreCollisions) == 0;
if (targetReported)
{
@ -433,13 +1036,31 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
return ownerReported || targetReported;
}
private bool IsExactDormantParticipant(
RuntimeEntityRecord record,
PhysicsBody body,
RuntimeEntityKey key,
ulong expectedPositionAuthorityVersion) =>
!body.InWorld
&& (body.TransientState & TransientStateFlags.Active) == 0
&& (body.State & PhysicsStateFlags.Hidden) == 0
&& _entities.IsCurrent(record)
&& !_leaving.Contains(key)
&& !_admissionBlocked.Contains(key)
&& expectedPositionAuthorityVersion != 0UL
&& record.PositionAuthorityVersion
== expectedPositionAuthorityVersion
&& IsKnownParticipant(record, body, key);
private bool ReportEnvironment(
RuntimeEntityRecord owner,
PhysicsBody ownerBody,
RuntimeEntityKey ownerKey,
bool previousContact)
bool previousContact,
ulong setPositionBatchId = 0UL)
{
OwnerState state = GetOrCreateOwner(ownerKey);
OwnerState state = GetOrCreateOwner(
ownerKey, setPositionBatchId);
if (state.CollidingWithEnvironment)
return false;
@ -467,7 +1088,9 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
PhysicsBody? ownerBody,
RuntimeEntityKey ownerKey,
double physicsTime,
bool force)
bool force,
ulong setPositionBatchId = 0UL,
ulong expectedPositionAuthorityVersion = 0UL)
{
if (!_owners.TryGetValue(ownerKey, out OwnerState? state)
|| state.Records.Count == 0)
@ -524,7 +1147,15 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
|| reportEpoch != _dispatchEpoch
|| _entities.SessionLifetimeVersion != sourceSessionVersion
|| _entities.CurrentLifetimeMutation(owner.ServerGuid)
!= sourceLifetimeMutation)
!= sourceLifetimeMutation
|| setPositionBatchId != 0UL
&& (owner.PositionAuthorityVersion
!= expectedPositionAuthorityVersion
|| !_owners.TryGetValue(
ownerKey, out OwnerState? currentOwner)
|| !ReferenceEquals(currentOwner, state)
|| currentOwner.SetPositionBatchId
!= setPositionBatchId))
{
break;
}
@ -563,6 +1194,13 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
TrimEmptyOwner(ownerKey);
}
private bool IsSetPositionBatchOwnerCurrent(
in SetPositionCollisionBatchReceipt receipt) =>
receipt.Owner.PositionAuthorityVersion
== receipt.OwnerPositionAuthorityVersion
&& (!_owners.TryGetValue(receipt.OwnerKey, out OwnerState? owner)
|| owner.SetPositionBatchId == receipt.BatchId);
private void PublishResolvedObjectEnd(
RuntimeEntityRecord owner,
PhysicsBody? ownerBody,
@ -654,13 +1292,16 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
(uint)owner.FinalPhysicsState);
}
private OwnerState GetOrCreateOwner(RuntimeEntityKey key)
private OwnerState GetOrCreateOwner(
RuntimeEntityKey key,
ulong setPositionBatchId = 0UL)
{
if (!_owners.TryGetValue(key, out OwnerState? owner))
{
owner = new OwnerState();
_owners.Add(key, owner);
}
owner.SetPositionBatchId = setPositionBatchId;
return owner;
}
@ -874,15 +1515,16 @@ internal sealed class RuntimeCollisionReportingState : IDisposable
private void EnsureNotDisposed() =>
ObjectDisposedException.ThrowIf(_disposed, this);
private sealed class OwnerState
internal sealed class OwnerState
{
internal Dictionary<RuntimeEntityKey, CollisionRecord> Records { get; }
= new();
internal List<RuntimeEntityKey> Order { get; } = [];
internal bool CollidingWithEnvironment { get; set; }
internal ulong SetPositionBatchId { get; set; }
}
private readonly record struct CollisionRecord(
internal readonly record struct CollisionRecord(
double TouchedTime,
bool Ethereal,
uint ServerGuid);

View file

@ -32,6 +32,8 @@ public readonly record struct RuntimePhysicsOwnershipSnapshot(
int PendingCollisionReportCount,
int LeavingCollisionReportOwnerCount,
int CollisionReportAdmissionBlockedOwnerCount,
int PendingCollisionSetPositionDispatchCount,
int PendingShadowSetPositionDispatchCount,
bool IsCollisionReportDispatching,
int CollisionAdmissionCount,
int CollisionGenerationCount,
@ -66,6 +68,8 @@ public readonly record struct RuntimePhysicsOwnershipSnapshot(
&& PendingCollisionReportCount == 0
&& LeavingCollisionReportOwnerCount == 0
&& CollisionReportAdmissionBlockedOwnerCount == 0
&& PendingCollisionSetPositionDispatchCount == 0
&& PendingShadowSetPositionDispatchCount == 0
&& !IsCollisionReportDispatching
&& CollisionAdmissionCount == 0
&& CollisionGenerationCount == 0
@ -1162,6 +1166,8 @@ public sealed class RuntimePhysicsState : IDisposable
collisionReports.PendingReportCount,
collisionReports.LeavingOwnerCount,
collisionReports.AdmissionBlockedOwnerCount,
collisionReports.PendingSetPositionDispatchCount,
Engine.ShadowObjects.PendingSetPositionDispatchCount,
collisionReports.IsDispatching,
_collisionAdmissions.Count,
_collisionGenerations.Count,
@ -2529,6 +2535,18 @@ public sealed class RuntimePhysicsState : IDisposable
TrimCollisionOwnerJournal();
}
internal bool TryPrepareSpatialRootAdmission(RuntimeEntityRecord record)
{
EnsureNotDisposed();
ArgumentNullException.ThrowIfNull(record);
if (record.Key is null || !Entities.IsCurrent(record))
return false;
_spatialRoots.EnsureCapacity(_spatialRoots.Count + 1);
_spatialRemotes.EnsureCapacity(_spatialRemotes.Count + 1);
_spatialProjectiles.EnsureCapacity(_spatialProjectiles.Count + 1);
return Entities.IsCurrent(record);
}
internal ulong ExpectedCollisionGeneration(uint exactCellId)
{
uint landblockId = CanonicalLandblock(exactCellId);
@ -2671,6 +2689,32 @@ public sealed class RuntimePhysicsState : IDisposable
return true;
}
internal bool IsCollisionEvaluationFatalAuthorityCurrent(
in RuntimeCollisionEvaluationAuthority authority)
{
if (!authority.IsValid
|| _collisionWorldAuthority != authority.CollisionWorldAuthority
|| !ReferenceEquals(ObjectTable, authority.ObjectTable)
|| ObjectTableBindingAuthority
!= authority.ObjectTableBindingAuthority
|| ObjectTableAuthority != authority.ObjectTableAuthority)
{
return false;
}
foreach (RuntimeCollisionGenerationAuthority generation
in authority.Generations)
{
if (generation.LandblockId == 0u
|| _collisionAdmissions.ContainsKey(generation.LandblockId)
|| CollisionGenerationAuthority(generation.LandblockId)
!= generation.Generation)
{
return false;
}
}
return true;
}
internal bool HandleSetPositionCollisions(
RuntimeEntityRecord record,
ulong positionAuthorityVersion,

View file

@ -4,6 +4,7 @@ using AcDream.Core.Items;
using AcDream.Core.Net.Messages;
using AcDream.Core.Physics;
using AcDream.Runtime.Entities;
using AcDream.Runtime.Gameplay;
namespace AcDream.Runtime.Physics;
@ -29,6 +30,7 @@ internal enum RuntimeEntityPlacementStage
AwaitingPreparation,
AwaitingWithdrawalAcknowledgement,
AwaitingCell,
AwaitingFinalShadowPreparation,
AwaitingCommitAcknowledgement,
CancelledAwaitingAcknowledgement,
}
@ -109,6 +111,65 @@ internal readonly record struct RuntimeDormantSetPositionEvaluation(
&& CollisionAuthority.IsValid;
}
internal enum RuntimeDormantSetPositionCommitStatus : byte
{
None,
AwaitingFinalShadowPreparation,
Committed,
DeferredCell,
RejectedPlacement,
RejectedAuthority,
}
internal sealed class PreparedDormantSetPositionCommit
{
internal required RuntimeDormantSetPositionEvaluation Evaluation
{ get; init; }
internal required RuntimeEntityKey Entity { get; init; }
internal required ulong OperationId { get; init; }
internal required ulong ExpectedProjectionSequence { get; init; }
internal required ShadowObjectRegistry.PreparedSetPositionShadowCommit?
Shadow { get; init; }
internal required RuntimeCollisionReportingState
.PreparedSetPositionCollisionBatch? Collision { get; init; }
internal required RuntimePlacementProjectionSnapshot Projection
{ get; init; }
internal required SortedDictionary<ulong, RuntimePlacementProjectionSnapshot>?
PendingProjection { get; init; }
internal required ulong DeferredCollisionGeneration { get; init; }
internal required List<RuntimeEntityKey>? DeferredBucket { get; init; }
internal required bool DeferredBucketIsNew { get; init; }
}
internal sealed class PreparedDormantActivationFinalCommit
{
internal required RuntimeEntityKey Entity { get; init; }
internal required ulong OperationId { get; init; }
internal required ulong ExpectedProjectionSequence { get; init; }
internal required ShadowObjectRegistry.PreparedSetPositionShadowCommit
Shadow { get; init; }
internal required RuntimePlacementProjectionSnapshot Projection
{ get; init; }
internal required SortedDictionary<ulong, RuntimePlacementProjectionSnapshot>
PendingProjection { get; init; }
}
internal readonly record struct RuntimeDormantSetPositionCommitReceipt(
RuntimeDormantSetPositionCommitStatus Status,
RuntimeEntityKey Entity,
ulong OperationId,
RuntimePlacementProjectionSnapshot Projection,
RuntimeCollisionReportingState.SetPositionCollisionBatchReceipt Collision,
ShadowObjectRegistry.SetPositionShadowCommitReceipt Shadow,
RuntimeCollisionEvaluationAuthority CollisionAuthority,
ulong SourceVectorAuthorityVersion,
bool HitGround,
bool LeaveGround)
{
internal bool IsCommitted => Status
is RuntimeDormantSetPositionCommitStatus.Committed;
}
public readonly record struct RuntimePlacementProjectionToken(
ulong Sequence,
ulong Revision,
@ -258,6 +319,7 @@ internal sealed class RuntimeSetPositionState : IDisposable
internal List<RuntimeEntityKey>? LostFamilyKeys { get; set; }
internal bool InheritedLostDeadline { get; set; }
internal bool EnteringWorldFromCelllessResidence { get; set; }
internal bool DormantLocalActivation { get; set; }
internal RuntimeSetPositionCommand? PreparedCommandAwaitingWithdrawalAck
{
get;
@ -289,7 +351,7 @@ internal sealed class RuntimeSetPositionState : IDisposable
private readonly Dictionary<RuntimeEntityKey, Operation> _operations = [];
private readonly Dictionary<CellGenerationKey, List<RuntimeEntityKey>>
_deferredByCellGeneration = [];
private readonly SortedDictionary<ulong, RuntimePlacementProjectionSnapshot>
private SortedDictionary<ulong, RuntimePlacementProjectionSnapshot>
_pendingProjection = [];
private readonly List<CellGenerationKey> _deferredBucketOrder = [];
private readonly Dictionary<uint, List<RuntimeEntityKey>>
@ -558,8 +620,12 @@ internal sealed class RuntimeSetPositionState : IDisposable
if (!token.IsValid
|| !_operations.TryGetValue(token.Entity, out Operation? operation)
|| operation.Token != token
|| operation.Stage
is not RuntimeEntityPlacementStage.AwaitingPreparation
|| operation.Stage is not (
RuntimeEntityPlacementStage.AwaitingPreparation
or RuntimeEntityPlacementStage.AwaitingCell)
|| operation.Stage is RuntimeEntityPlacementStage.AwaitingCell
&& (!operation.DormantLocalActivation
|| !operation.WakeableLostCell)
|| !IsCurrent(operation)
|| !_moverPreparationAuthorities.TryGetValue(
token.Entity,
@ -641,6 +707,20 @@ internal sealed class RuntimeSetPositionState : IDisposable
ArgumentNullException.ThrowIfNull(record);
ArgumentNullException.ThrowIfNull(body);
evaluation = default;
if (!IsExactDormantLocalActivationCurrent(
record,
body,
token,
command,
out _)
&& !TryRearmDeferredDormantLocalActivation(
record,
body,
token,
command))
{
return false;
}
if (!IsExactDormantLocalActivationCurrent(
record,
body,
@ -716,6 +796,779 @@ internal sealed class RuntimeSetPositionState : IDisposable
&& _physics.IsCollisionEvaluationAuthorityCurrent(
evaluation.CollisionAuthority);
internal bool IsDormantLocalActivationLeaseCurrent(
RuntimeEntityRecord record,
PhysicsBody body,
in RuntimeEntityPlacementToken token,
in RuntimeSetPositionCommand command) =>
IsExactDormantLocalActivationCurrent(
record,
body,
token,
command,
out _,
allowDeferredLease: true);
internal bool IsDormantLocalActivationAwaitingCell(
RuntimeEntityRecord record,
PhysicsBody body,
in RuntimeEntityPlacementToken token,
in RuntimeSetPositionCommand command)
{
return IsExactDormantLocalActivationCurrent(
record,
body,
token,
command,
out Operation? operation,
allowDeferredLease: true)
&& operation is not null
&& operation.Stage is RuntimeEntityPlacementStage.AwaitingCell
&& operation.DormantLocalActivation
&& operation.WakeableLostCell;
}
private bool TryRearmDeferredDormantLocalActivation(
RuntimeEntityRecord record,
PhysicsBody body,
in RuntimeEntityPlacementToken token,
in RuntimeSetPositionCommand command)
{
if (!IsExactDormantLocalActivationCurrent(
record,
body,
token,
command,
out Operation? operation,
allowDeferredLease: true)
|| operation is null
|| operation.Stage is not RuntimeEntityPlacementStage.AwaitingCell
|| !operation.DormantLocalActivation
|| !operation.WakeableLostCell
|| !operation.CollisionGenerationReady
|| operation.ProjectionSequence != 0UL
|| operation.CollisionGeneration != _physics
.ExpectedCollisionGeneration(operation.ExactCellId)
|| !_physics.Engine.IsSpawnCellReady(operation.ExactCellId))
{
return false;
}
UnindexDeferred(operation);
operation.WakeableLostCell = false;
operation.CollisionGenerationReady = false;
operation.Stage = RuntimeEntityPlacementStage.AwaitingPreparation;
return true;
}
internal bool TryPrepareDormantLocalActivationCommit(
RuntimeEntityRecord record,
PhysicsBody body,
in RuntimeDormantSetPositionEvaluation evaluation,
bool provenShapeless,
out PreparedDormantSetPositionCommit? prepared)
{
EnsureNotDisposed();
ArgumentNullException.ThrowIfNull(record);
ArgumentNullException.ThrowIfNull(body);
prepared = null;
if (!IsDormantLocalEvaluationCurrent(record, body, evaluation)
|| !IsExactDormantLocalActivationCurrent(
record,
body,
evaluation.Placement,
evaluation.Command,
out Operation? operation,
allowCanonicalCommand: true)
|| operation is null)
{
return false;
}
PhysicsSetPositionResult result = evaluation.Result;
ShadowObjectRegistry.PreparedSetPositionShadowCommit? shadow = null;
RuntimeCollisionReportingState.PreparedSetPositionCollisionBatch?
collision = null;
RuntimePlacementProjectionSnapshot projection = default;
SortedDictionary<ulong, RuntimePlacementProjectionSnapshot>?
pendingProjection = null;
ulong deferredCollisionGeneration = 0UL;
List<RuntimeEntityKey>? deferredBucket = null;
bool deferredBucketIsNew = false;
if (!result.IsDeferred)
{
if (!_physics.CollisionReports.TryPrepareSetPositionBatch(
record,
body,
evaluation.Command.GameTime,
result.IsCommitted && operation.PreviousContact,
result.IsCommitted && operation.PreviousOnWalkable,
result.IsCommitted && result.OnWalkable,
result.CollidedWithEnvironment,
result.CollidedObjectIds,
out collision)
|| collision is null)
{
return false;
}
}
if (result.IsDeferred)
{
deferredCollisionGeneration = _physics
.ExpectedCollisionGeneration(result.CellId);
_preparedMovers.EnsureCapacity(_preparedMovers.Count + 1);
if (result.CellId != 0u && deferredCollisionGeneration != 0UL)
{
var bucketKey = new CellGenerationKey(
result.CellId,
deferredCollisionGeneration);
if (_deferredByCellGeneration.TryGetValue(
bucketKey,
out deferredBucket))
{
deferredBucket.EnsureCapacity(deferredBucket.Count + 1);
}
else
{
deferredBucket = [operation.Key];
deferredBucketIsNew = true;
_deferredByCellGeneration.EnsureCapacity(
_deferredByCellGeneration.Count + 1);
_deferredBucketOrder.EnsureCapacity(
_deferredBucketOrder.Count + 1);
}
}
if (!_physics.Engine.ShadowObjects.TryPrepareSetPosition(
operation.Key.LocalEntityId,
result.Position,
result.Orientation,
result.CellId,
evaluation.Command.ShadowWorldOffsetX,
evaluation.Command.ShadowWorldOffsetY,
PhysicsShadowCommitAction.Preserve,
ImmutableArray<uint>.Empty,
provenShapeless,
suspendOwner: true,
out shadow)
|| shadow is null)
{
return false;
}
}
ulong expectedProjectionSequence = _nextProjectionSequence;
if (result.IsCommitted)
{
_ = checked(record.ObjectClockEpoch + 1UL);
_ = checked(record.PlacementCommitVersion + 1UL);
if (record.FullCellId != result.CellId)
_ = checked(record.SpatialAuthorityVersion + 1UL);
if (!_physics.TryPrepareSpatialRootAdmission(record))
return false;
ulong sequence = checked(expectedProjectionSequence + 1UL);
ulong spatial = record.SpatialAuthorityVersion
+ (record.FullCellId == result.CellId ? 0UL : 1UL);
ulong placement = checked(record.PlacementCommitVersion + 1UL);
var token = new RuntimePlacementProjectionToken(
sequence,
Revision: 1UL,
operation.Key,
operation.PositionAuthorityVersion,
spatial,
placement,
operation.SessionLifetimeVersion,
result.CellId,
operation.CollisionGeneration,
evaluation.Command.Portal);
projection = new RuntimePlacementProjectionSnapshot(
token,
RuntimePlacementProjectionKind.Place,
result.Position,
result.Orientation,
result.CellLocalPosition,
result.InContact,
result.OnWalkable);
pendingProjection = new SortedDictionary<
ulong,
RuntimePlacementProjectionSnapshot>(_pendingProjection)
{
[sequence] = projection,
};
}
prepared = new PreparedDormantSetPositionCommit
{
Evaluation = evaluation,
Entity = operation.Key,
OperationId = operation.Token.OperationId,
ExpectedProjectionSequence = expectedProjectionSequence,
Shadow = shadow,
Collision = collision,
Projection = projection,
PendingProjection = pendingProjection,
DeferredCollisionGeneration = deferredCollisionGeneration,
DeferredBucket = deferredBucket,
DeferredBucketIsNew = deferredBucketIsNew,
};
return IsPreparedDormantCommitCurrent(record, body, prepared);
}
internal bool TryApplyDormantLocalActivationCommit(
RuntimeEntityRecord record,
PhysicsBody body,
PlayerMovementController controller,
EntityPhysicsHost physicsHost,
PreparedDormantSetPositionCommit prepared,
out RuntimeDormantSetPositionCommitReceipt receipt)
{
EnsureNotDisposed();
ArgumentNullException.ThrowIfNull(record);
ArgumentNullException.ThrowIfNull(body);
ArgumentNullException.ThrowIfNull(controller);
ArgumentNullException.ThrowIfNull(physicsHost);
ArgumentNullException.ThrowIfNull(prepared);
receipt = default;
if (!IsPreparedDormantCommitCurrent(record, body, prepared)
|| !_operations.TryGetValue(
prepared.Entity,
out Operation? operation))
{
return false;
}
PhysicsSetPositionResult result = prepared.Evaluation.Result;
operation.Body = body;
operation.DormantLocalActivation = true;
if (result.IsDeferred)
{
if (prepared.Shadow is null
|| !_physics.Engine.ShadowObjects.TryApplySetPosition(
prepared.Shadow,
out ShadowObjectRegistry.SetPositionShadowCommitReceipt
deferredShadowReceipt))
{
return false;
}
body.Orientation = result.Orientation;
body.StageDormantCellFrame(
result.CellId,
result.Position,
result.CellLocalPosition);
body.InWorld = false;
body.TransientState &= ~TransientStateFlags.Active;
operation.Result = result;
operation.ExactCellId = result.CellId;
operation.WakeableLostCell = true;
operation.CollisionGeneration = prepared
.DeferredCollisionGeneration;
operation.CollisionGenerationReady = false;
operation.Stage = RuntimeEntityPlacementStage.AwaitingCell;
_preparedMovers[operation.Key] = prepared.Evaluation.Command.Physics;
if (prepared.DeferredBucket is { } deferredBucket)
{
var bucketKey = new CellGenerationKey(
result.CellId,
prepared.DeferredCollisionGeneration);
if (prepared.DeferredBucketIsNew)
{
_deferredByCellGeneration.Add(bucketKey, deferredBucket);
_deferredBucketOrder.Add(bucketKey);
}
else if (!deferredBucket.Contains(operation.Key))
{
deferredBucket.Add(operation.Key);
}
}
receipt = new RuntimeDormantSetPositionCommitReceipt(
RuntimeDormantSetPositionCommitStatus.DeferredCell,
operation.Key,
operation.Token.OperationId,
default,
default,
deferredShadowReceipt,
prepared.Evaluation.CollisionAuthority,
operation.Record.VectorAuthorityVersion,
HitGround: false,
LeaveGround: false);
return true;
}
if (prepared.Collision is null
|| !_physics.CollisionReports.TryInstallSetPositionBatch(
prepared.Collision,
out RuntimeCollisionReportingState
.SetPositionCollisionBatchReceipt collisionReceipt))
{
return false;
}
if (!result.IsCommitted)
{
operation.Result = result;
receipt = new RuntimeDormantSetPositionCommitReceipt(
RuntimeDormantSetPositionCommitStatus.RejectedPlacement,
operation.Key,
operation.Token.OperationId,
default,
collisionReceipt,
default,
prepared.Evaluation.CollisionAuthority,
operation.Record.VectorAuthorityVersion,
HitGround: false,
LeaveGround: false);
return true;
}
bool previousOnWalkable = operation.PreviousOnWalkable;
bool hitGround = !previousOnWalkable
&& result.InContact
&& result.OnWalkable;
bool leaveGround = previousOnWalkable
&& !(result.InContact && result.OnWalkable);
UnindexDeferred(operation);
body.Orientation = result.Orientation;
body.StageDormantCellFrame(
result.CellId,
result.Position,
result.CellLocalPosition);
body.LastUpdateTime = prepared.Evaluation.Command.GameTime;
body.ContactPlaneValid = result.InContact;
body.ContactPlane = result.ContactPlane;
body.ContactPlaneCellId = result.ContactPlaneCellId;
body.ContactPlaneIsWater = result.ContactPlaneIsWater;
if (result.InContact)
body.GroundNormal = result.ContactPlane.Normal;
_ = PhysicsObjUpdate.CommitSetPositionContactPrefix(
body,
result.InContact,
result.OnWalkable,
previousOnWalkable);
operation.Result = result;
operation.ExactCellId = result.CellId;
operation.WakeableLostCell = false;
operation.CollisionGenerationReady = false;
operation.EnteringWorldFromCelllessResidence = false;
operation.Stage = RuntimeEntityPlacementStage
.AwaitingFinalShadowPreparation;
receipt = new RuntimeDormantSetPositionCommitReceipt(
RuntimeDormantSetPositionCommitStatus
.AwaitingFinalShadowPreparation,
operation.Key,
operation.Token.OperationId,
prepared.Projection,
collisionReceipt,
default,
prepared.Evaluation.CollisionAuthority,
operation.Record.VectorAuthorityVersion,
hitGround,
leaveGround);
return true;
}
internal SetPositionCollisionBatchDispatchResult
DispatchDormantLocalActivationCollision(
in RuntimeDormantSetPositionCommitReceipt receipt)
{
if (receipt.Status
is RuntimeDormantSetPositionCommitStatus.DeferredCell
or RuntimeDormantSetPositionCommitStatus.RejectedAuthority)
{
return new(
SetPositionCollisionBatchDispatchStatus.RejectedReceipt,
Reported: false);
}
SetPositionCollisionBatchDispatchResult dispatch = _physics
.CollisionReports.DispatchSetPositionBatchResult(receipt.Collision);
bool reported = dispatch.Reported;
if (receipt.Status
is RuntimeDormantSetPositionCommitStatus.RejectedPlacement
&& _operations.TryGetValue(receipt.Entity, out Operation? operation)
&& operation.Token.OperationId == receipt.OperationId
&& IsCurrent(operation)
&& !operation.Result.IsCommitted
&& !operation.Result.IsDeferred)
{
operation.Result = operation.Result with
{
Error = reported
? PhysicsSetPositionError.Collided
: PhysicsSetPositionError.NoValidPosition,
CollisionHandlerResult = reported,
};
}
return dispatch;
}
internal bool IsDormantLocalActivationPrephaseCurrent(
RuntimeEntityRecord record,
PhysicsBody body,
in RuntimeDormantSetPositionCommitReceipt receipt)
{
return receipt.Status is RuntimeDormantSetPositionCommitStatus
.AwaitingFinalShadowPreparation
&& _operations.TryGetValue(receipt.Entity, out Operation? operation)
&& operation.Token.OperationId == receipt.OperationId
&& operation.Stage is RuntimeEntityPlacementStage
.AwaitingFinalShadowPreparation
&& operation.DormantLocalActivation
&& IsCurrent(operation)
&& ReferenceEquals(operation.Record, record)
&& ReferenceEquals(record.PhysicsBody, body)
&& !body.InWorld
&& (body.TransientState & TransientStateFlags.Active) == 0
&& record.PhysicsHost is null
&& record.RemoteMotion is null
&& record.Projectile is null
&& !_physics.IsSpatialRoot(record)
&& _moverPreparationAuthorities.TryGetValue(
receipt.Entity,
out MoverPreparationAuthority authority)
&& authority.OperationId == receipt.OperationId
&& authority.Prepared
&& record.PositionAuthorityVersion
== authority.PositionAuthorityVersion
&& record.ObjDescAuthorityVersion
== authority.ObjDescAuthorityVersion
&& record.CreateIntegrationVersion
== authority.CreateIntegrationVersion
&& CanonicalSetupTableId(record) == authority.SetupTableId;
}
internal bool IsDormantLocalActivationResponseCurrent(
RuntimeEntityRecord record,
PhysicsBody body,
in RuntimeDormantSetPositionCommitReceipt receipt)
{
if (receipt.Status is RuntimeDormantSetPositionCommitStatus
.AwaitingFinalShadowPreparation)
return IsDormantLocalActivationPrephaseCurrent(record, body, receipt);
return receipt.Status is RuntimeDormantSetPositionCommitStatus
.RejectedPlacement
&& _operations.TryGetValue(receipt.Entity, out Operation? operation)
&& operation.Token.OperationId == receipt.OperationId
&& operation.DormantLocalActivation
&& IsCurrent(operation)
&& ReferenceEquals(operation.Record, record)
&& ReferenceEquals(record.PhysicsBody, body)
&& !operation.Result.IsCommitted
&& !operation.Result.IsDeferred
&& !body.InWorld
&& (body.TransientState & TransientStateFlags.Active) == 0
&& record.PhysicsHost is null
&& record.RemoteMotion is null
&& record.Projectile is null
&& !_physics.IsSpatialRoot(record);
}
internal bool CommitDormantLocalActivationPostGround(
RuntimeEntityRecord record,
PhysicsBody body,
in RuntimeDormantSetPositionCommitReceipt receipt)
{
if (!IsDormantLocalActivationPrephaseCurrent(record, body, receipt))
return false;
PhysicsObjUpdate.CommitSetPositionPostGround(body);
PhysicsSetPositionResult result = _operations[receipt.Entity].Result;
body.SlidingNormal = result.SlidingNormal;
if (result.SlidingNormalValid)
body.TransientState |= TransientStateFlags.Sliding;
else
body.TransientState &= ~TransientStateFlags.Sliding;
return IsDormantLocalActivationPrephaseCurrent(record, body, receipt);
}
internal bool CommitDormantLocalActivationPostCollision(
RuntimeEntityRecord record,
PhysicsBody body,
in RuntimeDormantSetPositionCommitReceipt receipt)
{
if (!_operations.TryGetValue(receipt.Entity, out Operation? operation)
|| operation.Token.OperationId != receipt.OperationId
|| !IsCurrent(operation)
|| !ReferenceEquals(operation.Record, record)
|| !ReferenceEquals(record.PhysicsBody, body))
{
return false;
}
if (receipt.Status is RuntimeDormantSetPositionCommitStatus
.AwaitingFinalShadowPreparation
&& !IsDormantLocalActivationPrephaseCurrent(record, body, receipt))
{
return false;
}
PhysicsSetPositionResult result = operation.Result;
body.FramesStationaryFall = result.FramesStationaryFall;
if (IsVelocityCurrent(operation))
{
PhysicsObjUpdate.HandleAllCollisions(
body,
result.CollisionNormalValid,
result.CollisionNormal,
operation.PreviousContact,
operation.PreviousOnWalkable,
body.OnWalkable);
}
CommitStationaryBits(body, result.FramesStationaryFall);
return IsCurrent(operation);
}
internal bool TryPrepareDormantLocalActivationFinalCommit(
RuntimeEntityRecord record,
PhysicsBody body,
in RuntimeDormantSetPositionCommitReceipt receipt,
bool provenShapeless,
out PreparedDormantActivationFinalCommit? prepared)
{
prepared = null;
if (!IsDormantLocalActivationPrephaseCurrent(record, body, receipt)
|| !_operations.TryGetValue(receipt.Entity, out Operation? operation))
{
return false;
}
PhysicsSetPositionResult result = operation.Result;
if (!_physics.Engine.ShadowObjects.TryPrepareSetPosition(
operation.Key.LocalEntityId,
result.Position,
result.Orientation,
result.CellId,
operation.Command.ShadowWorldOffsetX,
operation.Command.ShadowWorldOffsetY,
result.ShadowAction,
result.CrossCellIds,
provenShapeless,
suspendOwner: false,
out ShadowObjectRegistry.PreparedSetPositionShadowCommit? shadow)
|| shadow is null
|| _nextProjectionSequence + 1UL
!= receipt.Projection.Token.Sequence)
{
return false;
}
var pending = new SortedDictionary<
ulong,
RuntimePlacementProjectionSnapshot>(_pendingProjection)
{
[receipt.Projection.Token.Sequence] = receipt.Projection,
};
prepared = new PreparedDormantActivationFinalCommit
{
Entity = receipt.Entity,
OperationId = receipt.OperationId,
ExpectedProjectionSequence = _nextProjectionSequence,
Shadow = shadow,
Projection = receipt.Projection,
PendingProjection = pending,
};
bool current = IsDormantLocalActivationPrephaseCurrent(
record, body, receipt);
bool shadowCurrent = _physics.Engine.ShadowObjects
.IsPreparedSetPositionCurrent(shadow);
return current && shadowCurrent;
}
internal bool TryApplyDormantLocalActivationFinalCommit(
RuntimeEntityRecord record,
PhysicsBody body,
PlayerMovementController controller,
EntityPhysicsHost physicsHost,
in RuntimeDormantSetPositionCommitReceipt prephase,
PreparedDormantActivationFinalCommit prepared,
out RuntimeDormantSetPositionCommitReceipt committed)
{
committed = default;
if (prepared.Entity != prephase.Entity
|| prepared.OperationId != prephase.OperationId
|| prepared.ExpectedProjectionSequence != _nextProjectionSequence
|| prepared.Projection != prephase.Projection
|| !IsDormantLocalActivationPrephaseCurrent(record, body, prephase)
|| !_physics.Engine.ShadowObjects.TryApplySetPosition(
prepared.Shadow,
out ShadowObjectRegistry.SetPositionShadowCommitReceipt shadow))
{
return false;
}
Operation operation = _operations[prephase.Entity];
PhysicsSetPositionResult result = operation.Result;
if (record.FullCellId != result.CellId)
{
_entities.SetFullCell(record, result.CellId,
(result.CellId & 0xFFFF0000u) | 0xFFFFu);
}
operation.SpatialAuthorityVersion = record.SpatialAuthorityVersion;
_entities.AdvancePlacementCommit(record);
operation.PlacementCommitVersion = record.PlacementCommitVersion;
body.InWorld = true;
bool isStatic = (record.FinalPhysicsState & PhysicsStateFlags.Static) != 0;
if (!isStatic)
body.TransientState |= TransientStateFlags.Active;
_entities.SetPhysicsHost(record, physicsHost);
controller.CommitRuntimeActivationFrame();
_physics.Engine.UpdatePlayerCurrCell(result.CellId);
_physics.AcknowledgeSpatialProjection(record, spatial: true);
_entities.ResetObjectClockForEnterWorld(record, isStatic);
operation.Stage = RuntimeEntityPlacementStage.AwaitingCommitAcknowledgement;
operation.ProjectionSequence = prepared.Projection.Token.Sequence;
_pendingProjection = prepared.PendingProjection;
_nextProjectionSequence = prepared.Projection.Token.Sequence;
CancelLostFamilyDeadlines(operation);
controller.ActivateRuntimePublication();
committed = prephase with
{
Status = RuntimeDormantSetPositionCommitStatus.Committed,
Shadow = shadow,
};
return true;
}
internal void DispatchDormantLocalActivationShadow(
in RuntimeDormantSetPositionCommitReceipt receipt)
{
if (receipt.Status is not (
RuntimeDormantSetPositionCommitStatus.Committed
or RuntimeDormantSetPositionCommitStatus.DeferredCell))
return;
_physics.Engine.ShadowObjects.DispatchSetPositionCommit(receipt.Shadow);
}
internal void DispatchDormantLocalActivationPlacement(
in RuntimeDormantSetPositionCommitReceipt receipt)
{
if (!receipt.IsCommitted)
return;
PublishPlacement(receipt.Projection);
}
internal void DiscardDormantLocalActivationDispatches(
in RuntimeDormantSetPositionCommitReceipt receipt,
bool collisionAlreadyDispatched)
{
if (!collisionAlreadyDispatched)
_physics.CollisionReports.DiscardSetPositionBatch(receipt.Collision);
_physics.Engine.ShadowObjects.DiscardSetPositionCommit(receipt.Shadow);
}
internal void RetireDormantLocalActivation(
in RuntimeDormantSetPositionCommitReceipt receipt,
bool collisionAlreadyDispatched)
{
DiscardDormantLocalActivationDispatches(
receipt,
collisionAlreadyDispatched);
_physics.CollisionReports.RetireSetPositionBatchOwner(
receipt.Collision);
if (_operations.TryGetValue(receipt.Entity, out Operation? operation)
&& operation.Token.OperationId == receipt.OperationId)
{
_ = CancelCore(operation);
}
}
internal void RetireDormantLocalActivationToken(
RuntimeEntityRecord record,
in RuntimeEntityPlacementToken token)
{
if (!token.IsValid
|| record.Key != token.Entity
|| !_operations.TryGetValue(token.Entity, out Operation? operation)
|| operation.Token != token
|| !ReferenceEquals(operation.Record, record))
{
return;
}
_ = CancelCore(operation);
}
internal bool IsDormantLocalActivationCommitCurrent(
RuntimeEntityRecord record,
PhysicsBody body,
in RuntimeDormantSetPositionCommitReceipt receipt)
{
if (!receipt.IsCommitted
|| !receipt.Projection.Token.IsValid
|| record.Key != receipt.Projection.Token.Entity
|| !ReferenceEquals(record.PhysicsBody, body)
|| !_pendingProjection.TryGetValue(
receipt.Projection.Token.Sequence,
out RuntimePlacementProjectionSnapshot pending)
|| pending != receipt.Projection
|| !_operations.TryGetValue(
receipt.Projection.Token.Entity,
out Operation? operation)
|| operation.Stage is not RuntimeEntityPlacementStage
.AwaitingCommitAcknowledgement
|| operation.ProjectionSequence
!= receipt.Projection.Token.Sequence
|| !IsCurrent(operation)
|| !body.InWorld
|| !_physics.IsSpatialRoot(record))
{
return false;
}
return true;
}
internal bool TryCaptureDormantLocalActivationResult(
in RuntimeEntityPlacementToken token,
out PhysicsSetPositionResult result)
{
if (!_disposed
&& token.IsValid
&& _operations.TryGetValue(token.Entity, out Operation? operation)
&& operation.Token == token)
{
result = operation.Result;
return true;
}
result = default;
return false;
}
private bool IsPreparedDormantCommitCurrent(
RuntimeEntityRecord record,
PhysicsBody body,
PreparedDormantSetPositionCommit prepared)
{
if (_nextProjectionSequence != prepared.ExpectedProjectionSequence
|| !IsDormantLocalEvaluationCurrent(
record,
body,
prepared.Evaluation)
|| !_operations.TryGetValue(
prepared.Entity,
out Operation? operation)
|| operation.Token.OperationId != prepared.OperationId)
{
return false;
}
if (prepared.Collision is not null
&& !_physics.CollisionReports.IsPreparedSetPositionBatchCurrent(
prepared.Collision))
return false;
return prepared.Shadow is null
|| _physics.Engine.ShadowObjects.IsPreparedSetPositionCurrent(
prepared.Shadow);
}
private static void CommitStationaryBits(
PhysicsBody body,
int framesStationaryFall)
{
body.TransientState &= ~(TransientStateFlags.StationaryFall
| TransientStateFlags.StationaryStop
| TransientStateFlags.StationaryStuck);
body.TransientState |= framesStationaryFall switch
{
1 => TransientStateFlags.StationaryFall,
2 => TransientStateFlags.StationaryStop,
3 => TransientStateFlags.StationaryStuck,
_ => TransientStateFlags.None,
};
}
internal RuntimeSetPositionOutcome SubmitPreparedPlacement(
in RuntimeEntityPlacementToken token,
in RuntimeSetPositionCommand command) =>
@ -1559,6 +2412,12 @@ internal sealed class RuntimeSetPositionState : IDisposable
private void RetryDeferred(Operation operation)
{
// The local-player activation lease owns its dormant body/controller
// and must re-enter through the same sealed evaluation/commit path.
// A collision-generation wake only marks readiness; it must never
// bypass that path through the ordinary remote CommitCanonical tail.
if (operation.DormantLocalActivation)
return;
if (!IsCurrent(operation)
|| !operation.WakeableLostCell
|| operation.RequiresPreparation
@ -2006,7 +2865,8 @@ internal sealed class RuntimeSetPositionState : IDisposable
in RuntimeEntityPlacementToken token,
in RuntimeSetPositionCommand command,
out Operation? operation,
bool allowCanonicalCommand = false)
bool allowCanonicalCommand = false,
bool allowDeferredLease = false)
{
operation = null;
if (!token.IsValid
@ -2021,6 +2881,14 @@ internal sealed class RuntimeSetPositionState : IDisposable
|| operation.Token != token
|| operation.Stage
is not RuntimeEntityPlacementStage.AwaitingPreparation
&& !(allowDeferredLease
&& operation.DormantLocalActivation
&& (operation.Stage
is RuntimeEntityPlacementStage.AwaitingCell
&& operation.WakeableLostCell
|| operation.Stage is RuntimeEntityPlacementStage
.AwaitingFinalShadowPreparation)
&& operation.ProjectionSequence == 0UL)
|| !ReferenceEquals(operation.Record, record)
|| !IsCurrent(operation)
|| !ReferenceEquals(record.PhysicsBody, body)