| 1 | import { normalizeConditionSet, normalizeEffects, emptyConditionSet } from "./conditions.js"; |
| 2 | |
| 3 | /** |
| 4 | * Step ids only, matching normalizeQuestDef, so condition conversion can run |
| 5 | * before (or without) a fully normalized catalog. |
| 6 | */ |
| 7 | export function previewQuestDef (raw) |
| 8 | { |
| 9 | const questId = slugId(raw?.questId ?? raw?.id, "quest"); |
| 10 | const stepsIn = Array.isArray(raw?.steps) ? raw.steps : []; |
| 11 | let steps = stepsIn.map((step, index) => ({ |
| 12 | stepId: slugId(step?.stepId ?? step?.id, `step-${index + 1}`) |
| 13 | })); |
| 14 | if (steps.length === 0) |
| 15 | { steps = [{ stepId: "step-1" }]; } |
| 16 | return { questId, steps }; |
| 17 | } |
| 18 | |
| 19 | /** Later raws overwrite the same questId (the def being saved wins). */ |
| 20 | export function questLookupFromRawDefs (raws) |
| 21 | { |
| 22 | const map = new Map(); |
| 23 | for (const raw of raws ?? []) |
| 24 | { |
| 25 | const preview = previewQuestDef(raw); |
| 26 | map.set(preview.questId, preview); |
| 27 | } |
| 28 | return (questId) => map.get(String(questId ?? "")) ?? null; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @param {object} raw |
| 33 | * @param {Function | null} [questLookup] Catalog for legacy quest-progress conditions. |
| 34 | */ |
| 35 | export function normalizeQuestDef (raw, questLookup = null) |
| 36 | { |
| 37 | const questId = slugId(raw?.questId ?? raw?.id, "quest"); |
| 38 | const stepsIn = Array.isArray(raw?.steps) ? raw.steps : []; |
| 39 | const steps = stepsIn.map((step, index) => normalizeQuestStep(step, index, questLookup)).filter(Boolean); |
| 40 | const safeSteps = steps.length > 0 ? steps : [normalizeQuestStep({ stepId: "step-1" }, 0, questLookup)]; |
| 41 | linkImplicitQuestExits(safeSteps); |
| 42 | return { |
| 43 | questId, |
| 44 | title: String(raw?.title ?? questId).trim().slice(0, 80) || questId, |
| 45 | description: String(raw?.description ?? "").trim().slice(0, 500), |
| 46 | steps: safeSteps, |
| 47 | rewards: normalizeEffects(raw?.rewards), |
| 48 | cancelable: !!raw?.cancelable |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | function normalizeQuestStep (raw, index, questLookup) |
| 53 | { |
| 54 | const stepId = slugId(raw?.stepId ?? raw?.id, `step-${index + 1}`); |
| 55 | const objectivesIn = Array.isArray(raw?.objectives) ? raw.objectives : []; |
| 56 | let objectives = objectivesIn.map((obj, i) => normalizeObjective(obj, i, questLookup)).filter(Boolean); |
| 57 | if (objectives.length === 0) |
| 58 | { |
| 59 | objectives = [normalizeObjective({ |
| 60 | objectiveId: "obj-1", |
| 61 | text: "Complete this step", |
| 62 | when: emptyConditionSet() |
| 63 | }, 0, questLookup)]; |
| 64 | } |
| 65 | return { |
| 66 | stepId, |
| 67 | title: String(raw?.title ?? stepId).trim().slice(0, 80) || stepId, |
| 68 | description: String(raw?.description ?? "").trim().slice(0, 500), |
| 69 | completionJournalText: String(raw?.completionJournalText ?? "").trim().slice(0, 500), |
| 70 | objectives, |
| 71 | onEnter: normalizeEffects(raw?.onEnter), |
| 72 | onComplete: normalizeEffects(raw?.onComplete), |
| 73 | exit: normalizeExplicitQuestExit(raw?.exit, questLookup) |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Missing exit, or exit.implicit, is re-linked to array order on every load |
| 79 | * so the linear editor can insert steps without rewriting stored blobs. |
| 80 | * Explicit exits (continue / branches / complete without implicit) are kept. |
| 81 | */ |
| 82 | function normalizeExplicitQuestExit (exit, questLookup) |
| 83 | { |
| 84 | if (!exit || typeof exit !== "object" || exit.implicit === true || !exit.kind) |
| 85 | { return null; } |
| 86 | if (exit.kind === "branches") |
| 87 | { |
| 88 | return { |
| 89 | kind: "branches", |
| 90 | branches: (Array.isArray(exit.branches) ? exit.branches : []) |
| 91 | .map((branch) => normalizeQuestBranch(branch, questLookup)) |
| 92 | .filter(Boolean) |
| 93 | }; |
| 94 | } |
| 95 | if (exit.kind === "continue") |
| 96 | { |
| 97 | const next = exit.nextStepId == null || exit.nextStepId === "" |
| 98 | ? null |
| 99 | : slugId(exit.nextStepId, ""); |
| 100 | return { kind: "continue", nextStepId: next || null }; |
| 101 | } |
| 102 | if (exit.kind === "complete") |
| 103 | { return { kind: "complete" }; } |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | function linkImplicitQuestExits (steps) |
| 108 | { |
| 109 | for (let index = 0; index < steps.length; index++) |
| 110 | { |
| 111 | if (steps[index].exit) |
| 112 | { continue; } |
| 113 | if (index === steps.length - 1) |
| 114 | { steps[index].exit = { kind: "complete", implicit: true }; } |
| 115 | else |
| 116 | { |
| 117 | steps[index].exit = { |
| 118 | kind: "continue", |
| 119 | nextStepId: steps[index + 1].stepId, |
| 120 | implicit: true |
| 121 | }; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | function normalizeQuestBranch (raw, questLookup) |
| 127 | { |
| 128 | if (!raw || typeof raw !== "object") |
| 129 | { return null; } |
| 130 | const next = raw.nextStepId == null || raw.nextStepId === "" |
| 131 | ? null |
| 132 | : slugId(raw.nextStepId, ""); |
| 133 | return { |
| 134 | label: String(raw.label ?? "").trim().slice(0, 80), |
| 135 | text: String(raw.text ?? "").trim().slice(0, 200), |
| 136 | when: normalizeConditionSet(raw.when ?? raw.require, { quests: questLookup }), |
| 137 | nextStepId: next || null, |
| 138 | effects: normalizeEffects(raw.effects) |
| 139 | }; |
| 140 | } |
| 141 | |
| 142 | function normalizeObjective (raw, index, questLookup) |
| 143 | { |
| 144 | const objectiveId = slugId(raw?.objectiveId ?? raw?.id, `obj-${index + 1}`); |
| 145 | const when = normalizeConditionSet(raw?.when ?? raw?.require, { quests: questLookup }); |
| 146 | return { |
| 147 | objectiveId, |
| 148 | text: String(raw?.text ?? objectiveId).trim().slice(0, 200) || objectiveId, |
| 149 | when |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | export function normalizeDialogueTree (raw, questLookup = null) |
| 154 | { |
| 155 | const dialogueId = slugId(raw?.dialogueId ?? raw?.id, "dialogue"); |
| 156 | const stepsIn = Array.isArray(raw?.steps) ? raw.steps : []; |
| 157 | const steps = stepsIn.map((step, index) => normalizeDialogueStep(step, index, questLookup)).filter(Boolean); |
| 158 | const safeSteps = steps.length > 0 |
| 159 | ? steps |
| 160 | : [normalizeDialogueStep({ |
| 161 | stepId: "greeting", |
| 162 | label: "Greeting", |
| 163 | lines: [{ speaker: "npc", text: "…" }], |
| 164 | exit: { kind: "end" } |
| 165 | }, 0, questLookup)]; |
| 166 | const entryStepId = slugId(raw?.entryStepId ?? safeSteps[0].stepId, safeSteps[0].stepId); |
| 167 | const ids = new Set(safeSteps.map((s) => s.stepId)); |
| 168 | return { |
| 169 | dialogueId, |
| 170 | label: String(raw?.label ?? dialogueId).trim().slice(0, 80) || dialogueId, |
| 171 | unique: !!raw?.unique, |
| 172 | entryStepId: ids.has(entryStepId) ? entryStepId : safeSteps[0].stepId, |
| 173 | steps: safeSteps |
| 174 | }; |
| 175 | } |
| 176 | |
| 177 | function normalizeDialogueStep (raw, index, questLookup) |
| 178 | { |
| 179 | const stepId = slugId(raw?.stepId ?? raw?.id, `step-${index + 1}`); |
| 180 | const linesIn = Array.isArray(raw?.lines) ? raw.lines : []; |
| 181 | const lines = linesIn.map(normalizeDialogueLine).filter(Boolean); |
| 182 | const exit = normalizeExit(raw?.exit, raw, questLookup); |
| 183 | const out = { |
| 184 | stepId, |
| 185 | label: String(raw?.label ?? stepId).trim().slice(0, 80) || stepId, |
| 186 | lines: lines.length > 0 ? lines : [{ speaker: "npc", text: "…", effects: [] }], |
| 187 | itemRewards: normalizeItemRewards(raw?.itemRewards), |
| 188 | itemTakes: normalizeItemRewards(raw?.itemTakes), |
| 189 | questRewards: normalizeQuestRewards(raw?.questRewards), |
| 190 | exit |
| 191 | }; |
| 192 | const editorX = Number(raw?.editorX); |
| 193 | const editorY = Number(raw?.editorY); |
| 194 | if (Number.isFinite(editorX)) |
| 195 | { out.editorX = editorX; } |
| 196 | if (Number.isFinite(editorY)) |
| 197 | { out.editorY = editorY; } |
| 198 | return out; |
| 199 | } |
| 200 | |
| 201 | /** Step-level gifts granted after all lines are read, before exit triggers/branches. */ |
| 202 | function normalizeItemRewards (raw) |
| 203 | { |
| 204 | if (!Array.isArray(raw)) |
| 205 | { return []; } |
| 206 | const out = []; |
| 207 | const seen = new Set(); |
| 208 | for (const entry of raw) |
| 209 | { |
| 210 | if (!entry || typeof entry !== "object") |
| 211 | { continue; } |
| 212 | const itemId = Number(entry.itemId); |
| 213 | if (!Number.isFinite(itemId) || itemId <= 0 || seen.has(itemId)) |
| 214 | { continue; } |
| 215 | seen.add(itemId); |
| 216 | out.push({ |
| 217 | itemId: itemId | 0, |
| 218 | quantity: Math.max(1, Math.min(99_999, Number(entry.quantity) || 1)) | 0 |
| 219 | }); |
| 220 | } |
| 221 | return out; |
| 222 | } |
| 223 | |
| 224 | /** Step-level quests started after lines (same moment as item rewards). */ |
| 225 | function normalizeQuestRewards (raw) |
| 226 | { |
| 227 | if (!Array.isArray(raw)) |
| 228 | { return []; } |
| 229 | const out = []; |
| 230 | const seen = new Set(); |
| 231 | for (const entry of raw) |
| 232 | { |
| 233 | const questId = String(entry?.questId ?? entry ?? "").trim().slice(0, 80); |
| 234 | if (!questId || seen.has(questId)) |
| 235 | { continue; } |
| 236 | seen.add(questId); |
| 237 | out.push(questId); |
| 238 | } |
| 239 | return out; |
| 240 | } |
| 241 | |
| 242 | function normalizeDialogueLine (raw) |
| 243 | { |
| 244 | if (!raw || typeof raw !== "object") |
| 245 | { return null; } |
| 246 | const text = String(raw.text ?? "").trim().slice(0, 500); |
| 247 | if (!text) |
| 248 | { return null; } |
| 249 | const speaker = raw.speaker === "player" ? "player" : "npc"; |
| 250 | return { speaker, text, effects: normalizeEffects(raw.effects) }; |
| 251 | } |
| 252 | |
| 253 | function normalizeExit (exit, legacy, questLookup) |
| 254 | { |
| 255 | if (exit && typeof exit === "object" && exit.kind) |
| 256 | { |
| 257 | if (exit.kind === "triggers") |
| 258 | { |
| 259 | return { |
| 260 | kind: "triggers", |
| 261 | triggers: (Array.isArray(exit.triggers) ? exit.triggers : []) |
| 262 | .map((trigger) => normalizeTrigger(trigger, questLookup)) |
| 263 | .filter(Boolean) |
| 264 | }; |
| 265 | } |
| 266 | if (exit.kind === "branches") |
| 267 | { |
| 268 | return { |
| 269 | kind: "branches", |
| 270 | branches: (Array.isArray(exit.branches) ? exit.branches : []) |
| 271 | .map((branch) => normalizeBranch(branch, questLookup)) |
| 272 | .filter(Boolean) |
| 273 | }; |
| 274 | } |
| 275 | if (exit.kind === "continue") |
| 276 | { |
| 277 | const next = exit.nextStepId == null || exit.nextStepId === "" |
| 278 | ? null |
| 279 | : slugId(exit.nextStepId, ""); |
| 280 | return { |
| 281 | kind: "continue", |
| 282 | nextStepId: next || null |
| 283 | }; |
| 284 | } |
| 285 | return { kind: "end" }; |
| 286 | } |
| 287 | if (Array.isArray(legacy?.triggers) && legacy.triggers.length > 0) |
| 288 | { |
| 289 | return { |
| 290 | kind: "triggers", |
| 291 | triggers: legacy.triggers.map((trigger) => normalizeTrigger(trigger, questLookup)).filter(Boolean) |
| 292 | }; |
| 293 | } |
| 294 | if (Array.isArray(legacy?.branches) && legacy.branches.length > 0) |
| 295 | { |
| 296 | return { |
| 297 | kind: "branches", |
| 298 | branches: legacy.branches.map((branch) => normalizeBranch(branch, questLookup)).filter(Boolean) |
| 299 | }; |
| 300 | } |
| 301 | return { kind: "end" }; |
| 302 | } |
| 303 | |
| 304 | function normalizeTrigger (raw, questLookup) |
| 305 | { |
| 306 | if (!raw || typeof raw !== "object") |
| 307 | { return null; } |
| 308 | const trigger = slugId(raw.trigger ?? raw.id, "choice"); |
| 309 | const text = String(raw.text ?? "").trim().slice(0, 120); |
| 310 | if (!text) |
| 311 | { return null; } |
| 312 | const next = raw.nextStepId == null || raw.nextStepId === "" |
| 313 | ? null |
| 314 | : slugId(raw.nextStepId, ""); |
| 315 | return { |
| 316 | trigger, |
| 317 | label: String(raw.label ?? trigger).trim().slice(0, 80) || trigger, |
| 318 | text, |
| 319 | nextStepId: next || null, |
| 320 | when: normalizeConditionSet(raw.when ?? raw.require, { quests: questLookup }), |
| 321 | effects: normalizeEffects(raw.effects) |
| 322 | }; |
| 323 | } |
| 324 | |
| 325 | function normalizeBranch (raw, questLookup) |
| 326 | { |
| 327 | if (!raw || typeof raw !== "object") |
| 328 | { return null; } |
| 329 | const next = raw.nextStepId == null || raw.nextStepId === "" |
| 330 | ? null |
| 331 | : slugId(raw.nextStepId, ""); |
| 332 | return { |
| 333 | label: String(raw.label ?? "").trim().slice(0, 80), |
| 334 | when: normalizeConditionSet(raw.when ?? raw.require, { quests: questLookup }), |
| 335 | nextStepId: next || null, |
| 336 | effects: normalizeEffects(raw.effects) |
| 337 | }; |
| 338 | } |
| 339 | |
| 340 | function slugId (raw, fallback) |
| 341 | { |
| 342 | const s = String(raw ?? "") |
| 343 | .trim() |
| 344 | .toLowerCase() |
| 345 | .replace(/[^a-z0-9_-]+/g, "-") |
| 346 | .replace(/^-+|-+$/g, "") |
| 347 | .slice(0, 64); |
| 348 | return s || fallback; |
| 349 | } |