Files
bworld/common/worldgen/biomes.ts
T
2026-10-09 22:09:21 -03:00

186 lines
7.2 KiB
TypeScript

// biomes as data: worldgen/biomes/<name>.json in a mod, see docs/modding/worldgen/biomes.md. which biome a column
// gets is still code (pick_biome in overworld.ts), these say what a biome looks like once it's picked
import { EverythingRegistry } from "../everything_registry.ts";
import { ID_PATTERN } from "../mod_data.ts";
import { farming_day } from "../time.ts";
import { TREE_KINDS, type TreeKind, type Wood, WOODS } from "./trees.ts";
export const SEASONS = ["spring", "summer", "autumn", "winter"] as const;
export type Season = typeof SEASONS[number];
// how long each season lasts. day 1 to 49 is spring, 50 to 98 summer and so on, a year is 196 days
export const SEASON_DAYS = 49;
// how a biome covers its ground, like minecraft's surface rules. blocks are ids
export interface SurfaceJson {
top: string;
filler: string;
filler_depth: number;
// the ground's cover under water, filler when not set
underwater_top?: string;
underwater_filler?: string;
// cliffs show bare stone instead of top
bare_cliffs?: boolean;
}
export interface BiomeJson {
id: string;
// a season it's always in, like winter for snowy biomes. when not set it follows the calendar, see calendar_season
season?: Season;
surface: SurfaceJson;
// "#rrggbb", minecraft's grass, foliage and water colors, which blocks pick with "tint". plains' when not set
colors?: { grass?: string; foliage?: string; water?: string };
// how likely each tree cell is to have a tree, and which kinds grow, picked evenly (list one twice to make it
// twice as likely). wood is the logs and leaves a kind grows here when it isn't its own, like big oaks being dark
// oak in the dark forest
trees?: { chance: number; kinds: TreeKind[]; wood?: Partial<Record<TreeKind, Wood>> };
}
// what's registered in "biomes", with the defaults filled in
export interface Biome {
id: string;
season?: Season;
surface: Required<SurfaceJson>;
// 0xRRGGBB
colors: { grass?: number; foliage?: number; water?: number };
trees?: { chance: number; kinds: TreeKind[]; wood: Partial<Record<TreeKind, Wood>> };
}
export function biome_from_json(json: BiomeJson): Biome {
const { surface, colors = {}, trees } = json;
const color = (value: string | undefined) => value === undefined ? undefined : parseInt(value.slice(1), 16);
return {
id: json.id,
season: json.season,
surface: {
top: surface.top,
filler: surface.filler,
filler_depth: surface.filler_depth,
underwater_top: surface.underwater_top ?? surface.filler,
underwater_filler: surface.underwater_filler ?? surface.filler,
bare_cliffs: surface.bare_cliffs ?? false,
},
colors: { grass: color(colors.grass), foliage: color(colors.foliage), water: color(colors.water) },
trees: trees && { chance: trees.chance, kinds: trees.kinds, wood: trees.wood ?? {} },
};
}
export function register_biome(json: BiomeJson) {
EverythingRegistry.register<Biome>("biomes", json.id, biome_from_json(json));
}
export function get_biome(id: string): Biome | undefined {
return EverythingRegistry.get<Biome>("biomes", id);
}
export function biome_ids(): string[] {
return EverythingRegistry.entries<Biome>("biomes").map(([id]) => id);
}
// the season on a farming day (see farming_day in common/time.ts), which is 0 on the first day and goes up at 06:00
export function calendar_season(day: number): Season {
const days_into_year = (day % (SEASON_DAYS * 4) + SEASON_DAYS * 4) % (SEASON_DAYS * 4);
return SEASONS[Math.floor(days_into_year / SEASON_DAYS)];
}
// a biome's season at a world time: its own when its json sets one, the calendar's otherwise, like for one that
// isn't registered (a removed mod's)
export function biome_season(id: string, time: number): Season {
return get_biome(id)?.season ?? calendar_season(farming_day(time));
}
// the logs and leaves a kind of tree grows in a biome
export function tree_wood(kind: TreeKind, biome: Biome): Wood {
const wood = biome.trees?.wood[kind];
if (wood) return wood;
return kind === "big_oak" ? "oak" : kind;
}
// the block ids a biome's json uses, for checking they exist
export function biome_blocks(json: BiomeJson): string[] {
const { top, filler, underwater_top, underwater_filler } = json.surface;
return [top, filler, underwater_top, underwater_filler].filter((id) => id !== undefined);
}
export function validate_biome(json: unknown): string[] {
if (!is_object(json)) return ["biome must be an object"];
const problems: string[] = [];
const id = (value: unknown, field: string) => {
if (!(typeof value === "string" && ID_PATTERN.test(value))) {
problems.push(`${field} must be an id like "my_mod:thing"`);
}
};
id(json.id, "id");
if (json.season !== undefined && !SEASONS.includes(json.season as Season)) {
problems.push(`season must be one of ${SEASONS.join(", ")}`);
}
const surface = json.surface;
if (!is_object(surface)) {
problems.push("surface must be { top, filler, filler_depth }");
} else {
id(surface.top, "surface.top");
id(surface.filler, "surface.filler");
if (surface.underwater_top !== undefined) id(surface.underwater_top, "surface.underwater_top");
if (surface.underwater_filler !== undefined) id(surface.underwater_filler, "surface.underwater_filler");
const depth = surface.filler_depth;
if (!Number.isInteger(depth) || (depth as number) < 0 || (depth as number) > 32) {
problems.push("surface.filler_depth must be a whole number from 0 to 32");
}
if (surface.bare_cliffs !== undefined && typeof surface.bare_cliffs !== "boolean") {
problems.push("surface.bare_cliffs must be true or false");
}
}
if (json.colors !== undefined) {
if (!is_object(json.colors)) {
problems.push("colors must be { grass, foliage, water }");
} else {
for (const [key, value] of Object.entries(json.colors)) {
if (!["grass", "foliage", "water"].includes(key)) problems.push(`colors.${key} isn't a biome color`);
else if (!(typeof value === "string" && /^#[0-9a-fA-F]{6}$/.test(value))) {
problems.push(`colors.${key} must be a color like "#79c05a"`);
}
}
}
}
const trees = json.trees;
if (trees !== undefined) {
if (!is_object(trees)) {
problems.push("trees must be { chance, kinds }");
} else {
if (typeof trees.chance !== "number" || trees.chance < 0 || trees.chance > 1) {
problems.push("trees.chance must be a number from 0 to 1");
}
if (!Array.isArray(trees.kinds) || trees.kinds.length === 0) {
problems.push("trees.kinds must list at least one kind of tree");
} else {
for (const kind of trees.kinds) {
if (!TREE_KINDS.includes(kind)) {
problems.push(`trees.kinds: ${kind} isn't one of ${TREE_KINDS.join(", ")}`);
}
}
}
if (trees.wood !== undefined) {
if (!is_object(trees.wood)) {
problems.push("trees.wood must be { <kind>: <wood> }");
} else {
for (const [kind, wood] of Object.entries(trees.wood)) {
if (!TREE_KINDS.includes(kind as TreeKind)) {
problems.push(`trees.wood: ${kind} isn't a kind of tree`);
}
if (!WOODS.includes(wood as Wood)) {
problems.push(`trees.wood.${kind} must be one of ${WOODS.join(", ")}`);
}
}
}
}
}
}
return problems;
}
function is_object(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}