acdream/src/AcDream.Core/Physics/EntityCollisionFlags.cs
Erik 6dc7ba51ee feat(physics): C4 route 4b-3 — remote teleport + cell-less through the canonical placement
Flips the last remote classification (SetPosition: teleport-advanced and
cell-less) onto 4b-1's RuntimeRemotePlacementDriveController, runs retail's
teleport_hook before the placement, and deletes the legacy remote-teleport
machinery. Contract: docs/research/2026-08-04-c4-route-4b-3-contract.md.

Retail: MoveOrTeleport @0x00516330's branch @0x00516386 -> teleport_hook
@0x005163EF -> SetFlags(0x1012) @0x00516414 -> SetPosition @0x00516420 ->
return 1 @0x00516438. The hook @0x00514ED0 runs BEFORE the placement and
regardless of its outcome. Retail places this branch unconditionally, at any
distance and any contact state (arg4 is read only @0x0051638E, after the
branch) — which is what retires AP-137's cell-less enqueue-vs-place delta.

D1 — the classifier's cell-less input is now the PRE-merge committed cell.
Retail's predicate is `this_1->cell == 0`, the BODY's own cell at
MoveOrTeleport entry (this_1 is assigned from this @0x00516334). acdream fed
the POST-merge canonical.FullCellId, which RefreshSnapshot ->
RefreshDerivedState -> SetFullCell has already stamped with the accepted wire
cell; a zero wire cell fails validation into RejectedData first. The shipped
remote cell-less predicate was therefore dead code, not merely different from
remotePlacementRequired. Threaded via a builder overload; route 1's overload
is untouched. The graphical !IsSpatiallyVisible arm of
projectionRequiresTeleportHook is deleted — a presentation predicate with no
retail analogue that fired the teleport machinery on a routine hot path.

Deleted: RemoteTeleportController (605), RemoteTeleportPlacement (85),
RemoteShadowPlacementSynchronizer (49), their 1,709 lines of tests, the
remotePlacementRequired predicate, the TeleportHookRequired plumbing, the
legacy pre-operation ConstrainTo fallback, and the player arm's legacy
!IsGrounded fallback. Net -2,030 lines.

Structural fix (two independent Opus reviews, round 1 FAIL/FAIL): three of the
four MAJORs were one defect — OnPosition carried two parallel inline copies of
the routing tail (player-guid, NPC-guid) that had drifted. Extracted
RunRemoteArmTail (3 call sites) and ApplyWireAirborneLeftoverBookkeeping (2),
both branches now share one implementation.

  A1  ToConstraintArm mapped AirborneSnap -> AirborneNoOperation, so the NPC
      arm armed ConstrainTo ZERO times for an out-of-contact wire-grounded
      creature — a regression this slice introduced while closing a
      structurally identical hole. Now maps to NearInterpolate; switch made
      total with a throwing default proven unreachable.
  R1  D2's write-nothing shape existed on the player arm only; NPC packets
      fell through and wrote the body. Retail makes no player/NPC distinction.
  R2  report_collision_end(this,1) @0x00514F31 was bound to
      ShadowObjects.Suspend, a port of a DIFFERENT retail function
      (remove_shadows_from_cells) that teleport_hook never calls. Now routes
      to RuntimeCollisionReportingState.LeaveWorld, which wraps the private
      ForceEnd in an admission-blocking transaction so a DoCollisionEnd
      callback cannot recreate the contact table.
  R3/A2 A teleported NPC synthesized ServerVelocity from the teleport distance
      (~1,000+ m/s) and planned a run cycle from it. Both the install and
      RemoteServerControlledVelocityCycle.Apply now gate on !isTeleportRoute.

BISECT HAZARD — A1's fix is correct only BECAUSE R1 landed. AirborneSnap is
reachable wire-airborne on the NPC arm only while D2's shape is missing there.
Reverting R1 alone silently inverts A1 into the opposite divergence: arming
where retail returns 0. Revert both or neither.

