Move world generation outside the engine and into mods and data files
This commit is contained in:
+35
-35
@@ -1,28 +1,33 @@
|
||||
// generating a chunk: the overworld's terrain (common/worldgen), then ores, then mods' features.
|
||||
// runs in chunk workers on the server and every client, which must all get the same blocks
|
||||
// generating a chunk: the terrain from the generator's data (common/worldgen), then ores, then mods' features.
|
||||
// only the server generates, players get finished chunks
|
||||
import { Alea } from "@paulaboks/rng";
|
||||
import { AIR, CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE, ID_MASK } from "$/common/constants.ts";
|
||||
import type { FeatureChunk } from "$/common/mod_api/worldgen.ts";
|
||||
import type { FeatureChunk, Position } from "$/common/mod_api/worldgen.ts";
|
||||
import type { OreJson } from "$/common/mod_data.ts";
|
||||
import { generate_overworld } from "./worldgen/overworld.ts";
|
||||
import { type Wood, WOODS } from "./worldgen/trees.ts";
|
||||
import type { Generator } from "./worldgen/generator.ts";
|
||||
import { named_noise_2d, named_noise_3d } from "./worldgen/noise.ts";
|
||||
|
||||
export { named_noise_2d, named_noise_3d };
|
||||
|
||||
// the base game's ores, placed like mods' ores.json. the world is 256 tall with the sea at 64
|
||||
const BASE_ORES: OreJson[] = [
|
||||
{ id: "bworld:coal_ore", replaces: "bworld:stone", min_y: 5, max_y: 200, scale: 0.05, threshold: 0.55 },
|
||||
{ id: "bworld:copper_ore", replaces: "bworld:stone", min_y: 5, max_y: 110, scale: 0.06, threshold: 0.6 },
|
||||
{ id: "bworld:tin_ore", replaces: "bworld:stone", min_y: 5, max_y: 70, scale: 0.06, threshold: 0.62 },
|
||||
{ id: "bworld:iron_ore", replaces: "bworld:stone", min_y: 5, max_y: 80, scale: 0.05, threshold: 0.65 },
|
||||
{ id: "bworld:gold_ore", replaces: "bworld:stone", min_y: 5, max_y: 36, scale: 0.04, threshold: 0.7 },
|
||||
];
|
||||
|
||||
// what mods add to generation, see docs/modding/worldgen/index.md
|
||||
export interface WorldgenSetup {
|
||||
ores: OreJson[];
|
||||
features: { id: string; generate: (chunk: FeatureChunk) => void }[];
|
||||
// the terrain, biomes and surface, from noise settings, see docs/modding/worldgen/terrain.md
|
||||
generator: Generator;
|
||||
// each mod's worldgen/ores.json, in load order. each is a pass of its own, so a mod's ores can replace an earlier
|
||||
// mod's, like the base game's
|
||||
ores: OreJson[][];
|
||||
// code and data features in the order they run, see load_worldgen
|
||||
features: SetupFeature[];
|
||||
// data features by id, for chunk.place_feature
|
||||
placeable: Map<string, (chunk: FeatureChunk, origin: Position) => boolean>;
|
||||
}
|
||||
|
||||
export interface SetupFeature {
|
||||
id: string;
|
||||
generate: (chunk: FeatureChunk) => void;
|
||||
// a code feature's biomes: it only runs in chunks with one of them. data features check each position themselves
|
||||
biomes?: Set<string>;
|
||||
}
|
||||
|
||||
export interface RawChunk {
|
||||
@@ -44,7 +49,7 @@ export function generate_raw_chunk(
|
||||
chunk_z: number,
|
||||
seed: string,
|
||||
block_ids: Record<string, number>,
|
||||
worldgen?: WorldgenSetup,
|
||||
worldgen: WorldgenSetup,
|
||||
// the value to store for each numeric id, with its default states. just the id when missing
|
||||
default_values?: number[],
|
||||
): RawChunk {
|
||||
@@ -73,25 +78,12 @@ export function generate_raw_chunk(
|
||||
const nid = block_ids[name];
|
||||
return nid === undefined ? AIR : default_values?.[nid] ?? nid;
|
||||
};
|
||||
// each wood's are named after it, like bworld:oak_log
|
||||
const wood_ids = (part: "log" | "leaves") =>
|
||||
Object.fromEntries(WOODS.map((wood) => [wood, id(`bworld:${wood}_${part}`)])) as Record<Wood, number>;
|
||||
generate_overworld(blocks, heights, biomes, chunk_x, chunk_z, seed, {
|
||||
stone: id("bworld:stone"),
|
||||
dirt: id("bworld:dirt"),
|
||||
grass: id("bworld:grass"),
|
||||
snow: id("bworld:snow_block"),
|
||||
water: id("bworld:water"),
|
||||
logs: wood_ids("log"),
|
||||
leaves: wood_ids("leaves"),
|
||||
block: id,
|
||||
}, (x, y, z, block) => spills.push(x, y, z, block));
|
||||
generate_ores(blocks, chunk_x, chunk_z, seed, block_ids, BASE_ORES, default_values);
|
||||
generate_overworld(blocks, heights, biomes, chunk_x, chunk_z, worldgen.generator.for_seed(seed), id);
|
||||
|
||||
if (worldgen) {
|
||||
generate_ores(blocks, chunk_x, chunk_z, seed, block_ids, worldgen.ores, default_values);
|
||||
generate_features(blocks, heights, biomes, set, chunk_x, chunk_z, seed, block_ids, worldgen.features);
|
||||
for (const ores of worldgen.ores) {
|
||||
generate_ores(blocks, chunk_x, chunk_z, seed, block_ids, ores, default_values);
|
||||
}
|
||||
generate_features(blocks, heights, biomes, set, chunk_x, chunk_z, seed, block_ids, worldgen);
|
||||
|
||||
return { blocks, spills: new Int32Array(spills), biomes };
|
||||
}
|
||||
@@ -148,7 +140,7 @@ function generate_features(
|
||||
chunk_z: number,
|
||||
seed: string,
|
||||
block_ids: Record<string, number>,
|
||||
features: WorldgenSetup["features"],
|
||||
worldgen: WorldgenSetup,
|
||||
) {
|
||||
const ids_by_nid: string[] = [];
|
||||
for (const [id, nid] of Object.entries(block_ids)) {
|
||||
@@ -164,12 +156,15 @@ function generate_features(
|
||||
return lz * CHUNK_SIZE + lx;
|
||||
};
|
||||
|
||||
for (const feature of features) {
|
||||
const chunk_biomes = new Set(biomes);
|
||||
for (const feature of worldgen.features) {
|
||||
if (feature.biomes && ![...feature.biomes].some((biome) => chunk_biomes.has(biome))) continue;
|
||||
const chunk: FeatureChunk = {
|
||||
x: chunk_x,
|
||||
z: chunk_z,
|
||||
seed,
|
||||
rng: new Alea(`${seed}_feature_${feature.id}_${chunk_x}_${chunk_z}`),
|
||||
random: (name) => new Alea(`${seed}_${name}`),
|
||||
noise_2d: (name) => named_noise_2d(seed, name),
|
||||
noise_3d: (name) => named_noise_3d(seed, name),
|
||||
height_at: (x, z) => heights[local(x, z, "height_at")],
|
||||
@@ -193,6 +188,11 @@ function generate_features(
|
||||
}
|
||||
set(x, y, z, nid);
|
||||
},
|
||||
place_feature(id, x, y, z) {
|
||||
const place = worldgen.placeable.get(id);
|
||||
if (!place) throw new Error(`place_feature: ${id} isn't a data feature`);
|
||||
return place(chunk, { x, y, z });
|
||||
},
|
||||
};
|
||||
try {
|
||||
feature.generate(chunk);
|
||||
|
||||
Reference in New Issue
Block a user