/** * Temporary unique 16×16 herb sprites under client/assets/herbs/. * One PNG per herbId — shared by world nodes and inventory icons. */ import { mkdirSync, writeFileSync } from "node:fs"; import { deflateSync } from "node:zlib"; import { fileURLToPath } from "node:url"; import { dirname, join } from "node:path"; import { HERB_ORDER, HERB_DEFINITIONS } from "../../shared/definitions/herbs.js"; const SIZE = 16; const OUT = join(dirname(fileURLToPath(import.meta.url)), "../assets/herbs"); 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) { 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 hashString (text) { let h = 2166136261; for (let i = 0; i < text.length; i++) { h ^= text.charCodeAt(i); h = Math.imul(h, 16777619); } return h >>> 0; } /** Distinct leaf/flower palettes keyed by herb identity. */ function paletteFor (herbId, tier) { const h = hashString(herbId); const hue = (h % 360); const sat = 0.35 + ((h >>> 8) % 40) / 100; const lit = 0.28 + (tier - 1) * 0.04 + ((h >>> 16) % 20) / 100; const flower = hsl(hue, sat, lit); const leaf = hsl((hue + 100) % 360, 0.45, 0.32); const stem = hsl((hue + 90) % 360, 0.35, 0.22); const accent = hsl((hue + 40) % 360, Math.min(0.85, sat + 0.2), Math.min(0.7, lit + 0.15)); return { flower, leaf, stem, accent }; } function hsl (h, s, l) { const a = s * Math.min(l, 1 - l); const f = (n) => { const k = (n + h / 30) % 12; const c = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); return Math.round(255 * Math.min(1, Math.max(0, c))); }; return [f(0), f(8), f(4), 255]; } function drawHerb (herbId, tier) { const rgba = new Uint8Array(SIZE * SIZE * 4); const { flower, leaf, stem, accent } = paletteFor(herbId, tier); const shape = hashString(herbId) % 5; // Stem for (let y = 10; y < 15; y++) { setPx(rgba, 7, y, stem); setPx(rgba, 8, y, stem); } if (shape === 0) { // Bushy leaves for (const [x, y] of [[4, 8], [5, 7], [6, 6], [7, 5], [8, 5], [9, 6], [10, 7], [11, 8], [5, 9], [6, 8], [9, 8], [10, 9], [6, 10], [9, 10]]) { setPx(rgba, x, y, leaf); } for (const [x, y] of [[6, 4], [7, 3], [8, 3], [9, 4], [7, 4], [8, 4]]) { setPx(rgba, x, y, flower); } } else if (shape === 1) { // Spike / upright for (let y = 3; y < 11; y++) { setPx(rgba, 7, y, flower); setPx(rgba, 8, y, accent); if (y % 2 === 0) { setPx(rgba, 6, y, leaf); setPx(rgba, 9, y, leaf); } } } else if (shape === 2) { // Bell / hanging for (const [x, y] of [[5, 5], [6, 4], [7, 3], [8, 3], [9, 4], [10, 5]]) { setPx(rgba, x, y, leaf); } 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]]) { setPx(rgba, x, y, flower); } setPx(rgba, 7, 9, accent); setPx(rgba, 8, 9, accent); } else if (shape === 3) { // Round bloom for (let dy = -3; dy <= 3; dy++) { for (let dx = -3; dx <= 3; dx++) { if (dx * dx + dy * dy <= 10) { setPx(rgba, 7 + dx, 6 + dy, flower); } if (dx * dx + dy * dy <= 3) { setPx(rgba, 7 + dx, 6 + dy, accent); } } } setPx(rgba, 4, 9, leaf); setPx(rgba, 5, 10, leaf); setPx(rgba, 10, 9, leaf); setPx(rgba, 11, 10, leaf); } else { // Berry cluster / root-ish for (const [x, y] of [[5, 7], [6, 6], [7, 5], [8, 5], [9, 6], [10, 7], [6, 8], [9, 8]]) { setPx(rgba, x, y, leaf); } for (const [x, y] of [[6, 9], [7, 8], [8, 8], [9, 9], [7, 9], [8, 9], [7, 10], [8, 10]]) { setPx(rgba, x, y, flower); } setPx(rgba, 7, 7, accent); setPx(rgba, 8, 7, accent); } // Tier mark (tiny dots for high tiers) if (tier >= 5) { setPx(rgba, 1, 1, accent); setPx(rgba, 2, 1, accent); } if (tier >= 7) { setPx(rgba, 13, 1, accent); setPx(rgba, 14, 1, accent); } return rgba; } mkdirSync(OUT, { recursive: true }); for (const herbId of HERB_ORDER) { const def = HERB_DEFINITIONS[herbId]; const png = encodePng(drawHerb(herbId, def.tier)); writeFileSync(join(OUT, `${herbId}.png`), png); console.log(`wrote ${herbId}.png (tier ${def.tier})`); } console.log(`Generated ${HERB_ORDER.length} herb sprites → ${OUT}`);