/** * Generate 16×16 magenta table PNGs for neighbor-connect masks 0–15. * Reads the artist `wood.png`, remaps browns → magenta (tintable), then * extends the tabletop along connected cardinals (no diagonals). * Legs stay on free south edges; extra legs appear on long spans. */ import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { deflateSync, inflateSync } 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/table"); const SOURCE = join(OUT, "wood.png"); /** Base tabletop AABB in the artist sprite (inclusive). */ const BASE = Object.freeze({ x0: 3, x1: 12, y0: 2, y1: 11 }); 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; ihdr[10] = 0; ihdr[11] = 0; ihdr[12] = 0; 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 decodePngRgba (buf) { let off = 8; let w = 0; let h = 0; const idat = []; while (off < buf.length) { const len = buf.readUInt32BE(off); const type = buf.slice(off + 4, off + 8).toString("ascii"); const data = buf.slice(off + 8, off + 8 + len); if (type === "IHDR") { w = data.readUInt32BE(0); h = data.readUInt32BE(4); } else if (type === "IDAT") { idat.push(data); } else if (type === "IEND") { break; } off += 12 + len; } if (w !== SIZE || h !== SIZE) { throw new Error(`Expected ${SIZE}×${SIZE} PNG, got ${w}×${h}`); } const raw = inflateSync(Buffer.concat(idat)); const stride = 1 + SIZE * 4; const rgba = new Uint8Array(SIZE * SIZE * 4); for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { const i = y * stride + 1 + x * 4; const o = (y * SIZE + x) * 4; rgba[o] = raw[i]; rgba[o + 1] = raw[i + 1]; rgba[o + 2] = raw[i + 2]; rgba[o + 3] = raw[i + 3]; } } return rgba; } function getPx (rgba, x, y) { const i = (y * SIZE + x) * 4; return [rgba[i], rgba[i + 1], rgba[i + 2], rgba[i + 3]]; } 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]; } /** Remap any opaque pixel into the magenta tint family (R≈B, G= SIZE) { break; } for (let lx = 0; lx < 3; lx++) { setPx(rgba, anchorX + lx, y, pattern[ly][lx]); } } } function paintTable (mask, base) { 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; if (mask === 0) { rgba.set(base); return rgba; } const topX0 = connectW ? 0 : BASE.x0; const topX1 = connectE ? SIZE - 1 : BASE.x1; const topY0 = connectN ? 0 : BASE.y0; const topY1 = connectS ? SIZE - 1 : BASE.y1; for (let y = topY0; y <= topY1; y++) { for (let x = topX0; x <= topX1; x++) { const onN = y === topY0 && !connectN; const onS = y === topY1 && !connectS; const onW = x === topX0 && !connectW; const onE = x === topX1 && !connectE; let color; if (onN) { color = sampleTop(base, Math.min(BASE.x1, Math.max(BASE.x0, x)), BASE.y0); } else if (onS) { color = sampleTop(base, Math.min(BASE.x1, Math.max(BASE.x0, x)), BASE.y1); } else if (onW) { color = sampleTop(base, BASE.x0, Math.min(BASE.y1, Math.max(BASE.y0, y))); } else if (onE) { color = sampleTop(base, BASE.x1, Math.min(BASE.y1, Math.max(BASE.y0, y))); } else { // Interior: wrap the artist's fill so long spans keep grain. const ix = BASE.x0 + 1 + ((x - topX0) % (BASE.x1 - BASE.x0 - 1)); const iy = BASE.y0 + 1 + ((y - topY0) % (BASE.y1 - BASE.y0 - 1)); color = sampleTop(base, ix, iy); } setPx(rgba, x, y, color); } } // Front legs only when this tile is a south terminus (perspective). if (!connectS && topY1 + 2 < SIZE) { const span = topX1 - topX0 + 1; const anchors = []; if (span <= 10) { anchors.push(topX0 + 1); anchors.push(Math.max(topX0 + 1, topX1 - 3)); } else { for (let x = topX0 + 1; x <= topX1 - 3; x += 5) { anchors.push(x); } const last = topX1 - 3; if (anchors[anchors.length - 1] !== last) { anchors.push(last); } } const seen = new Set(); for (const ax of anchors) { if (seen.has(ax)) { continue; } seen.add(ax); drawLeg(rgba, base, ax, topY1); } } return rgba; } const source = decodePngRgba(readFileSync(SOURCE)); const base = magentaize(source); mkdirSync(OUT, { recursive: true }); // Replace artist wood.png with magenta base (same silhouette). writeFileSync(SOURCE, encodePng(base)); for (let mask = 0; mask < 16; mask++) { writeFileSync(join(OUT, `${mask}.png`), encodePng(paintTable(mask, base))); } console.log(`Wrote magenta table sprites wood.png + 0–15 to ${OUT}`);