Save all generated chunks instead of only chunks that were changed
This commit is contained in:
+145
-140
@@ -6,7 +6,7 @@ import {
|
||||
EverythingRegistry,
|
||||
} from "$/common/everything_registry.ts";
|
||||
import { generate_raw_chunk, RawChunk, WorldgenSetup } from "$/common/generation.ts";
|
||||
import { AIR_ID, BlockChange } from "$/common/protocol.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import { block_value, chunk_key } from "$/common/utils.ts";
|
||||
import { block_numbering } from "$/common/worldgen_bundle.ts";
|
||||
import { LruMap } from "./lru.ts";
|
||||
@@ -14,7 +14,7 @@ import { collision_shape_at, EMPTY_SHAPE, type Shape } from "$/common/block_shap
|
||||
import { farming_day } from "$/common/time.ts";
|
||||
import { encode_level_chunk } from "$/common/chunk_streaming.ts";
|
||||
import type { ItemData } from "$/common/inventory.ts";
|
||||
import { CHUNK_VOLUME, ChunkBlocks, decode_chunk, encode_chunk, is_changed, SavedChunk } from "./saves.ts";
|
||||
import { CHUNK_VOLUME, ChunkBlocks, decode_chunk, encode_chunk, SavedChunk, SavedChunks } from "./saves.ts";
|
||||
|
||||
// generation only, rebuilt when needed. 256 KB each. with a worldgen pool, it holds what the pool made until the
|
||||
// chunks are built, a few dozen chunks ahead of what players are sent
|
||||
@@ -32,8 +32,6 @@ const LIGHT_BOX = LIGHT_REACH * 2 + 1;
|
||||
const SPAWN_SEARCH_CHUNKS = 32;
|
||||
// the chunks around the middle the spawn search usually finds land in, with their neighbors, worth generating ahead
|
||||
export const SPAWN_AREA_CHUNKS = 3;
|
||||
// generated for worldgen_fingerprint, spread out so most changes to world generation touch at least one
|
||||
export const FINGERPRINT_CHUNKS = [[0, 0], [7, -3], [-45, 18], [130, -80], [-310, 260], [1000, -1000]];
|
||||
|
||||
// a block with data the server keeps, like where a chest's items are. never sent to clients, but for item
|
||||
export interface Tile {
|
||||
@@ -47,11 +45,19 @@ export interface Tile {
|
||||
item?: ItemData;
|
||||
}
|
||||
|
||||
// a chunk as it is now, loaded from the save or generated
|
||||
interface LoadedChunk {
|
||||
blocks: Uint32Array;
|
||||
biomes: string[];
|
||||
// saved blocks of mods that are gone, by index in the chunk. air in blocks, and saved again until replaced
|
||||
unknown?: Map<number, [string, number]>;
|
||||
}
|
||||
|
||||
export function position_key(x: number, y: number, z: number) {
|
||||
return `${x},${y},${z}`;
|
||||
}
|
||||
|
||||
// the authoritative world: generated terrain plus everything players changed
|
||||
// the authoritative world: chunks from the save, and generated ones where there's nothing saved yet
|
||||
export class ServerWorld {
|
||||
readonly seed: string;
|
||||
readonly block_ids: Record<string, number> = {};
|
||||
@@ -62,9 +68,10 @@ export class ServerWorld {
|
||||
#opacity: Uint8Array;
|
||||
#emission: Uint8Array;
|
||||
|
||||
// what generation made, and the final blocks with neighbors' leaves and player changes applied
|
||||
// what generation made, and the chunks as they are, with neighbors' leaves and player changes. unsaved chunks
|
||||
// that are forgotten are kept in #evicted until they're saved
|
||||
#raw = new LruMap<number, RawChunk>(RAW_CACHE_SIZE);
|
||||
#chunks = new LruMap<number, Uint32Array>(CHUNK_CACHE_SIZE);
|
||||
#chunks = new LruMap<number, LoadedChunk>(CHUNK_CACHE_SIZE, (key, chunk) => this.#evict(key, chunk));
|
||||
// what generation decided each column's biome is, and what it put in neighbors, kept for longer than the rest
|
||||
#biomes = new LruMap<number, string[]>(SPILL_CACHE_SIZE);
|
||||
#spills = new LruMap<number, Int32Array>(SPILL_CACHE_SIZE);
|
||||
@@ -76,10 +83,13 @@ export class ServerWorld {
|
||||
// per column of a chunk, the highest block that isn't air, then the lowest y that sees the sky. see #columns_of
|
||||
#columns = new LruMap<number, Int16Array>(COLUMN_CACHE_SIZE);
|
||||
|
||||
// changes from generated terrain, per chunk. saved as whole chunks
|
||||
#changes = new Map<number, Map<string, BlockChange>>();
|
||||
// chunks changed since they were last saved, by chunk_key
|
||||
// where saved chunks are read from, and which there are, by chunk_key. chunks saved this session are added
|
||||
#saved_chunks: SavedChunks | undefined;
|
||||
#saved = new Set<number>();
|
||||
// chunks generated or changed since they were last saved, by chunk_key
|
||||
#unsaved = new Map<number, [number, number]>();
|
||||
// unsaved chunks #chunks forgot, ready to save, by chunk_key
|
||||
#evicted = new Map<number, SavedChunk>();
|
||||
tiles = new Map<string, Tile>();
|
||||
// the tiles showing an item, by chunk_key, to send with their chunk
|
||||
#item_tiles = new Map<number, Set<Tile>>();
|
||||
@@ -195,8 +205,17 @@ export class ServerWorld {
|
||||
biome_at(x: number, z: number): string {
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
const biomes = this.#biomes.get(chunk_key(chunk_x, chunk_z)) ?? this.#get_raw(chunk_x, chunk_z).biomes;
|
||||
return biomes[column_index(x, z)];
|
||||
return this.#chunk_biomes(chunk_x, chunk_z)[column_index(x, z)];
|
||||
}
|
||||
|
||||
// a saved chunk's are the ones saved with it, which loads it. other chunks' come from generation without building
|
||||
// them
|
||||
#chunk_biomes(chunk_x: number, chunk_z: number) {
|
||||
const key = chunk_key(chunk_x, chunk_z);
|
||||
const chunk = this.#chunks.get(key);
|
||||
if (chunk) return chunk.biomes;
|
||||
if (this.#is_saved(key)) return this.#get_loaded(chunk_x, chunk_z).biomes;
|
||||
return this.#biomes.get(key) ?? this.#get_raw(chunk_x, chunk_z).biomes;
|
||||
}
|
||||
|
||||
// builds the chunk now if it isn't, generating what it needs on this thread
|
||||
@@ -217,7 +236,7 @@ export class ServerWorld {
|
||||
// where in a kept chunk (see has_chunk) the blocks are whose numeric id is set in wanted, as x, y, z. nothing when
|
||||
// the chunk isn't kept
|
||||
find_blocks(chunk_x: number, chunk_z: number, wanted: Uint8Array): [number, number, number][] {
|
||||
const blocks = this.#chunks.get(chunk_key(chunk_x, chunk_z));
|
||||
const blocks = this.#chunks.get(chunk_key(chunk_x, chunk_z))?.blocks;
|
||||
if (!blocks) return [];
|
||||
const found: [number, number, number][] = [];
|
||||
for (let i = 0; i < blocks.length; i++) {
|
||||
@@ -356,14 +375,15 @@ export class ServerWorld {
|
||||
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
const blocks = this.#get_chunk(chunk_x, chunk_z);
|
||||
blocks[index_in_chunk(x, y, z, chunk_x, chunk_z)] = value;
|
||||
const chunk = this.#get_loaded(chunk_x, chunk_z);
|
||||
const index = index_in_chunk(x, y, z, chunk_x, chunk_z);
|
||||
chunk.blocks[index] = value;
|
||||
chunk.unknown?.delete(index);
|
||||
|
||||
this.#columns.delete(chunk_key(chunk_x, chunk_z));
|
||||
this.#mark_unsaved(chunk_x, chunk_z);
|
||||
|
||||
const state_bits = value >>> 16;
|
||||
this.#record_change(x, y, z, id, state_bits);
|
||||
this.dirty = true;
|
||||
this.on_block_change?.(x, y, z, id, state_bits);
|
||||
}
|
||||
|
||||
@@ -372,8 +392,7 @@ export class ServerWorld {
|
||||
return encode_level_chunk({
|
||||
x: chunk_x,
|
||||
z: chunk_z,
|
||||
blocks: this.#get_chunk(chunk_x, chunk_z),
|
||||
biomes: this.#biomes.get(chunk_key(chunk_x, chunk_z)) ?? this.#get_raw(chunk_x, chunk_z).biomes,
|
||||
...this.#get_loaded(chunk_x, chunk_z),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -387,86 +406,25 @@ export class ServerWorld {
|
||||
return table;
|
||||
}
|
||||
|
||||
#fingerprint: string | undefined;
|
||||
|
||||
// a hash of some generated chunks, saved with the world: when it's different world generation changed, like a mod
|
||||
// update, and saved chunks can't be trusted to say which of their blocks generation made
|
||||
worldgen_fingerprint(): string {
|
||||
if (this.#fingerprint === undefined) {
|
||||
// fnv-1a
|
||||
let hash = 0x811c9dc5;
|
||||
const add = (values: ArrayLike<number>) => {
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
hash = Math.imul(hash ^ values[i], 0x01000193) >>> 0;
|
||||
}
|
||||
};
|
||||
for (const [x, z] of FINGERPRINT_CHUNKS) {
|
||||
const raw = this.#get_raw(x, z);
|
||||
add(raw.blocks);
|
||||
add(raw.spills);
|
||||
}
|
||||
this.#fingerprint = hash.toString(16).padStart(8, "0");
|
||||
}
|
||||
return this.#fingerprint;
|
||||
// where to read saved chunks from. they're read when they're needed
|
||||
use_saved_chunks(chunks: SavedChunks) {
|
||||
this.#saved_chunks = chunks;
|
||||
for (const [x, z] of chunks.list) this.#saved.add(chunk_key(x, z));
|
||||
}
|
||||
|
||||
// chunks from a save. with a different fingerprint every block is compared with what generation makes now, so the
|
||||
// saved terrain stays as it was and only chunks never saved get the new generation, like minecraft
|
||||
load_chunks(chunks: SavedChunk[], fingerprint: string | undefined) {
|
||||
const regenerate = chunks.length > 0 && fingerprint !== this.worldgen_fingerprint();
|
||||
if (regenerate) {
|
||||
console.log(`World generation changed, comparing ${chunks.length} saved chunks with it`);
|
||||
}
|
||||
for (const { x: chunk_x, z: chunk_z, data } of chunks) {
|
||||
let chunk: ChunkBlocks;
|
||||
try {
|
||||
chunk = decode_chunk(data);
|
||||
} catch (e) {
|
||||
console.error(`Couldn't load chunk ${chunk_x}, ${chunk_z}: ${(e as Error).message}`);
|
||||
continue;
|
||||
}
|
||||
this.#columns.delete(chunk_key(chunk_x, chunk_z));
|
||||
const generated = regenerate ? this.#generate_chunk(chunk_x, chunk_z) : undefined;
|
||||
// unknown ids, from mods that are gone, stay as changes so they come back with the mod
|
||||
const values = chunk.palette.map(([id, state]) => {
|
||||
const nid = id === AIR_ID ? AIR : this.block_ids[id];
|
||||
return nid === undefined ? -1 : block_value(nid, state);
|
||||
});
|
||||
for (let i = 0; i < CHUNK_VOLUME; i++) {
|
||||
const block = chunk.blocks[i];
|
||||
if (generated ? generated[i] === values[block] : !is_changed(chunk, i)) {
|
||||
continue;
|
||||
}
|
||||
const [id, state] = chunk.palette[block];
|
||||
const x = chunk_x * CHUNK_SIZE + (i % CHUNK_SIZE);
|
||||
const z = chunk_z * CHUNK_SIZE + Math.floor(i / CHUNK_SIZE) % CHUNK_SIZE;
|
||||
this.#record_change(x, Math.floor(i / CHUNK_AREA), z, id, state);
|
||||
}
|
||||
if (regenerate) {
|
||||
this.#unsaved.set(chunk_key(chunk_x, chunk_z), [chunk_x, chunk_z]);
|
||||
this.dirty = true;
|
||||
} else {
|
||||
this.#unsaved.delete(chunk_key(chunk_x, chunk_z));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
has_unsaved_chunks() {
|
||||
return this.#unsaved.size > 0;
|
||||
}
|
||||
|
||||
// every chunk changed since the last call, as saved chunks
|
||||
// every chunk generated or changed since the last call, as saved chunks
|
||||
save_chunks(): SavedChunk[] {
|
||||
const saved = [...this.#unsaved.values()].map(([x, z]) => ({
|
||||
x,
|
||||
z,
|
||||
data: encode_chunk(this.#chunk_blocks(x, z)),
|
||||
}));
|
||||
const saved = [...this.#unsaved].map(([key, [x, z]]) => {
|
||||
const chunk = this.#chunks.peek(key);
|
||||
this.#saved.add(key);
|
||||
return chunk ? { x, z, data: encode_chunk(this.#chunk_blocks(chunk)) } : this.#evicted.get(key)!;
|
||||
});
|
||||
this.#unsaved.clear();
|
||||
this.#evicted.clear();
|
||||
return saved;
|
||||
}
|
||||
|
||||
#chunk_blocks(chunk_x: number, chunk_z: number): ChunkBlocks {
|
||||
#chunk_blocks(chunk: LoadedChunk): Required<ChunkBlocks> {
|
||||
const palette: [string, number][] = [];
|
||||
const palette_index = new Map<string, number>();
|
||||
const index_of = (id: string, state: number) => {
|
||||
@@ -481,27 +439,28 @@ export class ServerWorld {
|
||||
|
||||
const blocks = new Uint16Array(CHUNK_VOLUME);
|
||||
const by_value = new Map<number, number>();
|
||||
this.#get_chunk(chunk_x, chunk_z).forEach((value, i) => {
|
||||
let index = by_value.get(value);
|
||||
if (index === undefined) {
|
||||
const nid = value & ID_MASK;
|
||||
const id = nid === AIR
|
||||
? AIR_ID
|
||||
: EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid)?.id ?? AIR_ID;
|
||||
index = index_of(id, value >>> 16);
|
||||
by_value.set(value, index);
|
||||
// runs of the same block are common, air above the ground and stone below
|
||||
let last_value = -1;
|
||||
let last_index = 0;
|
||||
for (let i = 0; i < CHUNK_VOLUME; i++) {
|
||||
const value = chunk.blocks[i];
|
||||
if (value !== last_value) {
|
||||
let index = by_value.get(value);
|
||||
if (index === undefined) {
|
||||
const nid = value & ID_MASK;
|
||||
const id = nid === AIR
|
||||
? AIR_ID
|
||||
: EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid)?.id ?? AIR_ID;
|
||||
index = index_of(id, value >>> 16);
|
||||
by_value.set(value, index);
|
||||
}
|
||||
last_value = value;
|
||||
last_index = index;
|
||||
}
|
||||
blocks[i] = index;
|
||||
});
|
||||
|
||||
// the changes, which also keeps blocks of mods that are gone
|
||||
const changed = new Uint8Array(CHUNK_VOLUME / 8);
|
||||
for (const [x, y, z, id, state] of this.#changes.get(chunk_key(chunk_x, chunk_z))?.values() ?? []) {
|
||||
const i = index_in_chunk(x, y, z, chunk_x, chunk_z);
|
||||
blocks[i] = index_of(id, state ?? 0);
|
||||
changed[i >> 3] |= 1 << (i & 7);
|
||||
blocks[i] = last_index;
|
||||
}
|
||||
return { palette, blocks, changed };
|
||||
for (const [i, [id, state]] of chunk.unknown ?? []) blocks[i] = index_of(id, state);
|
||||
return { palette, blocks, biomes: chunk.biomes };
|
||||
}
|
||||
|
||||
get_tile(x: number, y: number, z: number) {
|
||||
@@ -547,15 +506,14 @@ export class ServerWorld {
|
||||
return this.#item_tiles.get(chunk_key(chunk_x, chunk_z)) ?? [];
|
||||
}
|
||||
|
||||
#record_change(x: number, y: number, z: number, id: string, state: number) {
|
||||
const key = chunk_key(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
|
||||
let chunk_changes = this.#changes.get(key);
|
||||
if (!chunk_changes) {
|
||||
chunk_changes = new Map();
|
||||
this.#changes.set(key, chunk_changes);
|
||||
}
|
||||
chunk_changes.set(position_key(x, y, z), state ? [x, y, z, id, state] : [x, y, z, id]);
|
||||
this.#unsaved.set(key, [Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE)]);
|
||||
#mark_unsaved(chunk_x: number, chunk_z: number) {
|
||||
this.#unsaved.set(chunk_key(chunk_x, chunk_z), [chunk_x, chunk_z]);
|
||||
this.dirty = true;
|
||||
}
|
||||
|
||||
// whether there's a saved chunk there, which the game's thread reads instead of generating anything
|
||||
#is_saved(key: number) {
|
||||
return this.#saved.has(key) || this.#evicted.has(key);
|
||||
}
|
||||
|
||||
#get_raw(chunk_x: number, chunk_z: number) {
|
||||
@@ -585,16 +543,17 @@ export class ServerWorld {
|
||||
if (!this.#raw.has(key)) this.#keep_raw(key, raw);
|
||||
}
|
||||
|
||||
// whether the chunk is built, or can be without generating anything: its own raw blocks and its neighbors' spills
|
||||
// are here
|
||||
// whether the chunk is built, or can be without generating anything: it's saved, or its own raw blocks and its
|
||||
// neighbors' spills are here
|
||||
can_build(chunk_x: number, chunk_z: number) {
|
||||
return this.raws_needed(chunk_x, chunk_z).length === 0;
|
||||
}
|
||||
|
||||
// the raw chunks building it would generate on this thread: its own, if it isn't built and its blocks aren't here,
|
||||
// and its neighbors' whose spills aren't
|
||||
// the raw chunks building it would generate on this thread: none if it's built or saved, otherwise its own if its
|
||||
// blocks aren't here, and its neighbors' whose spills aren't
|
||||
raws_needed(chunk_x: number, chunk_z: number): [number, number][] {
|
||||
if (this.#chunks.has(chunk_key(chunk_x, chunk_z))) return [];
|
||||
const key = chunk_key(chunk_x, chunk_z);
|
||||
if (this.#chunks.has(key) || this.#is_saved(key)) return [];
|
||||
const needed: [number, number][] = [];
|
||||
if (!this.#raw.has(chunk_key(chunk_x, chunk_z))) needed.push([chunk_x, chunk_z]);
|
||||
for (let dx = -1; dx <= 1; dx++) {
|
||||
@@ -612,25 +571,71 @@ export class ServerWorld {
|
||||
}
|
||||
|
||||
#get_chunk(chunk_x: number, chunk_z: number) {
|
||||
const key = chunk_key(chunk_x, chunk_z);
|
||||
let blocks = this.#chunks.get(key);
|
||||
if (!blocks) {
|
||||
blocks = this.#build_chunk(chunk_x, chunk_z);
|
||||
this.#chunks.set(key, blocks);
|
||||
}
|
||||
return blocks;
|
||||
return this.#get_loaded(chunk_x, chunk_z).blocks;
|
||||
}
|
||||
|
||||
// same rules as the client: what generation makes, then player changes
|
||||
#build_chunk(chunk_x: number, chunk_z: number) {
|
||||
const blocks = this.#generate_chunk(chunk_x, chunk_z);
|
||||
for (const [x, y, z, id, state] of this.#changes.get(chunk_key(chunk_x, chunk_z))?.values() ?? []) {
|
||||
const nid = id === AIR_ID ? AIR : this.block_ids[id];
|
||||
if (nid !== undefined) {
|
||||
blocks[index_in_chunk(x, y, z, chunk_x, chunk_z)] = block_value(nid, state ?? 0);
|
||||
#get_loaded(chunk_x: number, chunk_z: number) {
|
||||
const key = chunk_key(chunk_x, chunk_z);
|
||||
let chunk = this.#chunks.get(key);
|
||||
if (!chunk) {
|
||||
chunk = this.#load(chunk_x, chunk_z);
|
||||
this.#chunks.set(key, chunk);
|
||||
}
|
||||
return chunk;
|
||||
}
|
||||
|
||||
// the saved chunk, or a new one from generation, which is saved from then on like minecraft's
|
||||
#load(chunk_x: number, chunk_z: number): LoadedChunk {
|
||||
const key = chunk_key(chunk_x, chunk_z);
|
||||
if (this.#is_saved(key)) {
|
||||
const evicted = this.#evicted.get(key);
|
||||
this.#evicted.delete(key);
|
||||
const data = evicted?.data ?? this.#saved_chunks?.read(chunk_x, chunk_z);
|
||||
try {
|
||||
if (!data) throw new Error("it couldn't be read");
|
||||
return this.#from_saved(chunk_x, chunk_z, decode_chunk(data));
|
||||
} catch (e) {
|
||||
console.error(
|
||||
`Couldn't load chunk ${chunk_x}, ${chunk_z}, generating it again: ${(e as Error).message}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
return blocks;
|
||||
const blocks = this.#generate_chunk(chunk_x, chunk_z);
|
||||
this.#mark_unsaved(chunk_x, chunk_z);
|
||||
return { blocks, biomes: this.#biomes.get(key) ?? this.#get_raw(chunk_x, chunk_z).biomes };
|
||||
}
|
||||
|
||||
#from_saved(chunk_x: number, chunk_z: number, saved: ChunkBlocks): LoadedChunk {
|
||||
// ids of mods that are gone are air until they're back
|
||||
const values = saved.palette.map(([id, state]) => {
|
||||
const nid = id === AIR_ID ? AIR : this.block_ids[id];
|
||||
return nid === undefined ? undefined : block_value(nid, state);
|
||||
});
|
||||
const blocks = new Uint32Array(CHUNK_VOLUME);
|
||||
let unknown: Map<number, [string, number]> | undefined;
|
||||
for (let i = 0; i < CHUNK_VOLUME; i++) {
|
||||
const value = values[saved.blocks[i]];
|
||||
if (value === undefined) {
|
||||
(unknown ??= new Map()).set(i, saved.palette[saved.blocks[i]]);
|
||||
} else {
|
||||
blocks[i] = value;
|
||||
}
|
||||
}
|
||||
let biomes = saved.biomes;
|
||||
if (!biomes) {
|
||||
// saved before chunks had them. saved again with them
|
||||
biomes = this.#biomes.get(chunk_key(chunk_x, chunk_z)) ?? this.#get_raw(chunk_x, chunk_z).biomes;
|
||||
this.#mark_unsaved(chunk_x, chunk_z);
|
||||
}
|
||||
return { blocks, biomes, unknown };
|
||||
}
|
||||
|
||||
// an unsaved chunk is encoded to be saved, instead of being generated again or kept whole
|
||||
#evict(key: number, chunk: LoadedChunk) {
|
||||
const position = this.#unsaved.get(key);
|
||||
if (!position) return;
|
||||
const [x, z] = position;
|
||||
this.#evicted.set(key, { x, z, data: encode_chunk(this.#chunk_blocks(chunk)) });
|
||||
}
|
||||
|
||||
// own blocks, then neighbors' leaves only into air
|
||||
|
||||
Reference in New Issue
Block a user