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
+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;