BlueGit

timfa questward

Files

questward / server / tests / combat.test.js

combat.test.js

combat.test.js 8.7 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { mobComponents } from "../src/simulation/spawners.js";
4import { MobId } from "../../shared/definitions/mobs.js";
5import { ItemId, EQUIPMENT_SLOT } from "../../shared/definitions/items.js";
6import { Component } from "../src/ecs/components.js";
7import { WorkerMessage } from "../src/simulation/protocol.js";
8import { tileToRegion } from "../src/world/coordinates.js";
9import { SkillId } from "../../shared/definitions/skills.js";
10import { resolveMeleeSwing, attackProfileOf } from "../src/combat/service.js";
11import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js";
12
13function workerOf (simulation, entityId)
14{
15 const record = simulation.entityRecord(entityId);
16 assert.ok(record, "entity must be remembered");
17 return simulation.workers[record.workerId];
18}
19
20function componentsOf (simulation, entityId)
21{
22 const worker = workerOf(simulation, entityId);
23 const handle = worker.world.ecs.handleOf(entityId);
24 assert.ok(handle != null);
25 return worker.world.ecs.componentsOf(handle);
26}
27
28test("attack engages both combatants and retaliates", () =>
29{
30 const harness = makeSimulation({ workerCount: 1 });
31 try
32 {
33 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
34 const playerId = harness.simulation.spawnCharacter(1, character);
35 const ratId = harness.simulation.spawnEntity(mobComponents({
36 tileX: 9,
37 tileY: 8,
38 mobTypeId: MobId.rat,
39 spawnerId: null
40 }));
41
42 harness.simulation.enqueue(1, {
43 type: "attack",
44 characterId: character.character_id,
45 targetEntityId: ratId
46 });
47 harness.simulation.step();
48
49 const player = componentsOf(harness.simulation, playerId);
50 const rat = componentsOf(harness.simulation, ratId);
51 assert.equal(player.combat.targetEntityId, ratId);
52 assert.equal(rat.combat.targetEntityId, playerId);
53 }
54 finally
55 { harness.close(); }
56});
57
58test("melee swing only resolves at Chebyshev distance 1", () =>
59{
60 const harness = makeSimulation({ workerCount: 1 });
61 try
62 {
63 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
64 const playerId = harness.simulation.spawnCharacter(1, character);
65 const bag = harness.simulation.bagOf(playerId);
66 const stickSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.stick);
67 assert.ok(stickSlot >= 0);
68 harness.simulation.enqueue(1, {
69 type: "equip-item",
70 characterId: character.character_id,
71 slotIndex: stickSlot
72 });
73 harness.simulation.step();
74 assert.equal(
75 harness.simulation.bagOf(playerId).equipment.slots[EQUIPMENT_SLOT.weapon]?.itemId,
76 ItemId.stick
77 );
78
79 const farRat = harness.simulation.spawnEntity(mobComponents({
80 tileX: 12,
81 tileY: 8,
82 mobTypeId: MobId.rat
83 }));
84 harness.simulation.enqueue(1, {
85 type: "attack",
86 characterId: character.character_id,
87 targetEntityId: farRat
88 });
89 const farEvents = harness.simulation.step().events;
90 assert.equal(farEvents.some((event) => event.type === "combat-swing"), false);
91
92 const nearRat = harness.simulation.spawnEntity(mobComponents({
93 tileX: 9,
94 tileY: 8,
95 mobTypeId: MobId.rat
96 }));
97 harness.simulation.enqueue(1, {
98 type: "attack",
99 characterId: character.character_id,
100 targetEntityId: nearRat
101 });
102 const nearEvents = harness.simulation.step().events;
103 assert.ok(nearEvents.some((event) => event.type === "combat-swing"));
104 assert.ok(nearEvents.some((event) =>
105 event.type === "combat-hit"
106 || event.type === "combat-miss"
107 || event.type === "combat-block"));
108 }
109 finally
110 { harness.close(); }
111});
112
113test("pigeons are untargetable free-flight and never get path tiles", () =>
114{
115 const harness = makeSimulation({ workerCount: 1 });
116 try
117 {
118 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
119 harness.simulation.spawnCharacter(1, character);
120 const pigeonId = harness.simulation.spawnEntity(mobComponents({
121 tileX: 10,
122 tileY: 8,
123 mobTypeId: MobId.pigeon
124 }));
125
126 const before = componentsOf(harness.simulation, pigeonId);
127 assert.equal(before.flight != null, true);
128 assert.equal(before.combat == null, true);
129 assert.deepEqual(before.path?.tiles ?? [], []);
130
131 harness.setNow(harness.now() + 500);
132 stepTimes(harness.simulation, 10);
133 const after = componentsOf(harness.simulation, pigeonId);
134 assert.deepEqual(after.path?.tiles ?? [], []);
135 assert.equal(after.flight != null, true);
136
137 harness.simulation.enqueue(1, {
138 type: "attack",
139 characterId: character.character_id,
140 targetEntityId: pigeonId
141 });
142 const result = harness.simulation.step();
143 assert.ok(result.events.some((event) => event.type === "command-error"));
144 assert.equal(componentsOf(harness.simulation, pigeonId).path?.tiles?.length ?? 0, 0);
145 }
146 finally
147 { harness.close(); }
148});
149
150test("deer retaliate when attacked", () =>
151{
152 const harness = makeSimulation({ workerCount: 1 });
153 try
154 {
155 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
156 const playerId = harness.simulation.spawnCharacter(1, character);
157 const deerId = harness.simulation.spawnEntity(mobComponents({
158 tileX: 9,
159 tileY: 8,
160 mobTypeId: MobId.deer,
161 spawnerId: null
162 }));
163
164 harness.simulation.enqueue(1, {
165 type: "attack",
166 characterId: character.character_id,
167 targetEntityId: deerId
168 });
169 harness.simulation.step();
170
171 const deer = componentsOf(harness.simulation, deerId);
172 assert.equal(deer.combat?.targetEntityId, playerId);
173 assert.ok(deer.skills?.levels?.unarmed >= 1);
174 }
175 finally
176 { harness.close(); }
177});
178
179test("cancel attack stops the player but the mob keeps pursuing", () =>
180{
181 const harness = makeSimulation({ workerCount: 1 });
182 try
183 {
184 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
185 const playerId = harness.simulation.spawnCharacter(1, character);
186 const ratId = harness.simulation.spawnEntity(mobComponents({
187 tileX: 9,
188 tileY: 8,
189 mobTypeId: MobId.rat,
190 spawnerId: null
191 }));
192
193 harness.simulation.enqueue(1, {
194 type: "attack",
195 characterId: character.character_id,
196 targetEntityId: ratId
197 });
198 harness.simulation.step();
199
200 harness.simulation.enqueue(1, {
201 type: "cancel-attack",
202 characterId: character.character_id
203 });
204 harness.simulation.step();
205
206 const player = componentsOf(harness.simulation, playerId);
207 const rat = componentsOf(harness.simulation, ratId);
208 assert.equal(player.combat?.targetEntityId, null);
209 assert.equal(rat.combat?.targetEntityId, playerId);
210 }
211 finally
212 { harness.close(); }
213});
214
215test("player combat and skills components are present on spawn", () =>
216{
217 const harness = makeSimulation({ workerCount: 1 });
218 try
219 {
220 const character = makeCharacter(harness.stateDb);
221 const playerId = harness.simulation.spawnCharacter(1, character);
222 const comps = componentsOf(harness.simulation, playerId);
223 assert.ok(comps.combat);
224 assert.equal(comps.skills?.xp?.clubfighting, 0);
225 assert.ok(comps.health);
226 }
227 finally
228 { harness.close(); }
229});
230
231test("mobs heal to full when their region unloads", () =>
232{
233 const harness = makeSimulation({ workerCount: 1 });
234 try
235 {
236 const ratId = harness.simulation.spawnEntity(mobComponents({
237 tileX: 8,
238 tileY: 8,
239 mobTypeId: MobId.rat
240 }));
241 const worker = workerOf(harness.simulation, ratId);
242 const handle = worker.world.ecs.handleOf(ratId);
243 worker.world.ecs.get(handle, Component.health).current = 2;
244 const result = worker.call({
245 type: WorkerMessage.dropRegion,
246 regionX: tileToRegion(8),
247 regionY: tileToRegion(8)
248 });
249 const record = result.dropped.find((entry) => entry.entityId === ratId);
250 assert.ok(record);
251 assert.equal(
252 record.payload.components.health.current,
253 record.payload.components.health.max
254 );
255 }
256 finally
257 { harness.close(); }
258});
259
260test("taking damage awards Health XP equal to damage dealt", () =>
261{
262 const harness = makeSimulation({ workerCount: 1 });
263 const random = Math.random;
264 Math.random = () => 0;
265 try
266 {
267 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
268 const playerId = harness.simulation.spawnCharacter(1, character);
269 const ratId = harness.simulation.spawnEntity(mobComponents({
270 mobTypeId: MobId.rat,
271 tileX: 9,
272 tileY: 8
273 }));
274 const worker = harness.simulation.workers[0];
275 const playerHandle = worker.world.ecs.handleOf(playerId);
276 const before = worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health];
277 const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId));
278 const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now());
279 const hit = events.find((event) => event.type === "combat-hit");
280 assert.ok(hit);
281 const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId);
282 assert.ok(updated);
283 assert.equal(updated.stored[SkillId.health] - before, hit.damage);
284 }
285 finally
286 {
287 Math.random = random;
288 harness.close();
289 }
290});