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) {
|
||||
|
||||
+11
-6
@@ -6,6 +6,7 @@
|
||||
import { Alea } from "@paulaboks/rng";
|
||||
import { AIR, CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
|
||||
import type { WorldShape } from "@/common/world_shape.ts";
|
||||
import { ChunkColumn } from "@/common/section.ts";
|
||||
import type { FeatureChunk, Position } from "@/common/mod_api/worldgen.ts";
|
||||
import type { OreJson } from "@/common/mod_data.ts";
|
||||
import { generate_noise_chunk } from "./worldgen/noise_chunk.ts";
|
||||
@@ -37,17 +38,20 @@ export interface SetupFeature {
|
||||
}
|
||||
|
||||
export interface RawChunk {
|
||||
// numeric block ids, only what this chunk generated itself, indexed (y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x
|
||||
blocks: Uint32Array;
|
||||
// only what this chunk generated itself, with each column's biome
|
||||
column: ChunkColumn;
|
||||
// blocks it generated in other chunks (from features like a mod's trees), flattened as x, y, z, numeric id
|
||||
spills: Int32Array;
|
||||
// each column's biome id, x then z
|
||||
biomes: string[];
|
||||
}
|
||||
|
||||
// features that threw, so each is only reported once
|
||||
const failed_features = new Set<string>();
|
||||
|
||||
// what a chunk is generated into before it's made sections, indexed (y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x. a
|
||||
// column at a time is how terrain is worked out, and features set blocks one at a time. kept for the next chunk, so
|
||||
// generating isn't reentrant
|
||||
let scratch = new Uint32Array(0);
|
||||
|
||||
// generates one chunk on its own. neighbors' spills get merged in by whoever assembles the world:
|
||||
// a chunk's own blocks always win and spills only fill air, so the result doesn't depend on load order
|
||||
export function generate_raw_chunk(
|
||||
@@ -60,7 +64,8 @@ export function generate_raw_chunk(
|
||||
default_values?: number[],
|
||||
): RawChunk {
|
||||
const shape = worldgen.shape;
|
||||
const blocks = new Uint32Array(shape.volume);
|
||||
if (scratch.length !== shape.volume) scratch = new Uint32Array(shape.volume);
|
||||
const blocks = scratch.fill(AIR);
|
||||
const spills: number[] = [];
|
||||
const heights = new Int32Array(CHUNK_AREA);
|
||||
const biomes: string[] = new Array(CHUNK_AREA);
|
||||
@@ -92,7 +97,7 @@ export function generate_raw_chunk(
|
||||
}
|
||||
generate_features(blocks, heights, biomes, set, chunk_x, chunk_z, seed, block_ids, worldgen);
|
||||
|
||||
return { blocks, spills: new Int32Array(spills), biomes };
|
||||
return { column: ChunkColumn.from_flat(chunk_x, chunk_z, shape, blocks, biomes), spills: new Int32Array(spills) };
|
||||
}
|
||||
|
||||
// each block an ore replaces becomes the first ore whose noise is above its threshold
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// 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
|
||||
import { AIR, CHUNK_AREA, ID_MASK } from "./constants.ts";
|
||||
import { SECTION_HEIGHT, type WorldShape } from "./world_shape.ts";
|
||||
|
||||
export const SECTION_VOLUME = CHUNK_AREA * SECTION_HEIGHT;
|
||||
|
||||
// a block's place in its section, y then z then x like a chunk's layers. world coordinates work as they are, since
|
||||
// sections start at multiples of 16
|
||||
export function section_index(x: number, y: number, z: number) {
|
||||
return ((y & 15) << 8) | ((z & 15) << 4) | (x & 15);
|
||||
}
|
||||
|
||||
// what a section is made of, a block value or every block's, what's sent to and from workers
|
||||
export type SectionData = number | Uint32Array;
|
||||
|
||||
export class Section {
|
||||
// the block, while blocks is null
|
||||
value: number;
|
||||
// every block's value, once they differ
|
||||
blocks: Uint32Array | null;
|
||||
|
||||
constructor(data: SectionData) {
|
||||
this.value = typeof data === "number" ? data : 0;
|
||||
this.blocks = typeof data === "number" ? null : data;
|
||||
}
|
||||
|
||||
// a copy of values, one value when they're all the same
|
||||
static of(values: Uint32Array): Section {
|
||||
const first = values[0];
|
||||
for (let i = 1; i < values.length; i++) {
|
||||
if (values[i] !== first) return new Section(values.slice());
|
||||
}
|
||||
return new Section(first);
|
||||
}
|
||||
|
||||
get data(): SectionData {
|
||||
return this.blocks ?? this.value;
|
||||
}
|
||||
|
||||
get(index: number): number {
|
||||
return this.blocks === null ? this.value : this.blocks[index];
|
||||
}
|
||||
|
||||
set(index: number, value: number) {
|
||||
if (this.blocks === null) {
|
||||
if (value === this.value) return;
|
||||
this.blocks = new Uint32Array(SECTION_VOLUME).fill(this.value);
|
||||
}
|
||||
this.blocks[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
// a chunk's blocks, its dimension's whole height, and each column's biome
|
||||
export class ChunkColumn {
|
||||
readonly x: number;
|
||||
readonly z: number;
|
||||
readonly shape: WorldShape;
|
||||
// from min_y up
|
||||
readonly sections: Section[];
|
||||
// x then z
|
||||
readonly biomes: string[];
|
||||
|
||||
constructor(x: number, z: number, shape: WorldShape, sections: Section[], biomes: string[]) {
|
||||
if (sections.length * SECTION_HEIGHT !== shape.height) {
|
||||
throw new Error(`Chunk ${x}, ${z} has ${sections.length} sections, its dimension ${shape.height / 16}`);
|
||||
}
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
this.shape = shape;
|
||||
this.sections = sections;
|
||||
this.biomes = biomes;
|
||||
}
|
||||
|
||||
// from every block's value in one array, y from min_y then z then x, which is copied
|
||||
static from_flat(x: number, z: number, shape: WorldShape, blocks: Uint32Array, biomes: string[]): ChunkColumn {
|
||||
const sections: Section[] = [];
|
||||
for (let start = 0; start < blocks.length; start += SECTION_VOLUME) {
|
||||
sections.push(Section.of(blocks.subarray(start, start + SECTION_VOLUME)));
|
||||
}
|
||||
return new ChunkColumn(x, z, shape, sections, biomes);
|
||||
}
|
||||
|
||||
// the block value at world coordinates, air outside the dimension's height
|
||||
get(x: number, y: number, z: number): number {
|
||||
if (!this.shape.contains(y)) return AIR;
|
||||
return this.sections[(y - this.shape.min_y) >> 4].get(section_index(x, y, z));
|
||||
}
|
||||
|
||||
// does nothing outside the dimension's height
|
||||
set(x: number, y: number, z: number, value: number) {
|
||||
if (!this.shape.contains(y)) return;
|
||||
this.sections[(y - this.shape.min_y) >> 4].set(section_index(x, y, z), value);
|
||||
}
|
||||
|
||||
clone(): ChunkColumn {
|
||||
const sections = this.sections.map((section) => new Section(section.blocks?.slice() ?? section.value));
|
||||
return new ChunkColumn(this.x, this.z, this.shape, sections, [...this.biomes]);
|
||||
}
|
||||
|
||||
// every block's value in one array, like from_flat's
|
||||
to_flat(): Uint32Array {
|
||||
const blocks = new Uint32Array(this.shape.volume);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
// numeric id. sections that are all air, or all clear, are passed over whole
|
||||
export function column_heights(column: ChunkColumn, opacity: Uint8Array): Int16Array {
|
||||
const heights = new Int16Array(CHUNK_AREA * 2);
|
||||
const { sections, shape } = column;
|
||||
for (let c = 0; c < CHUNK_AREA; c++) {
|
||||
// from the top, by y above min_y
|
||||
let y = shape.height - 1;
|
||||
while (y >= 0) {
|
||||
const section = sections[y >> 4];
|
||||
if (section.blocks === null && (section.value & ID_MASK) === AIR) {
|
||||
y = (y & ~15) - 1;
|
||||
continue;
|
||||
}
|
||||
if ((section.get(((y & 15) << 8) | c) & ID_MASK) !== AIR) break;
|
||||
y--;
|
||||
}
|
||||
heights[c] = shape.min_y + y;
|
||||
while (y >= 0) {
|
||||
const section = sections[y >> 4];
|
||||
if (section.blocks === null && opacity[section.value & ID_MASK] === 0) {
|
||||
y = (y & ~15) - 1;
|
||||
continue;
|
||||
}
|
||||
if (opacity[section.get(((y & 15) << 8) | c) & ID_MASK] !== 0) break;
|
||||
y--;
|
||||
}
|
||||
heights[CHUNK_AREA + c] = shape.min_y + y + 1;
|
||||
}
|
||||
return heights;
|
||||
}
|
||||
+76
-77
@@ -3,6 +3,7 @@
|
||||
|
||||
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 {
|
||||
block_light_emission,
|
||||
block_light_opacity,
|
||||
@@ -19,7 +20,8 @@ import { farming_day } from "@/common/time.ts";
|
||||
import { encode_level_chunk } from "@/common/chunk_streaming.ts";
|
||||
import { ChunkBlocks, decode_chunk, encode_chunk, SavedChunk, SavedChunks } from "./saves.ts";
|
||||
|
||||
// generation only, rebuilt when needed. 256 KB each in a 256 tall dimension. with a worldgen pool, it holds what the pool made until the
|
||||
// generation only, rebuilt when needed. 16 KB for each section that isn't all one block, about 7 of a 256 tall
|
||||
// chunk's 16. with a worldgen pool, it holds what the pool made until the
|
||||
// chunks are built, a few dozen chunks ahead of what players are sent
|
||||
const RAW_CACHE_SIZE = 256;
|
||||
const CHUNK_CACHE_SIZE = 256;
|
||||
@@ -89,9 +91,9 @@ function tile_chunk_key(tile: Tile) {
|
||||
|
||||
// a chunk as it is now, loaded from the save or generated
|
||||
interface LoadedChunk {
|
||||
blocks: Uint32Array;
|
||||
biomes: string[];
|
||||
// saved blocks of mods that are gone, by index in the chunk. air in blocks, and saved again until replaced
|
||||
column: ChunkColumn;
|
||||
// saved blocks of mods that are gone, by index in the chunk (see #index). air in column, and saved again until
|
||||
// replaced
|
||||
unknown?: Map<number, [string, number]>;
|
||||
}
|
||||
|
||||
@@ -213,8 +215,7 @@ export class ServerWorld {
|
||||
}
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
const blocks = this.#get_chunk(chunk_x, chunk_z);
|
||||
return blocks[this.#index(x, y, z, chunk_x, chunk_z)] & ID_MASK;
|
||||
return this.#get_chunk(chunk_x, chunk_z).get(x, y, z) & ID_MASK;
|
||||
}
|
||||
|
||||
// the id with its state bits
|
||||
@@ -224,7 +225,7 @@ export class ServerWorld {
|
||||
}
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
return this.#get_chunk(chunk_x, chunk_z)[this.#index(x, y, z, chunk_x, chunk_z)];
|
||||
return this.#get_chunk(chunk_x, chunk_z).get(x, y, z);
|
||||
}
|
||||
|
||||
// whether entities bump into the block there, water and other blocks with "collision": false don't
|
||||
@@ -262,9 +263,9 @@ export class ServerWorld {
|
||||
#chunk_biomes(chunk_x: number, chunk_z: number) {
|
||||
const key = chunk_key(chunk_x, chunk_z);
|
||||
const chunk = this.#chunks.get(key);
|
||||
if (chunk) return chunk.biomes;
|
||||
if (this.#is_saved(key)) return this.#get_loaded(chunk_x, chunk_z).biomes;
|
||||
return this.#biomes.get(key) ?? this.#get_raw(chunk_x, chunk_z).biomes;
|
||||
if (chunk) return chunk.column.biomes;
|
||||
if (this.#is_saved(key)) return this.#get_loaded(chunk_x, chunk_z).column.biomes;
|
||||
return this.#biomes.get(key) ?? this.#get_raw(chunk_x, chunk_z).column.biomes;
|
||||
}
|
||||
|
||||
// builds the chunk now if it isn't, generating what it needs on this thread
|
||||
@@ -290,27 +291,28 @@ export class ServerWorld {
|
||||
}
|
||||
|
||||
// where in a kept chunk (see has_chunk) the blocks are whose numeric id is set in wanted, as x, y, z. nothing when
|
||||
// the chunk isn't kept
|
||||
// the chunk isn't kept. sections that are all a block that isn't wanted are passed over
|
||||
find_blocks(chunk_x: number, chunk_z: number, wanted: Uint8Array): [number, number, number][] {
|
||||
const blocks = this.#chunks.get(chunk_key(chunk_x, chunk_z))?.blocks;
|
||||
if (!blocks) return [];
|
||||
const column = this.#chunks.get(chunk_key(chunk_x, chunk_z))?.column;
|
||||
if (!column) return [];
|
||||
const found: [number, number, number][] = [];
|
||||
for (let i = 0; i < blocks.length; i++) {
|
||||
if (!wanted[blocks[i] & ID_MASK]) continue;
|
||||
const y = this.shape.min_y + Math.floor(i / CHUNK_AREA);
|
||||
const rest = i % CHUNK_AREA;
|
||||
found.push([
|
||||
chunk_x * CHUNK_SIZE + rest % CHUNK_SIZE,
|
||||
y,
|
||||
chunk_z * CHUNK_SIZE + Math.floor(rest / CHUNK_SIZE),
|
||||
]);
|
||||
}
|
||||
column.sections.forEach((section, s) => {
|
||||
if (section.blocks === null && !wanted[section.value & ID_MASK]) return;
|
||||
for (let i = 0; i < SECTION_VOLUME; i++) {
|
||||
if (!wanted[section.get(i) & ID_MASK]) continue;
|
||||
found.push([
|
||||
chunk_x * CHUNK_SIZE + (i & 15),
|
||||
this.shape.min_y + s * 16 + (i >> 8),
|
||||
chunk_z * CHUNK_SIZE + ((i >> 4) & 15),
|
||||
]);
|
||||
}
|
||||
});
|
||||
return found;
|
||||
}
|
||||
|
||||
// what generation makes in a chunk, neighbors' spills included, without what players changed. built again every
|
||||
// call, for comparing generation (tests/worldgen_golden_test.ts)
|
||||
generated_blocks(chunk_x: number, chunk_z: number): Uint32Array {
|
||||
generated_blocks(chunk_x: number, chunk_z: number): ChunkColumn {
|
||||
return this.#generate_chunk(chunk_x, chunk_z);
|
||||
}
|
||||
|
||||
@@ -350,7 +352,7 @@ export class ServerWorld {
|
||||
// the box around it, 3x3 chunks at most, fetched once each. blocks are in it by their offset from the start
|
||||
const first_x = Math.floor((x - LIGHT_REACH) / CHUNK_SIZE);
|
||||
const first_z = Math.floor((z - LIGHT_REACH) / CHUNK_SIZE);
|
||||
const chunks: (Uint32Array | undefined)[] = [];
|
||||
const chunks: (ChunkColumn | undefined)[] = [];
|
||||
const skies: (Int16Array | undefined)[] = [];
|
||||
const box_index = (dx: number, dy: number, dz: number) =>
|
||||
((dy + LIGHT_REACH) * LIGHT_BOX + dz + LIGHT_REACH) * LIGHT_BOX + dx + LIGHT_REACH;
|
||||
@@ -382,7 +384,7 @@ export class ServerWorld {
|
||||
const which = (chunk_z - first_z) * 3 + chunk_x - first_x;
|
||||
const blocks = chunks[which] ??= this.#get_chunk(chunk_x, chunk_z);
|
||||
const column = (bz - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + bx - chunk_x * CHUNK_SIZE;
|
||||
const nid = blocks[(by - min_y) * CHUNK_AREA + column] & ID_MASK;
|
||||
const nid = blocks.get(bx, by, bz) & ID_MASK;
|
||||
if (sky_light) {
|
||||
const columns = skies[which] ??= this.#columns_of(bx, bz);
|
||||
if (by >= columns[CHUNK_AREA + column]) sky = Math.max(sky, LIGHT_REACH - lost);
|
||||
@@ -408,17 +410,7 @@ export class ServerWorld {
|
||||
const key = chunk_key(chunk_x, chunk_z);
|
||||
let columns = this.#columns.get(key);
|
||||
if (!columns) {
|
||||
const blocks = this.#get_chunk(chunk_x, chunk_z);
|
||||
const { min_y, height } = this.shape;
|
||||
columns = new Int16Array(CHUNK_AREA * 2);
|
||||
for (let column = 0; column < CHUNK_AREA; column++) {
|
||||
// from the top, by y above min_y
|
||||
let y = height - 1;
|
||||
while (y >= 0 && (blocks[y * CHUNK_AREA + column] & ID_MASK) === AIR) y--;
|
||||
columns[column] = min_y + y;
|
||||
while (y >= 0 && this.#opacity[blocks[y * CHUNK_AREA + column] & ID_MASK] === 0) y--;
|
||||
columns[CHUNK_AREA + column] = min_y + y + 1;
|
||||
}
|
||||
columns = column_heights(this.#get_chunk(chunk_x, chunk_z), this.#opacity);
|
||||
this.#columns.set(key, columns);
|
||||
}
|
||||
return columns;
|
||||
@@ -438,9 +430,8 @@ export class ServerWorld {
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
const chunk = this.#get_loaded(chunk_x, chunk_z);
|
||||
const index = this.#index(x, y, z, chunk_x, chunk_z);
|
||||
chunk.blocks[index] = value;
|
||||
chunk.unknown?.delete(index);
|
||||
chunk.column.set(x, y, z, value);
|
||||
chunk.unknown?.delete(this.#index(x, y, z, chunk_x, chunk_z));
|
||||
|
||||
this.#columns.delete(chunk_key(chunk_x, chunk_z));
|
||||
this.#mark_unsaved(chunk_x, chunk_z);
|
||||
@@ -451,11 +442,7 @@ export class ServerWorld {
|
||||
|
||||
// what clients are sent, the whole chunk as it is now. block values use this world's numeric ids, see block_table
|
||||
level_chunk(chunk_x: number, chunk_z: number): Uint8Array<ArrayBuffer> {
|
||||
return encode_level_chunk({
|
||||
x: chunk_x,
|
||||
z: chunk_z,
|
||||
...this.#get_loaded(chunk_x, chunk_z),
|
||||
});
|
||||
return encode_level_chunk(this.#get_loaded(chunk_x, chunk_z).column);
|
||||
}
|
||||
|
||||
// the block id of each numeric id, for clients to read level_chunk with
|
||||
@@ -499,30 +486,42 @@ export class ServerWorld {
|
||||
return index;
|
||||
};
|
||||
|
||||
const blocks = new Uint16Array(chunk.blocks.length);
|
||||
const by_value = new Map<number, number>();
|
||||
// runs of the same block are common, air above the ground and stone below
|
||||
let last_value = -1;
|
||||
let last_index = 0;
|
||||
for (let i = 0; i < chunk.blocks.length; i++) {
|
||||
const value = chunk.blocks[i];
|
||||
if (value !== last_value) {
|
||||
let index = by_value.get(value);
|
||||
if (index === undefined) {
|
||||
const nid = value & ID_MASK;
|
||||
const id = nid === AIR
|
||||
? AIR_ID
|
||||
: EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid)?.id ?? AIR_ID;
|
||||
index = index_of(id, value >>> 16);
|
||||
by_value.set(value, index);
|
||||
}
|
||||
last_value = value;
|
||||
last_index = index;
|
||||
const value_index = (value: number) => {
|
||||
let index = by_value.get(value);
|
||||
if (index === undefined) {
|
||||
const nid = value & ID_MASK;
|
||||
const id = nid === AIR
|
||||
? AIR_ID
|
||||
: EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid)?.id ?? AIR_ID;
|
||||
index = index_of(id, value >>> 16);
|
||||
by_value.set(value, index);
|
||||
}
|
||||
blocks[i] = last_index;
|
||||
}
|
||||
return index;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
// runs of the same block are common, like air above the ground and stone below
|
||||
let last_value = -1;
|
||||
let last_index = 0;
|
||||
for (let i = 0; i < SECTION_VOLUME; i++) {
|
||||
const value = section.blocks[i];
|
||||
if (value !== last_value) {
|
||||
last_value = value;
|
||||
last_index = value_index(value);
|
||||
}
|
||||
blocks[start + i] = last_index;
|
||||
}
|
||||
});
|
||||
for (const [i, [id, state]] of chunk.unknown ?? []) blocks[i] = index_of(id, state);
|
||||
return { palette, blocks, biomes: chunk.biomes };
|
||||
return { palette, blocks, biomes };
|
||||
}
|
||||
|
||||
get_tile(x: number, y: number, z: number) {
|
||||
@@ -612,7 +611,7 @@ export class ServerWorld {
|
||||
|
||||
#keep_raw(key: number, raw: RawChunk) {
|
||||
this.#raw.set(key, raw);
|
||||
this.#biomes.set(key, raw.biomes);
|
||||
this.#biomes.set(key, raw.column.biomes);
|
||||
this.#spills.set(key, raw.spills);
|
||||
}
|
||||
|
||||
@@ -650,7 +649,7 @@ export class ServerWorld {
|
||||
}
|
||||
|
||||
#get_chunk(chunk_x: number, chunk_z: number) {
|
||||
return this.#get_loaded(chunk_x, chunk_z).blocks;
|
||||
return this.#get_loaded(chunk_x, chunk_z).column;
|
||||
}
|
||||
|
||||
#get_loaded(chunk_x: number, chunk_z: number) {
|
||||
@@ -679,9 +678,9 @@ export class ServerWorld {
|
||||
);
|
||||
}
|
||||
}
|
||||
const blocks = this.#generate_chunk(chunk_x, chunk_z);
|
||||
const column = this.#generate_chunk(chunk_x, chunk_z);
|
||||
this.#mark_unsaved(chunk_x, chunk_z);
|
||||
return { blocks, biomes: this.#biomes.get(key) ?? this.#get_raw(chunk_x, chunk_z).biomes };
|
||||
return { column };
|
||||
}
|
||||
|
||||
#from_saved(chunk_x: number, chunk_z: number, saved: ChunkBlocks): LoadedChunk {
|
||||
@@ -703,10 +702,11 @@ export class ServerWorld {
|
||||
let biomes = saved.biomes;
|
||||
if (!biomes) {
|
||||
// saved before chunks had them. saved again with them
|
||||
biomes = this.#biomes.get(chunk_key(chunk_x, chunk_z)) ?? this.#get_raw(chunk_x, chunk_z).biomes;
|
||||
const key = chunk_key(chunk_x, chunk_z);
|
||||
biomes = [...this.#biomes.get(key) ?? this.#get_raw(chunk_x, chunk_z).column.biomes];
|
||||
this.#mark_unsaved(chunk_x, chunk_z);
|
||||
}
|
||||
return { blocks, biomes, unknown };
|
||||
return { column: ChunkColumn.from_flat(chunk_x, chunk_z, this.shape, blocks, biomes), unknown };
|
||||
}
|
||||
|
||||
// an unsaved chunk is encoded to be saved, instead of being generated again or kept whole
|
||||
@@ -725,7 +725,7 @@ export class ServerWorld {
|
||||
|
||||
// own blocks, then neighbors' leaves only into air
|
||||
#generate_chunk(chunk_x: number, chunk_z: number) {
|
||||
const blocks = this.#get_raw(chunk_x, chunk_z).blocks.slice();
|
||||
const column = this.#get_raw(chunk_x, chunk_z).column.clone();
|
||||
|
||||
for (let dx = -1; dx <= 1; dx++) {
|
||||
for (let dz = -1; dz <= 1; dz++) {
|
||||
@@ -738,14 +738,13 @@ export class ServerWorld {
|
||||
if (Math.floor(x / CHUNK_SIZE) !== chunk_x || Math.floor(z / CHUNK_SIZE) !== chunk_z) {
|
||||
continue;
|
||||
}
|
||||
const index = this.#index(x, y, z, chunk_x, chunk_z);
|
||||
if (blocks[index] === AIR) {
|
||||
blocks[index] = nid;
|
||||
if (column.get(x, y, z) === AIR) {
|
||||
column.set(x, y, z, nid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return blocks;
|
||||
return column;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
// server/worldgen_workers.ts). requests go nearest first, a worker has a couple at a time so priorities stay fresh, and what the
|
||||
// game stops wanting is dropped before it starts
|
||||
import type { RawChunk } from "@/common/generation.ts";
|
||||
import { ChunkColumn, Section, type SectionData } from "@/common/section.ts";
|
||||
import { WorldShape } from "@/common/world_shape.ts";
|
||||
import type { WorldgenBundle } from "@/common/worldgen_bundle.ts";
|
||||
import { chunk_key } from "@/common/utils.ts";
|
||||
|
||||
@@ -16,7 +18,8 @@ export type PoolToWorker =
|
||||
export type WorkerToPool =
|
||||
| { type: "ready" }
|
||||
| { type: "failed"; error: string }
|
||||
| { type: "chunk"; x: number; z: number; blocks: Uint32Array; spills: Int32Array; biomes: string[] }
|
||||
// a raw chunk's sections as their data, since what's sent between workers loses its class
|
||||
| { type: "chunk"; x: number; z: number; sections: SectionData[]; spills: Int32Array; biomes: string[] }
|
||||
| { type: "error"; x: number; z: number; error: string };
|
||||
|
||||
// the game's end of a channel to a worker
|
||||
@@ -45,6 +48,8 @@ interface Queued {
|
||||
|
||||
export class WorldgenPool {
|
||||
#bundle: WorldgenBundle;
|
||||
// what chunks it makes hold, from the bundle
|
||||
#shape: WorldShape;
|
||||
#slots: Slot[] = [];
|
||||
#queue = new Map<number, Queued>();
|
||||
// asked for by prefetch, kept when want() drops the rest
|
||||
@@ -60,6 +65,7 @@ export class WorldgenPool {
|
||||
|
||||
constructor(ports: WorkerPort[], bundle: WorldgenBundle) {
|
||||
this.#bundle = bundle;
|
||||
this.#shape = new WorldShape(bundle.shape);
|
||||
this.#ready = new Promise((resolve, reject) => this.#settle_ready = { resolve, reject });
|
||||
ports.forEach((port, index) => this.#attach(index, port));
|
||||
if (ports.length === 0) this.#settle_ready.resolve();
|
||||
@@ -160,10 +166,10 @@ export class WorldgenPool {
|
||||
const key = chunk_key(message.x, message.z);
|
||||
slot.in_flight.delete(key);
|
||||
this.generated++;
|
||||
const sections = message.sections.map((data) => new Section(data));
|
||||
this.on_chunk(message.x, message.z, {
|
||||
blocks: message.blocks,
|
||||
column: new ChunkColumn(message.x, message.z, this.#shape, sections, message.biomes),
|
||||
spills: message.spills,
|
||||
biomes: message.biomes,
|
||||
});
|
||||
this.#done(key);
|
||||
break;
|
||||
|
||||
@@ -33,8 +33,10 @@ self.onmessage = (event: MessageEvent<{ port: MessagePort }>) => {
|
||||
const { x, z } = message;
|
||||
try {
|
||||
const raw = (await loaded!).generate(x, z);
|
||||
send({ type: "chunk", x, z, blocks: raw.blocks, spills: raw.spills, biomes: raw.biomes }, [
|
||||
raw.blocks.buffer,
|
||||
const sections = raw.column.sections.map((section) => section.data);
|
||||
const buffers = sections.flatMap((data) => typeof data === "number" ? [] : [data.buffer]);
|
||||
send({ type: "chunk", x, z, sections, spills: raw.spills, biomes: raw.column.biomes }, [
|
||||
...buffers,
|
||||
raw.spills.buffer,
|
||||
]);
|
||||
} catch (e) {
|
||||
|
||||
@@ -32,7 +32,7 @@ Deno.test({
|
||||
if (!is_within_view(cx, cz, player.chunks.view_distance, x, z)) continue;
|
||||
const chunk = chunks.get(`${x},${z}`);
|
||||
assert(chunk, `chunk ${x},${z} wasn't sent`);
|
||||
assertEquals(chunk.blocks, t.game.world.generated_blocks(x, z), `chunk ${x},${z}`);
|
||||
assertEquals(chunk.blocks, t.game.world.generated_blocks(x, z).to_flat(), `chunk ${x},${z}`);
|
||||
}
|
||||
}
|
||||
assert(t.pool!.generated - generated > chunks.size, "the pool made them, and their neighbors");
|
||||
|
||||
@@ -7,6 +7,8 @@ import { assert, assertEquals } from "@std/assert";
|
||||
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { decode_level_chunk, encode_level_chunk, is_within_view } from "@/common/chunk_streaming.ts";
|
||||
import { PROTOCOL_VERSION } from "@/common/protocol.ts";
|
||||
import { ChunkColumn } from "@/common/section.ts";
|
||||
import { WorldShape } from "@/common/world_shape.ts";
|
||||
import { sent_block, test_game } from "./helpers.ts";
|
||||
|
||||
Deno.test("a chunk comes back as it was encoded, whatever its sections hold", () => {
|
||||
@@ -21,8 +23,9 @@ Deno.test("a chunk comes back as it was encoded, whatever its sections hold", ()
|
||||
blocks[3 * section + i] = (i % 300) | ((i % 7) << 16);
|
||||
}
|
||||
const biomes = Array.from({ length: CHUNK_AREA }, (_, i) => i < 100 ? "bworld:plains" : "bworld:desert");
|
||||
const shape = new WorldShape({ min_y: -64, max_y: 320, sky_light: true });
|
||||
|
||||
const decoded = decode_level_chunk(encode_level_chunk({ x: -3, z: 70000, blocks, biomes }));
|
||||
const decoded = decode_level_chunk(encode_level_chunk(ChunkColumn.from_flat(-3, 70000, shape, blocks, biomes)));
|
||||
assertEquals(decoded.x, -3);
|
||||
assertEquals(decoded.z, 70000);
|
||||
assertEquals(decoded.blocks, blocks);
|
||||
@@ -30,7 +33,7 @@ Deno.test("a chunk comes back as it was encoded, whatever its sections hold", ()
|
||||
|
||||
// one biome, and the receiver's numbering
|
||||
const one = decode_level_chunk(
|
||||
encode_level_chunk({ x: 0, z: 0, blocks, biomes: biomes.map(() => "bworld:plains") }),
|
||||
encode_level_chunk(ChunkColumn.from_flat(0, 0, shape, blocks, biomes.map(() => "bworld:plains"))),
|
||||
(value) => value === 7 ? 70 : value,
|
||||
);
|
||||
assertEquals(one.biomes, biomes.map(() => "bworld:plains"));
|
||||
|
||||
@@ -143,6 +143,12 @@ Deno.test("a dimension's blocks go from its min_y to max_y - 1, and are sent and
|
||||
assertEquals(chunk.blocks[8], world.get_block_value(GROUND_X, -64, 0));
|
||||
assertEquals(chunk.blocks[383 * CHUNK_AREA + 8], world.get_block_value(GROUND_X, 319, 0));
|
||||
|
||||
// a column of ground: glass up to 69 and air above, all one block in every section but the one with the ground's
|
||||
// top in it
|
||||
const generated = world.generated_blocks(2, 0);
|
||||
assertEquals(generated.sections.map((section) => section.blocks !== null).indexOf(true), (64 + 64) / 16);
|
||||
assertEquals(generated.sections.filter((section) => section.blocks !== null).length, 1);
|
||||
|
||||
const loaded = await test_game(dir, game.save());
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
for (const y of [-64, 319]) assertEquals(loaded.game.world.get_block_id(GROUND_X, y, 0), "bworld:stone");
|
||||
|
||||
@@ -104,7 +104,8 @@ Deno.test("data features use types and modifiers scripts register, scripts place
|
||||
const glass = block_ids["bworld:glass"];
|
||||
const cobble = block_ids["bworld:cobblestone"];
|
||||
for (const [cx, cz] of [[0, 0], [3, -2]]) {
|
||||
const { blocks } = generate_raw_chunk(cx, cz, game.world.seed, block_ids, worldgen, default_values);
|
||||
const raw = generate_raw_chunk(cx, cz, game.world.seed, block_ids, worldgen, default_values);
|
||||
const blocks = raw.column.to_flat();
|
||||
// the pillar in the middle, which the code feature after it in its step saw
|
||||
let pillar = 0;
|
||||
for (let y = min_y; y < max_y; y++) if (at(blocks, 8, y, 8) === glass) pillar++;
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ export function raw_hash(raw: RawChunk, name: (value: number) => string): Promis
|
||||
for (let i = 0; i < raw.spills.length; i += 4) {
|
||||
spills.push(raw.spills[i], raw.spills[i + 1], raw.spills[i + 2], name(raw.spills[i + 3]));
|
||||
}
|
||||
return hash(blocks_text(raw.blocks, name), JSON.stringify(spills), raw.biomes.join(","));
|
||||
return hash(blocks_text(raw.column.to_flat(), name), JSON.stringify(spills), raw.column.biomes.join(","));
|
||||
}
|
||||
|
||||
// what the fixture's "final" is: the chunk with its neighbors' spills
|
||||
|
||||
@@ -139,7 +139,8 @@ Deno.test("the template's worldgen feature places boulders, the same way every t
|
||||
for (let cx = 0; cx < 20; cx++) {
|
||||
const plain = generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, without);
|
||||
const modded = generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, worldgen);
|
||||
const differences = plain.blocks.filter((b, i) => b !== modded.blocks[i]).length;
|
||||
const [plain_blocks, modded_blocks] = [plain.column.to_flat(), modded.column.to_flat()];
|
||||
const differences = plain_blocks.filter((b, i) => b !== modded_blocks[i]).length;
|
||||
if (differences > 0) boulders++;
|
||||
assert(differences <= 1, `chunk ${cx} changed ${differences} blocks`);
|
||||
}
|
||||
@@ -158,8 +159,8 @@ Deno.test("the template's worldgen feature places boulders, the same way every t
|
||||
});
|
||||
for (let cx = 0; cx < 20; cx++) {
|
||||
assertEquals(
|
||||
generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, again).blocks,
|
||||
generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, worldgen).blocks,
|
||||
generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, again).column.to_flat(),
|
||||
generate_raw_chunk(cx, 0, "boulder-seed", game.world.block_ids, worldgen).column.to_flat(),
|
||||
);
|
||||
}
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
@@ -186,11 +187,12 @@ Deno.test("mod ores generate into the block they replace", async () => {
|
||||
const ore = game.world.block_ids["copper_tools:example_block"];
|
||||
let found = 0;
|
||||
for (let cx = 0; cx < 4; cx++) {
|
||||
const blocks = generate_raw_chunk(cx, 0, "test-seed", game.world.block_ids, game.world.worldgen).blocks;
|
||||
const raw = generate_raw_chunk(cx, 0, "test-seed", game.world.block_ids, game.world.worldgen);
|
||||
const blocks = raw.column.to_flat();
|
||||
blocks.forEach((b, i) => {
|
||||
if (b === ore) {
|
||||
found++;
|
||||
assert(Math.floor(i / 256) <= 60, "ore above max_y");
|
||||
assert(game.world.shape.min_y + Math.floor(i / 256) <= 60, "ore above max_y");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// chunks' blocks in sections of 16x16x16, see common/section.ts
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { Alea } from "@paulaboks/rng";
|
||||
import { AIR, CHUNK_AREA } from "@/common/constants.ts";
|
||||
import { ChunkColumn, column_heights, Section, SECTION_VOLUME } from "@/common/section.ts";
|
||||
import { WorldShape } from "@/common/world_shape.ts";
|
||||
|
||||
const SHAPE = new WorldShape({ min_y: -64, max_y: 320, sky_light: true });
|
||||
const [STONE, GLASS, LEAVES] = [1, 2, 3];
|
||||
// light goes through air and glass, leaves and stone stop it
|
||||
const OPACITY = Uint8Array.of(0, 15, 0, 1);
|
||||
|
||||
function empty_column(x = 0, z = 0) {
|
||||
const sections = Array.from({ length: SHAPE.height / 16 }, () => new Section(AIR));
|
||||
return new ChunkColumn(x, z, SHAPE, sections, new Array(CHUNK_AREA).fill("test:plains"));
|
||||
}
|
||||
|
||||
Deno.test("a section is one value until a block in it differs", () => {
|
||||
const section = new Section(STONE);
|
||||
section.set(5, STONE);
|
||||
assertEquals(section.blocks, null);
|
||||
section.set(5, GLASS);
|
||||
assertEquals(section.get(5), GLASS);
|
||||
assertEquals(section.get(6), STONE);
|
||||
assertEquals(section.blocks?.length, SECTION_VOLUME);
|
||||
|
||||
assertEquals(Section.of(new Uint32Array(SECTION_VOLUME).fill(GLASS)).data, GLASS);
|
||||
});
|
||||
|
||||
Deno.test("a column's blocks are at their world coordinates, from min_y to max_y - 1", () => {
|
||||
const column = empty_column(-2, 3);
|
||||
column.set(-32, -64, 63, STONE);
|
||||
column.set(-17, 319, 48, GLASS);
|
||||
column.set(-20, 320, 50, STONE);
|
||||
assertEquals(column.get(-32, -64, 63), STONE);
|
||||
assertEquals(column.get(-17, 319, 48), GLASS);
|
||||
assertEquals(column.get(-20, 320, 50), AIR, "above the world");
|
||||
assertEquals(column.get(-20, -65, 50), AIR, "below the world");
|
||||
assertEquals(column.sections.filter((section) => section.blocks).length, 2);
|
||||
|
||||
// flat, y from min_y, then z, then x
|
||||
const flat = column.to_flat();
|
||||
assertEquals(flat[0 * CHUNK_AREA + 15 * 16 + 0], STONE);
|
||||
assertEquals(flat[383 * CHUNK_AREA + 0 * 16 + 15], GLASS);
|
||||
const again = ChunkColumn.from_flat(-2, 3, SHAPE, flat, column.biomes);
|
||||
assertEquals(again.to_flat(), flat);
|
||||
assertEquals(again.sections.filter((section) => section.blocks).length, 2);
|
||||
|
||||
const copy = column.clone();
|
||||
copy.set(-32, -64, 63, AIR);
|
||||
assertEquals(column.get(-32, -64, 63), STONE, "a clone's blocks are its own");
|
||||
});
|
||||
|
||||
Deno.test("column heights are the highest block and the lowest y that sees the sky, block by block", () => {
|
||||
const random = new Alea("section-test");
|
||||
const column = empty_column();
|
||||
// stone to y 0, then scattered blocks up to 200, and a glass section in the air
|
||||
for (let y = -64; y <= 0; y++) {
|
||||
for (let i = 0; i < CHUNK_AREA; i++) column.set(i % 16, y, i >> 4, STONE);
|
||||
}
|
||||
for (let n = 0; n < 2000; n++) {
|
||||
const block = [STONE, GLASS, LEAVES][Math.floor(random.next() * 3)];
|
||||
column.set(
|
||||
Math.floor(random.next() * 16),
|
||||
Math.floor(random.next() * 200),
|
||||
Math.floor(random.next() * 16),
|
||||
block,
|
||||
);
|
||||
}
|
||||
// y 240 to 255, all glass, which light passes over whole
|
||||
column.sections[(240 - SHAPE.min_y) / 16] = new Section(GLASS);
|
||||
|
||||
const heights = column_heights(column, OPACITY);
|
||||
for (let i = 0; i < CHUNK_AREA; i++) {
|
||||
const [x, z] = [i % 16, i >> 4];
|
||||
let y = SHAPE.max_y - 1;
|
||||
while (y >= SHAPE.min_y && column.get(x, y, z) === AIR) y--;
|
||||
assertEquals(heights[i], y, `surface of ${x}, ${z}`);
|
||||
while (y >= SHAPE.min_y && OPACITY[column.get(x, y, z)] === 0) y--;
|
||||
assertEquals(heights[CHUNK_AREA + i], y + 1, `sky of ${x}, ${z}`);
|
||||
}
|
||||
|
||||
const nothing = column_heights(empty_column(), OPACITY);
|
||||
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");
|
||||
});
|
||||
@@ -4,7 +4,6 @@
|
||||
// the terrain is data: noises, density functions, noise settings and biome sources in mods' worldgen folders, see
|
||||
// docs/modding/worldgen/terrain.md. tests/worldgen_golden_test.ts checks the base game's makes the same world
|
||||
import { assert, assertEquals, assertRejects } from "@std/assert";
|
||||
import { CHUNK_AREA } from "@/common/constants.ts";
|
||||
import { generate_raw_chunk } from "@/common/generation.ts";
|
||||
import { copy_dir, test_game } from "./helpers.ts";
|
||||
|
||||
@@ -54,14 +53,16 @@ Deno.test("a mod's biome generates where its rule, added to the base game's, say
|
||||
let fields = 0;
|
||||
for (let cx = -2; cx < 2; cx++) {
|
||||
for (let cz = -2; cz < 2; cz++) {
|
||||
const { blocks, biomes } = generate_raw_chunk(cx, cz, "lavender", block_ids, worldgen, default_values);
|
||||
biomes.forEach((biome, column) => {
|
||||
const { column } = generate_raw_chunk(cx, cz, "lavender", block_ids, worldgen, default_values);
|
||||
const { min_y, max_y } = game.world.shape;
|
||||
column.biomes.forEach((biome, i) => {
|
||||
if (biome !== "lavender:fields") return;
|
||||
fields++;
|
||||
// its surface: the top block of the column is its sand
|
||||
let y = game.world.shape.height - 1;
|
||||
while (y > 0 && blocks[y * CHUNK_AREA + column] === 0) y--;
|
||||
assertEquals(blocks[y * CHUNK_AREA + column], block_ids["bworld:sand"]);
|
||||
const [x, z] = [cx * 16 + i % 16, cz * 16 + Math.floor(i / 16)];
|
||||
let y = max_y - 1;
|
||||
while (y > min_y && column.get(x, y, z) === 0) y--;
|
||||
assertEquals(column.get(x, y, z), block_ids["bworld:sand"]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -124,7 +124,7 @@ Deno.test("every column the overworld generates has a biome with grass, foliage
|
||||
const block_ids: Record<string, number> = {};
|
||||
blocks.forEach((b, nid) => block_ids[b.id] = nid);
|
||||
for (const [x, z] of [[0, 0], [30, -12], [-80, 45]]) {
|
||||
const { biomes } = generate_raw_chunk(x, z, game.world.seed, block_ids, game.world.worldgen);
|
||||
const { biomes } = generate_raw_chunk(x, z, game.world.seed, block_ids, game.world.worldgen).column;
|
||||
assertEquals(biomes.length, CHUNK_AREA);
|
||||
for (const biome of biomes) {
|
||||
const { colors } = get_biome(biome)!;
|
||||
|
||||
+2
-1
@@ -59,7 +59,8 @@ Deno.test("trees grow the wood of their kind and biome", async () => {
|
||||
const found_leaves = new Set<string>();
|
||||
for (let cx = -8; cx < 8; cx++) {
|
||||
for (let cz = -8; cz < 8; cz++) {
|
||||
const { blocks, biomes } = generate_raw_chunk(cx, cz, "wood-test", ids, game.world.worldgen);
|
||||
const { column } = generate_raw_chunk(cx, cz, "wood-test", ids, game.world.worldgen);
|
||||
const [blocks, biomes] = [column.to_flat(), column.biomes];
|
||||
blocks.forEach((block, i) => {
|
||||
const leaves = leaves_of.get(block);
|
||||
if (leaves) found_leaves.add(leaves);
|
||||
|
||||
@@ -113,7 +113,7 @@ Deno.test("every chunk generated is saved whole, and saved again only when it ch
|
||||
const [id, state] = untouched.palette[block];
|
||||
return id === AIR_ID ? 0 : game.world.block_ids[id] | (state << 16);
|
||||
}),
|
||||
game.world.generated_blocks(5, 5),
|
||||
game.world.generated_blocks(5, 5).to_flat(),
|
||||
);
|
||||
|
||||
assertEquals(game.save().chunks, []);
|
||||
@@ -152,7 +152,7 @@ Deno.test("saved chunks stay as they were saved, whatever generation makes there
|
||||
const { saved } = await saved_with_old_terrain();
|
||||
const { game } = await test_game("mods", saved);
|
||||
assertEquals(game.world.get_block_id(3, 121, 5), "bworld:stone");
|
||||
assertEquals(game.world.generated_blocks(0, 0)[index(3, 121, 5)], 0);
|
||||
assertEquals(game.world.generated_blocks(0, 0).get(3, 121, 5), 0);
|
||||
assertEquals(game.save().chunks.filter((c) => c.x === 0 && c.z === 0), []);
|
||||
});
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
// the base game's worldgen features, see mods/bworld/scripts/worldgen.ts
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
|
||||
import { CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
|
||||
import { generate_raw_chunk } from "@/common/generation.ts";
|
||||
import { test_game } from "./helpers.ts";
|
||||
|
||||
@@ -21,8 +21,9 @@ Deno.test("cacti grow 1 to 3 tall on desert sand, 2 or 3 tries a chunk", async (
|
||||
for (let dz = -3; dz <= 3; dz++) {
|
||||
const chunk = generate_raw_chunk(DESERT_CHUNK.x + dx, DESERT_CHUNK.z + dz, SEED, ids, game.world.worldgen);
|
||||
const { min_y, max_y } = game.world.shape;
|
||||
const at = (x: number, y: number, z: number) =>
|
||||
chunk.blocks[(y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x] & ID_MASK;
|
||||
const x0 = chunk.column.x * CHUNK_SIZE;
|
||||
const z0 = chunk.column.z * CHUNK_SIZE;
|
||||
const at = (x: number, y: number, z: number) => chunk.column.get(x0 + x, y, z0 + z) & ID_MASK;
|
||||
let in_chunk = 0;
|
||||
for (let x = 0; x < CHUNK_SIZE; x++) {
|
||||
for (let z = 0; z < CHUNK_SIZE; z++) {
|
||||
@@ -30,7 +31,7 @@ Deno.test("cacti grow 1 to 3 tall on desert sand, 2 or 3 tries a chunk", async (
|
||||
// the bottom of each cactus
|
||||
if (at(x, y, z) !== cactus || at(x, y - 1, z) === cactus) continue;
|
||||
assertEquals(at(x, y - 1, z), sand, "on sand");
|
||||
assertEquals(chunk.biomes[z * CHUNK_SIZE + x], "bworld:desert");
|
||||
assertEquals(chunk.column.biomes[z * CHUNK_SIZE + x], "bworld:desert");
|
||||
let height = 0;
|
||||
while (at(x, y + height, z) === cactus) height++;
|
||||
heights.push(height);
|
||||
|
||||
@@ -22,7 +22,7 @@ Deno.test("world generation makes the same chunks as recorded", async () => {
|
||||
const raw = generate_raw_chunk(x, z, seed, world.block_ids, world.worldgen, world.default_values);
|
||||
hashes[seed][`${x},${z}`] = {
|
||||
raw: await raw_hash(raw, name),
|
||||
final: await final_hash(world.generated_blocks(x, z), name),
|
||||
final: await final_hash(world.generated_blocks(x, z).to_flat(), name),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,10 +13,10 @@ Deno.test("the overworld generates the same way every time, with water only belo
|
||||
|
||||
for (let cx = -4; cx < 4; cx++) {
|
||||
for (let cz = -4; cz < 4; cz++) {
|
||||
const { blocks } = generate_raw_chunk(cx, cz, "worldgen-test", ids, game.world.worldgen);
|
||||
const blocks = generate_raw_chunk(cx, cz, "worldgen-test", ids, game.world.worldgen).column.to_flat();
|
||||
assertEquals(
|
||||
blocks,
|
||||
generate_raw_chunk(cx, cz, "worldgen-test", ids, game.world.worldgen).blocks,
|
||||
generate_raw_chunk(cx, cz, "worldgen-test", ids, game.world.worldgen).column.to_flat(),
|
||||
"same chunk, same blocks",
|
||||
);
|
||||
|
||||
@@ -63,7 +63,8 @@ Deno.test("trees grow on the ground, never closer than 4 blocks, even across chu
|
||||
let spilled_leaves = 0;
|
||||
for (let cx = -6; cx < 6; cx++) {
|
||||
for (let cz = -6; cz < 6; cz++) {
|
||||
const { blocks, spills } = generate_raw_chunk(cx, cz, game.world.seed, ids, game.world.worldgen);
|
||||
const { column, spills } = generate_raw_chunk(cx, cz, game.world.seed, ids, game.world.worldgen);
|
||||
const blocks = column.to_flat();
|
||||
blocks.forEach((block, i) => {
|
||||
if (logs.has(block) && ground.has(blocks[i - CHUNK_AREA])) {
|
||||
const x = cx * 16 + (i % 16);
|
||||
|
||||
@@ -231,7 +231,7 @@ const shade = (color: [number, number, number], factor: number): [number, number
|
||||
for (let cx = 0; cx < chunks; cx++) {
|
||||
const chunk_x = origin / CHUNK_SIZE + cx + center_x;
|
||||
const chunk_z = origin / CHUNK_SIZE + cz + center_z;
|
||||
const { blocks } = generate_raw_chunk(chunk_x, chunk_z, seed, block_ids, worldgen);
|
||||
const blocks = generate_raw_chunk(chunk_x, chunk_z, seed, block_ids, worldgen).column.to_flat();
|
||||
for (let lz = 0; lz < CHUNK_SIZE; lz++) {
|
||||
for (let lx = 0; lx < CHUNK_SIZE; lx++) {
|
||||
const px = cx * CHUNK_SIZE + lx;
|
||||
|
||||
Reference in New Issue
Block a user