Send block data to clients
This commit is contained in:
@@ -279,10 +279,8 @@ export class GameServer {
|
||||
}
|
||||
}
|
||||
};
|
||||
this.world.on_tile_item_change = (x, y, z, item) => {
|
||||
const data = JSON.stringify(
|
||||
{ type: "set_block_item", x, y, z, item: item ?? null } satisfies ServerMessage,
|
||||
);
|
||||
this.world.on_client_data_change = (x, y, z, changed) => {
|
||||
const data = JSON.stringify({ type: "block_data", x, y, z, data: changed } satisfies ServerMessage);
|
||||
for (const player of this.#players.values()) {
|
||||
if (player.chunks.has_block(x, z)) {
|
||||
this.#host.send(player.conn, data);
|
||||
@@ -688,6 +686,15 @@ export class GameServer {
|
||||
this.world.dirty = true;
|
||||
}
|
||||
|
||||
// BlockRef.set_client_data. a block without a tile only gets one when a field is set, not removed
|
||||
set_tile_client_data(x: number, y: number, z: number, field: string, value: unknown) {
|
||||
if (this.world.get_block_id(x, y, z) === AIR_ID) {
|
||||
return;
|
||||
}
|
||||
const tile = value === undefined ? this.world.get_tile(x, y, z) : this.get_or_create_tile(x, y, z);
|
||||
if (tile) this.world.set_tile_client_data(tile, field, value);
|
||||
}
|
||||
|
||||
// a component turning ticking off for one block, or back on, see BlockRef.set_ticking. it stays off when saved
|
||||
set_tile_ticking(x: number, y: number, z: number, ticking: boolean) {
|
||||
if (this.world.get_block_id(x, y, z) === AIR_ID) {
|
||||
@@ -2108,8 +2115,14 @@ export class GameServer {
|
||||
this.#send(player, { type: "chunk_batch_start" });
|
||||
for (const [x, z] of batch) {
|
||||
this.#host.send(player.conn, this.world.level_chunk(x, z));
|
||||
for (const tile of this.world.item_tiles(x, z)) {
|
||||
this.#send(player, { type: "set_block_item", x: tile.x, y: tile.y, z: tile.z, item: tile.item! });
|
||||
for (const tile of this.world.client_tiles(x, z)) {
|
||||
this.#send(player, {
|
||||
type: "block_data",
|
||||
x: tile.x,
|
||||
y: tile.y,
|
||||
z: tile.z,
|
||||
data: tile.client_data!,
|
||||
});
|
||||
}
|
||||
}
|
||||
this.#send(player, { type: "chunk_batch_finished", size: batch.length });
|
||||
|
||||
@@ -549,6 +549,11 @@ export class ModRuntime {
|
||||
set data(value) {
|
||||
game.set_tile_data(x, y, z, value);
|
||||
},
|
||||
get_client_data: (field) => structuredClone(game.world.get_tile(x, y, z)?.client_data?.[field]) as never,
|
||||
set_client_data(field, value) {
|
||||
if (typeof field !== "string" || field === "") throw new Error("a client data field needs a name");
|
||||
game.set_tile_client_data(x, y, z, field, structuredClone(value));
|
||||
},
|
||||
set_ticking(ticking) {
|
||||
game.set_tile_ticking(x, y, z, ticking);
|
||||
},
|
||||
@@ -691,18 +696,11 @@ export class ModRuntime {
|
||||
get_block_data: (x, y, z) => game().world.get_tile(x, y, z)?.mod_data as never,
|
||||
set_block_ticking: (x, y, z, ticking) => game().set_tile_ticking(x, y, z, ticking),
|
||||
set_block_item: (x, y, z, item) => {
|
||||
if (game().world.get_block_id(x, y, z) === AIR_ID) return;
|
||||
if (!item || item.count <= 0) {
|
||||
const tile = game().world.get_tile(x, y, z);
|
||||
if (tile?.item) game().world.set_tile_item(tile, undefined);
|
||||
return;
|
||||
}
|
||||
game().world.set_tile_item(game().get_or_create_tile(x, y, z), new_stack(item).to_data());
|
||||
},
|
||||
get_block_item: (x, y, z) => {
|
||||
const item = game().world.get_tile(x, y, z)?.item;
|
||||
return item && structuredClone(item);
|
||||
const shown = item && item.count > 0 ? new_stack(item).to_data() : undefined;
|
||||
game().set_tile_client_data(x, y, z, "item", shown);
|
||||
},
|
||||
get_block_item: (x, y, z) =>
|
||||
structuredClone(game().world.get_tile(x, y, z)?.client_data?.item as ItemStack),
|
||||
drop_item: (x, y, z, item) => void game().pop_item(x, y, z, new_stack(item)),
|
||||
spawn_entity: (id, x, y, z) => {
|
||||
const entity = EverythingRegistry.get("entities", id)
|
||||
|
||||
+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