The AP-152 retail review (docs/research/2026-08-06-ap152-review-retail.md) FAILED `4abd1b5e` and is right. `ShadowObjectRegistry.BuildFloodSpheres` took each physics-BSP part's ROOT BOUNDING SPHERE RADIUS (FlatCollisionAssetBuilder.cs:393 -> LiveEntityCollisionBuilder.cs:137) and centred it on the PART ORIGIN (ShadowShapeBuilder.cs:194), discarding the root sphere's own Origin. Re-measured independently against the installed client_portal.dat, reproducing the reviewer's numbers exactly: 376 of 973 physics-BSP parts have |origin| > radius/2, worst 20.762 m on a 27.708 m sphere (gfx 0x010036DD, Setup 0x0200129A). Over the 172 Setups AP-152 moved onto that path the emitted flood FAILED TO CONTAIN the object's own BSP sphere for 170 of them (73 CylSphere-bearing, 97 Sphere-bearing), worst shortfall 9.911 m on Setup 0x02000255 — whose one part's sphere sits 9.911 m above the part origin — and for 43 the post-AP-152 flood was strictly SMALLER than the pre-AP-152 one. Indoor flooding is 3-D (CellTransit.cs:601 routes every id & 0xFFFF >= 0x0100 candidate through FindTransitCellsSphere), so a tall prop or door slab was absent from EnvCells it physically occupies and therefore never a broadphase candidate there (TransitionTypes.cs:3763 iterates only entries already in the cell). That is the #98 / #168 class AP-152 exists to remove. Retail, re-disassembled from the PDB-paired binary (check_exe_pdb.py MATCH, CodeView GUID 9e847e2f-777c-4bd9-886c-22256bb87f32), every address resolved back through named-retail/symbols.json: CGfxObj::physics_sphere is [gfxobj+0x74] (physics_bsp is [+0x78], as CPartArray::CacheHasPhysicsBSP @0x00518110 reads at 0x00518127), and acclient pseudo-C 0x00534b5b assigns it BSPTREE::GetSphere(physics_bsp). BSPTREE::GetSphere @0x005397e0 8b01 mov eax,[ecx] ; BSPTREE::root_node 83c004 add eax,4 ; past BSPNODE::vfptr -> CSphere sphere So retail's per-part flood sphere IS the BSP root bounding sphere, ORIGIN INCLUDED (acclient.h: BSPNODE { vfptr; CSphere sphere; ... }, CSphere { Vector3 center; float radius; } -> radius at +0xc). CPhysicsObj::find_bbox_cell_list @0x00510fc0 adds the object's own cell and then walks the PART ARRAY: 0x00511012 call 0x518160 (CPartArray::calc_cross_cells_static), which dispatches [edx+0x7c] with (num_parts, parts, cellarray). Its EnvCell body, CEnvCell::find_transit_cells @0x0052cae0: 0x0052cb31 mov edx,[eax+0x20] ; CPhysicsPart::gfxobj (CGfxObj**) 0x0052cb36 mov esi,[ecx+0x74] ; physics_sphere (else +0x90 drawing) 0x0052cb4c add eax,0x30 ; CPhysicsPart::pos 0x0052cb5a call Position::localtolocal ; transform the sphere CENTRE 0x0052cb65 fadd [esi+0xc] ; only NOW the radius Retail transforms the centre through the part's own Position before it ever touches the radius. Carrying the radius alone is not an approximation of that; it is a different sphere. Changes: * `ShadowShape` gains `BoundsCenter` — the bounding sphere's centre in the shape's own local frame, scaled like LocalPosition and Radius. Zero for Cylinder/Sphere shapes, whose LocalPosition already IS their centre. * `ShadowShapeBuilder.FromSetup` gains a `physicsBspBounds` resolver that supplies radius AND centre from ONE call, replacing the placeholder radius plus a downstream substitution. `LiveEntityCollisionBuilder` now holds a single `Func<uint, FlatCollisionSphere?>` and derives its dispatch predicate from it, so the gate and the geometry cannot disagree and the radius cannot be taken while the origin is dropped. That split is what produced this bug; it no longer exists. * `FromLandblockBspParts` carries the centre too. A landblock-baked part array is the same CPartArray walk, so stair runs, fences and rock clusters had the identical defect. Both storage forms (flat BSP and the graph fallback) are covered. * `BuildFloodSpheres` places each sphere at partWorldPos + rotate(BoundsCenter, partWorldRot), composed exactly as the ShadowEntry rows are. * The 10-sphere clamp now applies to the CYLSPHERE branch only. Retail's clamp is inside CObjCell::find_cell_list @0x0052b9f0 (0x0052ba21 cmp eax,0xa / 0x0052ba28 mov ebp,0xa); the BSP walk has none and the sorting-sphere overload @0x0052b990 takes one sphere. 7 installed Setups carry more than 10 physics-BSP parts (max 49, Setup 0x02001A91) and their tail parts were dropped from the flood entirely. Without this the new containment assertion would have covered shapes production never floods from. Register. AP-155 was two divergences with different code paths, populations and gates under one id; it is NARROWED to its static-publication half and its flood half is split out as AP-156 WITH ITS DIRECTION CORRECTED. AP-155(b) recorded the approximation as over-inclusive — "floods MORE cells rather than fewer, the safe direction for membership" — and that false direction was the stated reason the residual was safe to defer. It was under-inclusive for 170 of 172. AP-156 records the correction, this fix, and the one genuine residual: acdream's sphere-vs-portal traversal where retail walks each part's sphere against the cell's own portal planes. AP-155(b)'s "acdream approximates retail's bounding BOX" was wrong too — find_bbox_cell_list forms no box. AP-157 filed for the review's F4: retail's third branch floods from ONE CPartArray::GetSortingSphere @0x00518b00 ([partArray+0x54]+0x70 = CSetup::sorting_sphere; 4,154 of 5,935 installed Setups carry a non-zero one) where acdream floods from every Sphere shape, and acdream's cylinder flood ignores CylHeight. Deliberately NOT bundled here: different branch, disjoint population, different live gate. Active AP rows 107 -> 109, literal count. Tests. Both flood tests the review named substituted a CONCENTRIC Radius = 14f at LocalPosition = Zero — the one configuration in which the defect cannot appear. Every fixture is now off-centre by default, and `FromSetup_CylSphereAndBspSetup_FloodsTheBspFootprint` drives the production `physicsBspBounds` seam instead of hand-substituting. Five new facts: the flood centres on BoundsCenter not the part origin; it rotates BoundsCenter by the part rotation; it caps cylspheres at ten but never the BSP parts; the landblock path carries the scaled centre in both storage forms; and an installed-DAT containment sweep asserting every emitted BSP flood sphere contains that part's real bounding sphere at entity scale 1.75, behind four external controls — 973 parts, 376 off-centre, 172 affected, and 170 would-fail-if-the-origin-were-discarded, the last of which fails if the population ever stops exercising the field. Nine sabotages, each reverted and re-verified: A drop BoundsCenter from the flood -> 3 Core B rotate by entity rot, not part rot -> 1 Core (the rotation fact only) C FromSetup discards the origin -> 1 Core + 2 App + 1 Content (the shipped defect, now caught in three projects) D drop entScale on BoundsCenter -> 2 App + 1 Content E landblock flat branch drops the centre -> 1 Core F landblock graph branch drops it -> 1 Core G drop partScale on the landblock centre -> 1 Core H re-apply the 10-cap to every branch -> 1 Core I remove the cylsphere cap -> 1 Core AP-152's own two sabotages re-run against this tree: the step-0 gate disabled still reddens exactly its five facts with Headless 89/89 green, and cylinder-first flooding still reddens exactly one. Clean Release build after deleting all 44 bin/obj: 0 errors, 21 pre-existing warnings. Complete suite 11,208 passed / 4 skipped / 0 failed, +5 on the 11,203 baseline at4abd1b5e— Core 4264 -> 4268, Content 126 -> 127, App unchanged (one rename, not an addition). No new skips. NOT yet gated live. This moves shadow-cell membership for real objects, in both directions, and the connected session must look for both: props and doors that START blocking from a neighbouring cell (the 73 CylSphere+BSP Setups), AND ones that STOP blocking (the 99 Sphere+BSP Setups can shrink; 43 shrink below their pre-4abd1b5e size, which is the regression this fixes). Tall indoor props and door slabs — the ones whose sphere sits metres above the part origin — are where the change is largest. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
208 lines
9.8 KiB
C#
208 lines
9.8 KiB
C#
using System.Collections.Generic;
|
||
using System.Collections.Immutable;
|
||
using System.Linq;
|
||
using System.Numerics;
|
||
using AcDream.Core.Physics;
|
||
using AcDream.Core.World;
|
||
using DatReaderWriter.Enums;
|
||
using DatReaderWriter.Types;
|
||
using Xunit;
|
||
|
||
namespace AcDream.Core.Tests.Physics;
|
||
|
||
/// <summary>
|
||
/// #185 (2026-07-08) — the outdoor-stairs "invisible wall" root cause: the
|
||
/// former per-part landblock shadow registration used a synthetic part-id
|
||
/// <c>entity.Id * 256u + partIndex</c> that OVERFLOWED uint32 for class-prefixed
|
||
/// landblock ids (<c>0x40</c>/<c>0x80</c>/<c>0xC0</c>…). The <c><< 8</c> dropped the
|
||
/// prefix byte, so different-class entities sharing the low 24 bits collided on
|
||
/// one shadow part-id and <c>Register</c>'s deregister-then-insert silently
|
||
/// overwrote one entity's collision geometry — rendered stair steps with NO
|
||
/// collision. The fix registers each multi-part entity via
|
||
/// <see cref="ShadowObjectRegistry.RegisterMultiPart"/> under its UNIQUE 32-bit
|
||
/// <c>entity.Id</c> (retail <c>add_shadows_to_cells</c> / <c>CPartArray::AddPartsShadow</c>).
|
||
/// </summary>
|
||
public class ShadowRegistrationOverflowTests
|
||
{
|
||
// Two real stair-entity ids from the #185 capture that share the low 24 bits
|
||
// (0xF68221) but differ in the class-prefix byte.
|
||
private const uint EntityA = 0x40F68221u;
|
||
private const uint EntityB = 0xC0F68221u;
|
||
|
||
private const uint LbId = 0xF6820000u;
|
||
private const float OffX = 0f, OffY = 0f;
|
||
|
||
// ── The root cause, as pure arithmetic ────────────────────────────────
|
||
|
||
[Fact]
|
||
public void OldPartIdScheme_OverflowsUint32_AndCollides()
|
||
{
|
||
// entity.Id * 256u == entity.Id << 8, truncated to 32 bits → the prefix
|
||
// byte falls off the top and the two distinct entities alias.
|
||
uint oldPartA = unchecked(EntityA * 256u); // 0x40F68221 << 8 → 0xF6822100
|
||
uint oldPartB = unchecked(EntityB * 256u); // 0xC0F68221 << 8 → 0xF6822100
|
||
Assert.Equal(0xF6822100u, oldPartA);
|
||
Assert.Equal(oldPartA, oldPartB); // COLLISION = the #185 bug
|
||
Assert.NotEqual(EntityA, EntityB); // …yet the entities ARE distinct
|
||
}
|
||
|
||
// ── The bug: old per-part Register loses one registration ─────────────
|
||
|
||
private static ShadowShape Cyl(Vector3 local) => new(
|
||
GfxObjId: 0u, LocalPosition: local, LocalRotation: Quaternion.Identity,
|
||
Scale: 1f, CollisionType: ShadowCollisionType.Cylinder, Radius: 1f, CylHeight: 2f);
|
||
|
||
[Fact]
|
||
public void OldPerPartRegister_CollidingIds_SecondSilentlyOverwritesFirst()
|
||
{
|
||
var reg = new ShadowObjectRegistry();
|
||
|
||
// Two DIFFERENT physical objects at DIFFERENT cells, registered the OLD way
|
||
// (Register with the synthetic overflowing part-id). EntityA at cell (0,0),
|
||
// EntityB at cell (1,0) — 30 m apart in X.
|
||
var posA = new Vector3(12f, 12f, 50f); // → cell LbId|1
|
||
var posB = new Vector3(42f, 12f, 50f); // → cell LbId|9
|
||
|
||
reg.Register(unchecked(EntityA * 256u), 0u, posA, Quaternion.Identity, 1f,
|
||
OffX, OffY, LbId, ShadowCollisionType.Cylinder, 2f);
|
||
reg.Register(unchecked(EntityB * 256u), 0u, posB, Quaternion.Identity, 1f,
|
||
OffX, OffY, LbId, ShadowCollisionType.Cylinder, 2f);
|
||
|
||
// EntityA's registration is GONE (its part-id was reused by EntityB): its
|
||
// cell is empty. This is exactly the missing stair-step collision.
|
||
Assert.Empty(reg.GetObjectsInCell(LbId | 1u));
|
||
Assert.NotEmpty(reg.GetObjectsInCell(LbId | 9u));
|
||
Assert.Equal(1, reg.TotalRegistered); // one silently lost
|
||
}
|
||
|
||
// ── The fix: RegisterMultiPart keys on the unique entity.Id ───────────
|
||
|
||
[Fact]
|
||
public void RegisterMultiPart_CollidingLowBitsIds_BothSurvive()
|
||
{
|
||
var reg = new ShadowObjectRegistry();
|
||
|
||
var posA = new Vector3(12f, 12f, 50f); // → cell LbId|1
|
||
var posB = new Vector3(42f, 12f, 50f); // → cell LbId|9
|
||
|
||
reg.RegisterMultiPart(EntityA, posA, Quaternion.Identity,
|
||
new[] { Cyl(Vector3.Zero) }, 0u, EntityCollisionFlags.None,
|
||
OffX, OffY, LbId, isStatic: true);
|
||
reg.RegisterMultiPart(EntityB, posB, Quaternion.Identity,
|
||
new[] { Cyl(Vector3.Zero) }, 0u, EntityCollisionFlags.None,
|
||
OffX, OffY, LbId, isStatic: true);
|
||
|
||
// Both distinct entities survive at their own cells — no overflow collision.
|
||
Assert.Contains(reg.GetObjectsInCell(LbId | 1u), e => e.EntityId == EntityA);
|
||
Assert.Contains(reg.GetObjectsInCell(LbId | 9u), e => e.EntityId == EntityB);
|
||
Assert.Equal(2, reg.TotalRegistered);
|
||
}
|
||
|
||
// ── The builder: one BSP shape per BSP part; shells + no-BSP excluded ──
|
||
|
||
/// <summary>
|
||
/// Graph-form fixture. The bounding sphere is OFF-CENTRE by default
|
||
/// because that is the DAT-real case — a GfxObj's physics BSP is authored
|
||
/// in the GfxObj's own coordinates and 376 of the 973 installed
|
||
/// physics-BSP parts have a root sphere further from the part origin than
|
||
/// half their radius.
|
||
/// </summary>
|
||
private static GfxObjPhysics BspGfx(float radius, float centerZ = 0.75f)
|
||
{
|
||
var leaf = new PhysicsBSPNode { Type = BSPNodeType.Leaf };
|
||
return new GfxObjPhysics
|
||
{
|
||
BSP = new PhysicsBSPTree { Root = leaf },
|
||
BoundingSphere = new Sphere
|
||
{ Origin = new Vector3(0f, 0f, centerZ), Radius = radius },
|
||
Resolved = new Dictionary<ushort, ResolvedPolygon>(),
|
||
PhysicsPolygons = new Dictionary<ushort, Polygon>(),
|
||
Vertices = new VertexArray(),
|
||
};
|
||
}
|
||
|
||
/// <summary>Flat-form fixture — the production storage since I6/I7.</summary>
|
||
private static GfxObjPhysics FlatBspGfx(float radius, float centerZ)
|
||
{
|
||
var node = new FlatPhysicsBspNode(
|
||
BSPNodeType.Leaf, default, -1, -1, 0, 0,
|
||
new FlatCollisionSphere(new Vector3(0f, 0f, centerZ), radius),
|
||
new FlatIndexRange(0, 0));
|
||
GfxObjPhysics phys = BspGfx(radius, centerZ);
|
||
phys.FlatPhysicsBsp = new FlatPhysicsBsp(
|
||
0,
|
||
ImmutableArray.Create(node),
|
||
ImmutableArray<int>.Empty,
|
||
FlatPolygonTable.Empty);
|
||
return phys;
|
||
}
|
||
|
||
[Fact]
|
||
public void FromLandblockBspParts_OneShapePerBspPart_LocalTransformPreserved()
|
||
{
|
||
var meshRefs = new[]
|
||
{
|
||
new MeshRef(0x01000AC5u, Matrix4x4.CreateTranslation(0f, 0.5f, 0.4f)),
|
||
new MeshRef(0x01000AC5u, Matrix4x4.CreateTranslation(0f, 1.0f, 0.8f)),
|
||
new MeshRef(0x0BADBADu, Matrix4x4.Identity), // no physics BSP → skipped
|
||
};
|
||
|
||
var shapes = ShadowShapeBuilder.FromLandblockBspParts(
|
||
meshRefs, isBuildingShell: false,
|
||
getGfxObj: id => id == 0x01000AC5u ? BspGfx(1.05f) : null);
|
||
|
||
Assert.Equal(2, shapes.Count); // only the two BSP-bearing parts
|
||
Assert.All(shapes, s => Assert.Equal(ShadowCollisionType.BSP, s.CollisionType));
|
||
Assert.All(shapes, s => Assert.Equal(0x01000AC5u, s.GfxObjId));
|
||
// Local part offsets survive (decomposed from PartTransform).
|
||
Assert.Contains(shapes, s => Vector3.Distance(s.LocalPosition, new Vector3(0f, 0.5f, 0.4f)) < 1e-4f);
|
||
Assert.Contains(shapes, s => Vector3.Distance(s.LocalPosition, new Vector3(0f, 1.0f, 0.8f)) < 1e-4f);
|
||
// Radius = local BoundingSphere radius × part scale (1.0).
|
||
Assert.All(shapes, s => Assert.Equal(1.05f, s.Radius, 3));
|
||
}
|
||
|
||
/// <summary>
|
||
/// AP-156, landblock half. A landblock-baked multi-part entity is the
|
||
/// same <c>CPartArray</c> walk as a live Setup
|
||
/// (<c>CPhysicsObj::find_bbox_cell_list</c> @0x00510fc0 →
|
||
/// <c>CPartArray::calc_cross_cells_static</c> @0x00518160 →
|
||
/// <c>CEnvCell::find_transit_cells</c> @0x0052cae0), which transforms each
|
||
/// part's <c>CGfxObj::physics_sphere</c> CENTRE through the part's own
|
||
/// Position before reading its radius. Carrying the radius alone puts a
|
||
/// stair run's or fence's flood sphere at the part origin instead of on
|
||
/// its geometry. Both storage forms — the flat BSP that production uses
|
||
/// and the graph fallback — must supply the centre, SCALED with the part.
|
||
/// </summary>
|
||
[Fact]
|
||
public void FromLandblockBspParts_CarriesTheScaledRootSphereCentre()
|
||
{
|
||
Matrix4x4 halfScale = Matrix4x4.CreateScale(0.5f)
|
||
* Matrix4x4.CreateTranslation(0f, 2f, 0f);
|
||
|
||
var flat = ShadowShapeBuilder.FromLandblockBspParts(
|
||
[new MeshRef(0x01000AC5u, halfScale)],
|
||
isBuildingShell: false,
|
||
getGfxObj: _ => FlatBspGfx(4f, centerZ: 3f));
|
||
ShadowShape flatShape = Assert.Single(flat);
|
||
Assert.Equal(0.5f, flatShape.Scale, 3);
|
||
Assert.Equal(2f, flatShape.Radius, 3); // 4 * 0.5
|
||
Assert.Equal(new Vector3(0f, 0f, 1.5f), flatShape.BoundsCenter); // 3 * 0.5
|
||
|
||
// Graph fallback (fixtures without a flat BSP) takes the same path.
|
||
var graph = ShadowShapeBuilder.FromLandblockBspParts(
|
||
[new MeshRef(0x01000AC5u, halfScale)],
|
||
isBuildingShell: false,
|
||
getGfxObj: _ => BspGfx(4f, centerZ: 3f));
|
||
ShadowShape graphShape = Assert.Single(graph);
|
||
Assert.Equal(new Vector3(0f, 0f, 1.5f), graphShape.BoundsCenter);
|
||
}
|
||
|
||
[Fact]
|
||
public void FromLandblockBspParts_BuildingShell_ReturnsEmpty()
|
||
{
|
||
var meshRefs = new[] { new MeshRef(0x01000AC5u, Matrix4x4.Identity) };
|
||
var shapes = ShadowShapeBuilder.FromLandblockBspParts(
|
||
meshRefs, isBuildingShell: true, getGfxObj: _ => BspGfx(1f));
|
||
Assert.Empty(shapes); // building shells collide via the building channel
|
||
}
|
||
}
|