191 lines
6.0 KiB
TypeScript
191 lines
6.0 KiB
TypeScript
import { AIR, CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE, ID_MASK } from "$/common/constants.ts";
|
|
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
|
import { generate_raw_chunk, RawChunk, WorldgenSetup } from "$/common/generation.ts";
|
|
import { Container } from "$/common/inventory.ts";
|
|
import { AIR_ID, BlockChange } from "$/common/protocol.ts";
|
|
import { chunk_key } from "$/common/utils.ts";
|
|
import { LruMap } from "./lru.ts";
|
|
|
|
// generation only, rebuilt when needed. 128 KB each
|
|
const RAW_CACHE_SIZE = 256;
|
|
const CHUNK_CACHE_SIZE = 512;
|
|
|
|
// a block with state the server keeps, like a chest's items. never sent to clients as is
|
|
export interface Tile {
|
|
id: string;
|
|
x: number;
|
|
y: number;
|
|
z: number;
|
|
data: Record<string, unknown>;
|
|
containers: Record<string, Container>;
|
|
// a mod block's data, what BlockRef.data holds
|
|
mod_data?: unknown;
|
|
}
|
|
|
|
export function position_key(x: number, y: number, z: number) {
|
|
return `${x},${y},${z}`;
|
|
}
|
|
|
|
// the authoritative world: generated terrain plus everything players changed
|
|
export class ServerWorld {
|
|
readonly seed: string;
|
|
readonly block_ids: Record<string, number> = {};
|
|
readonly worldgen: WorldgenSetup | undefined;
|
|
|
|
// 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);
|
|
|
|
// changes from generated terrain, per chunk. these and the tiles are what gets saved
|
|
#changes = new Map<number, Map<string, BlockChange>>();
|
|
tiles = new Map<string, Tile>();
|
|
|
|
// set when anything that gets saved changes
|
|
dirty = false;
|
|
|
|
on_block_change?: (x: number, y: number, z: number, id: string) => void;
|
|
|
|
constructor(seed: string, worldgen?: WorldgenSetup) {
|
|
this.seed = seed;
|
|
this.worldgen = worldgen;
|
|
EverythingRegistry.get_registry<BlockRegistry>("blocks").forEach((block, nid) => {
|
|
this.block_ids[block.id] = nid;
|
|
});
|
|
}
|
|
|
|
get_block_id(x: number, y: number, z: number): string {
|
|
const nid = this.get_block_nid(x, y, z);
|
|
return nid === AIR ? AIR_ID : EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid)?.id ?? AIR_ID;
|
|
}
|
|
|
|
get_block_nid(x: number, y: number, z: number): number {
|
|
if (y < 0 || y >= CHUNK_HEIGHT) {
|
|
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;
|
|
}
|
|
|
|
get_block_info(x: number, y: number, z: number): BlockRegistry | undefined {
|
|
const nid = this.get_block_nid(x, y, z);
|
|
return nid === AIR ? undefined : EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid);
|
|
}
|
|
|
|
// only changes the block, the game server runs behaviors and tiles
|
|
set_block(x: number, y: number, z: number, id: string) {
|
|
if (y < 0 || y >= CHUNK_HEIGHT) {
|
|
return;
|
|
}
|
|
const nid = id === AIR_ID ? AIR : this.block_ids[id];
|
|
if (nid === undefined) {
|
|
throw new Error(`Unknown block ${id}`);
|
|
}
|
|
|
|
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);
|
|
blocks[index_in_chunk(x, y, z, chunk_x, chunk_z)] = nid;
|
|
|
|
this.#record_change(x, y, z, id);
|
|
this.dirty = true;
|
|
this.on_block_change?.(x, y, z, id);
|
|
}
|
|
|
|
all_changes(): BlockChange[] {
|
|
const all: BlockChange[] = [];
|
|
for (const chunk_changes of this.#changes.values()) {
|
|
all.push(...chunk_changes.values());
|
|
}
|
|
return all;
|
|
}
|
|
|
|
load_changes(changes: BlockChange[]) {
|
|
for (const [x, y, z, id] of changes) {
|
|
this.#record_change(x, y, z, id);
|
|
}
|
|
}
|
|
|
|
get_tile(x: number, y: number, z: number) {
|
|
return this.tiles.get(position_key(x, y, z));
|
|
}
|
|
|
|
add_tile(tile: Tile) {
|
|
this.tiles.set(position_key(tile.x, tile.y, tile.z), tile);
|
|
this.dirty = true;
|
|
}
|
|
|
|
remove_tile(x: number, y: number, z: number) {
|
|
this.tiles.delete(position_key(x, y, z));
|
|
this.dirty = true;
|
|
}
|
|
|
|
#record_change(x: number, y: number, z: number, id: string) {
|
|
const key = chunk_key(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
|
|
let chunk_changes = this.#changes.get(key);
|
|
if (!chunk_changes) {
|
|
chunk_changes = new Map();
|
|
this.#changes.set(key, chunk_changes);
|
|
}
|
|
chunk_changes.set(position_key(x, y, z), [x, y, z, id]);
|
|
}
|
|
|
|
#get_raw(chunk_x: number, chunk_z: number) {
|
|
const key = chunk_key(chunk_x, chunk_z);
|
|
let raw = this.#raw.get(key);
|
|
if (!raw) {
|
|
raw = generate_raw_chunk(chunk_x, chunk_z, this.seed, this.block_ids, this.worldgen);
|
|
this.#raw.set(key, raw);
|
|
}
|
|
return raw;
|
|
}
|
|
|
|
#get_chunk(chunk_x: number, chunk_z: number) {
|
|
const key = chunk_key(chunk_x, chunk_z);
|
|
let blocks = this.#chunks.get(key);
|
|
if (!blocks) {
|
|
blocks = this.#build_chunk(chunk_x, chunk_z);
|
|
this.#chunks.set(key, blocks);
|
|
}
|
|
return blocks;
|
|
}
|
|
|
|
// same rules as the client: own blocks, then neighbors' leaves only into air, then player changes
|
|
#build_chunk(chunk_x: number, chunk_z: number) {
|
|
const blocks = this.#get_raw(chunk_x, chunk_z).blocks.slice();
|
|
|
|
for (let dx = -1; dx <= 1; dx++) {
|
|
for (let dz = -1; dz <= 1; dz++) {
|
|
if (dx === 0 && dz === 0) {
|
|
continue;
|
|
}
|
|
const spills = this.#get_raw(chunk_x + dx, chunk_z + dz).spills;
|
|
for (let i = 0; i < spills.length; i += 4) {
|
|
const [x, y, z, nid] = [spills[i], spills[i + 1], spills[i + 2], spills[i + 3]];
|
|
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);
|
|
if (blocks[index] === AIR) {
|
|
blocks[index] = nid;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (const [x, y, z, id] of this.#changes.get(chunk_key(chunk_x, chunk_z))?.values() ?? []) {
|
|
const nid = id === AIR_ID ? AIR : this.block_ids[id];
|
|
if (nid !== undefined) {
|
|
blocks[index_in_chunk(x, y, z, chunk_x, chunk_z)] = nid;
|
|
}
|
|
}
|
|
|
|
return blocks;
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|