Move world generation outside the engine and into mods and data files
This commit is contained in:
@@ -61,7 +61,6 @@ import {
|
||||
number,
|
||||
word,
|
||||
} from "$/common/commands.ts";
|
||||
import { estimate_biome } from "$/common/worldgen/overworld.ts";
|
||||
import { biome_ids, get_biome } from "$/common/worldgen/biomes.ts";
|
||||
import type { GameLoop } from "./game_loop.ts";
|
||||
import { update_crafting_result } from "./crafting.ts";
|
||||
@@ -160,7 +159,7 @@ interface SavedTile {
|
||||
export interface GameMods {
|
||||
listings: ModListing[];
|
||||
recipes: RecipeBook;
|
||||
worldgen?: WorldgenSetup;
|
||||
worldgen: WorldgenSetup;
|
||||
runtime: ModRuntime;
|
||||
}
|
||||
|
||||
@@ -1304,7 +1303,7 @@ export class GameServer {
|
||||
for (let dz = -ring; dz <= ring; dz++) {
|
||||
if (Math.max(Math.abs(dx), Math.abs(dz)) !== ring) continue;
|
||||
const [cx, cz] = [x + dx * LOCATE_STEP, z + dz * LOCATE_STEP];
|
||||
const found = estimate_biome(this.world.seed, cx, cz);
|
||||
const found = this.world.estimate_biome(cx, cz);
|
||||
const distance = Math.hypot(dx, dz);
|
||||
if (found.biome === biome && (!best || distance < best.distance)) {
|
||||
best = { x: cx, y: found.y, z: cz, distance };
|
||||
|
||||
@@ -13,6 +13,10 @@ import { clear_hooks, expose_engine } from "$/common/hooks.ts";
|
||||
import { common_engine } from "$/common/engine_modules.ts";
|
||||
import { server_engine } from "./engine_modules.ts";
|
||||
|
||||
// the generator worlds use: a mod's worldgen/noise_settings, the base game's overworld. the server doesn't start
|
||||
// without it
|
||||
const GENERATOR = "bworld:overworld";
|
||||
|
||||
export interface ServerModSource {
|
||||
listing: ModListing;
|
||||
data: ModData;
|
||||
@@ -42,7 +46,12 @@ export async function start_game(
|
||||
? [{ mod: mod.listing.id, url: mod.worldgen_url, engine_access: mod.listing.engine_access }]
|
||||
: []
|
||||
);
|
||||
const worldgen = await load_worldgen(worldgen_scripts, recipes.ores);
|
||||
const worldgen = await load_worldgen({
|
||||
mods: mods.map((mod) => mod.listing.id),
|
||||
scripts: worldgen_scripts,
|
||||
ores: recipes.ores,
|
||||
generator: GENERATOR,
|
||||
});
|
||||
|
||||
const runtime = new ModRuntime();
|
||||
// before the world loads, its mobs can have effects
|
||||
|
||||
+13
-2
@@ -48,7 +48,7 @@ export function position_key(x: number, y: number, z: number) {
|
||||
export class ServerWorld {
|
||||
readonly seed: string;
|
||||
readonly block_ids: Record<string, number> = {};
|
||||
readonly worldgen: WorldgenSetup | undefined;
|
||||
readonly worldgen: WorldgenSetup;
|
||||
// what a freshly placed or generated block stores, by numeric id: the id with its default states
|
||||
readonly default_values: number[] = [];
|
||||
// for light_at, by numeric id
|
||||
@@ -85,7 +85,7 @@ export class ServerWorld {
|
||||
on_block_change?: (x: number, y: number, z: number, id: string, state: number) => void;
|
||||
on_tile_item_change?: (x: number, y: number, z: number, item: ItemData | undefined) => void;
|
||||
|
||||
constructor(seed: string, worldgen?: WorldgenSetup) {
|
||||
constructor(seed: string, worldgen: WorldgenSetup) {
|
||||
this.seed = seed;
|
||||
this.worldgen = worldgen;
|
||||
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
|
||||
@@ -173,6 +173,11 @@ export class ServerWorld {
|
||||
}
|
||||
|
||||
// like "bworld:plains", what generation decided
|
||||
// a column's biome without generating its chunk, a little off near coasts and cliffs, see OverworldTerrain
|
||||
estimate_biome(x: number, z: number): { biome: string; y: number } {
|
||||
return this.worldgen.generator.for_seed(this.seed).estimate_biome(x, z);
|
||||
}
|
||||
|
||||
biome_at(x: number, z: number): string {
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
@@ -209,6 +214,12 @@ export class ServerWorld {
|
||||
return found;
|
||||
}
|
||||
|
||||
// what generation makes in a chunk, neighbors' spills included, without what players changed. built again every
|
||||
// call, for comparing generation (tests/worldgen_golden_test.ts)
|
||||
generated_blocks(chunk_x: number, chunk_z: number): Uint32Array {
|
||||
return this.#generate_chunk(chunk_x, chunk_z);
|
||||
}
|
||||
|
||||
// whether every chunk within radius blocks of x, z is, see has_chunk
|
||||
has_chunks_around(x: number, z: number, radius: number) {
|
||||
for (let cx = Math.floor((x - radius) / CHUNK_SIZE); cx <= Math.floor((x + radius) / CHUNK_SIZE); cx++) {
|
||||
|
||||
@@ -108,6 +108,11 @@ function as_loaded_mod(file: string, bmod: Bmod): LoadedMod {
|
||||
tags: entries(bmod.data.tags),
|
||||
loot_tables: entries(bmod.data.loot_tables ?? []),
|
||||
biomes: entries(bmod.data.biomes ?? []),
|
||||
noises: entries(bmod.data.noises ?? []),
|
||||
density_functions: entries(bmod.data.density_functions ?? []),
|
||||
noise_settings: entries(bmod.data.noise_settings ?? []),
|
||||
biome_sources: entries(bmod.data.biome_sources ?? []),
|
||||
features: entries(bmod.data.features ?? []),
|
||||
textures: [...bmod.textures.keys()],
|
||||
texture_files: new Map(),
|
||||
texture_json: new Map(bmod.texture_json),
|
||||
|
||||
Reference in New Issue
Block a user