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
+1 -17
View File
@@ -1,6 +1,5 @@
import type { BlockRegistry } from "$/common/everything_registry.ts";
import type { SpriteRegion } from "$/common/constants.ts";
import type { OreJson } from "$/common/mod_data.ts";
import type { ModelJson } from "$/common/block_models.ts";
import type { SortType } from "./translucent_sort.ts";
@@ -29,16 +28,11 @@ export type ToChunkWorker =
type: "init";
// the blocks registry without its functions, indexed by numeric id
blocks_registry: BlockRegistry[];
block_ids: Record<string, number>;
// mods' block models by id, the engine's are built in
models: Record<string, ModelJson>;
textures_info: Record<string, SpriteRegion>;
image: { width: number; height: number };
// mods' worldgen scripts and ores, so generation matches the server
worldgen_scripts: { mod: string; url: string }[];
ores: OreJson[];
}
| { type: "generate"; chunk_x: number; chunk_z: number; seed: string }
| {
type: "mesh";
chunk_x: number;
@@ -46,7 +40,7 @@ export type ToChunkWorker =
version: number;
// copies of the blocks of the 3x3 chunks around it, going +x then +z from -x -z, for lighting
chunks: (Uint32Array | null)[];
// the same chunks' biome colors (see "generated"), blended across their edges
// the same chunks' biome colors (see Chunk.biome_colors in client_level.ts), blended across their edges
biome_colors: (Uint32Array | null)[];
// where the camera is, to sort the translucent quads
camera: number[];
@@ -70,16 +64,6 @@ export interface LayerMesh {
}
export type FromChunkWorker =
| {
type: "generated";
chunk_x: number;
chunk_z: number;
blocks: Uint32Array;
// blocks that landed in other chunks (from features like a mod's trees), flattened as x, y, z, numeric id
spills: Int32Array;
// each column's biome grass color as 0xRRGGBB, x then z, then each column's foliage color
biome_colors: Uint32Array;
}
| {
type: "meshed";
chunk_x: number;
+2 -34
View File
@@ -25,10 +25,7 @@ import {
type ToChunkWorker,
UNALIGNED_GROUP,
} from "./chunk_messages.ts";
import { generate_raw_chunk, WorldgenSetup } from "$/common/generation.ts";
import { foliage_color, grass_color } from "$/common/worldgen/biome_colors.ts";
import { load_worldgen } from "$/common/worldgen_loader.ts";
import { default_block_value, get_state_value } from "$/common/utils.ts";
import { get_state_value } from "$/common/utils.ts";
import {
bake_model,
block_variant,
@@ -86,7 +83,6 @@ const TRANSLUCENT = 2;
const LAYER_IDS: Record<RenderLayer, number> = { solid: SOLID, cutout: CUTOUT, translucent: TRANSLUCENT };
let blocks_registry: BlockRegistry[] = [];
let block_ids: Record<string, number> = {};
// by numeric id, looked up for every face. like sodium's light data cache, everything the mesher asks about a
// block is worked out once instead of per face
const TABLE_SIZE = ID_MASK + 1;
@@ -123,31 +119,17 @@ const baked = new Map<number, MeshQuad[]>();
let textures_info: TexturesInfo = {};
let image: Texture;
let worldgen: WorldgenSetup | undefined;
// numeric id to what a generated block stores, with its default states. matches the server
let default_values: number[] = [];
// generating has to wait for mods' worldgen scripts, or this worker's terrain wouldn't match the server's
let worldgen_ready: Promise<void> = Promise.resolve();
self.onmessage = async (event: MessageEvent<ToChunkWorker>) => {
self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
const message = event.data;
switch (message.type) {
case "init":
blocks_registry = message.blocks_registry;
block_ids = message.block_ids;
models = message.models;
baked.clear();
build_block_tables();
textures_info = message.textures_info;
image = message.image as Texture;
default_values = blocks_registry.map((block, nid) => default_block_value(nid, block));
worldgen_ready = load_worldgen(message.worldgen_scripts, message.ores).then((setup) => {
worldgen = setup;
});
break;
case "generate":
await worldgen_ready;
generate(message.chunk_x, message.chunk_z, message.seed);
break;
case "mesh": {
region.fill(message.chunks);
@@ -203,20 +185,6 @@ function post(message: FromChunkWorker, transfer: Transferable[]) {
self.postMessage(message, transfer);
}
function generate(chunk_x: number, chunk_z: number, seed: string) {
const { blocks, spills, biomes } = generate_raw_chunk(chunk_x, chunk_z, seed, block_ids, worldgen, default_values);
const biome_colors = new Uint32Array(CHUNK_AREA * 2);
biomes.forEach((biome, column) => {
biome_colors[column] = grass_color(biome);
biome_colors[CHUNK_AREA + column] = foliage_color(biome);
});
post({ type: "generated", chunk_x, chunk_z, blocks, spills, biome_colors }, [
blocks.buffer,
spills.buffer,
biome_colors.buffer,
]);
}
// like minecraft's biome blend at its default radius: each column's grass and foliage color is the average of the 5x5
// columns around it, so biome edges fade instead of being a line. neighbors that aren't there are left out
const BLEND_RADIUS = 2;