acdream had never implemented retail's SECOND cell-membership algorithm.
CPhysicsObj::calc_cross_cells @0x00515230 tests HAS_PHYSICS_BSP_PS at
0x00515285 and jumps (0x0051528f jne 0x515305) to find_bbox_cell_list
@0x00510fc0 for a BSP-bearing object; everything below that jump is the
OTHER algorithm, CObjCell::find_cell_list, and that is all we had. Every
object, BSP-bearing or not, was routed through it.
That path's outdoor expansion is a HARD CAP of one cell in each direction.
CellTransit.AddAllOutsideCells computes minRad = radius, maxRad = 24 - radius
and adds at most the eight neighbours of the sphere's own cell, so for any
radius >= 12 m both boundary tests are unconditionally true and the result is
exactly 3x3. Widening the radius or adding a second sphere is mechanically
incapable of adding a tenth cell. The user's live probe measured the
consequence directly: standing inside a Neftet formation, inCell=2 exempt=2
reached=0 -- the geometry was not a candidate at all.
The port. AddAllOutsideCellsFromParts is CLandCell::add_all_outside_cells
@0x00533360 plus add_cell_block @0x005331d0: base landcell from the FIRST
part's own adjust_to_outside, baseX/baseY within-block, each part's authored
CGfxObj::gfx_bound_box re-fit through all eight corners
(BBox::LocalToGlobal @0x005b2120), floor(v / square_length) where
square_length = 0x7c920c = 24.0f, four accumulators seeded to zero, ONE
rectangle unioned across all parts, FILLED, in GLOBAL lcoords so it crosses
landblocks freely, clamped only to [0, 0x7f8).
BuildShadowCellSetFromParts is find_bbox_cell_list's worklist.
RegisterMultiPart dispatches on the same flag retail does, and
BuildFloodSpheres' BSP arm is deleted rather than left unreachable.
Disassembled from the PDB-paired 2013-09-06 binary, not read from Binary
Ninja: BN mis-renders four separate constructs inside add_all_outside_cells
alone -- a dropped `and eax,0xffff` on baseX, a neg/sbb/and select shown as
identically zero, a wrong get_landcell argument, and both x87 flag tests as
`unimplemented {test ah}`.
ShadowPartGeometry pairs the BSP root sphere with the authored box so no
resolver can answer one and leave the other call site to synthesize a
substitute -- the AP-156 invariant applied a second time, since that split is
what produced AP-156 and then this. The box comes from
FlatGfxObjVisualBounds, already computed by exactly CGfxObj::init_end's
algorithm and already in the prepared package: no bake change, no DAT re-read.
Cost, measured over the installed DATs before any code was written: 1,258
physics-BSP GfxObjs, cells/object p50 4, p90 4, p99 12, max 49. The port is
CHEAPER than the old 3x3 = 9 for 98.97% of them. Row totals (shapes x cells)
over all 1,031 landblocks with BSP owners fall 97,173 -> 15,607 (0.161x);
dense Arwic 0xC6A9 falls 342 -> 43. One landblock more than doubles.
Precondition confirmed before pinning any expected cell set: 0x010046D8's box
is 96 m x 96 m about cell (2,2) = 0x87640013, which independently corroborates
the 3x3-centred-there diagnosis, and its rectangle does contain 0x87640011 and
0x87640019 -- the two cells the probe measured empty.
Register: AP-156's outdoor half CLOSED and its risk column CORRECTED (it read
"extra broadphase candidates, never a missed one", which generalised the indoor
direction to the whole row and is why #334 sat inside it unnoticed). AP-159 +
issue #335 file the unported indoor arm; AD-49 records the seed-time rectangle.
Issue #336 files a fourth load-sensitive test flake seen once during the gate.
Ten tests, every one sabotage-verified in both directions across eight
mutations (dispatch, 8-corner refit, floor-vs-truncation, union-vs-per-part,
map clamp, adjust guard, landblock clamp, box-path-for-everything). The
strongest is an installed-DAT replay of the user's own probe evidence.
Suite 11,208 -> 11,218 passed / 4 skipped / 0 failed; the +10 is exactly the
new tests.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
242 lines
9.6 KiB
C#
242 lines
9.6 KiB
C#
using System.Numerics;
|
|
using AcDream.App.World;
|
|
using AcDream.Core.Items;
|
|
using AcDream.Core.Net;
|
|
using AcDream.Core.Physics;
|
|
using AcDream.Core.World;
|
|
using DatReaderWriter.DBObjs;
|
|
using DatReaderWriter.Types;
|
|
|
|
namespace AcDream.App.Physics;
|
|
|
|
/// <summary>
|
|
/// Immutable input to the canonical shadow registry for one live object.
|
|
/// Building is DAT/physics-only and therefore testable without a window or GL
|
|
/// context; registration remains one explicit commit.
|
|
/// </summary>
|
|
internal sealed record LiveEntityCollisionRegistration(
|
|
uint EntityId,
|
|
uint SourceId,
|
|
Vector3 EntityWorldPosition,
|
|
Quaternion EntityWorldRotation,
|
|
IReadOnlyList<ShadowShape> Shapes,
|
|
uint State,
|
|
EntityCollisionFlags Flags,
|
|
float WorldOffsetX,
|
|
float WorldOffsetY,
|
|
uint LandblockId,
|
|
uint SeedCellId);
|
|
|
|
/// <summary>
|
|
/// Ports the live-object collision-shape policy used by
|
|
/// <c>CPhysicsObj::FindObjCollisions</c> (0x0050f050), which DISPATCHES
|
|
/// rather than unions: every physics-BSP part, ELSE the Setup's CylSpheres,
|
|
/// ELSE its Spheres, else nothing (AP-152). The gate itself lives in
|
|
/// <see cref="ShadowShapeBuilder.FromSetup"/>.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A Setup that yields no shape produces no registration. Retail synthesizes
|
|
/// nothing for a shapeless object: <c>CPhysicsObj::FindObjCollisions</c>
|
|
/// (0x0050f050) walks CylSpheres or Spheres or the physics BSP, and when
|
|
/// <c>CPartArray::GetNumSphere</c> returns zero it branches straight to the
|
|
/// epilogue (<c>0x0050f22f je 0x50f31b</c>) returning the seeded
|
|
/// <c>OK_TS</c>. <c>CPartArray::GetRadius</c> (0x005180a0) and
|
|
/// <c>GetHeight</c> (0x005180b0) are absent from that function's entire call
|
|
/// set — <c>Setup.Radius</c>/<c>Height</c> serve attack cones,
|
|
/// <c>cylinder_distance</c>, and MoveTo, never collision geometry.
|
|
/// </remarks>
|
|
internal sealed class LiveEntityCollisionBuilder
|
|
{
|
|
private readonly Func<uint, ShadowPartGeometry?> _physicsBspBounds;
|
|
/// <summary>
|
|
/// The dispatch gate, derived from <see cref="_physicsBspBounds"/> so the
|
|
/// two can never disagree (AP-156), and cached once so <see cref="Build"/>
|
|
/// allocates no closure per call — Slice I1's 0 B/resolve budget.
|
|
/// </summary>
|
|
private readonly Func<uint, bool> _hasPhysicsBsp;
|
|
private readonly LiveEntityDefaultPoseResolver _defaultPose;
|
|
|
|
public LiveEntityCollisionBuilder(
|
|
PhysicsDataCache physicsData,
|
|
LiveEntityDefaultPoseResolver defaultPose)
|
|
: this(
|
|
id =>
|
|
{
|
|
FlatGfxObjCollisionAsset? asset = physicsData.GetFlatGfxObj(id);
|
|
FlatPhysicsBsp? flat = asset?.PhysicsBsp;
|
|
return flat is { RootIndex: >= 0 }
|
|
? ShadowPartGeometry.Create(
|
|
flat.Nodes[flat.RootIndex].BoundingSphere,
|
|
asset!.VisualBounds)
|
|
: (ShadowPartGeometry?)null;
|
|
},
|
|
defaultPose)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(physicsData);
|
|
}
|
|
|
|
/// <param name="physicsBspBounds">The part GfxObj's physics-BSP root
|
|
/// bounding sphere AND its authored vertex-array box, or null when it has
|
|
/// no physics BSP. ONE resolver answers every question the builder asks —
|
|
/// "does this part dispatch as BSP?", "where and how big is its flood
|
|
/// sphere?", and "what is its outdoor extent?" — so the dispatch gate and
|
|
/// the emitted geometry cannot disagree, the sphere's radius cannot be
|
|
/// carried while its origin is dropped (AP-156), and the outdoor extent
|
|
/// walk cannot be left without a box (#334).</param>
|
|
internal LiveEntityCollisionBuilder(
|
|
Func<uint, ShadowPartGeometry?> physicsBspBounds,
|
|
LiveEntityDefaultPoseResolver defaultPose)
|
|
{
|
|
_physicsBspBounds = physicsBspBounds
|
|
?? throw new ArgumentNullException(nameof(physicsBspBounds));
|
|
_hasPhysicsBsp = id => _physicsBspBounds(id) is not null;
|
|
_defaultPose = defaultPose
|
|
?? throw new ArgumentNullException(nameof(defaultPose));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resolves collision slot zero for every post-AnimPartChanged identity.
|
|
/// This intentionally has no visual-LOD option or Setup-type input.
|
|
/// </summary>
|
|
public static uint[] ResolveEffectivePartIdentities(
|
|
IReadOnlyList<uint> postAnimPartGfxObjIds,
|
|
Func<uint, uint> resolveSlotZero)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(postAnimPartGfxObjIds);
|
|
ArgumentNullException.ThrowIfNull(resolveSlotZero);
|
|
var result = new uint[postAnimPartGfxObjIds.Count];
|
|
for (int i = 0; i < result.Length; i++)
|
|
result[i] = resolveSlotZero(postAnimPartGfxObjIds[i]);
|
|
return result;
|
|
}
|
|
|
|
public LiveEntityCollisionRegistration? Build(
|
|
WorldEntity entity,
|
|
Setup setup,
|
|
IReadOnlyList<uint> effectivePartGfxObjIds,
|
|
WorldSession.EntitySpawn spawn,
|
|
LiveEntityRecord exactRecord,
|
|
Vector3 worldOrigin,
|
|
bool retainEmptyPayload = false)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(entity);
|
|
ArgumentNullException.ThrowIfNull(setup);
|
|
ArgumentNullException.ThrowIfNull(effectivePartGfxObjIds);
|
|
ArgumentNullException.ThrowIfNull(exactRecord);
|
|
if (spawn.Position is not { } position)
|
|
return null;
|
|
if (spawn.Guid != exactRecord.ServerGuid
|
|
|| spawn.InstanceSequence != exactRecord.Generation
|
|
|| !ReferenceEquals(exactRecord.WorldEntity, entity))
|
|
{
|
|
throw new InvalidOperationException(
|
|
"Live collision construction requires the exact materialized record.");
|
|
}
|
|
|
|
float scale = spawn.ObjScale ?? 1f;
|
|
IReadOnlyList<Frame>? defaultPose = _defaultPose.Resolve(
|
|
spawn.MotionTableId ?? 0u,
|
|
setup.Parts.Count);
|
|
// One resolver drives both the dispatch gate and the emitted BSP
|
|
// geometry, and FromSetup applies the entity scale to radius and
|
|
// center alike — there is no downstream substitution that could take
|
|
// one and drop the other.
|
|
IReadOnlyList<ShadowShape> shapes = ShadowShapeBuilder.FromSetup(
|
|
setup,
|
|
scale,
|
|
_hasPhysicsBsp,
|
|
partPoseOverride: defaultPose,
|
|
effectivePartGfxObjIds: effectivePartGfxObjIds,
|
|
physicsBspBounds: _physicsBspBounds);
|
|
|
|
if (shapes.Count == 0 && !retainEmptyPayload)
|
|
return null;
|
|
|
|
EntityCollisionFlags flags = EntityCollisionFlags.HasWeenie;
|
|
if (spawn.ObjectDescriptionFlags is { } descriptionFlags)
|
|
flags |= EntityCollisionFlagsExt.FromPwdBitfield(descriptionFlags);
|
|
if (spawn.ItemType == (uint)ItemType.Creature)
|
|
flags |= EntityCollisionFlags.IsCreature;
|
|
|
|
return new LiveEntityCollisionRegistration(
|
|
entity.Id,
|
|
entity.SourceGfxObjOrSetupId,
|
|
entity.Position,
|
|
entity.Rotation,
|
|
shapes,
|
|
(uint)exactRecord.FinalPhysicsState,
|
|
flags,
|
|
worldOrigin.X,
|
|
worldOrigin.Y,
|
|
position.LandblockId,
|
|
position.LandblockId);
|
|
}
|
|
|
|
public static void Register(
|
|
ShadowObjectRegistry registry,
|
|
LiveEntityCollisionRegistration registration)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(registry);
|
|
ArgumentNullException.ThrowIfNull(registration);
|
|
registry.RegisterMultiPart(
|
|
registration.EntityId,
|
|
registration.EntityWorldPosition,
|
|
registration.EntityWorldRotation,
|
|
registration.Shapes,
|
|
registration.State,
|
|
registration.Flags,
|
|
registration.WorldOffsetX,
|
|
registration.WorldOffsetY,
|
|
registration.LandblockId,
|
|
registration.SeedCellId,
|
|
isStatic: false);
|
|
|
|
if (!PhysicsDiagnostics.ProbeBuildingEnabled)
|
|
return;
|
|
|
|
int cylinders = 0;
|
|
int bsps = 0;
|
|
foreach (ShadowShape shape in registration.Shapes)
|
|
{
|
|
if (shape.CollisionType == ShadowCollisionType.Cylinder)
|
|
cylinders++;
|
|
else
|
|
bsps++;
|
|
}
|
|
Console.WriteLine(FormattableString.Invariant(
|
|
$"[entity-source] id=0x{registration.EntityId:X8} entityId=0x{registration.EntityId:X8} src=0x{registration.SourceId:X8} gfxObj=0x{registration.SourceId:X8} lb=0x{registration.LandblockId:X8} shapes=cyl{cylinders}+bsp{bsps} note=server-spawn-root state=0x{registration.State:X8} flags={registration.Flags}"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Commits a retail ObjDesc part-array replacement. A shapeless result
|
|
/// removes the prior payload; a shaped result replaces it without making
|
|
/// a Hidden, parented, or cell-less object collide in world cells.
|
|
/// </summary>
|
|
public static void ReconcileAppearance(
|
|
ShadowObjectRegistry registry,
|
|
uint entityId,
|
|
LiveEntityCollisionRegistration? registration,
|
|
bool suspendIfNew)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(registry);
|
|
if (registration is null)
|
|
return;
|
|
|
|
if (registration.EntityId != entityId)
|
|
throw new InvalidOperationException("Collision replacement belongs to a different live entity.");
|
|
|
|
registry.ReplaceMultiPartPayload(
|
|
registration.EntityId,
|
|
registration.EntityWorldPosition,
|
|
registration.EntityWorldRotation,
|
|
registration.Shapes,
|
|
registration.State,
|
|
registration.Flags,
|
|
registration.WorldOffsetX,
|
|
registration.WorldOffsetY,
|
|
registration.LandblockId,
|
|
registration.SeedCellId,
|
|
isStatic: false,
|
|
suspendIfNew);
|
|
}
|
|
}
|