Files
2026-11-13 22:55:30 -03:00

101 lines
3.8 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// biomes as data: worldgen/biomes/<name>.json in a mod, see docs/modding/worldgen/biomes.md. which biome a column
// gets is the generator's biome rules (worldgen/biome_source), these say what a biome looks like once it's picked
import * as z from "zod";
import { EverythingRegistry } from "../everything_registry.ts";
import { color, find_problems, id, type Problems } from "../schema.ts";
import { farming_day } from "../time.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
const surface_schema = z.object({
top: id,
filler: id,
filler_depth: z.int().min(0).max(32),
// the ground's cover under water, filler when not set
underwater_top: id.optional(),
underwater_filler: id.optional(),
// cliffs show bare stone instead of top
bare_cliffs: z.boolean().optional(),
});
export type SurfaceJson = z.infer<typeof surface_schema>;
const biome_schema = z.object({
id,
// a season it's always in, like winter for snowy biomes. when not set it follows the calendar, see calendar_season
season: z.enum(SEASONS).optional(),
surface: surface_schema,
// minecraft's grass, foliage and water colors, which blocks pick with "tint". plains' when not set
colors: z.strictObject({ grass: color.optional(), foliage: color.optional(), water: color.optional() }).optional(),
});
export type BiomeJson = z.infer<typeof biome_schema>;
// 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 };
}
export function biome_from_json(json: BiomeJson): Biome {
const { surface, colors = {} } = 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) },
};
}
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 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): Problems {
return find_problems(biome_schema, json);
}