| 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 { OVERWORLD_ID, DimensionLightingMode, DimensionSeasonMode } from "../../shared/world/dimensions.js"; |
| 8 | import { WorldDatabase } from "../src/persistence/worldDb.js"; |
| 9 | import { StateDatabase } from "../src/persistence/stateDb.js"; |
| 10 | import { Simulation } from "../src/simulation/simulation.js"; |
| 11 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 12 | import { Component } from "../src/ecs/components.js"; |
| 13 | import { makeCharacter, openChunkStrip, writeOpenChunk } from "./helpers/simulationHarness.js"; |
| 14 | |
| 15 | const CAVE_ID = "altar-cave"; |
| 16 | |
| 17 | function placeAltar (worldDb, tileX, tileY, dimensionId = OVERWORLD_ID) |
| 18 | { |
| 19 | const chunkX = Math.floor(tileX / 16); |
| 20 | const chunkY = Math.floor(tileY / 16); |
| 21 | const data = createEmptyChunkData(); |
| 22 | data.open = true; |
| 23 | data.blocks[(tileY - chunkY * 16) * 16 + (tileX - chunkX * 16)] = { |
| 24 | typeId: BlockType.churchAltar, |
| 25 | state: 0 |
| 26 | }; |
| 27 | worldDb.saveChunkVersion( |
| 28 | chunkX, |
| 29 | chunkY, |
| 30 | worldDb.getActiveRevisionId(), |
| 31 | true, |
| 32 | encodeChunkBlob(data), |
| 33 | dimensionId |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | function makeAltarHarness () |
| 38 | { |
| 39 | const dir = mkdtempSync(join(tmpdir(), "mmo-altar-respawn-")); |
| 40 | const worldDb = new WorldDatabase(join(dir, "world.sqlite")); |
| 41 | const stateDb = new StateDatabase(join(dir, "state.sqlite")); |
| 42 | openChunkStrip(worldDb, 2); |
| 43 | const rev = worldDb.getActiveRevisionId(); |
| 44 | worldDb.saveDimension(rev, { |
| 45 | dimensionId: CAVE_ID, |
| 46 | displayName: "Altar Cave", |
| 47 | lightingMode: DimensionLightingMode.alwaysLit, |
| 48 | seasonMode: DimensionSeasonMode.summer, |
| 49 | defaultGroundTypeId: 1 |
| 50 | }); |
| 51 | writeOpenChunk(worldDb, 0, 0); |
| 52 | const cave = createEmptyChunkData(); |
| 53 | cave.open = true; |
| 54 | worldDb.saveChunkVersion(0, 0, rev, true, encodeChunkBlob(cave), CAVE_ID); |
| 55 | placeAltar(worldDb, 20, 8, OVERWORLD_ID); |
| 56 | let now = 1_000; |
| 57 | const simulation = new Simulation(worldDb, stateDb, { |
| 58 | workerCount: 1, |
| 59 | workerMode: "inprocess", |
| 60 | now: () => now |
| 61 | }); |
| 62 | return { |
| 63 | simulation, |
| 64 | worldDb, |
| 65 | stateDb, |
| 66 | advance (ms) |
| 67 | { |
| 68 | now += ms; |
| 69 | }, |
| 70 | close () |
| 71 | { |
| 72 | simulation.forceUnloadAll(); |
| 73 | worldDb.close(); |
| 74 | stateDb.close(); |
| 75 | } |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | function markDead (simulation, stateDb, characterId, playerId) |
| 80 | { |
| 81 | const record = simulation.entityRecord(playerId); |
| 82 | const worker = simulation.workers[record.workerId]; |
| 83 | const handle = worker.world.ecs.handleOf(playerId); |
| 84 | if (!worker.world.ecs.has(handle, Component.dead)) |
| 85 | { worker.world.ecs.add(handle, Component.dead, {}); } |
| 86 | stateDb.setCharacterDead(characterId, true); |
| 87 | } |
| 88 | |
| 89 | function componentsOf (simulation, entityId) |
| 90 | { |
| 91 | const record = simulation.entityRecord(entityId); |
| 92 | const worker = simulation.workers[record.workerId]; |
| 93 | const handle = worker.world.ecs.handleOf(entityId); |
| 94 | return worker.world.ecs.componentsOf(handle); |
| 95 | } |
| 96 | |
| 97 | test("pray at altar sets the bound respawn point and toasts", () => |
| 98 | { |
| 99 | const harness = makeAltarHarness(); |
| 100 | try |
| 101 | { |
| 102 | const character = makeCharacter(harness.stateDb, { tileX: 19, tileY: 8 }); |
| 103 | harness.simulation.spawnCharacter(1, character); |
| 104 | harness.simulation.enqueue(1, { |
| 105 | type: "pray-at-altar", |
| 106 | characterId: character.character_id, |
| 107 | tileX: 20, |
| 108 | tileY: 8 |
| 109 | }); |
| 110 | const result = harness.simulation.step(); |
| 111 | assert.ok(result.events.some((event) => |
| 112 | event.type === "toast" |
| 113 | && event.message === "Altar set as respawn point." |
| 114 | )); |
| 115 | assert.deepEqual(harness.stateDb.getCharacterRespawnAltar(character.character_id), { |
| 116 | tileX: 20, |
| 117 | tileY: 8, |
| 118 | dimensionId: OVERWORLD_ID |
| 119 | }); |
| 120 | } |
| 121 | finally |
| 122 | { harness.close(); } |
| 123 | }); |
| 124 | |
| 125 | test("death in a dimension without altars respawns at the bound altar", () => |
| 126 | { |
| 127 | const harness = makeAltarHarness(); |
| 128 | try |
| 129 | { |
| 130 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 131 | harness.stateDb.setCharacterRespawnAltar(character.character_id, 20, 8, OVERWORLD_ID); |
| 132 | harness.stateDb.saveCharacterPosition(character.character_id, 8, 8, "walk", CAVE_ID); |
| 133 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 134 | assert.equal(harness.simulation.positionOf(playerId).dimensionId, CAVE_ID); |
| 135 | markDead(harness.simulation, harness.stateDb, character.character_id, playerId); |
| 136 | |
| 137 | harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id }); |
| 138 | const result = harness.simulation.step(); |
| 139 | assert.ok(result.events.some((event) => event.type === "player-respawned")); |
| 140 | assert.ok(result.events.some((event) => |
| 141 | event.type === "dimension-changed" && event.dimensionId === OVERWORLD_ID |
| 142 | )); |
| 143 | const player = componentsOf(harness.simulation, playerId); |
| 144 | assert.equal(player.position.dimensionId, OVERWORLD_ID); |
| 145 | assert.equal( |
| 146 | Math.max(Math.abs(player.position.tileX - 20), Math.abs(player.position.tileY - 8)), |
| 147 | 1 |
| 148 | ); |
| 149 | assert.deepEqual(harness.stateDb.getCharacterRespawnAltar(character.character_id), { |
| 150 | tileX: 20, |
| 151 | tileY: 8, |
| 152 | dimensionId: OVERWORLD_ID |
| 153 | }); |
| 154 | } |
| 155 | finally |
| 156 | { harness.close(); } |
| 157 | }); |
| 158 | |
| 159 | test("respawn beside an altar remembers that altar for later deaths", () => |
| 160 | { |
| 161 | const harness = makeAltarHarness(); |
| 162 | try |
| 163 | { |
| 164 | const character = makeCharacter(harness.stateDb, { tileX: 19, tileY: 8 }); |
| 165 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 166 | markDead(harness.simulation, harness.stateDb, character.character_id, playerId); |
| 167 | harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id }); |
| 168 | harness.simulation.step(); |
| 169 | assert.deepEqual(harness.stateDb.getCharacterRespawnAltar(character.character_id), { |
| 170 | tileX: 20, |
| 171 | tileY: 8, |
| 172 | dimensionId: OVERWORLD_ID |
| 173 | }); |
| 174 | } |
| 175 | finally |
| 176 | { harness.close(); } |
| 177 | }); |