// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // everything generating a chunk reads, as one plain object a worker can be sent: the seed, the generator, and each // mod's data generation needs with its script's url. loading it registers the data in this realm and runs the // scripts' worldgen phases, which gives the same numeric block ids and the same generator as the game, so chunks come // out the same block for block. tests/worldgen_bundle_test.ts checks it with the golden fixture import type { ModData } from "./mod_loader.ts"; import { register_mod_data } from "./mod_loader.ts"; import { type BlockRegistry, EverythingRegistry } from "./everything_registry.ts"; import { default_block_value } from "./utils.ts"; import { generate_raw_chunk, type RawChunk, type WorldgenSetup } from "./generation.ts"; import { load_worldgen } from "./worldgen_loader.ts"; import { WorldShape, type WorldShapeJson } from "./world_shape.ts"; // the parts of a mod's data generation reads. tags only the block ones export type WorldgenModData = Pick< ModData, | "blocks" | "models" | "tags" | "ores" | "biomes" | "noises" | "density_functions" | "noise_settings" | "biome_sources" | "features" >; export interface WorldgenBundle { seed: string; // the noise settings the world uses generator: string; // the dimension's, see WorldShape shape: WorldShapeJson; // in load order // url is the mod's script, which worldgen workers run the worldgen phase of mods: { id: string; data: WorldgenModData; url?: string; engine_access?: boolean }[]; } export function worldgen_bundle( seed: string, generator: string, shape: WorldShapeJson, mods: { id: string; data: ModData; url?: string; engine_access?: boolean }[], ): WorldgenBundle { return { seed, generator, shape: { min_y: shape.min_y, max_y: shape.max_y, sky_light: shape.sky_light }, mods: mods.map(({ id, data, url, engine_access }) => ({ id, data: { blocks: data.blocks, models: data.models, tags: data.tags.filter((tag) => tag.registry === "blocks"), ores: data.ores, biomes: data.biomes ?? [], noises: data.noises ?? [], density_functions: data.density_functions ?? [], noise_settings: data.noise_settings ?? [], biome_sources: data.biome_sources ?? [], features: data.features ?? [], }, ...(url ? { url } : {}), ...(engine_access ? { engine_access } : {}), })), }; } // numeric block ids by id, and each one's value with its default states, the way ServerWorld numbers them export function block_numbering(): { block_ids: Record; default_values: number[] } { const block_ids: Record = {}; const default_values: number[] = []; EverythingRegistry.get_registry("blocks").forEach((block, nid) => { block_ids[block.id] = nid; default_values[nid] = default_block_value(nid, block); }); return { block_ids, default_values }; } export interface LoadedBundle { setup: WorldgenSetup; block_ids: Record; default_values: number[]; generate(chunk_x: number, chunk_z: number): RawChunk; } // sets up generation in this realm from a bundle. the registry has to be empty, like a worker's export async function load_bundle(bundle: WorldgenBundle): Promise { const recipes = register_mod_data(bundle.mods.map(({ id, data }) => ({ id, data: { ...data, items: [], recipes: [], spawns: [], entities: [], entity_models: [], animations: [], sounds: [], effects: [], fluids: [], loot_tables: [], }, }))); const setup = await load_worldgen({ mods: bundle.mods.map((mod) => mod.id), scripts: bundle.mods.flatMap((mod) => mod.url ? [{ mod: mod.id, url: mod.url, engine_access: mod.engine_access }] : [] ), ores: recipes.ores, generator: bundle.generator, shape: new WorldShape(bundle.shape), }); const { block_ids, default_values } = block_numbering(); return { setup, block_ids, default_values, generate: (chunk_x, chunk_z) => generate_raw_chunk(chunk_x, chunk_z, bundle.seed, block_ids, setup, default_values), }; }