168 lines
7.4 KiB
TypeScript
168 lines
7.4 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// what machines do to the world through server scripts: read which way water flows, find items lying around and
|
|
// change them, push things, break blocks over time, and use items on blocks like a player would
|
|
import { assert, assertEquals, assertThrows } from "@std/assert";
|
|
import type { ServerContext } from "bworld/server";
|
|
import { AIR_ID } from "$/common/protocol.ts";
|
|
import { copy_dir, stone_floor, test_game } from "./helpers.ts";
|
|
|
|
// hands the test its context, and has a block that says who right clicked it and an item that gives a stick when used
|
|
const SERVER_SCRIPT = `
|
|
import type { ServerContext } from "bworld/server";
|
|
export function setup(ctx: ServerContext) {
|
|
(globalThis as any).machines_ctx = ctx;
|
|
ctx.components.register_block("testmod:button", {
|
|
on_interact(block, _params, player) {
|
|
block.data = { pressed_by: player.name };
|
|
return true;
|
|
},
|
|
});
|
|
ctx.components.register_item("testmod:wand", {
|
|
on_use(_item, _params, player, target) {
|
|
if (target) player.give_item("bworld:stick", 1);
|
|
},
|
|
});
|
|
}
|
|
`;
|
|
|
|
async function setup() {
|
|
const dir = Deno.makeTempDirSync({ prefix: "bworld_machines_" });
|
|
copy_dir("mods/bworld", `${dir}/bworld`);
|
|
const mod = `${dir}/testmod`;
|
|
for (const folder of ["blocks", "items", "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" },
|
|
});
|
|
write("blocks/button.json", {
|
|
format_version: 1,
|
|
block: {
|
|
id: "testmod:button",
|
|
textures: "bworld:stone",
|
|
mining: { toughness: 1 },
|
|
interactive: true,
|
|
components: { "testmod:button": {} },
|
|
},
|
|
});
|
|
write("items/wand.json", {
|
|
format_version: 1,
|
|
item: { id: "testmod:wand", texture: "bworld:stick", components: { "testmod:wand": {} } },
|
|
});
|
|
Deno.writeTextFileSync(`${mod}/scripts/server.ts`, SERVER_SCRIPT);
|
|
const t = await test_game(dir);
|
|
Deno.removeSync(dir, { recursive: true });
|
|
t.join(1, "alice");
|
|
stone_floor(t.game, 6);
|
|
for (let x = -6; x <= 6; x++) {
|
|
for (let z = -6; z <= 6; z++) for (let y = 100; y < 104; y++) t.game.set_block(x, y, z, AIR_ID);
|
|
}
|
|
t.send(1, { type: "move", x: 0.5, y: 100, z: 5.5, yaw: 0, pitch: 0 });
|
|
for (let i = 0; i < 5; i++) t.game.tick();
|
|
t.take(1);
|
|
const ctx = (globalThis as unknown as { machines_ctx: ServerContext }).machines_ctx;
|
|
return { ...t, ctx };
|
|
}
|
|
|
|
Deno.test("which way water flows, from where it's higher to where it's lower", async () => {
|
|
const { game, ctx } = await setup();
|
|
game.set_block(0, 100, 0, "bworld:water");
|
|
for (let i = 0; i < 40; i++) game.tick();
|
|
assertEquals(ctx.world.liquid_flow(2, 100, 0), { x: 1, y: 0, z: 0 });
|
|
assertEquals(ctx.world.liquid_flow(0, 100, -2), { x: 0, y: 0, z: -1 });
|
|
// a source with water all around is still, and so is air
|
|
assertEquals(ctx.world.liquid_flow(0, 100, 0), { x: 0, y: 0, z: 0 });
|
|
assertEquals(ctx.world.liquid_flow(0, 102, 0), { x: 0, y: 0, z: 0 });
|
|
});
|
|
|
|
Deno.test("items lying in a box are found, changed and taken away", async () => {
|
|
const { game, ctx } = await setup();
|
|
const iron = ctx.world.spawn_item(0.5, 100, 0.5, { id: "bworld:iron_ingot", count: 3 });
|
|
ctx.world.spawn_item(3.5, 100, 0.5, { id: "bworld:gold_ingot", count: 1 });
|
|
// it lies where it was put, still
|
|
game.tick();
|
|
assertEquals(iron.position, { x: 0.5, y: 100, z: 0.5 });
|
|
|
|
const found = ctx.world.entities_in({ x: 0, y: 99.5, z: 0 }, { x: 1, y: 101, z: 1 }, "bworld:item");
|
|
assertEquals(found.map((entity) => entity.get_item()), [{ id: "bworld:iron_ingot", count: 3 }]);
|
|
assertEquals(ctx.world.entities_in({ x: 0, y: 99.5, z: 0 }, { x: 1, y: 101, z: 1 }, "bworld:chicken"), []);
|
|
|
|
// pressed into something else, one at a time
|
|
found[0].set_item({ id: "bworld:iron_ingot", count: 2 });
|
|
assertEquals(iron.get_item(), { id: "bworld:iron_ingot", count: 2 });
|
|
const copy = iron.get_item()!;
|
|
copy.count = 64;
|
|
assertEquals(iron.get_item()?.count, 2);
|
|
found[0].set_item(undefined);
|
|
assert(iron.removed);
|
|
assertEquals(game.item_entities().map((entity) => entity.item.type_id), ["bworld:gold_ingot"]);
|
|
});
|
|
|
|
Deno.test("entities are pushed, players by their client", async () => {
|
|
const { game, ctx, take } = await setup();
|
|
const item = ctx.world.spawn_item(0.5, 101, 0.5, { id: "bworld:stick", count: 1 });
|
|
item.push(4, 0, 0);
|
|
assertEquals(item.velocity, { x: 4, y: 0, z: 0 });
|
|
game.tick();
|
|
assert(item.position.x > 0.5);
|
|
|
|
const chicken = ctx.world.spawn_entity("bworld:chicken", 2.5, 100, 2.5);
|
|
assertThrows(() => chicken.set_item({ id: "bworld:stick", count: 1 }));
|
|
assertEquals(chicken.get_item(), undefined);
|
|
|
|
ctx.players.by_name("alice")!.push(0, 5, 0);
|
|
assertEquals(take(1).filter((m) => m.type === "push"), [{ type: "push", motion: [0, 5, 0] }]);
|
|
});
|
|
|
|
Deno.test("a block breaking over time shows its cracks to players, and machines get its drops", async () => {
|
|
const { game, ctx, take } = await setup();
|
|
const cracks = () => take(1).filter((m) => m.type === "break_progress");
|
|
ctx.world.set_break_progress(0, 99, 0, 0.5);
|
|
assertEquals(cracks(), [{ type: "break_progress", x: 0, y: 99, z: 0, stage: 4 }]);
|
|
// the same stage isn't sent again, and breaking the block takes them off
|
|
ctx.world.set_break_progress(0, 99, 0, 0.52);
|
|
assertEquals(cracks(), []);
|
|
|
|
// stone needs a pickaxe by hand, but a drill gets cobblestone
|
|
assert(ctx.blocks.toughness("bworld:stone") > 0);
|
|
assertEquals(ctx.blocks.toughness("bworld:light"), Infinity);
|
|
assertEquals(ctx.world.break_block(0, 99, 0), []);
|
|
assertEquals(cracks(), [{ type: "break_progress", x: 0, y: 99, z: 0 }]);
|
|
assertEquals(ctx.world.break_block(1, 99, 0, { harvest: true }), [{ id: "bworld:cobblestone", count: 1 }]);
|
|
assertEquals(game.item_entities().length, 1);
|
|
// collected, it's only returned
|
|
assertEquals(ctx.world.break_block(2, 99, 0, { harvest: true, collect: true }), [
|
|
{ id: "bworld:cobblestone", count: 1 },
|
|
]);
|
|
assertEquals(game.item_entities().length, 1);
|
|
assertEquals(ctx.world.break_block(2, 99, 0), undefined);
|
|
});
|
|
|
|
Deno.test("a machine uses items on blocks like a player holding them would", async () => {
|
|
const { game, ctx } = await setup();
|
|
// placing: the block goes in front of the face, and one fewer is left
|
|
const placed = ctx.world.use_item_on({ id: "bworld:cobblestone", count: 5 }, 0, 99, 0, "top");
|
|
assertEquals(game.world.get_block_id(0, 100, 0), "bworld:cobblestone");
|
|
assertEquals(placed, { item: { id: "bworld:cobblestone", count: 4 }, given: [] });
|
|
assertEquals(ctx.world.use_item_on({ id: "bworld:cobblestone", count: 1 }, 0, 100, 0, "east").item, undefined);
|
|
assertEquals(game.world.get_block_id(1, 100, 0), "bworld:cobblestone");
|
|
|
|
// right clicking a block, as a player that isn't anyone
|
|
game.set_block(3, 100, 0, "testmod:button");
|
|
ctx.world.use_item_on({ id: "bworld:stick", count: 1 }, 3, 100, 0, "west");
|
|
assertEquals(ctx.world.get_block_data<{ pressed_by: string }>(3, 100, 0)?.pressed_by, "[machine]");
|
|
|
|
// an item used on a block, which gives it something
|
|
const used = ctx.world.use_item_on({ id: "testmod:wand", count: 1 }, -3, 99, 0, "top");
|
|
assertEquals(used, { item: { id: "testmod:wand", count: 1 }, given: [{ id: "bworld:stick", count: 1 }] });
|
|
// none of it is anyone's: no player got anything, and nothing was dropped
|
|
assertEquals(game.players()[0].inventory.get_item(0), undefined);
|
|
assertEquals(game.item_entities(), []);
|
|
});
|