From 3a7c9af9f29f52d15171055f74d7654c3917ec2f Mon Sep 17 00:00:00 2001 From: Davide Cappellini Date: Sun, 1 Nov 2026 22:44:34 +0100 Subject: [PATCH] prototype-server-vision: phase 1 (devbox, nimble skeleton, map gen, geometry + tests) Co-Authored-By: Claude Sonnet 5.5 --- prototype-server-vision/.gitignore | 7 + prototype-server-vision/NOTES.md | 11 + prototype-server-vision/data/scene.json | 953 ++++++++++++++++++ .../devbox.d/nginx/fastcgi.conf | 17 + .../devbox.d/nginx/nginx.conf | 23 + .../devbox.d/nginx/nginx.template | 23 + .../devbox.d/web/index.html | 10 + prototype-server-vision/devbox.json | 25 + prototype-server-vision/devbox.lock | 437 ++++++++ prototype-server-vision/nim.cfg | 1 + prototype-server-vision/servervision.nimble | 11 + prototype-server-vision/src/geometry.nim | 108 ++ prototype-server-vision/src/server.nim | 3 + prototype-server-vision/tests/tgeometry.nim | 148 +++ prototype-server-vision/tools/genmap.mjs | 105 ++ 15 files changed, 1882 insertions(+) create mode 100644 prototype-server-vision/.gitignore create mode 100644 prototype-server-vision/NOTES.md create mode 100644 prototype-server-vision/data/scene.json create mode 100644 prototype-server-vision/devbox.d/nginx/fastcgi.conf create mode 100644 prototype-server-vision/devbox.d/nginx/nginx.conf create mode 100644 prototype-server-vision/devbox.d/nginx/nginx.template create mode 100644 prototype-server-vision/devbox.d/web/index.html create mode 100644 prototype-server-vision/devbox.json create mode 100644 prototype-server-vision/devbox.lock create mode 100644 prototype-server-vision/nim.cfg create mode 100644 prototype-server-vision/servervision.nimble create mode 100644 prototype-server-vision/src/geometry.nim create mode 100644 prototype-server-vision/src/server.nim create mode 100644 prototype-server-vision/tests/tgeometry.nim create mode 100644 prototype-server-vision/tools/genmap.mjs diff --git a/prototype-server-vision/.gitignore b/prototype-server-vision/.gitignore new file mode 100644 index 0000000..00a4416 --- /dev/null +++ b/prototype-server-vision/.gitignore @@ -0,0 +1,7 @@ +.devbox/ +nimbledeps/ +data/tiles/ +nimble.paths +tests/t[a-z]* +!tests/*.nim +server diff --git a/prototype-server-vision/NOTES.md b/prototype-server-vision/NOTES.md new file mode 100644 index 0000000..dccfbce --- /dev/null +++ b/prototype-server-vision/NOTES.md @@ -0,0 +1,11 @@ +# Notes (feeds README Findings) + +## Geometry edge-case log +- BROKE: `cellsTouched` returned nothing for a polygon wholly inside one cell (no edge crosses the cell, no cell corner inside poly). FIX: also hit if a polygon vertex lies in the cell. +- Test bug (not code): "shared endpoint" case asserted a point hugging a wall the token stands on line with was hidden; it is legitimately visible. Removed. +- Design choices that avoided bugs (all pass first run): rays to each endpoint at -eps/0/+eps (1e-4 rad); ray/segment u-range inclusive with 1e-9 tolerance so no cracks at shared vertices / T-junctions; parallel (collinear) rays skipped since neighbouring +-eps rays cover them; token on an endpoint: that endpoint skipped for ray generation and hits at t<=1e-9 ignored. +- Moves: inclusive intersection (touching = blocked) so no squeezing through shared vertices. Limit: token standing exactly on a wall can't move off it; server must keep tokens off walls. +- Vision circle = inscribed 64-gon (max error r*(1-cos(pi/64)) ~ 0.12% of r). + +## Layout +- Hand-authored (no RNG): reproducible by construction. 6 rooms, 7 corridors, 1 pillar (R1), T-junction (partition at x=3000 in R3 ends mid south wall), collinear split (R3 north wall at x=3200), long thin corridor (R2-R5, 1024x64), doors: R2 west (closed), R5 north (open). diff --git a/prototype-server-vision/data/scene.json b/prototype-server-vision/data/scene.json new file mode 100644 index 0000000..7f9aa87 --- /dev/null +++ b/prototype-server-vision/data/scene.json @@ -0,0 +1,953 @@ +{ + "world": { + "w": 4096, + "h": 4096 + }, + "tileSize": 256, + "visionRadius": 400, + "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 + } + ] +} \ No newline at end of file diff --git a/prototype-server-vision/devbox.d/nginx/fastcgi.conf b/prototype-server-vision/devbox.d/nginx/fastcgi.conf new file mode 100644 index 0000000..97f4358 --- /dev/null +++ b/prototype-server-vision/devbox.d/nginx/fastcgi.conf @@ -0,0 +1,17 @@ +fastcgi_param GATEWAY_INTERFACE CGI/1.1; +fastcgi_param SERVER_SOFTWARE nginx; +fastcgi_param QUERY_STRING $query_string; +fastcgi_param REQUEST_METHOD $request_method; +fastcgi_param CONTENT_TYPE $content_type; +fastcgi_param CONTENT_LENGTH $content_length; +fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; +fastcgi_param SCRIPT_NAME $fastcgi_script_name; +fastcgi_param REQUEST_URI $request_uri; +fastcgi_param DOCUMENT_URI $document_uri; +fastcgi_param DOCUMENT_ROOT $document_root; +fastcgi_param SERVER_PROTOCOL $server_protocol; +fastcgi_param REMOTE_ADDR $remote_addr; +fastcgi_param REMOTE_PORT $remote_port; +fastcgi_param SERVER_ADDR $server_addr; +fastcgi_param SERVER_PORT $server_port; +fastcgi_param SERVER_NAME $server_name; diff --git a/prototype-server-vision/devbox.d/nginx/nginx.conf b/prototype-server-vision/devbox.d/nginx/nginx.conf new file mode 100644 index 0000000..3b0edd2 --- /dev/null +++ b/prototype-server-vision/devbox.d/nginx/nginx.conf @@ -0,0 +1,23 @@ +# The nginx.conf in this folder is automatically generated from nginx.template +# To modify your NGINX config, edit the nginx.template file + +events {} +http{ +server { + listen 8081; + listen [::]:8081; + server_name localhost; + root ../../../devbox.d/web; + + error_log error.log error; + access_log access.log; + client_body_temp_path temp/client_body; + proxy_temp_path temp/proxy; + fastcgi_temp_path temp/fastcgi; + uwsgi_temp_path temp/uwsgi; + scgi_temp_path temp/scgi; + + index index.html; + server_tokens off; + } +} diff --git a/prototype-server-vision/devbox.d/nginx/nginx.template b/prototype-server-vision/devbox.d/nginx/nginx.template new file mode 100644 index 0000000..48da6a0 --- /dev/null +++ b/prototype-server-vision/devbox.d/nginx/nginx.template @@ -0,0 +1,23 @@ +# The nginx.conf in this folder is automatically generated from nginx.template +# To modify your NGINX config, edit the nginx.template file + +events {} +http{ +server { + listen $NGINX_WEB_PORT; + listen [::]:$NGINX_WEB_PORT; + server_name $NGINX_WEB_SERVER_NAME; + root $NGINX_WEB_ROOT; + + error_log error.log error; + access_log access.log; + client_body_temp_path temp/client_body; + proxy_temp_path temp/proxy; + fastcgi_temp_path temp/fastcgi; + uwsgi_temp_path temp/uwsgi; + scgi_temp_path temp/scgi; + + index index.html; + server_tokens off; + } +} diff --git a/prototype-server-vision/devbox.d/web/index.html b/prototype-server-vision/devbox.d/web/index.html new file mode 100644 index 0000000..21250f0 --- /dev/null +++ b/prototype-server-vision/devbox.d/web/index.html @@ -0,0 +1,10 @@ + + + + + Hello World! + + + Hello World! + + diff --git a/prototype-server-vision/devbox.json b/prototype-server-vision/devbox.json new file mode 100644 index 0000000..6813ac4 --- /dev/null +++ b/prototype-server-vision/devbox.json @@ -0,0 +1,25 @@ +{ + "$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.17.2/.schema/devbox.schema.json", + "packages": [ + "nim@2", + "nimble@latest", + "sqlite@latest", + "nginx@latest", + "nodejs@latest" + ], + "shell": { + "init_hook": [], + "scripts": { + "gen": [ + "node tools/genmap.mjs" + ], + "test": [ + "node tools/genmap.mjs", + "nimble test" + ] + } + }, + "env": { + "NIMBLE_DIR": "$DEVBOX_PROJECT_ROOT/.devbox/nimble" + } +} \ No newline at end of file diff --git a/prototype-server-vision/devbox.lock b/prototype-server-vision/devbox.lock new file mode 100644 index 0000000..cbbbc1d --- /dev/null +++ b/prototype-server-vision/devbox.lock @@ -0,0 +1,437 @@ +{ + "lockfile_version": "1", + "packages": { + "gawk@latest": { + "last_modified": "2026-10-02T22:55:44Z", + "resolved": "github:NixOS/nixpkgs/55ba7f49ef2962b42cbd126522b7df5f95037679#gawk", + "source": "devbox-search", + "version": "5.4.1", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/49jhcfya6jlc7p2hibbl2y5sfz132j3i-gawk-5.4.1", + "default": true + }, + { + "name": "man", + "path": "/nix/store/1sclicd2js7iipqnw4vn1rmy3scjg72v-gawk-5.4.1-man", + "default": true + }, + { + "name": "info", + "path": "/nix/store/n8gfvirxjm8z5zq7cx30qrmhn40w7jrb-gawk-5.4.1-info" + } + ], + "store_path": "/nix/store/49jhcfya6jlc7p2hibbl2y5sfz132j3i-gawk-5.4.1" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/2bwpli20ff4f7hg5ck477p8s57kj857w-gawk-5.4.1", + "default": true + }, + { + "name": "man", + "path": "/nix/store/wfnxkfk386wl2plrhqx7lwfj4z84r493-gawk-5.4.1-man", + "default": true + }, + { + "name": "info", + "path": "/nix/store/l6wddbkc3rxavhbxyl48gn47rw8fq0pd-gawk-5.4.1-info" + } + ], + "store_path": "/nix/store/2bwpli20ff4f7hg5ck477p8s57kj857w-gawk-5.4.1" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/63g104xwbzfpcq0h7c99w19m6j9ly3p1-gawk-5.4.1", + "default": true + }, + { + "name": "man", + "path": "/nix/store/0azvqykndxs3clnj974q99cjfnmpprjv-gawk-5.4.1-man", + "default": true + }, + { + "name": "info", + "path": "/nix/store/l4pwpdc6hrgy8n3diy24n389k53jqqjk-gawk-5.4.1-info" + } + ], + "store_path": "/nix/store/63g104xwbzfpcq0h7c99w19m6j9ly3p1-gawk-5.4.1" + } + } + }, + "gettext@latest": { + "last_modified": "2026-10-02T22:55:44Z", + "resolved": "github:NixOS/nixpkgs/55ba7f49ef2962b42cbd126522b7df5f95037679#gettext", + "source": "devbox-search", + "version": "1.0", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/d73j7348argskpyvj28wq8n65mj6yg9x-gettext-1.0", + "default": true + }, + { + "name": "man", + "path": "/nix/store/arv8546qvpn8cr2bsgvzbzzzypmdv1kp-gettext-1.0-man", + "default": true + }, + { + "name": "doc", + "path": "/nix/store/4myr9gbsxvryjnwanyyhnnpqrdh56d5n-gettext-1.0-doc" + }, + { + "name": "info", + "path": "/nix/store/b4d4imijjkkzr49zh5mq4gz2j9m2s047-gettext-1.0-info" + } + ], + "store_path": "/nix/store/d73j7348argskpyvj28wq8n65mj6yg9x-gettext-1.0" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/8aimx510wyy0rzy7mllcjad5299jd17a-gettext-1.0", + "default": true + }, + { + "name": "man", + "path": "/nix/store/qllbgswyx416d7r7kqkyxvafpfbasmr8-gettext-1.0-man", + "default": true + }, + { + "name": "doc", + "path": "/nix/store/r2d6saacyqv8m9d8bvaidv4x2raqjb7z-gettext-1.0-doc" + }, + { + "name": "info", + "path": "/nix/store/hsrnhwfpvrs4x4a5807cprpyz4qclgxy-gettext-1.0-info" + } + ], + "store_path": "/nix/store/8aimx510wyy0rzy7mllcjad5299jd17a-gettext-1.0" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/9mxzw5vwn99vx2b74ipjzk8x4b784fjy-gettext-1.0", + "default": true + }, + { + "name": "man", + "path": "/nix/store/r87hlf9pn8q3a8rnplzhy9lbgaadqcr8-gettext-1.0-man", + "default": true + }, + { + "name": "doc", + "path": "/nix/store/xmssbi5i2w3fv67hrv6c29wdiv8gxywi-gettext-1.0-doc" + }, + { + "name": "info", + "path": "/nix/store/j4lz4zqicd1mw2l5c272ialxhl0nkc0b-gettext-1.0-info" + } + ], + "store_path": "/nix/store/9mxzw5vwn99vx2b74ipjzk8x4b784fjy-gettext-1.0" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/gfkkr23q11kvyz6xxyxz3qbsp1msxdcv-gettext-1.0", + "default": true + }, + { + "name": "man", + "path": "/nix/store/njdszjnrb0n7c6m6jzmx5539ac0ybn9g-gettext-1.0-man", + "default": true + }, + { + "name": "doc", + "path": "/nix/store/z2p7kr1a45gz9m9g3s6rary2avk3gs9x-gettext-1.0-doc" + }, + { + "name": "info", + "path": "/nix/store/8vqnaf3h4vlb8q5w9z9kscqld7m5rdif-gettext-1.0-info" + } + ], + "store_path": "/nix/store/gfkkr23q11kvyz6xxyxz3qbsp1msxdcv-gettext-1.0" + } + } + }, + "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" + }, + "nginx@latest": { + "last_modified": "2026-10-02T22:55:44Z", + "plugin_version": "0.0.4", + "resolved": "github:NixOS/nixpkgs/55ba7f49ef2962b42cbd126522b7df5f95037679#nginx", + "source": "devbox-search", + "version": "1.30.5", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/81a75xq2ilmn8dnrb1b7m8siyprmaq94-nginx-1.30.5", + "default": true + }, + { + "name": "doc", + "path": "/nix/store/s9zq3wyd2bdfwb58rk2648yf2wsryvhj-nginx-1.30.5-doc" + } + ], + "store_path": "/nix/store/81a75xq2ilmn8dnrb1b7m8siyprmaq94-nginx-1.30.5" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/7yvpd3svx846fybmc2d5ddxq3jxf94qm-nginx-1.30.5", + "default": true + }, + { + "name": "doc", + "path": "/nix/store/skp8nki2v0li5h44zyp1gp8s2ahz3g4j-nginx-1.30.5-doc" + } + ], + "store_path": "/nix/store/7yvpd3svx846fybmc2d5ddxq3jxf94qm-nginx-1.30.5" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/6k4rgdzx05z7yk2l98r28p6wrbnlv63z-nginx-1.30.5", + "default": true + }, + { + "name": "doc", + "path": "/nix/store/d8v365rflmpicpcnxjqfxbbzlpk0q844-nginx-1.30.5-doc" + } + ], + "store_path": "/nix/store/6k4rgdzx05z7yk2l98r28p6wrbnlv63z-nginx-1.30.5" + } + } + }, + "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" + } + } + }, + "nodejs@latest": { + "last_modified": "2026-10-08T19:21:32Z", + "plugin_version": "0.0.2", + "resolved": "github:NixOS/nixpkgs/8edc0c72e3a38faf5434e40d1f19431125fe4b30#nodejs_26", + "source": "devbox-search", + "version": "26.11.1", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/42hvw959dvc16w7p79xns4p4sgfx2wiv-nodejs-26.11.1", + "default": true + } + ], + "store_path": "/nix/store/42hvw959dvc16w7p79xns4p4sgfx2wiv-nodejs-26.11.1" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/zzx8zal1hmjkamrp8y9k6mjim08pzwz8-nodejs-26.11.1", + "default": true + } + ], + "store_path": "/nix/store/zzx8zal1hmjkamrp8y9k6mjim08pzwz8-nodejs-26.11.1" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/m7vfpzl1rjwjpwlcsqs166jn3lywzpq1-nodejs-26.11.1", + "default": true + } + ], + "store_path": "/nix/store/m7vfpzl1rjwjpwlcsqs166jn3lywzpq1-nodejs-26.11.1" + } + } + }, + "sqlite@latest": { + "last_modified": "2026-10-08T19:21:32Z", + "resolved": "github:NixOS/nixpkgs/8edc0c72e3a38faf5434e40d1f19431125fe4b30#sqlite", + "source": "devbox-search", + "version": "3.53.3", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "bin", + "path": "/nix/store/9miwmq08y0pg4m97ws8b2gcqxrbr8d2c-sqlite-3.53.3-bin", + "default": true + }, + { + "name": "man", + "path": "/nix/store/wlwc2gz7r5884rdmfjnqjk38lwv94qsy-sqlite-3.53.3-man", + "default": true + }, + { + "name": "doc", + "path": "/nix/store/p6bsmbp6a5dz55isxvfr7941wbfjmjvf-sqlite-3.53.3-doc" + }, + { + "name": "out", + "path": "/nix/store/0k4r9hnfbnyngn5f3j31siddfs0iglwl-sqlite-3.53.3" + }, + { + "name": "dev", + "path": "/nix/store/r9cljv2bvqi3ci3n6m0z0wi3ldpvlyzq-sqlite-3.53.3-dev" + } + ], + "store_path": "/nix/store/9miwmq08y0pg4m97ws8b2gcqxrbr8d2c-sqlite-3.53.3-bin" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "bin", + "path": "/nix/store/kw0mkd9znldi5l0s4jygygdh4p9sms8r-sqlite-3.53.3-bin", + "default": true + }, + { + "name": "man", + "path": "/nix/store/9q84sy842rw7k4z4jdsb4hkbiapr7pm0-sqlite-3.53.3-man", + "default": true + }, + { + "name": "debug", + "path": "/nix/store/q39crs8z0dafx19dv0l2y4jxc8ahvy03-sqlite-3.53.3-debug" + }, + { + "name": "dev", + "path": "/nix/store/74r7ps7mmjw10fl23xsgcp7ss09jrkjr-sqlite-3.53.3-dev" + }, + { + "name": "doc", + "path": "/nix/store/g4f5y9laa7l7lbriv8x05clyi9x7w14z-sqlite-3.53.3-doc" + }, + { + "name": "out", + "path": "/nix/store/2lyx0wc6ccabpdwpviksww3x5xhsmnvk-sqlite-3.53.3" + } + ], + "store_path": "/nix/store/kw0mkd9znldi5l0s4jygygdh4p9sms8r-sqlite-3.53.3-bin" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "bin", + "path": "/nix/store/iy2zkfn4l0w24xabv6ryi0vh7bpjvqm6-sqlite-3.53.3-bin", + "default": true + }, + { + "name": "man", + "path": "/nix/store/is1ld1bxvwqqjwv89kl6vcnlpypbkg5r-sqlite-3.53.3-man", + "default": true + }, + { + "name": "doc", + "path": "/nix/store/l778wamidjdrjyahinx63c7n50fwh0il-sqlite-3.53.3-doc" + }, + { + "name": "out", + "path": "/nix/store/9x0m6s73sg89fv5awb95vsac2mbns8n0-sqlite-3.53.3" + }, + { + "name": "debug", + "path": "/nix/store/1vbdchnss15n90chl72l80z16shxjp7l-sqlite-3.53.3-debug" + }, + { + "name": "dev", + "path": "/nix/store/xrcjl59yi7zswbv0vzm8cmvw4vplsbgj-sqlite-3.53.3-dev" + } + ], + "store_path": "/nix/store/iy2zkfn4l0w24xabv6ryi0vh7bpjvqm6-sqlite-3.53.3-bin" + } + } + } + } +} diff --git a/prototype-server-vision/nim.cfg b/prototype-server-vision/nim.cfg new file mode 100644 index 0000000..42160c1 --- /dev/null +++ b/prototype-server-vision/nim.cfg @@ -0,0 +1 @@ +--path:"src" diff --git a/prototype-server-vision/servervision.nimble b/prototype-server-vision/servervision.nimble new file mode 100644 index 0000000..e917147 --- /dev/null +++ b/prototype-server-vision/servervision.nimble @@ -0,0 +1,11 @@ +version = "0.1.0" +author = "proto" +description = "Throwaway: server-authoritative vision prototype" +license = "MIT" +srcDir = "src" +bin = @["server"] + +requires "nim >= 2.0" +# no tags upstream: pin exact commit +requires "mummy#01deb1ff694988635c7bf0f24f6088a0bdf6db5d" +requires "db_connector" diff --git a/prototype-server-vision/src/geometry.nim b/prototype-server-vision/src/geometry.nim new file mode 100644 index 0000000..ed1fb92 --- /dev/null +++ b/prototype-server-vision/src/geometry.nim @@ -0,0 +1,108 @@ +## 2D vision geometry. Pure, no deps. +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 a0 + 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 moveBlocked*(walls: openArray[Wall], a, b: Vec): bool = + ## true if travelling a->b touches any blocking wall (touching counts: no leaking through shared vertices). + ## 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 + +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) diff --git a/prototype-server-vision/src/server.nim b/prototype-server-vision/src/server.nim new file mode 100644 index 0000000..2dadba5 --- /dev/null +++ b/prototype-server-vision/src/server.nim @@ -0,0 +1,3 @@ +# phase 1 stub: server lands in a later phase +import mummy, db_connector/db_sqlite +echo "todo" diff --git a/prototype-server-vision/tests/tgeometry.nim b/prototype-server-vision/tests/tgeometry.nim new file mode 100644 index 0000000..eada2f1 --- /dev/null +++ b/prototype-server-vision/tests/tgeometry.nim @@ -0,0 +1,148 @@ +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) + +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); point diagonally behind the vertex + let w = @[W(50, -50, 50, 0), W(50, 0, 100, 0)] + check not sees(w, (0.0, -20.0), (70.0, -20.0)) + check sees(w, (0.0, 20.0), (70.0, 20.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) + + 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)) + + 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) + + 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.len > 3 + check poly.inside((-50.0, -50.0)) # open outward quadrant + check poly.inside((100.0, -30.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)) + + 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 "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": + let sp = currentSourcePath().parentDir / ".." / "data" / "scene.json" + if not fileExists(sp): skip() + else: + 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) diff --git a/prototype-server-vision/tools/genmap.mjs b/prototype-server-vision/tools/genmap.mjs new file mode 100644 index 0000000..b0a1056 --- /dev/null +++ b/prototype-server-vision/tools/genmap.mjs @@ -0,0 +1,105 @@ +// One command: node tools/genmap.mjs -> data/scene.json (source of truth) + data/tiles/_.png +// `--from-seed` skips scene generation and re-renders tiles from the existing data/scene.json. +// Deterministic: layout is hand-authored (no RNG) so the seed edge cases are guaranteed. +import fs from 'node:fs' +import zlib from 'node:zlib' + +const SIZE = 4096, TILE = 256, DIR = new URL('../data/', import.meta.url).pathname +const seedPath = DIR + 'scene.json' + +function build() { + const walls = [] + const add = (a, b, extra = {}) => walls.push({ id: walls.length, a, b, door: false, open: false, ...extra }) + const H = (y, x0, x1, e) => add([x0, y], [x1, y], e) + const V = (x, y0, y1, e) => add([x, y0], [x, y1], e) + // split [lo,hi] by gaps [a,b,door?]; plain gap = opening, door gap = door segment + const side = (lo, hi, gaps, emit) => { + let c = lo + for (const [a, b, d] of [...gaps].sort((p, q) => p[0] - q[0])) { + if (a > c) emit(c, a) + if (d) emit(a, b, { door: true, open: d === 'open' }) + c = b + } + if (c < hi) emit(c, hi) + } + const room = (x0, y0, x1, y1, g = {}) => { + side(x0, x1, g.n ?? [], (a, b, e) => H(y0, a, b, e)) + side(x0, x1, g.s ?? [], (a, b, e) => H(y1, a, b, e)) + side(y0, y1, g.w ?? [], (a, b, e) => V(x0, a, b, e)) + side(y0, y1, g.e ?? [], (a, b, e) => V(x1, a, b, e)) + } + const rooms = [ + [256, 256, 1024, 1024, { e: [[608, 672]], s: [[608, 672]] }], // R1 (pillar) + [1536, 256, 2304, 1024, { w: [[608, 672, 'closed']], e: [[608, 672]], s: [[1888, 1952]] }], // R2: door 1 + [2816, 256, 3584, 1024, { w: [[608, 672]], s: [[3168, 3232]] }], // R3 (T-junction, split north wall) + [256, 2048, 1024, 2816, { n: [[608, 672]], e: [[2400, 2464]] }], // R4 + [1536, 2048, 2304, 2816, { n: [[1888, 1952, 'open']], w: [[2400, 2464]], e: [[2400, 2464]] }], // R5: door 2 + [2816, 2048, 3584, 2816, { n: [[3168, 3232]], w: [[2400, 2464]] }], // R6 + ] + const floors = rooms.map(r => r.slice(0, 4)) + rooms.forEach(r => room(...r)) + // collinear: split R3's north wall into two touching pieces at x=3200 + const n3 = walls.find(w => w.a[0] === 2816 && w.a[1] === 256 && w.b[0] === 3584 && w.b[1] === 256) + n3.b = [3200, 256]; H(256, 3200, 3584) + // corridors: two parallel walls + floor + const hc = (y0, y1, x0, x1) => { H(y0, x0, x1); H(y1, x0, x1); floors.push([x0, y0, x1, y1]) } + const vc = (x0, x1, y0, y1) => { V(x0, y0, y1); V(x1, y0, y1); floors.push([x0, y0, x1, y1]) } + hc(608, 672, 1024, 1536); hc(608, 672, 2304, 2816) // R1-R2 (door at R2 end), R2-R3 + hc(2400, 2464, 1024, 1536); hc(2400, 2464, 2304, 2816) // R4-R5, R5-R6 + vc(1888, 1952, 1024, 2048) // R2-R5: 1024 long, 64 wide (long thin) + vc(3168, 3232, 1024, 2048); vc(608, 672, 1024, 2048) // R3-R6, R1-R4 + // pillar mid-room R1 (centre 640,640) + H(600, 600, 680); H(680, 600, 680); V(600, 600, 680); V(680, 600, 680) + // T-junction: partition ends mid-way on R3's south wall piece (2816..3168) at x=3000 + V(3000, 700, 1024) + const tok = (id, kind, name, pos, gmOnly) => ({ id, kind, name, pos, gmOnly }) + const tokens = [ + tok('A', 'player', 'Player A', [400, 400], false), + tok('B', 'player', 'Player B', [400, 2200], false), + ...[[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 } +} + +// --- raster --- +const crcT = Uint32Array.from({ length: 256 }, (_, n) => { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; return c >>> 0 }) +const crc = b => { let c = ~0; for (const x of b) c = crcT[(c ^ x) & 255] ^ (c >>> 8); return ~c >>> 0 } +const chunk = (t, d) => { const b = Buffer.alloc(12 + d.length); b.writeUInt32BE(d.length); b.write(t, 4); d.copy(b, 8); b.writeUInt32BE(crc(b.subarray(4, 8 + d.length)), 8 + d.length); return b } +function png(rgb, w, h) { + const raw = Buffer.alloc((w * 3 + 1) * h) + for (let y = 0; y < h; y++) rgb.copy(raw, y * (w * 3 + 1) + 1, y * w * 3, (y + 1) * w * 3) + const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(w); ihdr.writeUInt32BE(h, 4); ihdr[8] = 8; ihdr[9] = 2 + return Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk('IHDR', ihdr), chunk('IDAT', zlib.deflateSync(raw)), chunk('IEND', Buffer.alloc(0))]) +} + +function render(scene) { + const img = Buffer.alloc(SIZE * SIZE * 3) + const fill = (x0, y0, x1, y1, [r, g, b]) => { + for (let y = y0; y < y1; y++) for (let x = x0; x < x1; x++) { const i = (y * SIZE + x) * 3; img[i] = r; img[i + 1] = g; img[i + 2] = b } + } + fill(0, 0, SIZE, SIZE, [24, 22, 28]) + for (const [x0, y0, x1, y1] of scene.floors) fill(x0, y0, x1, y1, [120, 112, 100]) + const HALF = 4 + for (const w of scene.walls) { + const [ax, ay] = w.a, [bx, by] = w.b + const col = w.door ? (w.open ? [90, 170, 90] : [150, 80, 30]) : [10, 10, 12] + const dx = bx - ax, dy = by - ay, L2 = dx * dx + dy * dy + for (let y = Math.max(0, Math.min(ay, by) - HALF); y <= Math.min(SIZE - 1, Math.max(ay, by) + HALF); y++) + for (let x = Math.max(0, Math.min(ax, bx) - HALF); x <= Math.min(SIZE - 1, Math.max(ax, bx) + HALF); x++) { + const t = Math.max(0, Math.min(1, ((x - ax) * dx + (y - ay) * dy) / L2)) + if (Math.hypot(x - ax - t * dx, y - ay - t * dy) <= HALF) { const i = (y * SIZE + x) * 3; img[i] = col[0]; img[i + 1] = col[1]; img[i + 2] = col[2] } + } + } + fs.rmSync(DIR + 'tiles', { recursive: true, force: true }); fs.mkdirSync(DIR + 'tiles', { recursive: true }) + const tile = Buffer.alloc(TILE * TILE * 3) + for (let ty = 0; ty < SIZE / TILE; ty++) for (let tx = 0; tx < SIZE / TILE; tx++) { + for (let y = 0; y < TILE; y++) img.copy(tile, y * TILE * 3, ((ty * TILE + y) * SIZE + tx * TILE) * 3, ((ty * TILE + y) * SIZE + (tx + 1) * TILE) * 3) + fs.writeFileSync(`${DIR}tiles/${tx}_${ty}.png`, png(tile, TILE, TILE)) + } +} + +fs.mkdirSync(DIR, { recursive: true }) +if (!process.argv.includes('--from-seed')) fs.writeFileSync(seedPath, JSON.stringify(build(), null, 1)) +render(JSON.parse(fs.readFileSync(seedPath, 'utf8'))) +console.log('scene.json + 256 tiles ->', DIR)