/** * Quest-graph helpers shared by the editor and tests. * Card size matches client/src/editor/dialogueCanvas.js (CARD_W / CARD_H). */ import { emptyConditionSet } from "./conditions.js"; export const QUEST_CARD_W = 168; export const QUEST_CARD_H = 72; const COL_GAP = 88; const ROW_GAP = 56; const ORIGIN = 48; export function blankQuestStep (stepId, editorX, editorY) { const step = { stepId, title: "New step", description: "", completionJournalText: "", objectives: [{ objectiveId: "obj-1", text: "Complete this step", when: emptyConditionSet() }], onEnter: [], onComplete: [], exit: { kind: "complete" } }; if (Number.isFinite(editorX)) { step.editorX = editorX; } if (Number.isFinite(editorY)) { step.editorY = editorY; } return step; } /** An empty condition set always passes, so it shadows every later branch. */ export function conditionSetIsVacuous (set) { const all = set?.all; return !Array.isArray(all) || all.length === 0; } export function branchShadowsLater (branches, index) { if (!Array.isArray(branches) || index < 0 || index >= branches.length - 1) { return false; } return conditionSetIsVacuous(branches[index]?.when); } /** * True when this exit is the chain the loader would synthesise from array order. * Branches are never implicit. A continue only matches the immediate next step. */ export function questExitMatchesImplicit (steps, index) { const step = steps?.[index]; if (!step) { return false; } const exit = step.exit; const last = index === steps.length - 1; if (!exit) { return true; } if (exit.kind === "branches") { return false; } if (last) { return exit.kind === "complete"; } return exit.kind === "continue" && (exit.nextStepId || null) === (steps[index + 1]?.stepId ?? null); } /** * Keep implicit exits where the graph still matches array order. * Anything the author changed (other target, branches, early complete) is explicit. */ export function prepareQuestExitsForSave (steps) { for (let index = 0; index < (steps ?? []).length; index++) { const step = steps[index]; if (questExitMatchesImplicit(steps, index)) { step.exit = index === steps.length - 1 ? { kind: "complete", implicit: true } : { kind: "continue", nextStepId: steps[index + 1].stepId, implicit: true }; } else if (step.exit) { delete step.exit.implicit; } } return steps; } function outgoingStepIds (step) { const exit = step?.exit; if (!exit) { return []; } if (exit.kind === "continue" && exit.nextStepId) { return [exit.nextStepId]; } if (exit.kind === "branches") { return (exit.branches ?? []) .map((branch) => branch?.nextStepId) .filter(Boolean); } return []; } function autoLayout (steps) { const depth = new Map(); const start = steps[0]?.stepId; if (start) { const queue = [start]; depth.set(start, 0); while (queue.length) { const id = queue.shift(); const step = steps.find((row) => row.stepId === id); const column = depth.get(id) ?? 0; for (const next of outgoingStepIds(step)) { if (!steps.some((row) => row.stepId === next) || depth.has(next)) { continue; } depth.set(next, column + 1); queue.push(next); } } } let maxDepth = 0; for (const column of depth.values()) { maxDepth = Math.max(maxDepth, column); } const orphanColumn = steps.some((step) => !depth.has(step.stepId)) ? maxDepth + 1 : maxDepth; const rows = new Map(); const positions = new Map(); for (const step of steps) { const column = depth.has(step.stepId) ? depth.get(step.stepId) : orphanColumn; const row = rows.get(column) ?? 0; rows.set(column, row + 1); positions.set(step.stepId, { x: ORIGIN + column * (QUEST_CARD_W + COL_GAP), y: ORIGIN + row * (QUEST_CARD_H + ROW_GAP) }); } return positions; } /** * Stored editorX/editorY win. Steps without a position use a left-to-right * flow of the current wires, so a linear quest opens as a chain. */ export function layoutQuestSteps (steps) { const list = steps ?? []; const auto = autoLayout(list); const out = new Map(); list.forEach((step, index) => { const storedX = Number(step?.editorX); const storedY = Number(step?.editorY); if (Number.isFinite(storedX) && Number.isFinite(storedY)) { out.set(step.stepId, { x: storedX, y: storedY }); } else { out.set(step.stepId, auto.get(step.stepId) ?? { x: ORIGIN, y: ORIGIN + index * (QUEST_CARD_H + ROW_GAP) }); } }); return out; } /** * Canvas ports for a quest step. Wires call setNext, which only writes nextStepId. * Rewiring a continue away from its current target drops the implicit flag. */ export function questCanvasPorts (step) { const exit = step?.exit ?? { kind: "complete" }; if (exit.kind === "continue") { return [{ key: "continue", label: "Continue", nextStepId: exit.nextStepId ?? null, setNext (id) { const next = id || null; if ((exit.nextStepId ?? null) !== next) { delete exit.implicit; } exit.nextStepId = next; } }]; } if (exit.kind === "branches") { return (exit.branches ?? []).map((branch, index) => { const name = String(branch?.label ?? "").trim() || String(branch?.text ?? "").trim() || `Branch ${index + 1}`; return { key: `branch:${index}`, label: `${index + 1}. ${name}`, nextStepId: branch?.nextStepId ?? null, setNext (id) { branch.nextStepId = id || null; delete exit.implicit; } }; }); } return []; }