Files
bworld/server/game/scheduled_ticks.ts
2026-11-06 22:49:43 -03:00

145 lines
5.4 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// ticks blocks asked for some ticks ahead, see ServerWorld.schedule_tick. kept by chunk and grouped by when they're due,
// so a tick costs nothing until it's due, and a chunk with none due is passed over with one comparison
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
import { chunk_key } from "@/common/utils.ts";
import { HIGHEST_Y, LOWEST_Y } from "@/common/world_shape.ts";
// the most that run in one tick. the rest run first the next
export const MAX_SCHEDULED_TICKS = 65536;
// priorities go from -8 to 7, so with 8 added one fits in 4 bits under the due tick
export const MIN_PRIORITY = -8;
export const MAX_PRIORITY = 7;
export interface ScheduledTick {
readonly x: number;
readonly y: number;
readonly z: number;
// the numeric id of the block it was scheduled for. it's dropped if another kind of block is there when it's due
readonly nid: number;
readonly due: number;
readonly priority: number;
// due * 16 + priority + 8: smaller runs first
readonly key: number;
// counts up with every tick scheduled, so of ticks with the same key the first scheduled runs first
readonly order: number;
// the block's numeric id and place in its chunk, which is one per block, see block_key and ChunkTicks.waiting
readonly block: number;
}
class ChunkTicks {
// by key, each in the order they were scheduled
groups = new Map<number, ScheduledTick[]>();
// the keys of groups, smallest first. only a few, since delays are short
keys: number[] = [];
// ScheduledTick.block of every tick waiting here, so the same block can't wait twice
waiting = new Set<number>();
constructor(readonly x: number, readonly z: number) {}
add(tick: ScheduledTick) {
this.waiting.add(tick.block);
const group = this.groups.get(tick.key);
if (group) {
group.push(tick);
return;
}
this.groups.set(tick.key, [tick]);
// usually later than every other
let i = this.keys.length;
while (i > 0 && this.keys[i - 1] > tick.key) i--;
this.keys.splice(i, 0, tick.key);
}
// takes out every tick that runs before or with the tick with this key and order
remove_through(key: number, order: number) {
while (this.keys.length > 0 && this.keys[0] <= key) {
const group = this.groups.get(this.keys[0])!;
let taken = group.length;
if (this.keys[0] === key) {
taken = 0;
while (taken < group.length && group[taken].order <= order) taken++;
}
for (let i = 0; i < taken; i++) this.waiting.delete(group[i].block);
if (taken < group.length) {
group.splice(0, taken);
return;
}
this.groups.delete(this.keys[0]);
this.keys.shift();
}
}
}
export class ScheduledTicks {
#chunks = new Map<number, ChunkTicks>();
#order = 0;
// adds a tick for the block nid at x y z on tick due, unless one already waits for it there
add(x: number, y: number, z: number, nid: number, due: number, priority: number) {
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
const key = chunk_key(chunk_x, chunk_z);
let chunk = this.#chunks.get(key);
const block = block_key(x, y, z, chunk_x, chunk_z, nid);
if (chunk?.waiting.has(block)) return;
if (!chunk) this.#chunks.set(key, chunk = new ChunkTicks(chunk_x, chunk_z));
const order = this.#order++;
chunk.add({ x, y, z, nid, due, priority, key: due * 16 + priority + 8, order, block });
}
has(x: number, y: number, z: number, nid: number) {
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
const chunk = this.#chunks.get(chunk_key(chunk_x, chunk_z));
return chunk?.waiting.has(block_key(x, y, z, chunk_x, chunk_z, nid)) ?? false;
}
// takes the ticks due by now in the chunks near says are near players, at most max, in the order they run: by
// due tick, then priority, then the order they were scheduled in. what's left over stays, and runs first next time
take_due(now: number, near: (chunk_x: number, chunk_z: number) => boolean, max = MAX_SCHEDULED_TICKS) {
const due_before = (now + 1) * 16;
const chunks: ChunkTicks[] = [];
const due: ScheduledTick[] = [];
for (const chunk of this.#chunks.values()) {
if (chunk.keys[0] >= due_before || !near(chunk.x, chunk.z)) continue;
chunks.push(chunk);
for (const key of chunk.keys) {
if (key >= due_before) break;
for (const tick of chunk.groups.get(key)!) due.push(tick);
}
}
if (due.length === 0) return due;
due.sort(run_order);
if (due.length > max) due.length = max;
const last = due[due.length - 1];
for (const chunk of chunks) {
chunk.remove_through(last.key, last.order);
if (chunk.waiting.size === 0) this.#chunks.delete(chunk_key(chunk.x, chunk.z));
}
return due;
}
// every tick waiting, in the order they'd run, for saving
all(): ScheduledTick[] {
const all: ScheduledTick[] = [];
for (const chunk of this.#chunks.values()) {
for (const group of chunk.groups.values()) all.push(...group);
}
return all.sort(run_order);
}
}
function run_order(a: ScheduledTick, b: ScheduledTick) {
return a.key - b.key || a.order - b.order;
}
// a block's place in its chunk with its numeric id above, the same whatever the dimension's height. under 2^40
function block_key(x: number, y: number, z: number, chunk_x: number, chunk_z: number, nid: number) {
const column = (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE);
return (nid * (HIGHEST_Y - LOWEST_Y) + y - LOWEST_Y) * CHUNK_AREA + column;
}