'use strict'; // The camera math of the renderer (cell <-> screen under rotation, zoom and pan) is what turns a fingertip into a cell. It runs in Node // here with a stub canvas: randomised round trips, so a rotated board, a zoomed board and a panned board all pick the same cell the // player sees. (public/js/render.js assigns to `this` when there is no `self`, i.e. to module.exports in Node.) const test = require('node:test'); const assert = require('node:assert/strict'); const TD = require('../public/js/sim.js'); const NW = require('../public/js/render.js').NW; function rng(seed) { let s = seed >>> 0; return () => { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; }; } const stubCanvas = { getContext() { return {}; }, width: 0, height: 0 }; function makeRenderer(mapId, rot, W, H) { const r = new NW.Renderer(stubCanvas); r.setBoard(new TD.Board(mapId)); r.setRotation(rot); r.resize(W, H, 1); return r; } function randomCamera(r, rand) { r.setZoom(1 + rand() * 3, rand() * r.W, rand() * r.H); // zoom about a random point ... r.panBy((rand() - 0.5) * r.W * 1.5, (rand() - 0.5) * r.H * 1.5); // ... then pan (the camera clamps what is too far) } const DIRS = [[-1, 0], [1, 0], [0, -1], [0, 1]]; test('every cell round-trips through the screen, and a point anywhere inside a cell picks it (rotated or not, zoomed, panned)', () => { const rand = rng(20261010); let checked = 0; for (const mapId of TD.MAP_ORDER) { for (const rot of [false, true]) { for (let run = 0; run < 6; run++) { const W = 280 + Math.floor(rand() * 700), H = 200 + Math.floor(rand() * 700); const r = makeRenderer(mapId, rot, W, H); if (run) randomCamera(r, rand); const cs = r.cellSizePx(), b = r.board; for (let y = 0; y < b.rows; y++) for (let x = 0; x < b.cols; x++) { const s = r.cellToScreen(x, y); const c = r.screenToCell(s.x, s.y); assert.deepEqual([c.x, c.y], [x, y], `${mapId} rot=${rot} ${W}x${H} zoom=${r.zoom.toFixed(2)}: cell ${x},${y} came back as ${c.x},${c.y}`); for (let k = 0; k < 3; k++) { const o = r.screenToCell(s.x + (rand() - 0.5) * 0.9 * cs, s.y + (rand() - 0.5) * 0.9 * cs); assert.deepEqual([o.x, o.y], [x, y], `${mapId} rot=${rot}: a point inside cell ${x},${y} picked ${o.x},${o.y}`); } checked++; } } } } assert.ok(checked > 3000); }); test('zooming keeps the point under the finger fixed', () => { const rand = rng(7); let checked = 0; for (const rot of [false, true]) { for (let run = 0; run < 120; run++) { const r = makeRenderer('open', rot, 300 + rand() * 500, 300 + rand() * 500); randomCamera(r, rand); const ax = rand() * r.W, ay = rand() * r.H, before = r._s2w(ax, ay); r.setZoom(r.zoom * (0.6 + rand() * 1.6), ax, ay); const after = r._s2w(ax, ay); // The camera refuses to keep it exactly where it clamps the pan: a board smaller than the canvas is centred, and a big one may // not be dragged more than 24 px past its edge. Only the free case is checked. const d = r._dims(), sw = d.w * r.scale, sh = d.h * r.scale; const free = sw > r.W && sh > r.H && Math.abs(r.cam.px) < (sw - r.W) / 2 + 23.9 && Math.abs(r.cam.py) < (sh - r.H) / 2 + 23.9; if (!free) continue; checked++; assert.ok(Math.hypot(after.x - before.x, after.y - before.y) < 0.01, `rot=${rot}: the world point under (${ax.toFixed(1)},${ay.toFixed(1)}) moved by ${Math.hypot(after.x - before.x, after.y - before.y).toFixed(3)}`); } } assert.ok(checked > 40, 'only ' + checked + ' unclamped runs'); }); test('stepCell moves exactly one cell in a SCREEN direction, also when the board is drawn turned 90 degrees', () => { const rand = rng(99); for (const rot of [false, true]) { for (let run = 0; run < 10; run++) { const r = makeRenderer('open', rot, 320 + rand() * 400, 320 + rand() * 400); if (run % 2) randomCamera(r, rand); const b = r.board, cs = r.cellSizePx(); for (let k = 0; k < 40; k++) { const x = 1 + Math.floor(rand() * (b.cols - 2)), y = 1 + Math.floor(rand() * (b.rows - 2)); const s0 = r.cellToScreen(x, y); for (const [dx, dy] of DIRS) { const n = r.stepCell(x, y, dx, dy), s1 = r.cellToScreen(n.x, n.y); assert.ok(Math.abs(s1.x - s0.x - dx * cs) < 1e-6 && Math.abs(s1.y - s0.y - dy * cs) < 1e-6, `rot=${rot}: step (${dx},${dy}) from ${x},${y} moved the cell by (${(s1.x - s0.x).toFixed(2)},${(s1.y - s0.y).toFixed(2)}) instead of (${dx * cs},${dy * cs})`); } } } } }); test('at zoom 1 the whole board, with its margin, is inside the canvas (a tap on any edge cell is on screen)', () => { const rand = rng(5); for (const mapId of TD.MAP_ORDER) for (const rot of [false, true]) { const W = 300 + Math.floor(rand() * 600), H = 300 + Math.floor(rand() * 600), r = makeRenderer(mapId, rot, W, H), b = r.board, h = r.cellSizePx() / 2; for (const [x, y] of [[0, 0], [b.cols - 1, 0], [0, b.rows - 1], [b.cols - 1, b.rows - 1]]) { const s = r.cellToScreen(x, y); assert.ok(s.x - h >= -0.01 && s.y - h >= -0.01 && s.x + h <= W + 0.01 && s.y + h <= H + 0.01, `${mapId} rot=${rot} ${W}x${H}: corner cell ${x},${y} is cut off`); } } });