Move world generation outside the engine and into mods and data files

This commit is contained in:
2026-10-11 00:33:59 -03:00
parent afe6e03d4c
commit 3d2b235f64
118 changed files with 8250 additions and 1108 deletions
+17
View File
@@ -22,6 +22,13 @@ import { png_size, texture_size_problem } from "$/common/bmod.ts";
import { type TextureJson, validate_texture_json } from "$/common/texture_animation.ts";
import { type LootTableJson, validate_loot_table } from "$/common/loot.ts";
import { validate_biome } from "$/common/worldgen/biomes.ts";
import {
validate_biome_source,
validate_density_function,
validate_noise,
validate_noise_settings,
} from "$/common/worldgen/data.ts";
import { validate_feature } from "$/common/worldgen/features.ts";
import { dirname } from "@std/path";
export { load_order, type LoadedMod, type ModReport };
@@ -84,6 +91,11 @@ function load_mod(mods_dir: string, id: string): LoadedMod {
tags: [],
loot_tables: [],
biomes: [],
noises: [],
density_functions: [],
noise_settings: [],
biome_sources: [],
features: [],
textures: [],
texture_files: new Map(),
texture_json: new Map(),
@@ -150,6 +162,11 @@ function load_mod(mods_dir: string, id: string): LoadedMod {
load_data("entities", "entity", validate_entity, mod.entities);
load_data("effects", "effect", validate_effect, mod.effects);
load_data("worldgen/biomes", "biome", validate_biome, mod.biomes);
load_data("worldgen/noise", "noise", validate_noise, mod.noises);
load_data("worldgen/density_function", "density_function", validate_density_function, mod.density_functions);
load_data("worldgen/noise_settings", "noise_settings", validate_noise_settings, mod.noise_settings);
load_data("worldgen/biome_source", "biome_source", validate_biome_source, mod.biome_sources);
load_data("worldgen/features", "feature", validate_feature, mod.features);
for (const file of walk(`${dir}/models`, ".geo.json")) {
const path = `models/${file}`;
+1 -1
View File
@@ -1,4 +1,4 @@
// measures the percentiles of octave noise, for the Quantiles tables in common/worldgen/terrain.ts.
// measures the percentiles of octave noise, for the "quantiles" of a noise file (worldgen/noise in a mod).
// they only depend on the amplitudes. deno run tools/noise_quantiles.ts '[1,1,2,2,1,1]' '[1,2,1]'
import { OctaveNoise2D } from "$/common/worldgen/noise.ts";
+5
View File
@@ -44,6 +44,11 @@ export async function pack_mod(mod: LoadedMod): Promise<Uint8Array<ArrayBuffer>>
tags: mod.tags.map((t) => t.json),
loot_tables: mod.loot_tables.map((t) => t.json),
biomes: mod.biomes.map((b) => b.json),
noises: mod.noises.map((n) => n.json),
density_functions: mod.density_functions.map((d) => d.json),
noise_settings: mod.noise_settings.map((n) => n.json),
biome_sources: mod.biome_sources.map((b) => b.json),
features: mod.features.map((f) => f.json),
},
scripts,
textures,
+47 -10
View File
@@ -5,19 +5,47 @@
// section.png: a slice down through the world along x, showing caves, overhangs and underground rivers
import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE, SEA_LEVEL } from "$/common/constants.ts";
import { generate_raw_chunk } from "$/common/generation.ts";
import { OverworldTerrain } from "$/common/worldgen/terrain.ts";
import { pick_biome } from "$/common/worldgen/overworld.ts";
import { register_biome } from "$/common/worldgen/biomes.ts";
import { load_worldgen } from "$/common/worldgen_loader.ts";
import { register_mod_data } from "$/common/mod_loader.ts";
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import { load_and_check } from "./check_mods.ts";
const seed = Deno.args[0] ?? "preview";
const out = Deno.args[1] ?? ".";
const [center_x, center_z] = [Number(Deno.args[2] ?? 0), Number(Deno.args[3] ?? 0)];
// the base game's biomes, from mods/bworld
for (const mod of load_and_check()) {
if (mod.id === "bworld") { for (const { json } of mod.biomes) register_biome(json); }
}
// the base game, from mods/bworld: its data registered like the game does, and its worldgen script for trees and the
// rest of its features
const bworld = load_and_check().find((mod) => mod.id === "bworld")!;
const jsons = <T>(list: { json: T }[]) => list.map((entry) => entry.json);
register_mod_data([{
id: "bworld",
data: {
blocks: jsons(bworld.blocks),
models: jsons(bworld.models),
items: jsons(bworld.items),
recipes: jsons(bworld.recipes),
ores: jsons(bworld.ores),
spawns: jsons(bworld.spawns),
entities: jsons(bworld.entities),
entity_models: jsons(bworld.entity_models),
effects: jsons(bworld.effects),
tags: jsons(bworld.tags),
loot_tables: jsons(bworld.loot_tables),
biomes: jsons(bworld.biomes),
noises: jsons(bworld.noises),
density_functions: jsons(bworld.density_functions),
noise_settings: jsons(bworld.noise_settings),
biome_sources: jsons(bworld.biome_sources),
features: jsons(bworld.features),
},
}]);
const worldgen = await load_worldgen({
mods: ["bworld"],
scripts: [{ mod: "bworld", url: new URL("../mods/bworld/scripts/worldgen.ts", import.meta.url).href }],
ores: [jsons(bworld.ores)],
generator: "bworld:overworld",
});
const NAMES = [
"bworld:stone",
@@ -51,6 +79,15 @@ const COLORS: Record<number, [number, number, number]> = {
12: [100, 70, 40],
13: [45, 110, 35],
};
// every other block after them, so features can place them. other woods look like oak, the rest magenta
for (const block of EverythingRegistry.get_registry<BlockRegistry>("blocks").filter((b) => b)) {
if (block.id in block_ids) continue;
const nid = Object.keys(block_ids).length + 1;
block_ids[block.id] = nid;
if (block.id.endsWith("_log")) COLORS[nid] = COLORS[block_ids["bworld:oak_log"]];
else if (block.id.endsWith("_leaves")) COLORS[nid] = COLORS[block_ids["bworld:oak_leaves"]];
else if (block.id === "bworld:cactus") COLORS[nid] = [70, 140, 50];
}
const BIOME_COLORS: Record<string, [number, number, number]> = {
"bworld:deep_ocean": [20, 40, 120],
@@ -135,7 +172,7 @@ const shade = (color: [number, number, number], factor: number): [number, number
{
const size = 1024;
const step = 4;
const terrain = new OverworldTerrain(seed);
const terrain = worldgen.generator.for_seed(seed);
const heights = new Float32Array(size * size);
const rgb = new Uint8Array(size * size * 3);
const started = performance.now();
@@ -146,7 +183,7 @@ const shade = (color: [number, number, number], factor: number): [number, number
const column = terrain.column(x, z);
heights[pz * size + px] = column.height;
const surface = Math.round(column.height);
const biome = pick_biome(column, surface, false);
const biome = terrain.biome(column, surface, false);
const color = BIOME_COLORS[biome] ?? [255, 0, 255];
rgb.set(color, (pz * size + px) * 3);
}
@@ -183,7 +220,7 @@ const shade = (color: [number, number, number], factor: number): [number, number
for (let cx = 0; cx < chunks; cx++) {
const chunk_x = origin / CHUNK_SIZE + cx + center_x;
const chunk_z = origin / CHUNK_SIZE + cz + center_z;
const { blocks } = generate_raw_chunk(chunk_x, chunk_z, seed, block_ids);
const { blocks } = generate_raw_chunk(chunk_x, chunk_z, seed, block_ids, worldgen);
for (let lz = 0; lz < CHUNK_SIZE; lz++) {
for (let lx = 0; lx < CHUNK_SIZE; lx++) {
const px = cx * CHUNK_SIZE + lx;