Save all generated chunks instead of only chunks that were changed

This commit is contained in:
2026-10-11 02:02:01 -03:00
parent 44a39bf8ec
commit 5a59a63862
17 changed files with 644 additions and 314 deletions
+33 -2
View File
@@ -7,7 +7,7 @@ import { start_game } from "$/server/game/load_mods.ts";
import { PROTOCOL_VERSION } from "$/common/protocol.ts";
import { decode_level_chunk } from "$/common/chunk_streaming.ts";
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "$/common/constants.ts";
import type { WorldSave } from "$/server/game/saves.ts";
import type { SavedChunk, SavedChunks, WorldSave } from "$/server/game/saves.ts";
// mods straight from their source folders, skipping the build: scripts are imported as typescript
export function mod_sources(mods_dir: string): ServerModSource[] {
@@ -53,6 +53,23 @@ export function mod_sources(mods_dir: string): ServerModSource[] {
});
}
// saved chunks kept in memory, the last saved where a chunk is saved more than once
class MemoryChunks implements SavedChunks {
#chunks = new Map<string, SavedChunk>();
get list(): [number, number][] {
return [...this.#chunks.values()].map(({ x, z }) => [x, z]);
}
read(x: number, z: number) {
return this.#chunks.get(`${x},${z}`)?.data;
}
add(chunks: SavedChunk[]) {
for (const chunk of chunks) this.#chunks.set(`${chunk.x},${chunk.z}`, chunk);
}
}
// a game with an in memory host that remembers what it sent to each connection. the seed has low, dry land around
// 0, 0, where players spawn and where tests build things
export async function test_game(mods_dir: string, save?: WorldSave, seed = "test-seed-90") {
@@ -77,7 +94,21 @@ export async function test_game(mods_dir: string, save?: WorldSave, seed = "test
return bytes ? Promise.resolve(bytes) : Promise.reject(new Error("Couldn't download it"));
},
};
const game: GameServer = await start_game(host, save, seed, mod_sources(mods_dir));
const chunks = new MemoryChunks();
if (save) chunks.add(save.chunks);
const game: GameServer = await start_game(
host,
{ level: save?.level, chunks },
seed,
mod_sources(mods_dir),
);
// what it saves is read back from memory, like a host's region files
const save_game = game.save.bind(game);
game.save = () => {
const saved = save_game();
chunks.add(saved.chunks);
return saved;
};
game.chunk_send_budget_ms = Infinity;
// tests that want natural spawning turn it back on
game.do_mob_spawning = false;