From 7e7ac8f6f4db2adfbaca007a62814783e015d83f Mon Sep 17 00:00:00 2001 From: Paula Date: Thu, 8 Oct 2026 13:34:32 -0300 Subject: [PATCH] Biome as data --- MODS.md | 1 + STRUCTURES.md | 4 +- common/bmod.ts | 7 +- common/engine_modules.ts | 2 - common/generation.ts | 2 +- common/mod_check.ts | 15 +- common/mod_loader.ts | 6 + common/worldgen/biome_colors.ts | 105 +---------- common/worldgen/biomes.ts | 171 ++++++++++++++++++ common/worldgen/overworld.ts | 116 +++++------- common/worldgen/trees.ts | 35 +--- docs/modding/gettingstarted/modfiles.md | 1 + docs/modding/worldgen/biomes.md | 72 ++++++++ docs/modding/worldgen/index.md | 2 +- mods/bworld/worldgen/biomes/beach.json | 16 ++ mods/bworld/worldgen/biomes/dark_forest.json | 28 +++ .../worldgen/biomes/deep_frozen_ocean.json | 16 ++ .../worldgen/biomes/deep_lukewarm_ocean.json | 16 ++ mods/bworld/worldgen/biomes/deep_ocean.json | 16 ++ mods/bworld/worldgen/biomes/desert.json | 16 ++ mods/bworld/worldgen/biomes/forest.json | 26 +++ mods/bworld/worldgen/biomes/frozen_ocean.json | 16 ++ mods/bworld/worldgen/biomes/frozen_peaks.json | 19 ++ mods/bworld/worldgen/biomes/frozen_river.json | 19 ++ mods/bworld/worldgen/biomes/jagged_peaks.json | 17 ++ mods/bworld/worldgen/biomes/jungle.json | 25 +++ mods/bworld/worldgen/biomes/meadow.json | 26 +++ mods/bworld/worldgen/biomes/ocean.json | 16 ++ mods/bworld/worldgen/biomes/plains.json | 23 +++ mods/bworld/worldgen/biomes/river.json | 25 +++ mods/bworld/worldgen/biomes/savanna.json | 25 +++ mods/bworld/worldgen/biomes/snowy_beach.json | 16 ++ mods/bworld/worldgen/biomes/snowy_plains.json | 23 +++ mods/bworld/worldgen/biomes/snowy_slopes.json | 23 +++ mods/bworld/worldgen/biomes/snowy_taiga.json | 23 +++ mods/bworld/worldgen/biomes/stony_peaks.json | 17 ++ mods/bworld/worldgen/biomes/stony_shore.json | 17 ++ mods/bworld/worldgen/biomes/swamp.json | 22 +++ mods/bworld/worldgen/biomes/taiga.json | 23 +++ mods/bworld/worldgen/biomes/warm_ocean.json | 16 ++ server/game/game_server.ts | 9 +- server/load_bmods.ts | 1 + tests/biome_test.ts | 37 ++++ tests/helpers.ts | 1 + tests/tint_test.ts | 9 +- tests/wood_test.ts | 5 +- tools/check_mods.ts | 3 + tools/pack_mod.ts | 1 + tools/worldgen_preview.ts | 7 + 49 files changed, 913 insertions(+), 224 deletions(-) create mode 100644 common/worldgen/biomes.ts create mode 100644 docs/modding/worldgen/biomes.md create mode 100644 mods/bworld/worldgen/biomes/beach.json create mode 100644 mods/bworld/worldgen/biomes/dark_forest.json create mode 100644 mods/bworld/worldgen/biomes/deep_frozen_ocean.json create mode 100644 mods/bworld/worldgen/biomes/deep_lukewarm_ocean.json create mode 100644 mods/bworld/worldgen/biomes/deep_ocean.json create mode 100644 mods/bworld/worldgen/biomes/desert.json create mode 100644 mods/bworld/worldgen/biomes/forest.json create mode 100644 mods/bworld/worldgen/biomes/frozen_ocean.json create mode 100644 mods/bworld/worldgen/biomes/frozen_peaks.json create mode 100644 mods/bworld/worldgen/biomes/frozen_river.json create mode 100644 mods/bworld/worldgen/biomes/jagged_peaks.json create mode 100644 mods/bworld/worldgen/biomes/jungle.json create mode 100644 mods/bworld/worldgen/biomes/meadow.json create mode 100644 mods/bworld/worldgen/biomes/ocean.json create mode 100644 mods/bworld/worldgen/biomes/plains.json create mode 100644 mods/bworld/worldgen/biomes/river.json create mode 100644 mods/bworld/worldgen/biomes/savanna.json create mode 100644 mods/bworld/worldgen/biomes/snowy_beach.json create mode 100644 mods/bworld/worldgen/biomes/snowy_plains.json create mode 100644 mods/bworld/worldgen/biomes/snowy_slopes.json create mode 100644 mods/bworld/worldgen/biomes/snowy_taiga.json create mode 100644 mods/bworld/worldgen/biomes/stony_peaks.json create mode 100644 mods/bworld/worldgen/biomes/stony_shore.json create mode 100644 mods/bworld/worldgen/biomes/swamp.json create mode 100644 mods/bworld/worldgen/biomes/taiga.json create mode 100644 mods/bworld/worldgen/biomes/warm_ocean.json create mode 100644 tests/biome_test.ts diff --git a/MODS.md b/MODS.md index 4f08165..8d07d31 100644 --- a/MODS.md +++ b/MODS.md @@ -66,6 +66,7 @@ deno task build && deno task server # packs the mods and starts a se **Everything else** - [World Generation](docs/modding/worldgen/index.md): ores and features +- [Biomes](docs/modding/worldgen/biomes.md): surfaces, colors, trees and seasons - [Screens](docs/modding/gui/index.md): plain screens, inventory overlays and drawing with `Graphics` - [Networking](docs/modding/networking/index.md): how players get mods and the world - [Saving Data](docs/modding/datastorage/index.md): where a mod keeps what should last diff --git a/STRUCTURES.md b/STRUCTURES.md index e844b01..28c4cc3 100644 --- a/STRUCTURES.md +++ b/STRUCTURES.md @@ -23,8 +23,8 @@ What world generation has today (see [World Generation](docs/modding/worldgen/in The biome is exact away from coasts and cliffs. The height is the height before 3D noise, so it isn't the real surface. - **Blocks:** numeric states (up to 16 bits), `variants`, `parts`, shapes, tile data, containers, components and shared - rules. Tags exist for blocks, items and entities. Biomes are engine constants (`BIOMES` in - `common/worldgen/overworld.ts`), not a registry. + rules. Tags exist for blocks, items and entities. Biomes are a registry of data (`worldgen/biomes/` in a mod), but + which one a column gets is still engine code (`pick_biome`). - **Loot:** a block's `drops` is one item id. There are no loot tables. What's missing for structures: diff --git a/common/bmod.ts b/common/bmod.ts index 5568874..a1d8a70 100644 --- a/common/bmod.ts +++ b/common/bmod.ts @@ -29,6 +29,7 @@ import { } from "./mod_data.ts"; import type { ModData } from "./mod_loader.ts"; import { type LootTableJson, validate_loot_table } from "./loot.ts"; +import { type BiomeJson, validate_biome } from "./worldgen/biomes.ts"; import { texture_animation, type TextureJson, validate_texture_json } from "./texture_animation.ts"; import { ENGINE_GLOBAL } from "./hooks.ts"; @@ -64,7 +65,8 @@ export class BmodError extends Error { // the zip's layout: // manifest.json the mod's manifest, with "scripts" and "credits" pointing into the zip -// data.json every block, model, item, recipe, ore, entity, entity model, effect, tag and loot table +// data.json every block, model, item, recipe, ore, entity, entity model, effect, tag, loot table and +// biome // scripts/.js // textures/.png the texture : // textures/.json its animation, if it has one @@ -163,10 +165,11 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod { effects: checked(raw_data.effects, "effects", validate_effect, problems) as EffectJson[], tags: checked(raw_data.tags, "tags", validate_tag, problems) as TagJson[], loot_tables: checked(raw_data.loot_tables, "loot_tables", validate_loot_table, problems) as LootTableJson[], + biomes: checked(raw_data.biomes, "biomes", validate_biome, problems) as BiomeJson[], }; // a mod only registers ids in its own namespace const registered = [...data.blocks, ...data.models, ...data.items, ...data.entities, ...data.entity_models]; - for (const { id } of [...registered, ...data.effects, ...data.loot_tables!]) { + for (const { id } of [...registered, ...data.effects, ...data.loot_tables!, ...data.biomes!]) { if (typeof id === "string" && id.split(":")[0] !== mod) problems.push(`${id} isn't in this mod's namespace`); } diff --git a/common/engine_modules.ts b/common/engine_modules.ts index 64abacf..279d6da 100644 --- a/common/engine_modules.ts +++ b/common/engine_modules.ts @@ -5,7 +5,6 @@ import { BlockRules } from "./block_rules.ts"; import { Tags } from "./tags.ts"; import { CommandDispatcher } from "./commands.ts"; import { OverworldTerrain } from "./worldgen/terrain.ts"; -import { BIOMES } from "./worldgen/overworld.ts"; import { physics } from "./physics.ts"; // built when it's exposed, not when this module loads, so import cycles can't leave anything undefined @@ -19,7 +18,6 @@ export function common_engine() { Tags, CommandDispatcher, OverworldTerrain, - BIOMES, physics, }; } diff --git a/common/generation.ts b/common/generation.ts index 32c4273..fa3fcb9 100644 --- a/common/generation.ts +++ b/common/generation.ts @@ -80,11 +80,11 @@ export function generate_raw_chunk( stone: id("bworld:stone"), dirt: id("bworld:dirt"), grass: id("bworld:grass"), - sand: id("bworld:sand"), snow: id("bworld:snow_block"), water: id("bworld:water"), logs: wood_ids("log"), leaves: wood_ids("leaves"), + block: id, }, (x, y, z, block) => spills.push(x, y, z, block)); generate_ores(blocks, chunk_x, chunk_z, seed, block_ids, BASE_ORES, default_values); diff --git a/common/mod_check.ts b/common/mod_check.ts index b2f0d68..c9a0c5d 100644 --- a/common/mod_check.ts +++ b/common/mod_check.ts @@ -19,7 +19,7 @@ import { BUILTIN_MODELS, type ModelJson } from "./block_models.ts"; import { resolve_tags } from "./tags.ts"; import type { TextureJson } from "./texture_animation.ts"; import { loot_references, type LootTableJson } from "./loot.ts"; -import { BIOMES } from "./worldgen/overworld.ts"; +import { biome_blocks, type BiomeJson } from "./worldgen/biomes.ts"; export interface ModReport { id: string; @@ -43,6 +43,7 @@ export interface LoadedMod { effects: { file: string; json: EffectJson }[]; tags: { file: string; json: TagJson }[]; loot_tables: { file: string; json: LootTableJson }[]; + biomes: { file: string; json: BiomeJson }[]; textures: string[]; // absolute paths by texture id texture_files: Map; @@ -90,6 +91,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable>(); const liquids = new Set(); const loot_tables = new Set(); + const biome_owners = new Map(); const add = (map: Map, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]); const textures = new Set(engine_textures); @@ -113,6 +115,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable 1) { + errors.push(`${file}: biome ${json.id} is also defined by ${biome_owners.get(json.id)!.join(", ")}`); + } + for (const id of biome_blocks(json)) uses(file, id, "block"); + } for (const { file, json } of mod.effects) { if ((effect_owners.get(json.id)?.length ?? 0) > 1) { errors.push(`${file}: effect ${json.id} is also defined by ${effect_owners.get(json.id)!.join(", ")}`); @@ -285,9 +294,9 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable("loot_tables", json.id, json); } + for (const json of data.biomes ?? []) { + register_biome(json); + } } catch (e) { throw new ModLoadError(id, (e as Error).message); } diff --git a/common/worldgen/biome_colors.ts b/common/worldgen/biome_colors.ts index 7f1ab3c..28c8bbc 100644 --- a/common/worldgen/biome_colors.ts +++ b/common/worldgen/biome_colors.ts @@ -1,107 +1,22 @@ -// what color each biome tints grass, leaves and water, as 0xRRGGBB, minecraft's grass, foliage and water colors. -// blocks pick them with "tint" -export const BIOME_GRASS_COLORS: Record = { - "bworld:deep_ocean": 0x8eb971, - "bworld:deep_frozen_ocean": 0x80b497, - "bworld:deep_lukewarm_ocean": 0x8eb971, - "bworld:ocean": 0x8eb971, - "bworld:frozen_ocean": 0x80b497, - "bworld:warm_ocean": 0x8eb971, - "bworld:river": 0x8eb971, - "bworld:frozen_river": 0x80b497, - "bworld:beach": 0x91bd59, - "bworld:snowy_beach": 0x83b593, - "bworld:stony_shore": 0x8ab689, - "bworld:plains": 0x91bd59, - "bworld:meadow": 0x83bb6d, - "bworld:forest": 0x79c05a, - "bworld:dark_forest": 0x507a32, - "bworld:swamp": 0x6a7039, - "bworld:taiga": 0x86b783, - "bworld:snowy_plains": 0x80b497, - "bworld:snowy_taiga": 0x80b497, - "bworld:savanna": 0xbfb755, - "bworld:jungle": 0x59c93c, - "bworld:desert": 0xbfb755, - "bworld:snowy_slopes": 0x80b497, - "bworld:stony_peaks": 0x9abe4b, - "bworld:jagged_peaks": 0x80b497, - "bworld:frozen_peaks": 0x80b497, -}; +// what color each biome tints grass, leaves and water, as 0xRRGGBB, from its "colors" (see biomes.ts). blocks pick +// them with "tint" +import { get_biome } from "./biomes.ts"; -export const BIOME_FOLIAGE_COLORS: Record = { - "bworld:deep_ocean": 0x71a74d, - "bworld:deep_frozen_ocean": 0x60a17b, - "bworld:deep_lukewarm_ocean": 0x71a74d, - "bworld:ocean": 0x71a74d, - "bworld:frozen_ocean": 0x60a17b, - "bworld:warm_ocean": 0x71a74d, - "bworld:river": 0x71a74d, - "bworld:frozen_river": 0x60a17b, - "bworld:beach": 0x77ab2f, - "bworld:snowy_beach": 0x64a278, - "bworld:stony_shore": 0x6da36b, - "bworld:plains": 0x77ab2f, - "bworld:meadow": 0x63a948, - "bworld:forest": 0x59ae30, - "bworld:dark_forest": 0x59ae30, - "bworld:swamp": 0x6a7039, - "bworld:taiga": 0x68a464, - "bworld:snowy_plains": 0x60a17b, - "bworld:snowy_taiga": 0x60a17b, - "bworld:savanna": 0xaea42a, - "bworld:jungle": 0x30bb0b, - "bworld:desert": 0xaea42a, - "bworld:snowy_slopes": 0x60a17b, - "bworld:stony_peaks": 0x82ac1e, - "bworld:jagged_peaks": 0x60a17b, - "bworld:frozen_peaks": 0x60a17b, -}; - -export const BIOME_WATER_COLORS: Record = { - "bworld:deep_ocean": 0x3f76e4, - "bworld:deep_frozen_ocean": 0x3938c9, - "bworld:deep_lukewarm_ocean": 0x45adf2, - "bworld:ocean": 0x3f76e4, - "bworld:frozen_ocean": 0x3938c9, - "bworld:warm_ocean": 0x43d5ee, - "bworld:river": 0x3f76e4, - "bworld:frozen_river": 0x3938c9, - "bworld:beach": 0x3f76e4, - "bworld:snowy_beach": 0x3d57d6, - "bworld:stony_shore": 0x3f76e4, - "bworld:plains": 0x3f76e4, - "bworld:meadow": 0x0e4ecf, - "bworld:forest": 0x3f76e4, - "bworld:dark_forest": 0x3f76e4, - "bworld:swamp": 0x617b64, - "bworld:taiga": 0x3f76e4, - "bworld:snowy_plains": 0x3f76e4, - "bworld:snowy_taiga": 0x3d57d6, - "bworld:savanna": 0x3f76e4, - "bworld:jungle": 0x3f76e4, - "bworld:desert": 0x3f76e4, - "bworld:snowy_slopes": 0x3f76e4, - "bworld:stony_peaks": 0x3f76e4, - "bworld:jagged_peaks": 0x3f76e4, - "bworld:frozen_peaks": 0x3f76e4, -}; - -// biomes mods add without a color, and items, which aren't in any biome -export const DEFAULT_GRASS_COLOR = BIOME_GRASS_COLORS["bworld:plains"]; -export const DEFAULT_FOLIAGE_COLOR = BIOME_FOLIAGE_COLORS["bworld:plains"]; -export const DEFAULT_WATER_COLOR = BIOME_WATER_COLORS["bworld:plains"]; +// biomes without a color, and items, which aren't in any biome: plains' colors +export const DEFAULT_GRASS_COLOR = 0x91bd59; +export const DEFAULT_FOLIAGE_COLOR = 0x77ab2f; +export const DEFAULT_WATER_COLOR = 0x3f76e4; export function grass_color(biome: string | undefined) { - return (biome !== undefined ? BIOME_GRASS_COLORS[biome] : undefined) ?? DEFAULT_GRASS_COLOR; + return (biome !== undefined ? get_biome(biome)?.colors.grass : undefined) ?? DEFAULT_GRASS_COLOR; } export function foliage_color(biome: string | undefined) { - return (biome !== undefined ? BIOME_FOLIAGE_COLORS[biome] : undefined) ?? DEFAULT_FOLIAGE_COLOR; + return (biome !== undefined ? get_biome(biome)?.colors.foliage : undefined) ?? DEFAULT_FOLIAGE_COLOR; } export function water_color(biome: string | undefined) { - return (biome !== undefined ? BIOME_WATER_COLORS[biome] : undefined) ?? DEFAULT_WATER_COLOR; + return (biome !== undefined ? get_biome(biome)?.colors.water : undefined) ?? DEFAULT_WATER_COLOR; } // a block's "tint" color: the biome's grass, foliage or water color, or a fixed one like "#80a755" diff --git a/common/worldgen/biomes.ts b/common/worldgen/biomes.ts new file mode 100644 index 0000000..f9082c6 --- /dev/null +++ b/common/worldgen/biomes.ts @@ -0,0 +1,171 @@ +// biomes as data: worldgen/biomes/.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> }; +} + +// what's registered in "biomes", with the defaults filled in +export interface Biome { + id: string; + season: Season; + surface: Required; + // 0xRRGGBB + colors: { grass?: number; foliage?: number; water?: number }; + trees?: { chance: number; kinds: TreeKind[]; wood: Partial> }; +} + +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("biomes", json.id, biome_from_json(json)); +} + +export function get_biome(id: string): Biome | undefined { + return EverythingRegistry.get("biomes", id); +} + +export function biome_ids(): string[] { + return EverythingRegistry.entries("biomes").map(([id]) => id); +} + +// 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 { : }"); + } 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 { + return typeof value === "object" && value !== null && !Array.isArray(value); +} diff --git a/common/worldgen/overworld.ts b/common/worldgen/overworld.ts index 797aafc..e9c7215 100644 --- a/common/worldgen/overworld.ts +++ b/common/worldgen/overworld.ts @@ -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; leaves: Record; + // 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 = { - "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(); + 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; } } } diff --git a/common/worldgen/trees.ts b/common/worldgen/trees.ts index abb1b6b..ea4d3ac 100644 --- a/common/worldgen/trees.ts +++ b/common/worldgen/trees.ts @@ -15,42 +15,11 @@ const REACH = Math.ceil(MIN_DISTANCE / CELL); // the widest a canopy layer gets const MAX_CANOPY = 3; -export type TreeKind = "oak" | "big_oak" | "birch" | "spruce" | "jungle" | "acacia" | "cherry"; +export const TREE_KINDS = ["oak", "big_oak", "birch", "spruce", "jungle", "acacia", "cherry"] as const; +export type TreeKind = typeof TREE_KINDS[number]; export const WOODS = ["oak", "spruce", "birch", "jungle", "acacia", "dark_oak", "cherry"] as const; export type Wood = typeof WOODS[number]; -// the logs and leaves a tree grows, big oaks are dark oak in the dark forest like minecraft's -export function tree_wood(kind: TreeKind, biome: string): Wood { - switch (kind) { - case "big_oak": - return biome === "bworld:dark_forest" ? "dark_oak" : "oak"; - case "birch": - case "spruce": - case "jungle": - case "acacia": - case "cherry": - return kind; - default: - return "oak"; - } -} - -// how likely each biome's cells are to have a tree, and which kinds grow there -export const BIOME_TREES: Record = { - "bworld:forest": { chance: 0.85, kinds: ["oak", "oak", "big_oak", "birch"] }, - "bworld:dark_forest": { chance: 1, kinds: ["big_oak", "big_oak", "oak"] }, - "bworld:plains": { chance: 0.05, kinds: ["oak"] }, - "bworld:meadow": { chance: 0.06, kinds: ["oak", "birch", "cherry", "cherry"] }, - "bworld:swamp": { chance: 0.4, kinds: ["big_oak"] }, - "bworld:river": { chance: 0.05, kinds: ["oak"] }, - "bworld:taiga": { chance: 0.75, kinds: ["spruce"] }, - "bworld:snowy_taiga": { chance: 0.6, kinds: ["spruce"] }, - "bworld:snowy_plains": { chance: 0.03, kinds: ["spruce"] }, - "bworld:snowy_slopes": { chance: 0.06, kinds: ["spruce"] }, - "bworld:savanna": { chance: 0.15, kinds: ["acacia", "acacia", "oak"] }, - "bworld:jungle": { chance: 1, kinds: ["jungle", "jungle", "big_oak"] }, -}; - export interface TreeSite { x: number; z: number; diff --git a/docs/modding/gettingstarted/modfiles.md b/docs/modding/gettingstarted/modfiles.md index f73dabf..bff8542 100644 --- a/docs/modding/gettingstarted/modfiles.md +++ b/docs/modding/gettingstarted/modfiles.md @@ -21,6 +21,7 @@ mods/ tags///*.json # tags, see resources/tags.md loot_tables/**/*.json # loot tables, see resources/loot_tables.md worldgen/ores.json + worldgen/biomes/*.json # one biome per file, see worldgen/biomes.md worldgen/spawns.json # monsters that spawn on their own, see entities/index.md textures/*.png textures/*.json # animations, see resources/textures.md diff --git a/docs/modding/worldgen/biomes.md b/docs/modding/worldgen/biomes.md new file mode 100644 index 0000000..e64ffe1 --- /dev/null +++ b/docs/modding/worldgen/biomes.md @@ -0,0 +1,72 @@ +# Biomes + +A biome says what the ground looks like where it's picked: the blocks on top, the colors grass, leaves and water are +tinted, which trees grow, and its season. Biomes are data, one file each in a mod's `worldgen/biomes/` folder. The base +game's are in `mods/bworld/worldgen/biomes/`. + +> [!NOTE] +> Which biome a column gets is still decided by the engine (`pick_biome` in `common/worldgen/overworld.ts`), from the +> column's climate and height. It only picks the base game's biomes, so a biome a mod adds doesn't generate anywhere +> yet. Mods can still list it in their spawns. + +## A Biome + +`worldgen/biomes/dark_forest.json`: + +```json +{ + "format_version": 1, + "biome": { + "id": "bworld:dark_forest", + "surface": { + "top": "bworld:grass", + "filler": "bworld:dirt", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#507a32", + "foliage": "#59ae30", + "water": "#3f76e4" + }, + "trees": { + "chance": 1, + "kinds": ["big_oak", "big_oak", "oak"], + "wood": { "big_oak": "dark_oak" } + } + } +} +``` + +| Field | What it does | When not set | +| --------------------------- | ---------------------------------------------------------------------------------------- | ------------ | +| `id` | in your mod's namespace | required | +| `season` | `spring`, `summer`, `autumn` or `winter`. Nothing uses it yet | `spring` | +| `surface.top` | the block on top of the ground | required | +| `surface.filler` | the blocks under it | required | +| `surface.filler_depth` | how many blocks of filler, 0 to 32. Stone is below | required | +| `surface.underwater_top` | the top block where the ground is under water | `filler` | +| `surface.underwater_filler` | the filler under water | `filler` | +| `surface.bare_cliffs` | steep ground shows stone instead of `top` | `false` | +| `colors.grass` | what blocks with `"tint": { "color": "grass" }` are drawn in, as `#rrggbb` | plains' | +| `colors.foliage` | the same for `"foliage"`, like leaves | plains' | +| `colors.water` | the same for `"water"` | plains' | +| `trees.chance` | how likely each tree spot is to get a tree, 0 to 1. Spots are at least 4 blocks apart | no trees | +| `trees.kinds` | which trees grow, picked evenly: list one twice to make it twice as likely | | +| `trees.wood` | the logs and leaves a kind grows here when it isn't its own, like dark oak for `big_oak` | | + +Tree kinds are `oak`, `big_oak`, `birch`, `spruce`, `jungle`, `acacia` and `cherry`. Woods are `oak`, `spruce`, `birch`, +`jungle`, `acacia`, `dark_oak` and `cherry`. Trees only grow on grass, dirt or snow with air above. + +Above the snow line, around y 176, the top block is snow whatever the biome says. + +## Biome Ids + +Biome ids are what features' `biome_at` gives and what spawns list. The base game has `bworld:deep_ocean`, +`bworld:deep_frozen_ocean`, `bworld:deep_lukewarm_ocean`, `bworld:ocean`, `bworld:frozen_ocean`, `bworld:warm_ocean`, +`bworld:river`, `bworld:frozen_river`, `bworld:beach`, `bworld:snowy_beach`, `bworld:stony_shore`, `bworld:plains`, +`bworld:meadow`, `bworld:forest`, `bworld:dark_forest`, `bworld:swamp`, `bworld:taiga`, `bworld:snowy_plains`, +`bworld:snowy_taiga`, `bworld:savanna`, `bworld:jungle`, `bworld:desert`, `bworld:snowy_slopes`, `bworld:stony_peaks`, +`bworld:jagged_peaks` and `bworld:frozen_peaks`. + +`/locatebiome ` finds the nearest one. diff --git a/docs/modding/worldgen/index.md b/docs/modding/worldgen/index.md index 09e5502..0b71abf 100644 --- a/docs/modding/worldgen/index.md +++ b/docs/modding/worldgen/index.md @@ -101,7 +101,7 @@ sea. Mountain ranges rise where erosion is low, and rivers run along the valleys they reach mountains. The flatter land is split into regions with their own shapes: valleys and plateaus (with badlands ridges where it's hot), tiered mesas, rolling hills and jungle pillars, and wetlands and desert dunes. -Biome ids are the keys of `BIOMES` in `common/worldgen/overworld.ts`, like `bworld:plains` or `bworld:jagged_peaks`. +Which biome each column gets is part of the terrain, but what a biome looks like is data: see [Biomes](biomes.md). `deno run -A tools/worldgen_preview.ts [seed] [out dir] [x z]` renders it to PNG files, around chunk `x z`. diff --git a/mods/bworld/worldgen/biomes/beach.json b/mods/bworld/worldgen/biomes/beach.json new file mode 100644 index 0000000..335f4de --- /dev/null +++ b/mods/bworld/worldgen/biomes/beach.json @@ -0,0 +1,16 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:beach", + "surface": { + "top": "bworld:sand", + "filler": "bworld:sand", + "filler_depth": 4 + }, + "colors": { + "grass": "#91bd59", + "foliage": "#77ab2f", + "water": "#3f76e4" + } + } +} diff --git a/mods/bworld/worldgen/biomes/dark_forest.json b/mods/bworld/worldgen/biomes/dark_forest.json new file mode 100644 index 0000000..b7aac0d --- /dev/null +++ b/mods/bworld/worldgen/biomes/dark_forest.json @@ -0,0 +1,28 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:dark_forest", + "surface": { + "top": "bworld:grass", + "filler": "bworld:dirt", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#507a32", + "foliage": "#59ae30", + "water": "#3f76e4" + }, + "trees": { + "chance": 1, + "kinds": [ + "big_oak", + "big_oak", + "oak" + ], + "wood": { + "big_oak": "dark_oak" + } + } + } +} diff --git a/mods/bworld/worldgen/biomes/deep_frozen_ocean.json b/mods/bworld/worldgen/biomes/deep_frozen_ocean.json new file mode 100644 index 0000000..a5b452c --- /dev/null +++ b/mods/bworld/worldgen/biomes/deep_frozen_ocean.json @@ -0,0 +1,16 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:deep_frozen_ocean", + "surface": { + "top": "bworld:sand", + "filler": "bworld:sand", + "filler_depth": 3 + }, + "colors": { + "grass": "#80b497", + "foliage": "#60a17b", + "water": "#3938c9" + } + } +} diff --git a/mods/bworld/worldgen/biomes/deep_lukewarm_ocean.json b/mods/bworld/worldgen/biomes/deep_lukewarm_ocean.json new file mode 100644 index 0000000..4540b11 --- /dev/null +++ b/mods/bworld/worldgen/biomes/deep_lukewarm_ocean.json @@ -0,0 +1,16 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:deep_lukewarm_ocean", + "surface": { + "top": "bworld:sand", + "filler": "bworld:sand", + "filler_depth": 3 + }, + "colors": { + "grass": "#8eb971", + "foliage": "#71a74d", + "water": "#45adf2" + } + } +} diff --git a/mods/bworld/worldgen/biomes/deep_ocean.json b/mods/bworld/worldgen/biomes/deep_ocean.json new file mode 100644 index 0000000..fc43f0c --- /dev/null +++ b/mods/bworld/worldgen/biomes/deep_ocean.json @@ -0,0 +1,16 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:deep_ocean", + "surface": { + "top": "bworld:sand", + "filler": "bworld:sand", + "filler_depth": 3 + }, + "colors": { + "grass": "#8eb971", + "foliage": "#71a74d", + "water": "#3f76e4" + } + } +} diff --git a/mods/bworld/worldgen/biomes/desert.json b/mods/bworld/worldgen/biomes/desert.json new file mode 100644 index 0000000..a610fcf --- /dev/null +++ b/mods/bworld/worldgen/biomes/desert.json @@ -0,0 +1,16 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:desert", + "surface": { + "top": "bworld:sand", + "filler": "bworld:sand", + "filler_depth": 4 + }, + "colors": { + "grass": "#bfb755", + "foliage": "#aea42a", + "water": "#3f76e4" + } + } +} diff --git a/mods/bworld/worldgen/biomes/forest.json b/mods/bworld/worldgen/biomes/forest.json new file mode 100644 index 0000000..4059ba9 --- /dev/null +++ b/mods/bworld/worldgen/biomes/forest.json @@ -0,0 +1,26 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:forest", + "surface": { + "top": "bworld:grass", + "filler": "bworld:dirt", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#79c05a", + "foliage": "#59ae30", + "water": "#3f76e4" + }, + "trees": { + "chance": 0.85, + "kinds": [ + "oak", + "oak", + "big_oak", + "birch" + ] + } + } +} diff --git a/mods/bworld/worldgen/biomes/frozen_ocean.json b/mods/bworld/worldgen/biomes/frozen_ocean.json new file mode 100644 index 0000000..c143e58 --- /dev/null +++ b/mods/bworld/worldgen/biomes/frozen_ocean.json @@ -0,0 +1,16 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:frozen_ocean", + "surface": { + "top": "bworld:sand", + "filler": "bworld:sand", + "filler_depth": 3 + }, + "colors": { + "grass": "#80b497", + "foliage": "#60a17b", + "water": "#3938c9" + } + } +} diff --git a/mods/bworld/worldgen/biomes/frozen_peaks.json b/mods/bworld/worldgen/biomes/frozen_peaks.json new file mode 100644 index 0000000..e79fecf --- /dev/null +++ b/mods/bworld/worldgen/biomes/frozen_peaks.json @@ -0,0 +1,19 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:frozen_peaks", + "surface": { + "top": "bworld:snow_block", + "filler": "bworld:stone", + "filler_depth": 3, + "underwater_top": "bworld:dirt", + "underwater_filler": "bworld:dirt", + "bare_cliffs": true + }, + "colors": { + "grass": "#80b497", + "foliage": "#60a17b", + "water": "#3f76e4" + } + } +} diff --git a/mods/bworld/worldgen/biomes/frozen_river.json b/mods/bworld/worldgen/biomes/frozen_river.json new file mode 100644 index 0000000..cc99a95 --- /dev/null +++ b/mods/bworld/worldgen/biomes/frozen_river.json @@ -0,0 +1,19 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:frozen_river", + "surface": { + "top": "bworld:snow_block", + "filler": "bworld:dirt", + "filler_depth": 3, + "underwater_top": "bworld:sand", + "underwater_filler": "bworld:sand", + "bare_cliffs": true + }, + "colors": { + "grass": "#80b497", + "foliage": "#60a17b", + "water": "#3938c9" + } + } +} diff --git a/mods/bworld/worldgen/biomes/jagged_peaks.json b/mods/bworld/worldgen/biomes/jagged_peaks.json new file mode 100644 index 0000000..dfaba44 --- /dev/null +++ b/mods/bworld/worldgen/biomes/jagged_peaks.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:jagged_peaks", + "surface": { + "top": "bworld:snow_block", + "filler": "bworld:stone", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#80b497", + "foliage": "#60a17b", + "water": "#3f76e4" + } + } +} diff --git a/mods/bworld/worldgen/biomes/jungle.json b/mods/bworld/worldgen/biomes/jungle.json new file mode 100644 index 0000000..218cc3d --- /dev/null +++ b/mods/bworld/worldgen/biomes/jungle.json @@ -0,0 +1,25 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:jungle", + "surface": { + "top": "bworld:grass", + "filler": "bworld:dirt", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#59c93c", + "foliage": "#30bb0b", + "water": "#3f76e4" + }, + "trees": { + "chance": 1, + "kinds": [ + "jungle", + "jungle", + "big_oak" + ] + } + } +} diff --git a/mods/bworld/worldgen/biomes/meadow.json b/mods/bworld/worldgen/biomes/meadow.json new file mode 100644 index 0000000..3fa29bc --- /dev/null +++ b/mods/bworld/worldgen/biomes/meadow.json @@ -0,0 +1,26 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:meadow", + "surface": { + "top": "bworld:grass", + "filler": "bworld:dirt", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#83bb6d", + "foliage": "#63a948", + "water": "#0e4ecf" + }, + "trees": { + "chance": 0.06, + "kinds": [ + "oak", + "birch", + "cherry", + "cherry" + ] + } + } +} diff --git a/mods/bworld/worldgen/biomes/ocean.json b/mods/bworld/worldgen/biomes/ocean.json new file mode 100644 index 0000000..1bb4bff --- /dev/null +++ b/mods/bworld/worldgen/biomes/ocean.json @@ -0,0 +1,16 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:ocean", + "surface": { + "top": "bworld:sand", + "filler": "bworld:sand", + "filler_depth": 3 + }, + "colors": { + "grass": "#8eb971", + "foliage": "#71a74d", + "water": "#3f76e4" + } + } +} diff --git a/mods/bworld/worldgen/biomes/plains.json b/mods/bworld/worldgen/biomes/plains.json new file mode 100644 index 0000000..26ea1be --- /dev/null +++ b/mods/bworld/worldgen/biomes/plains.json @@ -0,0 +1,23 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:plains", + "surface": { + "top": "bworld:grass", + "filler": "bworld:dirt", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#91bd59", + "foliage": "#77ab2f", + "water": "#3f76e4" + }, + "trees": { + "chance": 0.05, + "kinds": [ + "oak" + ] + } + } +} diff --git a/mods/bworld/worldgen/biomes/river.json b/mods/bworld/worldgen/biomes/river.json new file mode 100644 index 0000000..bbe6ac2 --- /dev/null +++ b/mods/bworld/worldgen/biomes/river.json @@ -0,0 +1,25 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:river", + "surface": { + "top": "bworld:grass", + "filler": "bworld:dirt", + "filler_depth": 3, + "underwater_top": "bworld:sand", + "underwater_filler": "bworld:sand", + "bare_cliffs": true + }, + "colors": { + "grass": "#8eb971", + "foliage": "#71a74d", + "water": "#3f76e4" + }, + "trees": { + "chance": 0.05, + "kinds": [ + "oak" + ] + } + } +} diff --git a/mods/bworld/worldgen/biomes/savanna.json b/mods/bworld/worldgen/biomes/savanna.json new file mode 100644 index 0000000..f5f6931 --- /dev/null +++ b/mods/bworld/worldgen/biomes/savanna.json @@ -0,0 +1,25 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:savanna", + "surface": { + "top": "bworld:grass", + "filler": "bworld:dirt", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#bfb755", + "foliage": "#aea42a", + "water": "#3f76e4" + }, + "trees": { + "chance": 0.15, + "kinds": [ + "acacia", + "acacia", + "oak" + ] + } + } +} diff --git a/mods/bworld/worldgen/biomes/snowy_beach.json b/mods/bworld/worldgen/biomes/snowy_beach.json new file mode 100644 index 0000000..3038b7b --- /dev/null +++ b/mods/bworld/worldgen/biomes/snowy_beach.json @@ -0,0 +1,16 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:snowy_beach", + "surface": { + "top": "bworld:snow_block", + "filler": "bworld:sand", + "filler_depth": 4 + }, + "colors": { + "grass": "#83b593", + "foliage": "#64a278", + "water": "#3d57d6" + } + } +} diff --git a/mods/bworld/worldgen/biomes/snowy_plains.json b/mods/bworld/worldgen/biomes/snowy_plains.json new file mode 100644 index 0000000..acbddb8 --- /dev/null +++ b/mods/bworld/worldgen/biomes/snowy_plains.json @@ -0,0 +1,23 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:snowy_plains", + "surface": { + "top": "bworld:snow_block", + "filler": "bworld:dirt", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#80b497", + "foliage": "#60a17b", + "water": "#3f76e4" + }, + "trees": { + "chance": 0.03, + "kinds": [ + "spruce" + ] + } + } +} diff --git a/mods/bworld/worldgen/biomes/snowy_slopes.json b/mods/bworld/worldgen/biomes/snowy_slopes.json new file mode 100644 index 0000000..64edfe1 --- /dev/null +++ b/mods/bworld/worldgen/biomes/snowy_slopes.json @@ -0,0 +1,23 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:snowy_slopes", + "surface": { + "top": "bworld:snow_block", + "filler": "bworld:dirt", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#80b497", + "foliage": "#60a17b", + "water": "#3f76e4" + }, + "trees": { + "chance": 0.06, + "kinds": [ + "spruce" + ] + } + } +} diff --git a/mods/bworld/worldgen/biomes/snowy_taiga.json b/mods/bworld/worldgen/biomes/snowy_taiga.json new file mode 100644 index 0000000..ba4d351 --- /dev/null +++ b/mods/bworld/worldgen/biomes/snowy_taiga.json @@ -0,0 +1,23 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:snowy_taiga", + "surface": { + "top": "bworld:snow_block", + "filler": "bworld:dirt", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#80b497", + "foliage": "#60a17b", + "water": "#3d57d6" + }, + "trees": { + "chance": 0.6, + "kinds": [ + "spruce" + ] + } + } +} diff --git a/mods/bworld/worldgen/biomes/stony_peaks.json b/mods/bworld/worldgen/biomes/stony_peaks.json new file mode 100644 index 0000000..b61966f --- /dev/null +++ b/mods/bworld/worldgen/biomes/stony_peaks.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:stony_peaks", + "surface": { + "top": "bworld:stone", + "filler": "bworld:stone", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#9abe4b", + "foliage": "#82ac1e", + "water": "#3f76e4" + } + } +} diff --git a/mods/bworld/worldgen/biomes/stony_shore.json b/mods/bworld/worldgen/biomes/stony_shore.json new file mode 100644 index 0000000..97acb29 --- /dev/null +++ b/mods/bworld/worldgen/biomes/stony_shore.json @@ -0,0 +1,17 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:stony_shore", + "surface": { + "top": "bworld:stone", + "filler": "bworld:stone", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#8ab689", + "foliage": "#6da36b", + "water": "#3f76e4" + } + } +} diff --git a/mods/bworld/worldgen/biomes/swamp.json b/mods/bworld/worldgen/biomes/swamp.json new file mode 100644 index 0000000..77e5666 --- /dev/null +++ b/mods/bworld/worldgen/biomes/swamp.json @@ -0,0 +1,22 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:swamp", + "surface": { + "top": "bworld:grass", + "filler": "bworld:dirt", + "filler_depth": 3 + }, + "colors": { + "grass": "#6a7039", + "foliage": "#6a7039", + "water": "#617b64" + }, + "trees": { + "chance": 0.4, + "kinds": [ + "big_oak" + ] + } + } +} diff --git a/mods/bworld/worldgen/biomes/taiga.json b/mods/bworld/worldgen/biomes/taiga.json new file mode 100644 index 0000000..81b9669 --- /dev/null +++ b/mods/bworld/worldgen/biomes/taiga.json @@ -0,0 +1,23 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:taiga", + "surface": { + "top": "bworld:grass", + "filler": "bworld:dirt", + "filler_depth": 3, + "bare_cliffs": true + }, + "colors": { + "grass": "#86b783", + "foliage": "#68a464", + "water": "#3f76e4" + }, + "trees": { + "chance": 0.75, + "kinds": [ + "spruce" + ] + } + } +} diff --git a/mods/bworld/worldgen/biomes/warm_ocean.json b/mods/bworld/worldgen/biomes/warm_ocean.json new file mode 100644 index 0000000..12442dc --- /dev/null +++ b/mods/bworld/worldgen/biomes/warm_ocean.json @@ -0,0 +1,16 @@ +{ + "format_version": 1, + "biome": { + "id": "bworld:warm_ocean", + "surface": { + "top": "bworld:sand", + "filler": "bworld:sand", + "filler_depth": 3 + }, + "colors": { + "grass": "#8eb971", + "foliage": "#71a74d", + "water": "#43d5ee" + } + } +} diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 64439ba..93a53b1 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -52,7 +52,8 @@ import { number, word, } from "$/common/commands.ts"; -import { BIOMES, estimate_biome } from "$/common/worldgen/overworld.ts"; +import { estimate_biome } from "$/common/worldgen/overworld.ts"; +import { biome_ids, get_biome } from "$/common/worldgen/biomes.ts"; import type { GameLoop } from "./game_loop.ts"; import { update_crafting_result } from "./crafting.ts"; import type { Menu } from "./menu.ts"; @@ -1401,12 +1402,10 @@ export class GameServer { register( literal("locatebiome").describe("Finds the nearest biome of a kind").then( argument("biome", word()) - .suggests((partial) => - Object.keys(BIOMES).filter((id) => id.startsWith(partial)).map((text) => ({ text })) - ) + .suggests((partial) => biome_ids().filter((id) => id.startsWith(partial)).map((text) => ({ text }))) .executes((context) => { const id = context.get("biome"); - if (!BIOMES[id]) throw new CommandError(`There's no biome ${id}`); + if (!get_biome(id)) throw new CommandError(`There's no biome ${id}`); const player = context.source.server_player; const found = this.#locate_biome(id, Math.floor(player.x), Math.floor(player.z)); if (!found) { diff --git a/server/load_bmods.ts b/server/load_bmods.ts index b9a0504..dfd3ac3 100644 --- a/server/load_bmods.ts +++ b/server/load_bmods.ts @@ -107,6 +107,7 @@ function as_loaded_mod(file: string, bmod: Bmod): LoadedMod { effects: entries(bmod.data.effects), tags: entries(bmod.data.tags), loot_tables: entries(bmod.data.loot_tables ?? []), + biomes: entries(bmod.data.biomes ?? []), textures: [...bmod.textures.keys()], texture_files: new Map(), texture_json: new Map(bmod.texture_json), diff --git a/tests/biome_test.ts b/tests/biome_test.ts new file mode 100644 index 0000000..ed78215 --- /dev/null +++ b/tests/biome_test.ts @@ -0,0 +1,37 @@ +// biomes are data, worldgen/biomes/.json in a mod. see common/worldgen/biomes.ts +import { assert, assertEquals } from "@std/assert"; +import { biome_from_json, biome_ids, get_biome, validate_biome } from "$/common/worldgen/biomes.ts"; +import { test_game } from "./helpers.ts"; + +const PLAIN = { id: "my_mod:flat", surface: { top: "bworld:grass", filler: "bworld:dirt", filler_depth: 3 } }; + +Deno.test("the bworld mod registers every biome the overworld picks", async () => { + await test_game("mods"); + assertEquals(biome_ids().length, 26); + assertEquals(get_biome("bworld:dark_forest")?.trees?.wood.big_oak, "dark_oak"); + assertEquals(get_biome("bworld:river")?.surface.underwater_top, "bworld:sand"); +}); + +Deno.test("a biome is in spring unless it says otherwise", async () => { + await test_game("mods"); + assertEquals(get_biome("bworld:plains")?.season, "spring"); + assertEquals(biome_from_json(PLAIN).season, "spring"); + assertEquals(biome_from_json({ ...PLAIN, season: "winter" }).season, "winter"); +}); + +Deno.test("a biome's surface defaults: underwater is its filler, cliffs aren't bare", () => { + const { surface, colors, trees } = biome_from_json(PLAIN); + assertEquals(surface.underwater_top, "bworld:dirt"); + assertEquals(surface.underwater_filler, "bworld:dirt"); + assertEquals(surface.bare_cliffs, false); + assertEquals(colors, { grass: undefined, foliage: undefined, water: undefined }); + assertEquals(trees, undefined); +}); + +Deno.test("bad biome json is reported", () => { + assertEquals(validate_biome(PLAIN), []); + assert(validate_biome({ ...PLAIN, season: "monsoon" }).some((p) => p.startsWith("season"))); + assert(validate_biome({ ...PLAIN, colors: { grass: "green" } }).some((p) => p.startsWith("colors.grass"))); + assert(validate_biome({ ...PLAIN, trees: { chance: 2, kinds: ["palm"] } }).length === 2); + assert(validate_biome({ id: "my_mod:flat" }).some((p) => p.startsWith("surface"))); +}); diff --git a/tests/helpers.ts b/tests/helpers.ts index 8dd54ba..c56cf8e 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -39,6 +39,7 @@ export function mod_sources(mods_dir: string): ServerModSource[] { effects: mod.effects.map((e) => e.json), tags: mod.tags.map((t) => t.json), loot_tables: mod.loot_tables.map((t) => t.json), + biomes: mod.biomes.map((b) => b.json), }, server_url: url(scripts.server), worldgen_url: url(scripts.worldgen), diff --git a/tests/tint_test.ts b/tests/tint_test.ts index 654203e..053f684 100644 --- a/tests/tint_test.ts +++ b/tests/tint_test.ts @@ -3,7 +3,7 @@ import { assert, assertEquals } from "@std/assert"; import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts"; import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "$/common/constants.ts"; import { generate_raw_chunk } from "$/common/generation.ts"; -import { BIOME_FOLIAGE_COLORS, BIOME_GRASS_COLORS, BIOME_WATER_COLORS } from "$/common/worldgen/biome_colors.ts"; +import { get_biome } from "$/common/worldgen/biomes.ts"; import { type LayerMesh, TERRAIN_VERTEX_BYTES } from "$/client/workers/chunk_messages.ts"; import { test_game } from "./helpers.ts"; @@ -121,9 +121,10 @@ Deno.test("every column the overworld generates has a biome with grass, foliage const { biomes } = generate_raw_chunk(x, z, game.world.seed, block_ids); assertEquals(biomes.length, CHUNK_AREA); for (const biome of biomes) { - assert(biome in BIOME_GRASS_COLORS, `${biome} has no grass color`); - assert(biome in BIOME_FOLIAGE_COLORS, `${biome} has no foliage color`); - assert(biome in BIOME_WATER_COLORS, `${biome} has no water color`); + const { colors } = get_biome(biome)!; + assert(colors.grass !== undefined, `${biome} has no grass color`); + assert(colors.foliage !== undefined, `${biome} has no foliage color`); + assert(colors.water !== undefined, `${biome} has no water color`); } } }); diff --git a/tests/wood_test.ts b/tests/wood_test.ts index 41496a9..33cdd04 100644 --- a/tests/wood_test.ts +++ b/tests/wood_test.ts @@ -3,7 +3,7 @@ import { CHUNK_AREA, CHUNK_SIZE } from "$/common/constants.ts"; import { Container, ItemStack } from "$/common/inventory.ts"; import { CRAFTING_RESULT_SLOT } from "$/common/protocol.ts"; import { generate_raw_chunk } from "$/common/generation.ts"; -import { BIOME_TREES, tree_wood } from "$/common/worldgen/trees.ts"; +import { get_biome, tree_wood } from "$/common/worldgen/biomes.ts"; import { update_crafting_result } from "$/server/game/crafting.ts"; import { test_game } from "./helpers.ts"; @@ -63,7 +63,8 @@ Deno.test("trees grow the wood of their kind and biome", async () => { if (!wood || !ground.has(blocks[i - CHUNK_AREA])) return; const column = i % CHUNK_AREA; const biome = biomes[column]; - const allowed = BIOME_TREES[biome].kinds.map((kind) => tree_wood(kind, biome)); + const info = get_biome(biome)!; + const allowed = info.trees!.kinds.map((kind) => tree_wood(kind, info)); assert( allowed.includes(wood as never), `${wood} in ${biome} at ${cx * CHUNK_SIZE}, ${cz * CHUNK_SIZE}`, diff --git a/tools/check_mods.ts b/tools/check_mods.ts index a7a9709..4633256 100644 --- a/tools/check_mods.ts +++ b/tools/check_mods.ts @@ -21,6 +21,7 @@ import { check_references, load_order, type LoadedMod, type ModReport } from "$/ import { png_size, texture_size_problem } from "$/common/bmod.ts"; import { type TextureJson, validate_texture_json } from "$/common/texture_animation.ts"; import { type LootTableJson, validate_loot_table } from "$/common/loot.ts"; +import { validate_biome } from "$/common/worldgen/biomes.ts"; import { dirname } from "@std/path"; export { load_order, type LoadedMod, type ModReport }; @@ -82,6 +83,7 @@ function load_mod(mods_dir: string, id: string): LoadedMod { effects: [], tags: [], loot_tables: [], + biomes: [], textures: [], texture_files: new Map(), texture_json: new Map(), @@ -147,6 +149,7 @@ function load_mod(mods_dir: string, id: string): LoadedMod { load_data("recipes", "recipe", validate_recipe, mod.recipes); load_data("entities", "entity", validate_entity, mod.entities); load_data("effects", "effect", validate_effect, mod.effects); + load_data("worldgen/biomes", "biome", validate_biome, mod.biomes); for (const file of walk(`${dir}/models`, ".geo.json")) { const path = `models/${file}`; diff --git a/tools/pack_mod.ts b/tools/pack_mod.ts index 62dea94..b743089 100644 --- a/tools/pack_mod.ts +++ b/tools/pack_mod.ts @@ -43,6 +43,7 @@ export async function pack_mod(mod: LoadedMod): Promise> effects: mod.effects.map((e) => e.json), tags: mod.tags.map((t) => t.json), loot_tables: mod.loot_tables.map((t) => t.json), + biomes: mod.biomes.map((b) => b.json), }, scripts, textures, diff --git a/tools/worldgen_preview.ts b/tools/worldgen_preview.ts index 15c46a9..2fd44f5 100644 --- a/tools/worldgen_preview.ts +++ b/tools/worldgen_preview.ts @@ -7,11 +7,18 @@ import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE, SEA_LEVEL } from "$/common/consta import { generate_raw_chunk } from "$/common/generation.ts"; import { OverworldTerrain } from "$/common/worldgen/terrain.ts"; import { pick_biome } from "$/common/worldgen/overworld.ts"; +import { register_biome } from "$/common/worldgen/biomes.ts"; +import { load_and_check } from "./check_mods.ts"; const seed = Deno.args[0] ?? "preview"; const out = Deno.args[1] ?? "."; const [center_x, center_z] = [Number(Deno.args[2] ?? 0), Number(Deno.args[3] ?? 0)]; +// the base game's biomes, from mods/bworld +for (const mod of load_and_check()) { + if (mod.id === "bworld") { for (const { json } of mod.biomes) register_biome(json); } +} + const NAMES = [ "bworld:stone", "bworld:dirt",