Send block data to clients
This commit is contained in:
+28
-19
@@ -16,7 +16,6 @@ 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 type { ItemData } from "$/common/inventory.ts";
|
||||
import { CHUNK_VOLUME, 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
|
||||
@@ -36,7 +35,7 @@ const SPAWN_SEARCH_CHUNKS = 32;
|
||||
// the chunks around the middle the spawn search usually finds land in, with their neighbors, worth generating ahead
|
||||
export const SPAWN_AREA_CHUNKS = 3;
|
||||
|
||||
// a block with data the server keeps, like where a chest's items are. never sent to clients, but for item
|
||||
// a block with data the server keeps, like where a chest's items are. only client_data is sent to clients
|
||||
export interface Tile {
|
||||
id: string;
|
||||
x: number;
|
||||
@@ -44,8 +43,9 @@ export interface Tile {
|
||||
z: number;
|
||||
// what BlockRef.data holds
|
||||
mod_data?: unknown;
|
||||
// the item it shows, sent to clients, see BlockRegistry.item_display. only set through set_tile_item
|
||||
item?: ItemData;
|
||||
// the fields clients see, see BlockRef.set_client_data. only set through set_tile_client_data, which replaces the
|
||||
// object, so a saved copy of the tile never changes under the save
|
||||
client_data?: Record<string, unknown>;
|
||||
// false when its components turned ticking off for it, see GameServer.set_tile_ticking
|
||||
ticking?: false;
|
||||
}
|
||||
@@ -132,8 +132,8 @@ export class ServerWorld {
|
||||
// unsaved chunks #chunks forgot, ready to save, by chunk_key
|
||||
#evicted = new Map<number, SavedChunk>();
|
||||
tiles = new Map<string, Tile>();
|
||||
// the tiles showing an item, to send with their chunk
|
||||
#item_tiles = new ChunkTiles();
|
||||
// the tiles with client data, to send with their chunk
|
||||
#client_tiles = new ChunkTiles();
|
||||
// the tiles that tick, which the game decides, see GameServer.set_tile_ticking
|
||||
#ticking_tiles = new ChunkTiles();
|
||||
|
||||
@@ -149,7 +149,8 @@ export class ServerWorld {
|
||||
dirty = false;
|
||||
|
||||
on_block_change?: (x: number, y: number, z: number, id: string, state: number) => void;
|
||||
on_tile_item_change?: (x: number, y: number, z: number, item: ItemData | undefined) => void;
|
||||
// the fields that changed, null for the ones that are gone
|
||||
on_client_data_change?: (x: number, y: number, z: number, changed: Record<string, unknown>) => void;
|
||||
|
||||
constructor(seed: string, worldgen: WorldgenSetup) {
|
||||
this.seed = seed;
|
||||
@@ -511,33 +512,41 @@ export class ServerWorld {
|
||||
}
|
||||
|
||||
add_tile(tile: Tile) {
|
||||
const item = tile.item;
|
||||
delete tile.item;
|
||||
this.tiles.set(position_key(tile.x, tile.y, tile.z), tile);
|
||||
if (item) this.set_tile_item(tile, item);
|
||||
this.#client_tiles.set(tile, tile.client_data !== undefined);
|
||||
this.dirty = true;
|
||||
}
|
||||
|
||||
// clients drop its client data with it
|
||||
remove_tile(x: number, y: number, z: number) {
|
||||
const tile = this.get_tile(x, y, z);
|
||||
if (!tile) return;
|
||||
if (tile.item) this.set_tile_item(tile, undefined);
|
||||
if (tile.client_data) {
|
||||
this.#client_tiles.set(tile, false);
|
||||
const gone = Object.fromEntries(Object.keys(tile.client_data).map((field) => [field, null]));
|
||||
this.on_client_data_change?.(x, y, z, gone);
|
||||
}
|
||||
this.#ticking_tiles.set(tile, false);
|
||||
this.tiles.delete(position_key(x, y, z));
|
||||
this.dirty = true;
|
||||
}
|
||||
|
||||
set_tile_item(tile: Tile, item: ItemData | undefined) {
|
||||
if (item) tile.item = item;
|
||||
else delete tile.item;
|
||||
this.#item_tiles.set(tile, item !== undefined);
|
||||
// one field of what clients see of it, any json value. undefined removes it. setting what it already is sends
|
||||
// nothing
|
||||
set_tile_client_data(tile: Tile, field: string, value: unknown) {
|
||||
if (JSON.stringify(tile.client_data?.[field]) === JSON.stringify(value)) return;
|
||||
const { [field]: _, ...data } = tile.client_data ?? {};
|
||||
if (value !== undefined) data[field] = value;
|
||||
if (Object.keys(data).length > 0) tile.client_data = data;
|
||||
else delete tile.client_data;
|
||||
this.#client_tiles.set(tile, tile.client_data !== undefined);
|
||||
this.dirty = true;
|
||||
this.on_tile_item_change?.(tile.x, tile.y, tile.z, item);
|
||||
this.on_client_data_change?.(tile.x, tile.y, tile.z, { [field]: value ?? null });
|
||||
}
|
||||
|
||||
// the tiles in a chunk that show an item
|
||||
item_tiles(chunk_x: number, chunk_z: number): Iterable<Tile> {
|
||||
return this.#item_tiles.in_chunk(chunk_x, chunk_z);
|
||||
// the tiles in a chunk that have client data
|
||||
client_tiles(chunk_x: number, chunk_z: number): Iterable<Tile> {
|
||||
return this.#client_tiles.in_chunk(chunk_x, chunk_z);
|
||||
}
|
||||
|
||||
// whether it runs its components' on_tick and on_second near players
|
||||
|
||||
Reference in New Issue
Block a user