Also in the velocity hunk: the NPC block's two !IsPlayerGuid(update.Guid)
guards were dropped when it was wrapped in `if (!isTeleportRoute)`. Safe — all
five exit paths of the enclosing IsPlayerGuid block return, so the predicate is
unconditionally false below it — but it was unremarked by both reviews.

Register: AP-137 REWRITTEN (not deleted) to the surviving acdream-only
divergences — null classification during the login window and Rejected*
through UnroutedCatchUp keep a row. AD-42's RemoteTeleportController citation
retired; AP-136/AP-138 writer lists corrected to the two surviving non-Position
rebucket writers; AP-138 gains the teleport arm as a second producer of the
visible-without-collision residual (retirement path remains #309). AP-135 is
untouched and its two airborne bookkeeping writes are preserved on both arms.
AP-131 does not retire; #276 does not close.

Proof obligation 1: ParkCollisionResidents' overlap throw stays unreachable —
the teleport arm adds packets to the same TryBeginExclusiveAuthoredPlacement
one-operation-per-key machinery the far arm uses, opens no new operation shape,
and every DeferredCell outcome cancels synchronously with
restoreCancelledPark: true. The guarded property remains
HasOldPrefixPlacementDebt's stall, not a throw (4b-1's B2 caveat stands).

Correction to an earlier claim: LiveEntityPresentationController's
_activePlacementOwners was NOT write-never at HEAD —
remotePlacementRequired -> BeginPlacement -> Begin -> BeginAuthoritativePlacement
was a live writer chain. It becomes write-never BECAUSE this slice deletes that
chain, which is why deleting the dead half is behaviour-preserving.

Probe: ACDREAM_PROBE_REMOTE_TELEPORT=1 emits one [remote-teleport] line per
routed arm (guid, cause, hook-ran, placement status). TEMPORARY, strip with the
probe family.

Carried, disclosed not fixed: no dedicated bidirectional collision-partner test
for R2 (the wiring, not LeaveWorld itself, is what lacks coverage); the
stress test's teleport step drives hand-written field assignments rather than
the canonical arm; the per-packet runTeleportHook closure allocation (network
path, not the resolve path Slice I's 0 B discipline governs — file before
route 5 adds a fourth call site). B2: IRuntimeCollisionReportObserver has zero
production implementations, so retail's bidirectional DoCollisionEnd half still
reaches no gameplay consumer — this fix closes the wrong-function binding, not
that nobody listens.

Complete Release suite MEASURED at 11,013 passed / 4 skipped / 0 failed
(baseline 11,027/4/0; net -14 = ~33 deleted test cases against ~19 added).
Neither known flake fired (#302 PortalProjectionTests GC-allocation, #308
NakEmissionTests wall-clock).

STILL OWED: the two-client connected gate, which MUST use an NPC/creature
teleport target. Both round-1 MAJORs lived on the NPC arm and the velocity
cycle early-returns for 0x50xxxxxx guids, so a player target structurally
cannot observe A1, A2, or R3.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-04 16:00:10 +02:00

157 lines
7.8 KiB
C#

