|
|
|
@@ -12,7 +12,6 @@
|
|
|
|
|
import type { FeatureBiome, FeatureChunk, WorldgenContext } from "cubi/worldgen";
|
|
|
|
|
|
|
|
|
|
const CHUNK_SIZE = 16;
|
|
|
|
|
const CHUNK_HEIGHT = 256;
|
|
|
|
|
|
|
|
|
|
const CELL = 4;
|
|
|
|
|
// trunks are at least this far apart
|
|
|
|
@@ -87,7 +86,8 @@ function tree_sites(chunk: FeatureChunk): TreeSite[] {
|
|
|
|
|
type PlaceBlock = (x: number, y: number, z: number, block: "log" | "leaves") => void;
|
|
|
|
|
|
|
|
|
|
// grows a tree with its trunk's bottom at x, y, z. returns false when it wouldn't fit under the top of the world
|
|
|
|
|
function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random, place: PlaceBlock) {
|
|
|
|
|
// max_y is the top of the world, nothing grows into it
|
|
|
|
|
function grow_tree(kind: TreeKind, x: number, y: number, z: number, max_y: number, rng: Random, place: PlaceBlock) {
|
|
|
|
|
const random = (min: number, max: number) => min + Math.floor(rng.next() * (max - min + 1));
|
|
|
|
|
const trunk = (height: number) => {
|
|
|
|
|
for (let i = 0; i < height; i++) place(x, y + i, z, "log");
|
|
|
|
@@ -110,7 +110,7 @@ function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random,
|
|
|
|
|
// birch is oak's canopy on a taller, straight trunk
|
|
|
|
|
const height = kind === "birch" ? random(5, 7) : random(4, 6);
|
|
|
|
|
top = y + height + 1;
|
|
|
|
|
if (top >= CHUNK_HEIGHT - 1) return false;
|
|
|
|
|
if (top >= max_y - 1) return false;
|
|
|
|
|
trunk(height);
|
|
|
|
|
layer(y + height - 2, 2, true);
|
|
|
|
|
layer(y + height - 1, 2, true);
|
|
|
|
@@ -122,7 +122,7 @@ function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random,
|
|
|
|
|
// a short trunk under a wide, rounded canopy
|
|
|
|
|
const height = random(4, 6);
|
|
|
|
|
top = y + height + 1;
|
|
|
|
|
if (top >= CHUNK_HEIGHT - 1) return false;
|
|
|
|
|
if (top >= max_y - 1) return false;
|
|
|
|
|
trunk(height);
|
|
|
|
|
layer(y + height - 2, 2, false);
|
|
|
|
|
layer(y + height - 1, MAX_CANOPY, false);
|
|
|
|
@@ -133,7 +133,7 @@ function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random,
|
|
|
|
|
case "big_oak": {
|
|
|
|
|
const height = random(6, 8);
|
|
|
|
|
top = y + height + 1;
|
|
|
|
|
if (top >= CHUNK_HEIGHT - 1) return false;
|
|
|
|
|
if (top >= max_y - 1) return false;
|
|
|
|
|
trunk(height);
|
|
|
|
|
layer(y + height - 3, 2, true);
|
|
|
|
|
layer(y + height - 2, 3, false);
|
|
|
|
@@ -145,7 +145,7 @@ function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random,
|
|
|
|
|
case "spruce": {
|
|
|
|
|
const height = random(7, 10);
|
|
|
|
|
top = y + height + 1;
|
|
|
|
|
if (top >= CHUNK_HEIGHT - 1) return false;
|
|
|
|
|
if (top >= max_y - 1) return false;
|
|
|
|
|
trunk(height);
|
|
|
|
|
// a cone of layers getting wider going down, every other one narrower, like minecraft's spruce
|
|
|
|
|
place(x, y + height + 1, z, "leaves");
|
|
|
|
@@ -160,7 +160,7 @@ function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random,
|
|
|
|
|
case "jungle": {
|
|
|
|
|
const height = random(9, 13);
|
|
|
|
|
top = y + height + 1;
|
|
|
|
|
if (top >= CHUNK_HEIGHT - 1) return false;
|
|
|
|
|
if (top >= max_y - 1) return false;
|
|
|
|
|
trunk(height);
|
|
|
|
|
layer(y + height - 2, 3, false);
|
|
|
|
|
layer(y + height - 1, 3, true);
|
|
|
|
@@ -172,7 +172,7 @@ function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random,
|
|
|
|
|
// a short trunk that leans one way at the top, under a flat, wide canopy
|
|
|
|
|
const height = random(4, 5);
|
|
|
|
|
top = y + height + 2;
|
|
|
|
|
if (top >= CHUNK_HEIGHT - 1) return false;
|
|
|
|
|
if (top >= max_y - 1) return false;
|
|
|
|
|
trunk(height);
|
|
|
|
|
const lean_x = random(-1, 1);
|
|
|
|
|
const lean_z = lean_x === 0 ? (rng.next() < 0.5 ? -1 : 1) : 0;
|
|
|
|
@@ -223,7 +223,7 @@ export function register_trees(gen: WorldgenContext) {
|
|
|
|
|
const x0 = chunk.x * CHUNK_SIZE;
|
|
|
|
|
const z0 = chunk.z * CHUNK_SIZE;
|
|
|
|
|
const place = (x: number, y: number, z: number, block: "log" | "leaves", wood: string) => {
|
|
|
|
|
if (y < 0 || y >= CHUNK_HEIGHT) return;
|
|
|
|
|
if (y < chunk.min_y || y >= chunk.max_y) return;
|
|
|
|
|
const id = `bworld:${wood}_${block}`;
|
|
|
|
|
if (x < x0 || x >= x0 + CHUNK_SIZE || z < z0 || z >= z0 + CHUNK_SIZE) {
|
|
|
|
|
// the other chunk only takes it where it has air
|
|
|
|
@@ -247,7 +247,15 @@ export function register_trees(gen: WorldgenContext) {
|
|
|
|
|
const kind = here.kinds[Math.floor(site.rng.next() * here.kinds.length)] as TreeKind;
|
|
|
|
|
// big oaks grow oak, every other kind its own wood, unless the biome says
|
|
|
|
|
const wood = here.wood[kind] ?? (kind === "big_oak" ? "oak" : kind);
|
|
|
|
|
grow_tree(kind, site.x, ground_y + 1, site.z, site.rng, (x, y, z, block) => place(x, y, z, block, wood));
|
|
|
|
|
grow_tree(
|
|
|
|
|
kind,
|
|
|
|
|
site.x,
|
|
|
|
|
ground_y + 1,
|
|
|
|
|
site.z,
|
|
|
|
|
chunk.max_y,
|
|
|
|
|
site.rng,
|
|
|
|
|
(x, y, z, block) => place(x, y, z, block, wood),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}, { step: "vegetal_decoration" });
|
|
|
|
|
}
|
|
|
|
|