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
+17 -18
View File
@@ -69,8 +69,8 @@ import { update_crafting_result } from "./crafting.ts";
import type { Menu } from "./menu.ts";
import { SavedPlayer, ServerPlayer } from "./player.ts";
import { encode_skin, MAX_SKIN_BYTES, skin_problem } from "./skins.ts";
import { FINGERPRINT_CHUNKS, ServerWorld, SPAWN_AREA_CHUNKS, Tile } from "./world.ts";
import type { WorldSave } from "./saves.ts";
import { ServerWorld, SPAWN_AREA_CHUNKS, Tile } from "./world.ts";
import type { WorldSave, WorldSource } from "./saves.ts";
import {
BLOCK_DROP_PICKUP_DELAY,
ITEM_ENTITY_TYPE,
@@ -135,8 +135,6 @@ export interface GameHost {
export interface SavedLevel {
version: 5;
seed: string;
// see ServerWorld.worldgen_fingerprint
worldgen: string;
// world time in ticks
time: number;
tiles: SavedTile[];
@@ -206,7 +204,7 @@ export class GameServer {
// world.fallbacks each second for the last minute, for /tps
#fallback_history: number[] = [];
constructor(host: GameHost, save: WorldSave | undefined, default_seed: string, mods: GameMods) {
constructor(host: GameHost, save: WorldSource | undefined, default_seed: string, mods: GameMods) {
this.#host = host;
this.#mods = mods;
this.mods = mods.runtime;
@@ -265,9 +263,7 @@ export class GameServer {
},
});
this.mods.storage = upgrade_mod_storage(saved?.mod_storage ?? {});
if (saved) {
this.world.load_chunks(save?.chunks ?? [], saved.worldgen);
}
if (save) this.world.use_saved_chunks(save.chunks);
this.world.time = saved?.time ?? 0;
this.#day = this.world.day;
for (const [id, items] of Object.entries(saved?.containers ?? {})) {
@@ -287,8 +283,8 @@ export class GameServer {
}
this.entities.load(saved?.version === 4 ? upgrade_v4_entities(saved.entities) : saved?.entities ?? []);
this.#saved_players = saved?.players ?? {};
// a new world is saved straight away, and a loaded one when load_chunks had to save chunks again
this.world.dirty = !saved || this.world.has_unsaved_chunks();
// a new world is saved straight away
this.world.dirty = !saved;
this.#register_commands();
// players without the chunk get the block with it when they do, like minecraft
@@ -430,7 +426,7 @@ export class GameServer {
}
}
// level.json and the chunks changed since the last save
// level.json and the chunks generated or changed since the last save
save(): Required<WorldSave> {
for (const player of this.#players.values()) {
this.#saved_players[player.name] = player.save_player();
@@ -438,7 +434,6 @@ export class GameServer {
const level: SavedLevel = {
version: 5,
seed: this.world.seed,
worldgen: this.world.worldgen_fingerprint(),
time: this.world.time,
tiles: [...this.world.tiles.values()].map((tile) => ({
id: tile.id,
@@ -1965,16 +1960,20 @@ export class GameServer {
}
// generates what's needed before players come with the pool, instead of on this thread when they do: the spawn
// search's chunks and the worldgen fingerprint's
// search's chunks
async warm_up() {
const pool = this.worldgen_pool;
if (!pool) return;
const reach = SPAWN_AREA_CHUNKS;
const around_spawn: [number, number][] = [];
for (let x = -reach; x <= reach; x++) for (let z = -reach; z <= reach; z++) around_spawn.push([x, z]);
await pool.prefetch([...around_spawn, ...FINGERPRINT_CHUNKS as [number, number][]]);
// what the chunks in it are built from, their neighbors' spills too. saved chunks need nothing
const reach = SPAWN_AREA_CHUNKS - 1;
const needed = new Map<string, [number, number]>();
for (let x = -reach; x <= reach; x++) {
for (let z = -reach; z <= reach; z++) {
for (const [rx, rz] of this.world.raws_needed(x, z)) needed.set(`${rx},${rz}`, [rx, rz]);
}
}
await pool.prefetch([...needed.values()]);
this.world.spawn_point();
this.world.worldgen_fingerprint();
}
// asks the pool for the raw chunks the nearest chunks each player is waiting for are built from, nearest first.