BlueGit

timfa questward

Files

questward / shared / quests / dialoguePathTakes.js

dialoguePathTakes.js

dialoguePathTakes.js 3.5 KiB Download Raw
1import { 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 */
7export 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 */
31export 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 return edges;
53}
54
55/**
56 * Item takes on a step that are not guaranteed on every path from the dialogue entry
57 * (via branch/trigger hasItem conditions + earlier step rewards/takes).
58 * @returns {Map<string, Set<number>>} stepId → unguaranteed itemIds
59 */
60export function unguaranteedDialogueTakes (tree)
61{
62 /** @type {Map<string, Set<number>>} */
63 const warnings = new Map();
64 const steps = Array.isArray(tree?.steps) ? tree.steps : [];
65 if (!steps.length)
66 { return warnings; }
67 const byId = new Map(steps.map((step) => [step.stepId, step]));
68 const entryStepId = tree.entryStepId && byId.has(tree.entryStepId)
69 ? tree.entryStepId
70 : steps[0].stepId;
71
72 function note (stepId, itemId)
73 {
74 let set = warnings.get(stepId);
75 if (!set)
76 {
77 set = new Set();
78 warnings.set(stepId, set);
79 }
80 set.add(itemId | 0);
81 }
82
83 /**
84 * @param {string} stepId
85 * @param {Map<number, number>} bag
86 * @param {Set<string>} path
87 */
88 function visit (stepId, bag, path)
89 {
90 if (path.has(stepId))
91 { return; }
92 const step = byId.get(stepId);
93 if (!step)
94 { return; }
95 const nextPath = new Set(path);
96 nextPath.add(stepId);
97 /** @type {Map<number, number>} */
98 const local = new Map(bag);
99
100 for (const take of step.itemTakes ?? [])
101 {
102 const itemId = take.itemId | 0;
103 const need = Math.max(1, Number(take.quantity) || 1) | 0;
104 if ((local.get(itemId) ?? 0) < need)
105 { note(stepId, itemId); }
106 local.set(itemId, Math.max(0, (local.get(itemId) ?? 0) - need));
107 }
108 for (const reward of step.itemRewards ?? [])
109 {
110 const itemId = reward.itemId | 0;
111 const qty = Math.max(1, Number(reward.quantity) || 1) | 0;
112 local.set(itemId, (local.get(itemId) ?? 0) + qty);
113 }
114
115 for (const edge of dialogueExitEdges(step.exit))
116 {
117 /** @type {Map<number, number>} */
118 const child = new Map(local);
119 for (const [itemId, qty] of hasItemGuaranteesFromWhen(edge.when))
120 { child.set(itemId, Math.max(child.get(itemId) ?? 0, qty)); }
121 visit(edge.nextStepId, child, nextPath);
122 }
123 }
124
125 visit(entryStepId, new Map(), new Set());
126 return warnings;
127}
128
129/** True when this take row should highlight yellow in the editor. */
130export function dialogueTakeIsUnguaranteed (warnings, stepId, itemId)
131{
132 return !!warnings?.get?.(stepId)?.has?.(itemId | 0);
133}