import { itemDefinition } from "../definitions/items.js"; import { CraftingItemId, TINDERBOX_MAX_CHARGES, tinderboxItemInstance } from "../definitions/craftingItems.js"; import { alchemyPotionDefinition, UNSTABLE_POTION_EFFECTS } from "../definitions/alchemy.js"; import { DEFAULT_DYE_FILL } from "../definitions/dyes.js"; /** Max charges for items that wear down (null = not charge-based). */ export function itemMaxCharges (itemIdOrDef) { const def = itemIdOrDef && typeof itemIdOrDef === "object" ? itemIdOrDef : itemDefinition(itemIdOrDef); const max = def?.maxCharges | 0; return max > 0 ? max : null; } /** Remaining charges on a stack, or null if the item is not charge-based. */ export function stackChargesRemaining (stack) { const max = itemMaxCharges(stack?.itemId); if (max == null) { return null; } if (stack?.itemId === CraftingItemId.tinderbox) { if (stack.instance?.kind === "tinderbox") { return Math.max(0, Math.min(max, stack.instance.charges | 0)); } return max; } if (stack?.instance && typeof stack.instance.charges === "number") { return Math.max(0, Math.min(max, stack.instance.charges | 0)); } return max; } /** 0–1 fraction of charges left (1 if not charge-based). */ export function chargeFractionRemaining (stack) { const max = itemMaxCharges(stack?.itemId); if (max == null) { return 1; } return Math.min(1, Math.max(0, stackChargesRemaining(stack) / max)); } /** True when a charge-based item has fewer than max charges left. */ export function isChargeWorn (stack) { const max = itemMaxCharges(stack?.itemId); if (max == null) { return false; } return stackChargesRemaining(stack) < max; } /** * Full bottle fill for a potion/dye instance (as bottled), or null if not a fill bottle. * Unstable draughts use their effect's bottled fill — not the success-potion default. */ export function bottleFullFillLevel (instance) { if (!instance) { return null; } if (instance.kind === "dye") { return DEFAULT_DYE_FILL; } if (instance.kind !== "potion") { return null; } const key = String(instance.potionKey ?? ""); if (instance.unstable || key.startsWith("unstable:")) { const effectKey = key.startsWith("unstable:") ? key.slice("unstable:".length) : String(instance.effectKey ?? ""); const entry = UNSTABLE_POTION_EFFECTS.find((row) => row.effectKey === effectKey); return entry?.fillLevel ?? Math.max(1, instance.fillLevel | 0 || 1); } const def = alchemyPotionDefinition(instance.potionKey); return def?.fillLevel ?? 4; } /** True when a potion/dye has been sipped or used below its bottled fill. */ export function isFillWorn (stack) { if (stack?.instance?.fillLevel == null) { return false; } const full = bottleFullFillLevel(stack.instance); if (full == null) { return false; } return (stack.instance.fillLevel | 0) < full; } /** Any partially consumed charge tool, potion, or dye — unsafe for blind CE matching. */ export function isPartiallyConsumed (stack) { return isChargeWorn(stack) || isFillWorn(stack); } /** * Sprite sub-image index for charge wear (frame N for N uses left). * 6 charges → frame index 5; 1 charge → frame index 0. */ export function chargeSpriteFrameIndex (stack) { const max = itemMaxCharges(stack?.itemId) ?? 1; const charges = Math.max(1, Math.min(max, stackChargesRemaining(stack) ?? max)); return charges - 1; } /** * Clamp / restore tinderbox (and future charge tools) instance on a stack. * Charges above max migrate down to max. Returns null if charges hit 0. */ export function normalizeChargeStack (stack) { if (!stack?.itemId) { return stack ?? null; } if (stack.itemId !== CraftingItemId.tinderbox) { return stack; } const max = itemMaxCharges(stack.itemId) ?? TINDERBOX_MAX_CHARGES; let charges; if (stack.instance?.kind === "tinderbox") { charges = stack.instance.charges | 0; } else { charges = max; } if (charges > max) { charges = max; } if (charges <= 0) { return null; } return { ...stack, instance: tinderboxItemInstance(charges) }; }