| 1 | import { mkdtempSync } from "node:fs"; |
| 2 | import { tmpdir } from "node:os"; |
| 3 | import { join } from "node:path"; |
| 4 | import { WorldDatabase } from "../../src/persistence/worldDb.js"; |
| 5 | import { StateDatabase } from "../../src/persistence/stateDb.js"; |
| 6 | import { Simulation } from "../../src/simulation/simulation.js"; |
| 7 | import { createEmptyChunkData, encodeChunkBlob } from "../../src/world/chunkData.js"; |
| 8 | |
| 9 | let surname = 1; |
| 10 | |
| 11 | export function writeOpenChunk (worldDb, chunkX, chunkY) |
| 12 | { |
| 13 | const data = createEmptyChunkData(); |
| 14 | data.open = true; |
| 15 | worldDb.saveChunkVersion( |
| 16 | chunkX, |
| 17 | chunkY, |
| 18 | worldDb.getActiveRevisionId(), |
| 19 | true, |
| 20 | encodeChunkBlob(data) |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | export function openChunkStrip (worldDb, maxChunkX = 5) |
| 25 | { |
| 26 | for (let chunkX = 0; chunkX <= maxChunkX; chunkX++) |
| 27 | { writeOpenChunk(worldDb, chunkX, 0); } |
| 28 | } |
| 29 | |
| 30 | export function makeSimulation (options = {}) |
| 31 | { |
| 32 | const dir = mkdtempSync(join(tmpdir(), "mmo-workers-")); |
| 33 | const worldDb = new WorldDatabase(join(dir, "world.sqlite")); |
| 34 | const stateDb = new StateDatabase(join(dir, "state.sqlite")); |
| 35 | openChunkStrip(worldDb, options.maxChunkX ?? 5); |
| 36 | for (const coord of options.extraChunks ?? []) |
| 37 | { writeOpenChunk(worldDb, coord[0], coord[1]); } |
| 38 | let now = options.now ?? 1_000; |
| 39 | const simulation = new Simulation(worldDb, stateDb, { |
| 40 | workerCount: options.workerCount ?? 2, |
| 41 | workerMode: options.workerMode ?? "inprocess", |
| 42 | pinRegion: options.pinRegion ?? null, |
| 43 | now: () => now |
| 44 | }); |
| 45 | if (options.graceMs != null) |
| 46 | { simulation.applySettings({ regionUnloadGracePeriodMs: options.graceMs }); } |
| 47 | return { |
| 48 | simulation, |
| 49 | worldDb, |
| 50 | stateDb, |
| 51 | now () |
| 52 | { |
| 53 | return now; |
| 54 | }, |
| 55 | setNow (value) |
| 56 | { |
| 57 | now = value; |
| 58 | }, |
| 59 | advance (ms) |
| 60 | { |
| 61 | now += ms; |
| 62 | }, |
| 63 | close () |
| 64 | { |
| 65 | simulation.forceUnloadAll(); |
| 66 | worldDb.close(); |
| 67 | stateDb.close(); |
| 68 | } |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | export function makeCharacter (stateDb, options = {}) |
| 73 | { |
| 74 | const lastName = options.lastName ?? `Worker${surname++}`; |
| 75 | const account = stateDb.createAccount(`admin-${lastName.toLowerCase()}`, true); |
| 76 | return stateDb.createCharacter( |
| 77 | account.account_id, |
| 78 | options.firstName ?? "Ada", |
| 79 | lastName, |
| 80 | options.sex ?? "female", |
| 81 | options.tileX ?? 8, |
| 82 | options.tileY ?? 8 |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | export function pinEastSplit (regionX) |
| 87 | { |
| 88 | return regionX >= 1 ? 1 : 0; |
| 89 | } |
| 90 | |
| 91 | export function stepTimes (simulation, count) |
| 92 | { |
| 93 | const events = []; |
| 94 | for (let i = 0; i < count; i++) |
| 95 | { events.push(...simulation.step().events); } |
| 96 | return events; |
| 97 | } |
| 98 | |
| 99 | export function walkTicks (tiles = 1) |
| 100 | { |
| 101 | return tiles * 10; |
| 102 | } |