import test from "node:test"; import assert from "node:assert/strict"; import { mobComponents } from "../src/simulation/spawners.js"; import { MobId } from "../../shared/definitions/mobs.js"; import { ItemId, EQUIPMENT_SLOT } from "../../shared/definitions/items.js"; import { BlockType } from "../../shared/definitions/tiles.js"; import { Component } from "../src/ecs/components.js"; import { tileKey } from "../src/world/coordinates.js"; import { WorkerMessage } from "../src/simulation/protocol.js"; import { tileToRegion } from "../src/world/coordinates.js"; import { SkillId } from "../../shared/definitions/skills.js"; import { resolveMeleeSwing, attackProfileOf } from "../src/combat/service.js"; import { makeCharacter, makeSimulation, pinEastSplit, stepTimes } from "./helpers/simulationHarness.js"; function workerOf (simulation, entityId) { const record = simulation.entityRecord(entityId); assert.ok(record, "entity must be remembered"); return simulation.workers[record.workerId]; } function componentsOf (simulation, entityId) { const worker = workerOf(simulation, entityId); const handle = worker.world.ecs.handleOf(entityId); assert.ok(handle != null); return worker.world.ecs.componentsOf(handle); } function teleportEntity (worker, entityId, tileX, tileY) { const world = worker.world; const handle = world.ecs.handleOf(entityId); const pos = world.ecs.get(handle, Component.position); const fromKey = tileKey(pos.tileX, pos.tileY); const fromSet = world.occupancy.get(fromKey); if (fromSet) { fromSet.delete(entityId); if (fromSet.size === 0) { world.occupancy.delete(fromKey); } } pos.tileX = tileX; pos.tileY = tileY; const toKey = tileKey(tileX, tileY); let toSet = world.occupancy.get(toKey); if (!toSet) { toSet = new Set(); world.occupancy.set(toKey, toSet); } toSet.add(entityId); } function entityIsAlive (simulation, entityId) { const record = simulation.entityRecord(entityId); if (!record) { return false; } const worker = simulation.workers[record.workerId]; const handle = worker.world.ecs.handleOf(entityId); if (handle == null) { return false; } const health = worker.world.ecs.get(handle, Component.health); return (health?.current ?? 0) > 0; } function simulateAttackClick (harness, character, targetEntityId) { harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId }); } /** Run ticks until one combatant dies or the step budget is exhausted. */ function runCombatUntilOutcome (harness, playerId, deerId, maxSteps = 600) { let swings = 0; for (let i = 0; i < maxSteps; i++) { harness.advance(50); const events = harness.simulation.step().events; swings += events.filter((event) => event.type === "combat-swing").length; const playerAlive = entityIsAlive(harness.simulation, playerId); const deerAlive = entityIsAlive(harness.simulation, deerId); if (!playerAlive || !deerAlive) { return { playerAlive, deerAlive, swings, steps: i + 1 }; } } return { playerAlive: entityIsAlive(harness.simulation, playerId), deerAlive: entityIsAlive(harness.simulation, deerId), swings, steps: maxSteps }; } function withDeterministicCombat (fn) { const random = Math.random; Math.random = () => 0; try { fn(); } finally { Math.random = random; } } test("immortal deer auto-attacks until the player dies", () => { withDeterministicCombat(() => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.deer, spawnerId: null })); const deer = componentsOf(harness.simulation, deerId); deer.health.current = 1_000_000; deer.health.max = 1_000_000; simulateAttackClick(harness, character, deerId); harness.simulation.step(); const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); assert.ok(outcome.swings >= 2, `expected sustained auto-attack swings, got ${outcome.swings}`); assert.equal(outcome.deerAlive, true, "immortal deer should survive"); assert.equal(outcome.playerAlive, false, "deer should kill the player via auto-attack"); assert.equal( componentsOf(harness.simulation, deerId).combat?.targetEntityId ?? null, null, "deer should clear engage after the player dies" ); } finally { harness.close(); } }); }); test("immortal deer keeps auto-attacking after the player walks away", () => { withDeterministicCombat(() => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.deer, spawnerId: null })); const deer = componentsOf(harness.simulation, deerId); deer.health.current = 1_000_000; deer.health.max = 1_000_000; simulateAttackClick(harness, character, deerId); harness.simulation.step(); // Let one exchange land, then cancel player engage by walking away (client moveTo). for (let i = 0; i < 30; i++) { harness.advance(50); const swings = harness.simulation.step().events.filter((event) => event.type === "combat-swing").length; if (swings > 0) { break; } } harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 8, tileY: 14 }); harness.simulation.step(); assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, null); assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId); const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); assert.ok(outcome.swings >= 1, `expected deer pursuit swings, got ${outcome.swings}`); assert.equal(outcome.deerAlive, true); assert.equal(outcome.playerAlive, false, "deer should chase and kill the player after walk-away"); } finally { harness.close(); } }); }); test("cross-worker immortal deer auto-attacks until the player dies", () => { withDeterministicCombat(() => { const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); try { const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 63, tileY: 8, mobTypeId: MobId.deer, spawnerId: null })); assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); const deerWorker = workerOf(harness.simulation, deerId); const deerHandle = deerWorker.world.ecs.handleOf(deerId); const deerHealth = deerWorker.world.ecs.get(deerHandle, Component.health); deerHealth.current = 1_000_000; deerHealth.max = 1_000_000; simulateAttackClick(harness, character, deerId); const first = harness.simulation.step(); assert.equal(first.events.some((event) => event.type === "command-error"), false); const outcome = runCombatUntilOutcome(harness, playerId, deerId, 2_000); assert.ok(outcome.swings >= 2, `expected sustained cross-worker swings, got ${outcome.swings}`); assert.equal(outcome.deerAlive, true); assert.equal(outcome.playerAlive, false, "cross-worker deer should kill the player via auto-attack"); } finally { harness.close(); } }); }); test("click attack from 5 tiles away ends with player or deer dead", () => { withDeterministicCombat(() => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 13, tileY: 8, mobTypeId: MobId.deer, spawnerId: null })); simulateAttackClick(harness, character, deerId); harness.simulation.step(); const outcome = runCombatUntilOutcome(harness, playerId, deerId); assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); assert.ok( !outcome.playerAlive || !outcome.deerAlive, `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` ); } finally { harness.close(); } }); }); test("click attack while adjacent ends with player or deer dead", () => { withDeterministicCombat(() => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.deer, spawnerId: null })); simulateAttackClick(harness, character, deerId); harness.simulation.step(); const outcome = runCombatUntilOutcome(harness, playerId, deerId); assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); assert.ok( !outcome.playerAlive || !outcome.deerAlive, `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` ); } finally { harness.close(); } }); }); test("cross-worker click attack from 5 tiles away ends with player or deer dead", () => { withDeterministicCombat(() => { const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); try { const character = makeCharacter(harness.stateDb, { tileX: 59, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 64, tileY: 8, mobTypeId: MobId.deer, spawnerId: null })); assert.equal(harness.simulation.entityRecord(playerId).workerId, 0); assert.equal(harness.simulation.entityRecord(deerId).workerId, 1); simulateAttackClick(harness, character, deerId); const first = harness.simulation.step(); assert.equal(first.events.some((event) => event.type === "command-error"), false); const outcome = runCombatUntilOutcome(harness, playerId, deerId); assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); assert.ok( !outcome.playerAlive || !outcome.deerAlive, `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` ); } finally { harness.close(); } }); }); test("cross-worker adjacent click attack ends with player or deer dead", () => { withDeterministicCombat(() => { const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); try { const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 63, tileY: 8, mobTypeId: MobId.deer, spawnerId: null })); assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); simulateAttackClick(harness, character, deerId); const first = harness.simulation.step(); assert.equal(first.events.some((event) => event.type === "command-error"), false); const outcome = runCombatUntilOutcome(harness, playerId, deerId); assert.ok(outcome.swings >= 1, `expected at least one swing, got ${outcome.swings}`); assert.ok( !outcome.playerAlive || !outcome.deerAlive, `expected a kill within ${outcome.steps} steps (${outcome.swings} swings); both still alive` ); } finally { harness.close(); } }); }); test("attack engages the attacker; target retaliates on first swing", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); // Deer are not aggressive — only retaliate after a real swing. const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 12, tileY: 8, mobTypeId: MobId.deer, spawnerId: null })); harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: deerId }); harness.simulation.step(); assert.equal(componentsOf(harness.simulation, playerId).combat.targetEntityId, deerId); assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId ?? null, null); let swung = false; for (let i = 0; i < 80; i++) { harness.advance(50); const events = harness.simulation.step().events; if (events.some((event) => event.type === "combat-swing")) { swung = true; break; } } assert.ok(swung, "expected a melee swing once adjacent"); assert.equal(componentsOf(harness.simulation, deerId).combat?.targetEntityId, playerId); } finally { harness.close(); } }); test("melee swing only resolves at Chebyshev distance 1", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const bag = harness.simulation.bagOf(playerId); const stickSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.stick); assert.ok(stickSlot >= 0); harness.simulation.enqueue(1, { type: "equip-item", characterId: character.character_id, slotIndex: stickSlot }); harness.simulation.step(); assert.equal( harness.simulation.bagOf(playerId).equipment.slots[EQUIPMENT_SLOT.weapon]?.itemId, ItemId.stick ); const farRat = harness.simulation.spawnEntity(mobComponents({ tileX: 12, tileY: 8, mobTypeId: MobId.rat })); harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: farRat }); const farEvents = harness.simulation.step().events; assert.equal(farEvents.some((event) => event.type === "combat-swing"), false); const nearRat = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.rat })); harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: nearRat }); const nearEvents = harness.simulation.step().events; assert.ok(nearEvents.some((event) => event.type === "combat-swing")); assert.ok(nearEvents.some((event) => event.type === "combat-hit" || event.type === "combat-miss" || event.type === "combat-block")); } finally { harness.close(); } }); test("pigeons are untargetable free-flight and never get path tiles", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); const pigeonId = harness.simulation.spawnEntity(mobComponents({ tileX: 10, tileY: 8, mobTypeId: MobId.pigeon })); const before = componentsOf(harness.simulation, pigeonId); assert.equal(before.flight != null, true); assert.equal(before.combat == null, true); assert.deepEqual(before.path?.tiles ?? [], []); harness.setNow(harness.now() + 500); stepTimes(harness.simulation, 10); const after = componentsOf(harness.simulation, pigeonId); assert.deepEqual(after.path?.tiles ?? [], []); assert.equal(after.flight != null, true); harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: pigeonId }); const result = harness.simulation.step(); assert.ok(result.events.some((event) => event.type === "command-error")); assert.equal(componentsOf(harness.simulation, pigeonId).path?.tiles?.length ?? 0, 0); } finally { harness.close(); } }); test("deer retaliate when attacked", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.deer, spawnerId: null })); harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: deerId }); const events = harness.simulation.step().events; assert.ok(events.some((event) => event.type === "combat-swing")); const deer = componentsOf(harness.simulation, deerId); assert.equal(deer.combat?.targetEntityId, playerId); assert.ok(deer.skills?.levels?.unarmed >= 1); } finally { harness.close(); } }); test("aggressive rats engage players within aggro range and LOS", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const ratId = harness.simulation.spawnEntity(mobComponents({ tileX: 12, tileY: 8, mobTypeId: MobId.rat, spawnerId: null })); harness.simulation.step(); assert.equal(componentsOf(harness.simulation, ratId).combat?.targetEntityId, playerId); } finally { harness.close(); } }); test("players retaliate when a mob swings at them", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const ratId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.rat, spawnerId: null })); assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId ?? null, null); let swung = false; for (let i = 0; i < 80; i++) { harness.advance(50); const events = harness.simulation.step().events; if (events.some((event) => event.type === "combat-swing" && event.attackerId === ratId && event.targetId === playerId )) { swung = true; break; } } assert.ok(swung, "expected the rat to swing at the player"); assert.equal( componentsOf(harness.simulation, playerId).combat?.targetEntityId, ratId, "player should auto-retaliate after the mob swing" ); } finally { harness.close(); } }); test("aggressive rats do not aggro through walls or beyond range", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const record = harness.simulation.entityRecord(playerId); const worker = harness.simulation.workers[record.workerId]; const blockedRat = harness.simulation.spawnEntity(mobComponents({ tileX: 12, tileY: 8, mobTypeId: MobId.rat, spawnerId: null })); const data = worker.world.chunkData(0, 0); data.blocks[8 * 16 + 10] = { typeId: BlockType.stoneWall, state: null, config: null }; const farRat = harness.simulation.spawnEntity(mobComponents({ tileX: 14, tileY: 8, mobTypeId: MobId.rat, spawnerId: null })); harness.simulation.step(); assert.equal(componentsOf(harness.simulation, blockedRat).combat?.targetEntityId ?? null, null); assert.equal(componentsOf(harness.simulation, farRat).combat?.targetEntityId ?? null, null); } finally { harness.close(); } }); test("attacker keeps pursuing and can swing after the target moves", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const ratId = harness.simulation.spawnEntity(mobComponents({ tileX: 12, tileY: 8, mobTypeId: MobId.rat, spawnerId: null })); harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: ratId }); harness.simulation.step(); const worker = workerOf(harness.simulation, playerId); stepTimes(harness.simulation, 4); teleportEntity(worker, ratId, 12, 10); let swings = 0; for (let i = 0; i < 120; i++) { const events = harness.simulation.step().events; swings += events.filter((event) => event.type === "combat-swing").length; if (swings > 0) { break; } } assert.ok(swings >= 1); assert.equal(componentsOf(harness.simulation, playerId).combat?.targetEntityId, ratId); } finally { harness.close(); } }); test("combatants keep auto-attacking while adjacent", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.deer, spawnerId: null })); harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: deerId }); harness.simulation.step(); let swings = 0; for (let i = 0; i < 50; i++) { harness.advance(50); const events = harness.simulation.step().events; swings += events.filter((event) => event.type === "combat-swing").length; const player = componentsOf(harness.simulation, playerId); const deer = componentsOf(harness.simulation, deerId); if (swings >= 4 && player.combat?.targetEntityId === deerId && deer.combat?.targetEntityId === playerId && (player.health?.current ?? 0) > 0) { break; } } assert.ok(swings >= 4, `expected repeated swings, got ${swings}`); const player = componentsOf(harness.simulation, playerId); const deer = componentsOf(harness.simulation, deerId); assert.ok((player.health?.current ?? 0) > 0, "player should still be alive during sustained combat"); assert.equal(player.combat?.targetEntityId, deerId); assert.equal(deer.combat?.targetEntityId, playerId); } finally { harness.close(); } }); test("cross-worker adjacent combat keeps engaging and swinging", () => { const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); try { const character = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 63, tileY: 8, mobTypeId: MobId.deer, spawnerId: null })); assert.equal(harness.simulation.entityRecord(playerId).workerId, 1); assert.equal(harness.simulation.entityRecord(deerId).workerId, 0); harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: deerId }); const first = harness.simulation.step(); assert.equal(first.events.some((event) => event.type === "command-error"), false); const random = Math.random; Math.random = () => 0; try { let swings = 0; for (let i = 0; i < 40; i++) { harness.advance(50); swings += harness.simulation.step().events.filter((event) => event.type === "combat-swing").length; if (swings >= 4) { break; } } assert.ok(swings >= 4, `expected cross-worker sustained swings, got ${swings}`); const deerWorker = harness.simulation.workers[0]; const playerWorker = harness.simulation.workers[1]; const deerHandle = deerWorker.world.ecs.handleOf(deerId); const playerHandle = playerWorker.world.ecs.handleOf(playerId); const deerCombat = deerWorker.world.ecs.get(deerHandle, Component.combat); const playerCombat = playerWorker.world.ecs.get(playerHandle, Component.combat); const playerHealth = playerWorker.world.ecs.get(playerHandle, Component.health); assert.ok((playerHealth?.current ?? 0) > 0, "player should survive sustained cross-worker combat"); assert.equal(deerCombat?.targetEntityId, playerId); assert.equal(playerCombat?.targetEntityId, deerId); } finally { Math.random = random; } } finally { harness.close(); } }); test("cancel attack stops the player but the mob keeps pursuing", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const ratId = harness.simulation.spawnEntity(mobComponents({ tileX: 9, tileY: 8, mobTypeId: MobId.rat, spawnerId: null })); harness.simulation.enqueue(1, { type: "attack", characterId: character.character_id, targetEntityId: ratId }); harness.simulation.step(); harness.simulation.enqueue(1, { type: "cancel-attack", characterId: character.character_id }); harness.simulation.step(); const player = componentsOf(harness.simulation, playerId); const rat = componentsOf(harness.simulation, ratId); assert.equal(player.combat?.targetEntityId, null); assert.equal(rat.combat?.targetEntityId, playerId); } finally { harness.close(); } }); test("player combat and skills components are present on spawn", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb); const playerId = harness.simulation.spawnCharacter(1, character); const comps = componentsOf(harness.simulation, playerId); assert.ok(comps.combat); assert.equal(comps.skills?.xp?.clubfighting, 0); assert.ok(comps.health); } finally { harness.close(); } }); test("mobs heal to full when their region unloads", () => { const harness = makeSimulation({ workerCount: 1 }); try { const ratId = harness.simulation.spawnEntity(mobComponents({ tileX: 8, tileY: 8, mobTypeId: MobId.rat })); const worker = workerOf(harness.simulation, ratId); const handle = worker.world.ecs.handleOf(ratId); worker.world.ecs.get(handle, Component.health).current = 2; const result = worker.call({ type: WorkerMessage.dropRegion, regionX: tileToRegion(8), regionY: tileToRegion(8) }); const record = result.dropped.find((entry) => entry.entityId === ratId); assert.ok(record); assert.equal( record.payload.components.health.current, record.payload.components.health.max ); } finally { harness.close(); } }); test("taking damage awards Health XP equal to damage dealt", () => { const harness = makeSimulation({ workerCount: 1 }); const random = Math.random; Math.random = () => 0; try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const ratId = harness.simulation.spawnEntity(mobComponents({ mobTypeId: MobId.rat, tileX: 9, tileY: 8 })); const worker = harness.simulation.workers[0]; const playerHandle = worker.world.ecs.handleOf(playerId); const before = worker.world.ecs.get(playerHandle, Component.skills).xp[SkillId.health]; const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(ratId)); const events = resolveMeleeSwing(worker.world, ratId, playerId, profile, Date.now()); const hit = events.find((event) => event.type === "combat-hit"); assert.ok(hit); const updated = events.find((event) => event.type === "skills-updated" && event.entityId === playerId); assert.ok(updated); assert.equal(updated.stored[SkillId.health] - before, hit.damage); } finally { Math.random = random; harness.close(); } });