World shape for dimension
This commit is contained in:
+18
-17
@@ -6,8 +6,9 @@
|
||||
//
|
||||
// u8 format, CHUNK_FORMAT
|
||||
// u32 palette size, then each entry: u8 id length, the id as utf-8, u16 state bits
|
||||
// the palette index of every block, in ServerWorld's order (y, then z, then x): u8 each for up to 256 entries,
|
||||
// u16 each for more
|
||||
// the palette index of every block, in ServerWorld's order (y from the dimension's min_y, then z, then x): u8 each
|
||||
// for up to 256 entries, u16 each for more. how many there are is the dimension's, see WorldShape.volume:
|
||||
// a chunk saved with another height doesn't read, and is generated again
|
||||
// u8 biome palette size, then each entry: u8 id length, the id as utf-8
|
||||
// the biome palette index of every column, u8 each, in the order of a layer's blocks
|
||||
//
|
||||
@@ -16,10 +17,9 @@
|
||||
// generation only shows in chunks never generated before.
|
||||
// format 1 had no biomes, and a bit per block after the blocks for whether world generation made it, from when only
|
||||
// chunks players changed were saved. both are read
|
||||
import { CHUNK_AREA, CHUNK_HEIGHT } from "@/common/constants.ts";
|
||||
import { CHUNK_AREA } from "@/common/constants.ts";
|
||||
|
||||
export const CHUNK_FORMAT = 2;
|
||||
export const CHUNK_VOLUME = CHUNK_AREA * CHUNK_HEIGHT;
|
||||
|
||||
export interface SavedChunk {
|
||||
x: number;
|
||||
@@ -64,7 +64,8 @@ export function encode_chunk(chunk: Required<ChunkBlocks>): Uint8Array<ArrayBuff
|
||||
if (chunk.biomes.length !== CHUNK_AREA || biome_palette.length > 255) throw new Error("Bad biomes");
|
||||
const biome_ids = biome_palette.map((id) => new TextEncoder().encode(id));
|
||||
if (biome_ids.some((id) => id.length > 255)) throw new Error("A biome id is too long to save");
|
||||
const size = 1 + 4 + ids.reduce((sum, id) => sum + 1 + id.length + 2, 0) + CHUNK_VOLUME * (wide ? 2 : 1) +
|
||||
const volume = chunk.blocks.length;
|
||||
const size = 1 + 4 + ids.reduce((sum, id) => sum + 1 + id.length + 2, 0) + volume * (wide ? 2 : 1) +
|
||||
1 + biome_ids.reduce((sum, id) => sum + 1 + id.length, 0) + CHUNK_AREA;
|
||||
const bytes = new Uint8Array(size);
|
||||
const view = new DataView(bytes.buffer);
|
||||
@@ -80,13 +81,13 @@ export function encode_chunk(chunk: Required<ChunkBlocks>): Uint8Array<ArrayBuff
|
||||
offset += 2;
|
||||
});
|
||||
if (wide) {
|
||||
for (let i = 0; i < CHUNK_VOLUME; i++) {
|
||||
for (let i = 0; i < volume; i++) {
|
||||
view.setUint16(offset, chunk.blocks[i]);
|
||||
offset += 2;
|
||||
}
|
||||
} else {
|
||||
bytes.set(chunk.blocks, offset);
|
||||
offset += CHUNK_VOLUME;
|
||||
offset += volume;
|
||||
}
|
||||
view.setUint8(offset++, biome_palette.length);
|
||||
for (const id of biome_ids) {
|
||||
@@ -98,15 +99,15 @@ export function encode_chunk(chunk: Required<ChunkBlocks>): Uint8Array<ArrayBuff
|
||||
return bytes;
|
||||
}
|
||||
|
||||
// throws when the bytes aren't a chunk this can read
|
||||
export function decode_chunk(bytes: Uint8Array): ChunkBlocks {
|
||||
// throws when the bytes aren't a chunk this can read. volume is how many blocks the dimension's chunks hold
|
||||
export function decode_chunk(bytes: Uint8Array, volume: number): ChunkBlocks {
|
||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
let offset = 0;
|
||||
const format = view.getUint8(offset++);
|
||||
if (format !== CHUNK_FORMAT && format !== 1) throw new Error(`Unknown chunk format ${format}`);
|
||||
const palette_size = view.getUint32(offset);
|
||||
offset += 4;
|
||||
if (palette_size > CHUNK_VOLUME) throw new Error("Bad palette");
|
||||
if (palette_size > volume) throw new Error("Bad palette");
|
||||
const palette: [string, number][] = [];
|
||||
for (let i = 0; i < palette_size; i++) {
|
||||
const length = view.getUint8(offset++);
|
||||
@@ -116,21 +117,21 @@ export function decode_chunk(bytes: Uint8Array): ChunkBlocks {
|
||||
offset += 2;
|
||||
}
|
||||
const wide = palette_size > 256;
|
||||
if (format === 1 && bytes.length !== offset + CHUNK_VOLUME * (wide ? 2 : 1) + CHUNK_VOLUME / 8) {
|
||||
if (format === 1 && bytes.length !== offset + volume * (wide ? 2 : 1) + volume / 8) {
|
||||
throw new Error("Wrong size");
|
||||
}
|
||||
if (bytes.length < offset + CHUNK_VOLUME * (wide ? 2 : 1)) throw new Error("Wrong size");
|
||||
const blocks = new Uint16Array(CHUNK_VOLUME);
|
||||
if (bytes.length < offset + volume * (wide ? 2 : 1)) throw new Error("Wrong size");
|
||||
const blocks = new Uint16Array(volume);
|
||||
if (wide) {
|
||||
for (let i = 0; i < CHUNK_VOLUME; i++) {
|
||||
for (let i = 0; i < volume; i++) {
|
||||
blocks[i] = view.getUint16(offset);
|
||||
offset += 2;
|
||||
}
|
||||
} else {
|
||||
blocks.set(bytes.subarray(offset, offset + CHUNK_VOLUME));
|
||||
offset += CHUNK_VOLUME;
|
||||
blocks.set(bytes.subarray(offset, offset + volume));
|
||||
offset += volume;
|
||||
}
|
||||
for (let i = 0; i < CHUNK_VOLUME; i++) {
|
||||
for (let i = 0; i < volume; i++) {
|
||||
if (blocks[i] >= palette_size) throw new Error("Bad palette index");
|
||||
}
|
||||
if (format === 1) return { palette, blocks };
|
||||
|
||||
Reference in New Issue
Block a user