Dimension api

This commit is contained in:
2026-11-05 23:34:43 -03:00
parent 34f001285d
commit 4766c9a808
14 changed files with 251 additions and 27 deletions
+5
View File
@@ -38,10 +38,12 @@ import { type BiomeJson, validate_biome } from "./worldgen/biomes.ts";
import {
type BiomeSourceJson,
type DensityFunctionJson,
type DimensionJson,
type NoiseJson,
type NoiseSettingsJson,
validate_biome_source,
validate_density_function,
validate_dimension,
validate_noise,
validate_noise_settings,
} from "./worldgen/data.ts";
@@ -202,6 +204,7 @@ export function read_mod(bytes: Uint8Array, file: string): Mod {
validate_biome_source,
problems,
) as BiomeSourceJson[],
dimensions: checked(raw_data.dimensions, "dimensions", validate_dimension, problems) as DimensionJson[],
features: checked(raw_data.features, "features", validate_feature, problems) as FeatureJson[],
config: raw_data.config,
};
@@ -223,6 +226,7 @@ export function read_mod(bytes: Uint8Array, file: string): Mod {
...data.density_functions!,
...data.noise_settings!,
...data.biome_sources!,
...data.dimensions!,
...data.features!,
];
for (const { id } of [...registered, ...data.loot_tables!, ...data.biomes!, ...worldgen]) {
@@ -303,6 +307,7 @@ export function client_copy(mod: Mod): Mod {
density_functions: _d,
noise_settings: _s,
biome_sources: _b,
dimensions: _dim,
features: _f,
config: _c,
...data
+22
View File
@@ -29,6 +29,7 @@ import {
type BiomeSourceJson,
density_function_refs,
type DensityFunctionJson,
type DimensionJson,
new_refs,
noise_refs,
noise_settings_refs,
@@ -68,6 +69,7 @@ export interface LoadedMod {
density_functions: { file: string; json: DensityFunctionJson }[];
noise_settings: { file: string; json: NoiseSettingsJson }[];
biome_sources: { file: string; json: BiomeSourceJson }[];
dimensions: { file: string; json: DimensionJson }[];
features: { file: string; json: FeatureJson }[];
// config.json's settings, see docs/modding/misc/config.md
config?: Record<string, unknown>;
@@ -98,6 +100,7 @@ export function mod_data(mod: LoadedMod): ModData {
density_functions: mod.density_functions.map((d) => d.json),
noise_settings: mod.noise_settings.map((n) => n.json),
biome_sources: mod.biome_sources.map((b) => b.json),
dimensions: mod.dimensions.map((d) => d.json),
features: mod.features.map((f) => f.json),
config: mod.config,
};
@@ -177,7 +180,10 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
"density function": new Map(),
"noise settings": new Map(),
"biome source": new Map(),
dimension: new Map(),
};
// the dimensions that say they're the default, only one can be
const defaults: string[] = [];
const biome_rule_owners = new Map<string, string[]>();
const all_noise_settings: NoiseSettingsJson[] = [];
const feature_owners = new Map<string, string[]>();
@@ -213,6 +219,10 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
add(worldgen_owners["noise settings"], json.id, mod.id);
all_noise_settings.push(json);
}
for (const { json } of mod.dimensions ?? []) {
add(worldgen_owners.dimension, json.id, mod.id);
if (json.default) defaults.push(`${json.id} (${mod.id})`);
}
for (const { json } of mod.biome_sources ?? []) {
add(worldgen_owners["biome source"], json.id, mod.id);
for (const rule of json.rules) add(biome_rule_owners, rule.id, mod.id);
@@ -406,6 +416,18 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
noise_settings_refs(json, refs);
check_worldgen("noise settings", file, json.id, refs);
}
for (const { file, json } of mod.dimensions ?? []) {
check_worldgen("dimension", file, json.id, new_refs());
check_namespace(file, json.generator);
if (!worldgen_owners["noise settings"].has(json.generator)) {
errors.push(`${file}: noise settings ${json.generator} doesn't exist`);
}
if (json.default && defaults.length > 1) {
errors.push(
`${file}: only one dimension can be the default, and these all are: ${defaults.join(", ")}`,
);
}
}
for (const { file, json } of mod.biome_sources ?? []) {
const refs = new_refs();
biome_source_refs(json, refs);
+9 -1
View File
@@ -34,7 +34,13 @@ import { fluid_exists } from "./fluids.ts";
import { resolve_tags, Tags } from "./tags.ts";
import type { LootTableJson } from "./loot.ts";
import { type BiomeJson, register_biome } from "./worldgen/biomes.ts";
import type { BiomeSourceJson, DensityFunctionJson, NoiseJson, NoiseSettingsJson } from "./worldgen/data.ts";
import type {
BiomeSourceJson,
DensityFunctionJson,
DimensionJson,
NoiseJson,
NoiseSettingsJson,
} from "./worldgen/data.ts";
import type { FeatureJson } from "./worldgen/features.ts";
import { register_block_item } from "./utils.ts";
import { find_model, type ModelFileJson, type ModelJson, resolve_models } from "./block_models.ts";
@@ -66,6 +72,8 @@ export interface ModData {
density_functions?: DensityFunctionJson[];
noise_settings?: NoiseSettingsJson[];
biome_sources?: BiomeSourceJson[];
// which noise settings the world is generated with, see DimensionJson. server only too
dimensions?: DimensionJson[];
// data features, see common/worldgen/features.ts. server only too
features?: FeatureJson[];
// the mod's settings, its config.json's "config", with the server's changes once the server loads it. server only
+21 -1
View File
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// world generation as data: noises, density functions, noise settings (a generator) and biome sources, see
// world generation as data: noises, density functions, noise settings (a generator), biome sources and dimensions, see
// docs/modding/worldgen/terrain.md. checked here, compiled by density.ts and generator.ts. the names follow
// minecraft's (density functions, the noise router, surface rules) where the idea is the same
import * as z from "zod";
@@ -130,6 +130,16 @@ export interface BiomeRuleJson {
biome: string;
}
// dimensions: a world to play and the noise settings that generate it, like neoforge's level stems. the world plays the
// one with "default": true, and only one loaded mod can have one
export interface DimensionJson {
id: string;
// the noise settings that generate it
generator: string;
default?: boolean;
}
// what worldgen data refers to, for checking it all exists
export interface WorldgenRefs {
noises: Set<string>;
@@ -313,6 +323,12 @@ const biome_source_schema: z.ZodType<BiomeSourceJson> = z.object({
});
});
const dimension_schema: z.ZodType<DimensionJson> = z.strictObject({
id: worldgen_id,
generator: z.string("must be a noise settings' id").regex(WORLDGEN_ID_PATTERN),
default: z.boolean().optional(),
});
export function validate_noise(json: unknown): Problems {
return find_problems(noise_schema, json);
}
@@ -329,6 +345,10 @@ export function validate_biome_source(json: unknown): Problems {
return find_problems(biome_source_schema, json);
}
export function validate_dimension(json: unknown): Problems {
return find_problems(dimension_schema, json);
}
// what worldgen data that passed its checks refers to
export function noise_refs(json: NoiseJson, refs: WorldgenRefs) {