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