/** * Generate thin + thick stone-wall PNGs for the 16 cardinal neighbor masks. * * Naming: `{key}.png` alone, `{key}-{suffix}.png` when connected. * Suffixes: n/e/s/w, ns, we, ne/es/sw/wn, esw/wne/swn/nes, nesw. * * Examples (shape only, no texture) live in * client/assets/blocks/_examples/stone-wall/ * * node client/scripts/generate-stone-wall-sprites.mjs */ import { mkdirSync, writeFileSync, readdirSync, unlinkSync, readFileSync, existsSync, cpSync, rmSync } from "node:fs"; import { deflateSync, inflateSync } from "node:zlib"; import { fileURLToPath } from "node:url"; import { dirname, join } from "node:path"; import { NeighborBit, cardinalConnectionSuffix, composeNamedNeighborConnectState } from "../../shared/world/neighborConnect.js"; const SIZE = 16; const ROOT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks"); const EXAMPLES = join(ROOT, "_examples/stone-wall"); const OUT_THICK = join(ROOT, "thick-stone-wall"); const OUT_THIN = join(ROOT, "thin-stone-wall"); const THICK = { key: "thick-stone-wall", neighborConnect: true, neighborConnectNamed: true }; const THIN = { key: "thin-stone-wall", neighborConnect: true, neighborConnectNamed: 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 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); } } } } /** Ensure example folder exists; migrate from legacy stone-wall/ if needed. */ function ensureExamples () { mkdirSync(EXAMPLES, { recursive: true }); const legacy = join(ROOT, "stone-wall"); if (!existsSync(legacy)) { return; } for (const name of readdirSync(legacy)) { if (!name.startsWith("example-") || !name.endsWith(".png")) { continue; } const dest = join(EXAMPLES, name); if (!existsSync(dest)) { cpSync(join(legacy, name), dest); } } } 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 south = 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 ellSE = 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 || !ns) { throw new Error(`Missing core ${kind} examples in ${EXAMPLES}`); } const west = mirrorX(east); const southFinal = south ?? (() => { const out = copyRgba(alone); 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 ellNEFinal = ellNE ?? (() => { const out = clearRgba(); blit(out, north); blit(out, east); return out; })(); const ellSEFinal = ellSE ?? (() => { const out = clearRgba(); blit(out, southFinal); blit(out, east); return out; })(); const ellNW = mirrorX(ellNEFinal); const ellSW = mirrorX(ellSEFinal); const ew = (() => { const out = clearRgba(); blit(out, east); blit(out, west); for (let y = 0; y < SIZE; y++) { if (!opaque(east, SIZE - 1, y)) { continue; } 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; })(); 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); const all = (() => { const out = clearRgba(); blit(out, ns); blit(out, ew); return out; })(); const byMask = new Map([ [0, alone], [NeighborBit.n, north], [NeighborBit.e, east], [NeighborBit.s, southFinal], [NeighborBit.w, west], [NeighborBit.n | NeighborBit.s, ns], [NeighborBit.e | NeighborBit.w, ew], [NeighborBit.n | NeighborBit.e, ellNEFinal], [NeighborBit.e | NeighborBit.s, ellSEFinal], [NeighborBit.s | NeighborBit.w, ellSW], [NeighborBit.w | NeighborBit.n, ellNW], [NeighborBit.e | NeighborBit.s | NeighborBit.w, teeESWFinal], [NeighborBit.w | NeighborBit.n | NeighborBit.e, teeNEWFinal], [NeighborBit.n | NeighborBit.e | NeighborBit.s, teeNES], [NeighborBit.n | NeighborBit.w | NeighborBit.s, teeNWS], [NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w, all] ]); return { byMask, alone, east, north, ns, ellNE: ellNEFinal }; } function clearGenerated (dir) { mkdirSync(dir, { recursive: true }); for (const name of readdirSync(dir)) { if (!name.endsWith(".png") || name.startsWith("example")) { continue; } unlinkSync(join(dir, name)); } } function writeSet (dir, def, pack) { clearGenerated(dir); for (let mask = 0; mask < 16; mask++) { const rgba = pack.byMask.get(mask); if (!rgba) { throw new Error(`Missing mask ${mask} for ${def.key}`); } const state = composeNamedNeighborConnectState(def, mask); writeFileSync(join(dir, `${state}.png`), encodePng(rgba)); } console.log(`Wrote 16 ${def.key} sprites → ${dir}`); } function removeLegacyFolders () { for (const name of ["stone-wall", "stone-wall-thin"]) { const dir = join(ROOT, name); if (!existsSync(dir)) { continue; } // Keep examples only in _examples/; remove entire legacy folder. rmSync(dir, { recursive: true, force: true }); console.log(`Removed legacy ${name}/`); } } ensureExamples(); const thin = buildPack("thin"); const thick = buildPack("thick"); function assertSame (a, b, label) { for (let i = 0; i < a.length; i++) { if (a[i] !== b[i]) { throw new Error(`Mismatch ${label}`); } } } assertSame(thin.byMask.get(0), thin.alone, "thin alone"); assertSame(thin.byMask.get(1), thin.north, "thin n"); assertSame(thin.byMask.get(2), thin.east, "thin e"); assertSame(thin.byMask.get(5), thin.ns, "thin ns"); assertSame(thick.byMask.get(0), thick.alone, "thick alone"); writeSet(OUT_THIN, THIN, thin); writeSet(OUT_THICK, THICK, thick); removeLegacyFolders(); console.log("Suffixes:", [...Array(16)].map((_, m) => `${m}=${cardinalConnectionSuffix(m) || "(alone)"}`).join(", "));