/** * Generate 16×16 top-down wooden counter PNGs for neighbor-connect masks 0–15. * Perspective: orthographic top-down (grid-aligned, not diamond isometric). * Connections remove the shared edge rim so adjacent tiles read as one counter. */ import { mkdirSync, writeFileSync } from "node:fs"; import { deflateSync } 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/marketplace"); /** Wood palette (RGBA). */ const RIM = [92, 58, 32, 255]; const RIM_DARK = [62, 38, 20, 255]; const TOP = [196, 148, 78, 255]; const TOP_LIGHT = [214, 170, 98, 255]; const TOP_SHADE = [168, 120, 58, 255]; const GRAIN = [180, 132, 64, 255]; 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 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 fillRect (rgba, x0, y0, x1, y1, color) { for (let y = y0; y <= y1; y++) { for (let x = x0; x <= x1; x++) { setPx(rgba, x, y, color); } } } function paintCounter (mask) { 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; const topY0 = connectN ? 0 : 2; const topY1 = connectS ? SIZE - 1 : SIZE - 3; const topX0 = connectW ? 0 : 2; const topX1 = connectE ? SIZE - 1 : SIZE - 3; // Base fill transparent then rim body where not connected. for (let i = 0; i < rgba.length; i += 4) { rgba[i + 3] = 0; } // Outer rim (only on non-connected sides). if (!connectN) { fillRect(rgba, 0, 0, SIZE - 1, 1, RIM_DARK); } if (!connectS) { fillRect(rgba, 0, SIZE - 2, SIZE - 1, SIZE - 1, RIM_DARK); } if (!connectW) { fillRect(rgba, 0, 0, 1, SIZE - 1, RIM_DARK); } if (!connectE) { fillRect(rgba, SIZE - 2, 0, SIZE - 1, SIZE - 1, RIM_DARK); } // Inner rim bevel if (!connectN) { fillRect(rgba, topX0, 1, topX1, 1, RIM); } if (!connectS) { fillRect(rgba, topX0, SIZE - 2, topX1, SIZE - 2, RIM); } if (!connectW) { fillRect(rgba, 1, topY0, 1, topY1, RIM); } if (!connectE) { fillRect(rgba, SIZE - 2, topY0, SIZE - 2, topY1, RIM); } // Countertop fillRect(rgba, topX0, topY0, topX1, topY1, TOP); // Subtle grain / highlight for (let y = topY0; y <= topY1; y++) { for (let x = topX0; x <= topX1; x++) { if (((x + y * 3) % 7) === 0) { setPx(rgba, x, y, GRAIN); } if (y === topY0 && !connectN) { setPx(rgba, x, y, TOP_LIGHT); } if (y === topY1 && !connectS) { setPx(rgba, x, y, TOP_SHADE); } } } // Corner posts when both adjacent sides are free (standalone corners). const corners = [ { x: 0, y: 0, n: connectN, w: connectW }, { x: SIZE - 1, y: 0, n: connectN, e: connectE }, { x: 0, y: SIZE - 1, s: connectS, w: connectW }, { x: SIZE - 1, y: SIZE - 1, s: connectS, e: connectE } ]; for (const c of corners) { const openN = c.n === false || c.n == null; const openS = c.s === false || c.s == null; const openE = c.e === false || c.e == null; const openW = c.w === false || c.w == null; if ((c.n != null || c.s != null) && (c.e != null || c.w != null)) { const vertOpen = (c.n != null ? !c.n : !c.s); const horzOpen = (c.e != null ? !c.e : !c.w); if (vertOpen && horzOpen) { setPx(rgba, c.x, c.y, RIM_DARK); } } void openN; void openS; void openE; void openW; } return rgba; } mkdirSync(OUT, { recursive: true }); for (let mask = 0; mask < 16; mask++) { const png = encodePng(paintCounter(mask)); writeFileSync(join(OUT, `${mask}.png`), png); } // Keep legacy fallback name pointing at isolated counter. writeFileSync(join(OUT, "marketplace.png"), encodePng(paintCounter(0))); console.log(`Wrote marketplace counter sprites 0–15 to ${OUT}`);