| 1 | /** |
| 2 | * Generate 16×16 gray cobblestone stone-wall PNGs for neighbor-connect masks 0–15. |
| 3 | * |
| 4 | * Shape is a truncated pyramid / frustum (wider base, narrower top). Footprint is a |
| 5 | * strip-union like tables: E–W bar ∪ N–S bar so L/T/+ bends keep concave corners |
| 6 | * while straight runs stay one tile thick and meet at the edges. |
| 7 | * |
| 8 | * node client/scripts/generate-stone-wall-sprites.mjs |
| 9 | */ |
| 10 | import { mkdirSync, writeFileSync, unlinkSync, existsSync } 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 | /** Gray cobble palette (no magenta — stone stays gray for now). */ |
| 20 | const TOP = [168, 168, 172, 255]; |
| 21 | const TOP_HI = [198, 198, 202, 255]; |
| 22 | const TOP_LO = [140, 140, 144, 255]; |
| 23 | const FACE = [112, 112, 116, 255]; |
| 24 | const FACE_HI = [132, 132, 136, 255]; |
| 25 | const FACE_LO = [88, 88, 92, 255]; |
| 26 | const EDGE = [70, 70, 74, 255]; |
| 27 | const MORTAR = [78, 78, 82, 255]; |
| 28 | |
| 29 | /** Alone / core frustum (inclusive pixel bounds). */ |
| 30 | const TOP_Y0 = 3; |
| 31 | const TOP_Y1 = 6; |
| 32 | const FACE_Y1 = 12; |
| 33 | const CX = 7.5; |
| 34 | const TOP_HALF_W = 3.0; |
| 35 | const BASE_HALF_W = 5.5; |
| 36 | |
| 37 | function crcTable () |
| 38 | { |
| 39 | const table = new Uint32Array(256); |
| 40 | for (let n = 0; n < 256; n++) |
| 41 | { |
| 42 | let c = n; |
| 43 | for (let k = 0; k < 8; k++) |
| 44 | { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); } |
| 45 | table[n] = c >>> 0; |
| 46 | } |
| 47 | return table; |
| 48 | } |
| 49 | |
| 50 | const CRC_TABLE = crcTable(); |
| 51 | |
| 52 | function crc32 (buf) |
| 53 | { |
| 54 | let c = 0xffffffff; |
| 55 | for (let i = 0; i < buf.length; i++) |
| 56 | { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); } |
| 57 | return (c ^ 0xffffffff) >>> 0; |
| 58 | } |
| 59 | |
| 60 | function chunk (type, data) |
| 61 | { |
| 62 | const len = Buffer.alloc(4); |
| 63 | len.writeUInt32BE(data.length); |
| 64 | const typeBuf = Buffer.from(type, "ascii"); |
| 65 | const crcBuf = Buffer.alloc(4); |
| 66 | crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data]))); |
| 67 | return Buffer.concat([len, typeBuf, data, crcBuf]); |
| 68 | } |
| 69 | |
| 70 | function encodePng (rgba) |
| 71 | { |
| 72 | const raw = Buffer.alloc((SIZE * 4 + 1) * SIZE); |
| 73 | for (let y = 0; y < SIZE; y++) |
| 74 | { |
| 75 | const row = y * (SIZE * 4 + 1); |
| 76 | raw[row] = 0; |
| 77 | for (let x = 0; x < SIZE; x++) |
| 78 | { |
| 79 | const i = (y * SIZE + x) * 4; |
| 80 | const o = row + 1 + x * 4; |
| 81 | raw[o] = rgba[i]; |
| 82 | raw[o + 1] = rgba[i + 1]; |
| 83 | raw[o + 2] = rgba[i + 2]; |
| 84 | raw[o + 3] = rgba[i + 3]; |
| 85 | } |
| 86 | } |
| 87 | const ihdr = Buffer.alloc(13); |
| 88 | ihdr.writeUInt32BE(SIZE, 0); |
| 89 | ihdr.writeUInt32BE(SIZE, 4); |
| 90 | ihdr[8] = 8; |
| 91 | ihdr[9] = 6; |
| 92 | return Buffer.concat([ |
| 93 | Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), |
| 94 | chunk("IHDR", ihdr), |
| 95 | chunk("IDAT", deflateSync(raw, { level: 9 })), |
| 96 | chunk("IEND", Buffer.alloc(0)) |
| 97 | ]); |
| 98 | } |
| 99 | |
| 100 | function setPx (rgba, x, y, color) |
| 101 | { |
| 102 | if (x < 0 || y < 0 || x >= SIZE || y >= SIZE) |
| 103 | { return; } |
| 104 | const i = (y * SIZE + x) * 4; |
| 105 | rgba[i] = color[0]; |
| 106 | rgba[i + 1] = color[1]; |
| 107 | rgba[i + 2] = color[2]; |
| 108 | rgba[i + 3] = color[3]; |
| 109 | } |
| 110 | |
| 111 | function halfWidthAtY (y) |
| 112 | { |
| 113 | if (y <= TOP_Y1) |
| 114 | { return TOP_HALF_W; } |
| 115 | if (y >= FACE_Y1) |
| 116 | { return BASE_HALF_W; } |
| 117 | const t = (y - TOP_Y1) / (FACE_Y1 - TOP_Y1); |
| 118 | return TOP_HALF_W + (BASE_HALF_W - TOP_HALF_W) * t; |
| 119 | } |
| 120 | |
| 121 | /** Core Y range for alone wall; extended to tile edges when connected N/S. */ |
| 122 | function yRange (connectN, connectS) |
| 123 | { |
| 124 | return { |
| 125 | y0: connectN ? 0 : TOP_Y0, |
| 126 | y1: connectS ? SIZE - 1 : FACE_Y1 |
| 127 | }; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Strip-union footprint: E–W frustum bar ∪ N–S frustum bar. |
| 132 | * Free ends keep the tapered half-width; connected sides run to the tile edge. |
| 133 | */ |
| 134 | function inWall (x, y, connectN, connectE, connectS, connectW) |
| 135 | { |
| 136 | const { y0, y1 } = yRange(connectN, connectS); |
| 137 | if (y < y0 || y > y1) |
| 138 | { return false; } |
| 139 | |
| 140 | const hw = halfWidthAtY(Math.min(FACE_Y1, Math.max(TOP_Y0, y))); |
| 141 | const coreX0 = Math.ceil(CX - hw); |
| 142 | const coreX1 = Math.floor(CX + hw); |
| 143 | |
| 144 | const inH = y >= (connectN ? 0 : TOP_Y0) |
| 145 | && y <= (connectS ? SIZE - 1 : FACE_Y1) |
| 146 | && ( |
| 147 | (x >= coreX0 && x <= coreX1) |
| 148 | || (connectW && x < coreX0) |
| 149 | || (connectE && x > coreX1) |
| 150 | ); |
| 151 | |
| 152 | const inV = x >= coreX0 && x <= coreX1 |
| 153 | && ( |
| 154 | (y >= TOP_Y0 && y <= FACE_Y1) |
| 155 | || (connectN && y < TOP_Y0) |
| 156 | || (connectS && y > FACE_Y1) |
| 157 | ); |
| 158 | |
| 159 | return inH || inV; |
| 160 | } |
| 161 | |
| 162 | function hash2 (x, y) |
| 163 | { |
| 164 | let n = (x * 374761393 + y * 668265263) | 0; |
| 165 | n = (n ^ (n >>> 13)) * 1274126177; |
| 166 | return (n ^ (n >>> 16)) >>> 0; |
| 167 | } |
| 168 | |
| 169 | function cobbleShade (base, x, y) |
| 170 | { |
| 171 | const h = hash2(x, y); |
| 172 | const speck = (h & 7); |
| 173 | if (speck === 0) |
| 174 | { return MORTAR; } |
| 175 | if (speck === 1 || speck === 2) |
| 176 | { return base === TOP || base === TOP_HI || base === TOP_LO ? TOP_HI : FACE_HI; } |
| 177 | if (speck === 3) |
| 178 | { return base === TOP || base === TOP_HI || base === TOP_LO ? TOP_LO : FACE_LO; } |
| 179 | return base; |
| 180 | } |
| 181 | |
| 182 | function paintWall (mask) |
| 183 | { |
| 184 | const rgba = new Uint8Array(SIZE * SIZE * 4); |
| 185 | const connectN = (mask & NeighborBit.n) !== 0; |
| 186 | const connectE = (mask & NeighborBit.e) !== 0; |
| 187 | const connectS = (mask & NeighborBit.s) !== 0; |
| 188 | const connectW = (mask & NeighborBit.w) !== 0; |
| 189 | |
| 190 | for (let y = 0; y < SIZE; y++) |
| 191 | { |
| 192 | for (let x = 0; x < SIZE; x++) |
| 193 | { |
| 194 | if (!inWall(x, y, connectN, connectE, connectS, connectW)) |
| 195 | { continue; } |
| 196 | |
| 197 | const n = inWall(x, y - 1, connectN, connectE, connectS, connectW); |
| 198 | const e = inWall(x + 1, y, connectN, connectE, connectS, connectW); |
| 199 | const s = inWall(x, y + 1, connectN, connectE, connectS, connectW); |
| 200 | const w = inWall(x - 1, y, connectN, connectE, connectS, connectW); |
| 201 | |
| 202 | let base; |
| 203 | const onTop = y <= TOP_Y1 || (connectN && y < TOP_Y0 + 2 && n && s); |
| 204 | // Prefer a clear top deck in the core band; south of TOP_Y1 is the face. |
| 205 | if (y <= TOP_Y1 && (y >= TOP_Y0 || connectN)) |
| 206 | { |
| 207 | if (!n) |
| 208 | { base = TOP_HI; } |
| 209 | else if (!s && y === TOP_Y1) |
| 210 | { base = TOP_LO; } |
| 211 | else |
| 212 | { base = TOP; } |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | // South face / body of the frustum |
| 217 | if (!s) |
| 218 | { base = FACE_LO; } |
| 219 | else if (!n) |
| 220 | { base = FACE_HI; } |
| 221 | else |
| 222 | { base = FACE; } |
| 223 | } |
| 224 | |
| 225 | // Free side rims read as darker bevels (trapezoid silhouette). |
| 226 | if ((!w && !connectW) || (!e && !connectE)) |
| 227 | { |
| 228 | if (base === TOP || base === TOP_HI) |
| 229 | { base = TOP_LO; } |
| 230 | else if (base === FACE || base === FACE_HI) |
| 231 | { base = EDGE; } |
| 232 | } |
| 233 | |
| 234 | setPx(rgba, x, y, cobbleShade(base, x, y)); |
| 235 | } |
| 236 | } |
| 237 | return rgba; |
| 238 | } |
| 239 | |
| 240 | mkdirSync(OUT, { recursive: true }); |
| 241 | for (const legacy of ["1.png", "2.png"]) |
| 242 | { |
| 243 | const path = join(OUT, legacy); |
| 244 | if (existsSync(path)) |
| 245 | { unlinkSync(path); } |
| 246 | } |
| 247 | for (let mask = 0; mask < 16; mask++) |
| 248 | { |
| 249 | writeFileSync(join(OUT, `${mask}.png`), encodePng(paintWall(mask))); |
| 250 | } |
| 251 | console.log(`Wrote cobblestone frustum stone-wall sprites 0–15 to ${OUT}`); |