| 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 } from "../src/combat/service.js"; |
| 16 | import { Component } from "../src/ecs/components.js"; |
| 17 | import { makeCharacter, openChunkStrip, writeOpenChunk } from "./helpers/simulationHarness.js"; |
| 18 | |
| 19 | const CAVE_ID = "test-cave"; |
| 20 | |
| 21 | function makeCaveHarness () |
| 22 | { |
| 23 | const dir = mkdtempSync(join(tmpdir(), "mmo-dim-interact-")); |
| 24 | const worldDb = new WorldDatabase(join(dir, "world.sqlite")); |
| 25 | const stateDb = new StateDatabase(join(dir, "state.sqlite")); |
| 26 | openChunkStrip(worldDb, 2); |
| 27 | const rev = worldDb.getActiveRevisionId(); |
| 28 | worldDb.saveDimension(rev, { |
| 29 | dimensionId: CAVE_ID, |
| 30 | displayName: "Test Cave", |
| 31 | lightingMode: DimensionLightingMode.alwaysLit, |
| 32 | seasonMode: DimensionSeasonMode.summer, |
| 33 | defaultGroundTypeId: 1 |
| 34 | }); |
| 35 | const cave = createEmptyChunkData(); |
| 36 | cave.open = true; |
| 37 | cave.blocks[8 * 16 + 9] = { |
| 38 | typeId: BlockType.furnace, |
| 39 | state: "furnace", |
| 40 | config: null |
| 41 | }; |
| 42 | worldDb.saveChunkVersion(0, 0, rev, true, encodeChunkBlob(cave), CAVE_ID); |
| 43 | writeOpenChunk(worldDb, 0, 0); |
| 44 | let now = 1_000; |
| 45 | const simulation = new Simulation(worldDb, stateDb, { |
| 46 | workerCount: 1, |
| 47 | workerMode: "inprocess", |
| 48 | now: () => now |
| 49 | }); |
| 50 | return { |
| 51 | simulation, |
| 52 | worldDb, |
| 53 | stateDb, |
| 54 | now () |
| 55 | { |
| 56 | return now; |
| 57 | }, |
| 58 | advance (ms) |
| 59 | { |
| 60 | now += ms; |
| 61 | }, |
| 62 | close () |
| 63 | { |
| 64 | simulation.forceUnloadAll(); |
| 65 | worldDb.close(); |
| 66 | stateDb.close(); |
| 67 | } |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | function workerOf (simulation, entityId) |
| 72 | { |
| 73 | const record = simulation.entityRecord(entityId); |
| 74 | assert.ok(record, "entity record"); |
| 75 | return simulation.workers[record.workerId]; |
| 76 | } |
| 77 | |
| 78 | test("open furnace works in a non-overworld dimension", () => |
| 79 | { |
| 80 | const harness = makeCaveHarness(); |
| 81 | try |
| 82 | { |
| 83 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 84 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 85 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 86 | const position = harness.simulation.positionOf(playerId); |
| 87 | assert.equal(position.dimensionId, CAVE_ID); |
| 88 | |
| 89 | harness.simulation.enqueue(1, { |
| 90 | type: "open-station", |
| 91 | characterId: character.character_id, |
| 92 | stationId: CraftingStationId.furnace |
| 93 | }); |
| 94 | const result = harness.simulation.step(); |
| 95 | assert.ok( |
| 96 | result.events.some((event) => |
| 97 | event.type === "station-open" && event.stationId === CraftingStationId.furnace), |
| 98 | "expected station-open in cave" |
| 99 | ); |
| 100 | } |
| 101 | finally |
| 102 | { harness.close(); } |
| 103 | }); |
| 104 | |
| 105 | test("melee combat ticks in a non-overworld dimension", () => |
| 106 | { |
| 107 | const harness = makeCaveHarness(); |
| 108 | try |
| 109 | { |
| 110 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 111 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 112 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 113 | assert.equal(harness.simulation.positionOf(playerId).dimensionId, CAVE_ID); |
| 114 | |
| 115 | const ratId = harness.simulation.spawnEntity(mobComponents({ |
| 116 | tileX: 8, |
| 117 | tileY: 9, |
| 118 | mobTypeId: MobId.rat, |
| 119 | spawnerId: null, |
| 120 | dimensionId: CAVE_ID |
| 121 | })); |
| 122 | assert.equal(harness.simulation.positionOf(ratId).dimensionId, CAVE_ID); |
| 123 | |
| 124 | const worker = workerOf(harness.simulation, ratId); |
| 125 | const world = worker.world; |
| 126 | assert.ok(world.chunkIsSimulating(8, 9, CAVE_ID)); |
| 127 | assert.equal(world.chunkIsSimulating(8, 9, OVERWORLD_ID), false); |
| 128 | |
| 129 | assert.ok(ensureMobCombat(world.ecs, ratId)); |
| 130 | engageTarget(world.ecs, ratId, playerId, harness.now()); |
| 131 | const combat = world.ecs.get(world.ecs.handleOf(ratId), Component.combat); |
| 132 | combat.nextSwingAt = harness.now(); |
| 133 | |
| 134 | const events = stepCombat(world, harness.now()); |
| 135 | const combatFx = events.find((event) => |
| 136 | event.type === "combat-swing" || event.type === "combat-hit" |
| 137 | || event.type === "combat-miss" || event.type === "combat-block" |
| 138 | ); |
| 139 | assert.ok( |
| 140 | combatFx, |
| 141 | `expected combat resolution in cave, got ${events.map((e) => e.type).join(",") || "none"}` |
| 142 | ); |
| 143 | assert.equal(combatFx.dimensionId, CAVE_ID); |
| 144 | |
| 145 | stepAggression(world, harness.now()); |
| 146 | } |
| 147 | finally |
| 148 | { harness.close(); } |
| 149 | }); |