| 1 | import { ItemId, itemDefinition, itemAssetPathForStack } from "/shared/definitions/items.js"; |
| 2 | import { SPRITE_SIZE_PX } from "/shared/protocol/constants.js"; |
| 3 | import { chargeSpriteFrameIndex, itemMaxCharges } from "/shared/items/charges.js"; |
| 4 | import { chequeReservedGoodsStack, isFilledChequeStack } from "/shared/items/cheque.js"; |
| 5 | import |
| 6 | { |
| 7 | applyMagentaTintToImageData, |
| 8 | applyDualChannelTintToImageData, |
| 9 | normalizeRgb, |
| 10 | resolveItemTint, |
| 11 | resolveItemDualTint |
| 12 | } from "/shared/render/tint.js"; |
| 13 | |
| 14 | const bottleCache = new Map(); |
| 15 | const fillCache = new Map(); |
| 16 | const urlCache = new Map(); |
| 17 | const imageCache = new Map(); |
| 18 | |
| 19 | function loadImage (src) |
| 20 | { |
| 21 | if (imageCache.has(src)) |
| 22 | { return imageCache.get(src); } |
| 23 | const promise = new Promise((resolve, reject) => |
| 24 | { |
| 25 | const img = new Image(); |
| 26 | img.onload = () => resolve(img); |
| 27 | img.onerror = () => reject(new Error(`Failed to load ${src}`)); |
| 28 | img.src = src; |
| 29 | }); |
| 30 | imageCache.set(src, promise); |
| 31 | return promise; |
| 32 | } |
| 33 | |
| 34 | async function getBottle () |
| 35 | { |
| 36 | if (!bottleCache.has("bottle")) |
| 37 | { bottleCache.set("bottle", loadImage("/assets/items/glass-bottle/glass-bottle.png")); } |
| 38 | return bottleCache.get("bottle"); |
| 39 | } |
| 40 | |
| 41 | async function getFill (level) |
| 42 | { |
| 43 | const key = Math.max(1, Math.min(5, level | 0 || 4)); |
| 44 | if (!fillCache.has(key)) |
| 45 | { fillCache.set(key, loadImage(`/assets/items/potion/fill-${key}.png`)); } |
| 46 | return fillCache.get(key); |
| 47 | } |
| 48 | |
| 49 | function colorKey (color) |
| 50 | { |
| 51 | const c = normalizeRgb(color); |
| 52 | return `${Math.round(c.r * 255)},${Math.round(c.g * 255)},${Math.round(c.b * 255)}`; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Bottle + magenta fill mask → tinted potion icon (UI). |
| 57 | * Fill sprites use magenta lightness; bottle stays non-magenta. |
| 58 | */ |
| 59 | export async function potionIconDataUrl (instance) |
| 60 | { |
| 61 | const fillLevel = Math.max(1, Math.min(5, instance?.fillLevel | 0 || 4)); |
| 62 | const tint = normalizeRgb(instance?.color); |
| 63 | const key = `potion|${fillLevel}|${colorKey(tint)}`; |
| 64 | if (urlCache.has(key)) |
| 65 | { return urlCache.get(key); } |
| 66 | |
| 67 | const [bottle, fill] = await Promise.all([getBottle(), getFill(fillLevel)]); |
| 68 | const canvas = document.createElement("canvas"); |
| 69 | canvas.width = SPRITE_SIZE_PX; |
| 70 | canvas.height = SPRITE_SIZE_PX; |
| 71 | const ctx = canvas.getContext("2d"); |
| 72 | ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 73 | ctx.drawImage(fill, 0, 0); |
| 74 | const image = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 75 | applyMagentaTintToImageData(image, tint); |
| 76 | ctx.putImageData(image, 0, 0); |
| 77 | ctx.drawImage(bottle, 0, 0); |
| 78 | |
| 79 | const url = canvas.toDataURL("image/png"); |
| 80 | urlCache.set(key, url); |
| 81 | return url; |
| 82 | } |
| 83 | |
| 84 | /** Tint magenta pixels in an arbitrary item PNG for UI icons. */ |
| 85 | export async function tintedItemIconDataUrl (assetPath, tint) |
| 86 | { |
| 87 | const key = `item|${assetPath}|${colorKey(tint)}`; |
| 88 | if (urlCache.has(key)) |
| 89 | { return urlCache.get(key); } |
| 90 | const img = await loadImage(assetPath); |
| 91 | const canvas = document.createElement("canvas"); |
| 92 | canvas.width = img.width || SPRITE_SIZE_PX; |
| 93 | canvas.height = img.height || SPRITE_SIZE_PX; |
| 94 | const ctx = canvas.getContext("2d"); |
| 95 | ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 96 | ctx.drawImage(img, 0, 0); |
| 97 | const image = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 98 | applyMagentaTintToImageData(image, tint); |
| 99 | ctx.putImageData(image, 0, 0); |
| 100 | const url = canvas.toDataURL("image/png"); |
| 101 | urlCache.set(key, url); |
| 102 | return url; |
| 103 | } |
| 104 | |
| 105 | /** Dual/triple-channel arrow icon (red shaft + blue tip + optional green coat). */ |
| 106 | export async function dualTintedItemIconDataUrl (assetPath, dualTint) |
| 107 | { |
| 108 | const coatKey = dualTint.coat ? colorKey(dualTint.coat) : "-"; |
| 109 | const key = `dual|${assetPath}|${colorKey(dualTint.shaft)}|${colorKey(dualTint.tip)}|${coatKey}`; |
| 110 | if (urlCache.has(key)) |
| 111 | { return urlCache.get(key); } |
| 112 | const img = await loadImage(assetPath); |
| 113 | const canvas = document.createElement("canvas"); |
| 114 | canvas.width = img.width || SPRITE_SIZE_PX; |
| 115 | canvas.height = img.height || SPRITE_SIZE_PX; |
| 116 | const ctx = canvas.getContext("2d"); |
| 117 | ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 118 | ctx.drawImage(img, 0, 0); |
| 119 | const image = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 120 | applyDualChannelTintToImageData(image, dualTint); |
| 121 | ctx.putImageData(image, 0, 0); |
| 122 | const url = canvas.toDataURL("image/png"); |
| 123 | urlCache.set(key, url); |
| 124 | return url; |
| 125 | } |
| 126 | |
| 127 | /** Crop one frame from a multi-frame item sheet for UI icons. */ |
| 128 | export async function framedItemIconDataUrl (assetPath, frameIndex, framesPerRow = 1, frameCount = 1) |
| 129 | { |
| 130 | const index = Math.max(0, Math.min((frameCount | 0 || 1) - 1, frameIndex | 0)); |
| 131 | const perRow = Math.max(1, framesPerRow | 0 || 1); |
| 132 | const key = `frame|${assetPath}|${index}|${perRow}|${frameCount}`; |
| 133 | if (urlCache.has(key)) |
| 134 | { return urlCache.get(key); } |
| 135 | const img = await loadImage(assetPath); |
| 136 | const canvas = document.createElement("canvas"); |
| 137 | canvas.width = SPRITE_SIZE_PX; |
| 138 | canvas.height = SPRITE_SIZE_PX; |
| 139 | const ctx = canvas.getContext("2d"); |
| 140 | ctx.imageSmoothingEnabled = false; |
| 141 | ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 142 | const sx = (index % perRow) * SPRITE_SIZE_PX; |
| 143 | const sy = Math.floor(index / perRow) * SPRITE_SIZE_PX; |
| 144 | ctx.drawImage(img, sx, sy, SPRITE_SIZE_PX, SPRITE_SIZE_PX, 0, 0, SPRITE_SIZE_PX, SPRITE_SIZE_PX); |
| 145 | const url = canvas.toDataURL("image/png"); |
| 146 | urlCache.set(key, url); |
| 147 | return url; |
| 148 | } |
| 149 | |
| 150 | export function isPotionStack (stack) |
| 151 | { |
| 152 | return (stack?.itemId === ItemId.potion && stack.instance?.kind === "potion") |
| 153 | || (stack?.itemId === ItemId.dye && stack.instance?.kind === "dye"); |
| 154 | } |
| 155 | |
| 156 | /** Resolve a stack to a UI icon data URL (tint / potion / framed when needed). */ |
| 157 | export async function itemIconDataUrl (stack) |
| 158 | { |
| 159 | const path = itemAssetPathForStack(stack); |
| 160 | const def = itemDefinition(stack?.itemId); |
| 161 | const frames = def?.frames | 0 || 1; |
| 162 | if (frames > 1 && itemMaxCharges(def) != null) |
| 163 | { |
| 164 | return framedItemIconDataUrl( |
| 165 | path, |
| 166 | chargeSpriteFrameIndex(stack), |
| 167 | def.framesPerRow ?? 1, |
| 168 | frames |
| 169 | ); |
| 170 | } |
| 171 | const dualTint = def?.iconTintFromTier ? null : resolveItemDualTint(stack, def); |
| 172 | if (dualTint) |
| 173 | { return dualTintedItemIconDataUrl(path, dualTint); } |
| 174 | const tint = resolveItemTint(stack, def); |
| 175 | if (tint && isPotionStack(stack)) |
| 176 | { return potionIconDataUrl(stack.instance); } |
| 177 | if (tint) |
| 178 | { return tintedItemIconDataUrl(path, tint); } |
| 179 | return path; |
| 180 | } |
| 181 | |
| 182 | /** Cheque paper with reserved goods icon centered at 50% scale. */ |
| 183 | export async function filledChequeIconDataUrl (chequeStack) |
| 184 | { |
| 185 | const goods = chequeReservedGoodsStack(chequeStack); |
| 186 | const chequePath = itemAssetPathForStack(chequeStack); |
| 187 | if (!goods?.itemId) |
| 188 | { return chequePath; } |
| 189 | const goodsKey = JSON.stringify({ |
| 190 | itemId: goods.itemId, |
| 191 | quantity: goods.quantity, |
| 192 | instance: goods.instance |
| 193 | }); |
| 194 | const cacheKey = `cheque|${goodsKey}`; |
| 195 | if (urlCache.has(cacheKey)) |
| 196 | { return urlCache.get(cacheKey); } |
| 197 | |
| 198 | const [chequeImg, goodsUrl] = await Promise.all([ |
| 199 | loadImage(chequePath), |
| 200 | itemIconDataUrl(goods) |
| 201 | ]); |
| 202 | const goodsImg = await loadImage(goodsUrl); |
| 203 | const size = SPRITE_SIZE_PX; |
| 204 | const canvas = document.createElement("canvas"); |
| 205 | canvas.width = size; |
| 206 | canvas.height = size; |
| 207 | const ctx = canvas.getContext("2d"); |
| 208 | ctx.imageSmoothingEnabled = false; |
| 209 | ctx.clearRect(0, 0, size, size); |
| 210 | ctx.drawImage(chequeImg, 0, 0, size, size); |
| 211 | const half = size * 0.5; |
| 212 | const ox = (size - half) / 2; |
| 213 | const oy = (size - half) / 2; |
| 214 | ctx.drawImage(goodsImg, 0, 0, goodsImg.width || size, goodsImg.height || size, ox, oy, half, half); |
| 215 | const url = canvas.toDataURL("image/png"); |
| 216 | urlCache.set(cacheKey, url); |
| 217 | return url; |
| 218 | } |
| 219 | |
| 220 | /** Set img.src to tinted icon when the stack/def has a tint; else fallback path. */ |
| 221 | export function applyItemIcon (img, stack, fallbackPath) |
| 222 | { |
| 223 | if (!img) |
| 224 | { return; } |
| 225 | const path = fallbackPath || itemAssetPathForStack(stack); |
| 226 | img.src = path; |
| 227 | if (isFilledChequeStack(stack)) |
| 228 | { |
| 229 | filledChequeIconDataUrl(stack).then((url) => |
| 230 | { |
| 231 | img.src = url; |
| 232 | }).catch(() => |
| 233 | { |
| 234 | img.src = path; |
| 235 | }); |
| 236 | return; |
| 237 | } |
| 238 | itemIconDataUrl(stack).then((url) => |
| 239 | { |
| 240 | if (url !== path) |
| 241 | { img.src = url; } |
| 242 | }).catch(() => |
| 243 | { |
| 244 | img.src = path; |
| 245 | }); |
| 246 | } |