100 lines
4.3 KiB
TypeScript
100 lines
4.3 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// items that hold one fluid, full or empty: how many stack, what they look like, what they sell for, and turning the
|
|
// held one into another
|
|
import { assertEquals } from "@std/assert";
|
|
import { item_tooltip } from "@/client/gui/tooltip.ts";
|
|
import { item_layers } from "@/client/rendering/item_renderer.ts";
|
|
import { ItemStack, sell_value } from "@/common/inventory.ts";
|
|
import { copy_dir, test_game } from "./helpers.ts";
|
|
|
|
const JAR = "testjars:jar";
|
|
const FLASK = "testjars:flask";
|
|
const FULL = { fluid: { id: "testjars:syrup", data: { tree: "maple" } } };
|
|
|
|
// mods/bworld, and testjars: syrup worth 10 a bucket, a jar of 250 mB that stacks to 16 empty and 4 full, drawn with
|
|
// the syrup in it and a red label, and a flask of 1000 mB worth 2 on its own
|
|
async function setup() {
|
|
const dir = Deno.makeTempDirSync({ prefix: "bworld_jars_" });
|
|
copy_dir("mods/bworld", `${dir}/bworld`);
|
|
const write = (path: string, json: unknown) => {
|
|
Deno.mkdirSync(`${dir}/testjars/${path.split("/").slice(0, -1).join("/")}`, { recursive: true });
|
|
Deno.writeTextFileSync(`${dir}/testjars/${path}`, JSON.stringify(json));
|
|
};
|
|
write("manifest.json", { format_version: 1, id: "testjars", name: "Test Jars", version: "1.0.0" });
|
|
write("fluids/syrup.json", {
|
|
format_version: 1,
|
|
fluid: { id: "testjars:syrup", color: "#a0522d", base_sell_value: 10 },
|
|
});
|
|
write("items/jar.json", {
|
|
format_version: 1,
|
|
item: {
|
|
id: JAR,
|
|
texture: "cubi:missing",
|
|
max_stack: 16,
|
|
fluid_container: { capacity: 250, full_max_stack: 4 },
|
|
layers: [{ texture: "cubi:break_0", tint: "fluid" }, { texture: "cubi:break_1", tint: "#ff0000" }],
|
|
},
|
|
});
|
|
write("items/flask.json", {
|
|
format_version: 1,
|
|
item: { id: FLASK, texture: "cubi:missing", base_sell_value: 2, fluid_container: { capacity: 1000 } },
|
|
});
|
|
const t = await test_game(dir);
|
|
Deno.removeSync(dir, { recursive: true });
|
|
t.join(1, "alice");
|
|
const player = t.game.players()[0];
|
|
return { ...t, player, api: t.game.mods.player(player) };
|
|
}
|
|
|
|
const slots = (t: Awaited<ReturnType<typeof setup>>) =>
|
|
t.player.inventory.to_data().flatMap((item, slot) => item ? [[slot, item.count, item.data !== undefined]] : []);
|
|
|
|
Deno.test("a full fluid container stacks to its full_max_stack, and an empty one to the item's max_stack", async () => {
|
|
const t = await setup();
|
|
t.api.give_item(JAR, 20);
|
|
t.api.give_item(JAR, 6, FULL);
|
|
assertEquals(slots(t), [[0, 16, false], [1, 4, false], [2, 4, true], [3, 2, true]]);
|
|
// the fluid's name, from its id since it has none, and how much there is
|
|
const tooltip = (slot: number) => item_tooltip(t.player.inventory.get_item(slot)!).map((line) => line.text);
|
|
assertEquals(tooltip(2).slice(0, 2), ["Jar", "Syrup, 250 mB"]);
|
|
assertEquals(tooltip(0), ["Jar"]);
|
|
});
|
|
|
|
Deno.test("a layer tinted fluid is drawn in the fluid's color while there's one, and others always", async () => {
|
|
await setup();
|
|
const jar = (data?: unknown) => {
|
|
const stack = new ItemStack(JAR);
|
|
stack.data = data;
|
|
return item_layers(stack);
|
|
};
|
|
const red = { texture: "cubi:break_1", color: [1, 0, 0] };
|
|
assertEquals(jar(), [red]);
|
|
assertEquals(jar(FULL), [{ texture: "cubi:break_0", color: [0xa0 / 255, 0x52 / 255, 0x2d / 255] }, red]);
|
|
});
|
|
|
|
Deno.test("a full fluid container sells for its own value and its fluid's, rounded up", async () => {
|
|
const t = await setup();
|
|
const value = (id: string, data?: unknown) => sell_value(id, data, t.game.mods.fluid_rules);
|
|
// a jar of syrup is 2.5, and a flask of it is the flask's 2 and the syrup's 10
|
|
assertEquals([value(JAR), value(JAR, FULL)], [undefined, 3]);
|
|
assertEquals([value(FLASK), value(FLASK, FULL)], [2, 12]);
|
|
const jar = new ItemStack(JAR);
|
|
jar.data = FULL;
|
|
assertEquals(item_tooltip(jar).map((line) => line.text), ["Jar", "Syrup, 250 mB", "3"]);
|
|
});
|
|
|
|
Deno.test("exchange_held puts the result in the hand from a stack of one, and in the inventory from more", async () => {
|
|
const t = await setup();
|
|
t.api.give_item(JAR, 3);
|
|
t.api.exchange_held({ id: JAR, count: 1, data: FULL });
|
|
assertEquals(slots(t), [[0, 2, false], [1, 1, true]]);
|
|
|
|
t.player.inventory.set_item(0, undefined);
|
|
t.api.give_item(JAR, 1);
|
|
t.api.exchange_held({ id: JAR, count: 1, data: FULL });
|
|
// the filled one in the hand, the one filled before still beside it
|
|
assertEquals(slots(t), [[0, 1, true], [1, 1, true]]);
|
|
});
|