acdream/tests/AcDream.Core.Tests/Physics/CollisionExemptionTests.cs
Erik 3361a8d776 fix(physics): #137 Task 3 — port obstruction_ethereal verbatim; retire AD-7 shim
Retail CPhysicsObj::FindObjCollisions (0x0050f050) only instant-skips when
BOTH ETHEREAL_PS (0x4) AND IGNORE_COLLISIONS_PS (0x10) are set (pc:276782).
ETHEREAL-alone sets sphere_path.obstruction_ethereal=1 (pc:276806) and
continues to the shape dispatch. BSPTREE::find_collisions (0x0053a496) routes
Path 1 (sphere_intersects_solid) when the flag is set (pc:323742): the open
door has no solid leaf at the doorway, so the test returns OK → player passes
through. CEnvCell::find_env_collisions (0x0052c144) clears the flag first so
ENV walls are never weakened (pc:309580, "D5 clear").

Changes:
- CollisionExemption.ShouldSkip: require BOTH bits for Gate-1 early-out
  (previously ETHEREAL alone returned true — the AD-7 shim). Divergence
  register row AD-7 deleted.
- SpherePath: add ObstructionEthereal field (mirrors retail
  SPHEREPATH.obstruction_ethereal).
- FindObjCollisionsInternal loop: set sp.ObstructionEthereal=(target&0x4)!=0
  before shape dispatch; clear it after (per-object clear pc:276989).
  Also clear at the null-BSP continue site to keep flag clean.
- FindEnvCollisions: clear sp.ObstructionEthereal=false at top (D5 clear
  pc:309580) — ENV cell walls are always solid.
- BSPQuery.FindCollisions Path 1: change `obj.Ethereal` (ObjectInfo.Ethereal,
  always false — dead code) to `path.ObstructionEthereal`. Gate now correctly
  mirrors retail pc:323742: PLACEMENT_INSERT || obstruction_ethereal.

Consume site change (BSPQuery.cs before/after):
  BEFORE: if (path.InsertType == InsertType.Placement || obj.Ethereal)
  AFTER:  if (path.InsertType == InsertType.Placement || path.ObstructionEthereal)
Mirrors retail pc:323742 exactly. obj.Ethereal was dead code (ObjectInfo.Ethereal
is never set true anywhere); the correct flag is SpherePath.ObstructionEthereal.

