BlueGit

timfa questward

Files

questward / server / tests / follow.test.js

follow.test.js

follow.test.js 5.9 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { GAIT } from "../../shared/protocol/constants.js";
4import { MobId } from "../../shared/definitions/mobs.js";
5import { mobComponents } from "../src/simulation/spawners.js";
6import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js";
7
8function workerOf (simulation, entityId)
9{
10 const record = simulation.entityRecord(entityId);
11 assert.ok(record);
12 return simulation.workers[record.workerId];
13}
14
15function componentsOf (simulation, entityId)
16{
17 const worker = workerOf(simulation, entityId);
18 const handle = worker.world.ecs.handleOf(entityId);
19 assert.ok(handle != null);
20 return worker.world.ecs.componentsOf(handle);
21}
22
23test("follow stays put when already adjacent", () =>
24{
25 const harness = makeSimulation({ workerCount: 1 });
26 try
27 {
28 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
29 const playerId = harness.simulation.spawnCharacter(1, character);
30 const deerId = harness.simulation.spawnEntity(mobComponents({
31 tileX: 9,
32 tileY: 8,
33 mobTypeId: MobId.deer
34 }));
35 harness.simulation.enqueue(1, {
36 type: "follow",
37 characterId: character.character_id,
38 targetEntityId: deerId
39 });
40 harness.simulation.step();
41 const comps = componentsOf(harness.simulation, playerId);
42 assert.equal(comps.follow.targetEntityId, deerId);
43 assert.deepEqual(comps.path?.tiles ?? [], []);
44 }
45 finally
46 { harness.close(); }
47});
48
49test("follow runs when more than three tiles away and breaks past a chunk", () =>
50{
51 const harness = makeSimulation({ workerCount: 1 });
52 try
53 {
54 const character = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 });
55 const playerId = harness.simulation.spawnCharacter(1, character);
56 const deerId = harness.simulation.spawnEntity(mobComponents({
57 tileX: 7,
58 tileY: 8,
59 mobTypeId: MobId.deer
60 }));
61 harness.simulation.enqueue(1, {
62 type: "follow",
63 characterId: character.character_id,
64 targetEntityId: deerId
65 });
66 harness.simulation.step();
67 assert.equal(componentsOf(harness.simulation, playerId).movement.gait, GAIT.run);
68 assert.ok((componentsOf(harness.simulation, playerId).path?.tiles?.length ?? 0) > 0);
69
70 const farDeer = harness.simulation.spawnEntity(mobComponents({
71 tileX: 20,
72 tileY: 8,
73 mobTypeId: MobId.deer
74 }));
75 harness.simulation.enqueue(1, {
76 type: "follow",
77 characterId: character.character_id,
78 targetEntityId: farDeer
79 });
80 harness.simulation.step();
81 assert.equal(componentsOf(harness.simulation, playerId).follow.targetEntityId, null);
82 }
83 finally
84 { harness.close(); }
85});
86
87test("follow matches target gait within three tiles and works on any mobile", () =>
88{
89 const harness = makeSimulation({ workerCount: 1 });
90 try
91 {
92 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
93 const playerId = harness.simulation.spawnCharacter(1, character);
94 const ratId = harness.simulation.spawnEntity(mobComponents({
95 tileX: 11,
96 tileY: 8,
97 mobTypeId: MobId.rat
98 }));
99 const ratHandle = workerOf(harness.simulation, ratId).world.ecs.handleOf(ratId);
100 workerOf(harness.simulation, ratId).world.ecs.get(ratHandle, "movement").gait = GAIT.run;
101
102 harness.simulation.enqueue(1, {
103 type: "follow",
104 characterId: character.character_id,
105 targetEntityId: ratId
106 });
107 harness.simulation.step();
108 assert.equal(componentsOf(harness.simulation, playerId).movement.gait, GAIT.run);
109
110 const pigeonId = harness.simulation.spawnEntity(mobComponents({
111 tileX: 10,
112 tileY: 8,
113 mobTypeId: MobId.pigeon
114 }));
115 harness.simulation.enqueue(1, {
116 type: "follow",
117 characterId: character.character_id,
118 targetEntityId: pigeonId
119 });
120 harness.simulation.step();
121 assert.equal(componentsOf(harness.simulation, playerId).follow.targetEntityId, pigeonId);
122 }
123 finally
124 { harness.close(); }
125});
126
127test("move-to clears follow", () =>
128{
129 const harness = makeSimulation({ workerCount: 1 });
130 try
131 {
132 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
133 const playerId = harness.simulation.spawnCharacter(1, character);
134 const deerId = harness.simulation.spawnEntity(mobComponents({
135 tileX: 10,
136 tileY: 8,
137 mobTypeId: MobId.deer
138 }));
139 harness.simulation.enqueue(1, {
140 type: "follow",
141 characterId: character.character_id,
142 targetEntityId: deerId
143 });
144 harness.simulation.step();
145 harness.simulation.enqueue(1, {
146 type: "move-to",
147 characterId: character.character_id,
148 tileX: 4,
149 tileY: 8
150 });
151 harness.simulation.step();
152 assert.equal(componentsOf(harness.simulation, playerId).follow.targetEntityId, null);
153 }
154 finally
155 { harness.close(); }
156});
157
158test("follower reuses the target vacated trail and waits off their tile", () =>
159{
160 const harness = makeSimulation({ workerCount: 1 });
161 try
162 {
163 const leader = makeCharacter(harness.stateDb, { firstName: "Lead", tileX: 8, tileY: 8 });
164 const follower = makeCharacter(harness.stateDb, { firstName: "Follow", lastName: "Bot", tileX: 6, tileY: 8 });
165 const leaderId = harness.simulation.spawnCharacter(1, leader);
166 const followerId = harness.simulation.spawnCharacter(2, follower);
167
168 harness.simulation.enqueue(2, {
169 type: "follow",
170 characterId: follower.character_id,
171 targetEntityId: leaderId
172 });
173 harness.simulation.step();
174
175 harness.simulation.enqueue(1, {
176 type: "move-to",
177 characterId: leader.character_id,
178 tileX: 12,
179 tileY: 8
180 });
181 stepTimes(harness.simulation, 30);
182
183 const followerComps = componentsOf(harness.simulation, followerId);
184 const leaderComps = componentsOf(harness.simulation, leaderId);
185 assert.equal(followerComps.follow.targetEntityId, leaderId);
186 const dist = Math.max(
187 Math.abs(followerComps.position.tileX - leaderComps.position.tileX),
188 Math.abs(followerComps.position.tileY - leaderComps.position.tileY)
189 );
190 assert.ok(dist >= 1);
191 assert.ok(dist <= 3);
192 assert.ok(
193 followerComps.position.tileX > 6,
194 "follower should have advanced along the leader path"
195 );
196 }
197 finally
198 { harness.close(); }
199});