541 lines
20 KiB
TypeScript
541 lines
20 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// data features: worldgen/features/<path>.json in a mod, a feature's type and config (what) with a list of placement
|
|
// modifiers (where), see docs/modding/worldgen/index.md. like minecraft's configured and placed features joined into
|
|
// one file. the types and modifiers here are built in, worldgen scripts register more
|
|
import { EverythingRegistry } from "../everything_registry.ts";
|
|
import type { BlockRegistry } from "../everything_registry.ts";
|
|
import { CHUNK_SIZE } from "../constants.ts";
|
|
import * as z from "zod";
|
|
import {
|
|
add_problems,
|
|
by_kind,
|
|
find_problems,
|
|
id_or_tag,
|
|
ID_PATTERN,
|
|
is_object,
|
|
json_object,
|
|
type Problems,
|
|
TAG_ID_PATTERN,
|
|
} from "../schema.ts";
|
|
import { list_matches } from "../tags.ts";
|
|
import { can_stand_on } from "../utils.ts";
|
|
import { AIR_ID } from "../protocol.ts";
|
|
import type { FeatureChunk, FeatureStep, Position } from "../mod_api/worldgen.ts";
|
|
|
|
// the order features run in, minecraft's decoration steps. a feature without a step runs in the last
|
|
export const FEATURE_STEPS: FeatureStep[] = [
|
|
"raw_generation",
|
|
"lakes",
|
|
"local_modifications",
|
|
"underground_structures",
|
|
"surface_structures",
|
|
"strongholds",
|
|
"underground_ores",
|
|
"underground_decoration",
|
|
"fluid_springs",
|
|
"vegetal_decoration",
|
|
"top_layer_modification",
|
|
];
|
|
export const DEFAULT_STEP: FeatureStep = "top_layer_modification";
|
|
|
|
export interface FeatureJson {
|
|
id: string;
|
|
// a built in type, like "minecraft:block_column", or one a worldgen script registers
|
|
type: string;
|
|
config?: Record<string, unknown>;
|
|
placement?: PlacementJson[];
|
|
// only where the biome at the position is one of these
|
|
biomes?: string | string[];
|
|
step?: FeatureStep;
|
|
}
|
|
|
|
export interface PlacementJson {
|
|
type: string;
|
|
[field: string]: unknown;
|
|
}
|
|
|
|
// a type of feature: places config at origin with the chunk's random numbers, returns whether it placed anything
|
|
export type FeatureType = (chunk: FeatureChunk, config: unknown, origin: Position) => boolean;
|
|
// a placement modifier: the positions after it, from the ones before
|
|
export type PlacementModifier = (chunk: FeatureChunk, config: unknown, positions: Position[]) => Position[];
|
|
|
|
// a built in name without its namespace, so "minecraft:count" and "count" are the same. undefined for a mod's own
|
|
export function builtin_name(type: string): string | undefined {
|
|
if (!type.includes(":")) return type;
|
|
return type.startsWith("minecraft:") ? type.slice("minecraft:".length) : undefined;
|
|
}
|
|
|
|
// checking
|
|
|
|
// what a feature uses, for check-mods
|
|
export interface FeatureRefs {
|
|
blocks: Set<string>;
|
|
biomes: Set<string>;
|
|
// types and modifiers that aren't built in, which a worldgen script has to register
|
|
custom: Set<string>;
|
|
}
|
|
|
|
// a built in name, with its namespace or without, see builtin_name
|
|
const builtin = (...names: string[]) => z.literal(names.flatMap((name) => [name, `minecraft:${name}`]));
|
|
|
|
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);
|
|
|
|
// a block, see state_id
|
|
const block_state_schema = z.union(
|
|
[block_id, z.object({ state: z.object({ Name: block_id }) }), z.object({ Name: block_id })],
|
|
'must be a block\'s id or { "type": "simple_state_provider", "state": { "Name": ... } }',
|
|
);
|
|
|
|
// a whole number, see int_provider
|
|
const int_provider_schema: z.ZodType = z.lazy(() =>
|
|
z.union([
|
|
z.int(),
|
|
z.discriminatedUnion("type", [
|
|
z.object({ type: builtin("constant"), value: z.int() }),
|
|
z.object({ type: builtin("uniform"), min_inclusive: z.int(), max_inclusive: z.int() }).refine(
|
|
({ min_inclusive, max_inclusive }) => min_inclusive <= max_inclusive,
|
|
{ path: ["min_inclusive"], message: "can't be above max_inclusive" },
|
|
),
|
|
z.object({
|
|
type: builtin("weighted_list"),
|
|
distribution: z.array(z.object({ data: int_provider_schema, weight: z.int().min(1) })).min(1),
|
|
}),
|
|
], "must be constant, uniform or weighted_list"),
|
|
], 'must be a whole number or { "type": "uniform", ... }')
|
|
);
|
|
|
|
// a y, see anchor
|
|
const anchor_schema = z.union([
|
|
z.strictObject({ absolute: z.int() }),
|
|
z.strictObject({ above_bottom: z.int() }),
|
|
z.strictObject({ below_top: z.int() }),
|
|
], 'must be one of { "absolute": y }, { "above_bottom": n } or { "below_top": n }');
|
|
|
|
// see height_provider
|
|
const height_schema = z.union([
|
|
anchor_schema,
|
|
z.discriminatedUnion("type", [
|
|
z.object({ type: builtin("constant"), value: anchor_schema }),
|
|
z.object({ type: builtin("uniform"), min_inclusive: anchor_schema, max_inclusive: anchor_schema }),
|
|
], "must be constant or uniform"),
|
|
], 'must be a height, like { "absolute": 10 } or { "type": "uniform", ... }');
|
|
|
|
// see block_predicate. offset moves where it looks
|
|
const predicate_base = z.object({
|
|
offset: z.tuple([z.int(), z.int(), z.int()], "must be [x, y, z], whole numbers").optional(),
|
|
});
|
|
|
|
const block_predicate_schema: z.ZodType = z.lazy(() =>
|
|
z.discriminatedUnion("type", [
|
|
predicate_base.extend({
|
|
type: builtin("matching_blocks"),
|
|
blocks: z.union([id_or_tag, z.array(id_or_tag)], "must be a block, a #tag, or a list of them"),
|
|
}),
|
|
predicate_base.extend({ type: builtin("solid", "replaceable", "true") }),
|
|
predicate_base.extend({ type: builtin("would_survive"), state: block_state_schema }),
|
|
predicate_base.extend({ type: builtin("not"), predicate: block_predicate_schema }),
|
|
predicate_base.extend({ type: builtin("all_of", "any_of"), predicates: z.array(block_predicate_schema) }),
|
|
], "must be matching_blocks, solid, replaceable, would_survive, not, all_of, any_of or true")
|
|
);
|
|
|
|
// a mod's own modifier is checked by the worldgen script that registers it
|
|
const placement_modifier = by_kind<PlacementJson>("type", (type) => {
|
|
const name = builtin_name(type);
|
|
if (name === undefined) return json_object;
|
|
return Object.hasOwn(MODIFIERS, name) ? MODIFIERS[name].schema : undefined;
|
|
}, "isn't a built in placement modifier");
|
|
|
|
const feature_schema: z.ZodType<FeatureJson> = z.object({
|
|
id: z.string('must be an id like "my_mod:thing" or "my_mod:folder/thing"').regex(TAG_ID_PATTERN),
|
|
type: z.string(),
|
|
config: json_object.optional(),
|
|
placement: z.array(placement_modifier).optional(),
|
|
biomes: z.union([biome_id, z.array(biome_id)], "must be a biome's id or a list of them").optional(),
|
|
step: z.enum(FEATURE_STEPS).optional(),
|
|
}).superRefine((feature, ctx) => {
|
|
// a built in type's config. a mod's own type is checked by the worldgen script that registers it, like modifiers
|
|
const name = builtin_name(feature.type);
|
|
if (name === undefined) return;
|
|
if (!Object.hasOwn(FEATURE_TYPES, name)) {
|
|
return ctx.addIssue({ code: "custom", path: ["type"], message: `${feature.type} isn't built in` });
|
|
}
|
|
add_problems(ctx, FEATURE_TYPES[name].schema, feature.config ?? {}, ["config"]);
|
|
});
|
|
|
|
export function validate_feature(json: unknown): Problems {
|
|
return find_problems(feature_schema, json);
|
|
}
|
|
|
|
// what a feature that passed its checks uses
|
|
export function feature_refs(json: FeatureJson, refs: FeatureRefs) {
|
|
for (const biome of [json.biomes ?? []].flat()) refs.biomes.add(biome);
|
|
const type = builtin_name(json.type);
|
|
if (type === undefined) refs.custom.add(json.type);
|
|
else FEATURE_TYPES[type].refs?.(json.config ?? {}, refs);
|
|
for (const modifier of json.placement ?? []) {
|
|
const name = builtin_name(modifier.type);
|
|
if (name === undefined) refs.custom.add(modifier.type);
|
|
else MODIFIERS[name].refs?.(modifier, refs);
|
|
}
|
|
}
|
|
|
|
// compiling: a data feature becomes a function of the chunk, run with the chunk's random numbers
|
|
|
|
export interface CompiledFeature {
|
|
// where it runs, then what it places at each position
|
|
generate(chunk: FeatureChunk): void;
|
|
// what chunk.place_feature does: its type and config at a position, without its placement
|
|
place(chunk: FeatureChunk, origin: Position): boolean;
|
|
}
|
|
|
|
export function compile_feature(
|
|
json: FeatureJson,
|
|
types: Map<string, FeatureType>,
|
|
modifiers: Map<string, PlacementModifier>,
|
|
): CompiledFeature {
|
|
const config = json.config ?? {};
|
|
const builtin = builtin_name(json.type);
|
|
let place: (chunk: FeatureChunk, origin: Position) => boolean;
|
|
if (builtin !== undefined) {
|
|
place = FEATURE_TYPES[builtin].compile(config);
|
|
} else {
|
|
const type = types.get(json.type);
|
|
if (!type) {
|
|
throw new Error(
|
|
`feature ${json.id}: type ${json.type} isn't built in, and no worldgen script registered it`,
|
|
);
|
|
}
|
|
place = (chunk, origin) => type(chunk, config, origin);
|
|
}
|
|
const steps = (json.placement ?? []).map((modifier, i) => {
|
|
const name = builtin_name(modifier.type);
|
|
if (name !== undefined) return MODIFIERS[name].compile(modifier);
|
|
const custom = modifiers.get(modifier.type);
|
|
if (!custom) {
|
|
throw new Error(
|
|
`feature ${json.id}: placement[${i}] ${modifier.type} isn't built in, and no worldgen script registered it`,
|
|
);
|
|
}
|
|
return (chunk: FeatureChunk, positions: Position[]) => custom(chunk, modifier, positions);
|
|
});
|
|
const biomes = json.biomes === undefined
|
|
? undefined
|
|
: new Set(typeof json.biomes === "string" ? [json.biomes] : json.biomes);
|
|
return {
|
|
place,
|
|
generate(chunk) {
|
|
// minecraft's origin: the chunk's corner at the bottom of the world
|
|
let positions: Position[] = [{ x: chunk.x * CHUNK_SIZE, y: 0, z: chunk.z * CHUNK_SIZE }];
|
|
for (const step of steps) positions = step(chunk, positions);
|
|
for (const position of positions) {
|
|
if (
|
|
biomes &&
|
|
!(inside(chunk, position.x, position.z) &&
|
|
biomes.has(chunk.biome_at(position.x, position.y, position.z)))
|
|
) {
|
|
continue;
|
|
}
|
|
place(chunk, position);
|
|
}
|
|
},
|
|
};
|
|
}
|
|
|
|
// built in feature types
|
|
|
|
interface Builtin<T> {
|
|
// its json: a feature type's config, or the placement modifier
|
|
schema: z.ZodType;
|
|
// what it uses, when it uses something
|
|
refs?(json: Record<string, unknown>, refs: FeatureRefs): void;
|
|
compile(json: Record<string, unknown>): T;
|
|
}
|
|
|
|
const FEATURE_TYPES: Record<string, Builtin<(chunk: FeatureChunk, origin: Position) => boolean>> = {
|
|
// one block, where it can stand
|
|
simple_block: {
|
|
schema: z.object({ to_place: block_state_schema }),
|
|
refs(json, refs) {
|
|
refs.blocks.add(state_id(json.to_place));
|
|
},
|
|
compile(json) {
|
|
const id = state_id(json.to_place);
|
|
return (chunk, { x, y, z }) => {
|
|
if (!survives(chunk, id, x, y, z)) return false;
|
|
chunk.set_block(x, y, z, id);
|
|
return true;
|
|
};
|
|
},
|
|
},
|
|
// layers of blocks stacked up (or down) from the origin, cut short where allowed_placement doesn't hold
|
|
block_column: {
|
|
schema: z.object({
|
|
layers: z.array(z.object({ height: int_provider_schema, provider: block_state_schema })).min(1),
|
|
direction: z.enum(["up", "down"]).optional(),
|
|
allowed_placement: block_predicate_schema.optional(),
|
|
prioritize_tip: z.boolean().optional(),
|
|
}),
|
|
refs(json, refs) {
|
|
for (const layer of json.layers as { provider: unknown }[]) refs.blocks.add(state_id(layer.provider));
|
|
if (json.allowed_placement !== undefined) predicate_refs(json.allowed_placement, refs);
|
|
},
|
|
compile(json) {
|
|
const layers = (json.layers as { height: unknown; provider: unknown }[]).map((layer) => ({
|
|
height: int_provider(layer.height),
|
|
id: state_id(layer.provider),
|
|
}));
|
|
const dy = json.direction === "down" ? -1 : 1;
|
|
const allowed = json.allowed_placement === undefined
|
|
? (chunk: FeatureChunk, x: number, y: number, z: number) => block_at(chunk, x, y, z) === AIR_ID
|
|
: block_predicate(json.allowed_placement);
|
|
const tip_first = json.prioritize_tip === true;
|
|
return (chunk, { x, y, z }) => {
|
|
const column: string[] = [];
|
|
for (const layer of layers) {
|
|
const height = layer.height(chunk);
|
|
for (let i = 0; i < height; i++) column.push(layer.id);
|
|
}
|
|
let fits = 0;
|
|
while (fits < column.length && allowed(chunk, x, y + fits * dy, z)) fits++;
|
|
if (fits === 0) return false;
|
|
const placed = tip_first ? column.slice(column.length - fits) : column.slice(0, fits);
|
|
placed.forEach((id, i) => chunk.set_block(x, y + i * dy, z, id));
|
|
return true;
|
|
};
|
|
},
|
|
},
|
|
};
|
|
|
|
// built in placement modifiers
|
|
|
|
type Modifier = (chunk: FeatureChunk, positions: Position[]) => Position[];
|
|
|
|
const HEIGHTMAPS = ["WORLD_SURFACE_WG", "WORLD_SURFACE", "MOTION_BLOCKING", "OCEAN_FLOOR_WG", "OCEAN_FLOOR"] as const;
|
|
|
|
const MODIFIERS: Record<string, Builtin<Modifier>> = {
|
|
// each position becomes count of them
|
|
count: {
|
|
schema: z.object({ count: int_provider_schema }),
|
|
compile(json) {
|
|
const count = int_provider(json.count);
|
|
return (chunk, positions) =>
|
|
positions.flatMap((p) => Array.from({ length: count(chunk) }, () => ({ ...p })));
|
|
},
|
|
},
|
|
// one in chance positions are kept
|
|
rarity_filter: {
|
|
schema: z.object({ chance: z.int().min(1) }),
|
|
compile(json) {
|
|
const chance = json.chance as number;
|
|
return (chunk, positions) => positions.filter(() => chunk.rng.next() < 1 / chance);
|
|
},
|
|
},
|
|
// somewhere random in the chunk, across
|
|
in_square: {
|
|
schema: z.object({}),
|
|
compile() {
|
|
return (chunk, positions) =>
|
|
positions.map((p) => {
|
|
const x = p.x + Math.floor(chunk.rng.next() * CHUNK_SIZE);
|
|
const z = p.z + Math.floor(chunk.rng.next() * CHUNK_SIZE);
|
|
return { x, y: p.y, z };
|
|
});
|
|
},
|
|
},
|
|
// moved by up to xz_spread across and y_spread up or down
|
|
random_offset: {
|
|
schema: z.object({ xz_spread: int_provider_schema.optional(), y_spread: int_provider_schema.optional() }),
|
|
compile(json) {
|
|
const xz = int_provider(json.xz_spread ?? 0);
|
|
const y = int_provider(json.y_spread ?? 0);
|
|
return (chunk, positions) =>
|
|
positions.map((p) => ({ x: p.x + xz(chunk), y: p.y + y(chunk), z: p.z + xz(chunk) }));
|
|
},
|
|
},
|
|
// at a height
|
|
height_range: {
|
|
schema: z.object({ height: height_schema }),
|
|
compile(json) {
|
|
const height = height_provider(json.height);
|
|
return (chunk, positions) => positions.map((p) => ({ x: p.x, y: height(chunk), z: p.z }));
|
|
},
|
|
},
|
|
// on top of the column: WORLD_SURFACE on its highest block, OCEAN_FLOOR on its highest that isn't a fluid.
|
|
// positions outside the chunk are dropped
|
|
heightmap: {
|
|
schema: z.object({ heightmap: z.enum(HEIGHTMAPS) }),
|
|
compile(json) {
|
|
const floor = String(json.heightmap).startsWith("OCEAN_FLOOR");
|
|
return (chunk, positions) =>
|
|
positions.flatMap((p) => {
|
|
if (!inside(chunk, p.x, p.z)) return [];
|
|
let y = chunk.max_y - 1;
|
|
while (y >= chunk.min_y && !counts_for_heightmap(chunk.get_block(p.x, y, p.z), floor)) y--;
|
|
return [{ x: p.x, y: y + 1, z: p.z }];
|
|
});
|
|
},
|
|
},
|
|
// where a block predicate holds
|
|
block_predicate_filter: {
|
|
schema: z.object({ predicate: block_predicate_schema }),
|
|
refs(json, refs) {
|
|
predicate_refs(json.predicate, refs);
|
|
},
|
|
compile(json) {
|
|
const predicate = block_predicate(json.predicate);
|
|
return (chunk, positions) => positions.filter((p) => predicate(chunk, p.x, p.y, p.z));
|
|
},
|
|
},
|
|
};
|
|
|
|
function counts_for_heightmap(id: string | undefined, floor: boolean) {
|
|
if (id === undefined || id === AIR_ID) return false;
|
|
return !floor || !EverythingRegistry.get<BlockRegistry>("blocks", id)?.fluid;
|
|
}
|
|
|
|
// block predicates, like minecraft's
|
|
|
|
type Predicate = (chunk: FeatureChunk, x: number, y: number, z: number) => boolean;
|
|
// the blocks a predicate tests for
|
|
function predicate_refs(json: unknown, refs: FeatureRefs) {
|
|
const predicate = json as Record<string, unknown>;
|
|
switch (builtin_name(predicate.type as string)) {
|
|
case "matching_blocks":
|
|
for (const block of [predicate.blocks].flat()) refs.blocks.add(block as string);
|
|
break;
|
|
case "would_survive":
|
|
refs.blocks.add(state_id(predicate.state));
|
|
break;
|
|
case "not":
|
|
predicate_refs(predicate.predicate, refs);
|
|
break;
|
|
case "all_of":
|
|
case "any_of":
|
|
for (const inner of predicate.predicates as unknown[]) predicate_refs(inner, refs);
|
|
}
|
|
}
|
|
|
|
function block_predicate(json: unknown): Predicate {
|
|
const predicate = json as Record<string, unknown>;
|
|
const [ox, oy, oz] = (predicate.offset as number[] | undefined) ?? [0, 0, 0];
|
|
const at =
|
|
(test: (id: string | undefined, chunk: FeatureChunk, x: number, y: number, z: number) => boolean): Predicate =>
|
|
(chunk, x, y, z) => test(block_at(chunk, x + ox, y + oy, z + oz), chunk, x + ox, y + oy, z + oz);
|
|
const info = (id: string | undefined) =>
|
|
id === undefined ? undefined : EverythingRegistry.get<BlockRegistry>("blocks", id);
|
|
switch (builtin_name(predicate.type as string)) {
|
|
case "matching_blocks": {
|
|
const blocks = typeof predicate.blocks === "string" ? [predicate.blocks] : predicate.blocks as string[];
|
|
return at((id) => id !== undefined && list_matches("blocks", blocks, id));
|
|
}
|
|
case "solid":
|
|
return at((id) => id !== undefined && info(id)?.has_collision === true);
|
|
case "replaceable":
|
|
return at((id) => id === AIR_ID || info(id)?.replaceable === true);
|
|
case "would_survive": {
|
|
const state = state_id(predicate.state);
|
|
return at((_id, chunk, x, y, z) => survives(chunk, state, x, y, z));
|
|
}
|
|
case "not": {
|
|
const inner = block_predicate(predicate.predicate);
|
|
return (chunk, x, y, z) => !inner(chunk, x + ox, y + oy, z + oz);
|
|
}
|
|
case "all_of": {
|
|
const inner = (predicate.predicates as unknown[]).map(block_predicate);
|
|
return (chunk, x, y, z) => inner.every((p) => p(chunk, x + ox, y + oy, z + oz));
|
|
}
|
|
case "any_of": {
|
|
const inner = (predicate.predicates as unknown[]).map(block_predicate);
|
|
return (chunk, x, y, z) => inner.some((p) => p(chunk, x + ox, y + oy, z + oz));
|
|
}
|
|
default:
|
|
return () => true;
|
|
}
|
|
}
|
|
|
|
// whether a block could be at x y z: where it is, it isn't air, and it can stand on what's under it (place_on)
|
|
function survives(chunk: FeatureChunk, id: string, x: number, y: number, z: number) {
|
|
if (y < chunk.min_y || y >= chunk.max_y) return false;
|
|
const below = block_at(chunk, x, y - 1, z);
|
|
return below !== undefined && can_stand_on(EverythingRegistry.get<BlockRegistry>("blocks", id), below);
|
|
}
|
|
|
|
// the block at x y z, undefined outside the chunk or the world
|
|
function block_at(chunk: FeatureChunk, x: number, y: number, z: number) {
|
|
return inside(chunk, x, z) ? chunk.get_block(x, y, z) : undefined;
|
|
}
|
|
|
|
function inside(chunk: FeatureChunk, x: number, z: number) {
|
|
return Math.floor(x / CHUNK_SIZE) === chunk.x && Math.floor(z / CHUNK_SIZE) === chunk.z;
|
|
}
|
|
|
|
// block states: a block's id, or minecraft's { "type": "simple_state_provider", "state": { "Name": id } }
|
|
|
|
function state_id(json: unknown): string {
|
|
if (typeof json === "string") return json;
|
|
const object = json as Record<string, unknown>;
|
|
return (is_object(object.state) ? object.state.Name : object.Name) as string;
|
|
}
|
|
|
|
// numbers: a whole number, { "type": "constant", "value" }, { "type": "uniform", "min_inclusive", "max_inclusive" } or
|
|
// { "type": "weighted_list", "distribution": [{ "data", "weight" }] }, like minecraft's int providers
|
|
|
|
type IntProvider = (chunk: FeatureChunk) => number;
|
|
|
|
function int_provider(json: unknown): IntProvider {
|
|
if (typeof json === "number") return () => json;
|
|
const object = json as Record<string, unknown>;
|
|
switch (builtin_name(String(object.type))) {
|
|
case "constant": {
|
|
const value = object.value as number;
|
|
return () => value;
|
|
}
|
|
case "uniform": {
|
|
const min = object.min_inclusive as number;
|
|
const span = (object.max_inclusive as number) - min + 1;
|
|
return (chunk) => min + Math.floor(chunk.rng.next() * span);
|
|
}
|
|
default: {
|
|
const entries = (object.distribution as { data: unknown; weight: number }[]).map((entry) => ({
|
|
data: int_provider(entry.data),
|
|
weight: entry.weight,
|
|
}));
|
|
const total = entries.reduce((sum, entry) => sum + entry.weight, 0);
|
|
return (chunk) => {
|
|
let roll = Math.floor(chunk.rng.next() * total);
|
|
for (const entry of entries) {
|
|
if (roll < entry.weight) return entry.data(chunk);
|
|
roll -= entry.weight;
|
|
}
|
|
return entries.at(-1)!.data(chunk);
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
// heights: an anchor ({ "absolute" }, { "above_bottom" } or { "below_top" }), { "type": "constant", "value": anchor }
|
|
// or { "type": "uniform", "min_inclusive": anchor, "max_inclusive": anchor }
|
|
|
|
// the bottom and top are the chunk's dimension's
|
|
function anchor(json: unknown): IntProvider {
|
|
const object = json as Record<string, number>;
|
|
if (object.absolute !== undefined) return () => object.absolute;
|
|
if (object.above_bottom !== undefined) return (chunk) => chunk.min_y + object.above_bottom;
|
|
return (chunk) => chunk.max_y - 1 - object.below_top;
|
|
}
|
|
|
|
function height_provider(json: unknown): IntProvider {
|
|
const object = json as Record<string, unknown>;
|
|
if (object.type === undefined) return anchor(object);
|
|
if (builtin_name(String(object.type)) === "constant") return anchor(object.value);
|
|
const min = anchor(object.min_inclusive);
|
|
const max = anchor(object.max_inclusive);
|
|
return (chunk) => {
|
|
const low = min(chunk);
|
|
return low + Math.floor(chunk.rng.next() * (max(chunk) - low + 1));
|
|
};
|
|
}
|