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.
This commit is contained in:
2026-11-21 15:59:54 +01:00
parent bacf2e1412
commit ad610abd65
12 changed files with 700 additions and 30 deletions
+26 -9
View File
@@ -36,29 +36,46 @@ function chunk(type, data) {
// 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(size, 0)
ihdr.writeUInt32BE(size, 4)
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 row = Buffer.alloc(1 + size * 3)
for (let x = 0; x < size; x++) {
row[1 + x * 3] = rgb[0]
row[2 + x * 3] = rgb[1]
row[3 + x * 3] = rgb[2]
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)
}
const raw = Buffer.concat(Array.from({ length: size }, () => row))
return Buffer.concat([
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
chunk('IHDR', ihdr),
chunk('IDAT', zlib.deflateSync(raw, { level: 6 })),
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]))