// 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; }