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
|
// 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
|
// player, and which chunks are in a player's view. see "Chunk Streaming" in docs/modding_plan.md
|
||||||
import { deflateSync, inflateSync } from "fflate";
|
import { deflateSync, inflateSync } from "fflate";
|
||||||
import { CHUNK_AREA } from "./constants.ts";
|
import { Reader, Writer } from "./bytes.ts";
|
||||||
import { type ChunkColumn, type Section, SECTION_VOLUME } from "./section.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
|
// the view distances clients can ask for, in chunks, like minecraft's
|
||||||
export const MIN_VIEW_DISTANCE = 2;
|
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
|
// the first byte of a binary message, the only kind there is so far
|
||||||
export const LEVEL_CHUNK = 1;
|
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
|
// 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
|
// client needs to mesh and light the edge of what it shows
|
||||||
export function is_within_view(
|
export function is_within_view(
|
||||||
@@ -51,28 +47,10 @@ export interface LevelChunk {
|
|||||||
//
|
//
|
||||||
// u8 LEVEL_CHUNK
|
// u8 LEVEL_CHUNK
|
||||||
// i32 chunk x, i32 chunk z
|
// i32 chunk x, i32 chunk z
|
||||||
// the rest deflated:
|
// the rest deflated: the biomes and the sections' block values, see write_column in common/section.ts
|
||||||
// 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
|
|
||||||
export function encode_level_chunk(chunk: ChunkColumn): Uint8Array<ArrayBuffer> {
|
export function encode_level_chunk(chunk: ChunkColumn): Uint8Array<ArrayBuffer> {
|
||||||
const writer = new Writer();
|
const writer = new Writer();
|
||||||
|
write_column(writer, chunk.sections, chunk.biomes);
|
||||||
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);
|
|
||||||
|
|
||||||
const body = deflateSync(writer.bytes());
|
const body = deflateSync(writer.bytes());
|
||||||
const bytes = new Uint8Array(9 + body.length);
|
const bytes = new Uint8Array(9 + body.length);
|
||||||
const view = new DataView(bytes.buffer);
|
const view = new DataView(bytes.buffer);
|
||||||
@@ -83,45 +61,6 @@ export function encode_level_chunk(chunk: ChunkColumn): Uint8Array<ArrayBuffer>
|
|||||||
return bytes;
|
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
|
// 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 {
|
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");
|
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 x = header.getInt32(1, true);
|
||||||
const z = header.getInt32(5, true);
|
const z = header.getInt32(5, true);
|
||||||
const reader = new Reader(inflateSync(bytes.subarray(9)));
|
const reader = new Reader(inflateSync(bytes.subarray(9)));
|
||||||
|
const { sections, biomes } = read_column(reader, map);
|
||||||
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);
|
|
||||||
}
|
|
||||||
if (!reader.done) throw new Error("Wrong size");
|
if (!reader.done) throw new Error("Wrong size");
|
||||||
return { x, z, blocks, biomes };
|
return { x, z, blocks: to_flat(sections), 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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+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
|
// 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
|
// 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 { 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;
|
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
|
// every block's value in one array, like from_flat's
|
||||||
to_flat(): Uint32Array {
|
to_flat(): Uint32Array {
|
||||||
const blocks = new Uint32Array(this.shape.volume);
|
return to_flat(this.sections);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
// 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
|
// 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
|
// numeric id. sections that are all air, or all clear, are passed over whole
|
||||||
|
|||||||
+70
-100
@@ -2,24 +2,30 @@
|
|||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
// what a world is saved as, apart from the files: level.json's text and each chunk's blocks. server/regions.ts puts
|
// what a world is saved as, apart from the files: level.json's text and each chunk's blocks. server/regions.ts puts
|
||||||
// the chunks into region files, this is what's inside one chunk's entry before it's compressed:
|
// the chunks into region files, this is what's inside one chunk's entry before it's compressed, little-endian:
|
||||||
//
|
//
|
||||||
// u8 format, CHUNK_FORMAT
|
// 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
|
// 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 from the dimension's min_y, then z, then x): u8 each
|
// the biomes and the sections, bottom first, each block the index of its entry in the palette. see write_column in
|
||||||
// for up to 256 entries, u16 each for more. how many there are is the dimension's, see WorldShape.volume:
|
// common/section.ts, which level_chunk uses too
|
||||||
// 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
|
|
||||||
//
|
//
|
||||||
// numbers are big-endian. blocks are saved by their string ids, so mods can come and go without a save's numbers
|
// blocks are saved by their string ids, so mods can come and go without a save's numbers meaning other blocks. every
|
||||||
// meaning other blocks. every chunk the game generates is saved whole, like minecraft's, so a change to world
|
// chunk the game generates is saved whole, like minecraft's, so a change to world generation only shows in chunks never
|
||||||
// generation only shows in chunks never generated before.
|
// generated before. a chunk loaded into a dimension with another height keeps the sections both have (see
|
||||||
// format 1 had no biomes, and a bit per block after the blocks for whether world generation made it, from when only
|
// ServerWorld's #from_saved).
|
||||||
// chunks players changed were saved. both are read
|
// 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
|
||||||
|
// 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 { 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 { SECTION_HEIGHT } from "@/common/world_shape.ts";
|
||||||
|
|
||||||
export const CHUNK_FORMAT = 2;
|
export const CHUNK_FORMAT = 3;
|
||||||
|
// how many blocks a chunk of formats 1 and 2 has
|
||||||
|
const FLAT_VOLUME = CHUNK_AREA * 256;
|
||||||
|
|
||||||
export interface SavedChunk {
|
export interface SavedChunk {
|
||||||
x: number;
|
x: number;
|
||||||
@@ -50,103 +56,67 @@ export interface SavedChunks {
|
|||||||
export interface ChunkBlocks {
|
export interface ChunkBlocks {
|
||||||
// an id and its state bits
|
// an id and its state bits
|
||||||
palette: [string, number][];
|
palette: [string, number][];
|
||||||
// the palette index of each block
|
// the bottom of the lowest section
|
||||||
blocks: Uint16Array;
|
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
|
// each column's, see ServerWorld.biome_at. format 1 chunks don't have them
|
||||||
biomes?: string[];
|
biomes?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function encode_chunk(chunk: Required<ChunkBlocks>): Uint8Array<ArrayBuffer> {
|
export function encode_chunk(chunk: Required<ChunkBlocks>): Uint8Array<ArrayBuffer> {
|
||||||
const ids = chunk.palette.map(([id]) => new TextEncoder().encode(id));
|
const writer = new Writer();
|
||||||
if (ids.some((id) => id.length > 255)) throw new Error("A block id is too long to save");
|
writer.u8(CHUNK_FORMAT);
|
||||||
const wide = chunk.palette.length > 256;
|
writer.i16(chunk.min_y);
|
||||||
const biome_palette = [...new Set(chunk.biomes)];
|
writer.u32(chunk.palette.length);
|
||||||
if (chunk.biomes.length !== CHUNK_AREA || biome_palette.length > 255) throw new Error("Bad biomes");
|
for (const [id, state] of chunk.palette) {
|
||||||
const biome_ids = biome_palette.map((id) => new TextEncoder().encode(id));
|
writer.string(id);
|
||||||
if (biome_ids.some((id) => id.length > 255)) throw new Error("A biome id is too long to save");
|
writer.u16(state);
|
||||||
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);
|
|
||||||
let offset = 0;
|
|
||||||
view.setUint8(offset++, CHUNK_FORMAT);
|
|
||||||
view.setUint32(offset, chunk.palette.length);
|
|
||||||
offset += 4;
|
|
||||||
chunk.palette.forEach(([, state], i) => {
|
|
||||||
view.setUint8(offset++, ids[i].length);
|
|
||||||
bytes.set(ids[i], offset);
|
|
||||||
offset += ids[i].length;
|
|
||||||
view.setUint16(offset, state);
|
|
||||||
offset += 2;
|
|
||||||
});
|
|
||||||
if (wide) {
|
|
||||||
for (let i = 0; i < volume; i++) {
|
|
||||||
view.setUint16(offset, chunk.blocks[i]);
|
|
||||||
offset += 2;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
bytes.set(chunk.blocks, offset);
|
|
||||||
offset += volume;
|
|
||||||
}
|
}
|
||||||
view.setUint8(offset++, biome_palette.length);
|
write_column(writer, chunk.sections, chunk.biomes);
|
||||||
for (const id of biome_ids) {
|
return writer.bytes().slice();
|
||||||
view.setUint8(offset++, id.length);
|
|
||||||
bytes.set(id, offset);
|
|
||||||
offset += id.length;
|
|
||||||
}
|
|
||||||
for (const biome of chunk.biomes) bytes[offset++] = biome_palette.indexOf(biome);
|
|
||||||
return bytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// throws when the bytes aren't a chunk this can read. volume is how many blocks the dimension's chunks hold
|
// throws when the bytes aren't a chunk this can read
|
||||||
export function decode_chunk(bytes: Uint8Array, volume: number): ChunkBlocks {
|
export function decode_chunk(bytes: Uint8Array): ChunkBlocks {
|
||||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
const format = bytes[0];
|
||||||
let offset = 0;
|
if (format !== CHUNK_FORMAT && format !== 2 && format !== 1) throw new Error(`Unknown chunk format ${format}`);
|
||||||
const format = view.getUint8(offset++);
|
const reader = new Reader(bytes.subarray(1), format === CHUNK_FORMAT);
|
||||||
if (format !== CHUNK_FORMAT && format !== 1) throw new Error(`Unknown chunk format ${format}`);
|
const min_y = format === CHUNK_FORMAT ? reader.i16() : 0;
|
||||||
const palette_size = view.getUint32(offset);
|
if (min_y % SECTION_HEIGHT !== 0) throw new Error(`Bad min_y ${min_y}`);
|
||||||
offset += 4;
|
|
||||||
if (palette_size > volume) throw new Error("Bad palette");
|
|
||||||
const palette: [string, number][] = [];
|
const palette: [string, number][] = [];
|
||||||
for (let i = 0; i < palette_size; i++) {
|
const palette_size = reader.u32();
|
||||||
const length = view.getUint8(offset++);
|
for (let i = 0; i < palette_size; i++) palette.push([reader.string(), reader.u16()]);
|
||||||
const id = new TextDecoder().decode(bytes.subarray(offset, offset + length));
|
const index = (value: number) => {
|
||||||
offset += length;
|
if (value >= palette_size) throw new Error("Bad palette index");
|
||||||
palette.push([id, view.getUint16(offset)]);
|
return value;
|
||||||
offset += 2;
|
};
|
||||||
}
|
|
||||||
const wide = palette_size > 256;
|
|
||||||
if (format === 1 && bytes.length !== offset + volume * (wide ? 2 : 1) + volume / 8) {
|
|
||||||
throw new Error("Wrong size");
|
|
||||||
}
|
|
||||||
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 < volume; i++) {
|
|
||||||
blocks[i] = view.getUint16(offset);
|
|
||||||
offset += 2;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
blocks.set(bytes.subarray(offset, offset + volume));
|
|
||||||
offset += volume;
|
|
||||||
}
|
|
||||||
for (let i = 0; i < volume; i++) {
|
|
||||||
if (blocks[i] >= palette_size) throw new Error("Bad palette index");
|
|
||||||
}
|
|
||||||
if (format === 1) return { palette, blocks };
|
|
||||||
|
|
||||||
const biome_palette: string[] = [];
|
if (format === CHUNK_FORMAT) {
|
||||||
const biome_palette_size = view.getUint8(offset++);
|
const { sections, biomes } = read_column(reader, index);
|
||||||
for (let i = 0; i < biome_palette_size; i++) {
|
if (!reader.done) throw new Error("Wrong size");
|
||||||
const length = view.getUint8(offset++);
|
return { palette, min_y, sections, biomes };
|
||||||
biome_palette.push(new TextDecoder().decode(bytes.subarray(offset, offset + length)));
|
|
||||||
offset += length;
|
|
||||||
}
|
}
|
||||||
if (bytes.length !== offset + CHUNK_AREA) throw new Error("Wrong size");
|
|
||||||
const biomes = [...bytes.subarray(offset)].map((index) => {
|
const blocks = new Uint32Array(FLAT_VOLUME);
|
||||||
if (index >= biome_palette_size) throw new Error("Bad biome palette index");
|
const wide = palette_size > 256;
|
||||||
return biome_palette[index];
|
for (let i = 0; i < FLAT_VOLUME; i++) blocks[i] = index(wide ? reader.u16() : reader.u8());
|
||||||
});
|
const sections = Array.from(
|
||||||
return { palette, blocks, biomes };
|
{ length: FLAT_VOLUME / SECTION_VOLUME },
|
||||||
|
(_, i) => Section.of(blocks.subarray(i * SECTION_VOLUME, (i + 1) * SECTION_VOLUME)),
|
||||||
|
);
|
||||||
|
let biomes: string[] | 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) => {
|
||||||
|
if (i >= biome_palette_size) throw new Error("Bad biome palette index");
|
||||||
|
return biome_palette[i];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (!reader.done) throw new Error("Wrong size");
|
||||||
|
return { palette, min_y, sections, biomes };
|
||||||
}
|
}
|
||||||
|
|||||||
+35
-22
@@ -2,8 +2,8 @@
|
|||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
import { AIR, CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
|
import { AIR, CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
|
||||||
import type { WorldShape } from "@/common/world_shape.ts";
|
import { SECTION_HEIGHT, type WorldShape } from "@/common/world_shape.ts";
|
||||||
import { ChunkColumn, column_heights, SECTION_VOLUME } from "@/common/section.ts";
|
import { ChunkColumn, column_heights, Section, SECTION_VOLUME } from "@/common/section.ts";
|
||||||
import {
|
import {
|
||||||
block_light_emission,
|
block_light_emission,
|
||||||
block_light_opacity,
|
block_light_opacity,
|
||||||
@@ -501,13 +501,9 @@ export class ServerWorld {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const { sections, biomes } = chunk.column;
|
const { sections, biomes } = chunk.column;
|
||||||
const blocks = new Uint16Array(this.shape.volume);
|
const saved = sections.map((section) => {
|
||||||
sections.forEach((section, s) => {
|
if (section.blocks === null) return new Section(value_index(section.value));
|
||||||
const start = s * SECTION_VOLUME;
|
const indexes = new Uint32Array(SECTION_VOLUME);
|
||||||
if (section.blocks === null) {
|
|
||||||
blocks.fill(value_index(section.value), start, start + SECTION_VOLUME);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// runs of the same block are common, like air above the ground and stone below
|
// runs of the same block are common, like air above the ground and stone below
|
||||||
let last_value = -1;
|
let last_value = -1;
|
||||||
let last_index = 0;
|
let last_index = 0;
|
||||||
@@ -517,11 +513,14 @@ export class ServerWorld {
|
|||||||
last_value = value;
|
last_value = value;
|
||||||
last_index = value_index(value);
|
last_index = value_index(value);
|
||||||
}
|
}
|
||||||
blocks[start + i] = last_index;
|
indexes[i] = last_index;
|
||||||
}
|
}
|
||||||
|
return new Section(indexes);
|
||||||
});
|
});
|
||||||
for (const [i, [id, state]] of chunk.unknown ?? []) blocks[i] = index_of(id, state);
|
for (const [i, [id, state]] of chunk.unknown ?? []) {
|
||||||
return { palette, blocks, biomes };
|
saved[Math.floor(i / SECTION_VOLUME)].set(i % SECTION_VOLUME, index_of(id, state));
|
||||||
|
}
|
||||||
|
return { palette, min_y: this.shape.min_y, sections: saved, biomes };
|
||||||
}
|
}
|
||||||
|
|
||||||
get_tile(x: number, y: number, z: number) {
|
get_tile(x: number, y: number, z: number) {
|
||||||
@@ -671,7 +670,7 @@ export class ServerWorld {
|
|||||||
const data = evicted?.data ?? this.#saved_chunks?.read(chunk_x, chunk_z);
|
const data = evicted?.data ?? this.#saved_chunks?.read(chunk_x, chunk_z);
|
||||||
try {
|
try {
|
||||||
if (!data) throw new Error("it couldn't be read");
|
if (!data) throw new Error("it couldn't be read");
|
||||||
return this.#from_saved(chunk_x, chunk_z, decode_chunk(data, this.shape.volume));
|
return this.#from_saved(chunk_x, chunk_z, decode_chunk(data));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(
|
console.error(
|
||||||
`Couldn't load chunk ${chunk_x}, ${chunk_z}, generating it again: ${(e as Error).message}`,
|
`Couldn't load chunk ${chunk_x}, ${chunk_z}, generating it again: ${(e as Error).message}`,
|
||||||
@@ -689,16 +688,30 @@ export class ServerWorld {
|
|||||||
const nid = id === AIR_ID ? AIR : this.block_ids[id];
|
const nid = id === AIR_ID ? AIR : this.block_ids[id];
|
||||||
return nid === undefined ? undefined : block_value(nid, state);
|
return nid === undefined ? undefined : block_value(nid, state);
|
||||||
});
|
});
|
||||||
const blocks = new Uint32Array(this.shape.volume);
|
|
||||||
let unknown: Map<number, [string, number]> | undefined;
|
let unknown: Map<number, [string, number]> | undefined;
|
||||||
for (let i = 0; i < blocks.length; i++) {
|
// sections the save doesn't have are air, and ones the dimension doesn't have are left out
|
||||||
const value = values[saved.blocks[i]];
|
const offset = (this.shape.min_y - saved.min_y) / SECTION_HEIGHT;
|
||||||
if (value === undefined) {
|
const sections = Array.from({ length: this.shape.height / SECTION_HEIGHT }, (_, s) => {
|
||||||
(unknown ??= new Map()).set(i, saved.palette[saved.blocks[i]]);
|
const section = saved.sections[offset + s];
|
||||||
} else {
|
if (!section) return new Section(AIR);
|
||||||
blocks[i] = value;
|
const start = s * SECTION_VOLUME;
|
||||||
|
const blocks = section.blocks;
|
||||||
|
if (blocks === null) {
|
||||||
|
const value = values[section.value];
|
||||||
|
if (value === undefined) {
|
||||||
|
for (let i = 0; i < SECTION_VOLUME; i++) {
|
||||||
|
(unknown ??= new Map()).set(start + i, saved.palette[section.value]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Section(value ?? AIR);
|
||||||
}
|
}
|
||||||
}
|
for (let i = 0; i < SECTION_VOLUME; i++) {
|
||||||
|
const value = values[blocks[i]];
|
||||||
|
if (value === undefined) (unknown ??= new Map()).set(start + i, saved.palette[blocks[i]]);
|
||||||
|
blocks[i] = value ?? AIR;
|
||||||
|
}
|
||||||
|
return section;
|
||||||
|
});
|
||||||
let biomes = saved.biomes;
|
let biomes = saved.biomes;
|
||||||
if (!biomes) {
|
if (!biomes) {
|
||||||
// saved before chunks had them. saved again with them
|
// saved before chunks had them. saved again with them
|
||||||
@@ -706,7 +719,7 @@ export class ServerWorld {
|
|||||||
biomes = [...this.#biomes.get(key) ?? this.#get_raw(chunk_x, chunk_z).column.biomes];
|
biomes = [...this.#biomes.get(key) ?? this.#get_raw(chunk_x, chunk_z).column.biomes];
|
||||||
this.#mark_unsaved(chunk_x, chunk_z);
|
this.#mark_unsaved(chunk_x, chunk_z);
|
||||||
}
|
}
|
||||||
return { column: ChunkColumn.from_flat(chunk_x, chunk_z, this.shape, blocks, biomes), unknown };
|
return { column: new ChunkColumn(chunk_x, chunk_z, this.shape, sections, biomes), unknown };
|
||||||
}
|
}
|
||||||
|
|
||||||
// an unsaved chunk is encoded to be saved, instead of being generated again or kept whole
|
// an unsaved chunk is encoded to be saved, instead of being generated again or kept whole
|
||||||
|
|||||||
@@ -154,6 +154,25 @@ Deno.test("a dimension's blocks go from its min_y to max_y - 1, and are sent and
|
|||||||
for (const y of [-64, 319]) assertEquals(loaded.game.world.get_block_id(GROUND_X, y, 0), "bworld:stone");
|
for (const y of [-64, 319]) assertEquals(loaded.game.world.get_block_id(GROUND_X, y, 0), "bworld:stone");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("a chunk saved at another height keeps the sections both heights have", async () => {
|
||||||
|
const tall = mods({ flat: true, bworld_default: false, shape: TALL });
|
||||||
|
const { game } = await test_game(tall);
|
||||||
|
for (const y of [-64, 0, 255, 319]) game.set_block(GROUND_X, y, 0, "bworld:stone");
|
||||||
|
const saved = game.save();
|
||||||
|
|
||||||
|
const short = mods({ flat: true, bworld_default: false });
|
||||||
|
const { game: shorter } = await test_game(short, saved);
|
||||||
|
for (const y of [0, 255]) assertEquals(shorter.world.get_block_id(GROUND_X, y, 0), "bworld:stone");
|
||||||
|
assertEquals(shorter.world.get_block_id(GROUND_X, 254, 0), "cubi:air");
|
||||||
|
shorter.set_block(GROUND_X, 1, 0, "bworld:stone");
|
||||||
|
|
||||||
|
// and back, where what the shorter one didn't keep is air, like a dimension that grew
|
||||||
|
const { game: again } = await test_game(tall, shorter.save());
|
||||||
|
for (const dir of [tall, short]) Deno.removeSync(dir, { recursive: true });
|
||||||
|
for (const y of [0, 1, 255]) assertEquals(again.world.get_block_id(GROUND_X, y, 0), "bworld:stone");
|
||||||
|
for (const y of [-64, 319]) assertEquals(again.world.get_block_id(GROUND_X, y, 0), "cubi:air");
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test("under a dimension's ground it's dark, above its top it's open sky, and things fall out of its bottom", async () => {
|
Deno.test("under a dimension's ground it's dark, above its top it's open sky, and things fall out of its bottom", async () => {
|
||||||
const dir = mods({ flat: true, bworld_default: false, shape: TALL, ground: "bworld:stone" });
|
const dir = mods({ flat: true, bworld_default: false, shape: TALL, ground: "bworld:stone" });
|
||||||
const { game } = await test_game(dir);
|
const { game } = await test_game(dir);
|
||||||
|
|||||||
+3
-6
@@ -17,10 +17,7 @@ import { PROTOCOL_VERSION } from "@/common/protocol.ts";
|
|||||||
import { decode_level_chunk, type LevelChunk } from "@/common/chunk_streaming.ts";
|
import { decode_level_chunk, type LevelChunk } from "@/common/chunk_streaming.ts";
|
||||||
import { decode_chunk, encode_chunk } from "@/server/game/saves.ts";
|
import { decode_chunk, encode_chunk } from "@/server/game/saves.ts";
|
||||||
import { list_chunks, read_chunk, write_chunks } from "@/server/regions.ts";
|
import { list_chunks, read_chunk, write_chunks } from "@/server/regions.ts";
|
||||||
import { DEFAULT_SHAPE } from "@/common/world_shape.ts";
|
import { section_index } from "@/common/section.ts";
|
||||||
|
|
||||||
// the base game's dimension, which the test worlds play
|
|
||||||
const VOLUME = DEFAULT_SHAPE.volume;
|
|
||||||
|
|
||||||
const ENGINE_TEXTURES = ["cubi:missing", ...Array.from({ length: 9 }, (_, i) => `cubi:break_${i}`)];
|
const ENGINE_TEXTURES = ["cubi:missing", ...Array.from({ length: 9 }, (_, i) => `cubi:break_${i}`)];
|
||||||
|
|
||||||
@@ -407,8 +404,8 @@ Deno.test({
|
|||||||
await write_chunks(
|
await write_chunks(
|
||||||
region,
|
region,
|
||||||
await Promise.all(saved.map(async ([x, z]) => {
|
await Promise.all(saved.map(async ([x, z]) => {
|
||||||
const chunk = decode_chunk((await read_chunk(region, x, z))!, VOLUME);
|
const chunk = decode_chunk((await read_chunk(region, x, z))!);
|
||||||
chunk.blocks[VOLUME - 256] = chunk.palette.push(["bworld:stone", 0]) - 1;
|
chunk.sections.at(-1)!.set(section_index(0, 15, 0), chunk.palette.push(["bworld:stone", 0]) - 1);
|
||||||
return { x, z, data: encode_chunk(chunk as Required<typeof chunk>) };
|
return { x, z, data: encode_chunk(chunk as Required<typeof chunk>) };
|
||||||
})),
|
})),
|
||||||
);
|
);
|
||||||
|
|||||||
+41
-2
@@ -2,10 +2,19 @@
|
|||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
// chunks' blocks in sections of 16x16x16, see common/section.ts
|
// chunks' blocks in sections of 16x16x16, see common/section.ts
|
||||||
import { assert, assertEquals } from "@std/assert";
|
import { assert, assertEquals, assertThrows } from "@std/assert";
|
||||||
import { Alea } from "@paulaboks/rng";
|
import { Alea } from "@paulaboks/rng";
|
||||||
import { AIR, CHUNK_AREA } from "@/common/constants.ts";
|
import { AIR, CHUNK_AREA } from "@/common/constants.ts";
|
||||||
import { ChunkColumn, column_heights, Section, SECTION_VOLUME } from "@/common/section.ts";
|
import { Reader, Writer } from "@/common/bytes.ts";
|
||||||
|
import {
|
||||||
|
ChunkColumn,
|
||||||
|
column_heights,
|
||||||
|
read_column,
|
||||||
|
Section,
|
||||||
|
SECTION_VOLUME,
|
||||||
|
to_flat,
|
||||||
|
write_column,
|
||||||
|
} from "@/common/section.ts";
|
||||||
import { WorldShape } from "@/common/world_shape.ts";
|
import { WorldShape } from "@/common/world_shape.ts";
|
||||||
|
|
||||||
const SHAPE = new WorldShape({ min_y: -64, max_y: 320, sky_light: true });
|
const SHAPE = new WorldShape({ min_y: -64, max_y: 320, sky_light: true });
|
||||||
@@ -87,3 +96,33 @@ Deno.test("column heights are the highest block and the lowest y that sees the s
|
|||||||
assertEquals([nothing[0], nothing[CHUNK_AREA]], [SHAPE.min_y - 1, SHAPE.min_y]);
|
assertEquals([nothing[0], nothing[CHUNK_AREA]], [SHAPE.min_y - 1, SHAPE.min_y]);
|
||||||
assert(heights.some((height, i) => i < CHUNK_AREA && height === 255), "the glass is the top");
|
assert(heights.some((height, i) => i < CHUNK_AREA && height === 255), "the glass is the top");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("sections are written and read back as they were, however many kinds of block they hold", () => {
|
||||||
|
const random = new Alea("paletted");
|
||||||
|
// one kind, then the most and one more than each palette size holds, past what a palette is for
|
||||||
|
const kinds = [1, 2, 3, 4, 5, 16, 17, 256, 257, 4096];
|
||||||
|
const sections = kinds.map((count) => {
|
||||||
|
// big values too, with state bits
|
||||||
|
const values = Array.from({ length: count }, (_, i) => (i * 7919 + (i % 3) * 0x8000_0000) >>> 0);
|
||||||
|
return Section.of(
|
||||||
|
Uint32Array.from(
|
||||||
|
{ length: SECTION_VOLUME },
|
||||||
|
(_, i) => values[i < count ? i : Math.floor(random.next() * count)],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
sections.push(new Section(GLASS));
|
||||||
|
const biomes = Array.from({ length: CHUNK_AREA }, (_, i) => `test:biome_${i % 3}`);
|
||||||
|
|
||||||
|
const writer = new Writer();
|
||||||
|
write_column(writer, sections, biomes);
|
||||||
|
const bytes = writer.bytes();
|
||||||
|
const read = read_column(new Reader(bytes), (value) => value);
|
||||||
|
assertEquals(to_flat(read.sections), to_flat(sections));
|
||||||
|
assertEquals(read.biomes, biomes);
|
||||||
|
assertEquals(read.sections.at(-1)!.blocks, null, "a section of one block stays one value");
|
||||||
|
|
||||||
|
// the values as the reader numbers them
|
||||||
|
assertEquals(read_column(new Reader(bytes), (value) => value + 1).sections.at(-1)!.value, GLASS + 1);
|
||||||
|
assertThrows(() => read_column(new Reader(bytes.subarray(0, bytes.length - 1)), (value) => value));
|
||||||
|
});
|
||||||
|
|||||||
+81
-51
@@ -4,52 +4,82 @@
|
|||||||
import { assert, assertEquals } from "@std/assert";
|
import { assert, assertEquals } from "@std/assert";
|
||||||
import { AIR_ID } from "@/common/protocol.ts";
|
import { AIR_ID } from "@/common/protocol.ts";
|
||||||
import { CHUNK_AREA } from "@/common/constants.ts";
|
import { CHUNK_AREA } from "@/common/constants.ts";
|
||||||
import { decode_chunk, encode_chunk, SavedChunk } from "@/server/game/saves.ts";
|
import { ChunkBlocks, decode_chunk, encode_chunk, SavedChunk } from "@/server/game/saves.ts";
|
||||||
import { list_chunks, read_chunk, region_file, write_chunks } from "@/server/regions.ts";
|
import { list_chunks, read_chunk, region_file, write_chunks } from "@/server/regions.ts";
|
||||||
import { sent_block, test_game } from "./helpers.ts";
|
import { sent_block, test_game } from "./helpers.ts";
|
||||||
import { DEFAULT_SHAPE } from "@/common/world_shape.ts";
|
import { DEFAULT_SHAPE, WorldShape } from "@/common/world_shape.ts";
|
||||||
|
import { ChunkColumn, section_index, to_flat } from "@/common/section.ts";
|
||||||
|
|
||||||
// the base game's dimension, which the test worlds play
|
// a chunk at the height of the base game's dimension, which the test worlds play
|
||||||
const VOLUME = DEFAULT_SHAPE.volume;
|
function chunk_data(seed: number, palette_size = 3, shape = DEFAULT_SHAPE): Required<ChunkBlocks> {
|
||||||
|
|
||||||
function chunk_data(seed: number, palette_size = 3) {
|
|
||||||
const palette: [string, number][] = [];
|
const palette: [string, number][] = [];
|
||||||
for (let i = 0; i < palette_size; i++) palette.push([`test:block_${i}`, i % 5]);
|
for (let i = 0; i < palette_size; i++) palette.push([`test:block_${i}`, i % 5]);
|
||||||
const blocks = new Uint16Array(VOLUME).map((_, i) => (i * seed) % palette_size);
|
const blocks = new Uint32Array(shape.volume).map((_, i) => (i * seed) % palette_size);
|
||||||
const biomes = Array.from({ length: CHUNK_AREA }, (_, i) => `test:biome_${(i * seed) % 4}`);
|
const biomes = Array.from({ length: CHUNK_AREA }, (_, i) => `test:biome_${(i * seed) % 4}`);
|
||||||
return { palette, blocks, biomes };
|
return {
|
||||||
|
palette,
|
||||||
|
min_y: shape.min_y,
|
||||||
|
sections: ChunkColumn.from_flat(0, 0, shape, blocks, biomes).sections,
|
||||||
|
biomes,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const index = (x: number, y: number, z: number) => y * CHUNK_AREA + z * 16 + x;
|
// the palette entry of a saved chunk's block
|
||||||
|
function saved_block(chunk: ChunkBlocks, x: number, y: number, z: number) {
|
||||||
// what a chunk was saved as before chunks had biomes: a bit per block after the blocks instead, for whether world
|
return chunk.palette[chunk.sections[(y - chunk.min_y) >> 4].get(section_index(x, y, z))];
|
||||||
// generation made it
|
|
||||||
function format_1(data: Uint8Array<ArrayBuffer>) {
|
|
||||||
const chunk = decode_chunk(data, VOLUME);
|
|
||||||
const biomes = 1 +
|
|
||||||
[...new Set(chunk.biomes)].reduce((sum, id) => sum + 1 + new TextEncoder().encode(id).length, 0) +
|
|
||||||
CHUNK_AREA;
|
|
||||||
const old = new Uint8Array(data.length - biomes + VOLUME / 8);
|
|
||||||
old.set(data.subarray(0, data.length - biomes));
|
|
||||||
old[0] = 1;
|
|
||||||
return old;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function set_saved_block(chunk: ChunkBlocks, x: number, y: number, z: number, index: number) {
|
||||||
|
chunk.sections[(y - chunk.min_y) >> 4].set(section_index(x, y, z), index);
|
||||||
|
}
|
||||||
|
|
||||||
|
// a chunk as formats 1 and 2 saved it, 0 to 256 high and big-endian. format 1 had no biomes, and a bit per block after
|
||||||
|
// the blocks instead, for whether world generation made it
|
||||||
|
function old_format(format: 1 | 2, chunk: ChunkBlocks) {
|
||||||
|
const flat = to_flat(chunk.sections);
|
||||||
|
const wide = chunk.palette.length > 256;
|
||||||
|
const bytes: number[] = [format, ...u32(chunk.palette.length)];
|
||||||
|
for (const [id, state] of chunk.palette) bytes.push(...string(id), state >> 8, state & 255);
|
||||||
|
for (const index of flat) bytes.push(...wide ? [index >> 8, index & 255] : [index]);
|
||||||
|
if (format === 1) {
|
||||||
|
bytes.push(...new Uint8Array(flat.length / 8));
|
||||||
|
} else {
|
||||||
|
const biome_palette = [...new Set(chunk.biomes)];
|
||||||
|
bytes.push(biome_palette.length, ...biome_palette.flatMap(string));
|
||||||
|
bytes.push(...chunk.biomes!.map((biome) => biome_palette.indexOf(biome)));
|
||||||
|
}
|
||||||
|
return Uint8Array.from(bytes);
|
||||||
|
}
|
||||||
|
const u32 = (value: number) => [value >>> 24, (value >> 16) & 255, (value >> 8) & 255, value & 255];
|
||||||
|
const string = (text: string) => [text.length, ...new TextEncoder().encode(text)];
|
||||||
|
|
||||||
Deno.test("a chunk's blocks and biomes come back as they were saved, with more than 256 kinds too", () => {
|
Deno.test("a chunk's blocks and biomes come back as they were saved, with more than 256 kinds too", () => {
|
||||||
for (const palette_size of [1, 3, 256, 300]) {
|
for (const palette_size of [1, 3, 256, 300]) {
|
||||||
const chunk = chunk_data(7, palette_size);
|
const chunk = chunk_data(7, palette_size);
|
||||||
const decoded = decode_chunk(encode_chunk(chunk), VOLUME);
|
const decoded = decode_chunk(encode_chunk(chunk));
|
||||||
assertEquals(decoded.palette, chunk.palette);
|
assertEquals(decoded.palette, chunk.palette);
|
||||||
assertEquals(decoded.blocks, chunk.blocks);
|
assertEquals(decoded.min_y, chunk.min_y);
|
||||||
|
assertEquals(to_flat(decoded.sections), to_flat(chunk.sections));
|
||||||
assertEquals(decoded.biomes, chunk.biomes);
|
assertEquals(decoded.biomes, chunk.biomes);
|
||||||
}
|
}
|
||||||
// a byte a block when that's enough
|
// a section that's all one block takes a few bytes
|
||||||
assert(encode_chunk(chunk_data(7, 256)).length < encode_chunk(chunk_data(7, 257)).length - VOLUME / 2);
|
const uniform = chunk_data(0, 1);
|
||||||
|
assert(encode_chunk(uniform).length < 16 * 8 + 1024);
|
||||||
|
|
||||||
// format 1 chunks still load, without biomes
|
// a taller dimension's, from below 0
|
||||||
const old = decode_chunk(format_1(encode_chunk(chunk_data(3, 300))), VOLUME);
|
const tall = chunk_data(5, 40, new WorldShape({ min_y: -64, max_y: 320, sky_light: true }));
|
||||||
assertEquals(old.blocks, chunk_data(3, 300).blocks);
|
const decoded = decode_chunk(encode_chunk(tall));
|
||||||
assertEquals(old.biomes, undefined);
|
assertEquals([decoded.min_y, decoded.sections.length], [-64, 24]);
|
||||||
|
assertEquals(to_flat(decoded.sections), to_flat(tall.sections));
|
||||||
|
|
||||||
|
// formats 1 and 2 still load, 1 without biomes
|
||||||
|
for (const format of [1, 2] as const) {
|
||||||
|
const chunk = chunk_data(3, 300);
|
||||||
|
const old = decode_chunk(old_format(format, chunk));
|
||||||
|
assertEquals([old.min_y, old.palette], [0, chunk.palette]);
|
||||||
|
assertEquals(to_flat(old.sections), to_flat(chunk.sections));
|
||||||
|
assertEquals(old.biomes, format === 1 ? undefined : chunk.biomes);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("region files keep chunks, including negative ones, and saving one again doesn't touch the others", async () => {
|
Deno.test("region files keep chunks, including negative ones, and saving one again doesn't touch the others", async () => {
|
||||||
@@ -65,7 +95,7 @@ Deno.test("region files keep chunks, including negative ones, and saving one aga
|
|||||||
await Promise.all(
|
await Promise.all(
|
||||||
positions.map(async (
|
positions.map(async (
|
||||||
[x, z],
|
[x, z],
|
||||||
) => [`${x},${z}`, decode_chunk((await read_chunk(dir, x, z))!, VOLUME).blocks[5]]),
|
) => [`${x},${z}`, decode_chunk((await read_chunk(dir, x, z))!).sections[0].get(5)]),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -103,13 +133,13 @@ Deno.test("every chunk generated is saved whole, and saved again only when it ch
|
|||||||
for (const chunk of ["0,0", "-2,2", "5,5"]) assert(positions.includes(chunk), `${chunk} is saved`);
|
for (const chunk of ["0,0", "-2,2", "5,5"]) assert(positions.includes(chunk), `${chunk} is saved`);
|
||||||
|
|
||||||
// every block, and each column's biome
|
// every block, and each column's biome
|
||||||
const chunk = decode_chunk(saved.chunks.find((c) => c.x === 0 && c.z === 0)!.data, VOLUME);
|
const chunk = decode_chunk(saved.chunks.find((c) => c.x === 0 && c.z === 0)!.data);
|
||||||
assertEquals(chunk.palette[chunk.blocks[index(3, 120, 5)]], ["bworld:stone", 0]);
|
assertEquals(saved_block(chunk, 3, 120, 5), ["bworld:stone", 0]);
|
||||||
assert(chunk.palette[chunk.blocks[index(3, 10, 5)]][0] !== AIR_ID);
|
assert(saved_block(chunk, 3, 10, 5)[0] !== AIR_ID);
|
||||||
assertEquals(chunk.biomes![index(3, 0, 5)], game.world.biome_at(3, 5));
|
assertEquals(chunk.biomes![5 * 16 + 3], game.world.biome_at(3, 5));
|
||||||
const untouched = decode_chunk(saved.chunks.find((c) => c.x === 5 && c.z === 5)!.data, VOLUME);
|
const untouched = decode_chunk(saved.chunks.find((c) => c.x === 5 && c.z === 5)!.data);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
Uint32Array.from(untouched.blocks, (block) => {
|
Uint32Array.from(to_flat(untouched.sections), (block) => {
|
||||||
const [id, state] = untouched.palette[block];
|
const [id, state] = untouched.palette[block];
|
||||||
return id === AIR_ID ? 0 : game.world.block_ids[id] | (state << 16);
|
return id === AIR_ID ? 0 : game.world.block_ids[id] | (state << 16);
|
||||||
}),
|
}),
|
||||||
@@ -141,9 +171,9 @@ async function saved_with_old_terrain() {
|
|||||||
game.set_block(3, 120, 5, "bworld:stone");
|
game.set_block(3, 120, 5, "bworld:stone");
|
||||||
const saved = game.save();
|
const saved = game.save();
|
||||||
const at_0_0 = saved.chunks.find((c) => c.x === 0 && c.z === 0)!;
|
const at_0_0 = saved.chunks.find((c) => c.x === 0 && c.z === 0)!;
|
||||||
const chunk = decode_chunk(at_0_0.data, VOLUME);
|
const chunk = decode_chunk(at_0_0.data);
|
||||||
assertEquals(chunk.palette[chunk.blocks[index(3, 121, 5)]][0], AIR_ID);
|
assertEquals(saved_block(chunk, 3, 121, 5)[0], AIR_ID);
|
||||||
chunk.blocks[index(3, 121, 5)] = chunk.blocks[index(3, 120, 5)];
|
set_saved_block(chunk, 3, 121, 5, chunk.palette.indexOf(saved_block(chunk, 3, 120, 5)));
|
||||||
at_0_0.data = encode_chunk(chunk as Required<typeof chunk>);
|
at_0_0.data = encode_chunk(chunk as Required<typeof chunk>);
|
||||||
return { saved, at_0_0 };
|
return { saved, at_0_0 };
|
||||||
}
|
}
|
||||||
@@ -158,33 +188,33 @@ Deno.test("saved chunks stay as they were saved, whatever generation makes there
|
|||||||
|
|
||||||
Deno.test("chunks saved before they had biomes get them from generation, and are saved again with them", async () => {
|
Deno.test("chunks saved before they had biomes get them from generation, and are saved again with them", async () => {
|
||||||
const { saved, at_0_0 } = await saved_with_old_terrain();
|
const { saved, at_0_0 } = await saved_with_old_terrain();
|
||||||
const biomes = decode_chunk(at_0_0.data, VOLUME).biomes!;
|
const biomes = decode_chunk(at_0_0.data).biomes!;
|
||||||
at_0_0.data = format_1(at_0_0.data);
|
at_0_0.data = old_format(1, decode_chunk(at_0_0.data));
|
||||||
const { game } = await test_game("mods", saved);
|
const { game } = await test_game("mods", saved);
|
||||||
assertEquals(game.world.get_block_id(3, 121, 5), "bworld:stone");
|
assertEquals(game.world.get_block_id(3, 121, 5), "bworld:stone");
|
||||||
assertEquals(game.world.biome_at(3, 5), biomes[index(3, 0, 5)]);
|
assertEquals(game.world.biome_at(3, 5), biomes[5 * 16 + 3]);
|
||||||
const again = game.save().chunks.find((c) => c.x === 0 && c.z === 0)!;
|
const again = game.save().chunks.find((c) => c.x === 0 && c.z === 0)!;
|
||||||
assertEquals(decode_chunk(again.data, VOLUME).biomes, biomes);
|
assertEquals(decode_chunk(again.data).biomes, biomes);
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("blocks of a mod that's gone are kept in the save until something replaces them", async () => {
|
Deno.test("blocks of a mod that's gone are kept in the save until something replaces them", async () => {
|
||||||
const { saved, at_0_0 } = await saved_with_old_terrain();
|
const { saved, at_0_0 } = await saved_with_old_terrain();
|
||||||
const chunk = decode_chunk(at_0_0.data, VOLUME);
|
const chunk = decode_chunk(at_0_0.data);
|
||||||
chunk.palette[chunk.blocks[index(3, 120, 5)]] = ["gone:thing", 2];
|
chunk.palette[chunk.palette.indexOf(saved_block(chunk, 3, 120, 5))] = ["gone:thing", 2];
|
||||||
at_0_0.data = encode_chunk(chunk as Required<typeof chunk>);
|
at_0_0.data = encode_chunk(chunk as Required<typeof chunk>);
|
||||||
|
|
||||||
const { game } = await test_game("mods", saved);
|
const { game } = await test_game("mods", saved);
|
||||||
// air stands in for it
|
// air stands in for it
|
||||||
assertEquals(game.world.get_block_id(3, 120, 5), AIR_ID);
|
assertEquals(game.world.get_block_id(3, 120, 5), AIR_ID);
|
||||||
game.set_block(0, 120, 0, "bworld:stone");
|
game.set_block(0, 120, 0, "bworld:stone");
|
||||||
const again = decode_chunk(game.save().chunks.find((c) => c.x === 0 && c.z === 0)!.data, VOLUME);
|
const again = decode_chunk(game.save().chunks.find((c) => c.x === 0 && c.z === 0)!.data);
|
||||||
assertEquals(again.palette[again.blocks[index(3, 120, 5)]], ["gone:thing", 2]);
|
assertEquals(saved_block(again, 3, 120, 5), ["gone:thing", 2]);
|
||||||
assertEquals(again.palette[again.blocks[index(3, 121, 5)]], ["gone:thing", 2]);
|
assertEquals(saved_block(again, 3, 121, 5), ["gone:thing", 2]);
|
||||||
|
|
||||||
game.set_block(3, 120, 5, "bworld:oak_planks");
|
game.set_block(3, 120, 5, "bworld:oak_planks");
|
||||||
const replaced = decode_chunk(game.save().chunks.find((c) => c.x === 0 && c.z === 0)!.data, VOLUME);
|
const replaced = decode_chunk(game.save().chunks.find((c) => c.x === 0 && c.z === 0)!.data);
|
||||||
assertEquals(replaced.palette[replaced.blocks[index(3, 120, 5)]], ["bworld:oak_planks", 0]);
|
assertEquals(saved_block(replaced, 3, 120, 5), ["bworld:oak_planks", 0]);
|
||||||
assertEquals(replaced.palette[replaced.blocks[index(3, 121, 5)]], ["gone:thing", 2]);
|
assertEquals(saved_block(replaced, 3, 121, 5), ["gone:thing", 2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("chunks forgotten before they're saved are still saved, and load from the save after", async () => {
|
Deno.test("chunks forgotten before they're saved are still saved, and load from the save after", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user