177 lines
6.6 KiB
TypeScript
177 lines
6.6 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 { 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];
|
|
export const DEFAULT_SEASON: Season = "spring";
|
|
|
|
// 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;
|
|
// spring when not set
|
|
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 ?? DEFAULT_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);
|
|
}
|
|
|
|
// a biome's season, spring for one that isn't registered, like a removed mod's
|
|
export function biome_season(id: string): Season {
|
|
return get_biome(id)?.season ?? DEFAULT_SEASON;
|
|
}
|
|
|
|
// 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);
|
|
}
|