Wooooorld files
This commit is contained in:
+108
@@ -0,0 +1,108 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// binary messages and saves, written and read a number at a time. little-endian, which is what both use
|
||||
export class Writer {
|
||||
#bytes = new Uint8Array(16 * 1024);
|
||||
#view = new DataView(this.#bytes.buffer);
|
||||
#length = 0;
|
||||
|
||||
#reserve(size: number) {
|
||||
if (this.#length + size <= this.#bytes.length) return;
|
||||
const bytes = new Uint8Array(Math.max(this.#bytes.length * 2, this.#length + size));
|
||||
bytes.set(this.#bytes);
|
||||
this.#bytes = bytes;
|
||||
this.#view = new DataView(bytes.buffer);
|
||||
}
|
||||
|
||||
u8(value: number) {
|
||||
this.#reserve(1);
|
||||
this.#bytes[this.#length++] = value;
|
||||
}
|
||||
|
||||
u16(value: number) {
|
||||
this.#reserve(2);
|
||||
this.#view.setUint16(this.#length, value, true);
|
||||
this.#length += 2;
|
||||
}
|
||||
|
||||
i16(value: number) {
|
||||
this.#reserve(2);
|
||||
this.#view.setInt16(this.#length, value, true);
|
||||
this.#length += 2;
|
||||
}
|
||||
|
||||
u32(value: number) {
|
||||
this.#reserve(4);
|
||||
this.#view.setUint32(this.#length, value, true);
|
||||
this.#length += 4;
|
||||
}
|
||||
|
||||
// a u8 length, then the text as utf-8
|
||||
string(text: string) {
|
||||
const bytes = new TextEncoder().encode(text);
|
||||
if (bytes.length > 255) throw new Error(`${text} is too long`);
|
||||
this.u8(bytes.length);
|
||||
this.#reserve(bytes.length);
|
||||
this.#bytes.set(bytes, this.#length);
|
||||
this.#length += bytes.length;
|
||||
}
|
||||
|
||||
bytes() {
|
||||
return this.#bytes.subarray(0, this.#length);
|
||||
}
|
||||
}
|
||||
|
||||
// throws when there's less left than what's read
|
||||
export class Reader {
|
||||
#bytes: Uint8Array;
|
||||
#view: DataView;
|
||||
#offset = 0;
|
||||
// only old saves are big-endian
|
||||
#little: boolean;
|
||||
|
||||
constructor(bytes: Uint8Array, little_endian = true) {
|
||||
this.#bytes = bytes;
|
||||
this.#view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
this.#little = little_endian;
|
||||
}
|
||||
|
||||
get done() {
|
||||
return this.#offset === this.#bytes.length;
|
||||
}
|
||||
|
||||
#take(size: number) {
|
||||
if (this.#offset + size > this.#bytes.length) throw new Error("Too short");
|
||||
const offset = this.#offset;
|
||||
this.#offset += size;
|
||||
return offset;
|
||||
}
|
||||
|
||||
u8() {
|
||||
return this.#bytes[this.#take(1)];
|
||||
}
|
||||
|
||||
u16() {
|
||||
return this.#view.getUint16(this.#take(2), this.#little);
|
||||
}
|
||||
|
||||
i16() {
|
||||
return this.#view.getInt16(this.#take(2), this.#little);
|
||||
}
|
||||
|
||||
u32() {
|
||||
return this.#view.getUint32(this.#take(4), this.#little);
|
||||
}
|
||||
|
||||
string() {
|
||||
const length = this.u8();
|
||||
const offset = this.#take(length);
|
||||
return new TextDecoder().decode(this.#bytes.subarray(offset, offset + length));
|
||||
}
|
||||
|
||||
// the next size bytes, not copied
|
||||
take(size: number) {
|
||||
const offset = this.#take(size);
|
||||
return this.#bytes.subarray(offset, offset + size);
|
||||
}
|
||||
}
|
||||
+6
-195
@@ -4,8 +4,8 @@
|
||||
// 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 } from "./constants.ts";
|
||||
import { type ChunkColumn, type Section, SECTION_VOLUME } from "./section.ts";
|
||||
import { Reader, Writer } from "./bytes.ts";
|
||||
import { type ChunkColumn, read_column, to_flat, write_column } from "./section.ts";
|
||||
|
||||
// the view distances clients can ask for, in chunks, like minecraft's
|
||||
export const MIN_VIEW_DISTANCE = 2;
|
||||
@@ -14,10 +14,6 @@ export const MAX_VIEW_DISTANCE = 32;
|
||||
// the first byte of a binary message, the only kind there is so far
|
||||
export const LEVEL_CHUNK = 1;
|
||||
|
||||
// bits per block of a section's indirect palette, the smallest that fits. more than 256 kinds are stored directly
|
||||
const PALETTE_BITS = [1, 2, 4, 8];
|
||||
const DIRECT_BITS = 32;
|
||||
|
||||
// minecraft's ChunkTrackingView.isWithinDistance: a round view, with the ring of chunks around it (outer) that the
|
||||
// client needs to mesh and light the edge of what it shows
|
||||
export function is_within_view(
|
||||
@@ -51,28 +47,10 @@ export interface LevelChunk {
|
||||
//
|
||||
// u8 LEVEL_CHUNK
|
||||
// i32 chunk x, i32 chunk z
|
||||
// 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
|
||||
// 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
|
||||
// 32: u32 each block's value
|
||||
// the rest deflated: the biomes and the sections' block values, see write_column in common/section.ts
|
||||
export function encode_level_chunk(chunk: ChunkColumn): Uint8Array<ArrayBuffer> {
|
||||
const writer = new Writer();
|
||||
|
||||
const biome_palette = [...new Set(chunk.biomes)];
|
||||
writer.u16(biome_palette.length);
|
||||
for (const biome of biome_palette) writer.string(biome);
|
||||
if (biome_palette.length > 1) {
|
||||
for (const biome of chunk.biomes) writer.u8(biome_palette.indexOf(biome));
|
||||
}
|
||||
|
||||
writer.u16(chunk.sections.length);
|
||||
for (const section of chunk.sections) write_section(writer, section);
|
||||
|
||||
write_column(writer, chunk.sections, chunk.biomes);
|
||||
const body = deflateSync(writer.bytes());
|
||||
const bytes = new Uint8Array(9 + body.length);
|
||||
const view = new DataView(bytes.buffer);
|
||||
@@ -83,45 +61,6 @@ export function encode_level_chunk(chunk: ChunkColumn): Uint8Array<ArrayBuffer>
|
||||
return bytes;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (!index_of.has(value)) {
|
||||
index_of.set(value, palette.length);
|
||||
palette.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
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 |= index_of.get(blocks[i + j])! << (j * bits);
|
||||
writer.u8(byte);
|
||||
}
|
||||
}
|
||||
|
||||
// throws when the bytes aren't a level_chunk. map turns each block value into the receiver's numbering
|
||||
export function decode_level_chunk(bytes: Uint8Array, map: (value: number) => number = (value) => value): LevelChunk {
|
||||
if (bytes[0] !== LEVEL_CHUNK || bytes.length < 9) throw new Error("Not a level_chunk");
|
||||
@@ -129,135 +68,7 @@ export function decode_level_chunk(bytes: Uint8Array, map: (value: number) => nu
|
||||
const x = header.getInt32(1, true);
|
||||
const z = header.getInt32(5, true);
|
||||
const reader = new Reader(inflateSync(bytes.subarray(9)));
|
||||
|
||||
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 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);
|
||||
}
|
||||
const { sections, biomes } = read_column(reader, map);
|
||||
if (!reader.done) throw new Error("Wrong size");
|
||||
return { x, z, blocks, biomes };
|
||||
}
|
||||
|
||||
function read_section(reader: Reader, blocks: Uint32Array, map: (value: number) => number) {
|
||||
const bits = reader.u8();
|
||||
if (bits === 0) {
|
||||
blocks.fill(map(reader.u32()));
|
||||
return;
|
||||
}
|
||||
if (bits === DIRECT_BITS) {
|
||||
for (let i = 0; i < SECTION_VOLUME; i++) blocks[i] = map(reader.u32());
|
||||
return;
|
||||
}
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Writer {
|
||||
#bytes = new Uint8Array(16 * 1024);
|
||||
#view = new DataView(this.#bytes.buffer);
|
||||
#length = 0;
|
||||
|
||||
#reserve(size: number) {
|
||||
if (this.#length + size <= this.#bytes.length) return;
|
||||
const bytes = new Uint8Array(Math.max(this.#bytes.length * 2, this.#length + size));
|
||||
bytes.set(this.#bytes);
|
||||
this.#bytes = bytes;
|
||||
this.#view = new DataView(bytes.buffer);
|
||||
}
|
||||
|
||||
u8(value: number) {
|
||||
this.#reserve(1);
|
||||
this.#bytes[this.#length++] = value;
|
||||
}
|
||||
|
||||
u16(value: number) {
|
||||
this.#reserve(2);
|
||||
this.#view.setUint16(this.#length, value, true);
|
||||
this.#length += 2;
|
||||
}
|
||||
|
||||
u32(value: number) {
|
||||
this.#reserve(4);
|
||||
this.#view.setUint32(this.#length, value, true);
|
||||
this.#length += 4;
|
||||
}
|
||||
|
||||
string(text: string) {
|
||||
const bytes = new TextEncoder().encode(text);
|
||||
if (bytes.length > 255) throw new Error(`${text} is too long to send`);
|
||||
this.u8(bytes.length);
|
||||
this.#reserve(bytes.length);
|
||||
this.#bytes.set(bytes, this.#length);
|
||||
this.#length += bytes.length;
|
||||
}
|
||||
|
||||
bytes() {
|
||||
return this.#bytes.subarray(0, this.#length);
|
||||
}
|
||||
}
|
||||
|
||||
class Reader {
|
||||
#bytes: Uint8Array;
|
||||
#view: DataView;
|
||||
#offset = 0;
|
||||
|
||||
constructor(bytes: Uint8Array) {
|
||||
this.#bytes = bytes;
|
||||
this.#view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
}
|
||||
|
||||
get done() {
|
||||
return this.#offset === this.#bytes.length;
|
||||
}
|
||||
|
||||
#take(size: number) {
|
||||
if (this.#offset + size > this.#bytes.length) throw new Error("Too short");
|
||||
const offset = this.#offset;
|
||||
this.#offset += size;
|
||||
return offset;
|
||||
}
|
||||
|
||||
u8() {
|
||||
return this.#bytes[this.#take(1)];
|
||||
}
|
||||
|
||||
u16() {
|
||||
return this.#view.getUint16(this.#take(2), true);
|
||||
}
|
||||
|
||||
u32() {
|
||||
return this.#view.getUint32(this.#take(4), true);
|
||||
}
|
||||
|
||||
string() {
|
||||
const length = this.u8();
|
||||
const offset = this.#take(length);
|
||||
return new TextDecoder().decode(this.#bytes.subarray(offset, offset + length));
|
||||
}
|
||||
return { x, z, blocks: to_flat(sections), biomes };
|
||||
}
|
||||
|
||||
+138
-7
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user