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 { Component } from "../src/ecs/components.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, 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); } test("attack engages both combatants and retaliates", () => { 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(); const player = componentsOf(harness.simulation, playerId); const rat = componentsOf(harness.simulation, ratId); assert.equal(player.combat.targetEntityId, ratId); assert.equal(rat.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 }); harness.simulation.step(); const deer = componentsOf(harness.simulation, deerId); assert.equal(deer.combat?.targetEntityId, playerId); assert.ok(deer.skills?.levels?.unarmed >= 1); } 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(); } });