| 1 | import { ConditionKind } from "./conditions.js"; |
| 2 | |
| 3 | /** |
| 4 | * Positive hasItem floors implied by a ConditionSet (ignores `not` clauses). |
| 5 | * @returns {Map<number, number>} itemId → minimum quantity |
| 6 | */ |
| 7 | export function hasItemGuaranteesFromWhen (when) |
| 8 | { |
| 9 | /** @type {Map<number, number>} */ |
| 10 | const out = new Map(); |
| 11 | for (const clause of when?.all ?? []) |
| 12 | { |
| 13 | if (clause?.not) |
| 14 | { continue; } |
| 15 | const condition = clause?.condition; |
| 16 | if (condition?.kind !== ConditionKind.hasItem) |
| 17 | { continue; } |
| 18 | const itemId = Number(condition.itemId) | 0; |
| 19 | if (itemId <= 0) |
| 20 | { continue; } |
| 21 | const quantity = Math.max(1, Number(condition.quantity) || 1) | 0; |
| 22 | out.set(itemId, Math.max(out.get(itemId) ?? 0, quantity)); |
| 23 | } |
| 24 | return out; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Outgoing dialogue edges from a step exit (branches + player triggers). |
| 29 | * @returns {{ nextStepId: string, when: object }[]} |
| 30 | */ |
| 31 | export function dialogueExitEdges (exit) |
| 32 | { |
| 33 | const edges = []; |
| 34 | if (exit?.kind === "branches") |
| 35 | { |
| 36 | for (const branch of exit.branches ?? []) |
| 37 | { |
| 38 | if (!branch?.nextStepId) |
| 39 | { continue; } |
| 40 | edges.push({ nextStepId: branch.nextStepId, when: branch.when ?? { all: [] } }); |
| 41 | } |
| 42 | } |
| 43 | else if (exit?.kind === "triggers") |
| 44 | { |
| 45 | for (const trigger of exit.triggers ?? []) |
| 46 | { |
| 47 | if (!trigger?.nextStepId) |
| 48 | { continue; } |
| 49 | edges.push({ nextStepId: trigger.nextStepId, when: trigger.when ?? { all: [] } }); |
| 50 | } |
| 51 | } |
| 52 | else if (exit?.kind === "continue") |
| 53 | { |
| 54 | if (exit.nextStepId) |
| 55 | { edges.push({ nextStepId: exit.nextStepId, when: { all: [] } }); } |
| 56 | } |
| 57 | return edges; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Item takes on a step that are not guaranteed on every path from the dialogue entry |
| 62 | * (via branch/trigger hasItem conditions + earlier step rewards/takes). |
| 63 | * @returns {Map<string, Set<number>>} stepId → unguaranteed itemIds |
| 64 | */ |
| 65 | export function unguaranteedDialogueTakes (tree) |
| 66 | { |
| 67 | /** @type {Map<string, Set<number>>} */ |
| 68 | const warnings = new Map(); |
| 69 | const steps = Array.isArray(tree?.steps) ? tree.steps : []; |
| 70 | if (!steps.length) |
| 71 | { return warnings; } |
| 72 | const byId = new Map(steps.map((step) => [step.stepId, step])); |
| 73 | const entryStepId = tree.entryStepId && byId.has(tree.entryStepId) |
| 74 | ? tree.entryStepId |
| 75 | : steps[0].stepId; |
| 76 | |
| 77 | function note (stepId, itemId) |
| 78 | { |
| 79 | let set = warnings.get(stepId); |
| 80 | if (!set) |
| 81 | { |
| 82 | set = new Set(); |
| 83 | warnings.set(stepId, set); |
| 84 | } |
| 85 | set.add(itemId | 0); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param {string} stepId |
| 90 | * @param {Map<number, number>} bag |
| 91 | * @param {Set<string>} path |
| 92 | */ |
| 93 | function visit (stepId, bag, path) |
| 94 | { |
| 95 | if (path.has(stepId)) |
| 96 | { return; } |
| 97 | const step = byId.get(stepId); |
| 98 | if (!step) |
| 99 | { return; } |
| 100 | const nextPath = new Set(path); |
| 101 | nextPath.add(stepId); |
| 102 | /** @type {Map<number, number>} */ |
| 103 | const local = new Map(bag); |
| 104 | |
| 105 | for (const take of step.itemTakes ?? []) |
| 106 | { |
| 107 | const itemId = take.itemId | 0; |
| 108 | const need = Math.max(1, Number(take.quantity) || 1) | 0; |
| 109 | if ((local.get(itemId) ?? 0) < need) |
| 110 | { note(stepId, itemId); } |
| 111 | local.set(itemId, Math.max(0, (local.get(itemId) ?? 0) - need)); |
| 112 | } |
| 113 | for (const reward of step.itemRewards ?? []) |
| 114 | { |
| 115 | const itemId = reward.itemId | 0; |
| 116 | const qty = Math.max(1, Number(reward.quantity) || 1) | 0; |
| 117 | local.set(itemId, (local.get(itemId) ?? 0) + qty); |
| 118 | } |
| 119 | |
| 120 | for (const edge of dialogueExitEdges(step.exit)) |
| 121 | { |
| 122 | /** @type {Map<number, number>} */ |
| 123 | const child = new Map(local); |
| 124 | for (const [itemId, qty] of hasItemGuaranteesFromWhen(edge.when)) |
| 125 | { child.set(itemId, Math.max(child.get(itemId) ?? 0, qty)); } |
| 126 | visit(edge.nextStepId, child, nextPath); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | visit(entryStepId, new Map(), new Set()); |
| 131 | return warnings; |
| 132 | } |
| 133 | |
| 134 | /** True when this take row should highlight yellow in the editor. */ |
| 135 | export function dialogueTakeIsUnguaranteed (warnings, stepId, itemId) |
| 136 | { |
| 137 | return !!warnings?.get?.(stepId)?.has?.(itemId | 0); |
| 138 | } |