World shape for dimension

This commit is contained in:
2026-11-06 22:49:43 -03:00
parent 7458a2d42a
commit 818ce22dd6
60 changed files with 749 additions and 349 deletions
+22 -1
View File
@@ -6,6 +6,7 @@
// minecraft's (density functions, the noise router, surface rules) where the idea is the same
import * as z from "zod";
import { find_problems, ID_PATTERN, is_object, type Problems, TAG_ID_PATTERN } from "../schema.ts";
import { DEFAULT_SHAPE, HIGHEST_Y, LOWEST_Y, shape_problem, WorldShape } from "../world_shape.ts";
// ids of worldgen data can have folders, like "bworld:overworld/erosion"
export const WORLDGEN_ID_PATTERN = TAG_ID_PATTERN;
@@ -138,6 +139,20 @@ export interface DimensionJson {
// the noise settings that generate it
generator: string;
default?: boolean;
// its blocks go from min_y up to max_y - 1, see WorldShape. 0 and 256 when not set
min_y?: number;
max_y?: number;
// whether light comes from the sky, true when not set
sky_light?: boolean;
}
// the shape a dimension's json gives it
export function dimension_shape(json: DimensionJson): WorldShape {
return new WorldShape({
min_y: json.min_y ?? DEFAULT_SHAPE.min_y,
max_y: json.max_y ?? DEFAULT_SHAPE.max_y,
sky_light: json.sky_light ?? DEFAULT_SHAPE.sky_light,
});
}
// what worldgen data refers to, for checking it all exists
@@ -271,7 +286,7 @@ const cell_height = z.literal([1, 2, 4, 8, 16, 32]);
const noise_settings_schema: z.ZodType<NoiseSettingsJson> = z.object({
id: worldgen_id,
sea_level: z.int().min(0).max(256),
sea_level: z.int().min(LOWEST_Y).max(HIGHEST_Y),
default_block: block_id,
cell_width: z.literal([1, 2, 4, 8, 16]),
terrain: z.object({ density, cell_height }),
@@ -327,6 +342,12 @@ const dimension_schema: z.ZodType<DimensionJson> = z.strictObject({
id: worldgen_id,
generator: z.string("must be a noise settings' id").regex(WORLDGEN_ID_PATTERN),
default: z.boolean().optional(),
min_y: z.int().optional(),
max_y: z.int().optional(),
sky_light: z.boolean().optional(),
}).superRefine((json, ctx) => {
const problem = shape_problem(json.min_y ?? DEFAULT_SHAPE.min_y, json.max_y ?? DEFAULT_SHAPE.max_y);
if (problem) ctx.addIssue({ code: "custom", message: problem });
});
export function validate_noise(json: unknown): Problems {
+16 -18
View File
@@ -6,7 +6,7 @@
// one file. the types and modifiers here are built in, worldgen scripts register more
import { EverythingRegistry } from "../everything_registry.ts";
import type { BlockRegistry } from "../everything_registry.ts";
import { CHUNK_HEIGHT, CHUNK_SIZE } from "../constants.ts";
import { CHUNK_SIZE } from "../constants.ts";
import * as z from "zod";
import {
add_problems,
@@ -371,8 +371,8 @@ const MODIFIERS: Record<string, Builtin<Modifier>> = {
return (chunk, positions) =>
positions.flatMap((p) => {
if (!inside(chunk, p.x, p.z)) return [];
let y = CHUNK_HEIGHT - 1;
while (y >= 0 && !counts_for_heightmap(chunk.get_block(p.x, y, p.z), floor)) y--;
let y = chunk.max_y - 1;
while (y >= chunk.min_y && !counts_for_heightmap(chunk.get_block(p.x, y, p.z), floor)) y--;
return [{ x: p.x, y: y + 1, z: p.z }];
});
},
@@ -457,7 +457,7 @@ function block_predicate(json: unknown): Predicate {
// whether a block could be at x y z: where it is, it isn't air, and it can stand on what's under it (place_on)
function survives(chunk: FeatureChunk, id: string, x: number, y: number, z: number) {
if (y < 0 || y >= CHUNK_HEIGHT) return false;
if (y < chunk.min_y || y >= chunk.max_y) return false;
const below = block_at(chunk, x, y - 1, z);
return below !== undefined && can_stand_on(EverythingRegistry.get<BlockRegistry>("blocks", id), below);
}
@@ -518,24 +518,22 @@ function int_provider(json: unknown): IntProvider {
// heights: an anchor ({ "absolute" }, { "above_bottom" } or { "below_top" }), { "type": "constant", "value": anchor }
// or { "type": "uniform", "min_inclusive": anchor, "max_inclusive": anchor }
function anchor(json: unknown): number {
// the bottom and top are the chunk's dimension's
function anchor(json: unknown): IntProvider {
const object = json as Record<string, number>;
if (object.absolute !== undefined) return object.absolute;
if (object.above_bottom !== undefined) return object.above_bottom;
return CHUNK_HEIGHT - 1 - object.below_top;
if (object.absolute !== undefined) return () => object.absolute;
if (object.above_bottom !== undefined) return (chunk) => chunk.min_y + object.above_bottom;
return (chunk) => chunk.max_y - 1 - object.below_top;
}
function height_provider(json: unknown): IntProvider {
const object = json as Record<string, unknown>;
if (object.type === undefined) {
const y = anchor(object);
return () => y;
}
if (builtin_name(String(object.type)) === "constant") {
const y = anchor(object.value);
return () => y;
}
if (object.type === undefined) return anchor(object);
if (builtin_name(String(object.type)) === "constant") return anchor(object.value);
const min = anchor(object.min_inclusive);
const span = anchor(object.max_inclusive) - min + 1;
return (chunk) => min + Math.floor(chunk.rng.next() * span);
const max = anchor(object.max_inclusive);
return (chunk) => {
const low = min(chunk);
return low + Math.floor(chunk.rng.next() * (max(chunk) - low + 1));
};
}
+17 -14
View File
@@ -5,7 +5,8 @@
// 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 NoiseTerrain and docs/modding/worldgen/terrain.md. trees and the rest
// are features, after it
import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "@/common/constants.ts";
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
import type { WorldShape } from "@/common/world_shape.ts";
import { get_biome } from "./biomes.ts";
import type { NoiseTerrain, TerrainColumn } from "./terrain.ts";
import {
@@ -16,7 +17,7 @@ import {
type SurfaceContext,
} from "./surface_rules.ts";
// fills blocks (indexed y * CHUNK_AREA + z * CHUNK_SIZE + x) and each column's surface height and biome. block is
// fills blocks (indexed (y - min_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
export function generate_noise_chunk(
blocks: Uint32Array,
@@ -24,6 +25,7 @@ export function generate_noise_chunk(
biomes: string[],
chunk_x: number,
chunk_z: number,
shape: WorldShape,
terrain: NoiseTerrain,
block: (id: string) => number,
) {
@@ -32,8 +34,9 @@ export function generate_noise_chunk(
const cell_height = settings.terrain.cell_height;
const cave_cell_height = settings.caves.cell_height;
const corners = CHUNK_SIZE / cell_width + 1;
const terrain_levels = CHUNK_HEIGHT / cell_height + 1;
const cave_levels = CHUNK_HEIGHT / cave_cell_height + 1;
const { min_y, max_y, height } = shape;
const terrain_levels = height / cell_height + 1;
const cave_levels = height / cave_cell_height + 1;
const x0 = chunk_x * CHUNK_SIZE;
const z0 = chunk_z * CHUNK_SIZE;
@@ -51,10 +54,10 @@ export function generate_noise_chunk(
const x = x0 + (i % corners) * cell_width;
const z = z0 + Math.floor(i / corners) * cell_width;
for (let level = 0; level < terrain_levels; level++) {
solid[i * terrain_levels + level] = terrain.density(column, x, level * cell_height, z);
solid[i * terrain_levels + level] = terrain.density(column, x, min_y + level * cell_height, z);
}
for (let level = 0; level < cave_levels; level++) {
caves[i * cave_levels + level] = terrain.cave(column, x, level * cave_cell_height, z);
caves[i * cave_levels + level] = terrain.cave(column, x, min_y + level * cave_cell_height, z);
}
}
@@ -134,9 +137,9 @@ export function generate_noise_chunk(
grid[c00 * levels + level] * w00 + grid[c10 * levels + level] * w10 +
grid[c01 * levels + level] * w01 + grid[c11 * levels + level] * w11;
// solid where both the terrain and the caves are above 0
const is_solid = new Uint8Array(CHUNK_HEIGHT);
for (let y = 0; y < CHUNK_HEIGHT; y++) {
// solid where both the terrain and the caves are above 0, by y above min_y
const is_solid = new Uint8Array(height);
for (let y = 0; y < height; y++) {
const level = Math.min(Math.floor(y / cell_height), terrain_levels - 2);
const t = (y - level * cell_height) / cell_height;
let density = blend(solid, terrain_levels, level) * (1 - t) + blend(solid, terrain_levels, level + 1) * t;
@@ -151,8 +154,8 @@ export function generate_noise_chunk(
}
const column = column_at(x, z);
let surface_y = CHUNK_HEIGHT - 1;
while (surface_y > 0 && !is_solid[surface_y]) surface_y--;
let surface_y = max_y - 1;
while (surface_y > min_y && !is_solid[surface_y - min_y]) surface_y--;
const ground = column.height;
const neighbors = [column_at(x - 1, z), column_at(x + 1, z), column_at(x, z - 1), column_at(x, z + 1)];
@@ -168,7 +171,7 @@ export function generate_noise_chunk(
context.preliminary = ground;
context.biome = biome;
context.surface = surface_of(biome);
const index = (y: number) => y * CHUNK_AREA + z * CHUNK_SIZE + x;
const index = (y: number) => (y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x;
// the same sum as the rule's own condition, so it can't disagree with it
const lowest = ground + reach;
// solid blocks since the last air going down, 0 is a block with air on top
@@ -177,9 +180,9 @@ export function generate_noise_chunk(
let open_sky = true;
// whether the ground's surface is under water, for its cover
let underwater = false;
for (let y = CHUNK_HEIGHT - 1; y >= 0; y--) {
for (let y = max_y - 1; y >= min_y; y--) {
context.y = y;
if (!is_solid[y]) {
if (!is_solid[y - min_y]) {
depth = -1;
if (y < fluid_top) {
const fluid = fluids(context, open_sky);