/** * Generate thin + thick stone-wall PNGs for blob neighbor-connect masks * (cardinals + diagonal corners — 47 valid states). * * Flat greys only (no texture). Authoritative examples live beside the outputs: * example-stone-{thin|thick}.png * example-stone-{thin|thick}-connecting.png E * example-stone-{thin|thick}-connecting-up.png N * example-stone-thin-connecting-down.png S (thin) * example-stone-{thin|thick}-connecting-updown.png N+S * example-stone-{thin|thick}-connecting-L.png N+E (concave) * example-stone-thin-connecting-Ldownright.png S+E (concave, thin) * example-stone-{thin|thick}-connecting-2x2.png N+E+NE (filled) * example-stone-thin-connecting-T.png E+S+W * example-stone-thin-connecting-T-upsidedown.png N+E+W * example-stone-thick-T.png E+S+W (thick) * * West variants are X-mirrors. Remaining masks compose from these stamps + * the L↔2×2 corner-fill delta. * * node client/scripts/generate-stone-wall-sprites.mjs */ import { mkdirSync, writeFileSync, readdirSync, unlinkSync, readFileSync, existsSync } from "node:fs"; import { deflateSync, inflateSync } from "node:zlib"; import { fileURLToPath } from "node:url"; import { dirname, join } from "node:path"; import { NeighborBit, listValidNeighborConnectCornerMasks } from "../../shared/world/neighborConnect.js"; const SIZE = 16; const ROOT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks"); const OUT_THICK = join(ROOT, "stone-wall"); const OUT_THIN = join(ROOT, "stone-wall-thin"); const EXAMPLES = join(ROOT, "stone-wall"); function crcTable () { const table = new Uint32Array(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 >>> 0; } return table; } const CRC_TABLE = crcTable(); 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 len = Buffer.alloc(4); len.writeUInt32BE(data.length); const typeBuf = Buffer.from(type, "ascii"); const crcBuf = Buffer.alloc(4); crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data]))); return Buffer.concat([len, typeBuf, data, crcBuf]); } function encodePng (rgba) { const raw = Buffer.alloc((SIZE * 4 + 1) * SIZE); for (let y = 0; y < SIZE; y++) { const row = y * (SIZE * 4 + 1); raw[row] = 0; for (let x = 0; x < SIZE; x++) { const i = (y * SIZE + x) * 4; const o = row + 1 + x * 4; raw[o] = rgba[i]; raw[o + 1] = rgba[i + 1]; raw[o + 2] = rgba[i + 2]; raw[o + 3] = rgba[i + 3]; } } const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(SIZE, 0); ihdr.writeUInt32BE(SIZE, 4); ihdr[8] = 8; ihdr[9] = 6; return Buffer.concat([ Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk("IHDR", ihdr), chunk("IDAT", deflateSync(raw, { level: 9 })), chunk("IEND", Buffer.alloc(0)) ]); } function decodePngRgba (path) { const buf = readFileSync(path); let off = 8; let w = 0; let h = 0; const idat = []; while (off < buf.length) { const len = buf.readUInt32BE(off); const type = buf.slice(off + 4, off + 8).toString("ascii"); const data = buf.slice(off + 8, off + 8 + len); if (type === "IHDR") { w = data.readUInt32BE(0); h = data.readUInt32BE(4); if (data[8] !== 8 || data[9] !== 6) { throw new Error(`${path}: expected 8-bit RGBA`); } } else if (type === "IDAT") { idat.push(data); } else if (type === "IEND") { break; } off += 12 + len; } const raw = inflateSync(Buffer.concat(idat)); const bpp = 4; const stride = w * bpp; const rgba = new Uint8ClampedArray(w * h * 4); let src = 0; const prev = Buffer.alloc(stride); const cur = Buffer.alloc(stride); for (let y = 0; y < h; y++) { const filter = raw[src++]; for (let i = 0; i < stride; i++) { cur[i] = raw[src++]; } for (let i = 0; i < stride; i++) { const left = i >= bpp ? cur[i - bpp] : 0; const up = prev[i]; const upLeft = i >= bpp ? prev[i - bpp] : 0; let val = cur[i]; if (filter === 1) { val = (val + left) & 255; } else if (filter === 2) { val = (val + up) & 255; } else if (filter === 3) { val = (val + ((left + up) >> 1)) & 255; } else if (filter === 4) { const p = left + up - upLeft; const pa = Math.abs(p - left); const pb = Math.abs(p - up); const pc = Math.abs(p - upLeft); const pr = (pa <= pb && pa <= pc) ? left : (pb <= pc ? up : upLeft); val = (val + pr) & 255; } cur[i] = val; } rgba.set(cur, y * stride); prev.set(cur); } if (w !== SIZE || h !== SIZE) { throw new Error(`${path}: expected ${SIZE}×${SIZE}, got ${w}×${h}`); } return rgba; } function loadExample (name) { const path = join(EXAMPLES, name); if (!existsSync(path)) { return null; } return decodePngRgba(path); } function getPx (rgba, x, y) { if (x < 0 || y < 0 || x >= SIZE || y >= SIZE) { return [0, 0, 0, 0]; } const i = (y * SIZE + x) * 4; return [rgba[i], rgba[i + 1], rgba[i + 2], rgba[i + 3]]; } function setPx (rgba, x, y, color) { if (x < 0 || y < 0 || x >= SIZE || y >= SIZE) { return; } const i = (y * SIZE + x) * 4; rgba[i] = color[0]; rgba[i + 1] = color[1]; rgba[i + 2] = color[2]; rgba[i + 3] = color[3]; } function opaque (rgba, x, y) { return getPx(rgba, x, y)[3] > 128; } function copyRgba (rgba) { return new Uint8ClampedArray(rgba); } function clearRgba () { return new Uint8ClampedArray(SIZE * SIZE * 4); } function mirrorX (rgba) { const out = clearRgba(); for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { setPx(out, x, y, getPx(rgba, SIZE - 1 - x, y)); } } return out; } function blit (dst, src) { if (!src) { return; } for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { const c = getPx(src, x, y); if (c[3] > 128) { setPx(dst, x, y, c); } } } } /** Pixels where filled differs from concave (the diagonal corner fill). */ function cornerDelta (concave, filled) { const deltas = []; for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { const a = getPx(concave, x, y); const b = getPx(filled, x, y); if (b[3] > 128 && (a[3] <= 128 || a[0] !== b[0] || a[1] !== b[1] || a[2] !== b[2])) { deltas.push({ x, y, color: b }); } else if (a[3] > 128 && b[3] <= 128) { deltas.push({ x, y, color: [0, 0, 0, 0] }); } } } return deltas; } function applyDelta (dst, deltas, mapX, mapY) { for (const d of deltas) { const x = mapX(d.x); const y = mapY(d.y); setPx(dst, x, y, d.color); } } function idX (x) { return x; } function idY (y) { return y; } function flipX (x) { return SIZE - 1 - x; } function flipY (y) { return SIZE - 1 - y; } /** * Build south-east L by compositing down+east and punching the SE corner using * the NE L↔2×2 delta rotated 180° in corner space (flipX+flipY of NE delta * relative to tile — actually SE is flipY of NE region for thin). * * Simpler: if we have an authored Ldownright, use it. Otherwise compose. */ function composeSEFromNE (ellNE, fillNE, east, south) { const out = clearRgba(); blit(out, south); blit(out, east); // Remove NE-style fill if any leaked; then we want SE concave by default. // Punch using mapped NE delta: NE fill region → clear for concave SE. const delta = cornerDelta(ellNE, fillNE); for (const d of delta) { // Map NE corner (top-right) → SE corner (bottom-right): keep x, flip y. const x = d.x; const y = flipY(d.y); // Only clear / don't apply fill for the concave L. if (d.color[3] > 128) { // Was a fill pixel in 2x2 — ensure concave leaves it empty (or outline). const src = getPx(ellNE, d.x, d.y); setPx(out, x, y, src[3] > 128 ? getPx(east, x, Math.min(12, y)) : [0, 0, 0, 0]); if (src[3] <= 128) { setPx(out, x, y, [0, 0, 0, 0]); } } } return out; } function fillSEFromNE (ellSE, ellNE, fillNE) { const out = copyRgba(ellSE); const delta = cornerDelta(ellNE, fillNE); for (const d of delta) { const x = d.x; const y = flipY(d.y); if (d.color[3] > 128) { // Prefer sampling from the SE L's nearby interior. const sample = getPx(ellSE, Math.min(SIZE - 1, Math.max(0, x - 1)), Math.min(SIZE - 1, Math.max(0, y - 1))); setPx(out, x, y, sample[3] > 128 ? sample : d.color); } } return out; } function buildPack (kind) { const alone = loadExample(`example-stone-${kind}.png`); const east = loadExample(`example-stone-${kind}-connecting.png`); const north = loadExample(`example-stone-${kind}-connecting-up.png`); const southAuthored = loadExample(`example-stone-${kind}-connecting-down.png`); const ns = loadExample(`example-stone-${kind}-connecting-updown.png`); const ellNE = loadExample(`example-stone-${kind}-connecting-L.png`); const fillNE = loadExample(`example-stone-${kind}-connecting-2x2.png`); const ellSEAuthored = loadExample(`example-stone-${kind}-connecting-Ldownright.png`); const teeESW = loadExample(`example-stone-${kind}-T.png`) ?? loadExample(`example-stone-${kind}-connecting-T.png`); const teeNEW = loadExample(`example-stone-${kind}-connecting-T-upsidedown.png`); if (!alone || !east || !north || !ellNE || !fillNE || !ns) { throw new Error(`Missing core ${kind} examples`); } const west = mirrorX(east); const southFinal = southAuthored ?? (() => { // No authored south: alone top + updown shaft through the bottom. const out = copyRgba(alone); for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { if (y < 2 && opaque(alone, x, y)) { continue; } if (opaque(ns, x, y)) { setPx(out, x, y, getPx(ns, x, y)); } } } // Ensure bottom rows are open shaft from updown. for (let y = 10; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { if (opaque(ns, x, y)) { setPx(out, x, y, getPx(ns, x, y)); } } } return out; })(); const ellSEFinal = ellSEAuthored ?? composeSEFromNE(ellNE, fillNE, east, southFinal); const fillSE = fillSEFromNE(ellSEFinal, ellNE, fillNE); const ellNW = mirrorX(ellNE); const fillNW = mirrorX(fillNE); const ellSW = mirrorX(ellSEFinal); const fillSW = mirrorX(fillSE); const ew = (() => { const out = clearRgba(); blit(out, east); blit(out, west); // Close the center seam: fill any gap on rows that both sides use. for (let y = 0; y < SIZE; y++) { let fill = null; for (let x = 0; x < SIZE; x++) { const c = getPx(east, x, y); if (c[3] > 128) { fill = c; } } if (!fill) { continue; } for (let x = 0; x < SIZE; x++) { if (!opaque(out, x, y) && (opaque(east, SIZE - 1, y) || opaque(west, 0, y))) { setPx(out, x, y, fill); } } // Force full row when east template opens to the edge on this row. if (opaque(east, SIZE - 1, y)) { for (let x = 0; x < SIZE; x++) { if (!opaque(out, x, y)) { setPx(out, x, y, fill); } } } } return out; })(); const teeESWFinal = teeESW ?? (() => { const out = clearRgba(); blit(out, ew); blit(out, southFinal); return out; })(); const teeNEWFinal = teeNEW ?? (() => { const out = clearRgba(); blit(out, ew); blit(out, north); return out; })(); const teeNES = (() => { const out = clearRgba(); blit(out, north); blit(out, east); blit(out, southFinal); return out; })(); const teeNWS = mirrorX(teeNES); /** Exact mask → rgba for authored / derived cardinal templates (no diagonals). */ const byMask = new Map(); byMask.set(0, alone); byMask.set(NeighborBit.n, north); byMask.set(NeighborBit.e, east); byMask.set(NeighborBit.s, southFinal); byMask.set(NeighborBit.w, west); byMask.set(NeighborBit.n | NeighborBit.s, ns); byMask.set(NeighborBit.e | NeighborBit.w, ew); byMask.set(NeighborBit.n | NeighborBit.e, ellNE); byMask.set(NeighborBit.n | NeighborBit.w, ellNW); byMask.set(NeighborBit.s | NeighborBit.e, ellSEFinal); byMask.set(NeighborBit.s | NeighborBit.w, ellSW); byMask.set(NeighborBit.e | NeighborBit.s | NeighborBit.w, teeESWFinal); byMask.set(NeighborBit.n | NeighborBit.e | NeighborBit.w, teeNEWFinal); byMask.set(NeighborBit.n | NeighborBit.e | NeighborBit.s, teeNES); byMask.set(NeighborBit.n | NeighborBit.w | NeighborBit.s, teeNWS); byMask.set( NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w, (() => { const out = clearRgba(); blit(out, ns); blit(out, ew); blit(out, teeESWFinal); blit(out, teeNEWFinal); return out; })() ); // Filled-corner variants for the primary L corners. byMask.set(NeighborBit.n | NeighborBit.e | NeighborBit.ne, fillNE); byMask.set(NeighborBit.n | NeighborBit.w | NeighborBit.nw, fillNW); byMask.set(NeighborBit.s | NeighborBit.e | NeighborBit.se, fillSE); byMask.set(NeighborBit.s | NeighborBit.w | NeighborBit.sw, fillSW); const neDelta = cornerDelta(ellNE, fillNE); const nwDelta = cornerDelta(ellNW, fillNW); const seDelta = cornerDelta(ellSEFinal, fillSE); const swDelta = cornerDelta(ellSW, fillSW); return { byMask, neDelta, nwDelta, seDelta, swDelta, alone, east, west, north, south: southFinal, ns, ew, ellNE, fillNE }; } function paintMask (mask, pack) { const n = (mask & NeighborBit.n) !== 0; const e = (mask & NeighborBit.e) !== 0; const s = (mask & NeighborBit.s) !== 0; const w = (mask & NeighborBit.w) !== 0; const ne = (mask & NeighborBit.ne) !== 0; const se = (mask & NeighborBit.se) !== 0; const sw = (mask & NeighborBit.sw) !== 0; const nw = (mask & NeighborBit.nw) !== 0; const cardinal = (n ? NeighborBit.n : 0) | (e ? NeighborBit.e : 0) | (s ? NeighborBit.s : 0) | (w ? NeighborBit.w : 0); // Exact hit (including known filled corners). if (pack.byMask.has(mask)) { return copyRgba(pack.byMask.get(mask)); } // Start from cardinal-only template, then apply diagonal fills. let out; if (pack.byMask.has(cardinal)) { out = copyRgba(pack.byMask.get(cardinal)); } else { out = clearRgba(); if (n) { blit(out, pack.north); } if (s) { blit(out, pack.south); } if (e) { blit(out, pack.east); } if (w) { blit(out, pack.west); } if (n && s) { blit(out, pack.ns); } if (e && w) { blit(out, pack.ew); } } if (n && e && ne) { applyDelta(out, pack.neDelta, idX, idY); } if (n && w && nw) { applyDelta(out, pack.nwDelta, idX, idY); } if (s && e && se) { applyDelta(out, pack.seDelta, idX, idY); } if (s && w && sw) { applyDelta(out, pack.swDelta, idX, idY); } // When diagonal is absent but cardinal template accidentally included fill, // strip using inverse delta (already concave templates are used for cardinal). return out; } function clearGenerated (dir) { mkdirSync(dir, { recursive: true }); for (const name of readdirSync(dir)) { if (!/^\d+\.png$/i.test(name)) { continue; } unlinkSync(join(dir, name)); } } function writeSet (dir, pack, label) { clearGenerated(dir); const masks = listValidNeighborConnectCornerMasks(); for (const mask of masks) { writeFileSync(join(dir, `${mask}.png`), encodePng(paintMask(mask, pack))); } console.log(`Wrote ${masks.length} ${label} stone-wall sprites to ${dir}`); } function assertSame (a, b, label) { for (let i = 0; i < a.length; i++) { if (a[i] !== b[i]) { throw new Error(`Mismatch ${label} @${i}: ${a[i]}≠${b[i]}`); } } } const thin = buildPack("thin"); const thick = buildPack("thick"); assertSame(paintMask(0, thin), thin.alone, "thin/0"); assertSame(paintMask(1, thin), thin.north, "thin/1"); assertSame(paintMask(2, thin), thin.east, "thin/2"); assertSame(paintMask(3, thin), thin.ellNE, "thin/3"); assertSame(paintMask(5, thin), thin.ns, "thin/5"); assertSame(paintMask(19, thin), thin.fillNE, "thin/19"); assertSame(paintMask(0, thick), thick.alone, "thick/0"); assertSame(paintMask(1, thick), thick.north, "thick/1"); assertSame(paintMask(2, thick), thick.east, "thick/2"); assertSame(paintMask(3, thick), thick.ellNE, "thick/3"); assertSame(paintMask(5, thick), thick.ns, "thick/5"); assertSame(paintMask(19, thick), thick.fillNE, "thick/19"); writeSet(OUT_THIN, thin, "thin"); writeSet(OUT_THICK, thick, "thick"); console.log("All authored core templates verified.");