Move trees out of cubi
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// the base game's trees, a feature that's code. biomes say which kinds grow and how often (their json's "trees"), this
|
||||
// the base game's trees, a feature that's code. BIOME_TREES says which kinds grow in each biome and how often, the rest
|
||||
// says what the kinds are. it isn't data features: each kind would need placers drawing random numbers in exactly
|
||||
// the order grow_tree does, for the world to stay the same, to say what this already says. it runs after ores, which
|
||||
// only replace stone, while trees only fill air or replace leaves, so that order changes nothing. where they grow can't depend on which chunk generates first, so it only depends on the
|
||||
@@ -9,7 +9,7 @@
|
||||
// other candidate within MIN_DISTANCE has a higher priority (then its biome may still say no). that spreads trees out
|
||||
// like poisson disk sampling: never closer than MIN_DISTANCE, but without lining up in a grid. a tree is placed by the
|
||||
// chunk its trunk is in, leaves that reach into the next chunk only fill air there
|
||||
import type { FeatureBiome, FeatureChunk, WorldgenContext } from "cubi/worldgen";
|
||||
import type { FeatureChunk, WorldgenContext } from "cubi/worldgen";
|
||||
|
||||
const CHUNK_SIZE = 16;
|
||||
|
||||
@@ -26,6 +26,36 @@ export type TreeKind = typeof TREE_KINDS[number];
|
||||
export const WOODS = ["oak", "spruce", "birch", "jungle", "acacia", "dark_oak", "cherry"] as const;
|
||||
export type Wood = typeof WOODS[number];
|
||||
|
||||
export interface BiomeTrees {
|
||||
// how likely each cell's spot is to get a tree, 0 to 1
|
||||
chance: number;
|
||||
// picked evenly, so one listed twice is twice as likely
|
||||
kinds: TreeKind[];
|
||||
// the logs and leaves a kind grows here when it isn't its own, like big oaks being dark oak in the dark forest
|
||||
wood?: Partial<Record<TreeKind, Wood>>;
|
||||
}
|
||||
|
||||
// biomes not listed have no trees
|
||||
export const BIOME_TREES: Record<string, BiomeTrees> = {
|
||||
"bworld:dark_forest": { chance: 1, kinds: ["big_oak", "big_oak", "oak"], wood: { big_oak: "dark_oak" } },
|
||||
"bworld:forest": { chance: 0.85, kinds: ["oak", "oak", "big_oak", "birch"] },
|
||||
"bworld:jungle": { chance: 1, kinds: ["jungle", "jungle", "big_oak"] },
|
||||
"bworld:meadow": { chance: 0.06, kinds: ["oak", "birch", "cherry", "cherry"] },
|
||||
"bworld:plains": { chance: 0.05, kinds: ["oak"] },
|
||||
"bworld:river": { chance: 0.05, kinds: ["oak"] },
|
||||
"bworld:savanna": { chance: 0.15, kinds: ["acacia", "acacia", "oak"] },
|
||||
"bworld:snowy_plains": { chance: 0.03, kinds: ["spruce"] },
|
||||
"bworld:snowy_slopes": { chance: 0.06, kinds: ["spruce"] },
|
||||
"bworld:snowy_taiga": { chance: 0.6, kinds: ["spruce"] },
|
||||
"bworld:swamp": { chance: 0.4, kinds: ["big_oak"] },
|
||||
"bworld:taiga": { chance: 0.75, kinds: ["spruce"] },
|
||||
};
|
||||
|
||||
// the wood a kind grows in a biome: its own, big oaks' is oak, unless the biome says
|
||||
export function wood_of(trees: BiomeTrees, kind: TreeKind): Wood {
|
||||
return trees.wood?.[kind] ?? (kind === "big_oak" ? "oak" : kind);
|
||||
}
|
||||
|
||||
type Random = { next(): number };
|
||||
|
||||
export interface TreeSite {
|
||||
@@ -200,25 +230,6 @@ const SOIL = new Set(["bworld:grass_block", "bworld:dirt", "bworld:snow_block"])
|
||||
const LEAVES = new Set(WOODS.map((wood) => `bworld:${wood}_leaves`));
|
||||
|
||||
export function register_trees(gen: WorldgenContext) {
|
||||
const trees = new Map<string, NonNullable<FeatureBiome["trees"]>>();
|
||||
for (const biome of gen.biomes()) {
|
||||
if (!biome.trees) continue;
|
||||
for (const kind of biome.trees.kinds) {
|
||||
if (!TREE_KINDS.includes(kind as TreeKind)) {
|
||||
throw new Error(`biome ${biome.id}: trees.kinds: ${kind} isn't one of ${TREE_KINDS.join(", ")}`);
|
||||
}
|
||||
}
|
||||
for (const [kind, wood] of Object.entries(biome.trees.wood)) {
|
||||
if (!TREE_KINDS.includes(kind as TreeKind)) {
|
||||
throw new Error(`biome ${biome.id}: trees.wood: ${kind} isn't a kind of tree`);
|
||||
}
|
||||
if (!WOODS.includes(wood as Wood)) {
|
||||
throw new Error(`biome ${biome.id}: trees.wood.${kind} must be one of ${WOODS.join(", ")}`);
|
||||
}
|
||||
}
|
||||
trees.set(biome.id, biome.trees);
|
||||
}
|
||||
|
||||
gen.register_feature("bworld:trees", (chunk) => {
|
||||
const x0 = chunk.x * CHUNK_SIZE;
|
||||
const z0 = chunk.z * CHUNK_SIZE;
|
||||
@@ -237,7 +248,7 @@ export function register_trees(gen: WorldgenContext) {
|
||||
for (const site of tree_sites(chunk)) {
|
||||
// the biome of the ground it grows from
|
||||
const ground_y = chunk.height_at(site.x, site.z);
|
||||
const here = trees.get(chunk.biome_at(site.x, ground_y, site.z));
|
||||
const here = BIOME_TREES[chunk.biome_at(site.x, ground_y, site.z)];
|
||||
if (!here || site.rng.next() >= here.chance) continue;
|
||||
|
||||
// on soil with air above it, so never under water or on bare rock
|
||||
@@ -245,9 +256,8 @@ export function register_trees(gen: WorldgenContext) {
|
||||
const above = chunk.get_block(site.x, ground_y + 1, site.z);
|
||||
if (!SOIL.has(ground!) || above !== "cubi:air") continue;
|
||||
|
||||
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);
|
||||
const kind = here.kinds[Math.floor(site.rng.next() * here.kinds.length)];
|
||||
const wood = wood_of(here, kind);
|
||||
grow_tree(
|
||||
kind,
|
||||
site.x,
|
||||
|
||||
@@ -12,17 +12,6 @@
|
||||
"grass": "#507a32",
|
||||
"foliage": "#59ae30",
|
||||
"water": "#3f76e4"
|
||||
},
|
||||
"trees": {
|
||||
"chance": 1,
|
||||
"kinds": [
|
||||
"big_oak",
|
||||
"big_oak",
|
||||
"oak"
|
||||
],
|
||||
"wood": {
|
||||
"big_oak": "dark_oak"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,15 +12,6 @@
|
||||
"grass": "#79c05a",
|
||||
"foliage": "#59ae30",
|
||||
"water": "#3f76e4"
|
||||
},
|
||||
"trees": {
|
||||
"chance": 0.85,
|
||||
"kinds": [
|
||||
"oak",
|
||||
"oak",
|
||||
"big_oak",
|
||||
"birch"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,14 +12,6 @@
|
||||
"grass": "#59c93c",
|
||||
"foliage": "#30bb0b",
|
||||
"water": "#3f76e4"
|
||||
},
|
||||
"trees": {
|
||||
"chance": 1,
|
||||
"kinds": [
|
||||
"jungle",
|
||||
"jungle",
|
||||
"big_oak"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,15 +12,6 @@
|
||||
"grass": "#83bb6d",
|
||||
"foliage": "#63a948",
|
||||
"water": "#0e4ecf"
|
||||
},
|
||||
"trees": {
|
||||
"chance": 0.06,
|
||||
"kinds": [
|
||||
"oak",
|
||||
"birch",
|
||||
"cherry",
|
||||
"cherry"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,12 +12,6 @@
|
||||
"grass": "#91bd59",
|
||||
"foliage": "#77ab2f",
|
||||
"water": "#3f76e4"
|
||||
},
|
||||
"trees": {
|
||||
"chance": 0.05,
|
||||
"kinds": [
|
||||
"oak"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,12 +14,6 @@
|
||||
"grass": "#8eb971",
|
||||
"foliage": "#71a74d",
|
||||
"water": "#3f76e4"
|
||||
},
|
||||
"trees": {
|
||||
"chance": 0.05,
|
||||
"kinds": [
|
||||
"oak"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,14 +12,6 @@
|
||||
"grass": "#bfb755",
|
||||
"foliage": "#aea42a",
|
||||
"water": "#3f76e4"
|
||||
},
|
||||
"trees": {
|
||||
"chance": 0.15,
|
||||
"kinds": [
|
||||
"acacia",
|
||||
"acacia",
|
||||
"oak"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,12 +13,6 @@
|
||||
"foliage": "#60a17b",
|
||||
"water": "#3f76e4"
|
||||
},
|
||||
"trees": {
|
||||
"chance": 0.03,
|
||||
"kinds": [
|
||||
"spruce"
|
||||
]
|
||||
},
|
||||
"season": "winter"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,12 +13,6 @@
|
||||
"foliage": "#60a17b",
|
||||
"water": "#3f76e4"
|
||||
},
|
||||
"trees": {
|
||||
"chance": 0.06,
|
||||
"kinds": [
|
||||
"spruce"
|
||||
]
|
||||
},
|
||||
"season": "winter"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,12 +13,6 @@
|
||||
"foliage": "#60a17b",
|
||||
"water": "#3d57d6"
|
||||
},
|
||||
"trees": {
|
||||
"chance": 0.6,
|
||||
"kinds": [
|
||||
"spruce"
|
||||
]
|
||||
},
|
||||
"season": "winter"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,12 +11,6 @@
|
||||
"grass": "#6a7039",
|
||||
"foliage": "#6a7039",
|
||||
"water": "#617b64"
|
||||
},
|
||||
"trees": {
|
||||
"chance": 0.4,
|
||||
"kinds": [
|
||||
"big_oak"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,12 +12,6 @@
|
||||
"grass": "#86b783",
|
||||
"foliage": "#68a464",
|
||||
"water": "#3f76e4"
|
||||
},
|
||||
"trees": {
|
||||
"chance": 0.75,
|
||||
"kinds": [
|
||||
"spruce"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user