BlueGit

timfa questward

Files

questward / server / tests / death.test.js

death.test.js

death.test.js 5.1 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { BlockType } from "../../shared/definitions/tiles.js";
4import { ItemId } from "../../shared/definitions/items.js";
5import { Component } from "../src/ecs/components.js";
6import { MobId } from "../../shared/definitions/mobs.js";
7import { mobComponents } from "../src/simulation/spawners.js";
8import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
9import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
10
11function componentsOf (simulation, entityId)
12{
13 const record = simulation.entityRecord(entityId);
14 const worker = simulation.workers[record.workerId];
15 const handle = worker.world.ecs.handleOf(entityId);
16 return worker.world.ecs.componentsOf(handle);
17}
18
19function placeAltar (worldDb, tileX, tileY)
20{
21 const chunkX = Math.floor(tileX / 16);
22 const chunkY = Math.floor(tileY / 16);
23 const data = createEmptyChunkData();
24 data.open = true;
25 data.blocks[(tileY - chunkY * 16) * 16 + (tileX - chunkX * 16)] = {
26 typeId: BlockType.churchAltar,
27 state: 0
28 };
29 worldDb.saveChunkVersion(
30 chunkX,
31 chunkY,
32 worldDb.getActiveRevisionId(),
33 true,
34 encodeChunkBlob(data)
35 );
36}
37
38function killPlayer (harness, character, playerId)
39{
40 const deerId = harness.simulation.spawnEntity(mobComponents({
41 tileX: 9,
42 tileY: 8,
43 mobTypeId: MobId.deer,
44 spawnerId: null
45 }));
46 const deer = componentsOf(harness.simulation, deerId);
47 deer.health.current = 1_000_000;
48 deer.health.max = 1_000_000;
49 const player = componentsOf(harness.simulation, playerId);
50 player.health.current = 1;
51 harness.simulation.enqueue(1, {
52 type: "attack",
53 characterId: character.character_id,
54 targetEntityId: deerId
55 });
56 const first = harness.simulation.step();
57 const immediate = first.events.find((event) => event.type === "player-died");
58 if (immediate)
59 { return { died: immediate, events: first.events }; }
60 for (let i = 0; i < 100; i++)
61 {
62 harness.advance(1000);
63 const result = harness.simulation.step();
64 const died = result.events.find((event) => event.type === "player-died");
65 if (died)
66 { return { died, events: result.events }; }
67 }
68 throw new Error("Player did not die.");
69}
70
71function withDeterministicCombat (fn)
72{
73 const random = Math.random;
74 Math.random = () => 0;
75 try
76 { fn(); }
77 finally
78 { Math.random = random; }
79}
80
81test("player death marks dead and drops bones and skull", () =>
82{
83 withDeterministicCombat(() =>
84 {
85 const harness = makeSimulation({ workerCount: 1 });
86 try
87 {
88 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
89 const playerId = harness.simulation.spawnCharacter(1, character);
90 const result = killPlayer(harness, character, playerId);
91 assert.equal(result.died.sessionId, 1);
92 assert.ok(componentsOf(harness.simulation, playerId)[Component.dead]);
93 assert.equal(harness.stateDb.getCharacter(character.character_id).is_dead, 1);
94 const drops = harness.stateDb.listGroundItemsInRegion(0, 0, 63, 63);
95 assert.deepEqual(
96 drops.map((item) => item.stack.itemId).sort((a, b) => a - b),
97 [ItemId.bones, ItemId.skull]
98 );
99 }
100 finally
101 { harness.close(); }
102 });
103});
104
105test("respawn moves beside nearest church altar with full vitals", () =>
106{
107 withDeterministicCombat(() =>
108 {
109 const harness = makeSimulation({ workerCount: 1 });
110 try
111 {
112 placeAltar(harness.worldDb, 20, 8);
113 harness.simulation.reloadWorld();
114 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
115 const playerId = harness.simulation.spawnCharacter(1, character);
116 killPlayer(harness, character, playerId);
117 harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id });
118 const result = harness.simulation.step();
119 assert.ok(result.events.some((event) => event.type === "player-respawned"));
120 const player = componentsOf(harness.simulation, playerId);
121 assert.equal(player[Component.dead], undefined);
122 assert.equal(player.health.current, player.health.max);
123 assert.equal(player.stamina.current, player.stamina.max);
124 assert.equal(
125 Math.max(Math.abs(player.position.tileX - 20), Math.abs(player.position.tileY - 8)),
126 1
127 );
128 assert.equal(harness.stateDb.getCharacter(character.character_id).is_dead, 0);
129 }
130 finally
131 { harness.close(); }
132 });
133});
134
135test("respawn without an altar falls back to the death tile", () =>
136{
137 withDeterministicCombat(() =>
138 {
139 const harness = makeSimulation({ workerCount: 1 });
140 try
141 {
142 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
143 const playerId = harness.simulation.spawnCharacter(1, character);
144 const { died } = killPlayer(harness, character, playerId);
145 harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id });
146 const result = harness.simulation.step();
147 assert.equal(result.events.some((event) => event.type === "command-error"), false);
148 const player = componentsOf(harness.simulation, playerId);
149 assert.equal(player.position.tileX, died.tileX);
150 assert.equal(player.position.tileY, died.tileY);
151 assert.equal(player.health.current, player.health.max);
152 assert.equal(player[Component.dead], undefined);
153 }
154 finally
155 { harness.close(); }
156 });
157});