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 { 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")); } 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("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.ok(comps.skills?.levels?.clubfighting); assert.ok(comps.health); } finally { harness.close(); } });