| 1 | import { ItemId } from "../definitions/items.js"; |
| 2 | import { dyeColorKey } from "../definitions/dyes.js"; |
| 3 | |
| 4 | export function dyeFillLevel (stack) |
| 5 | { |
| 6 | return Math.max(1, Math.min(5, stack?.instance?.fillLevel | 0 || 4)); |
| 7 | } |
| 8 | |
| 9 | export function dyeStacksForColor (slots, colorKey) |
| 10 | { |
| 11 | const out = []; |
| 12 | for (let index = 0; index < (slots?.length ?? 0); index++) |
| 13 | { |
| 14 | const stack = slots[index]; |
| 15 | if (!stack || stack.itemId !== ItemId.dye) |
| 16 | { continue; } |
| 17 | if (dyeColorKey(stack.instance) !== colorKey) |
| 18 | { continue; } |
| 19 | out.push({ index, stack, fill: dyeFillLevel(stack) }); |
| 20 | } |
| 21 | return out; |
| 22 | } |
| 23 | |
| 24 | /** Bag slot of the chosen colour with the least fill remaining. */ |
| 25 | export function pickEmptiestDyeSlot (slots, colorKey) |
| 26 | { |
| 27 | const matches = dyeStacksForColor(slots, colorKey); |
| 28 | if (!matches.length) |
| 29 | { return null; } |
| 30 | matches.sort((a, b) => a.fill - b.fill || a.index - b.index); |
| 31 | return matches[0].index; |
| 32 | } |
| 33 | |
| 34 | /** Fullest bottle of a colour — shown in the dye picker. */ |
| 35 | export function pickFullestDyeDisplay (slots, colorKey) |
| 36 | { |
| 37 | const matches = dyeStacksForColor(slots, colorKey); |
| 38 | if (!matches.length) |
| 39 | { return null; } |
| 40 | matches.sort((a, b) => b.fill - a.fill || a.index - b.index); |
| 41 | return matches[0]; |
| 42 | } |
| 43 | |
| 44 | /** One entry per unique dye colour (fullest bottle for display). */ |
| 45 | export function listDyePickerChoices (slots) |
| 46 | { |
| 47 | const byColor = new Map(); |
| 48 | for (let index = 0; index < (slots?.length ?? 0); index++) |
| 49 | { |
| 50 | const stack = slots[index]; |
| 51 | if (!stack || stack.itemId !== ItemId.dye) |
| 52 | { continue; } |
| 53 | const key = dyeColorKey(stack.instance); |
| 54 | if (!key) |
| 55 | { continue; } |
| 56 | const fill = dyeFillLevel(stack); |
| 57 | const prev = byColor.get(key); |
| 58 | if (!prev || fill > prev.fill || (fill === prev.fill && index < prev.displayIndex)) |
| 59 | { |
| 60 | byColor.set(key, { |
| 61 | colorKey: key, |
| 62 | displayIndex: index, |
| 63 | displayStack: stack, |
| 64 | fill |
| 65 | }); |
| 66 | } |
| 67 | } |
| 68 | return [...byColor.values()].sort((a, b) => a.displayIndex - b.displayIndex); |
| 69 | } |
| 70 | |
| 71 | export function resolveDyeColorKey (slots, options = {}) |
| 72 | { |
| 73 | if (options.dyeColorKey != null) |
| 74 | { return options.dyeColorKey; } |
| 75 | if (options.dyeSlotIndex == null) |
| 76 | { return null; } |
| 77 | const picked = slots?.[options.dyeSlotIndex | 0]; |
| 78 | return picked ? dyeColorKey(picked.instance) : null; |
| 79 | } |
| 80 | |
| 81 | export function resolveDyeConsumeSlot (slots, options = {}) |
| 82 | { |
| 83 | const colorKey = resolveDyeColorKey(slots, options); |
| 84 | if (!colorKey) |
| 85 | { return null; } |
| 86 | return pickEmptiestDyeSlot(slots, colorKey); |
| 87 | } |
| 88 | |
| 89 | export function resolveDyeDisplayStack (slots, options = {}) |
| 90 | { |
| 91 | const colorKey = resolveDyeColorKey(slots, options); |
| 92 | if (!colorKey) |
| 93 | { return null; } |
| 94 | return pickFullestDyeDisplay(slots, colorKey)?.displayStack ?? null; |
| 95 | } |