3D biomes
This commit is contained in:
+25
-14
@@ -7,23 +7,31 @@
|
||||
// u8 format, CHUNK_FORMAT
|
||||
// i16 the bottom of the lowest section, the dimension's min_y when it was saved
|
||||
// u32 palette size, then each entry: u8 id length, the id as utf-8, u16 state bits
|
||||
// the biomes and the sections, bottom first, each block the index of its entry in the palette. see write_column in
|
||||
// common/section.ts, which level_chunk uses too
|
||||
// the sections' blocks and biomes, bottom first, each block the index of its entry in the palette. see
|
||||
// write_column in common/section.ts, which level_chunk uses too
|
||||
//
|
||||
// blocks are saved by their string ids, so mods can come and go without a save's numbers meaning other blocks. every
|
||||
// chunk the game generates is saved whole, like minecraft's, so a change to world generation only shows in chunks never
|
||||
// generated before. a chunk loaded into a dimension with another height keeps the sections both have (see
|
||||
// ServerWorld's #from_saved).
|
||||
// formats 1 and 2 are still read. they're big-endian and from when the world was 0 to 256 high: after the palette, the
|
||||
// formats 1, 2 and 3 are still read, with each column's biome all the way up. format 3 is this one from before biomes
|
||||
// had heights, see read_column's flat_biomes. they're big-endian and from when the world was 0 to 256 high: after the palette, the
|
||||
// index of every block from y 0 up, then z, then x, u8 each or u16 for more than 256 entries. format 2 then has a u8
|
||||
// biome palette size, each biome as u8 length and utf-8, and a u8 biome index a column. format 1 has no biomes, but a
|
||||
// bit per block for whether world generation made it, from when only chunks players changed were saved
|
||||
import { CHUNK_AREA } from "@/common/constants.ts";
|
||||
import { Reader, Writer } from "@/common/bytes.ts";
|
||||
import { read_column, Section, SECTION_VOLUME, write_column } from "@/common/section.ts";
|
||||
import {
|
||||
column_biomes,
|
||||
type ColumnBiomes,
|
||||
read_column,
|
||||
Section,
|
||||
SECTION_VOLUME,
|
||||
write_column,
|
||||
} from "@/common/section.ts";
|
||||
import { SECTION_HEIGHT } from "@/common/world_shape.ts";
|
||||
|
||||
export const CHUNK_FORMAT = 3;
|
||||
export const CHUNK_FORMAT = 4;
|
||||
// how many blocks a chunk of formats 1 and 2 has
|
||||
const FLAT_VOLUME = CHUNK_AREA * 256;
|
||||
|
||||
@@ -60,8 +68,8 @@ export interface ChunkBlocks {
|
||||
min_y: number;
|
||||
// the palette index of each block, a section at a time from min_y up
|
||||
sections: Section[];
|
||||
// each column's, see ServerWorld.biome_at. format 1 chunks don't have them
|
||||
biomes?: string[];
|
||||
// see ServerWorld.biome_at. format 1 chunks don't have them
|
||||
biomes?: ColumnBiomes;
|
||||
}
|
||||
|
||||
export function encode_chunk(chunk: Required<ChunkBlocks>): Uint8Array<ArrayBuffer> {
|
||||
@@ -80,9 +88,11 @@ export function encode_chunk(chunk: Required<ChunkBlocks>): Uint8Array<ArrayBuff
|
||||
// throws when the bytes aren't a chunk this can read
|
||||
export function decode_chunk(bytes: Uint8Array): ChunkBlocks {
|
||||
const format = bytes[0];
|
||||
if (format !== CHUNK_FORMAT && format !== 2 && format !== 1) throw new Error(`Unknown chunk format ${format}`);
|
||||
const reader = new Reader(bytes.subarray(1), format === CHUNK_FORMAT);
|
||||
const min_y = format === CHUNK_FORMAT ? reader.i16() : 0;
|
||||
if (format < 1 || format > CHUNK_FORMAT) throw new Error(`Unknown chunk format ${format}`);
|
||||
// formats 1 and 2 are big-endian, and flat
|
||||
const flat = format <= 2;
|
||||
const reader = new Reader(bytes.subarray(1), !flat);
|
||||
const min_y = flat ? 0 : reader.i16();
|
||||
if (min_y % SECTION_HEIGHT !== 0) throw new Error(`Bad min_y ${min_y}`);
|
||||
const palette: [string, number][] = [];
|
||||
const palette_size = reader.u32();
|
||||
@@ -92,8 +102,8 @@ export function decode_chunk(bytes: Uint8Array): ChunkBlocks {
|
||||
return value;
|
||||
};
|
||||
|
||||
if (format === CHUNK_FORMAT) {
|
||||
const { sections, biomes } = read_column(reader, index);
|
||||
if (!flat) {
|
||||
const { sections, biomes } = read_column(reader, index, format === 3);
|
||||
if (!reader.done) throw new Error("Wrong size");
|
||||
return { palette, min_y, sections, biomes };
|
||||
}
|
||||
@@ -105,17 +115,18 @@ export function decode_chunk(bytes: Uint8Array): ChunkBlocks {
|
||||
{ length: FLAT_VOLUME / SECTION_VOLUME },
|
||||
(_, i) => Section.of(blocks.subarray(i * SECTION_VOLUME, (i + 1) * SECTION_VOLUME)),
|
||||
);
|
||||
let biomes: string[] | undefined;
|
||||
let biomes: ColumnBiomes | undefined;
|
||||
if (format === 1) {
|
||||
reader.take(FLAT_VOLUME / 8);
|
||||
} else {
|
||||
const biome_palette: string[] = [];
|
||||
const biome_palette_size = reader.u8();
|
||||
for (let i = 0; i < biome_palette_size; i++) biome_palette.push(reader.string());
|
||||
biomes = [...reader.take(CHUNK_AREA)].map((i) => {
|
||||
const columns = [...reader.take(CHUNK_AREA)].map((i) => {
|
||||
if (i >= biome_palette_size) throw new Error("Bad biome palette index");
|
||||
return biome_palette[i];
|
||||
});
|
||||
biomes = column_biomes(columns, sections.length);
|
||||
}
|
||||
if (!reader.done) throw new Error("Wrong size");
|
||||
return { palette, min_y, sections, biomes };
|
||||
|
||||
Reference in New Issue
Block a user