| 1 | /** |
| 2 | * Temporary unique 16×16 herb sprites under client/assets/herbs/. |
| 3 | * One PNG per herbId — shared by world nodes and inventory icons. |
| 4 | */ |
| 5 | import { mkdirSync, writeFileSync } from "node:fs"; |
| 6 | import { deflateSync } from "node:zlib"; |
| 7 | import { fileURLToPath } from "node:url"; |
| 8 | import { dirname, join } from "node:path"; |
| 9 | import { HERB_ORDER, HERB_DEFINITIONS } from "../../shared/definitions/herbs.js"; |
| 10 | |
| 11 | const SIZE = 16; |
| 12 | const OUT = join(dirname(fileURLToPath(import.meta.url)), "../assets/herbs"); |
| 13 | |
| 14 | function crcTable () |
| 15 | { |
| 16 | const table = new Uint32Array(256); |
| 17 | for (let n = 0; n < 256; n++) |
| 18 | { |
| 19 | let c = n; |
| 20 | for (let k = 0; k < 8; k++) |
| 21 | { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); } |
| 22 | table[n] = c >>> 0; |
| 23 | } |
| 24 | return table; |
| 25 | } |
| 26 | |
| 27 | const CRC_TABLE = crcTable(); |
| 28 | |
| 29 | function crc32 (buf) |
| 30 | { |
| 31 | let c = 0xffffffff; |
| 32 | for (let i = 0; i < buf.length; i++) |
| 33 | { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); } |
| 34 | return (c ^ 0xffffffff) >>> 0; |
| 35 | } |
| 36 | |
| 37 | function chunk (type, data) |
| 38 | { |
| 39 | const len = Buffer.alloc(4); |
| 40 | len.writeUInt32BE(data.length); |
| 41 | const typeBuf = Buffer.from(type, "ascii"); |
| 42 | const crcBuf = Buffer.alloc(4); |
| 43 | crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data]))); |
| 44 | return Buffer.concat([len, typeBuf, data, crcBuf]); |
| 45 | } |
| 46 | |
| 47 | function encodePng (rgba) |
| 48 | { |
| 49 | const raw = Buffer.alloc((SIZE * 4 + 1) * SIZE); |
| 50 | for (let y = 0; y < SIZE; y++) |
| 51 | { |
| 52 | const row = y * (SIZE * 4 + 1); |
| 53 | raw[row] = 0; |
| 54 | for (let x = 0; x < SIZE; x++) |
| 55 | { |
| 56 | const i = (y * SIZE + x) * 4; |
| 57 | const o = row + 1 + x * 4; |
| 58 | raw[o] = rgba[i]; |
| 59 | raw[o + 1] = rgba[i + 1]; |
| 60 | raw[o + 2] = rgba[i + 2]; |
| 61 | raw[o + 3] = rgba[i + 3]; |
| 62 | } |
| 63 | } |
| 64 | const ihdr = Buffer.alloc(13); |
| 65 | ihdr.writeUInt32BE(SIZE, 0); |
| 66 | ihdr.writeUInt32BE(SIZE, 4); |
| 67 | ihdr[8] = 8; |
| 68 | ihdr[9] = 6; |
| 69 | ihdr[10] = 0; |
| 70 | ihdr[11] = 0; |
| 71 | ihdr[12] = 0; |
| 72 | return Buffer.concat([ |
| 73 | Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), |
| 74 | chunk("IHDR", ihdr), |
| 75 | chunk("IDAT", deflateSync(raw, { level: 9 })), |
| 76 | chunk("IEND", Buffer.alloc(0)) |
| 77 | ]); |
| 78 | } |
| 79 | |
| 80 | function setPx (rgba, x, y, color) |
| 81 | { |
| 82 | if (x < 0 || y < 0 || x >= SIZE || y >= SIZE) |
| 83 | { return; } |
| 84 | const i = (y * SIZE + x) * 4; |
| 85 | rgba[i] = color[0]; |
| 86 | rgba[i + 1] = color[1]; |
| 87 | rgba[i + 2] = color[2]; |
| 88 | rgba[i + 3] = color[3]; |
| 89 | } |
| 90 | |
| 91 | function hashString (text) |
| 92 | { |
| 93 | let h = 2166136261; |
| 94 | for (let i = 0; i < text.length; i++) |
| 95 | { |
| 96 | h ^= text.charCodeAt(i); |
| 97 | h = Math.imul(h, 16777619); |
| 98 | } |
| 99 | return h >>> 0; |
| 100 | } |
| 101 | |
| 102 | /** Distinct leaf/flower palettes keyed by herb identity. */ |
| 103 | function paletteFor (herbId, tier) |
| 104 | { |
| 105 | const h = hashString(herbId); |
| 106 | const hue = (h % 360); |
| 107 | const sat = 0.35 + ((h >>> 8) % 40) / 100; |
| 108 | const lit = 0.28 + (tier - 1) * 0.04 + ((h >>> 16) % 20) / 100; |
| 109 | const flower = hsl(hue, sat, lit); |
| 110 | const leaf = hsl((hue + 100) % 360, 0.45, 0.32); |
| 111 | const stem = hsl((hue + 90) % 360, 0.35, 0.22); |
| 112 | const accent = hsl((hue + 40) % 360, Math.min(0.85, sat + 0.2), Math.min(0.7, lit + 0.15)); |
| 113 | return { flower, leaf, stem, accent }; |
| 114 | } |
| 115 | |
| 116 | function hsl (h, s, l) |
| 117 | { |
| 118 | const a = s * Math.min(l, 1 - l); |
| 119 | const f = (n) => |
| 120 | { |
| 121 | const k = (n + h / 30) % 12; |
| 122 | const c = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); |
| 123 | return Math.round(255 * Math.min(1, Math.max(0, c))); |
| 124 | }; |
| 125 | return [f(0), f(8), f(4), 255]; |
| 126 | } |
| 127 | |
| 128 | function drawHerb (herbId, tier) |
| 129 | { |
| 130 | const rgba = new Uint8Array(SIZE * SIZE * 4); |
| 131 | const { flower, leaf, stem, accent } = paletteFor(herbId, tier); |
| 132 | const shape = hashString(herbId) % 5; |
| 133 | |
| 134 | // Stem |
| 135 | for (let y = 10; y < 15; y++) |
| 136 | { |
| 137 | setPx(rgba, 7, y, stem); |
| 138 | setPx(rgba, 8, y, stem); |
| 139 | } |
| 140 | |
| 141 | if (shape === 0) |
| 142 | { |
| 143 | // Bushy leaves |
| 144 | for (const [x, y] of [[4, 8], [5, 7], [6, 6], [7, 5], [8, 5], [9, 6], [10, 7], [11, 8], |
| 145 | [5, 9], [6, 8], [9, 8], [10, 9], [6, 10], [9, 10]]) |
| 146 | { setPx(rgba, x, y, leaf); } |
| 147 | for (const [x, y] of [[6, 4], [7, 3], [8, 3], [9, 4], [7, 4], [8, 4]]) |
| 148 | { setPx(rgba, x, y, flower); } |
| 149 | } |
| 150 | else if (shape === 1) |
| 151 | { |
| 152 | // Spike / upright |
| 153 | for (let y = 3; y < 11; y++) |
| 154 | { |
| 155 | setPx(rgba, 7, y, flower); |
| 156 | setPx(rgba, 8, y, accent); |
| 157 | if (y % 2 === 0) |
| 158 | { |
| 159 | setPx(rgba, 6, y, leaf); |
| 160 | setPx(rgba, 9, y, leaf); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | else if (shape === 2) |
| 165 | { |
| 166 | // Bell / hanging |
| 167 | for (const [x, y] of [[5, 5], [6, 4], [7, 3], [8, 3], [9, 4], [10, 5]]) |
| 168 | { setPx(rgba, x, y, leaf); } |
| 169 | for (const [x, y] of [[6, 6], [7, 6], [8, 6], [9, 6], [6, 7], [7, 8], [8, 8], [9, 7], [7, 7], [8, 7]]) |
| 170 | { setPx(rgba, x, y, flower); } |
| 171 | setPx(rgba, 7, 9, accent); |
| 172 | setPx(rgba, 8, 9, accent); |
| 173 | } |
| 174 | else if (shape === 3) |
| 175 | { |
| 176 | // Round bloom |
| 177 | for (let dy = -3; dy <= 3; dy++) |
| 178 | { |
| 179 | for (let dx = -3; dx <= 3; dx++) |
| 180 | { |
| 181 | if (dx * dx + dy * dy <= 10) |
| 182 | { setPx(rgba, 7 + dx, 6 + dy, flower); } |
| 183 | if (dx * dx + dy * dy <= 3) |
| 184 | { setPx(rgba, 7 + dx, 6 + dy, accent); } |
| 185 | } |
| 186 | } |
| 187 | setPx(rgba, 4, 9, leaf); |
| 188 | setPx(rgba, 5, 10, leaf); |
| 189 | setPx(rgba, 10, 9, leaf); |
| 190 | setPx(rgba, 11, 10, leaf); |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | // Berry cluster / root-ish |
| 195 | for (const [x, y] of [[5, 7], [6, 6], [7, 5], [8, 5], [9, 6], [10, 7], [6, 8], [9, 8]]) |
| 196 | { setPx(rgba, x, y, leaf); } |
| 197 | for (const [x, y] of [[6, 9], [7, 8], [8, 8], [9, 9], [7, 9], [8, 9], [7, 10], [8, 10]]) |
| 198 | { setPx(rgba, x, y, flower); } |
| 199 | setPx(rgba, 7, 7, accent); |
| 200 | setPx(rgba, 8, 7, accent); |
| 201 | } |
| 202 | |
| 203 | // Tier mark (tiny dots for high tiers) |
| 204 | if (tier >= 5) |
| 205 | { |
| 206 | setPx(rgba, 1, 1, accent); |
| 207 | setPx(rgba, 2, 1, accent); |
| 208 | } |
| 209 | if (tier >= 7) |
| 210 | { |
| 211 | setPx(rgba, 13, 1, accent); |
| 212 | setPx(rgba, 14, 1, accent); |
| 213 | } |
| 214 | |
| 215 | return rgba; |
| 216 | } |
| 217 | |
| 218 | mkdirSync(OUT, { recursive: true }); |
| 219 | for (const herbId of HERB_ORDER) |
| 220 | { |
| 221 | const def = HERB_DEFINITIONS[herbId]; |
| 222 | const png = encodePng(drawHerb(herbId, def.tier)); |
| 223 | writeFileSync(join(OUT, `${herbId}.png`), png); |
| 224 | console.log(`wrote ${herbId}.png (tier ${def.tier})`); |
| 225 | } |
| 226 | console.log(`Generated ${HERB_ORDER.length} herb sprites → ${OUT}`); |