BlueGit

timfa questward

Files

questward / server / tests / follow.test.js

follow.test.js

follow.test.js 9.1 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 <= 2, `follower should settle adjacent, dist=${dist}`);
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});
200
201test("walking follower keeps pace at short range without stalling walkCarry", () =>
202{
203 const harness = makeSimulation({ workerCount: 1 });
204 try
205 {
206 harness.simulation.applySettings({ walkTilesPerSecond: 2, simulationTickHz: 20 });
207 const leader = makeCharacter(harness.stateDb, { firstName: "Lead", tileX: 8, tileY: 8 });
208 const follower = makeCharacter(harness.stateDb, { firstName: "Follow", lastName: "Walk", tileX: 6, tileY: 8 });
209 const leaderId = harness.simulation.spawnCharacter(1, leader);
210 const followerId = harness.simulation.spawnCharacter(2, follower);
211
212 harness.simulation.enqueue(2, {
213 type: "follow",
214 characterId: follower.character_id,
215 targetEntityId: leaderId
216 });
217 harness.simulation.step();
218 assert.equal(componentsOf(harness.simulation, followerId).movement.gait, GAIT.walk);
219
220 harness.simulation.enqueue(1, {
221 type: "move-to",
222 characterId: leader.character_id,
223 tileX: 14,
224 tileY: 8
225 });
226
227 // Stay in the walk-match band (dist 2–3): systems ticks must not wipe walkCarry.
228 stepTimes(harness.simulation, 24);
229 const followerComps = componentsOf(harness.simulation, followerId);
230 const leaderComps = componentsOf(harness.simulation, leaderId);
231 assert.equal(followerComps.follow.targetEntityId, leaderId);
232 assert.ok(
233 followerComps.position.tileX >= 8,
234 `walking follower should have moved (at ${followerComps.position.tileX}), leader at ${leaderComps.position.tileX}`
235 );
236 const dist = Math.max(
237 Math.abs(followerComps.position.tileX - leaderComps.position.tileX),
238 Math.abs(followerComps.position.tileY - leaderComps.position.tileY)
239 );
240 assert.ok(dist <= 2, `follower fell behind to dist ${dist}`);
241 }
242 finally
243 { harness.close(); }
244});
245
246test("follower settles on the tile behind an idle leader, not one further back", () =>
247{
248 const harness = makeSimulation({ workerCount: 1 });
249 try
250 {
251 harness.simulation.applySettings({ walkTilesPerSecond: 4, simulationTickHz: 20 });
252 const leader = makeCharacter(harness.stateDb, { firstName: "Lead", tileX: 8, tileY: 8 });
253 const follower = makeCharacter(harness.stateDb, { firstName: "Follow", lastName: "Close", tileX: 5, tileY: 8 });
254 const leaderId = harness.simulation.spawnCharacter(1, leader);
255 const followerId = harness.simulation.spawnCharacter(2, follower);
256
257 harness.simulation.enqueue(2, {
258 type: "follow",
259 characterId: follower.character_id,
260 targetEntityId: leaderId
261 });
262 harness.simulation.step();
263 harness.simulation.enqueue(1, {
264 type: "move-to",
265 characterId: leader.character_id,
266 tileX: 14,
267 tileY: 8
268 });
269 stepTimes(harness.simulation, 60);
270
271 const followerComps = componentsOf(harness.simulation, followerId);
272 const leaderComps = componentsOf(harness.simulation, leaderId);
273 assert.equal(leaderComps.position.tileX, 14);
274 assert.equal(followerComps.follow.targetEntityId, leaderId);
275 const dist = Math.max(
276 Math.abs(followerComps.position.tileX - leaderComps.position.tileX),
277 Math.abs(followerComps.position.tileY - leaderComps.position.tileY)
278 );
279 assert.equal(dist, 1, `expected adjacent settle, follower=${followerComps.position.tileX} leader=${leaderComps.position.tileX}`);
280 }
281 finally
282 { harness.close(); }
283});