| 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, ensureDimension, makeDimensionSimulation, pinEastSplit } from "./helpers/simulationHarness.js"; |
| 24 | import { normalizeQuestDef } from "../../shared/quests/normalize.js"; |
| 25 | import { normalizeSpawnerConfig } from "../../shared/definitions/mobs.js"; |
| 26 | import { regionKey } from "../src/world/coordinates.js"; |
| 27 | import |
| 28 | { |
| 29 | CHURCH_LETTER_AUTHOR, |
| 30 | formatChurchOfflineResurrectionBody |
| 31 | } from "../../shared/courier/systemLetters.js"; |
| 32 | |
| 33 | const CAVE_ID = "test-cave"; |
| 34 | |
| 35 | function makeCaveHarness () |
| 36 | { |
| 37 | const dir = mkdtempSync(join(tmpdir(), "mmo-dim-interact-")); |
| 38 | const worldDb = new WorldDatabase(join(dir, "world.sqlite")); |
| 39 | const stateDb = new StateDatabase(join(dir, "state.sqlite")); |
| 40 | openChunkStrip(worldDb, 2); |
| 41 | const rev = worldDb.getActiveRevisionId(); |
| 42 | worldDb.saveDimension(rev, { |
| 43 | dimensionId: CAVE_ID, |
| 44 | displayName: "Test Cave", |
| 45 | lightingMode: DimensionLightingMode.alwaysLit, |
| 46 | seasonMode: DimensionSeasonMode.summer, |
| 47 | defaultGroundTypeId: 1 |
| 48 | }); |
| 49 | const cave = createEmptyChunkData(); |
| 50 | cave.open = true; |
| 51 | cave.blocks[8 * 16 + 9] = { |
| 52 | typeId: BlockType.furnace, |
| 53 | state: "furnace", |
| 54 | config: null |
| 55 | }; |
| 56 | worldDb.saveChunkVersion(0, 0, rev, true, encodeChunkBlob(cave), CAVE_ID); |
| 57 | writeOpenChunk(worldDb, 0, 0); |
| 58 | let now = 1_000; |
| 59 | const simulation = new Simulation(worldDb, stateDb, { |
| 60 | workerCount: 1, |
| 61 | workerMode: "inprocess", |
| 62 | now: () => now |
| 63 | }); |
| 64 | return { |
| 65 | simulation, |
| 66 | worldDb, |
| 67 | stateDb, |
| 68 | now () |
| 69 | { |
| 70 | return now; |
| 71 | }, |
| 72 | advance (ms) |
| 73 | { |
| 74 | now += ms; |
| 75 | }, |
| 76 | close () |
| 77 | { |
| 78 | simulation.forceUnloadAll(); |
| 79 | worldDb.close(); |
| 80 | stateDb.close(); |
| 81 | } |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | function workerOf (simulation, entityId) |
| 86 | { |
| 87 | const record = simulation.entityRecord(entityId); |
| 88 | assert.ok(record, "entity record"); |
| 89 | return simulation.workers[record.workerId]; |
| 90 | } |
| 91 | |
| 92 | test("open furnace works in a non-overworld dimension", () => |
| 93 | { |
| 94 | const harness = makeCaveHarness(); |
| 95 | try |
| 96 | { |
| 97 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 98 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 99 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 100 | const position = harness.simulation.positionOf(playerId); |
| 101 | assert.equal(position.dimensionId, CAVE_ID); |
| 102 | |
| 103 | harness.simulation.enqueue(1, { |
| 104 | type: "open-station", |
| 105 | characterId: character.character_id, |
| 106 | stationId: CraftingStationId.furnace |
| 107 | }); |
| 108 | const result = harness.simulation.step(); |
| 109 | assert.ok( |
| 110 | result.events.some((event) => |
| 111 | event.type === "station-open" && event.stationId === CraftingStationId.furnace), |
| 112 | "expected station-open in cave" |
| 113 | ); |
| 114 | } |
| 115 | finally |
| 116 | { harness.close(); } |
| 117 | }); |
| 118 | |
| 119 | test("melee combat ticks in a non-overworld dimension", () => |
| 120 | { |
| 121 | const harness = makeCaveHarness(); |
| 122 | try |
| 123 | { |
| 124 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 125 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 126 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 127 | assert.equal(harness.simulation.positionOf(playerId).dimensionId, CAVE_ID); |
| 128 | |
| 129 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 130 | tileX: 8, |
| 131 | tileY: 9, |
| 132 | mobTypeId: MobId.rat, |
| 133 | spawnerId: null, |
| 134 | dimensionId: CAVE_ID |
| 135 | })); |
| 136 | assert.equal(harness.simulation.positionOf(ratId).dimensionId, CAVE_ID); |
| 137 | |
| 138 | const worker = workerOf(harness.simulation, ratId); |
| 139 | const world = worker.world; |
| 140 | assert.ok(world.chunkIsSimulating(8, 9, CAVE_ID)); |
| 141 | assert.equal(world.chunkIsSimulating(8, 9, OVERWORLD_ID), false); |
| 142 | |
| 143 | assert.ok(ensureMobCombat(world.ecs, ratId)); |
| 144 | engageTarget(world.ecs, ratId, playerId, harness.now()); |
| 145 | const combat = world.ecs.get(world.ecs.handleOf(ratId), Component.combat); |
| 146 | combat.nextSwingAt = harness.now(); |
| 147 | |
| 148 | const events = stepCombat(world, harness.now()); |
| 149 | const combatFx = events.find((event) => |
| 150 | event.type === "combat-swing" || event.type === "combat-hit" |
| 151 | || event.type === "combat-miss" || event.type === "combat-block" |
| 152 | ); |
| 153 | assert.ok( |
| 154 | combatFx, |
| 155 | `expected combat resolution in cave, got ${events.map((e) => e.type).join(",") || "none"}` |
| 156 | ); |
| 157 | assert.equal(combatFx.dimensionId, CAVE_ID); |
| 158 | |
| 159 | stepAggression(world, harness.now()); |
| 160 | } |
| 161 | finally |
| 162 | { harness.close(); } |
| 163 | }); |
| 164 | |
| 165 | test("mob kill entity-despawn carries cave dimensionId for interest", () => |
| 166 | { |
| 167 | const harness = makeCaveHarness(); |
| 168 | try |
| 169 | { |
| 170 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 171 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 172 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 173 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 174 | tileX: 8, |
| 175 | tileY: 9, |
| 176 | mobTypeId: MobId.rat, |
| 177 | spawnerId: null, |
| 178 | dimensionId: CAVE_ID |
| 179 | })); |
| 180 | const worker = workerOf(harness.simulation, ratId); |
| 181 | const handle = worker.world.ecs.handleOf(ratId); |
| 182 | worker.world.ecs.get(handle, Component.health).current = 1; |
| 183 | const events = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now()); |
| 184 | const despawn = events.find((event) => |
| 185 | event.type === "entity-despawn" && event.entityId === ratId && event.cause === "death" |
| 186 | ); |
| 187 | assert.ok(despawn, "expected combat death despawn"); |
| 188 | assert.equal(despawn.dimensionId, CAVE_ID); |
| 189 | // Entity is already gone — interest must not fall back to overworld. |
| 190 | const keys = eventInterestKeys(despawn, () => null); |
| 191 | assert.equal(keys.dimensionId, CAVE_ID); |
| 192 | assert.equal(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(9), CAVE_ID)); |
| 193 | assert.notEqual(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(9), OVERWORLD_ID)); |
| 194 | } |
| 195 | finally |
| 196 | { harness.close(); } |
| 197 | }); |
| 198 | |
| 199 | test("cave rat kill advances killCount quest progress", () => |
| 200 | { |
| 201 | const harness = makeCaveHarness(); |
| 202 | try |
| 203 | { |
| 204 | const quest = normalizeQuestDef({ |
| 205 | questId: "kill-rats-cave", |
| 206 | title: "Kill Rats", |
| 207 | steps: [{ |
| 208 | stepId: "step-1", |
| 209 | title: "Hunt", |
| 210 | objectives: [{ |
| 211 | objectiveId: "obj-1", |
| 212 | text: "Kill 5 rats", |
| 213 | when: { all: [{ condition: { kind: "killCount", mobTypeId: "rat", count: 5 } }] } |
| 214 | }], |
| 215 | completionJournalText: "Done." |
| 216 | }] |
| 217 | }); |
| 218 | const pending = harness.worldDb.ensurePendingRevision(1); |
| 219 | harness.worldDb.saveQuestDef(pending, quest); |
| 220 | harness.worldDb.promotePendingRevision(); |
| 221 | harness.simulation.questContent.reload(); |
| 222 | |
| 223 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 224 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 225 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 226 | harness.simulation.quests.startQuest(character.character_id, "kill-rats-cave", null, []); |
| 227 | |
| 228 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 229 | tileX: 8, |
| 230 | tileY: 9, |
| 231 | mobTypeId: MobId.rat, |
| 232 | spawnerId: null, |
| 233 | dimensionId: CAVE_ID |
| 234 | })); |
| 235 | const worker = workerOf(harness.simulation, ratId); |
| 236 | worker.world.ecs.get(worker.world.ecs.handleOf(ratId), Component.health).current = 1; |
| 237 | const killEvents = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now()); |
| 238 | const mobKilled = killEvents.find((event) => event.type === "mob-killed"); |
| 239 | assert.ok(mobKilled); |
| 240 | const noteEvents = harness.simulation.quests.noteKill(character.character_id, { |
| 241 | mobTypeId: mobKilled.mobTypeId, |
| 242 | spawnerId: mobKilled.spawnerId |
| 243 | }); |
| 244 | assert.ok(noteEvents.some((event) => event.type === "questsDirty")); |
| 245 | assert.equal( |
| 246 | harness.stateDb.getQuestState(character.character_id, "kill-rats-cave").progress?.counters?.["mob:rat"], |
| 247 | 1 |
| 248 | ); |
| 249 | const snap = harness.simulation.quests.questSnapshot(character.character_id); |
| 250 | assert.equal(snap.quests[0].step.objectives[0].current, 1); |
| 251 | assert.equal(snap.quests[0].step.objectives[0].required, 5); |
| 252 | } |
| 253 | finally |
| 254 | { harness.close(); } |
| 255 | }); |
| 256 | |
| 257 | test("character logout despawn carries dimensionId for cave interest", () => |
| 258 | { |
| 259 | const harness = makeCaveHarness(); |
| 260 | try |
| 261 | { |
| 262 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 263 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 264 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 265 | assert.equal(harness.simulation.positionOf(entityId).dimensionId, CAVE_ID); |
| 266 | harness.simulation.drainReplication(); |
| 267 | |
| 268 | harness.simulation.despawnCharacter(character.character_id); |
| 269 | const gone = harness.simulation.drainReplication(); |
| 270 | const despawn = gone.find((event) => |
| 271 | event.type === "entity-despawn" && event.entityId === entityId |
| 272 | ); |
| 273 | assert.ok(despawn, "expected entity-despawn replication"); |
| 274 | assert.equal(despawn.dimensionId, CAVE_ID); |
| 275 | const keys = eventInterestKeys(despawn, () => null); |
| 276 | assert.equal(keys.dimensionId, CAVE_ID); |
| 277 | assert.equal(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(8), CAVE_ID)); |
| 278 | } |
| 279 | finally |
| 280 | { harness.close(); } |
| 281 | }); |
| 282 | |
| 283 | test("mob death loot ground-item-appear stays in cave dimension", () => |
| 284 | { |
| 285 | const harness = makeCaveHarness(); |
| 286 | try |
| 287 | { |
| 288 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 289 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 290 | harness.stateDb.saveBag(character.character_id, { |
| 291 | inventory: emptySlots(), |
| 292 | equipment: emptyEquipment(), |
| 293 | vault: [], |
| 294 | vaultReserved: [] |
| 295 | }); |
| 296 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 297 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 298 | tileX: 8, |
| 299 | tileY: 9, |
| 300 | mobTypeId: MobId.rat, |
| 301 | spawnerId: "cave-loot-rat", |
| 302 | dimensionId: CAVE_ID, |
| 303 | drops: [{ itemId: ItemId.stick, chance: 1, quantityMin: 1, quantityMax: 1 }] |
| 304 | })); |
| 305 | const worker = workerOf(harness.simulation, ratId); |
| 306 | worker.world.ecs.get(worker.world.ecs.handleOf(ratId), Component.health).current = 1; |
| 307 | const events = applyDamageAndKill(worker.world, playerId, ratId, 1, harness.now()); |
| 308 | const appear = events.find((event) => |
| 309 | event.type === "ground-item-appear" && event.stack?.itemId === ItemId.stick |
| 310 | ); |
| 311 | assert.ok(appear, JSON.stringify(events.filter((e) => e.type === "ground-item-appear" || e.type === "mob-killed"))); |
| 312 | assert.equal(appear.dimensionId, CAVE_ID); |
| 313 | assert.equal(appear.tileX, 8); |
| 314 | assert.equal(appear.tileY, 9); |
| 315 | const onGround = [...worker.world.groundItems.values()].filter( |
| 316 | (item) => item.dimensionId === CAVE_ID && item.stack?.itemId === ItemId.stick |
| 317 | ); |
| 318 | assert.ok(onGround.length >= 1); |
| 319 | assert.equal( |
| 320 | [...worker.world.groundItems.values()].filter( |
| 321 | (item) => item.dimensionId === OVERWORLD_ID && item.stack?.itemId === ItemId.stick |
| 322 | ).length, |
| 323 | 0 |
| 324 | ); |
| 325 | } |
| 326 | finally |
| 327 | { harness.close(); } |
| 328 | }); |
| 329 | |
| 330 | test("combat logout quiet despawn carries cave dimensionId", () => |
| 331 | { |
| 332 | const harness = makeCaveHarness(); |
| 333 | try |
| 334 | { |
| 335 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 336 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 337 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 338 | assert.equal(harness.simulation.positionOf(playerId).dimensionId, CAVE_ID); |
| 339 | |
| 340 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 341 | tileX: 8, |
| 342 | tileY: 9, |
| 343 | mobTypeId: MobId.rat, |
| 344 | spawnerId: null, |
| 345 | dimensionId: CAVE_ID |
| 346 | })); |
| 347 | harness.simulation.enqueue(1, { |
| 348 | type: "attack", |
| 349 | characterId: character.character_id, |
| 350 | targetEntityId: ratId |
| 351 | }); |
| 352 | harness.simulation.step(); |
| 353 | assert.equal( |
| 354 | harness.simulation.beginCombatLogout(character.character_id), |
| 355 | true, |
| 356 | "expected combat logout after engaging" |
| 357 | ); |
| 358 | harness.simulation.drainReplication(); |
| 359 | |
| 360 | const record = harness.simulation.entityRecord(playerId); |
| 361 | harness.simulation.workers[record.workerId].call({ |
| 362 | type: WorkerMessage.clearCombat, |
| 363 | entityId: playerId, |
| 364 | targetEntityId: playerId |
| 365 | }); |
| 366 | const track = harness.simulation.combatTracking.get(character.character_id); |
| 367 | assert.ok(track); |
| 368 | track.lastCombatActivityAt = harness.now() - COMBAT_LOGOUT_MS - 1; |
| 369 | track.wasInCombat = false; |
| 370 | const logout = harness.simulation.combatLogouts.get(character.character_id); |
| 371 | assert.ok(logout); |
| 372 | logout.startedAt = harness.now() - COMBAT_LOGOUT_MS - 1; |
| 373 | |
| 374 | const replication = []; |
| 375 | for (let i = 0; i < 60; i++) |
| 376 | { |
| 377 | harness.advance(50); |
| 378 | const stepped = harness.simulation.step(); |
| 379 | replication.push(...(stepped.replication ?? [])); |
| 380 | replication.push(...harness.simulation.drainReplication()); |
| 381 | } |
| 382 | assert.equal(harness.simulation.isCharacterInWorld(character.character_id), false); |
| 383 | const despawn = replication.find((event) => |
| 384 | event.type === "entity-despawn" && event.entityId === playerId |
| 385 | ); |
| 386 | assert.ok(despawn, JSON.stringify(replication.filter((e) => e.type === "entity-despawn"))); |
| 387 | assert.equal(despawn.dimensionId, CAVE_ID); |
| 388 | const keys = eventInterestKeys(despawn, () => null); |
| 389 | assert.equal(keys.dimensionId, CAVE_ID); |
| 390 | assert.notEqual(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(8), OVERWORLD_ID)); |
| 391 | } |
| 392 | finally |
| 393 | { harness.close(); } |
| 394 | }); |
| 395 | |
| 396 | |
| 397 | const SHARED_MOB_SPAWNER_ID = "shared-mob-local"; |
| 398 | |
| 399 | function placeMobSpawner (worldDb, localX, localY, dimensionId, spawnerId, extras = {}) |
| 400 | { |
| 401 | ensureDimension(worldDb, dimensionId); |
| 402 | const rev = worldDb.getActiveRevisionId(); |
| 403 | const data = createEmptyChunkData(); |
| 404 | data.open = true; |
| 405 | data.spawners = [{ |
| 406 | localX, |
| 407 | localY, |
| 408 | config: normalizeSpawnerConfig({ |
| 409 | spawnerId, |
| 410 | mobTypeId: MobId.rat, |
| 411 | maxAlive: 1, |
| 412 | spawnRangeTiles: 0, |
| 413 | maxWanderRangeTiles: 0, |
| 414 | respawnDelaySeconds: 60, |
| 415 | ...extras |
| 416 | }) |
| 417 | }]; |
| 418 | worldDb.saveChunkVersion(0, 0, rev, true, encodeChunkBlob(data), dimensionId); |
| 419 | } |
| 420 | |
| 421 | test("authored overworld + cave mob spawners with same local spawnerId stay independent", () => |
| 422 | { |
| 423 | const harness = makeDimensionSimulation(CAVE_ID, { workerCount: 1 }); |
| 424 | try |
| 425 | { |
| 426 | placeMobSpawner(harness.worldDb, 9, 8, OVERWORLD_ID, SHARED_MOB_SPAWNER_ID, { |
| 427 | drops: [{ itemId: ItemId.apple, chance: 1, quantityMin: 1, quantityMax: 1 }] |
| 428 | }); |
| 429 | placeMobSpawner(harness.worldDb, 9, 8, CAVE_ID, SHARED_MOB_SPAWNER_ID, { |
| 430 | drops: [{ itemId: ItemId.stick, chance: 1, quantityMin: 1, quantityMax: 1 }] |
| 431 | }); |
| 432 | harness.simulation.reloadWorld(); |
| 433 | |
| 434 | const overChar = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, lastName: "MobOver" }); |
| 435 | const overId = harness.simulation.spawnCharacter(1, overChar); |
| 436 | const caveChar = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, lastName: "MobCave" }); |
| 437 | harness.stateDb.saveCharacterPosition(caveChar.character_id, 8, 8, "walk", CAVE_ID); |
| 438 | const caveId = harness.simulation.spawnCharacter(2, caveChar); |
| 439 | assert.equal(harness.simulation.positionOf(overId).dimensionId, OVERWORLD_ID); |
| 440 | assert.equal(harness.simulation.positionOf(caveId).dimensionId, CAVE_ID); |
| 441 | |
| 442 | for (let i = 0; i < 40; i++) |
| 443 | { |
| 444 | harness.advance(50); |
| 445 | harness.simulation.step(); |
| 446 | } |
| 447 | |
| 448 | const worker = workerOf(harness.simulation, caveId); |
| 449 | const rats = []; |
| 450 | for (const entity of worker.world.ecs.query(Component.creature, Component.position, Component.spawnOrigin)) |
| 451 | { |
| 452 | if (entity.creature.mobTypeId !== MobId.rat) |
| 453 | { continue; } |
| 454 | rats.push({ |
| 455 | entityId: entity.entityId, |
| 456 | dimensionId: entity.position.dimensionId, |
| 457 | spawnerId: entity.spawnOrigin.spawnerId |
| 458 | }); |
| 459 | } |
| 460 | const caveRats = rats.filter((r) => r.dimensionId === CAVE_ID); |
| 461 | const overRats = rats.filter((r) => r.dimensionId === OVERWORLD_ID); |
| 462 | assert.equal(caveRats.length, 1, JSON.stringify(rats)); |
| 463 | assert.equal(overRats.length, 1, JSON.stringify(rats)); |
| 464 | assert.equal(caveRats[0].spawnerId, `${CAVE_ID}:${SHARED_MOB_SPAWNER_ID}`); |
| 465 | assert.equal(overRats[0].spawnerId, `${OVERWORLD_ID}:${SHARED_MOB_SPAWNER_ID}`); |
| 466 | |
| 467 | worker.world.ecs.get(worker.world.ecs.handleOf(caveRats[0].entityId), Component.health).current = 1; |
| 468 | const killEvents = applyDamageAndKill( |
| 469 | worker.world, |
| 470 | caveId, |
| 471 | caveRats[0].entityId, |
| 472 | 1, |
| 473 | harness.now() |
| 474 | ); |
| 475 | assert.ok(killEvents.some((e) => e.type === "entity-despawn" && e.entityId === caveRats[0].entityId)); |
| 476 | const stillOver = []; |
| 477 | for (const entity of worker.world.ecs.query(Component.creature, Component.position, Component.spawnOrigin)) |
| 478 | { |
| 479 | if (entity.creature.mobTypeId !== MobId.rat) |
| 480 | { continue; } |
| 481 | if (entity.position.dimensionId === OVERWORLD_ID) |
| 482 | { stillOver.push(entity.spawnOrigin.spawnerId); } |
| 483 | } |
| 484 | assert.deepEqual(stillOver, [`${OVERWORLD_ID}:${SHARED_MOB_SPAWNER_ID}`]); |
| 485 | } |
| 486 | finally |
| 487 | { harness.close(); } |
| 488 | }); |
| 489 | |
| 490 | test("region ownership keys are independent per dimension", () => |
| 491 | { |
| 492 | assert.notEqual(regionKey(0, 0, OVERWORLD_ID), regionKey(0, 0, CAVE_ID)); |
| 493 | const harness = makeDimensionSimulation(CAVE_ID, { |
| 494 | workerCount: 2, |
| 495 | pinRegion: pinEastSplit |
| 496 | }); |
| 497 | try |
| 498 | { |
| 499 | const overChar = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, lastName: "OwnOver" }); |
| 500 | const overId = harness.simulation.spawnCharacter(1, overChar); |
| 501 | const caveChar = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, lastName: "OwnCave" }); |
| 502 | harness.stateDb.saveCharacterPosition(caveChar.character_id, 8, 8, "walk", CAVE_ID); |
| 503 | const caveId = harness.simulation.spawnCharacter(2, caveChar); |
| 504 | harness.simulation.step(); |
| 505 | assert.equal(harness.simulation.positionOf(overId).dimensionId, OVERWORLD_ID); |
| 506 | assert.equal(harness.simulation.positionOf(caveId).dimensionId, CAVE_ID); |
| 507 | assert.equal( |
| 508 | harness.simulation.chunkIsSimulating(8, 8, OVERWORLD_ID), |
| 509 | true |
| 510 | ); |
| 511 | assert.equal( |
| 512 | harness.simulation.chunkIsSimulating(8, 8, CAVE_ID), |
| 513 | true |
| 514 | ); |
| 515 | const owners = harness.simulation.regionOwner; |
| 516 | assert.ok(owners.has(regionKey(0, 0, OVERWORLD_ID)), "overworld region owned"); |
| 517 | assert.ok(owners.has(regionKey(0, 0, CAVE_ID)), "cave region owned"); |
| 518 | } |
| 519 | finally |
| 520 | { harness.close(); } |
| 521 | }); |
| 522 | |
| 523 | test("PvP combat-logout offline death in cave keeps loot and altar/letter dimension-aware", () => |
| 524 | { |
| 525 | const harness = makeCaveHarness(); |
| 526 | try |
| 527 | { |
| 528 | // Overworld altar + bank; death happens in cave (no cave altar). |
| 529 | const overChunk = createEmptyChunkData(); |
| 530 | overChunk.open = true; |
| 531 | overChunk.blocks[8 * 16 + 4] = { typeId: BlockType.bank, state: 0 }; |
| 532 | harness.worldDb.saveChunkVersion( |
| 533 | 0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(overChunk), OVERWORLD_ID |
| 534 | ); |
| 535 | const altarChunk = createEmptyChunkData(); |
| 536 | altarChunk.open = true; |
| 537 | altarChunk.blocks[8 * 16 + 4] = { typeId: BlockType.churchAltar, state: 0 }; // tile 20,8 in chunk 1,0 |
| 538 | // chunk 1,0 local (4,8) => tileX=20, tileY=8 |
| 539 | harness.worldDb.saveChunkVersion( |
| 540 | 1, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(altarChunk), OVERWORLD_ID |
| 541 | ); |
| 542 | // Also place a closer cave altar so letter/respawn preference is tested against XY proximity. |
| 543 | const caveAltar = createEmptyChunkData(); |
| 544 | caveAltar.open = true; |
| 545 | caveAltar.blocks[8 * 16 + 10] = { typeId: BlockType.churchAltar, state: 0 }; // tile 10,8 |
| 546 | harness.worldDb.saveChunkVersion( |
| 547 | 0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(caveAltar), CAVE_ID |
| 548 | ); |
| 549 | harness.simulation.reloadWorld(); |
| 550 | |
| 551 | const victim = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Vic", lastName: "CaveVic" }); |
| 552 | harness.stateDb.saveCharacterPosition(victim.character_id, 8, 8, "walk", CAVE_ID); |
| 553 | harness.stateDb.saveBag(victim.character_id, { |
| 554 | inventory: emptySlots(), |
| 555 | equipment: emptyEquipment(), |
| 556 | vault: [], |
| 557 | vaultReserved: [] |
| 558 | }); |
| 559 | const attacker = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8, firstName: "Atk", lastName: "CaveAtk" }); |
| 560 | harness.stateDb.saveCharacterPosition(attacker.character_id, 9, 8, "walk", CAVE_ID); |
| 561 | const victimId = harness.simulation.spawnCharacter(1, victim); |
| 562 | const attackerId = harness.simulation.spawnCharacter(2, attacker); |
| 563 | assert.equal(harness.simulation.positionOf(victimId).dimensionId, CAVE_ID); |
| 564 | assert.equal(harness.simulation.positionOf(attackerId).dimensionId, CAVE_ID); |
| 565 | |
| 566 | harness.simulation.enqueue(1, { |
| 567 | type: "attack", |
| 568 | characterId: victim.character_id, |
| 569 | targetEntityId: attackerId |
| 570 | }); |
| 571 | harness.simulation.step(); |
| 572 | assert.equal(harness.simulation.beginCombatLogout(victim.character_id), true); |
| 573 | assert.equal( |
| 574 | harness.simulation.combatLogouts.get(victim.character_id).pendingCowardice, |
| 575 | true |
| 576 | ); |
| 577 | |
| 578 | const worker = workerOf(harness.simulation, victimId); |
| 579 | worker.world.ecs.get(worker.world.ecs.handleOf(victimId), Component.health).current = 1; |
| 580 | const deathEvents = applyDamageAndKill(worker.world, attackerId, victimId, 999, harness.now()); |
| 581 | const died = deathEvents.find((event) => event.type === "player-died"); |
| 582 | assert.ok(died, JSON.stringify(deathEvents.map((e) => e.type))); |
| 583 | const applied = harness.simulation.applyPlayerDiedEvent(died); |
| 584 | |
| 585 | const loot = applied.filter((event) => |
| 586 | event.type === "ground-item-appear" |
| 587 | && (event.stack?.itemId === ItemId.bones || event.stack?.itemId === ItemId.skull) |
| 588 | ); |
| 589 | assert.ok(loot.length >= 2, JSON.stringify(applied.filter((e) => e.type === "ground-item-appear"))); |
| 590 | for (const drop of loot) |
| 591 | { assert.equal(drop.dimensionId, CAVE_ID); } |
| 592 | const onGround = [...worker.world.groundItems.values()].filter( |
| 593 | (item) => item.dimensionId === CAVE_ID |
| 594 | && (item.stack?.itemId === ItemId.bones || item.stack?.itemId === ItemId.skull) |
| 595 | ); |
| 596 | assert.ok(onGround.length >= 2); |
| 597 | assert.equal( |
| 598 | [...worker.world.groundItems.values()].filter( |
| 599 | (item) => item.dimensionId === OVERWORLD_ID |
| 600 | && (item.stack?.itemId === ItemId.bones || item.stack?.itemId === ItemId.skull) |
| 601 | ).length, |
| 602 | 0 |
| 603 | ); |
| 604 | |
| 605 | const despawn = applied.find((event) => |
| 606 | event.type === "entity-despawn" && event.entityId === victimId && event.cause === "death" |
| 607 | ); |
| 608 | assert.ok(despawn); |
| 609 | assert.equal(despawn.dimensionId, CAVE_ID); |
| 610 | |
| 611 | const letters = harness.stateDb.listHeldCourierDeliveries(victim.character_id) |
| 612 | .map((hold) => hold.cargo?.stack) |
| 613 | .filter((stack) => stack?.itemId === ItemId.letter); |
| 614 | assert.ok(letters.some((stack) => |
| 615 | stack.instance?.authorDisplayName === CHURCH_LETTER_AUTHOR |
| 616 | && stack.instance?.body === formatChurchOfflineResurrectionBody() |
| 617 | )); |
| 618 | // Church letter should originate from the cave altar (same dim), not the farther overworld one. |
| 619 | const churchHold = harness.stateDb.listHeldCourierDeliveries(victim.character_id) |
| 620 | .find((hold) => hold.cargo?.stack?.instance?.authorDisplayName === CHURCH_LETTER_AUTHOR); |
| 621 | assert.ok(churchHold); |
| 622 | assert.equal(churchHold.originDimensionId, CAVE_ID); |
| 623 | |
| 624 | assert.equal(harness.simulation.isCharacterInWorld(victim.character_id), false); |
| 625 | assert.equal(harness.stateDb.getCharacter(victim.character_id).is_dead, 0); |
| 626 | const row = harness.stateDb.getCharacter(victim.character_id); |
| 627 | // Respawn prefers death-dimension altar at (10,8). |
| 628 | assert.equal(row.dimension_id, CAVE_ID); |
| 629 | assert.equal( |
| 630 | Math.max(Math.abs(row.tile_x - 10), Math.abs(row.tile_y - 8)), |
| 631 | 1 |
| 632 | ); |
| 633 | } |
| 634 | finally |
| 635 | { harness.close(); } |
| 636 | }); |
| 637 | |
| 638 | test("PvP combat-logout quiet despawn in cave still carries cave dimensionId", () => |
| 639 | { |
| 640 | const harness = makeCaveHarness(); |
| 641 | try |
| 642 | { |
| 643 | const victim = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Quiet", lastName: "Vic" }); |
| 644 | harness.stateDb.saveCharacterPosition(victim.character_id, 8, 8, "walk", CAVE_ID); |
| 645 | const attacker = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8, firstName: "Quiet", lastName: "Atk" }); |
| 646 | harness.stateDb.saveCharacterPosition(attacker.character_id, 9, 8, "walk", CAVE_ID); |
| 647 | const victimId = harness.simulation.spawnCharacter(1, victim); |
| 648 | const attackerId = harness.simulation.spawnCharacter(2, attacker); |
| 649 | harness.simulation.enqueue(1, { |
| 650 | type: "attack", |
| 651 | characterId: victim.character_id, |
| 652 | targetEntityId: attackerId |
| 653 | }); |
| 654 | harness.simulation.step(); |
| 655 | assert.equal(harness.simulation.beginCombatLogout(victim.character_id), true); |
| 656 | harness.simulation.drainReplication(); |
| 657 | |
| 658 | const record = harness.simulation.entityRecord(victimId); |
| 659 | harness.simulation.workers[record.workerId].call({ |
| 660 | type: WorkerMessage.clearCombat, |
| 661 | entityId: victimId, |
| 662 | targetEntityId: victimId |
| 663 | }); |
| 664 | const track = harness.simulation.combatTracking.get(victim.character_id); |
| 665 | track.lastCombatActivityAt = harness.now() - COMBAT_LOGOUT_MS - 1; |
| 666 | track.wasInCombat = false; |
| 667 | harness.simulation.combatLogouts.get(victim.character_id).startedAt = harness.now() - COMBAT_LOGOUT_MS - 1; |
| 668 | |
| 669 | const replication = []; |
| 670 | for (let i = 0; i < 60; i++) |
| 671 | { |
| 672 | harness.advance(50); |
| 673 | const stepped = harness.simulation.step(); |
| 674 | replication.push(...(stepped.replication ?? [])); |
| 675 | replication.push(...harness.simulation.drainReplication()); |
| 676 | } |
| 677 | assert.equal(harness.simulation.isCharacterInWorld(victim.character_id), false); |
| 678 | const despawn = replication.find((event) => |
| 679 | event.type === "entity-despawn" && event.entityId === victimId |
| 680 | ); |
| 681 | assert.ok(despawn); |
| 682 | assert.equal(despawn.dimensionId, CAVE_ID); |
| 683 | const keys = eventInterestKeys(despawn, () => null); |
| 684 | assert.equal(keys.dimensionId, CAVE_ID); |
| 685 | assert.equal(keys.toKey, chunkKey(tileToChunk(8), tileToChunk(8), CAVE_ID)); |
| 686 | } |
| 687 | finally |
| 688 | { harness.close(); } |
| 689 | }); |