| 1 | /** |
| 2 | * Generate 16×16 gray cobblestone stone-wall PNGs for neighbor-connect masks 0–15. |
| 3 | * |
| 4 | * Truncated-pyramid / frustum look when alone or at free ends. Connected runs use a |
| 5 | * constant cross-section extruded to the tile edge (strip-union like tables/fences) |
| 6 | * so N/S and E/W seams match. |
| 7 | * |
| 8 | * node client/scripts/generate-stone-wall-sprites.mjs |
| 9 | */ |
| 10 | import { mkdirSync, writeFileSync } from "node:fs"; |
| 11 | import { deflateSync } from "node:zlib"; |
| 12 | import { fileURLToPath } from "node:url"; |
| 13 | import { dirname, join } from "node:path"; |
| 14 | import { NeighborBit } from "../../shared/world/neighborConnect.js"; |
| 15 | |
| 16 | const SIZE = 16; |
| 17 | const OUT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks/stone-wall"); |
| 18 | |
| 19 | const TOP = [168, 168, 172, 255]; |
| 20 | const TOP_HI = [198, 198, 202, 255]; |
| 21 | const TOP_LO = [140, 140, 144, 255]; |
| 22 | const FACE = [112, 112, 116, 255]; |
| 23 | const FACE_HI = [132, 132, 136, 255]; |
| 24 | const FACE_LO = [88, 88, 92, 255]; |
| 25 | const EDGE = [70, 70, 74, 255]; |
| 26 | const MORTAR = [78, 78, 82, 255]; |
| 27 | |
| 28 | /** Constant core used for connected runs (must meet neighbour tile edges cleanly). */ |
| 29 | const CORE_X0 = 5; |
| 30 | const CORE_X1 = 10; |
| 31 | const TOP_Y0 = 3; |
| 32 | const TOP_Y1 = 6; |
| 33 | const FACE_Y1 = 12; |
| 34 | /** Alone / free-end base is wider than the top (frustum). */ |
| 35 | const BASE_X0 = 3; |
| 36 | const BASE_X1 = 12; |
| 37 | |
| 38 | function crcTable () |
| 39 | { |
| 40 | const table = new Uint32Array(256); |
| 41 | for (let n = 0; n < 256; n++) |
| 42 | { |
| 43 | let c = n; |
| 44 | for (let k = 0; k < 8; k++) |
| 45 | { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); } |
| 46 | table[n] = c >>> 0; |
| 47 | } |
| 48 | return table; |
| 49 | } |
| 50 | |
| 51 | const CRC_TABLE = crcTable(); |
| 52 | |
| 53 | function crc32 (buf) |
| 54 | { |
| 55 | let c = 0xffffffff; |
| 56 | for (let i = 0; i < buf.length; i++) |
| 57 | { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); } |
| 58 | return (c ^ 0xffffffff) >>> 0; |
| 59 | } |
| 60 | |
| 61 | function chunk (type, data) |
| 62 | { |
| 63 | const len = Buffer.alloc(4); |
| 64 | len.writeUInt32BE(data.length); |
| 65 | const typeBuf = Buffer.from(type, "ascii"); |
| 66 | const crcBuf = Buffer.alloc(4); |
| 67 | crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data]))); |
| 68 | return Buffer.concat([len, typeBuf, data, crcBuf]); |
| 69 | } |
| 70 | |
| 71 | function encodePng (rgba) |
| 72 | { |
| 73 | const raw = Buffer.alloc((SIZE * 4 + 1) * SIZE); |
| 74 | for (let y = 0; y < SIZE; y++) |
| 75 | { |
| 76 | const row = y * (SIZE * 4 + 1); |
| 77 | raw[row] = 0; |
| 78 | for (let x = 0; x < SIZE; x++) |
| 79 | { |
| 80 | const i = (y * SIZE + x) * 4; |
| 81 | const o = row + 1 + x * 4; |
| 82 | raw[o] = rgba[i]; |
| 83 | raw[o + 1] = rgba[i + 1]; |
| 84 | raw[o + 2] = rgba[i + 2]; |
| 85 | raw[o + 3] = rgba[i + 3]; |
| 86 | } |
| 87 | } |
| 88 | const ihdr = Buffer.alloc(13); |
| 89 | ihdr.writeUInt32BE(SIZE, 0); |
| 90 | ihdr.writeUInt32BE(SIZE, 4); |
| 91 | ihdr[8] = 8; |
| 92 | ihdr[9] = 6; |
| 93 | return Buffer.concat([ |
| 94 | Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), |
| 95 | chunk("IHDR", ihdr), |
| 96 | chunk("IDAT", deflateSync(raw, { level: 9 })), |
| 97 | chunk("IEND", Buffer.alloc(0)) |
| 98 | ]); |
| 99 | } |
| 100 | |
| 101 | function setPx (rgba, x, y, color) |
| 102 | { |
| 103 | if (x < 0 || y < 0 || x >= SIZE || y >= SIZE) |
| 104 | { return; } |
| 105 | const i = (y * SIZE + x) * 4; |
| 106 | rgba[i] = color[0]; |
| 107 | rgba[i + 1] = color[1]; |
| 108 | rgba[i + 2] = color[2]; |
| 109 | rgba[i + 3] = color[3]; |
| 110 | } |
| 111 | |
| 112 | /** Frustum half-width in X at row y (alone / free-end silhouette). */ |
| 113 | function frustumX0X1 (y) |
| 114 | { |
| 115 | if (y <= TOP_Y1) |
| 116 | { return { x0: CORE_X0, x1: CORE_X1 }; } |
| 117 | if (y >= FACE_Y1) |
| 118 | { return { x0: BASE_X0, x1: BASE_X1 }; } |
| 119 | const t = (y - TOP_Y1) / (FACE_Y1 - TOP_Y1); |
| 120 | const x0 = Math.round(CORE_X0 + (BASE_X0 - CORE_X0) * t); |
| 121 | const x1 = Math.round(CORE_X1 + (BASE_X1 - CORE_X1) * t); |
| 122 | return { x0, x1 }; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Strip-union with constant core thickness on connected sides. |
| 127 | * Free ends keep the frustum taper so alone / stubs still read as 3D. |
| 128 | */ |
| 129 | function inWall (x, y, connectN, connectE, connectS, connectW) |
| 130 | { |
| 131 | const alone = !connectN && !connectE && !connectS && !connectW; |
| 132 | const wantH = alone || connectE || connectW; |
| 133 | const wantV = alone || connectN || connectS; |
| 134 | |
| 135 | if (wantH) |
| 136 | { |
| 137 | const y0 = connectN ? 0 : TOP_Y0; |
| 138 | const y1 = connectS ? SIZE - 1 : FACE_Y1; |
| 139 | if (y >= y0 && y <= y1) |
| 140 | { |
| 141 | const { x0: fx0, x1: fx1 } = frustumX0X1(Math.min(FACE_Y1, Math.max(TOP_Y0, y))); |
| 142 | // Connected cardinals run to the tile edge (constant seam). |
| 143 | // Free ends keep the frustum taper. |
| 144 | const x0 = connectW ? 0 : fx0; |
| 145 | const x1 = connectE ? SIZE - 1 : fx1; |
| 146 | if (x >= x0 && x <= x1) |
| 147 | { return true; } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (wantV) |
| 152 | { |
| 153 | const x0 = CORE_X0; |
| 154 | const x1 = CORE_X1; |
| 155 | if (x >= x0 && x <= x1) |
| 156 | { |
| 157 | const y0 = connectN ? 0 : TOP_Y0; |
| 158 | const y1 = connectS ? SIZE - 1 : FACE_Y1; |
| 159 | if (y >= y0 && y <= y1) |
| 160 | { return true; } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | function hash2 (x, y) |
| 168 | { |
| 169 | let n = (x * 374761393 + y * 668265263) | 0; |
| 170 | n = (n ^ (n >>> 13)) * 1274126177; |
| 171 | return (n ^ (n >>> 16)) >>> 0; |
| 172 | } |
| 173 | |
| 174 | function cobbleShade (base, x, y) |
| 175 | { |
| 176 | const h = hash2(x, y); |
| 177 | const speck = (h & 7); |
| 178 | if (speck === 0) |
| 179 | { return MORTAR; } |
| 180 | if (speck === 1 || speck === 2) |
| 181 | { return base === TOP || base === TOP_HI || base === TOP_LO ? TOP_HI : FACE_HI; } |
| 182 | if (speck === 3) |
| 183 | { return base === TOP || base === TOP_HI || base === TOP_LO ? TOP_LO : FACE_LO; } |
| 184 | return base; |
| 185 | } |
| 186 | |
| 187 | function isTopRow (y, connectN) |
| 188 | { |
| 189 | if (connectN) |
| 190 | { return y <= TOP_Y1; } |
| 191 | return y >= TOP_Y0 && y <= TOP_Y1; |
| 192 | } |
| 193 | |
| 194 | function paintWall (mask) |
| 195 | { |
| 196 | const rgba = new Uint8Array(SIZE * SIZE * 4); |
| 197 | const connectN = (mask & NeighborBit.n) !== 0; |
| 198 | const connectE = (mask & NeighborBit.e) !== 0; |
| 199 | const connectS = (mask & NeighborBit.s) !== 0; |
| 200 | const connectW = (mask & NeighborBit.w) !== 0; |
| 201 | |
| 202 | for (let y = 0; y < SIZE; y++) |
| 203 | { |
| 204 | for (let x = 0; x < SIZE; x++) |
| 205 | { |
| 206 | if (!inWall(x, y, connectN, connectE, connectS, connectW)) |
| 207 | { continue; } |
| 208 | |
| 209 | const n = inWall(x, y - 1, connectN, connectE, connectS, connectW); |
| 210 | const e = inWall(x + 1, y, connectN, connectE, connectS, connectW); |
| 211 | const s = inWall(x, y + 1, connectN, connectE, connectS, connectW); |
| 212 | const w = inWall(x - 1, y, connectN, connectE, connectS, connectW); |
| 213 | |
| 214 | let base; |
| 215 | if (isTopRow(y, connectN)) |
| 216 | { |
| 217 | if (!n) |
| 218 | { base = TOP_HI; } |
| 219 | else if (!s && y === TOP_Y1) |
| 220 | { base = TOP_LO; } |
| 221 | else |
| 222 | { base = TOP; } |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | if (!s) |
| 227 | { base = FACE_LO; } |
| 228 | else if (!n) |
| 229 | { base = FACE_HI; } |
| 230 | else |
| 231 | { base = FACE; } |
| 232 | } |
| 233 | |
| 234 | // Free side bevels (not on edges that continue into a neighbour). |
| 235 | const freeW = !w && !(connectW && x === 0); |
| 236 | const freeE = !e && !(connectE && x === SIZE - 1); |
| 237 | if (freeW || freeE) |
| 238 | { |
| 239 | if (base === TOP || base === TOP_HI) |
| 240 | { base = TOP_LO; } |
| 241 | else if (base === FACE || base === FACE_HI) |
| 242 | { base = EDGE; } |
| 243 | } |
| 244 | |
| 245 | setPx(rgba, x, y, cobbleShade(base, x, y)); |
| 246 | } |
| 247 | } |
| 248 | return rgba; |
| 249 | } |
| 250 | |
| 251 | mkdirSync(OUT, { recursive: true }); |
| 252 | for (let mask = 0; mask < 16; mask++) |
| 253 | { |
| 254 | writeFileSync(join(OUT, `${mask}.png`), encodePng(paintWall(mask))); |
| 255 | } |
| 256 | console.log(`Wrote cobblestone frustum stone-wall sprites 0–15 to ${OUT}`); |