// 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; }