| 1 | /** |
| 2 | * Client-only dialogue tree walk for the world editor. |
| 3 | * Default player look; triggers use the same choice flow; branches are freely selectable. |
| 4 | */ |
| 5 | import { itemDefinition } from "/shared/definitions/items.js"; |
| 6 | import { evaluateConditionSet } from "/shared/quests/conditions.js"; |
| 7 | import { |
| 8 | formatDialogueRewardMessage, |
| 9 | formatDialogueQuestRewardMessage, |
| 10 | formatDialogueTakeMessage |
| 11 | } from "/shared/quests/dialogueRewards.js"; |
| 12 | import { applyDialogueTextReplacements } from "/shared/speech/dialogueText.js"; |
| 13 | |
| 14 | const ICON = 48; |
| 15 | const SIM_IDENTITY = Object.freeze({ |
| 16 | firstName: "Ada", |
| 17 | lastName: "Worker", |
| 18 | sex: "male" |
| 19 | }); |
| 20 | |
| 21 | /** |
| 22 | * @param {{ |
| 23 | * getTree: () => object | null, |
| 24 | * getQuestTitle?: (questId: string) => string |
| 25 | * }} options |
| 26 | */ |
| 27 | export function initDialogueSimulate ({ getTree, getQuestTitle = null } = {}) |
| 28 | { |
| 29 | const backdrop = document.getElementById("dialogue-simulate-backdrop"); |
| 30 | const panel = document.getElementById("dialogue-simulate-panel"); |
| 31 | const titleEl = document.getElementById("dialogue-simulate-title"); |
| 32 | const linesEl = document.getElementById("dialogue-simulate-lines"); |
| 33 | const actionsEl = document.getElementById("dialogue-simulate-actions"); |
| 34 | const closeBtn = document.getElementById("dialogue-simulate-close"); |
| 35 | |
| 36 | /** @type {null | { |
| 37 | * tree: object, |
| 38 | * stepId: string, |
| 39 | * lineIndex: number, |
| 40 | * rewardMessages: string[]|null, |
| 41 | * rewardIndex: number |
| 42 | * }} */ |
| 43 | let session = null; |
| 44 | |
| 45 | function show (visible) |
| 46 | { |
| 47 | if (backdrop) |
| 48 | { backdrop.hidden = !visible; } |
| 49 | if (panel) |
| 50 | { panel.hidden = !visible; } |
| 51 | if (!visible) |
| 52 | { session = null; } |
| 53 | } |
| 54 | |
| 55 | function speakerIcon (speaker) |
| 56 | { |
| 57 | const canvas = document.createElement("canvas"); |
| 58 | canvas.className = "dialogue-simulate-icon"; |
| 59 | canvas.width = ICON; |
| 60 | canvas.height = ICON; |
| 61 | const ctx = canvas.getContext("2d"); |
| 62 | if (speaker === "player") |
| 63 | { |
| 64 | // Default player look stand-in (soft blue body + hair). |
| 65 | ctx.fillStyle = "#5b7ea6"; |
| 66 | ctx.fillRect(8, 14, 32, 28); |
| 67 | ctx.fillStyle = "#c9a06a"; |
| 68 | ctx.fillRect(12, 8, 24, 14); |
| 69 | ctx.fillStyle = "#2a3340"; |
| 70 | ctx.fillRect(12, 6, 24, 8); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | ctx.fillStyle = "#8a6a4a"; |
| 75 | ctx.fillRect(8, 14, 32, 28); |
| 76 | ctx.fillStyle = "#d4b896"; |
| 77 | ctx.fillRect(12, 8, 24, 14); |
| 78 | ctx.fillStyle = "#3a2a1a"; |
| 79 | ctx.fillRect(12, 4, 24, 10); |
| 80 | } |
| 81 | return canvas; |
| 82 | } |
| 83 | |
| 84 | /** Permissive snapshot so authored trigger gates can still be previewed. */ |
| 85 | function simulateCtx () |
| 86 | { |
| 87 | return { |
| 88 | countItem: () => 999, |
| 89 | tileX: 0, |
| 90 | tileY: 0, |
| 91 | chunkX: 0, |
| 92 | chunkY: 0, |
| 93 | dimensionId: "overworld", |
| 94 | hasVisitedDimension: () => true, |
| 95 | questState: () => "completed", |
| 96 | dialogueHeard: () => true, |
| 97 | dialogueTriggerLatched: () => true, |
| 98 | hp: 999, |
| 99 | maxHp: 999, |
| 100 | stamina: 999, |
| 101 | maxStamina: 999, |
| 102 | skillLevel: () => 99, |
| 103 | killCount: () => 999, |
| 104 | interactBlockDone: () => true |
| 105 | }; |
| 106 | } |
| 107 | |
| 108 | function rewardMessagesForStep (step) |
| 109 | { |
| 110 | const messages = []; |
| 111 | const takeParts = []; |
| 112 | for (const take of step?.itemTakes ?? []) |
| 113 | { |
| 114 | const def = itemDefinition(take.itemId); |
| 115 | takeParts.push({ |
| 116 | displayName: def?.displayName ?? `Item ${take.itemId}`, |
| 117 | quantity: Math.max(1, take.quantity | 0 || 1) |
| 118 | }); |
| 119 | } |
| 120 | const takeMsg = formatDialogueTakeMessage(takeParts); |
| 121 | if (takeMsg) |
| 122 | { messages.push(takeMsg); } |
| 123 | const bagParts = []; |
| 124 | for (const reward of step?.itemRewards ?? []) |
| 125 | { |
| 126 | const def = itemDefinition(reward.itemId); |
| 127 | bagParts.push({ |
| 128 | displayName: def?.displayName ?? `Item ${reward.itemId}`, |
| 129 | quantity: Math.max(1, reward.quantity | 0 || 1) |
| 130 | }); |
| 131 | } |
| 132 | const bagMsg = formatDialogueRewardMessage(bagParts, "bag"); |
| 133 | if (bagMsg) |
| 134 | { messages.push(bagMsg); } |
| 135 | const titles = []; |
| 136 | for (const questId of step?.questRewards ?? []) |
| 137 | { |
| 138 | titles.push(getQuestTitle?.(questId) || questId); |
| 139 | } |
| 140 | const questMsg = formatDialogueQuestRewardMessage(titles); |
| 141 | if (questMsg) |
| 142 | { messages.push(questMsg); } |
| 143 | return messages; |
| 144 | } |
| 145 | |
| 146 | function enterStep (stepId) |
| 147 | { |
| 148 | if (!session) |
| 149 | { return; } |
| 150 | const step = session.tree.steps.find((s) => s.stepId === stepId); |
| 151 | if (!step) |
| 152 | { |
| 153 | show(false); |
| 154 | return; |
| 155 | } |
| 156 | session.stepId = stepId; |
| 157 | session.lineIndex = 0; |
| 158 | session.rewardMessages = null; |
| 159 | session.rewardIndex = 0; |
| 160 | render(); |
| 161 | } |
| 162 | |
| 163 | function resolveExit () |
| 164 | { |
| 165 | if (!session) |
| 166 | { return; } |
| 167 | const step = session.tree.steps.find((s) => s.stepId === session.stepId); |
| 168 | const exit = step?.exit ?? { kind: "end" }; |
| 169 | if (exit.kind === "triggers") |
| 170 | { |
| 171 | const ctx = simulateCtx(); |
| 172 | const triggers = (exit.triggers ?? []).filter((t) => evaluateConditionSet(t.when, ctx)); |
| 173 | renderTriggerChoices(triggers); |
| 174 | return; |
| 175 | } |
| 176 | if (exit.kind === "branches") |
| 177 | { |
| 178 | renderBranchChoices(exit.branches ?? []); |
| 179 | return; |
| 180 | } |
| 181 | if (exit.kind === "continue") |
| 182 | { |
| 183 | if (exit.nextStepId) |
| 184 | { enterStep(exit.nextStepId); } |
| 185 | else |
| 186 | { show(false); } |
| 187 | return; |
| 188 | } |
| 189 | show(false); |
| 190 | } |
| 191 | |
| 192 | function afterLines () |
| 193 | { |
| 194 | const step = session.tree.steps.find((s) => s.stepId === session.stepId); |
| 195 | const messages = rewardMessagesForStep(step); |
| 196 | if (messages.length) |
| 197 | { |
| 198 | session.rewardMessages = messages; |
| 199 | session.rewardIndex = 0; |
| 200 | render(); |
| 201 | return; |
| 202 | } |
| 203 | resolveExit(); |
| 204 | } |
| 205 | |
| 206 | function renderTriggerChoices (triggers) |
| 207 | { |
| 208 | if (titleEl) |
| 209 | { titleEl.textContent = "Choices"; } |
| 210 | linesEl?.replaceChildren(); |
| 211 | actionsEl?.replaceChildren(); |
| 212 | if (!triggers.length) |
| 213 | { |
| 214 | const hint = document.createElement("p"); |
| 215 | hint.className = "content-editor-hint"; |
| 216 | hint.textContent = "No triggers matched (simulate uses a permissive player snapshot)."; |
| 217 | linesEl?.append(hint); |
| 218 | const close = document.createElement("button"); |
| 219 | close.type = "button"; |
| 220 | close.textContent = "Close"; |
| 221 | close.addEventListener("click", () => show(false)); |
| 222 | actionsEl?.append(close); |
| 223 | return; |
| 224 | } |
| 225 | for (const trig of triggers) |
| 226 | { |
| 227 | const btn = document.createElement("button"); |
| 228 | btn.type = "button"; |
| 229 | btn.textContent = applyDialogueTextReplacements( |
| 230 | trig.text || trig.trigger || "…", |
| 231 | SIM_IDENTITY |
| 232 | ); |
| 233 | btn.addEventListener("click", () => |
| 234 | { |
| 235 | if (trig.nextStepId) |
| 236 | { enterStep(trig.nextStepId); } |
| 237 | else |
| 238 | { show(false); } |
| 239 | }); |
| 240 | actionsEl?.append(btn); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | function renderBranchChoices (branches) |
| 245 | { |
| 246 | if (titleEl) |
| 247 | { titleEl.textContent = "Branches (pick any)"; } |
| 248 | linesEl?.replaceChildren(); |
| 249 | actionsEl?.replaceChildren(); |
| 250 | const hint = document.createElement("p"); |
| 251 | hint.className = "content-editor-hint"; |
| 252 | hint.textContent = "Simulate ignores branch conditions so you can walk every path."; |
| 253 | linesEl?.append(hint); |
| 254 | if (!branches.length) |
| 255 | { |
| 256 | const close = document.createElement("button"); |
| 257 | close.type = "button"; |
| 258 | close.textContent = "Close"; |
| 259 | close.addEventListener("click", () => show(false)); |
| 260 | actionsEl?.append(close); |
| 261 | return; |
| 262 | } |
| 263 | branches.forEach((branch, index) => |
| 264 | { |
| 265 | const btn = document.createElement("button"); |
| 266 | btn.type = "button"; |
| 267 | btn.textContent = branch.nextStepId |
| 268 | ? `Branch ${index + 1} → ${branch.nextStepId}` |
| 269 | : `Branch ${index + 1} → end`; |
| 270 | btn.addEventListener("click", () => |
| 271 | { |
| 272 | if (branch.nextStepId) |
| 273 | { enterStep(branch.nextStepId); } |
| 274 | else |
| 275 | { show(false); } |
| 276 | }); |
| 277 | actionsEl?.append(btn); |
| 278 | }); |
| 279 | } |
| 280 | |
| 281 | function render () |
| 282 | { |
| 283 | if (!session || !linesEl || !actionsEl) |
| 284 | { return; } |
| 285 | linesEl.replaceChildren(); |
| 286 | actionsEl.replaceChildren(); |
| 287 | |
| 288 | if (session.rewardMessages?.length) |
| 289 | { |
| 290 | if (titleEl) |
| 291 | { titleEl.textContent = "Reward"; } |
| 292 | const row = document.createElement("div"); |
| 293 | row.className = "dialogue-simulate-line dialogue-simulate-reward"; |
| 294 | const text = document.createElement("span"); |
| 295 | text.textContent = session.rewardMessages[session.rewardIndex] ?? ""; |
| 296 | row.append(text); |
| 297 | linesEl.append(row); |
| 298 | const atLast = session.rewardIndex >= session.rewardMessages.length - 1; |
| 299 | const next = document.createElement("button"); |
| 300 | next.type = "button"; |
| 301 | next.textContent = atLast ? "Continue" : "Next"; |
| 302 | next.addEventListener("click", () => |
| 303 | { |
| 304 | if (!atLast) |
| 305 | { |
| 306 | session.rewardIndex += 1; |
| 307 | render(); |
| 308 | return; |
| 309 | } |
| 310 | session.rewardMessages = null; |
| 311 | resolveExit(); |
| 312 | }); |
| 313 | actionsEl.append(next); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | const step = session.tree.steps.find((s) => s.stepId === session.stepId); |
| 318 | const line = step?.lines?.[session.lineIndex]; |
| 319 | const speaker = line?.speaker === "player" ? "player" : "npc"; |
| 320 | if (titleEl) |
| 321 | { titleEl.textContent = speaker === "player" ? "You" : (session.tree.label || "NPC"); } |
| 322 | if (line) |
| 323 | { |
| 324 | const row = document.createElement("div"); |
| 325 | row.className = `dialogue-simulate-line dialogue-simulate-${speaker}`; |
| 326 | row.append(speakerIcon(speaker)); |
| 327 | const text = document.createElement("span"); |
| 328 | text.textContent = applyDialogueTextReplacements(line.text ?? "", SIM_IDENTITY); |
| 329 | row.append(text); |
| 330 | linesEl.append(row); |
| 331 | } |
| 332 | const atLast = session.lineIndex >= (step?.lines?.length ?? 1) - 1; |
| 333 | const next = document.createElement("button"); |
| 334 | next.type = "button"; |
| 335 | next.textContent = atLast ? "Continue" : "Next"; |
| 336 | next.addEventListener("click", () => |
| 337 | { |
| 338 | if (!atLast) |
| 339 | { |
| 340 | session.lineIndex += 1; |
| 341 | render(); |
| 342 | return; |
| 343 | } |
| 344 | afterLines(); |
| 345 | }); |
| 346 | actionsEl.append(next); |
| 347 | } |
| 348 | |
| 349 | function start () |
| 350 | { |
| 351 | const tree = getTree?.(); |
| 352 | if (!tree?.steps?.length) |
| 353 | { return false; } |
| 354 | session = { |
| 355 | tree, |
| 356 | stepId: tree.entryStepId || tree.steps[0].stepId, |
| 357 | lineIndex: 0, |
| 358 | rewardMessages: null, |
| 359 | rewardIndex: 0 |
| 360 | }; |
| 361 | show(true); |
| 362 | enterStep(session.stepId); |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | closeBtn?.addEventListener("click", () => show(false)); |
| 367 | backdrop?.addEventListener("click", () => show(false)); |
| 368 | |
| 369 | return { |
| 370 | start, |
| 371 | close: () => show(false), |
| 372 | isOpen: () => !!session && panel && !panel.hidden |
| 373 | }; |
| 374 | } |