Caves
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// carvers: winding tunnels with the odd room, cut out of the terrain before surface rules run, so their floors and fluids
|
||||
// work like the density caves'. a tunnel starts in one chunk and wanders up to REACH chunks away, so each chunk replays
|
||||
// the tunnels of every chunk around it and keeps the blocks that are its own
|
||||
import { Alea } from "@paulaboks/rng";
|
||||
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import type { WorldShape } from "@/common/world_shape.ts";
|
||||
import type { CarverJson } from "./data.ts";
|
||||
|
||||
const REACH = 8;
|
||||
|
||||
type Random = { next(): number };
|
||||
const int = (random: Random, bound: number) => Math.floor(random.next() * bound);
|
||||
|
||||
// 1 for each block a carver cut out, indexed like noise_chunk's blocks. undefined when there are no carvers
|
||||
export function carve_chunk(
|
||||
seed: string,
|
||||
carvers: CarverJson[],
|
||||
chunk_x: number,
|
||||
chunk_z: number,
|
||||
shape: WorldShape,
|
||||
): Uint8Array | undefined {
|
||||
if (carvers.length === 0) return undefined;
|
||||
const carved = new Uint8Array(CHUNK_AREA * shape.height);
|
||||
const x0 = chunk_x * CHUNK_SIZE;
|
||||
const z0 = chunk_z * CHUNK_SIZE;
|
||||
// how wide and tall the current cave start's tunnels are, and its floor: how far down its ellipsoids go, from
|
||||
// -1 their bottom to 0 their middle
|
||||
let horizontal = 1, vertical = 1, floor = -1;
|
||||
|
||||
// marks the chunk's blocks inside an ellipsoid, above the floor and above the world's lowest layer
|
||||
const ellipsoid = (x: number, y: number, z: number, radius: number, height: number) => {
|
||||
const max_x = Math.min(Math.floor(x + radius) - x0, CHUNK_SIZE - 1);
|
||||
const max_z = Math.min(Math.floor(z + radius) - z0, CHUNK_SIZE - 1);
|
||||
const min_y = Math.max(Math.floor(y - height), shape.min_y + 1);
|
||||
const max_y = Math.min(Math.floor(y + height) + 1, shape.max_y - 1);
|
||||
for (let lx = Math.max(Math.floor(x - radius) - x0, 0); lx <= max_x; lx++) {
|
||||
const nx = (x0 + lx + 0.5 - x) / radius;
|
||||
for (let lz = Math.max(Math.floor(z - radius) - z0, 0); lz <= max_z; lz++) {
|
||||
const nz = (z0 + lz + 0.5 - z) / radius;
|
||||
if (nx * nx + nz * nz >= 1) continue;
|
||||
for (let by = min_y; by <= max_y; by++) {
|
||||
const ny = (by - 0.5 - y) / height;
|
||||
if (ny > floor && nx * nx + ny * ny + nz * nz < 1) {
|
||||
carved[(by - shape.min_y) * CHUNK_AREA + lz * CHUNK_SIZE + lx] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// each tunnel has its own random numbers, so where it stops for this chunk doesn't change what comes after it
|
||||
const tunnel = (
|
||||
random: Random,
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
thickness: number,
|
||||
yaw: number,
|
||||
pitch: number,
|
||||
start: number,
|
||||
end: number,
|
||||
vertical_scale: number,
|
||||
) => {
|
||||
const split = int(random, Math.floor(end / 2)) + Math.floor(end / 4);
|
||||
const gentle = int(random, 6) === 0;
|
||||
let yaw_change = 0, pitch_change = 0;
|
||||
for (let step = start; step < end; step++) {
|
||||
const radius = 1.5 + Math.sin(Math.PI * step / end) * thickness;
|
||||
x += Math.cos(yaw) * Math.cos(pitch);
|
||||
y += Math.sin(pitch);
|
||||
z += Math.sin(yaw) * Math.cos(pitch);
|
||||
pitch = pitch * (gentle ? 0.92 : 0.7) + pitch_change * 0.1;
|
||||
yaw += yaw_change * 0.1;
|
||||
pitch_change = pitch_change * 0.9 + (random.next() - random.next()) * random.next() * 2;
|
||||
yaw_change = yaw_change * 0.75 + (random.next() - random.next()) * random.next() * 4;
|
||||
if (step === split && thickness > 1) {
|
||||
for (const side of [-1, 1]) {
|
||||
const branch = new Alea(`${random.next()}`);
|
||||
const branch_thickness = random.next() * 0.5 + 0.5;
|
||||
tunnel(branch, x, y, z, branch_thickness, yaw + side * Math.PI / 2, pitch / 3, step, end, 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (int(random, 4) === 0) continue;
|
||||
// too far to come back to this chunk in the steps it has left
|
||||
const dx = x - (x0 + 8), dz = z - (z0 + 8), left = end - step, reach = thickness + 18;
|
||||
if (dx * dx + dz * dz - left * left > reach * reach) return;
|
||||
ellipsoid(x, y, z, radius * horizontal, radius * vertical_scale * vertical);
|
||||
}
|
||||
};
|
||||
|
||||
carvers.forEach((carver, i) => {
|
||||
for (let oz = chunk_z - REACH; oz <= chunk_z + REACH; oz++) {
|
||||
for (let ox = chunk_x - REACH; ox <= chunk_x + REACH; ox++) {
|
||||
const random = new Alea(`${seed}_carver_${i}_${ox}_${oz}`);
|
||||
if (random.next() >= carver.probability) continue;
|
||||
// mostly a few, now and then many
|
||||
const count = int(random, int(random, int(random, 15) + 1) + 1);
|
||||
for (let n = 0; n < count; n++) {
|
||||
const x = ox * CHUNK_SIZE + int(random, CHUNK_SIZE);
|
||||
const y = carver.min_y + int(random, carver.max_y - carver.min_y + 1);
|
||||
const z = oz * CHUNK_SIZE + int(random, CHUNK_SIZE);
|
||||
horizontal = 0.7 + random.next() * 0.7;
|
||||
vertical = 0.8 + random.next() * 0.5;
|
||||
floor = -1 + random.next() * 0.6;
|
||||
let tunnels = 1;
|
||||
if (int(random, 4) === 0) {
|
||||
const radius = 2.5 + random.next() * 6;
|
||||
ellipsoid(x + 1, y, z, radius, radius * (0.75 + random.next() * 0.25));
|
||||
tunnels += int(random, 4);
|
||||
}
|
||||
for (let t = 0; t < tunnels; t++) {
|
||||
const yaw = random.next() * Math.PI * 2;
|
||||
const pitch = (random.next() - 0.5) / 4;
|
||||
let thickness = random.next() * 2 + random.next();
|
||||
if (int(random, 10) === 0) thickness *= random.next() * random.next() * 3 + 1;
|
||||
const end = 112 - int(random, 28);
|
||||
tunnel(new Alea(`${random.next()}`), x, y, z, thickness, yaw, pitch, 0, end, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return carved;
|
||||
}
|
||||
@@ -66,6 +66,8 @@ export interface NoiseSettingsJson {
|
||||
terrain: { density: DensityJson; cell_height: number };
|
||||
// solid only where both terrain and caves are above 0
|
||||
caves: { density: DensityJson; cell_height: number };
|
||||
// tunnels cut out after caves, see carvers.ts
|
||||
carvers?: CarverJson[];
|
||||
// the ground's height before 3d noise, for surface rules, biomes and finding things far away
|
||||
preliminary_surface: DensityJson;
|
||||
// named values biome rules read, like temperature
|
||||
@@ -78,6 +80,14 @@ export interface NoiseSettingsJson {
|
||||
surface_rule: SurfaceRuleJson;
|
||||
}
|
||||
|
||||
export interface CarverJson {
|
||||
// the chance a chunk has tunnels starting in it
|
||||
probability: number;
|
||||
// where they start
|
||||
min_y: number;
|
||||
max_y: number;
|
||||
}
|
||||
|
||||
export interface FluidJson {
|
||||
block: string;
|
||||
// only below this y
|
||||
@@ -303,6 +313,13 @@ const noise_settings_schema: z.ZodType<NoiseSettingsJson> = z.object({
|
||||
cell_width: z.literal([1, 2, 4, 8, 16]),
|
||||
terrain: z.object({ density, cell_height }),
|
||||
caves: z.object({ density, cell_height }),
|
||||
carvers: z.array(
|
||||
z.object({
|
||||
probability: z.number().min(0).max(1),
|
||||
min_y: z.int().min(LOWEST_Y).max(HIGHEST_Y),
|
||||
max_y: z.int().min(LOWEST_Y).max(HIGHEST_Y),
|
||||
}).refine((carver) => carver.min_y <= carver.max_y, "min_y can't be above max_y"),
|
||||
).optional(),
|
||||
preliminary_surface: density,
|
||||
climate: z.record(
|
||||
z.string(`isn't a name a climate value can have: lowercase letters and _, but not ${RULE_NAMES.join(", ")}`)
|
||||
|
||||
@@ -2,13 +2,14 @@
|
||||
// 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
|
||||
// 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,
|
||||
@@ -66,6 +67,8 @@ export function generate_noise_chunk(
|
||||
}
|
||||
}
|
||||
|
||||
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[] = [];
|
||||
@@ -155,7 +158,7 @@ export function generate_noise_chunk(
|
||||
blend(caves, cave_levels, cave_level + 1) * ct;
|
||||
density = Math.min(density, cave);
|
||||
}
|
||||
is_solid[y] = density > 0 ? 1 : 0;
|
||||
is_solid[y] = density > 0 && !carved?.[y * CHUNK_AREA + z * CHUNK_SIZE + x] ? 1 : 0;
|
||||
}
|
||||
|
||||
const column = column_at(x, z);
|
||||
|
||||
@@ -120,6 +120,7 @@ A generator. The base game's is `worldgen/noise_settings/overworld.json`:
|
||||
"cell_width": 4,
|
||||
"terrain": { "density": "bworld:overworld/terrain", "cell_height": 8 },
|
||||
"caves": { "density": "bworld:overworld/caves", "cell_height": 4 },
|
||||
"carvers": [{ "probability": 0.15, "min_y": 8, "max_y": 120 }],
|
||||
"preliminary_surface": "bworld:overworld/height",
|
||||
"climate": {
|
||||
"continentalness": "bworld:overworld/continents",
|
||||
@@ -150,6 +151,7 @@ A generator. The base game's is `worldgen/noise_settings/overworld.json`:
|
||||
| `default_block` | What solid ground is where the surface rule doesn't say otherwise. |
|
||||
| `cell_width` | How far apart across `terrain` and `caves` are sampled, 1 to 16. |
|
||||
| `terrain`, `caves` | Their density function, and how far apart up they're sampled. Solid where both are above 0. |
|
||||
| `carvers` | Optional tunnels cut after `caves`: the `probability` they start in a chunk, between `min_y` and `max_y`. |
|
||||
| `preliminary_surface` | The ground's height before 3D noise. Surface rules, `steep` and `/locatebiome` use it. |
|
||||
| `climate` | Named values biome rules look at. Any names, except `surface`, `y`, `depth` and `steep`. |
|
||||
| `steep` | A column is steep when its preliminary surface is this much higher or lower than a neighbor's. |
|
||||
|
||||
@@ -13,6 +13,13 @@
|
||||
"density": "bworld:overworld/caves",
|
||||
"cell_height": 4
|
||||
},
|
||||
"carvers": [
|
||||
{
|
||||
"probability": 0.15,
|
||||
"min_y": 8,
|
||||
"max_y": 120
|
||||
}
|
||||
],
|
||||
"preliminary_surface": "bworld:overworld/height",
|
||||
"climate": {
|
||||
"continentalness": "bworld:overworld/continents",
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { carve_chunk } from "@/common/worldgen/carvers.ts";
|
||||
import { WorldShape } from "@/common/world_shape.ts";
|
||||
|
||||
const shape = new WorldShape({ min_y: -64, max_y: 320, sky_light: true });
|
||||
const carvers = [{ probability: 1, min_y: 0, max_y: 40 }];
|
||||
|
||||
Deno.test("carvers cut tunnels that carry on across chunk borders", () => {
|
||||
assertEquals(carve_chunk("seed", [], 0, 0, shape), undefined, "no carvers, nothing to cut");
|
||||
const west = carve_chunk("seed", carvers, 0, 0, shape)!;
|
||||
const east = carve_chunk("seed", carvers, 1, 0, shape)!;
|
||||
assertEquals(west, carve_chunk("seed", carvers, 0, 0, shape), "same chunk, same tunnels");
|
||||
// a cell on the border cut in both chunks
|
||||
let joined = false;
|
||||
for (let y = 0; y < shape.height; y++) {
|
||||
for (let z = 0; z < CHUNK_SIZE; z++) {
|
||||
const row = y * CHUNK_AREA + z * CHUNK_SIZE;
|
||||
if (west[row + CHUNK_SIZE - 1] && east[row]) joined = true;
|
||||
}
|
||||
}
|
||||
assert(joined, "a tunnel crosses from one chunk into the next");
|
||||
assert(west.subarray(0, CHUNK_AREA).every((cut) => !cut), "the world's bottom layer stays");
|
||||
});
|
||||
Vendored
+384
-384
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user