277 lines
14 KiB
Nim
277 lines
14 KiB
Nim
import std/[unittest, json, os]
|
|
import geometry
|
|
|
|
proc W(ax, ay, bx, by: float, door = false, open = false): Wall =
|
|
Wall(a: (ax, ay), b: (bx, by), door: door, open: open)
|
|
|
|
proc box(x0, y0, x1, y1: float): seq[Wall] =
|
|
@[W(x0, y0, x1, y0), W(x1, y0, x1, y1), W(x1, y1, x0, y1), W(x0, y1, x0, y0)]
|
|
|
|
proc sees(walls: openArray[Wall], o, p: Vec, r = 500.0): bool =
|
|
visibilityPolygon(o, walls, r).inside(p)
|
|
|
|
proc hasVertex(poly: openArray[Vec], p: Vec): bool =
|
|
for v in poly:
|
|
if abs(v.x - p.x) < 1e-6 and abs(v.y - p.y) < 1e-6: return true
|
|
|
|
suite "visibility":
|
|
test "open space is a circle":
|
|
let poly = visibilityPolygon((0.0, 0.0), @[], 100)
|
|
check poly.inside((60.0, 0.0))
|
|
check not poly.inside((101.0, 0.0))
|
|
check poly.inside((0.0, -95.0))
|
|
|
|
test "wall blocks":
|
|
let w = @[W(50, -50, 50, 50)]
|
|
check not sees(w, (0.0, 0.0), (80.0, 0.0))
|
|
check sees(w, (0.0, 0.0), (40.0, 0.0))
|
|
|
|
test "shared endpoint: no leak at the vertex":
|
|
# L corner at (50,0): vertical arm (50,-50)-(50,0), horizontal arm (50,0)-(100,0)
|
|
let w = @[W(50, -50, 50, 0), W(50, 0, 100, 0)]
|
|
# viewer on the diagonal through the shared vertex: the ray touches both arms' endpoint
|
|
let v: Vec = (0.0, -25.0)
|
|
check visibilityPolygon(v, w, 500).hasVertex((50.0, 0.0))
|
|
check not sees(w, v, (70.0, 9.0)) # just below the ray through the vertex (y=10 at x=70): vertical arm shadows it
|
|
check sees(w, v, (70.0, 11.0)) # just above the ray: past the vertex, the corner is not a crack
|
|
# token on the line of the horizontal arm: a point just above the line is visible past the arm's end-on
|
|
# edge, just below it is shadowed by the vertical arm
|
|
check sees(w, (0.0, 0.0), (70.0, 5.0))
|
|
check not sees(w, (0.0, 0.0), (70.0, -5.0))
|
|
|
|
test "shared vertex: both arms required (mutation d1/d2)":
|
|
# L corner at (50,0). The viewer's ray through the vertex splits the shadow: points above it are
|
|
# shadowed only by the vertical arm, points below it only by the horizontal arm. Removing either arm
|
|
# makes one assertion fail (tools/mutation.sh d1/d2 edit the SHARED-VERTEX line below).
|
|
let arm1 = W(50, -50, 50, 0) # vertical
|
|
let arm2 = W(50, 0, 100, 0) # horizontal
|
|
let w = @[arm1, arm2] # SHARED-VERTEX
|
|
let v: Vec = (0.0, 25.0) # ray v->(50,0) has slope -1/2: (70,-10) is on it
|
|
check visibilityPolygon(v, w, 500).hasVertex((50.0, 0.0))
|
|
check not sees(w, v, (70.0, -11.0)) # above the vertex ray: crosses x=50 at y<0, only arm1 stops it
|
|
check not sees(w, v, (70.0, -9.0)) # below the vertex ray: crosses y=0 at x>50, only arm2 stops it
|
|
check sees(w, v, (70.0, 9.0)) # control: well clear of both arms, not a blanket "blocked"
|
|
|
|
test "'<' apex: ray through the shared tip must not leak":
|
|
for flip in [false, true]: # each wall in both directions: the tip is an upper end of one and a lower end of the other
|
|
let w = if flip: @[W(100, 0, 50, -40), W(100, 0, 50, 40)] else: @[W(50, -40, 100, 0), W(50, 40, 100, 0)]
|
|
check visibilityPolygon((0.0, 0.0), w, 500).hasVertex((100.0, 0.0))
|
|
check not sees(w, (0.0, 0.0), (150.0, 0.0))
|
|
check not sees(w, (0.0, 0.0), (300.0, 0.0))
|
|
check sees(w, (0.0, 0.0), (90.0, 0.0))
|
|
|
|
test "every segment endpoint is a polygon vertex (lower and upper end inclusive)":
|
|
# free-standing wall, nothing else shares the endpoints: an exclusive end would drop the exact-endpoint ray
|
|
let w = @[W(50, -30, 80, 10)]
|
|
let poly = visibilityPolygon((0.0, 0.0), w, 500)
|
|
check poly.hasVertex((50.0, -30.0)) # lower end (u = 0)
|
|
check poly.hasVertex((80.0, 10.0)) # upper end (u = 1)
|
|
let r = @[W(80, 10, 50, -30)] # same wall, ends swapped
|
|
let pr = visibilityPolygon((0.0, 0.0), r, 500)
|
|
check pr.hasVertex((50.0, -30.0))
|
|
check pr.hasVertex((80.0, 10.0))
|
|
|
|
test "rays nudged past a corner: area just beyond a wall end is visible":
|
|
# viewer heights are chosen so the corner ray sits mid-way between two circle rays: no other ray can mask a missing nudge ray
|
|
# wall (50,-50)-(50,0) seen from (0,7.4): the ray through the end (50,0) continues to (100,-7.4)
|
|
let w = @[W(50, -50, 50, 0)]
|
|
check sees(w, (0.0, 7.4), (100.0, -6.0)) # lit side of that ray
|
|
check not sees(w, (0.0, 7.4), (100.0, -9.0)) # shadow side
|
|
# mirror: wall (50,0)-(50,50) seen from (0,-7.4)
|
|
let m = @[W(50, 0, 50, 50)]
|
|
check sees(m, (0.0, -7.4), (100.0, 6.0))
|
|
check not sees(m, (0.0, -7.4), (100.0, 9.0))
|
|
# box (pillar) seen from (0,-24): the rays through corners (100,50) and (100,-50) continue to (200,124) and (200,-76)
|
|
let b = box(100, -50, 150, 50)
|
|
check sees(b, (0.0, -24.0), (200.0, 130.0))
|
|
check not sees(b, (0.0, -24.0), (200.0, 118.0))
|
|
check sees(b, (0.0, -24.0), (200.0, -85.0))
|
|
check not sees(b, (0.0, -24.0), (200.0, -70.0))
|
|
|
|
test "shared endpoint: closed corner of box has no crack":
|
|
let w = box(-100, -100, 100, 100)
|
|
check not sees(w, (0.0, 0.0), (130.0, 130.0), 1000)
|
|
check not sees(w, (-90.0, -90.0), (-130.0, -130.0), 1000)
|
|
check sees(w, (0.0, 0.0), (95.0, 95.0), 1000) # the corner itself is visible from inside
|
|
check sees(w, (-90.0, -90.0), (90.0, 90.0), 1000)
|
|
|
|
test "collinear walls act as one":
|
|
let w = @[W(50, -50, 50, 0), W(50, 0, 50, 50)]
|
|
check not sees(w, (0.0, 0.0), (80.0, 0.0)) # ray through the join
|
|
check not sees(w, (0.0, 30.0), (80.0, 30.0))
|
|
check sees(w, (0.0, 0.0), (40.0, 0.0)) # in front of the join
|
|
check sees(w, (0.0, 0.0), (-80.0, 0.0)) # behind the viewer
|
|
check sees(w, (0.0, 0.0), (80.0, 100.0)) # past the far end of the line
|
|
check not sees(w, (0.0, 0.0), (80.0, -30.0))
|
|
|
|
test "collinear walls with a gap let light through":
|
|
let w = @[W(50, -50, 50, -5), W(50, 5, 50, 50)]
|
|
check sees(w, (0.0, 0.0), (80.0, 0.0))
|
|
check not sees(w, (0.0, 0.0), (80.0, 30.0))
|
|
|
|
test "T-junction: no leak and correct shadow":
|
|
# horizontal y=0 from x=-100..100, vertical stem from (0,0) up to (0,-100)
|
|
let w = @[W(-100, 0, 100, 0), W(0, 0, 0, -100)]
|
|
check not sees(w, (-30.0, -30.0), (30.0, -30.0)) # stem blocks
|
|
check not sees(w, (-30.0, -30.0), (-30.0, 30.0)) # base blocks
|
|
check sees(w, (-30.0, -30.0), (-80.0, -60.0))
|
|
check not sees(w, (-30.0, -30.0), (10.0, -5.0), 500)
|
|
check sees(w, (-30.0, -30.0), (-10.0, -5.0)) # same side of the stem, above the base
|
|
check sees(w, (30.0, -30.0), (80.0, -60.0)) # mirror side
|
|
check not sees(w, (30.0, -30.0), (-10.0, -5.0))
|
|
|
|
test "pillar mid-room casts a shadow, sides visible":
|
|
var w = box(-200, -200, 200, 200)
|
|
w.add box(-10, -10, 10, 10)
|
|
check not sees(w, (-100.0, 0.0), (100.0, 0.0))
|
|
check sees(w, (-100.0, 0.0), (100.0, 50.0))
|
|
check sees(w, (-100.0, 0.0), (0.0, 50.0))
|
|
check not sees(w, (-100.0, 0.0), (50.0, 3.0))
|
|
|
|
test "closed door blocks, open door passes":
|
|
let closed = @[W(50, -50, 50, -10), W(50, -10, 50, 10, true, false), W(50, 10, 50, 50)]
|
|
var opened = closed
|
|
opened[1].open = true
|
|
check not sees(closed, (0.0, 0.0), (90.0, 0.0))
|
|
check sees(opened, (0.0, 0.0), (90.0, 0.0))
|
|
check not sees(opened, (0.0, 0.0), (90.0, 40.0))
|
|
|
|
test "token exactly at wall endpoint":
|
|
let w = @[W(0, 0, 100, 0), W(0, 0, 0, 100)] # corner, token on it
|
|
let poly = visibilityPolygon((0.0, 0.0), w, 200)
|
|
check poly.inside((-50.0, -50.0)) # open outward quadrant
|
|
check poly.inside((100.0, -30.0))
|
|
check poly.inside((50.0, 50.0)) # arms radiate from the token: the inner quadrant is open too
|
|
check poly.inside((-50.0, 50.0))
|
|
check not poly.inside((150.0, 150.0)) # beyond the radius (212 > 200)
|
|
check not poly.inside((0.0, 250.0))
|
|
|
|
test "token at endpoint of free-standing wall sees along it":
|
|
let w = @[W(0, 0, 100, 0)]
|
|
check sees(w, (0.0, 0.0), (50.0, -20.0))
|
|
check sees(w, (0.0, 0.0), (-50.0, 20.0))
|
|
check sees(w, (0.0, 0.0), (50.0, 20.0))
|
|
check not sees(w, (0.0, 0.0), (600.0, 0.0)) # beyond the 500 radius
|
|
|
|
test "long thin corridor":
|
|
# 1000 long, 20 wide, token at one end; radius 2000
|
|
let w = @[W(0, -10, 1000, -10), W(0, 10, 1000, 10), W(1000, -10, 1000, 10)]
|
|
check sees(w, (5.0, 0.0), (990.0, 0.0), 2000)
|
|
check not sees(w, (5.0, 0.0), (500.0, 15.0), 2000)
|
|
check not sees(w, (5.0, 0.0), (1010.0, 0.0), 2000)
|
|
check sees(w, (5.0, 0.0), (900.0, 8.0), 2000)
|
|
|
|
test "wall endpoints behind the viewer: fan stays angularly ordered":
|
|
# regression: circle angles in [0,2pi) were sorted against atan2 angles in (-pi,pi] -> bow-tie polygon
|
|
let w = box(600, 600, 680, 680) & @[W(1024, 256, 1024, 608)]
|
|
check sees(w, (908.66, 672.03), (789.36, 584.65), 400)
|
|
check sees(w, (789.36, 584.65), (908.66, 672.03), 400)
|
|
check not sees(w, (908.66, 672.03), (1100.0, 500.0), 400) # the x=1024 wall (y 256..608) is in the way
|
|
|
|
test "radius clips":
|
|
check not sees(@[], (0.0, 0.0), (300.0, 0.0), 200)
|
|
|
|
suite "moves":
|
|
let wall = W(0, -50, 0, 50)
|
|
test "crossing blocked, parallel ok":
|
|
check moveBlocked([wall], (-10.0, 0.0), (10.0, 0.0))
|
|
check not moveBlocked([wall], (-10.0, 0.0), (-10.0, 30.0))
|
|
check not moveBlocked([wall], (-10.0, 60.0), (10.0, 60.0)) # passes beyond the end
|
|
test "grazing an endpoint is blocked":
|
|
check moveBlocked([wall], (-10.0, 50.0), (10.0, 50.0))
|
|
check moveBlocked([wall], (-10.0, -60.0), (10.0, -40.0)) # crosses
|
|
test "no leak through shared vertex":
|
|
let w = [W(0, -50, 0, 0), W(0, 0, 0, 50)]
|
|
check moveBlocked(w, (-10.0, 0.0), (10.0, 0.0))
|
|
test "diagonal squeeze through corner vertex of L":
|
|
let w = [W(0, 0, 50, 0), W(0, 0, 0, 50)]
|
|
check moveBlocked(w, (10.0, -10.0), (-10.0, 10.0))
|
|
test "moving along a collinear wall is blocked":
|
|
check moveBlocked([wall], (0.0, -100.0), (0.0, 100.0))
|
|
test "doors":
|
|
var d = W(0, -10, 0, 10, true, false)
|
|
check moveBlocked([d], (-5.0, 0.0), (5.0, 0.0))
|
|
d.open = true
|
|
check not moveBlocked([d], (-5.0, 0.0), (5.0, 0.0))
|
|
test "zero-length move":
|
|
check not moveBlocked([wall], (-5.0, 0.0), (-5.0, 0.0))
|
|
|
|
suite "cells":
|
|
test "square polygon":
|
|
let sq = [(10.0, 10.0), (50.0, 10.0), (50.0, 50.0), (10.0, 50.0)]
|
|
check cellsTouched(sq, 32) == @[(0, 0), (1, 0), (0, 1), (1, 1)]
|
|
test "tiny polygon inside one cell":
|
|
check cellsTouched([(1.0, 1.0), (5.0, 1.0), (3.0, 5.0)], 32) == @[(0, 0)]
|
|
test "polygon spanning a cell with no vertex in it":
|
|
# thin horizontal strip crossing 3 cells
|
|
let strip = [(1.0, 10.0), (95.0, 10.0), (95.0, 12.0), (1.0, 12.0)]
|
|
check cellsTouched(strip, 32) == @[(0, 0), (1, 0), (2, 0)]
|
|
test "big polygon includes fully covered interior cells":
|
|
let big = [(0.0, 0.0), (200.0, 0.0), (200.0, 200.0), (0.0, 200.0)]
|
|
check (3, 3) in cellsTouched(big, 32)
|
|
|
|
suite "seed":
|
|
test "every token in seed sees itself and is not walled in":
|
|
# fixture copied from prototype-server-vision/data/scene.json (tests/fixtures/); a missing file fails, never skips
|
|
let sp = currentSourcePath().parentDir / "fixtures" / "scene.json"
|
|
block:
|
|
let j = parseFile(sp)
|
|
var walls: seq[Wall]
|
|
for w in j["walls"]:
|
|
walls.add Wall(a: (w["a"][0].getFloat, w["a"][1].getFloat), b: (w["b"][0].getFloat, w["b"][1].getFloat),
|
|
door: w{"door"}.getBool, open: w{"open"}.getBool)
|
|
for t in j["tokens"]:
|
|
let p: Vec = (t["pos"][0].getFloat, t["pos"][1].getFloat)
|
|
let poly = visibilityPolygon(p, walls, j["visionRadius"].getFloat)
|
|
check poly.len > 8
|
|
check cellsTouched(poly, j["exploredCell"].getFloat).len > 0
|
|
check not moveBlocked(walls, p, p, j["tokenRadius"].getFloat)
|
|
|
|
suite "token clearance (r=20)":
|
|
const r = 20.0
|
|
test "near pillar: circle overlapping blocked, clear ok":
|
|
let pil = box(600, 600, 680, 680)
|
|
check moveBlocked(pil, (500.0, 640.0), (585.0, 640.0), r) # center outside, circle inside wall
|
|
check not moveBlocked(pil, (500.0, 640.0), (570.0, 640.0), r) # 30 away
|
|
test "near closed door blocks, open door passes":
|
|
let cl = @[W(0, 0, 0, 64, door = true)]
|
|
check moveBlocked(cl, (-50.0, 32.0), (10.0, 32.0), r) # dest 10 from the wall
|
|
check moveBlocked(cl, (-50.0, 32.0), (-10.0, 32.0), r)
|
|
let op = @[W(0, 0, 0, 64, door = true, open = true)]
|
|
check not moveBlocked(op, (-50.0, 32.0), (50.0, 32.0), r)
|
|
test "64px corridor and doorway passable on centerline":
|
|
let c = @[W(0, 0, 500, 0), W(0, 64, 500, 64)]
|
|
check not moveBlocked(c, (10.0, 32.0), (490.0, 32.0), r)
|
|
check not moveBlocked(c, (10.0, 32.0), (11.0, 32.0), r)
|
|
let door = @[W(0, -200, 0, -32), W(0, 32, 0, 200)]
|
|
check not moveBlocked(door, (-30.0, 0.0), (30.0, 0.0), r)
|
|
test "legacy overlap can escape, not stay overlapped":
|
|
let w = @[W(0, 0, 100, 0)]
|
|
check not moveBlocked(w, (50.0, 5.0), (50.0, 40.0), r)
|
|
check moveBlocked(w, (50.0, 5.0), (50.0, 15.0), r)
|
|
|
|
suite "door/wall closing on a token (M1 pins current behaviour)":
|
|
test "door closed on a token: Q14 undecided, DESIGN 5.4 vs 12 H2 conflict":
|
|
# DESIGN section 14 Q14 (allow walk-out / auto-nudge / refuse edit) is OPEN. DESIGN 5.4 says the token "may step
|
|
# perpendicular out"; H2 says the server keeps tokens >= r off walls. Pinned: current moveBlocked only.
|
|
const r = 20.0
|
|
let door = @[W(0, -50, 0, 50, door = true)] # closed, token radius 20
|
|
# token centre 10 off the door (circle overlaps it): may leave to a clear spot, but not cross or stay overlapped
|
|
check not moveBlocked(door, (10.0, 0.0), (40.0, 0.0), r) # walk away: dest 40 off
|
|
check moveBlocked(door, (10.0, 0.0), (15.0, 0.0), r) # still overlapping: blocked
|
|
check moveBlocked(door, (10.0, 0.0), (-40.0, 0.0), r) # through the closed door: blocked
|
|
var op = door
|
|
op[0].open = true # opening it lifts everything
|
|
check not moveBlocked(op, (10.0, 0.0), (-40.0, 0.0), r)
|
|
|
|
test "KNOWN LIMITATION: token exactly on a wall cannot move off it":
|
|
# prototype-server-vision/HANDOFF.md ("token exactly on a wall cannot move off it", inclusive segsCross);
|
|
# DESIGN 5.4 / H2 / Q14. A future fix deliberately flips these checks.
|
|
let wall = W(0, -50, 0, 50)
|
|
check moveBlocked([wall], (0.0, 10.0), (30.0, 10.0)) # perpendicular, away
|
|
check moveBlocked([wall], (0.0, 10.0), (-30.0, 10.0))
|
|
check moveBlocked([wall], (0.0, 10.0), (0.0, 80.0)) # along the wall, off its end
|
|
check moveBlocked([wall], (0.0, 10.0), (1.0, 10.0)) # one pixel
|
|
check moveBlocked([wall], (0.0, 10.0), (0.0, 10.0)) # even a zero-length move
|
|
check moveBlocked([wall], (0.0, 10.0), (30.0, 10.0), 20.0) # with a token radius too
|