From eef899e0153f337a75eed15b65c6bc7097d37a9f Mon Sep 17 00:00:00 2001 From: Davide Cappellini Date: Wed, 4 Nov 2026 21:12:53 +0100 Subject: [PATCH] prototype-server-vision: token radius (r=20) clearance in move validation; tokenRadius in scene.json + hello Co-Authored-By: Claude Sonnet 5.5 --- prototype-server-vision/NOTES.md | 3 +++ prototype-server-vision/README.md | 1 + prototype-server-vision/client/src/main.ts | 7 +++--- prototype-server-vision/client/src/state.ts | 6 ++--- prototype-server-vision/data/scene.json | 1 + prototype-server-vision/src/geometry.nim | 14 +++++++++--- prototype-server-vision/src/server.nim | 7 +++--- prototype-server-vision/tests/tgeometry.nim | 25 ++++++++++++++++++++- prototype-server-vision/tools/genmap.mjs | 2 +- prototype-server-vision/tools/leak.mjs | 6 ++--- 10 files changed, 54 insertions(+), 18 deletions(-) diff --git a/prototype-server-vision/NOTES.md b/prototype-server-vision/NOTES.md index 973b08d..0eb8681 100644 --- a/prototype-server-vision/NOTES.md +++ b/prototype-server-vision/NOTES.md @@ -27,3 +27,6 @@ ## Post-phase-3 manual-test bug - BUG (server, geometry.nim): `visibilityPolygon` sorted circle-segment angles in [0,2pi) together with wall-endpoint atan2 angles in (-pi,pi] -> fan vertices out of angular order -> bow-tie polygon, even-odd `inside`/`cellsTouched` dropped regions (tokens in plain LOS not shown, explored/tiles missing). Position dependent. FIX: wrap circle angles with atan2(sin,cos). - Why tests missed it: leakcheck recomputed its oracle with the same `visibilityPolygon` (only checked "nothing extra leaks", never "everything visible shows"), and only subject A was checked. Now: independent clear-LOS (segment test) oracle, `SUBJECT=A|B`, geometry regression test. + +## Token radius bug +- BUG (server): move validation treated tokens as points; client drew radius 20, so tokens stopped half inside walls/pillar/doors. FIX: `moveBlocked(..., r)` also requires dest circle clear (point-segment dist >= r); `tokenRadius` in scene.json + `hello`. 64 px corridors/doorways leave 12 px slack each side of the centreline. Leak scenario steps adjusted to stop >= r from the closed door/wall. diff --git a/prototype-server-vision/README.md b/prototype-server-vision/README.md index 64958d1..101670d 100644 --- a/prototype-server-vision/README.md +++ b/prototype-server-vision/README.md @@ -69,6 +69,7 @@ Bytes per delivery (only recipients that got something): **Worked** - Server-only filtering is simple under one global lock: validate -> mutate -> recompute -> persist -> per-connection diff -> send. Players never receive walls/doors/other tokens; leak test finds nothing. - Per-connection `known` map gives correct enter/leave (`token`/`remove`) and a fresh connection gets full state; clients reset on `hello`. +- Token radius `tokenRadius` (20 px, data/scene.json via tools/genmap.mjs) is server-authoritative and sent in `hello` (client falls back to 20 if absent). A move is accepted only if the centre path crosses no blocking wall AND the destination circle is >= r from every blocking wall (open doors ignored). Only the destination is checked, so a token already overlapping a wall (closed door, legacy DB) can leave. Vision origin stays the token centre. - Tile gating via nginx `internal` + `X-Accel-Redirect` after Nim checks explored cells; `/_tiles/` unreachable directly. - Moves: inclusive segment test blocks squeezing through shared vertices; non-owned/malformed moves get `error` (not an ack, an ack would echo the position of an unseen token). diff --git a/prototype-server-vision/client/src/main.ts b/prototype-server-vision/client/src/main.ts index fb5c099..34fd85c 100644 --- a/prototype-server-vision/client/src/main.ts +++ b/prototype-server-vision/client/src/main.ts @@ -1,7 +1,6 @@ import { Application, Container, Graphics, Sprite, Text, Texture } from 'pixi.js'; import { apply, newState, tilesOfCells, type Tok, type V2 } from './state.ts'; -const R = 20; // token radius, world px const s = newState(); const bar = document.getElementById('bar')!; let conn = 'connecting'; @@ -52,9 +51,9 @@ function drawToken(t: Tok) { let c = sprites.get(t.id); if (!c) { c = new Container(); - const g = new Graphics().circle(0, 0, R).fill(t.mine ? 0x3a9d3a : 0xb03030).stroke({ width: 3, color: 0xffffff }); + const g = new Graphics().circle(0, 0, s.tokenR).fill(t.mine ? 0x3a9d3a : 0xb03030).stroke({ width: 3, color: 0xffffff }); const l = new Text({ text: t.name, style: { fontSize: 14, fill: 0xffffff, stroke: { color: 0, width: 3 } } }); - l.anchor.set(0.5, 0); l.y = R + 2; + l.anchor.set(0.5, 0); l.y = s.tokenR + 2; c.addChild(g, l); tokensL.addChild(c); sprites.set(t.id, c); } @@ -142,7 +141,7 @@ cv.addEventListener('pointerdown', (e) => { const p = toWorld(e); for (const [id, c] of [...sprites].reverse()) { // O(N) hit test on press const t = s.tokens.get(id)!; - if (canDrag(t) && Math.hypot(p[0] - c.x, p[1] - c.y) <= R) { drag = { id, dx: c.x - p[0], dy: c.y - p[1], last: 0 }; dragging.set(id, true); return; } + if (canDrag(t) && Math.hypot(p[0] - c.x, p[1] - c.y) <= s.tokenR) { drag = { id, dx: c.x - p[0], dy: c.y - p[1], last: 0 }; dragging.set(id, true); return; } } if (s.role === 'gm') { const d = pickDoor(p); if (d !== null) { send({ t: 'door', id: d, open: !s.walls.get(d)!.open }); return; } } pan = { x: e.clientX - world.x, y: e.clientY - world.y }; diff --git a/prototype-server-vision/client/src/state.ts b/prototype-server-vision/client/src/state.ts index 8ca414a..283201e 100644 --- a/prototype-server-vision/client/src/state.ts +++ b/prototype-server-vision/client/src/state.ts @@ -7,7 +7,7 @@ export type Msg = { t: string; gseq: number; [k: string]: any }; export type State = { role: 'gm' | 'player' | null; owned: string[]; - w: number; h: number; tileSize: number; cell: number; + w: number; h: number; tileSize: number; cell: number; tokenR: number; walls: Map; polys: V2[][]; explored: Set; @@ -17,7 +17,7 @@ export type State = { }; export const newState = (): State => ({ - role: null, owned: [], w: 0, h: 0, tileSize: 512, cell: 32, + role: null, owned: [], w: 0, h: 0, tileSize: 512, cell: 32, tokenR: 20, walls: new Map(), polys: [], explored: new Set(), tokens: new Map(), gseq: 0, lastError: '', }); @@ -28,7 +28,7 @@ export function apply(s: State, m: Msg): V2[] | undefined { case 'hello': // server resends full state after hello: start clean (reconnect) Object.assign(s, newState(), { gseq: m.gseq }); s.role = m.role; s.owned = m.tokens; s.w = m.world.w; s.h = m.world.h; - s.tileSize = m.tileSize; s.cell = m.exploredCell; + s.tileSize = m.tileSize; s.cell = m.exploredCell; s.tokenR = m.tokenRadius ?? s.tokenR; // ?? : older server omits it break; case 'walls': s.walls = new Map((m.walls as Wall[]).map((w) => [w.id, w])); diff --git a/prototype-server-vision/data/scene.json b/prototype-server-vision/data/scene.json index 7f9aa87..4fc3501 100644 --- a/prototype-server-vision/data/scene.json +++ b/prototype-server-vision/data/scene.json @@ -5,6 +5,7 @@ }, "tileSize": 256, "visionRadius": 400, + "tokenRadius": 20, "exploredCell": 32, "walls": [ { diff --git a/prototype-server-vision/src/geometry.nim b/prototype-server-vision/src/geometry.nim index 46a49dd..e6968db 100644 --- a/prototype-server-vision/src/geometry.nim +++ b/prototype-server-vision/src/geometry.nim @@ -70,11 +70,19 @@ proc segsCross(p, q, a, b: Vec): bool = (o1 == 0 and onSeg(p, q, a)) or (o2 == 0 and onSeg(p, q, b)) or (o3 == 0 and onSeg(a, b, p)) or (o4 == 0 and onSeg(a, b, q)) -proc moveBlocked*(walls: openArray[Wall], a, b: Vec): bool = - ## true if travelling a->b touches any blocking wall (touching counts: no leaking through shared vertices). +proc segDist(p, a, b: Vec): float = + let e = b - a + let l2 = e.x * e.x + e.y * e.y + let t = if l2 < Eps: 0.0 else: clamp(((p.x - a.x) * e.x + (p.y - a.y) * e.y) / l2, 0.0, 1.0) + hypot(p.x - a.x - t * e.x, p.y - a.y - t * e.y) + +proc moveBlocked*(walls: openArray[Wall], a, b: Vec, r = 0.0): bool = + ## true if travelling a->b touches any blocking wall (touching counts: no leaking through shared vertices) + ## or the token circle (radius r) at b overlaps one. Only b is checked for clearance, so a token + ## already overlapping (legacy position, door closed on it) can still leave to a clear spot. ## shortcut: a token standing exactly on a wall can't move off it; server must keep tokens off walls. for w in walls: - if w.blocks and segsCross(a, b, w.a, w.b): return true + if w.blocks and (segsCross(a, b, w.a, w.b) or segDist(b, w.a, w.b) < r): return true proc inside*(poly: openArray[Vec], p: Vec): bool = ## even-odd point-in-polygon (boundary ambiguous) diff --git a/prototype-server-vision/src/server.nim b/prototype-server-vision/src/server.nim index f261fd8..4111873 100644 --- a/prototype-server-vision/src/server.nim +++ b/prototype-server-vision/src/server.nim @@ -41,7 +41,7 @@ var walls: seq[Wall] # index == wall id toks: seq[Tok] worldW, worldH, tileSize, cell, cw: int - radius: float + radius, tokRadius: float roles: Table[string, Role] secrets: Table[string, string] # secret -> "gm"|"A"|"B" conns: Table[string, Conn] @@ -156,7 +156,7 @@ proc handleMove(c: Conn, m: JsonNode) = let reqId = m["reqId"].getInt let np: Vec = (p[0].getFloat, p[1].getFloat) let old = toks[ti].pos - if np.x < 0 or np.y < 0 or np.x > worldW.float or np.y > worldH.float or moveBlocked(walls, old, np): + if np.x < 0 or np.y < 0 or np.x > worldW.float or np.y > worldH.float or moveBlocked(walls, old, np, tokRadius): c.send("moveAck", %*{"reqId": reqId, "ok": false, "pos": pt(old)}); return toks[ti].pos = np inc gseq @@ -198,7 +198,7 @@ proc openConn(ws: WebSocket, role: string) = for t in toks: if owns(role, t.id): ids.add %t.id c.send("hello", %*{"role": (if role == "gm": "gm" else: "player"), "tokens": ids, - "world": {"w": worldW, "h": worldH}, "tileSize": tileSize, "exploredCell": cell}) + "world": {"w": worldW, "h": worldH}, "tileSize": tileSize, "exploredCell": cell, "tokenRadius": tokRadius}) if role == "gm": var ws = newJArray() for i, w in walls: ws.add %*{"id": i, "a": pt(w.a), "b": pt(w.b), "door": w.door, "open": w.open} @@ -276,6 +276,7 @@ proc loadState() = worldW = sc["world"]["w"].getInt; worldH = sc["world"]["h"].getInt tileSize = sc["tileSize"].getInt; cell = sc["exploredCell"].getInt radius = sc["visionRadius"].getFloat + tokRadius = sc["tokenRadius"].getFloat cw = (worldW + cell - 1) div cell for w in sc["walls"]: walls.add Wall(a: (w["a"][0].getFloat, w["a"][1].getFloat), b: (w["b"][0].getFloat, w["b"][1].getFloat), diff --git a/prototype-server-vision/tests/tgeometry.nim b/prototype-server-vision/tests/tgeometry.nim index 90931db..5ea8bf4 100644 --- a/prototype-server-vision/tests/tgeometry.nim +++ b/prototype-server-vision/tests/tgeometry.nim @@ -151,4 +151,27 @@ suite "seed": 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) + 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) diff --git a/prototype-server-vision/tools/genmap.mjs b/prototype-server-vision/tools/genmap.mjs index 6d04382..1ba4dd3 100644 --- a/prototype-server-vision/tools/genmap.mjs +++ b/prototype-server-vision/tools/genmap.mjs @@ -59,7 +59,7 @@ function build() { ...[[900, 900], [1920, 640], [3400, 500], [900, 2700], [1920, 2400], [3200, 2400]] .map((p, i) => tok(`M${i + 1}`, 'monster', `Monster ${i + 1}`, p, true)), ] - return { world: { w: SIZE, h: SIZE }, tileSize: TILE, visionRadius: 400, exploredCell: 32, walls, floors, tokens } + return { world: { w: SIZE, h: SIZE }, tileSize: TILE, visionRadius: 400, tokenRadius: 20, exploredCell: 32, walls, floors, tokens } } // --- raster --- diff --git a/prototype-server-vision/tools/leak.mjs b/prototype-server-vision/tools/leak.mjs index 54ab4f8..2e94443 100644 --- a/prototype-server-vision/tools/leak.mjs +++ b/prototype-server-vision/tools/leak.mjs @@ -40,13 +40,13 @@ const sw = label => acts.push({ sweep: label }) sw('start') walk('B', [700, 2300]) walk('A', [400, 800]); walk('A', [800, 800]); sw('M1 in view') -walk('A', [800, 640]); walk('A', [1000, 640]); walk('A', [1500, 640]) -walk('A', [1560, 640], true); sw('closed door blocks') +walk('A', [800, 640]); walk('A', [1000, 640]); walk('A', [1510, 640]) +walk('A', [1540, 640], true); sw('closed door blocks') walk('M2', [1700, 640]); sw('M2 behind closed door') door(10, true); sw('door open'); walk('A', [1700, 640]); walk('M2', [1900, 700]); walk('A', [1900, 640]); sw('M2 close') walk('M2', [2200, 300]); sw('M2 left'); walk('M1', [900, 640]); walk('M1', [1200, 640]); sw('M1 into corridor') walk('A', [1920, 900]); walk('A', [1920, 1900]); sw('corridor') -door(27, false); sw('door 27 closed'); walk('A', [1920, 2030]); walk('A', [1920, 2060], true); sw('blocked'); door(27, true); sw('door 27 open') +door(27, false); sw('door 27 closed'); walk('A', [1920, 2020]); walk('A', [1920, 2060], true); sw('blocked'); door(27, true); sw('door 27 open') walk('A', [1920, 2300]); sw('R5') walk('B', [700, 2430]); walk('B', [1400, 2430]); walk('B', [1700, 2430]); sw('B visible?'); walk('B', [1000, 2430]); sw('B away') walk('A', [1920, 2500]); sw('M5 in view'); walk('M5', [2200, 2700]); sw('M5 away'); walk('A', [1700, 2700]); sw('end')