| 1 | /** |
| 2 | * Generate 16×16 magenta table PNGs for neighbor-connect masks 0–15. |
| 3 | * Reads the artist `wood.png`, remaps browns → magenta (tintable), then |
| 4 | * extends the tabletop along connected cardinals (no diagonals). |
| 5 | * Legs stay on free south edges; extra legs appear on long spans. |
| 6 | */ |
| 7 | import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 8 | import { deflateSync, inflateSync } from "node:zlib"; |
| 9 | import { fileURLToPath } from "node:url"; |
| 10 | import { dirname, join } from "node:path"; |
| 11 | import { NeighborBit } from "../../shared/world/neighborConnect.js"; |
| 12 | |
| 13 | const SIZE = 16; |
| 14 | const OUT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks/table"); |
| 15 | const SOURCE = join(OUT, "wood.png"); |
| 16 | |
| 17 | /** Base tabletop AABB in the artist sprite (inclusive). */ |
| 18 | const BASE = Object.freeze({ |
| 19 | x0: 3, |
| 20 | x1: 12, |
| 21 | y0: 2, |
| 22 | y1: 11 |
| 23 | }); |
| 24 | |
| 25 | function crcTable () |
| 26 | { |
| 27 | const table = new Uint32Array(256); |
| 28 | for (let n = 0; n < 256; n++) |
| 29 | { |
| 30 | let c = n; |
| 31 | for (let k = 0; k < 8; k++) |
| 32 | { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); } |
| 33 | table[n] = c >>> 0; |
| 34 | } |
| 35 | return table; |
| 36 | } |
| 37 | |
| 38 | const CRC_TABLE = crcTable(); |
| 39 | |
| 40 | function crc32 (buf) |
| 41 | { |
| 42 | let c = 0xffffffff; |
| 43 | for (let i = 0; i < buf.length; i++) |
| 44 | { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); } |
| 45 | return (c ^ 0xffffffff) >>> 0; |
| 46 | } |
| 47 | |
| 48 | function chunk (type, data) |
| 49 | { |
| 50 | const len = Buffer.alloc(4); |
| 51 | len.writeUInt32BE(data.length); |
| 52 | const typeBuf = Buffer.from(type, "ascii"); |
| 53 | const crcBuf = Buffer.alloc(4); |
| 54 | crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data]))); |
| 55 | return Buffer.concat([len, typeBuf, data, crcBuf]); |
| 56 | } |
| 57 | |
| 58 | function encodePng (rgba) |
| 59 | { |
| 60 | const raw = Buffer.alloc((SIZE * 4 + 1) * SIZE); |
| 61 | for (let y = 0; y < SIZE; y++) |
| 62 | { |
| 63 | const row = y * (SIZE * 4 + 1); |
| 64 | raw[row] = 0; |
| 65 | for (let x = 0; x < SIZE; x++) |
| 66 | { |
| 67 | const i = (y * SIZE + x) * 4; |
| 68 | const o = row + 1 + x * 4; |
| 69 | raw[o] = rgba[i]; |
| 70 | raw[o + 1] = rgba[i + 1]; |
| 71 | raw[o + 2] = rgba[i + 2]; |
| 72 | raw[o + 3] = rgba[i + 3]; |
| 73 | } |
| 74 | } |
| 75 | const ihdr = Buffer.alloc(13); |
| 76 | ihdr.writeUInt32BE(SIZE, 0); |
| 77 | ihdr.writeUInt32BE(SIZE, 4); |
| 78 | ihdr[8] = 8; |
| 79 | ihdr[9] = 6; |
| 80 | ihdr[10] = 0; |
| 81 | ihdr[11] = 0; |
| 82 | ihdr[12] = 0; |
| 83 | return Buffer.concat([ |
| 84 | Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), |
| 85 | chunk("IHDR", ihdr), |
| 86 | chunk("IDAT", deflateSync(raw, { level: 9 })), |
| 87 | chunk("IEND", Buffer.alloc(0)) |
| 88 | ]); |
| 89 | } |
| 90 | |
| 91 | function decodePngRgba (buf) |
| 92 | { |
| 93 | let off = 8; |
| 94 | let w = 0; |
| 95 | let h = 0; |
| 96 | const idat = []; |
| 97 | while (off < buf.length) |
| 98 | { |
| 99 | const len = buf.readUInt32BE(off); |
| 100 | const type = buf.slice(off + 4, off + 8).toString("ascii"); |
| 101 | const data = buf.slice(off + 8, off + 8 + len); |
| 102 | if (type === "IHDR") |
| 103 | { |
| 104 | w = data.readUInt32BE(0); |
| 105 | h = data.readUInt32BE(4); |
| 106 | } |
| 107 | else if (type === "IDAT") |
| 108 | { idat.push(data); } |
| 109 | else if (type === "IEND") |
| 110 | { break; } |
| 111 | off += 12 + len; |
| 112 | } |
| 113 | if (w !== SIZE || h !== SIZE) |
| 114 | { throw new Error(`Expected ${SIZE}×${SIZE} PNG, got ${w}×${h}`); } |
| 115 | const raw = inflateSync(Buffer.concat(idat)); |
| 116 | const stride = 1 + SIZE * 4; |
| 117 | const rgba = new Uint8Array(SIZE * SIZE * 4); |
| 118 | for (let y = 0; y < SIZE; y++) |
| 119 | { |
| 120 | for (let x = 0; x < SIZE; x++) |
| 121 | { |
| 122 | const i = y * stride + 1 + x * 4; |
| 123 | const o = (y * SIZE + x) * 4; |
| 124 | rgba[o] = raw[i]; |
| 125 | rgba[o + 1] = raw[i + 1]; |
| 126 | rgba[o + 2] = raw[i + 2]; |
| 127 | rgba[o + 3] = raw[i + 3]; |
| 128 | } |
| 129 | } |
| 130 | return rgba; |
| 131 | } |
| 132 | |
| 133 | function getPx (rgba, x, y) |
| 134 | { |
| 135 | const i = (y * SIZE + x) * 4; |
| 136 | return [rgba[i], rgba[i + 1], rgba[i + 2], rgba[i + 3]]; |
| 137 | } |
| 138 | |
| 139 | function setPx (rgba, x, y, color) |
| 140 | { |
| 141 | if (x < 0 || y < 0 || x >= SIZE || y >= SIZE) |
| 142 | { return; } |
| 143 | const i = (y * SIZE + x) * 4; |
| 144 | rgba[i] = color[0]; |
| 145 | rgba[i + 1] = color[1]; |
| 146 | rgba[i + 2] = color[2]; |
| 147 | rgba[i + 3] = color[3]; |
| 148 | } |
| 149 | |
| 150 | /** Remap any opaque pixel into the magenta tint family (R≈B, G<R). */ |
| 151 | function toMagenta (r, g, b, a) |
| 152 | { |
| 153 | if (a < 8) |
| 154 | { return [0, 0, 0, 0]; } |
| 155 | const L = Math.max(1, Math.max(r, g, b)); |
| 156 | const minC = Math.min(r, g, b); |
| 157 | const chroma = (L - minC) / 255; |
| 158 | // Slight G softens grain; keep rims more saturated (low G). |
| 159 | const gMag = Math.min(L - 1, Math.round(L * (0.08 + (1 - chroma) * 0.22))); |
| 160 | return [L, Math.max(0, gMag), L, 255]; |
| 161 | } |
| 162 | |
| 163 | function magentaize (src) |
| 164 | { |
| 165 | const out = new Uint8Array(src.length); |
| 166 | for (let i = 0; i < src.length; i += 4) |
| 167 | { |
| 168 | const [r, g, b, a] = toMagenta(src[i], src[i + 1], src[i + 2], src[i + 3]); |
| 169 | out[i] = r; |
| 170 | out[i + 1] = g; |
| 171 | out[i + 2] = b; |
| 172 | out[i + 3] = a; |
| 173 | } |
| 174 | return out; |
| 175 | } |
| 176 | |
| 177 | function sampleTop (base, x, y) |
| 178 | { |
| 179 | const sx = Math.min(BASE.x1, Math.max(BASE.x0, x)); |
| 180 | const sy = Math.min(BASE.y1, Math.max(BASE.y0, y)); |
| 181 | return getPx(base, sx, sy); |
| 182 | } |
| 183 | |
| 184 | function drawLeg (rgba, base, anchorX, topY) |
| 185 | { |
| 186 | // Artist legs: 3×2 under the top at (4,12) and (9,12). |
| 187 | const pattern = [ |
| 188 | [getPx(base, 4, 12), getPx(base, 5, 12), getPx(base, 6, 12)], |
| 189 | [getPx(base, 4, 13), getPx(base, 5, 13), getPx(base, 6, 13)] |
| 190 | ]; |
| 191 | for (let ly = 0; ly < 2; ly++) |
| 192 | { |
| 193 | const y = topY + 1 + ly; |
| 194 | if (y >= SIZE) |
| 195 | { break; } |
| 196 | for (let lx = 0; lx < 3; lx++) |
| 197 | { setPx(rgba, anchorX + lx, y, pattern[ly][lx]); } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | function paintTable (mask, base) |
| 202 | { |
| 203 | const rgba = new Uint8Array(SIZE * SIZE * 4); |
| 204 | const connectN = (mask & NeighborBit.n) !== 0; |
| 205 | const connectE = (mask & NeighborBit.e) !== 0; |
| 206 | const connectS = (mask & NeighborBit.s) !== 0; |
| 207 | const connectW = (mask & NeighborBit.w) !== 0; |
| 208 | |
| 209 | if (mask === 0) |
| 210 | { |
| 211 | rgba.set(base); |
| 212 | return rgba; |
| 213 | } |
| 214 | |
| 215 | const topX0 = connectW ? 0 : BASE.x0; |
| 216 | const topX1 = connectE ? SIZE - 1 : BASE.x1; |
| 217 | const topY0 = connectN ? 0 : BASE.y0; |
| 218 | const topY1 = connectS ? SIZE - 1 : BASE.y1; |
| 219 | |
| 220 | for (let y = topY0; y <= topY1; y++) |
| 221 | { |
| 222 | for (let x = topX0; x <= topX1; x++) |
| 223 | { |
| 224 | const onN = y === topY0 && !connectN; |
| 225 | const onS = y === topY1 && !connectS; |
| 226 | const onW = x === topX0 && !connectW; |
| 227 | const onE = x === topX1 && !connectE; |
| 228 | let color; |
| 229 | if (onN) |
| 230 | { color = sampleTop(base, Math.min(BASE.x1, Math.max(BASE.x0, x)), BASE.y0); } |
| 231 | else if (onS) |
| 232 | { color = sampleTop(base, Math.min(BASE.x1, Math.max(BASE.x0, x)), BASE.y1); } |
| 233 | else if (onW) |
| 234 | { color = sampleTop(base, BASE.x0, Math.min(BASE.y1, Math.max(BASE.y0, y))); } |
| 235 | else if (onE) |
| 236 | { color = sampleTop(base, BASE.x1, Math.min(BASE.y1, Math.max(BASE.y0, y))); } |
| 237 | else |
| 238 | { |
| 239 | // Interior: wrap the artist's fill so long spans keep grain. |
| 240 | const ix = BASE.x0 + 1 + ((x - topX0) % (BASE.x1 - BASE.x0 - 1)); |
| 241 | const iy = BASE.y0 + 1 + ((y - topY0) % (BASE.y1 - BASE.y0 - 1)); |
| 242 | color = sampleTop(base, ix, iy); |
| 243 | } |
| 244 | setPx(rgba, x, y, color); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // Front legs only when this tile is a south terminus (perspective). |
| 249 | if (!connectS && topY1 + 2 < SIZE) |
| 250 | { |
| 251 | const span = topX1 - topX0 + 1; |
| 252 | const anchors = []; |
| 253 | if (span <= 10) |
| 254 | { |
| 255 | anchors.push(topX0 + 1); |
| 256 | anchors.push(Math.max(topX0 + 1, topX1 - 3)); |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | for (let x = topX0 + 1; x <= topX1 - 3; x += 5) |
| 261 | { anchors.push(x); } |
| 262 | const last = topX1 - 3; |
| 263 | if (anchors[anchors.length - 1] !== last) |
| 264 | { anchors.push(last); } |
| 265 | } |
| 266 | const seen = new Set(); |
| 267 | for (const ax of anchors) |
| 268 | { |
| 269 | if (seen.has(ax)) |
| 270 | { continue; } |
| 271 | seen.add(ax); |
| 272 | drawLeg(rgba, base, ax, topY1); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return rgba; |
| 277 | } |
| 278 | |
| 279 | const source = decodePngRgba(readFileSync(SOURCE)); |
| 280 | const base = magentaize(source); |
| 281 | |
| 282 | mkdirSync(OUT, { recursive: true }); |
| 283 | // Replace artist wood.png with magenta base (same silhouette). |
| 284 | writeFileSync(SOURCE, encodePng(base)); |
| 285 | for (let mask = 0; mask < 16; mask++) |
| 286 | { |
| 287 | writeFileSync(join(OUT, `${mask}.png`), encodePng(paintTable(mask, base))); |
| 288 | } |
| 289 | console.log(`Wrote magenta table sprites wood.png + 0–15 to ${OUT}`); |