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
+1 -1
View File
@@ -291,7 +291,7 @@ export function register_fishing(ctx: ServerContext) {
!player || player.selected_slot !== cast.slot || player.held_item?.id !== FISHING_ROD ||
distance > LINE_LENGTH || cast.hook.removed ||
(water && ctx.world.get_block(water.x, water.y, water.z) !== water.block) ||
(cast.velocity && (now - cast.thrown_at > MAX_FLIGHT || cast.position.y < 0))
(cast.velocity && (now - cast.thrown_at > MAX_FLIGHT || cast.position.y < ctx.world.min_y))
) {
stop(player, id);
continue;
+18 -10
View File
@@ -12,7 +12,6 @@
import type { FeatureBiome, FeatureChunk, WorldgenContext } from "cubi/worldgen";
const CHUNK_SIZE = 16;
const CHUNK_HEIGHT = 256;
const CELL = 4;
// trunks are at least this far apart
@@ -87,7 +86,8 @@ function tree_sites(chunk: FeatureChunk): TreeSite[] {
type PlaceBlock = (x: number, y: number, z: number, block: "log" | "leaves") => void;
// grows a tree with its trunk's bottom at x, y, z. returns false when it wouldn't fit under the top of the world
function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random, place: PlaceBlock) {
// max_y is the top of the world, nothing grows into it
function grow_tree(kind: TreeKind, x: number, y: number, z: number, max_y: number, rng: Random, place: PlaceBlock) {
const random = (min: number, max: number) => min + Math.floor(rng.next() * (max - min + 1));
const trunk = (height: number) => {
for (let i = 0; i < height; i++) place(x, y + i, z, "log");
@@ -110,7 +110,7 @@ function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random,
// birch is oak's canopy on a taller, straight trunk
const height = kind === "birch" ? random(5, 7) : random(4, 6);
top = y + height + 1;
if (top >= CHUNK_HEIGHT - 1) return false;
if (top >= max_y - 1) return false;
trunk(height);
layer(y + height - 2, 2, true);
layer(y + height - 1, 2, true);
@@ -122,7 +122,7 @@ function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random,
// a short trunk under a wide, rounded canopy
const height = random(4, 6);
top = y + height + 1;
if (top >= CHUNK_HEIGHT - 1) return false;
if (top >= max_y - 1) return false;
trunk(height);
layer(y + height - 2, 2, false);
layer(y + height - 1, MAX_CANOPY, false);
@@ -133,7 +133,7 @@ function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random,
case "big_oak": {
const height = random(6, 8);
top = y + height + 1;
if (top >= CHUNK_HEIGHT - 1) return false;
if (top >= max_y - 1) return false;
trunk(height);
layer(y + height - 3, 2, true);
layer(y + height - 2, 3, false);
@@ -145,7 +145,7 @@ function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random,
case "spruce": {
const height = random(7, 10);
top = y + height + 1;
if (top >= CHUNK_HEIGHT - 1) return false;
if (top >= max_y - 1) return false;
trunk(height);
// a cone of layers getting wider going down, every other one narrower, like minecraft's spruce
place(x, y + height + 1, z, "leaves");
@@ -160,7 +160,7 @@ function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random,
case "jungle": {
const height = random(9, 13);
top = y + height + 1;
if (top >= CHUNK_HEIGHT - 1) return false;
if (top >= max_y - 1) return false;
trunk(height);
layer(y + height - 2, 3, false);
layer(y + height - 1, 3, true);
@@ -172,7 +172,7 @@ function grow_tree(kind: TreeKind, x: number, y: number, z: number, rng: Random,
// a short trunk that leans one way at the top, under a flat, wide canopy
const height = random(4, 5);
top = y + height + 2;
if (top >= CHUNK_HEIGHT - 1) return false;
if (top >= max_y - 1) return false;
trunk(height);
const lean_x = random(-1, 1);
const lean_z = lean_x === 0 ? (rng.next() < 0.5 ? -1 : 1) : 0;
@@ -223,7 +223,7 @@ export function register_trees(gen: WorldgenContext) {
const x0 = chunk.x * CHUNK_SIZE;
const z0 = chunk.z * CHUNK_SIZE;
const place = (x: number, y: number, z: number, block: "log" | "leaves", wood: string) => {
if (y < 0 || y >= CHUNK_HEIGHT) return;
if (y < chunk.min_y || y >= chunk.max_y) return;
const id = `bworld:${wood}_${block}`;
if (x < x0 || x >= x0 + CHUNK_SIZE || z < z0 || z >= z0 + CHUNK_SIZE) {
// the other chunk only takes it where it has air
@@ -247,7 +247,15 @@ export function register_trees(gen: WorldgenContext) {
const kind = here.kinds[Math.floor(site.rng.next() * here.kinds.length)] as TreeKind;
// big oaks grow oak, every other kind its own wood, unless the biome says
const wood = here.wood[kind] ?? (kind === "big_oak" ? "oak" : kind);
grow_tree(kind, site.x, ground_y + 1, site.z, site.rng, (x, y, z, block) => place(x, y, z, block, wood));
grow_tree(
kind,
site.x,
ground_y + 1,
site.z,
chunk.max_y,
site.rng,
(x, y, z, block) => place(x, y, z, block, wood),
);
}
}, { step: "vegetal_decoration" });
}
+7 -7
View File
@@ -32,17 +32,17 @@ export function step(x: number, y: number, z: number, face: Face): [number, numb
return [x + dx, y + dy, z + dz];
}
// a position as one number, so maps and sets of them don't make a string each: x and z in 21 bits and y in 8, further
// than chunk_key lets a world go
// a position as one number, so maps and sets of them don't make a string each: x and z in 20 bits, as far as
// chunk_key lets a world go, and y in 12, as tall as a dimension can be
export function pack(x: number, y: number, z: number) {
return ((x + 2 ** 20) * 2 ** 21 + z + 2 ** 20) * 256 + y;
return ((x + 2 ** 19) * 2 ** 20 + z + 2 ** 19) * 4096 + y + 2048;
}
export function unpack(key: number): [number, number, number] {
const y = key % 256;
const xz = (key - y) / 256;
const z = xz % 2 ** 21;
return [(xz - z) / 2 ** 21 - 2 ** 20, y, z - 2 ** 20];
const y = key % 4096;
const xz = (key - y) / 4096;
const z = xz % 2 ** 20;
return [(xz - z) / 2 ** 20 - 2 ** 19, y - 2048, z - 2 ** 19];
}
// which way a block points, from its states: the name of a facing state, read as FACES' values with 2 bits or 3, or
@@ -3,6 +3,9 @@
"dimension": {
"id": "bworld:overworld",
"generator": "bworld:overworld",
"default": true
"default": true,
"min_y": 0,
"max_y": 256,
"sky_light": true
}
}