diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index d0ea58c..f1827ed 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -78,6 +78,8 @@ export interface BlockComponent

{ // its own states changed without it being replaced, by set_state or a rule, like a clutch another mod powers. // before is what they were. it runs right away, before the blocks beside it hear of the change on_state_change?(block: BlockRef, params: P, before: Readonly>): void; + // the tick ServerWorld.schedule_tick asked for came, like neoforge's tick from scheduleTick + on_scheduled_tick?(block: BlockRef, params: P): void; // placing and neighbor rules are in shared scripts, see BlockRules in bworld/shared } @@ -255,6 +257,12 @@ export interface ServerWorld { // like neoforge's neighborChanged. for a block whose effect on others changed without it changing, or for blocks // it reaches that aren't beside it. it hears once the change going on is done, like the blocks beside one do neighbor_changed(x: number, y: number, z: number, from: Position): void; + // runs the block's components' on_scheduled_tick in delay ticks (at least 1), if the same kind of block is still + // there, like neoforge's scheduleTick. one per block: scheduling while one waits does nothing. of those due on the + // same tick, lower priority (-8 to 7, 0 when not given) runs first, then the order they were scheduled in. saved + // with the world. they only run within 6 chunks of a player, the rest wait until one comes near + schedule_tick(x: number, y: number, z: number, delay: number, priority?: number): void; + has_scheduled_tick(x: number, y: number, z: number): boolean; // whether the block's side is a full square things can attach to, like minecraft's isFaceSturdy: its collision // shape covers the whole face. false for air, plants, water and the sides of a slab is_face_sturdy(x: number, y: number, z: number, face: Face): boolean; diff --git a/docs/modding/blocks/components.md b/docs/modding/blocks/components.md index 1369b6e..e5bb8c2 100644 --- a/docs/modding/blocks/components.md +++ b/docs/modding/blocks/components.md @@ -70,6 +70,7 @@ component. | `on_new_day(block, params, day)` | Every morning at 06:00. See [Ticking](#ticking) for where. | | | `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. See [Changes](#changes). | | | `on_state_change(block, params, before)` | Its own states change without it being replaced, by `set_state` or a rule. See [Changes](#changes). | | +| `on_scheduled_tick(block, params)` | A tick it asked for comes. See [Scheduled Ticks](#scheduled-ticks). | | `block` is `{ id, x, y, z, biome, season, data, get_client_data, set_client_data, set_ticking }`, where `data` is the block's [tile data](tiledata.md) and the client data functions its [part clients see](tiledata.md#client-data), `biome` @@ -235,6 +236,35 @@ of a player, like the blocks Minecraft random ticks. Blocks one of them places t block taller, wait for the next one. When time jumps forward past several mornings, like with `/time add`, each of them runs, up to the last 64. +### Scheduled Ticks + +A block that needs to do something a few ticks from now, like a button popping back out or a signal passing on after a +delay, asks for a tick instead of ticking all the time, like NeoForge's `scheduleTick`. It costs nothing while it waits, +and the block needs no tile data: + +```ts +ctx.components.register_block("copper_tools:button", { + on_interact(block) { + ctx.world.set_state(block.x, block.y, block.z, "pressed", 1); + ctx.world.schedule_tick(block.x, block.y, block.z, 20); + return true; + }, + on_scheduled_tick(block) { + ctx.world.set_state(block.x, block.y, block.z, "pressed", 0); + }, +}); +``` + +`ctx.world.schedule_tick(x, y, z, delay, priority?)` runs the block's `on_scheduled_tick` in `delay` ticks, at least 1. +A block has one at a time: scheduling while one waits does nothing, and `ctx.world.has_scheduled_tick(x, y, z)` says +whether one does. Its own `on_scheduled_tick` can schedule the next one, like a clock. Of the ticks due on the same +tick, the lower `priority` runs first, from -8 to 7 and 0 when not given, then the one scheduled first. If another kind +of block has taken its place by then, the tick is dropped. + +Scheduled ticks run within 6 chunks of a player, like `on_tick`. Farther away they wait, and run as soon as a player +comes near. They're saved with the world, with how many ticks each had left, so they pick up where they were. At most +65,536 run in one tick, and the rest run first in the next. + The server runs a fixed 20 ticks a second. When a tick takes too long, up to ten of the next ones run back to back to catch up. If it's further behind than that, it skips the rest and logs a warning. `/tps` shows how it's keeping up. diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 075af6f..d97370f 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -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; players: Record; @@ -164,6 +169,8 @@ export class GameServer { #next_chunk_player = 0; #saved_players: Record = {}; #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 { 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("blocks", tick.nid)!.id, + tick.due - this.#tick, + tick.priority, + ]), containers: Object.fromEntries( [...this.containers].map(([id, container]) => [id, container.to_data()]), ), diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index 51e3acc..7f78c01 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -58,6 +58,7 @@ import { } from "$/common/commands.ts"; import { AIR_ID } from "$/common/protocol.ts"; import type { GameServer } from "./game_server.ts"; +import { MAX_PRIORITY, MIN_PRIORITY } from "./scheduled_ticks.ts"; import type { ServerPlayer } from "./player.ts"; import { Menu, type MenuSlot } from "./menu.ts"; import { damage_source, type Entity } from "./entity/entity.ts"; @@ -746,6 +747,16 @@ export class ModRuntime { }, neighbor_changed: (x, y, z, from) => this.rules.neighbor_changed(x, y, z, { x: from.x, y: from.y, z: from.z }), + schedule_tick: (x, y, z, delay, priority = 0) => { + if (!Number.isInteger(delay) || delay < 1) { + throw new Error(`a tick is scheduled at least 1 tick ahead, in whole ticks, not ${delay}`); + } + if (!Number.isInteger(priority) || priority < MIN_PRIORITY || priority > MAX_PRIORITY) { + throw new Error(`a tick's priority is ${MIN_PRIORITY} to ${MAX_PRIORITY}, not ${priority}`); + } + game().schedule_tick(x, y, z, delay, priority); + }, + has_scheduled_tick: (x, y, z) => game().has_scheduled_tick(x, y, z), is_face_sturdy: (x, y, z, face) => covers_face(game().world.collision_shape(x, y, z), face), get_block_data: (x, y, z) => game().world.get_tile(x, y, z)?.mod_data as never, get_block_ref: (x, y, z) => { diff --git a/server/game/scheduled_ticks.ts b/server/game/scheduled_ticks.ts new file mode 100644 index 0000000..84ad010 --- /dev/null +++ b/server/game/scheduled_ticks.ts @@ -0,0 +1,143 @@ +// 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_SIZE } from "$/common/constants.ts"; +import { chunk_key } from "$/common/utils.ts"; +import { index_in_chunk } from "./world.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; + // nid * 65536 + index in chunk, which is one per block, see ChunkTicks.waiting + readonly block: number; +} + +class ChunkTicks { + // by key, each in the order they were scheduled + groups = new Map(); + // 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(); + + 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(); + #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, under 65536 since chunks are 256 tall, with its numeric id above +function block_key(x: number, y: number, z: number, chunk_x: number, chunk_z: number, nid: number) { + return nid * 65536 + index_in_chunk(x, y, z, chunk_x, chunk_z); +} diff --git a/server/game/world.ts b/server/game/world.ts index 663b095..74afbc6 100644 --- a/server/game/world.ts +++ b/server/game/world.ts @@ -733,7 +733,7 @@ function fallback_caller(stack: string) { return match ? `${match[1] ?? "?"} ${match[2]}` : outside; } -function index_in_chunk(x: number, y: number, z: number, chunk_x: number, chunk_z: number) { +export function index_in_chunk(x: number, y: number, z: number, chunk_x: number, chunk_z: number) { return y * CHUNK_AREA + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE); } diff --git a/tests/scheduled_ticks_test.ts b/tests/scheduled_ticks_test.ts new file mode 100644 index 0000000..3ab2b45 --- /dev/null +++ b/tests/scheduled_ticks_test.ts @@ -0,0 +1,193 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// ticks blocks schedule ahead with ctx.world.schedule_tick: when and in what order they run, and that they last +import { assert, assertEquals } from "@std/assert"; +import { ScheduledTicks } from "$/server/game/scheduled_ticks.ts"; +import type { WorldSave } from "$/server/game/saves.ts"; +import { copy_dir, stone_floor, test_game } from "./helpers.ts"; + +const SERVER_SCRIPT = ` +import type { ServerContext } from "bworld/server"; +export function setup(ctx: ServerContext) { + const log = (event: unknown[]) => ctx.storage.set("log", [...ctx.storage.get("log") ?? [], event]); + ctx.components.register_block("testmod:clock", { + on_scheduled_tick(block) { log([block.x, ctx.system.current_tick]); }, + }); + // ticks 3 times, 2 ticks apart, scheduling each next one from the last + ctx.components.register_block("testmod:pulser", { + on_scheduled_tick(block) { + const { x, y, z } = block; + const count = ctx.world.get_state(x, y, z, "count")! + 1; + ctx.world.set_state(x, y, z, "count", count); + log([x, ctx.system.current_tick]); + if (count < 3) ctx.world.schedule_tick(x, y, z, 2); + }, + }); + ctx.commands.register("schedule", { + description: "", usage: "", + run(args) { + const [x, y, z, delay, priority] = args.map(Number); + try { + ctx.world.schedule_tick(x, y, z, delay, args[4] === undefined ? undefined : priority); + } catch (e) { + log(["error", (e as Error).message]); + } + }, + }); + ctx.commands.register("scheduled", { + description: "", usage: "", + run([x, y, z]) { log(["scheduled", ctx.world.has_scheduled_tick(Number(x), Number(y), Number(z))]); }, + }); +} +`; + +function test_mods() { + const dir = Deno.makeTempDirSync({ prefix: "bworld_scheduled_" }); + copy_dir("mods/bworld", `${dir}/bworld`); + const mod = `${dir}/testmod`; + for (const folder of ["blocks", "scripts"]) Deno.mkdirSync(`${mod}/${folder}`, { recursive: true }); + const write = (path: string, json: unknown) => Deno.writeTextFileSync(`${mod}/${path}`, JSON.stringify(json)); + write("manifest.json", { + format_version: 1, + id: "testmod", + name: "Test", + version: "1.0.0", + dependencies: [{ id: "bworld", version: "*" }], + scripts: { server: "scripts/server.ts" }, + }); + const block = (name: string, states: unknown[] = []) => + write(`blocks/${name}.json`, { + format_version: 1, + block: { + id: `testmod:${name}`, + textures: "bworld:stone", + mining: { toughness: 1 }, + states, + components: { [`testmod:${name}`]: {} }, + }, + }); + block("clock"); + block("pulser", [{ name: "count", bits: 2, default: 0 }]); + Deno.writeTextFileSync(`${mod}/scripts/server.ts`, SERVER_SCRIPT); + return dir; +} + +async function setup(dir = test_mods(), save?: WorldSave) { + const { game, send, join } = await test_game(dir, save); + join(1, "alice"); + stone_floor(game); + send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: 0, pitch: 0 }); + // what happened since it was last called + const log = () => { + const events = (game.mods.storage.testmod?.log ?? []) as unknown[]; + delete game.mods.storage.testmod?.log; + return events; + }; + const command = (text: string) => send(1, { type: "chat", text }); + const ticks = (n: number) => { + for (let i = 0; i < n; i++) game.tick(); + }; + const cleanup = () => Deno.removeSync(dir, { recursive: true }); + return { game, dir, log, command, ticks, cleanup }; +} + +Deno.test("scheduled ticks run when they're due, by priority, then in the order they were scheduled", async () => { + const { game, log, command, ticks, cleanup } = await setup(); + for (let x = 0; x < 5; x++) game.set_block(x, 100, 0, "testmod:clock"); + const start = game.current_tick; + command("/schedule 0 100 0 3"); + command("/schedule 1 100 0 2"); + command("/schedule 2 100 0 2 -1"); + command("/schedule 3 100 0 2"); + command("/schedule 4 100 0 1"); + ticks(4); + assertEquals(log(), [[4, start + 1], [2, start + 2], [1, start + 2], [3, start + 2], [0, start + 3]]); + cleanup(); +}); + +Deno.test("a block waits for one tick at a time, and can schedule its next one from it", async () => { + const { game, log, command, ticks, cleanup } = await setup(); + game.set_block(0, 100, 0, "testmod:clock"); + const start = game.current_tick; + command("/scheduled 0 100 0"); + command("/schedule 0 100 0 5"); + command("/schedule 0 100 0 1"); + command("/scheduled 0 100 0"); + ticks(5); + assertEquals(log(), [["scheduled", false], ["scheduled", true], [0, start + 5]], "the second did nothing"); + + game.set_block(1, 100, 0, "testmod:pulser"); + command("/schedule 1 100 0 2"); + ticks(10); + assertEquals(log(), [[1, start + 7], [1, start + 9], [1, start + 11]]); + command("/scheduled 1 100 0"); + assertEquals(log(), [["scheduled", false]]); + + command("/schedule 0 100 0 0"); + command("/schedule 0 100 0 1 8"); + assertEquals(log().map((event) => (event as string[])[0]), ["error", "error"]); + cleanup(); +}); + +Deno.test("a tick is dropped when another kind of block takes its place", async () => { + const { game, log, command, ticks, cleanup } = await setup(); + game.set_block(0, 100, 0, "testmod:clock"); + command("/schedule 0 100 0 2"); + game.set_block(0, 100, 0, "bworld:stone"); + command("/scheduled 0 100 0"); + ticks(2); + game.set_block(0, 100, 0, "testmod:clock"); + ticks(3); + assertEquals(log(), [["scheduled", false]], "the stone hasn't one, and it was gone by the clock put back after"); + cleanup(); +}); + +Deno.test("ticks far from every player wait until one comes near", async () => { + const { game, log, command, ticks, cleanup } = await setup(); + game.set_block(16 * 20, 100, 0, "testmod:clock"); + command(`/schedule ${16 * 20} 100 0 1`); + ticks(5); + assertEquals(log(), []); + game.teleport(game.players()[0], 16 * 20 + 0.5, 101, 0.5); + ticks(1); + assertEquals(log(), [[16 * 20, game.current_tick]]); + cleanup(); +}); + +Deno.test("ticks waiting when the world is saved run where they left off after it's loaded", async () => { + const { game, dir, command, ticks } = await setup(); + game.set_block(0, 100, 0, "testmod:clock"); + game.set_block(1, 100, 0, "testmod:clock"); + game.set_block(16 * 20, 100, 0, "testmod:clock"); + command("/schedule 0 100 0 10"); + command("/schedule 1 100 0 10 -1"); + command(`/schedule ${16 * 20} 100 0 1`); + ticks(4); + const saved = game.save(); + + const again = await setup(dir, saved); + const start = again.game.current_tick; + again.ticks(5); + assertEquals(again.log(), []); + again.ticks(1); + assertEquals(again.log(), [[1, start + 6], [0, start + 6]]); + // the far one had been due for 3 ticks, and still waits + again.game.teleport(again.game.players()[0], 16 * 20 + 0.5, 101, 0.5); + again.ticks(1); + assertEquals(again.log(), [[16 * 20, start + 7]]); + again.cleanup(); +}); + +Deno.test("past the most that run in a tick, the rest run first the next", () => { + const ticks = new ScheduledTicks(); + const near = () => true; + for (let x = 0; x < 5; x++) ticks.add(x, 100, 0, 1, 10, 0); + ticks.add(0, 100, 20, 1, 11, -8); + const taken = (now: number) => ticks.take_due(now, near, 3).map((tick) => `${tick.x} ${tick.z}`); + assertEquals(taken(9), []); + assertEquals(taken(10), ["0 0", "1 0", "2 0"]); + assertEquals(taken(11), ["3 0", "4 0", "0 20"]); + assertEquals(taken(12), []); + assert(!ticks.has(0, 100, 0, 1)); +});