| 1 | /** |
| 2 | * Temporary 16×16 status-effect icons (replace with final art later). |
| 3 | */ |
| 4 | import { mkdirSync, writeFileSync } from "node:fs"; |
| 5 | import { deflateSync } from "node:zlib"; |
| 6 | import { fileURLToPath } from "node:url"; |
| 7 | import { dirname, join } from "node:path"; |
| 8 | import { STATUS_EFFECT_DISPLAY } from "../../shared/statusEffects/display.js"; |
| 9 | |
| 10 | const SIZE = 16; |
| 11 | const OUT = join(dirname(fileURLToPath(import.meta.url)), "../assets/status-effects"); |
| 12 | |
| 13 | const PALETTE = { |
| 14 | "health-regen": [80, 200, 100], |
| 15 | "stamina-regen": [90, 200, 170], |
| 16 | "strength-bonus": [220, 100, 60], |
| 17 | "mind-bonus": [140, 120, 230], |
| 18 | "stamina-drain": [180, 60, 90], |
| 19 | glow: [240, 220, 90], |
| 20 | shrink: [180, 140, 210], |
| 21 | blind: [40, 40, 50] |
| 22 | }; |
| 23 | |
| 24 | function crcTable () |
| 25 | { |
| 26 | const table = new Uint32Array(256); |
| 27 | for (let n = 0; n < 256; n++) |
| 28 | { |
| 29 | let c = n; |
| 30 | for (let k = 0; k < 8; k++) |
| 31 | { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); } |
| 32 | table[n] = c >>> 0; |
| 33 | } |
| 34 | return table; |
| 35 | } |
| 36 | |
| 37 | const CRC_TABLE = crcTable(); |
| 38 | |
| 39 | function crc32 (buf) |
| 40 | { |
| 41 | let c = 0xffffffff; |
| 42 | for (let i = 0; i < buf.length; i++) |
| 43 | { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); } |
| 44 | return (c ^ 0xffffffff) >>> 0; |
| 45 | } |
| 46 | |
| 47 | function chunk (type, data) |
| 48 | { |
| 49 | const len = Buffer.alloc(4); |
| 50 | len.writeUInt32BE(data.length); |
| 51 | const typeBuf = Buffer.from(type, "ascii"); |
| 52 | const crcBuf = Buffer.alloc(4); |
| 53 | crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data]))); |
| 54 | return Buffer.concat([len, typeBuf, data, crcBuf]); |
| 55 | } |
| 56 | |
| 57 | function encodePng (rgba) |
| 58 | { |
| 59 | const raw = Buffer.alloc((SIZE * 4 + 1) * SIZE); |
| 60 | for (let y = 0; y < SIZE; y++) |
| 61 | { |
| 62 | const row = y * (SIZE * 4 + 1); |
| 63 | raw[row] = 0; |
| 64 | for (let x = 0; x < SIZE; x++) |
| 65 | { |
| 66 | const i = (y * SIZE + x) * 4; |
| 67 | const o = row + 1 + x * 4; |
| 68 | raw[o] = rgba[i]; |
| 69 | raw[o + 1] = rgba[i + 1]; |
| 70 | raw[o + 2] = rgba[i + 2]; |
| 71 | raw[o + 3] = rgba[i + 3]; |
| 72 | } |
| 73 | } |
| 74 | const ihdr = Buffer.alloc(13); |
| 75 | ihdr.writeUInt32BE(SIZE, 0); |
| 76 | ihdr.writeUInt32BE(SIZE, 4); |
| 77 | ihdr[8] = 8; |
| 78 | ihdr[9] = 6; |
| 79 | return Buffer.concat([ |
| 80 | Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), |
| 81 | chunk("IHDR", ihdr), |
| 82 | chunk("IDAT", deflateSync(raw, { level: 9 })), |
| 83 | chunk("IEND", Buffer.alloc(0)) |
| 84 | ]); |
| 85 | } |
| 86 | |
| 87 | function setPx (rgba, x, y, rgb) |
| 88 | { |
| 89 | if (x < 0 || y < 0 || x >= SIZE || y >= SIZE) |
| 90 | { return; } |
| 91 | const i = (y * SIZE + x) * 4; |
| 92 | rgba[i] = rgb[0]; |
| 93 | rgba[i + 1] = rgb[1]; |
| 94 | rgba[i + 2] = rgb[2]; |
| 95 | rgba[i + 3] = 255; |
| 96 | } |
| 97 | |
| 98 | function drawIcon (rgb) |
| 99 | { |
| 100 | const rgba = new Uint8Array(SIZE * SIZE * 4); |
| 101 | const rim = [Math.max(0, rgb[0] - 50), Math.max(0, rgb[1] - 50), Math.max(0, rgb[2] - 50)]; |
| 102 | for (let y = 2; y < 14; y++) |
| 103 | { |
| 104 | for (let x = 2; x < 14; x++) |
| 105 | { |
| 106 | const edge = x === 2 || x === 13 || y === 2 || y === 13; |
| 107 | setPx(rgba, x, y, edge ? rim : rgb); |
| 108 | } |
| 109 | } |
| 110 | // Simple glyph: plus / diamond / bar |
| 111 | for (let i = 5; i < 11; i++) |
| 112 | { |
| 113 | setPx(rgba, i, 7, [255, 255, 255]); |
| 114 | setPx(rgba, 7, i, [255, 255, 255]); |
| 115 | } |
| 116 | return rgba; |
| 117 | } |
| 118 | |
| 119 | mkdirSync(OUT, { recursive: true }); |
| 120 | for (const display of Object.values(STATUS_EFFECT_DISPLAY)) |
| 121 | { |
| 122 | const rgb = PALETTE[display.iconKey] ?? [160, 160, 160]; |
| 123 | writeFileSync(join(OUT, `${display.iconKey}.png`), encodePng(drawIcon(rgb))); |
| 124 | console.log(`wrote ${display.iconKey}.png`); |
| 125 | } |