More changes, im going to bed
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
// 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 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_HEIGHT, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { get_biome } from "./biomes.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 * CHUNK_AREA + z * CHUNK_SIZE + x) and each column's surface height and biome. 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,
|
||||
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 terrain_levels = CHUNK_HEIGHT / cell_height + 1;
|
||||
const cave_levels = CHUNK_HEIGHT / cave_cell_height + 1;
|
||||
const x0 = chunk_x * CHUNK_SIZE;
|
||||
const z0 = chunk_z * CHUNK_SIZE;
|
||||
|
||||
// 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, level * cell_height, z);
|
||||
}
|
||||
for (let level = 0; level < cave_levels; level++) {
|
||||
caves[i * cave_levels + level] = terrain.cave(column, x, level * cave_cell_height, z);
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
const is_solid = new Uint8Array(CHUNK_HEIGHT);
|
||||
for (let y = 0; y < CHUNK_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 ? 1 : 0;
|
||||
}
|
||||
|
||||
const column = column_at(x, z);
|
||||
let surface_y = CHUNK_HEIGHT - 1;
|
||||
while (surface_y > 0 && !is_solid[surface_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);
|
||||
const biome = terrain.biome(column, surface_y, steep);
|
||||
|
||||
heights[z * CHUNK_SIZE + x] = surface_y;
|
||||
biomes[z * CHUNK_SIZE + x] = biome;
|
||||
|
||||
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 * 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 = CHUNK_HEIGHT - 1; y >= 0; y--) {
|
||||
context.y = y;
|
||||
if (!is_solid[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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user