World shape for dimension

This commit is contained in:
2026-11-06 22:49:43 -03:00
parent 7458a2d42a
commit 818ce22dd6
60 changed files with 749 additions and 349 deletions
+34 -40
View File
@@ -21,7 +21,8 @@ import {
RENDER_LAYERS,
RenderLayer,
} from "@/common/everything_registry.ts";
import { AIR, CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE, Faces, ID_MASK, VOID } from "../../common/constants.ts";
import { AIR, CHUNK_AREA, CHUNK_SIZE, Faces, ID_MASK, VOID } from "../../common/constants.ts";
import type { WorldShape } from "@/common/world_shape.ts";
import { AssetManager } from "../assets.ts";
import { ChunkWorkerPool } from "../chunk_workers.ts";
import type { FaceGroup, FromChunkWorker } from "../workers/chunk_messages.ts";
@@ -41,11 +42,12 @@ export interface Block {
z: number;
}
export { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE };
export { CHUNK_AREA, CHUNK_SIZE };
export interface Chunk {
x: number;
z: number;
// the dimension's whole height, y from its min_y then z then x
blocks: Uint32Array;
dirty: boolean;
// bumped on every mesh request so late results from older requests get ignored
@@ -112,8 +114,6 @@ const ALL_NEIGHBOR_OFFSETS = [...NEIGHBOR_OFFSETS, [-1, -1], [1, -1], [-1, 1], [
// how far past the view distance chunks are kept, like minecraft's ClientChunkCache. the server tells the client
// which ones to forget, this only catches the ones it didn't
const STORAGE_MARGIN = 3;
// block light 0 and sky light 15, packed like Chunk.light
export const FULL_SKY_LIGHT = 15;
// what minecraft calls the ClientLevel: this client's copy of the world, its chunks and the entities in it. the
// chunks are what the server sent, see common/chunk_streaming.ts
@@ -131,6 +131,8 @@ export class ClientLevel {
ticks = 0;
// in chunks, what the server was asked to send
view_distance: number;
// the dimension's height and sky light
readonly shape: WorldShape;
// the chunk the server says the player is in
center: { x: number; z: number } | undefined;
// what this client changed ahead of the server, see MultiPlayerGameMode.predict
@@ -151,8 +153,9 @@ export class ClientLevel {
break_progress = new Map<string, { x: number; y: number; z: number; stage: number }>();
// server_blocks is the server's block id for each of its numeric ids
constructor(server_blocks: string[], view_distance: number) {
constructor(server_blocks: string[], shape: WorldShape, view_distance: number) {
this.view_distance = view_distance;
this.shape = shape;
this.#to_local = new Uint32Array(server_blocks.length);
server_blocks.forEach((id, nid) => {
if (!id) return;
@@ -173,6 +176,7 @@ export class ClientLevel {
textures_info: AssetManager.instance.get("cubi:textures_info"),
image: { width: this.image.width, height: this.image.height },
visuals: [...block_visuals].map(([nid, visual]) => [nid, visual.replaces_model]),
shape: { min_y: shape.min_y, max_y: shape.max_y, sky_light: shape.sky_light },
});
}
@@ -202,6 +206,9 @@ export class ClientLevel {
if (!this.#in_storage_range(x, z)) {
return;
}
if (blocks.length !== this.shape.volume) {
throw new Error(`Chunk ${x}, ${z} isn't as tall as the dimension`);
}
this.set_chunk(x, z, blocks, biomes);
}
@@ -286,14 +293,16 @@ export class ClientLevel {
return;
}
if (!this.shape.contains(block.y)) {
return;
}
const [nid, info] = EverythingRegistry.get_full<BlockRegistry>("blocks", block.id)!;
this.#retain(block.x, block.y, block.z);
const lx = block.x - block_chunk_x * CHUNK_SIZE;
const lz = block.z - block_chunk_z * CHUNK_SIZE;
const ly = block.y;
const index = ly * CHUNK_SIZE * CHUNK_SIZE + lz * CHUNK_SIZE + lx;
const index = this.#index(block.x, block.y, block.z, block_chunk_x, block_chunk_z);
const old_nid = chunk.blocks[index] & ID_MASK;
chunk.blocks[index] = state === undefined ? default_block_value(nid, info) : block_value(nid, state);
@@ -301,19 +310,18 @@ export class ClientLevel {
this.#mark_neighbors_dirty(block_chunk_x, block_chunk_z, lx, lz, old_nid, nid);
}
// block light << 4 | sky light there, as of its chunk's last mesh. full sky light above the world and where it isn't
// known yet, so entities don't go dark while their chunk loads
// block light << 4 | sky light there, as of its chunk's last mesh. the shape's open light (full sky light, unless
// the dimension has none) above the world and where it isn't known yet, so entities don't go dark while their
// chunk loads
get_light(x: number, y: number, z: number): number {
const block_y = Math.floor(y);
if (block_y >= CHUNK_HEIGHT) return FULL_SKY_LIGHT;
if (block_y < 0) return 0;
if (block_y >= this.shape.max_y) return this.shape.open_light;
if (block_y < this.shape.min_y) return 0;
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
const light = this.get_chunk(chunk_x, chunk_z)?.light;
if (!light) return FULL_SKY_LIGHT;
const lx = Math.floor(x) - chunk_x * CHUNK_SIZE;
const lz = Math.floor(z) - chunk_z * CHUNK_SIZE;
return light[block_y * CHUNK_AREA + lz * CHUNK_SIZE + lx];
if (!light) return this.shape.open_light;
return light[this.#index(Math.floor(x), block_y, Math.floor(z), chunk_x, chunk_z)];
}
// the id with its state bits, VOID outside loaded chunks
@@ -324,8 +332,7 @@ export class ClientLevel {
if (!chunk) {
return VOID;
}
return chunk.blocks[y * CHUNK_AREA + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE)] ??
AIR;
return this.shape.contains(y) ? chunk.blocks[this.#index(x, y, z, chunk_x, chunk_z)] : AIR;
}
get_block(x: number, y: number, z: number) {
@@ -337,14 +344,13 @@ export class ClientLevel {
if (!chunk) {
return VOID;
}
return this.shape.contains(y) ? chunk.blocks[this.#index(x, y, z, chunk_x, chunk_z)] & ID_MASK : AIR;
}
const lx = x - chunk_x * CHUNK_SIZE;
const lz = z - chunk_z * CHUNK_SIZE;
const ly = y;
const index = ly * CHUNK_SIZE * CHUNK_SIZE + lz * CHUNK_SIZE + lx;
return chunk.blocks[index] & ID_MASK;
// a block's place in its chunk's blocks and light. y has to be in the shape
#index(x: number, y: number, z: number, chunk_x: number, chunk_z: number) {
return (y - this.shape.min_y) * CHUNK_AREA + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE +
(x - chunk_x * CHUNK_SIZE);
}
// whether entities bump into the block there. unloaded chunks count as solid, so nothing falls out of the world
@@ -400,7 +406,7 @@ export class ClientLevel {
const block_chunk_x = Math.floor(x / CHUNK_SIZE);
const block_chunk_z = Math.floor(z / CHUNK_SIZE);
const chunk = this.get_chunk(block_chunk_x, block_chunk_z);
if (!chunk) {
if (!chunk || !this.shape.contains(y)) {
return;
}
@@ -408,9 +414,7 @@ export class ClientLevel {
const lx = x - block_chunk_x * CHUNK_SIZE;
const lz = z - block_chunk_z * CHUNK_SIZE;
const ly = y;
const index = ly * CHUNK_SIZE * CHUNK_SIZE + lz * CHUNK_SIZE + lx;
const index = this.#index(x, y, z, block_chunk_x, block_chunk_z);
const old_value = chunk.blocks[index];
const old_nid = old_value & ID_MASK;
const old_block = this.#blocks[old_nid];
@@ -472,16 +476,6 @@ export class ClientLevel {
}
}
index_to_xyz(index: number) {
const y = Math.floor(index / CHUNK_AREA);
const rem = index % CHUNK_AREA;
const z = Math.floor(rem / CHUNK_SIZE);
const x = rem % CHUNK_SIZE;
return [x, y, z];
}
// set a block the server told us about. the server only sends them for chunks this client has. a block this
// client guessed waits for the server's ack instead, see BlockPredictions
apply_change(x: number, y: number, z: number, id: string, state = 0) {
@@ -691,7 +685,7 @@ export class ClientLevel {
chunk.light = message.light;
chunk.min_y = message.min_y;
chunk.max_y = message.max_y;
this.visuals.meshed(chunk.x, chunk.z, message.visuals);
this.visuals.meshed(chunk.x, chunk.z, this.shape.min_y, message.visuals);
for (const layer of RENDER_LAYERS) {
const { vertices, quad_count, groups } = message[layer];