Files
SirStone a79d809394 feat(m4): cap map images at Full HD and shrink on import (#85)
The owner fixed the map cap at 1920 px on the long side and 1080 px on the
short side (either orientation). fff-vtt import now shrinks a larger source
with ImageMagick Lanczos before tiling (alpha kept, proportions preserved,
no upscaling), prints `scaled WxH -> WxH`, and records the shrunk w/h; a
source at or below the cap is untouched, so the pixel-identity tests still
hold. A scene row already above the cap is not rewritten: the server logs
`map_above_cap` at startup and serve prints the re-import advice.

Docs: ADR-0023, DESIGN 3.3/5.3/8/11.1/13/14, TESTING 5.3/13, README,
HANDOFF-M4, client README. The M4 phone gate and the fogbench measurement
now use the Full HD maximum; the 16384 px / 262144-cell bench and test rows
stay as labelled server stress references. import-scale now proves a 16384²
input is shrunk to 1080x1080 (289 tiles at 64 px) quickly and bounded.

Part of #63, #85. #77 is blocked by this issue.
2026-11-21 22:37:34 +01:00

163 lines
6.8 KiB
JavaScript

// T13 (#76): the 50-token, Full HD map measurement the finish ticket records in
// `results/m4-fog-bench.md`. This is NOT the phone gate: headless Chromium here renders with
// SwiftShader (software), so its frame cadence is a floor for correctness work, never a substitute
// for the Note 10 run (#77). The server half of the same measurement is `tools/fogbench.nim`.
//
// The scene is the real one, patched to the Full HD cap (1920x1080; only the importer owns w/h, so
// the harness writes the scene row before the server loads it). 50 tokens sit inside the player's
// sight; the fog texture is therefore 60x34 cells, the real M4 maximum (#85).
import test from 'node:test'
import assert from 'node:assert/strict'
import { spawnSync } from 'node:child_process'
import path from 'node:path'
import { createPlayer, namePlayer } from '../../tools/lib.mjs'
import { openAs } from '../../tools/withserver.mjs'
import { writeTileGrid } from '../../tools/tilefixture.mjs'
import { launchBrowser, signIn, waitForVisible, withStack, sleep } from './stack.mjs'
const MOBILE = { viewport: { width: 412, height: 869 }, deviceScaleFactor: 2.625, isMobile: true, hasTouch: true }
const SIZE = { w: 1920, h: 1080 }
const CENTRE = [SIZE.w / 2, SIZE.h / 2]
/** The scene row is the importer's (DESIGN 6.1: w/h are not wire fields); write it before boot. */
function setSceneSize(stateDir, size) {
const db = path.join(stateDir, 'state.db')
const r = spawnSync('sqlite3', [db, `UPDATE scene SET w=${size.w}, h=${size.h} WHERE id=1`], { encoding: 'utf8' })
assert.equal(r.status, 0, `set scene size: ${r.stderr}`)
}
async function gmWorld(origin, gmKey) {
const gm = await openAs(origin, gmKey)
const hello = await gm.ws.waitText('hello')
const walls = await gm.ws.waitText('walls')
return { ...gm, hello, scene: hello.scene ?? walls.scene }
}
/** 50 tokens: one controlled sighted token and 49 NPCs packed inside its radius. */
async function fiftyTokens(gm, scene, player) {
const own = await gm.ws.request({
t: 'token.create', scene: scene.id, kind: 'pc', pos: CENTRE,
descriptive_name: 'Ann', actual_name: 'Ann', controller: player.id,
flags: { vision: true }, vision_radius: 400, radius: 20,
})
assert.equal(own.ok, true, JSON.stringify(own))
for (let i = 0; i < 49; i += 1) {
const ack = await gm.ws.request({
t: 'token.create', scene: scene.id, kind: 'npc',
pos: [CENTRE[0] + ((i % 7) - 3) * 28, CENTRE[1] + (Math.floor(i / 7) - 3) * 28],
descriptive_name: `T${i}`, actual_name: `T${i}`,
})
assert.equal(ack.ok, true, JSON.stringify(ack))
}
}
/** rAF cadence over `count` frames, summarised the way the fog-cost test does. */
async function cadence(page, count) {
return page.evaluate(async frames => {
const samples = []
await new Promise(resolve => {
let last = performance.now()
let seen = 0
const step = () => {
const now = performance.now()
samples.push(now - last)
last = now
seen += 1
if (seen < frames) requestAnimationFrame(step)
else resolve()
}
requestAnimationFrame(step)
})
samples.sort((a, b) => a - b)
return {
samples: samples.length,
median: samples[Math.floor(samples.length / 2)],
p95: samples[Math.floor(samples.length * 0.95)],
}
}, count)
}
const fogState = page =>
page.evaluate(() => {
const data = document.querySelector('#stage canvas')?.dataset
if (!data) return null
return {
cells: Number(data.fogCells ?? -1),
polys: Number(data.fogPolys ?? -1),
fullMs: Number(data.fogFullMs ?? -1),
tokens: Number(data.tokens ?? -1),
}
})
test('50-token fog measurement on a Full HD map (headless SwiftShader, not the phone gate)', { timeout: 180000 }, async () => {
await withStack(async ({ origin, gmKey, stateDir }) => {
const gm = await gmWorld(origin, gmKey)
const scene = gm.scene
assert.equal(scene.w, SIZE.w, 'the prepared scene is 1920 px wide')
assert.equal(scene.h, SIZE.h, 'the prepared scene is 1080 px high')
const player = await createPlayer(gm.ws)
await namePlayer(origin, player.key, 'Ann')
await fiftyTokens(gm, scene, { id: player.id })
// The capped map is only 60x34 tiles at 32 px, so write the whole grid: no viewport edge can
// ask for a missing tile (the old 16384 px scene only had a viewport window, thousands of files).
writeTileGrid(stateDir, {
scene: scene.id, artVersion: scene.art_version, size: scene.tile_size,
x0: 0, x1: Math.ceil(scene.w / scene.tile_size) - 1,
y0: 0, y1: Math.ceil(scene.h / scene.tile_size) - 1,
})
const browser = await launchBrowser()
try {
const context = await browser.newContext({ ignoreHTTPSErrors: true, ...MOBILE })
const page = await context.newPage()
const cdp = await context.newCDPSession(page)
const errors = []
page.on('pageerror', error => errors.push(error.message))
page.on('console', message => {
if (message.type() === 'error' && !message.text().includes('WebSocket connection') &&
!message.text().includes('403') && !message.text().includes('net::ERR_')) {
errors.push(message.text())
}
})
await page.goto(`${origin}/`)
await waitForVisible(page, '#signin')
await signIn(page, player.key)
await waitForVisible(page, '#stage canvas')
await page.waitForFunction(
() => document.querySelector('#stage canvas')?.dataset.tiles === 'ready',
null,
{ timeout: 30000 },
)
await page.waitForFunction(
() => Number(document.querySelector('#stage canvas')?.dataset.fogPolys ?? 0) > 0,
null,
{ timeout: 30000 },
)
const initial = await fogState(page)
assert.equal(initial.tokens, 50, `the client draws 50 tokens: ${initial.tokens}`)
assert.ok(initial.cells > 300, `the Full HD map's explored cells: ${initial.cells}`)
assert.ok(initial.fullMs > 0, `the 60x34 fog snapshot was repainted: ${initial.fullMs}`)
const real = await cadence(page, 120)
await cdp.send('Emulation.setCPUThrottlingRate', { rate: 4 })
await sleep(200)
const throttled = await cadence(page, 90)
assert.deepEqual(errors, [])
console.log(
`fogbench (#76): Full HD map 1920x1080 (60x34 cells), 50 tokens, 412x869 dpr2.625, ` +
`SwiftShader: fog snapshot=${initial.fullMs.toFixed(3)}ms exploredCells=${initial.cells} ` +
`frames real median=${real.median.toFixed(1)}ms p95=${real.p95.toFixed(1)}ms ` +
`4xCPU median=${throttled.median.toFixed(1)}ms p95=${throttled.p95.toFixed(1)}ms ` +
`(software rasteriser: floor only, not the phone number)`,
)
await context.close()
} finally {
await browser.close()
gm.ws.close()
}
}, { prepare: stateDir => setSceneSize(stateDir, SIZE) })
})