| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { GAIT } from "../../shared/protocol/constants.js"; |
| 4 | import { MobId } from "../../shared/definitions/mobs.js"; |
| 5 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 6 | import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js"; |
| 7 | |
| 8 | function workerOf (simulation, entityId) |
| 9 | { |
| 10 | const record = simulation.entityRecord(entityId); |
| 11 | assert.ok(record); |
| 12 | return simulation.workers[record.workerId]; |
| 13 | } |
| 14 | |
| 15 | function 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 | |
| 23 | test("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 | |
| 49 | test("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 | |
| 87 | test("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 | |
| 127 | test("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 | |
| 158 | test("move-to paths into the static chunk ring outside the 3x3 entity view", () => |
| 159 | { |
| 160 | const harness = makeSimulation({ workerCount: 1 }); |
| 161 | try |
| 162 | { |
| 163 | // Player in chunk 0,0: VISIBLE bounds end at tile 31; STATIC at 47. |
| 164 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 165 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 166 | harness.simulation.enqueue(1, { |
| 167 | type: "move-to", |
| 168 | characterId: character.character_id, |
| 169 | tileX: 40, |
| 170 | tileY: 8 |
| 171 | }); |
| 172 | harness.simulation.step(); |
| 173 | const path = componentsOf(harness.simulation, playerId).path?.tiles ?? []; |
| 174 | assert.ok(path.length > 0, "expected a path into the static map ring"); |
| 175 | const goal = path[path.length - 1]; |
| 176 | assert.equal(goal.x, 40); |
| 177 | assert.equal(goal.y, 8); |
| 178 | } |
| 179 | finally |
| 180 | { harness.close(); } |
| 181 | }); |
| 182 | |
| 183 | test("follower reuses the target vacated trail and waits off their tile", () => |
| 184 | { |
| 185 | const harness = makeSimulation({ workerCount: 1 }); |
| 186 | try |
| 187 | { |
| 188 | const leader = makeCharacter(harness.stateDb, { firstName: "Lead", tileX: 8, tileY: 8 }); |
| 189 | const follower = makeCharacter(harness.stateDb, { firstName: "Follow", lastName: "Bot", tileX: 6, tileY: 8 }); |
| 190 | const leaderId = harness.simulation.spawnCharacter(1, leader); |
| 191 | const followerId = harness.simulation.spawnCharacter(2, follower); |
| 192 | |
| 193 | harness.simulation.enqueue(2, { |
| 194 | type: "follow", |
| 195 | characterId: follower.character_id, |
| 196 | targetEntityId: leaderId |
| 197 | }); |
| 198 | harness.simulation.step(); |
| 199 | |
| 200 | harness.simulation.enqueue(1, { |
| 201 | type: "move-to", |
| 202 | characterId: leader.character_id, |
| 203 | tileX: 12, |
| 204 | tileY: 8 |
| 205 | }); |
| 206 | stepTimes(harness.simulation, 30); |
| 207 | |
| 208 | const followerComps = componentsOf(harness.simulation, followerId); |
| 209 | const leaderComps = componentsOf(harness.simulation, leaderId); |
| 210 | assert.equal(followerComps.follow.targetEntityId, leaderId); |
| 211 | const dist = Math.max( |
| 212 | Math.abs(followerComps.position.tileX - leaderComps.position.tileX), |
| 213 | Math.abs(followerComps.position.tileY - leaderComps.position.tileY) |
| 214 | ); |
| 215 | assert.ok(dist >= 1); |
| 216 | assert.ok(dist <= 2, `follower should settle adjacent, dist=${dist}`); |
| 217 | assert.ok( |
| 218 | followerComps.position.tileX > 6, |
| 219 | "follower should have advanced along the leader path" |
| 220 | ); |
| 221 | } |
| 222 | finally |
| 223 | { harness.close(); } |
| 224 | }); |
| 225 | |
| 226 | test("walking follower keeps pace at short range without stalling walkCarry", () => |
| 227 | { |
| 228 | const harness = makeSimulation({ workerCount: 1 }); |
| 229 | try |
| 230 | { |
| 231 | harness.simulation.applySettings({ walkTilesPerSecond: 2, simulationTickHz: 20 }); |
| 232 | const leader = makeCharacter(harness.stateDb, { firstName: "Lead", tileX: 8, tileY: 8 }); |
| 233 | const follower = makeCharacter(harness.stateDb, { firstName: "Follow", lastName: "Walk", tileX: 6, tileY: 8 }); |
| 234 | const leaderId = harness.simulation.spawnCharacter(1, leader); |
| 235 | const followerId = harness.simulation.spawnCharacter(2, follower); |
| 236 | |
| 237 | harness.simulation.enqueue(2, { |
| 238 | type: "follow", |
| 239 | characterId: follower.character_id, |
| 240 | targetEntityId: leaderId |
| 241 | }); |
| 242 | harness.simulation.step(); |
| 243 | assert.equal(componentsOf(harness.simulation, followerId).movement.gait, GAIT.walk); |
| 244 | |
| 245 | harness.simulation.enqueue(1, { |
| 246 | type: "move-to", |
| 247 | characterId: leader.character_id, |
| 248 | tileX: 14, |
| 249 | tileY: 8 |
| 250 | }); |
| 251 | |
| 252 | // Stay in the walk-match band (dist 2–3): systems ticks must not wipe walkCarry. |
| 253 | stepTimes(harness.simulation, 24); |
| 254 | const followerComps = componentsOf(harness.simulation, followerId); |
| 255 | const leaderComps = componentsOf(harness.simulation, leaderId); |
| 256 | assert.equal(followerComps.follow.targetEntityId, leaderId); |
| 257 | assert.ok( |
| 258 | followerComps.position.tileX >= 8, |
| 259 | `walking follower should have moved (at ${followerComps.position.tileX}), leader at ${leaderComps.position.tileX}` |
| 260 | ); |
| 261 | const dist = Math.max( |
| 262 | Math.abs(followerComps.position.tileX - leaderComps.position.tileX), |
| 263 | Math.abs(followerComps.position.tileY - leaderComps.position.tileY) |
| 264 | ); |
| 265 | assert.ok(dist <= 2, `follower fell behind to dist ${dist}`); |
| 266 | } |
| 267 | finally |
| 268 | { harness.close(); } |
| 269 | }); |
| 270 | |
| 271 | test("follower settles on the tile behind an idle leader, not one further back", () => |
| 272 | { |
| 273 | const harness = makeSimulation({ workerCount: 1 }); |
| 274 | try |
| 275 | { |
| 276 | harness.simulation.applySettings({ walkTilesPerSecond: 4, simulationTickHz: 20 }); |
| 277 | const leader = makeCharacter(harness.stateDb, { firstName: "Lead", tileX: 8, tileY: 8 }); |
| 278 | const follower = makeCharacter(harness.stateDb, { firstName: "Follow", lastName: "Close", tileX: 5, tileY: 8 }); |
| 279 | const leaderId = harness.simulation.spawnCharacter(1, leader); |
| 280 | const followerId = harness.simulation.spawnCharacter(2, follower); |
| 281 | |
| 282 | harness.simulation.enqueue(2, { |
| 283 | type: "follow", |
| 284 | characterId: follower.character_id, |
| 285 | targetEntityId: leaderId |
| 286 | }); |
| 287 | harness.simulation.step(); |
| 288 | harness.simulation.enqueue(1, { |
| 289 | type: "move-to", |
| 290 | characterId: leader.character_id, |
| 291 | tileX: 14, |
| 292 | tileY: 8 |
| 293 | }); |
| 294 | stepTimes(harness.simulation, 60); |
| 295 | |
| 296 | const followerComps = componentsOf(harness.simulation, followerId); |
| 297 | const leaderComps = componentsOf(harness.simulation, leaderId); |
| 298 | assert.equal(leaderComps.position.tileX, 14); |
| 299 | assert.equal(followerComps.follow.targetEntityId, leaderId); |
| 300 | const dist = Math.max( |
| 301 | Math.abs(followerComps.position.tileX - leaderComps.position.tileX), |
| 302 | Math.abs(followerComps.position.tileY - leaderComps.position.tileY) |
| 303 | ); |
| 304 | assert.equal(dist, 1, `expected adjacent settle, follower=${followerComps.position.tileX} leader=${leaderComps.position.tileX}`); |
| 305 | } |
| 306 | finally |
| 307 | { harness.close(); } |
| 308 | }); |