| 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 | }); |
| 150 | |
| 151 | test("pickup rejects when player is in a different dimension than the item", () => |
| 152 | { |
| 153 | const harness = makeDimHarness(); |
| 154 | try |
| 155 | { |
| 156 | const caveChar = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, lastName: "CaveDrop" }); |
| 157 | harness.stateDb.saveCharacterPosition(caveChar.character_id, 8, 8, "walk", CAVE_ID); |
| 158 | harness.stateDb.saveBag(caveChar.character_id, { |
| 159 | inventory: [{ itemId: ItemId.paper, quantity: 1 }, ...emptySlots().slice(1)], |
| 160 | equipment: emptyEquipment(), |
| 161 | vault: [], |
| 162 | vaultReserved: [] |
| 163 | }); |
| 164 | const caveId = harness.simulation.spawnCharacter(2, caveChar); |
| 165 | assert.equal(harness.simulation.positionOf(caveId).dimensionId, CAVE_ID); |
| 166 | |
| 167 | harness.simulation.enqueue(2, { |
| 168 | type: "drop-item", |
| 169 | characterId: caveChar.character_id, |
| 170 | slotIndex: 0, |
| 171 | quantity: 1 |
| 172 | }); |
| 173 | const dropped = harness.simulation.step(); |
| 174 | const appear = dropped.events.find((event) => event.type === "ground-item-appear"); |
| 175 | assert.ok(appear); |
| 176 | assert.equal(appear.dimensionId, CAVE_ID); |
| 177 | const groundItemId = appear.groundItemId; |
| 178 | |
| 179 | const overworld = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, lastName: "OwPickup" }); |
| 180 | harness.stateDb.saveBag(overworld.character_id, { |
| 181 | inventory: emptySlots(), |
| 182 | equipment: emptyEquipment(), |
| 183 | vault: [], |
| 184 | vaultReserved: [] |
| 185 | }); |
| 186 | const overId = harness.simulation.spawnCharacter(1, overworld); |
| 187 | assert.equal(harness.simulation.positionOf(overId).dimensionId, OVERWORLD_ID); |
| 188 | |
| 189 | harness.simulation.enqueue(1, { |
| 190 | type: "pickup-item", |
| 191 | characterId: overworld.character_id, |
| 192 | groundItemId |
| 193 | }); |
| 194 | const result = harness.simulation.step(); |
| 195 | const err = result.events.find((event) => event.type === "command-error"); |
| 196 | assert.ok(err, JSON.stringify(result.events)); |
| 197 | assert.match(err.message, /stand on the item/i); |
| 198 | assert.equal( |
| 199 | harness.stateDb.listGroundItemsInRegion(0, 0, 63, 63, CAVE_ID).length, |
| 200 | 1, |
| 201 | "cave ground item must remain" |
| 202 | ); |
| 203 | } |
| 204 | finally |
| 205 | { harness.close(); } |
| 206 | }); |