Move world generation outside the engine and into mods and data files
This commit is contained in:
+11
-29
@@ -1,9 +1,8 @@
|
||||
// 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
|
||||
// gets is the generator's biome rules (worldgen/biome_source), 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];
|
||||
@@ -31,8 +30,9 @@ export interface BiomeJson {
|
||||
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>> };
|
||||
// oak in the dark forest. the trees feature reads it (bworld's, in its worldgen script), which says what kinds
|
||||
// and woods there are
|
||||
trees?: { chance: number; kinds: string[]; wood?: Record<string, string> };
|
||||
}
|
||||
|
||||
// what's registered in "biomes", with the defaults filled in
|
||||
@@ -42,7 +42,7 @@ export interface Biome {
|
||||
surface: Required<SurfaceJson>;
|
||||
// 0xRRGGBB
|
||||
colors: { grass?: number; foliage?: number; water?: number };
|
||||
trees?: { chance: number; kinds: TreeKind[]; wood: Partial<Record<TreeKind, Wood>> };
|
||||
trees?: { chance: number; kinds: string[]; wood: Record<string, string> };
|
||||
}
|
||||
|
||||
export function biome_from_json(json: BiomeJson): Biome {
|
||||
@@ -88,13 +88,6 @@ 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;
|
||||
@@ -152,27 +145,16 @@ export function validate_biome(json: unknown): string[] {
|
||||
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) {
|
||||
// which kinds and woods there are is up to the trees feature, bworld's checks them when it loads
|
||||
if (
|
||||
!Array.isArray(trees.kinds) || trees.kinds.length === 0 ||
|
||||
!trees.kinds.every((kind) => typeof kind === "string")
|
||||
) {
|
||||
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)) {
|
||||
if (!is_object(trees.wood) || !Object.values(trees.wood).every((wood) => typeof wood === "string")) {
|
||||
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(", ")}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user