| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { normalizeQuestDef } from "../../shared/quests/normalize.js"; |
| 4 | import { ConditionKind } from "../../shared/quests/conditions.js"; |
| 5 | import { normalizeDoorConfig } from "../../shared/world/doorRequirements.js"; |
| 6 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 7 | import { ItemId } from "../../shared/definitions/items.js"; |
| 8 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 9 | import { emptyEquipment, emptySlots, INVENTORY_SLOTS } from "../../shared/items/inventory.js"; |
| 10 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 11 | |
| 12 | function saveLiveQuest (harness, raw) |
| 13 | { |
| 14 | const revisionId = harness.worldDb.getActiveRevisionId(); |
| 15 | const quest = normalizeQuestDef(raw); |
| 16 | harness.worldDb.saveQuestDef(revisionId, quest); |
| 17 | harness.simulation.questContent.reload(); |
| 18 | return quest; |
| 19 | } |
| 20 | |
| 21 | function stuckObjective (itemId = 99999) |
| 22 | { |
| 23 | return [{ |
| 24 | objectiveId: "stay", |
| 25 | text: "Stay", |
| 26 | when: { all: [{ condition: { kind: "hasItem", itemId, quantity: 1 } }] } |
| 27 | }]; |
| 28 | } |
| 29 | |
| 30 | function openObjective () |
| 31 | { |
| 32 | return [{ objectiveId: "go", text: "Go", when: { all: [] } }]; |
| 33 | } |
| 34 | |
| 35 | function workerOf (simulation, entityId) |
| 36 | { |
| 37 | const record = simulation.entityRecord(entityId); |
| 38 | assert.ok(record); |
| 39 | return simulation.workers[record.workerId]; |
| 40 | } |
| 41 | |
| 42 | function placeDoor (worker, tileX, tileY, when) |
| 43 | { |
| 44 | const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); |
| 45 | const localX = ((tileX % 16) + 16) % 16; |
| 46 | const localY = ((tileY % 16) + 16) % 16; |
| 47 | data.blocks[localY * 16 + localX] = { |
| 48 | typeId: BlockType.door, |
| 49 | state: "oak-n-closed", |
| 50 | config: normalizeDoorConfig({ when, blockedMessage: "Not yet." }) |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | test("linear quests still advance one step per evaluation and reward once", () => |
| 55 | { |
| 56 | const harness = makeSimulation({ workerCount: 1 }); |
| 57 | try |
| 58 | { |
| 59 | const quest = saveLiveQuest(harness, { |
| 60 | questId: "linear-chain", |
| 61 | title: "Linear", |
| 62 | steps: [ |
| 63 | { stepId: "s1", title: "One", objectives: openObjective(), completionJournalText: "One done." }, |
| 64 | { stepId: "s2", title: "Two", objectives: openObjective(), completionJournalText: "Two done." }, |
| 65 | { stepId: "s3", title: "Three", objectives: openObjective(), completionJournalText: "Three done." } |
| 66 | ], |
| 67 | rewards: [{ kind: "giveXp", skillId: SkillId.woodworking, amount: 11 }] |
| 68 | }); |
| 69 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 70 | harness.simulation.spawnCharacter(1, character); |
| 71 | harness.simulation.step(); |
| 72 | const view = harness.simulation.quests.playerViewFor(character.character_id); |
| 73 | const events = []; |
| 74 | harness.simulation.quests.startQuest(character.character_id, quest.questId, view, events); |
| 75 | assert.equal(events.some((event) => event.type === "questComplete"), false); |
| 76 | let state = harness.stateDb.getQuestState(character.character_id, quest.questId); |
| 77 | assert.equal(state.state, "s2"); |
| 78 | assert.deepEqual(state.progress.completedStepIds, ["s1"]); |
| 79 | |
| 80 | harness.simulation.quests.reevaluateQuests(character.character_id, view, []); |
| 81 | state = harness.stateDb.getQuestState(character.character_id, quest.questId); |
| 82 | assert.equal(state.state, "s3"); |
| 83 | assert.deepEqual(state.progress.completedStepIds, ["s1", "s2"]); |
| 84 | assert.equal(harness.stateDb.getSkills(character.character_id).xp.woodworking, 0); |
| 85 | |
| 86 | harness.simulation.quests.reevaluateQuests(character.character_id, view, []); |
| 87 | state = harness.stateDb.getQuestState(character.character_id, quest.questId); |
| 88 | assert.equal(state.state, "completed"); |
| 89 | assert.deepEqual(state.progress.completedStepIds, ["s1", "s2", "s3"]); |
| 90 | assert.equal(harness.stateDb.getSkills(character.character_id).xp.woodworking, 11); |
| 91 | |
| 92 | harness.simulation.quests.reevaluateQuests(character.character_id, view, []); |
| 93 | assert.equal(harness.stateDb.getSkills(character.character_id).xp.woodworking, 11); |
| 94 | assert.equal( |
| 95 | harness.stateDb.getQuestState(character.character_id, quest.questId).state, |
| 96 | "completed" |
| 97 | ); |
| 98 | } |
| 99 | finally |
| 100 | { harness.close(); } |
| 101 | }); |
| 102 | |
| 103 | test("branch exit takes the first matching condition set only", () => |
| 104 | { |
| 105 | const harness = makeSimulation({ workerCount: 1 }); |
| 106 | try |
| 107 | { |
| 108 | const quest = saveLiveQuest(harness, { |
| 109 | questId: "branch-first", |
| 110 | title: "Branch", |
| 111 | steps: [{ |
| 112 | stepId: "hub", |
| 113 | title: "Hub", |
| 114 | exit: { |
| 115 | kind: "branches", |
| 116 | branches: [ |
| 117 | { |
| 118 | label: "Bones", |
| 119 | text: "Bring bones", |
| 120 | when: { all: [{ condition: { kind: "hasItem", itemId: ItemId.bones, quantity: 1 } }] }, |
| 121 | nextStepId: "bones-path" |
| 122 | }, |
| 123 | { |
| 124 | label: "Other", |
| 125 | text: "Otherwise", |
| 126 | when: { all: [] }, |
| 127 | nextStepId: "other-path" |
| 128 | } |
| 129 | ] |
| 130 | } |
| 131 | }, { |
| 132 | stepId: "bones-path", |
| 133 | title: "Bones", |
| 134 | objectives: stuckObjective() |
| 135 | }, { |
| 136 | stepId: "other-path", |
| 137 | title: "Other", |
| 138 | objectives: stuckObjective() |
| 139 | }] |
| 140 | }); |
| 141 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 142 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 143 | harness.simulation.step(); |
| 144 | const inventory = emptySlots(INVENTORY_SLOTS); |
| 145 | inventory[0] = { itemId: ItemId.bones, quantity: 1 }; |
| 146 | harness.simulation.applyBag(playerId, { |
| 147 | inventory, |
| 148 | equipment: emptyEquipment(), |
| 149 | vault: [], |
| 150 | vaultReserved: [] |
| 151 | }); |
| 152 | const view = harness.simulation.quests.playerViewFor(character.character_id); |
| 153 | harness.simulation.quests.startQuest(character.character_id, quest.questId, view, []); |
| 154 | const state = harness.stateDb.getQuestState(character.character_id, quest.questId); |
| 155 | assert.equal(state.state, "bones-path"); |
| 156 | assert.deepEqual(state.progress.completedStepIds, ["hub"]); |
| 157 | const snap = harness.simulation.quests.questSnapshot(character.character_id); |
| 158 | assert.equal(snap.quests[0].step.stepId, "bones-path"); |
| 159 | } |
| 160 | finally |
| 161 | { harness.close(); } |
| 162 | }); |
| 163 | |
| 164 | test("unmatched branch step shows branch texts with kill progress", () => |
| 165 | { |
| 166 | const harness = makeSimulation({ workerCount: 1 }); |
| 167 | try |
| 168 | { |
| 169 | const quest = saveLiveQuest(harness, { |
| 170 | questId: "branch-log", |
| 171 | title: "Log", |
| 172 | steps: [{ |
| 173 | stepId: "choice", |
| 174 | title: "Choose", |
| 175 | exit: { |
| 176 | kind: "branches", |
| 177 | branches: [ |
| 178 | { |
| 179 | text: "Kill rats", |
| 180 | when: { all: [{ condition: { kind: "killCount", mobTypeId: "rat", count: 3 } }] }, |
| 181 | nextStepId: "done" |
| 182 | }, |
| 183 | { |
| 184 | text: "Bring bones", |
| 185 | when: { all: [{ condition: { kind: "hasItem", itemId: ItemId.bones, quantity: 2 } }] }, |
| 186 | nextStepId: "done" |
| 187 | } |
| 188 | ] |
| 189 | } |
| 190 | }, { |
| 191 | stepId: "done", |
| 192 | title: "Done", |
| 193 | objectives: stuckObjective() |
| 194 | }] |
| 195 | }); |
| 196 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 197 | harness.simulation.spawnCharacter(1, character); |
| 198 | harness.simulation.step(); |
| 199 | const view = harness.simulation.quests.playerViewFor(character.character_id); |
| 200 | harness.simulation.quests.startQuest(character.character_id, quest.questId, view, []); |
| 201 | let snap = harness.simulation.quests.questSnapshot(character.character_id); |
| 202 | assert.deepEqual( |
| 203 | snap.quests[0].step.objectives.map((line) => line.text), |
| 204 | ["Kill rats", "Bring bones"] |
| 205 | ); |
| 206 | assert.equal(snap.quests[0].step.objectives[0].current, 0); |
| 207 | assert.equal(snap.quests[0].step.objectives[0].required, 3); |
| 208 | |
| 209 | harness.simulation.quests.noteKill(character.character_id, { mobTypeId: "rat", spawnerId: null }); |
| 210 | snap = harness.simulation.quests.questSnapshot(character.character_id); |
| 211 | assert.equal(snap.quests[0].step.stepId, "choice"); |
| 212 | assert.equal(snap.quests[0].step.objectives[0].current, 1); |
| 213 | assert.equal( |
| 214 | harness.stateDb.getQuestState(character.character_id, quest.questId).state, |
| 215 | "choice" |
| 216 | ); |
| 217 | } |
| 218 | finally |
| 219 | { harness.close(); } |
| 220 | }); |
| 221 | |
| 222 | test("noteKill counts only the active step resolved by step id", () => |
| 223 | { |
| 224 | const harness = makeSimulation({ workerCount: 1 }); |
| 225 | try |
| 226 | { |
| 227 | const quest = saveLiveQuest(harness, { |
| 228 | questId: "kill-which", |
| 229 | title: "Which", |
| 230 | steps: [{ |
| 231 | stepId: "first", |
| 232 | title: "Deer", |
| 233 | objectives: [{ |
| 234 | objectiveId: "deer", |
| 235 | text: "Deer", |
| 236 | when: { all: [{ condition: { kind: "killCount", mobTypeId: "deer", count: 1 } }] } |
| 237 | }] |
| 238 | }, { |
| 239 | stepId: "second", |
| 240 | title: "Rats", |
| 241 | objectives: [{ |
| 242 | objectiveId: "rats", |
| 243 | text: "Rats", |
| 244 | when: { all: [{ condition: { kind: "killCount", mobTypeId: "rat", count: 2 } }] } |
| 245 | }] |
| 246 | }] |
| 247 | }); |
| 248 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 249 | harness.simulation.spawnCharacter(1, character); |
| 250 | harness.simulation.step(); |
| 251 | harness.stateDb.saveQuestState(character.character_id, quest.questId, "second", { |
| 252 | completedObjectiveIds: [], |
| 253 | counters: {}, |
| 254 | completedStepIds: ["first"] |
| 255 | }, Date.now(), null); |
| 256 | harness.simulation.quests.noteKill(character.character_id, { mobTypeId: "deer", spawnerId: null }); |
| 257 | harness.simulation.quests.noteKill(character.character_id, { mobTypeId: "rat", spawnerId: null }); |
| 258 | const state = harness.stateDb.getQuestState(character.character_id, quest.questId); |
| 259 | assert.equal(state.state, "second"); |
| 260 | assert.equal(state.progress.counters?.["mob:deer"], undefined); |
| 261 | assert.equal(state.progress.counters?.["mob:rat"], 1); |
| 262 | assert.deepEqual(state.progress.completedStepIds, ["first"]); |
| 263 | } |
| 264 | finally |
| 265 | { harness.close(); } |
| 266 | }); |
| 267 | |
| 268 | test("completed step ids survive a loop back onto the same step", () => |
| 269 | { |
| 270 | const harness = makeSimulation({ workerCount: 1 }); |
| 271 | try |
| 272 | { |
| 273 | const quest = saveLiveQuest(harness, { |
| 274 | questId: "loop-q", |
| 275 | title: "Loop", |
| 276 | steps: [{ |
| 277 | stepId: "alpha", |
| 278 | title: "Alpha", |
| 279 | exit: { |
| 280 | kind: "branches", |
| 281 | branches: [{ text: "To beta", when: { all: [] }, nextStepId: "beta" }] |
| 282 | } |
| 283 | }, { |
| 284 | stepId: "beta", |
| 285 | title: "Beta", |
| 286 | exit: { |
| 287 | kind: "branches", |
| 288 | branches: [{ text: "To alpha", when: { all: [] }, nextStepId: "alpha" }] |
| 289 | } |
| 290 | }] |
| 291 | }); |
| 292 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 293 | harness.simulation.spawnCharacter(1, character); |
| 294 | harness.simulation.step(); |
| 295 | const view = harness.simulation.quests.playerViewFor(character.character_id); |
| 296 | harness.simulation.quests.startQuest(character.character_id, quest.questId, view, []); |
| 297 | let state = harness.stateDb.getQuestState(character.character_id, quest.questId); |
| 298 | assert.equal(state.state, "beta"); |
| 299 | assert.deepEqual(state.progress.completedStepIds, ["alpha"]); |
| 300 | |
| 301 | harness.simulation.quests.reevaluateQuests(character.character_id, view, []); |
| 302 | state = harness.stateDb.getQuestState(character.character_id, quest.questId); |
| 303 | assert.equal(state.state, "alpha"); |
| 304 | assert.deepEqual(state.progress.completedStepIds, ["alpha", "beta"]); |
| 305 | |
| 306 | harness.simulation.quests.reevaluateQuests(character.character_id, view, []); |
| 307 | state = harness.stateDb.getQuestState(character.character_id, quest.questId); |
| 308 | assert.equal(state.state, "beta"); |
| 309 | assert.deepEqual(state.progress.completedStepIds, ["alpha", "beta"]); |
| 310 | assert.equal(state.state === "completed", false); |
| 311 | } |
| 312 | finally |
| 313 | { harness.close(); } |
| 314 | }); |
| 315 | |
| 316 | test("legacy numeric quest index resolves the step and unlocks doors", () => |
| 317 | { |
| 318 | const harness = makeSimulation({ workerCount: 1 }); |
| 319 | try |
| 320 | { |
| 321 | const quest = saveLiveQuest(harness, { |
| 322 | questId: "door-steps", |
| 323 | title: "Door Steps", |
| 324 | steps: [{ |
| 325 | stepId: "gate-a", |
| 326 | title: "First", |
| 327 | objectives: stuckObjective(99991) |
| 328 | }, { |
| 329 | stepId: "gate-b", |
| 330 | title: "Second", |
| 331 | objectives: stuckObjective(99992) |
| 332 | }] |
| 333 | }); |
| 334 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 335 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 336 | harness.simulation.step(); |
| 337 | const worker = workerOf(harness.simulation, entityId); |
| 338 | placeDoor(worker, 9, 8, { |
| 339 | all: [{ condition: { kind: ConditionKind.questStepAtLeast, questId: quest.questId, step: 2 } }] |
| 340 | }); |
| 341 | assert.equal(worker.world.walkableFor(entityId, 9, 8), false); |
| 342 | |
| 343 | harness.stateDb.saveQuestState(character.character_id, quest.questId, 2, {}, Date.now(), null); |
| 344 | harness.simulation.syncCharacterConditionAccess(character.character_id); |
| 345 | const snap = harness.simulation.quests.questSnapshot(character.character_id); |
| 346 | assert.equal(snap.quests[0].step.stepId, "gate-b"); |
| 347 | assert.equal(worker.world.walkableFor(entityId, 9, 8), true, "at least step 2 means gate-a was completed"); |
| 348 | |
| 349 | placeDoor(worker, 9, 8, { |
| 350 | all: [{ |
| 351 | condition: { |
| 352 | kind: ConditionKind.hasCompletedStep, |
| 353 | questId: quest.questId, |
| 354 | stepId: "gate-b" |
| 355 | } |
| 356 | }] |
| 357 | }); |
| 358 | assert.equal(worker.world.walkableFor(entityId, 9, 8), false); |
| 359 | |
| 360 | placeDoor(worker, 9, 8, { |
| 361 | all: [{ |
| 362 | condition: { kind: ConditionKind.questState, questId: quest.questId, state: "gate-b" } |
| 363 | }] |
| 364 | }); |
| 365 | assert.equal(worker.world.walkableFor(entityId, 9, 8), true); |
| 366 | |
| 367 | placeDoor(worker, 9, 8, { |
| 368 | all: [{ |
| 369 | condition: { kind: ConditionKind.questState, questId: quest.questId, state: "gate-a" } |
| 370 | }] |
| 371 | }); |
| 372 | assert.equal(worker.world.walkableFor(entityId, 9, 8), false); |
| 373 | } |
| 374 | finally |
| 375 | { harness.close(); } |
| 376 | }); |