acdream/tests/AcDream.Runtime.Tests/Entities/RuntimeEntityPvpBitfieldSnapshotSyncTests.cs
Erik 9b1e6fc637 fix(physics): #297 — keep the PWD bitfield live so PK status reaches the client
The user typed @pklite and then walked straight through other PKLite players.

Root cause: ClientObject.PublicWeenieBitfield was written exactly once, from the
0xF745 CreateObject parse, and never refreshed. ACE's only PK-change message is
PropertyInt.PlayerKillerStatus (134) over 0x02CE/0x02CD, which we parsed and
stored into Properties.Ints[134] but never translated back into the bitfield —
and ACE never re-sends a PublicWeenieDesc at all (EnqueueBroadcastUpdateObject
has zero live callers), so that property is the ONLY signal a client can learn
from. Both sides of the collision test read the frozen value, so
CollisionExemption's "4c. both PKLite -> collide" rule could never fire.

Retail's missing port: PublicWeenieDesc::SetPlayerKillerStatus @0x005AC7C0
rewrites _bitfield in place — PK(4) -> (b & 0xfddfffff) | 0x20; PKLite(0x40) ->
(b & 0xffdfffdf) | 0x2000000; Free(0x20) -> (b & 0xfdffffdf) | 0x200000; else
b &= 0xfddfffdf. Mutually exclusive, verified byte-for-byte, with input values
confirmed against retail's own PKStatusEnum (acclient.h:6412-6427), not just
ACE's. Driven from ACCWeenieObject::OnStatUpdated @0x0058DF20 case 0x86.

The fix rewrites the value at its source rather than patching consumers. Two
review rounds were needed because the first pass missed that there are TWO
snapshot stores: InboundPhysicsStateController keeps its own private _snapshots
dictionary, and every untimestamped-field merge (ApplyAcceptedObjDesc and
friends) reads `old` from THAT store, not from RuntimeEntityRecord.Snapshot.
Refreshing only the active record left the target-side shadow flags correct
until the remote's next equip or unequip — ACE broadcasts an ObjDesc on every
one — at which point the appearance path rebuilt the registration from the
frozen spawn and dropped the bit permanently. The regression test demanded by
review is what surfaced that; it is verified discriminating (reverting gives
Actual: 8 instead of 33554440).

Five stores now hold this value, kept coherent from one source by two
ObjectUpdated subscribers plus the appearance-rebuild path. The two shadow-flag
writers are the same invalidation applied at the two edges that can invalidate
it, not competing authorities — review enumerated every drift path and closed
each. That coherence invariant is new as of this commit and is recorded as
register row AP-134, with AP-133 as the precedent for filing a row when the
danger is a future writer rather than current behaviour.

Also corrects TS-23's retirement narrative, which claimed every mover-flags call
site read the mover's "real" PK bits from 2026-07-30. The bits existed but their
source was frozen, so that only became true here; the site enumeration also
missed RuntimeSetPositionMoverPreparation, a seventh site that decodes the
snapshot directly.

Unblocks #298 (melee/missile admission needs the local player's own PKLite bit).
Follow-ups filed: #300 (Properties.Ints[134] vs bitfield mirror gap), #301 (same
defect class for radar blip colour and radar behaviour), #302 (a pre-existing
PortalProjection allocation-assertion flake, 1 in 6, found while verifying this
gate), #303 (LiveEntityPvpBitfieldSync is App-resident but Runtime-owned-state).

