Workers for chunk generation !

This commit is contained in:
2026-10-11 01:15:14 -03:00
parent 3d2b235f64
commit cd3b39c3d7
24 changed files with 1119 additions and 91 deletions
+15 -2
View File
@@ -74,9 +74,21 @@ export class PlayerChunks {
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
next_batch(can_send: () => boolean = () => true): [number, number][] {
// 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 [];
}
@@ -95,6 +107,7 @@ export class PlayerChunks {
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]);