Send block data to clients

This commit is contained in:
2026-10-13 23:26:10 -03:00
parent a10b15a3fd
commit 55ad08266e
18 changed files with 283 additions and 90 deletions
+34 -17
View File
@@ -31,7 +31,6 @@ import { Camera } from "../camera.ts";
import type { Entity } from "../entity/entity.ts";
import type { ModelJson } from "$/common/block_models.ts";
import { clip_shape, collision_shape_at, FULL_SHAPE, type Shape, shape_at } from "$/common/block_shapes.ts";
import type { ItemStack } from "$/common/inventory.ts";
import { BlockPredictions } from "./block_predictions.ts";
export interface Block {
@@ -98,6 +97,14 @@ export interface BlockHitResult {
hit: [number, number, number];
}
// a block's client data, see ClientLevel.block_data
export interface BlockData {
x: number;
y: number;
z: number;
data: Record<string, unknown>;
}
const NEIGHBOR_OFFSETS = [[-1, 0], [1, 0], [0, -1], [0, 1]] as const;
// light spreads diagonally too, so meshing and lighting need all 8
const ALL_NEIGHBOR_OFFSETS = [...NEIGHBOR_OFFSETS, [-1, -1], [1, -1], [-1, 1], [1, 1]] as const;
@@ -133,8 +140,8 @@ export class ClientLevel {
// every entity this client knows about, the local player included, by id
entities = new Map<string, Entity>();
// what blocks with item_display show, by chunk_key then position_key "x,y,z"
block_items = new Map<number, Map<string, { x: number; y: number; z: number; item: ItemStack }>>();
// the client data of blocks that have some, see BlockRef.set_client_data, by chunk_key then position_key "x,y,z"
block_data = new Map<number, Map<string, BlockData>>();
// server_blocks is the server's block id for each of its numeric ids
constructor(server_blocks: string[], view_distance: number) {
@@ -517,24 +524,34 @@ export class ClientLevel {
this.delete_chunk_mesh(chunk);
this.chunks.delete(key);
this.#dirty.delete(chunk);
this.block_items.delete(key);
this.block_data.delete(key);
}
// see set_block_item in common/protocol.ts
set_block_item(x: number, y: number, z: number, item: ItemStack | undefined) {
// see block_data in common/protocol.ts
set_block_data(x: number, y: number, z: number, changed: Record<string, unknown>) {
const key = chunk_key(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
let items = this.block_items.get(key);
if (item) {
if (!this.chunks.has(key)) return;
if (!items) {
items = new Map();
this.block_items.set(key, items);
}
items.set(`${x},${y},${z}`, { x, y, z, item });
} else {
items?.delete(`${x},${y},${z}`);
if (items?.size === 0) this.block_items.delete(key);
if (!this.chunks.has(key)) return;
let blocks = this.block_data.get(key);
const position = `${x},${y},${z}`;
const data = { ...blocks?.get(position)?.data };
for (const [field, value] of Object.entries(changed)) {
if (value === null) delete data[field];
else data[field] = value;
}
if (Object.keys(data).length > 0) {
if (!blocks) {
blocks = new Map();
this.block_data.set(key, blocks);
}
blocks.set(position, { x, y, z, data });
} else if (blocks?.delete(position) && blocks.size === 0) {
this.block_data.delete(key);
}
}
get_block_data(x: number, y: number, z: number): Readonly<Record<string, unknown>> | undefined {
const key = chunk_key(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
return this.block_data.get(key)?.get(`${x},${y},${z}`)?.data;
}
// a chunk is only meshed once all its neighbors exist, otherwise its border faces and light would be
+2
View File
@@ -294,6 +294,8 @@ function client_context(listing: ModListing): ClientContext {
if (nid === AIR) return AIR_ID;
return EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid)?.id;
},
get_client_data: (x, y, z, field) =>
structuredClone(need_client().level.get_block_data(x, y, z)?.[field]) as never,
biome_at: (x, z) => rules_world.biome_at(x, z),
season_at: (x, z) => rules_world.season_at(x, z),
get time() {
+2 -7
View File
@@ -186,13 +186,8 @@ export class ClientPacketListener {
case "block_ack":
level.ack_predictions(message.seq);
break;
case "set_block_item":
level.set_block_item(
message.x,
message.y,
message.z,
message.item ? ItemStack.from_data(message.item) : undefined,
);
case "block_data":
level.set_block_data(message.x, message.y, message.z, message.data);
break;
case "chunk_batch_start":
this.#chunk_batch.on_batch_start();
+15 -5
View File
@@ -7,7 +7,7 @@ import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everyt
import { AssetManager } from "$/client/assets.ts";
import { get_sprite_region } from "$/client/sprites.ts";
import type { ItemEntity } from "$/client/entity/item_entity.ts";
import type { ItemStack } from "$/common/inventory.ts";
import { type ItemData, ItemStack } from "$/common/inventory.ts";
import { light_color, push_vertex, Texture } from "$/client/renderer/mod.ts";
import {
bake_model,
@@ -105,17 +105,27 @@ export function render_item_entity(entity: ItemEntity, partial_tick: number) {
}
}
// every item blocks with item_display show on top of them, see BlockRegistry.item_display. the atlas has to be the
// current texture
// the stacks shown items are drawn as, by their "item" client data, which is replaced whenever it changes
const shown_stacks = new WeakMap<ItemData, ItemStack>();
// every item blocks with item_display show on top of them, their "item" client data, see BlockRegistry.item_display.
// the atlas has to be the current texture
export function render_block_items(level: ClientLevel) {
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
for (const items of level.block_items.values()) {
for (const { x, y, z, item } of items.values()) {
for (const in_chunk of level.block_data.values()) {
for (const { x, y, z, data } of in_chunk.values()) {
const shown = data.item as ItemData | undefined;
if (!shown) continue;
// gone already when this client predicted breaking it
const value = level.get_block_value(x, y, z);
const block = blocks[value & ID_MASK];
if (!block?.item_display) continue;
let item = shown_stacks.get(shown);
if (!item) {
item = ItemStack.from_data(shown);
shown_stacks.set(shown, item);
}
// the block's own light, or the one above for blocks light doesn't get into, since the items sit on top
const here = level.get_light(x, y, z);
const above = level.get_light(x, y + 1, z);