diff --git a/server/moverules.nim b/server/moverules.nim new file mode 100644 index 0000000..c6d2295 --- /dev/null +++ b/server/moverules.nim @@ -0,0 +1,171 @@ +## Move rules and the `blocked` flag (DESIGN 5.5). Pure and in-process: plain data in, plain data out, no IO, no clock. +## The WebSocket layer (M3) only maps `MoveResult` to `moveAck`; nothing here knows about the wire. +## +## Player move s -> d (checks in order, any failure = the same nack): +## 1. owner (else mkError, never an ack: an ack would echo an unseen position) and token not `blocked`; +## 2. in bounds, and |d - s| <= stepCap (token vision radius if it has vision and sets one, else scene default); +## 3. per blocking wall W: start overlapping W (dist(s,W) < r, "escape"): allowed iff the path does not cross W +## farther than EscapeEps from s AND dist(d,W) >= r; otherwise swept circle: allowed iff dist(seg s->d, W) >= r. +## GM move: bounds only (no walls, no cap, ignores `blocked`). GM moves/placements never set `blocked`. +## `blocked`: set when an added wall / closed door has dist(token centre, W) < r; cleared when that wall is removed, +## that door opened, or the GM clears it (gmClearBlocked). wall update = remove then add. +## +## Thresholds (px unless noted): +## EscapeEps = 1e-6: a crossing this close to the start is "the token stands on the wall", not a crossing. A token +## within 1e-6 of a wall can only exist via GM placement/edit (a player move keeps dist >= r), so this cannot be +## used to tunnel; 1e-6 is far below one pixel and far above float noise at world scale (|coords| < 1e5, ~1e-11). +## geometry.Eps = 1e-9 (parallel/collinear/touch tolerance, shared with the vision code). +## Interpretation notes (DESIGN 5.5 "Unconfirmed"): (b) opening a door clears `blocked` for every token whose circle +## overlaps that door even if another closed wall still overlaps it; removing/updating a wall likewise. Only a +## BLOCKING wall's removal clears (removing an already-open door changes nothing). removeWall shifts later wall +## indices down (dense ids); M3 owns id stability. +## Escape corner case (spec-normative): an escaping token may graze closer to W than it started, mid-path; only +## crossing and the destination are checked. Moving along the wall's own line counts as crossing. +## Not checked here: path crossing a wall W2 whose capsule distance is >= r is fine by definition. +import std/math +import geometry + +const + EscapeEps* = 1e-6 + NoOwner* = -1 + +type + Role* = enum rolePlayer, roleGM + Actor* = object + role*: Role + player*: int # player id, meaningful for rolePlayer + Token* = object + pos*: Vec + owner*: int # player id or NoOwner (GM-controlled) + hasVision*: bool # `vision` flag + visionRadius*: float # <= 0: unset, use the scene default + blocked*: bool + Scene* = object + w*, h*: float + tokenRadius*, visionDefault*: float + walls*: seq[Wall] + tokens*: seq[Token] + MoveKind* = enum mkAck, mkNack, mkError + MoveResult* = object + kind*: MoveKind + pos*: Vec # mkAck: destination; mkNack: the old position; mkError: (0,0), must not be sent + +proc dot(a, b: Vec): float = a.x * b.x + a.y * b.y + +proc segSegDist*(p, q, a, b: Vec): float = + ## distance between segments pq and ab (0 if they touch or cross) + if segsCross(p, q, a, b): return 0.0 + min(min(segDist(p, a, b), segDist(q, a, b)), min(segDist(a, p, q), segDist(b, p, q))) + +proc pathCrosses*(s, d, a, b: Vec): bool = + ## does the path s->d meet segment ab at a point farther than EscapeEps from s? Collinear overlap beyond that counts. + let m = d - s + let len = hypot(m.x, m.y) + if len <= EscapeEps: return false + let e = b - a + let le = hypot(e.x, e.y) + let sa = a - s + if le < Eps: # degenerate wall = a point + return segDist(a, s, d) < Eps and hypot(sa.x, sa.y) > EscapeEps + let den = cross(m, e) + if abs(den) <= 1e-12 * len * le: # parallel + if abs(cross(sa, e)) / le > Eps: return false # offset parallel + let ta = dot(sa, m) / len + let tb = dot(b - s, m) / len + return max(ta, tb) > EscapeEps and min(ta, tb) <= len + let t = cross(sa, e) / den # fraction along s->d + let u = cross(sa, m) / den # fraction along ab + t * len > EscapeEps and t <= 1 + Eps / len and u >= -Eps / le and u <= 1 + Eps / le + +proc pathClear*(walls: openArray[Wall], s, d: Vec, r: float): bool = + ## DESIGN 5.5 step 3 for a player move (no owner/cap checks) + let (bx0, bx1) = (min(s.x, d.x) - r, max(s.x, d.x) + r) + let (by0, by1) = (min(s.y, d.y) - r, max(s.y, d.y) + r) + for w in walls: + if not w.blocks: continue + # a wall whose bbox misses the capsule bbox can neither be within r of the path nor of s + if max(w.a.x, w.b.x) < bx0 or min(w.a.x, w.b.x) > bx1 or + max(w.a.y, w.b.y) < by0 or min(w.a.y, w.b.y) > by1: continue + if segDist(s, w.a, w.b) < r: + if pathCrosses(s, d, w.a, w.b) or segDist(d, w.a, w.b) < r: return false + elif segsCross(s, d, w.a, w.b) or segSegDist(s, d, w.a, w.b) < r: return false + true + +proc stepCap*(sc: Scene, t: Token): float = + if t.hasVision and t.visionRadius > 0: t.visionRadius else: sc.visionDefault + +proc inBounds(sc: Scene, p: Vec): bool = # written so NaN fails + p.x >= 0 and p.y >= 0 and p.x <= sc.w and p.y <= sc.h + +proc nack(s: Vec): MoveResult = MoveResult(kind: mkNack, pos: s) + +proc moveToken*(sc: var Scene, who: Actor, ti: int, d: Vec): MoveResult = + ## validates and, on mkAck, applies the move. mkNack / mkError mutate nothing. + if ti < 0 or ti >= sc.tokens.len: return MoveResult(kind: mkError) + let s = sc.tokens[ti].pos + case who.role + of roleGM: + if not inBounds(sc, d): return nack(s) + of rolePlayer: + if sc.tokens[ti].owner != who.player: return MoveResult(kind: mkError) + if sc.tokens[ti].blocked or not inBounds(sc, d): return nack(s) + let dd = d - s + if hypot(dd.x, dd.y) > stepCap(sc, sc.tokens[ti]): return nack(s) + if not pathClear(sc.walls, s, d, sc.tokenRadius): return nack(s) + sc.tokens[ti].pos = d + MoveResult(kind: mkAck, pos: d) + +# ---- `blocked` bookkeeping ------------------------------------------------------------------------------------- + +proc overlaps(sc: Scene, t: Token, w: Wall): bool = segDist(t.pos, w.a, w.b) < sc.tokenRadius + +proc addToken*(sc: var Scene, t: Token): int = + ## GM placement: ignores walls, never computes `blocked` (stored as given) + sc.tokens.add t + sc.tokens.high + +proc gmClearBlocked*(sc: var Scene, ti: int) = + if ti >= 0 and ti < sc.tokens.len: sc.tokens[ti].blocked = false + +proc applyAdded(sc: var Scene, w: Wall) = + if w.blocks: + for i in 0 ..< sc.tokens.len: + if overlaps(sc, sc.tokens[i], w): sc.tokens[i].blocked = true + +proc applyRemoved(sc: var Scene, w: Wall) = + if w.blocks: + for i in 0 ..< sc.tokens.len: + if overlaps(sc, sc.tokens[i], w): sc.tokens[i].blocked = false + +proc addWall*(sc: var Scene, w: Wall): int = + sc.walls.add w + applyAdded(sc, w) + sc.walls.high + +proc removeWall*(sc: var Scene, id: int): bool = + if id < 0 or id >= sc.walls.len: return false + let w = sc.walls[id] + sc.walls.delete id + applyRemoved(sc, w) + true + +proc updateWall*(sc: var Scene, id: int, w: Wall): bool = + ## remove then add, id kept + if id < 0 or id >= sc.walls.len: return false + applyRemoved(sc, sc.walls[id]) + sc.walls[id] = w + applyAdded(sc, w) + true + +proc setDoor*(sc: var Scene, id: int, open: bool): bool = + ## false if `id` is not a door. Closing sets `blocked` on overlapped tokens, opening clears it. + if id < 0 or id >= sc.walls.len or not sc.walls[id].door: return false + if sc.walls[id].open == open: return true + if open: + let closed = sc.walls[id] # it blocked until now + sc.walls[id].open = true + applyRemoved(sc, closed) + else: + sc.walls[id].open = false + applyAdded(sc, sc.walls[id]) + true diff --git a/tests/testutil.nim b/tests/testutil.nim new file mode 100644 index 0000000..4e197e0 --- /dev/null +++ b/tests/testutil.nim @@ -0,0 +1,24 @@ +## shared test helpers (not a test file) +import std/math +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)] + +# Tiny deterministic xorshift64 so tests do not depend on std/random version behaviour. +type Rng* = object + s*: uint64 +proc next*(r: var Rng): uint64 = + r.s = r.s xor (r.s shl 13); r.s = r.s xor (r.s shr 7); r.s = r.s xor (r.s shl 17); r.s +proc uni*(r: var Rng, lo, hi: float): float = lo + (hi - lo) * float(r.next shr 11) / float(1'u64 shl 53) + +proc randomWalls*(r: var Rng, n: int, span: float, x0 = 0.0): seq[Wall] = + ## n random segments (length 10..300) with start in [x0, x0+span) x [0, span); every 7th is a door, every 14th open + for i in 0 ..< n: + let a: Vec = (x0 + r.uni(0, span), r.uni(0, span)) + let ang = r.uni(0, 2 * PI) + let l = r.uni(10, 300) + result.add Wall(a: a, b: (a.x + l * cos ang, a.y + l * sin ang), door: i mod 7 == 0, open: i mod 14 == 0) diff --git a/tests/tgeometry.nim b/tests/tgeometry.nim index 8940f84..f6dbf61 100644 --- a/tests/tgeometry.nim +++ b/tests/tgeometry.nim @@ -1,11 +1,5 @@ import std/[unittest, json, os, math] -import geometry, refold - -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)] +import geometry, refold, testutil proc sees(walls: openArray[Wall], o, p: Vec, r = 500.0): bool = visibilityPolygon(o, walls, r).inside(p) @@ -257,20 +251,6 @@ suite "corpus (prototype leak transcripts)": check doorsSeen > 0 # and exercises opened doors # ---- differential: scanline vs prototype per-cell --------------------------------------------------------------- -# Tiny deterministic PRNG so the tests do not depend on std/random version behaviour. -type Rng = object - s: uint64 -proc next(r: var Rng): uint64 = - r.s = r.s xor (r.s shl 13); r.s = r.s xor (r.s shr 7); r.s = r.s xor (r.s shl 17); r.s -proc uni(r: var Rng, lo, hi: float): float = lo + (hi - lo) * float(r.next shr 11) / float(1'u64 shl 53) - -proc randomWalls(r: var Rng, n: int, span: float): seq[Wall] = - for i in 0 ..< n: - let a: Vec = (r.uni(0, span), r.uni(0, span)) - let ang = r.uni(0, 2 * PI) - let l = r.uni(10, 300) - result.add Wall(a: a, b: (a.x + l * cos ang, a.y + l * sin ang), door: i mod 7 == 0, open: i mod 14 == 0) - suite "cellsTouched differential vs prototype": test "random visibility polygons: identical cell lists": var r = Rng(s: 0x9E3779B97F4A7C15'u64) diff --git a/tests/tmoverules.nim b/tests/tmoverules.nim new file mode 100644 index 0000000..7bd2854 --- /dev/null +++ b/tests/tmoverules.nim @@ -0,0 +1,279 @@ +import std/[unittest, math] +import geometry, moverules, refold, testutil + +const R = 20.0 +let me = Actor(role: rolePlayer, player: 1) +let other = Actor(role: rolePlayer, player: 2) +let gm = Actor(role: roleGM) + +proc scene(walls: seq[Wall], toks: seq[Token] = @[]): Scene = + Scene(w: 4096, h: 4096, tokenRadius: R, visionDefault: 400, walls: walls, tokens: toks) +proc tok(x, y: float, owner = 1, vision = false, vr = 0.0): Token = + Token(pos: (x, y), owner: owner, hasVision: vision, visionRadius: vr) +proc ok(sc: Scene, s, d: Vec, r = R): bool = pathClear(sc.walls, s, d, r) + +suite "pathClear, r = 0 (ported prototype move tests)": + let wall = W(0, -50, 0, 50) + let sc = scene(@[wall]) + test "crossing blocked, parallel ok": + check not sc.ok((-10.0, 0.0), (10.0, 0.0), 0) + check sc.ok((-10.0, 0.0), (-10.0, 30.0), 0) + check sc.ok((-10.0, 60.0), (10.0, 60.0), 0) # passes beyond the end + test "grazing an endpoint is blocked": + check not sc.ok((-10.0, 50.0), (10.0, 50.0), 0) + check not sc.ok((-10.0, -60.0), (10.0, -40.0), 0) # crosses + test "no leak through shared vertex": + check not scene(@[W(0, -50, 0, 0), W(0, 0, 0, 50)]).ok((-10.0, 0.0), (10.0, 0.0), 0) + test "diagonal squeeze through corner vertex of L": + check not scene(@[W(0, 0, 50, 0), W(0, 0, 0, 50)]).ok((10.0, -10.0), (-10.0, 10.0), 0) + test "moving along a collinear wall is blocked": + check not sc.ok((0.0, -100.0), (0.0, 100.0), 0) + test "doors": + var d = scene(@[W(0, -10, 0, 10, true, false)]) + check not d.ok((-5.0, 0.0), (5.0, 0.0), 0) + d.walls[0].open = true + check d.ok((-5.0, 0.0), (5.0, 0.0), 0) + test "zero-length move": + check sc.ok((-5.0, 0.0), (-5.0, 0.0), 0) + +suite "capsule (swept circle), r = 20": + test "near pillar: circle overlapping blocked, clear ok (ported)": + let c = scene(box(600, 600, 680, 680)) + check not c.ok((500.0, 640.0), (585.0, 640.0)) + check c.ok((500.0, 640.0), (570.0, 640.0)) # 30 away + test "closed door blocks near, open door passes (ported)": + let cl = scene(@[W(0, 0, 0, 64, door = true)]) + check not cl.ok((-50.0, 32.0), (10.0, 32.0)) + check not cl.ok((-50.0, 32.0), (-10.0, 32.0)) + check cl.ok((-50.0, 32.0), (-30.0, 32.0)) + let op = scene(@[W(0, 0, 0, 64, door = true, open = true)]) + check op.ok((-50.0, 32.0), (50.0, 32.0)) + test "64px corridor and doorway passable on the centreline (ported)": + let c = scene(@[W(0, 0, 500, 0), W(0, 64, 500, 64)]) + check c.ok((10.0, 32.0), (490.0, 32.0)) + check c.ok((10.0, 32.0), (11.0, 32.0)) + let door = scene(@[W(0, -200, 0, -32), W(0, 32, 0, 200)]) + check door.ok((-30.0, 0.0), (30.0, 0.0)) + test "corner clipping: path passes the wall end closer than r though both ends are clear": + let w = scene(@[W(0, 0, 0, 100)]) # wall end at (0,0) + let (s, d): (Vec, Vec) = (( -60.0, -10.0), (60.0, -10.0)) # passes 10 px past the end + check not w.ok(s, d) + check not moveBlockedOld(w.walls, s, d, R) # the prototype said clear: that was the H10 hole + check w.ok((-60.0, -25.0), (60.0, -25.0)) # 25 past the end: clear + test "parallel pass: r clear, just under r blocked (touching at exactly r is clear)": + let w = scene(@[W(0, 0, 200, 0)]) + check w.ok((20.0, 20.0), (180.0, 20.0)) + check not w.ok((20.0, 19.9), (180.0, 19.9)) + check not w.ok((20.0, 20.0), (180.0, 19.0)) # tilts closer at the far end + test "diagonal squeeze past an L corner with a gap narrower than 2r is blocked": + let w = scene(@[W(0, 0, 50, 0), W(0, 0, 0, 50)]) + check not w.ok((30.0, -30.0), (-30.0, 30.0)) + test "leg of the path inside r of a non-start wall blocks even if start and end are clear": + let w = scene(@[W(100, -5, 100, 5)]) # tiny post mid-path + check not w.ok((0.0, 0.0), (200.0, 0.0)) + check not w.ok((0.0, 15.0), (200.0, 15.0)) # 15 < 20 from the post + check w.ok((0.0, 26.0), (200.0, 26.0)) # 21 from the post's end + +suite "token on / near a wall: escape (replaces the KNOWN LIMITATION pin)": + let sc = scene(@[W(0, -50, 0, 50)]) + test "exactly on a wall: can step off perpendicular, either side, to a clear spot": + check sc.ok((0.0, 10.0), (30.0, 10.0)) + check sc.ok((0.0, 10.0), (-30.0, 10.0)) + check sc.ok((0.0, 10.0), (20.0, 10.0)) # exactly r away: clear + check sc.ok((0.0, 10.0), (25.0, 40.0)) # diagonal away + test "exactly on a wall: still refused when it would stay overlapped or run along/through": + check not sc.ok((0.0, 10.0), (1.0, 10.0)) # one pixel: dest still overlaps + check not sc.ok((0.0, 10.0), (19.0, 10.0)) + check not sc.ok((0.0, 10.0), (0.0, 10.0)) # zero-length: stays overlapped + check not sc.ok((0.0, 10.0), (0.0, 80.0)) # along the wall's line (collinear overlap counts as crossing) + check not sc.ok((0.0, 10.0), (0.0, -10.0)) # along the wall, staying on it + test "r = 0: standing on a wall is not an escape case (touch blocks), as the prototype": + check not sc.ok((0.0, 10.0), (30.0, 10.0), 0) + test "overlapping 10 px off: may leave away from the wall, not cross it, not stay overlapped": + check sc.ok((10.0, 0.0), (40.0, 0.0)) + check not sc.ok((10.0, 0.0), (15.0, 0.0)) + check not sc.ok((10.0, 0.0), (-40.0, 0.0)) # through + check not sc.ok((1.0, 0.0), (-40.0, 0.0)) # 1 px off the wall: crossing at t > eps + check not sc.ok((1e-3, 0.0), (-40.0, 0.0)) # even 1e-3 px off + test "escaping one wall does not excuse another": + let two = scene(@[W(0, -50, 0, 50), W(60, -50, 60, 50)]) + check two.ok((0.0, 10.0), (30.0, 10.0)) # 30 from the far wall: fine + check not two.ok((0.0, 10.0), (45.0, 10.0)) # clear of the first, 15 from the second + check two.ok((0.0, 10.0), (-30.0, 10.0)) # control: the other side is free + test "escape from a closed door; an open door never needed an escape": + var d = scene(@[W(0, -50, 0, 50, door = true)]) + check d.ok((0.0, 0.0), (30.0, 0.0)) + d.walls[0].open = true + check d.ok((0.0, 0.0), (1.0, 0.0)) + test "overlapping at a wall end: leave past the end without crossing": + check sc.ok((5.0, 55.0), (5.0, 100.0)) # 5 off the line, 5 past the end: dist 7.07 < r, leaves along +y + check not sc.ok((5.0, 55.0), (5.0, 60.0)) # still within r of the end + check sc.ok((5.0, 55.0), (40.0, 80.0)) + +suite "move rules: player vs GM, step cap, uniform nack": + test "owner check: not yours is mkError, no position": + var s = scene(@[], @[tok(100, 100, owner = 2)]) + let r = s.moveToken(me, 0, (110.0, 100.0)) + check r.kind == mkError + check r.pos == (0.0, 0.0) + check s.tokens[0].pos == (100.0, 100.0) + check s.moveToken(me, 7, (1.0, 1.0)).kind == mkError # unknown token + check s.moveToken(other, 0, (110.0, 100.0)).kind == mkAck # the owner can + test "GM-controlled token (no owner): players get mkError": + var s = scene(@[], @[tok(100, 100, owner = NoOwner)]) + check s.moveToken(me, 0, (110.0, 100.0)).kind == mkError + check s.moveToken(gm, 0, (110.0, 100.0)).kind == mkAck + test "step cap = scene default without vision, token vision radius with it": + var s = scene(@[], @[tok(500, 500), tok(500, 500, vision = true, vr = 100), tok(500, 500, vision = true, vr = 0)]) + check s.moveToken(me, 0, (500.0 + 400, 500.0)).kind == mkAck # exactly the default cap + check s.moveToken(me, 0, (500.0 + 800.5, 500.0)).kind == mkNack # from 900: 400.5 > cap + check s.moveToken(me, 1, (600.0, 500.0)).kind == mkAck # exactly 100 + check s.moveToken(me, 1, (600.0 + 100.01, 500.0)).kind == mkNack + check s.moveToken(me, 2, (500.0 + 400, 500.0)).kind == mkAck # vision flag, radius unset: default + check s.moveToken(me, 2, (500.0 + 400 + 400.5, 500.0)).kind == mkNack + check stepCap(s, s.tokens[1]) == 100 + test "over-cap, blocked-by-wall, blocked-flag and out-of-bounds nacks are identical": + var s = scene(@[W(200, 0, 200, 1000)], @[tok(100, 500), tok(100, 500), tok(100, 500)]) + s.tokens[2].blocked = true + let cap = s.moveToken(me, 0, (100.0 + 2000, 500.0)) # over cap, nothing in the way + let wal = s.moveToken(me, 1, (300.0, 500.0)) # in cap, through the wall + let flg = s.moveToken(me, 2, (130.0, 500.0)) # in cap, clear, but blocked + let oob = s.moveToken(me, 0, (-5.0, 500.0)) + let nan = s.moveToken(me, 0, (NaN, 500.0)) + for r in [cap, wal, flg, oob, nan]: + check r == MoveResult(kind: mkNack, pos: (100.0, 500.0)) + check cap == wal and wal == flg + check s.tokens[0].pos == (100.0, 500.0) and s.tokens[1].pos == (100.0, 500.0) # nothing mutated + test "an accepted move applies and echoes the destination": + var s = scene(@[W(200, 0, 200, 100)], @[tok(100, 500)]) + let r = s.moveToken(me, 0, (150.0, 520.0)) + check r == MoveResult(kind: mkAck, pos: (150.0, 520.0)) + check s.tokens[0].pos == (150.0, 520.0) + test "player move honours walls (capsule) via moveToken": + var s = scene(@[W(500, 0, 500, 200)], @[tok(440, 100)]) + check s.moveToken(me, 0, (490.0, 100.0)).kind == mkNack # would overlap + check s.moveToken(me, 0, (470.0, 100.0)).kind == mkAck + check s.moveToken(me, 0, (530.0, 100.0)).kind == mkNack # through + test "GM: ignores walls, step cap and blocked; bounds still apply": + var s = scene(@[W(200, 0, 200, 1000)], @[tok(100, 500, vision = true, vr = 50)]) + s.tokens[0].blocked = true + check s.moveToken(gm, 0, (300.0, 500.0)).kind == mkAck # through a wall + check s.moveToken(gm, 0, (3000.0, 3000.0)).kind == mkAck # far beyond the cap + check s.moveToken(gm, 0, (200.0, 500.0)).kind == mkAck # onto the wall + check s.moveToken(gm, 0, (-1.0, 0.0)) == MoveResult(kind: mkNack, pos: (200.0, 500.0)) + check s.moveToken(gm, 0, (5000.0, 0.0)).kind == mkNack + check s.tokens[0].blocked # GM moves never touch the flag + check s.moveToken(gm, 9, (1.0, 1.0)).kind == mkError + +suite "blocked flag": + proc blockedSc(): Scene = scene(@[], @[tok(100, 100), tok(100, 300, owner = NoOwner), tok(900, 900)]) + test "wall added within r of the circle (centre not on it) sets blocked; exactly r does not": + var s = blockedSc() + discard s.addWall(W(115, 0, 115, 200)) # 15 from token 0's centre: circle overlaps + check s.tokens[0].blocked + check not s.tokens[1].blocked # wall ends at y=200, token 1 is at y=300 + check not s.tokens[2].blocked + var t = blockedSc() + discard t.addWall(W(120, 0, 120, 200)) # exactly r: clear + check not t.tokens[0].blocked + test "GM-controlled tokens get the flag too; wall bbox does not matter, distance does": + var s = blockedSc() + discard s.addWall(W(0, 310, 200, 310)) + check s.tokens[1].blocked and not s.tokens[0].blocked + test "open door added or opened never sets; closing a door on a token does": + var s = blockedSc() + let id = s.addWall(W(100, 50, 100, 150, door = true, open = true)) + check not s.tokens[0].blocked + check s.setDoor(id, false) + check s.tokens[0].blocked + test "blocked token: player moves refused, GM move allowed and does not clear": + var s = blockedSc() + discard s.addWall(W(110, 0, 110, 200)) + check s.moveToken(me, 0, (40.0, 100.0)).kind == mkNack # would be a perfectly clear step + check s.tokens[0].pos == (100.0, 100.0) + check s.moveToken(gm, 0, (40.0, 100.0)).kind == mkAck + check s.tokens[0].blocked + test "cleared by removing the wall; unrelated removals keep it": + var s = blockedSc() + let a = s.addWall(W(110, 0, 110, 200)) + let b = s.addWall(W(2000, 0, 2000, 200)) + check s.removeWall(b) + check s.tokens[0].blocked + check s.removeWall(a) + check not s.tokens[0].blocked + check s.moveToken(me, 0, (40.0, 100.0)).kind == mkAck + check not s.removeWall(5) + test "cleared by opening the door; a second open is a no-op": + var s = blockedSc() + let id = s.addWall(W(110, 0, 110, 200, door = true)) + check s.tokens[0].blocked + check s.setDoor(id, true) + check not s.tokens[0].blocked + check s.setDoor(id, true) + check not s.setDoor(77, true) + var w = blockedSc() + check not w.setDoor(w.addWall(W(110, 0, 110, 200)), true) # a plain wall is not a door + check w.tokens[0].blocked + test "cleared by the GM; not re-set by itself": + var s = blockedSc() + discard s.addWall(W(110, 0, 110, 200)) + s.gmClearBlocked(0) + check not s.tokens[0].blocked + check s.moveToken(me, 0, (100.0, 150.0)).kind == mkNack # clear flag, but the capsule still refuses (overlap, dest overlaps) + check s.moveToken(me, 0, (40.0, 100.0)).kind == mkAck # and it can walk out (escape) + test "wall update = remove then add": + var s = blockedSc() + let id = s.addWall(W(110, 0, 110, 200)) + check s.updateWall(id, W(500, 0, 500, 200)) # moved away: clears + check not s.tokens[0].blocked + check s.updateWall(id, W(105, 0, 105, 200)) # moved onto the token: sets + check s.tokens[0].blocked + check s.updateWall(id, W(110, 0, 110, 200, door = true, open = true)) # became an open door: clears + check not s.tokens[0].blocked + check not s.updateWall(9, W(0, 0, 1, 1)) + test "GM placement and GM move onto a wall never set blocked, and the token can escape": + var s = scene(@[W(500, 0, 500, 1000)]) + let i = s.addToken(tok(500, 400)) # placed exactly on the wall + check not s.tokens[i].blocked + check s.moveToken(me, i, (530.0, 400.0)).kind == mkAck # escape works (was the KNOWN LIMITATION) + check s.moveToken(gm, i, (505.0, 400.0)).kind == mkAck # GM puts it back within r + check not s.tokens[i].blocked + check s.moveToken(me, i, (505.0, 450.0)).kind == mkNack # still overlapped: refused + check s.moveToken(me, i, (540.0, 400.0)).kind == mkAck # but it can leave + test "Q14: a door closed 10 px from a token: token blocked, player refused, GM moves it, opening frees it": + var s = scene(@[], @[tok(110, 100)]) + let id = s.addWall(W(100, 50, 100, 150, door = true, open = true)) + check s.setDoor(id, false) + check s.tokens[0].blocked + check s.moveToken(me, 0, (140.0, 100.0)).kind == mkNack + check s.moveToken(gm, 0, (60.0, 100.0)).kind == mkAck # through the closed door + check s.tokens[0].blocked # GM moved it off the door; the bool flag cannot say which wall set it + check s.setDoor(id, true) + check s.tokens[0].blocked # GAP: opening clears only tokens still overlapping that door + s.gmClearBlocked(0) + check not s.tokens[0].blocked + check s.setDoor(id, false) # closing on nobody sets nothing + check not s.tokens[0].blocked + s.tokens[0].pos = (110.0, 100.0) + check s.setDoor(id, true) and s.setDoor(id, false) # token back on the door: set again, then opening frees it + check s.tokens[0].blocked + check s.setDoor(id, true) + check not s.tokens[0].blocked + test "unconfirmed (b), pinned: opening a door clears blocked although another closed wall still overlaps": + var s = scene(@[], @[tok(100, 100)]) + discard s.addWall(W(110, 0, 110, 200)) # plain wall, overlaps + let id = s.addWall(W(100, 0, 100, 200, door = true)) # closed door, overlaps + check s.tokens[0].blocked + check s.setDoor(id, true) + check not s.tokens[0].blocked # literal Q14; DESIGN 5.5 lists this as unconfirmed + check s.moveToken(me, 0, (100.0, 150.0)).kind == mkNack # but the capsule still refuses an overlapped destination + +suite "seed scene": + test "no escape hatch: a player cannot walk through any wall of a boxed room": + var s = scene(box(0, 0, 400, 400), @[tok(200, 200)]) + var p: Vec = (200.0, 200.0) + for d in [(500.0, 200.0), (200.0, -100.0), (-100.0, 200.0), (200.0, 500.0), (390.0, 200.0), (385.0, 385.0)]: + let r = s.moveToken(me, 0, d) + check r.kind == mkNack + check r.pos == p + check s.moveToken(me, 0, (370.0, 200.0)).kind == mkAck # 30 from the wall diff --git a/tests/tprobing.nim b/tests/tprobing.nim new file mode 100644 index 0000000..2ebd159 --- /dev/null +++ b/tests/tprobing.nim @@ -0,0 +1,126 @@ +## T8 probing test, in-process (DESIGN 3.4 T8, 10.3, 5.5). The WebSocket / hostile-client version is M3. +## +## Attacker model: owns one token and may send any destination, any number of times (rate limit is a transport +## concern, M3). It sees only the reply (ack/nack + position), exactly what the wire carries. +## +## CONCRETE BOUND B = stepCap + tokenRadius (token vision radius, or the scene default, plus r): +## P1 a probe longer than the cap always gets the same nack, whatever the walls and doors are; +## P2 the reply to any probe from position s depends on walls only within distance B of s; so by induction two +## scenes that differ only in walls/doors farther than B from every position the token ever occupied produce +## IDENTICAL reply sequences (no wall or door state beyond B is recoverable, however many probes); +## P3 inside B, one probe yields one bit (ack/nack): it can reveal walls behind a wall within the disc, which is +## what the cap allows (cap = LOS radius); the negative control shows the harness does detect that; +## P4 nacks mutate nothing (position, blocked flag), so probing cannot be used to move or unflag a token. +import std/[unittest, math] +import geometry, moverules, testutil + +const + R = 20.0 + Cap = 300.0 + Bound = Cap + R + WorldW = 4096.0 +let me = Actor(role: rolePlayer, player: 1) + +proc mkScene(walls: seq[Wall]): Scene = + Scene(w: WorldW, h: WorldW, tokenRadius: R, visionDefault: Cap, walls: walls, + tokens: @[Token(pos: (300.0, 2000.0), owner: 1, hasVision: true, visionRadius: Cap)]) + +type Trace = object + replies: seq[MoveResult] + visited: seq[Vec] + +proc attack(sc: var Scene, seed: uint64, steps: int, xMax: float): Trace = + ## random prober confined (by its own choice of destinations) to x <= xMax; half the probes are over-cap + var r = Rng(s: seed) + for i in 0 ..< steps: + let s = sc.tokens[0].pos + var d: Vec + if i mod 2 == 0: + d = (r.uni(0, xMax), r.uni(0, WorldW)) # anywhere (mostly over the cap) + else: + let a = r.uni(0, 2 * PI) + let l = r.uni(0, 1.1 * Cap) # around the cap + d = (min(xMax, max(0.0, s.x + l * cos a)), min(WorldW, max(0.0, s.y + l * sin a))) + let res = sc.moveToken(me, 0, d) + result.replies.add res + result.visited.add sc.tokens[0].pos + # P4: every nack leaves the token where it was + if res.kind == mkNack: doAssert sc.tokens[0].pos == s and res.pos == s + +proc minDist(trace: Trace, walls: seq[Wall]): float = + result = Inf + for p in trace.visited: + for w in walls: result = min(result, segDist(p, w.a, w.b)) + +suite "probing (T8)": + test "P1: probes longer than the cap get one fixed nack regardless of the scene": + var empty = mkScene(@[]) + var rnd = Rng(s: 99) + var dense = mkScene(randomWalls(rnd, 400, 3000)) + dense.tokens[0].pos = (300.0, 2000.0) # may overlap a wall: not blocked, attacker cannot tell + var r = Rng(s: 5) + for i in 0 ..< 2000: + let a = r.uni(0, 2 * PI) + let l = r.uni(Cap + 0.01, 3000) + let d: Vec = (300 + l * cos a, 2000 + l * sin a) + let (x, y) = (empty.moveToken(me, 0, d), dense.moveToken(me, 0, d)) + check x == y + check x == MoveResult(kind: mkNack, pos: (300.0, 2000.0)) + + test "P2: scenes differing only beyond the bound give identical replies over 3000 probes": + # shared near field (x < 1500) + a far field (x >= 2600) that differs: other walls, extra walls, doors toggled + var r = Rng(s: 2024) + let near = randomWalls(r, 80, 1500) + var farA = randomWalls(r, 120, 1200, 2600) + var farB = randomWalls(r, 200, 1200, 2600) # different walls + for w in farA.mitems: w.door = true; w.open = false + for w in farB.mitems: w.door = true; w.open = true # same count class, doors flipped + var a = mkScene(near & farA) + var b = mkScene(near & farB) + for seed in [1'u64, 2, 3]: + a = mkScene(near & farA); b = mkScene(near & farB) + let ta = attack(a, seed, 3000, 1500) + let tb = attack(b, seed, 3000, 1500) + check ta.replies == tb.replies + # the premise really held: the walks stayed farther than B from every differing wall + check minDist(ta, farA & farB) > Bound + var acks = 0 + for x in ta.replies: + if x.kind == mkAck: inc acks + check acks > 100 and acks < 2900 # the attacker both moved and was refused: not a trivial run + + test "P3 negative control: a wall inside the disc IS visible through replies (the harness can fail)": + var r = Rng(s: 2024) + let near = randomWalls(r, 80, 1500) + let a = mkScene(near) + # same scene plus one wall right in front of the start, within B + let b = mkScene(near & @[W(300 + 150, 1900, 300 + 150, 2100)]) + var sa = a + var sb = b + let ta = attack(sa, 7, 400, 1500) + let tb = attack(sb, 7, 400, 1500) + check ta.replies != tb.replies + # and a single targeted probe shows the one-bit leak the cap allows: through-the-wall move nacks, free one acks + var ca = mkScene(@[]) + var cb = mkScene(@[W(450, 1900, 450, 2100)]) + check ca.moveToken(me, 0, (500.0, 2000.0)).kind == mkAck + check cb.moveToken(me, 0, (500.0, 2000.0)).kind == mkNack + + test "P4: probing cannot unflag or move a blocked token": + var s = mkScene(@[W(310, 1900, 310, 2100)]) # overlaps the token at (300,2000) + s.tokens[0].blocked = true + var r = Rng(s: 11) + for i in 0 ..< 500: + let res = s.moveToken(me, 0, (r.uni(0, 600), r.uni(1700, 2300))) + check res == MoveResult(kind: mkNack, pos: (300.0, 2000.0)) + check s.tokens[0].blocked and s.tokens[0].pos == (300.0, 2000.0) + + test "doors beyond the bound: opening or closing them changes no reply": + var r = Rng(s: 31) + let near = randomWalls(r, 60, 1200) + let doors = @[W(2800, 2000, 2800, 2200, door = true), W(3000, 1800, 3000, 2100, door = true, open = true)] + var a = mkScene(near & doors) + var closedAll = doors + closedAll[1].open = false + var b = mkScene(near & closedAll) + check attack(a, 77, 2000, 1200).replies == attack(b, 77, 2000, 1200).replies