Send chunks to players connected to server

This commit is contained in:
2026-09-28 11:14:40 -03:00
parent 677ad8115a
commit ded9a7e5d0
26 changed files with 920 additions and 357 deletions
+19 -6
View File
@@ -5,6 +5,7 @@ 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 { encode_level_chunk } from "$/common/chunk_streaming.ts";
import { CHUNK_VOLUME, ChunkBlocks, decode_chunk, encode_chunk, is_changed, SavedChunk } from "./saves.ts";
// generation only, rebuilt when needed. 256 KB each
@@ -41,7 +42,7 @@ 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, which is also what clients get sent. saved as whole chunks
// 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
#unsaved = new Map<number, [number, number]>();
@@ -156,12 +157,24 @@ export class ServerWorld {
this.on_block_change?.(x, y, z, id, state_bits);
}
all_changes(): BlockChange[] {
const all: BlockChange[] = [];
for (const chunk_changes of this.#changes.values()) {
all.push(...chunk_changes.values());
// what clients are sent, the whole chunk as it is now. block values use this world's numeric ids, see block_table
level_chunk(chunk_x: number, chunk_z: number): Uint8Array<ArrayBuffer> {
return encode_level_chunk({
x: chunk_x,
z: chunk_z,
blocks: this.#get_chunk(chunk_x, chunk_z),
biomes: this.#get_raw(chunk_x, chunk_z).biomes,
});
}
// the block id of each numeric id, for clients to read level_chunk with
block_table(): string[] {
const table: string[] = [];
for (const [id, nid] of Object.entries(this.block_ids)) {
table[nid] = id;
}
return all;
table[AIR] = AIR_ID;
return table;
}
#fingerprint: string | undefined;