BlueGit

timfa questward

Files

questward / server / tests / combat.test.js

combat.test.js

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