moverules: door close pushes overlapped tokens aside (valid-side check, blocked only if no side valid); Push list; tests; mutations m-o

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 18:56:06 +01:00
co-authored by Claude Sonnet 5.5
parent 153ff6cd18
commit a29bb0bb46
4 changed files with 150 additions and 33 deletions
+62 -6
View File
@@ -7,11 +7,21 @@
## 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`), then blocked = overlaps any blocking wall.
## `blocked` ("stuck", DESIGN 5.5): overlap = dist(token centre, W) < r. SET: wall added / updated / door closed on a
## token; GM places or moves a token onto a blocking wall. KEEP: stays while ANY blocking wall overlaps. CLEAR: wall
## removed / door opened -> tokens that overlapped it get blocked = overlaps any remaining blocking wall; GM move to a
## `blocked` ("stuck", DESIGN 5.5): overlap = dist(token centre, W) < r. SET: wall added / updated; GM places or moves a token onto a blocking wall. KEEP: stays while ANY blocking wall
## overlaps. CLEAR: wall removed / door opened -> tokens that overlapped it get blocked = overlaps any remaining blocking wall; GM move to a
## free spot; gmClearBlocked (unconditional). Only tokens touched by the event change. wall update = remove then add.
##
## Door closing on a token = PUSH, not stuck (owner 2026-11-08): each token with dist(centre, door) < r moves
## perpendicular to the door, to the side its centre is on, by the minimal distance that ends >= r (+ PushEps); if the
## projection falls past the door's end the perpendicular offset is sqrt(r^2 - e^2), e = along-axis overshoot. Centre
## exactly on the door line (|side| <= Eps): side of `prev` (position before its most recent move/push); no history
## or prev also on the line: the left side of a->b (cross(b-a, p-a) > 0). Valid = in bounds and >= r from EVERY blocking
## wall (the closed door included); else try the opposite side; neither valid -> token stays and becomes `blocked`
## (opening that door later clears it, Clear rule). A pushed token gets blocked = false (its new spot overlaps no wall).
## Pushes are returned (Push list), no step cap. Each token is pushed independently; tokens never block tokens here
## (no token-token collision exists; `blocks_move` is not modelled), so pushed tokens may end overlapping each other.
## Wall add/update and GM placement/move inside a wall are unchanged (stuck). Door OPEN unchanged.
##
## 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
@@ -28,6 +38,7 @@ import geometry
const
EscapeEps* = 1e-6
NoOwner* = -1
PushEps* = 1e-6 # extra clearance of a pushed token so float noise never leaves it at dist < r
type
Role* = enum rolePlayer, roleGM
@@ -40,6 +51,11 @@ type
hasVision*: bool # `vision` flag
visionRadius*: float # <= 0: unset, use the scene default
blocked*: bool
prev*: Vec # position before the most recent move/push (valid iff hasPrev); decides the door-push side
hasPrev*: bool
Push* = object # server-originated position change (M3: broadcast + recompute vision)
ti*: int
pos*: Vec
Scene* = object
w*, h*: float
tokenRadius*, visionDefault*: float
@@ -118,6 +134,7 @@ proc moveToken*(sc: var Scene, who: Actor, ti: int, d: Vec): MoveResult =
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].prev = s; sc.tokens[ti].hasPrev = true
sc.tokens[ti].pos = d
if who.role == roleGM: sc.tokens[ti].blocked = overlapsAny(sc, sc.tokens[ti])
MoveResult(kind: mkAck, pos: d)
@@ -164,8 +181,34 @@ proc updateWall*(sc: var Scene, id: int, w: Wall): bool =
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.
proc pushFrom(sc: Scene, t: Token, w: Wall): tuple[ok: bool, pos: Vec] =
## door-close push of t off w (see header); ok=false if neither side is valid
let e = w.b - w.a
let le = hypot(e.x, e.y)
let r = sc.tokenRadius
var n: Vec = (0.0, 1.0) # degenerate door (a point): push along +y
var along = 0.0 # projection of the centre on the door axis, from a
var side = 1.0
if le >= Eps:
n = (-e.y / le, e.x / le) # left normal of a->b
along = dot(t.pos - w.a, e) / le
side = dot(t.pos - w.a, n)
if abs(side) <= Eps:
side = if t.hasPrev: dot(t.prev - w.a, n) else: 0.0
if abs(side) <= Eps: side = 1.0
let over = if along < 0: -along elif along > le: along - le else: 0.0
let perp = sqrt(max(0.0, r * r - over * over)) + PushEps
let foot: Vec = if le >= Eps: (w.a.x + e.x * along / le, w.a.y + e.y * along / le) else: w.a
for sg in [(if side > 0: 1.0 else: -1.0), (if side > 0: -1.0 else: 1.0)]:
let p: Vec = (foot.x + n.x * sg * perp, foot.y + n.y * sg * perp)
var tt = t
tt.pos = p
if inBounds(sc, p) and not overlapsAny(sc, tt): return (true, p)
(false, t.pos)
proc setDoor*(sc: var Scene, id: int, open: bool, pushed: var seq[Push]): bool =
## false if `id` is not a door. Closing pushes overlapped tokens aside (appended to `pushed`; those with no valid
## spot become `blocked`); opening clears `blocked` per the Clear rule.
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:
@@ -174,5 +217,18 @@ proc setDoor*(sc: var Scene, id: int, open: bool): bool =
applyRemoved(sc, closed)
else:
sc.walls[id].open = false
applyAdded(sc, sc.walls[id])
let w = sc.walls[id]
for i in 0 ..< sc.tokens.len:
if not overlaps(sc, sc.tokens[i], w): continue
let (ok, p) = pushFrom(sc, sc.tokens[i], w)
if ok:
sc.tokens[i].prev = sc.tokens[i].pos; sc.tokens[i].hasPrev = true
sc.tokens[i].pos = p
sc.tokens[i].blocked = false
pushed.add Push(ti: i, pos: p)
else: sc.tokens[i].blocked = true
true
proc setDoor*(sc: var Scene, id: int, open: bool): bool =
var ignored: seq[Push]
setDoor(sc, id, open, ignored)