| 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 { ItemId } from "../../shared/definitions/items.js"; |
| 10 | import { OVERWORLD_ID, DimensionLightingMode, DimensionSeasonMode } from "../../shared/world/dimensions.js"; |
| 11 | import { COMBAT_LOGOUT_MS } from "../../shared/combat/combatLogout.js"; |
| 12 | import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; |
| 13 | import { WorldDatabase } from "../src/persistence/worldDb.js"; |
| 14 | import { StateDatabase } from "../src/persistence/stateDb.js"; |
| 15 | import { Simulation } from "../src/simulation/simulation.js"; |
| 16 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 17 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 18 | import { engageTarget, ensureMobCombat, stepCombat, stepAggression, applyDamageAndKill } from "../src/combat/service.js"; |
| 19 | import { Component } from "../src/ecs/components.js"; |
| 20 | import { WorkerMessage } from "../src/simulation/protocol.js"; |
| 21 | import { eventInterestKeys } from "../src/gateway/eventInterest.js"; |
| 22 | import { chunkKey, tileToChunk } from "../src/world/coordinates.js"; |
| 23 | import { makeCharacter, openChunkStrip, writeOpenChunk } from "./helpers/simulationHarness.js"; |
| 24 | import { normalizeQuestDef } from "../../shared/quests/normalize.js"; |
| 25 | |
| 26 | const CAVE_ID = "test-cave"; |
| 27 | |
| 28 | function makeCaveHarness () |
| 29 | { |
| 30 | const dir = mkdtempSync(join(tmpdir(), "mmo-dim-interact-")); |
| 31 | const worldDb = new WorldDatabase(join(dir, "world.sqlite")); |
| 32 | const stateDb = new StateDatabase(join(dir, "state.sqlite")); |
| 33 | openChunkStrip(worldDb, 2); |
| 34 | const rev = worldDb.getActiveRevisionId(); |
| 35 | worldDb.saveDimension(rev, { |
| 36 | dimensionId: CAVE_ID, |
| 37 | displayName: "Test Cave", |
| 38 | lightingMode: DimensionLightingMode.alwaysLit, |
| 39 | seasonMode: DimensionSeasonMode.summer, |
| 40 | defaultGroundTypeId: 1 |
| 41 | }); |
| 42 | const cave = createEmptyChunkData(); |
| 43 | cave.open = true; |
| 44 | cave.blocks[8 * 16 + 9] = { |
| 45 | typeId: BlockType.furnace, |
| 46 | state: "furnace", |
| 47 | config: null |
| 48 | }; |
| 49 | worldDb.saveChunkVersion(0, 0, rev, true, encodeChunkBlob(cave), CAVE_ID); |
| 50 | writeOpenChunk(worldDb, 0, 0); |
| 51 | let now = 1_000; |
| 52 | const simulation = new Simulation(worldDb, stateDb, { |
| 53 | workerCount: 1, |
| 54 | workerMode: "inprocess", |
| 55 | now: () => now |
| 56 | }); |
| 57 | return { |
| 58 | simulation, |
| 59 | worldDb, |
| 60 | stateDb, |
| 61 | now () |
| 62 | { |
| 63 | return now; |
| 64 | }, |
| 65 | advance (ms) |
| 66 | { |
| 67 | now += ms; |
| 68 | }, |
| 69 | close () |
| 70 | { |
| 71 | simulation.forceUnloadAll(); |
| 72 | worldDb.close(); |
| 73 | stateDb.close(); |
| 74 | } |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | function workerOf (simulation, entityId) |
| 79 | { |
| 80 | const record = simulation.entityRecord(entityId); |
| 81 | assert.ok(record, "entity record"); |
| 82 | return simulation.workers[record.workerId]; |
| 83 | } |
| 84 | |
| 85 | test("open furnace works in a non-overworld dimension", () => |
| 86 | { |
| 87 | const harness = makeCaveHarness(); |
| 88 | try |
| 89 | { |
| 90 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 91 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 92 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 93 | const position = harness.simulation.positionOf(playerId); |
| 94 | assert.equal(position.dimensionId, CAVE_ID); |
| 95 | |
| 96 | harness.simulation.enqueue(1, { |
| 97 | type: "open-station", |
| 98 | characterId: character.character_id, |
| 99 | stationId: CraftingStationId.furnace |
| 100 | }); |
| 101 | const result = harness.simulation.step(); |
| 102 | assert.ok( |
| 103 | result.events.some((event) => |
| 104 | event.type === "station-open" && event.stationId === CraftingStationId.furnace), |
| 105 | "expected station-open in cave" |
| 106 | ); |
| 107 | } |
| 108 | finally |
| 109 | { harness.close(); } |
| 110 | }); |
| 111 | |
| 112 | test("melee combat ticks in a non-overworld dimension", () => |
| 113 | { |
| 114 | const harness = makeCaveHarness(); |
| 115 | try |
| 116 | { |
| 117 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 118 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 119 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 120 | assert.equal(harness.simulation.positionOf(playerId).dimensionId, CAVE_ID); |
| 121 | |
| 122 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 123 | tileX: 8, |
| 124 | tileY: 9, |
| 125 | mobTypeId: MobId.rat, |
| 126 | spawnerId: null, |
| 127 | dimensionId: CAVE_ID |
| 128 | })); |
| 129 | assert.equal(harness.simulation.positionOf(ratId).dimensionId, CAVE_ID); |
| 130 | |
| 131 | const worker = workerOf(harness.simulation, ratId); |
| 132 | const world = worker.world; |
| 133 | assert.ok(world.chunkIsSimulating(8, 9, CAVE_ID)); |
| 134 | assert.equal(world.chunkIsSimulating(8, 9, OVERWORLD_ID), false); |
| 135 | |
| 136 | assert.ok(ensureMobCombat(world.ecs, ratId)); |
| 137 | engageTarget(world.ecs, ratId, playerId, harness.now()); |
| 138 | const combat = world.ecs.get(world.ecs.handleOf(ratId), Component.combat); |
| 139 | combat.nextSwingAt = harness.now(); |
| 140 | |
| 141 | const events = stepCombat(world, harness.now()); |
| 142 | const combatFx = events.find((event) => |
| 143 | event.type === "combat-swing" || event.type === "combat-hit" |
| 144 | || event.type === "combat-miss" || event.type === "combat-block" |
| 145 | ); |
| 146 | assert.ok( |
| 147 | combatFx, |
| 148 | `expected combat resolution in cave, got ${events.map((e) => e.type).join(",") || "none"}` |
| 149 | ); |
| 150 | assert.equal(combatFx.dimensionId, CAVE_ID); |
| 151 | |
| 152 | stepAggression(world, harness.now()); |
| 153 | } |
| 154 | finally |
| 155 | { harness.close(); } |
| 156 | }); |
| 157 | |
| 158 | test("mob kill entity-despawn carries cave dimensionId for interest", () => |
| 159 | { |
| 160 | const harness = makeCaveHarness(); |
| 161 | try |
| 162 | { |
| 163 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 164 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 165 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 166 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 167 | tileX: 8, |
| 168 | tileY: 9, |
| 169 | mobTypeId: MobId.rat, |
| 170 | spawnerId: null, |
| 171 | dimensionId: CAVE_ID |
| 172 | })); |
| 173 | const worker = workerOf(harness.simulation, ratId); |
| 174 | const handle = worker.world.ecs.handleOf(ratId); |
| 175 | worker.world.ecs.get(handle, Component.health).current = 1; |
| 176 | const events = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now()); |
| 177 | const despawn = events.find((event) => |
| 178 | event.type === "entity-despawn" && event.entityId === ratId && event.cause === "death" |
| 179 | ); |
| 180 | assert.ok(despawn, "expected combat death despawn"); |
| 181 | assert.equal(despawn.dimensionId, CAVE_ID); |
| 182 | // Entity is already gone — interest must not fall back to overworld. |
| 183 | const keys = eventInterestKeys(despawn, () => null); |
| 184 | assert.equal(keys.dimensionId, CAVE_ID); |
| 185 | assert.equal(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(9), CAVE_ID)); |
| 186 | assert.notEqual(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(9), OVERWORLD_ID)); |
| 187 | } |
| 188 | finally |
| 189 | { harness.close(); } |
| 190 | }); |
| 191 | |
| 192 | test("cave rat kill advances killCount quest progress", () => |
| 193 | { |
| 194 | const harness = makeCaveHarness(); |
| 195 | try |
| 196 | { |
| 197 | const quest = normalizeQuestDef({ |
| 198 | questId: "kill-rats-cave", |
| 199 | title: "Kill Rats", |
| 200 | steps: [{ |
| 201 | stepId: "step-1", |
| 202 | title: "Hunt", |
| 203 | objectives: [{ |
| 204 | objectiveId: "obj-1", |
| 205 | text: "Kill 5 rats", |
| 206 | when: { all: [{ condition: { kind: "killCount", mobTypeId: "rat", count: 5 } }] } |
| 207 | }], |
| 208 | completionJournalText: "Done." |
| 209 | }] |
| 210 | }); |
| 211 | const pending = harness.worldDb.ensurePendingRevision(1); |
| 212 | harness.worldDb.saveQuestDef(pending, quest); |
| 213 | harness.worldDb.promotePendingRevision(); |
| 214 | harness.simulation.questContent.reload(); |
| 215 | |
| 216 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 217 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 218 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 219 | harness.simulation.quests.startQuest(character.character_id, "kill-rats-cave", null, []); |
| 220 | |
| 221 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 222 | tileX: 8, |
| 223 | tileY: 9, |
| 224 | mobTypeId: MobId.rat, |
| 225 | spawnerId: null, |
| 226 | dimensionId: CAVE_ID |
| 227 | })); |
| 228 | const worker = workerOf(harness.simulation, ratId); |
| 229 | worker.world.ecs.get(worker.world.ecs.handleOf(ratId), Component.health).current = 1; |
| 230 | const killEvents = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now()); |
| 231 | const mobKilled = killEvents.find((event) => event.type === "mob-killed"); |
| 232 | assert.ok(mobKilled); |
| 233 | const noteEvents = harness.simulation.quests.noteKill(character.character_id, { |
| 234 | mobTypeId: mobKilled.mobTypeId, |
| 235 | spawnerId: mobKilled.spawnerId |
| 236 | }); |
| 237 | assert.ok(noteEvents.some((event) => event.type === "questsDirty")); |
| 238 | assert.equal( |
| 239 | harness.stateDb.getQuestState(character.character_id, "kill-rats-cave").progress?.counters?.["mob:rat"], |
| 240 | 1 |
| 241 | ); |
| 242 | const snap = harness.simulation.quests.questSnapshot(character.character_id); |
| 243 | assert.equal(snap.quests[0].step.objectives[0].current, 1); |
| 244 | assert.equal(snap.quests[0].step.objectives[0].required, 5); |
| 245 | } |
| 246 | finally |
| 247 | { harness.close(); } |
| 248 | }); |
| 249 | |
| 250 | test("character logout despawn carries dimensionId for cave interest", () => |
| 251 | { |
| 252 | const harness = makeCaveHarness(); |
| 253 | try |
| 254 | { |
| 255 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 256 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 257 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 258 | assert.equal(harness.simulation.positionOf(entityId).dimensionId, CAVE_ID); |
| 259 | harness.simulation.drainReplication(); |
| 260 | |
| 261 | harness.simulation.despawnCharacter(character.character_id); |
| 262 | const gone = harness.simulation.drainReplication(); |
| 263 | const despawn = gone.find((event) => |
| 264 | event.type === "entity-despawn" && event.entityId === entityId |
| 265 | ); |
| 266 | assert.ok(despawn, "expected entity-despawn replication"); |
| 267 | assert.equal(despawn.dimensionId, CAVE_ID); |
| 268 | const keys = eventInterestKeys(despawn, () => null); |
| 269 | assert.equal(keys.dimensionId, CAVE_ID); |
| 270 | assert.equal(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(8), CAVE_ID)); |
| 271 | } |
| 272 | finally |
| 273 | { harness.close(); } |
| 274 | }); |
| 275 | |
| 276 | test("mob death loot ground-item-appear stays in cave dimension", () => |
| 277 | { |
| 278 | const harness = makeCaveHarness(); |
| 279 | try |
| 280 | { |
| 281 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 282 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 283 | harness.stateDb.saveBag(character.character_id, { |
| 284 | inventory: emptySlots(), |
| 285 | equipment: emptyEquipment(), |
| 286 | vault: [], |
| 287 | vaultReserved: [] |
| 288 | }); |
| 289 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 290 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 291 | tileX: 8, |
| 292 | tileY: 9, |
| 293 | mobTypeId: MobId.rat, |
| 294 | spawnerId: "cave-loot-rat", |
| 295 | dimensionId: CAVE_ID, |
| 296 | drops: [{ itemId: ItemId.stick, chance: 1, quantityMin: 1, quantityMax: 1 }] |
| 297 | })); |
| 298 | const worker = workerOf(harness.simulation, ratId); |
| 299 | worker.world.ecs.get(worker.world.ecs.handleOf(ratId), Component.health).current = 1; |
| 300 | const events = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now()); |
| 301 | const appear = events.find((event) => |
| 302 | event.type === "ground-item-appear" && event.stack?.itemId === ItemId.stick |
| 303 | ); |
| 304 | assert.ok(appear, JSON.stringify(events.filter((e) => e.type === "ground-item-appear" || e.type === "mob-killed"))); |
| 305 | assert.equal(appear.dimensionId, CAVE_ID); |
| 306 | assert.equal(appear.tileX, 8); |
| 307 | assert.equal(appear.tileY, 9); |
| 308 | const onGround = [...worker.world.groundItems.values()].filter( |
| 309 | (item) => item.dimensionId === CAVE_ID && item.stack?.itemId === ItemId.stick |
| 310 | ); |
| 311 | assert.ok(onGround.length >= 1); |
| 312 | assert.equal( |
| 313 | [...worker.world.groundItems.values()].filter( |
| 314 | (item) => item.dimensionId === OVERWORLD_ID && item.stack?.itemId === ItemId.stick |
| 315 | ).length, |
| 316 | 0 |
| 317 | ); |
| 318 | } |
| 319 | finally |
| 320 | { harness.close(); } |
| 321 | }); |
| 322 | |
| 323 | test("combat logout quiet despawn carries cave dimensionId", () => |
| 324 | { |
| 325 | const harness = makeCaveHarness(); |
| 326 | try |
| 327 | { |
| 328 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 329 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 330 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 331 | assert.equal(harness.simulation.positionOf(playerId).dimensionId, CAVE_ID); |
| 332 | |
| 333 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 334 | tileX: 8, |
| 335 | tileY: 9, |
| 336 | mobTypeId: MobId.rat, |
| 337 | spawnerId: null, |
| 338 | dimensionId: CAVE_ID |
| 339 | })); |
| 340 | harness.simulation.enqueue(1, { |
| 341 | type: "attack", |
| 342 | characterId: character.character_id, |
| 343 | targetEntityId: ratId |
| 344 | }); |
| 345 | harness.simulation.step(); |
| 346 | assert.equal( |
| 347 | harness.simulation.beginCombatLogout(character.character_id), |
| 348 | true, |
| 349 | "expected combat logout after engaging" |
| 350 | ); |
| 351 | harness.simulation.drainReplication(); |
| 352 | |
| 353 | const record = harness.simulation.entityRecord(playerId); |
| 354 | harness.simulation.workers[record.workerId].call({ |
| 355 | type: WorkerMessage.clearCombat, |
| 356 | entityId: playerId, |
| 357 | targetEntityId: playerId |
| 358 | }); |
| 359 | const track = harness.simulation.combatTracking.get(character.character_id); |
| 360 | assert.ok(track); |
| 361 | track.lastCombatActivityAt = harness.now() - COMBAT_LOGOUT_MS - 1; |
| 362 | track.wasInCombat = false; |
| 363 | const logout = harness.simulation.combatLogouts.get(character.character_id); |
| 364 | assert.ok(logout); |
| 365 | logout.startedAt = harness.now() - COMBAT_LOGOUT_MS - 1; |
| 366 | |
| 367 | const replication = []; |
| 368 | for (let i = 0; i < 60; i++) |
| 369 | { |
| 370 | harness.advance(50); |
| 371 | const stepped = harness.simulation.step(); |
| 372 | replication.push(...(stepped.replication ?? [])); |
| 373 | replication.push(...harness.simulation.drainReplication()); |
| 374 | } |
| 375 | assert.equal(harness.simulation.isCharacterInWorld(character.character_id), false); |
| 376 | const despawn = replication.find((event) => |
| 377 | event.type === "entity-despawn" && event.entityId === playerId |
| 378 | ); |
| 379 | assert.ok(despawn, JSON.stringify(replication.filter((e) => e.type === "entity-despawn"))); |
| 380 | assert.equal(despawn.dimensionId, CAVE_ID); |
| 381 | const keys = eventInterestKeys(despawn, () => null); |
| 382 | assert.equal(keys.dimensionId, CAVE_ID); |
| 383 | assert.notEqual(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(8), OVERWORLD_ID)); |
| 384 | } |
| 385 | finally |
| 386 | { harness.close(); } |
| 387 | }); |