using AcDream.Core.Items;
namespace AcDream.Core.Physics;
/// <summary>
/// Per-entity flags driving the retail-faithful PvP / Player /
/// Impenetrable exemption logic in <c>FindObjCollisions</c>. Decoded
/// from <c>PublicWeenieDesc._bitfield</c> at <c>CreateObject</c> time.
///
/// <para>
/// Bit positions (verified against
/// <c>docs/research/named-retail/acclient_2013_pseudo_c.txt:406898-406918</c>
/// where <c>ACCWeenieObject::IsPK/IsPKLite/IsImpenetrable</c> read directly
/// from <c>this-&gt;pwd._bitfield</c>):
/// </para>
/// <list type="bullet">
/// <item><c>BF_PLAYER = 0x8</c> (bit 3) → <see cref="IsPlayer"/></item>
/// <item><c>BF_PLAYER_KILLER = 0x20</c> (bit 5) → <see cref="IsPK"/></item>
/// <item><c>BF_FREE_PKSTATUS = 0x200000</c> (bit 21) → <see cref="IsImpenetrable"/></item>
/// <item><c>BF_PKLITE_PKSTATUS = 0x2000000</c> (bit 25) → <see cref="IsPKLite"/></item>
/// </list>
///
/// <para>
/// <see cref="IsCreature"/> is NOT a PWD bit — retail derives it from
/// <c>PublicWeenieDesc._type</c> matching <c>ITEM_TYPE_CREATURE</c>
/// (acclient.h ITEM_TYPE enum). Set at registration time by callers that
/// already know the item type.
/// </para>
/// </summary>
[Flags]
public enum EntityCollisionFlags : byte
{
None = 0x00,
/// <summary>Set when <c>BF_PLAYER (0x8)</c> is set in <c>pwd._bitfield</c>.</summary>
IsPlayer = 0x01,
/// <summary>Derived from <c>ItemType.Creature</c> on the spawn payload.</summary>
IsCreature = 0x02,
/// <summary>Set when <c>BF_PLAYER_KILLER (0x20)</c> is set.</summary>
IsPK = 0x04,
/// <summary>Set when <c>BF_PKLITE_PKSTATUS (0x2000000)</c> is set.</summary>
IsPKLite = 0x08,
/// <summary>Set when <c>BF_FREE_PKSTATUS (0x200000)</c> is set (a.k.a. "Free" PK status — cannot be PKed).</summary>
IsImpenetrable = 0x10,
/// <summary>
/// Runtime collision metadata: the target has a retail
/// <c>CWeenieObject</c>. This is deliberately distinct from
/// <see cref="IsCreature"/> because doors, portals, and items also have a
/// weenie. <c>OBJECTINFO::missile_ignore</c> at <c>0x0050CEB0</c>
/// requires this distinction for ethereal targets.
/// </summary>
HasWeenie = 0x20,
/// <summary>
/// AP-71 (Campaign P Slice P4, 2026-07-30): set when BOTH
/// <c>BF_ADMIN (0x100000)</c> and <c>BF_IMMUNE_CELL_RESTRICTIONS
/// (0x400000)</c> are set in <c>pwd._bitfield</c> — the exact two-bit AND
/// retail's <c>ACCWeenieObject::CanBypassMoveRestrictions</c>
/// (0x0058c500) evaluates to let a mover through an access-restricted
/// (house-barrier) cell without an owner/guest-list check. Bit names
/// confirmed at <c>acclient.h:6452-6454</c>
/// (<c>PublicWeenieDesc::BitfieldIndex</c>); <c>BF_ADMIN</c> is
/// independently decoded the same way in
/// <see cref="AcDream.Core.Ui.RadarBlipColors"/>.
/// </summary>
CanBypassMoveRestrictions = 0x40,
}
/// <summary>Helpers to convert raw retail bitfields into <see cref="EntityCollisionFlags"/>.</summary>
public static class EntityCollisionFlagsExt
{
/// <summary>
/// #297 (target-side refresh): exactly the subset of
/// <see cref="EntityCollisionFlags"/> that <see cref="FromPwdBitfield"/>
/// can produce — IsPlayer/IsPK/IsPKLite/IsImpenetrable/
/// CanBypassMoveRestrictions. Disjoint from <see cref="EntityCollisionFlags.IsCreature"/>
/// (derived from ItemType) and <see cref="EntityCollisionFlags.HasWeenie"/>
/// (set once at registration), so a live PWD-bitfield refresh can replace
/// exactly this mask without disturbing either.
/// </summary>
public const EntityCollisionFlags PwdBitfieldDerivedMask =
EntityCollisionFlags.IsPlayer
| EntityCollisionFlags.IsPK
| EntityCollisionFlags.IsPKLite
| EntityCollisionFlags.IsImpenetrable
| EntityCollisionFlags.CanBypassMoveRestrictions;
/// <summary>
/// Decode the player/PK/PKLite/Impenetrable bits from a
/// <c>PublicWeenieDesc._bitfield</c> value (the WeenieHeader trailer
/// field acdream's parser surfaces as <c>ObjectDescriptionFlags</c>).
///
/// <para>Bit positions per
/// <c>docs/research/named-retail/acclient.h:6431-6463</c>
/// (<c>PublicWeenieDesc::BitfieldIndex</c>) and
/// <c>acclient_2013_pseudo_c.txt:441868-441890</c>
/// (<c>PublicWeenieDesc::SetPlayerKillerStatus</c>).</para>
/// </summary>
public static EntityCollisionFlags FromPwdBitfield(uint bitfield)
{
var flags = EntityCollisionFlags.None;
if ((bitfield & 0x8u) != 0) flags |= EntityCollisionFlags.IsPlayer;
if ((bitfield & 0x20u) != 0) flags |= EntityCollisionFlags.IsPK;
if ((bitfield & 0x200000u) != 0) flags |= EntityCollisionFlags.IsImpenetrable;
if ((bitfield & 0x2000000u) != 0) flags |= EntityCollisionFlags.IsPKLite;
// AP-71: BF_ADMIN (0x100000) AND BF_IMMUNE_CELL_RESTRICTIONS (0x400000),
// matching CanBypassMoveRestrictions' own AND (not OR) of the two bits.
if ((bitfield & 0x100000u) != 0 && (bitfield & 0x400000u) != 0)
flags |= EntityCollisionFlags.CanBypassMoveRestrictions;
return flags;
}
/// <summary>
/// TS-23 (Campaign P Slice P3, 2026-07-30): translate the decoded
/// per-entity <see cref="EntityCollisionFlags"/> (the PWD-bitfield
/// bit-space) into the <see cref="ObjectInfoState"/> bits
/// <c>CollisionExemption.ShouldSkip</c> and the moverFlags argument to
/// <c>PhysicsEngine.ResolveWithTransition</c> actually consume — a
/// DIFFERENT bit-space (retail <c>OBJECTINFO::init</c> 0x0050cf30
/// `state |= 0x80/0x800/0x1000`, acclient.h:6190-6194) that must not be
/// confused with the PWD wire numbering. <see cref="EntityCollisionFlags.IsPlayer"/>
/// is deliberately NOT translated here — every existing mover-flags call
/// site already derives <see cref="ObjectInfoState.IsPlayer"/> from its
/// own GUID-prefix heuristic (correct per #184 Slice 2b) and this helper
/// only fills the gap that heuristic cannot: PK/PKLite/Impenetrable.
/// </summary>
public static ObjectInfoState ToMoverState(this EntityCollisionFlags flags)
{
var state = ObjectInfoState.None;
if ((flags & EntityCollisionFlags.IsPK) != 0) state |= ObjectInfoState.IsPK;
if ((flags & EntityCollisionFlags.IsPKLite) != 0) state |= ObjectInfoState.IsPKLite;
if ((flags & EntityCollisionFlags.IsImpenetrable) != 0) state |= ObjectInfoState.IsImpenetrable;
// AP-71 (Campaign P Slice P4): the mover's own house-restriction bypass,
// consumed by ObjectInfo.CheckEntryRestrictions.
if ((flags & EntityCollisionFlags.CanBypassMoveRestrictions) != 0)
state |= ObjectInfoState.CanBypassMoveRestrictions;
return state;
}
/// <summary>
/// TS-23 (Campaign P Slice P3, 2026-07-30): the one shared
/// <c>ClientObjectTable</c>-backed mover-flags lookup every physics
/// call site (local player world-entry, remote DR sweep + teleport,
/// ordinary movers) uses — was inlined three times (App
/// <c>GameWindow</c>/<c>LivePresentationComposition</c>/the since-deleted
/// <c>RemoteTeleportController</c>, C4 route 4b-3) before being
/// consolidated here.
/// An entity with no row, or a row with no wire bitfield yet, resolves
/// to <see cref="ObjectInfoState.None"/> — a no-op OR into moverFlags,
/// bit-identical to every pre-P3 caller.
/// </summary>
public static ObjectInfoState ResolveMoverPvpState(this ClientObjectTable objects, uint serverGuid)
{
ArgumentNullException.ThrowIfNull(objects);
return objects.Get(serverGuid)?.PublicWeenieBitfield is { } bitfield
? FromPwdBitfield(bitfield).ToMoverState()
: ObjectInfoState.None;
}
}