| 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 | import |
| 9 | { |
| 10 | OVERWORLD_ID, |
| 11 | DimensionLightingMode, |
| 12 | DimensionSeasonMode |
| 13 | } from "../../../shared/world/dimensions.js"; |
| 14 | |
| 15 | let surname = 1; |
| 16 | |
| 17 | export function writeOpenChunk (worldDb, chunkX, chunkY, dimensionId = OVERWORLD_ID) |
| 18 | { |
| 19 | const data = createEmptyChunkData(); |
| 20 | data.open = true; |
| 21 | worldDb.saveChunkVersion( |
| 22 | chunkX, |
| 23 | chunkY, |
| 24 | worldDb.getActiveRevisionId(), |
| 25 | true, |
| 26 | encodeChunkBlob(data), |
| 27 | dimensionId |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | /** Register a custom dimension on the active revision (idempotent enough for tests). */ |
| 32 | export function ensureDimension (worldDb, dimensionId, options = {}) |
| 33 | { |
| 34 | const rev = worldDb.getActiveRevisionId(); |
| 35 | worldDb.saveDimension(rev, { |
| 36 | dimensionId, |
| 37 | displayName: options.displayName ?? String(dimensionId), |
| 38 | lightingMode: options.lightingMode ?? DimensionLightingMode.alwaysLit, |
| 39 | seasonMode: options.seasonMode ?? DimensionSeasonMode.summer, |
| 40 | defaultGroundTypeId: options.defaultGroundTypeId ?? 1 |
| 41 | }); |
| 42 | return rev; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Overworld strip + one open chunk (and dimension row) for a custom dimensionId. |
| 47 | * Optional mutateCave(data) can seed blocks/spawners before save. |
| 48 | */ |
| 49 | export function makeDimensionSimulation (dimensionId, options = {}) |
| 50 | { |
| 51 | const harness = makeSimulation({ |
| 52 | workerCount: options.workerCount ?? 1, |
| 53 | workerMode: options.workerMode, |
| 54 | maxChunkX: options.maxChunkX ?? 2, |
| 55 | extraChunks: options.extraChunks, |
| 56 | now: options.now, |
| 57 | graceMs: options.graceMs, |
| 58 | pinRegion: options.pinRegion |
| 59 | }); |
| 60 | ensureDimension(harness.worldDb, dimensionId, options.dimension); |
| 61 | const data = createEmptyChunkData(); |
| 62 | data.open = true; |
| 63 | if (typeof options.mutateChunk === "function") |
| 64 | { options.mutateChunk(data); } |
| 65 | harness.worldDb.saveChunkVersion( |
| 66 | options.chunkX ?? 0, |
| 67 | options.chunkY ?? 0, |
| 68 | harness.worldDb.getActiveRevisionId(), |
| 69 | true, |
| 70 | encodeChunkBlob(data), |
| 71 | dimensionId |
| 72 | ); |
| 73 | harness.simulation.reloadWorld(); |
| 74 | return harness; |
| 75 | } |
| 76 | |
| 77 | export function openChunkStrip (worldDb, maxChunkX = 5) |
| 78 | { |
| 79 | for (let chunkX = 0; chunkX <= maxChunkX; chunkX++) |
| 80 | { writeOpenChunk(worldDb, chunkX, 0); } |
| 81 | } |
| 82 | |
| 83 | export function makeSimulation (options = {}) |
| 84 | { |
| 85 | const dir = mkdtempSync(join(tmpdir(), "mmo-workers-")); |
| 86 | const worldDb = new WorldDatabase(join(dir, "world.sqlite")); |
| 87 | const stateDb = new StateDatabase(join(dir, "state.sqlite")); |
| 88 | openChunkStrip(worldDb, options.maxChunkX ?? 5); |
| 89 | for (const coord of options.extraChunks ?? []) |
| 90 | { writeOpenChunk(worldDb, coord[0], coord[1]); } |
| 91 | let now = options.now ?? 1_000; |
| 92 | const simulation = new Simulation(worldDb, stateDb, { |
| 93 | workerCount: options.workerCount ?? 2, |
| 94 | workerMode: options.workerMode ?? "inprocess", |
| 95 | pinRegion: options.pinRegion ?? null, |
| 96 | now: () => now |
| 97 | }); |
| 98 | if (options.graceMs != null) |
| 99 | { simulation.applySettings({ regionUnloadGracePeriodMs: options.graceMs }); } |
| 100 | return { |
| 101 | simulation, |
| 102 | worldDb, |
| 103 | stateDb, |
| 104 | now () |
| 105 | { |
| 106 | return now; |
| 107 | }, |
| 108 | setNow (value) |
| 109 | { |
| 110 | now = value; |
| 111 | }, |
| 112 | advance (ms) |
| 113 | { |
| 114 | now += ms; |
| 115 | }, |
| 116 | close () |
| 117 | { |
| 118 | simulation.forceUnloadAll(); |
| 119 | worldDb.close(); |
| 120 | stateDb.close(); |
| 121 | } |
| 122 | }; |
| 123 | } |
| 124 | |
| 125 | export function makeCharacter (stateDb, options = {}) |
| 126 | { |
| 127 | const lastName = options.lastName ?? `Worker${surname++}`; |
| 128 | const account = stateDb.createAccount(`admin-${lastName.toLowerCase()}`, true); |
| 129 | return stateDb.createCharacter( |
| 130 | account.account_id, |
| 131 | options.firstName ?? "Ada", |
| 132 | lastName, |
| 133 | options.sex ?? "female", |
| 134 | options.tileX ?? 8, |
| 135 | options.tileY ?? 8 |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | export function pinEastSplit (regionX) |
| 140 | { |
| 141 | return regionX >= 1 ? 1 : 0; |
| 142 | } |
| 143 | |
| 144 | export function stepTimes (simulation, count) |
| 145 | { |
| 146 | const events = []; |
| 147 | for (let i = 0; i < count; i++) |
| 148 | { events.push(...simulation.step().events); } |
| 149 | return events; |
| 150 | } |
| 151 | |
| 152 | export function walkTicks (tiles = 1) |
| 153 | { |
| 154 | return tiles * 10; |
| 155 | } |