Tests: 1580 pass / 0 fail / 2 skip (was 1576/0/2 + 4 new in ObstructionEtherealTests.
CellarUp, CornerFlood, DoorCollision, HouseExitWalk all green — no wall regressions.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-24 19:20:02 +02:00

173 lines
7 KiB
C#

using AcDream.Core.Physics;
using Xunit;
namespace AcDream.Core.Tests.Physics;
/// <summary>
/// Unit tests for <see cref="CollisionExemption"/> — Commit C of the
/// 2026-04-29 live-entity collision port. Covers retail's
/// <c>CPhysicsObj::FindObjCollisions</c> exemption block, ported
/// line-for-line from
/// <c>docs/research/named-retail/acclient_2013_pseudo_c.txt:276782-276839,276971</c>.
///
/// <para>
/// Behaviour matrix (target / mover columns):
/// </para>
/// <list type="table">
/// <listheader><term>Mover</term><term>Target</term><description>Skip?</description></listheader>
/// <item><term>any</term><term>ETHEREAL+IGNORE_COLLISIONS</term><description>YES (early-out)</description></item>
/// <item><term>IsViewer</term><term>IsCreature</term><description>YES (camera ray-test passes through)</description></item>
/// <item><term>IGNORE_CREATURES</term><term>IsCreature</term><description>YES (mover walks through creatures)</description></item>
/// <item><term>IsPlayer</term><term>IsPlayer (no PK)</term><description>YES (non-PK pair walks through)</description></item>
/// <item><term>IsPlayer + IsPK</term><term>IsPlayer + IsPK</term><description>NO (PK pair collides)</description></item>
/// <item><term>IsPlayer + IsPKLite</term><term>IsPlayer + IsPKLite</term><description>NO (PKLite pair collides)</description></item>
/// <item><term>IsPlayer</term><term>IsPlayer + IsImpenetrable</term><description>NO (Impenetrable target always collides)</description></item>
/// <item><term>IsPlayer + IsPK</term><term>IsPlayer (no PK)</term><description>YES (mismatched PK skip)</description></item>
/// <item><term>IsPlayer</term><term>IsCreature (NPC)</term><description>NO (player vs NPC always collides)</description></item>
/// </list>
/// </summary>
public class CollisionExemptionTests
{
private const uint ETHEREAL_PS = 0x4u;
private const uint IGNORE_COLLISIONS_PS = 0x10u;
[Fact]
public void EtherealAndIgnoreCollisions_AlwaysSkipped()
{
// Target with both bits set is exempted from any mover.
Assert.True(CollisionExemption.ShouldSkip(
targetState: ETHEREAL_PS | IGNORE_COLLISIONS_PS,
targetFlags: EntityCollisionFlags.None,
moverState: ObjectInfoState.IsPlayer));
}
[Fact]
public void EtherealOnly_NotInstantSkipped()
{
// Task 3 (2026-06-24): AD-7 shim retired. ETHEREAL alone (0x4) no
// longer instant-skips — retail requires BOTH ETHEREAL_PS (0x4) AND
// IGNORE_COLLISIONS_PS (0x10) for the Gate-1 early-out.
// ETHEREAL-alone takes the obstruction_ethereal path: ShouldSkip
// returns false → sp.ObstructionEthereal = true → BSP Path 1 fires
// (sphere_intersects_solid) → open door is passable because its BSP
// has no solid leaf at the opening. Retail pc:276782 / 0x0050f067.
Assert.False(CollisionExemption.ShouldSkip(
targetState: ETHEREAL_PS,
targetFlags: EntityCollisionFlags.None,
moverState: ObjectInfoState.IsPlayer));
}
[Fact]
public void Viewer_VsCreature_Skipped()
{
// Camera-ray viewer transitions through creatures.
Assert.True(CollisionExemption.ShouldSkip(
targetState: 0u,
targetFlags: EntityCollisionFlags.IsCreature,
moverState: ObjectInfoState.IsViewer));
}
[Fact]
public void Viewer_VsNonCreature_NotSkipped()
{
Assert.False(CollisionExemption.ShouldSkip(
targetState: 0u,
targetFlags: EntityCollisionFlags.None,
moverState: ObjectInfoState.IsViewer));
}
[Fact]
public void IgnoreCreatures_VsCreature_Skipped()
{
// Per acclient_2013_pseudo_c.txt:276971 — an arrow with
// IGNORE_CREATURES doesn't get blocked by the very monster it's
// tracking towards (until missile_ignore filters its target).
Assert.True(CollisionExemption.ShouldSkip(
targetState: 0u,
targetFlags: EntityCollisionFlags.IsCreature,
moverState: ObjectInfoState.IgnoreCreatures));
}
[Fact]
public void NonPkPlayer_VsNonPkPlayer_Skipped()
{
// The user-visible payoff: two ordinary players walk through each
// other instead of blocking.
Assert.True(CollisionExemption.ShouldSkip(
targetState: 0u,
targetFlags: EntityCollisionFlags.IsPlayer,
moverState: ObjectInfoState.IsPlayer));
}
[Fact]
public void Pk_VsPk_NotSkipped()
{
// Two PK players collide.
Assert.False(CollisionExemption.ShouldSkip(
targetState: 0u,
targetFlags: EntityCollisionFlags.IsPlayer | EntityCollisionFlags.IsPK,
moverState: ObjectInfoState.IsPlayer | ObjectInfoState.IsPK));
}
[Fact]
public void PkLite_VsPkLite_NotSkipped()
{
Assert.False(CollisionExemption.ShouldSkip(
targetState: 0u,
targetFlags: EntityCollisionFlags.IsPlayer | EntityCollisionFlags.IsPKLite,
moverState: ObjectInfoState.IsPlayer | ObjectInfoState.IsPKLite));
}
[Fact]
public void Pk_VsNonPk_Skipped()
{
// Mismatched PK status: still exempt — only matching pair collides.
Assert.True(CollisionExemption.ShouldSkip(
targetState: 0u,
targetFlags: EntityCollisionFlags.IsPlayer,
moverState: ObjectInfoState.IsPlayer | ObjectInfoState.IsPK));
}
[Fact]
public void Pk_VsPkLite_Skipped()
{
// PK and PKLite are different pools — pair doesn't match.
Assert.True(CollisionExemption.ShouldSkip(
targetState: 0u,
targetFlags: EntityCollisionFlags.IsPlayer | EntityCollisionFlags.IsPKLite,
moverState: ObjectInfoState.IsPlayer | ObjectInfoState.IsPK));
}
[Fact]
public void ImpenetrableTarget_VsAnyPlayer_NotSkipped()
{
// Impenetrable target ("Free" PK status) always collides with
// any player mover — regardless of mover's PK state.
Assert.False(CollisionExemption.ShouldSkip(
targetState: 0u,
targetFlags: EntityCollisionFlags.IsPlayer | EntityCollisionFlags.IsImpenetrable,
moverState: ObjectInfoState.IsPlayer));
}
[Fact]
public void Player_VsCreature_NotSkipped()
{
// PvP exemption only applies player-on-player. Player vs creature
// (NPC, monster) is the normal blocking case.
Assert.False(CollisionExemption.ShouldSkip(
targetState: 0u,
targetFlags: EntityCollisionFlags.IsCreature,
moverState: ObjectInfoState.IsPlayer));
}
[Fact]
public void NonPlayerMover_VsPlayer_NotSkipped()
{
// PvP rule requires BOTH to be players. Mover is not a player
// (e.g., dead-reckoned remote NPC) → no exemption applies.
Assert.False(CollisionExemption.ShouldSkip(
targetState: 0u,
targetFlags: EntityCollisionFlags.IsPlayer,
moverState: ObjectInfoState.None));
}
}