This commit is contained in:
2026-03-12 22:41:27 -03:00
parent d18942196b
commit c154356e93
17 changed files with 287 additions and 128 deletions
+27 -27
View File
@@ -1,47 +1,47 @@
import { Alea, create_noise_2d } from "@paulaboks/rng";
import { Dimension } from "./components/dimension.ts";
import { ClientWorld } from "./client_world.ts";
export function generate(dimension: Dimension, width: number, height: number, seed = "seed") {
export function generate(
world: ClientWorld,
dimension: Dimension,
width: number,
depth: number,
max_height = 64,
seed = "seed",
) {
const height_noise = create_noise_2d(new Alea(seed + "_height"));
const tree_noise = create_noise_2d(new Alea(seed + "_trees"));
const scale = 0.05;
for (let z = 0; z < height; z++) {
for (let z = 0; z < depth; z++) {
for (let x = 0; x < width; x++) {
// multi-octave terrain
const nx = x * scale;
const nz = z * scale;
let h = height_noise(nx, nz) * 1 + height_noise(nx * 2, nz * 2) * 0.5 + height_noise(nx * 4, nz * 4) * 0.25;
// multi-octave terrain
let h = height_noise(nx, nz) * 1 +
height_noise(nx * 2, nz * 2) * 0.5 +
height_noise(nx * 4, nz * 4) * 0.25;
h /= 1.75; // normalize
h /= 1.75;
let id = "bworld:grass";
// convert noise to terrain height
const terrain_height = Math.floor((h * 0.5 + 0.5) * max_height);
if (h < -0.25) {
id = "bworld:water";
} else if (h < -0.15) {
id = "bworld:sand";
} else {
id = "bworld:grass";
}
for (let y = 0; y <= terrain_height; y++) {
let id = "bworld:stone";
dimension.tiles.push({ x, z, id, y: 0 });
// tree placement
/*if (id === "bworld:grass") {
const t = tree_noise(x * 0.12, y * 0.12);
if (t > 0.65) {
dimension.tiles.push({
x,
y,
id: "bworld:tree",
});
// simple terrain layers
if (y === terrain_height) {
id = "bworld:grass";
} else if (y > terrain_height - 4) {
id = "bworld:dirt";
}
}*/
dimension.add_block(world, { x, y, z, id });
}
}
}
}