Workers for chunk generation !
This commit is contained in:
@@ -22,6 +22,8 @@ import {
|
||||
} from "$/common/protocol.ts";
|
||||
import type { ModListing, RecipeBook } from "$/common/mod_loader.ts";
|
||||
import type { WorldgenSetup } from "$/common/generation.ts";
|
||||
import type { WorldgenBundle } from "$/common/worldgen_bundle.ts";
|
||||
import type { WorldgenPool } from "./worldgen_pool.ts";
|
||||
import { MAX_VIEW_DISTANCE, MIN_VIEW_DISTANCE } from "$/common/chunk_streaming.ts";
|
||||
import { CYCLE_TICKS, format_clock, time_of_day, TIMES_OF_DAY } from "$/common/time.ts";
|
||||
import {
|
||||
@@ -67,7 +69,7 @@ import { update_crafting_result } from "./crafting.ts";
|
||||
import type { Menu } from "./menu.ts";
|
||||
import { SavedPlayer, ServerPlayer } from "./player.ts";
|
||||
import { encode_skin, MAX_SKIN_BYTES, skin_problem } from "./skins.ts";
|
||||
import { ServerWorld, Tile } from "./world.ts";
|
||||
import { FINGERPRINT_CHUNKS, ServerWorld, SPAWN_AREA_CHUNKS, Tile } from "./world.ts";
|
||||
import type { WorldSave } from "./saves.ts";
|
||||
import {
|
||||
BLOCK_DROP_PICKUP_DELAY,
|
||||
@@ -116,6 +118,9 @@ const READY_TIMEOUT_TICKS = 60 * TICKS_PER_SECOND;
|
||||
const SERVER_VIEW_DISTANCE = 16;
|
||||
// how long a tick can spend building chunks for players, the rest go out on the next ticks
|
||||
const CHUNK_SEND_BUDGET_MS = 20;
|
||||
// with a worldgen pool, how many of the chunks each player is waiting for it's asked for at once, nearest first. a
|
||||
// few ticks of sending ahead, without filling the world's raw chunk cache with ones that won't be sent for a while
|
||||
const WORLDGEN_REQUEST_WINDOW = 64;
|
||||
|
||||
// everything the game server needs from whatever runs it
|
||||
export interface GameHost {
|
||||
@@ -194,6 +199,12 @@ export class GameServer {
|
||||
view_distance = SERVER_VIEW_DISTANCE;
|
||||
// how long a tick can spend building chunks for players. tests lift it so what gets sent doesn't depend on speed
|
||||
chunk_send_budget_ms = CHUNK_SEND_BUDGET_MS;
|
||||
// what a worldgen worker needs to generate the same chunks as this game, see start_game
|
||||
worldgen_bundle?: WorldgenBundle;
|
||||
// generates chunks off the game's thread, see use_worldgen_pool. without it, they're generated when needed
|
||||
worldgen_pool?: WorldgenPool;
|
||||
// world.fallbacks each second for the last minute, for /tps
|
||||
#fallback_history: number[] = [];
|
||||
|
||||
constructor(host: GameHost, save: WorldSave | undefined, default_seed: string, mods: GameMods) {
|
||||
this.#host = host;
|
||||
@@ -411,7 +422,12 @@ export class GameServer {
|
||||
this.#sync_worn(player);
|
||||
this.#sync(player);
|
||||
}
|
||||
this.#request_chunks();
|
||||
this.#send_chunks();
|
||||
if (second) {
|
||||
this.#fallback_history.push(this.world.fallbacks);
|
||||
if (this.#fallback_history.length > 60) this.#fallback_history.shift();
|
||||
}
|
||||
}
|
||||
|
||||
// level.json and the chunks changed since the last save
|
||||
@@ -1491,6 +1507,14 @@ export class GameServer {
|
||||
`${tps} ticks per second (aiming for ${TICKS_PER_SECOND}), ${mspt.toFixed(1)} ms per tick, ` +
|
||||
`${skipped} skipped since starting`,
|
||||
);
|
||||
const pool = this.worldgen_pool;
|
||||
const last_minute = this.world.fallbacks - (this.#fallback_history[0] ?? 0);
|
||||
context.source.send_message(
|
||||
pool
|
||||
? `Worldgen: ${pool.workers} workers, ${pool.queued} chunks waiting, ${pool.generated} generated. ` +
|
||||
`${last_minute} chunks generated on the game thread in the last minute`
|
||||
: `Worldgen: on the game thread, ${last_minute} chunks in the last minute`,
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -1662,9 +1686,10 @@ export class GameServer {
|
||||
|
||||
#tick_vitals() {
|
||||
for (const player of this.#players.values()) {
|
||||
// burning, effects, falling out of the world, and being knocked back
|
||||
// burning, effects, falling out of the world, and being knocked back. like entities, not while the chunk
|
||||
// they're in is still being generated, see #tick_entities
|
||||
const knocked_back = player.knockback_ticks !== undefined;
|
||||
player.tick();
|
||||
if (this.#generated_at(player.x, player.z)) player.tick();
|
||||
if (knocked_back && player.alive) {
|
||||
// the last one hands what's left of the slide to their client
|
||||
this.#place(player, [player.vx, player.vy, player.vz]);
|
||||
@@ -1769,10 +1794,26 @@ export class GameServer {
|
||||
|
||||
// entities, see entity/
|
||||
|
||||
// like minecraft: entities only move near players, and players pick up items by walking into them
|
||||
// whether the chunks within a block of x, z (what an entity there touches) can be built without generating anything
|
||||
// here. always without a worldgen pool, which is what anything ticking there would otherwise wait for
|
||||
#generated_at(x: number, z: number) {
|
||||
if (!this.worldgen_pool) return true;
|
||||
for (let cx = Math.floor((x - 1) / CHUNK_SIZE); cx <= Math.floor((x + 1) / CHUNK_SIZE); cx++) {
|
||||
for (let cz = Math.floor((z - 1) / CHUNK_SIZE); cz <= Math.floor((z + 1) / CHUNK_SIZE); cz++) {
|
||||
if (!this.world.can_build(cx, cz)) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// like minecraft: entities only move near players, and players pick up items by walking into them. with a worldgen
|
||||
// pool, only where the chunk is generated too, like minecraft's entities in chunks that aren't loaded: one that
|
||||
// just got somewhere new, like a teleported player, waits for the pool instead of the game generating it here
|
||||
#tick_entities() {
|
||||
if (this.do_mob_spawning) this.spawner.tick(this, SIMULATION_DISTANCE);
|
||||
this.entities.tick((entity) => this.#near_any_player(entity.x, entity.z));
|
||||
this.entities.tick((entity) =>
|
||||
this.#near_any_player(entity.x, entity.z) && this.#generated_at(entity.x, entity.z)
|
||||
);
|
||||
const items = this.item_entities();
|
||||
for (const player of this.#players.values()) {
|
||||
for (const entity of items) {
|
||||
@@ -1916,6 +1957,47 @@ export class GameServer {
|
||||
}
|
||||
}
|
||||
|
||||
// generates chunks in a pool's workers from now on: what players need goes to it, and only chunks it has made the
|
||||
// parts of are sent (see #request_chunks). undefined goes back to generating on this thread
|
||||
use_worldgen_pool(pool: WorldgenPool | undefined) {
|
||||
this.worldgen_pool = pool;
|
||||
if (pool) pool.on_chunk = (x, z, raw) => this.world.add_raw(x, z, raw);
|
||||
}
|
||||
|
||||
// generates what's needed before players come with the pool, instead of on this thread when they do: the spawn
|
||||
// search's chunks and the worldgen fingerprint's
|
||||
async warm_up() {
|
||||
const pool = this.worldgen_pool;
|
||||
if (!pool) return;
|
||||
const reach = SPAWN_AREA_CHUNKS;
|
||||
const around_spawn: [number, number][] = [];
|
||||
for (let x = -reach; x <= reach; x++) for (let z = -reach; z <= reach; z++) around_spawn.push([x, z]);
|
||||
await pool.prefetch([...around_spawn, ...FINGERPRINT_CHUNKS as [number, number][]]);
|
||||
this.world.spawn_point();
|
||||
this.world.worldgen_fingerprint();
|
||||
}
|
||||
|
||||
// asks the pool for the raw chunks the nearest chunks each player is waiting for are built from, nearest first.
|
||||
// what nobody is waiting for any more is dropped from its queue
|
||||
#request_chunks() {
|
||||
const pool = this.worldgen_pool;
|
||||
if (!pool) return;
|
||||
const wanted = new Map<number, { x: number; z: number; priority: number }>();
|
||||
for (const player of this.#players.values()) {
|
||||
const center = player.chunks.center;
|
||||
if (!center) continue;
|
||||
for (const [cx, cz] of player.chunks.nearest_pending(WORLDGEN_REQUEST_WINDOW)) {
|
||||
const priority = (cx - center.x) ** 2 + (cz - center.z) ** 2;
|
||||
for (const [x, z] of this.world.raws_needed(cx, cz)) {
|
||||
const key = chunk_key(x, z);
|
||||
const known = wanted.get(key);
|
||||
if (!known || priority < known.priority) wanted.set(key, { x, z, priority });
|
||||
}
|
||||
}
|
||||
}
|
||||
pool.want(wanted);
|
||||
}
|
||||
|
||||
// every player's next batch, like minecraft's PlayerChunkSender. building chunks is the slow part, so they share
|
||||
// a time budget and whoever didn't get theirs goes first next tick
|
||||
#send_chunks() {
|
||||
@@ -1925,6 +2007,11 @@ export class GameServer {
|
||||
}
|
||||
const start = performance.now();
|
||||
const can_send = () => performance.now() - start < this.chunk_send_budget_ms;
|
||||
// with a pool, only chunks it has made the parts of, the rest wait for it instead of being generated here.
|
||||
// without one, each is built as it's picked, so the time it takes counts against the budget for the next
|
||||
const ready = this.worldgen_pool
|
||||
? (x: number, z: number) => this.world.can_build(x, z)
|
||||
: (x: number, z: number) => (this.world.build(x, z), true);
|
||||
const first = this.#next_chunk_player % players.length;
|
||||
for (let i = 0; i < players.length; i++) {
|
||||
const player = players[(first + i) % players.length];
|
||||
@@ -1932,7 +2019,7 @@ export class GameServer {
|
||||
this.#next_chunk_player = first + i;
|
||||
return;
|
||||
}
|
||||
const batch = player.chunks.next_batch(can_send);
|
||||
const batch = player.chunks.next_batch(can_send, ready);
|
||||
if (batch.length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user