diff --git a/results/nimble-test.log b/results/nimble-test.log index 0d881a9..8b5c3ec 100644 --- a/results/nimble-test.log +++ b/results/nimble-test.log @@ -79,15 +79,20 @@ ... [Suite] blocked flag ... [OK] wall added within r of the circle (centre not on it) sets blocked; exactly r does not ... [OK] GM-controlled tokens get the flag too; wall bbox does not matter, distance does - ... [OK] open door added or opened never sets; closing a door on a token does + ... [OK] open door added or opened never sets; closing a door on a token pushes it, never sets ... [OK] blocked token: player moves refused, GM move allowed ... [OK] cleared by removing the wall; unrelated removals keep it ... [OK] cleared by opening the door; a second open is a no-op ... [OK] cleared by the GM; not re-set by itself ... [OK] wall update = remove then add ... [OK] GM placement and GM move onto a wall: blocked; player escape works only after a GM clear - ... [OK] Q14: a door closed 10 px from a token: token blocked, player refused, GM moves it clear, opening frees it - ... [OK] doorway: token in an open doorway, door closes -> blocked, door opens -> free + ... [OK] Q14: a door closed 10 px from a token: pushed to its side, clear by r, not blocked; GM-owned too + ... [OK] push: centre on the door line -> side of the previous position; no history -> left of a->b + ... [OK] push: past the end of the door the offset shrinks (minimal), still >= r from the door + ... [OK] push: one side blocked by another wall -> the other side; out of bounds -> the other side + ... [OK] push: both sides invalid -> blocked in place; door open -> free; GM can still move it + ... [OK] push: wall added on a token still sticks it (regression); closing on nobody pushes nobody + ... [OK] doorway: token in an open doorway, door closes -> pushed clear, free; door reopens/closes again -> unchanged ... [OK] KEEP: opening one door / removing one wall while another overlaps -> still blocked; last one frees ... [OK] KEEP: wall update away from one of two overlapping walls keeps blocked ... [OK] GM moves a stuck token clear -> free; to another wall-overlapping spot -> still blocked diff --git a/server/moverules.nim b/server/moverules.nim index d9a7d58..ce98562 100644 --- a/server/moverules.nim +++ b/server/moverules.nim @@ -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) diff --git a/tests/tmoverules.nim b/tests/tmoverules.nim index 209ef9d..43fe3dd 100644 --- a/tests/tmoverules.nim +++ b/tests/tmoverules.nim @@ -10,6 +10,10 @@ 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 clearOfWalls(sc: Scene, t: Token): bool = + for w in sc.walls: + if w.blocks and segDist(t.pos, w.a, w.b) < sc.tokenRadius: return false + true proc ok(sc: Scene, s, d: Vec, r = R): bool = pathClear(sc.walls, s, d, r) suite "pathClear, r = 0 (ported prototype move tests)": @@ -180,12 +184,13 @@ suite "blocked flag": 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": + test "open door added or opened never sets; closing a door on a token pushes it, never sets": 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 + check not s.tokens[0].blocked + check s.tokens[0].pos != (100.0, 100.0) test "blocked token: player moves refused, GM move allowed": var s = blockedSc() discard s.addWall(W(110, 0, 110, 200)) @@ -243,31 +248,73 @@ suite "blocked flag": s.gmClearBlocked(i) 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 clear, opening frees it": - var s = scene(@[], @[tok(110, 100)]) + test "Q14: a door closed 10 px from a token: pushed to its side, clear by r, not blocked; GM-owned too": + var s = scene(@[], @[tok(110, 100), tok(90, 100, owner = NoOwner), tok(130, 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, ends clear of every wall - check not s.tokens[0].blocked - check s.setDoor(id, true) and 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, then opening frees it - check s.tokens[0].blocked - check s.setDoor(id, true) - check not s.tokens[0].blocked - test "doorway: token in an open doorway, door closes -> blocked, door opens -> free": + var pushed: seq[Push] + check s.setDoor(id, false, pushed) + check pushed.len == 2 and pushed[0].ti == 0 and pushed[1].ti == 1 # token 2 is exactly r away: untouched + check s.tokens[0].pos == pushed[0].pos and s.tokens[1].pos == pushed[1].pos + check s.tokens[0].pos.x >= 120.0 and s.tokens[0].pos.x < 120.001 and s.tokens[0].pos.y == 100.0 + check s.tokens[1].pos.x <= 80.0 and s.tokens[1].pos.x > 79.999 + check s.tokens[2].pos == (130.0, 100.0) + for t in s.tokens: check not t.blocked and clearOfWalls(s, t) + check s.moveToken(me, 0, (150.0, 100.0)).kind == mkAck + test "push: centre on the door line -> side of the previous position; no history -> left of a->b": + for (prev, wantLeft) in [((130.0, 100.0), false), ((60.0, 100.0), true)]: # door runs +y: left = -x + var s = scene(@[], @[tok(prev[0], prev[1])]) + let id = s.addWall(W(100, 50, 100, 150, door = true, open = true)) + check s.moveToken(me, 0, (100.0, 100.0)).kind == mkAck # lands in the open doorway + check s.setDoor(id, false) + check (s.tokens[0].pos.x < 100.0) == wantLeft + check abs(s.tokens[0].pos.x - 100.0) >= R var s = scene(@[], @[tok(100, 100)]) 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 - check s.moveToken(me, 0, (140.0, 100.0)).kind == mkNack + check s.tokens[0].pos.x < 100.0 and not s.tokens[0].blocked # default: left side + test "push: past the end of the door the offset shrinks (minimal), still >= r from the door": + var s = scene(@[], @[tok(110, 160)]) # 10 beyond the end a-b = (100,50)-(100,150): dist 14.1 < 20 + let id = s.addWall(W(100, 50, 100, 150, door = true, open = true)) + check s.setDoor(id, false) + let d = segDist(s.tokens[0].pos, (100.0, 50.0), (100.0, 150.0)) + check d >= R and d < R + 1e-4 + check s.tokens[0].pos.x > 110.0 and s.tokens[0].pos.y == 160.0 # perpendicular only + test "push: one side blocked by another wall -> the other side; out of bounds -> the other side": + var s = scene(@[W(125, 0, 125, 300)], @[tok(110, 100)]) # +x side lands at 120, 5 from the wall + let id = s.addWall(W(100, 50, 100, 150, door = true, open = true)) + check s.setDoor(id, false) + check s.tokens[0].pos.x <= 80.0 and not s.tokens[0].blocked + var b = scene(@[], @[tok(110, 100)]) + let j = b.addWall(W(100, 50, 100, 150, door = true, open = true)) + b.w = 110 # +x side (120) is out of bounds + check b.setDoor(j, false) + check b.tokens[0].pos.x <= 80.0 and not b.tokens[0].blocked + test "push: both sides invalid -> blocked in place; door open -> free; GM can still move it": + var s = scene(@[W(125, 0, 125, 300), W(75, 0, 75, 300)], @[tok(110, 100)]) + let id = s.addWall(W(100, 50, 100, 150, door = true, open = true)) + var pushed: seq[Push] + check s.setDoor(id, false, pushed) + check pushed.len == 0 and s.tokens[0].blocked and s.tokens[0].pos == (110.0, 100.0) + check s.moveToken(me, 0, (112.0, 100.0)).kind == mkNack check s.setDoor(id, true) + check s.tokens[0].blocked # 125-wall still overlaps (KEEP) + test "push: wall added on a token still sticks it (regression); closing on nobody pushes nobody": + var s = scene(@[], @[tok(110, 100)]) + discard s.addWall(W(100, 50, 100, 150)) + check s.tokens[0].blocked and s.tokens[0].pos == (110.0, 100.0) + var t = scene(@[], @[tok(300, 300)]) + let id = t.addWall(W(100, 50, 100, 150, door = true, open = true)) + var pushed: seq[Push] + check t.setDoor(id, false, pushed) and pushed.len == 0 + test "doorway: token in an open doorway, door closes -> pushed clear, free; door reopens/closes again -> unchanged": + var s = scene(@[], @[tok(100, 100)]) + let id = s.addWall(W(100, 50, 100, 150, door = true, open = true)) + check s.setDoor(id, false) check not s.tokens[0].blocked - check s.moveToken(me, 0, (140.0, 100.0)).kind == mkAck + let p = s.tokens[0].pos + check s.moveToken(me, 0, (p.x - 40.0, 100.0)).kind == mkAck + check s.setDoor(id, true) and s.setDoor(id, false) + check s.tokens[0].pos.x == p.x - 40.0 test "KEEP: opening one door / removing one wall while another overlaps -> still blocked; last one frees": var s = scene(@[], @[tok(100, 100)]) let a = s.addWall(W(110, 0, 110, 200)) @@ -307,10 +354,12 @@ suite "blocked flag": check not s.tokens[1].blocked check s.moveToken(gm, 0, (100.0, 100.0)).kind == mkAck check not s.tokens[0].blocked # open door: no wall - check s.setDoor(0, false) - check s.tokens[0].blocked and s.tokens[1].blocked - check s.setDoor(0, true) + check s.setDoor(0, false) # both pushed clear, none stuck check not s.tokens[0].blocked and not s.tokens[1].blocked + check s.moveToken(gm, 0, (112.0, 100.0)).kind == mkAck # GM drop back inside the closed door: stuck + check s.tokens[0].blocked + check s.setDoor(0, true) + check not s.tokens[0].blocked var t = scene(@[W(110, 0, 110, 200)], @[tok(300, 300)]) check t.moveToken(gm, 0, (100.0, 100.0)).kind == mkAck check t.tokens[0].blocked diff --git a/tools/mutation.sh b/tools/mutation.sh index 8bb92af..fbb450f 100755 --- a/tools/mutation.sh +++ b/tools/mutation.sh @@ -7,6 +7,7 @@ # e: cellsTouched without the fill spans | f: escape rule without the crossing check | g: GM moves checked against walls # h: capsule reduced to centre path + destination circle (prototype H10) | i: no step cap | j: blocked trigger needs centre-on-wall # k: wall removal / door open clears blocked unconditionally (no KEEP) | l: GM moves never set/clear blocked +# m: door push goes to the wrong side first | n: door push skips the validity check | o: push ignores the previous-position rule # Log: results/mutation.log cd "$(dirname "$0")/.." || exit 1 mkdir -p results @@ -22,6 +23,9 @@ I="step cap|over-cap, blocked-by-wall|P1:|P2:" J="wall added within r of the circle|Q14: a door closed" K="KEEP:|GM manual clear inside|Q14: a door closed" L="GM moves a stuck token|GM drops a token|GM manual clear inside|GM: ignores walls" +M="Q14: a door closed|push: centre on the door line|push: one side blocked|doorway: token in an open doorway|GM drops a token" +N="push: one side blocked|push: both sides invalid" +O="push: centre on the door line" rc=0 : > "$LOG" run() { # $1 id $2 title $3 file to mutate $4 sed script ("" = control) $5 expected-failure regex @@ -63,5 +67,8 @@ run i "no step cap" server/moverules.nim '/if hypot(dd.x, dd.y) > stepCap(sc, sc run j "blocked trigger needs the centre on the wall" server/moverules.nim 's/segDist(t.pos, w.a, w.b) < sc.tokenRadius/segDist(t.pos, w.a, w.b) < 1e-9/' "$J" run k "removal/open clears blocked unconditionally (no KEEP)" server/moverules.nim 's/sc.tokens\[i\].blocked = overlapsAny(sc, sc.tokens\[i\])/sc.tokens[i].blocked = false/' "$K" run l "GM moves never set or clear blocked" server/moverules.nim '/^ if who.role == roleGM: sc.tokens\[ti\].blocked = /d' "$L" +run m "door push: wrong side first" server/moverules.nim 's/for sg in \[(if side > 0: 1.0 else: -1.0), (if side > 0: -1.0 else: 1.0)\]:/for sg in [(if side > 0: -1.0 else: 1.0), (if side > 0: 1.0 else: -1.0)]:/' "$M" +run n "door push: no validity check" server/moverules.nim 's/if inBounds(sc, p) and not overlapsAny(sc, tt): return (true, p)/return (true, p)/' "$N" +run o "door push: previous position ignored on the line" server/moverules.nim 's/side = if t.hasPrev: dot(t.prev - w.a, n) else: 0.0/side = 0.0/' "$O" echo "exit=$rc" | tee -a "$LOG" exit $rc