Biome as data

This commit is contained in:
2026-10-08 13:34:32 -03:00
parent 9f0b049148
commit 7e7ac8f6f4
49 changed files with 913 additions and 224 deletions
+42 -74
View File
@@ -3,7 +3,8 @@
import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE, SEA_LEVEL } from "$/common/constants.ts";
import { OctaveNoise2D } from "./noise.ts";
import { OverworldTerrain, TerrainColumn } from "./terrain.ts";
import { BIOME_TREES, grow_tree, tree_sites, tree_wood, type Wood } from "./trees.ts";
import { grow_tree, tree_sites, type Wood } from "./trees.ts";
import { type Biome, get_biome, tree_wood } from "./biomes.ts";
// density is sampled every CELL_WIDTH blocks across and CELL_HEIGHT up, caves every CAVE_CELL_HEIGHT up since
// tunnels are thinner than terrain features
@@ -26,83 +27,24 @@ export interface OverworldBlocks {
stone: number;
dirt: number;
grass: number;
sand: number;
snow: number;
water: number;
logs: Record<Wood, number>;
leaves: Record<Wood, number>;
// any other block, for biomes' surfaces
block(id: string): number;
}
type Palette = "stone" | "dirt" | "grass" | "sand" | "snow";
// how a biome covers its ground, like minecraft's surface rules
// a biome's surface, as numeric ids
interface Surface {
top: Palette;
filler: Palette;
top: number;
filler: number;
filler_depth: number;
// the ground's cover under water
underwater_top: Palette;
underwater_filler: Palette;
// cliffs show bare stone instead of top
underwater_top: number;
underwater_filler: number;
bare_cliffs: boolean;
}
const GRASSY: Surface = {
top: "grass",
filler: "dirt",
filler_depth: 3,
underwater_top: "dirt",
underwater_filler: "dirt",
bare_cliffs: true,
};
const SANDY: Surface = {
top: "sand",
filler: "sand",
filler_depth: 4,
underwater_top: "sand",
underwater_filler: "sand",
bare_cliffs: false,
};
const SNOWY: Surface = { ...GRASSY, top: "snow" };
const STONY: Surface = {
...GRASSY,
top: "stone",
filler: "stone",
underwater_top: "stone",
underwater_filler: "stone",
};
const SEA_FLOOR: Surface = { ...SANDY, filler_depth: 3 };
// every biome the overworld has, and its surface. names follow minecraft's
export const BIOMES: Record<string, Surface> = {
"bworld:deep_ocean": SEA_FLOOR,
"bworld:deep_frozen_ocean": SEA_FLOOR,
"bworld:deep_lukewarm_ocean": SEA_FLOOR,
"bworld:ocean": SEA_FLOOR,
"bworld:frozen_ocean": SEA_FLOOR,
"bworld:warm_ocean": SEA_FLOOR,
"bworld:river": { ...GRASSY, underwater_top: "sand", underwater_filler: "sand" },
"bworld:frozen_river": { ...SNOWY, underwater_top: "sand", underwater_filler: "sand" },
"bworld:beach": SANDY,
"bworld:snowy_beach": { ...SANDY, top: "snow" },
"bworld:stony_shore": STONY,
"bworld:plains": GRASSY,
"bworld:meadow": GRASSY,
"bworld:forest": GRASSY,
"bworld:dark_forest": GRASSY,
"bworld:swamp": { ...GRASSY, bare_cliffs: false },
"bworld:taiga": GRASSY,
"bworld:snowy_plains": SNOWY,
"bworld:snowy_taiga": SNOWY,
"bworld:savanna": GRASSY,
"bworld:jungle": GRASSY,
"bworld:desert": SANDY,
"bworld:snowy_slopes": SNOWY,
"bworld:stony_peaks": STONY,
"bworld:jagged_peaks": { ...STONY, top: "snow" },
"bworld:frozen_peaks": { ...SNOWY, filler: "stone" },
};
// picks a column's biome from its climate and shape, like minecraft's multi noise biome source
export function pick_biome(column: TerrainColumn, surface_y: number, steep: boolean): string {
const { continentalness: c, erosion, ridges, temperature: t, humidity: h } = column.climate;
@@ -230,6 +172,31 @@ export function generate_overworld(
}
const column_at = (x: number, z: number) => columns[(z + 1) * ring + x + 1];
// biomes are registered by mods (bworld's are in mods/bworld/worldgen/biomes), with their surfaces' ids looked up
// once per chunk
const found = new Map<string, { biome: Biome; surface: Surface }>();
const biome_info = (id: string) => {
let info = found.get(id);
if (!info) {
const biome = get_biome(id);
if (!biome) throw new Error(`biome ${id} isn't registered, is the bworld mod loaded?`);
const { surface } = biome;
info = {
biome,
surface: {
top: ids.block(surface.top),
filler: ids.block(surface.filler),
filler_depth: surface.filler_depth,
underwater_top: ids.block(surface.underwater_top),
underwater_filler: ids.block(surface.underwater_filler),
bare_cliffs: surface.bare_cliffs,
},
};
found.set(id, info);
}
return info;
};
for (let z = 0; z < CHUNK_SIZE; z++) {
for (let x = 0; x < CHUNK_SIZE; x++) {
fill_column(x, z);
@@ -259,7 +226,8 @@ export function generate_overworld(
for (const site of tree_sites(seed, chunk_x, chunk_z)) {
const column = (site.z - z0) * CHUNK_SIZE + (site.x - x0);
const trees = BIOME_TREES[biomes[column]];
const { biome } = biome_info(biomes[column]);
const trees = biome.trees;
if (!trees || site.rng.next() >= trees.chance) continue;
// on soil with air above it, so never under water or on bare rock
@@ -269,7 +237,7 @@ export function generate_overworld(
if ((ground !== ids.grass && ground !== ids.dirt && ground !== ids.snow) || above !== 0) continue;
const kind = trees.kinds[Math.floor(site.rng.next() * trees.kinds.length)];
const wood = tree_wood(kind, biomes[column]);
const wood = tree_wood(kind, biome);
grow_tree(kind, site.x, ground_y + 1, site.z, site.rng, (x, y, z, block) => place(x, y, z, block, wood));
}
}
@@ -315,7 +283,7 @@ export function generate_overworld(
const neighbors = [column_at(x - 1, z), column_at(x + 1, z), column_at(x, z - 1), column_at(x, z + 1)];
const steep = neighbors.some((neighbor) => Math.abs(neighbor.height - ground) >= STEEP);
const biome = pick_biome(column, surface_y, steep);
const surface = BIOMES[biome];
const { surface } = biome_info(biome);
const snow_y = SNOW_LINE + snow_line.sample(x0 + x, z0 + z) * 10;
heights[z * CHUNK_SIZE + x] = surface_y;
@@ -344,17 +312,17 @@ export function generate_overworld(
}
// surface rules reach the ground and anything above it, not cave floors
let block: Palette = "stone";
let block = ids.stone;
if (y >= ground - SURFACE_REACH) {
if (depth === 0) {
if (underwater) block = surface.underwater_top;
else if (y >= snow_y) block = steep ? "stone" : "snow";
else block = steep && surface.bare_cliffs ? "stone" : surface.top;
else if (y >= snow_y) block = steep ? ids.stone : ids.snow;
else block = steep && surface.bare_cliffs ? ids.stone : surface.top;
} else if (depth <= surface.filler_depth) {
block = underwater ? surface.underwater_filler : surface.filler;
}
}
blocks[index(y)] = ids[block];
blocks[index(y)] = block;
}
}
}