BlueGit

timfa questward

Files

questward / shared / items / charges.js

charges.js

charges.js 3.9 KiB Download Raw
1import { itemDefinition } from "../definitions/items.js";
2import { CraftingItemId, TINDERBOX_MAX_CHARGES, tinderboxItemInstance } from "../definitions/craftingItems.js";
3import { alchemyPotionDefinition, UNSTABLE_POTION_EFFECTS } from "../definitions/alchemy.js";
4import { DEFAULT_DYE_FILL } from "../definitions/dyes.js";
5
6/** Max charges for items that wear down (null = not charge-based). */
7export function itemMaxCharges (itemIdOrDef)
8{
9 const def = itemIdOrDef && typeof itemIdOrDef === "object"
10 ? itemIdOrDef
11 : itemDefinition(itemIdOrDef);
12 const max = def?.maxCharges | 0;
13 return max > 0 ? max : null;
14}
15
16/** Remaining charges on a stack, or null if the item is not charge-based. */
17export function stackChargesRemaining (stack)
18{
19 const max = itemMaxCharges(stack?.itemId);
20 if (max == null)
21 { return null; }
22 if (stack?.itemId === CraftingItemId.tinderbox)
23 {
24 if (stack.instance?.kind === "tinderbox")
25 { return Math.max(0, Math.min(max, stack.instance.charges | 0)); }
26 return max;
27 }
28 if (stack?.instance && typeof stack.instance.charges === "number")
29 { return Math.max(0, Math.min(max, stack.instance.charges | 0)); }
30 return max;
31}
32
33/** 0–1 fraction of charges left (1 if not charge-based). */
34export function chargeFractionRemaining (stack)
35{
36 const max = itemMaxCharges(stack?.itemId);
37 if (max == null)
38 { return 1; }
39 return Math.min(1, Math.max(0, stackChargesRemaining(stack) / max));
40}
41
42/** True when a charge-based item has fewer than max charges left. */
43export function isChargeWorn (stack)
44{
45 const max = itemMaxCharges(stack?.itemId);
46 if (max == null)
47 { return false; }
48 return stackChargesRemaining(stack) < max;
49}
50
51/**
52 * Full bottle fill for a potion/dye instance (as bottled), or null if not a fill bottle.
53 * Unstable draughts use their effect's bottled fill — not the success-potion default.
54 */
55export function bottleFullFillLevel (instance)
56{
57 if (!instance)
58 { return null; }
59 if (instance.kind === "dye")
60 { return DEFAULT_DYE_FILL; }
61 if (instance.kind !== "potion")
62 { return null; }
63 const key = String(instance.potionKey ?? "");
64 if (instance.unstable || key.startsWith("unstable:"))
65 {
66 const effectKey = key.startsWith("unstable:")
67 ? key.slice("unstable:".length)
68 : String(instance.effectKey ?? "");
69 const entry = UNSTABLE_POTION_EFFECTS.find((row) => row.effectKey === effectKey);
70 return entry?.fillLevel ?? Math.max(1, instance.fillLevel | 0 || 1);
71 }
72 const def = alchemyPotionDefinition(instance.potionKey);
73 return def?.fillLevel ?? 4;
74}
75
76/** True when a potion/dye has been sipped or used below its bottled fill. */
77export function isFillWorn (stack)
78{
79 if (stack?.instance?.fillLevel == null)
80 { return false; }
81 const full = bottleFullFillLevel(stack.instance);
82 if (full == null)
83 { return false; }
84 return (stack.instance.fillLevel | 0) < full;
85}
86
87/** Any partially consumed charge tool, potion, or dye — unsafe for blind CE matching. */
88export function isPartiallyConsumed (stack)
89{
90 return isChargeWorn(stack) || isFillWorn(stack);
91}
92
93/**
94 * Sprite sub-image index for charge wear (frame N for N uses left).
95 * 6 charges → frame index 5; 1 charge → frame index 0.
96 */
97export function chargeSpriteFrameIndex (stack)
98{
99 const max = itemMaxCharges(stack?.itemId) ?? 1;
100 const charges = Math.max(1, Math.min(max, stackChargesRemaining(stack) ?? max));
101 return charges - 1;
102}
103
104/**
105 * Clamp / restore tinderbox (and future charge tools) instance on a stack.
106 * Charges above max migrate down to max. Returns null if charges hit 0.
107 */
108export function normalizeChargeStack (stack)
109{
110 if (!stack?.itemId)
111 { return stack ?? null; }
112 if (stack.itemId !== CraftingItemId.tinderbox)
113 { return stack; }
114 const max = itemMaxCharges(stack.itemId) ?? TINDERBOX_MAX_CHARGES;
115 let charges;
116 if (stack.instance?.kind === "tinderbox")
117 { charges = stack.instance.charges | 0; }
118 else
119 { charges = max; }
120 if (charges > max)
121 { charges = max; }
122 if (charges <= 0)
123 { return null; }
124 return {
125 ...stack,
126 instance: tinderboxItemInstance(charges)
127 };
128}