Gates: complete Release solution 10,895 passed / 4 skipped / 0 failed (baseline
10,887 including #299). Adversarial + retail-conformance review PASS after one
FAIL round. Every new test discrimination-verified by reverting the fix.
Connected acceptance NOT run — needs a live two-client PKLite session.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-03 20:59:01 +02:00

189 lines
7 KiB
C#

using AcDream.Core.Items;
using AcDream.Core.Net;
using AcDream.Core.Net.Messages;
using AcDream.Core.Physics;
using AcDream.Runtime.Entities;
namespace AcDream.Runtime.Tests.Entities;
/// <summary>
/// #297 review round 2 (preferred fix): <see cref="RuntimeEntityPvpBitfieldSnapshotSync"/>
/// keeps <c>RuntimeEntityRecord.Snapshot.ObjectDescriptionFlags</c> live
/// against <see cref="ClientObject.PublicWeenieBitfield"/>, so every
/// downstream reader of the canonical snapshot (ObjDesc/appearance rebuild,
/// placement/teleport mover-flags resolution, the vivid target indicator)
/// inherits the live PK/PKLite/Free state instead of the value frozen at
/// CreateObject.
/// </summary>
public sealed class RuntimeEntityPvpBitfieldSnapshotSyncTests
{
private const uint Guid = 0x70000020u;
private static WorldSession.EntitySpawn Spawn(uint? objectDescriptionFlags) => new(
Guid,
new CreateObject.ServerPosition(0x0101FFFFu, 10f, 10f, 5f, 1f, 0f, 0f, 0f),
0x02000001u,
Array.Empty<CreateObject.AnimPartChange>(),
Array.Empty<CreateObject.TextureChange>(),
Array.Empty<CreateObject.SubPaletteSwap>(),
null,
null,
"fixture",
(uint)ItemType.Creature,
null,
0x09000001u,
ObjectDescriptionFlags: objectDescriptionFlags);
[Fact]
public void ObjectUpdated_WithLiveBitfield_RewritesSnapshotObjectDescriptionFlags()
{
var entities = new RuntimeEntityDirectory();
var objects = new ClientObjectTable();
using var sync = new RuntimeEntityPvpBitfieldSnapshotSync(entities, objects);
WorldSession.EntitySpawn spawn = Spawn(0x8u); // BF_PLAYER only
entities.AcceptCreate(spawn); // seeds InboundPhysicsStateController's own retained copy
RuntimeEntityRecord record = entities.AddActive(spawn);
objects.AddOrUpdate(new ClientObject
{
ObjectId = Guid,
PublicWeenieBitfield = 0x8u,
});
objects.UpdateIntProperty(
Guid,
ClientObjectTable.PlayerKillerStatusPropertyId,
value: PlayerKillerStatusBitfield.PkLite);
Assert.Equal(0x2000008u, record.Snapshot.ObjectDescriptionFlags);
}
[Fact]
public void ObjectUpdated_UnrelatedProperty_DoesNotRewriteSnapshot()
{
var entities = new RuntimeEntityDirectory();
var objects = new ClientObjectTable();
using var sync = new RuntimeEntityPvpBitfieldSnapshotSync(entities, objects);
WorldSession.EntitySpawn spawn = Spawn(0x8u);
entities.AcceptCreate(spawn);
RuntimeEntityRecord record = entities.AddActive(spawn);
objects.AddOrUpdate(new ClientObject
{
ObjectId = Guid,
PublicWeenieBitfield = 0x8u,
});
// Effects (18) is unrelated to PK status; PublicWeenieBitfield is
// untouched, so the idempotency guard must skip the rewrite.
objects.UpdateIntProperty(Guid, ClientObjectTable.UiEffectsPropertyId, value: 4);
Assert.Equal(0x8u, record.Snapshot.ObjectDescriptionFlags);
}
[Fact]
public void ObjectUpdated_NoPublicWeenieBitfieldYet_NoOp()
{
var entities = new RuntimeEntityDirectory();
var objects = new ClientObjectTable();
using var sync = new RuntimeEntityPvpBitfieldSnapshotSync(entities, objects);
RuntimeEntityRecord record = entities.AddActive(Spawn(null));
objects.AddOrUpdate(new ClientObject { ObjectId = Guid });
// No PublicWeenieBitfield yet (CreateObject omitted it) — the
// handler must no-op rather than throw or write a bogus 0.
objects.UpdateIntProperty(Guid, ClientObjectTable.UiEffectsPropertyId, value: 1);
Assert.Null(record.Snapshot.ObjectDescriptionFlags);
}
[Fact]
public void ObjectUpdated_NoMatchingActiveEntity_NoOp()
{
var entities = new RuntimeEntityDirectory();
var objects = new ClientObjectTable();
using var sync = new RuntimeEntityPvpBitfieldSnapshotSync(entities, objects);
// Item exists in the object table but has no active Runtime entity
// (e.g. an inventory item) — must not throw.
objects.AddOrUpdate(new ClientObject
{
ObjectId = 0x80000001u,
PublicWeenieBitfield = 0x8u,
});
objects.UpdateIntProperty(
0x80000001u,
ClientObjectTable.PlayerKillerStatusPropertyId,
value: PlayerKillerStatusBitfield.Pk);
Assert.Equal(0, entities.Count);
}
[Fact]
public void ObjDescAfterPkUpdate_MergedSnapshotKeepsLiveBitfield()
{
// #297 F1: InboundPhysicsStateController.ApplyAcceptedObjDesc merges
// a new ObjDesc onto _snapshots[guid] — a DIFFERENT store than the
// active RuntimeEntityRecord.Snapshot this class also rewrites.
// Without RuntimeEntityDirectory.TryRefreshObjectDescriptionFlags
// keeping BOTH stores in lockstep, this ObjDesc would silently
// revert the live PK-status bits back to whatever CreateObject
// originally carried.
var entities = new RuntimeEntityDirectory();
var objects = new ClientObjectTable();
using var sync = new RuntimeEntityPvpBitfieldSnapshotSync(entities, objects);
WorldSession.EntitySpawn spawn = Spawn(0x8u);
entities.AcceptCreate(spawn);
entities.AddActive(spawn);
objects.AddOrUpdate(new ClientObject
{
ObjectId = Guid,
PublicWeenieBitfield = 0x8u,
});
objects.UpdateIntProperty(
Guid,
ClientObjectTable.PlayerKillerStatusPropertyId,
value: PlayerKillerStatusBitfield.PkLite);
var update = new ObjDescEvent.Parsed(
Guid,
new CreateObject.ModelData(
0x04000001u,
Array.Empty<CreateObject.SubPaletteSwap>(),
Array.Empty<CreateObject.TextureChange>(),
Array.Empty<CreateObject.AnimPartChange>()),
InstanceSequence: 0,
ObjDescSequence: 1);
Assert.True(entities.TryApplyObjDesc(update, out WorldSession.EntitySpawn accepted));
Assert.Equal(0x2000008u, accepted.ObjectDescriptionFlags);
}
[Fact]
public void Dispose_UnsubscribesFromObjectUpdated()
{
var entities = new RuntimeEntityDirectory();
var objects = new ClientObjectTable();
var sync = new RuntimeEntityPvpBitfieldSnapshotSync(entities, objects);
WorldSession.EntitySpawn spawn = Spawn(0x8u);
entities.AcceptCreate(spawn);
RuntimeEntityRecord record = entities.AddActive(spawn);
objects.AddOrUpdate(new ClientObject
{
ObjectId = Guid,
PublicWeenieBitfield = 0x8u,
});
sync.Dispose();
objects.UpdateIntProperty(
Guid,
ClientObjectTable.PlayerKillerStatusPropertyId,
value: PlayerKillerStatusBitfield.PkLite);
Assert.Equal(0x8u, record.Snapshot.ObjectDescriptionFlags);
}
}