More changes, im going to bed

This commit is contained in:
2026-11-06 00:17:27 -03:00
parent 9bec0f3718
commit 7458a2d42a
13 changed files with 35 additions and 53 deletions
+2 -3
View File
@@ -20,9 +20,8 @@ What world generation has today (see [World Generation](docs/modding/worldgen/in
- **Determinism matters at chunk borders.** Every chunk generated is saved whole, like Minecraft, so a change to - **Determinism matters at chunk borders.** Every chunk generated is saved whole, like Minecraft, so a change to
generation only shows in chunks never generated before. A chunk generated next to a saved one still takes the saved generation only shows in chunks never generated before. A chunk generated next to a saved one still takes the saved
one's spills from generation, so features cross the border the same way only when generation hasn't changed. one's spills from generation, so features cross the border the same way only when generation hasn't changed.
- **Heights and biomes for any column**, without generating its chunk: `OverworldTerrain.column` and `estimate_biome`. - **Heights and biomes for any column**, without generating its chunk: `NoiseTerrain.column` and `estimate_biome`. The
The biome is exact away from coasts and cliffs. The height is the height before 3D noise, so it isn't the real biome is exact away from coasts and cliffs. The height is the height before 3D noise, so it isn't the real surface.
surface.
- **Blocks:** numeric states (up to 16 bits), `variants`, `parts`, shapes, tile data, containers, components and shared - **Blocks:** numeric states (up to 16 bits), `variants`, `parts`, shapes, tile data, containers, components and shared
rules. Tags exist for blocks, items and entities. Biomes are a registry of data (`worldgen/biomes/` in a mod), but rules. Tags exist for blocks, items and entities. Biomes are a registry of data (`worldgen/biomes/` in a mod), but
which one a column gets is data too, a biome source's rules (see [biomes.md](docs/modding/worldgen/biomes.md)). which one a column gets is data too, a biome source's rules (see [biomes.md](docs/modding/worldgen/biomes.md)).
+2 -2
View File
@@ -7,7 +7,7 @@ import { Container, ContainerSlot, ItemStack } from "./inventory.ts";
import { BlockRules } from "./block_rules.ts"; import { BlockRules } from "./block_rules.ts";
import { Tags } from "./tags.ts"; import { Tags } from "./tags.ts";
import { CommandDispatcher } from "./commands.ts"; import { CommandDispatcher } from "./commands.ts";
import { OverworldTerrain } from "./worldgen/terrain.ts"; import { NoiseTerrain } from "./worldgen/terrain.ts";
import { physics } from "./physics.ts"; import { physics } from "./physics.ts";
// built when it's exposed, not when this module loads, so import cycles can't leave anything undefined // built when it's exposed, not when this module loads, so import cycles can't leave anything undefined
@@ -20,7 +20,7 @@ export function common_engine() {
BlockRules, BlockRules,
Tags, Tags,
CommandDispatcher, CommandDispatcher,
OverworldTerrain, NoiseTerrain,
physics, physics,
}; };
} }
+2 -2
View File
@@ -7,7 +7,7 @@ import { Alea } from "@paulaboks/rng";
import { AIR, CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts"; import { AIR, CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import type { FeatureChunk, Position } 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 type { OreJson } from "@/common/mod_data.ts";
import { generate_overworld } from "./worldgen/overworld.ts"; import { generate_noise_chunk } from "./worldgen/noise_chunk.ts";
import type { Generator } from "./worldgen/generator.ts"; import type { Generator } from "./worldgen/generator.ts";
import { named_noise_2d, named_noise_3d } from "./worldgen/noise.ts"; import { named_noise_2d, named_noise_3d } from "./worldgen/noise.ts";
@@ -81,7 +81,7 @@ export function generate_raw_chunk(
const nid = block_ids[name]; const nid = block_ids[name];
return nid === undefined ? AIR : default_values?.[nid] ?? nid; return nid === undefined ? AIR : default_values?.[nid] ?? nid;
}; };
generate_overworld(blocks, heights, biomes, chunk_x, chunk_z, worldgen.generator.for_seed(seed), id); generate_noise_chunk(blocks, heights, biomes, chunk_x, chunk_z, worldgen.generator.for_seed(seed), id);
for (const ores of worldgen.ores) { for (const ores of worldgen.ores) {
generate_ores(blocks, chunk_x, chunk_z, seed, block_ids, ores, default_values); generate_ores(blocks, chunk_x, chunk_z, seed, block_ids, ores, default_values);
+5 -5
View File
@@ -2,11 +2,11 @@
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
// a generator: noise settings, and the density functions, noises and biome rules it uses. compiled once for each // a generator: noise settings, and the density functions, noises and biome rules it uses. compiled once for each
// seed, see OverworldTerrain. the data comes from mods' worldgen folders, see docs/modding/worldgen/terrain.md // seed, see NoiseTerrain. the data comes from mods' worldgen folders, see docs/modding/worldgen/terrain.md
import { EverythingRegistry } from "../everything_registry.ts"; import { EverythingRegistry } from "../everything_registry.ts";
import type { BiomeRuleJson, BiomeSourceJson, DensityFunctionJson, NoiseJson, NoiseSettingsJson } from "./data.ts"; import type { BiomeRuleJson, BiomeSourceJson, DensityFunctionJson, NoiseJson, NoiseSettingsJson } from "./data.ts";
import type { DensitySources } from "./density.ts"; import type { DensitySources } from "./density.ts";
import { OverworldTerrain } from "./terrain.ts"; import { NoiseTerrain } from "./terrain.ts";
// everything worldgen data mods registered, in load order // everything worldgen data mods registered, in load order
export interface WorldgenData { export interface WorldgenData {
@@ -21,7 +21,7 @@ export class Generator {
// the biome source's rules with every mod's added in, the first that holds picks a column's biome // the biome source's rules with every mod's added in, the first that holds picks a column's biome
readonly rules: BiomeRuleJson[]; readonly rules: BiomeRuleJson[];
readonly sources: DensitySources; readonly sources: DensitySources;
#seeds = new Map<string, OverworldTerrain>(); #seeds = new Map<string, NoiseTerrain>();
// throws when the data doesn't add up, like a density function that doesn't exist // throws when the data doesn't add up, like a density function that doesn't exist
constructor(id: string, data: WorldgenData) { constructor(id: string, data: WorldgenData) {
@@ -36,10 +36,10 @@ export class Generator {
this.for_seed(""); this.for_seed("");
} }
for_seed(seed: string): OverworldTerrain { for_seed(seed: string): NoiseTerrain {
let terrain = this.#seeds.get(seed); let terrain = this.#seeds.get(seed);
if (!terrain) { if (!terrain) {
terrain = new OverworldTerrain(seed, this); terrain = new NoiseTerrain(seed, this);
this.#seeds.set(seed, terrain); this.#seeds.set(seed, terrain);
} }
return terrain; return terrain;
@@ -1,13 +1,13 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
// fills a chunk with the overworld: the terrain's density sampled on a coarse grid and interpolated (minecraft's // fills a chunk from a generator's noise settings: the terrain's density sampled on a coarse grid and interpolated (minecraft's
// noise cells), caves cut out of it, then each column's biome, its surface blocks and fluids. what all of it is comes // noise cells), caves cut out of it, then each column's biome, its surface blocks and fluids. what all of it is comes
// from the generator's noise settings, see OverworldTerrain and docs/modding/worldgen/terrain.md. trees and the rest // from the generator's noise settings, see NoiseTerrain and docs/modding/worldgen/terrain.md. trees and the rest
// are features, after it // are features, after it
import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "@/common/constants.ts"; import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "@/common/constants.ts";
import { get_biome } from "./biomes.ts"; import { get_biome } from "./biomes.ts";
import type { OverworldTerrain, TerrainColumn } from "./terrain.ts"; import type { NoiseTerrain, TerrainColumn } from "./terrain.ts";
import { import {
compile_fluids, compile_fluids,
compile_surface_rule, compile_surface_rule,
@@ -18,13 +18,13 @@ import {
// fills blocks (indexed y * CHUNK_AREA + z * CHUNK_SIZE + x) and each column's surface height and biome. block is // fills blocks (indexed y * CHUNK_AREA + z * CHUNK_SIZE + x) and each column's surface height and biome. block is
// a block's numeric id with its default states // a block's numeric id with its default states
export function generate_overworld( export function generate_noise_chunk(
blocks: Uint32Array, blocks: Uint32Array,
heights: Int32Array, heights: Int32Array,
biomes: string[], biomes: string[],
chunk_x: number, chunk_x: number,
chunk_z: number, chunk_z: number,
terrain: OverworldTerrain, terrain: NoiseTerrain,
block: (id: string) => number, block: (id: string) => number,
) { ) {
const settings = terrain.settings; const settings = terrain.settings;
+4 -4
View File
@@ -5,7 +5,7 @@
// for each chunk with its block ids. like minecraft's surface rules: a rule gives a block or nothing, and a sequence // for each chunk with its block ids. like minecraft's surface rules: a rule gives a block or nothing, and a sequence
// gives its first rule's block // gives its first rule's block
import { type FluidJson, range_test, type SurfaceConditionJson, type SurfaceRuleJson } from "./data.ts"; import { type FluidJson, range_test, type SurfaceConditionJson, type SurfaceRuleJson } from "./data.ts";
import type { OverworldTerrain } from "./terrain.ts"; import type { NoiseTerrain } from "./terrain.ts";
// a biome's surface as numeric ids // a biome's surface as numeric ids
export interface SurfaceBlocks { export interface SurfaceBlocks {
@@ -38,7 +38,7 @@ type Condition = (context: SurfaceContext) => boolean;
export function compile_surface_rule( export function compile_surface_rule(
json: SurfaceRuleJson, json: SurfaceRuleJson,
terrain: OverworldTerrain, terrain: NoiseTerrain,
block: (id: string) => number, block: (id: string) => number,
): Rule { ): Rule {
switch (json.type) { switch (json.type) {
@@ -68,7 +68,7 @@ export function compile_surface_rule(
} }
} }
function compile_condition(json: SurfaceConditionJson, terrain: OverworldTerrain): Condition { function compile_condition(json: SurfaceConditionJson, terrain: NoiseTerrain): Condition {
switch (json.type) { switch (json.type) {
case "stone_depth": { case "stone_depth": {
const offset = json.offset; const offset = json.offset;
@@ -113,7 +113,7 @@ export function surface_rule_reach(json: SurfaceRuleJson): number {
// the fluid empty space at the context's y becomes, if any. open_sky is whether there's only air above it // the fluid empty space at the context's y becomes, if any. open_sky is whether there's only air above it
export function compile_fluids( export function compile_fluids(
json: FluidJson[], json: FluidJson[],
terrain: OverworldTerrain, terrain: NoiseTerrain,
block: (id: string) => number, block: (id: string) => number,
): (context: SurfaceContext, open_sky: boolean) => number | undefined { ): (context: SurfaceContext, open_sky: boolean) => number | undefined {
const fluids = json.map((fluid) => { const fluids = json.map((fluid) => {
+3 -4
View File
@@ -2,9 +2,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
// the shape of the world and which biome each column gets, from a generator's data (see generator.ts): its density // the shape of the world and which biome each column gets, from a generator's data (see generator.ts): its density
// functions compiled for one seed. generate_overworld calls density and cave through here, so engine access mods can // functions compiled for one seed. generate_noise_chunk calls density and cave through here, so engine access mods can
// hook them. bworld's overworld is shaped after tectonic (https://github.com/Apollounknowndev/tectonic, MIT), its // hook them
// splines are in mods/bworld/worldgen/density_function/overworld
import { type Density, DensityCompiler } from "./density.ts"; import { type Density, DensityCompiler } from "./density.ts";
import { type DensityJson, type NoiseSettingsJson, range_test, type RangeJson } from "./data.ts"; import { type DensityJson, type NoiseSettingsJson, range_test, type RangeJson } from "./data.ts";
import type { Generator } from "./generator.ts"; import type { Generator } from "./generator.ts";
@@ -25,7 +24,7 @@ interface CompiledRule {
holds(climate: number[], surface: number, steep: boolean): boolean; holds(climate: number[], surface: number, steep: boolean): boolean;
} }
export class OverworldTerrain { export class NoiseTerrain {
readonly seed: string; readonly seed: string;
readonly settings: NoiseSettingsJson; readonly settings: NoiseSettingsJson;
readonly compiler: DensityCompiler; readonly compiler: DensityCompiler;
+6 -6
View File
@@ -53,11 +53,11 @@ made changes too. It returns a function that unhooks it.
## What's in `engine` ## What's in `engine`
| Side | Where | Like | | Side | Where | Like |
| -------- | -------------- | ------------------------------------------------------------------------------------------- | | -------- | -------------- | ----------------------------------------------------------------------------------------- |
| `common` | every script | `EverythingRegistry`, `ItemStack`, `Container`, `BlockRules`, `OverworldTerrain`, `physics` | | `common` | every script | `EverythingRegistry`, `ItemStack`, `Container`, `BlockRules`, `NoiseTerrain`, `physics` |
| `server` | server scripts | `GameServer`, `ServerWorld`, `ServerPlayer`, `Entity`, `Mob`, `ModRuntime`, the mob goals | | `server` | server scripts | `GameServer`, `ServerWorld`, `ServerPlayer`, `Entity`, `Mob`, `ModRuntime`, the mob goals |
| `client` | client scripts | `Client`, `LocalPlayer`, `ClientLevel`, `GameRenderer`, `Camera`, `GuiScreen` | | `client` | client scripts | `Client`, `LocalPlayer`, `ClientLevel`, `GameRenderer`, `Camera`, `GuiScreen` |
The full lists are in `common/engine_modules.ts`, `server/game/engine_modules.ts` and `client/engine_modules.ts`. The The full lists are in `common/engine_modules.ts`, `server/game/engine_modules.ts` and `client/engine_modules.ts`. The
types are the engine's own source, so `check-mods` typechecks against exactly the engine the mod runs on. `cubi/engine` types are the engine's own source, so `check-mods` typechecks against exactly the engine the mod runs on. `cubi/engine`
@@ -68,7 +68,7 @@ also exports `game_version`, the version of the game that's running.
- `#private` members can't be hooked. They're kept for things with rules that must hold, like a container's slots. - `#private` members can't be hooked. They're kept for things with rules that must hold, like a container's slots.
- Hooks reach one JavaScript realm. Chunk workers run their own copy of the engine, so a client hook doesn't change - Hooks reach one JavaScript realm. Chunk workers run their own copy of the engine, so a client hook doesn't change
meshing. The server generates chunks in worldgen workers, and each loads the worldgen scripts into its own copy, so meshing. The server generates chunks in worldgen workers, and each loads the worldgen scripts into its own copy, so
hooks on the terrain (`engine.common.OverworldTerrain`) go in the worldgen script. A server script's hooks there only hooks on the terrain (`engine.common.NoiseTerrain`) go in the worldgen script. A server script's hooks there only
reach the few chunks the game generates itself, so the world would come out different in places. reach the few chunks the game generates itself, so the world would come out different in places.
## Performance ## Performance
+2 -2
View File
@@ -282,8 +282,8 @@ A mod can add biomes to it, but can't replace its files yet: an id defined by tw
changes the terrain changes land not generated yet in existing worlds, the same as adding a changes the terrain changes land not generated yet in existing worlds, the same as adding a
[feature](index.md#features). Every chunk generated is saved, so what's been generated stays as it was. [feature](index.md#features). Every chunk generated is saved, so what's been generated stays as it was.
[Engine access](../misc/engine.md) mods can hook `engine.common.OverworldTerrain`, whose `density` and `cave` are called [Engine access](../misc/engine.md) mods can hook `engine.common.NoiseTerrain`, whose `density` and `cave` are called for
for every cell corner. every cell corner.
`deno run -A tools/worldgen_preview.ts [seed] [out dir] [x z]` renders the base game's generator to PNG files, around `deno run -A tools/worldgen_preview.ts [seed] [out dir] [x z]` renders the base game's generator to PNG files, around
chunk `x z`. chunk `x z`.
+1
View File
@@ -276,6 +276,7 @@ export class GameServer {
}); });
}, },
}); });
this.mods.storage = saved?.mod_storage ?? {};
if (save) this.world.use_saved_chunks(save.chunks); if (save) this.world.use_saved_chunks(save.chunks);
this.world.time = saved?.time ?? 0; this.world.time = saved?.time ?? 0;
this.#day = this.world.day; this.#day = this.world.day;
+1 -1
View File
@@ -241,7 +241,7 @@ export class ServerWorld {
} }
// like "bworld:plains", what generation decided // like "bworld:plains", what generation decided
// a column's biome without generating its chunk, a little off near coasts and cliffs, see OverworldTerrain // a column's biome without generating its chunk, a little off near coasts and cliffs, see NoiseTerrain
estimate_biome(x: number, z: number): { biome: string; y: number } { estimate_biome(x: number, z: number): { biome: string; y: number } {
return this.worldgen.generator.for_seed(this.seed).estimate_biome(x, z); return this.worldgen.generator.for_seed(this.seed).estimate_biome(x, z);
} }
-17
View File
@@ -102,23 +102,6 @@ Deno.test("items only go in the slots they fit, and fall out when the player die
assert(curios_items(t.take(1)).every((item: Msg) => item === null)); assert(curios_items(t.take(1)).every((item: Msg) => item === null));
}); });
Deno.test("curios worn when curios was a mod of its own are still worn", async () => {
const t = await setup();
t.send(1, { type: "click", menu: 0, slot: 0, button: 0, shift: true });
t.game.on_disconnect(1);
// how a save from then kept which container is each player's curios
const save = t.game.save();
const level = JSON.parse(save.level);
level.mod_storage.curios = { containers: level.mod_storage.bworld.curios };
delete level.mod_storage.bworld.curios;
const loaded = await setup({ ...save, level: JSON.stringify(level) });
const ring = loaded.game.players()[0].inventory_menu.extra[curio_slot(loaded, "ring")];
assertEquals(ring.container.get_item(ring.index)?.to_data(), { id: "jewelry:ring", count: 1 });
assertEquals(loaded.game.mods.storage.curios, undefined);
});
Deno.test("the giant chestplate goes in the body slot and gives 20 more health while worn", async () => { Deno.test("the giant chestplate goes in the body slot and gives 20 more health while worn", async () => {
const t = await setup(); const t = await setup();
t.send(1, { type: "chat", text: "/give bworld:giant_chestplate" }); t.send(1, { type: "chat", text: "/give bworld:giant_chestplate" });
+2 -2
View File
@@ -111,7 +111,7 @@ Deno.test("a worldgen mod hooks the terrain in engine.common", async () => {
`import { engine, hook } from "cubi/engine"; `import { engine, hook } from "cubi/engine";
// no ground anywhere, so it's all sea // no ground anywhere, so it's all sea
export function setup() { export function setup() {
hook(engine.common.OverworldTerrain.prototype, "density", { replace: () => -1 }); hook(engine.common.NoiseTerrain.prototype, "density", { replace: () => -1 });
}`, }`,
], ],
}) })
@@ -136,7 +136,7 @@ Deno.test({
"worldgen.ts", "worldgen.ts",
`import { engine, hook } from "cubi/engine"; `import { engine, hook } from "cubi/engine";
export function setup() { export function setup() {
hook(engine.common.OverworldTerrain.prototype, "density", { replace: () => -1 }); hook(engine.common.NoiseTerrain.prototype, "density", { replace: () => -1 });
}`, }`,
], ],
}) })