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
+20 -2
View File
@@ -5,6 +5,8 @@ import type { ServerModSource } from "$/server/game/load_mods.ts";
import { GameServer } from "$/server/game/game_server.ts";
import { start_game } from "$/server/game/load_mods.ts";
import { PROTOCOL_VERSION } from "$/common/protocol.ts";
import { decode_level_chunk } from "$/common/chunk_streaming.ts";
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "$/common/constants.ts";
import type { WorldSave } from "$/server/game/saves.ts";
// mods straight from their source folders, skipping the build: scripts are imported as typescript
@@ -45,9 +47,12 @@ export async function test_game(mods_dir: string, save?: WorldSave, seed = "test
// what host.download gives back, by url. other urls fail
const downloads = new Map<string, Uint8Array>();
const host = {
send: (conn: number, data: string) => {
// chunks come out decoded, as { type: "level_chunk", x, z, blocks, biomes }
send: (conn: number, data: string | Uint8Array) => {
if (!outbox.has(conn)) outbox.set(conn, []);
outbox.get(conn)!.push(JSON.parse(data));
outbox.get(conn)!.push(
typeof data === "string" ? JSON.parse(data) : { type: "level_chunk", ...decode_level_chunk(data) },
);
},
close(conn: number) {
closed.add(conn);
@@ -58,6 +63,7 @@ export async function test_game(mods_dir: string, save?: WorldSave, seed = "test
},
};
const game: GameServer = await start_game(host, save, seed, mod_sources(mods_dir));
game.chunk_send_budget_ms = Infinity;
// deno-lint-ignore no-explicit-any
const take = (conn: number): any[] => {
const messages = outbox.get(conn) ?? [];
@@ -75,6 +81,18 @@ export async function test_game(mods_dir: string, save?: WorldSave, seed = "test
return { game, take, send, join, closed, downloads };
}
// the block id and state bits at x, y, z in the chunks among messages, read the way a client does with the table
// join sent. undefined when no chunk there was sent
// deno-lint-ignore no-explicit-any
export function sent_block(messages: any[], join: { blocks: string[] }, x: number, y: number, z: number) {
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
const chunk = messages.findLast((m) => m.type === "level_chunk" && m.x === chunk_x && m.z === chunk_z);
if (!chunk) return undefined;
const value = chunk.blocks[y * CHUNK_AREA + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE)];
return [join.blocks[value & ID_MASK], value >>> 16];
}
export function copy_dir(from: string, to: string) {
if (Deno.statSync(from).isDirectory) {
Deno.mkdirSync(to, { recursive: true });