/** * Derive cloth shorts/trousers and leather-shoe sprites from greaves/boots overlays. * Shorts: greaves with the bottom few rows cleared. */ import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { deflateSync, inflateSync } from "node:zlib"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; const SIZE = 16; const SHORTS_TRIM_ROWS = 4; const root = join(dirname(fileURLToPath(import.meta.url)), "../assets/items"); const armour = join(root, "armour"); const clothing = join(root, "clothing"); 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, width = SIZE, height = SIZE) { const raw = Buffer.alloc((width * 4 + 1) * height); for (let y = 0; y < height; y++) { const row = y * (width * 4 + 1); raw[row] = 0; for (let x = 0; x < width; x++) { const i = (y * width + 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(width, 0); ihdr.writeUInt32BE(height, 4); ihdr[8] = 8; ihdr[9] = 6; 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; } const raw = inflateSync(Buffer.concat(idat)); const stride = 1 + w * 4; const rgba = new Uint8Array(w * h * 4); for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { const i = y * stride + 1 + x * 4; const o = (y * w + 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 { width: w, height: h, rgba }; } function writePair (name, rgba) { mkdirSync(clothing, { recursive: true }); writeFileSync(join(clothing, `${name}.png`), encodePng(rgba)); writeFileSync(join(clothing, `${name}-equip.png`), encodePng(rgba)); } function trimBottomRows (rgba, width, height, rows) { const next = new Uint8Array(rgba); for (let y = height - rows; y < height; y++) { for (let x = 0; x < width; x++) { const o = (y * width + x) * 4; next[o] = 0; next[o + 1] = 0; next[o + 2] = 0; next[o + 3] = 0; } } return next; } const greaves = decodePngRgba(readFileSync(join(armour, "greaves.png"))); const boots = decodePngRgba(readFileSync(join(armour, "boots.png"))); writePair("trousers", greaves.rgba); writePair("shorts", trimBottomRows(greaves.rgba, greaves.width, greaves.height, SHORTS_TRIM_ROWS)); writePair("leather-shoes", boots.rgba); mkdirSync(join(root, "leather"), { recursive: true }); writeFileSync(join(root, "leather", "leather.png"), encodePng(greaves.rgba)); console.log("Wrote shorts, trousers, leather-shoes, and leather icon sprites");