50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
// the base game's features, on top of the terrain (which is data, worldgen/). runs on the server and must give the
|
|
// same world every time: only chunk.rng, chunk.random and the noise helpers, never Math.random or the time
|
|
import type { FeatureChunk, WorldgenContext } from "bworld/worldgen";
|
|
import { register_trees } from "./trees.ts";
|
|
|
|
export function setup(gen: WorldgenContext) {
|
|
// trees grow in vegetal_decoration, before the cactus looks for air in the last step
|
|
register_trees(gen);
|
|
gen.register_feature("bworld:cactus", cactus);
|
|
}
|
|
|
|
// how tall a cactus grows, and how likely each height is
|
|
const CACTUS_HEIGHTS = [
|
|
{ height: 1, chance: 0.65 },
|
|
{ height: 2, chance: 0.25 },
|
|
{ height: 3, chance: 0.1 },
|
|
];
|
|
|
|
// 2 or 3 tries a chunk, each a cactus where it lands on desert sand with room above
|
|
function cactus(chunk: FeatureChunk) {
|
|
const tries = chunk.rng.next() < 0.5 ? 2 : 3;
|
|
for (let i = 0; i < tries; i++) {
|
|
// every try rolls the same numbers whether or not it places, so one try doesn't move the next
|
|
const x = chunk.x * 16 + Math.floor(chunk.rng.next() * 16);
|
|
const z = chunk.z * 16 + Math.floor(chunk.rng.next() * 16);
|
|
const height = pick_height(chunk.rng.next());
|
|
if (chunk.biome_at(x, z) !== "bworld:desert") {
|
|
continue;
|
|
}
|
|
const ground = chunk.height_at(x, z);
|
|
if (chunk.get_block(x, ground, z) !== "bworld:sand") {
|
|
continue;
|
|
}
|
|
for (let y = ground + 1; y <= ground + height; y++) {
|
|
if (chunk.get_block(x, y, z) !== "bworld:air") {
|
|
break;
|
|
}
|
|
chunk.set_block(x, y, z, "bworld:cactus");
|
|
}
|
|
}
|
|
}
|
|
|
|
function pick_height(roll: number) {
|
|
for (const { height, chance } of CACTUS_HEIGHTS) {
|
|
if (roll < chance) return height;
|
|
roll -= chance;
|
|
}
|
|
return CACTUS_HEIGHTS.at(-1)!.height;
|
|
}
|