Workers for chunk generation !

This commit is contained in:
2026-10-11 01:15:14 -03:00
parent 3d2b235f64
commit cd3b39c3d7
24 changed files with 1119 additions and 91 deletions
+83 -14
View File
@@ -7,7 +7,8 @@ import {
} from "$/common/everything_registry.ts";
import { generate_raw_chunk, RawChunk, WorldgenSetup } from "$/common/generation.ts";
import { AIR_ID, BlockChange } from "$/common/protocol.ts";
import { block_value, chunk_key, default_block_value } from "$/common/utils.ts";
import { block_value, chunk_key } from "$/common/utils.ts";
import { block_numbering } from "$/common/worldgen_bundle.ts";
import { LruMap } from "./lru.ts";
import { collision_shape_at, EMPTY_SHAPE, type Shape } from "$/common/block_shapes.ts";
import { farming_day } from "$/common/time.ts";
@@ -15,9 +16,13 @@ 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";
// generation only, rebuilt when needed. 256 KB each
const RAW_CACHE_SIZE = 128;
// 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
const RAW_CACHE_SIZE = 256;
const CHUNK_CACHE_SIZE = 256;
// a raw chunk's spills and biomes, a few KB each, kept longer than its blocks: building a chunk needs its own raw
// blocks and only its neighbors' spills
const SPILL_CACHE_SIZE = 4096;
// surface and sky heights, 1 KB each
const COLUMN_CACHE_SIZE = 1024;
// light reaches at most this far, see light_at
@@ -25,8 +30,10 @@ const LIGHT_REACH = 15;
const LIGHT_BOX = LIGHT_REACH * 2 + 1;
// how far from the middle of the world to look for dry land to spawn on, in chunks
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
const FINGERPRINT_CHUNKS = [[0, 0], [7, -3], [-45, 18], [130, -80], [-310, 260], [1000, -1000]];
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 {
@@ -58,8 +65,14 @@ export class ServerWorld {
// what generation made, and the final blocks with neighbors' leaves and player changes applied
#raw = new LruMap<number, RawChunk>(RAW_CACHE_SIZE);
#chunks = new LruMap<number, Uint32Array>(CHUNK_CACHE_SIZE);
// what generation decided each column's biome is, kept for longer than the rest of it
#biomes = new LruMap<number, string[]>(COLUMN_CACHE_SIZE);
// 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);
// raw chunks generated on this thread because nothing had made them yet: with a worldgen pool, what still waits
// on generation. see GameServer's /tps
fallbacks = 0;
// what called for them, by the caller's place in the code, when set. for finding what to move off the fallback
fallback_callers?: Map<string, number>;
// 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);
@@ -88,12 +101,13 @@ export class ServerWorld {
constructor(seed: string, worldgen: WorldgenSetup) {
this.seed = seed;
this.worldgen = worldgen;
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
// numbered the same way as generation in a worker numbers them, see common/worldgen_bundle.ts
const { block_ids, default_values } = block_numbering();
Object.assign(this.block_ids, block_ids);
this.default_values.push(...default_values);
this.#opacity = new Uint8Array(ID_MASK + 1);
this.#emission = new Uint8Array(ID_MASK + 1);
blocks.forEach((block, nid) => {
this.block_ids[block.id] = nid;
this.default_values[nid] = default_block_value(nid, block);
EverythingRegistry.get_registry<BlockRegistry>("blocks").forEach((block, nid) => {
this.#opacity[nid] = block_light_opacity(block);
this.#emission[nid] = block_light_emission(block);
});
@@ -185,6 +199,11 @@ export class ServerWorld {
return biomes[column_index(x, z)];
}
// builds the chunk now if it isn't, generating what it needs on this thread
build(chunk_x: number, chunk_z: number) {
this.#get_chunk(chunk_x, chunk_z);
}
// whether the chunk is built and kept, like a loaded chunk in minecraft. nothing builds one by asking
has_chunk(chunk_x: number, chunk_z: number) {
return this.#chunks.has(chunk_key(chunk_x, chunk_z));
@@ -354,7 +373,7 @@ export class ServerWorld {
x: chunk_x,
z: chunk_z,
blocks: this.#get_chunk(chunk_x, chunk_z),
biomes: this.#get_raw(chunk_x, chunk_z).biomes,
biomes: this.#biomes.get(chunk_key(chunk_x, chunk_z)) ?? this.#get_raw(chunk_x, chunk_z).biomes,
});
}
@@ -543,13 +562,55 @@ export class ServerWorld {
const key = chunk_key(chunk_x, chunk_z);
let raw = this.#raw.get(key);
if (!raw) {
this.fallbacks += 1;
if (this.fallback_callers) {
const caller = fallback_caller(new Error().stack ?? "");
this.fallback_callers.set(caller, (this.fallback_callers.get(caller) ?? 0) + 1);
}
raw = generate_raw_chunk(chunk_x, chunk_z, this.seed, this.block_ids, this.worldgen, this.default_values);
this.#raw.set(key, raw);
this.#biomes.set(key, raw.biomes);
this.#keep_raw(key, raw);
}
return raw;
}
#keep_raw(key: number, raw: RawChunk) {
this.#raw.set(key, raw);
this.#biomes.set(key, raw.biomes);
this.#spills.set(key, raw.spills);
}
// a raw chunk a worldgen worker made. generation is the same everywhere, so one made here first is kept
add_raw(chunk_x: number, chunk_z: number, raw: RawChunk) {
const key = chunk_key(chunk_x, chunk_z);
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
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
raws_needed(chunk_x: number, chunk_z: number): [number, number][] {
if (this.#chunks.has(chunk_key(chunk_x, chunk_z))) 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++) {
for (let dz = -1; dz <= 1; dz++) {
if ((dx !== 0 || dz !== 0) && !this.#spills.has(chunk_key(chunk_x + dx, chunk_z + dz))) {
needed.push([chunk_x + dx, chunk_z + dz]);
}
}
}
return needed;
}
#spills_of(chunk_x: number, chunk_z: number) {
return this.#spills.get(chunk_key(chunk_x, chunk_z)) ?? this.#get_raw(chunk_x, chunk_z).spills;
}
#get_chunk(chunk_x: number, chunk_z: number) {
const key = chunk_key(chunk_x, chunk_z);
let blocks = this.#chunks.get(key);
@@ -581,7 +642,7 @@ export class ServerWorld {
if (dx === 0 && dz === 0) {
continue;
}
const spills = this.#get_raw(chunk_x + dx, chunk_z + dz).spills;
const spills = this.#spills_of(chunk_x + dx, chunk_z + dz);
for (let i = 0; i < spills.length; i += 4) {
const [x, y, z, nid] = [spills[i], spills[i + 1], spills[i + 2], spills[i + 3]];
if (Math.floor(x / CHUNK_SIZE) !== chunk_x || Math.floor(z / CHUNK_SIZE) !== chunk_z) {
@@ -598,6 +659,14 @@ export class ServerWorld {
}
}
// where in the code a fallback came from: the first caller outside this file, as file:line
function fallback_caller(stack: string) {
const frames = stack.split("\n").slice(1).map((line) => line.trim());
const outside = frames.find((frame) => !frame.includes("/world.ts")) ?? frames[0] ?? "unknown";
const match = outside.match(/at (?:(\S+) )?\(?.*\/([^/]+:\d+):\d+\)?$/);
return match ? `${match[1] ?? "?"} ${match[2]}` : outside;
}
function index_in_chunk(x: number, y: number, z: number, chunk_x: number, chunk_z: number) {
return y * CHUNK_AREA + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE);
}