moverules: swept-circle move check, token-on-wall escape, blocked flag, step cap, GM bypass
Pure in-process module (server/moverules.nim, plain data, no IO) per DESIGN 5.5. Replaces moveBlocked tests (ported at r=0), the KNOWN LIMITATION pin and the Q14 pin with positive escape/capsule assertions and blocked-policy tests. tests/tprobing.nim: in-process T8 probing test with a concrete bound (stepCap + r). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user