Wooooorld files

This commit is contained in:
2026-11-06 23:41:32 -03:00
parent 1fa47c027d
commit 8e14e9842a
9 changed files with 501 additions and 383 deletions
+138 -7
View File
@@ -4,7 +4,8 @@
// 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";
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;
@@ -103,15 +104,145 @@ export class ChunkColumn {
// 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;
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