Biome as data
This commit is contained in:
+5
-2
@@ -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/<side>.js
|
||||
// textures/<name>.png the texture <mod id>:<name>
|
||||
// textures/<name>.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`);
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
+12
-3
@@ -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<string, string>;
|
||||
@@ -90,6 +91,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
const tag_owners = new Map<TagRegistry, Set<string>>();
|
||||
const liquids = new Set<string>();
|
||||
const loot_tables = new Set<string>();
|
||||
const biome_owners = new Map<string, string[]>();
|
||||
const add = (map: Map<string, string[]>, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]);
|
||||
|
||||
const textures = new Set<string>(engine_textures);
|
||||
@@ -113,6 +115,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
}
|
||||
for (const { json } of mod.effects) add(effect_owners, json.id, mod.id);
|
||||
for (const { json } of mod.loot_tables ?? []) loot_tables.add(json.id);
|
||||
for (const { json } of mod.biomes ?? []) add(biome_owners, json.id, mod.id);
|
||||
for (const { json } of mod.tags) {
|
||||
if (!tag_owners.has(json.registry)) tag_owners.set(json.registry, new Set());
|
||||
tag_owners.get(json.registry)!.add(json.id);
|
||||
@@ -272,6 +275,12 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
// mods' conditions and functions are registered by scripts, the server checks them when it starts
|
||||
for (const id of refs.custom) check_namespace(file, id);
|
||||
}
|
||||
for (const { file, json } of mod.biomes ?? []) {
|
||||
if ((biome_owners.get(json.id)?.length ?? 0) > 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<st
|
||||
else if (entity.behavior !== "monster") {
|
||||
errors.push(`${file}: ${json.entity} isn't a monster, only monsters spawn on their own for now`);
|
||||
}
|
||||
// biomes are the engine's, whatever their namespace
|
||||
for (const biome of json.biomes ?? []) {
|
||||
if (!(biome in BIOMES)) errors.push(`${file}: biome ${biome} doesn't exist`);
|
||||
check_namespace(file, biome);
|
||||
if (!biome_owners.has(biome)) errors.push(`${file}: biome ${biome} doesn't exist`);
|
||||
}
|
||||
}
|
||||
for (const { file, json } of mod.entity_models) {
|
||||
|
||||
@@ -22,6 +22,7 @@ import {
|
||||
} from "./mod_data.ts";
|
||||
import { resolve_tags, Tags } from "./tags.ts";
|
||||
import type { LootTableJson } from "./loot.ts";
|
||||
import { type BiomeJson, register_biome } from "./worldgen/biomes.ts";
|
||||
import { register_block_item } from "./utils.ts";
|
||||
import type { ModelJson } from "./block_models.ts";
|
||||
|
||||
@@ -43,6 +44,8 @@ export interface ModData {
|
||||
tags: TagJson[];
|
||||
// see common/loot.ts. missing from mods packed before there were loot tables
|
||||
loot_tables?: LootTableJson[];
|
||||
// see common/worldgen/biomes.ts. missing from mods packed before biomes were data
|
||||
biomes?: BiomeJson[];
|
||||
}
|
||||
|
||||
// a mod as the server lists it to clients, in load order: the .bmod players download, without its server script
|
||||
@@ -123,6 +126,9 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
|
||||
for (const json of data.loot_tables ?? []) {
|
||||
EverythingRegistry.register<LootTableJson>("loot_tables", json.id, json);
|
||||
}
|
||||
for (const json of data.biomes ?? []) {
|
||||
register_biome(json);
|
||||
}
|
||||
} catch (e) {
|
||||
throw new ModLoadError(id, (e as Error).message);
|
||||
}
|
||||
|
||||
@@ -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<string, number> = {
|
||||
"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<string, number> = {
|
||||
"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<string, number> = {
|
||||
"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"
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
// 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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
@@ -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<Wood, number>;
|
||||
leaves: Record<Wood, number>;
|
||||
// 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<string, Surface> = {
|
||||
"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<string, { biome: Biome; surface: Surface }>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<string, { chance: number; kinds: TreeKind[] }> = {
|
||||
"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;
|
||||
|
||||
Reference in New Issue
Block a user