| 1 | /** |
| 2 | * Derive cloth shorts/trousers and leather-shoe sprites from greaves/boots overlays. |
| 3 | * Shorts: greaves with the bottom few rows cleared. |
| 4 | */ |
| 5 | import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 6 | import { deflateSync, inflateSync } from "node:zlib"; |
| 7 | import { dirname, join } from "node:path"; |
| 8 | import { fileURLToPath } from "node:url"; |
| 9 | |
| 10 | const SIZE = 16; |
| 11 | const SHORTS_TRIM_ROWS = 4; |
| 12 | const root = join(dirname(fileURLToPath(import.meta.url)), "../assets/items"); |
| 13 | const armour = join(root, "armour"); |
| 14 | const clothing = join(root, "clothing"); |
| 15 | |
| 16 | function crcTable () |
| 17 | { |
| 18 | const table = new Uint32Array(256); |
| 19 | for (let n = 0; n < 256; n++) |
| 20 | { |
| 21 | let c = n; |
| 22 | for (let k = 0; k < 8; k++) |
| 23 | { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); } |
| 24 | table[n] = c >>> 0; |
| 25 | } |
| 26 | return table; |
| 27 | } |
| 28 | |
| 29 | const CRC_TABLE = crcTable(); |
| 30 | |
| 31 | function crc32 (buf) |
| 32 | { |
| 33 | let c = 0xffffffff; |
| 34 | for (let i = 0; i < buf.length; i++) |
| 35 | { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); } |
| 36 | return (c ^ 0xffffffff) >>> 0; |
| 37 | } |
| 38 | |
| 39 | function chunk (type, data) |
| 40 | { |
| 41 | const len = Buffer.alloc(4); |
| 42 | len.writeUInt32BE(data.length); |
| 43 | const typeBuf = Buffer.from(type, "ascii"); |
| 44 | const crcBuf = Buffer.alloc(4); |
| 45 | crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data]))); |
| 46 | return Buffer.concat([len, typeBuf, data, crcBuf]); |
| 47 | } |
| 48 | |
| 49 | function encodePng (rgba, width = SIZE, height = SIZE) |
| 50 | { |
| 51 | const raw = Buffer.alloc((width * 4 + 1) * height); |
| 52 | for (let y = 0; y < height; y++) |
| 53 | { |
| 54 | const row = y * (width * 4 + 1); |
| 55 | raw[row] = 0; |
| 56 | for (let x = 0; x < width; x++) |
| 57 | { |
| 58 | const i = (y * width + x) * 4; |
| 59 | const o = row + 1 + x * 4; |
| 60 | raw[o] = rgba[i]; |
| 61 | raw[o + 1] = rgba[i + 1]; |
| 62 | raw[o + 2] = rgba[i + 2]; |
| 63 | raw[o + 3] = rgba[i + 3]; |
| 64 | } |
| 65 | } |
| 66 | const ihdr = Buffer.alloc(13); |
| 67 | ihdr.writeUInt32BE(width, 0); |
| 68 | ihdr.writeUInt32BE(height, 4); |
| 69 | ihdr[8] = 8; |
| 70 | ihdr[9] = 6; |
| 71 | return Buffer.concat([ |
| 72 | Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), |
| 73 | chunk("IHDR", ihdr), |
| 74 | chunk("IDAT", deflateSync(raw, { level: 9 })), |
| 75 | chunk("IEND", Buffer.alloc(0)) |
| 76 | ]); |
| 77 | } |
| 78 | |
| 79 | function decodePngRgba (buf) |
| 80 | { |
| 81 | let off = 8; |
| 82 | let w = 0; |
| 83 | let h = 0; |
| 84 | const idat = []; |
| 85 | while (off < buf.length) |
| 86 | { |
| 87 | const len = buf.readUInt32BE(off); |
| 88 | const type = buf.slice(off + 4, off + 8).toString("ascii"); |
| 89 | const data = buf.slice(off + 8, off + 8 + len); |
| 90 | if (type === "IHDR") |
| 91 | { |
| 92 | w = data.readUInt32BE(0); |
| 93 | h = data.readUInt32BE(4); |
| 94 | } |
| 95 | else if (type === "IDAT") |
| 96 | { idat.push(data); } |
| 97 | else if (type === "IEND") |
| 98 | { break; } |
| 99 | off += 12 + len; |
| 100 | } |
| 101 | const raw = inflateSync(Buffer.concat(idat)); |
| 102 | const stride = 1 + w * 4; |
| 103 | const rgba = new Uint8Array(w * h * 4); |
| 104 | for (let y = 0; y < h; y++) |
| 105 | { |
| 106 | for (let x = 0; x < w; x++) |
| 107 | { |
| 108 | const i = y * stride + 1 + x * 4; |
| 109 | const o = (y * w + x) * 4; |
| 110 | rgba[o] = raw[i]; |
| 111 | rgba[o + 1] = raw[i + 1]; |
| 112 | rgba[o + 2] = raw[i + 2]; |
| 113 | rgba[o + 3] = raw[i + 3]; |
| 114 | } |
| 115 | } |
| 116 | return { width: w, height: h, rgba }; |
| 117 | } |
| 118 | |
| 119 | function writePair (name, rgba) |
| 120 | { |
| 121 | mkdirSync(clothing, { recursive: true }); |
| 122 | writeFileSync(join(clothing, `${name}.png`), encodePng(rgba)); |
| 123 | writeFileSync(join(clothing, `${name}-equip.png`), encodePng(rgba)); |
| 124 | } |
| 125 | |
| 126 | function trimBottomRows (rgba, width, height, rows) |
| 127 | { |
| 128 | const next = new Uint8Array(rgba); |
| 129 | for (let y = height - rows; y < height; y++) |
| 130 | { |
| 131 | for (let x = 0; x < width; x++) |
| 132 | { |
| 133 | const o = (y * width + x) * 4; |
| 134 | next[o] = 0; |
| 135 | next[o + 1] = 0; |
| 136 | next[o + 2] = 0; |
| 137 | next[o + 3] = 0; |
| 138 | } |
| 139 | } |
| 140 | return next; |
| 141 | } |
| 142 | |
| 143 | const greaves = decodePngRgba(readFileSync(join(armour, "greaves.png"))); |
| 144 | const boots = decodePngRgba(readFileSync(join(armour, "boots.png"))); |
| 145 | |
| 146 | writePair("trousers", greaves.rgba); |
| 147 | writePair("shorts", trimBottomRows(greaves.rgba, greaves.width, greaves.height, SHORTS_TRIM_ROWS)); |
| 148 | writePair("leather-shoes", boots.rgba); |
| 149 | |
| 150 | mkdirSync(join(root, "leather"), { recursive: true }); |
| 151 | writeFileSync(join(root, "leather", "leather.png"), encodePng(greaves.rgba)); |
| 152 | |
| 153 | console.log("Wrote shorts, trousers, leather-shoes, and leather icon sprites"); |