World sections

This commit is contained in:
2026-11-06 23:13:34 -03:00
parent 818ce22dd6
commit 1fa47c027d
21 changed files with 392 additions and 127 deletions
+12 -10
View File
@@ -5,14 +5,12 @@
// 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 } from "./constants.ts";
import { SECTION_HEIGHT } from "./world_shape.ts";
import { type ChunkColumn, type Section, SECTION_VOLUME } from "./section.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;
const SECTION_VOLUME = CHUNK_AREA * SECTION_HEIGHT;
// the first byte of a binary message, the only kind there is so far
export const LEVEL_CHUNK = 1;
@@ -37,6 +35,7 @@ export function is_within_view(
return near * near + far * far < view_distance * view_distance;
}
// what a client reads a level_chunk as
export interface LevelChunk {
x: number;
z: number;
@@ -61,7 +60,7 @@ export interface LevelChunk {
// 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
// 32: u32 each block's value
export function encode_level_chunk(chunk: LevelChunk): Uint8Array<ArrayBuffer> {
export function encode_level_chunk(chunk: ChunkColumn): Uint8Array<ArrayBuffer> {
const writer = new Writer();
const biome_palette = [...new Set(chunk.biomes)];
@@ -71,11 +70,8 @@ export function encode_level_chunk(chunk: LevelChunk): Uint8Array<ArrayBuffer> {
for (const biome of chunk.biomes) writer.u8(biome_palette.indexOf(biome));
}
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));
}
writer.u16(chunk.sections.length);
for (const section of chunk.sections) write_section(writer, section);
const body = deflateSync(writer.bytes());
const bytes = new Uint8Array(9 + body.length);
@@ -87,7 +83,13 @@ export function encode_level_chunk(chunk: LevelChunk): Uint8Array<ArrayBuffer> {
return bytes;
}
function write_section(writer: Writer, blocks: Uint32Array) {
function write_section(writer: Writer, section: Section) {
const blocks = section.blocks;
if (blocks === null) {
writer.u8(0);
writer.u32(section.value);
return;
}
const palette: number[] = [];
const index_of = new Map<number, number>();
for (const value of blocks) {
+11 -6
View File
@@ -6,6 +6,7 @@
import { Alea } from "@paulaboks/rng";
import { AIR, CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import type { WorldShape } from "@/common/world_shape.ts";
import { ChunkColumn } from "@/common/section.ts";
import type { FeatureChunk, Position } from "@/common/mod_api/worldgen.ts";
import type { OreJson } from "@/common/mod_data.ts";
import { generate_noise_chunk } from "./worldgen/noise_chunk.ts";
@@ -37,17 +38,20 @@ export interface SetupFeature {
}
export interface RawChunk {
// numeric block ids, only what this chunk generated itself, indexed (y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x
blocks: Uint32Array;
// only what this chunk generated itself, with each column's biome
column: ChunkColumn;
// blocks it generated in other chunks (from features like a mod's trees), flattened as x, y, z, numeric id
spills: Int32Array;
// each column's biome id, x then z
biomes: string[];
}
// features that threw, so each is only reported once
const failed_features = new Set<string>();
// what a chunk is generated into before it's made sections, indexed (y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x. a
// column at a time is how terrain is worked out, and features set blocks one at a time. kept for the next chunk, so
// generating isn't reentrant
let scratch = new Uint32Array(0);
// generates one chunk on its own. neighbors' spills get merged in by whoever assembles the world:
// a chunk's own blocks always win and spills only fill air, so the result doesn't depend on load order
export function generate_raw_chunk(
@@ -60,7 +64,8 @@ export function generate_raw_chunk(
default_values?: number[],
): RawChunk {
const shape = worldgen.shape;
const blocks = new Uint32Array(shape.volume);
if (scratch.length !== shape.volume) scratch = new Uint32Array(shape.volume);
const blocks = scratch.fill(AIR);
const spills: number[] = [];
const heights = new Int32Array(CHUNK_AREA);
const biomes: string[] = new Array(CHUNK_AREA);
@@ -92,7 +97,7 @@ export function generate_raw_chunk(
}
generate_features(blocks, heights, biomes, set, chunk_x, chunk_z, seed, block_ids, worldgen);
return { blocks, spills: new Int32Array(spills), biomes };
return { column: ChunkColumn.from_flat(chunk_x, chunk_z, shape, blocks, biomes), spills: new Int32Array(spills) };
}
// each block an ore replaces becomes the first ore whose noise is above its threshold
+146
View File
@@ -0,0 +1,146 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// a chunk's blocks in sections of 16x16x16, from the bottom of its dimension up, like minecraft's LevelChunkSection. a
// section that's all one block keeps just that value: most of a tall column is air above the ground and stone under it
import { AIR, CHUNK_AREA, ID_MASK } from "./constants.ts";
import { SECTION_HEIGHT, type WorldShape } from "./world_shape.ts";
export const SECTION_VOLUME = CHUNK_AREA * SECTION_HEIGHT;
// a block's place in its section, y then z then x like a chunk's layers. world coordinates work as they are, since
// sections start at multiples of 16
export function section_index(x: number, y: number, z: number) {
return ((y & 15) << 8) | ((z & 15) << 4) | (x & 15);
}
// what a section is made of, a block value or every block's, what's sent to and from workers
export type SectionData = number | Uint32Array;
export class Section {
// the block, while blocks is null
value: number;
// every block's value, once they differ
blocks: Uint32Array | null;
constructor(data: SectionData) {
this.value = typeof data === "number" ? data : 0;
this.blocks = typeof data === "number" ? null : data;
}
// a copy of values, one value when they're all the same
static of(values: Uint32Array): Section {
const first = values[0];
for (let i = 1; i < values.length; i++) {
if (values[i] !== first) return new Section(values.slice());
}
return new Section(first);
}
get data(): SectionData {
return this.blocks ?? this.value;
}
get(index: number): number {
return this.blocks === null ? this.value : this.blocks[index];
}
set(index: number, value: number) {
if (this.blocks === null) {
if (value === this.value) return;
this.blocks = new Uint32Array(SECTION_VOLUME).fill(this.value);
}
this.blocks[index] = value;
}
}
// a chunk's blocks, its dimension's whole height, and each column's biome
export class ChunkColumn {
readonly x: number;
readonly z: number;
readonly shape: WorldShape;
// from min_y up
readonly sections: Section[];
// x then z
readonly biomes: string[];
constructor(x: number, z: number, shape: WorldShape, sections: Section[], biomes: string[]) {
if (sections.length * SECTION_HEIGHT !== shape.height) {
throw new Error(`Chunk ${x}, ${z} has ${sections.length} sections, its dimension ${shape.height / 16}`);
}
this.x = x;
this.z = z;
this.shape = shape;
this.sections = sections;
this.biomes = biomes;
}
// from every block's value in one array, y from min_y then z then x, which is copied
static from_flat(x: number, z: number, shape: WorldShape, blocks: Uint32Array, biomes: string[]): ChunkColumn {
const sections: Section[] = [];
for (let start = 0; start < blocks.length; start += SECTION_VOLUME) {
sections.push(Section.of(blocks.subarray(start, start + SECTION_VOLUME)));
}
return new ChunkColumn(x, z, shape, sections, biomes);
}
// the block value at world coordinates, air outside the dimension's height
get(x: number, y: number, z: number): number {
if (!this.shape.contains(y)) return AIR;
return this.sections[(y - this.shape.min_y) >> 4].get(section_index(x, y, z));
}
// does nothing outside the dimension's height
set(x: number, y: number, z: number, value: number) {
if (!this.shape.contains(y)) return;
this.sections[(y - this.shape.min_y) >> 4].set(section_index(x, y, z), value);
}
clone(): ChunkColumn {
const sections = this.sections.map((section) => new Section(section.blocks?.slice() ?? section.value));
return new ChunkColumn(this.x, this.z, this.shape, sections, [...this.biomes]);
}
// every block's value in one array, like from_flat's
to_flat(): Uint32Array {
const blocks = new Uint32Array(this.shape.volume);
this.sections.forEach((section, i) => {
if (section.blocks) blocks.set(section.blocks, i * SECTION_VOLUME);
else blocks.fill(section.value, i * SECTION_VOLUME, (i + 1) * SECTION_VOLUME);
});
return blocks;
}
}
// per column of a chunk (z * 16 + x), the highest y that isn't air, min_y - 1 for an empty one, then the lowest y with
// nothing above it that stops light, where sky light is 15 (see light_at in server/game/world.ts). opacity is by
// numeric id. sections that are all air, or all clear, are passed over whole
export function column_heights(column: ChunkColumn, opacity: Uint8Array): Int16Array {
const heights = new Int16Array(CHUNK_AREA * 2);
const { sections, shape } = column;
for (let c = 0; c < CHUNK_AREA; c++) {
// from the top, by y above min_y
let y = shape.height - 1;
while (y >= 0) {
const section = sections[y >> 4];
if (section.blocks === null && (section.value & ID_MASK) === AIR) {
y = (y & ~15) - 1;
continue;
}
if ((section.get(((y & 15) << 8) | c) & ID_MASK) !== AIR) break;
y--;
}
heights[c] = shape.min_y + y;
while (y >= 0) {
const section = sections[y >> 4];
if (section.blocks === null && opacity[section.value & ID_MASK] === 0) {
y = (y & ~15) - 1;
continue;
}
if (opacity[section.get(((y & 15) << 8) | c) & ID_MASK] !== 0) break;
y--;
}
heights[CHUNK_AREA + c] = shape.min_y + y + 1;
}
return heights;
}