import { normalizeConditionSet, normalizeEffects, emptyConditionSet } from "./conditions.js"; export function normalizeQuestDef (raw) { const questId = slugId(raw?.questId ?? raw?.id, "quest"); const stepsIn = Array.isArray(raw?.steps) ? raw.steps : []; const steps = stepsIn.map((step, index) => normalizeQuestStep(step, index)).filter(Boolean); return { questId, title: String(raw?.title ?? questId).trim().slice(0, 80) || questId, description: String(raw?.description ?? "").trim().slice(0, 500), steps: steps.length > 0 ? steps : [normalizeQuestStep({ stepId: "step-1" }, 0)], rewards: normalizeEffects(raw?.rewards), cancelable: !!raw?.cancelable }; } function normalizeQuestStep (raw, index) { const stepId = slugId(raw?.stepId ?? raw?.id, `step-${index + 1}`); const objectivesIn = Array.isArray(raw?.objectives) ? raw.objectives : []; let objectives = objectivesIn.map((obj, i) => normalizeObjective(obj, i)).filter(Boolean); if (objectives.length === 0) { objectives = [normalizeObjective({ objectiveId: "obj-1", text: "Complete this step", when: emptyConditionSet() }, 0)]; } return { stepId, title: String(raw?.title ?? stepId).trim().slice(0, 80) || stepId, description: String(raw?.description ?? "").trim().slice(0, 500), completionJournalText: String(raw?.completionJournalText ?? "").trim().slice(0, 500), objectives, onEnter: normalizeEffects(raw?.onEnter), onComplete: normalizeEffects(raw?.onComplete) }; } function normalizeObjective (raw, index) { const objectiveId = slugId(raw?.objectiveId ?? raw?.id, `obj-${index + 1}`); const when = normalizeConditionSet(raw?.when ?? raw?.require); return { objectiveId, text: String(raw?.text ?? objectiveId).trim().slice(0, 200) || objectiveId, when }; } export function normalizeDialogueTree (raw) { const dialogueId = slugId(raw?.dialogueId ?? raw?.id, "dialogue"); const stepsIn = Array.isArray(raw?.steps) ? raw.steps : []; const steps = stepsIn.map((step, index) => normalizeDialogueStep(step, index)).filter(Boolean); const safeSteps = steps.length > 0 ? steps : [normalizeDialogueStep({ stepId: "greeting", label: "Greeting", lines: [{ speaker: "npc", text: "…" }], exit: { kind: "end" } }, 0)]; const entryStepId = slugId(raw?.entryStepId ?? safeSteps[0].stepId, safeSteps[0].stepId); const ids = new Set(safeSteps.map((s) => s.stepId)); return { dialogueId, label: String(raw?.label ?? dialogueId).trim().slice(0, 80) || dialogueId, unique: !!raw?.unique, entryStepId: ids.has(entryStepId) ? entryStepId : safeSteps[0].stepId, steps: safeSteps }; } function normalizeDialogueStep (raw, index) { const stepId = slugId(raw?.stepId ?? raw?.id, `step-${index + 1}`); const linesIn = Array.isArray(raw?.lines) ? raw.lines : []; const lines = linesIn.map(normalizeDialogueLine).filter(Boolean); const exit = normalizeExit(raw?.exit, raw); const out = { stepId, label: String(raw?.label ?? stepId).trim().slice(0, 80) || stepId, lines: lines.length > 0 ? lines : [{ speaker: "npc", text: "…", effects: [] }], itemRewards: normalizeItemRewards(raw?.itemRewards), itemTakes: normalizeItemRewards(raw?.itemTakes), questRewards: normalizeQuestRewards(raw?.questRewards), exit }; const editorX = Number(raw?.editorX); const editorY = Number(raw?.editorY); if (Number.isFinite(editorX)) { out.editorX = editorX; } if (Number.isFinite(editorY)) { out.editorY = editorY; } return out; } /** Step-level gifts granted after all lines are read, before exit triggers/branches. */ function normalizeItemRewards (raw) { if (!Array.isArray(raw)) { return []; } const out = []; const seen = new Set(); for (const entry of raw) { if (!entry || typeof entry !== "object") { continue; } const itemId = Number(entry.itemId); if (!Number.isFinite(itemId) || itemId <= 0 || seen.has(itemId)) { continue; } seen.add(itemId); out.push({ itemId: itemId | 0, quantity: Math.max(1, Math.min(99_999, Number(entry.quantity) || 1)) | 0 }); } return out; } /** Step-level quests started after lines (same moment as item rewards). */ function normalizeQuestRewards (raw) { if (!Array.isArray(raw)) { return []; } const out = []; const seen = new Set(); for (const entry of raw) { const questId = String(entry?.questId ?? entry ?? "").trim().slice(0, 80); if (!questId || seen.has(questId)) { continue; } seen.add(questId); out.push(questId); } return out; } function normalizeDialogueLine (raw) { if (!raw || typeof raw !== "object") { return null; } const text = String(raw.text ?? "").trim().slice(0, 500); if (!text) { return null; } const speaker = raw.speaker === "player" ? "player" : "npc"; return { speaker, text, effects: normalizeEffects(raw.effects) }; } function normalizeExit (exit, legacy) { if (exit && typeof exit === "object" && exit.kind) { if (exit.kind === "triggers") { return { kind: "triggers", triggers: (Array.isArray(exit.triggers) ? exit.triggers : []) .map(normalizeTrigger) .filter(Boolean) }; } if (exit.kind === "branches") { return { kind: "branches", branches: (Array.isArray(exit.branches) ? exit.branches : []) .map(normalizeBranch) .filter(Boolean) }; } if (exit.kind === "continue") { const next = exit.nextStepId == null || exit.nextStepId === "" ? null : slugId(exit.nextStepId, ""); return { kind: "continue", nextStepId: next || null }; } return { kind: "end" }; } if (Array.isArray(legacy?.triggers) && legacy.triggers.length > 0) { return { kind: "triggers", triggers: legacy.triggers.map(normalizeTrigger).filter(Boolean) }; } if (Array.isArray(legacy?.branches) && legacy.branches.length > 0) { return { kind: "branches", branches: legacy.branches.map(normalizeBranch).filter(Boolean) }; } return { kind: "end" }; } function normalizeTrigger (raw) { if (!raw || typeof raw !== "object") { return null; } const trigger = slugId(raw.trigger ?? raw.id, "choice"); const text = String(raw.text ?? "").trim().slice(0, 120); if (!text) { return null; } const next = raw.nextStepId == null || raw.nextStepId === "" ? null : slugId(raw.nextStepId, ""); return { trigger, label: String(raw.label ?? trigger).trim().slice(0, 80) || trigger, text, nextStepId: next || null, when: normalizeConditionSet(raw.when ?? raw.require), effects: normalizeEffects(raw.effects) }; } function normalizeBranch (raw) { if (!raw || typeof raw !== "object") { return null; } const next = raw.nextStepId == null || raw.nextStepId === "" ? null : slugId(raw.nextStepId, ""); return { label: String(raw.label ?? "").trim().slice(0, 80), when: normalizeConditionSet(raw.when ?? raw.require), nextStepId: next || null, effects: normalizeEffects(raw.effects) }; } function slugId (raw, fallback) { const s = String(raw ?? "") .trim() .toLowerCase() .replace(/[^a-z0-9_-]+/g, "-") .replace(/^-+|-+$/g, "") .slice(0, 64); return s || fallback; }