fix: hit a wall lying along a sight ray so it casts its line's shadow (#26)

visibilityPolygon casts one ray per 64-gon vertex and ang, ang+-RayEps per wall
endpoint. A wall radial to the viewer is collinear with the ray that crosses it,
and rayHit rejected the collinear case; its own +-RayEps rays meet the wall's
line at the viewer (t = 0, rejected by t > Eps). So the wall got no ray, no
vertex and no shadow, and a token on the line beyond it stayed inside the
polygon: a player-view fog leak (DESIGN 3.1).

Fix: rayHit returns the nearest point of the overlap when the wall lies on the
ray's line (and Inf only for a parallel wall on another line). {.inline.} keeps
the fan's inner loop at its previous cost (CPU-time cross-check on 20k dense
polygons: 25.8s before, 32.5s without the pragma, 24.9s with it).

Proof: tgeometry visibility tests (horizontal k=0, vertical k=16, both wall
directions, a 32 px grid translation, 10 degrees, plus the non-radial and
0.5 px-off negative controls); the leak world gains a wall on the subject's own
column with a hidden token beyond it, and leakcheck's HIDDEN_TOKEN class now
asserts the token-level over-approximation (token centre + viewer->token
segment, 5.4 tolerance), which the GEOMETRY class structurally cannot see.
This commit is contained in:
2026-11-20 22:17:45 +01:00
parent a11782f00d
commit 414bb0326c
7 changed files with 169 additions and 6 deletions
+15 -2
View File
@@ -6,6 +6,9 @@
## One-way sight (DESIGN 5.8, #58): `Wall.sightDir` names the blind side of `a -> b` (`left` = the `(-dy, dx)` normal)
## and `visibilityPolygon` blocks such a wall only for a viewer on that side; the other side casts rays past it.
## Movement never reads it: `moverules` gets the same wall and blocks both ways.
## Radial walls (#26): a wall lying along a sight ray (radial to the viewer) is hit by that ray at the
## nearest point of the overlap, so it contributes a vertex and the line beyond it is outside the
## polygon. Its own +-RayEps rays meet the wall's line at the viewer, so they never covered it.
## Known limitations (DESIGN 5.4 / 12):
## - boundary epsilon ambiguity: RayEps 1e-4 rad, inclusive tests; token on a vision boundary is ambiguous
## (leak checker and explored oracle use a 0.5 px neighbourhood).
@@ -67,11 +70,21 @@ proc blindTo*(w: Wall, o: Vec): bool =
of wdLeft: s >= -Eps
of wdRight: s <= Eps
proc rayHit(o, d: Vec, a, b: Vec): float =
proc rayHit(o, d: Vec, a, b: Vec): float {.inline.} =
## smallest t > Eps where o + t*d hits segment ab (end-inclusive), else Inf
let e = b - a
let den = cross(d, e)
if abs(den) < 1e-12: return Inf # parallel/collinear: grazing, other rays cover it
if abs(den) < 1e-12:
# Parallel. A wall on another line never meets the ray. A wall *on* the ray's line (radial to the
# viewer, #26) is hit at the nearest point of the overlap: skipping the collinear case left such a
# wall without a vertex and without a shadow, so the line beyond it stayed inside the polygon (a
# player-view fog leak, DESIGN 3.1). Its own +-RayEps rays cannot cover it: they meet the wall's
# line at the viewer (t = 0) and `t > Eps` rejects them.
if abs(cross(a - o, d)) > Eps: return Inf
let ta = (a.x - o.x) * d.x + (a.y - o.y) * d.y
let tb = (b.x - o.x) * d.x + (b.y - o.y) * d.y
let t0 = min(ta, tb)
return (if t0 > Eps: t0 else: Inf) # viewer on or inside the wall: on-wall sight is ambiguous (5.5)
let ao = a - o
let t = cross(ao, e) / den
let u = cross(ao, d) / den