/** * Generate thin + thick stone-wall PNGs for blob neighbor-connect masks * (cardinals + diagonal corners — 47 valid states). * * Shape sources (authoritative): * example-stone-thin.png / example-stone-thin-connecting.png * example-stone-thick.png / example-stone-thick-connecting.png * * Alone = free-standing frustum / bevelled block. * *-connecting = same bands extruded to the east edge (free west end kept). * Other directions are mirrors / vertical extrusions of that rule. * Diagonal bits fill the concave L cutout so solid 2×2 reads continuous. * * 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"); const MORTAR = [0x58, 0x58, 0x5c, 255]; 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)) ]); } /** Decode 8-bit RGBA PNG (with scanline filters) into Uint8ClampedArray. */ 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 mirrorX (rgba) { const out = new Uint8ClampedArray(rgba.length); 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 hash2 (x, y) { let n = (x * 374761393 + y * 668265263) | 0; n = (n ^ (n >>> 13)) * 1274126177; return (n ^ (n >>> 16)) >>> 0; } function cobbleShade (base, x, y) { if (base[3] < 128) { return base; } const h = hash2(x, y); const speck = h & 7; if (speck === 0) { return MORTAR; } if (speck === 1) { return [ Math.min(255, base[0] + 14), Math.min(255, base[1] + 14), Math.min(255, base[2] + 14), 255 ]; } if (speck === 2) { return [ Math.max(0, base[0] - 12), Math.max(0, base[1] - 12), Math.max(0, base[2] - 12), 255 ]; } return base; } 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 }; } function aloneRowRange (alone, y) { let x0 = -1; let x1 = -1; for (let x = 0; x < SIZE; x++) { if (!opaque(alone, x, y)) { continue; } if (x0 < 0) { x0 = x; } x1 = x; } return x0 < 0 ? null : { x0, x1 }; } function aloneColRange (alone, x) { let y0 = -1; let y1 = -1; for (let y = 0; y < SIZE; y++) { if (!opaque(alone, x, y)) { continue; } if (y0 < 0) { y0 = y; } y1 = y; } return y0 < 0 ? null : { y0, y1 }; } function aloneBounds (alone) { let minX = SIZE; let maxX = -1; let minY = SIZE; let maxY = -1; for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { if (!opaque(alone, x, y)) { continue; } minX = Math.min(minX, x); maxX = Math.max(maxX, x); minY = Math.min(minY, y); maxY = Math.max(maxY, y); } } return { minX, maxX, minY, maxY }; } /** * Strip-union of alone rows (E/W extrusion) and alone columns (N/S extrusion), * plus diagonal fills for solid 2×2 corners. */ function thinOccupied (x, y, c, alone) { if (!c.n && !c.e && !c.s && !c.w) { return opaque(alone, x, y); } const row = aloneRowRange(alone, y); if (row && (c.e || c.w || opaque(alone, x, y))) { const x0 = c.w ? 0 : row.x0; const x1 = c.e ? SIZE - 1 : row.x1; if (x >= x0 && x <= x1) { return true; } } const col = aloneColRange(alone, x); if (col && (c.n || c.s || opaque(alone, x, y))) { const y0 = c.n ? 0 : col.y0; const y1 = c.s ? SIZE - 1 : col.y1; if (y >= y0 && y <= y1) { return true; } } if (opaque(alone, x, y)) { return true; } const b = aloneBounds(alone); // Diagonal fills close the concave cutout of an L/T. if (c.se && c.s && c.e && x > b.maxX && y > b.maxY) { return true; } if (c.sw && c.s && c.w && x < b.minX && y > b.maxY) { return true; } if (c.ne && c.n && c.e && x > b.maxX && y < b.minY) { return true; } if (c.nw && c.n && c.w && x < b.minX && y < b.minY) { return true; } // Also fill the "inside" corner within the extruded AABB when diagonal joins // (region east of alone footprint ∩ south of alone footprint, etc.). if (c.se && c.s && c.e && x > b.maxX && y >= b.minY && y <= SIZE - 1 && x <= SIZE - 1) { // Only below the horizontal band's natural bottom is the true cutout; // pixels with y in alone rows are already filled by E extrusion. if (y > b.maxY) { return true; } } if (c.sw && c.s && c.w && x < b.minX && y > b.maxY) { return true; } if (c.ne && c.n && c.e && x > b.maxX && y < b.minY) { return true; } if (c.nw && c.n && c.w && x < b.minX && y < b.minY) { return true; } return false; } /** * Colour for a thin pixel: prefer alone, else east/west connecting template, * else sample the nearest alone edge colour along the extrusion axis. */ function thinColor (x, y, c, alone, east, west) { if (opaque(alone, x, y)) { return getPx(alone, x, y); } if (c.e && !c.w && opaque(east, x, y)) { return getPx(east, x, y); } if (c.w && !c.e && opaque(west, x, y)) { return getPx(west, x, y); } if (c.e && c.w) { // Full E–W run: take interior colour from the east template at this y. const row = aloneRowRange(alone, y); if (row) { const sampleX = Math.min(SIZE - 1, row.x1); const col = getPx(east, sampleX, y); if (col[3] > 128) { return col; } return getPx(alone, sampleX, y); } } // N/S extrusion: continue the colour of the alone column end. const col = aloneColRange(alone, x); if (col) { if (c.n && y < col.y0) { return getPx(alone, x, col.y0); } if (c.s && y > col.y1) { return getPx(alone, x, col.y1); } } // Diagonal fill / extended cutout: pick nearest alone interior. const b = aloneBounds(alone); const sx = Math.min(b.maxX, Math.max(b.minX, x)); const sy = Math.min(b.maxY, Math.max(b.minY, y)); return getPx(alone, sx, sy); } /** * Thick walls always fill the tile. Shading: start from alone; for each * connected side, replace that edge's bevel with the extruded connecting look. */ function thickColor (x, y, c, alone, east, west) { // Build by compositing directional templates. // Base = alone. let color = getPx(alone, x, y); const fromEast = opaque(east, x, y) ? getPx(east, x, y) : null; const fromWest = opaque(west, x, y) ? getPx(west, x, y) : null; // East connection: use east template where it differs from alone on the right half, // or more simply — if only E: use east template entirely. if (c.e && !c.w && !c.n && !c.s) { return fromEast ?? color; } if (c.w && !c.e && !c.n && !c.s) { return fromWest ?? color; } // General case: for each axis, prefer the connecting template's pixel when // that side is connected and we're on the extruded side of the tile. if (c.e && x >= 8 && fromEast) { color = fromEast; } if (c.w && x <= 7 && fromWest) { color = fromWest; } // North/south: extend the top/bottom band colours toward the edge. if (c.n && y <= 2) { // Sample the y=1 band from the appropriate horizontal template. const src = c.e && !c.w ? east : (c.w && !c.e ? west : alone); color = getPx(src, x, 1); } if (c.s && y >= 13) { const src = c.e && !c.w ? east : (c.w && !c.e ? west : alone); color = getPx(src, x, 14); } // Both E+W: full horizontal extrusion like two connecting halves. if (c.e && c.w) { const left = getPx(west, Math.min(x, 7), y); const right = getPx(east, Math.max(x, 8), y); color = x <= 7 ? left : right; } // Open L corner (cardinals without diagonal): keep alone's outer-corner bevel // in that quadrant. Filled diagonal: use connecting interior instead. if (c.e && c.s && !c.se && x >= 12 && y >= 12) { color = getPx(alone, x, y); } if (c.e && c.s && c.se && x >= 12 && y >= 12) { color = getPx(east, Math.min(15, x), Math.min(14, y)); } if (c.w && c.s && !c.sw && x <= 3 && y >= 12) { color = getPx(alone, x, y); } if (c.w && c.s && c.sw && x <= 3 && y >= 12) { color = getPx(west, Math.max(0, x), Math.min(14, y)); } if (c.e && c.n && !c.ne && x >= 12 && y <= 3) { color = getPx(alone, x, y); } if (c.e && c.n && c.ne && x >= 12 && y <= 3) { color = getPx(east, Math.min(15, x), Math.max(1, y)); } if (c.w && c.n && !c.nw && x <= 3 && y <= 3) { color = getPx(alone, x, y); } if (c.w && c.n && c.nw && x <= 3 && y <= 3) { color = getPx(west, Math.max(0, x), Math.max(1, y)); } // Interior of solid block (all 4 + diagonals): flat top mid colour. if (c.n && c.e && c.s && c.w && c.ne && c.se && c.sw && c.nw) { if (x > 0 && x < SIZE - 1 && y > 0 && y < SIZE - 1) { color = getPx(east, 8, 5); } } return color; } function paintThin (mask, alone, east, west) { const rgba = new Uint8ClampedArray(SIZE * SIZE * 4); const c = decodeMask(mask); for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { if (!thinOccupied(x, y, c, alone)) { continue; } const col = thinColor(x, y, c, alone, east, west); setPx(rgba, x, y, cobbleShade(col, x, y)); } } return rgba; } function paintThick (mask, alone, east, west) { const rgba = new Uint8ClampedArray(SIZE * SIZE * 4); const c = decodeMask(mask); for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { const col = thickColor(x, y, c, alone, east, west); setPx(rgba, x, y, cobbleShade(col, x, y)); } } return rgba; } 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, paint, label) { clearGenerated(dir); const masks = listValidNeighborConnectCornerMasks(); for (const mask of masks) { writeFileSync(join(dir, `${mask}.png`), encodePng(paint(mask))); } console.log(`Wrote ${masks.length} ${label} stone-wall sprites to ${dir}`); } const thinAlone = decodePngRgba(join(EXAMPLES, "example-stone-thin.png")); const thinEast = decodePngRgba(join(EXAMPLES, "example-stone-thin-connecting.png")); const thinWest = mirrorX(thinEast); const thickAlone = decodePngRgba(join(EXAMPLES, "example-stone-thick.png")); const thickEast = decodePngRgba(join(EXAMPLES, "example-stone-thick-connecting.png")); const thickWest = mirrorX(thickEast); // Sanity: alone / east templates must decode to expected coverage. const thinAloneCount = [...thinAlone].filter((_, i) => i % 4 === 3 && thinAlone[i] > 128).length; const thinEastCount = [...thinEast].filter((_, i) => i % 4 === 3 && thinEast[i] > 128).length; if (thinAloneCount < 80 || thinEastCount < 120) { throw new Error(`Example decode looks wrong (alone=${thinAloneCount}, east=${thinEastCount})`); } writeSet(OUT_THICK, (mask) => paintThick(mask, thickAlone, thickEast, thickWest), "thick"); writeSet(OUT_THIN, (mask) => paintThin(mask, thinAlone, thinEast, thinWest), "thin"); // Keep mask 0 / east-only (2) pixel-identical to examples (minus cobble speck on 0). writeFileSync(join(OUT_THIN, "0.png"), encodePng(thinAlone)); writeFileSync(join(OUT_THICK, "0.png"), encodePng(thickAlone)); writeFileSync(join(OUT_THIN, "2.png"), encodePng(thinEast)); writeFileSync(join(OUT_THICK, "2.png"), encodePng(thickEast)); writeFileSync(join(OUT_THIN, "8.png"), encodePng(thinWest)); writeFileSync(join(OUT_THICK, "8.png"), encodePng(thickWest)); console.log("Pinned 0 / E / W templates to example pixels");