World shape for dimension
This commit is contained in:
@@ -4,16 +4,14 @@
|
||||
// how the server streams the world to clients, like minecraft java since 1.20.2: whole chunks in batches around each
|
||||
// player, and which chunks are in a player's view. see "Chunk Streaming" in docs/modding_plan.md
|
||||
import { deflateSync, inflateSync } from "fflate";
|
||||
import { CHUNK_AREA, CHUNK_HEIGHT } from "./constants.ts";
|
||||
import { CHUNK_AREA } from "./constants.ts";
|
||||
import { SECTION_HEIGHT } from "./world_shape.ts";
|
||||
|
||||
// the view distances clients can ask for, in chunks, like minecraft's
|
||||
export const MIN_VIEW_DISTANCE = 2;
|
||||
export const MAX_VIEW_DISTANCE = 32;
|
||||
|
||||
export const SECTION_HEIGHT = 16;
|
||||
const SECTION_VOLUME = CHUNK_AREA * SECTION_HEIGHT;
|
||||
const SECTIONS = CHUNK_HEIGHT / SECTION_HEIGHT;
|
||||
const CHUNK_VOLUME = CHUNK_AREA * CHUNK_HEIGHT;
|
||||
|
||||
// the first byte of a binary message, the only kind there is so far
|
||||
export const LEVEL_CHUNK = 1;
|
||||
@@ -42,7 +40,8 @@ export function is_within_view(
|
||||
export interface LevelChunk {
|
||||
x: number;
|
||||
z: number;
|
||||
// block values (numeric id | state bits << 16) in the sender's numbering, y then z then x
|
||||
// block values (numeric id | state bits << 16) in the sender's numbering, y from the bottom of the world, then z,
|
||||
// then x. the dimension's whole height
|
||||
blocks: Uint32Array;
|
||||
// each column's biome, x then z
|
||||
biomes: string[];
|
||||
@@ -56,7 +55,8 @@ export interface LevelChunk {
|
||||
// the rest deflated:
|
||||
// u16 biome palette size, then each biome: u8 length and the id as utf-8
|
||||
// u8 each column's biome palette index, x then z. left out when there's one biome
|
||||
// 16 sections of 16x16x16 blocks, bottom first, each a paletted container like minecraft's:
|
||||
// u16 how many sections there are, the dimension's height / 16
|
||||
// each section of 16x16x16 blocks, bottom first, a paletted container like minecraft's:
|
||||
// u8 bits per block
|
||||
// 0: u32 the value of every block
|
||||
// 1, 2, 4, 8: u16 palette size, u32 each value, then each block's palette index packed from the low bits up
|
||||
@@ -71,7 +71,9 @@ export function encode_level_chunk(chunk: LevelChunk): Uint8Array<ArrayBuffer> {
|
||||
for (const biome of chunk.biomes) writer.u8(biome_palette.indexOf(biome));
|
||||
}
|
||||
|
||||
for (let section = 0; section < SECTIONS; section++) {
|
||||
const sections = chunk.blocks.length / SECTION_VOLUME;
|
||||
writer.u16(sections);
|
||||
for (let section = 0; section < sections; section++) {
|
||||
write_section(writer, chunk.blocks.subarray(section * SECTION_VOLUME, (section + 1) * SECTION_VOLUME));
|
||||
}
|
||||
|
||||
@@ -137,8 +139,9 @@ export function decode_level_chunk(bytes: Uint8Array, map: (value: number) => nu
|
||||
biomes[column] = biome_palette[index];
|
||||
}
|
||||
|
||||
const blocks = new Uint32Array(CHUNK_VOLUME);
|
||||
for (let section = 0; section < SECTIONS; section++) {
|
||||
const sections = reader.u16();
|
||||
const blocks = new Uint32Array(sections * SECTION_VOLUME);
|
||||
for (let section = 0; section < sections; section++) {
|
||||
read_section(reader, blocks.subarray(section * SECTION_VOLUME, (section + 1) * SECTION_VOLUME), map);
|
||||
}
|
||||
if (!reader.done) throw new Error("Wrong size");
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ export const ID_MASK = 0xFFFF;
|
||||
export const STATE_SHIFT = 16;
|
||||
|
||||
export const CHUNK_SIZE = 16;
|
||||
export const CHUNK_HEIGHT = 256;
|
||||
// how tall chunks are is up to their dimension, see common/world_shape.ts
|
||||
// the top of oceans and rivers
|
||||
export const SEA_LEVEL = 64;
|
||||
export const CHUNK_AREA = CHUNK_SIZE * CHUNK_SIZE;
|
||||
|
||||
+19
-11
@@ -4,7 +4,8 @@
|
||||
// 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 { AIR, CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
|
||||
import type { WorldShape } from "@/common/world_shape.ts";
|
||||
import type { FeatureChunk, Position } from "@/common/mod_api/worldgen.ts";
|
||||
import type { OreJson } from "@/common/mod_data.ts";
|
||||
import { generate_noise_chunk } from "./worldgen/noise_chunk.ts";
|
||||
@@ -15,6 +16,8 @@ export { named_noise_2d, named_noise_3d };
|
||||
|
||||
// what mods add to generation, see docs/modding/worldgen/index.md
|
||||
export interface WorldgenSetup {
|
||||
// where the dimension's blocks are, chunks hold its whole height
|
||||
shape: WorldShape;
|
||||
// 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
|
||||
@@ -34,7 +37,7 @@ export interface SetupFeature {
|
||||
}
|
||||
|
||||
export interface RawChunk {
|
||||
// numeric block ids, only what this chunk generated itself
|
||||
// numeric block ids, only what this chunk generated itself, indexed (y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x
|
||||
blocks: Uint32Array;
|
||||
// blocks it generated in other chunks (from features like a mod's trees), flattened as x, y, z, numeric id
|
||||
spills: Int32Array;
|
||||
@@ -56,13 +59,14 @@ export function generate_raw_chunk(
|
||||
// the value to store for each numeric id, with its default states. just the id when missing
|
||||
default_values?: number[],
|
||||
): RawChunk {
|
||||
const blocks = new Uint32Array(CHUNK_SIZE * CHUNK_SIZE * CHUNK_HEIGHT);
|
||||
const shape = worldgen.shape;
|
||||
const blocks = new Uint32Array(shape.volume);
|
||||
const spills: number[] = [];
|
||||
const heights = new Int32Array(CHUNK_AREA);
|
||||
const biomes: string[] = new Array(CHUNK_AREA);
|
||||
|
||||
const set = (x: number, y: number, z: number, nid: number) => {
|
||||
if (y < 0 || y >= CHUNK_HEIGHT) {
|
||||
if (!shape.contains(y)) {
|
||||
return;
|
||||
}
|
||||
nid = default_values?.[nid] ?? nid;
|
||||
@@ -74,17 +78,17 @@ export function generate_raw_chunk(
|
||||
}
|
||||
const lx = x - chunk_x * CHUNK_SIZE;
|
||||
const lz = z - chunk_z * CHUNK_SIZE;
|
||||
blocks[y * CHUNK_AREA + lz * CHUNK_SIZE + lx] = nid;
|
||||
blocks[(y - shape.min_y) * CHUNK_AREA + lz * CHUNK_SIZE + lx] = nid;
|
||||
};
|
||||
|
||||
const id = (name: string) => {
|
||||
const nid = block_ids[name];
|
||||
return nid === undefined ? AIR : default_values?.[nid] ?? nid;
|
||||
};
|
||||
generate_noise_chunk(blocks, heights, biomes, chunk_x, chunk_z, worldgen.generator.for_seed(seed), id);
|
||||
generate_noise_chunk(blocks, heights, biomes, chunk_x, chunk_z, shape, worldgen.generator.for_seed(seed), id);
|
||||
|
||||
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, shape, seed, block_ids, ores, default_values);
|
||||
}
|
||||
generate_features(blocks, heights, biomes, set, chunk_x, chunk_z, seed, block_ids, worldgen);
|
||||
|
||||
@@ -96,6 +100,7 @@ function generate_ores(
|
||||
blocks: Uint32Array,
|
||||
chunk_x: number,
|
||||
chunk_z: number,
|
||||
shape: WorldShape,
|
||||
seed: string,
|
||||
block_ids: Record<string, number>,
|
||||
ores: OreJson[],
|
||||
@@ -109,10 +114,10 @@ function generate_ores(
|
||||
}
|
||||
const noises = usable.map((ore) => named_noise_3d(seed, ore.id));
|
||||
|
||||
for (let y = 0; y < CHUNK_HEIGHT; y++) {
|
||||
for (let y = shape.min_y; y < shape.max_y; y++) {
|
||||
for (let lz = 0; lz < CHUNK_SIZE; lz++) {
|
||||
for (let lx = 0; lx < CHUNK_SIZE; lx++) {
|
||||
const index = y * CHUNK_AREA + lz * CHUNK_SIZE + lx;
|
||||
const index = (y - shape.min_y) * CHUNK_AREA + lz * CHUNK_SIZE + lx;
|
||||
const current = blocks[index] & ID_MASK;
|
||||
if (current === AIR) {
|
||||
continue;
|
||||
@@ -159,6 +164,7 @@ function generate_features(
|
||||
return lz * CHUNK_SIZE + lx;
|
||||
};
|
||||
|
||||
const shape = worldgen.shape;
|
||||
const chunk_biomes = new Set(biomes);
|
||||
for (const feature of worldgen.features) {
|
||||
if (feature.biomes && ![...feature.biomes].some((biome) => chunk_biomes.has(biome))) continue;
|
||||
@@ -166,6 +172,8 @@ function generate_features(
|
||||
x: chunk_x,
|
||||
z: chunk_z,
|
||||
seed,
|
||||
min_y: shape.min_y,
|
||||
max_y: shape.max_y,
|
||||
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),
|
||||
@@ -173,10 +181,10 @@ function generate_features(
|
||||
height_at: (x, z) => heights[local(x, z, "height_at")],
|
||||
biome_at: (x, z) => biomes[local(x, z, "biome_at")],
|
||||
get_block(x, y, z) {
|
||||
if (y < 0 || y >= CHUNK_HEIGHT) {
|
||||
if (!shape.contains(y)) {
|
||||
return undefined;
|
||||
}
|
||||
const nid = blocks[y * CHUNK_AREA + local(x, z, "get_block")] & ID_MASK;
|
||||
const nid = blocks[(y - shape.min_y) * CHUNK_AREA + local(x, z, "get_block")] & ID_MASK;
|
||||
return nid === AIR ? "cubi:air" : ids_by_nid[nid];
|
||||
},
|
||||
set_block(x, y, z, id) {
|
||||
|
||||
@@ -58,6 +58,9 @@ export interface ClientContext {
|
||||
// the column's biome and its season, undefined when the chunk isn't loaded
|
||||
biome_at(x: number, z: number): Id | undefined;
|
||||
season_at(x: number, z: number): Season | undefined;
|
||||
// the dimension's blocks go from min_y up to max_y - 1
|
||||
readonly min_y: number;
|
||||
readonly max_y: number;
|
||||
// ticks since 06:00 of day 1, see ServerWorld.time
|
||||
readonly time: number;
|
||||
};
|
||||
|
||||
@@ -362,6 +362,9 @@ export interface ServerWorld {
|
||||
// generation makes there, which takes a while the first time
|
||||
biome_at(x: number, z: number): Id;
|
||||
season_at(x: number, z: number): Season;
|
||||
// the dimension's blocks go from min_y up to max_y - 1. outside them get_block is air and nothing can be placed
|
||||
readonly min_y: number;
|
||||
readonly max_y: number;
|
||||
readonly seed: string;
|
||||
// ticks since 06:00 of day 1. a day is 36000 ticks (06:00 to 18:00), a night 12000
|
||||
readonly time: number;
|
||||
|
||||
@@ -54,6 +54,10 @@ export interface FeatureChunk {
|
||||
readonly x: number;
|
||||
readonly z: number;
|
||||
readonly seed: string;
|
||||
// the dimension's blocks go from min_y up to max_y - 1, get_block is undefined outside them and set_block does
|
||||
// nothing
|
||||
readonly min_y: number;
|
||||
readonly max_y: number;
|
||||
// seeded from the seed, chunk and feature id
|
||||
readonly rng: { next(): number };
|
||||
// seeded from the seed and name alone, the same in every chunk. for things decided across chunks, like trees
|
||||
|
||||
@@ -29,6 +29,7 @@ import {
|
||||
type BiomeSourceJson,
|
||||
density_function_refs,
|
||||
type DensityFunctionJson,
|
||||
dimension_shape,
|
||||
type DimensionJson,
|
||||
new_refs,
|
||||
noise_refs,
|
||||
@@ -38,6 +39,7 @@ import {
|
||||
type WorldgenRefs,
|
||||
} from "./worldgen/data.ts";
|
||||
import { feature_refs, type FeatureJson } from "./worldgen/features.ts";
|
||||
import { cell_height_problem } from "./world_shape.ts";
|
||||
import type { ModData } from "./mod_loader.ts";
|
||||
|
||||
export interface ModReport {
|
||||
@@ -422,6 +424,12 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
if (!worldgen_owners["noise settings"].has(json.generator)) {
|
||||
errors.push(`${file}: noise settings ${json.generator} doesn't exist`);
|
||||
}
|
||||
const settings = all_noise_settings.find((settings) => settings.id === json.generator);
|
||||
const problem = settings && cell_height_problem(
|
||||
dimension_shape(json),
|
||||
[settings.terrain.cell_height, settings.caves.cell_height],
|
||||
);
|
||||
if (problem) errors.push(`${file}: can't be generated by ${json.generator}: ${problem}`);
|
||||
if (json.default && defaults.length > 1) {
|
||||
errors.push(
|
||||
`${file}: only one dimension can be the default, and these all are: ${defaults.join(", ")}`,
|
||||
|
||||
+4
-1
@@ -6,12 +6,13 @@ import type { Faces } from "./constants.ts";
|
||||
import type { ItemData } from "./inventory.ts";
|
||||
import type { FluidVariant } from "./mod_api/shared.ts";
|
||||
import type { ModListing } from "./mod_loader.ts";
|
||||
import type { WorldShapeJson } from "./world_shape.ts";
|
||||
|
||||
export const AIR_ID = "cubi:air";
|
||||
|
||||
// bump when a client and server of different versions can't play together.
|
||||
// the server rejects a different version before the client downloads anything
|
||||
export const PROTOCOL_VERSION = 26;
|
||||
export const PROTOCOL_VERSION = 27;
|
||||
|
||||
export interface PlayerInfo {
|
||||
id: string;
|
||||
@@ -189,6 +190,8 @@ export type ServerMessage =
|
||||
entities: EntityInfo[];
|
||||
// the server's block id for each of its numeric ids, which level_chunk uses. clients number blocks their own way
|
||||
blocks: string[];
|
||||
// the dimension's height and sky light, every level_chunk holds all of its height
|
||||
shape: WorldShapeJson;
|
||||
spawn: { x: number; y: number; z: number; yaw: number; pitch: number };
|
||||
selected_slot: number;
|
||||
// the slots mods added to menu 0, from PLAYER_MENU_EXTRA on, see ctx.menus.extend_player_menu
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// a dimension's shape: where its blocks start and stop going up, and whether light comes from the sky. it comes from
|
||||
// the dimension's json (see DimensionJson) and travels with what needs it, the world, generation and the client's
|
||||
// level, so nothing reads a height of its own
|
||||
import { CHUNK_AREA } from "./constants.ts";
|
||||
|
||||
// the sizes a shape can have, so every y fits in an Int16Array
|
||||
export const LOWEST_Y = -2048;
|
||||
export const HIGHEST_Y = 2048;
|
||||
export const SECTION_HEIGHT = 16;
|
||||
|
||||
// what's sent and saved of it
|
||||
export interface WorldShapeJson {
|
||||
min_y: number;
|
||||
max_y: number;
|
||||
sky_light: boolean;
|
||||
}
|
||||
|
||||
// blocks go from min_y up to max_y, not included. both are multiples of SECTION_HEIGHT
|
||||
export class WorldShape implements WorldShapeJson {
|
||||
readonly min_y: number;
|
||||
readonly max_y: number;
|
||||
// without it, sky light is 0 everywhere and only blocks give off light
|
||||
readonly sky_light: boolean;
|
||||
|
||||
// throws when it isn't a shape a dimension can have, see shape_problem
|
||||
constructor({ min_y, max_y, sky_light }: WorldShapeJson) {
|
||||
const problem = shape_problem(min_y, max_y);
|
||||
if (problem) throw new Error(problem);
|
||||
this.min_y = min_y;
|
||||
this.max_y = max_y;
|
||||
this.sky_light = sky_light;
|
||||
}
|
||||
|
||||
get height() {
|
||||
return this.max_y - this.min_y;
|
||||
}
|
||||
|
||||
// how many blocks a chunk of it holds
|
||||
get volume() {
|
||||
return CHUNK_AREA * this.height;
|
||||
}
|
||||
|
||||
contains(y: number) {
|
||||
return y >= this.min_y && y < this.max_y;
|
||||
}
|
||||
|
||||
// sky and block light, packed as block << 4 | sky, above the world and where nothing has lit yet
|
||||
get open_light() {
|
||||
return this.sky_light ? 15 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
// what a dimension gets when its json doesn't say
|
||||
export const DEFAULT_SHAPE = new WorldShape({ min_y: 0, max_y: 256, sky_light: true });
|
||||
|
||||
// why min_y and max_y can't be a dimension's, undefined when they can
|
||||
export function shape_problem(min_y: number, max_y: number): string | undefined {
|
||||
if (!Number.isInteger(min_y) || !Number.isInteger(max_y)) return `min_y and max_y must be whole numbers`;
|
||||
if (min_y % SECTION_HEIGHT !== 0 || max_y % SECTION_HEIGHT !== 0) {
|
||||
return `min_y ${min_y} and max_y ${max_y} must be multiples of ${SECTION_HEIGHT}`;
|
||||
}
|
||||
if (max_y - min_y < SECTION_HEIGHT) return `max_y ${max_y} must be at least ${SECTION_HEIGHT} above min_y ${min_y}`;
|
||||
if (min_y < LOWEST_Y || max_y > HIGHEST_Y) return `the world has to fit in ${LOWEST_Y} to ${HIGHEST_Y}`;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// why a generator can't fill the shape, undefined when it can: its density is sampled every cell_height blocks up from
|
||||
// min_y, so the cells have to fit the world exactly
|
||||
export function cell_height_problem(shape: WorldShapeJson, cell_heights: number[]): string | undefined {
|
||||
for (const cell_height of cell_heights) {
|
||||
if (shape.min_y % cell_height !== 0 || shape.max_y % cell_height !== 0) {
|
||||
return `its min_y ${shape.min_y} and max_y ${shape.max_y} must be multiples of the generator's ` +
|
||||
`cell_height ${cell_height}`;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
+22
-1
@@ -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
@@ -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));
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -11,6 +11,7 @@ import { type BlockRegistry, EverythingRegistry } from "./everything_registry.ts
|
||||
import { default_block_value } from "./utils.ts";
|
||||
import { generate_raw_chunk, type RawChunk, type WorldgenSetup } from "./generation.ts";
|
||||
import { load_worldgen } from "./worldgen_loader.ts";
|
||||
import { WorldShape, type WorldShapeJson } from "./world_shape.ts";
|
||||
|
||||
// the parts of a mod's data generation reads. tags only the block ones
|
||||
export type WorldgenModData = Pick<
|
||||
@@ -31,6 +32,8 @@ export interface WorldgenBundle {
|
||||
seed: string;
|
||||
// the noise settings the world uses
|
||||
generator: string;
|
||||
// the dimension's, see WorldShape
|
||||
shape: WorldShapeJson;
|
||||
// in load order
|
||||
mods: { id: string; data: WorldgenModData; worldgen_url?: string; engine_access?: boolean }[];
|
||||
}
|
||||
@@ -38,11 +41,13 @@ export interface WorldgenBundle {
|
||||
export function worldgen_bundle(
|
||||
seed: string,
|
||||
generator: string,
|
||||
shape: WorldShapeJson,
|
||||
mods: { id: string; data: ModData; worldgen_url?: string; engine_access?: boolean }[],
|
||||
): WorldgenBundle {
|
||||
return {
|
||||
seed,
|
||||
generator,
|
||||
shape: { min_y: shape.min_y, max_y: shape.max_y, sky_light: shape.sky_light },
|
||||
mods: mods.map(({ id, data, worldgen_url, engine_access }) => ({
|
||||
id,
|
||||
data: {
|
||||
@@ -104,6 +109,7 @@ export async function load_bundle(bundle: WorldgenBundle): Promise<LoadedBundle>
|
||||
),
|
||||
ores: recipes.ores,
|
||||
generator: bundle.generator,
|
||||
shape: new WorldShape(bundle.shape),
|
||||
});
|
||||
const { block_ids, default_values } = block_numbering();
|
||||
return {
|
||||
|
||||
@@ -10,6 +10,7 @@ import { EverythingRegistry } from "./everything_registry.ts";
|
||||
import { ModLoadError } from "./mod_loader.ts";
|
||||
import { loading_mod } from "./hooks.ts";
|
||||
import { compile_generator } from "./worldgen/generator.ts";
|
||||
import type { WorldShape } from "./world_shape.ts";
|
||||
import type { Biome } from "./worldgen/biomes.ts";
|
||||
import {
|
||||
compile_feature,
|
||||
@@ -27,6 +28,8 @@ export interface WorldgenSources {
|
||||
ores: OreJson[][];
|
||||
// the noise settings the world uses. it and data features come from mods' data, which is already registered
|
||||
generator: string;
|
||||
// the dimension's, which the generator fills
|
||||
shape: WorldShape;
|
||||
}
|
||||
|
||||
// a feature before it's put in order
|
||||
@@ -41,6 +44,7 @@ interface Pending {
|
||||
|
||||
export async function load_worldgen(sources: WorldgenSources): Promise<WorldgenSetup> {
|
||||
const setup: WorldgenSetup = {
|
||||
shape: sources.shape,
|
||||
generator: compile_generator(sources.generator),
|
||||
ores: sources.ores,
|
||||
features: [],
|
||||
|
||||
Reference in New Issue
Block a user