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
+12 -9
View File
@@ -4,16 +4,14 @@
// how the server streams the world to clients, like minecraft java since 1.20.2: whole chunks in batches around each
// player, and which chunks are in a player's view. see "Chunk Streaming" in docs/modding_plan.md
import { deflateSync, inflateSync } from "fflate";
import { CHUNK_AREA, CHUNK_HEIGHT } from "./constants.ts";
import { CHUNK_AREA } from "./constants.ts";
import { SECTION_HEIGHT } from "./world_shape.ts";
// the view distances clients can ask for, in chunks, like minecraft's
export const MIN_VIEW_DISTANCE = 2;
export const MAX_VIEW_DISTANCE = 32;
export const SECTION_HEIGHT = 16;
const SECTION_VOLUME = CHUNK_AREA * SECTION_HEIGHT;
const SECTIONS = CHUNK_HEIGHT / SECTION_HEIGHT;
const CHUNK_VOLUME = CHUNK_AREA * CHUNK_HEIGHT;
// the first byte of a binary message, the only kind there is so far
export const LEVEL_CHUNK = 1;
@@ -42,7 +40,8 @@ export function is_within_view(
export interface LevelChunk {
x: number;
z: number;
// block values (numeric id | state bits << 16) in the sender's numbering, y then z then x
// block values (numeric id | state bits << 16) in the sender's numbering, y from the bottom of the world, then z,
// then x. the dimension's whole height
blocks: Uint32Array;
// each column's biome, x then z
biomes: string[];
@@ -56,7 +55,8 @@ export interface LevelChunk {
// the rest deflated:
// u16 biome palette size, then each biome: u8 length and the id as utf-8
// u8 each column's biome palette index, x then z. left out when there's one biome
// 16 sections of 16x16x16 blocks, bottom first, each a paletted container like minecraft's:
// u16 how many sections there are, the dimension's height / 16
// each section of 16x16x16 blocks, bottom first, a paletted container like minecraft's:
// u8 bits per block
// 0: u32 the value of every block
// 1, 2, 4, 8: u16 palette size, u32 each value, then each block's palette index packed from the low bits up
@@ -71,7 +71,9 @@ export function encode_level_chunk(chunk: LevelChunk): Uint8Array<ArrayBuffer> {
for (const biome of chunk.biomes) writer.u8(biome_palette.indexOf(biome));
}
for (let section = 0; section < SECTIONS; section++) {
const sections = chunk.blocks.length / SECTION_VOLUME;
writer.u16(sections);
for (let section = 0; section < sections; section++) {
write_section(writer, chunk.blocks.subarray(section * SECTION_VOLUME, (section + 1) * SECTION_VOLUME));
}
@@ -137,8 +139,9 @@ export function decode_level_chunk(bytes: Uint8Array, map: (value: number) => nu
biomes[column] = biome_palette[index];
}
const blocks = new Uint32Array(CHUNK_VOLUME);
for (let section = 0; section < SECTIONS; section++) {
const sections = reader.u16();
const blocks = new Uint32Array(sections * SECTION_VOLUME);
for (let section = 0; section < sections; section++) {
read_section(reader, blocks.subarray(section * SECTION_VOLUME, (section + 1) * SECTION_VOLUME), map);
}
if (!reader.done) throw new Error("Wrong size");