| 1 | import { itemDefinition } from "../definitions/items.js"; |
| 2 | import { CraftingItemId, TINDERBOX_MAX_CHARGES, tinderboxItemInstance } from "../definitions/craftingItems.js"; |
| 3 | |
| 4 | /** Max charges for items that wear down (null = not charge-based). */ |
| 5 | export function itemMaxCharges (itemIdOrDef) |
| 6 | { |
| 7 | const def = itemIdOrDef && typeof itemIdOrDef === "object" |
| 8 | ? itemIdOrDef |
| 9 | : itemDefinition(itemIdOrDef); |
| 10 | const max = def?.maxCharges | 0; |
| 11 | return max > 0 ? max : null; |
| 12 | } |
| 13 | |
| 14 | /** Remaining charges on a stack, or null if the item is not charge-based. */ |
| 15 | export function stackChargesRemaining (stack) |
| 16 | { |
| 17 | const max = itemMaxCharges(stack?.itemId); |
| 18 | if (max == null) |
| 19 | { return null; } |
| 20 | if (stack?.itemId === CraftingItemId.tinderbox) |
| 21 | { |
| 22 | if (stack.instance?.kind === "tinderbox") |
| 23 | { return Math.max(0, Math.min(max, stack.instance.charges | 0)); } |
| 24 | return max; |
| 25 | } |
| 26 | if (stack?.instance && typeof stack.instance.charges === "number") |
| 27 | { return Math.max(0, Math.min(max, stack.instance.charges | 0)); } |
| 28 | return max; |
| 29 | } |
| 30 | |
| 31 | /** 0–1 fraction of charges left (1 if not charge-based). */ |
| 32 | export function chargeFractionRemaining (stack) |
| 33 | { |
| 34 | const max = itemMaxCharges(stack?.itemId); |
| 35 | if (max == null) |
| 36 | { return 1; } |
| 37 | return Math.min(1, Math.max(0, stackChargesRemaining(stack) / max)); |
| 38 | } |
| 39 | |
| 40 | /** True when a charge-based item has fewer than max charges left. */ |
| 41 | export function isChargeWorn (stack) |
| 42 | { |
| 43 | const max = itemMaxCharges(stack?.itemId); |
| 44 | if (max == null) |
| 45 | { return false; } |
| 46 | return stackChargesRemaining(stack) < max; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Sprite sub-image index for charge wear (frame N for N uses left). |
| 51 | * 6 charges → frame index 5; 1 charge → frame index 0. |
| 52 | */ |
| 53 | export function chargeSpriteFrameIndex (stack) |
| 54 | { |
| 55 | const max = itemMaxCharges(stack?.itemId) ?? 1; |
| 56 | const charges = Math.max(1, Math.min(max, stackChargesRemaining(stack) ?? max)); |
| 57 | return charges - 1; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Clamp / restore tinderbox (and future charge tools) instance on a stack. |
| 62 | * Charges above max migrate down to max. Returns null if charges hit 0. |
| 63 | */ |
| 64 | export function normalizeChargeStack (stack) |
| 65 | { |
| 66 | if (!stack?.itemId) |
| 67 | { return stack ?? null; } |
| 68 | if (stack.itemId !== CraftingItemId.tinderbox) |
| 69 | { return stack; } |
| 70 | const max = itemMaxCharges(stack.itemId) ?? TINDERBOX_MAX_CHARGES; |
| 71 | let charges; |
| 72 | if (stack.instance?.kind === "tinderbox") |
| 73 | { charges = stack.instance.charges | 0; } |
| 74 | else |
| 75 | { charges = max; } |
| 76 | if (charges > max) |
| 77 | { charges = max; } |
| 78 | if (charges <= 0) |
| 79 | { return null; } |
| 80 | return { |
| 81 | ...stack, |
| 82 | instance: tinderboxItemInstance(charges) |
| 83 | }; |
| 84 | } |