Move world generation outside the engine and into mods and data files
This commit is contained in:
@@ -0,0 +1,601 @@
|
||||
// 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_HEIGHT, CHUNK_SIZE } from "../constants.ts";
|
||||
import { ID_PATTERN, TAG_ID_PATTERN } from "../mod_data.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 column is one of these biomes
|
||||
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>;
|
||||
}
|
||||
|
||||
export function validate_feature(json: unknown): string[] {
|
||||
const problems: string[] = [];
|
||||
feature_refs(json, { blocks: new Set(), biomes: new Set(), custom: new Set() }, problems);
|
||||
return problems;
|
||||
}
|
||||
|
||||
export function feature_refs(json: unknown, refs: FeatureRefs, problems: string[] = []) {
|
||||
if (!is_object(json)) {
|
||||
problems.push("feature must be an object");
|
||||
return;
|
||||
}
|
||||
if (!(typeof json.id === "string" && TAG_ID_PATTERN.test(json.id))) {
|
||||
problems.push(`id must be an id like "my_mod:thing" or "my_mod:folder/thing"`);
|
||||
}
|
||||
if (json.step !== undefined && !FEATURE_STEPS.includes(json.step as FeatureStep)) {
|
||||
problems.push(`step must be one of ${FEATURE_STEPS.join(", ")}`);
|
||||
}
|
||||
if (json.biomes !== undefined) {
|
||||
const biomes = typeof json.biomes === "string" ? [json.biomes] : json.biomes;
|
||||
if (!Array.isArray(biomes) || !biomes.every((b) => typeof b === "string" && ID_PATTERN.test(b))) {
|
||||
problems.push("biomes must be a biome's id or a list of them");
|
||||
} else {
|
||||
for (const biome of biomes) refs.biomes.add(biome);
|
||||
}
|
||||
}
|
||||
if (json.config !== undefined && !is_object(json.config)) problems.push("config must be an object");
|
||||
if (typeof json.type !== "string") {
|
||||
problems.push("type must be a feature type");
|
||||
} else {
|
||||
const name = builtin_name(json.type);
|
||||
if (name === undefined) refs.custom.add(json.type);
|
||||
else if (!(name in FEATURE_TYPES)) problems.push(`type ${json.type} isn't built in`);
|
||||
else FEATURE_TYPES[name].check((json.config ?? {}) as Record<string, unknown>, "config", refs, problems);
|
||||
}
|
||||
if (json.placement !== undefined) {
|
||||
if (!Array.isArray(json.placement)) {
|
||||
problems.push("placement must be a list of placement modifiers");
|
||||
return;
|
||||
}
|
||||
json.placement.forEach((modifier: unknown, i: number) => {
|
||||
const at = `placement[${i}]`;
|
||||
if (!is_object(modifier) || typeof modifier.type !== "string") {
|
||||
problems.push(`${at}: must be { "type": ... }`);
|
||||
return;
|
||||
}
|
||||
const name = builtin_name(modifier.type);
|
||||
if (name === undefined) refs.custom.add(modifier.type);
|
||||
else if (!(name in MODIFIERS)) problems.push(`${at}: ${modifier.type} isn't a built in placement modifier`);
|
||||
else MODIFIERS[name].check(modifier, at, refs, problems);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 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.z)))
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
place(chunk, position);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// built in feature types
|
||||
|
||||
interface Builtin<T> {
|
||||
check(json: Record<string, unknown>, at: string, refs: FeatureRefs, problems: string[]): 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: {
|
||||
check(json, at, refs, problems) {
|
||||
check_state(json.to_place, `${at}.to_place`, refs, problems);
|
||||
},
|
||||
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: {
|
||||
check(json, at, refs, problems) {
|
||||
if (!Array.isArray(json.layers) || json.layers.length === 0) {
|
||||
problems.push(`${at}.layers must be a list of { "height", "provider" }`);
|
||||
} else {
|
||||
json.layers.forEach((layer: unknown, i: number) => {
|
||||
const here = `${at}.layers[${i}]`;
|
||||
if (!is_object(layer)) {
|
||||
problems.push(`${here} must be { "height", "provider" }`);
|
||||
return;
|
||||
}
|
||||
check_int(layer.height, `${here}.height`, problems);
|
||||
check_state(layer.provider, `${here}.provider`, refs, problems);
|
||||
});
|
||||
}
|
||||
if (json.direction !== undefined && json.direction !== "up" && json.direction !== "down") {
|
||||
problems.push(`${at}.direction must be "up" or "down"`);
|
||||
}
|
||||
if (json.allowed_placement !== undefined) {
|
||||
check_predicate(json.allowed_placement, `${at}.allowed_placement`, refs, problems);
|
||||
}
|
||||
if (json.prioritize_tip !== undefined && typeof json.prioritize_tip !== "boolean") {
|
||||
problems.push(`${at}.prioritize_tip must be true or false`);
|
||||
}
|
||||
},
|
||||
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"];
|
||||
|
||||
const MODIFIERS: Record<string, Builtin<Modifier>> = {
|
||||
// each position becomes count of them
|
||||
count: {
|
||||
check(json, at, _refs, problems) {
|
||||
check_int(json.count, `${at}.count`, problems);
|
||||
},
|
||||
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: {
|
||||
check(json, at, _refs, problems) {
|
||||
if (!(Number.isInteger(json.chance) && (json.chance as number) >= 1)) {
|
||||
problems.push(`${at}.chance must be a whole number, 1 or more`);
|
||||
}
|
||||
},
|
||||
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: {
|
||||
check() {},
|
||||
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: {
|
||||
check(json, at, _refs, problems) {
|
||||
check_int(json.xz_spread ?? 0, `${at}.xz_spread`, problems);
|
||||
check_int(json.y_spread ?? 0, `${at}.y_spread`, problems);
|
||||
},
|
||||
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: {
|
||||
check(json, at, _refs, problems) {
|
||||
check_height(json.height, `${at}.height`, problems);
|
||||
},
|
||||
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 liquid.
|
||||
// positions outside the chunk are dropped
|
||||
heightmap: {
|
||||
check(json, at, _refs, problems) {
|
||||
if (!HEIGHTMAPS.includes(json.heightmap as string)) {
|
||||
problems.push(`${at}.heightmap must be one of ${HEIGHTMAPS.join(", ")}`);
|
||||
}
|
||||
},
|
||||
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_HEIGHT - 1;
|
||||
while (y >= 0 && !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: {
|
||||
check(json, at, refs, problems) {
|
||||
check_predicate(json.predicate, `${at}.predicate`, refs, problems);
|
||||
},
|
||||
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)?.liquid;
|
||||
}
|
||||
|
||||
// block predicates, like minecraft's
|
||||
|
||||
type Predicate = (chunk: FeatureChunk, x: number, y: number, z: number) => boolean;
|
||||
const PREDICATES = ["matching_blocks", "solid", "replaceable", "would_survive", "not", "all_of", "any_of", "true"];
|
||||
|
||||
function check_predicate(json: unknown, at: string, refs: FeatureRefs, problems: string[]) {
|
||||
if (!is_object(json) || typeof json.type !== "string") {
|
||||
problems.push(`${at} must be a block predicate, { "type": ... }`);
|
||||
return;
|
||||
}
|
||||
const name = builtin_name(json.type);
|
||||
if (name === undefined || !PREDICATES.includes(name)) {
|
||||
problems.push(`${at}: ${json.type} isn't one of ${PREDICATES.join(", ")}`);
|
||||
return;
|
||||
}
|
||||
if (
|
||||
json.offset !== undefined &&
|
||||
!(Array.isArray(json.offset) && json.offset.length === 3 && json.offset.every(Number.isInteger))
|
||||
) {
|
||||
problems.push(`${at}.offset must be [x, y, z], whole numbers`);
|
||||
}
|
||||
switch (name) {
|
||||
case "matching_blocks": {
|
||||
const blocks = typeof json.blocks === "string" ? [json.blocks] : json.blocks;
|
||||
if (!Array.isArray(blocks) || !blocks.every((b) => typeof b === "string")) {
|
||||
problems.push(`${at}.blocks must be a block, a #tag, or a list of them`);
|
||||
} else {
|
||||
for (const block of blocks) refs.blocks.add(block);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "would_survive":
|
||||
check_state(json.state, `${at}.state`, refs, problems);
|
||||
break;
|
||||
case "not":
|
||||
check_predicate(json.predicate, `${at}.predicate`, refs, problems);
|
||||
break;
|
||||
case "all_of":
|
||||
case "any_of":
|
||||
if (!Array.isArray(json.predicates)) problems.push(`${at}.predicates must be a list`);
|
||||
else json.predicates.forEach((p, i) => check_predicate(p, `${at}.predicates[${i}]`, refs, problems));
|
||||
}
|
||||
}
|
||||
|
||||
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 < 0 || y >= CHUNK_HEIGHT) 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 check_state(json: unknown, at: string, refs: FeatureRefs, problems: string[]) {
|
||||
const id = typeof json === "string"
|
||||
? json
|
||||
: is_object(json) && is_object(json.state)
|
||||
? json.state.Name
|
||||
: is_object(json)
|
||||
? json.Name
|
||||
: undefined;
|
||||
if (typeof id === "string" && ID_PATTERN.test(id)) refs.blocks.add(id);
|
||||
else problems.push(`${at} must be a block's id or { "type": "simple_state_provider", "state": { "Name": ... } }`);
|
||||
}
|
||||
|
||||
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 check_int(json: unknown, at: string, problems: string[]) {
|
||||
if (Number.isInteger(json)) return;
|
||||
if (!is_object(json)) {
|
||||
problems.push(`${at} must be a whole number or { "type": "uniform", ... }`);
|
||||
return;
|
||||
}
|
||||
switch (builtin_name(String(json.type))) {
|
||||
case "constant":
|
||||
if (!Number.isInteger(json.value)) problems.push(`${at}.value must be a whole number`);
|
||||
break;
|
||||
case "uniform":
|
||||
if (!Number.isInteger(json.min_inclusive) || !Number.isInteger(json.max_inclusive)) {
|
||||
problems.push(`${at} needs whole numbers min_inclusive and max_inclusive`);
|
||||
} else if ((json.min_inclusive as number) > (json.max_inclusive as number)) {
|
||||
problems.push(`${at}: min_inclusive is above max_inclusive`);
|
||||
}
|
||||
break;
|
||||
case "weighted_list":
|
||||
if (!Array.isArray(json.distribution) || json.distribution.length === 0) {
|
||||
problems.push(`${at}.distribution must be a list of { "data", "weight" }`);
|
||||
} else {
|
||||
json.distribution.forEach((entry: unknown, i: number) => {
|
||||
if (!is_object(entry) || !(Number.isInteger(entry.weight) && (entry.weight as number) > 0)) {
|
||||
problems.push(`${at}.distribution[${i}] needs a whole number weight above 0`);
|
||||
} else {
|
||||
check_int(entry.data, `${at}.distribution[${i}].data`, problems);
|
||||
}
|
||||
});
|
||||
}
|
||||
break;
|
||||
default:
|
||||
problems.push(`${at}.type must be constant, uniform or weighted_list`);
|
||||
}
|
||||
}
|
||||
|
||||
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 }
|
||||
|
||||
function check_height(json: unknown, at: string, problems: string[]) {
|
||||
if (!is_object(json)) {
|
||||
problems.push(`${at} must be a height, like { "absolute": 10 } or { "type": "uniform", ... }`);
|
||||
return;
|
||||
}
|
||||
if (json.type === undefined) return check_anchor(json, at, problems);
|
||||
switch (builtin_name(String(json.type))) {
|
||||
case "constant":
|
||||
check_anchor(json.value, `${at}.value`, problems);
|
||||
break;
|
||||
case "uniform":
|
||||
check_anchor(json.min_inclusive, `${at}.min_inclusive`, problems);
|
||||
check_anchor(json.max_inclusive, `${at}.max_inclusive`, problems);
|
||||
break;
|
||||
default:
|
||||
problems.push(`${at}.type must be constant or uniform`);
|
||||
}
|
||||
}
|
||||
|
||||
function check_anchor(json: unknown, at: string, problems: string[]) {
|
||||
const keys = ["absolute", "above_bottom", "below_top"];
|
||||
if (
|
||||
!is_object(json) || Object.keys(json).length !== 1 || !keys.includes(Object.keys(json)[0]) ||
|
||||
!Number.isInteger(Object.values(json)[0])
|
||||
) {
|
||||
problems.push(`${at} must be one of { "absolute": y }, { "above_bottom": n } or { "below_top": n }`);
|
||||
}
|
||||
}
|
||||
|
||||
function anchor(json: unknown): number {
|
||||
const object = json as Record<string, number>;
|
||||
if (object.absolute !== undefined) return object.absolute;
|
||||
if (object.above_bottom !== undefined) return object.above_bottom;
|
||||
return CHUNK_HEIGHT - 1 - object.below_top;
|
||||
}
|
||||
|
||||
function height_provider(json: unknown): IntProvider {
|
||||
const object = json as Record<string, unknown>;
|
||||
if (object.type === undefined) {
|
||||
const y = anchor(object);
|
||||
return () => y;
|
||||
}
|
||||
if (builtin_name(String(object.type)) === "constant") {
|
||||
const y = anchor(object.value);
|
||||
return () => y;
|
||||
}
|
||||
const min = anchor(object.min_inclusive);
|
||||
const span = anchor(object.max_inclusive) - min + 1;
|
||||
return (chunk) => min + Math.floor(chunk.rng.next() * span);
|
||||
}
|
||||
|
||||
function is_object(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
Reference in New Issue
Block a user