BlueGit

timfa questward

Files

questward / shared / quests / questGraph.js

questGraph.js

feat: edit quests on the shared step canvas
timfa project_85689633_bot_760517ee9267c86ff1d6c3a0237abf7d · pushed by timfa · 4 weeks ago
745e7e52 History
questGraph.js 5.4 KiB Download Raw
1/**
2 * Quest-graph helpers shared by the editor and tests.
3 * Card size matches client/src/editor/dialogueCanvas.js (CARD_W / CARD_H).
4 */
5import { emptyConditionSet } from "./conditions.js";
6
7export const QUEST_CARD_W = 168;
8export const QUEST_CARD_H = 72;
9
10const COL_GAP = 88;
11const ROW_GAP = 56;
12const ORIGIN = 48;
13
14export function blankQuestStep (stepId, editorX, editorY)
15{
16 const step = {
17 stepId,
18 title: "New step",
19 description: "",
20 completionJournalText: "",
21 objectives: [{
22 objectiveId: "obj-1",
23 text: "Complete this step",
24 when: emptyConditionSet()
25 }],
26 onEnter: [],
27 onComplete: [],
28 exit: { kind: "complete" }
29 };
30 if (Number.isFinite(editorX))
31 { step.editorX = editorX; }
32 if (Number.isFinite(editorY))
33 { step.editorY = editorY; }
34 return step;
35}
36
37/** An empty condition set always passes, so it shadows every later branch. */
38export function conditionSetIsVacuous (set)
39{
40 const all = set?.all;
41 return !Array.isArray(all) || all.length === 0;
42}
43
44export function branchShadowsLater (branches, index)
45{
46 if (!Array.isArray(branches) || index < 0 || index >= branches.length - 1)
47 { return false; }
48 return conditionSetIsVacuous(branches[index]?.when);
49}
50
51/**
52 * True when this exit is the chain the loader would synthesise from array order.
53 * Branches are never implicit. A continue only matches the immediate next step.
54 */
55export function questExitMatchesImplicit (steps, index)
56{
57 const step = steps?.[index];
58 if (!step)
59 { return false; }
60 const exit = step.exit;
61 const last = index === steps.length - 1;
62 if (!exit)
63 { return true; }
64 if (exit.kind === "branches")
65 { return false; }
66 if (last)
67 { return exit.kind === "complete"; }
68 return exit.kind === "continue"
69 && (exit.nextStepId || null) === (steps[index + 1]?.stepId ?? null);
70}
71
72/**
73 * Keep implicit exits where the graph still matches array order.
74 * Anything the author changed (other target, branches, early complete) is explicit.
75 */
76export function prepareQuestExitsForSave (steps)
77{
78 for (let index = 0; index < (steps ?? []).length; index++)
79 {
80 const step = steps[index];
81 if (questExitMatchesImplicit(steps, index))
82 {
83 step.exit = index === steps.length - 1
84 ? { kind: "complete", implicit: true }
85 : {
86 kind: "continue",
87 nextStepId: steps[index + 1].stepId,
88 implicit: true
89 };
90 }
91 else if (step.exit)
92 { delete step.exit.implicit; }
93 }
94 return steps;
95}
96
97function outgoingStepIds (step)
98{
99 const exit = step?.exit;
100 if (!exit)
101 { return []; }
102 if (exit.kind === "continue" && exit.nextStepId)
103 { return [exit.nextStepId]; }
104 if (exit.kind === "branches")
105 {
106 return (exit.branches ?? [])
107 .map((branch) => branch?.nextStepId)
108 .filter(Boolean);
109 }
110 return [];
111}
112
113function autoLayout (steps)
114{
115 const depth = new Map();
116 const start = steps[0]?.stepId;
117 if (start)
118 {
119 const queue = [start];
120 depth.set(start, 0);
121 while (queue.length)
122 {
123 const id = queue.shift();
124 const step = steps.find((row) => row.stepId === id);
125 const column = depth.get(id) ?? 0;
126 for (const next of outgoingStepIds(step))
127 {
128 if (!steps.some((row) => row.stepId === next) || depth.has(next))
129 { continue; }
130 depth.set(next, column + 1);
131 queue.push(next);
132 }
133 }
134 }
135 let maxDepth = 0;
136 for (const column of depth.values())
137 { maxDepth = Math.max(maxDepth, column); }
138 const orphanColumn = steps.some((step) => !depth.has(step.stepId))
139 ? maxDepth + 1
140 : maxDepth;
141 const rows = new Map();
142 const positions = new Map();
143 for (const step of steps)
144 {
145 const column = depth.has(step.stepId) ? depth.get(step.stepId) : orphanColumn;
146 const row = rows.get(column) ?? 0;
147 rows.set(column, row + 1);
148 positions.set(step.stepId, {
149 x: ORIGIN + column * (QUEST_CARD_W + COL_GAP),
150 y: ORIGIN + row * (QUEST_CARD_H + ROW_GAP)
151 });
152 }
153 return positions;
154}
155
156/**
157 * Stored editorX/editorY win. Steps without a position use a left-to-right
158 * flow of the current wires, so a linear quest opens as a chain.
159 */
160export function layoutQuestSteps (steps)
161{
162 const list = steps ?? [];
163 const auto = autoLayout(list);
164 const out = new Map();
165 list.forEach((step, index) =>
166 {
167 const storedX = Number(step?.editorX);
168 const storedY = Number(step?.editorY);
169 if (Number.isFinite(storedX) && Number.isFinite(storedY))
170 { out.set(step.stepId, { x: storedX, y: storedY }); }
171 else
172 {
173 out.set(step.stepId, auto.get(step.stepId) ?? {
174 x: ORIGIN,
175 y: ORIGIN + index * (QUEST_CARD_H + ROW_GAP)
176 });
177 }
178 });
179 return out;
180}
181
182/**
183 * Canvas ports for a quest step. Wires call setNext, which only writes nextStepId.
184 * Rewiring a continue away from its current target drops the implicit flag.
185 */
186export function questCanvasPorts (step)
187{
188 const exit = step?.exit ?? { kind: "complete" };
189 if (exit.kind === "continue")
190 {
191 return [{
192 key: "continue",
193 label: "Continue",
194 nextStepId: exit.nextStepId ?? null,
195 setNext (id)
196 {
197 const next = id || null;
198 if ((exit.nextStepId ?? null) !== next)
199 { delete exit.implicit; }
200 exit.nextStepId = next;
201 }
202 }];
203 }
204 if (exit.kind === "branches")
205 {
206 return (exit.branches ?? []).map((branch, index) =>
207 {
208 const name = String(branch?.label ?? "").trim()
209 || String(branch?.text ?? "").trim()
210 || `Branch ${index + 1}`;
211 return {
212 key: `branch:${index}`,
213 label: `${index + 1}. ${name}`,
214 nextStepId: branch?.nextStepId ?? null,
215 setNext (id)
216 {
217 branch.nextStepId = id || null;
218 delete exit.implicit;
219 }
220 };
221 });
222 }
223 return [];
224}