BlueGit

timfa questward

Files

questward / server / tests / combat.test.js

combat.test.js

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