Files
bworld/common/worldgen/surface_rules.ts
T
2026-11-13 00:01:59 -03:00

143 lines
4.6 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// what solid blocks and empty space become, from the noise settings' surface_rule and fluids (see data.ts), compiled
// for each chunk with its block ids. like minecraft's surface rules: a rule gives a block or nothing, and a sequence
// gives its first rule's block
import { type FluidJson, range_test, type SurfaceConditionJson, type SurfaceRuleJson } from "./data.ts";
import type { NoiseTerrain } from "./terrain.ts";
// a biome's surface as numeric ids
export interface SurfaceBlocks {
top: number;
filler: number;
filler_depth: number;
underwater_top: number;
underwater_filler: number;
bare_cliffs: boolean;
}
// the block being decided, one context reused for the whole chunk
export interface SurfaceContext {
x: number;
y: number;
z: number;
// solid blocks above it before air, 0 for one with air on top
depth: number;
// the column's ground, the first solid block from the top, is below sea level - 1
underwater: boolean;
steep: boolean;
// the column's height before 3d noise
preliminary: number;
biome: string;
surface: SurfaceBlocks;
}
type Rule = (context: SurfaceContext) => number | undefined;
type Condition = (context: SurfaceContext) => boolean;
export function compile_surface_rule(
json: SurfaceRuleJson,
terrain: NoiseTerrain,
block: (id: string) => number,
): Rule {
switch (json.type) {
case "sequence": {
const rules = json.sequence.map((rule) => compile_surface_rule(rule, terrain, block));
return (context) => {
for (const rule of rules) {
const result = rule(context);
if (result !== undefined) return result;
}
return undefined;
};
}
case "condition": {
const condition = compile_condition(json.if_true, terrain);
const then = compile_surface_rule(json.then_run, terrain, block);
return (context) => condition(context) ? then(context) : undefined;
}
case "block": {
const id = block(json.result_state.Name);
return () => id;
}
case "biome_surface": {
const part = json.part;
return (context) => context.surface[part];
}
}
}
function compile_condition(json: SurfaceConditionJson, terrain: NoiseTerrain): Condition {
switch (json.type) {
case "stone_depth": {
const offset = json.offset;
return (context) => context.depth <= offset;
}
case "filler_depth":
return (context) => context.depth <= context.surface.filler_depth;
case "above_preliminary_surface": {
const offset = json.offset ?? 0;
return (context) => context.y >= context.preliminary + offset;
}
case "surface_underwater":
return (context) => context.underwater;
case "steep":
return (context) => context.steep;
case "bare_cliffs":
return (context) => context.surface.bare_cliffs;
case "y_at_least": {
const at = terrain.compiler.compile(json.function);
return (context) => context.y >= at(context.x, context.y, context.z);
}
case "density": {
const value = terrain.compiler.compile(json.function);
const test = range_test(json);
return (context) => test(value(context.x, context.y, context.z));
}
case "biome": {
const biomes = new Set(json.biome_is);
return (context) => biomes.has(context.biome);
}
case "not": {
const inner = compile_condition(json.invert, terrain);
return (context) => !inner(context);
}
}
}
// how far above the preliminary surface the rule starts giving blocks, when its outermost condition says, so the
// blocks below it can skip it. -Infinity when it doesn't say
export function surface_rule_reach(json: SurfaceRuleJson): number {
if (json.type === "condition" && json.if_true.type === "above_preliminary_surface") {
return json.if_true.offset ?? 0;
}
return -Infinity;
}
// the fluid empty space at the context's y becomes, if any. open_sky is whether there's only air above it
export function compile_fluids(
json: FluidJson[],
terrain: NoiseTerrain,
block: (id: string) => number,
): (context: SurfaceContext, open_sky: boolean) => number | undefined {
const fluids = json.map((fluid) => {
const id = block(fluid.block);
const below = fluid.below;
if (fluid.where === "open_sky") {
return (context: SurfaceContext, open_sky: boolean) => open_sky && context.y < below ? id : undefined;
}
const where = terrain.compiler.compile(fluid.where.function);
const test = range_test(fluid.where);
return (context: SurfaceContext) =>
context.y < below && test(where(context.x, context.y, context.z)) ? id : undefined;
});
return (context, open_sky) => {
for (const fluid of fluids) {
const id = fluid(context, open_sky);
if (id !== undefined) return id;
}
return undefined;
};
}