Validators using zod
This commit is contained in:
+222
-352
@@ -4,7 +4,8 @@
|
||||
// world generation as data: noises, density functions, noise settings (a generator) and biome sources, 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 { ID_PATTERN, is_object, TAG_ID_PATTERN } from "../mod_data.ts";
|
||||
import * as z from "zod";
|
||||
import { find_problems, ID_PATTERN, is_object, type Problems, TAG_ID_PATTERN } from "../schema.ts";
|
||||
|
||||
// ids of worldgen data can have folders, like "bworld:overworld/erosion"
|
||||
export const WORLDGEN_ID_PATTERN = TAG_ID_PATTERN;
|
||||
@@ -142,376 +143,256 @@ export function new_refs(): WorldgenRefs {
|
||||
return { noises: new Set(), functions: new Set(), blocks: new Set(), biomes: new Set(), biome_sources: new Set() };
|
||||
}
|
||||
|
||||
const ARITHMETIC = ["add", "sub", "mul", "div", "min", "max"];
|
||||
export const DENSITY_TYPES = [
|
||||
"constant",
|
||||
"x",
|
||||
"y",
|
||||
"z",
|
||||
"noise",
|
||||
...ARITHMETIC,
|
||||
"abs",
|
||||
"clamp",
|
||||
"smoothstep",
|
||||
"spline",
|
||||
"range_choice",
|
||||
"cache_2d",
|
||||
];
|
||||
// checking
|
||||
|
||||
const worldgen_id = z.string('must be an id like "my_mod:thing" or "my_mod:folder/thing"').regex(WORLDGEN_ID_PATTERN);
|
||||
const block_id = z.string("must be a block's id").regex(ID_PATTERN);
|
||||
const biome_id = z.string("must be a biome's id").regex(ID_PATTERN);
|
||||
|
||||
const noise_schema: z.ZodType<NoiseJson> = z.object({
|
||||
id: worldgen_id,
|
||||
seed_name: z.string().optional(),
|
||||
wavelength: z.number().positive(),
|
||||
amplitudes: z.array(z.number()).min(1).refine((amplitudes) => amplitudes.some((a) => a !== 0), "can't all be 0"),
|
||||
dimensions: z.literal([2, 3]).optional(),
|
||||
vertical_stretch: z.number().positive().optional(),
|
||||
quantiles: z.union([
|
||||
worldgen_id,
|
||||
z.array(z.number()).min(2).refine((q) => q.every((value, i) => i === 0 || value > q[i - 1]), "must go up"),
|
||||
], "must be at least 2 numbers going up, or a noise's id").optional(),
|
||||
}).refine((noise) => noise.vertical_stretch === undefined || noise.dimensions === 3, {
|
||||
path: ["vertical_stretch"],
|
||||
message: "is only for 3d noises",
|
||||
});
|
||||
|
||||
const RANGE_KEYS = ["min_inclusive", "min_exclusive", "max_inclusive", "max_exclusive"] as const;
|
||||
|
||||
export function validate_noise(json: unknown): string[] {
|
||||
const problems: string[] = [];
|
||||
if (!is_object(json)) return ["noise must be an object"];
|
||||
worldgen_id(json.id, "id", problems);
|
||||
if (json.seed_name !== undefined && typeof json.seed_name !== "string") problems.push("seed_name must be text");
|
||||
if (!(typeof json.wavelength === "number" && json.wavelength > 0)) problems.push("wavelength must be above 0");
|
||||
if (
|
||||
!Array.isArray(json.amplitudes) || json.amplitudes.length === 0 ||
|
||||
!json.amplitudes.every((a) => typeof a === "number" && Number.isFinite(a)) ||
|
||||
json.amplitudes.every((a) => a === 0)
|
||||
) {
|
||||
problems.push("amplitudes must be a list of numbers, not all 0");
|
||||
}
|
||||
if (json.dimensions !== undefined && json.dimensions !== 2 && json.dimensions !== 3) {
|
||||
problems.push("dimensions must be 2 or 3");
|
||||
}
|
||||
if (json.vertical_stretch !== undefined) {
|
||||
if (json.dimensions !== 3) problems.push("vertical_stretch is only for 3d noises");
|
||||
if (!(typeof json.vertical_stretch === "number" && json.vertical_stretch > 0)) {
|
||||
problems.push("vertical_stretch must be above 0");
|
||||
// a range's bounds, numbers or (in range_choice) density functions. a range needs at least one
|
||||
function bounds<T extends z.ZodType>(bound: T) {
|
||||
return {
|
||||
min_inclusive: bound.optional(),
|
||||
min_exclusive: bound.optional(),
|
||||
max_inclusive: bound.optional(),
|
||||
max_exclusive: bound.optional(),
|
||||
};
|
||||
}
|
||||
const has_bound = (range: Partial<Record<typeof RANGE_KEYS[number], unknown>>) =>
|
||||
RANGE_KEYS.some((key) => range[key] !== undefined);
|
||||
const NEEDS_BOUND = `needs at least one of ${RANGE_KEYS.join(", ")}`;
|
||||
|
||||
const range = z.object(bounds(z.number())).refine(has_bound, NEEDS_BOUND);
|
||||
|
||||
// a density function, by itself or by its id
|
||||
const density: z.ZodType<DensityJson> = z.lazy(() =>
|
||||
z.union(
|
||||
[z.number(), z.string("must be a density function's id").regex(WORLDGEN_ID_PATTERN), density_object],
|
||||
'must be a number, a density function\'s id or { "type": ... }',
|
||||
)
|
||||
);
|
||||
|
||||
const spline: z.ZodType<SplineJson> = z.object({
|
||||
coordinate: density,
|
||||
points: z.array(z.object({
|
||||
location: z.number(),
|
||||
value: z.union([z.number(), z.lazy(() => spline)], "must be a number or a spline"),
|
||||
derivative: z.number().optional(),
|
||||
})).min(1).superRefine((points, ctx) => {
|
||||
points.forEach((point, i) => {
|
||||
if (i > 0 && point.location < points[i - 1].location) {
|
||||
ctx.addIssue({ code: "custom", path: [i, "location"], message: "can't be below the point before's" });
|
||||
}
|
||||
});
|
||||
}),
|
||||
});
|
||||
|
||||
const density_object = z.discriminatedUnion("type", [
|
||||
z.object({ type: z.literal("constant"), argument: z.number() }),
|
||||
z.object({ type: z.literal(["x", "y", "z"]) }),
|
||||
z.object({
|
||||
type: z.literal("noise"),
|
||||
noise: z.string("must be a noise's id").regex(WORLDGEN_ID_PATTERN),
|
||||
even: z.boolean().optional(),
|
||||
x: density.optional(),
|
||||
y: density.optional(),
|
||||
z: density.optional(),
|
||||
}),
|
||||
z.object({ type: z.literal(["add", "sub", "mul", "div", "min", "max"]), argument1: density, argument2: density }),
|
||||
z.object({ type: z.literal(["abs", "cache_2d"]), argument: density }),
|
||||
z.object({ type: z.literal("clamp"), input: density, min: z.number(), max: z.number() })
|
||||
.refine(({ min, max }) => min <= max, { path: ["min"], message: "can't be above max" }),
|
||||
z.object({ type: z.literal("smoothstep"), input: density, from: z.number(), to: z.number() })
|
||||
.refine(({ from, to }) => from !== to, { path: ["to"], message: "must be different from from" }),
|
||||
z.object({ type: z.literal("spline"), spline }),
|
||||
z.object({
|
||||
type: z.literal("range_choice"),
|
||||
input: density,
|
||||
...bounds(density),
|
||||
when_in_range: density,
|
||||
when_out_of_range: density,
|
||||
}).refine(has_bound, NEEDS_BOUND),
|
||||
]);
|
||||
|
||||
const density_function_schema: z.ZodType<DensityFunctionJson> = z.intersection(
|
||||
z.object({ id: worldgen_id }),
|
||||
density_object,
|
||||
);
|
||||
|
||||
const surface_rule: z.ZodType<SurfaceRuleJson> = z.lazy(() =>
|
||||
z.discriminatedUnion("type", [
|
||||
z.object({ type: z.literal("sequence"), sequence: z.array(surface_rule) }),
|
||||
z.object({ type: z.literal("condition"), if_true: surface_condition, then_run: surface_rule }),
|
||||
z.object({ type: z.literal("block"), result_state: z.object({ Name: block_id }) }),
|
||||
z.object({ type: z.literal("biome_surface"), part: z.enum(BIOME_SURFACE_PARTS) }),
|
||||
])
|
||||
);
|
||||
|
||||
const surface_condition: z.ZodType<SurfaceConditionJson> = z.lazy(() =>
|
||||
z.discriminatedUnion("type", [
|
||||
z.object({ type: z.literal("stone_depth"), offset: z.int() }),
|
||||
z.object({ type: z.literal(["filler_depth", "surface_underwater", "steep", "bare_cliffs"]) }),
|
||||
z.object({ type: z.literal("above_preliminary_surface"), offset: z.number().optional() }),
|
||||
z.object({ type: z.literal("y_at_least"), function: density }),
|
||||
z.object({ type: z.literal("biome"), biome_is: z.array(biome_id) }),
|
||||
z.object({ type: z.literal("not"), invert: surface_condition }),
|
||||
])
|
||||
);
|
||||
|
||||
const cell_height = z.literal([1, 2, 4, 8, 16, 32]);
|
||||
|
||||
const noise_settings_schema: z.ZodType<NoiseSettingsJson> = z.object({
|
||||
id: worldgen_id,
|
||||
sea_level: z.int().min(0).max(256),
|
||||
default_block: block_id,
|
||||
cell_width: z.literal([1, 2, 4, 8, 16]),
|
||||
terrain: z.object({ density, cell_height }),
|
||||
caves: z.object({ density, cell_height }),
|
||||
preliminary_surface: density,
|
||||
climate: z.record(
|
||||
z.string("isn't a name a climate value can have: lowercase letters and _, but not surface or steep")
|
||||
.regex(/^[a-z_]+$/).refine((name) => name !== "surface" && name !== "steep"),
|
||||
density,
|
||||
),
|
||||
steep: z.number().min(0),
|
||||
biome_source: z.string("must be a biome source's id").regex(WORLDGEN_ID_PATTERN),
|
||||
fluids: z.array(z.object({
|
||||
block: block_id,
|
||||
below: z.int(),
|
||||
where: z.union([
|
||||
z.literal("open_sky"),
|
||||
z.object({ function: density, ...bounds(z.number()) }).refine(has_bound, NEEDS_BOUND),
|
||||
], 'must be "open_sky" or { "function", <range> }'),
|
||||
})),
|
||||
surface_rule,
|
||||
});
|
||||
|
||||
const biome_source_schema: z.ZodType<BiomeSourceJson> = z.object({
|
||||
id: worldgen_id,
|
||||
extends: z.string("must be a biome source's id").regex(WORLDGEN_ID_PATTERN).optional(),
|
||||
rules: z.array(z.object({
|
||||
id: worldgen_id,
|
||||
before: worldgen_id.optional(),
|
||||
after: worldgen_id.optional(),
|
||||
// steep is true or false, any other name is a range of that climate value
|
||||
when: z.object({ steep: z.boolean().optional() }).catchall(range).optional(),
|
||||
biome: biome_id,
|
||||
})),
|
||||
}).superRefine((source, ctx) => {
|
||||
// a rule added to another biome source goes before or after one of its rules
|
||||
source.rules.forEach((rule, i) => {
|
||||
const placed = [rule.before, rule.after].filter((id) => id !== undefined).length;
|
||||
if (source.extends !== undefined && placed !== 1) {
|
||||
ctx.addIssue({ code: "custom", path: ["rules", i], message: "needs before or after, one of them" });
|
||||
}
|
||||
}
|
||||
const quantiles = json.quantiles;
|
||||
if (quantiles !== undefined && typeof quantiles !== "string") {
|
||||
if (
|
||||
!Array.isArray(quantiles) || quantiles.length < 2 ||
|
||||
!quantiles.every((q, i) => typeof q === "number" && (i === 0 || q > quantiles[i - 1]))
|
||||
) {
|
||||
problems.push("quantiles must be at least 2 numbers going up, or a noise's id");
|
||||
if (source.extends === undefined && placed > 0) {
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
path: ["rules", i],
|
||||
message: "can't have before or after, they're for rules added to another biome source with extends",
|
||||
});
|
||||
}
|
||||
} else if (typeof quantiles === "string") {
|
||||
worldgen_id(quantiles, "quantiles", problems);
|
||||
}
|
||||
return problems;
|
||||
});
|
||||
});
|
||||
|
||||
export function validate_noise(json: unknown): Problems {
|
||||
return find_problems(noise_schema, json);
|
||||
}
|
||||
|
||||
// the noises a noise file refers to: the one it takes its quantiles from
|
||||
export function validate_density_function(json: unknown): Problems {
|
||||
return find_problems(density_function_schema, json);
|
||||
}
|
||||
|
||||
export function validate_noise_settings(json: unknown): Problems {
|
||||
return find_problems(noise_settings_schema, json);
|
||||
}
|
||||
|
||||
export function validate_biome_source(json: unknown): Problems {
|
||||
return find_problems(biome_source_schema, json);
|
||||
}
|
||||
|
||||
// what worldgen data that passed its checks refers to
|
||||
|
||||
export function noise_refs(json: NoiseJson, refs: WorldgenRefs) {
|
||||
if (typeof json.quantiles === "string") refs.noises.add(json.quantiles);
|
||||
}
|
||||
|
||||
export function validate_density_function(json: unknown): string[] {
|
||||
if (!is_object(json)) return ["density_function must be an object"];
|
||||
const problems: string[] = [];
|
||||
worldgen_id(json.id, "id", problems);
|
||||
const { id: _, ...fn } = json;
|
||||
check_density(fn, "", new_refs(), problems);
|
||||
return problems;
|
||||
}
|
||||
|
||||
export function density_function_refs(json: DensityFunctionJson, refs: WorldgenRefs) {
|
||||
const { id: _, ...fn } = json;
|
||||
check_density(fn, "", refs, []);
|
||||
density_refs(fn, refs);
|
||||
}
|
||||
|
||||
// checks a density function, adding what it refers to to refs. at is where it is, for messages
|
||||
export function check_density(json: unknown, at: string, refs: WorldgenRefs, problems: string[]) {
|
||||
const where = at ? `${at}: ` : "";
|
||||
if (typeof json === "number") {
|
||||
if (!Number.isFinite(json)) problems.push(`${where}must be a finite number`);
|
||||
return;
|
||||
}
|
||||
function density_refs(json: DensityJson, refs: WorldgenRefs) {
|
||||
if (typeof json === "number") return;
|
||||
if (typeof json === "string") {
|
||||
if (WORLDGEN_ID_PATTERN.test(json)) refs.functions.add(json);
|
||||
else problems.push(`${where}"${json}" isn't a density function's id`);
|
||||
refs.functions.add(json);
|
||||
return;
|
||||
}
|
||||
if (!is_object(json) || typeof json.type !== "string") {
|
||||
problems.push(`${where}must be a number, a density function's id or { "type": ... }`);
|
||||
return;
|
||||
}
|
||||
const field = (name: string) => check_density(json[name], at ? `${at}.${name}` : name, refs, problems);
|
||||
const number = (name: string) => {
|
||||
if (typeof json[name] !== "number" || !Number.isFinite(json[name])) {
|
||||
problems.push(`${where}${name} must be a number`);
|
||||
for (const [key, value] of Object.entries(json)) {
|
||||
if (key === "noise") refs.noises.add(value as string);
|
||||
else if (key === "spline") spline_refs(value as SplineJson, refs);
|
||||
// the other fields that are text or objects are density functions, the rest numbers and true or false
|
||||
else if (key !== "type" && (typeof value === "string" || is_object(value))) {
|
||||
density_refs(value as DensityJson, refs);
|
||||
}
|
||||
};
|
||||
switch (json.type) {
|
||||
case "constant":
|
||||
number("argument");
|
||||
break;
|
||||
case "x":
|
||||
case "y":
|
||||
case "z":
|
||||
break;
|
||||
case "noise":
|
||||
if (typeof json.noise === "string" && WORLDGEN_ID_PATTERN.test(json.noise)) refs.noises.add(json.noise);
|
||||
else problems.push(`${where}noise must be a noise's id`);
|
||||
if (json.even !== undefined && typeof json.even !== "boolean") {
|
||||
problems.push(`${where}even must be true or false`);
|
||||
}
|
||||
for (const axis of ["x", "y", "z"]) if (json[axis] !== undefined) field(axis);
|
||||
break;
|
||||
case "abs":
|
||||
case "cache_2d":
|
||||
field("argument");
|
||||
break;
|
||||
case "clamp":
|
||||
field("input");
|
||||
number("min");
|
||||
number("max");
|
||||
if ((json.min as number) > (json.max as number)) problems.push(`${where}min is above max`);
|
||||
break;
|
||||
case "smoothstep":
|
||||
field("input");
|
||||
number("from");
|
||||
number("to");
|
||||
if (json.from === json.to) problems.push(`${where}from and to must be different`);
|
||||
break;
|
||||
case "spline":
|
||||
check_spline(json.spline, at ? `${at}.spline` : "spline", refs, problems);
|
||||
break;
|
||||
case "range_choice":
|
||||
field("input");
|
||||
if (!RANGE_KEYS.some((key) => json[key] !== undefined)) problems.push(`${where}needs at least one bound`);
|
||||
for (const key of RANGE_KEYS) if (json[key] !== undefined) field(key);
|
||||
field("when_in_range");
|
||||
field("when_out_of_range");
|
||||
break;
|
||||
default:
|
||||
if (ARITHMETIC.includes(json.type)) {
|
||||
field("argument1");
|
||||
field("argument2");
|
||||
} else {
|
||||
problems.push(`${where}type ${json.type} isn't one of ${DENSITY_TYPES.join(", ")}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function check_spline(json: unknown, at: string, refs: WorldgenRefs, problems: string[]) {
|
||||
if (!is_object(json) || !Array.isArray(json.points) || json.points.length === 0) {
|
||||
problems.push(`${at}: must be { "coordinate": ..., "points": [ ... ] }`);
|
||||
return;
|
||||
}
|
||||
check_density(json.coordinate, `${at}.coordinate`, refs, problems);
|
||||
let last = -Infinity;
|
||||
json.points.forEach((point: unknown, i: number) => {
|
||||
const here = `${at}.points[${i}]`;
|
||||
if (!is_object(point) || typeof point.location !== "number") {
|
||||
problems.push(`${here}: must be { "location", "value", "derivative" }`);
|
||||
return;
|
||||
}
|
||||
if (point.location < last) problems.push(`${here}: locations must go up`);
|
||||
last = point.location;
|
||||
if (typeof point.value !== "number") check_spline(point.value, `${here}.value`, refs, problems);
|
||||
if (point.derivative !== undefined && typeof point.derivative !== "number") {
|
||||
problems.push(`${here}: derivative must be a number`);
|
||||
}
|
||||
});
|
||||
function spline_refs(spline: SplineJson, refs: WorldgenRefs) {
|
||||
density_refs(spline.coordinate, refs);
|
||||
for (const point of spline.points) if (typeof point.value !== "number") spline_refs(point.value, refs);
|
||||
}
|
||||
|
||||
export function validate_noise_settings(json: unknown): string[] {
|
||||
if (!is_object(json)) return ["noise_settings must be an object"];
|
||||
const problems: string[] = [];
|
||||
noise_settings_refs(json, new_refs(), problems);
|
||||
return problems;
|
||||
export function noise_settings_refs(json: NoiseSettingsJson, refs: WorldgenRefs) {
|
||||
refs.blocks.add(json.default_block);
|
||||
const { terrain, caves, preliminary_surface, climate } = json;
|
||||
for (const fn of [terrain.density, caves.density, preliminary_surface, ...Object.values(climate)]) {
|
||||
density_refs(fn, refs);
|
||||
}
|
||||
refs.biome_sources.add(json.biome_source);
|
||||
for (const fluid of json.fluids) {
|
||||
refs.blocks.add(fluid.block);
|
||||
if (fluid.where !== "open_sky") density_refs(fluid.where.function, refs);
|
||||
}
|
||||
surface_rule_refs(json.surface_rule, refs);
|
||||
}
|
||||
|
||||
export function noise_settings_refs(json: Record<string, unknown>, refs: WorldgenRefs, problems: string[] = []) {
|
||||
worldgen_id(json.id, "id", problems);
|
||||
const whole = (name: string, min: number, max: number) => {
|
||||
const value = json[name];
|
||||
if (!(Number.isInteger(value) && (value as number) >= min && (value as number) <= max)) {
|
||||
problems.push(`${name} must be a whole number from ${min} to ${max}`);
|
||||
}
|
||||
};
|
||||
whole("sea_level", 0, 256);
|
||||
block_id(json.default_block, "default_block", refs, problems);
|
||||
if (![1, 2, 4, 8, 16].includes(json.cell_width as number)) problems.push("cell_width must be 1, 2, 4, 8 or 16");
|
||||
for (const part of ["terrain", "caves"]) {
|
||||
const value = json[part];
|
||||
if (!is_object(value)) {
|
||||
problems.push(`${part} must be { "density", "cell_height" }`);
|
||||
continue;
|
||||
}
|
||||
check_density(value.density, `${part}.density`, refs, problems);
|
||||
if (![1, 2, 4, 8, 16, 32].includes(value.cell_height as number)) {
|
||||
problems.push(`${part}.cell_height must be 1, 2, 4, 8, 16 or 32`);
|
||||
}
|
||||
function surface_rule_refs(rule: SurfaceRuleJson, refs: WorldgenRefs) {
|
||||
if (rule.type === "sequence") { for (const inner of rule.sequence) surface_rule_refs(inner, refs); }
|
||||
if (rule.type === "condition") {
|
||||
surface_condition_refs(rule.if_true, refs);
|
||||
surface_rule_refs(rule.then_run, refs);
|
||||
}
|
||||
check_density(json.preliminary_surface, "preliminary_surface", refs, problems);
|
||||
if (!is_object(json.climate)) {
|
||||
problems.push("climate must be { <name>: <density function> }");
|
||||
} else {
|
||||
for (const [name, value] of Object.entries(json.climate)) {
|
||||
if (!/^[a-z_]+$/.test(name) || name === "surface" || name === "steep") {
|
||||
problems.push(`climate.${name}: names are lowercase letters and _, and not surface or steep`);
|
||||
}
|
||||
check_density(value, `climate.${name}`, refs, problems);
|
||||
}
|
||||
}
|
||||
if (!(typeof json.steep === "number" && json.steep >= 0)) problems.push("steep must be a number, 0 or more");
|
||||
if (typeof json.biome_source === "string" && WORLDGEN_ID_PATTERN.test(json.biome_source)) {
|
||||
refs.biome_sources.add(json.biome_source);
|
||||
} else {
|
||||
problems.push("biome_source must be a biome source's id");
|
||||
}
|
||||
if (!Array.isArray(json.fluids)) {
|
||||
problems.push("fluids must be a list");
|
||||
} else {
|
||||
json.fluids.forEach((fluid: unknown, i: number) => {
|
||||
const at = `fluids[${i}]`;
|
||||
if (!is_object(fluid)) {
|
||||
problems.push(`${at}: must be { "block", "below", "where" }`);
|
||||
return;
|
||||
}
|
||||
block_id(fluid.block, `${at}.block`, refs, problems);
|
||||
if (!Number.isInteger(fluid.below)) problems.push(`${at}.below must be a whole number`);
|
||||
if (fluid.where !== "open_sky") {
|
||||
if (!is_object(fluid.where)) {
|
||||
problems.push(`${at}.where must be "open_sky" or { "function", <range> }`);
|
||||
} else {
|
||||
check_density(fluid.where.function, `${at}.where.function`, refs, problems);
|
||||
check_range(fluid.where, `${at}.where`, problems);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
check_surface_rule(json.surface_rule, "surface_rule", refs, problems);
|
||||
if (rule.type === "block") refs.blocks.add(rule.result_state.Name);
|
||||
}
|
||||
|
||||
function check_surface_rule(json: unknown, at: string, refs: WorldgenRefs, problems: string[]) {
|
||||
if (!is_object(json)) {
|
||||
problems.push(`${at}: must be a rule`);
|
||||
return;
|
||||
}
|
||||
switch (json.type) {
|
||||
case "sequence":
|
||||
if (!Array.isArray(json.sequence)) problems.push(`${at}.sequence must be a list of rules`);
|
||||
else json.sequence.forEach((rule, i) => check_surface_rule(rule, `${at}.sequence[${i}]`, refs, problems));
|
||||
break;
|
||||
case "condition":
|
||||
check_surface_condition(json.if_true, `${at}.if_true`, refs, problems);
|
||||
check_surface_rule(json.then_run, `${at}.then_run`, refs, problems);
|
||||
break;
|
||||
case "block":
|
||||
if (!is_object(json.result_state)) problems.push(`${at}.result_state must be { "Name": <block> }`);
|
||||
else block_id(json.result_state.Name, `${at}.result_state.Name`, refs, problems);
|
||||
break;
|
||||
case "biome_surface":
|
||||
if (!BIOME_SURFACE_PARTS.includes(json.part as BiomeSurfacePart)) {
|
||||
problems.push(`${at}.part must be one of ${BIOME_SURFACE_PARTS.join(", ")}`);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
problems.push(`${at}: type must be sequence, condition, block or biome_surface`);
|
||||
}
|
||||
function surface_condition_refs(condition: SurfaceConditionJson, refs: WorldgenRefs) {
|
||||
if (condition.type === "y_at_least") density_refs(condition.function, refs);
|
||||
if (condition.type === "biome") { for (const biome of condition.biome_is) refs.biomes.add(biome); }
|
||||
if (condition.type === "not") surface_condition_refs(condition.invert, refs);
|
||||
}
|
||||
|
||||
function check_surface_condition(json: unknown, at: string, refs: WorldgenRefs, problems: string[]) {
|
||||
if (!is_object(json)) {
|
||||
problems.push(`${at}: must be a condition`);
|
||||
return;
|
||||
}
|
||||
switch (json.type) {
|
||||
case "stone_depth":
|
||||
if (!Number.isInteger(json.offset)) problems.push(`${at}.offset must be a whole number`);
|
||||
break;
|
||||
case "above_preliminary_surface":
|
||||
if (json.offset !== undefined && typeof json.offset !== "number") {
|
||||
problems.push(`${at}.offset must be a number`);
|
||||
}
|
||||
break;
|
||||
case "filler_depth":
|
||||
case "surface_underwater":
|
||||
case "steep":
|
||||
case "bare_cliffs":
|
||||
break;
|
||||
case "y_at_least":
|
||||
check_density(json.function, `${at}.function`, refs, problems);
|
||||
break;
|
||||
case "biome":
|
||||
if (!Array.isArray(json.biome_is) || !json.biome_is.every((b) => typeof b === "string")) {
|
||||
problems.push(`${at}.biome_is must be a list of biome ids`);
|
||||
} else {
|
||||
for (const biome of json.biome_is) refs.biomes.add(biome);
|
||||
}
|
||||
break;
|
||||
case "not":
|
||||
check_surface_condition(json.invert, `${at}.invert`, refs, problems);
|
||||
break;
|
||||
default:
|
||||
problems.push(
|
||||
`${at}: type must be stone_depth, filler_depth, above_preliminary_surface, surface_underwater, steep, ` +
|
||||
`bare_cliffs, y_at_least, biome or not`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function validate_biome_source(json: unknown): string[] {
|
||||
if (!is_object(json)) return ["biome_source must be an object"];
|
||||
const problems: string[] = [];
|
||||
biome_source_refs(json, new_refs(), problems);
|
||||
return problems;
|
||||
}
|
||||
|
||||
export function biome_source_refs(json: Record<string, unknown>, refs: WorldgenRefs, problems: string[] = []) {
|
||||
worldgen_id(json.id, "id", problems);
|
||||
if (json.extends !== undefined) {
|
||||
if (typeof json.extends === "string" && WORLDGEN_ID_PATTERN.test(json.extends)) {
|
||||
refs.biome_sources.add(json.extends);
|
||||
} else problems.push("extends must be a biome source's id");
|
||||
}
|
||||
if (!Array.isArray(json.rules)) {
|
||||
problems.push("rules must be a list");
|
||||
return;
|
||||
}
|
||||
json.rules.forEach((rule: unknown, i: number) => {
|
||||
const at = `rules[${i}]`;
|
||||
if (!is_object(rule)) {
|
||||
problems.push(`${at}: must be { "id", "when", "biome" }`);
|
||||
return;
|
||||
}
|
||||
worldgen_id(rule.id, `${at}.id`, problems);
|
||||
const placed = [rule.before, rule.after].filter((id) => id !== undefined);
|
||||
if (json.extends !== undefined && placed.length !== 1) {
|
||||
problems.push(`${at}: a rule added to another biome source needs before or after, one of them`);
|
||||
}
|
||||
if (json.extends === undefined && placed.length > 0) {
|
||||
problems.push(`${at}: before and after are for rules added to another biome source, with extends`);
|
||||
}
|
||||
for (const id of placed) worldgen_id(id, `${at}.before/after`, problems);
|
||||
if (typeof rule.biome === "string" && ID_PATTERN.test(rule.biome)) refs.biomes.add(rule.biome);
|
||||
else problems.push(`${at}.biome must be a biome's id`);
|
||||
if (rule.when !== undefined) {
|
||||
if (!is_object(rule.when)) {
|
||||
problems.push(`${at}.when must be { <climate value>: <range>, "steep": true or false }`);
|
||||
} else {
|
||||
for (const [name, condition] of Object.entries(rule.when)) {
|
||||
if (name === "steep") {
|
||||
if (typeof condition !== "boolean") problems.push(`${at}.when.steep must be true or false`);
|
||||
} else {
|
||||
check_range(condition, `${at}.when.${name}`, problems);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// a range of numbers, with at least one bound
|
||||
function check_range(json: unknown, at: string, problems: string[]) {
|
||||
if (!is_object(json)) {
|
||||
problems.push(`${at} must be a range like { "min_inclusive": 0 }`);
|
||||
return;
|
||||
}
|
||||
let bounds = 0;
|
||||
for (const key of RANGE_KEYS) {
|
||||
if (json[key] === undefined) continue;
|
||||
bounds++;
|
||||
if (typeof json[key] !== "number") problems.push(`${at}.${key} must be a number`);
|
||||
}
|
||||
if (bounds === 0) problems.push(`${at} needs at least one of ${RANGE_KEYS.join(", ")}`);
|
||||
export function biome_source_refs(json: BiomeSourceJson, refs: WorldgenRefs) {
|
||||
if (json.extends !== undefined) refs.biome_sources.add(json.extends);
|
||||
for (const rule of json.rules) refs.biomes.add(rule.biome);
|
||||
}
|
||||
|
||||
// a test of whether a value is in the range, with only the bounds it has: value >= min, value < max
|
||||
@@ -529,14 +410,3 @@ export function range_test(range: RangeJson): (value: number) => boolean {
|
||||
}
|
||||
return (value) => tests.every((test) => test(value));
|
||||
}
|
||||
|
||||
function worldgen_id(value: unknown, field: string, problems: string[]) {
|
||||
if (!(typeof value === "string" && WORLDGEN_ID_PATTERN.test(value))) {
|
||||
problems.push(`${field} must be an id like "my_mod:thing" or "my_mod:folder/thing"`);
|
||||
}
|
||||
}
|
||||
|
||||
function block_id(value: unknown, field: string, refs: WorldgenRefs, problems: string[]) {
|
||||
if (typeof value === "string" && ID_PATTERN.test(value)) refs.blocks.add(value);
|
||||
else problems.push(`${field} must be a block's id`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user