World gen stuff

This commit is contained in:
2026-11-13 00:01:59 -03:00
parent e013fbe550
commit e8ca9753f5
25 changed files with 645 additions and 162 deletions
+13 -3
View File
@@ -13,6 +13,7 @@ import { generate_noise_chunk } from "./worldgen/noise_chunk.ts";
import { get_biome } from "./worldgen/biomes.ts";
import type { Generator } from "./worldgen/generator.ts";
import { named_noise_2d, named_noise_3d } from "./worldgen/noise.ts";
import { Tags } from "./tags.ts";
export { named_noise_2d, named_noise_3d };
@@ -115,8 +116,17 @@ function generate_ores(
default_values: number[] | undefined,
) {
const usable = ores
.map((ore) => ({ ...ore, nid: block_ids[ore.id], replaces_nid: block_ids[ore.replaces] }))
.filter((ore) => ore.nid !== undefined && ore.replaces_nid !== undefined);
.map((ore) => ({
...ore,
nid: block_ids[ore.id],
replaces_nids: new Set(
[ore.replaces].flat()
.flatMap((id) => id.startsWith("#") ? Tags.get("blocks", id) : [id])
.map((id) => block_ids[id])
.filter((nid) => nid !== undefined),
),
}))
.filter((ore) => ore.nid !== undefined && ore.replaces_nids.size > 0);
if (usable.length === 0) {
return;
}
@@ -132,7 +142,7 @@ function generate_ores(
}
for (let i = 0; i < usable.length; i++) {
const ore = usable[i];
if (current !== ore.replaces_nid || y < ore.min_y || y > ore.max_y) {
if (!ore.replaces_nids.has(current) || y < ore.min_y || y > ore.max_y) {
continue;
}
const wx = chunk_x * CHUNK_SIZE + lx;
+1 -1
View File
@@ -369,7 +369,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
}
for (const { file, json } of mod.ores) {
uses(file, json.id, "block");
uses(file, json.replaces, "block");
for (const replaces of [json.replaces].flat()) uses(file, replaces, "block");
}
for (const { file, json } of mod.entities) {
if ((entity_owners.get(json.id)?.length ?? 0) > 1) {
+2 -1
View File
@@ -330,7 +330,8 @@ const model_schema: z.ZodType<ModelFileJson> = z.object({
const ore_schema = z.object({
id,
replaces: id,
// a block, a "#tag" of blocks, or a list of those
replaces: z.union([id_or_tag, z.array(id_or_tag).min(1)]),
min_y: z.number(),
max_y: z.number(),
scale: z.number(),
+6
View File
@@ -109,6 +109,8 @@ export type SurfaceConditionJson =
| { type: "bare_cliffs" }
// at or above where a density function says
| { type: "y_at_least"; function: DensityJson }
// where a density function is in a range
| ({ type: "density"; function: DensityJson } & RangeJson)
| { type: "biome"; biome_is: string[] }
| { type: "not"; invert: SurfaceConditionJson };
@@ -283,6 +285,10 @@ const surface_condition: z.ZodType<SurfaceConditionJson> = z.lazy(() =>
z.object({ type: z.literal(["filler_depth", "surface_underwater", "steep", "bare_cliffs"]) }),
z.object({ type: z.literal("above_preliminary_surface"), offset: z.number().optional() }),
z.object({ type: z.literal("y_at_least"), function: density }),
z.object({ type: z.literal("density"), function: density, ...bounds(z.number()) }).refine(
has_bound,
NEEDS_BOUND,
),
z.object({ type: z.literal("biome"), biome_is: z.array(biome_id) }),
z.object({ type: z.literal("not"), invert: surface_condition }),
])
+5
View File
@@ -90,6 +90,11 @@ function compile_condition(json: SurfaceConditionJson, terrain: NoiseTerrain): C
const at = terrain.compiler.compile(json.function);
return (context) => context.y >= at(context.x, context.y, context.z);
}
case "density": {
const value = terrain.compiler.compile(json.function);
const test = range_test(json);
return (context) => test(value(context.x, context.y, context.z));
}
case "biome": {
const biomes = new Set(json.biome_is);
return (context) => biomes.has(context.biome);
+5 -4
View File
@@ -38,7 +38,7 @@ at y 64.
"ores": [
{
"id": "copper_tools:rich_copper_ore",
"replaces": "bworld:stone",
"replaces": "#bworld:raw_rocks",
"min_y": 5,
"max_y": 40,
"scale": 0.05,
@@ -48,9 +48,10 @@ at y 64.
}
```
Every block in the chunk that is `replaces` and between `min_y` and `max_y` becomes the first ore whose 3D noise,
sampled at `(x, y, z) × scale`, is above `threshold`. A smaller `scale` makes bigger veins, and a higher `threshold`
makes rarer ones. Each ore's noise is seeded with its id, so ores don't line up with each other.
`replaces` is a block, a `#tag` of blocks, or a list of those, like `["bworld:stone", "#bworld:raw_rocks"]`. Every block
in the chunk that is one of them and between `min_y` and `max_y` becomes the first ore whose 3D noise, sampled at
`(x, y, z) × scale`, is above `threshold`. A smaller `scale` makes bigger veins, and a higher `threshold` makes rarer
ones. Each ore's noise is seeded with its id, so ores don't line up with each other.
Each mod's ores are a pass of their own, after the mods loaded before it. So a mod's ore can replace another mod's, like
`"replaces": "bworld:coal_ore"` for a rare vein inside coal. The base game's are in `mods/bworld/worldgen/ores.json`.
+15 -12
View File
@@ -267,21 +267,24 @@ What a solid block becomes, like Minecraft's surface rules: a rule gives a block
| `block` | `result_state: { Name }` | That block. |
| `biome_surface` | `part` | The column's biome's `top`, `filler`, `underwater_top` or `underwater_filler`. bworld's own. |
| Condition | Fields | Holds |
| --------------------------- | ---------- | ----------------------------------------------------------------------------------- |
| `stone_depth` | `offset` | When the solid blocks above it before air are at most `offset`. 0 is the top block. |
| `filler_depth` | | Within the biome's `filler_depth` below the top. bworld's own. |
| `above_preliminary_surface` | `offset` | At or above the preliminary surface plus `offset`. |
| `surface_underwater` | | When the column's ground is below `sea_level` - 1. bworld's own. |
| `steep` | | When the column is steep, see the noise settings' `steep`. |
| `bare_cliffs` | | When the biome's surface has `bare_cliffs`. bworld's own. |
| `y_at_least` | `function` | At or above the height a density function gives, like a snow line. bworld's own. |
| `biome` | `biome_is` | In one of the biomes. |
| `not` | `invert` | When `invert` doesn't. |
| Condition | Fields | Holds |
| --------------------------- | ---------------------- | ------------------------------------------------------------------------------------ |
| `stone_depth` | `offset` | When the solid blocks above it before air are at most `offset`. 0 is the top block. |
| `filler_depth` | | Within the biome's `filler_depth` below the top. bworld's own. |
| `above_preliminary_surface` | `offset` | At or above the preliminary surface plus `offset`. |
| `surface_underwater` | | When the column's ground is below `sea_level` - 1. bworld's own. |
| `steep` | | When the column is steep, see the noise settings' `steep`. |
| `bare_cliffs` | | When the biome's surface has `bare_cliffs`. bworld's own. |
| `y_at_least` | `function` | At or above the height a density function gives, like a snow line. bworld's own. |
| `density` | `function` and a range | Where a density function is in the range, like a noise picking a rock. bworld's own. |
| `biome` | `biome_is` | In one of the biomes. |
| `not` | `invert` | When `invert` doesn't. |
The base game's: within 20 blocks of the ground, the top block is the biome's underwater cover under water, snow (or
stone on cliffs) above the snow line, stone on bare cliffs, and the biome's `top` otherwise. Below it, the biome's
filler down to its `filler_depth`. Everything else is stone.
filler down to its `filler_depth`. Below that are three layers of rock that follow the ground: sedimentary down to 20
blocks below the preliminary surface, metamorphic down to 45, and igneous the rest of the way. In each layer, a noise of
its own (`bworld:overworld/sedimentary_rock` and so on) picks the rock, split evenly between the layer's rocks.
## Dimensions
@@ -0,0 +1,28 @@
{
"format_version": 1,
"tag": {
"values": [
"bworld:raw_shale",
"bworld:raw_claystone",
"bworld:raw_limestone",
"bworld:raw_conglomerate",
"bworld:raw_dolomite",
"bworld:raw_chert",
"bworld:raw_chalk",
"bworld:raw_quartzite",
"bworld:raw_slate",
"bworld:raw_phyllite",
"bworld:raw_schist",
"bworld:raw_gneiss",
"bworld:raw_marble",
"bworld:raw_granite",
"bworld:raw_diorite",
"bworld:raw_gabbro",
"bworld:raw_rhyolite",
"bworld:raw_basalt",
"bworld:raw_andesite",
"bworld:raw_dacite",
"bworld:raw_tuff"
]
}
}
@@ -0,0 +1,9 @@
{
"format_version": 1,
"density_function": {
"id": "bworld:overworld/igneous_rock",
"type": "noise",
"noise": "bworld:igneous_rock",
"even": true
}
}
@@ -0,0 +1,9 @@
{
"format_version": 1,
"density_function": {
"id": "bworld:overworld/metamorphic_rock",
"type": "noise",
"noise": "bworld:metamorphic_rock",
"even": true
}
}
@@ -0,0 +1,9 @@
{
"format_version": 1,
"density_function": {
"id": "bworld:overworld/sedimentary_rock",
"type": "noise",
"noise": "bworld:sedimentary_rock",
"even": true
}
}
@@ -4,8 +4,8 @@
"id": "bworld:overworld",
"generator": "bworld:overworld",
"default": true,
"min_y": 0,
"max_y": 256,
"min_y": -64,
"max_y": 320,
"sky_light": true
}
}
@@ -0,0 +1,12 @@
{
"format_version": 1,
"noise": {
"id": "bworld:igneous_rock",
"wavelength": 512,
"amplitudes": [
1,
1
],
"quantiles": "bworld:humidity"
}
}
@@ -0,0 +1,12 @@
{
"format_version": 1,
"noise": {
"id": "bworld:metamorphic_rock",
"wavelength": 512,
"amplitudes": [
1,
1
],
"quantiles": "bworld:humidity"
}
}
@@ -0,0 +1,12 @@
{
"format_version": 1,
"noise": {
"id": "bworld:sedimentary_rock",
"wavelength": 512,
"amplitudes": [
1,
1
],
"quantiles": "bworld:humidity"
}
}
@@ -3,7 +3,7 @@
"noise_settings": {
"id": "bworld:overworld",
"sea_level": 64,
"default_block": "bworld:stone",
"default_block": "bworld:raw_granite",
"cell_width": 4,
"terrain": {
"density": "bworld:overworld/terrain",
@@ -40,46 +40,75 @@
}
],
"surface_rule": {
"type": "condition",
"if_true": {
"type": "above_preliminary_surface",
"offset": -20
},
"then_run": {
"type": "sequence",
"sequence": [
{
"type": "condition",
"if_true": {
"type": "stone_depth",
"offset": 0
},
"then_run": {
"type": "sequence",
"sequence": [
{
"type": "condition",
"if_true": {
"type": "surface_underwater"
},
"then_run": {
"type": "biome_surface",
"part": "underwater_top"
}
"type": "sequence",
"sequence": [
{
"type": "condition",
"if_true": {
"type": "above_preliminary_surface",
"offset": -20
},
"then_run": {
"type": "sequence",
"sequence": [
{
"type": "condition",
"if_true": {
"type": "stone_depth",
"offset": 0
},
{
"type": "condition",
"if_true": {
"type": "y_at_least",
"function": "bworld:overworld/snow_line"
},
"then_run": {
"type": "sequence",
"sequence": [
{
"then_run": {
"type": "sequence",
"sequence": [
{
"type": "condition",
"if_true": {
"type": "surface_underwater"
},
"then_run": {
"type": "biome_surface",
"part": "underwater_top"
}
},
{
"type": "condition",
"if_true": {
"type": "y_at_least",
"function": "bworld:overworld/snow_line"
},
"then_run": {
"type": "sequence",
"sequence": [
{
"type": "condition",
"if_true": {
"type": "steep"
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:stone"
}
}
},
{
"type": "block",
"result_state": {
"Name": "bworld:snow_block"
}
}
]
}
},
{
"type": "condition",
"if_true": {
"type": "steep"
},
"then_run": {
"type": "condition",
"if_true": {
"type": "steep"
"type": "bare_cliffs"
},
"then_run": {
"type": "block",
@@ -87,68 +116,336 @@
"Name": "bworld:stone"
}
}
},
{
"type": "block",
"result_state": {
"Name": "bworld:snow_block"
}
}
]
}
},
{
"type": "biome_surface",
"part": "top"
}
]
}
},
{
"type": "condition",
"if_true": {
"type": "filler_depth"
},
{
"type": "condition",
"if_true": {
"type": "steep"
},
"then_run": {
"then_run": {
"type": "sequence",
"sequence": [
{
"type": "condition",
"if_true": {
"type": "surface_underwater"
},
"then_run": {
"type": "biome_surface",
"part": "underwater_filler"
}
},
{
"type": "biome_surface",
"part": "filler"
}
]
}
},
{
"type": "sequence",
"sequence": [
{
"type": "condition",
"if_true": {
"type": "bare_cliffs"
"type": "density",
"function": "bworld:overworld/sedimentary_rock",
"max_exclusive": -0.714
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:stone"
"Name": "bworld:raw_shale"
}
}
}
},
{
"type": "biome_surface",
"part": "top"
}
]
}
},
{
"type": "condition",
"if_true": {
"type": "filler_depth"
},
"then_run": {
"type": "sequence",
"sequence": [
{
"type": "condition",
"if_true": {
"type": "surface_underwater"
},
"then_run": {
"type": "biome_surface",
"part": "underwater_filler"
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/sedimentary_rock",
"max_exclusive": -0.429
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_claystone"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/sedimentary_rock",
"max_exclusive": -0.143
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_limestone"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/sedimentary_rock",
"max_exclusive": 0.143
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_conglomerate"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/sedimentary_rock",
"max_exclusive": 0.429
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_dolomite"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/sedimentary_rock",
"max_exclusive": 0.714
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_chert"
}
}
},
{
"type": "block",
"result_state": {
"Name": "bworld:raw_chalk"
}
}
},
{
"type": "biome_surface",
"part": "filler"
}
]
}
]
}
]
}
]
}
},
{
"type": "condition",
"if_true": {
"type": "above_preliminary_surface",
"offset": -45
},
"then_run": {
"type": "sequence",
"sequence": [
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/metamorphic_rock",
"max_exclusive": -0.667
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_quartzite"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/metamorphic_rock",
"max_exclusive": -0.333
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_slate"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/metamorphic_rock",
"max_exclusive": 0.0
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_phyllite"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/metamorphic_rock",
"max_exclusive": 0.333
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_schist"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/metamorphic_rock",
"max_exclusive": 0.667
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_gneiss"
}
}
},
{
"type": "block",
"result_state": {
"Name": "bworld:raw_marble"
}
}
]
}
},
{
"type": "sequence",
"sequence": [
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/igneous_rock",
"max_exclusive": -0.75
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_granite"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/igneous_rock",
"max_exclusive": -0.5
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_diorite"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/igneous_rock",
"max_exclusive": -0.25
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_gabbro"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/igneous_rock",
"max_exclusive": 0.0
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_rhyolite"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/igneous_rock",
"max_exclusive": 0.25
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_basalt"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/igneous_rock",
"max_exclusive": 0.5
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_andesite"
}
}
},
{
"type": "condition",
"if_true": {
"type": "density",
"function": "bworld:overworld/igneous_rock",
"max_exclusive": 0.75
},
"then_run": {
"type": "block",
"result_state": {
"Name": "bworld:raw_dacite"
}
}
},
{
"type": "block",
"result_state": {
"Name": "bworld:raw_tuff"
}
}
]
}
]
}
}
}
+20 -5
View File
@@ -3,7 +3,10 @@
"ores": [
{
"id": "bworld:coal_ore",
"replaces": "bworld:stone",
"replaces": [
"bworld:stone",
"#bworld:raw_rocks"
],
"min_y": 5,
"max_y": 200,
"scale": 0.07,
@@ -11,7 +14,10 @@
},
{
"id": "bworld:copper_ore",
"replaces": "bworld:stone",
"replaces": [
"bworld:stone",
"#bworld:raw_rocks"
],
"min_y": 5,
"max_y": 110,
"scale": 0.075,
@@ -19,7 +25,10 @@
},
{
"id": "bworld:tin_ore",
"replaces": "bworld:stone",
"replaces": [
"bworld:stone",
"#bworld:raw_rocks"
],
"min_y": 5,
"max_y": 70,
"scale": 0.08,
@@ -27,7 +36,10 @@
},
{
"id": "bworld:iron_ore",
"replaces": "bworld:stone",
"replaces": [
"bworld:stone",
"#bworld:raw_rocks"
],
"min_y": 5,
"max_y": 80,
"scale": 0.08,
@@ -35,7 +47,10 @@
},
{
"id": "bworld:gold_ore",
"replaces": "bworld:stone",
"replaces": [
"bworld:stone",
"#bworld:raw_rocks"
],
"min_y": 5,
"max_y": 36,
"scale": 0.06,
+2 -2
View File
@@ -14,7 +14,7 @@ import { load_client_mods } from "@/client/mods.ts";
import { load_mods } from "@/server/load_mods.ts";
import { load_and_check } from "@/tools/check_mods.ts";
import { pack_mod } from "@/tools/pack_mod.ts";
import { copy_dir, test_game } from "./helpers.ts";
import { copy_dir, GROUND_BLOCKS, test_game } from "./helpers.ts";
// a block for every rock and its bricks, and one more where the script can't read files, like the server's worker
const CONTENT = `
@@ -57,7 +57,7 @@ function rocks_mod(extra = "") {
write("tags/blocks/rocks/rocks.json", { format_version: 1, tag: { values: ["rocks:granite", "rocks:basalt"] } });
write("worldgen/ores.json", {
format_version: 1,
ores: [{ id: "rocks:basalt", replaces: "bworld:stone", min_y: 5, max_y: 200, scale: 0.07, threshold: 0.3 }],
ores: [{ id: "rocks:basalt", replaces: GROUND_BLOCKS, min_y: 5, max_y: 200, scale: 0.07, threshold: 0.3 }],
});
return dir;
}
+38 -1
View File
@@ -25,6 +25,7 @@ function mods(options: {
flat_default?: boolean;
shape?: Pick<DimensionJson, "min_y" | "max_y" | "sky_light">;
ground?: string;
surface_rule?: unknown;
cell_height?: number;
}) {
const dir = Deno.makeTempDirSync({ prefix: "bworld_dimensions_" });
@@ -73,7 +74,8 @@ function mods(options: {
steep: 100,
biome_source: "flat:flat",
fluids: [{ block: "bworld:water", below: SEA_TOP + 1, where: "open_sky" }],
surface_rule: { type: "block", result_state: { Name: options.ground ?? "bworld:glass" } },
surface_rule: options.surface_rule ??
{ type: "block", result_state: { Name: options.ground ?? "bworld:glass" } },
},
});
write("worldgen/dimension/flat.json", {
@@ -96,6 +98,41 @@ Deno.test("the world is generated by the default dimension's noise settings, fro
}
});
Deno.test("a surface rule's density condition holds where its density function is in range", async () => {
const block = (id: string) => ({ type: "block", result_state: { Name: id } });
const dir = mods({
flat: true,
bworld_default: false,
// glass where x is from 30 to 50, and below y 60 too
surface_rule: {
type: "sequence",
sequence: [
{
type: "condition",
if_true: { type: "density", function: { type: "x" }, min_inclusive: 30, max_exclusive: 50 },
then_run: block("bworld:glass"),
},
{
type: "condition",
if_true: { type: "density", function: { type: "y" }, max_exclusive: 60 },
then_run: block("bworld:glass"),
},
block("bworld:stone"),
],
},
});
const { game } = await test_game(dir);
Deno.removeSync(dir, { recursive: true });
const at = (x: number, y: number) => game.world.get_block_id(x, y, 5);
assertEquals([29, 30, 49, 50].map((x) => at(x, GROUND_TOP)), [
"bworld:stone",
"bworld:glass",
"bworld:glass",
"bworld:stone",
]);
assertEquals([at(60, 60), at(60, 59)], ["bworld:stone", "bworld:glass"]);
});
Deno.test("players spawn on ground with collision, not on the water in the middle of the world", async () => {
const dir = mods({ flat: true, bworld_default: false });
const { game, join } = await test_game(dir);
+29 -1
View File
@@ -13,7 +13,10 @@ import { mod_data } from "@/common/mod_check.ts";
import type { ServerModSource } from "@/server/game/load_mods.ts";
import { GameServer } from "@/server/game/game_server.ts";
import { start_game } from "@/server/game/load_mods.ts";
import { apply_config } from "@/server/load_mods.ts";
import { apply_config, load_mods } from "@/server/load_mods.ts";
import { load_client_mods } from "@/client/mods.ts";
import { read_mod } from "@/common/mod.ts";
import { pack_mod } from "@/tools/pack_mod.ts";
import { AIR_ID, PROTOCOL_VERSION } from "@/common/protocol.ts";
import { decode_level_chunk } from "@/common/chunk_streaming.ts";
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
@@ -274,6 +277,31 @@ export function stone_floor(game: GameServer, radius = 3) {
}
}
// what the base game's terrain is made of underground, for ores in tests to replace
export const GROUND_BLOCKS = "#bworld:raw_rocks";
// the cubi: textures mods can use without a build
export const ENGINE_TEXTURES = ["cubi:missing", ...Array.from({ length: 9 }, (_, i) => `cubi:break_${i}`)];
// registers a folder of mod sources the way a player gets them: packed, loaded by a server, which adds the blocks
// content scripts register, and the copies it serves loaded on the client
export async function load_as_player(sources: string) {
const folder = Deno.makeTempDirSync({ prefix: "bworld_player_mods_" });
try {
for (const mod of await load_and_check(sources)) {
Deno.writeFileSync(`${folder}/${mod.id}.cubi`, await pack_mod(mod));
}
const mods = await load_mods(folder, ENGINE_TEXTURES);
EverythingRegistry.clear();
await load_client_mods(
mods.map((mod) => mod.listing),
mods.map((mod) => read_mod(mod.client_bytes, mod.listing.file)),
);
} finally {
Deno.removeSync(folder, { recursive: true });
}
}
export function copy_dir(from: string, to: string) {
if (Deno.statSync(from).isDirectory) {
Deno.mkdirSync(to, { recursive: true });
+2 -2
View File
@@ -11,7 +11,7 @@ import { load_worldgen } from "@/common/worldgen_loader.ts";
import { create_mod } from "@/tools/new_mod.ts";
import { PLAYER_MENU_CRAFTING, PROTOCOL_VERSION } from "@/common/protocol.ts";
import { recipe_lookup } from "@/common/mod_loader.ts";
import { container_items, copy_dir, test_game } from "./helpers.ts";
import { container_items, copy_dir, GROUND_BLOCKS, test_game } from "./helpers.ts";
// mods/bworld plus a mod made from the template, in a temp folder
function mods_with_template() {
@@ -175,7 +175,7 @@ Deno.test("mod ores generate into the block they replace", async () => {
format_version: 1,
ores: [{
id: "copper_tools:example_block",
replaces: "bworld:stone",
replaces: GROUND_BLOCKS,
min_y: 0,
max_y: 60,
scale: 0.1,
+1 -3
View File
@@ -12,15 +12,13 @@ import { pack_mod } from "@/tools/pack_mod.ts";
import { create_mod } from "@/tools/new_mod.ts";
import { load_mods } from "@/server/load_mods.ts";
import { start_host } from "@/server/host.ts";
import { copy_dir } from "./helpers.ts";
import { copy_dir, ENGINE_TEXTURES } from "./helpers.ts";
import { PROTOCOL_VERSION } from "@/common/protocol.ts";
import { decode_level_chunk, type LevelChunk } from "@/common/chunk_streaming.ts";
import { decode_chunk, encode_chunk } from "@/server/game/saves.ts";
import { list_chunks, read_chunk, write_chunks } from "@/server/regions.ts";
import { section_index } from "@/common/section.ts";
const ENGINE_TEXTURES = ["cubi:missing", ...Array.from({ length: 9 }, (_, i) => `cubi:break_${i}`)];
// mods/bworld and one made from the template, packed into a folder of .cubi files
async function packed_mods() {
const sources = Deno.makeTempDirSync({ prefix: "bworld_mod_src_" });
+2 -14
View File
@@ -8,10 +8,9 @@ import { EverythingRegistry } from "@/common/everything_registry.ts";
import type { ModListing } from "@/common/mod_loader.ts";
import { read_mod } from "@/common/mod.ts";
import { load_client_mods } from "@/client/mods.ts";
import { load_order } from "@/common/mod_check.ts";
import { load_and_check } from "@/tools/check_mods.ts";
import { pack_mod } from "@/tools/pack_mod.ts";
import { copy_dir, test_game } from "./helpers.ts";
import { copy_dir, load_as_player, test_game } from "./helpers.ts";
// sifts into what its component says, and a shaped recipe made in code. then sifting and washing as testrecipes' own
// types, found by the item put in
@@ -222,16 +221,5 @@ Deno.test("recipes can only be registered while shared scripts set up", async ()
});
Deno.test("every mod's recipe types are registered on a client too, so it loads them", async () => {
const mods = load_order(await load_and_check("mods"));
const packed = await Promise.all(mods.map(async (mod) => read_mod(await pack_mod(mod), `${mod.id}.cubi`)));
const listings = mods.map((mod): ModListing => ({
id: mod.id,
name: mod.id,
version: "1.0.0",
sha256: "",
file: "",
size: 0,
}));
EverythingRegistry.clear();
await load_client_mods(listings, packed);
await load_as_player("mods");
});
+6 -20
View File
@@ -4,14 +4,8 @@
// item tooltips: the game's lines, the ones client scripts add and where they go
import { assertEquals } from "@std/assert";
import { item_tooltip } from "@/client/gui/tooltip.ts";
import { load_client_mods } from "@/client/mods.ts";
import { EverythingRegistry } from "@/common/everything_registry.ts";
import { ItemStack } from "@/common/inventory.ts";
import type { ModListing } from "@/common/mod_loader.ts";
import { read_mod } from "@/common/mod.ts";
import { load_and_check, load_order } from "@/tools/check_mods.ts";
import { pack_mod } from "@/tools/pack_mod.ts";
import { copy_dir } from "./helpers.ts";
import { copy_dir, load_as_player } from "./helpers.ts";
const SNACK = "tips:snack";
@@ -47,19 +41,11 @@ async function load(client_script: string) {
}),
);
write("scripts/client.ts", client_script);
const mods = load_order(await load_and_check(dir));
const packed = await Promise.all(mods.map(async (mod) => read_mod(await pack_mod(mod), `${mod.id}.cubi`)));
Deno.removeSync(dir, { recursive: true });
const listings: ModListing[] = mods.map((mod) => ({
id: mod.id,
name: mod.id,
version: "1.0.0",
sha256: "",
file: "",
size: 0,
}));
EverythingRegistry.clear();
await load_client_mods(listings, packed);
try {
await load_as_player(dir);
} finally {
Deno.removeSync(dir, { recursive: true });
}
return (id: string) => item_tooltip(new ItemStack(id)).map((line) => line.text);
}
+21 -4
View File
@@ -17,6 +17,7 @@ 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)];
Deno.mkdirSync(out, { recursive: true });
// 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
@@ -89,7 +90,8 @@ 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
// every other block after them, so features can place them. other woods look like oak, the rest, like rocks, get a
// muted color of their own from their id
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;
@@ -97,6 +99,21 @@ for (const block of EverythingRegistry.get_registry<BlockRegistry>("blocks").fil
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];
else COLORS[nid] = color_of(block.id);
}
const names: string[] = ["air"];
for (const [id, nid] of Object.entries(block_ids)) names[nid] = id;
function color_of(id: string): [number, number, number] {
let hash = 2166136261;
for (let i = 0; i < id.length; i++) hash = Math.imul(hash ^ id.charCodeAt(i), 16777619);
const hue = (hash >>> 0) % 360 / 60;
const lightness = 90 + (hash >>> 9) % 90;
const channel = (offset: number) => {
const k = (offset + hue) % 6;
return Math.round(lightness * (1 - 0.45 * Math.max(0, Math.min(k, 4 - k, 1))));
};
return [channel(5), channel(3), channel(1)];
}
const BIOME_COLORS: Record<string, [number, number, number]> = {
@@ -216,10 +233,10 @@ const shade = (color: [number, number, number], factor: number): [number, number
const chunks = 48;
const size = chunks * CHUNK_SIZE;
const top = new Int32Array(size * size);
const top_block = new Uint8Array(size * size);
const top_block = new Uint32Array(size * size);
const water_depth = new Int32Array(size * size);
// rows from the top of the world down
const section = new Uint8Array(size * shape.height);
const section = new Uint32Array(size * shape.height);
const section_z = size / 2;
const origin = -size / 2;
const counts = new Map<number, number>();
@@ -292,6 +309,6 @@ const shade = (color: [number, number, number], factor: number): [number, number
console.log(`underground air: ${(underground_air / (solid + underground_air) * 100).toFixed(1)}% of the ground`);
const total = [...counts.values()].reduce((a, b) => a + b, 0);
for (const [nid, count] of [...counts].sort((a, b) => b[1] - a[1])) {
console.log(` ${nid === 0 ? "air" : NAMES[nid - 1]}: ${(count / total * 100).toFixed(2)}%`);
console.log(` ${names[nid]}: ${(count / total * 100).toFixed(2)}%`);
}
}