Wooooorld files
This commit is contained in:
+35
-22
@@ -2,8 +2,8 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { AIR, CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
|
||||
import type { WorldShape } from "@/common/world_shape.ts";
|
||||
import { ChunkColumn, column_heights, SECTION_VOLUME } from "@/common/section.ts";
|
||||
import { SECTION_HEIGHT, type WorldShape } from "@/common/world_shape.ts";
|
||||
import { ChunkColumn, column_heights, Section, SECTION_VOLUME } from "@/common/section.ts";
|
||||
import {
|
||||
block_light_emission,
|
||||
block_light_opacity,
|
||||
@@ -501,13 +501,9 @@ export class ServerWorld {
|
||||
};
|
||||
|
||||
const { sections, biomes } = chunk.column;
|
||||
const blocks = new Uint16Array(this.shape.volume);
|
||||
sections.forEach((section, s) => {
|
||||
const start = s * SECTION_VOLUME;
|
||||
if (section.blocks === null) {
|
||||
blocks.fill(value_index(section.value), start, start + SECTION_VOLUME);
|
||||
return;
|
||||
}
|
||||
const saved = sections.map((section) => {
|
||||
if (section.blocks === null) return new Section(value_index(section.value));
|
||||
const indexes = new Uint32Array(SECTION_VOLUME);
|
||||
// runs of the same block are common, like air above the ground and stone below
|
||||
let last_value = -1;
|
||||
let last_index = 0;
|
||||
@@ -517,11 +513,14 @@ export class ServerWorld {
|
||||
last_value = 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);
|
||||
return { palette, blocks, biomes };
|
||||
for (const [i, [id, state]] of chunk.unknown ?? []) {
|
||||
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) {
|
||||
@@ -671,7 +670,7 @@ export class ServerWorld {
|
||||
const data = evicted?.data ?? this.#saved_chunks?.read(chunk_x, chunk_z);
|
||||
try {
|
||||
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) {
|
||||
console.error(
|
||||
`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];
|
||||
return nid === undefined ? undefined : block_value(nid, state);
|
||||
});
|
||||
const blocks = new Uint32Array(this.shape.volume);
|
||||
let unknown: Map<number, [string, number]> | undefined;
|
||||
for (let i = 0; i < blocks.length; i++) {
|
||||
const value = values[saved.blocks[i]];
|
||||
if (value === undefined) {
|
||||
(unknown ??= new Map()).set(i, saved.palette[saved.blocks[i]]);
|
||||
} else {
|
||||
blocks[i] = value;
|
||||
// sections the save doesn't have are air, and ones the dimension doesn't have are left out
|
||||
const offset = (this.shape.min_y - saved.min_y) / SECTION_HEIGHT;
|
||||
const sections = Array.from({ length: this.shape.height / SECTION_HEIGHT }, (_, s) => {
|
||||
const section = saved.sections[offset + s];
|
||||
if (!section) return new Section(AIR);
|
||||
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;
|
||||
if (!biomes) {
|
||||
// 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];
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user