/** * Generate thin + thick stone-wall PNGs for blob neighbor-connect masks * (cardinals + diagonal corners — 47 valid states). * * Shape references (silhouette / shading only, not final texture): * client/assets/blocks/stone-wall/example-stone-thin.png * client/assets/blocks/stone-wall/example-stone-thick.png * * Corner bits: when both adjacent cardinals join but the diagonal does not, * paint an L-strip (concave). When the diagonal also joins, fill the quadrant * so a solid 2×2 reads as one block. * * node client/scripts/generate-stone-wall-sprites.mjs */ import { mkdirSync, writeFileSync, readdirSync, unlinkSync } from "node:fs"; import { deflateSync } 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 OUTLINE = [0x40, 0x40, 0x40, 255]; const TOP_HI = [0xaf, 0xaf, 0xaf, 255]; const TOP = [0x80, 0x80, 0x80, 255]; const TOP_LO = [0x73, 0x73, 0x73, 255]; const FACE_L = [0x95, 0x95, 0x95, 255]; const FACE = [0x73, 0x73, 0x73, 255]; const FACE_R = [0x6c, 0x6c, 0x6c, 255]; const FACE_FRONT = [0x54, 0x54, 0x54, 255]; const HI_L = [0xb6, 0xb6, 0xb6, 255]; const HI_R = [0x80, 0x80, 0x80, 255]; const MORTAR = [0x58, 0x58, 0x5c, 255]; /** * Thin: compact top core (table-style strip-union) so L keeps a concave cutout; * frustum face hangs below on free south edges. Alone matches example-stone-thin. */ const THIN = Object.freeze({ topY0: 2, coreY0: 3, coreY1: 7, faceY1: 12, coreX0: 5, coreX1: 10, baseX0: 3, baseX1: 12, fullTile: false }); /** Thick: full-tile footprint; bevels react to free edges / missing diagonals. */ const THICK = Object.freeze({ topY0: 1, coreY0: 1, coreY1: 8, faceY1: 14, coreX0: 3, coreX1: 12, baseX0: 0, baseX1: 15, fullTile: true }); 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 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 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) { const h = hash2(x, y); const speck = h & 7; if (speck === 0) { return MORTAR; } if (speck === 1) { return [ Math.min(255, base[0] + 18), Math.min(255, base[1] + 18), Math.min(255, base[2] + 18), 255 ]; } if (speck === 2) { return [ Math.max(0, base[0] - 14), Math.max(0, base[1] - 14), Math.max(0, base[2] - 14), 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 frustumX0X1 (y, profile) { if (y <= 2) { return { x0: 6, x1: 9 }; } if (y <= 4) { return { x0: 5, x1: 10 }; } if (y <= 6) { return { x0: 4, x1: 11 }; } return { x0: profile.baseX0, x1: profile.baseX1 }; } /** Top-plane strip-union + diagonal fills (no front skirt). */ function inTopPlane (x, y, c, profile) { const { coreX0, coreX1, coreY0, coreY1 } = profile; const inCoreX = x >= coreX0 && x <= coreX1; const inCoreY = y >= coreY0 && y <= coreY1; const wantH = c.e || c.w; const wantV = c.n || c.s; if (inCoreX && inCoreY) { return true; } if (wantH && inCoreY) { const x0 = c.w ? 0 : coreX0; const x1 = c.e ? SIZE - 1 : coreX1; if (x >= x0 && x <= x1) { return true; } } if (wantV && inCoreX) { const y0 = c.n ? 0 : coreY0; const y1 = c.s ? SIZE - 1 : coreY1; if (y >= y0 && y <= y1) { return true; } } // Close concave L/T cutouts when the diagonal neighbour is present. if (c.se && c.s && c.e && x > coreX1 && y > coreY1) { return true; } if (c.sw && c.s && c.w && x < coreX0 && y > coreY1) { return true; } if (c.ne && c.n && c.e && x > coreX1 && y < coreY0) { return true; } if (c.nw && c.n && c.w && x < coreX0 && y < coreY0) { return true; } return false; } function inWall (x, y, c, profile) { if (profile.fullTile) { return true; } const alone = !c.n && !c.e && !c.s && !c.w; if (alone) { if (y < profile.topY0 || y > profile.faceY1) { return false; } const { x0, x1 } = frustumX0X1(y, profile); return x >= x0 && x <= x1; } if (inTopPlane(x, y, c, profile)) { return true; } // Front-face skirt under top-plane pixels that do not continue south. if (y > profile.coreY1 && y <= profile.faceY1) { if (!inTopPlane(x, profile.coreY1, c, profile)) { return false; } // Vertical run into a southern neighbour: no skirt in the core column. if (c.s && x >= profile.coreX0 && x <= profile.coreX1) { return false; } // Diagonal-filled SE mass continues as solid top — skirt only at very bottom // of columns that are the south perimeter (no further south neighbour top). if (c.s && ((c.se && c.e && x > profile.coreX1) || (c.sw && c.w && x < profile.coreX0))) { return false; } const { x0, x1 } = frustumX0X1(y, profile); const skirtX0 = c.w ? 0 : x0; const skirtX1 = c.e ? SIZE - 1 : x1; if (x < skirtX0 || x > skirtX1) { return false; } return inTopPlane(x, profile.coreY1, c, profile); } return false; } function shadeAt (x, y, c, profile, occ) { const n = occ(x, y - 1); const e = occ(x + 1, y); const s = occ(x, y + 1); const w = occ(x - 1, y); const openCount = (!n ? 1 : 0) + (!s ? 1 : 0) + (!e ? 1 : 0) + (!w ? 1 : 0); if (openCount > 0) { const edgeHeavy = (!n && y <= (profile.fullTile ? 0 : profile.topY0)) || (!s && y >= (profile.fullTile ? SIZE - 1 : profile.faceY1)) || (!w && x <= (profile.fullTile ? 0 : profile.baseX0)) || (!e && x >= (profile.fullTile ? SIZE - 1 : profile.baseX1)); if (edgeHeavy || openCount >= 2) { return OUTLINE; } } if (profile.fullTile) { const freeN = !c.n; const freeS = !c.s; const freeW = !c.w; const freeE = !c.e; const openNe = c.n && c.e && !c.ne; const openSe = c.s && c.e && !c.se; const openSw = c.s && c.w && !c.sw; const openNw = c.n && c.w && !c.nw; if (y === 0 || x === 0 || x === SIZE - 1 || y === SIZE - 1) { return OUTLINE; } if ((freeW && x <= 2) || (openNw && x <= 2 && y <= 2) || (openSw && x <= 2 && y >= SIZE - 3)) { return FACE_L; } if ((freeE && x >= SIZE - 3) || (openNe && x >= SIZE - 3 && y <= 2) || (openSe && x >= SIZE - 3 && y >= SIZE - 3)) { return FACE_R; } if (freeN && y === 1) { return TOP_HI; } if (y <= 8 && x >= 3 && x <= 12) { return TOP; } if (freeS || y >= 10) { return FACE_FRONT; } return TOP_LO; } if (y <= profile.coreY1) { if (!n) { return OUTLINE; } if (!w) { return HI_L; } if (!e) { return HI_R; } return TOP; } if (!s) { return OUTLINE; } if (!w) { return HI_L; } if (!e) { return HI_R; } return FACE; } function paintWall (mask, profile) { const rgba = new Uint8Array(SIZE * SIZE * 4); const c = decodeMask(mask); const occ = (x, y) => { if (x < 0) { return c.w; } if (x >= SIZE) { return c.e; } if (y < 0) { return c.n; } if (y >= SIZE) { return c.s; } return inWall(x, y, c, profile); }; for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { if (!occ(x, y)) { continue; } setPx(rgba, x, y, cobbleShade(shadeAt(x, y, c, profile, occ), 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, profile, label) { clearGenerated(dir); const masks = listValidNeighborConnectCornerMasks(); for (const mask of masks) { writeFileSync(join(dir, `${mask}.png`), encodePng(paintWall(mask, profile))); } console.log(`Wrote ${masks.length} ${label} stone-wall sprites to ${dir}`); } writeSet(OUT_THICK, THICK, "thick"); writeSet(OUT_THIN, THIN, "thin");