import test from "node:test"; import assert from "node:assert/strict"; import { BlockType } from "../../shared/definitions/tiles.js"; import { ItemId } from "../../shared/definitions/items.js"; import { Component } from "../src/ecs/components.js"; import { MobId } from "../../shared/definitions/mobs.js"; import { mobComponents } from "../src/simulation/spawners.js"; import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; function componentsOf (simulation, entityId) { const record = simulation.entityRecord(entityId); const worker = simulation.workers[record.workerId]; const handle = worker.world.ecs.handleOf(entityId); return worker.world.ecs.componentsOf(handle); } function placeAltar (worldDb, tileX, tileY) { const chunkX = Math.floor(tileX / 16); const chunkY = Math.floor(tileY / 16); const data = createEmptyChunkData(); data.open = true; data.blocks[(tileY - chunkY * 16) * 16 + (tileX - chunkX * 16)] = { typeId: BlockType.churchAltar, state: 0 }; worldDb.saveChunkVersion( chunkX, chunkY, worldDb.getActiveRevisionId(), true, encodeChunkBlob(data) ); } function killPlayer (harness, character, playerId) { const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.deer, spawnerId: null })); const deer = componentsOf(harness.simulation, deerId); deer.health.current = 1_000_000; deer.health.max = 1_000_000; const player = componentsOf(harness.simulation, playerId); player.health.current = 1; harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: deerId }); const first = harness.simulation.step(); const immediate = first.events.find((event) => event.type === "player-died"); if (immediate) { return { died: immediate, events: first.events }; } for (let i = 0; i < 100; i++) { harness.advance(1000); const result = harness.simulation.step(); const died = result.events.find((event) => event.type === "player-died"); if (died) { return { died, events: result.events }; } } throw new Error("Player did not die."); } function withDeterministicCombat (fn) { const random = Math.random; Math.random = () => 0; try { fn(); } finally { Math.random = random; } } test("player death marks dead and drops bones and skull", () => { withDeterministicCombat(() => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const result = killPlayer(harness, character, playerId); assert.equal(result.died.sessionId, 1); assert.ok(componentsOf(harness.simulation, playerId)[Component.dead]); assert.equal(harness.stateDb.getCharacter(character.character_id).is_dead, 1); const drops = harness.stateDb.listGroundItemsInRegion(0, 0, 63, 63); assert.deepEqual( drops.map((item) => item.stack.itemId).sort((a, b) => a - b), [ItemId.bones, ItemId.skull] ); } finally { harness.close(); } }); }); test("respawn moves beside nearest church altar with full vitals", () => { withDeterministicCombat(() => { const harness = makeSimulation({ workerCount: 1 }); try { placeAltar(harness.worldDb, 20, 8); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); killPlayer(harness, character, playerId); harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id }); const result = harness.simulation.step(); assert.ok(result.events.some((event) => event.type === "player-respawned")); const player = componentsOf(harness.simulation, playerId); assert.equal(player[Component.dead], undefined); assert.equal(player.health.current, player.health.max); assert.equal(player.stamina.current, player.stamina.max); assert.equal( Math.max(Math.abs(player.position.tileX - 20), Math.abs(player.position.tileY - 8)), 1 ); assert.equal(harness.stateDb.getCharacter(character.character_id).is_dead, 0); } finally { harness.close(); } }); }); test("respawn without an altar falls back to the death tile", () => { withDeterministicCombat(() => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const { died } = killPlayer(harness, character, playerId); harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id }); const result = harness.simulation.step(); assert.equal(result.events.some((event) => event.type === "command-error"), false); const player = componentsOf(harness.simulation, playerId); assert.equal(player.position.tileX, died.tileX); assert.equal(player.position.tileY, died.tileY); assert.equal(player.health.current, player.health.max); assert.equal(player[Component.dead], undefined); } finally { harness.close(); } }); });