BlueGit

timfa questward

Files

questward / server / tests / combat.test.js

combat.test.js

combat.test.js 25.3 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 { BlockType } from "../../shared/definitions/tiles.js";
7import { Component } from "../src/ecs/components.js";
8import { tileKey } from "../src/world/coordinates.js";
9import { WorkerMessage } from "../src/simulation/protocol.js";
10import { tileToRegion } from "../src/world/coordinates.js";
11import { SkillId } from "../../shared/definitions/skills.js";
12import { resolveMeleeSwing, attackProfileOf } from "../src/combat/service.js";
13import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js";
14
15function workerOf (simulation, entityId)
16{
17 const record = simulation.entityRecord(entityId);
18 assert.ok(record, "entity must be remembered");
19 return simulation.workers[record.workerId];
20}
21
22function componentsOf (simulation, entityId)
23{
24 const worker = workerOf(simulation, entityId);
25 const handle = worker.world.ecs.handleOf(entityId);
26 assert.ok(handle != null);
27 return worker.world.ecs.componentsOf(handle);
28}
29
30function teleportEntity (worker, entityId, tileX, tileY)
31{
32 const world = worker.world;
33 const handle = world.ecs.handleOf(entityId);
34 const pos = world.ecs.get(handle, Component.position);
35 world.occupancy.delete(tileKey(pos.tileX, pos.tileY));
36 pos.tileX = tileX;
37 pos.tileY = tileY;
38 world.occupancy.set(tileKey(tileX, tileY), entityId);
39}
40
41function entityIsAlive (simulation, entityId)
42{
43 const record = simulation.entityRecord(entityId);
44 if (!record)
45 { return false; }
46 const worker = simulation.workers[record.workerId];
47 const handle = worker.world.ecs.handleOf(entityId);
48 if (handle == null)
49 { return false; }
50 const health = worker.world.ecs.get(handle, Component.health);
51 return (health?.current ?? 0) > 0;
52}
53
54function simulateAttackClick (harness, character, targetEntityId)
55{
56 harness.simulation.enqueue(1, {
57 type: "attack",
58 characterId: character.character_id,
59 targetEntityId
60 });
61}
62
63/** Run ticks until one combatant dies or the step budget is exhausted. */
64function runCombatUntilOutcome (harness, playerId, deerId, maxSteps = 600)
65{
66 let swings = 0;
67 for (let i = 0; i < maxSteps; i++)
68 {
69 harness.advance(50);
70 const events = harness.simulation.step().events;
71 swings += events.filter((event) => event.type === "combat-swing").length;
72 const playerAlive = entityIsAlive(harness.simulation, playerId);
73 const deerAlive = entityIsAlive(harness.simulation, deerId);
74 if (!playerAlive || !deerAlive)
75 { return { playerAlive, deerAlive, swings, steps: i + 1 }; }
76 }
77 return {
78 playerAlive: entityIsAlive(harness.simulation, playerId),
79 deerAlive: entityIsAlive(harness.simulation, deerId),
80 swings,
81 steps: maxSteps
82 };
83}
84
85function withDeterministicCombat (fn)
86{
87 const random = Math.random;
88 Math.random = () => 0;
89 try
90 { fn(); }
91 finally
92 { Math.random = random; }
93}
94
95test("immortal deer auto-attacks until the player dies", () =>
96{
97 withDeterministicCombat(() =>
98 {
99 const harness = makeSimulation({ workerCount: 1 });
100 try
101 {
102 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
103 const playerId = harness.simulation.spawnCharacter(1, character);
104 const deerId = harness.simulation.spawnEntity(mobComponents({
105 tileX: 9,
106 tileY: 8,
107 mobTypeId: MobId.deer,
108 spawnerId: null
109 }));
110 const deer = componentsOf(harness.simulation, deerId);
111 deer.health.current = 1_000_000;
112 deer.health.max = 1_000_000;
113
114 simulateAttackClick(harness, character, deerId);
115 harness.simulation.step();
116
117 const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000);
118 assert.ok(outcome.swings >= 2, `expected sustained auto-attack swings, got ${outcome.swings}`);
119 assert.equal(outcome.deerAlive, true, "immortal deer should survive");
120 assert.equal(outcome.playerAlive, false, "deer should kill the player via auto-attack");
121 assert.equal(
122 componentsOf(harness.simulation, deerId).combat?.targetEntityId ?? null,
123 null,
124 "deer should clear engage after the player dies"
125 );
126 }
127 finally
128 { harness.close(); }
129 });
130});
131
132test("immortal deer keeps auto-attacking after the player walks away", () =>
133{
134 withDeterministicCombat(() =>
135 {
136 const harness = makeSimulation({ workerCount: 1 });
137 try
138 {
139 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
140 const playerId = harness.simulation.spawnCharacter(1, character);
141 const deerId = harness.simulation.spawnEntity(mobComponents({
142 tileX: 9,
143 tileY: 8,
144 mobTypeId: MobId.deer,
145 spawnerId: null
146 }));
147 const deer = componentsOf(harness.simulation, deerId);
148 deer.health.current = 1_000_000;
149 deer.health.max = 1_000_000;
150
151 simulateAttackClick(harness, character, deerId);
152 harness.simulation.step();
153
154 // Let one exchange land, then cancel player engage by walking away (client moveTo).
155 for (let i = 0; i < 30; i++)
156 {
157 harness.advance(50);
158 const swings = harness.simulation.step().events.filter((event) => event.type === "combat-swing").length;
159 if (swings > 0)
160 { break; }
161 }
162 harness.simulation.enqueue(1, {
163 type: "move-to",
164 characterId: character.character_id,
165 tileX: 8,
166 tileY: 14
167 });
168 harness.simulation.step();
169 assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, null);
170 assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId);
171
172 const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000);
173 assert.ok(outcome.swings >= 1, `expected deer pursuit swings, got ${outcome.swings}`);
174 assert.equal(outcome.deerAlive, true);
175 assert.equal(outcome.playerAlive, false, "deer should chase and kill the player after walk-away");
176 }
177 finally
178 { harness.close(); }
179 });
180});
181
182test("cross-worker immortal deer auto-attacks until the player dies", () =>
183{
184 withDeterministicCombat(() =>
185 {
186 const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 });
187 try
188 {
189 const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 });
190 const playerId = harness.simulation.spawnCharacter(1, character);
191 const deerId = harness.simulation.spawnEntity(mobComponents({
192 tileX: 63,
193 tileY: 8,
194 mobTypeId: MobId.deer,
195 spawnerId: null
196 }));
197 assert.equal(harness.simulation.entityRecord(playerId).workerId, 1);
198 assert.equal(harness.simulation.entityRecord(deerId).workerId, 0);
199
200 const deerWorker = workerOf(harness.simulation, deerId);
201 const deerHandle = deerWorker.world.ecs.handleOf(deerId);
202 const deerHealth = deerWorker.world.ecs.get(deerHandle, Component.health);
203 deerHealth.current = 1_000_000;
204 deerHealth.max = 1_000_000;
205
206 simulateAttackClick(harness, character, deerId);
207 const first = harness.simulation.step();
208 assert.equal(first.events.some((event) => event.type === "command-error"), false);
209
210 const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000);
211 assert.ok(outcome.swings >= 2, `expected sustained cross-worker swings, got ${outcome.swings}`);
212 assert.equal(outcome.deerAlive, true);
213 assert.equal(outcome.playerAlive, false, "cross-worker deer should kill the player via auto-attack");
214 }
215 finally
216 { harness.close(); }
217 });
218});
219
220test("click attack from 5 tiles away ends with player or deer dead", () =>
221{
222 withDeterministicCombat(() =>
223 {
224 const harness = makeSimulation({ workerCount: 1 });
225 try
226 {
227 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
228 const playerId = harness.simulation.spawnCharacter(1, character);
229 const deerId = harness.simulation.spawnEntity(mobComponents({
230 tileX: 13,
231 tileY: 8,
232 mobTypeId: MobId.deer,
233 spawnerId: null
234 }));
235
236 simulateAttackClick(harness, character, deerId);
237 harness.simulation.step();
238
239 const outcome = runCombatUntilOutcome(harness, playerId, deerId);
240 assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`);
241 assert.ok(
242 !outcome.playerAlive || !outcome.deerAlive,
243 `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive`
244 );
245 }
246 finally
247 { harness.close(); }
248 });
249});
250
251test("click attack while adjacent ends with player or deer dead", () =>
252{
253 withDeterministicCombat(() =>
254 {
255 const harness = makeSimulation({ workerCount: 1 });
256 try
257 {
258 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
259 const playerId = harness.simulation.spawnCharacter(1, character);
260 const deerId = harness.simulation.spawnEntity(mobComponents({
261 tileX: 9,
262 tileY: 8,
263 mobTypeId: MobId.deer,
264 spawnerId: null
265 }));
266
267 simulateAttackClick(harness, character, deerId);
268 harness.simulation.step();
269
270 const outcome = runCombatUntilOutcome(harness, playerId, deerId);
271 assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`);
272 assert.ok(
273 !outcome.playerAlive || !outcome.deerAlive,
274 `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive`
275 );
276 }
277 finally
278 { harness.close(); }
279 });
280});
281
282test("cross-worker click attack from 5 tiles away ends with player or deer dead", () =>
283{
284 withDeterministicCombat(() =>
285 {
286 const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 });
287 try
288 {
289 const character = makeCharacter(harness.stateDb, { tileX: 59, tileY: 8 });
290 const playerId = harness.simulation.spawnCharacter(1, character);
291 const deerId = harness.simulation.spawnEntity(mobComponents({
292 tileX: 64,
293 tileY: 8,
294 mobTypeId: MobId.deer,
295 spawnerId: null
296 }));
297 assert.equal(harness.simulation.entityRecord(playerId).workerId, 0);
298 assert.equal(harness.simulation.entityRecord(deerId).workerId, 1);
299
300 simulateAttackClick(harness, character, deerId);
301 const first = harness.simulation.step();
302 assert.equal(first.events.some((event) => event.type === "command-error"), false);
303
304 const outcome = runCombatUntilOutcome(harness, playerId, deerId);
305 assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`);
306 assert.ok(
307 !outcome.playerAlive || !outcome.deerAlive,
308 `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive`
309 );
310 }
311 finally
312 { harness.close(); }
313 });
314});
315
316test("cross-worker adjacent click attack ends with player or deer dead", () =>
317{
318 withDeterministicCombat(() =>
319 {
320 const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 });
321 try
322 {
323 const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 });
324 const playerId = harness.simulation.spawnCharacter(1, character);
325 const deerId = harness.simulation.spawnEntity(mobComponents({
326 tileX: 63,
327 tileY: 8,
328 mobTypeId: MobId.deer,
329 spawnerId: null
330 }));
331 assert.equal(harness.simulation.entityRecord(playerId).workerId, 1);
332 assert.equal(harness.simulation.entityRecord(deerId).workerId, 0);
333
334 simulateAttackClick(harness, character, deerId);
335 const first = harness.simulation.step();
336 assert.equal(first.events.some((event) => event.type === "command-error"), false);
337
338 const outcome = runCombatUntilOutcome(harness, playerId, deerId);
339 assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`);
340 assert.ok(
341 !outcome.playerAlive || !outcome.deerAlive,
342 `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive`
343 );
344 }
345 finally
346 { harness.close(); }
347 });
348});
349
350test("attack engages the attacker; target retaliates on first swing", () =>
351{
352 const harness = makeSimulation({ workerCount: 1 });
353 try
354 {
355 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
356 const playerId = harness.simulation.spawnCharacter(1, character);
357 // Deer are not aggressive — only retaliate after a real swing.
358 const deerId = harness.simulation.spawnEntity(mobComponents({
359 tileX: 12,
360 tileY: 8,
361 mobTypeId: MobId.deer,
362 spawnerId: null
363 }));
364
365 harness.simulation.enqueue(1, {
366 type: "attack",
367 characterId: character.character_id,
368 targetEntityId: deerId
369 });
370 harness.simulation.step();
371
372 assert.equal(componentsOf(harness.simulation, playerId).combat.targetEntityId, deerId);
373 assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId ?? null, null);
374
375 let swung = false;
376 for (let i = 0; i < 80; i++)
377 {
378 harness.advance(50);
379 const events = harness.simulation.step().events;
380 if (events.some((event) => event.type === "combat-swing"))
381 {
382 swung = true;
383 break;
384 }
385 }
386 assert.ok(swung, "expected a melee swing once adjacent");
387 assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId);
388 }
389 finally
390 { harness.close(); }
391});
392
393test("melee swing only resolves at Chebyshev distance 1", () =>
394{
395 const harness = makeSimulation({ workerCount: 1 });
396 try
397 {
398 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
399 const playerId = harness.simulation.spawnCharacter(1, character);
400 const bag = harness.simulation.bagOf(playerId);
401 const stickSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.stick);
402 assert.ok(stickSlot >= 0);
403 harness.simulation.enqueue(1, {
404 type: "equip-item",
405 characterId: character.character_id,
406 slotIndex: stickSlot
407 });
408 harness.simulation.step();
409 assert.equal(
410 harness.simulation.bagOf(playerId).equipment.slots[EQUIPMENT_SLOT.weapon]?.itemId,
411 ItemId.stick
412 );
413
414 const farRat = harness.simulation.spawnEntity(mobComponents({
415 tileX: 12,
416 tileY: 8,
417 mobTypeId: MobId.rat
418 }));
419 harness.simulation.enqueue(1, {
420 type: "attack",
421 characterId: character.character_id,
422 targetEntityId: farRat
423 });
424 const farEvents = harness.simulation.step().events;
425 assert.equal(farEvents.some((event) => event.type === "combat-swing"), false);
426
427 const nearRat = harness.simulation.spawnEntity(mobComponents({
428 tileX: 9,
429 tileY: 8,
430 mobTypeId: MobId.rat
431 }));
432 harness.simulation.enqueue(1, {
433 type: "attack",
434 characterId: character.character_id,
435 targetEntityId: nearRat
436 });
437 const nearEvents = harness.simulation.step().events;
438 assert.ok(nearEvents.some((event) => event.type === "combat-swing"));
439 assert.ok(nearEvents.some((event) =>
440 event.type === "combat-hit"
441 || event.type === "combat-miss"
442 || event.type === "combat-block"));
443 }
444 finally
445 { harness.close(); }
446});
447
448test("pigeons are untargetable free-flight and never get path tiles", () =>
449{
450 const harness = makeSimulation({ workerCount: 1 });
451 try
452 {
453 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
454 harness.simulation.spawnCharacter(1, character);
455 const pigeonId = harness.simulation.spawnEntity(mobComponents({
456 tileX: 10,
457 tileY: 8,
458 mobTypeId: MobId.pigeon
459 }));
460
461 const before = componentsOf(harness.simulation, pigeonId);
462 assert.equal(before.flight != null, true);
463 assert.equal(before.combat == null, true);
464 assert.deepEqual(before.path?.tiles ?? [], []);
465
466 harness.setNow(harness.now() + 500);
467 stepTimes(harness.simulation, 10);
468 const after = componentsOf(harness.simulation, pigeonId);
469 assert.deepEqual(after.path?.tiles ?? [], []);
470 assert.equal(after.flight != null, true);
471
472 harness.simulation.enqueue(1, {
473 type: "attack",
474 characterId: character.character_id,
475 targetEntityId: pigeonId
476 });
477 const result = harness.simulation.step();
478 assert.ok(result.events.some((event) => event.type === "command-error"));
479 assert.equal(componentsOf(harness.simulation, pigeonId).path?.tiles?.length ?? 0, 0);
480 }
481 finally
482 { harness.close(); }
483});
484
485test("deer retaliate when attacked", () =>
486{
487 const harness = makeSimulation({ workerCount: 1 });
488 try
489 {
490 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
491 const playerId = harness.simulation.spawnCharacter(1, character);
492 const deerId = harness.simulation.spawnEntity(mobComponents({
493 tileX: 9,
494 tileY: 8,
495 mobTypeId: MobId.deer,
496 spawnerId: null
497 }));
498
499 harness.simulation.enqueue(1, {
500 type: "attack",
501 characterId: character.character_id,
502 targetEntityId: deerId
503 });
504 const events = harness.simulation.step().events;
505 assert.ok(events.some((event) => event.type === "combat-swing"));
506
507 const deer = componentsOf(harness.simulation, deerId);
508 assert.equal(deer.combat?.targetEntityId, playerId);
509 assert.ok(deer.skills?.levels?.unarmed >= 1);
510 }
511 finally
512 { harness.close(); }
513});
514
515test("aggressive rats engage players within aggro range and LOS", () =>
516{
517 const harness = makeSimulation({ workerCount: 1 });
518 try
519 {
520 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
521 const playerId = harness.simulation.spawnCharacter(1, character);
522 const ratId = harness.simulation.spawnEntity(mobComponents({
523 tileX: 12,
524 tileY: 8,
525 mobTypeId: MobId.rat,
526 spawnerId: null
527 }));
528
529 harness.simulation.step();
530 assert.equal(componentsOf(harness.simulation, ratId).combat?.targetEntityId, playerId);
531 }
532 finally
533 { harness.close(); }
534});
535
536test("aggressive rats do not aggro through walls or beyond range", () =>
537{
538 const harness = makeSimulation({ workerCount: 1 });
539 try
540 {
541 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
542 const playerId = harness.simulation.spawnCharacter(1, character);
543 const record = harness.simulation.entityRecord(playerId);
544 const worker = harness.simulation.workers[record.workerId];
545
546 const blockedRat = harness.simulation.spawnEntity(mobComponents({
547 tileX: 12,
548 tileY: 8,
549 mobTypeId: MobId.rat,
550 spawnerId: null
551 }));
552 const data = worker.world.chunkData(0, 0);
553 data.blocks[8 * 16 + 10] = { typeId: BlockType.stoneWall, state: null, config: null };
554
555 const farRat = harness.simulation.spawnEntity(mobComponents({
556 tileX: 14,
557 tileY: 8,
558 mobTypeId: MobId.rat,
559 spawnerId: null
560 }));
561
562 harness.simulation.step();
563 assert.equal(componentsOf(harness.simulation, blockedRat).combat?.targetEntityId ?? null, null);
564 assert.equal(componentsOf(harness.simulation, farRat).combat?.targetEntityId ?? null, null);
565 }
566 finally
567 { harness.close(); }
568});
569
570test("attacker keeps pursuing and can swing after the target moves", () =>
571{
572 const harness = makeSimulation({ workerCount: 1 });
573 try
574 {
575 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
576 const playerId = harness.simulation.spawnCharacter(1, character);
577 const ratId = harness.simulation.spawnEntity(mobComponents({
578 tileX: 12,
579 tileY: 8,
580 mobTypeId: MobId.rat,
581 spawnerId: null
582 }));
583
584 harness.simulation.enqueue(1, {
585 type: "attack",
586 characterId: character.character_id,
587 targetEntityId: ratId
588 });
589 harness.simulation.step();
590
591 const worker = workerOf(harness.simulation, playerId);
592 stepTimes(harness.simulation, 4);
593 teleportEntity(worker, ratId, 12, 10);
594
595 let swings = 0;
596 for (let i = 0; i < 120; i++)
597 {
598 const events = harness.simulation.step().events;
599 swings += events.filter((event) => event.type === "combat-swing").length;
600 if (swings > 0)
601 { break; }
602 }
603
604 assert.ok(swings >= 1);
605 assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId, ratId);
606 }
607 finally
608 { harness.close(); }
609});
610
611test("combatants keep auto-attacking while adjacent", () =>
612{
613 const harness = makeSimulation({ workerCount: 1 });
614 try
615 {
616 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
617 const playerId = harness.simulation.spawnCharacter(1, character);
618 const deerId = harness.simulation.spawnEntity(mobComponents({
619 tileX: 9,
620 tileY: 8,
621 mobTypeId: MobId.deer,
622 spawnerId: null
623 }));
624
625 harness.simulation.enqueue(1, {
626 type: "attack",
627 characterId: character.character_id,
628 targetEntityId: deerId
629 });
630 harness.simulation.step();
631
632 let swings = 0;
633 for (let i = 0; i < 50; i++)
634 {
635 harness.advance(50);
636 swings += harness.simulation.step().events.filter((event) => event.type === "combat-swing").length;
637 }
638
639 assert.ok(swings >= 4, `expected repeated swings, got ${swings}`);
640 const player = componentsOf(harness.simulation, playerId);
641 const deer = componentsOf(harness.simulation, deerId);
642 assert.ok((player.health?.current ?? 0) > 0, "player should still be alive during sustained combat");
643 assert.equal(player.combat?.targetEntityId, deerId);
644 assert.equal(deer.combat?.targetEntityId, playerId);
645 }
646 finally
647 { harness.close(); }
648});
649
650test("cross-worker adjacent combat keeps engaging and swinging", () =>
651{
652 const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 });
653 try
654 {
655 const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 });
656 const playerId = harness.simulation.spawnCharacter(1, character);
657 const deerId = harness.simulation.spawnEntity(mobComponents({
658 tileX: 63,
659 tileY: 8,
660 mobTypeId: MobId.deer,
661 spawnerId: null
662 }));
663 assert.equal(harness.simulation.entityRecord(playerId).workerId, 1);
664 assert.equal(harness.simulation.entityRecord(deerId).workerId, 0);
665
666 harness.simulation.enqueue(1, {
667 type: "attack",
668 characterId: character.character_id,
669 targetEntityId: deerId
670 });
671 const first = harness.simulation.step();
672 assert.equal(first.events.some((event) => event.type === "command-error"), false);
673
674 const random = Math.random;
675 Math.random = () => 0;
676 try
677 {
678 let swings = 0;
679 for (let i = 0; i < 5; i++)
680 {
681 harness.advance(50);
682 swings += harness.simulation.step().events.filter((event) => event.type === "combat-swing").length;
683 }
684
685 assert.ok(swings >= 4, `expected cross-worker sustained swings, got ${swings}`);
686 const deerWorker = harness.simulation.workers[0];
687 const playerWorker = harness.simulation.workers[1];
688 const deerHandle = deerWorker.world.ecs.handleOf(deerId);
689 const playerHandle = playerWorker.world.ecs.handleOf(playerId);
690 const deerCombat = deerWorker.world.ecs.get(deerHandle, Component.combat);
691 const playerCombat = playerWorker.world.ecs.get(playerHandle, Component.combat);
692 const playerHealth = playerWorker.world.ecs.get(playerHandle, Component.health);
693 assert.ok((playerHealth?.current ?? 0) > 0, "player should survive sustained cross-worker combat");
694 assert.equal(deerCombat?.targetEntityId, playerId);
695 assert.equal(playerCombat?.targetEntityId, deerId);
696 }
697 finally
698 { Math.random = random; }
699 }
700 finally
701 { harness.close(); }
702});
703
704test("cancel attack stops the player but the mob keeps pursuing", () =>
705{
706 const harness = makeSimulation({ workerCount: 1 });
707 try
708 {
709 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
710 const playerId = harness.simulation.spawnCharacter(1, character);
711 const ratId = harness.simulation.spawnEntity(mobComponents({
712 tileX: 9,
713 tileY: 8,
714 mobTypeId: MobId.rat,
715 spawnerId: null
716 }));
717
718 harness.simulation.enqueue(1, {
719 type: "attack",
720 characterId: character.character_id,
721 targetEntityId: ratId
722 });
723 harness.simulation.step();
724
725 harness.simulation.enqueue(1, {
726 type: "cancel-attack",
727 characterId: character.character_id
728 });
729 harness.simulation.step();
730
731 const player = componentsOf(harness.simulation, playerId);
732 const rat = componentsOf(harness.simulation, ratId);
733 assert.equal(player.combat?.targetEntityId, null);
734 assert.equal(rat.combat?.targetEntityId, playerId);
735 }
736 finally
737 { harness.close(); }
738});
739
740test("player combat and skills components are present on spawn", () =>
741{
742 const harness = makeSimulation({ workerCount: 1 });
743 try
744 {
745 const character = makeCharacter(harness.stateDb);
746 const playerId = harness.simulation.spawnCharacter(1, character);
747 const comps = componentsOf(harness.simulation, playerId);
748 assert.ok(comps.combat);
749 assert.equal(comps.skills?.xp?.clubfighting, 0);
750 assert.ok(comps.health);
751 }
752 finally
753 { harness.close(); }
754});
755
756test("mobs heal to full when their region unloads", () =>
757{
758 const harness = makeSimulation({ workerCount: 1 });
759 try
760 {
761 const ratId = harness.simulation.spawnEntity(mobComponents({
762 tileX: 8,
763 tileY: 8,
764 mobTypeId: MobId.rat
765 }));
766 const worker = workerOf(harness.simulation, ratId);
767 const handle = worker.world.ecs.handleOf(ratId);
768 worker.world.ecs.get(handle, Component.health).current = 2;
769 const result = worker.call({
770 type: WorkerMessage.dropRegion,
771 regionX: tileToRegion(8),
772 regionY: tileToRegion(8)
773 });
774 const record = result.dropped.find((entry) => entry.entityId === ratId);
775 assert.ok(record);
776 assert.equal(
777 record.payload.components.health.current,
778 record.payload.components.health.max
779 );
780 }
781 finally
782 { harness.close(); }
783});
784
785test("taking damage awards Health XP equal to damage dealt", () =>
786{
787 const harness = makeSimulation({ workerCount: 1 });
788 const random = Math.random;
789 Math.random = () => 0;
790 try
791 {
792 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
793 const playerId = harness.simulation.spawnCharacter(1, character);
794 const ratId = harness.simulation.spawnEntity(mobComponents({
795 mobTypeId: MobId.rat,
796 tileX: 9,
797 tileY: 8
798 }));
799 const worker = harness.simulation.workers[0];
800 const playerHandle = worker.world.ecs.handleOf(playerId);
801 const before = worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health];
802 const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId));
803 const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now());
804 const hit = events.find((event) => event.type === "combat-hit");
805 assert.ok(hit);
806 const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId);
807 assert.ok(updated);
808 assert.equal(updated.stored[SkillId.health] - before, hit.damage);
809 }
810 finally
811 {
812 Math.random = random;
813 harness.close();
814 }
815});