World shape for dimension
This commit is contained in:
@@ -17,8 +17,8 @@ import type { EntityData, EntityEvent, EntityInfo } from "@/common/protocol.ts";
|
||||
import type { ServerPlayer } from "../player.ts";
|
||||
import type { LivingEntity } from "./living_entity.ts";
|
||||
|
||||
// below this, entities have fallen out of the world
|
||||
export const VOID_Y = -64;
|
||||
// this far below the bottom of the world, entities have fallen out of it
|
||||
export const VOID_DEPTH = 64;
|
||||
// the most air most things hold, 15 seconds
|
||||
export const MAX_AIR = 300;
|
||||
// motion faster than minecraft's 10 blocks a tick is reset when loading
|
||||
@@ -69,6 +69,8 @@ export function damage_source(cause: string): DamageSource {
|
||||
// what entities can see and do in the world, the entity manager is one
|
||||
export interface EntityLevel {
|
||||
readonly current_tick: number;
|
||||
// the bottom of the world, see WorldShape
|
||||
readonly min_y: number;
|
||||
has_collision(x: number, y: number, z: number): boolean;
|
||||
// what it bumps into at a block, see common/physics.ts
|
||||
collision_shape(x: number, y: number, z: number): boolean | Shape;
|
||||
@@ -269,7 +271,7 @@ export abstract class Entity {
|
||||
}
|
||||
}
|
||||
|
||||
if (this.y < VOID_Y) {
|
||||
if (this.y < this.level.min_y - VOID_DEPTH) {
|
||||
this.on_below_world();
|
||||
}
|
||||
}
|
||||
@@ -344,7 +346,7 @@ export abstract class Entity {
|
||||
return (this.invulnerable || this.invulnerable_time > 0) && !source.bypasses_invulnerability;
|
||||
}
|
||||
|
||||
// y is below VOID_Y. things that can't die just go away
|
||||
// y is VOID_DEPTH below the bottom of the world. things that can't die just go away
|
||||
on_below_world() {
|
||||
this.remove();
|
||||
}
|
||||
|
||||
@@ -62,6 +62,10 @@ export class EntityManager implements EntityLevel {
|
||||
return this.#host.current_tick;
|
||||
}
|
||||
|
||||
get min_y() {
|
||||
return this.#host.world.shape.min_y;
|
||||
}
|
||||
|
||||
get count() {
|
||||
return this.#entities.size;
|
||||
}
|
||||
|
||||
@@ -4,14 +4,15 @@
|
||||
// fluids flowing, like minecraft's FlowingFluid: a change next to a fluid gets the cells around it looked at again
|
||||
// after the fluid's ticks, and each works out what it should be from its neighbors (see next_level). that changes
|
||||
// more cells, and so it spreads one block a step, and drains away the same way once its source is gone
|
||||
import { AIR, CHUNK_HEIGHT, ID_MASK } from "@/common/constants.ts";
|
||||
import { AIR, ID_MASK } from "@/common/constants.ts";
|
||||
import type { BlockRegistry } from "@/common/everything_registry.ts";
|
||||
import { AIR_ID } from "@/common/protocol.ts";
|
||||
import { fluid_block, get_state_value, set_state_value } from "@/common/utils.ts";
|
||||
|
||||
// what the flow needs of the world
|
||||
export interface FluidWorld {
|
||||
// undefined where the chunk isn't loaded, so fluid stops at the edge of the world instead of making new chunks
|
||||
// undefined where the chunk isn't loaded and above and below the world, so fluid stops at the edge of the world
|
||||
// instead of making new chunks
|
||||
value_at(x: number, y: number, z: number): number | undefined;
|
||||
// by numeric id
|
||||
block(nid: number): BlockRegistry | undefined;
|
||||
@@ -152,7 +153,6 @@ export class FluidFlow {
|
||||
}
|
||||
|
||||
#fluid(x: number, y: number, z: number): Fluid | undefined {
|
||||
if (y < 0 || y >= CHUNK_HEIGHT) return undefined;
|
||||
const value = this.#world.value_at(x, y, z);
|
||||
if (value === undefined) return undefined;
|
||||
const nid = value & ID_MASK;
|
||||
|
||||
+15
-11
@@ -4,7 +4,6 @@
|
||||
import {
|
||||
AIR,
|
||||
BREAK_STAGES,
|
||||
CHUNK_HEIGHT,
|
||||
CHUNK_SIZE,
|
||||
FACE_OFFSETS,
|
||||
Faces,
|
||||
@@ -71,6 +70,7 @@ import type { Menu } from "./menu.ts";
|
||||
import { MACHINE_CONN, SavedPlayer, ServerPlayer } from "./player.ts";
|
||||
import { encode_skin, MAX_SKIN_BYTES, skin_problem } from "./skins.ts";
|
||||
import { ServerWorld, SPAWN_AREA_CHUNKS, Tile } from "./world.ts";
|
||||
import type { WorldShape } from "@/common/world_shape.ts";
|
||||
import { ScheduledTicks } from "./scheduled_ticks.ts";
|
||||
import type { WorldSave, WorldSource } from "./saves.ts";
|
||||
import {
|
||||
@@ -219,7 +219,7 @@ export class GameServer {
|
||||
this.world = new ServerWorld(saved?.seed ?? default_seed, mods.worldgen);
|
||||
this.fluids = new FluidFlow({
|
||||
value_at: (x, y, z) => {
|
||||
if (y < 0 || y >= CHUNK_HEIGHT) return undefined;
|
||||
if (!this.world.shape.contains(y)) return undefined;
|
||||
const loaded = this.world.has_chunk(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
|
||||
return loaded ? this.world.get_block_value(x, y, z) : undefined;
|
||||
},
|
||||
@@ -720,7 +720,7 @@ export class GameServer {
|
||||
|
||||
// a block standing on this one that can't stand on what it is now breaks, like a crop on farmland that was
|
||||
// dug
|
||||
if (y + 1 < CHUNK_HEIGHT) {
|
||||
if (this.world.shape.contains(y + 1)) {
|
||||
const above = this.world.get_block_info(x, y + 1, z);
|
||||
if (above && !can_stand_on(above, id)) {
|
||||
this.destroy_block(x, y + 1, z, player);
|
||||
@@ -1021,6 +1021,7 @@ export class GameServer {
|
||||
players: [...this.#players.values()].map((p) => p.player_info()),
|
||||
entities: this.entities.start_tracking(player),
|
||||
blocks: this.world.block_table(),
|
||||
shape: this.world.shape,
|
||||
spawn: { x: player.x, y: player.y, z: player.z, yaw: player.yaw, pitch: player.pitch },
|
||||
selected_slot: player.selected_slot,
|
||||
player_menu: player.inventory_menu.extra.map(({ x, y, icon }) => ({ x, y, ...(icon ? { icon } : {}) })),
|
||||
@@ -1084,13 +1085,13 @@ export class GameServer {
|
||||
}
|
||||
break;
|
||||
case "break_block":
|
||||
if (is_block_position(message.x, message.y, message.z)) {
|
||||
if (is_block_position(this.world.shape, message.x, message.y, message.z)) {
|
||||
this.#break_block(player, message.x, message.y, message.z);
|
||||
}
|
||||
this.#ack(player, message.seq);
|
||||
break;
|
||||
case "hit_block":
|
||||
if (is_block_position(message.x, message.y, message.z)) {
|
||||
if (is_block_position(this.world.shape, message.x, message.y, message.z)) {
|
||||
this.#hit_block(player, message.x, message.y, message.z);
|
||||
}
|
||||
break;
|
||||
@@ -1110,7 +1111,9 @@ export class GameServer {
|
||||
this.#release_using(player);
|
||||
break;
|
||||
case "use_block":
|
||||
if (is_block_position(message.x, message.y, message.z) && faces.includes(message.face)) {
|
||||
if (
|
||||
is_block_position(this.world.shape, message.x, message.y, message.z) && faces.includes(message.face)
|
||||
) {
|
||||
if (player.using) {
|
||||
// a client that hadn't heard it's using an item yet: what it guessed the click did, it didn't
|
||||
this.#correct_use(player, message.x, message.y, message.z, message.face);
|
||||
@@ -1129,7 +1132,8 @@ export class GameServer {
|
||||
break;
|
||||
case "scroll_block":
|
||||
if (
|
||||
is_block_position(message.x, message.y, message.z) && faces.includes(message.face) &&
|
||||
is_block_position(this.world.shape, message.x, message.y, message.z) &&
|
||||
faces.includes(message.face) &&
|
||||
is_int(message.amount) && message.amount !== 0
|
||||
) {
|
||||
const { x, y, z, face, amount } = message;
|
||||
@@ -1405,7 +1409,7 @@ export class GameServer {
|
||||
|
||||
// place the held block against the face, or into the clicked block if it's replaceable. food that can be planted,
|
||||
// like a potato on farmland, is planted, and eaten when it can't be
|
||||
const placeable = held?.block_id !== undefined && ty >= 0 && ty < CHUNK_HEIGHT &&
|
||||
const placeable = held?.block_id !== undefined && this.world.shape.contains(ty) &&
|
||||
can_place(
|
||||
EverythingRegistry.get<BlockRegistry>("blocks", held.block_id),
|
||||
this.world.get_block_info(tx, ty, tz),
|
||||
@@ -2247,7 +2251,7 @@ export class GameServer {
|
||||
}
|
||||
|
||||
#correct(player: ServerPlayer, x: number, y: number, z: number) {
|
||||
if (y < 0 || y >= CHUNK_HEIGHT || !player.chunks.has_block(x, z)) {
|
||||
if (!this.world.shape.contains(y) || !player.chunks.has_block(x, z)) {
|
||||
return;
|
||||
}
|
||||
// with its states, or the client would show it at its defaults, like a fence that lost its connections
|
||||
@@ -2482,8 +2486,8 @@ function hit_on_block(hit: unknown): [number, number, number] {
|
||||
}
|
||||
|
||||
// messages are parsed json, so the types in ClientMessage are only what a well behaved client sends
|
||||
function is_block_position(x: unknown, y: unknown, z: unknown): boolean {
|
||||
return is_int(x) && is_int(y) && is_int(z) && y >= 0 && y < CHUNK_HEIGHT &&
|
||||
function is_block_position(shape: WorldShape, x: unknown, y: unknown, z: unknown): boolean {
|
||||
return is_int(x) && is_int(y) && is_int(z) && shape.contains(y) &&
|
||||
Math.abs(x) < 30000 * CHUNK_SIZE && Math.abs(z) < 30000 * CHUNK_SIZE;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
import { register_mod_data } from "@/common/mod_loader.ts";
|
||||
import type { ModData, ModListing } from "@/common/mod_loader.ts";
|
||||
import { load_worldgen } from "@/common/worldgen_loader.ts";
|
||||
import type { DimensionJson } from "@/common/worldgen/data.ts";
|
||||
import { dimension_shape, type DimensionJson } from "@/common/worldgen/data.ts";
|
||||
import { cell_height_problem } from "@/common/world_shape.ts";
|
||||
import { worldgen_bundle } from "@/common/worldgen_bundle.ts";
|
||||
import { GameHost, GameServer } from "./game_server.ts";
|
||||
import type { WorldSource } from "./saves.ts";
|
||||
@@ -42,7 +43,9 @@ export async function start_game(
|
||||
for (const json of mod.data.entities) register_entity_type(json.id, mod_entity_factory(json));
|
||||
}
|
||||
|
||||
const generator = default_dimension(mods).generator;
|
||||
const dimension = default_dimension(mods);
|
||||
const generator = dimension.generator;
|
||||
const shape = dimension_shape(dimension);
|
||||
const worldgen_scripts = mods.flatMap((mod) =>
|
||||
mod.worldgen_url
|
||||
? [{ mod: mod.listing.id, url: mod.worldgen_url, engine_access: mod.listing.engine_access }]
|
||||
@@ -53,7 +56,11 @@ export async function start_game(
|
||||
scripts: worldgen_scripts,
|
||||
ores: recipes.ores,
|
||||
generator,
|
||||
shape,
|
||||
});
|
||||
const { terrain, caves } = worldgen.generator.settings;
|
||||
const problem = cell_height_problem(shape, [terrain.cell_height, caves.cell_height]);
|
||||
if (problem) throw new Error(`dimension ${dimension.id} can't be generated by ${generator}: ${problem}`);
|
||||
|
||||
const runtime = new ModRuntime();
|
||||
// before the world loads, its mobs can have effects
|
||||
@@ -80,6 +87,7 @@ export async function start_game(
|
||||
game.worldgen_bundle = worldgen_bundle(
|
||||
game.world.seed,
|
||||
generator,
|
||||
shape,
|
||||
mods.map((mod) => ({
|
||||
id: mod.listing.id,
|
||||
data: mod.data,
|
||||
|
||||
@@ -945,6 +945,12 @@ export class ModRuntime {
|
||||
biome_at: (x, z) => game().world.biome_at(Math.floor(x), Math.floor(z)),
|
||||
season_at: (x, z) =>
|
||||
biome_season(game().world.biome_at(Math.floor(x), Math.floor(z)), game().world.time),
|
||||
get min_y() {
|
||||
return game().world.shape.min_y;
|
||||
},
|
||||
get max_y() {
|
||||
return game().world.shape.max_y;
|
||||
},
|
||||
get seed() {
|
||||
return game().world.seed;
|
||||
},
|
||||
|
||||
+18
-17
@@ -6,8 +6,9 @@
|
||||
//
|
||||
// u8 format, CHUNK_FORMAT
|
||||
// u32 palette size, then each entry: u8 id length, the id as utf-8, u16 state bits
|
||||
// the palette index of every block, in ServerWorld's order (y, then z, then x): u8 each for up to 256 entries,
|
||||
// u16 each for more
|
||||
// the palette index of every block, in ServerWorld's order (y from the dimension's min_y, then z, then x): u8 each
|
||||
// for up to 256 entries, u16 each for more. how many there are is the dimension's, see WorldShape.volume:
|
||||
// a chunk saved with another height doesn't read, and is generated again
|
||||
// u8 biome palette size, then each entry: u8 id length, the id as utf-8
|
||||
// the biome palette index of every column, u8 each, in the order of a layer's blocks
|
||||
//
|
||||
@@ -16,10 +17,9 @@
|
||||
// generation only shows in chunks never generated before.
|
||||
// format 1 had no biomes, and a bit per block after the blocks for whether world generation made it, from when only
|
||||
// chunks players changed were saved. both are read
|
||||
import { CHUNK_AREA, CHUNK_HEIGHT } from "@/common/constants.ts";
|
||||
import { CHUNK_AREA } from "@/common/constants.ts";
|
||||
|
||||
export const CHUNK_FORMAT = 2;
|
||||
export const CHUNK_VOLUME = CHUNK_AREA * CHUNK_HEIGHT;
|
||||
|
||||
export interface SavedChunk {
|
||||
x: number;
|
||||
@@ -64,7 +64,8 @@ export function encode_chunk(chunk: Required<ChunkBlocks>): Uint8Array<ArrayBuff
|
||||
if (chunk.biomes.length !== CHUNK_AREA || biome_palette.length > 255) throw new Error("Bad biomes");
|
||||
const biome_ids = biome_palette.map((id) => new TextEncoder().encode(id));
|
||||
if (biome_ids.some((id) => id.length > 255)) throw new Error("A biome id is too long to save");
|
||||
const size = 1 + 4 + ids.reduce((sum, id) => sum + 1 + id.length + 2, 0) + CHUNK_VOLUME * (wide ? 2 : 1) +
|
||||
const volume = chunk.blocks.length;
|
||||
const size = 1 + 4 + ids.reduce((sum, id) => sum + 1 + id.length + 2, 0) + volume * (wide ? 2 : 1) +
|
||||
1 + biome_ids.reduce((sum, id) => sum + 1 + id.length, 0) + CHUNK_AREA;
|
||||
const bytes = new Uint8Array(size);
|
||||
const view = new DataView(bytes.buffer);
|
||||
@@ -80,13 +81,13 @@ export function encode_chunk(chunk: Required<ChunkBlocks>): Uint8Array<ArrayBuff
|
||||
offset += 2;
|
||||
});
|
||||
if (wide) {
|
||||
for (let i = 0; i < CHUNK_VOLUME; i++) {
|
||||
for (let i = 0; i < volume; i++) {
|
||||
view.setUint16(offset, chunk.blocks[i]);
|
||||
offset += 2;
|
||||
}
|
||||
} else {
|
||||
bytes.set(chunk.blocks, offset);
|
||||
offset += CHUNK_VOLUME;
|
||||
offset += volume;
|
||||
}
|
||||
view.setUint8(offset++, biome_palette.length);
|
||||
for (const id of biome_ids) {
|
||||
@@ -98,15 +99,15 @@ export function encode_chunk(chunk: Required<ChunkBlocks>): Uint8Array<ArrayBuff
|
||||
return bytes;
|
||||
}
|
||||
|
||||
// throws when the bytes aren't a chunk this can read
|
||||
export function decode_chunk(bytes: Uint8Array): ChunkBlocks {
|
||||
// throws when the bytes aren't a chunk this can read. volume is how many blocks the dimension's chunks hold
|
||||
export function decode_chunk(bytes: Uint8Array, volume: number): ChunkBlocks {
|
||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
let offset = 0;
|
||||
const format = view.getUint8(offset++);
|
||||
if (format !== CHUNK_FORMAT && format !== 1) throw new Error(`Unknown chunk format ${format}`);
|
||||
const palette_size = view.getUint32(offset);
|
||||
offset += 4;
|
||||
if (palette_size > CHUNK_VOLUME) throw new Error("Bad palette");
|
||||
if (palette_size > volume) throw new Error("Bad palette");
|
||||
const palette: [string, number][] = [];
|
||||
for (let i = 0; i < palette_size; i++) {
|
||||
const length = view.getUint8(offset++);
|
||||
@@ -116,21 +117,21 @@ export function decode_chunk(bytes: Uint8Array): ChunkBlocks {
|
||||
offset += 2;
|
||||
}
|
||||
const wide = palette_size > 256;
|
||||
if (format === 1 && bytes.length !== offset + CHUNK_VOLUME * (wide ? 2 : 1) + CHUNK_VOLUME / 8) {
|
||||
if (format === 1 && bytes.length !== offset + volume * (wide ? 2 : 1) + volume / 8) {
|
||||
throw new Error("Wrong size");
|
||||
}
|
||||
if (bytes.length < offset + CHUNK_VOLUME * (wide ? 2 : 1)) throw new Error("Wrong size");
|
||||
const blocks = new Uint16Array(CHUNK_VOLUME);
|
||||
if (bytes.length < offset + volume * (wide ? 2 : 1)) throw new Error("Wrong size");
|
||||
const blocks = new Uint16Array(volume);
|
||||
if (wide) {
|
||||
for (let i = 0; i < CHUNK_VOLUME; i++) {
|
||||
for (let i = 0; i < volume; i++) {
|
||||
blocks[i] = view.getUint16(offset);
|
||||
offset += 2;
|
||||
}
|
||||
} else {
|
||||
blocks.set(bytes.subarray(offset, offset + CHUNK_VOLUME));
|
||||
offset += CHUNK_VOLUME;
|
||||
blocks.set(bytes.subarray(offset, offset + volume));
|
||||
offset += volume;
|
||||
}
|
||||
for (let i = 0; i < CHUNK_VOLUME; i++) {
|
||||
for (let i = 0; i < volume; i++) {
|
||||
if (blocks[i] >= palette_size) throw new Error("Bad palette index");
|
||||
}
|
||||
if (format === 1) return { palette, blocks };
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
|
||||
// ticks blocks asked for some ticks ahead, see ServerWorld.schedule_tick. kept by chunk and grouped by when they're due,
|
||||
// so a tick costs nothing until it's due, and a chunk with none due is passed over with one comparison
|
||||
import { CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
|
||||
import { chunk_key } from "@/common/utils.ts";
|
||||
import { index_in_chunk } from "./world.ts";
|
||||
import { HIGHEST_Y, LOWEST_Y } from "@/common/world_shape.ts";
|
||||
|
||||
// the most that run in one tick. the rest run first the next
|
||||
export const MAX_SCHEDULED_TICKS = 65536;
|
||||
@@ -26,7 +26,7 @@ export interface ScheduledTick {
|
||||
readonly key: number;
|
||||
// counts up with every tick scheduled, so of ticks with the same key the first scheduled runs first
|
||||
readonly order: number;
|
||||
// nid * 65536 + index in chunk, which is one per block, see ChunkTicks.waiting
|
||||
// the block's numeric id and place in its chunk, which is one per block, see block_key and ChunkTicks.waiting
|
||||
readonly block: number;
|
||||
}
|
||||
|
||||
@@ -137,7 +137,8 @@ function run_order(a: ScheduledTick, b: ScheduledTick) {
|
||||
return a.key - b.key || a.order - b.order;
|
||||
}
|
||||
|
||||
// a block's place in its chunk, under 65536 since chunks are 256 tall, with its numeric id above
|
||||
// a block's place in its chunk with its numeric id above, the same whatever the dimension's height. under 2^40
|
||||
function block_key(x: number, y: number, z: number, chunk_x: number, chunk_z: number, nid: number) {
|
||||
return nid * 65536 + index_in_chunk(x, y, z, chunk_x, chunk_z);
|
||||
const column = (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE);
|
||||
return (nid * (HIGHEST_Y - LOWEST_Y) + y - LOWEST_Y) * CHUNK_AREA + column;
|
||||
}
|
||||
|
||||
+48
-35
@@ -1,7 +1,8 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
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 {
|
||||
block_light_emission,
|
||||
block_light_opacity,
|
||||
@@ -16,9 +17,9 @@ import { LruMap } from "./lru.ts";
|
||||
import { collision_shape_at, EMPTY_SHAPE, type Shape } from "@/common/block_shapes.ts";
|
||||
import { farming_day } from "@/common/time.ts";
|
||||
import { encode_level_chunk } from "@/common/chunk_streaming.ts";
|
||||
import { CHUNK_VOLUME, ChunkBlocks, decode_chunk, encode_chunk, SavedChunk, SavedChunks } from "./saves.ts";
|
||||
import { ChunkBlocks, decode_chunk, encode_chunk, SavedChunk, SavedChunks } from "./saves.ts";
|
||||
|
||||
// generation only, rebuilt when needed. 256 KB each. with a worldgen pool, it holds what the pool made until the
|
||||
// generation only, rebuilt when needed. 256 KB each in a 256 tall dimension. with a worldgen pool, it holds what the pool made until the
|
||||
// chunks are built, a few dozen chunks ahead of what players are sent
|
||||
const RAW_CACHE_SIZE = 256;
|
||||
const CHUNK_CACHE_SIZE = 256;
|
||||
@@ -103,6 +104,8 @@ export class ServerWorld {
|
||||
readonly seed: string;
|
||||
readonly block_ids: Record<string, number> = {};
|
||||
readonly worldgen: WorldgenSetup;
|
||||
// the dimension's height and sky light, chunks hold all of it
|
||||
readonly shape: WorldShape;
|
||||
// what a freshly placed or generated block stores, by numeric id: the id with its default states
|
||||
readonly default_values: number[] = [];
|
||||
// for light_at, by numeric id
|
||||
@@ -155,6 +158,7 @@ export class ServerWorld {
|
||||
constructor(seed: string, worldgen: WorldgenSetup) {
|
||||
this.seed = seed;
|
||||
this.worldgen = worldgen;
|
||||
this.shape = worldgen.shape;
|
||||
// numbered the same way as generation in a worker numbers them, see common/worldgen_bundle.ts
|
||||
const { block_ids, default_values } = block_numbering();
|
||||
Object.assign(this.block_ids, block_ids);
|
||||
@@ -179,6 +183,7 @@ export class ServerWorld {
|
||||
}
|
||||
|
||||
#find_spawn() {
|
||||
const { min_y, max_y } = this.shape;
|
||||
// chunks in rings around the middle, one column each
|
||||
for (let radius = 0; radius <= SPAWN_SEARCH_CHUNKS; radius++) {
|
||||
for (let cz = -radius; cz <= radius; cz++) {
|
||||
@@ -186,15 +191,15 @@ export class ServerWorld {
|
||||
if (Math.max(Math.abs(cx), Math.abs(cz)) !== radius) continue;
|
||||
const x = cx * CHUNK_SIZE + CHUNK_SIZE / 2;
|
||||
const z = cz * CHUNK_SIZE + CHUNK_SIZE / 2;
|
||||
let y = CHUNK_HEIGHT - 1;
|
||||
while (y > 0 && this.get_block_nid(x, y, z) === AIR) y--;
|
||||
if (y > 0 && this.get_block_info(x, y, z)?.has_collision) {
|
||||
let y = max_y - 1;
|
||||
while (y > min_y && this.get_block_nid(x, y, z) === AIR) y--;
|
||||
if (y > min_y && this.get_block_info(x, y, z)?.has_collision) {
|
||||
return { x: x + 0.5, y: y + 1, z: z + 0.5 };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return { x: 0.5, y: CHUNK_HEIGHT - 1, z: 0.5 };
|
||||
return { x: 0.5, y: max_y - 1, z: 0.5 };
|
||||
}
|
||||
|
||||
get_block_id(x: number, y: number, z: number): string {
|
||||
@@ -203,23 +208,23 @@ export class ServerWorld {
|
||||
}
|
||||
|
||||
get_block_nid(x: number, y: number, z: number): number {
|
||||
if (y < 0 || y >= CHUNK_HEIGHT) {
|
||||
if (!this.shape.contains(y)) {
|
||||
return AIR;
|
||||
}
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
const blocks = this.#get_chunk(chunk_x, chunk_z);
|
||||
return blocks[index_in_chunk(x, y, z, chunk_x, chunk_z)] & ID_MASK;
|
||||
return blocks[this.#index(x, y, z, chunk_x, chunk_z)] & ID_MASK;
|
||||
}
|
||||
|
||||
// the id with its state bits
|
||||
get_block_value(x: number, y: number, z: number): number {
|
||||
if (y < 0 || y >= CHUNK_HEIGHT) {
|
||||
if (!this.shape.contains(y)) {
|
||||
return AIR;
|
||||
}
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
return this.#get_chunk(chunk_x, chunk_z)[index_in_chunk(x, y, z, chunk_x, chunk_z)];
|
||||
return this.#get_chunk(chunk_x, chunk_z)[this.#index(x, y, z, chunk_x, chunk_z)];
|
||||
}
|
||||
|
||||
// whether entities bump into the block there, water and other blocks with "collision": false don't
|
||||
@@ -292,7 +297,7 @@ export class ServerWorld {
|
||||
const found: [number, number, number][] = [];
|
||||
for (let i = 0; i < blocks.length; i++) {
|
||||
if (!wanted[blocks[i] & ID_MASK]) continue;
|
||||
const y = Math.floor(i / CHUNK_AREA);
|
||||
const y = this.shape.min_y + Math.floor(i / CHUNK_AREA);
|
||||
const rest = i % CHUNK_AREA;
|
||||
found.push([
|
||||
chunk_x * CHUNK_SIZE + rest % CHUNK_SIZE,
|
||||
@@ -324,7 +329,7 @@ export class ServerWorld {
|
||||
return this.#opacity[this.get_block_nid(x, y, z)];
|
||||
}
|
||||
|
||||
// the highest block that isn't air, -1 for an empty column. minecraft's WORLD_SURFACE heightmap
|
||||
// the highest block that isn't air, min_y - 1 for an empty column. minecraft's WORLD_SURFACE heightmap
|
||||
surface_height(x: number, z: number): number {
|
||||
return this.#columns_of(x, z)[column_index(x, z)];
|
||||
}
|
||||
@@ -336,10 +341,12 @@ export class ServerWorld {
|
||||
|
||||
// the sky and block light there, 0-15, the same as clients work out (see client/workers/lighting.ts) but for one
|
||||
// position: searching out from it, cheapest first, for the brightest place light could come from. light loses
|
||||
// max(1, opacity) for every block it goes into, and never gets further than 15 blocks
|
||||
// max(1, opacity) for every block it goes into, and never gets further than 15 blocks. no sky light at all in a
|
||||
// dimension without it
|
||||
light_at(x: number, y: number, z: number): { sky: number; block: number } {
|
||||
if (y >= CHUNK_HEIGHT) return { sky: 15, block: 0 };
|
||||
if (y < 0) return { sky: 0, block: 0 };
|
||||
const { min_y, max_y, sky_light } = this.shape;
|
||||
if (y >= max_y) return { sky: sky_light ? 15 : 0, block: 0 };
|
||||
if (y < min_y) return { sky: 0, block: 0 };
|
||||
// the box around it, 3x3 chunks at most, fetched once each. blocks are in it by their offset from the start
|
||||
const first_x = Math.floor((x - LIGHT_REACH) / CHUNK_SIZE);
|
||||
const first_z = Math.floor((z - LIGHT_REACH) / CHUNK_SIZE);
|
||||
@@ -352,7 +359,7 @@ export class ServerWorld {
|
||||
const queues: number[][] = Array.from({ length: LIGHT_REACH }, () => []);
|
||||
let next = 0;
|
||||
const reach = (dx: number, dy: number, dz: number) => {
|
||||
if (y + dy < 0 || y + dy >= CHUNK_HEIGHT) return;
|
||||
if (y + dy < min_y || y + dy >= max_y) return;
|
||||
const at = box_index(dx, dy, dz);
|
||||
if (cost[at] <= next) return;
|
||||
cost[at] = next;
|
||||
@@ -375,9 +382,11 @@ export class ServerWorld {
|
||||
const which = (chunk_z - first_z) * 3 + chunk_x - first_x;
|
||||
const blocks = chunks[which] ??= this.#get_chunk(chunk_x, chunk_z);
|
||||
const column = (bz - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + bx - chunk_x * CHUNK_SIZE;
|
||||
const nid = blocks[by * CHUNK_AREA + column] & ID_MASK;
|
||||
const columns = skies[which] ??= this.#columns_of(bx, bz);
|
||||
if (by >= columns[CHUNK_AREA + column]) sky = Math.max(sky, LIGHT_REACH - lost);
|
||||
const nid = blocks[(by - min_y) * CHUNK_AREA + column] & ID_MASK;
|
||||
if (sky_light) {
|
||||
const columns = skies[which] ??= this.#columns_of(bx, bz);
|
||||
if (by >= columns[CHUNK_AREA + column]) sky = Math.max(sky, LIGHT_REACH - lost);
|
||||
}
|
||||
block = Math.max(block, this.#emission[nid] - lost);
|
||||
// light comes to this block from its neighbors, losing what going into it costs
|
||||
next = lost + Math.max(1, this.#opacity[nid]);
|
||||
@@ -400,13 +409,15 @@ export class ServerWorld {
|
||||
let columns = this.#columns.get(key);
|
||||
if (!columns) {
|
||||
const blocks = this.#get_chunk(chunk_x, chunk_z);
|
||||
const { min_y, height } = this.shape;
|
||||
columns = new Int16Array(CHUNK_AREA * 2);
|
||||
for (let column = 0; column < CHUNK_AREA; column++) {
|
||||
let y = CHUNK_HEIGHT - 1;
|
||||
// from the top, by y above min_y
|
||||
let y = height - 1;
|
||||
while (y >= 0 && (blocks[y * CHUNK_AREA + column] & ID_MASK) === AIR) y--;
|
||||
columns[column] = y;
|
||||
columns[column] = min_y + y;
|
||||
while (y >= 0 && this.#opacity[blocks[y * CHUNK_AREA + column] & ID_MASK] === 0) y--;
|
||||
columns[CHUNK_AREA + column] = y + 1;
|
||||
columns[CHUNK_AREA + column] = min_y + y + 1;
|
||||
}
|
||||
this.#columns.set(key, columns);
|
||||
}
|
||||
@@ -415,7 +426,7 @@ export class ServerWorld {
|
||||
|
||||
// only changes the block, the game server runs behaviors and tiles. state bits default to the block's defaults
|
||||
set_block(x: number, y: number, z: number, id: string, state?: number) {
|
||||
if (y < 0 || y >= CHUNK_HEIGHT) {
|
||||
if (!this.shape.contains(y)) {
|
||||
return;
|
||||
}
|
||||
const nid = id === AIR_ID ? AIR : this.block_ids[id];
|
||||
@@ -427,7 +438,7 @@ export class ServerWorld {
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
const chunk = this.#get_loaded(chunk_x, chunk_z);
|
||||
const index = index_in_chunk(x, y, z, chunk_x, chunk_z);
|
||||
const index = this.#index(x, y, z, chunk_x, chunk_z);
|
||||
chunk.blocks[index] = value;
|
||||
chunk.unknown?.delete(index);
|
||||
|
||||
@@ -488,12 +499,12 @@ export class ServerWorld {
|
||||
return index;
|
||||
};
|
||||
|
||||
const blocks = new Uint16Array(CHUNK_VOLUME);
|
||||
const blocks = new Uint16Array(chunk.blocks.length);
|
||||
const by_value = new Map<number, number>();
|
||||
// runs of the same block are common, air above the ground and stone below
|
||||
let last_value = -1;
|
||||
let last_index = 0;
|
||||
for (let i = 0; i < CHUNK_VOLUME; i++) {
|
||||
for (let i = 0; i < chunk.blocks.length; i++) {
|
||||
const value = chunk.blocks[i];
|
||||
if (value !== last_value) {
|
||||
let index = by_value.get(value);
|
||||
@@ -661,7 +672,7 @@ export class ServerWorld {
|
||||
const data = evicted?.data ?? this.#saved_chunks?.read(chunk_x, chunk_z);
|
||||
try {
|
||||
if (!data) throw new Error("it couldn't be read");
|
||||
return this.#from_saved(chunk_x, chunk_z, decode_chunk(data));
|
||||
return this.#from_saved(chunk_x, chunk_z, decode_chunk(data, this.shape.volume));
|
||||
} catch (e) {
|
||||
console.error(
|
||||
`Couldn't load chunk ${chunk_x}, ${chunk_z}, generating it again: ${(e as Error).message}`,
|
||||
@@ -679,9 +690,9 @@ export class ServerWorld {
|
||||
const nid = id === AIR_ID ? AIR : this.block_ids[id];
|
||||
return nid === undefined ? undefined : block_value(nid, state);
|
||||
});
|
||||
const blocks = new Uint32Array(CHUNK_VOLUME);
|
||||
const blocks = new Uint32Array(this.shape.volume);
|
||||
let unknown: Map<number, [string, number]> | undefined;
|
||||
for (let i = 0; i < CHUNK_VOLUME; i++) {
|
||||
for (let i = 0; i < blocks.length; i++) {
|
||||
const value = values[saved.blocks[i]];
|
||||
if (value === undefined) {
|
||||
(unknown ??= new Map()).set(i, saved.palette[saved.blocks[i]]);
|
||||
@@ -706,6 +717,12 @@ export class ServerWorld {
|
||||
this.#evicted.set(key, { x, z, data: encode_chunk(this.#chunk_blocks(chunk)) });
|
||||
}
|
||||
|
||||
// a block's place in its chunk's blocks, from the bottom of the world. y has to be in the shape
|
||||
#index(x: number, y: number, z: number, chunk_x: number, chunk_z: number) {
|
||||
return (y - this.shape.min_y) * CHUNK_AREA + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE +
|
||||
(x - chunk_x * CHUNK_SIZE);
|
||||
}
|
||||
|
||||
// own blocks, then neighbors' leaves only into air
|
||||
#generate_chunk(chunk_x: number, chunk_z: number) {
|
||||
const blocks = this.#get_raw(chunk_x, chunk_z).blocks.slice();
|
||||
@@ -721,7 +738,7 @@ export class ServerWorld {
|
||||
if (Math.floor(x / CHUNK_SIZE) !== chunk_x || Math.floor(z / CHUNK_SIZE) !== chunk_z) {
|
||||
continue;
|
||||
}
|
||||
const index = index_in_chunk(x, y, z, chunk_x, chunk_z);
|
||||
const index = this.#index(x, y, z, chunk_x, chunk_z);
|
||||
if (blocks[index] === AIR) {
|
||||
blocks[index] = nid;
|
||||
}
|
||||
@@ -740,10 +757,6 @@ function fallback_caller(stack: string) {
|
||||
return match ? `${match[1] ?? "?"} ${match[2]}` : outside;
|
||||
}
|
||||
|
||||
export function index_in_chunk(x: number, y: number, z: number, chunk_x: number, chunk_z: number) {
|
||||
return y * CHUNK_AREA + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE);
|
||||
}
|
||||
|
||||
// a column's place in its chunk, the same as its blocks' in a layer
|
||||
function column_index(x: number, z: number) {
|
||||
return (z - Math.floor(z / CHUNK_SIZE) * CHUNK_SIZE) * CHUNK_SIZE + x - Math.floor(x / CHUNK_SIZE) * CHUNK_SIZE;
|
||||
|
||||
Reference in New Issue
Block a user