Files
bworld/common/world_shape.ts
T
2026-11-06 22:49:43 -03:00

81 lines
3.0 KiB
TypeScript

// 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;
}