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
+48 -35
View File
@@ -1,7 +1,8 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import { AIR, CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import { AIR, CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import type { WorldShape } from "@/common/world_shape.ts";
import {
block_light_emission,
block_light_opacity,
@@ -16,9 +17,9 @@ 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";
import { encode_level_chunk } from "@/common/chunk_streaming.ts";
import { CHUNK_VOLUME, ChunkBlocks, decode_chunk, encode_chunk, SavedChunk, SavedChunks } from "./saves.ts";
import { ChunkBlocks, decode_chunk, encode_chunk, SavedChunk, SavedChunks } from "./saves.ts";
// generation only, rebuilt when needed. 256 KB each. with a worldgen pool, it holds what the pool made until the
// 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
// chunks are built, a few dozen chunks ahead of what players are sent
const RAW_CACHE_SIZE = 256;
const CHUNK_CACHE_SIZE = 256;
@@ -103,6 +104,8 @@ export class ServerWorld {
readonly seed: string;
readonly block_ids: Record<string, number> = {};
readonly worldgen: WorldgenSetup;
// the dimension's height and sky light, chunks hold all of it
readonly shape: WorldShape;
// what a freshly placed or generated block stores, by numeric id: the id with its default states
readonly default_values: number[] = [];
// for light_at, by numeric id
@@ -155,6 +158,7 @@ export class ServerWorld {
constructor(seed: string, worldgen: WorldgenSetup) {
this.seed = seed;
this.worldgen = worldgen;
this.shape = worldgen.shape;
// 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);
@@ -179,6 +183,7 @@ export class ServerWorld {
}
#find_spawn() {
const { min_y, max_y } = this.shape;
// chunks in rings around the middle, one column each
for (let radius = 0; radius <= SPAWN_SEARCH_CHUNKS; radius++) {
for (let cz = -radius; cz <= radius; cz++) {
@@ -186,15 +191,15 @@ export class ServerWorld {
if (Math.max(Math.abs(cx), Math.abs(cz)) !== radius) continue;
const x = cx * CHUNK_SIZE + CHUNK_SIZE / 2;
const z = cz * CHUNK_SIZE + CHUNK_SIZE / 2;
let y = CHUNK_HEIGHT - 1;
while (y > 0 && this.get_block_nid(x, y, z) === AIR) y--;
if (y > 0 && this.get_block_info(x, y, z)?.has_collision) {
let y = max_y - 1;
while (y > min_y && this.get_block_nid(x, y, z) === AIR) y--;
if (y > min_y && this.get_block_info(x, y, z)?.has_collision) {
return { x: x + 0.5, y: y + 1, z: z + 0.5 };
}
}
}
}
return { x: 0.5, y: CHUNK_HEIGHT - 1, z: 0.5 };
return { x: 0.5, y: max_y - 1, z: 0.5 };
}
get_block_id(x: number, y: number, z: number): string {
@@ -203,23 +208,23 @@ export class ServerWorld {
}
get_block_nid(x: number, y: number, z: number): number {
if (y < 0 || y >= CHUNK_HEIGHT) {
if (!this.shape.contains(y)) {
return AIR;
}
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[index_in_chunk(x, y, z, chunk_x, chunk_z)] & ID_MASK;
return blocks[this.#index(x, y, z, chunk_x, chunk_z)] & ID_MASK;
}
// the id with its state bits
get_block_value(x: number, y: number, z: number): number {
if (y < 0 || y >= CHUNK_HEIGHT) {
if (!this.shape.contains(y)) {
return AIR;
}
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
return this.#get_chunk(chunk_x, chunk_z)[index_in_chunk(x, y, z, chunk_x, chunk_z)];
return this.#get_chunk(chunk_x, chunk_z)[this.#index(x, y, z, chunk_x, chunk_z)];
}
// whether entities bump into the block there, water and other blocks with "collision": false don't
@@ -292,7 +297,7 @@ export class ServerWorld {
const found: [number, number, number][] = [];
for (let i = 0; i < blocks.length; i++) {
if (!wanted[blocks[i] & ID_MASK]) continue;
const y = Math.floor(i / CHUNK_AREA);
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,
@@ -324,7 +329,7 @@ export class ServerWorld {
return this.#opacity[this.get_block_nid(x, y, z)];
}
// the highest block that isn't air, -1 for an empty column. minecraft's WORLD_SURFACE heightmap
// the highest block that isn't air, min_y - 1 for an empty column. minecraft's WORLD_SURFACE heightmap
surface_height(x: number, z: number): number {
return this.#columns_of(x, z)[column_index(x, z)];
}
@@ -336,10 +341,12 @@ export class ServerWorld {
// the sky and block light there, 0-15, the same as clients work out (see client/workers/lighting.ts) but for one
// position: searching out from it, cheapest first, for the brightest place light could come from. light loses
// max(1, opacity) for every block it goes into, and never gets further than 15 blocks
// max(1, opacity) for every block it goes into, and never gets further than 15 blocks. no sky light at all in a
// dimension without it
light_at(x: number, y: number, z: number): { sky: number; block: number } {
if (y >= CHUNK_HEIGHT) return { sky: 15, block: 0 };
if (y < 0) return { sky: 0, block: 0 };
const { min_y, max_y, sky_light } = this.shape;
if (y >= max_y) return { sky: sky_light ? 15 : 0, block: 0 };
if (y < min_y) return { sky: 0, block: 0 };
// 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);
@@ -352,7 +359,7 @@ export class ServerWorld {
const queues: number[][] = Array.from({ length: LIGHT_REACH }, () => []);
let next = 0;
const reach = (dx: number, dy: number, dz: number) => {
if (y + dy < 0 || y + dy >= CHUNK_HEIGHT) return;
if (y + dy < min_y || y + dy >= max_y) return;
const at = box_index(dx, dy, dz);
if (cost[at] <= next) return;
cost[at] = next;
@@ -375,9 +382,11 @@ 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 * CHUNK_AREA + column] & ID_MASK;
const columns = skies[which] ??= this.#columns_of(bx, bz);
if (by >= columns[CHUNK_AREA + column]) sky = Math.max(sky, LIGHT_REACH - lost);
const nid = blocks[(by - min_y) * CHUNK_AREA + column] & 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);
}
block = Math.max(block, this.#emission[nid] - lost);
// light comes to this block from its neighbors, losing what going into it costs
next = lost + Math.max(1, this.#opacity[nid]);
@@ -400,13 +409,15 @@ export class ServerWorld {
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++) {
let y = CHUNK_HEIGHT - 1;
// 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] = y;
columns[column] = min_y + y;
while (y >= 0 && this.#opacity[blocks[y * CHUNK_AREA + column] & ID_MASK] === 0) y--;
columns[CHUNK_AREA + column] = y + 1;
columns[CHUNK_AREA + column] = min_y + y + 1;
}
this.#columns.set(key, columns);
}
@@ -415,7 +426,7 @@ export class ServerWorld {
// only changes the block, the game server runs behaviors and tiles. state bits default to the block's defaults
set_block(x: number, y: number, z: number, id: string, state?: number) {
if (y < 0 || y >= CHUNK_HEIGHT) {
if (!this.shape.contains(y)) {
return;
}
const nid = id === AIR_ID ? AIR : this.block_ids[id];
@@ -427,7 +438,7 @@ 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 = index_in_chunk(x, y, z, chunk_x, chunk_z);
const index = this.#index(x, y, z, chunk_x, chunk_z);
chunk.blocks[index] = value;
chunk.unknown?.delete(index);
@@ -488,12 +499,12 @@ export class ServerWorld {
return index;
};
const blocks = new Uint16Array(CHUNK_VOLUME);
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_VOLUME; i++) {
for (let i = 0; i < chunk.blocks.length; i++) {
const value = chunk.blocks[i];
if (value !== last_value) {
let index = by_value.get(value);
@@ -661,7 +672,7 @@ export class ServerWorld {
const data = evicted?.data ?? this.#saved_chunks?.read(chunk_x, chunk_z);
try {
if (!data) throw new Error("it couldn't be read");
return this.#from_saved(chunk_x, chunk_z, decode_chunk(data));
return this.#from_saved(chunk_x, chunk_z, decode_chunk(data, this.shape.volume));
} catch (e) {
console.error(
`Couldn't load chunk ${chunk_x}, ${chunk_z}, generating it again: ${(e as Error).message}`,
@@ -679,9 +690,9 @@ export class ServerWorld {
const nid = id === AIR_ID ? AIR : this.block_ids[id];
return nid === undefined ? undefined : block_value(nid, state);
});
const blocks = new Uint32Array(CHUNK_VOLUME);
const blocks = new Uint32Array(this.shape.volume);
let unknown: Map<number, [string, number]> | undefined;
for (let i = 0; i < CHUNK_VOLUME; i++) {
for (let i = 0; i < blocks.length; i++) {
const value = values[saved.blocks[i]];
if (value === undefined) {
(unknown ??= new Map()).set(i, saved.palette[saved.blocks[i]]);
@@ -706,6 +717,12 @@ export class ServerWorld {
this.#evicted.set(key, { x, z, data: encode_chunk(this.#chunk_blocks(chunk)) });
}
// a block's place in its chunk's blocks, from the bottom of the world. 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);
}
// 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();
@@ -721,7 +738,7 @@ export class ServerWorld {
if (Math.floor(x / CHUNK_SIZE) !== chunk_x || Math.floor(z / CHUNK_SIZE) !== chunk_z) {
continue;
}
const index = index_in_chunk(x, y, z, chunk_x, chunk_z);
const index = this.#index(x, y, z, chunk_x, chunk_z);
if (blocks[index] === AIR) {
blocks[index] = nid;
}
@@ -740,10 +757,6 @@ function fallback_caller(stack: string) {
return match ? `${match[1] ?? "?"} ${match[2]}` : outside;
}
export 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);
}
// a column's place in its chunk, the same as its blocks' in a layer
function column_index(x: number, z: number) {
return (z - Math.floor(z / CHUNK_SIZE) * CHUNK_SIZE) * CHUNK_SIZE + x - Math.floor(x / CHUNK_SIZE) * CHUNK_SIZE;