/** * Generate 16×16 magenta table PNGs for neighbor-connect masks 0–15. * * Geometry is a **strip union** (horizontal bar ∪ vertical bar), so L/T/+ bends * keep a concave inner corner instead of filling the AABB. Straight runs stay * one tile wide. Future filled rectangles (2×3, etc.) can add alternate corner * sprites keyed by diagonal neighbors without changing these line pieces. * * Legs stay on free south edges of the footprint. */ 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]; } function isAlreadyMagenta (rgba) { for (let i = 0; i < rgba.length; i += 4) { if (rgba[i + 3] < 8) { continue; } if (Math.abs(rgba[i] - rgba[i + 2]) > 2 || rgba[i + 1] >= rgba[i]) { return false; } } return true; } /** Remap any opaque pixel into the magenta tint family (R≈B, G= BASE.x0 && x <= BASE.x1; const inCoreY = y >= BASE.y0 && y <= BASE.y1; const inHBar = inCoreY && ( inCoreX || (connectW && x < BASE.x0) || (connectE && x > BASE.x1) ); const inVBar = inCoreX && ( inCoreY || (connectN && y < BASE.y0) || (connectS && y > BASE.y1) ); return inHBar || inVBar; } function drawLeg (rgba, base, anchorX, topY) { const pattern = [ [getPx(base, 4, 12), getPx(base, 5, 12), getPx(base, 6, 12)], [getPx(base, 4, 13), getPx(base, 5, 13), getPx(base, 6, 13)] ]; for (let ly = 0; ly < 2; ly++) { const y = topY + 1 + ly; if (y >= SIZE) { break; } for (let lx = 0; lx < 3; lx++) { setPx(rgba, anchorX + lx, y, pattern[ly][lx]); } } } function rimFlags (x, y, connectN, connectE, connectS, connectW) { const n = isTabletopPixel(x, y - 1, connectN, connectE, connectS, connectW); const e = isTabletopPixel(x + 1, y, connectN, connectE, connectS, connectW); const s = isTabletopPixel(x, y + 1, connectN, connectE, connectS, connectW); const w = isTabletopPixel(x - 1, y, connectN, connectE, connectS, connectW); return { onN: !n, onE: !e, onS: !s, onW: !w }; } 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; } for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { if (!isTabletopPixel(x, y, connectN, connectE, connectS, connectW)) { continue; } const rim = rimFlags(x, y, connectN, connectE, connectS, connectW); let color; if (rim.onN) { color = sampleTop(base, Math.min(BASE.x1, Math.max(BASE.x0, x)), BASE.y0); } else if (rim.onS) { color = sampleTop(base, Math.min(BASE.x1, Math.max(BASE.x0, x)), BASE.y1); } else if (rim.onW) { color = sampleTop(base, BASE.x0, Math.min(BASE.y1, Math.max(BASE.y0, y))); } else if (rim.onE) { color = sampleTop(base, BASE.x1, Math.min(BASE.y1, Math.max(BASE.y0, y))); } else { const ix = BASE.x0 + 1 + ((x - BASE.x0 + SIZE) % (BASE.x1 - BASE.x0 - 1)); const iy = BASE.y0 + 1 + ((y - BASE.y0 + SIZE) % (BASE.y1 - BASE.y0 - 1)); color = sampleTop(base, ix, iy); } setPx(rgba, x, y, color); } } // Front legs on free south edge of the footprint (line tables only show these). if (!connectS) { const footY = BASE.y1; if (footY + 2 < SIZE) { const anchors = []; for (let x = 0; x < SIZE; ) { if (!isTabletopPixel(x, footY, connectN, connectE, connectS, connectW)) { x++; continue; } let x1 = x; while (x1 + 1 < SIZE && isTabletopPixel(x1 + 1, footY, connectN, connectE, connectS, connectW)) { x1++; } const span = x1 - x + 1; if (span >= 3) { anchors.push(x + 1); if (span >= 8) { anchors.push(x1 - 2); } } x = x1 + 1; } const seen = new Set(); for (const ax of anchors) { const clamped = Math.max(0, Math.min(SIZE - 3, ax)); if (seen.has(clamped)) { continue; } seen.add(clamped); drawLeg(rgba, base, clamped, footY); } } } return rgba; } const source = decodePngRgba(readFileSync(SOURCE)); const base = isAlreadyMagenta(source) ? source : magentaize(source); mkdirSync(OUT, { recursive: true }); if (!isAlreadyMagenta(source)) { writeFileSync(SOURCE, encodePng(base)); } for (let mask = 0; mask < 16; mask++) { writeFileSync(join(OUT, `${mask}.png`), encodePng(paintTable(mask, base))); } console.log(`Wrote strip-union table sprites 0–15 to ${OUT}`);