World sections

This commit is contained in:
2026-11-06 23:13:34 -03:00
parent 818ce22dd6
commit 1fa47c027d
21 changed files with 392 additions and 127 deletions
+76 -77
View File
@@ -3,6 +3,7 @@
import { AIR, CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import type { WorldShape } from "@/common/world_shape.ts";
import { ChunkColumn, column_heights, SECTION_VOLUME } from "@/common/section.ts";
import {
block_light_emission,
block_light_opacity,
@@ -19,7 +20,8 @@ import { farming_day } from "@/common/time.ts";
import { encode_level_chunk } from "@/common/chunk_streaming.ts";
import { ChunkBlocks, decode_chunk, encode_chunk, SavedChunk, SavedChunks } from "./saves.ts";
// generation only, rebuilt when needed. 256 KB each in a 256 tall dimension. with a worldgen pool, it holds what the pool made until the
// generation only, rebuilt when needed. 16 KB for each section that isn't all one block, about 7 of a 256 tall
// chunk's 16. 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
const RAW_CACHE_SIZE = 256;
const CHUNK_CACHE_SIZE = 256;
@@ -89,9 +91,9 @@ function tile_chunk_key(tile: Tile) {
// 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
column: ChunkColumn;
// saved blocks of mods that are gone, by index in the chunk (see #index). air in column, and saved again until
// replaced
unknown?: Map<number, [string, number]>;
}
@@ -213,8 +215,7 @@ 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);
return blocks[this.#index(x, y, z, chunk_x, chunk_z)] & ID_MASK;
return this.#get_chunk(chunk_x, chunk_z).get(x, y, z) & ID_MASK;
}
// the id with its state bits
@@ -224,7 +225,7 @@ export class ServerWorld {
}
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
return this.#get_chunk(chunk_x, chunk_z)[this.#index(x, y, z, chunk_x, chunk_z)];
return this.#get_chunk(chunk_x, chunk_z).get(x, y, z);
}
// whether entities bump into the block there, water and other blocks with "collision": false don't
@@ -262,9 +263,9 @@ export class ServerWorld {
#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;
if (chunk) return chunk.column.biomes;
if (this.#is_saved(key)) return this.#get_loaded(chunk_x, chunk_z).column.biomes;
return this.#biomes.get(key) ?? this.#get_raw(chunk_x, chunk_z).column.biomes;
}
// builds the chunk now if it isn't, generating what it needs on this thread
@@ -290,27 +291,28 @@ 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
// the chunk isn't kept. sections that are all a block that isn't wanted are passed over
find_blocks(chunk_x: number, chunk_z: number, wanted: Uint8Array): [number, number, number][] {
const blocks = this.#chunks.get(chunk_key(chunk_x, chunk_z))?.blocks;
if (!blocks) return [];
const column = this.#chunks.get(chunk_key(chunk_x, chunk_z))?.column;
if (!column) return [];
const found: [number, number, number][] = [];
for (let i = 0; i < blocks.length; i++) {
if (!wanted[blocks[i] & ID_MASK]) continue;
const y = this.shape.min_y + Math.floor(i / CHUNK_AREA);
const rest = i % CHUNK_AREA;
found.push([
chunk_x * CHUNK_SIZE + rest % CHUNK_SIZE,
y,
chunk_z * CHUNK_SIZE + Math.floor(rest / CHUNK_SIZE),
]);
}
column.sections.forEach((section, s) => {
if (section.blocks === null && !wanted[section.value & ID_MASK]) return;
for (let i = 0; i < SECTION_VOLUME; i++) {
if (!wanted[section.get(i) & ID_MASK]) continue;
found.push([
chunk_x * CHUNK_SIZE + (i & 15),
this.shape.min_y + s * 16 + (i >> 8),
chunk_z * CHUNK_SIZE + ((i >> 4) & 15),
]);
}
});
return found;
}
// what generation makes in a chunk, neighbors' spills included, without what players changed. built again every
// call, for comparing generation (tests/worldgen_golden_test.ts)
generated_blocks(chunk_x: number, chunk_z: number): Uint32Array {
generated_blocks(chunk_x: number, chunk_z: number): ChunkColumn {
return this.#generate_chunk(chunk_x, chunk_z);
}
@@ -350,7 +352,7 @@ export class ServerWorld {
// the box around it, 3x3 chunks at most, fetched once each. blocks are in it by their offset from the start
const first_x = Math.floor((x - LIGHT_REACH) / CHUNK_SIZE);
const first_z = Math.floor((z - LIGHT_REACH) / CHUNK_SIZE);
const chunks: (Uint32Array | undefined)[] = [];
const chunks: (ChunkColumn | undefined)[] = [];
const skies: (Int16Array | undefined)[] = [];
const box_index = (dx: number, dy: number, dz: number) =>
((dy + LIGHT_REACH) * LIGHT_BOX + dz + LIGHT_REACH) * LIGHT_BOX + dx + LIGHT_REACH;
@@ -382,7 +384,7 @@ export class ServerWorld {
const which = (chunk_z - first_z) * 3 + chunk_x - first_x;
const blocks = chunks[which] ??= this.#get_chunk(chunk_x, chunk_z);
const column = (bz - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + bx - chunk_x * CHUNK_SIZE;
const nid = blocks[(by - min_y) * CHUNK_AREA + column] & ID_MASK;
const nid = blocks.get(bx, by, bz) & ID_MASK;
if (sky_light) {
const columns = skies[which] ??= this.#columns_of(bx, bz);
if (by >= columns[CHUNK_AREA + column]) sky = Math.max(sky, LIGHT_REACH - lost);
@@ -408,17 +410,7 @@ export class ServerWorld {
const key = chunk_key(chunk_x, chunk_z);
let columns = this.#columns.get(key);
if (!columns) {
const blocks = this.#get_chunk(chunk_x, chunk_z);
const { min_y, height } = this.shape;
columns = new Int16Array(CHUNK_AREA * 2);
for (let column = 0; column < CHUNK_AREA; column++) {
// from the top, by y above min_y
let y = height - 1;
while (y >= 0 && (blocks[y * CHUNK_AREA + column] & ID_MASK) === AIR) y--;
columns[column] = min_y + y;
while (y >= 0 && this.#opacity[blocks[y * CHUNK_AREA + column] & ID_MASK] === 0) y--;
columns[CHUNK_AREA + column] = min_y + y + 1;
}
columns = column_heights(this.#get_chunk(chunk_x, chunk_z), this.#opacity);
this.#columns.set(key, columns);
}
return columns;
@@ -438,9 +430,8 @@ export class ServerWorld {
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
const chunk = this.#get_loaded(chunk_x, chunk_z);
const index = this.#index(x, y, z, chunk_x, chunk_z);
chunk.blocks[index] = value;
chunk.unknown?.delete(index);
chunk.column.set(x, y, z, value);
chunk.unknown?.delete(this.#index(x, y, z, chunk_x, chunk_z));
this.#columns.delete(chunk_key(chunk_x, chunk_z));
this.#mark_unsaved(chunk_x, chunk_z);
@@ -451,11 +442,7 @@ export class ServerWorld {
// 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,
...this.#get_loaded(chunk_x, chunk_z),
});
return encode_level_chunk(this.#get_loaded(chunk_x, chunk_z).column);
}
// the block id of each numeric id, for clients to read level_chunk with
@@ -499,30 +486,42 @@ export class ServerWorld {
return index;
};
const blocks = new Uint16Array(chunk.blocks.length);
const by_value = new Map<number, number>();
// 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.blocks.length; 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;
const value_index = (value: number) => {
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] = last_index;
}
return index;
};
const { sections, biomes } = chunk.column;
const blocks = new Uint16Array(this.shape.volume);
sections.forEach((section, s) => {
const start = s * SECTION_VOLUME;
if (section.blocks === null) {
blocks.fill(value_index(section.value), start, start + SECTION_VOLUME);
return;
}
// runs of the same block are common, like air above the ground and stone below
let last_value = -1;
let last_index = 0;
for (let i = 0; i < SECTION_VOLUME; i++) {
const value = section.blocks[i];
if (value !== last_value) {
last_value = value;
last_index = value_index(value);
}
blocks[start + i] = last_index;
}
});
for (const [i, [id, state]] of chunk.unknown ?? []) blocks[i] = index_of(id, state);
return { palette, blocks, biomes: chunk.biomes };
return { palette, blocks, biomes };
}
get_tile(x: number, y: number, z: number) {
@@ -612,7 +611,7 @@ export class ServerWorld {
#keep_raw(key: number, raw: RawChunk) {
this.#raw.set(key, raw);
this.#biomes.set(key, raw.biomes);
this.#biomes.set(key, raw.column.biomes);
this.#spills.set(key, raw.spills);
}
@@ -650,7 +649,7 @@ export class ServerWorld {
}
#get_chunk(chunk_x: number, chunk_z: number) {
return this.#get_loaded(chunk_x, chunk_z).blocks;
return this.#get_loaded(chunk_x, chunk_z).column;
}
#get_loaded(chunk_x: number, chunk_z: number) {
@@ -679,9 +678,9 @@ export class ServerWorld {
);
}
}
const blocks = this.#generate_chunk(chunk_x, chunk_z);
const column = 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 };
return { column };
}
#from_saved(chunk_x: number, chunk_z: number, saved: ChunkBlocks): LoadedChunk {
@@ -703,10 +702,11 @@ export class ServerWorld {
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;
const key = chunk_key(chunk_x, chunk_z);
biomes = [...this.#biomes.get(key) ?? this.#get_raw(chunk_x, chunk_z).column.biomes];
this.#mark_unsaved(chunk_x, chunk_z);
}
return { blocks, biomes, unknown };
return { column: ChunkColumn.from_flat(chunk_x, chunk_z, this.shape, blocks, biomes), unknown };
}
// an unsaved chunk is encoded to be saved, instead of being generated again or kept whole
@@ -725,7 +725,7 @@ export class ServerWorld {
// 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();
const column = this.#get_raw(chunk_x, chunk_z).column.clone();
for (let dx = -1; dx <= 1; dx++) {
for (let dz = -1; dz <= 1; dz++) {
@@ -738,14 +738,13 @@ export class ServerWorld {
if (Math.floor(x / CHUNK_SIZE) !== chunk_x || Math.floor(z / CHUNK_SIZE) !== chunk_z) {
continue;
}
const index = this.#index(x, y, z, chunk_x, chunk_z);
if (blocks[index] === AIR) {
blocks[index] = nid;
if (column.get(x, y, z) === AIR) {
column.set(x, y, z, nid);
}
}
}
}
return blocks;
return column;
}
}
+9 -3
View File
@@ -6,6 +6,8 @@
// server/worldgen_workers.ts). requests go nearest first, a worker has a couple at a time so priorities stay fresh, and what the
// game stops wanting is dropped before it starts
import type { RawChunk } from "@/common/generation.ts";
import { ChunkColumn, Section, type SectionData } from "@/common/section.ts";
import { WorldShape } from "@/common/world_shape.ts";
import type { WorldgenBundle } from "@/common/worldgen_bundle.ts";
import { chunk_key } from "@/common/utils.ts";
@@ -16,7 +18,8 @@ export type PoolToWorker =
export type WorkerToPool =
| { type: "ready" }
| { type: "failed"; error: string }
| { type: "chunk"; x: number; z: number; blocks: Uint32Array; spills: Int32Array; biomes: string[] }
// a raw chunk's sections as their data, since what's sent between workers loses its class
| { type: "chunk"; x: number; z: number; sections: SectionData[]; spills: Int32Array; biomes: string[] }
| { type: "error"; x: number; z: number; error: string };
// the game's end of a channel to a worker
@@ -45,6 +48,8 @@ interface Queued {
export class WorldgenPool {
#bundle: WorldgenBundle;
// what chunks it makes hold, from the bundle
#shape: WorldShape;
#slots: Slot[] = [];
#queue = new Map<number, Queued>();
// asked for by prefetch, kept when want() drops the rest
@@ -60,6 +65,7 @@ export class WorldgenPool {
constructor(ports: WorkerPort[], bundle: WorldgenBundle) {
this.#bundle = bundle;
this.#shape = new WorldShape(bundle.shape);
this.#ready = new Promise((resolve, reject) => this.#settle_ready = { resolve, reject });
ports.forEach((port, index) => this.#attach(index, port));
if (ports.length === 0) this.#settle_ready.resolve();
@@ -160,10 +166,10 @@ export class WorldgenPool {
const key = chunk_key(message.x, message.z);
slot.in_flight.delete(key);
this.generated++;
const sections = message.sections.map((data) => new Section(data));
this.on_chunk(message.x, message.z, {
blocks: message.blocks,
column: new ChunkColumn(message.x, message.z, this.#shape, sections, message.biomes),
spills: message.spills,
biomes: message.biomes,
});
this.#done(key);
break;
+4 -2
View File
@@ -33,8 +33,10 @@ self.onmessage = (event: MessageEvent<{ port: MessagePort }>) => {
const { x, z } = message;
try {
const raw = (await loaded!).generate(x, z);
send({ type: "chunk", x, z, blocks: raw.blocks, spills: raw.spills, biomes: raw.biomes }, [
raw.blocks.buffer,
const sections = raw.column.sections.map((section) => section.data);
const buffers = sections.flatMap((data) => typeof data === "number" ? [] : [data.buffer]);
send({ type: "chunk", x, z, sections, spills: raw.spills, biomes: raw.column.biomes }, [
...buffers,
raw.spills.buffer,
]);
} catch (e) {