Scheduled ticks !!
This commit is contained in:
@@ -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<unknown[]>("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));
|
||||
});
|
||||
Reference in New Issue
Block a user