Files
bworld/server/game/player_chunks.ts
T
2026-10-11 01:15:14 -03:00

134 lines
5.0 KiB
TypeScript

import { CHUNK_SIZE } from "$/common/constants.ts";
import { is_within_view } from "$/common/chunk_streaming.ts";
import { chunk_key } from "$/common/utils.ts";
// minecraft's PlayerChunkSender numbers
const START_CHUNKS_PER_TICK = 9;
const MIN_CHUNKS_PER_TICK = 0.01;
const MAX_CHUNKS_PER_TICK = 64;
const MAX_UNACKNOWLEDGED_BATCHES = 10;
// which chunks a player's client has and which it still needs, like minecraft's ChunkTrackingView and
// PlayerChunkSender: chunks go out nearest first, in batches as big as the client says it can take
export class PlayerChunks {
view_distance: number;
// the chunk the player is in, undefined until the first update
center: { x: number; z: number } | undefined;
// chunks the client was sent and hasn't been told to forget, by chunk_key
#sent = new Map<number, [number, number]>();
// in view and not sent yet
#pending = new Map<number, [number, number]>();
#chunks_per_tick = START_CHUNKS_PER_TICK;
#batch_quota = 0;
#unacknowledged_batches = 0;
// one batch until the client first answers, so a client that can't keep up isn't flooded
#max_unacknowledged_batches = 1;
constructor(view_distance: number) {
this.view_distance = view_distance;
}
has(chunk_x: number, chunk_z: number) {
return this.#sent.has(chunk_key(chunk_x, chunk_z));
}
// has the chunk the block at x, z is in
has_block(x: number, z: number) {
return this.has(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
}
get pending_count() {
return this.#pending.size;
}
// follows the player to the chunk at x, z (in blocks). returns whether that's a new center, and the chunks the
// client should forget
move_to(x: number, z: number, force = false): { moved: boolean; forget: [number, number][] } {
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
if (!force && this.center?.x === chunk_x && this.center.z === chunk_z) {
return { moved: false, forget: [] };
}
this.center = { x: chunk_x, z: chunk_z };
const in_view = (cx: number, cz: number) => is_within_view(chunk_x, chunk_z, this.view_distance, cx, cz);
const forget: [number, number][] = [];
for (const [key, [cx, cz]] of this.#sent) {
if (!in_view(cx, cz)) {
this.#sent.delete(key);
forget.push([cx, cz]);
}
}
for (const [key, [cx, cz]] of this.#pending) {
if (!in_view(cx, cz)) this.#pending.delete(key);
}
// the view plus its outer ring fits in this square
const reach = this.view_distance + 1;
for (let cx = chunk_x - reach; cx <= chunk_x + reach; cx++) {
for (let cz = chunk_z - reach; cz <= chunk_z + reach; cz++) {
const key = chunk_key(cx, cz);
if (in_view(cx, cz) && !this.#sent.has(key)) this.#pending.set(key, [cx, cz]);
}
}
return { moved: true, forget };
}
// the count chunks in view the client doesn't have yet that are nearest the player
nearest_pending(count: number): [number, number][] {
if (!this.center) return [];
const { x, z } = this.center;
return [...this.#pending.values()]
.map(([cx, cz]) => ({ cx, cz, distance: (cx - x) ** 2 + (cz - z) ** 2 }))
.sort((a, b) => a.distance - b.distance)
.slice(0, count)
.map(({ cx, cz }): [number, number] => [cx, cz]);
}
// the chunks to send this tick, nearest the player first, and counts them as sent. empty when the client is
// still busy with earlier batches. can_send says whether there's time left to build one more, ready whether a
// chunk can be built yet (with a worldgen pool, whether what it's built from has been generated)
next_batch(can_send: () => boolean = () => true, ready?: (x: number, z: number) => boolean): [number, number][] {
if (this.#unacknowledged_batches >= this.#max_unacknowledged_batches || !this.center) {
return [];
}
this.#batch_quota = Math.min(
this.#batch_quota + this.#chunks_per_tick,
Math.max(1, this.#chunks_per_tick),
);
if (this.#batch_quota < 1 || this.#pending.size === 0) {
return [];
}
const { x, z } = this.center;
const nearest = [...this.#pending.entries()]
.map(([key, [cx, cz]]) => ({ key, cx, cz, distance: (cx - x) ** 2 + (cz - z) ** 2 }))
.sort((a, b) => a.distance - b.distance);
const batch: [number, number][] = [];
for (const { key, cx, cz } of nearest) {
if (batch.length >= Math.floor(this.#batch_quota) || !can_send()) break;
if (ready && !ready(cx, cz)) continue;
this.#pending.delete(key);
this.#sent.set(key, [cx, cz]);
batch.push([cx, cz]);
}
if (batch.length > 0) {
this.#unacknowledged_batches += 1;
this.#batch_quota -= batch.length;
}
return batch;
}
// the client loaded a batch and says how many chunks a tick it can take
on_batch_received(chunks_per_tick: number) {
this.#unacknowledged_batches = Math.max(0, this.#unacknowledged_batches - 1);
this.#chunks_per_tick = Number.isNaN(chunks_per_tick)
? MIN_CHUNKS_PER_TICK
: Math.min(Math.max(chunks_per_tick, MIN_CHUNKS_PER_TICK), MAX_CHUNKS_PER_TICK);
if (this.#unacknowledged_batches === 0) {
this.#batch_quota = 1;
}
this.#max_unacknowledged_batches = MAX_UNACKNOWLEDGED_BATCHES;
}
}