| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { mkdtempSync } from "node:fs"; |
| 4 | import { tmpdir } from "node:os"; |
| 5 | import { join } from "node:path"; |
| 6 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 7 | import { CraftingStationId } from "../../shared/definitions/craftingStations.js"; |
| 8 | import { MobId } from "../../shared/definitions/mobs.js"; |
| 9 | import { OVERWORLD_ID, DimensionLightingMode, DimensionSeasonMode } from "../../shared/world/dimensions.js"; |
| 10 | import { WorldDatabase } from "../src/persistence/worldDb.js"; |
| 11 | import { StateDatabase } from "../src/persistence/stateDb.js"; |
| 12 | import { Simulation } from "../src/simulation/simulation.js"; |
| 13 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 14 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 15 | import { engageTarget, ensureMobCombat, stepCombat, stepAggression, applyDamageAndKill } from "../src/combat/service.js"; |
| 16 | import { Component } from "../src/ecs/components.js"; |
| 17 | import { eventInterestKeys } from "../src/gateway/eventInterest.js"; |
| 18 | import { chunkKey, tileToChunk } from "../src/world/coordinates.js"; |
| 19 | import { makeCharacter, openChunkStrip, writeOpenChunk } from "./helpers/simulationHarness.js"; |
| 20 | import { normalizeQuestDef } from "../../shared/quests/normalize.js"; |
| 21 | |
| 22 | const CAVE_ID = "test-cave"; |
| 23 | |
| 24 | function makeCaveHarness () |
| 25 | { |
| 26 | const dir = mkdtempSync(join(tmpdir(), "mmo-dim-interact-")); |
| 27 | const worldDb = new WorldDatabase(join(dir, "world.sqlite")); |
| 28 | const stateDb = new StateDatabase(join(dir, "state.sqlite")); |
| 29 | openChunkStrip(worldDb, 2); |
| 30 | const rev = worldDb.getActiveRevisionId(); |
| 31 | worldDb.saveDimension(rev, { |
| 32 | dimensionId: CAVE_ID, |
| 33 | displayName: "Test Cave", |
| 34 | lightingMode: DimensionLightingMode.alwaysLit, |
| 35 | seasonMode: DimensionSeasonMode.summer, |
| 36 | defaultGroundTypeId: 1 |
| 37 | }); |
| 38 | const cave = createEmptyChunkData(); |
| 39 | cave.open = true; |
| 40 | cave.blocks[8 * 16 + 9] = { |
| 41 | typeId: BlockType.furnace, |
| 42 | state: "furnace", |
| 43 | config: null |
| 44 | }; |
| 45 | worldDb.saveChunkVersion(0, 0, rev, true, encodeChunkBlob(cave), CAVE_ID); |
| 46 | writeOpenChunk(worldDb, 0, 0); |
| 47 | let now = 1_000; |
| 48 | const simulation = new Simulation(worldDb, stateDb, { |
| 49 | workerCount: 1, |
| 50 | workerMode: "inprocess", |
| 51 | now: () => now |
| 52 | }); |
| 53 | return { |
| 54 | simulation, |
| 55 | worldDb, |
| 56 | stateDb, |
| 57 | now () |
| 58 | { |
| 59 | return now; |
| 60 | }, |
| 61 | advance (ms) |
| 62 | { |
| 63 | now += ms; |
| 64 | }, |
| 65 | close () |
| 66 | { |
| 67 | simulation.forceUnloadAll(); |
| 68 | worldDb.close(); |
| 69 | stateDb.close(); |
| 70 | } |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | function workerOf (simulation, entityId) |
| 75 | { |
| 76 | const record = simulation.entityRecord(entityId); |
| 77 | assert.ok(record, "entity record"); |
| 78 | return simulation.workers[record.workerId]; |
| 79 | } |
| 80 | |
| 81 | test("open furnace works in a non-overworld dimension", () => |
| 82 | { |
| 83 | const harness = makeCaveHarness(); |
| 84 | try |
| 85 | { |
| 86 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 87 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 88 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 89 | const position = harness.simulation.positionOf(playerId); |
| 90 | assert.equal(position.dimensionId, CAVE_ID); |
| 91 | |
| 92 | harness.simulation.enqueue(1, { |
| 93 | type: "open-station", |
| 94 | characterId: character.character_id, |
| 95 | stationId: CraftingStationId.furnace |
| 96 | }); |
| 97 | const result = harness.simulation.step(); |
| 98 | assert.ok( |
| 99 | result.events.some((event) => |
| 100 | event.type === "station-open" && event.stationId === CraftingStationId.furnace), |
| 101 | "expected station-open in cave" |
| 102 | ); |
| 103 | } |
| 104 | finally |
| 105 | { harness.close(); } |
| 106 | }); |
| 107 | |
| 108 | test("melee combat ticks in a non-overworld dimension", () => |
| 109 | { |
| 110 | const harness = makeCaveHarness(); |
| 111 | try |
| 112 | { |
| 113 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 114 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 115 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 116 | assert.equal(harness.simulation.positionOf(playerId).dimensionId, CAVE_ID); |
| 117 | |
| 118 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 119 | tileX: 8, |
| 120 | tileY: 9, |
| 121 | mobTypeId: MobId.rat, |
| 122 | spawnerId: null, |
| 123 | dimensionId: CAVE_ID |
| 124 | })); |
| 125 | assert.equal(harness.simulation.positionOf(ratId).dimensionId, CAVE_ID); |
| 126 | |
| 127 | const worker = workerOf(harness.simulation, ratId); |
| 128 | const world = worker.world; |
| 129 | assert.ok(world.chunkIsSimulating(8, 9, CAVE_ID)); |
| 130 | assert.equal(world.chunkIsSimulating(8, 9, OVERWORLD_ID), false); |
| 131 | |
| 132 | assert.ok(ensureMobCombat(world.ecs, ratId)); |
| 133 | engageTarget(world.ecs, ratId, playerId, harness.now()); |
| 134 | const combat = world.ecs.get(world.ecs.handleOf(ratId), Component.combat); |
| 135 | combat.nextSwingAt = harness.now(); |
| 136 | |
| 137 | const events = stepCombat(world, harness.now()); |
| 138 | const combatFx = events.find((event) => |
| 139 | event.type === "combat-swing" || event.type === "combat-hit" |
| 140 | || event.type === "combat-miss" || event.type === "combat-block" |
| 141 | ); |
| 142 | assert.ok( |
| 143 | combatFx, |
| 144 | `expected combat resolution in cave, got ${events.map((e) => e.type).join(",") || "none"}` |
| 145 | ); |
| 146 | assert.equal(combatFx.dimensionId, CAVE_ID); |
| 147 | |
| 148 | stepAggression(world, harness.now()); |
| 149 | } |
| 150 | finally |
| 151 | { harness.close(); } |
| 152 | }); |
| 153 | |
| 154 | test("mob kill entity-despawn carries cave dimensionId for interest", () => |
| 155 | { |
| 156 | const harness = makeCaveHarness(); |
| 157 | try |
| 158 | { |
| 159 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 160 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 161 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 162 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 163 | tileX: 8, |
| 164 | tileY: 9, |
| 165 | mobTypeId: MobId.rat, |
| 166 | spawnerId: null, |
| 167 | dimensionId: CAVE_ID |
| 168 | })); |
| 169 | const worker = workerOf(harness.simulation, ratId); |
| 170 | const handle = worker.world.ecs.handleOf(ratId); |
| 171 | worker.world.ecs.get(handle, Component.health).current = 1; |
| 172 | const events = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now()); |
| 173 | const despawn = events.find((event) => |
| 174 | event.type === "entity-despawn" && event.entityId === ratId && event.cause === "death" |
| 175 | ); |
| 176 | assert.ok(despawn, "expected combat death despawn"); |
| 177 | assert.equal(despawn.dimensionId, CAVE_ID); |
| 178 | // Entity is already gone — interest must not fall back to overworld. |
| 179 | const keys = eventInterestKeys(despawn, () => null); |
| 180 | assert.equal(keys.dimensionId, CAVE_ID); |
| 181 | assert.equal(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(9), CAVE_ID)); |
| 182 | assert.notEqual(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(9), OVERWORLD_ID)); |
| 183 | } |
| 184 | finally |
| 185 | { harness.close(); } |
| 186 | }); |
| 187 | |
| 188 | test("cave rat kill advances killCount quest progress", () => |
| 189 | { |
| 190 | const harness = makeCaveHarness(); |
| 191 | try |
| 192 | { |
| 193 | const quest = normalizeQuestDef({ |
| 194 | questId: "kill-rats-cave", |
| 195 | title: "Kill Rats", |
| 196 | steps: [{ |
| 197 | stepId: "step-1", |
| 198 | title: "Hunt", |
| 199 | objectives: [{ |
| 200 | objectiveId: "obj-1", |
| 201 | text: "Kill 5 rats", |
| 202 | when: { all: [{ condition: { kind: "killCount", mobTypeId: "rat", count: 5 } }] } |
| 203 | }], |
| 204 | completionJournalText: "Done." |
| 205 | }] |
| 206 | }); |
| 207 | const pending = harness.worldDb.ensurePendingRevision(1); |
| 208 | harness.worldDb.saveQuestDef(pending, quest); |
| 209 | harness.worldDb.promotePendingRevision(); |
| 210 | harness.simulation.questContent.reload(); |
| 211 | |
| 212 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 213 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 214 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 215 | harness.simulation.quests.startQuest(character.character_id, "kill-rats-cave", null, []); |
| 216 | |
| 217 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 218 | tileX: 8, |
| 219 | tileY: 9, |
| 220 | mobTypeId: MobId.rat, |
| 221 | spawnerId: null, |
| 222 | dimensionId: CAVE_ID |
| 223 | })); |
| 224 | const worker = workerOf(harness.simulation, ratId); |
| 225 | worker.world.ecs.get(worker.world.ecs.handleOf(ratId), Component.health).current = 1; |
| 226 | const killEvents = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now()); |
| 227 | const mobKilled = killEvents.find((event) => event.type === "mob-killed"); |
| 228 | assert.ok(mobKilled); |
| 229 | const noteEvents = harness.simulation.quests.noteKill(character.character_id, { |
| 230 | mobTypeId: mobKilled.mobTypeId, |
| 231 | spawnerId: mobKilled.spawnerId |
| 232 | }); |
| 233 | assert.ok(noteEvents.some((event) => event.type === "questsDirty")); |
| 234 | assert.equal( |
| 235 | harness.stateDb.getQuestState(character.character_id, "kill-rats-cave").progress?.counters?.["mob:rat"], |
| 236 | 1 |
| 237 | ); |
| 238 | const snap = harness.simulation.quests.questSnapshot(character.character_id); |
| 239 | assert.equal(snap.quests[0].step.objectives[0].current, 1); |
| 240 | assert.equal(snap.quests[0].step.objectives[0].required, 5); |
| 241 | } |
| 242 | finally |
| 243 | { harness.close(); } |
| 244 | }); |