Make cactus generare in deserts
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
"scripts": {
|
||||
"server": "scripts/server.ts",
|
||||
"client": "scripts/client.ts",
|
||||
"shared": "scripts/shared.ts"
|
||||
"shared": "scripts/shared.ts",
|
||||
"worldgen": "scripts/worldgen.ts"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
// the base game's features, on top of the engine's terrain. runs in chunk workers and must give the same world every
|
||||
// time: only chunk.rng and the noise helpers, never Math.random or the time
|
||||
import type { FeatureChunk, WorldgenContext } from "bworld/worldgen";
|
||||
|
||||
export function setup(gen: WorldgenContext) {
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user