| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { NAV_STUCK_MS } from "../../shared/protocol/constants.js"; |
| 4 | import { MobId } from "../../shared/definitions/mobs.js"; |
| 5 | import { Component } from "../src/ecs/components.js"; |
| 6 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 7 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 8 | |
| 9 | function workerOf (simulation, entityId) |
| 10 | { |
| 11 | const record = simulation.entityRecord(entityId); |
| 12 | assert.ok(record); |
| 13 | return simulation.workers[record.workerId]; |
| 14 | } |
| 15 | |
| 16 | test("navigation stuck for 5s repaths around a blocker", () => |
| 17 | { |
| 18 | const harness = makeSimulation({ workerCount: 1 }); |
| 19 | try |
| 20 | { |
| 21 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 22 | harness.simulation.spawnCharacter(1, character); |
| 23 | const moverId = harness.simulation.spawnEntity(mobComponents({ |
| 24 | tileX: 8, |
| 25 | tileY: 9, |
| 26 | mobTypeId: MobId.deer |
| 27 | })); |
| 28 | harness.simulation.spawnEntity(mobComponents({ |
| 29 | tileX: 9, |
| 30 | tileY: 9, |
| 31 | mobTypeId: MobId.deer |
| 32 | })); |
| 33 | harness.simulation.step(); |
| 34 | |
| 35 | const worker = workerOf(harness.simulation, moverId); |
| 36 | const handle = worker.world.ecs.handleOf(moverId); |
| 37 | const path = worker.world.ecs.get(handle, Component.path); |
| 38 | const position = worker.world.ecs.get(handle, Component.position); |
| 39 | path.tiles = [ |
| 40 | { x: 9, y: 9 }, |
| 41 | { x: 10, y: 9 }, |
| 42 | { x: 11, y: 9 } |
| 43 | ]; |
| 44 | path.stuckSince = harness.now(); |
| 45 | path.stuckTileX = position.tileX; |
| 46 | path.stuckTileY = position.tileY; |
| 47 | |
| 48 | harness.advance(1_000); |
| 49 | harness.simulation.step(); |
| 50 | assert.equal(position.tileX, 8); |
| 51 | assert.equal(position.tileY, 9); |
| 52 | assert.equal(path.tiles[0]?.x, 9); |
| 53 | assert.equal(path.tiles[0]?.y, 9); |
| 54 | |
| 55 | harness.advance(NAV_STUCK_MS); |
| 56 | harness.simulation.step(); |
| 57 | assert.equal(position.tileX, 8); |
| 58 | assert.equal(position.tileY, 9); |
| 59 | const next = path.tiles[0]; |
| 60 | assert.ok(next, "expected a repath with remaining steps"); |
| 61 | assert.ok(!(next.x === 9 && next.y === 9), "repath should not keep stepping into the blocker"); |
| 62 | } |
| 63 | finally |
| 64 | { harness.close(); } |
| 65 | }); |