Send chunks to players connected to server

This commit is contained in:
2026-09-28 11:14:40 -03:00
parent 677ad8115a
commit ded9a7e5d0
26 changed files with 920 additions and 357 deletions
+22 -7
View File
@@ -1,4 +1,4 @@
// messages sent between the client and the server, as json over a websocket
// messages sent between the client and the server, as json over a websocket. chunks are binary, see level_chunk
import type { Faces } from "./constants.ts";
import type { ItemData } from "./inventory.ts";
import type { ModListing } from "./mod_loader.ts";
@@ -7,7 +7,7 @@ export const AIR_ID = "bworld:air";
// bump when a client and server of different versions can't play together.
// the server rejects a different version before the client downloads anything
export const PROTOCOL_VERSION = 7;
export const PROTOCOL_VERSION = 8;
export interface PlayerInfo {
id: string;
@@ -63,8 +63,13 @@ export interface ScreenLayout {
// clients send what the player is trying to do, the server decides what happens
export type ClientMessage =
| { type: "hello"; name: string; protocol: number }
// sent once the client has downloaded, checked and loaded everything in welcome
| { type: "ready" }
// sent once the client has downloaded, checked and loaded everything in welcome. view_distance is in chunks, the
// server may use less
| { type: "ready"; view_distance?: number }
// the player changed their view distance, like minecraft's client information
| { type: "view_distance"; distance: number }
// answers chunk_batch_finished once the batch is loaded, with how many chunks a tick the client can take
| { type: "chunk_batch_received"; chunks_per_tick: number }
| { type: "move"; x: number; y: number; z: number; yaw: number; pitch: number }
| { type: "break_block"; x: number; y: number; z: number }
// the player started hitting a block, for on_click
@@ -88,7 +93,6 @@ export type ServerMessage =
| {
type: "welcome";
protocol: number;
seed: string;
// in load order, the client downloads and loads them before joining the world
mods: ModListing[];
}
@@ -103,12 +107,22 @@ export type ServerMessage =
players: PlayerInfo[];
// items on the ground
entities: EntityInfo[];
changes: BlockChange[];
// the server's block id for each of its numeric ids, which level_chunk uses. clients number blocks their own way
blocks: string[];
spawn: { x: number; y: number; z: number; yaw: number; pitch: number };
selected_slot: number;
// world time in ticks, see common/time.ts
time: number;
}
// a whole chunk, sent as a binary message and not json. data is what common/chunk_streaming.ts decodes
| { type: "level_chunk"; data: Uint8Array }
// the client can drop the chunk, it's out of view. the server won't send changes to it anymore
| { type: "forget_level_chunk"; x: number; z: number }
// the chunk the player is in, sent when it changes. chunks come in around it
| { type: "set_center_chunk"; x: number; z: number }
// level_chunk messages come between these, size is how many. the client answers with chunk_batch_received
| { type: "chunk_batch_start" }
| { type: "chunk_batch_finished"; size: number }
| { type: "player_join"; player: PlayerInfo }
| { type: "player_leave"; id: string }
| { type: "player_move"; id: string; x: number; y: number; z: number; yaw: number; pitch: number }
@@ -121,7 +135,8 @@ export type ServerMessage =
| { type: "remove_entity"; id: string }
// a player picked up an item, it flies to them and goes away
| { type: "take_entity"; id: string; player: string }
// also sent to the player who caused it, which corrects anything their client predicted wrong
// to the players who have the chunk, including the one who caused it, which corrects anything their client
// predicted wrong
| { type: "set_block"; x: number; y: number; z: number; id: string; state?: number }
| { type: "chat"; from?: string; text: string }
| { type: "teleport"; x: number; y: number; z: number }