Files
SirStone ad610abd65 feat(server): map import CLI, pulled forward to M4 (#79)
Slice a PNG/WebP map into the gate's <x>_<y>.png tree under
<state>/tiles/<scene>/v<art_version>/ and update the one scene row
(w, h, tile_size, art_version); a re-import grows art_version so
clients never serve stale tiles. The state-dir lock keeps a running
server out (it loads the scene at startup), so stop/import/restart.

Decoding is ImageMagick from the root devbox with a disk-backed
pixel cache (-limit memory/map), keeping a 16384 px source
memory-sane; no Nim/npm image dependency and the FIXED stack is
unchanged. The black-box harness covers the tile geometry, scene
row, restart pickup through the gate, re-import bump and every
refused-import case.

Records the M6 -> M4 move in DESIGN 3.3/11.1/13 and DESIGN-history,
and documents the CLI plus the 16384 px/50-token recipe in README.
2026-11-21 15:59:54 +01:00

113 lines
4.0 KiB
JavaScript

// Fixture map tiles, generated at test time (DESIGN 13 M3 "fixture tiles at 32 px generated at test
// time by a helper (not committed)", slice 9 / #24). Zero npm deps: the PNG writer uses node:zlib.
//
// The fixture is deliberately boring art: each 32 px tile is one solid colour derived from its
// coordinates, so a test can tell two tiles apart by their bytes and no image work is needed. Tiles
// land under <stateDir>/tiles/<scene>/v<art_version>/, the tree the test nginx aliases for
// /_tiles/ (3.3, 11.1).
import fs from 'node:fs'
import path from 'node:path'
import zlib from 'node:zlib'
const CRC_TABLE = (() => {
const table = new Int32Array(256)
for (let n = 0; n < 256; n++) {
let c = n
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
table[n] = c
}
return table
})()
function crc32(buf) {
let c = 0xffffffff
for (let i = 0; i < buf.length; i++) c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8)
return (c ^ 0xffffffff) >>> 0
}
function chunk(type, data) {
const head = Buffer.alloc(4)
head.writeUInt32BE(data.length)
const body = Buffer.concat([Buffer.from(type, 'latin1'), data])
const crc = Buffer.alloc(4)
crc.writeUInt32BE(crc32(body))
return Buffer.concat([head, body, crc])
}
// A solid-colour 8-bit RGB PNG of `size` x `size` (filter 0 on every scanline).
export function pngBytes(size, rgb) {
return pngBytesWH(size, size, () => rgb)
}
// A width x height 8-bit RGB PNG; `colourAt(x, y)` returns [r,g,b] (filter 0 on every scanline).
export function pngBytesWH(width, height, colourAt) {
const ihdr = Buffer.alloc(13)
ihdr.writeUInt32BE(width, 0)
ihdr.writeUInt32BE(height, 4)
ihdr[8] = 8 // bit depth
ihdr[9] = 2 // colour type: truecolour (RGB)
ihdr[10] = 0 // compression
ihdr[11] = 0 // filter
ihdr[12] = 0 // interlace
const rows = []
for (let y = 0; y < height; y++) {
const row = Buffer.alloc(1 + width * 3)
for (let x = 0; x < width; x++) {
const rgb = colourAt(x, y)
row[1 + x * 3] = rgb[0]
row[2 + x * 3] = rgb[1]
row[3 + x * 3] = rgb[2]
}
rows.push(row)
}
return Buffer.concat([
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
chunk('IHDR', ihdr),
chunk('IDAT', zlib.deflateSync(Buffer.concat(rows), { level: 6 })),
chunk('IEND', Buffer.alloc(0)),
])
}
// The width/height of a PNG file from its IHDR (the import test checks the sliced geometry).
export function pngSize(file) {
const buf = fs.readFileSync(file)
if (!isPng(buf) || buf.length < 24) throw new Error(`${file}: not a PNG`)
return { width: buf.readUInt32BE(16), height: buf.readUInt32BE(20) }
}
export function isPng(buf) {
return Buffer.isBuffer(buf) && buf.length > 8 &&
buf.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))
}
export function tileDir(stateDir, scene, artVersion) {
return path.join(stateDir, 'tiles', String(scene), `v${artVersion}`)
}
// Deterministic colour of a tile: distinct neighbours, stable across runs.
function colourOf(x, y) {
return [(x * 37 + y * 11) & 0xff, (x * 17 + y * 53) & 0xff, (x * 97 + y * 5) & 0xff]
}
// Writes the given [x, y] tiles (default size 32 px) and returns the paths.
export function writeTiles(stateDir, { scene = 1, artVersion = 1, size = 32, tiles }) {
const dir = tileDir(stateDir, scene, artVersion)
fs.mkdirSync(dir, { recursive: true })
const files = []
for (const [x, y] of tiles) {
const file = path.join(dir, `${x}_${y}.png`)
fs.writeFileSync(file, pngBytes(size, colourOf(x, y)))
files.push(file)
}
return files
}
// Writes every tile of the rectangle x0..x1 / y0..y1 (inclusive). The leak check enumerates the
// same rectangle, so every tile it expects to be allowed has real bytes behind the internal alias.
export function writeTileGrid(stateDir, { scene = 1, artVersion = 1, size = 32, x0, x1, y0, y1 }) {
const tiles = []
for (let y = y0; y <= y1; y++) for (let x = x0; x <= x1; x++) tiles.push([x, y])
return writeTiles(stateDir, { scene, artVersion, size, tiles })
}