Files
2026-11-14 12:18:41 -03:00

77 lines
3.4 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// what worldgen phases get, see docs/modding/worldgen/index.md. runs on the server and must be deterministic
import type { Id, Position } from "./shared.ts";
export type { Id, Position } from "./shared.ts";
export interface WorldgenContext {
register_terrain(id: Id, generate: (chunk: TerrainChunk) => void): void;
// a feature that's code, run on every chunk. see FeatureOptions for when
register_feature(id: Id, generate: (chunk: FeatureChunk) => void, options?: FeatureOptions): void;
// a type of feature data features can use: places config at origin, returns whether it placed anything
// deno-lint-ignore no-explicit-any
register_feature_type<C = any>(id: Id, place: (chunk: FeatureChunk, config: C, origin: Position) => boolean): void;
// a placement modifier data features can use: the positions after it, from the ones before. config is its json
register_placement_modifier<C = Record<string, unknown>>(
id: Id,
modify: (chunk: FeatureChunk, config: C, positions: Position[]) => Position[],
): void;
}
// the order features run in, minecraft's decoration steps. features without one run in the last
export type FeatureStep =
| "raw_generation"
| "lakes"
| "local_modifications"
| "underground_structures"
| "surface_structures"
| "strongholds"
| "underground_ores"
| "underground_decoration"
| "fluid_springs"
| "vegetal_decoration"
| "top_layer_modification";
export interface FeatureOptions {
step?: FeatureStep;
// only runs in chunks with one of these biomes somewhere, at any height
biomes?: Id | Id[];
}
export interface FeatureChunk {
// chunk coordinates
readonly x: number;
readonly z: number;
readonly seed: string;
// the dimension's blocks go from min_y up to max_y - 1, get_block is undefined outside them and set_block does
// nothing
readonly min_y: number;
readonly max_y: number;
// seeded from the seed, chunk and feature id
readonly rng: { next(): number };
// seeded from the seed and name alone, the same in every chunk. for things decided across chunks, like trees
// spread over a grid of cells
random(name: string): { next(): number };
// create_noise_2d(new Alea(seed + "_" + name)), cached per seed and name
noise_2d(name: string): (x: number, z: number) => number;
noise_3d(name: string): (x: number, y: number, z: number) => number;
// the column's surface height, inside this chunk only
height_at(x: number, z: number): number;
// the biome at a block, like neoforge's level.getBiome(pos), inside this chunk only. biomes are one per block
// across and one every 4 blocks up, see docs/modding/worldgen/biomes.md. above and below the world it's the one at
// its top or bottom
biome_at(x: number, y: number, z: number): Id;
// sets the biome of the 4 blocks tall cell y is in, inside this chunk only. nothing outside the world's height
set_biome(x: number, y: number, z: number, biome: Id): void;
get_block(x: number, y: number, z: number): Id | undefined; // inside this chunk only
set_block(x: number, y: number, z: number, id: Id): void; // up to one chunk away, like trees
// a data feature's type and config at x, y, z, without its placement. whether it placed anything
place_feature(id: Id, x: number, y: number, z: number): boolean;
}
export interface TerrainChunk extends FeatureChunk {
set_height(x: number, z: number, height: number): void;
}