223 lines
8.6 KiB
TypeScript
223 lines
8.6 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// fills a chunk from a generator's noise settings: the terrain's density sampled on a coarse grid and interpolated (minecraft's
|
|
// noise cells), caves and carvers cut out of it, then each column's biome, its surface blocks and fluids. what all of it is comes
|
|
// from the generator's noise settings, see NoiseTerrain and docs/modding/worldgen/terrain.md. trees and the rest
|
|
// are features, after it
|
|
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
|
|
import type { WorldShape } from "@/common/world_shape.ts";
|
|
import { BIOME_CELL_HEIGHT } from "@/common/section.ts";
|
|
import { get_biome } from "./biomes.ts";
|
|
import { carve_chunk } from "./carvers.ts";
|
|
import type { NoiseTerrain, TerrainColumn } from "./terrain.ts";
|
|
import {
|
|
compile_fluids,
|
|
compile_surface_rule,
|
|
surface_rule_reach,
|
|
type SurfaceBlocks,
|
|
type SurfaceContext,
|
|
} from "./surface_rules.ts";
|
|
|
|
// fills blocks (indexed (y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x), each column's surface height and each biome
|
|
// cell's biome (indexed like blocks, a layer of cells at a time, see column_biomes). block is a block's numeric id with
|
|
// its default states
|
|
export function generate_noise_chunk(
|
|
blocks: Uint32Array,
|
|
heights: Int32Array,
|
|
biomes: string[],
|
|
chunk_x: number,
|
|
chunk_z: number,
|
|
shape: WorldShape,
|
|
terrain: NoiseTerrain,
|
|
block: (id: string) => number,
|
|
) {
|
|
const settings = terrain.settings;
|
|
const cell_width = settings.cell_width;
|
|
const cell_height = settings.terrain.cell_height;
|
|
const cave_cell_height = settings.caves.cell_height;
|
|
const corners = CHUNK_SIZE / cell_width + 1;
|
|
const { min_y, max_y, height } = shape;
|
|
const terrain_levels = height / cell_height + 1;
|
|
const cave_levels = height / cave_cell_height + 1;
|
|
const x0 = chunk_x * CHUNK_SIZE;
|
|
const z0 = chunk_z * CHUNK_SIZE;
|
|
const cells = height / BIOME_CELL_HEIGHT;
|
|
// the cell a y is in, below the world the lowest
|
|
const ground_cell = (y: number) => Math.max(0, Math.floor((y - min_y) / BIOME_CELL_HEIGHT));
|
|
|
|
// density at the corners of every cell
|
|
const corner_columns: TerrainColumn[] = [];
|
|
for (let cz = 0; cz < corners; cz++) {
|
|
for (let cx = 0; cx < corners; cx++) {
|
|
corner_columns.push(terrain.column(x0 + cx * cell_width, z0 + cz * cell_width));
|
|
}
|
|
}
|
|
const solid = new Float32Array(corners * corners * terrain_levels);
|
|
const caves = new Float32Array(corners * corners * cave_levels);
|
|
for (let i = 0; i < corner_columns.length; i++) {
|
|
const column = corner_columns[i];
|
|
const x = x0 + (i % corners) * cell_width;
|
|
const z = z0 + Math.floor(i / corners) * cell_width;
|
|
for (let level = 0; level < terrain_levels; level++) {
|
|
solid[i * terrain_levels + level] = terrain.density(column, x, min_y + level * cell_height, z);
|
|
}
|
|
for (let level = 0; level < cave_levels; level++) {
|
|
caves[i * cave_levels + level] = terrain.cave(column, x, min_y + level * cave_cell_height, z);
|
|
}
|
|
}
|
|
|
|
const carved = carve_chunk(terrain.seed, settings.carvers ?? [], chunk_x, chunk_z, shape);
|
|
|
|
// every column of the chunk plus a ring around it, for how steep the ground is. the chunk's own with their
|
|
// climate, for their biome
|
|
const columns: TerrainColumn[] = [];
|
|
const ring = CHUNK_SIZE + 2;
|
|
for (let z = -1; z <= CHUNK_SIZE; z++) {
|
|
for (let x = -1; x <= CHUNK_SIZE; x++) {
|
|
const inside = x >= 0 && x < CHUNK_SIZE && z >= 0 && z < CHUNK_SIZE;
|
|
columns.push(terrain.column(x0 + x, z0 + z, inside));
|
|
}
|
|
}
|
|
const column_at = (x: number, z: number) => columns[(z + 1) * ring + x + 1];
|
|
|
|
// biomes are registered by mods (bworld's are in mods/bworld/worldgen/biomes), with their surfaces' ids looked up
|
|
// once per chunk
|
|
const found = new Map<string, SurfaceBlocks>();
|
|
const surface_of = (id: string) => {
|
|
let surface = found.get(id);
|
|
if (!surface) {
|
|
const biome = get_biome(id);
|
|
if (!biome) throw new Error(`biome ${id} isn't registered, is the mod that adds it loaded?`);
|
|
surface = {
|
|
top: block(biome.surface.top),
|
|
filler: block(biome.surface.filler),
|
|
filler_depth: biome.surface.filler_depth,
|
|
underwater_top: block(biome.surface.underwater_top),
|
|
underwater_filler: block(biome.surface.underwater_filler),
|
|
bare_cliffs: biome.surface.bare_cliffs,
|
|
};
|
|
found.set(id, surface);
|
|
}
|
|
return surface;
|
|
};
|
|
|
|
const surface_rule = compile_surface_rule(settings.surface_rule, terrain, block);
|
|
const fluids = compile_fluids(settings.fluids, terrain, block);
|
|
// no fluid goes higher than this, and nothing below the rule's reach needs it
|
|
const fluid_top = Math.max(...settings.fluids.map((fluid) => fluid.below));
|
|
const reach = surface_rule_reach(settings.surface_rule);
|
|
const default_block = block(settings.default_block);
|
|
const sea_level = settings.sea_level;
|
|
const context: SurfaceContext = {
|
|
x: 0,
|
|
y: 0,
|
|
z: 0,
|
|
depth: 0,
|
|
underwater: false,
|
|
steep: false,
|
|
preliminary: 0,
|
|
biome: "",
|
|
surface: undefined!,
|
|
};
|
|
|
|
for (let z = 0; z < CHUNK_SIZE; z++) {
|
|
for (let x = 0; x < CHUNK_SIZE; x++) {
|
|
fill_column(x, z);
|
|
}
|
|
}
|
|
|
|
function fill_column(x: number, z: number) {
|
|
const cell_x = Math.min(Math.floor(x / cell_width), corners - 2);
|
|
const cell_z = Math.min(Math.floor(z / cell_width), corners - 2);
|
|
const tx = (x - cell_x * cell_width) / cell_width;
|
|
const tz = (z - cell_z * cell_width) / cell_width;
|
|
const c00 = cell_z * corners + cell_x;
|
|
const c10 = c00 + 1;
|
|
const c01 = c00 + corners;
|
|
const c11 = c01 + 1;
|
|
const w00 = (1 - tx) * (1 - tz);
|
|
const w10 = tx * (1 - tz);
|
|
const w01 = (1 - tx) * tz;
|
|
const w11 = tx * tz;
|
|
// one column through the corner grid, blended between its four corners
|
|
const blend = (grid: Float32Array, levels: number, level: number) =>
|
|
grid[c00 * levels + level] * w00 + grid[c10 * levels + level] * w10 +
|
|
grid[c01 * levels + level] * w01 + grid[c11 * levels + level] * w11;
|
|
|
|
// solid where both the terrain and the caves are above 0, by y above min_y
|
|
const is_solid = new Uint8Array(height);
|
|
for (let y = 0; y < height; y++) {
|
|
const level = Math.min(Math.floor(y / cell_height), terrain_levels - 2);
|
|
const t = (y - level * cell_height) / cell_height;
|
|
let density = blend(solid, terrain_levels, level) * (1 - t) + blend(solid, terrain_levels, level + 1) * t;
|
|
if (density > 0) {
|
|
const cave_level = Math.min(Math.floor(y / cave_cell_height), cave_levels - 2);
|
|
const ct = (y - cave_level * cave_cell_height) / cave_cell_height;
|
|
const cave = blend(caves, cave_levels, cave_level) * (1 - ct) +
|
|
blend(caves, cave_levels, cave_level + 1) * ct;
|
|
density = Math.min(density, cave);
|
|
}
|
|
is_solid[y] = density > 0 && !carved?.[y * CHUNK_AREA + z * CHUNK_SIZE + x] ? 1 : 0;
|
|
}
|
|
|
|
const column = column_at(x, z);
|
|
let surface_y = max_y - 1;
|
|
while (surface_y > min_y && !is_solid[surface_y - min_y]) surface_y--;
|
|
|
|
const ground = column.height;
|
|
const neighbors = [column_at(x - 1, z), column_at(x + 1, z), column_at(x, z - 1), column_at(x, z + 1)];
|
|
const steep = neighbors.some((neighbor) => Math.abs(neighbor.height - ground) >= settings.steep);
|
|
heights[z * CHUNK_SIZE + x] = surface_y;
|
|
|
|
// the biome of each cell, worked out once for the whole column when no rule looks at heights. the ground's
|
|
// surface is its own cell's
|
|
let biome = "";
|
|
for (let cell = 0; cell < cells; cell++) {
|
|
const y = min_y + cell * BIOME_CELL_HEIGHT;
|
|
if (cell === 0 || terrain.by_height) biome = terrain.biome(column, surface_y, steep, y);
|
|
biomes[cell * CHUNK_AREA + z * CHUNK_SIZE + x] = biome;
|
|
}
|
|
biome = biomes[ground_cell(surface_y) * CHUNK_AREA + z * CHUNK_SIZE + x];
|
|
|
|
context.x = x0 + x;
|
|
context.z = z0 + z;
|
|
context.steep = steep;
|
|
context.preliminary = ground;
|
|
context.biome = biome;
|
|
context.surface = surface_of(biome);
|
|
const index = (y: number) => (y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x;
|
|
// the same sum as the rule's own condition, so it can't disagree with it
|
|
const lowest = ground + reach;
|
|
// solid blocks since the last air going down, 0 is a block with air on top
|
|
let depth = -1;
|
|
// nothing but air above so far
|
|
let open_sky = true;
|
|
// whether the ground's surface is under water, for its cover
|
|
let underwater = false;
|
|
for (let y = max_y - 1; y >= min_y; y--) {
|
|
context.y = y;
|
|
if (!is_solid[y - min_y]) {
|
|
depth = -1;
|
|
if (y < fluid_top) {
|
|
const fluid = fluids(context, open_sky);
|
|
if (fluid !== undefined) blocks[index(y)] = fluid;
|
|
}
|
|
continue;
|
|
}
|
|
depth += 1;
|
|
if (open_sky) {
|
|
open_sky = false;
|
|
underwater = y < sea_level - 1;
|
|
}
|
|
if (y < lowest) {
|
|
blocks[index(y)] = default_block;
|
|
continue;
|
|
}
|
|
context.depth = depth;
|
|
context.underwater = underwater;
|
|
blocks[index(y)] = surface_rule(context) ?? default_block;
|
|
}
|
|
}
|
|
}
|