BlueGit

timfa questward

Files

questward / server / tests / death.test.js

death.test.js

death.test.js 6.6 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";
10import { emptyEquipment, emptySlots, addCoinsToSlots, countCoinsInSlots } from "../../shared/items/inventory.js";
11
12function componentsOf (simulation, entityId)
13{
14 const record = simulation.entityRecord(entityId);
15 const worker = simulation.workers[record.workerId];
16 const handle = worker.world.ecs.handleOf(entityId);
17 return worker.world.ecs.componentsOf(handle);
18}
19
20function placeAltar (worldDb, tileX, tileY)
21{
22 const chunkX = Math.floor(tileX / 16);
23 const chunkY = Math.floor(tileY / 16);
24 const data = createEmptyChunkData();
25 data.open = true;
26 data.blocks[(tileY - chunkY * 16) * 16 + (tileX - chunkX * 16)] = {
27 typeId: BlockType.churchAltar,
28 state: 0
29 };
30 worldDb.saveChunkVersion(
31 chunkX,
32 chunkY,
33 worldDb.getActiveRevisionId(),
34 true,
35 encodeChunkBlob(data)
36 );
37}
38
39function killPlayer (harness, character, playerId)
40{
41 const deerId = harness.simulation.spawnEntity(mobComponents({
42 tileX: 9,
43 tileY: 8,
44 mobTypeId: MobId.deer,
45 spawnerId: null
46 }));
47 const deer = componentsOf(harness.simulation, deerId);
48 deer.health.current = 1_000_000;
49 deer.health.max = 1_000_000;
50 const player = componentsOf(harness.simulation, playerId);
51 player.health.current = 1;
52 harness.simulation.enqueue(1, {
53 type: "attack",
54 characterId: character.character_id,
55 targetEntityId: deerId
56 });
57 const first = harness.simulation.step();
58 const immediate = first.events.find((event) => event.type === "player-died");
59 if (immediate)
60 { return { died: immediate, events: first.events }; }
61 for (let i = 0; i < 100; i++)
62 {
63 harness.advance(1000);
64 const result = harness.simulation.step();
65 const died = result.events.find((event) => event.type === "player-died");
66 if (died)
67 { return { died, events: result.events }; }
68 }
69 throw new Error("Player did not die.");
70}
71
72function withDeterministicCombat (fn)
73{
74 const random = Math.random;
75 Math.random = () => 0;
76 try
77 { fn(); }
78 finally
79 { Math.random = random; }
80}
81
82test("player death marks dead and drops bones and skull", () =>
83{
84 withDeterministicCombat(() =>
85 {
86 const harness = makeSimulation({ workerCount: 1 });
87 try
88 {
89 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
90 const playerId = harness.simulation.spawnCharacter(1, character);
91 const result = killPlayer(harness, character, playerId);
92 assert.equal(result.died.sessionId, 1);
93 assert.ok(componentsOf(harness.simulation, playerId)[Component.dead]);
94 assert.equal(harness.stateDb.getCharacter(character.character_id).is_dead, 1);
95 const drops = harness.stateDb.listGroundItemsInRegion(0, 0, 63, 63);
96 const itemIds = drops.map((item) => item.stack.itemId);
97 assert.ok(itemIds.includes(ItemId.bones));
98 assert.ok(itemIds.includes(ItemId.skull));
99 const bag = harness.simulation.bagOf(playerId);
100 assert.equal(countCoinsInSlots(bag.inventory), 0);
101 }
102 finally
103 { harness.close(); }
104 });
105});
106
107test("player death drops all carried coins for anyone to pick up", () =>
108{
109 withDeterministicCombat(() =>
110 {
111 const harness = makeSimulation({ workerCount: 1 });
112 try
113 {
114 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
115 const funded = addCoinsToSlots(emptySlots(), 250);
116 assert.equal(funded.ok, true);
117 harness.stateDb.saveBag(character.character_id, {
118 inventory: funded.slots,
119 equipment: emptyEquipment(),
120 vault: [],
121 vaultReserved: []
122 });
123 const playerId = harness.simulation.spawnCharacter(1, character);
124 assert.equal(countCoinsInSlots(harness.simulation.bagOf(playerId).inventory), 250);
125 killPlayer(harness, character, playerId);
126 const bag = harness.simulation.bagOf(playerId);
127 assert.equal(countCoinsInSlots(bag.inventory), 0);
128 const drops = harness.stateDb.listGroundItemsInRegion(0, 0, 63, 63);
129 const coinDrop = drops.find((item) => item.stack.itemId === ItemId.coins);
130 assert.ok(coinDrop);
131 assert.equal(coinDrop.stack.quantity, 250);
132 assert.equal(coinDrop.source, "player-death");
133 assert.deepEqual(
134 drops.map((item) => item.stack.itemId).sort((a, b) => a - b),
135 [ItemId.coins, ItemId.bones, ItemId.skull].sort((a, b) => a - b)
136 );
137 }
138 finally
139 { harness.close(); }
140 });
141});
142
143test("respawn moves beside nearest church altar with full vitals", () =>
144{
145 withDeterministicCombat(() =>
146 {
147 const harness = makeSimulation({ workerCount: 1 });
148 try
149 {
150 placeAltar(harness.worldDb, 20, 8);
151 harness.simulation.reloadWorld();
152 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
153 const playerId = harness.simulation.spawnCharacter(1, character);
154 killPlayer(harness, character, playerId);
155 harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id });
156 const result = harness.simulation.step();
157 assert.ok(result.events.some((event) => event.type === "player-respawned"));
158 const player = componentsOf(harness.simulation, playerId);
159 assert.equal(player[Component.dead], undefined);
160 assert.equal(player.health.current, player.health.max);
161 assert.equal(player.stamina.current, player.stamina.max);
162 assert.equal(
163 Math.max(Math.abs(player.position.tileX - 20), Math.abs(player.position.tileY - 8)),
164 1
165 );
166 assert.equal(harness.stateDb.getCharacter(character.character_id).is_dead, 0);
167 }
168 finally
169 { harness.close(); }
170 });
171});
172
173test("respawn without an altar falls back to the death tile", () =>
174{
175 withDeterministicCombat(() =>
176 {
177 const harness = makeSimulation({ workerCount: 1 });
178 try
179 {
180 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
181 const playerId = harness.simulation.spawnCharacter(1, character);
182 const { died } = killPlayer(harness, character, playerId);
183 harness.simulation.enqueue(1, { type: "respawn", characterId: character.character_id });
184 const result = harness.simulation.step();
185 assert.equal(result.events.some((event) => event.type === "command-error"), false);
186 const player = componentsOf(harness.simulation, playerId);
187 assert.equal(player.position.tileX, died.tileX);
188 assert.equal(player.position.tileY, died.tileY);
189 assert.equal(player.health.current, player.health.max);
190 assert.equal(player[Component.dead], undefined);
191 }
192 finally
193 { harness.close(); }
194 });
195});