205 lines
9.3 KiB
TypeScript
205 lines
9.3 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// fluid handlers: blocks' from their components, fluid containers', the tanks ctx.fluids.tank_handler keeps for a
|
|
// script, and pouring from one into another
|
|
import { assertEquals } from "@std/assert";
|
|
import { item_handler, transfer } from "@/server/game/fluid_handlers.ts";
|
|
import { copy_dir, stone_floor, test_game } from "./helpers.ts";
|
|
|
|
const SYRUP = { id: "testmod:syrup" };
|
|
const MAPLE = { id: "testmod:syrup", data: { tree: "maple" } };
|
|
const HONEY = { id: "testmod:honey" };
|
|
const JAR = "testmod:jar";
|
|
|
|
const SERVER_SCRIPT = `
|
|
import type { FluidStack, ServerContext } from "cubi/server";
|
|
export function server(ctx: ServerContext) {
|
|
// two tanks kept in its data, reached from every side but the bottom
|
|
ctx.components.register_block("testmod:tank", {
|
|
fluid_handler(block, _params, face) {
|
|
if (face === "bottom") return undefined;
|
|
return ctx.fluids.tank_handler({
|
|
tanks: 2,
|
|
capacity: 1000,
|
|
get: () => block.data?.tanks ?? [],
|
|
set: (tanks: FluidStack[]) => { block.data = { tanks }; },
|
|
});
|
|
},
|
|
on_interact(block, _params, player) {
|
|
const handler = ctx.fluids.block_handler(block.x, block.y, block.z, "top");
|
|
return handler !== undefined && ctx.fluids.interact(player, handler);
|
|
},
|
|
});
|
|
}
|
|
`;
|
|
|
|
async function setup() {
|
|
const dir = Deno.makeTempDirSync({ prefix: "bworld_fluid_handler_" });
|
|
copy_dir("mods/bworld", `${dir}/bworld`);
|
|
const mod = `${dir}/testmod`;
|
|
for (const folder of ["blocks", "items", "fluids", "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: "*" }],
|
|
main: "scripts/server.ts",
|
|
});
|
|
write("fluids/syrup.json", { format_version: 1, fluid: { id: SYRUP.id, color: "#a0522d" } });
|
|
write("fluids/honey.json", { format_version: 1, fluid: { id: HONEY.id, color: "#e0a030" } });
|
|
write("items/jar.json", {
|
|
format_version: 1,
|
|
item: { id: JAR, texture: "cubi:missing", fluid_container: { capacity: 250 } },
|
|
});
|
|
write("blocks/tank.json", {
|
|
format_version: 1,
|
|
block: {
|
|
id: "testmod:tank",
|
|
textures: "bworld:stone",
|
|
mining: { toughness: 1 },
|
|
components: { "testmod:tank": {} },
|
|
},
|
|
});
|
|
Deno.writeTextFileSync(`${mod}/scripts/server.ts`, SERVER_SCRIPT);
|
|
try {
|
|
const t = await test_game(dir);
|
|
stone_floor(t.game);
|
|
t.game.set_block(1, 100, 0, "testmod:tank");
|
|
const tank = (face?: "top" | "bottom") => t.game.mods.block_fluid_handler(1, 100, 0, face);
|
|
const tanks = () => [0, 1].map((i) => tank()!.fluid_in_tank(i));
|
|
return { ...t, tank, tanks };
|
|
} finally {
|
|
Deno.removeSync(dir, { recursive: true });
|
|
}
|
|
}
|
|
|
|
Deno.test("a block's fluid handler comes from its components, for the side it's reached from", async () => {
|
|
const { game, tank } = await setup();
|
|
assertEquals(tank()?.tanks(), 2);
|
|
assertEquals(tank("top")?.tanks(), 2);
|
|
assertEquals(tank("bottom"), undefined);
|
|
// blocks without one, and air
|
|
assertEquals(game.mods.block_fluid_handler(0, 99, 0, "top"), undefined);
|
|
assertEquals(game.mods.block_fluid_handler(0, 110, 0, undefined), undefined);
|
|
});
|
|
|
|
Deno.test("tank_handler fills the tank with the same fluid or else an empty one, and drains from the first", async () => {
|
|
const { tank, tanks } = await setup();
|
|
assertEquals(tank()!.fill({ fluid: SYRUP, amount: 600 }, "execute"), 600);
|
|
// only what fits, and simulating changes nothing
|
|
assertEquals(tank()!.fill({ fluid: SYRUP, amount: 600 }, "simulate"), 400);
|
|
assertEquals(tanks(), [{ fluid: SYRUP, amount: 600 }, undefined]);
|
|
// the same fluid with other data is another fluid, in the other tank
|
|
assertEquals(tank()!.fill({ fluid: MAPLE, amount: 300 }, "execute"), 300);
|
|
// and with both tanks taken there's no room for a third
|
|
assertEquals(tank()!.fill({ fluid: HONEY, amount: 100 }, "execute"), 0);
|
|
assertEquals(tanks(), [{ fluid: SYRUP, amount: 600 }, { fluid: MAPLE, amount: 300 }]);
|
|
|
|
// a number of mB comes from the first tank, a fluid from its own
|
|
assertEquals(tank()!.drain(100, "execute"), { fluid: SYRUP, amount: 100 });
|
|
assertEquals(tank()!.drain({ fluid: MAPLE, amount: 1000 }, "simulate"), { fluid: MAPLE, amount: 300 });
|
|
assertEquals(tank()!.drain({ fluid: HONEY, amount: 1000 }, "execute"), undefined);
|
|
assertEquals(tanks(), [{ fluid: SYRUP, amount: 500 }, { fluid: MAPLE, amount: 300 }]);
|
|
// an emptied tank takes any fluid again
|
|
assertEquals(tank()!.drain({ fluid: MAPLE, amount: 1000 }, "execute"), { fluid: MAPLE, amount: 300 });
|
|
assertEquals(tank()!.fill({ fluid: HONEY, amount: 100 }, "execute"), 100);
|
|
assertEquals(tanks(), [{ fluid: SYRUP, amount: 500 }, { fluid: HONEY, amount: 100 }]);
|
|
});
|
|
|
|
Deno.test("a fluid container's handler fills and drains all it holds at once, changing its container", async () => {
|
|
await setup();
|
|
assertEquals(item_handler({ id: "bworld:stone", count: 1 }), undefined);
|
|
// one of the stack
|
|
const jar = item_handler({ id: JAR, count: 5 })!;
|
|
assertEquals([jar.tanks(), jar.tank_capacity(0), jar.fluid_in_tank(0)], [1, 250, undefined]);
|
|
assertEquals(jar.container, { id: JAR, count: 1 });
|
|
assertEquals(jar.drain(1000, "execute"), undefined);
|
|
// less than it holds doesn't fill it
|
|
assertEquals(jar.fill({ fluid: SYRUP, amount: 100 }, "execute"), 0);
|
|
assertEquals(jar.fill({ fluid: SYRUP, amount: 1000 }, "simulate"), 250);
|
|
assertEquals(jar.container, { id: JAR, count: 1 });
|
|
assertEquals(jar.fill({ fluid: SYRUP, amount: 1000 }, "execute"), 250);
|
|
assertEquals(jar.container, { id: JAR, count: 1, data: { fluid: SYRUP } });
|
|
assertEquals(jar.fill({ fluid: SYRUP, amount: 1000 }, "execute"), 0);
|
|
|
|
// and it only empties all at once, of its own fluid
|
|
assertEquals(jar.drain(100, "execute"), undefined);
|
|
assertEquals(jar.drain({ fluid: HONEY, amount: 250 }, "execute"), undefined);
|
|
assertEquals(jar.drain(1000, "execute"), { fluid: SYRUP, amount: 250 });
|
|
assertEquals(jar.container, { id: JAR, count: 1 });
|
|
|
|
// a full one from its data
|
|
assertEquals(item_handler({ id: JAR, count: 1, data: { fluid: MAPLE } })!.fluid_in_tank(0), {
|
|
fluid: MAPLE,
|
|
amount: 250,
|
|
});
|
|
});
|
|
|
|
Deno.test("transfer pours what the second handler takes of what the first gives", async () => {
|
|
const { tank, tanks } = await setup();
|
|
tank()!.fill({ fluid: SYRUP, amount: 600 }, "execute");
|
|
const jar = item_handler({ id: JAR, count: 1 })!;
|
|
assertEquals(transfer(tank()!, jar, Infinity), { fluid: SYRUP, amount: 250 });
|
|
assertEquals(tanks(), [{ fluid: SYRUP, amount: 350 }, undefined]);
|
|
assertEquals(transfer(tank()!, jar, Infinity), undefined);
|
|
// a jar takes no less than it holds
|
|
assertEquals(transfer(tank()!, item_handler({ id: JAR, count: 1 })!, 200), undefined);
|
|
assertEquals(tanks(), [{ fluid: SYRUP, amount: 350 }, undefined]);
|
|
assertEquals(transfer(jar, tank()!, Infinity), { fluid: SYRUP, amount: 250 });
|
|
assertEquals(tanks(), [{ fluid: SYRUP, amount: 600 }, undefined]);
|
|
assertEquals(jar.container, { id: JAR, count: 1 });
|
|
});
|
|
|
|
Deno.test("using a fluid container on a block's handler fills it from there, or else empties it in", async () => {
|
|
const { game, join, send, tank, tanks } = await setup();
|
|
join(1, "alice");
|
|
send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
|
|
const player = game.players()[0];
|
|
game.mods.player(player).give_item(JAR, 2);
|
|
const jars = () => player.inventory.to_data().filter((item) => item?.id === JAR);
|
|
const use = () => send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
|
|
tank()!.fill({ fluid: SYRUP, amount: 300 }, "execute");
|
|
|
|
use();
|
|
assertEquals(jars(), [{ id: JAR, count: 1 }, { id: JAR, count: 1, data: { fluid: SYRUP } }]);
|
|
assertEquals(tanks(), [{ fluid: SYRUP, amount: 50 }, undefined]);
|
|
// 50 mB doesn't fill a jar
|
|
use();
|
|
assertEquals(tanks(), [{ fluid: SYRUP, amount: 50 }, undefined]);
|
|
|
|
send(1, { type: "select_slot", slot: player.inventory.to_data().findIndex((item) => item?.data) });
|
|
use();
|
|
assertEquals(tanks(), [{ fluid: SYRUP, amount: 300 }, undefined]);
|
|
assertEquals(jars(), [{ id: JAR, count: 1 }, { id: JAR, count: 1 }]);
|
|
});
|
|
|
|
Deno.test("the juicer's fluid handler only gives juice out, and the basin's takes any two fluids", async () => {
|
|
// every mod in mods/, create among them
|
|
const { game } = await test_game("mods");
|
|
stone_floor(game);
|
|
const juice = { id: "bworld:juice" };
|
|
game.set_block(1, 100, 0, "bworld:juicer");
|
|
const juicer = () => game.mods.block_fluid_handler(1, 100, 0, "top")!;
|
|
assertEquals(juicer().fill({ fluid: juice, amount: 100 }, "execute"), 0);
|
|
game.mods.block_ref(1, 100, 0, "bworld:juicer").set_client_data("tank", { fluid: juice, amount: 500 });
|
|
assertEquals(juicer().drain(200, "execute"), { fluid: juice, amount: 200 });
|
|
assertEquals(game.world.get_tile(1, 100, 0)?.client_data?.tank, { fluid: juice, amount: 300 });
|
|
|
|
game.set_block(2, 100, 0, "create:basin");
|
|
const basin = () => game.mods.block_fluid_handler(2, 100, 0, "top")!;
|
|
assertEquals(transfer(juicer(), basin(), Infinity), { fluid: juice, amount: 300 });
|
|
assertEquals(
|
|
basin().fill({ fluid: { id: "bworld:juice", data: { item: "bworld:apple" } }, amount: 100 }, "execute"),
|
|
100,
|
|
);
|
|
assertEquals(game.world.get_tile(2, 100, 0)?.client_data?.tanks, [
|
|
{ fluid: juice, amount: 300 },
|
|
{ fluid: { id: "bworld:juice", data: { item: "bworld:apple" } }, amount: 100 },
|
|
]);
|
|
});
|