/** * Generate 16×16 magenta wooden fence PNGs for neighbor-connect masks 0–15. * Top-down posts + dual rails; rails extend to tile edges where connected so * adjacent same-wood fences read as one continuous run (same as tables). */ 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/fence"); /** Magenta tint family (R≈B, G>> 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 paintHorizontalRails (rgba, x0, x1) { for (const band of H_RAILS) { fillRect(rgba, x0, band.y0, x1, band.y1, RAIL); fillRect(rgba, x0, band.y0, x1, band.y0, RAIL_LIGHT); fillRect(rgba, x0, band.y1, x1, band.y1, RAIL_SHADE); } } function paintVerticalRails (rgba, y0, y1) { for (const band of V_RAILS) { fillRect(rgba, band.x0, y0, band.x1, y1, RAIL); fillRect(rgba, band.x0, y0, band.x0, y1, RAIL_LIGHT); fillRect(rgba, band.x1, y0, band.x1, y1, RAIL_SHADE); } } function paintPost (rgba) { fillRect(rgba, POST_X0, POST_Y0, POST_X1, POST_Y1, POST); fillRect(rgba, POST_X0, POST_Y0, POST_X1, POST_Y0, POST_LIGHT); fillRect(rgba, POST_X0, POST_Y1, POST_X1, POST_Y1, POST_SHADE); fillRect(rgba, POST_X0, POST_Y0, POST_X0, POST_Y1, POST_SHADE); fillRect(rgba, POST_X1, POST_Y0, POST_X1, POST_Y1, POST_LIGHT); // Cap fillRect(rgba, POST_X0 - 1, POST_Y0 - 1, POST_X1 + 1, POST_Y0 - 1, POST_SHADE); fillRect(rgba, POST_X0, POST_Y0 - 1, POST_X1, POST_Y0 - 1, POST_LIGHT); } function paintFence (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 alone = mask === 0; const wantH = alone || connectE || connectW; const wantV = alone || connectN || connectS; if (wantH) { const x0 = alone ? POST_X0 - 2 : (connectW ? 0 : POST_X0); const x1 = alone ? POST_X1 + 2 : (connectE ? SIZE - 1 : POST_X1); paintHorizontalRails(rgba, x0, x1); } if (wantV) { const y0 = alone ? POST_Y0 + 1 : (connectN ? 0 : POST_Y0); const y1 = alone ? POST_Y1 - 1 : (connectS ? SIZE - 1 : POST_Y1); paintVerticalRails(rgba, y0, y1); } paintPost(rgba); return rgba; } mkdirSync(OUT, { recursive: true }); for (let mask = 0; mask < 16; mask++) { writeFileSync(join(OUT, `${mask}.png`), encodePng(paintFence(mask))); } console.log(`Wrote magenta fence sprites 0–15 to ${OUT}`);