Region file format
This commit is contained in:
+133
-13
@@ -5,12 +5,15 @@ import { AIR_ID, BlockChange } from "$/common/protocol.ts";
|
||||
import { block_value, chunk_key, default_block_value } from "$/common/utils.ts";
|
||||
import { LruMap } from "./lru.ts";
|
||||
import { CYCLE_TICKS } from "$/common/time.ts";
|
||||
import { CHUNK_VOLUME, ChunkBlocks, decode_chunk, encode_chunk, is_changed, SavedChunk } from "./saves.ts";
|
||||
|
||||
// generation only, rebuilt when needed. 256 KB each
|
||||
const RAW_CACHE_SIZE = 128;
|
||||
const CHUNK_CACHE_SIZE = 256;
|
||||
// how far from the middle of the world to look for dry land to spawn on, in chunks
|
||||
const SPAWN_SEARCH_CHUNKS = 32;
|
||||
// generated for worldgen_fingerprint, spread out so most changes to world generation touch at least one
|
||||
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
|
||||
export interface Tile {
|
||||
@@ -38,8 +41,10 @@ export class ServerWorld {
|
||||
#raw = new LruMap<number, RawChunk>(RAW_CACHE_SIZE);
|
||||
#chunks = new LruMap<number, Uint32Array>(CHUNK_CACHE_SIZE);
|
||||
|
||||
// changes from generated terrain, per chunk. these and the tiles are what gets saved
|
||||
// changes from generated terrain, per chunk, which is also what clients get sent. saved as whole chunks
|
||||
#changes = new Map<number, Map<string, BlockChange>>();
|
||||
// chunks changed since they were last saved, by chunk_key
|
||||
#unsaved = new Map<number, [number, number]>();
|
||||
tiles = new Map<string, Tile>();
|
||||
|
||||
// ticks since 06:00 of day 1, see common/time.ts
|
||||
@@ -159,10 +164,120 @@ export class ServerWorld {
|
||||
return all;
|
||||
}
|
||||
|
||||
load_changes(changes: BlockChange[]) {
|
||||
for (const [x, y, z, id, state] of changes) {
|
||||
this.#record_change(x, y, z, id, state ?? 0);
|
||||
#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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
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
|
||||
save_chunks(): SavedChunk[] {
|
||||
const saved = [...this.#unsaved.values()].map(([x, z]) => ({
|
||||
x,
|
||||
z,
|
||||
data: encode_chunk(this.#chunk_blocks(x, z)),
|
||||
}));
|
||||
this.#unsaved.clear();
|
||||
return saved;
|
||||
}
|
||||
|
||||
#chunk_blocks(chunk_x: number, chunk_z: number): ChunkBlocks {
|
||||
const palette: [string, number][] = [];
|
||||
const palette_index = new Map<string, number>();
|
||||
const index_of = (id: string, state: number) => {
|
||||
const key = `${id} ${state}`;
|
||||
let index = palette_index.get(key);
|
||||
if (index === undefined) {
|
||||
index = palette.push([id, state]) - 1;
|
||||
palette_index.set(key, index);
|
||||
}
|
||||
return index;
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
return { palette, blocks, changed };
|
||||
}
|
||||
|
||||
get_tile(x: number, y: number, z: number) {
|
||||
@@ -187,6 +302,7 @@ export class ServerWorld {
|
||||
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)]);
|
||||
}
|
||||
|
||||
#get_raw(chunk_x: number, chunk_z: number) {
|
||||
@@ -209,8 +325,20 @@ export class ServerWorld {
|
||||
return blocks;
|
||||
}
|
||||
|
||||
// same rules as the client: own blocks, then neighbors' leaves only into air, then player changes
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
return blocks;
|
||||
}
|
||||
|
||||
// own blocks, then neighbors' leaves only into air
|
||||
#generate_chunk(chunk_x: number, chunk_z: number) {
|
||||
const blocks = this.#get_raw(chunk_x, chunk_z).blocks.slice();
|
||||
|
||||
for (let dx = -1; dx <= 1; dx++) {
|
||||
@@ -231,14 +359,6 @@ export class ServerWorld {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return blocks;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user