import test from "node:test"; import assert from "node:assert/strict"; import { GAIT } from "../../shared/protocol/constants.js"; import { MobId } from "../../shared/definitions/mobs.js"; import { mobComponents } from "../src/simulation/spawners.js"; import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js"; function workerOf (simulation, entityId) { const record = simulation.entityRecord(entityId); assert.ok(record); 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("follow stays put when already 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 })); harness.simulation.enqueue(1, { type: "follow", characterId: character.character_id, targetEntityId: deerId }); harness.simulation.step(); const comps = componentsOf(harness.simulation, playerId); assert.equal(comps.follow.targetEntityId, deerId); assert.deepEqual(comps.path?.tiles ?? [], []); } finally { harness.close(); } }); test("follow runs when more than three tiles away and breaks past a chunk", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const deerId = harness.simulation.spawnEntity(mobComponents({ tileX: 7, tileY: 8, mobTypeId: MobId.deer })); harness.simulation.enqueue(1, { type: "follow", characterId: character.character_id, targetEntityId: deerId }); harness.simulation.step(); assert.equal(componentsOf(harness.simulation, playerId).movement.gait, GAIT.run); assert.ok((componentsOf(harness.simulation, playerId).path?.tiles?.length ?? 0) > 0); const farDeer = harness.simulation.spawnEntity(mobComponents({ tileX: 20, tileY: 8, mobTypeId: MobId.deer })); harness.simulation.enqueue(1, { type: "follow", characterId: character.character_id, targetEntityId: farDeer }); harness.simulation.step(); assert.equal(componentsOf(harness.simulation, playerId).follow.targetEntityId, null); } finally { harness.close(); } }); test("follow matches target gait within three tiles and works on any mobile", () => { 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: 11, tileY: 8, mobTypeId: MobId.rat })); const ratHandle = workerOf(harness.simulation, ratId).world.ecs.handleOf(ratId); workerOf(harness.simulation, ratId).world.ecs.get(ratHandle, "movement").gait = GAIT.run; harness.simulation.enqueue(1, { type: "follow", characterId: character.character_id, targetEntityId: ratId }); harness.simulation.step(); assert.equal(componentsOf(harness.simulation, playerId).movement.gait, GAIT.run); const pigeonId = harness.simulation.spawnEntity(mobComponents({ tileX: 10, tileY: 8, mobTypeId: MobId.pigeon })); harness.simulation.enqueue(1, { type: "follow", characterId: character.character_id, targetEntityId: pigeonId }); harness.simulation.step(); assert.equal(componentsOf(harness.simulation, playerId).follow.targetEntityId, pigeonId); } finally { harness.close(); } }); test("move-to clears follow", () => { 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: 10, tileY: 8, mobTypeId: MobId.deer })); harness.simulation.enqueue(1, { type: "follow", characterId: character.character_id, targetEntityId: deerId }); harness.simulation.step(); harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 4, tileY: 8 }); harness.simulation.step(); assert.equal(componentsOf(harness.simulation, playerId).follow.targetEntityId, null); } finally { harness.close(); } }); test("follower reuses the target vacated trail and waits off their tile", () => { const harness = makeSimulation({ workerCount: 1 }); try { const leader = makeCharacter(harness.stateDb, { firstName: "Lead", tileX: 8, tileY: 8 }); const follower = makeCharacter(harness.stateDb, { firstName: "Follow", lastName: "Bot", tileX: 6, tileY: 8 }); const leaderId = harness.simulation.spawnCharacter(1, leader); const followerId = harness.simulation.spawnCharacter(2, follower); harness.simulation.enqueue(2, { type: "follow", characterId: follower.character_id, targetEntityId: leaderId }); harness.simulation.step(); harness.simulation.enqueue(1, { type: "move-to", characterId: leader.character_id, tileX: 12, tileY: 8 }); stepTimes(harness.simulation, 30); const followerComps = componentsOf(harness.simulation, followerId); const leaderComps = componentsOf(harness.simulation, leaderId); assert.equal(followerComps.follow.targetEntityId, leaderId); const dist = Math.max( Math.abs(followerComps.position.tileX - leaderComps.position.tileX), Math.abs(followerComps.position.tileY - leaderComps.position.tileY) ); assert.ok(dist >= 1); assert.ok(dist <= 3); assert.ok( followerComps.position.tileX > 6, "follower should have advanced along the leader path" ); } finally { harness.close(); } });