Files
bworld/common/chunk_streaming.ts
2026-11-07 12:13:43 -03:00

74 lines
3.0 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// 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 { Reader, Writer } from "./bytes.ts";
import { type ChunkColumn, type ColumnBiomes, read_column, type Section, write_column } 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;
// the first byte of a binary message, the only kind there is so far
export const LEVEL_CHUNK = 1;
// 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(
center_x: number,
center_z: number,
view_distance: number,
x: number,
z: number,
outer = true,
) {
const dx = Math.max(0, Math.abs(x - center_x) - 1);
const dz = Math.max(0, Math.abs(z - center_z) - 1);
const far = Math.max(0, Math.max(dx, dz) - (outer ? 1 : 0));
const near = Math.min(dx, dz);
return near * near + far * far < view_distance * view_distance;
}
// what a client reads a level_chunk as
export interface LevelChunk {
x: number;
z: number;
// block values (numeric id | state bits << 16) in the receiver's numbering, see decode_level_chunk. from the bottom
// of the world up, the dimension's whole height
sections: Section[];
biomes: ColumnBiomes;
}
// a level_chunk message, like minecraft's ClientboundLevelChunkWithLightPacket without the light (clients light
// chunks themselves). sent as a binary websocket message, little-endian:
//
// u8 LEVEL_CHUNK
// i32 chunk x, i32 chunk z
// the rest deflated: the sections' block values and biomes, see write_column in common/section.ts
export function encode_level_chunk(chunk: ChunkColumn): Uint8Array<ArrayBuffer> {
const writer = new Writer();
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);
bytes[0] = LEVEL_CHUNK;
view.setInt32(1, chunk.x, true);
view.setInt32(5, chunk.z, true);
bytes.set(body, 9);
return bytes;
}
// 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");
const header = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const x = header.getInt32(1, true);
const z = header.getInt32(5, true);
const reader = new Reader(inflateSync(bytes.subarray(9)));
const { sections, biomes } = read_column(reader, map);
if (!reader.done) throw new Error("Wrong size");
return { x, z, sections, biomes };
}