Add natural mob spawning for rotwalker
This commit is contained in:
+155
-2
@@ -1,5 +1,10 @@
|
||||
import { AIR, CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE, ID_MASK } from "$/common/constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import {
|
||||
block_light_emission,
|
||||
block_light_opacity,
|
||||
BlockRegistry,
|
||||
EverythingRegistry,
|
||||
} from "$/common/everything_registry.ts";
|
||||
import { generate_raw_chunk, RawChunk, WorldgenSetup } from "$/common/generation.ts";
|
||||
import { AIR_ID, BlockChange } from "$/common/protocol.ts";
|
||||
import { block_value, chunk_key, default_block_value } from "$/common/utils.ts";
|
||||
@@ -12,6 +17,11 @@ import { CHUNK_VOLUME, ChunkBlocks, decode_chunk, encode_chunk, is_changed, Save
|
||||
// generation only, rebuilt when needed. 256 KB each
|
||||
const RAW_CACHE_SIZE = 128;
|
||||
const CHUNK_CACHE_SIZE = 256;
|
||||
// surface and sky heights, 1 KB each
|
||||
const COLUMN_CACHE_SIZE = 1024;
|
||||
// light reaches at most this far, see light_at
|
||||
const LIGHT_REACH = 15;
|
||||
const LIGHT_BOX = LIGHT_REACH * 2 + 1;
|
||||
// how far from the middle of the world to look for dry land to spawn on, in chunks
|
||||
const SPAWN_SEARCH_CHUNKS = 32;
|
||||
// generated for worldgen_fingerprint, spread out so most changes to world generation touch at least one
|
||||
@@ -40,10 +50,17 @@ export class ServerWorld {
|
||||
readonly worldgen: WorldgenSetup | undefined;
|
||||
// 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
|
||||
#opacity: Uint8Array;
|
||||
#emission: Uint8Array;
|
||||
|
||||
// what generation made, and the final blocks with neighbors' leaves and player changes applied
|
||||
#raw = new LruMap<number, RawChunk>(RAW_CACHE_SIZE);
|
||||
#chunks = new LruMap<number, Uint32Array>(CHUNK_CACHE_SIZE);
|
||||
// what generation decided each column's biome is, kept for longer than the rest of it
|
||||
#biomes = new LruMap<number, string[]>(COLUMN_CACHE_SIZE);
|
||||
// per column of a chunk, the highest block that isn't air, then the lowest y that sees the sky. see #columns_of
|
||||
#columns = new LruMap<number, Int16Array>(COLUMN_CACHE_SIZE);
|
||||
|
||||
// changes from generated terrain, per chunk. saved as whole chunks
|
||||
#changes = new Map<number, Map<string, BlockChange>>();
|
||||
@@ -70,9 +87,14 @@ export class ServerWorld {
|
||||
constructor(seed: string, worldgen?: WorldgenSetup) {
|
||||
this.seed = seed;
|
||||
this.worldgen = worldgen;
|
||||
EverythingRegistry.get_registry<BlockRegistry>("blocks").forEach((block, nid) => {
|
||||
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
|
||||
this.#opacity = new Uint8Array(ID_MASK + 1);
|
||||
this.#emission = new Uint8Array(ID_MASK + 1);
|
||||
blocks.forEach((block, nid) => {
|
||||
this.block_ids[block.id] = nid;
|
||||
this.default_values[nid] = default_block_value(nid, block);
|
||||
this.#opacity[nid] = block_light_opacity(block);
|
||||
this.#emission[nid] = block_light_emission(block);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -141,6 +163,128 @@ export class ServerWorld {
|
||||
return nid === AIR ? undefined : EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid);
|
||||
}
|
||||
|
||||
// like "bworld:plains", what generation decided
|
||||
biome_at(x: number, z: number): string {
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
const biomes = this.#biomes.get(chunk_key(chunk_x, chunk_z)) ?? this.#get_raw(chunk_x, chunk_z).biomes;
|
||||
return biomes[column_index(x, z)];
|
||||
}
|
||||
|
||||
// whether the chunk is built and kept, like a loaded chunk in minecraft. nothing builds one by asking
|
||||
has_chunk(chunk_x: number, chunk_z: number) {
|
||||
return this.#chunks.has(chunk_key(chunk_x, chunk_z));
|
||||
}
|
||||
|
||||
// counts as using the chunk so it's kept longer, if it's there. returns whether it is, see has_chunk
|
||||
keep_chunk(chunk_x: number, chunk_z: number) {
|
||||
return this.#chunks.get(chunk_key(chunk_x, chunk_z)) !== undefined;
|
||||
}
|
||||
|
||||
// whether every chunk within radius blocks of x, z is, see has_chunk
|
||||
has_chunks_around(x: number, z: number, radius: number) {
|
||||
for (let cx = Math.floor((x - radius) / CHUNK_SIZE); cx <= Math.floor((x + radius) / CHUNK_SIZE); cx++) {
|
||||
for (let cz = Math.floor((z - radius) / CHUNK_SIZE); cz <= Math.floor((z + radius) / CHUNK_SIZE); cz++) {
|
||||
if (!this.has_chunk(cx, cz)) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// how much light going through the block there loses, 0-15
|
||||
light_opacity(x: number, y: number, z: number) {
|
||||
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
|
||||
surface_height(x: number, z: number): number {
|
||||
return this.#columns_of(x, z)[column_index(x, z)];
|
||||
}
|
||||
|
||||
// the lowest y with nothing above it that stops light, where sky light is 15
|
||||
sky_height(x: number, z: number): number {
|
||||
return this.#columns_of(x, z)[CHUNK_AREA + column_index(x, z)];
|
||||
}
|
||||
|
||||
// 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
|
||||
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 };
|
||||
// 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);
|
||||
const chunks: (Uint32Array | undefined)[] = [];
|
||||
const skies: (Int16Array | undefined)[] = [];
|
||||
const box_index = (dx: number, dy: number, dz: number) =>
|
||||
((dy + LIGHT_REACH) * LIGHT_BOX + dz + LIGHT_REACH) * LIGHT_BOX + dx + LIGHT_REACH;
|
||||
// what light loses on its way from a block to the start. 255 is not reached yet
|
||||
const cost = new Uint8Array(LIGHT_BOX ** 3).fill(255);
|
||||
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;
|
||||
const at = box_index(dx, dy, dz);
|
||||
if (cost[at] <= next) return;
|
||||
cost[at] = next;
|
||||
queues[next].push(dx, dy, dz);
|
||||
};
|
||||
reach(0, 0, 0);
|
||||
|
||||
let sky = 0;
|
||||
let block = 0;
|
||||
for (let lost = 0; lost < LIGHT_REACH; lost++) {
|
||||
// nothing further away can be brighter than what's been found
|
||||
if (LIGHT_REACH - lost <= sky && LIGHT_REACH - lost <= block) break;
|
||||
const queue = queues[lost];
|
||||
for (let i = 0; i < queue.length; i += 3) {
|
||||
const dx = queue[i], dy = queue[i + 1], dz = queue[i + 2];
|
||||
if (cost[box_index(dx, dy, dz)] !== lost) continue;
|
||||
const bx = x + dx, by = y + dy, bz = z + dz;
|
||||
const chunk_x = Math.floor(bx / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(bz / CHUNK_SIZE);
|
||||
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);
|
||||
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]);
|
||||
if (next >= LIGHT_REACH) continue;
|
||||
reach(dx, dy - 1, dz);
|
||||
reach(dx, dy + 1, dz);
|
||||
reach(dx - 1, dy, dz);
|
||||
reach(dx + 1, dy, dz);
|
||||
reach(dx, dy, dz - 1);
|
||||
reach(dx, dy, dz + 1);
|
||||
}
|
||||
}
|
||||
return { sky, block };
|
||||
}
|
||||
|
||||
#columns_of(x: number, z: number) {
|
||||
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
||||
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
||||
const key = chunk_key(chunk_x, chunk_z);
|
||||
let columns = this.#columns.get(key);
|
||||
if (!columns) {
|
||||
const blocks = this.#get_chunk(chunk_x, chunk_z);
|
||||
columns = new Int16Array(CHUNK_AREA * 2);
|
||||
for (let column = 0; column < CHUNK_AREA; column++) {
|
||||
let y = CHUNK_HEIGHT - 1;
|
||||
while (y >= 0 && (blocks[y * CHUNK_AREA + column] & ID_MASK) === AIR) y--;
|
||||
columns[column] = y;
|
||||
while (y >= 0 && this.#opacity[blocks[y * CHUNK_AREA + column] & ID_MASK] === 0) y--;
|
||||
columns[CHUNK_AREA + column] = y + 1;
|
||||
}
|
||||
this.#columns.set(key, columns);
|
||||
}
|
||||
return columns;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
@@ -157,6 +301,8 @@ export class ServerWorld {
|
||||
const blocks = this.#get_chunk(chunk_x, chunk_z);
|
||||
blocks[index_in_chunk(x, y, z, chunk_x, chunk_z)] = value;
|
||||
|
||||
this.#columns.delete(chunk_key(chunk_x, chunk_z));
|
||||
|
||||
const state_bits = value >>> 16;
|
||||
this.#record_change(x, y, z, id, state_bits);
|
||||
this.dirty = true;
|
||||
@@ -221,6 +367,7 @@ export class ServerWorld {
|
||||
console.error(`Couldn't load chunk ${chunk_x}, ${chunk_z}: ${(e as Error).message}`);
|
||||
continue;
|
||||
}
|
||||
this.#columns.delete(chunk_key(chunk_x, chunk_z));
|
||||
const generated = regenerate ? this.#generate_chunk(chunk_x, chunk_z) : undefined;
|
||||
// unknown ids, from mods that are gone, stay as changes so they come back with the mod
|
||||
const values = chunk.palette.map(([id, state]) => {
|
||||
@@ -359,6 +506,7 @@ export class ServerWorld {
|
||||
if (!raw) {
|
||||
raw = generate_raw_chunk(chunk_x, chunk_z, this.seed, this.block_ids, this.worldgen, this.default_values);
|
||||
this.#raw.set(key, raw);
|
||||
this.#biomes.set(key, raw.biomes);
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
@@ -414,3 +562,8 @@ export class ServerWorld {
|
||||
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