/** * Generate 16×16 gray cobblestone stone-wall PNGs for neighbor-connect masks 0–15. * * Truncated-pyramid / frustum look when alone or at free ends. Connected runs use a * constant cross-section extruded to the tile edge (strip-union like tables/fences) * so N/S and E/W seams match. * * node client/scripts/generate-stone-wall-sprites.mjs */ import { mkdirSync, writeFileSync } from "node:fs"; import { deflateSync } from "node:zlib"; import { fileURLToPath } from "node:url"; import { dirname, join } from "node:path"; import { NeighborBit } from "../../shared/world/neighborConnect.js"; const SIZE = 16; const OUT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks/stone-wall"); const TOP = [168, 168, 172, 255]; const TOP_HI = [198, 198, 202, 255]; const TOP_LO = [140, 140, 144, 255]; const FACE = [112, 112, 116, 255]; const FACE_HI = [132, 132, 136, 255]; const FACE_LO = [88, 88, 92, 255]; const EDGE = [70, 70, 74, 255]; const MORTAR = [78, 78, 82, 255]; /** Constant core used for connected runs (must meet neighbour tile edges cleanly). */ const CORE_X0 = 5; const CORE_X1 = 10; const TOP_Y0 = 3; const TOP_Y1 = 6; const FACE_Y1 = 12; /** Alone / free-end base is wider than the top (frustum). */ const BASE_X0 = 3; const BASE_X1 = 12; 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]; } /** Frustum half-width in X at row y (alone / free-end silhouette). */ function frustumX0X1 (y) { if (y <= TOP_Y1) { return { x0: CORE_X0, x1: CORE_X1 }; } if (y >= FACE_Y1) { return { x0: BASE_X0, x1: BASE_X1 }; } const t = (y - TOP_Y1) / (FACE_Y1 - TOP_Y1); const x0 = Math.round(CORE_X0 + (BASE_X0 - CORE_X0) * t); const x1 = Math.round(CORE_X1 + (BASE_X1 - CORE_X1) * t); return { x0, x1 }; } /** * Strip-union with constant core thickness on connected sides. * Free ends keep the frustum taper so alone / stubs still read as 3D. */ function inWall (x, y, connectN, connectE, connectS, connectW) { const alone = !connectN && !connectE && !connectS && !connectW; const wantH = alone || connectE || connectW; const wantV = alone || connectN || connectS; if (wantH) { const y0 = connectN ? 0 : TOP_Y0; const y1 = connectS ? SIZE - 1 : FACE_Y1; if (y >= y0 && y <= y1) { const { x0: fx0, x1: fx1 } = frustumX0X1(Math.min(FACE_Y1, Math.max(TOP_Y0, y))); // Connected cardinals run to the tile edge (constant seam). // Free ends keep the frustum taper. const x0 = connectW ? 0 : fx0; const x1 = connectE ? SIZE - 1 : fx1; if (x >= x0 && x <= x1) { return true; } } } if (wantV) { const x0 = CORE_X0; const x1 = CORE_X1; if (x >= x0 && x <= x1) { const y0 = connectN ? 0 : TOP_Y0; const y1 = connectS ? SIZE - 1 : FACE_Y1; if (y >= y0 && y <= y1) { return true; } } } return false; } 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 || speck === 2) { return base === TOP || base === TOP_HI || base === TOP_LO ? TOP_HI : FACE_HI; } if (speck === 3) { return base === TOP || base === TOP_HI || base === TOP_LO ? TOP_LO : FACE_LO; } return base; } function isTopRow (y, connectN) { if (connectN) { return y <= TOP_Y1; } return y >= TOP_Y0 && y <= TOP_Y1; } function paintWall (mask) { const rgba = new Uint8Array(SIZE * SIZE * 4); const connectN = (mask & NeighborBit.n) !== 0; const connectE = (mask & NeighborBit.e) !== 0; const connectS = (mask & NeighborBit.s) !== 0; const connectW = (mask & NeighborBit.w) !== 0; for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { if (!inWall(x, y, connectN, connectE, connectS, connectW)) { continue; } const n = inWall(x, y - 1, connectN, connectE, connectS, connectW); const e = inWall(x + 1, y, connectN, connectE, connectS, connectW); const s = inWall(x, y + 1, connectN, connectE, connectS, connectW); const w = inWall(x - 1, y, connectN, connectE, connectS, connectW); let base; if (isTopRow(y, connectN)) { if (!n) { base = TOP_HI; } else if (!s && y === TOP_Y1) { base = TOP_LO; } else { base = TOP; } } else { if (!s) { base = FACE_LO; } else if (!n) { base = FACE_HI; } else { base = FACE; } } // Free side bevels (not on edges that continue into a neighbour). const freeW = !w && !(connectW && x === 0); const freeE = !e && !(connectE && x === SIZE - 1); if (freeW || freeE) { if (base === TOP || base === TOP_HI) { base = TOP_LO; } else if (base === FACE || base === FACE_HI) { base = EDGE; } } setPx(rgba, x, y, cobbleShade(base, x, y)); } } return rgba; } mkdirSync(OUT, { recursive: true }); for (let mask = 0; mask < 16; mask++) { writeFileSync(join(OUT, `${mask}.png`), encodePng(paintWall(mask))); } console.log(`Wrote cobblestone frustum stone-wall sprites 0–15 to ${OUT}`);