Scheduled ticks !!

This commit is contained in:
2026-10-20 13:50:35 -03:00
parent 77cd327f94
commit 1c2dcd0dbc
7 changed files with 444 additions and 1 deletions
+58
View File
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {
AIR,
BREAK_STAGES,
CHUNK_HEIGHT,
CHUNK_SIZE,
@@ -68,6 +69,7 @@ import type { Menu } from "./menu.ts";
import { MACHINE_CONN, SavedPlayer, ServerPlayer } from "./player.ts";
import { encode_skin, MAX_SKIN_BYTES, skin_problem } from "./skins.ts";
import { ServerWorld, SPAWN_AREA_CHUNKS, Tile } from "./world.ts";
import { ScheduledTicks } from "./scheduled_ticks.ts";
import type { WorldSave, WorldSource } from "./saves.ts";
import {
BLOCK_DROP_PICKUP_DELAY,
@@ -137,6 +139,9 @@ export interface SavedLevel {
// world time in ticks
time: number;
tiles: Tile[];
// the ticks blocks scheduled that haven't run, in the order they'll run, see ServerWorld.schedule_tick. ticks_left
// is 0 or less for ones waiting for a player to come near
scheduled_ticks: [x: number, y: number, z: number, id: string, ticks_left: number, priority: number][];
// every ctx.containers container, by id
containers: Record<string, (ItemData | null)[]>;
players: Record<string, SavedPlayer>;
@@ -164,6 +169,8 @@ export class GameServer {
#next_chunk_player = 0;
#saved_players: Record<string, SavedPlayer> = {};
#tick = 0;
// see ServerWorld.schedule_tick, due on game ticks
#scheduled = new ScheduledTicks();
// the last morning new day handlers ran for
#day = 0;
#mods: GameMods;
@@ -260,6 +267,11 @@ export class GameServer {
this.containers.set(id, container);
}
for (const tile of saved?.tiles ?? []) this.world.add_tile({ ...tile });
for (const [x, y, z, id, ticks_left, priority] of saved?.scheduled_ticks ?? []) {
// unless its mod is gone
const nid = this.world.block_ids[id];
if (nid !== undefined) this.#scheduled.add(x, y, z, nid, ticks_left, priority);
}
this.entities.load(saved?.version === 4 ? upgrade_v4_entities(saved.entities) : saved?.entities ?? []);
this.#saved_players = saved?.players ?? {};
// a new world is saved straight away
@@ -344,6 +356,8 @@ export class GameServer {
this.#broadcast({ type: "time", time: this.world.time });
}
this.#run_scheduled_ticks();
// the tiles near players that tick, picked before any runs so one that a handler adds waits for the next tick
const ticking: Tile[] = [];
if (this.world.has_ticking_tiles) {
@@ -399,6 +413,42 @@ export class GameServer {
}
}
// runs the block's components' on_scheduled_tick in delay ticks, see ServerWorld.schedule_tick
schedule_tick(x: number, y: number, z: number, delay: number, priority: number) {
const nid = this.world.get_block_nid(x, y, z);
if (nid === AIR) return;
this.#scheduled.add(x, y, z, nid, this.#tick + delay, priority);
this.world.dirty = true;
}
has_scheduled_tick(x: number, y: number, z: number) {
return this.#scheduled.has(x, y, z, this.world.get_block_nid(x, y, z));
}
// the scheduled ticks due near players, all taken before any runs, so one a handler schedules waits for its tick
#run_scheduled_ticks() {
const due = this.#scheduled.take_due(
this.#tick,
(chunk_x, chunk_z) => this.#near_any_player(chunk_x * CHUNK_SIZE, chunk_z * CHUNK_SIZE),
);
for (const { x, y, z, nid } of due) {
// another kind of block took its place
if (this.world.get_block_nid(x, y, z) !== nid) continue;
const id = this.world.get_block_id(x, y, z);
const components = this.mods.components_with(id, "on_scheduled_tick");
if (components.length === 0) continue;
const block = this.mods.block_ref(x, y, z, id);
for (const { mod, id: component_id, component, params } of components) {
run_guarded(
mod,
`${component_id} on_scheduled_tick`,
() => component.on_scheduled_tick!(block, params),
);
}
}
if (due.length > 0) this.world.dirty = true;
}
// level.json and the chunks generated or changed since the last save
save(): Required<WorldSave> {
for (const player of this.#players.values()) {
@@ -409,6 +459,14 @@ export class GameServer {
seed: this.world.seed,
time: this.world.time,
tiles: [...this.world.tiles.values()].map((tile) => ({ ...tile })),
scheduled_ticks: this.#scheduled.all().map((tick) => [
tick.x,
tick.y,
tick.z,
EverythingRegistry.get_by_id<BlockRegistry>("blocks", tick.nid)!.id,
tick.due - this.#tick,
tick.priority,
]),
containers: Object.fromEntries(
[...this.containers].map(([id, container]) => [id, container.to_data()]),
),