import { ConditionKind } from "./conditions.js"; /** * Positive hasItem floors implied by a ConditionSet (ignores `not` clauses). * @returns {Map} itemId → minimum quantity */ export function hasItemGuaranteesFromWhen (when) { /** @type {Map} */ const out = new Map(); for (const clause of when?.all ?? []) { if (clause?.not) { continue; } const condition = clause?.condition; if (condition?.kind !== ConditionKind.hasItem) { continue; } const itemId = Number(condition.itemId) | 0; if (itemId <= 0) { continue; } const quantity = Math.max(1, Number(condition.quantity) || 1) | 0; out.set(itemId, Math.max(out.get(itemId) ?? 0, quantity)); } return out; } /** * Outgoing dialogue edges from a step exit (branches + player triggers). * @returns {{ nextStepId: string, when: object }[]} */ export function dialogueExitEdges (exit) { const edges = []; if (exit?.kind === "branches") { for (const branch of exit.branches ?? []) { if (!branch?.nextStepId) { continue; } edges.push({ nextStepId: branch.nextStepId, when: branch.when ?? { all: [] } }); } } else if (exit?.kind === "triggers") { for (const trigger of exit.triggers ?? []) { if (!trigger?.nextStepId) { continue; } edges.push({ nextStepId: trigger.nextStepId, when: trigger.when ?? { all: [] } }); } } else if (exit?.kind === "continue") { if (exit.nextStepId) { edges.push({ nextStepId: exit.nextStepId, when: { all: [] } }); } } return edges; } /** * Item takes on a step that are not guaranteed on every path from the dialogue entry * (via branch/trigger hasItem conditions + earlier step rewards/takes). * @returns {Map>} stepId → unguaranteed itemIds */ export function unguaranteedDialogueTakes (tree) { /** @type {Map>} */ const warnings = new Map(); const steps = Array.isArray(tree?.steps) ? tree.steps : []; if (!steps.length) { return warnings; } const byId = new Map(steps.map((step) => [step.stepId, step])); const entryStepId = tree.entryStepId && byId.has(tree.entryStepId) ? tree.entryStepId : steps[0].stepId; function note (stepId, itemId) { let set = warnings.get(stepId); if (!set) { set = new Set(); warnings.set(stepId, set); } set.add(itemId | 0); } /** * @param {string} stepId * @param {Map} bag * @param {Set} path */ function visit (stepId, bag, path) { if (path.has(stepId)) { return; } const step = byId.get(stepId); if (!step) { return; } const nextPath = new Set(path); nextPath.add(stepId); /** @type {Map} */ const local = new Map(bag); for (const take of step.itemTakes ?? []) { const itemId = take.itemId | 0; const need = Math.max(1, Number(take.quantity) || 1) | 0; if ((local.get(itemId) ?? 0) < need) { note(stepId, itemId); } local.set(itemId, Math.max(0, (local.get(itemId) ?? 0) - need)); } for (const reward of step.itemRewards ?? []) { const itemId = reward.itemId | 0; const qty = Math.max(1, Number(reward.quantity) || 1) | 0; local.set(itemId, (local.get(itemId) ?? 0) + qty); } for (const edge of dialogueExitEdges(step.exit)) { /** @type {Map} */ const child = new Map(local); for (const [itemId, qty] of hasItemGuaranteesFromWhen(edge.when)) { child.set(itemId, Math.max(child.get(itemId) ?? 0, qty)); } visit(edge.nextStepId, child, nextPath); } } visit(entryStepId, new Map(), new Set()); return warnings; } /** True when this take row should highlight yellow in the editor. */ export function dialogueTakeIsUnguaranteed (warnings, stepId, itemId) { return !!warnings?.get?.(stepId)?.has?.(itemId | 0); }