m1: devbox, nimble, layout; port geometry.nim and 33 tgeometry tests (98 checks)

Fixture tests/fixtures/scene.json copied from prototype data/; seed test no longer skips if missing.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 10:06:58 +01:00
co-authored by Claude Sonnet 5.5
parent daeadca9c2
commit 547458c37e
9 changed files with 1740 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
.devbox/
nix/
nimbledeps/
nimble.paths
nimcache/
tests/t[a-z]*
!tests/*.nim
!tests/fixtures/
+25
View File
@@ -0,0 +1,25 @@
{
"$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.17.2/.schema/devbox.schema.json",
"packages": {
"nim": "2",
"nimble": "latest",
"coreutils": "latest",
"gnused": "latest",
"diffutils": "latest",
"gnugrep": "latest"
},
"shell": {
"init_hook": [],
"scripts": {
"test": [
"mkdir -p results; nimble test -y > results/nimble-test.log 2>&1; rc=$?; cat results/nimble-test.log; exit $rc"
],
"mutation": [
"tools/mutation.sh"
]
}
},
"env": {
"NIMBLE_DIR": "$DEVBOX_PROJECT_ROOT/.devbox/nimble"
}
}
+335
View File
@@ -0,0 +1,335 @@
{
"lockfile_version": "1",
"packages": {
"coreutils@latest": {
"last_modified": "2026-10-08T19:21:32Z",
"resolved": "github:NixOS/nixpkgs/8edc0c72e3a38faf5434e40d1f19431125fe4b30#coreutils",
"source": "devbox-search",
"version": "9.11",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/82fb09q2gbhbhpc8ykkqwkcdj6h9c7f1-coreutils-9.11",
"default": true
},
{
"name": "info",
"path": "/nix/store/60lm7sjnjcw93h8c6bgk1lgams4a0z8z-coreutils-9.11-info"
}
],
"store_path": "/nix/store/82fb09q2gbhbhpc8ykkqwkcdj6h9c7f1-coreutils-9.11"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/dq1apsyiah48nmc4n45dpwc33k887hdn-coreutils-9.11",
"default": true
},
{
"name": "debug",
"path": "/nix/store/zf1kvml4h5wk3n22h17fnqvyzm1cjnbc-coreutils-9.11-debug"
},
{
"name": "info",
"path": "/nix/store/982a6ix3bby4pqw5gqs9lhzbkay0h19z-coreutils-9.11-info"
}
],
"store_path": "/nix/store/dq1apsyiah48nmc4n45dpwc33k887hdn-coreutils-9.11"
},
"x86_64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/rg42k0h3hj1mwldhywll29n7m0kcpxq2-coreutils-9.11",
"default": true
},
{
"name": "info",
"path": "/nix/store/8jc9262zif5m5gh6h5n1kbs5yrv594xn-coreutils-9.11-info"
}
],
"store_path": "/nix/store/rg42k0h3hj1mwldhywll29n7m0kcpxq2-coreutils-9.11"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/2gfxiwls9hbgwdwcy43mprchwsq36mg6-coreutils-9.11",
"default": true
},
{
"name": "info",
"path": "/nix/store/afwhyjd9mwhk09dql7n25q0l7005a5gd-coreutils-9.11-info"
},
{
"name": "debug",
"path": "/nix/store/phqk9ra56bnhhi6f5scyvnfbxfd7bffd-coreutils-9.11-debug"
}
],
"store_path": "/nix/store/2gfxiwls9hbgwdwcy43mprchwsq36mg6-coreutils-9.11"
}
}
},
"diffutils@latest": {
"last_modified": "2026-10-02T22:55:44Z",
"resolved": "github:NixOS/nixpkgs/55ba7f49ef2962b42cbd126522b7df5f95037679#diffutils",
"source": "devbox-search",
"version": "3.12",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/i5gfxrhmkxqjdk4r8ar1gq80sg180z9d-diffutils-3.12",
"default": true
},
{
"name": "info",
"path": "/nix/store/16plsg2g71qywwsakysmcyhnwxxqn81y-diffutils-3.12-info"
}
],
"store_path": "/nix/store/i5gfxrhmkxqjdk4r8ar1gq80sg180z9d-diffutils-3.12"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/19m79n6f7c3ay59121jqgacf8bw4wcq2-diffutils-3.12",
"default": true
},
{
"name": "info",
"path": "/nix/store/r6da78vrvnqfbrx7jlbmhwnj5kig5zik-diffutils-3.12-info"
}
],
"store_path": "/nix/store/19m79n6f7c3ay59121jqgacf8bw4wcq2-diffutils-3.12"
},
"x86_64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/lwghldxh6irjmbp73lpxv6mrdwb4aqhv-diffutils-3.12",
"default": true
},
{
"name": "info",
"path": "/nix/store/7rh8a74vwdb6lvvvwjd0dlpv84hl6mr6-diffutils-3.12-info"
}
],
"store_path": "/nix/store/lwghldxh6irjmbp73lpxv6mrdwb4aqhv-diffutils-3.12"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/1jhkxfbnx35m08gv5fyamp6ai5qhbjdh-diffutils-3.12",
"default": true
},
{
"name": "info",
"path": "/nix/store/lzbs18ypdiv6b4zvryfwr3sdy2n7f81l-diffutils-3.12-info"
}
],
"store_path": "/nix/store/1jhkxfbnx35m08gv5fyamp6ai5qhbjdh-diffutils-3.12"
}
}
},
"github:NixOS/nixpkgs/nixpkgs-unstable": {
"last_modified": "2026-10-20T14:23:57Z",
"resolved": "github:NixOS/nixpkgs/5adc50831c259f87faeb09ddf930d43597197473?lastModified=1792506237&narHash=sha256-Q%2F8KLB6jNfpaUN1HAedE%2Fu%2FEt9feE%2FzYSsutBLKrCFk%3D"
},
"gnugrep@latest": {
"last_modified": "2026-10-02T22:55:44Z",
"resolved": "github:NixOS/nixpkgs/55ba7f49ef2962b42cbd126522b7df5f95037679#gnugrep",
"source": "devbox-search",
"version": "3.12",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/f14505vy0sckbv11qb22s7yd2a7dxq1r-gnugrep-3.12",
"default": true
},
{
"name": "info",
"path": "/nix/store/h8bfc07gly890mlfy46ypgj3k9p8lcf2-gnugrep-3.12-info"
}
],
"store_path": "/nix/store/f14505vy0sckbv11qb22s7yd2a7dxq1r-gnugrep-3.12"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/82643wb56lminz3a4b6gk92d2g4ydwks-gnugrep-3.12",
"default": true
},
{
"name": "info",
"path": "/nix/store/abjzd0f148kdgb2c4hbmch63swjv6s03-gnugrep-3.12-info"
}
],
"store_path": "/nix/store/82643wb56lminz3a4b6gk92d2g4ydwks-gnugrep-3.12"
},
"x86_64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/c7yhik3dfpz5csbfc2v8lg0lgnfhz7wy-gnugrep-3.12",
"default": true
},
{
"name": "info",
"path": "/nix/store/pyd0kfynkm5h94ms170a5fvaf4yy57gz-gnugrep-3.12-info"
}
],
"store_path": "/nix/store/c7yhik3dfpz5csbfc2v8lg0lgnfhz7wy-gnugrep-3.12"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/3668q0d9zhc5n9l2pnyvpd44kz9bjsjm-gnugrep-3.12",
"default": true
},
{
"name": "info",
"path": "/nix/store/b9darrc5lshz7x9zxda2d5s9fkbdafk0-gnugrep-3.12-info"
}
],
"store_path": "/nix/store/3668q0d9zhc5n9l2pnyvpd44kz9bjsjm-gnugrep-3.12"
}
}
},
"gnused@latest": {
"last_modified": "2026-10-02T22:55:44Z",
"resolved": "github:NixOS/nixpkgs/55ba7f49ef2962b42cbd126522b7df5f95037679#gnused",
"source": "devbox-search",
"version": "4.10",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/s5mhqidsrm8aw2982k3mv82ajanirjm8-gnused-4.10",
"default": true
},
{
"name": "info",
"path": "/nix/store/f3i35na89qgpdblj1jf9qmpciq77imv3-gnused-4.10-info"
}
],
"store_path": "/nix/store/s5mhqidsrm8aw2982k3mv82ajanirjm8-gnused-4.10"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/2jk00rxplr3qjmjl2q46l7izbf8gxcs6-gnused-4.10",
"default": true
},
{
"name": "info",
"path": "/nix/store/4fklshzmpnha3wqvv91ivyr3ypfqncq8-gnused-4.10-info"
}
],
"store_path": "/nix/store/2jk00rxplr3qjmjl2q46l7izbf8gxcs6-gnused-4.10"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/1ggxndc1gd5061j5hqprhnhx9p30yilv-gnused-4.10",
"default": true
},
{
"name": "info",
"path": "/nix/store/c27vx9wa4v50lrb30d60sa57iga6mc98-gnused-4.10-info"
}
],
"store_path": "/nix/store/1ggxndc1gd5061j5hqprhnhx9p30yilv-gnused-4.10"
}
}
},
"nim@2": {
"last_modified": "2026-10-02T22:55:44Z",
"resolved": "github:NixOS/nixpkgs/55ba7f49ef2962b42cbd126522b7df5f95037679#nim",
"source": "devbox-search",
"version": "2.2.12",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/h055h3lxxsm8kql0mannw3706x740x7f-arm64-apple-darwin-nim-wrapper-2.2.12",
"default": true
}
],
"store_path": "/nix/store/h055h3lxxsm8kql0mannw3706x740x7f-arm64-apple-darwin-nim-wrapper-2.2.12"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/2fh7rg634j8969c1r394llrsjmmhaip7-aarch64-unknown-linux-gnu-nim-wrapper-2.2.12",
"default": true
}
],
"store_path": "/nix/store/2fh7rg634j8969c1r394llrsjmmhaip7-aarch64-unknown-linux-gnu-nim-wrapper-2.2.12"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/0cbdz34lsakfirycixckf6ypsd8hazhg-x86_64-unknown-linux-gnu-nim-wrapper-2.2.12",
"default": true
}
],
"store_path": "/nix/store/0cbdz34lsakfirycixckf6ypsd8hazhg-x86_64-unknown-linux-gnu-nim-wrapper-2.2.12"
}
}
},
"nimble@latest": {
"last_modified": "2026-10-02T22:55:44Z",
"resolved": "github:NixOS/nixpkgs/55ba7f49ef2962b42cbd126522b7df5f95037679#nimble",
"source": "devbox-search",
"version": "0.24.1",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/clxsxdsqjfcc43rfd7rad733nd0ndrkf-nimble-0.24.1",
"default": true
}
],
"store_path": "/nix/store/clxsxdsqjfcc43rfd7rad733nd0ndrkf-nimble-0.24.1"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/c5d0r4a8qi1vqs8q82pr3c652q8d6jv7-nimble-0.24.1",
"default": true
}
],
"store_path": "/nix/store/c5d0r4a8qi1vqs8q82pr3c652q8d6jv7-nimble-0.24.1"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/8f5wx2s80gnw0lkmdpbl63n1xlilcfxi-nimble-0.24.1",
"default": true
}
],
"store_path": "/nix/store/8f5wx2s80gnw0lkmdpbl63n1xlilcfxi-nimble-0.24.1"
}
}
}
}
}
+7
View File
@@ -0,0 +1,7 @@
version = "0.1.0"
author = "SirStone"
description = "FFF_vtt: server-authoritative vision VTT"
license = "MIT"
srcDir = "server"
requires "nim >= 2.0"
+2
View File
@@ -0,0 +1,2 @@
--path:"server"
--mm:orc
+46
View File
@@ -0,0 +1,46 @@
Info: Using the environment variable: NIMBLE_DIR='/home/davide/Projects/GDR/FFF_vtt/.devbox/nimble'
Prompt: No local packages.json found, download it from internet? -> [forced yes]
Downloading Official package list
Success Package list downloaded.
Info: using /nix/store/0cbdz34lsakfirycixckf6ypsd8hazhg-x86_64-unknown-linux-gnu-nim-wrapper-2.2.12/bin/nim for compilation
Compiling /home/davide/Projects/GDR/FFF_vtt/tests/tgeometry (from package fff_vtt) using c backend
Nim Output [Suite] visibility
... [OK] open space is a circle
... [OK] wall blocks
... [OK] shared endpoint: no leak at the vertex
... [OK] '<' apex: ray through the shared tip must not leak
... [OK] every segment endpoint is a polygon vertex (lower and upper end inclusive)
... [OK] rays nudged past a corner: area just beyond a wall end is visible
... [OK] shared endpoint: closed corner of box has no crack
... [OK] collinear walls act as one
... [OK] collinear walls with a gap let light through
... [OK] T-junction: no leak and correct shadow
... [OK] pillar mid-room casts a shadow, sides visible
... [OK] closed door blocks, open door passes
... [OK] token exactly at wall endpoint
... [OK] token at endpoint of free-standing wall sees along it
... [OK] long thin corridor
... [OK] wall endpoints behind the viewer: fan stays angularly ordered
... [OK] radius clips
... [Suite] moves
... [OK] crossing blocked, parallel ok
... [OK] grazing an endpoint is blocked
... [OK] no leak through shared vertex
... [OK] diagonal squeeze through corner vertex of L
... [OK] moving along a collinear wall is blocked
... [OK] doors
... [OK] zero-length move
... [Suite] cells
... [OK] square polygon
... [OK] tiny polygon inside one cell
... [OK] polygon spanning a cell with no vertex in it
... [OK] big polygon includes fully covered interior cells
... [Suite] seed
... [OK] every token in seed sees itself and is not walled in
... [Suite] token clearance (r=20)
... [OK] near pillar: circle overlapping blocked, clear ok
... [OK] near closed door blocks, open door passes
... [OK] 64px corridor and doorway passable on centerline
... [OK] legacy overlap can escape, not stay overlapped
Success: Execution finished
Success: All tests passed
+125
View File
@@ -0,0 +1,125 @@
## 2D vision geometry. Pure, stdlib only. Ported from prototype-server-vision/src/geometry.nim.
## Known limitations (see prototype-server-vision/HANDOFF.md, docs/DESIGN.md 5.4 / 12):
## - token exactly on a wall cannot move off it (inclusive segsCross); pinned by a KNOWN LIMITATION test.
## - closing a door on a token: policy open, DESIGN Q14 (5.4 vs H2 conflict); behaviour pinned in tests.
## - boundary epsilon ambiguity: RayEps 1e-4 rad, inclusive tests; token on a vision boundary is ambiguous
## (leak checker uses a 0.5 px neighbourhood).
## - move collision = centre path a->b + destination circle only, NOT the swept circle (corner clipping possible).
## - vision is O(rays x walls) per token, bbox cull only.
## - cellsTouched costs ~12x a polygon (results/bench.log in the prototype).
## - circle range limit is an inscribed 64-gon: error <= ~0.12% of R.
import std/[math, algorithm]
type
Vec* = tuple[x, y: float]
Wall* = object
a*, b*: Vec
door*, open*: bool
const
Eps = 1e-9 # geometric tolerance (px)
RayEps = 1e-4 # angular nudge past endpoints (rad)
proc blocks*(w: Wall): bool = not (w.door and w.open)
proc cross(a, b: Vec): float = a.x * b.y - a.y * b.x
proc `-`(a, b: Vec): Vec = (a.x - b.x, a.y - b.y)
proc rayHit(o, d: Vec, a, b: Vec): float =
## smallest t > Eps where o + t*d hits segment ab (end-inclusive), else Inf
let e = b - a
let den = cross(d, e)
if abs(den) < 1e-12: return Inf # parallel/collinear: grazing, other rays cover it
let ao = a - o
let t = cross(ao, e) / den
let u = cross(ao, d) / den
if t > Eps and u >= -Eps and u <= 1 + Eps: t else: Inf
proc visibilityPolygon*(o: Vec, walls: openArray[Wall], radius: float,
circleSegs = 64): seq[Vec] =
## CCW polygon of what `o` sees, bounded by an inscribed circleSegs-gon of `radius`.
var segs: seq[(Vec, Vec)]
var angles: seq[float]
for i in 0 ..< circleSegs:
let a0 = 2 * PI * i.float / circleSegs.float
let a1 = 2 * PI * (i + 1).float / circleSegs.float
segs.add(((o.x + radius * cos a0, o.y + radius * sin a0),
(o.x + radius * cos a1, o.y + radius * sin a1)))
angles.add arctan2(sin a0, cos a0) # same (-pi,pi] range as the wall angles, else the sort scrambles the fan
for w in walls:
if not w.blocks: continue
# shortcut: walls beyond the circle are only culled by ray clipping, O(rays*walls); upgrade: spatial grid
if max(w.a.x, w.b.x) < o.x - radius or min(w.a.x, w.b.x) > o.x + radius or
max(w.a.y, w.b.y) < o.y - radius or min(w.a.y, w.b.y) > o.y + radius: continue
segs.add((w.a, w.b))
for p in [w.a, w.b]:
if abs(p.x - o.x) < Eps and abs(p.y - o.y) < Eps: continue # token on endpoint
let ang = arctan2(p.y - o.y, p.x - o.x)
angles.add ang
angles.add ang - RayEps
angles.add ang + RayEps
angles.sort
for ang in angles:
let d: Vec = (cos ang, sin ang)
var best = Inf
for s in segs: best = min(best, rayHit(o, d, s[0], s[1]))
if best < Inf: result.add((o.x + d.x * best, o.y + d.y * best))
proc segsCross(p, q, a, b: Vec): bool =
## inclusive segment intersection (touching and collinear overlap count)
proc orient(a, b, c: Vec): int =
let v = cross(b - a, c - a)
if v > Eps: 1 elif v < -Eps: -1 else: 0
proc onSeg(a, b, c: Vec): bool = # c collinear with ab: within bbox?
c.x >= min(a.x, b.x) - Eps and c.x <= max(a.x, b.x) + Eps and
c.y >= min(a.y, b.y) - Eps and c.y <= max(a.y, b.y) + Eps
let o1 = orient(p, q, a); let o2 = orient(p, q, b)
let o3 = orient(a, b, p); let o4 = orient(a, b, q)
if o1 != o2 and o3 != o4: return true
(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 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) 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)
var j = poly.len - 1
for i in 0 ..< poly.len:
if (poly[i].y > p.y) != (poly[j].y > p.y) and
p.x < (poly[j].x - poly[i].x) * (p.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x:
result = not result
j = i
proc cellsTouched*(poly: openArray[Vec], cell: float): seq[(int, int)] =
## grid cells (ix, iy) overlapped by the polygon (area overlap, boundary touch included), sorted.
if poly.len < 3: return
var x0, y0 = Inf
var x1, y1 = -Inf
for p in poly:
x0 = min(x0, p.x); x1 = max(x1, p.x); y0 = min(y0, p.y); y1 = max(y1, p.y)
for iy in int(floor(y0 / cell)) .. int(floor(y1 / cell)):
for ix in int(floor(x0 / cell)) .. int(floor(x1 / cell)):
let (cx0, cy0, cx1, cy1) = (ix.float * cell, iy.float * cell, (ix + 1).float * cell, (iy + 1).float * cell)
let c = [(cx0, cy0), (cx1, cy0), (cx1, cy1), (cx0, cy1)]
# no edge crosses => cell wholly inside poly (a corner decides) or poly wholly inside cell (a vertex decides) or disjoint
var hit = inside(poly, c[0]) or
(poly[0].x >= cx0 and poly[0].x <= cx1 and poly[0].y >= cy0 and poly[0].y <= cy1)
if not hit:
for i in 0 ..< poly.len:
let p = poly[i]; let q = poly[(i + 1) mod poly.len]
for k in 0 .. 3:
if segsCross(p, q, c[k], c[(k + 1) mod 4]): hit = true; break
if hit: break
if hit: result.add (ix, iy)
+954
View File
@@ -0,0 +1,954 @@
{
"world": {
"w": 4096,
"h": 4096
},
"tileSize": 256,
"visionRadius": 400,
"tokenRadius": 20,
"exploredCell": 32,
"walls": [
{
"id": 0,
"a": [
256,
256
],
"b": [
1024,
256
],
"door": false,
"open": false
},
{
"id": 1,
"a": [
256,
1024
],
"b": [
608,
1024
],
"door": false,
"open": false
},
{
"id": 2,
"a": [
672,
1024
],
"b": [
1024,
1024
],
"door": false,
"open": false
},
{
"id": 3,
"a": [
256,
256
],
"b": [
256,
1024
],
"door": false,
"open": false
},
{
"id": 4,
"a": [
1024,
256
],
"b": [
1024,
608
],
"door": false,
"open": false
},
{
"id": 5,
"a": [
1024,
672
],
"b": [
1024,
1024
],
"door": false,
"open": false
},
{
"id": 6,
"a": [
1536,
256
],
"b": [
2304,
256
],
"door": false,
"open": false
},
{
"id": 7,
"a": [
1536,
1024
],
"b": [
1888,
1024
],
"door": false,
"open": false
},
{
"id": 8,
"a": [
1952,
1024
],
"b": [
2304,
1024
],
"door": false,
"open": false
},
{
"id": 9,
"a": [
1536,
256
],
"b": [
1536,
608
],
"door": false,
"open": false
},
{
"id": 10,
"a": [
1536,
608
],
"b": [
1536,
672
],
"door": true,
"open": false
},
{
"id": 11,
"a": [
1536,
672
],
"b": [
1536,
1024
],
"door": false,
"open": false
},
{
"id": 12,
"a": [
2304,
256
],
"b": [
2304,
608
],
"door": false,
"open": false
},
{
"id": 13,
"a": [
2304,
672
],
"b": [
2304,
1024
],
"door": false,
"open": false
},
{
"id": 14,
"a": [
2816,
256
],
"b": [
3200,
256
],
"door": false,
"open": false
},
{
"id": 15,
"a": [
2816,
1024
],
"b": [
3168,
1024
],
"door": false,
"open": false
},
{
"id": 16,
"a": [
3232,
1024
],
"b": [
3584,
1024
],
"door": false,
"open": false
},
{
"id": 17,
"a": [
2816,
256
],
"b": [
2816,
608
],
"door": false,
"open": false
},
{
"id": 18,
"a": [
2816,
672
],
"b": [
2816,
1024
],
"door": false,
"open": false
},
{
"id": 19,
"a": [
3584,
256
],
"b": [
3584,
1024
],
"door": false,
"open": false
},
{
"id": 20,
"a": [
256,
2048
],
"b": [
608,
2048
],
"door": false,
"open": false
},
{
"id": 21,
"a": [
672,
2048
],
"b": [
1024,
2048
],
"door": false,
"open": false
},
{
"id": 22,
"a": [
256,
2816
],
"b": [
1024,
2816
],
"door": false,
"open": false
},
{
"id": 23,
"a": [
256,
2048
],
"b": [
256,
2816
],
"door": false,
"open": false
},
{
"id": 24,
"a": [
1024,
2048
],
"b": [
1024,
2400
],
"door": false,
"open": false
},
{
"id": 25,
"a": [
1024,
2464
],
"b": [
1024,
2816
],
"door": false,
"open": false
},
{
"id": 26,
"a": [
1536,
2048
],
"b": [
1888,
2048
],
"door": false,
"open": false
},
{
"id": 27,
"a": [
1888,
2048
],
"b": [
1952,
2048
],
"door": true,
"open": true
},
{
"id": 28,
"a": [
1952,
2048
],
"b": [
2304,
2048
],
"door": false,
"open": false
},
{
"id": 29,
"a": [
1536,
2816
],
"b": [
2304,
2816
],
"door": false,
"open": false
},
{
"id": 30,
"a": [
1536,
2048
],
"b": [
1536,
2400
],
"door": false,
"open": false
},
{
"id": 31,
"a": [
1536,
2464
],
"b": [
1536,
2816
],
"door": false,
"open": false
},
{
"id": 32,
"a": [
2304,
2048
],
"b": [
2304,
2400
],
"door": false,
"open": false
},
{
"id": 33,
"a": [
2304,
2464
],
"b": [
2304,
2816
],
"door": false,
"open": false
},
{
"id": 34,
"a": [
2816,
2048
],
"b": [
3168,
2048
],
"door": false,
"open": false
},
{
"id": 35,
"a": [
3232,
2048
],
"b": [
3584,
2048
],
"door": false,
"open": false
},
{
"id": 36,
"a": [
2816,
2816
],
"b": [
3584,
2816
],
"door": false,
"open": false
},
{
"id": 37,
"a": [
2816,
2048
],
"b": [
2816,
2400
],
"door": false,
"open": false
},
{
"id": 38,
"a": [
2816,
2464
],
"b": [
2816,
2816
],
"door": false,
"open": false
},
{
"id": 39,
"a": [
3584,
2048
],
"b": [
3584,
2816
],
"door": false,
"open": false
},
{
"id": 40,
"a": [
3200,
256
],
"b": [
3584,
256
],
"door": false,
"open": false
},
{
"id": 41,
"a": [
1024,
608
],
"b": [
1536,
608
],
"door": false,
"open": false
},
{
"id": 42,
"a": [
1024,
672
],
"b": [
1536,
672
],
"door": false,
"open": false
},
{
"id": 43,
"a": [
2304,
608
],
"b": [
2816,
608
],
"door": false,
"open": false
},
{
"id": 44,
"a": [
2304,
672
],
"b": [
2816,
672
],
"door": false,
"open": false
},
{
"id": 45,
"a": [
1024,
2400
],
"b": [
1536,
2400
],
"door": false,
"open": false
},
{
"id": 46,
"a": [
1024,
2464
],
"b": [
1536,
2464
],
"door": false,
"open": false
},
{
"id": 47,
"a": [
2304,
2400
],
"b": [
2816,
2400
],
"door": false,
"open": false
},
{
"id": 48,
"a": [
2304,
2464
],
"b": [
2816,
2464
],
"door": false,
"open": false
},
{
"id": 49,
"a": [
1888,
1024
],
"b": [
1888,
2048
],
"door": false,
"open": false
},
{
"id": 50,
"a": [
1952,
1024
],
"b": [
1952,
2048
],
"door": false,
"open": false
},
{
"id": 51,
"a": [
3168,
1024
],
"b": [
3168,
2048
],
"door": false,
"open": false
},
{
"id": 52,
"a": [
3232,
1024
],
"b": [
3232,
2048
],
"door": false,
"open": false
},
{
"id": 53,
"a": [
608,
1024
],
"b": [
608,
2048
],
"door": false,
"open": false
},
{
"id": 54,
"a": [
672,
1024
],
"b": [
672,
2048
],
"door": false,
"open": false
},
{
"id": 55,
"a": [
600,
600
],
"b": [
680,
600
],
"door": false,
"open": false
},
{
"id": 56,
"a": [
600,
680
],
"b": [
680,
680
],
"door": false,
"open": false
},
{
"id": 57,
"a": [
600,
600
],
"b": [
600,
680
],
"door": false,
"open": false
},
{
"id": 58,
"a": [
680,
600
],
"b": [
680,
680
],
"door": false,
"open": false
},
{
"id": 59,
"a": [
3000,
700
],
"b": [
3000,
1024
],
"door": false,
"open": false
}
],
"floors": [
[
256,
256,
1024,
1024
],
[
1536,
256,
2304,
1024
],
[
2816,
256,
3584,
1024
],
[
256,
2048,
1024,
2816
],
[
1536,
2048,
2304,
2816
],
[
2816,
2048,
3584,
2816
],
[
1024,
608,
1536,
672
],
[
2304,
608,
2816,
672
],
[
1024,
2400,
1536,
2464
],
[
2304,
2400,
2816,
2464
],
[
1888,
1024,
1952,
2048
],
[
3168,
1024,
3232,
2048
],
[
608,
1024,
672,
2048
]
],
"tokens": [
{
"id": "A",
"kind": "player",
"name": "Player A",
"pos": [
400,
400
],
"gmOnly": false
},
{
"id": "B",
"kind": "player",
"name": "Player B",
"pos": [
400,
2200
],
"gmOnly": false
},
{
"id": "M1",
"kind": "monster",
"name": "Monster 1",
"pos": [
900,
900
],
"gmOnly": true
},
{
"id": "M2",
"kind": "monster",
"name": "Monster 2",
"pos": [
1920,
640
],
"gmOnly": true
},
{
"id": "M3",
"kind": "monster",
"name": "Monster 3",
"pos": [
3400,
500
],
"gmOnly": true
},
{
"id": "M4",
"kind": "monster",
"name": "Monster 4",
"pos": [
900,
2700
],
"gmOnly": true
},
{
"id": "M5",
"kind": "monster",
"name": "Monster 5",
"pos": [
1920,
2400
],
"gmOnly": true
},
{
"id": "M6",
"kind": "monster",
"name": "Monster 6",
"pos": [
3200,
2400
],
"gmOnly": true
}
]
}
+238
View File
@@ -0,0 +1,238 @@
import std/[unittest, json, os]
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)]
proc sees(walls: openArray[Wall], o, p: Vec, r = 500.0): bool =
visibilityPolygon(o, walls, r).inside(p)
proc hasVertex(poly: openArray[Vec], p: Vec): bool =
for v in poly:
if abs(v.x - p.x) < 1e-6 and abs(v.y - p.y) < 1e-6: return true
suite "visibility":
test "open space is a circle":
let poly = visibilityPolygon((0.0, 0.0), @[], 100)
check poly.inside((60.0, 0.0))
check not poly.inside((101.0, 0.0))
check poly.inside((0.0, -95.0))
test "wall blocks":
let w = @[W(50, -50, 50, 50)]
check not sees(w, (0.0, 0.0), (80.0, 0.0))
check sees(w, (0.0, 0.0), (40.0, 0.0))
test "shared endpoint: no leak at the vertex":
# L corner at (50,0): vertical arm (50,-50)-(50,0), horizontal arm (50,0)-(100,0)
let w = @[W(50, -50, 50, 0), W(50, 0, 100, 0)]
# viewer on the diagonal through the shared vertex: the ray touches both arms' endpoint
let v: Vec = (0.0, -25.0)
check visibilityPolygon(v, w, 500).hasVertex((50.0, 0.0))
check not sees(w, v, (70.0, 9.0)) # just below the ray through the vertex (y=10 at x=70): vertical arm shadows it
check sees(w, v, (70.0, 11.0)) # just above the ray: past the vertex, the corner is not a crack
# token on the line of the horizontal arm: a point just above the line is visible past the arm's end-on
# edge, just below it is shadowed by the vertical arm
check sees(w, (0.0, 0.0), (70.0, 5.0))
check not sees(w, (0.0, 0.0), (70.0, -5.0))
test "'<' apex: ray through the shared tip must not leak":
for flip in [false, true]: # each wall in both directions: the tip is an upper end of one and a lower end of the other
let w = if flip: @[W(100, 0, 50, -40), W(100, 0, 50, 40)] else: @[W(50, -40, 100, 0), W(50, 40, 100, 0)]
check visibilityPolygon((0.0, 0.0), w, 500).hasVertex((100.0, 0.0))
check not sees(w, (0.0, 0.0), (150.0, 0.0))
check not sees(w, (0.0, 0.0), (300.0, 0.0))
check sees(w, (0.0, 0.0), (90.0, 0.0))
test "every segment endpoint is a polygon vertex (lower and upper end inclusive)":
# free-standing wall, nothing else shares the endpoints: an exclusive end would drop the exact-endpoint ray
let w = @[W(50, -30, 80, 10)]
let poly = visibilityPolygon((0.0, 0.0), w, 500)
check poly.hasVertex((50.0, -30.0)) # lower end (u = 0)
check poly.hasVertex((80.0, 10.0)) # upper end (u = 1)
let r = @[W(80, 10, 50, -30)] # same wall, ends swapped
let pr = visibilityPolygon((0.0, 0.0), r, 500)
check pr.hasVertex((50.0, -30.0))
check pr.hasVertex((80.0, 10.0))
test "rays nudged past a corner: area just beyond a wall end is visible":
# viewer heights are chosen so the corner ray sits mid-way between two circle rays: no other ray can mask a missing nudge ray
# wall (50,-50)-(50,0) seen from (0,7.4): the ray through the end (50,0) continues to (100,-7.4)
let w = @[W(50, -50, 50, 0)]
check sees(w, (0.0, 7.4), (100.0, -6.0)) # lit side of that ray
check not sees(w, (0.0, 7.4), (100.0, -9.0)) # shadow side
# mirror: wall (50,0)-(50,50) seen from (0,-7.4)
let m = @[W(50, 0, 50, 50)]
check sees(m, (0.0, -7.4), (100.0, 6.0))
check not sees(m, (0.0, -7.4), (100.0, 9.0))
# box (pillar) seen from (0,-24): the rays through corners (100,50) and (100,-50) continue to (200,124) and (200,-76)
let b = box(100, -50, 150, 50)
check sees(b, (0.0, -24.0), (200.0, 130.0))
check not sees(b, (0.0, -24.0), (200.0, 118.0))
check sees(b, (0.0, -24.0), (200.0, -85.0))
check not sees(b, (0.0, -24.0), (200.0, -70.0))
test "shared endpoint: closed corner of box has no crack":
let w = box(-100, -100, 100, 100)
check not sees(w, (0.0, 0.0), (130.0, 130.0), 1000)
check not sees(w, (-90.0, -90.0), (-130.0, -130.0), 1000)
check sees(w, (0.0, 0.0), (95.0, 95.0), 1000) # the corner itself is visible from inside
check sees(w, (-90.0, -90.0), (90.0, 90.0), 1000)
test "collinear walls act as one":
let w = @[W(50, -50, 50, 0), W(50, 0, 50, 50)]
check not sees(w, (0.0, 0.0), (80.0, 0.0)) # ray through the join
check not sees(w, (0.0, 30.0), (80.0, 30.0))
check sees(w, (0.0, 0.0), (40.0, 0.0)) # in front of the join
check sees(w, (0.0, 0.0), (-80.0, 0.0)) # behind the viewer
check sees(w, (0.0, 0.0), (80.0, 100.0)) # past the far end of the line
check not sees(w, (0.0, 0.0), (80.0, -30.0))
test "collinear walls with a gap let light through":
let w = @[W(50, -50, 50, -5), W(50, 5, 50, 50)]
check sees(w, (0.0, 0.0), (80.0, 0.0))
check not sees(w, (0.0, 0.0), (80.0, 30.0))
test "T-junction: no leak and correct shadow":
# horizontal y=0 from x=-100..100, vertical stem from (0,0) up to (0,-100)
let w = @[W(-100, 0, 100, 0), W(0, 0, 0, -100)]
check not sees(w, (-30.0, -30.0), (30.0, -30.0)) # stem blocks
check not sees(w, (-30.0, -30.0), (-30.0, 30.0)) # base blocks
check sees(w, (-30.0, -30.0), (-80.0, -60.0))
check not sees(w, (-30.0, -30.0), (10.0, -5.0), 500)
check sees(w, (-30.0, -30.0), (-10.0, -5.0)) # same side of the stem, above the base
check sees(w, (30.0, -30.0), (80.0, -60.0)) # mirror side
check not sees(w, (30.0, -30.0), (-10.0, -5.0))
test "pillar mid-room casts a shadow, sides visible":
var w = box(-200, -200, 200, 200)
w.add box(-10, -10, 10, 10)
check not sees(w, (-100.0, 0.0), (100.0, 0.0))
check sees(w, (-100.0, 0.0), (100.0, 50.0))
check sees(w, (-100.0, 0.0), (0.0, 50.0))
check not sees(w, (-100.0, 0.0), (50.0, 3.0))
test "closed door blocks, open door passes":
let closed = @[W(50, -50, 50, -10), W(50, -10, 50, 10, true, false), W(50, 10, 50, 50)]
var opened = closed
opened[1].open = true
check not sees(closed, (0.0, 0.0), (90.0, 0.0))
check sees(opened, (0.0, 0.0), (90.0, 0.0))
check not sees(opened, (0.0, 0.0), (90.0, 40.0))
test "token exactly at wall endpoint":
let w = @[W(0, 0, 100, 0), W(0, 0, 0, 100)] # corner, token on it
let poly = visibilityPolygon((0.0, 0.0), w, 200)
check poly.inside((-50.0, -50.0)) # open outward quadrant
check poly.inside((100.0, -30.0))
check poly.inside((50.0, 50.0)) # arms radiate from the token: the inner quadrant is open too
check poly.inside((-50.0, 50.0))
check not poly.inside((150.0, 150.0)) # beyond the radius (212 > 200)
check not poly.inside((0.0, 250.0))
test "token at endpoint of free-standing wall sees along it":
let w = @[W(0, 0, 100, 0)]
check sees(w, (0.0, 0.0), (50.0, -20.0))
check sees(w, (0.0, 0.0), (-50.0, 20.0))
check sees(w, (0.0, 0.0), (50.0, 20.0))
check not sees(w, (0.0, 0.0), (600.0, 0.0)) # beyond the 500 radius
test "long thin corridor":
# 1000 long, 20 wide, token at one end; radius 2000
let w = @[W(0, -10, 1000, -10), W(0, 10, 1000, 10), W(1000, -10, 1000, 10)]
check sees(w, (5.0, 0.0), (990.0, 0.0), 2000)
check not sees(w, (5.0, 0.0), (500.0, 15.0), 2000)
check not sees(w, (5.0, 0.0), (1010.0, 0.0), 2000)
check sees(w, (5.0, 0.0), (900.0, 8.0), 2000)
test "wall endpoints behind the viewer: fan stays angularly ordered":
# regression: circle angles in [0,2pi) were sorted against atan2 angles in (-pi,pi] -> bow-tie polygon
let w = box(600, 600, 680, 680) & @[W(1024, 256, 1024, 608)]
check sees(w, (908.66, 672.03), (789.36, 584.65), 400)
check sees(w, (789.36, 584.65), (908.66, 672.03), 400)
check not sees(w, (908.66, 672.03), (1100.0, 500.0), 400) # the x=1024 wall (y 256..608) is in the way
test "radius clips":
check not sees(@[], (0.0, 0.0), (300.0, 0.0), 200)
suite "moves":
let wall = W(0, -50, 0, 50)
test "crossing blocked, parallel ok":
check moveBlocked([wall], (-10.0, 0.0), (10.0, 0.0))
check not moveBlocked([wall], (-10.0, 0.0), (-10.0, 30.0))
check not moveBlocked([wall], (-10.0, 60.0), (10.0, 60.0)) # passes beyond the end
test "grazing an endpoint is blocked":
check moveBlocked([wall], (-10.0, 50.0), (10.0, 50.0))
check moveBlocked([wall], (-10.0, -60.0), (10.0, -40.0)) # crosses
test "no leak through shared vertex":
let w = [W(0, -50, 0, 0), W(0, 0, 0, 50)]
check moveBlocked(w, (-10.0, 0.0), (10.0, 0.0))
test "diagonal squeeze through corner vertex of L":
let w = [W(0, 0, 50, 0), W(0, 0, 0, 50)]
check moveBlocked(w, (10.0, -10.0), (-10.0, 10.0))
test "moving along a collinear wall is blocked":
check moveBlocked([wall], (0.0, -100.0), (0.0, 100.0))
test "doors":
var d = W(0, -10, 0, 10, true, false)
check moveBlocked([d], (-5.0, 0.0), (5.0, 0.0))
d.open = true
check not moveBlocked([d], (-5.0, 0.0), (5.0, 0.0))
test "zero-length move":
check not moveBlocked([wall], (-5.0, 0.0), (-5.0, 0.0))
suite "cells":
test "square polygon":
let sq = [(10.0, 10.0), (50.0, 10.0), (50.0, 50.0), (10.0, 50.0)]
check cellsTouched(sq, 32) == @[(0, 0), (1, 0), (0, 1), (1, 1)]
test "tiny polygon inside one cell":
check cellsTouched([(1.0, 1.0), (5.0, 1.0), (3.0, 5.0)], 32) == @[(0, 0)]
test "polygon spanning a cell with no vertex in it":
# thin horizontal strip crossing 3 cells
let strip = [(1.0, 10.0), (95.0, 10.0), (95.0, 12.0), (1.0, 12.0)]
check cellsTouched(strip, 32) == @[(0, 0), (1, 0), (2, 0)]
test "big polygon includes fully covered interior cells":
let big = [(0.0, 0.0), (200.0, 0.0), (200.0, 200.0), (0.0, 200.0)]
check (3, 3) in cellsTouched(big, 32)
suite "seed":
test "every token in seed sees itself and is not walled in":
# fixture copied from prototype-server-vision/data/scene.json (tests/fixtures/); a missing file fails, never skips
let sp = currentSourcePath().parentDir / "fixtures" / "scene.json"
block:
let j = parseFile(sp)
var walls: seq[Wall]
for w in j["walls"]:
walls.add Wall(a: (w["a"][0].getFloat, w["a"][1].getFloat), b: (w["b"][0].getFloat, w["b"][1].getFloat),
door: w{"door"}.getBool, open: w{"open"}.getBool)
for t in j["tokens"]:
let p: Vec = (t["pos"][0].getFloat, t["pos"][1].getFloat)
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, 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)