| 1 | import { evaluateConditionSet, EffectKind } from "../../../shared/quests/conditions.js";
|
| 2 | import { countItemInSlots, insertStack, takeMatchingFromSlots, insertStackGrowable } from "../../../shared/items/inventory.js";
|
| 3 | import { itemDefinition } from "../../../shared/definitions/items.js";
|
| 4 | import { applySkillXp } from "../../../shared/skills/state.js";
|
| 5 | import { skillLevel } from "../../../shared/definitions/skills.js";
|
| 6 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
|
| 7 | import { floorDiv } from "../world/coordinates.js";
|
| 8 | import { WorkerMessage } from "../simulation/protocol.js";
|
| 9 |
|
| 10 | /**
|
| 11 | * Quest progress + dialogue sessions (main simulation thread).
|
| 12 | */
|
| 13 | export class QuestRuntime
|
| 14 | {
|
| 15 | constructor ({ stateDb, content, simulation })
|
| 16 | {
|
| 17 | this.stateDb = stateDb;
|
| 18 | this.content = content;
|
| 19 | this.simulation = simulation;
|
| 20 | /** @type {Map<number, { entityId: number, dialogueId: string, stepId: string }>} */
|
| 21 | this.sessions = new Map();
|
| 22 | }
|
| 23 |
|
| 24 | reloadContent ()
|
| 25 | {
|
| 26 | this.content.reload();
|
| 27 | }
|
| 28 |
|
| 29 | closeAllSessions ()
|
| 30 | {
|
| 31 | for (const characterId of [...this.sessions.keys()])
|
| 32 | { this.closeDialogue(characterId, "revision"); }
|
| 33 | }
|
| 34 |
|
| 35 | questSnapshot (characterId)
|
| 36 | {
|
| 37 | const states = this.stateDb.listQuestStates(characterId)
|
| 38 | .filter((row) => row.state !== "not-started");
|
| 39 | const quests = [];
|
| 40 | for (const row of states)
|
| 41 | {
|
| 42 | const def = this.content.quest(row.questId);
|
| 43 | if (!def)
|
| 44 | {
|
| 45 | quests.push({
|
| 46 | questId: row.questId,
|
| 47 | title: "Quest unavailable",
|
| 48 | orphaned: true,
|
| 49 | state: row.state,
|
| 50 | description: "",
|
| 51 | step: null,
|
| 52 | journal: this.stateDb.listQuestJournal(characterId, row.questId).map(journalPublic)
|
| 53 | });
|
| 54 | continue;
|
| 55 | }
|
| 56 | const stepIndex = typeof row.state === "number" ? row.state : null;
|
| 57 | const stepDef = stepIndex != null ? def.steps[stepIndex - 1] : null;
|
| 58 | const completedObjectiveIds = row.progress?.completedObjectiveIds ?? [];
|
| 59 | quests.push({
|
| 60 | questId: def.questId,
|
| 61 | title: def.title,
|
| 62 | orphaned: false,
|
| 63 | state: row.state,
|
| 64 | description: def.description,
|
| 65 | step: stepDef
|
| 66 | ? {
|
| 67 | stepId: stepDef.stepId,
|
| 68 | title: stepDef.title,
|
| 69 | description: stepDef.description,
|
| 70 | objectives: stepDef.objectives.map((obj) => ({
|
| 71 | objectiveId: obj.objectiveId,
|
| 72 | text: obj.text,
|
| 73 | done: completedObjectiveIds.includes(obj.objectiveId)
|
| 74 | }))
|
| 75 | }
|
| 76 | : null,
|
| 77 | journal: this.stateDb.listQuestJournal(characterId, def.questId).map(journalPublic)
|
| 78 | });
|
| 79 | }
|
| 80 | quests.sort((a, b) =>
|
| 81 | {
|
| 82 | const aDone = a.state === "completed" ? 1 : 0;
|
| 83 | const bDone = b.state === "completed" ? 1 : 0;
|
| 84 | if (aDone !== bDone)
|
| 85 | { return aDone - bDone; }
|
| 86 | return String(a.title).localeCompare(String(b.title));
|
| 87 | });
|
| 88 | return { quests };
|
| 89 | }
|
| 90 |
|
| 91 | playerViewFor (characterId)
|
| 92 | {
|
| 93 | const entityId = this.simulation.characterEntities.get(characterId | 0);
|
| 94 | const record = entityId != null ? this.simulation.directory.get(entityId) : null;
|
| 95 | const skills = this.stateDb.getSkills(characterId) ?? {};
|
| 96 | let hp = 1;
|
| 97 | let maxHp = 1;
|
| 98 | let stamina = 1;
|
| 99 | let maxStamina = 1;
|
| 100 | if (entityId != null && record)
|
| 101 | {
|
| 102 | const staminaComp = this.simulation.staminaOf(entityId);
|
| 103 | if (staminaComp)
|
| 104 | {
|
| 105 | stamina = Number(staminaComp.current) || 0;
|
| 106 | maxStamina = Math.max(1, Number(staminaComp.max) || 1);
|
| 107 | }
|
| 108 | const worker = this.simulation.workers[record.workerId];
|
| 109 | const view = worker?.call?.({
|
| 110 | type: WorkerMessage.inspect,
|
| 111 | entityId
|
| 112 | });
|
| 113 | if (view && view.health != null)
|
| 114 | {
|
| 115 | hp = Number(view.health) || 0;
|
| 116 | maxHp = Math.max(1, Number(view.maxHealth) || 1);
|
| 117 | }
|
| 118 | }
|
| 119 | return {
|
| 120 | tileX: record?.tileX | 0,
|
| 121 | tileY: record?.tileY | 0,
|
| 122 | dimensionId: record?.dimensionId ?? "overworld",
|
| 123 | skills,
|
| 124 | hp,
|
| 125 | maxHp,
|
| 126 | stamina,
|
| 127 | maxStamina
|
| 128 | };
|
| 129 | }
|
| 130 |
|
| 131 | buildConditionContext (characterId, playerView, activeQuestId = null)
|
| 132 | {
|
| 133 | const bag = this.#loadBag(characterId);
|
| 134 | const inventory = bag?.inventory ?? [];
|
| 135 | const tileX = playerView?.tileX | 0;
|
| 136 | const tileY = playerView?.tileY | 0;
|
| 137 | const dimensionId = playerView?.dimensionId ?? "overworld";
|
| 138 | const chunkX = floorDiv(tileX, CHUNK_SIZE);
|
| 139 | const chunkY = floorDiv(tileY, CHUNK_SIZE);
|
| 140 | const questRow = activeQuestId ? this.stateDb.getQuestState(characterId, activeQuestId) : null;
|
| 141 | return {
|
| 142 | tileX,
|
| 143 | tileY,
|
| 144 | chunkX,
|
| 145 | chunkY,
|
| 146 | dimensionId,
|
| 147 | hp: playerView?.hp ?? 0,
|
| 148 | maxHp: playerView?.maxHp ?? 1,
|
| 149 | stamina: playerView?.stamina ?? 0,
|
| 150 | maxStamina: playerView?.maxStamina ?? 1,
|
| 151 | countItem: (itemId) => countItemInSlots(inventory, itemId | 0),
|
| 152 | questState: (questId) => this.stateDb.getQuestState(characterId, questId).state,
|
| 153 | dialogueHeard: (dialogueId, stepId) => this.stateDb.isDialogueHeard(characterId, dialogueId, stepId),
|
| 154 | dialogueTriggerLatched: (dialogueId, trigger) =>
|
| 155 | this.stateDb.isDialogueTriggerLatched(characterId, dialogueId, trigger),
|
| 156 | skillLevel: (skillId) => skillLevel(playerView?.skills ?? {}, skillId),
|
| 157 | hasVisitedDimension: (dimId) => this.stateDb.hasVisitedDimension(characterId, dimId),
|
| 158 | killCount: (condition) =>
|
| 159 | {
|
| 160 | if (!questRow)
|
| 161 | { return 0; }
|
| 162 | return questRow.progress?.counters?.[killKey(condition)] | 0;
|
| 163 | },
|
| 164 | interactBlockDone: (condition) =>
|
| 165 | {
|
| 166 | if (!questRow)
|
| 167 | { return false; }
|
| 168 | return !!questRow.progress?.interacts?.[interactKey(condition)];
|
| 169 | }
|
| 170 | };
|
| 171 | }
|
| 172 |
|
| 173 | tryOpenDialogue (characterId, entityId, dialogueId)
|
| 174 | {
|
| 175 | const tree = this.content.dialogue(dialogueId);
|
| 176 | if (!tree)
|
| 177 | { return { ok: false, error: "Nothing to say." }; }
|
| 178 | this.closeDialogue(characterId, "replace");
|
| 179 | const playerView = this.playerViewFor(characterId);
|
| 180 | return this.#enterStep(characterId, entityId, tree, tree.entryStepId, playerView);
|
| 181 | }
|
| 182 |
|
| 183 | chooseDialogue (characterId, triggerId)
|
| 184 | {
|
| 185 | const session = this.sessions.get(characterId | 0);
|
| 186 | if (!session)
|
| 187 | { return { ok: false, error: "No dialogue open." }; }
|
| 188 | if (session.phase === "lines")
|
| 189 | { return { ok: false, error: "Finish reading first." }; }
|
| 190 | const tree = this.content.dialogue(session.dialogueId);
|
| 191 | if (!tree)
|
| 192 | {
|
| 193 | this.closeDialogue(characterId, "missing");
|
| 194 | return { ok: false, error: "Dialogue missing." };
|
| 195 | }
|
| 196 | const step = tree.steps.find((s) => s.stepId === session.stepId);
|
| 197 | if (!step || step.exit?.kind !== "triggers")
|
| 198 | { return { ok: false, error: "Cannot choose now." }; }
|
| 199 | const trigger = step.exit.triggers.find((t) => t.trigger === String(triggerId));
|
| 200 | if (!trigger)
|
| 201 | { return { ok: false, error: "Unknown choice." }; }
|
| 202 | const playerView = this.playerViewFor(characterId);
|
| 203 | const ctx = this.buildConditionContext(characterId, playerView);
|
| 204 | if (!evaluateConditionSet(trigger.when, ctx))
|
| 205 | { return { ok: false, error: "That choice is unavailable." }; }
|
| 206 | this.stateDb.latchDialogueTrigger(characterId, tree.dialogueId, trigger.trigger);
|
| 207 | const events = [];
|
| 208 | this.#applyEffects(characterId, trigger.effects, playerView, events);
|
| 209 | this.#reevaluateQuests(characterId, playerView, events);
|
| 210 | if (!trigger.nextStepId)
|
| 211 | {
|
| 212 | const npcEntityId = session.entityId;
|
| 213 | this.sessions.delete(characterId | 0);
|
| 214 | events.push({ type: "dialogueClose", reason: "end", npcEntityId });
|
| 215 | return { ok: true, events };
|
| 216 | }
|
| 217 | return this.#enterStep(characterId, session.entityId, tree, trigger.nextStepId, playerView, events);
|
| 218 | }
|
| 219 |
|
| 220 | closeDialogue (characterId, reason = "close")
|
| 221 | {
|
| 222 | const id = characterId | 0;
|
| 223 | const session = this.sessions.get(id);
|
| 224 | if (!session)
|
| 225 | { return { events: [] }; }
|
| 226 | this.sessions.delete(id);
|
| 227 | return {
|
| 228 | events: [{
|
| 229 | type: "dialogueClose",
|
| 230 | reason,
|
| 231 | npcEntityId: session.entityId
|
| 232 | }]
|
| 233 | };
|
| 234 | }
|
| 235 |
|
| 236 | interruptDialogueForEntity (entityId)
|
| 237 | {
|
| 238 | const events = [];
|
| 239 | for (const [characterId, session] of [...this.sessions])
|
| 240 | {
|
| 241 | if (session.entityId === (entityId | 0))
|
| 242 | {
|
| 243 | const closed = this.closeDialogue(characterId, "attacked");
|
| 244 | events.push(...closed.events.map((e) => ({ ...e, characterId })));
|
| 245 | }
|
| 246 | }
|
| 247 | return events;
|
| 248 | }
|
| 249 |
|
| 250 | #enterStep (characterId, entityId, tree, stepId, playerView, priorEvents = [])
|
| 251 | {
|
| 252 | const step = tree.steps.find((s) => s.stepId === stepId);
|
| 253 | if (!step)
|
| 254 | {
|
| 255 | this.sessions.delete(characterId | 0);
|
| 256 | priorEvents.push({ type: "dialogueClose", reason: "missing-step" });
|
| 257 | return { ok: false, error: "Broken dialogue.", events: priorEvents };
|
| 258 | }
|
| 259 | this.stateDb.markDialogueHeard(characterId, tree.dialogueId, step.stepId);
|
| 260 | const events = [...priorEvents];
|
| 261 | const linePayloads = [];
|
| 262 | for (const line of step.lines)
|
| 263 | {
|
| 264 | this.#applyEffects(characterId, line.effects, playerView, events);
|
| 265 | linePayloads.push({ speaker: line.speaker, text: line.text });
|
| 266 | }
|
| 267 | // Stay open while the client pages lines; resolve exit only on dialogueAdvance.
|
| 268 | this.sessions.set(characterId | 0, {
|
| 269 | entityId: entityId | 0,
|
| 270 | dialogueId: tree.dialogueId,
|
| 271 | stepId: step.stepId,
|
| 272 | phase: "lines",
|
| 273 | pendingExit: step.exit ?? { kind: "end" }
|
| 274 | });
|
| 275 | events.push({
|
| 276 | type: "dialogueStep",
|
| 277 | dialogueId: tree.dialogueId,
|
| 278 | stepId: step.stepId,
|
| 279 | label: step.label,
|
| 280 | npcEntityId: entityId | 0,
|
| 281 | lines: linePayloads
|
| 282 | });
|
| 283 | this.#reevaluateQuests(characterId, playerView, events);
|
| 284 | return { ok: true, events };
|
| 285 | }
|
| 286 |
|
| 287 | /** Client finished reading all lines of the current step. */
|
| 288 | advanceDialogue (characterId)
|
| 289 | {
|
| 290 | const session = this.sessions.get(characterId | 0);
|
| 291 | if (!session || session.phase !== "lines")
|
| 292 | { return { ok: false, error: "No dialogue lines to advance." }; }
|
| 293 | const tree = this.content.dialogue(session.dialogueId);
|
| 294 | if (!tree)
|
| 295 | {
|
| 296 | this.sessions.delete(characterId | 0);
|
| 297 | return { ok: true, events: [{ type: "dialogueClose", reason: "missing" }] };
|
| 298 | }
|
| 299 | const playerView = this.playerViewFor(characterId);
|
| 300 | const exit = session.pendingExit ?? { kind: "end" };
|
| 301 | const events = [];
|
| 302 |
|
| 303 | if (exit.kind === "branches")
|
| 304 | {
|
| 305 | const ctx = this.buildConditionContext(characterId, playerView);
|
| 306 | let matched = null;
|
| 307 | for (const branch of exit.branches ?? [])
|
| 308 | {
|
| 309 | if (evaluateConditionSet(branch.when, ctx))
|
| 310 | {
|
| 311 | matched = branch;
|
| 312 | break;
|
| 313 | }
|
| 314 | }
|
| 315 | if (!matched || !matched.nextStepId)
|
| 316 | {
|
| 317 | this.#applyEffects(characterId, matched?.effects, playerView, events);
|
| 318 | const npcEntityId = session.entityId;
|
| 319 | this.sessions.delete(characterId | 0);
|
| 320 | events.push({ type: "dialogueClose", reason: "branch-end", npcEntityId });
|
| 321 | return { ok: true, events };
|
| 322 | }
|
| 323 | this.#applyEffects(characterId, matched.effects, playerView, events);
|
| 324 | return this.#enterStep(characterId, session.entityId, tree, matched.nextStepId, playerView, events);
|
| 325 | }
|
| 326 |
|
| 327 | if (exit.kind === "triggers")
|
| 328 | {
|
| 329 | const ctx = this.buildConditionContext(characterId, playerView);
|
| 330 | const triggers = (exit.triggers ?? [])
|
| 331 | .filter((t) => evaluateConditionSet(t.when, ctx))
|
| 332 | .map((t) => ({ trigger: t.trigger, text: t.text }));
|
| 333 | if (triggers.length === 0)
|
| 334 | {
|
| 335 | const npcEntityId = session.entityId;
|
| 336 | this.sessions.delete(characterId | 0);
|
| 337 | events.push({ type: "dialogueClose", reason: "no-triggers", npcEntityId });
|
| 338 | return { ok: true, events };
|
| 339 | }
|
| 340 | session.phase = "choices";
|
| 341 | session.pendingExit = null;
|
| 342 | events.push({
|
| 343 | type: "dialogueChoices",
|
| 344 | dialogueId: session.dialogueId,
|
| 345 | stepId: session.stepId,
|
| 346 | npcEntityId: session.entityId,
|
| 347 | triggers
|
| 348 | });
|
| 349 | return { ok: true, events };
|
| 350 | }
|
| 351 |
|
| 352 | const npcEntityId = session.entityId;
|
| 353 | this.sessions.delete(characterId | 0);
|
| 354 | events.push({ type: "dialogueClose", reason: "end", npcEntityId });
|
| 355 | return { ok: true, events };
|
| 356 | }
|
| 357 |
|
| 358 | #loadBag (characterId)
|
| 359 | {
|
| 360 | const onlineId = this.simulation.characterEntities.get(characterId | 0);
|
| 361 | const bag = onlineId
|
| 362 | ? this.simulation.bagOf(onlineId)
|
| 363 | : this.stateDb.getBag(characterId);
|
| 364 | return bag;
|
| 365 | }
|
| 366 |
|
| 367 | #persistBag (characterId, bag)
|
| 368 | {
|
| 369 | this.stateDb.saveBag(characterId, bag);
|
| 370 | const onlineId = this.simulation.characterEntities.get(characterId | 0);
|
| 371 | if (onlineId)
|
| 372 | {
|
| 373 | this.simulation.applyBag?.(onlineId, bag);
|
| 374 | this.simulation.pushInventory?.(characterId, bag);
|
| 375 | }
|
| 376 | }
|
| 377 |
|
| 378 | #applyEffects (characterId, effects, playerView, events)
|
| 379 | {
|
| 380 | if (!Array.isArray(effects) || effects.length === 0)
|
| 381 | { return; }
|
| 382 | for (const effect of effects)
|
| 383 | {
|
| 384 | if (effect.kind === EffectKind.giveItem)
|
| 385 | {
|
| 386 | const bag = this.#loadBag(characterId);
|
| 387 | if (!bag)
|
| 388 | { continue; }
|
| 389 | const def = itemDefinition(effect.itemId | 0);
|
| 390 | const quantity = Math.max(1, Number(effect.quantity) || 1);
|
| 391 | const result = insertStack(bag.inventory, { itemId: effect.itemId | 0, quantity }, def);
|
| 392 | bag.inventory = result.slots;
|
| 393 | if (result.remainder > 0)
|
| 394 | {
|
| 395 | const vaultResult = insertStackGrowable(
|
| 396 | bag.vault,
|
| 397 | { itemId: effect.itemId | 0, quantity: result.remainder },
|
| 398 | def
|
| 399 | );
|
| 400 | bag.vault = vaultResult.slots;
|
| 401 | }
|
| 402 | this.#persistBag(characterId, bag);
|
| 403 | events.push({ type: "inventoryDirty" });
|
| 404 | }
|
| 405 | else if (effect.kind === EffectKind.takeItem)
|
| 406 | {
|
| 407 | const bag = this.#loadBag(characterId);
|
| 408 | if (!bag)
|
| 409 | { continue; }
|
| 410 | const taken = takeMatchingFromSlots(bag.inventory, {
|
| 411 | itemId: effect.itemId | 0,
|
| 412 | quantity: Math.max(1, Number(effect.quantity) || 1),
|
| 413 | compact: false
|
| 414 | });
|
| 415 | if (taken.ok)
|
| 416 | {
|
| 417 | bag.inventory = taken.slots;
|
| 418 | this.#persistBag(characterId, bag);
|
| 419 | events.push({ type: "inventoryDirty" });
|
| 420 | }
|
| 421 | }
|
| 422 | else if (effect.kind === EffectKind.giveXp)
|
| 423 | {
|
| 424 | const skills = this.stateDb.getSkills(characterId);
|
| 425 | if (skills)
|
| 426 | {
|
| 427 | applySkillXp(skills, effect.skillId, Math.max(0, Number(effect.amount) || 0));
|
| 428 | this.stateDb.saveSkills(characterId, skills);
|
| 429 | if (playerView)
|
| 430 | { playerView.skills = skills; }
|
| 431 | events.push({ type: "skillsDirty" });
|
| 432 | }
|
| 433 | }
|
| 434 | else if (effect.kind === EffectKind.startQuest)
|
| 435 | { this.startQuest(characterId, effect.questId, playerView, events); }
|
| 436 | }
|
| 437 | }
|
| 438 |
|
| 439 | startQuest (characterId, questId, playerView, events = [])
|
| 440 | {
|
| 441 | const def = this.content.quest(questId);
|
| 442 | if (!def)
|
| 443 | { return; }
|
| 444 | const current = this.stateDb.getQuestState(characterId, questId);
|
| 445 | if (current.state !== "not-started")
|
| 446 | { return; }
|
| 447 | this.stateDb.saveQuestState(characterId, questId, 1, {
|
| 448 | completedObjectiveIds: [],
|
| 449 | counters: {},
|
| 450 | interacts: {}
|
| 451 | }, Date.now(), null);
|
| 452 | this.#applyEffects(characterId, def.steps[0]?.onEnter, playerView, events);
|
| 453 | events.push({ type: "questsDirty" });
|
| 454 | this.#reevaluateQuests(characterId, playerView, events);
|
| 455 | }
|
| 456 |
|
| 457 | #reevaluateQuests (characterId, playerView, events)
|
| 458 | {
|
| 459 | const states = this.stateDb.listQuestStates(characterId)
|
| 460 | .filter((row) => typeof row.state === "number");
|
| 461 | for (const row of states)
|
| 462 | {
|
| 463 | const def = this.content.quest(row.questId);
|
| 464 | if (!def)
|
| 465 | { continue; }
|
| 466 | const stepDef = def.steps[row.state - 1];
|
| 467 | if (!stepDef)
|
| 468 | { continue; }
|
| 469 | const ctx = this.buildConditionContext(characterId, playerView, row.questId);
|
| 470 | const completed = new Set(row.progress?.completedObjectiveIds ?? []);
|
| 471 | let changed = false;
|
| 472 | for (const obj of stepDef.objectives)
|
| 473 | {
|
| 474 | if (completed.has(obj.objectiveId))
|
| 475 | { continue; }
|
| 476 | if (evaluateConditionSet(obj.when, ctx))
|
| 477 | {
|
| 478 | completed.add(obj.objectiveId);
|
| 479 | changed = true;
|
| 480 | }
|
| 481 | }
|
| 482 | if (!changed)
|
| 483 | { continue; }
|
| 484 | const progress = {
|
| 485 | ...row.progress,
|
| 486 | completedObjectiveIds: [...completed]
|
| 487 | };
|
| 488 | const allDone = stepDef.objectives.every((obj) => completed.has(obj.objectiveId));
|
| 489 | if (!allDone)
|
| 490 | {
|
| 491 | this.stateDb.saveQuestState(characterId, row.questId, row.state, progress, row.startedAt, row.completedAt);
|
| 492 | events.push({ type: "questsDirty" });
|
| 493 | continue;
|
| 494 | }
|
| 495 | this.#applyEffects(characterId, stepDef.onComplete, playerView, events);
|
| 496 | const journal = this.stateDb.appendQuestJournal(
|
| 497 | characterId,
|
| 498 | row.questId,
|
| 499 | stepDef.stepId,
|
| 500 | stepDef.completionJournalText
|
| 501 | );
|
| 502 | const nextIndex = row.state + 1;
|
| 503 | if (nextIndex > def.steps.length)
|
| 504 | {
|
| 505 | this.#applyEffects(characterId, def.rewards, playerView, events);
|
| 506 | this.stateDb.saveQuestState(characterId, row.questId, "completed", progress, row.startedAt, Date.now());
|
| 507 | }
|
| 508 | else
|
| 509 | {
|
| 510 | const nextStep = def.steps[nextIndex - 1];
|
| 511 | this.stateDb.saveQuestState(characterId, row.questId, nextIndex, {
|
| 512 | completedObjectiveIds: [],
|
| 513 | counters: {},
|
| 514 | interacts: {}
|
| 515 | }, row.startedAt, null);
|
| 516 | this.#applyEffects(characterId, nextStep?.onEnter, playerView, events);
|
| 517 | }
|
| 518 | events.push({
|
| 519 | type: "questStepComplete",
|
| 520 | questId: row.questId,
|
| 521 | title: def.title,
|
| 522 | journalText: journal?.text ?? null
|
| 523 | });
|
| 524 | events.push({ type: "questsDirty" });
|
| 525 | }
|
| 526 | }
|
| 527 |
|
| 528 | noteKill (characterId, { mobTypeId, spawnerId })
|
| 529 | {
|
| 530 | const events = [];
|
| 531 | const playerView = this.playerViewFor(characterId);
|
| 532 | const states = this.stateDb.listQuestStates(characterId)
|
| 533 | .filter((row) => typeof row.state === "number");
|
| 534 | for (const row of states)
|
| 535 | {
|
| 536 | const def = this.content.quest(row.questId);
|
| 537 | const stepDef = def?.steps[row.state - 1];
|
| 538 | if (!stepDef)
|
| 539 | { continue; }
|
| 540 | let changed = false;
|
| 541 | const counters = { ...(row.progress?.counters ?? {}) };
|
| 542 | for (const obj of stepDef.objectives)
|
| 543 | {
|
| 544 | for (const clause of obj.when?.all ?? [])
|
| 545 | {
|
| 546 | const c = clause.condition;
|
| 547 | if (c?.kind !== "killCount")
|
| 548 | { continue; }
|
| 549 | const matchMob = c.mobTypeId != null && String(c.mobTypeId) === String(mobTypeId);
|
| 550 | const matchSpawner = c.spawnerId != null && String(c.spawnerId) === String(spawnerId);
|
| 551 | if (!matchMob && !matchSpawner)
|
| 552 | { continue; }
|
| 553 | const key = killKey(c);
|
| 554 | counters[key] = (counters[key] | 0) + 1;
|
| 555 | changed = true;
|
| 556 | }
|
| 557 | }
|
| 558 | if (!changed)
|
| 559 | { continue; }
|
| 560 | this.stateDb.saveQuestState(characterId, row.questId, row.state, {
|
| 561 | ...row.progress,
|
| 562 | counters
|
| 563 | }, row.startedAt, row.completedAt);
|
| 564 | }
|
| 565 | this.#reevaluateQuests(characterId, playerView, events);
|
| 566 | return events;
|
| 567 | }
|
| 568 | }
|
| 569 |
|
| 570 | function journalPublic (row)
|
| 571 | {
|
| 572 | return {
|
| 573 | entryId: row.entry_id ?? row.entryId,
|
| 574 | questId: row.quest_id ?? row.questId,
|
| 575 | stepId: row.step_id ?? row.stepId,
|
| 576 | text: row.text,
|
| 577 | writtenAt: row.written_at ?? row.writtenAt
|
| 578 | };
|
| 579 | }
|
| 580 |
|
| 581 | function killKey (condition)
|
| 582 | {
|
| 583 | if (condition.spawnerId)
|
| 584 | { return `spawner:${condition.spawnerId}`; }
|
| 585 | return `mob:${condition.mobTypeId}`;
|
| 586 | }
|
| 587 |
|
| 588 | function interactKey (condition)
|
| 589 | {
|
| 590 | if (condition.tileX != null)
|
| 591 | { return `tile:${condition.tileX},${condition.tileY}`; }
|
| 592 | return `block:${condition.blockTypeId}`;
|
| 593 | }
|