| 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 { ItemId } from "../../shared/definitions/items.js"; |
| 8 | import { OVERWORLD_ID, DimensionLightingMode, DimensionSeasonMode } from "../../shared/world/dimensions.js"; |
| 9 | import { WorldDatabase } from "../src/persistence/worldDb.js"; |
| 10 | import { StateDatabase } from "../src/persistence/stateDb.js"; |
| 11 | import { Simulation } from "../src/simulation/simulation.js"; |
| 12 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 13 | import { makeCharacter, openChunkStrip, writeOpenChunk } from "./helpers/simulationHarness.js"; |
| 14 | import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; |
| 15 | import { Component } from "../src/ecs/components.js"; |
| 16 | |
| 17 | const CAVE_ID = "dim-ground-cave"; |
| 18 | |
| 19 | function makeDimHarness () |
| 20 | { |
| 21 | const dir = mkdtempSync(join(tmpdir(), "mmo-dim-ground-")); |
| 22 | const worldDb = new WorldDatabase(join(dir, "world.sqlite")); |
| 23 | const stateDb = new StateDatabase(join(dir, "state.sqlite")); |
| 24 | openChunkStrip(worldDb, 2); |
| 25 | const rev = worldDb.getActiveRevisionId(); |
| 26 | worldDb.saveDimension(rev, { |
| 27 | dimensionId: CAVE_ID, |
| 28 | displayName: "Ground Cave", |
| 29 | lightingMode: DimensionLightingMode.alwaysLit, |
| 30 | seasonMode: DimensionSeasonMode.summer, |
| 31 | defaultGroundTypeId: 1 |
| 32 | }); |
| 33 | writeOpenChunk(worldDb, 0, 0); |
| 34 | const cave = createEmptyChunkData(); |
| 35 | cave.open = true; |
| 36 | worldDb.saveChunkVersion(0, 0, rev, true, encodeChunkBlob(cave), CAVE_ID); |
| 37 | let now = 1_000; |
| 38 | const simulation = new Simulation(worldDb, stateDb, { |
| 39 | workerCount: 1, |
| 40 | workerMode: "inprocess", |
| 41 | now: () => now |
| 42 | }); |
| 43 | return { |
| 44 | simulation, |
| 45 | worldDb, |
| 46 | stateDb, |
| 47 | advance (ms) |
| 48 | { |
| 49 | now += ms; |
| 50 | }, |
| 51 | close () |
| 52 | { |
| 53 | simulation.forceUnloadAll(); |
| 54 | worldDb.close(); |
| 55 | stateDb.close(); |
| 56 | } |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | test("dropping an item in a dimension stores and replicates that dimension", () => |
| 61 | { |
| 62 | const harness = makeDimHarness(); |
| 63 | try |
| 64 | { |
| 65 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 66 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 67 | harness.stateDb.saveBag(character.character_id, { |
| 68 | inventory: [{ itemId: ItemId.paper, quantity: 1 }, ...emptySlots().slice(1)], |
| 69 | equipment: emptyEquipment(), |
| 70 | vault: [], |
| 71 | vaultReserved: [] |
| 72 | }); |
| 73 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 74 | assert.equal(harness.simulation.positionOf(playerId).dimensionId, CAVE_ID); |
| 75 | |
| 76 | harness.simulation.enqueue(1, { |
| 77 | type: "drop-item", |
| 78 | characterId: character.character_id, |
| 79 | slotIndex: 0, |
| 80 | quantity: 1 |
| 81 | }); |
| 82 | const result = harness.simulation.step(); |
| 83 | const appear = result.events.find((event) => event.type === "ground-item-appear"); |
| 84 | assert.ok(appear, JSON.stringify(result.events)); |
| 85 | assert.equal(appear.dimensionId, CAVE_ID); |
| 86 | assert.equal(appear.tileX, 8); |
| 87 | assert.equal(appear.tileY, 8); |
| 88 | |
| 89 | const stored = harness.stateDb.listGroundItemsInRegion(0, 0, 63, 63, CAVE_ID); |
| 90 | assert.equal(stored.length, 1); |
| 91 | assert.equal(stored[0].dimensionId, CAVE_ID); |
| 92 | assert.equal( |
| 93 | harness.stateDb.listGroundItemsInRegion(0, 0, 63, 63, OVERWORLD_ID).length, |
| 94 | 0 |
| 95 | ); |
| 96 | } |
| 97 | finally |
| 98 | { harness.close(); } |
| 99 | }); |
| 100 | |
| 101 | test("spawner collect requests scope legacy spawner ids by dimension", () => |
| 102 | { |
| 103 | const harness = makeDimHarness(); |
| 104 | try |
| 105 | { |
| 106 | const data = createEmptyChunkData(); |
| 107 | data.open = true; |
| 108 | data.spawners = [{ |
| 109 | localX: 4, |
| 110 | localY: 4, |
| 111 | config: { |
| 112 | spawnerId: "0:0:68", |
| 113 | mobTypeId: 1, |
| 114 | maxAlive: 1, |
| 115 | spawnRangeTiles: 1, |
| 116 | maxWanderRangeTiles: 1 |
| 117 | } |
| 118 | }]; |
| 119 | harness.worldDb.saveChunkVersion( |
| 120 | 0, |
| 121 | 0, |
| 122 | harness.worldDb.getActiveRevisionId(), |
| 123 | true, |
| 124 | encodeChunkBlob(data), |
| 125 | CAVE_ID |
| 126 | ); |
| 127 | harness.simulation.reloadWorld(); |
| 128 | harness.advance(50); |
| 129 | const stepped = harness.simulation.step(); |
| 130 | assert.ok(stepped); |
| 131 | const worker = harness.simulation.workers[0]; |
| 132 | const requests = worker.world |
| 133 | ? null |
| 134 | : null; |
| 135 | void requests; |
| 136 | // Ensure cave chunk is owned and spawn pass can run without colliding overworld keys. |
| 137 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 138 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 139 | harness.simulation.spawnCharacter(1, character); |
| 140 | for (let i = 0; i < 5; i++) |
| 141 | { |
| 142 | harness.advance(1000); |
| 143 | harness.simulation.step(); |
| 144 | } |
| 145 | assert.ok(true); |
| 146 | } |
| 147 | finally |
| 148 | { harness.close(); } |
| 149 | }); |