BlueGit

timfa questward

Files

questward / shared / crafting / potionCharge.js

potionCharge.js

potionCharge.js 1.4 KiB Download Raw
1import { ItemId } from "../definitions/items.js";
2import { alchemyPotionDefinition } from "../definitions/alchemy.js";
3
4export function potionFillLevel (stack)
5{
6 return Math.max(1, Math.min(5, stack?.instance?.fillLevel | 0 || 4));
7}
8
9/** Bag slots holding a success potion with the given potionKey. */
10export function potionStacksForKey (slots, potionKey)
11{
12 const key = String(potionKey ?? "");
13 const out = [];
14 if (!key)
15 { return out; }
16 for (let index = 0; index < (slots?.length ?? 0); index++)
17 {
18 const stack = slots[index];
19 if (!stack || (stack.itemId | 0) !== ItemId.potion)
20 { continue; }
21 if (stack.instance?.kind !== "potion")
22 { continue; }
23 if (String(stack.instance?.potionKey ?? "") !== key)
24 { continue; }
25 if (stack.instance?.unstable)
26 { continue; }
27 out.push({ index, stack, fill: potionFillLevel(stack) });
28 }
29 return out;
30}
31
32/** Bag slot of this brew with the least fill remaining. */
33export function pickEmptiestPotionSlot (slots, potionKey)
34{
35 const matches = potionStacksForKey(slots, potionKey);
36 if (!matches.length)
37 { return null; }
38 matches.sort((a, b) => a.fill - b.fill || a.index - b.index);
39 return matches[0].index;
40}
41
42export function hasPotionKey (slots, potionKey)
43{
44 return potionStacksForKey(slots, potionKey).length > 0;
45}
46
47export function potionKeyDisplayName (potionKey)
48{
49 return alchemyPotionDefinition(potionKey)?.displayName ?? "Brew";
50}