278 lines
9.9 KiB
TypeScript
278 lines
9.9 KiB
TypeScript
// 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 type { Reader, Writer } from "./bytes.ts";
|
|
import { HIGHEST_Y, LOWEST_Y, 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 {
|
|
return to_flat(this.sections);
|
|
}
|
|
}
|
|
|
|
export function to_flat(sections: Section[]): Uint32Array {
|
|
const blocks = new Uint32Array(sections.length * SECTION_VOLUME);
|
|
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;
|
|
}
|
|
|
|
// bits per block of a section's palette, the smallest that fits. more than 256 kinds are written whole
|
|
const PALETTE_BITS = [1, 2, 4, 8];
|
|
const DIRECT_BITS = 32;
|
|
|
|
// a column's biomes and sections, as level_chunk and saved chunks hold them (see common/chunk_streaming.ts and
|
|
// server/game/saves.ts). the network sends block values, a save the index of each block in its palette:
|
|
//
|
|
// 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
|
|
// u16 how many sections there are
|
|
// each section of 16x16x16 blocks, bottom first, a paletted container:
|
|
// 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
|
|
// 32: u32 each block's value
|
|
export function write_column(writer: Writer, sections: Section[], biomes: string[]) {
|
|
if (biomes.length !== CHUNK_AREA) throw new Error(`${biomes.length} biomes in a chunk`);
|
|
const biome_palette = [...new Set(biomes)];
|
|
writer.u16(biome_palette.length);
|
|
for (const biome of biome_palette) writer.string(biome);
|
|
if (biome_palette.length > 1) {
|
|
for (const biome of biomes) writer.u8(biome_palette.indexOf(biome));
|
|
}
|
|
|
|
writer.u16(sections.length);
|
|
for (const section of sections) write_section(writer, section);
|
|
}
|
|
|
|
function write_section(writer: Writer, section: Section) {
|
|
const blocks = section.blocks;
|
|
if (blocks === null) {
|
|
writer.u8(0);
|
|
writer.u32(section.value);
|
|
return;
|
|
}
|
|
// each block's palette index. runs of the same block are common, so it's looked up when the block changes
|
|
const indexes = new Uint16Array(SECTION_VOLUME);
|
|
const palette: number[] = [];
|
|
const index_of = new Map<number, number>();
|
|
let last_value = blocks[0] + 1;
|
|
let last_index = 0;
|
|
for (let i = 0; i < SECTION_VOLUME; i++) {
|
|
const value = blocks[i];
|
|
if (value !== last_value) {
|
|
last_value = value;
|
|
let index = index_of.get(value);
|
|
if (index === undefined) {
|
|
index = palette.push(value) - 1;
|
|
index_of.set(value, index);
|
|
}
|
|
last_index = index;
|
|
}
|
|
indexes[i] = last_index;
|
|
}
|
|
|
|
if (palette.length === 1) {
|
|
writer.u8(0);
|
|
writer.u32(palette[0]);
|
|
return;
|
|
}
|
|
const bits = PALETTE_BITS.find((bits) => palette.length <= 1 << bits);
|
|
if (bits === undefined) {
|
|
writer.u8(DIRECT_BITS);
|
|
for (const value of blocks) writer.u32(value);
|
|
return;
|
|
}
|
|
|
|
writer.u8(bits);
|
|
writer.u16(palette.length);
|
|
for (const value of palette) writer.u32(value);
|
|
const per_byte = 8 / bits;
|
|
for (let i = 0; i < SECTION_VOLUME; i += per_byte) {
|
|
let byte = 0;
|
|
for (let j = 0; j < per_byte; j++) byte |= indexes[i + j] << (j * bits);
|
|
writer.u8(byte);
|
|
}
|
|
}
|
|
|
|
// throws when it isn't what write_column writes. map turns each value read into what the sections hold, and can throw
|
|
// for a value that's wrong
|
|
export function read_column(reader: Reader, map: (value: number) => number): { sections: Section[]; biomes: string[] } {
|
|
const biome_palette: string[] = [];
|
|
const biome_count = reader.u16();
|
|
if (biome_count === 0) throw new Error("No biomes");
|
|
for (let i = 0; i < biome_count; i++) biome_palette.push(reader.string());
|
|
const biomes: string[] = new Array(CHUNK_AREA);
|
|
for (let column = 0; column < CHUNK_AREA; column++) {
|
|
const index = biome_count > 1 ? reader.u8() : 0;
|
|
if (index >= biome_count) throw new Error("Bad biome index");
|
|
biomes[column] = biome_palette[index];
|
|
}
|
|
|
|
const count = reader.u16();
|
|
if (count * SECTION_HEIGHT > HIGHEST_Y - LOWEST_Y) throw new Error(`Too many sections, ${count}`);
|
|
const sections: Section[] = [];
|
|
for (let i = 0; i < count; i++) sections.push(read_section(reader, map));
|
|
return { sections, biomes };
|
|
}
|
|
|
|
function read_section(reader: Reader, map: (value: number) => number): Section {
|
|
const bits = reader.u8();
|
|
if (bits === 0) return new Section(map(reader.u32()));
|
|
const blocks = new Uint32Array(SECTION_VOLUME);
|
|
if (bits === DIRECT_BITS) {
|
|
for (let i = 0; i < SECTION_VOLUME; i++) blocks[i] = map(reader.u32());
|
|
return new Section(blocks);
|
|
}
|
|
if (!PALETTE_BITS.includes(bits)) throw new Error(`Bad bits per block ${bits}`);
|
|
|
|
const palette: number[] = [];
|
|
const size = reader.u16();
|
|
if (size > 1 << bits) throw new Error("Bad palette");
|
|
for (let i = 0; i < size; i++) palette.push(map(reader.u32()));
|
|
const per_byte = 8 / bits;
|
|
const mask = (1 << bits) - 1;
|
|
for (let i = 0; i < SECTION_VOLUME; i += per_byte) {
|
|
const byte = reader.u8();
|
|
for (let j = 0; j < per_byte; j++) {
|
|
const index = (byte >> (j * bits)) & mask;
|
|
if (index >= size) throw new Error("Bad palette index");
|
|
blocks[i + j] = palette[index];
|
|
}
|
|
}
|
|
return new Section(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;
|
|
}
|