| 1 | /** |
| 2 | * Generate 16×16 magenta table PNGs for neighbor-connect masks 0–15. |
| 3 | * |
| 4 | * Geometry is a **strip union** (horizontal bar ∪ vertical bar), so L/T/+ bends |
| 5 | * keep a concave inner corner instead of filling the AABB. Straight runs stay |
| 6 | * one tile wide. Future filled rectangles (2×3, etc.) can add alternate corner |
| 7 | * sprites keyed by diagonal neighbors without changing these line pieces. |
| 8 | * |
| 9 | * Legs stay on free south edges of the footprint. |
| 10 | */ |
| 11 | import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 12 | import { deflateSync, inflateSync } from "node:zlib"; |
| 13 | import { fileURLToPath } from "node:url"; |
| 14 | import { dirname, join } from "node:path"; |
| 15 | import { NeighborBit } from "../../shared/world/neighborConnect.js"; |
| 16 | |
| 17 | const SIZE = 16; |
| 18 | const OUT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks/table"); |
| 19 | const SOURCE = join(OUT, "wood.png"); |
| 20 | |
| 21 | /** Base tabletop AABB in the artist sprite (inclusive). */ |
| 22 | const BASE = Object.freeze({ |
| 23 | x0: 3, |
| 24 | x1: 12, |
| 25 | y0: 2, |
| 26 | y1: 11 |
| 27 | }); |
| 28 | |
| 29 | function crcTable () |
| 30 | { |
| 31 | const table = new Uint32Array(256); |
| 32 | for (let n = 0; n < 256; n++) |
| 33 | { |
| 34 | let c = n; |
| 35 | for (let k = 0; k < 8; k++) |
| 36 | { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); } |
| 37 | table[n] = c >>> 0; |
| 38 | } |
| 39 | return table; |
| 40 | } |
| 41 | |
| 42 | const CRC_TABLE = crcTable(); |
| 43 | |
| 44 | function crc32 (buf) |
| 45 | { |
| 46 | let c = 0xffffffff; |
| 47 | for (let i = 0; i < buf.length; i++) |
| 48 | { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); } |
| 49 | return (c ^ 0xffffffff) >>> 0; |
| 50 | } |
| 51 | |
| 52 | function chunk (type, data) |
| 53 | { |
| 54 | const len = Buffer.alloc(4); |
| 55 | len.writeUInt32BE(data.length); |
| 56 | const typeBuf = Buffer.from(type, "ascii"); |
| 57 | const crcBuf = Buffer.alloc(4); |
| 58 | crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data]))); |
| 59 | return Buffer.concat([len, typeBuf, data, crcBuf]); |
| 60 | } |
| 61 | |
| 62 | function encodePng (rgba) |
| 63 | { |
| 64 | const raw = Buffer.alloc((SIZE * 4 + 1) * SIZE); |
| 65 | for (let y = 0; y < SIZE; y++) |
| 66 | { |
| 67 | const row = y * (SIZE * 4 + 1); |
| 68 | raw[row] = 0; |
| 69 | for (let x = 0; x < SIZE; x++) |
| 70 | { |
| 71 | const i = (y * SIZE + x) * 4; |
| 72 | const o = row + 1 + x * 4; |
| 73 | raw[o] = rgba[i]; |
| 74 | raw[o + 1] = rgba[i + 1]; |
| 75 | raw[o + 2] = rgba[i + 2]; |
| 76 | raw[o + 3] = rgba[i + 3]; |
| 77 | } |
| 78 | } |
| 79 | const ihdr = Buffer.alloc(13); |
| 80 | ihdr.writeUInt32BE(SIZE, 0); |
| 81 | ihdr.writeUInt32BE(SIZE, 4); |
| 82 | ihdr[8] = 8; |
| 83 | ihdr[9] = 6; |
| 84 | ihdr[10] = 0; |
| 85 | ihdr[11] = 0; |
| 86 | ihdr[12] = 0; |
| 87 | return Buffer.concat([ |
| 88 | Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), |
| 89 | chunk("IHDR", ihdr), |
| 90 | chunk("IDAT", deflateSync(raw, { level: 9 })), |
| 91 | chunk("IEND", Buffer.alloc(0)) |
| 92 | ]); |
| 93 | } |
| 94 | |
| 95 | function decodePngRgba (buf) |
| 96 | { |
| 97 | let off = 8; |
| 98 | let w = 0; |
| 99 | let h = 0; |
| 100 | const idat = []; |
| 101 | while (off < buf.length) |
| 102 | { |
| 103 | const len = buf.readUInt32BE(off); |
| 104 | const type = buf.slice(off + 4, off + 8).toString("ascii"); |
| 105 | const data = buf.slice(off + 8, off + 8 + len); |
| 106 | if (type === "IHDR") |
| 107 | { |
| 108 | w = data.readUInt32BE(0); |
| 109 | h = data.readUInt32BE(4); |
| 110 | } |
| 111 | else if (type === "IDAT") |
| 112 | { idat.push(data); } |
| 113 | else if (type === "IEND") |
| 114 | { break; } |
| 115 | off += 12 + len; |
| 116 | } |
| 117 | if (w !== SIZE || h !== SIZE) |
| 118 | { throw new Error(`Expected ${SIZE}×${SIZE} PNG, got ${w}×${h}`); } |
| 119 | const raw = inflateSync(Buffer.concat(idat)); |
| 120 | const stride = 1 + SIZE * 4; |
| 121 | const rgba = new Uint8Array(SIZE * SIZE * 4); |
| 122 | for (let y = 0; y < SIZE; y++) |
| 123 | { |
| 124 | for (let x = 0; x < SIZE; x++) |
| 125 | { |
| 126 | const i = y * stride + 1 + x * 4; |
| 127 | const o = (y * SIZE + x) * 4; |
| 128 | rgba[o] = raw[i]; |
| 129 | rgba[o + 1] = raw[i + 1]; |
| 130 | rgba[o + 2] = raw[i + 2]; |
| 131 | rgba[o + 3] = raw[i + 3]; |
| 132 | } |
| 133 | } |
| 134 | return rgba; |
| 135 | } |
| 136 | |
| 137 | function getPx (rgba, x, y) |
| 138 | { |
| 139 | const i = (y * SIZE + x) * 4; |
| 140 | return [rgba[i], rgba[i + 1], rgba[i + 2], rgba[i + 3]]; |
| 141 | } |
| 142 | |
| 143 | function setPx (rgba, x, y, color) |
| 144 | { |
| 145 | if (x < 0 || y < 0 || x >= SIZE || y >= SIZE) |
| 146 | { return; } |
| 147 | const i = (y * SIZE + x) * 4; |
| 148 | rgba[i] = color[0]; |
| 149 | rgba[i + 1] = color[1]; |
| 150 | rgba[i + 2] = color[2]; |
| 151 | rgba[i + 3] = color[3]; |
| 152 | } |
| 153 | |
| 154 | function isAlreadyMagenta (rgba) |
| 155 | { |
| 156 | for (let i = 0; i < rgba.length; i += 4) |
| 157 | { |
| 158 | if (rgba[i + 3] < 8) |
| 159 | { continue; } |
| 160 | if (Math.abs(rgba[i] - rgba[i + 2]) > 2 || rgba[i + 1] >= rgba[i]) |
| 161 | { return false; } |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | /** Remap any opaque pixel into the magenta tint family (R≈B, G<R). */ |
| 167 | function toMagenta (r, g, b, a) |
| 168 | { |
| 169 | if (a < 8) |
| 170 | { return [0, 0, 0, 0]; } |
| 171 | const L = Math.max(1, Math.max(r, g, b)); |
| 172 | const minC = Math.min(r, g, b); |
| 173 | const chroma = (L - minC) / 255; |
| 174 | const gMag = Math.min(L - 1, Math.round(L * (0.08 + (1 - chroma) * 0.22))); |
| 175 | return [L, Math.max(0, gMag), L, 255]; |
| 176 | } |
| 177 | |
| 178 | function magentaize (src) |
| 179 | { |
| 180 | const out = new Uint8Array(src.length); |
| 181 | for (let i = 0; i < src.length; i += 4) |
| 182 | { |
| 183 | const [r, g, b, a] = toMagenta(src[i], src[i + 1], src[i + 2], src[i + 3]); |
| 184 | out[i] = r; |
| 185 | out[i + 1] = g; |
| 186 | out[i + 2] = b; |
| 187 | out[i + 3] = a; |
| 188 | } |
| 189 | return out; |
| 190 | } |
| 191 | |
| 192 | function sampleTop (base, x, y) |
| 193 | { |
| 194 | const sx = Math.min(BASE.x1, Math.max(BASE.x0, x)); |
| 195 | const sy = Math.min(BASE.y1, Math.max(BASE.y0, y)); |
| 196 | return getPx(base, sx, sy); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Line-table footprint: union of an E–W bar (core height) and an N–S bar (core width), |
| 201 | * each extended only along connected cardinals. Leaves concave cutouts for L/T bends. |
| 202 | */ |
| 203 | function isTabletopPixel (x, y, connectN, connectE, connectS, connectW) |
| 204 | { |
| 205 | const inCoreX = x >= BASE.x0 && x <= BASE.x1; |
| 206 | const inCoreY = y >= BASE.y0 && y <= BASE.y1; |
| 207 | const inHBar = inCoreY && ( |
| 208 | inCoreX |
| 209 | || (connectW && x < BASE.x0) |
| 210 | || (connectE && x > BASE.x1) |
| 211 | ); |
| 212 | const inVBar = inCoreX && ( |
| 213 | inCoreY |
| 214 | || (connectN && y < BASE.y0) |
| 215 | || (connectS && y > BASE.y1) |
| 216 | ); |
| 217 | return inHBar || inVBar; |
| 218 | } |
| 219 | |
| 220 | function drawLeg (rgba, base, anchorX, topY) |
| 221 | { |
| 222 | const pattern = [ |
| 223 | [getPx(base, 4, 12), getPx(base, 5, 12), getPx(base, 6, 12)], |
| 224 | [getPx(base, 4, 13), getPx(base, 5, 13), getPx(base, 6, 13)] |
| 225 | ]; |
| 226 | for (let ly = 0; ly < 2; ly++) |
| 227 | { |
| 228 | const y = topY + 1 + ly; |
| 229 | if (y >= SIZE) |
| 230 | { break; } |
| 231 | for (let lx = 0; lx < 3; lx++) |
| 232 | { setPx(rgba, anchorX + lx, y, pattern[ly][lx]); } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | function rimFlags (x, y, connectN, connectE, connectS, connectW) |
| 237 | { |
| 238 | const n = isTabletopPixel(x, y - 1, connectN, connectE, connectS, connectW); |
| 239 | const e = isTabletopPixel(x + 1, y, connectN, connectE, connectS, connectW); |
| 240 | const s = isTabletopPixel(x, y + 1, connectN, connectE, connectS, connectW); |
| 241 | const w = isTabletopPixel(x - 1, y, connectN, connectE, connectS, connectW); |
| 242 | return { |
| 243 | onN: !n, |
| 244 | onE: !e, |
| 245 | onS: !s, |
| 246 | onW: !w |
| 247 | }; |
| 248 | } |
| 249 | |
| 250 | function paintTable (mask, base) |
| 251 | { |
| 252 | const rgba = new Uint8Array(SIZE * SIZE * 4); |
| 253 | const connectN = (mask & NeighborBit.n) !== 0; |
| 254 | const connectE = (mask & NeighborBit.e) !== 0; |
| 255 | const connectS = (mask & NeighborBit.s) !== 0; |
| 256 | const connectW = (mask & NeighborBit.w) !== 0; |
| 257 | |
| 258 | if (mask === 0) |
| 259 | { |
| 260 | rgba.set(base); |
| 261 | return rgba; |
| 262 | } |
| 263 | |
| 264 | for (let y = 0; y < SIZE; y++) |
| 265 | { |
| 266 | for (let x = 0; x < SIZE; x++) |
| 267 | { |
| 268 | if (!isTabletopPixel(x, y, connectN, connectE, connectS, connectW)) |
| 269 | { continue; } |
| 270 | const rim = rimFlags(x, y, connectN, connectE, connectS, connectW); |
| 271 | let color; |
| 272 | if (rim.onN) |
| 273 | { color = sampleTop(base, Math.min(BASE.x1, Math.max(BASE.x0, x)), BASE.y0); } |
| 274 | else if (rim.onS) |
| 275 | { color = sampleTop(base, Math.min(BASE.x1, Math.max(BASE.x0, x)), BASE.y1); } |
| 276 | else if (rim.onW) |
| 277 | { color = sampleTop(base, BASE.x0, Math.min(BASE.y1, Math.max(BASE.y0, y))); } |
| 278 | else if (rim.onE) |
| 279 | { color = sampleTop(base, BASE.x1, Math.min(BASE.y1, Math.max(BASE.y0, y))); } |
| 280 | else |
| 281 | { |
| 282 | const ix = BASE.x0 + 1 + ((x - BASE.x0 + SIZE) % (BASE.x1 - BASE.x0 - 1)); |
| 283 | const iy = BASE.y0 + 1 + ((y - BASE.y0 + SIZE) % (BASE.y1 - BASE.y0 - 1)); |
| 284 | color = sampleTop(base, ix, iy); |
| 285 | } |
| 286 | setPx(rgba, x, y, color); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // Front legs on free south edge of the footprint (line tables only show these). |
| 291 | if (!connectS) |
| 292 | { |
| 293 | const footY = BASE.y1; |
| 294 | if (footY + 2 < SIZE) |
| 295 | { |
| 296 | const anchors = []; |
| 297 | for (let x = 0; x < SIZE; ) |
| 298 | { |
| 299 | if (!isTabletopPixel(x, footY, connectN, connectE, connectS, connectW)) |
| 300 | { |
| 301 | x++; |
| 302 | continue; |
| 303 | } |
| 304 | let x1 = x; |
| 305 | while (x1 + 1 < SIZE && isTabletopPixel(x1 + 1, footY, connectN, connectE, connectS, connectW)) |
| 306 | { x1++; } |
| 307 | const span = x1 - x + 1; |
| 308 | if (span >= 3) |
| 309 | { |
| 310 | anchors.push(x + 1); |
| 311 | if (span >= 8) |
| 312 | { anchors.push(x1 - 2); } |
| 313 | } |
| 314 | x = x1 + 1; |
| 315 | } |
| 316 | const seen = new Set(); |
| 317 | for (const ax of anchors) |
| 318 | { |
| 319 | const clamped = Math.max(0, Math.min(SIZE - 3, ax)); |
| 320 | if (seen.has(clamped)) |
| 321 | { continue; } |
| 322 | seen.add(clamped); |
| 323 | drawLeg(rgba, base, clamped, footY); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | return rgba; |
| 329 | } |
| 330 | |
| 331 | const source = decodePngRgba(readFileSync(SOURCE)); |
| 332 | const base = isAlreadyMagenta(source) ? source : magentaize(source); |
| 333 | |
| 334 | mkdirSync(OUT, { recursive: true }); |
| 335 | if (!isAlreadyMagenta(source)) |
| 336 | { writeFileSync(SOURCE, encodePng(base)); } |
| 337 | for (let mask = 0; mask < 16; mask++) |
| 338 | { |
| 339 | writeFileSync(join(OUT, `${mask}.png`), encodePng(paintTable(mask, base))); |
| 340 | } |
| 341 | console.log(`Wrote strip-union table sprites 0–15 to ${OUT}`); |