World shape for dimension

This commit is contained in:
2026-11-06 22:49:43 -03:00
parent 7458a2d42a
commit 818ce22dd6
60 changed files with 749 additions and 349 deletions
+19 -11
View File
@@ -4,7 +4,8 @@
// generating a chunk: the terrain from the generator's data (common/worldgen), then ores, then mods' features.
// only the server generates, players get finished chunks
import { Alea } from "@paulaboks/rng";
import { AIR, CHUNK_AREA, CHUNK_HEIGHT, 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 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";
@@ -15,6 +16,8 @@ export { named_noise_2d, named_noise_3d };
// what mods add to generation, see docs/modding/worldgen/index.md
export interface WorldgenSetup {
// where the dimension's blocks are, chunks hold its whole height
shape: WorldShape;
// the terrain, biomes and surface, from noise settings, see docs/modding/worldgen/terrain.md
generator: Generator;
// each mod's worldgen/ores.json, in load order. each is a pass of its own, so a mod's ores can replace an earlier
@@ -34,7 +37,7 @@ export interface SetupFeature {
}
export interface RawChunk {
// numeric block ids, only what this chunk generated itself
// numeric block ids, only what this chunk generated itself, indexed (y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x
blocks: Uint32Array;
// blocks it generated in other chunks (from features like a mod's trees), flattened as x, y, z, numeric id
spills: Int32Array;
@@ -56,13 +59,14 @@ export function generate_raw_chunk(
// the value to store for each numeric id, with its default states. just the id when missing
default_values?: number[],
): RawChunk {
const blocks = new Uint32Array(CHUNK_SIZE * CHUNK_SIZE * CHUNK_HEIGHT);
const shape = worldgen.shape;
const blocks = new Uint32Array(shape.volume);
const spills: number[] = [];
const heights = new Int32Array(CHUNK_AREA);
const biomes: string[] = new Array(CHUNK_AREA);
const set = (x: number, y: number, z: number, nid: number) => {
if (y < 0 || y >= CHUNK_HEIGHT) {
if (!shape.contains(y)) {
return;
}
nid = default_values?.[nid] ?? nid;
@@ -74,17 +78,17 @@ export function generate_raw_chunk(
}
const lx = x - chunk_x * CHUNK_SIZE;
const lz = z - chunk_z * CHUNK_SIZE;
blocks[y * CHUNK_AREA + lz * CHUNK_SIZE + lx] = nid;
blocks[(y - shape.min_y) * CHUNK_AREA + lz * CHUNK_SIZE + lx] = nid;
};
const id = (name: string) => {
const nid = block_ids[name];
return nid === undefined ? AIR : default_values?.[nid] ?? nid;
};
generate_noise_chunk(blocks, heights, biomes, chunk_x, chunk_z, worldgen.generator.for_seed(seed), id);
generate_noise_chunk(blocks, heights, biomes, chunk_x, chunk_z, shape, worldgen.generator.for_seed(seed), id);
for (const ores of worldgen.ores) {
generate_ores(blocks, chunk_x, chunk_z, seed, block_ids, ores, default_values);
generate_ores(blocks, chunk_x, chunk_z, shape, seed, block_ids, ores, default_values);
}
generate_features(blocks, heights, biomes, set, chunk_x, chunk_z, seed, block_ids, worldgen);
@@ -96,6 +100,7 @@ function generate_ores(
blocks: Uint32Array,
chunk_x: number,
chunk_z: number,
shape: WorldShape,
seed: string,
block_ids: Record<string, number>,
ores: OreJson[],
@@ -109,10 +114,10 @@ function generate_ores(
}
const noises = usable.map((ore) => named_noise_3d(seed, ore.id));
for (let y = 0; y < CHUNK_HEIGHT; y++) {
for (let y = shape.min_y; y < shape.max_y; y++) {
for (let lz = 0; lz < CHUNK_SIZE; lz++) {
for (let lx = 0; lx < CHUNK_SIZE; lx++) {
const index = y * CHUNK_AREA + lz * CHUNK_SIZE + lx;
const index = (y - shape.min_y) * CHUNK_AREA + lz * CHUNK_SIZE + lx;
const current = blocks[index] & ID_MASK;
if (current === AIR) {
continue;
@@ -159,6 +164,7 @@ function generate_features(
return lz * CHUNK_SIZE + lx;
};
const shape = worldgen.shape;
const chunk_biomes = new Set(biomes);
for (const feature of worldgen.features) {
if (feature.biomes && ![...feature.biomes].some((biome) => chunk_biomes.has(biome))) continue;
@@ -166,6 +172,8 @@ function generate_features(
x: chunk_x,
z: chunk_z,
seed,
min_y: shape.min_y,
max_y: shape.max_y,
rng: new Alea(`${seed}_feature_${feature.id}_${chunk_x}_${chunk_z}`),
random: (name) => new Alea(`${seed}_${name}`),
noise_2d: (name) => named_noise_2d(seed, name),
@@ -173,10 +181,10 @@ function generate_features(
height_at: (x, z) => heights[local(x, z, "height_at")],
biome_at: (x, z) => biomes[local(x, z, "biome_at")],
get_block(x, y, z) {
if (y < 0 || y >= CHUNK_HEIGHT) {
if (!shape.contains(y)) {
return undefined;
}
const nid = blocks[y * CHUNK_AREA + local(x, z, "get_block")] & ID_MASK;
const nid = blocks[(y - shape.min_y) * CHUNK_AREA + local(x, z, "get_block")] & ID_MASK;
return nid === AIR ? "cubi:air" : ids_by_nid[nid];
},
set_block(x, y, z, id) {