World sections
This commit is contained in:
+12
-10
@@ -5,14 +5,12 @@
|
||||
// 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 { SECTION_HEIGHT } from "./world_shape.ts";
|
||||
import { type ChunkColumn, type Section, SECTION_VOLUME } 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;
|
||||
|
||||
const SECTION_VOLUME = CHUNK_AREA * SECTION_HEIGHT;
|
||||
|
||||
// the first byte of a binary message, the only kind there is so far
|
||||
export const LEVEL_CHUNK = 1;
|
||||
|
||||
@@ -37,6 +35,7 @@ export function is_within_view(
|
||||
return near * near + far * far < view_distance * view_distance;
|
||||
}
|
||||
|
||||
// what a client reads a level_chunk as
|
||||
export interface LevelChunk {
|
||||
x: number;
|
||||
z: number;
|
||||
@@ -61,7 +60,7 @@ export interface LevelChunk {
|
||||
// 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 encode_level_chunk(chunk: LevelChunk): Uint8Array<ArrayBuffer> {
|
||||
export function encode_level_chunk(chunk: ChunkColumn): Uint8Array<ArrayBuffer> {
|
||||
const writer = new Writer();
|
||||
|
||||
const biome_palette = [...new Set(chunk.biomes)];
|
||||
@@ -71,11 +70,8 @@ export function encode_level_chunk(chunk: LevelChunk): Uint8Array<ArrayBuffer> {
|
||||
for (const biome of chunk.biomes) writer.u8(biome_palette.indexOf(biome));
|
||||
}
|
||||
|
||||
const sections = chunk.blocks.length / SECTION_VOLUME;
|
||||
writer.u16(sections);
|
||||
for (let section = 0; section < sections; section++) {
|
||||
write_section(writer, chunk.blocks.subarray(section * SECTION_VOLUME, (section + 1) * SECTION_VOLUME));
|
||||
}
|
||||
writer.u16(chunk.sections.length);
|
||||
for (const section of chunk.sections) write_section(writer, section);
|
||||
|
||||
const body = deflateSync(writer.bytes());
|
||||
const bytes = new Uint8Array(9 + body.length);
|
||||
@@ -87,7 +83,13 @@ export function encode_level_chunk(chunk: LevelChunk): Uint8Array<ArrayBuffer> {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
function write_section(writer: Writer, blocks: Uint32Array) {
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user