/** * Generate thin + thick stone-wall PNGs for blob neighbor-connect masks * (cardinals + diagonal corners — 47 valid states). * * Authoritative shape examples (no texture — flat greys only): * example-stone-{thin|thick}.png alone * example-stone-{thin|thick}-connecting.png east * example-stone-{thin|thick}-connecting-up.png north * example-stone-{thin|thick}-connecting-L.png north+east (concave) * example-stone-{thin|thick}-connecting-2x2.png north+east+NE (filled) * * Other directions are mirrors / south extrusions of those rules. * * node client/scripts/generate-stone-wall-sprites.mjs */ import { mkdirSync, writeFileSync, readdirSync, unlinkSync, readFileSync } 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 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; } /** Stamp every opaque pixel from src onto dst (src wins). */ function blit (dst, src) { 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); } } } } /** * Where `filled` is opaque and `concave` is not (or differs), copy `filled`. * Used to turn an L template into a 2×2 corner by applying the NE fill. */ function applyCornerFill (dst, concave, filled) { 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])) { setPx(dst, x, y, b); } } } } /** * Where `filled` differs from `concave`, clear dst back toward concave * (remove corner fill — used when diagonal bit is absent). */ function stripCornerFill (dst, concave, filled) { 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])) { setPx(dst, x, y, a); } } } } function decodeMask (mask) { return { n: (mask & NeighborBit.n) !== 0, e: (mask & NeighborBit.e) !== 0, s: (mask & NeighborBit.s) !== 0, w: (mask & NeighborBit.w) !== 0, ne: (mask & NeighborBit.ne) !== 0, se: (mask & NeighborBit.se) !== 0, sw: (mask & NeighborBit.sw) !== 0, nw: (mask & NeighborBit.nw) !== 0 }; } /** * South-only: alone silhouette with the body columns extended to y=15 * (mirror of how up opens the top while keeping the bottom face). */ function makeSouth (alone, up) { const out = copyRgba(alone); for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { if (y <= 12) { continue; } // Extend columns that belong to the vertical shaft (up body width). if (!opaque(up, x, 12)) { continue; } setPx(out, x, y, getPx(alone, x, 12)); } } return out; } /** * North+south shaft: up template opened through the bottom as well. */ function makeNS (up, south) { const out = copyRgba(up); for (let y = 13; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { if (opaque(south, x, y)) { setPx(out, x, y, getPx(south, x, y)); } else if (opaque(up, x, 12)) { setPx(out, x, y, getPx(up, x, 12)); } } } return out; } /** * East+west run: east template with the west side also opened (mirror blend). */ function makeEW (east, west) { const out = clearRgba(); for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { const e = getPx(east, x, y); const w = getPx(west, x, y); if (e[3] > 128 && w[3] > 128) { // Prefer interior sample from east beyond the free-end bevels. setPx(out, x, y, x < 8 ? w : e); } else if (e[3] > 128) { setPx(out, x, y, e); } else if (w[3] > 128) { setPx(out, x, y, w); } } } // Ensure full-width rows where either side has the band. for (let y = 0; y < SIZE; y++) { let any = false; for (let x = 0; x < SIZE; x++) { if (opaque(out, x, y)) { any = true; break; } } if (!any) { continue; } // Find a representative interior colour from east. let fill = null; for (let x = SIZE - 1; x >= 0; x--) { const c = getPx(east, x, y); if (c[3] > 128) { fill = c; break; } } if (!fill) { continue; } for (let x = 0; x < SIZE; x++) { if (!opaque(out, x, y)) { setPx(out, x, y, fill); } } } return out; } /** * Build one wall variant's template pack from the five authored examples. */ function buildPack (prefix) { const alone = decodePngRgba(join(EXAMPLES, `example-stone-${prefix}.png`)); const east = decodePngRgba(join(EXAMPLES, `example-stone-${prefix}-connecting.png`)); const north = decodePngRgba(join(EXAMPLES, `example-stone-${prefix}-connecting-up.png`)); const ellNE = decodePngRgba(join(EXAMPLES, `example-stone-${prefix}-connecting-L.png`)); const fillNE = decodePngRgba(join(EXAMPLES, `example-stone-${prefix}-connecting-2x2.png`)); const west = mirrorX(east); const south = makeSouth(alone, north); const ellNW = mirrorX(ellNE); const fillNW = mirrorX(fillNE); // South-east / south-west L: mirror the north L vertically in occupancy terms // by compositing south + east with the SE corner left open / filled. const ellSE = makeCardinalL(south, east, "se"); const fillSE = makeCardinalFill(ellSE, south, east, "se", ellNE, fillNE); const ellSW = mirrorX(ellSE); const fillSW = mirrorX(fillSE); const ns = makeNS(north, south); const ew = makeEW(east, west); return { alone, east, west, north, south, ns, ew, ellNE, fillNE, ellNW, fillNW, ellSE, fillSE, ellSW, fillSW }; } /** * Compose an L for south+east from south & east templates, leaving SE concave. * Corner open region is inferred from the NE L vs 2×2 delta, rotated to SE. */ function makeCardinalL (vert, horiz, corner) { const out = clearRgba(); blit(out, vert); blit(out, horiz); // Punch the concave corner using the NE L/2×2 delta mapped to this corner. return out; } function makeCardinalFill (ell, vert, horiz, corner, ellNE, fillNE) { const out = copyRgba(ell); // Map NE fill delta onto the target corner. for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { let sx = x; let sy = y; if (corner === "se") { sx = x; sy = SIZE - 1 - y; } else if (corner === "sw") { sx = SIZE - 1 - x; sy = SIZE - 1 - y; } else if (corner === "nw") { sx = SIZE - 1 - x; sy = y; } const a = getPx(ellNE, sx, sy); const b = getPx(fillNE, sx, sy); if (b[3] > 128 && (a[3] <= 128 || a[0] !== b[0])) { // Sample fill colour from the horizontal arm interior. const sample = getPx(horiz, Math.min(SIZE - 1, x), Math.min(SIZE - 1, Math.max(0, y))); const col = sample[3] > 128 ? sample : b; setPx(out, x, y, col); } } } return out; } /** * Paint one mask by starting from the best base template, then OR-ing extra * arms and applying / stripping corner fills for each diagonal bit. */ function paintMask (mask, pack) { const c = decodeMask(mask); const { alone, east, west, north, south, ns, ew, ellNE, fillNE, ellNW, fillNW, ellSE, fillSE, ellSW, fillSW } = pack; const n = c.n; const e = c.e; const s = c.s; const w = c.w; const cardinal = (n ? 1 : 0) + (e ? 1 : 0) + (s ? 1 : 0) + (w ? 1 : 0); // --- Exact / near-exact authored cases --- if (cardinal === 0) { return copyRgba(alone); } if (cardinal === 1) { if (e) { return copyRgba(east); } if (w) { return copyRgba(west); } if (n) { return copyRgba(north); } if (s) { return copyRgba(south); } } // Pure L corners (two adjacent cardinals) if (cardinal === 2 && n && e && !s && !w) { return c.ne ? copyRgba(fillNE) : copyRgba(ellNE); } if (cardinal === 2 && n && w && !s && !e) { return c.nw ? copyRgba(fillNW) : copyRgba(ellNW); } if (cardinal === 2 && s && e && !n && !w) { return c.se ? copyRgba(fillSE) : copyRgba(ellSE); } if (cardinal === 2 && s && w && !n && !e) { return c.sw ? copyRgba(fillSW) : copyRgba(ellSW); } // Opposite pairs if (cardinal === 2 && n && s && !e && !w) { return copyRgba(ns); } if (cardinal === 2 && e && w && !n && !s) { return copyRgba(ew); } // --- General compose --- const out = clearRgba(); // Prefer richer multi-arm templates when available, then blit extras. if (n && e) { blit(out, c.ne ? fillNE : ellNE); } else if (n && w) { blit(out, c.nw ? fillNW : ellNW); } else if (s && e) { blit(out, c.se ? fillSE : ellSE); } else if (s && w) { blit(out, c.sw ? fillSW : ellSW); } else if (n && s) { blit(out, ns); } else if (e && w) { blit(out, ew); } else if (n) { blit(out, north); } else if (s) { blit(out, south); } else if (e) { blit(out, east); } else if (w) { blit(out, west); } // Add missing opposite / side arms. if (n) { blit(out, north); } if (s) { blit(out, south); } if (e) { blit(out, east); } if (w) { blit(out, west); } if (n && s) { blit(out, ns); } if (e && w) { blit(out, ew); } // Corner fills / strips for every relevant diagonal. if (n && e) { if (c.ne) { applyCornerFill(out, ellNE, fillNE); } else { stripCornerFill(out, ellNE, fillNE); } } if (n && w) { if (c.nw) { applyCornerFill(out, ellNW, fillNW); } else { stripCornerFill(out, ellNW, fillNW); } } if (s && e) { if (c.se) { applyCornerFill(out, ellSE, fillSE); } else { stripCornerFill(out, ellSE, fillSE); } } if (s && w) { if (c.sw) { applyCornerFill(out, ellSW, fillSW); } else { stripCornerFill(out, ellSW, fillSW); } } 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}`); } const thin = buildPack("thin"); const thick = buildPack("thick"); // Sanity checks against authored examples function assertSame (a, b, label) { for (let i = 0; i < a.length; i++) { if (a[i] !== b[i]) { throw new Error(`Template mismatch: ${label} at byte ${i}`); } } } assertSame(paintMask(0, thin), thin.alone, "thin alone"); assertSame(paintMask(2, thin), thin.east, "thin east"); assertSame(paintMask(1, thin), thin.north, "thin north"); assertSame(paintMask(3, thin), thin.ellNE, "thin L"); assertSame(paintMask(19, thin), thin.fillNE, "thin 2x2"); assertSame(paintMask(0, thick), thick.alone, "thick alone"); assertSame(paintMask(2, thick), thick.east, "thick east"); assertSame(paintMask(1, thick), thick.north, "thick north"); assertSame(paintMask(3, thick), thick.ellNE, "thick L"); assertSame(paintMask(19, thick), thick.fillNE, "thick 2x2"); writeSet(OUT_THIN, thin, "thin"); writeSet(OUT_THICK, thick, "thick");