| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { |
| 4 | makeCharacter, |
| 5 | makeSimulation, |
| 6 | pinEastSplit, |
| 7 | stepTimes, |
| 8 | walkTicks |
| 9 | } from "./helpers/simulationHarness.js"; |
| 10 | |
| 11 | test("a player at a region edge receives snapshots from both workers", () => |
| 12 | { |
| 13 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 14 | try |
| 15 | { |
| 16 | const west = makeCharacter(harness.stateDb, { tileX: 63, tileY: 8 }); |
| 17 | const east = makeCharacter(harness.stateDb, { tileX: 70, tileY: 8, firstName: "Ben" }); |
| 18 | const westId = harness.simulation.spawnCharacter(1, west); |
| 19 | const eastId = harness.simulation.spawnCharacter(2, east); |
| 20 | assert.equal(harness.simulation.entityRecord(westId).workerId, 0); |
| 21 | assert.equal(harness.simulation.entityRecord(eastId).workerId, 1); |
| 22 | |
| 23 | const snapshots = harness.simulation.snapshotFor(westId); |
| 24 | assert.equal(snapshots.length, 9); |
| 25 | const chunkFour = snapshots.find((snapshot) => snapshot.chunkX === 4 && snapshot.chunkY === 0); |
| 26 | assert.ok(chunkFour); |
| 27 | assert.equal(chunkFour.open, true); |
| 28 | assert.ok(chunkFour.entities.some((entity) => entity.entityId === eastId)); |
| 29 | const chunkThree = snapshots.find((snapshot) => snapshot.chunkX === 3 && snapshot.chunkY === 0); |
| 30 | assert.ok(chunkThree.entities.some((entity) => entity.entityId === westId)); |
| 31 | } |
| 32 | finally |
| 33 | { harness.close(); } |
| 34 | }); |
| 35 | |
| 36 | test("entities can path through each other across a region border", () => |
| 37 | { |
| 38 | const harness = makeSimulation({ pinRegion: pinEastSplit, workerCount: 2 }); |
| 39 | try |
| 40 | { |
| 41 | const walker = makeCharacter(harness.stateDb, { tileX: 63, tileY: 8 }); |
| 42 | const blocker = makeCharacter(harness.stateDb, { tileX: 64, tileY: 8, firstName: "Bob" }); |
| 43 | const walkerId = harness.simulation.spawnCharacter(1, walker); |
| 44 | harness.simulation.spawnCharacter(2, blocker); |
| 45 | harness.simulation.enqueue(1, { type: "move-to", characterId: walker.character_id, tileX: 64, tileY: 8 }); |
| 46 | stepTimes(harness.simulation, walkTicks(2)); |
| 47 | assert.equal(harness.simulation.entityRecord(walkerId).tileX, 64); |
| 48 | assert.equal(harness.simulation.entityRecord(walkerId).tileY, 8); |
| 49 | } |
| 50 | finally |
| 51 | { harness.close(); } |
| 52 | }); |
| 53 | |
| 54 | test("local movement still works on a single worker", () => |
| 55 | { |
| 56 | const harness = makeSimulation({ workerCount: 1 }); |
| 57 | try |
| 58 | { |
| 59 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 60 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 61 | harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 10, tileY: 8 }); |
| 62 | stepTimes(harness.simulation, walkTicks(3)); |
| 63 | assert.equal(harness.simulation.entityRecord(entityId).tileX, 10); |
| 64 | assert.equal(harness.simulation.entityRecord(entityId).tileY, 8); |
| 65 | } |
| 66 | finally |
| 67 | { harness.close(); } |
| 68 | }); |