255 lines
11 KiB
TypeScript
255 lines
11 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// juice: one fluid named, colored and valued after the bworld:juiceable item in its data, and the juicer that makes it
|
|
import { assert, assertEquals } from "@std/assert";
|
|
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
|
|
import type { FluidJson } from "$/common/mod_data.ts";
|
|
import type { ClientContext, Instances, Visual, VisualBlock } from "$/common/mod_api/client.ts";
|
|
import { CHUNK_AREA, CHUNK_SIZE } from "$/common/constants.ts";
|
|
import { block_value, display_name } from "$/common/utils.ts";
|
|
import { sell_value } from "$/common/inventory.ts";
|
|
import { block_visuals, Visuals } from "$/client/rendering/visuals.ts";
|
|
import { AssetManager } from "$/client/assets.ts";
|
|
import { fluid_rules, set_fluid_rules } from "$/client/fluid_rules.ts";
|
|
import { setup as setup_client } from "$/mods/bworld/scripts/client.ts";
|
|
import { JUICE_VALUE, type JuiceableParams } from "$/mods/bworld/scripts/shared.ts";
|
|
import { copy_dir, stone_floor, test_game } from "./helpers.ts";
|
|
|
|
const CLOUDBERRY = "testberries:cloudberry";
|
|
|
|
// mods/bworld and another mod with a juiceable berry, so there's always one whatever bworld's items have
|
|
async function setup() {
|
|
const dir = Deno.makeTempDirSync({ prefix: "bworld_juice_" });
|
|
copy_dir("mods/bworld", `${dir}/bworld`);
|
|
const write = (path: string, json: unknown) => {
|
|
Deno.mkdirSync(`${dir}/testberries/${path.split("/").slice(0, -1).join("/")}`, { recursive: true });
|
|
Deno.writeTextFileSync(`${dir}/testberries/${path}`, JSON.stringify(json));
|
|
};
|
|
write("manifest.json", {
|
|
format_version: 1,
|
|
id: "testberries",
|
|
name: "Test Berries",
|
|
version: "1.0.0",
|
|
dependencies: [{ id: "bworld", version: "*" }],
|
|
});
|
|
write("items/cloudberry.json", {
|
|
format_version: 1,
|
|
item: {
|
|
id: CLOUDBERRY,
|
|
texture: "engine:missing",
|
|
components: { "bworld:juiceable": { juice: 100, color: "#e0a030" } },
|
|
},
|
|
});
|
|
const t = await test_game(dir);
|
|
Deno.removeSync(dir, { recursive: true });
|
|
return { ...t, fluids: t.game.mods.fluid_rules.api() };
|
|
}
|
|
|
|
Deno.test("juice made from any item with bworld:juiceable is named after it, in the component's color", async () => {
|
|
const { fluids } = await setup();
|
|
const juiceable = EverythingRegistry.entries<ItemRegistry>("items")
|
|
.filter(([, item]) => item.components?.["bworld:juiceable"]);
|
|
assert(juiceable.length > 0);
|
|
for (const [id, item] of juiceable) {
|
|
const juice = { id: "bworld:juice", data: { item: id } };
|
|
assertEquals(fluids.name(juice), `${display_name(id)} Juice`);
|
|
assertEquals(fluids.color(juice), (item.components!["bworld:juiceable"] as { color: string }).color);
|
|
}
|
|
|
|
// juice that isn't from an item is plain juice
|
|
const plain = EverythingRegistry.get<FluidJson>("fluids", "bworld:juice")!;
|
|
assertEquals([fluids.name({ id: "bworld:juice" }), fluids.color({ id: "bworld:juice" })], [
|
|
plain.name,
|
|
plain.color,
|
|
]);
|
|
});
|
|
|
|
Deno.test("juice in any container sells for JUICE_VALUE times the items it took, rounded up", async () => {
|
|
const { game } = await setup();
|
|
const sellable = EverythingRegistry.entries<ItemRegistry>("items")
|
|
.filter(([, item]) => item.components?.["bworld:juiceable"] && item.base_sell_value !== undefined);
|
|
const containers = EverythingRegistry.entries<ItemRegistry>("items").filter(([, item]) => item.fluid_container);
|
|
assert(sellable.length > 0 && containers.length > 0);
|
|
for (const [id, item] of sellable) {
|
|
const { juice } = item.components!["bworld:juiceable"] as JuiceableParams;
|
|
for (const [container, info] of containers) {
|
|
const full = { fluid: { id: "bworld:juice", data: { item: id } } };
|
|
const value = sell_value(container, full, game.mods.fluid_rules)! - (info.base_sell_value ?? 0);
|
|
const exact = JUICE_VALUE * item.base_sell_value! * info.fluid_container!.capacity / juice;
|
|
assert(Number.isInteger(value) && value >= exact - 1e-9 && value < exact + 1, `${container} of ${id}`);
|
|
}
|
|
}
|
|
|
|
// juice from what can't be sold, the cloudberry, can't be sold either
|
|
assertEquals(game.mods.fluid_rules.sell_value({ id: "bworld:juice", data: { item: CLOUDBERRY } }), undefined);
|
|
});
|
|
|
|
Deno.test("the juicer takes juiceable items, juices one every 4 presses, and fills empty containers from it", async () => {
|
|
const t = await setup();
|
|
t.join(1, "alice");
|
|
stone_floor(t.game);
|
|
const move = (sneaking: boolean) =>
|
|
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0, ...(sneaking && { sneaking }) });
|
|
move(false);
|
|
const player = t.game.players()[0];
|
|
const api = t.game.mods.player(player);
|
|
api.give_item(CLOUDBERRY, 4);
|
|
api.give_item("bworld:glass_bottle", 2);
|
|
// selects the slot with id, or an empty one without
|
|
const hold = (id?: string) =>
|
|
t.send(1, {
|
|
type: "select_slot",
|
|
slot: player.inventory.to_data().findIndex((item) => (item?.id ?? null) === (id ?? null)),
|
|
});
|
|
const use = () => t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
|
|
const press = (times: number) => {
|
|
hold();
|
|
move(true);
|
|
for (let i = 0; i < times; i++) use();
|
|
move(false);
|
|
};
|
|
// the bottles, empty ones first
|
|
const bottles = () =>
|
|
player.inventory.to_data().filter((item) => item?.id === "bworld:glass_bottle")
|
|
.sort((a, b) => Number(a!.data !== undefined) - Number(b!.data !== undefined));
|
|
t.game.set_block(1, 100, 0, "bworld:juicer");
|
|
|
|
// what's in it and its juice, which clients see
|
|
const shown = () => t.game.world.get_tile(1, 100, 0)?.client_data?.stack;
|
|
const tank = () => t.game.world.get_tile(1, 100, 0)?.client_data?.tank;
|
|
const presses = () => t.game.world.get_tile(1, 100, 0)?.client_data?.presses;
|
|
const juice = { id: "bworld:juice", data: { item: CLOUDBERRY } };
|
|
hold(CLOUDBERRY);
|
|
use();
|
|
assertEquals(player.inventory.to_data().filter((item) => item?.id === CLOUDBERRY), []);
|
|
assertEquals(shown(), { id: CLOUDBERRY, count: 4 });
|
|
// right clicking with an empty hand without sneaking doesn't press it
|
|
hold();
|
|
use();
|
|
|
|
// 2 cloudberries and 3 presses toward the next are 200 mB, not a bottle's 250
|
|
press(11);
|
|
assertEquals([tank(), presses()], [{ fluid: juice, amount: 200 }, 3]);
|
|
hold("bworld:glass_bottle");
|
|
use();
|
|
assertEquals(bottles(), [{ id: "bworld:glass_bottle", count: 2 }]);
|
|
|
|
// the 3rd makes 300 mB, a bottle and 50 mB left, which isn't another
|
|
press(1);
|
|
assertEquals([shown(), presses()], [{ id: CLOUDBERRY, count: 1 }, undefined]);
|
|
hold("bworld:glass_bottle");
|
|
use();
|
|
use();
|
|
assertEquals(tank(), { fluid: juice, amount: 50 });
|
|
assertEquals(bottles(), [
|
|
{ id: "bworld:glass_bottle", count: 1 },
|
|
{ id: "bworld:glass_bottle", count: 1, data: { fluid: juice } },
|
|
]);
|
|
|
|
// the cloudberry it didn't press yet comes out when it's broken
|
|
t.send(1, { type: "break_block", x: 1, y: 100, z: 0 });
|
|
const dropped = t.game.item_entities().map((entity) => entity.item.to_data());
|
|
assertEquals(dropped.sort((a, b) => a.id.localeCompare(b.id)), [
|
|
{ id: "bworld:juicer", count: 1 },
|
|
{ id: CLOUDBERRY, count: 1 },
|
|
]);
|
|
});
|
|
|
|
// a juicer at 1 100 1 drawn by bworld's client script, and a way to set a field of its client data. its fluids have the
|
|
// server's rules, the same as a client's
|
|
async function drawn_juicer() {
|
|
const { game } = await setup();
|
|
AssetManager.instance.assets["bworld:textures_info"] = {};
|
|
const client_rules = fluid_rules;
|
|
set_fluid_rules(game.mods.fluid_rules);
|
|
// bworld's client script, with only what it needs for its visuals
|
|
const made = new Map<string, (block: VisualBlock, instances: Instances) => Visual>();
|
|
setup_client({
|
|
ui: { register_menu_screen() {} },
|
|
render: { register_visual: (id: string, create: never) => made.set(id, create) },
|
|
} as unknown as ClientContext);
|
|
const juicer = EverythingRegistry.get_id("blocks", "bworld:juicer")!;
|
|
block_visuals.clear();
|
|
block_visuals.set(juicer, { mod: "bworld", replaces_model: false, create: made.get("bworld:juicer")! });
|
|
const data: Record<string, unknown> = {};
|
|
const visuals = new Visuals({ get_block_data: () => data, get_light: () => 0 });
|
|
visuals.meshed(0, 0, Uint32Array.of(100 * CHUNK_AREA + 1 * CHUNK_SIZE + 1, block_value(juicer, 0)));
|
|
const set = (field: string, value: unknown) => {
|
|
data[field] = value;
|
|
visuals.data_changed(1, 100, 1);
|
|
};
|
|
const done = () => {
|
|
block_visuals.clear();
|
|
set_fluid_rules(client_rules);
|
|
};
|
|
return { visuals, set, done };
|
|
}
|
|
|
|
Deno.test("the juicer draws its press, what's in it, more of it the more there is, and nothing when it's empty", async () => {
|
|
const { visuals, set, done } = await drawn_juicer();
|
|
// the press, whatever's in it
|
|
const press = [...visuals.groups()].filter((group) => group.instances.length > 0);
|
|
assertEquals(press.map((group) => group.instances.length), [1]);
|
|
// one item, however many of it are drawn
|
|
const drawn = () =>
|
|
[...visuals.groups()].filter((group) => !press.includes(group) && group.instances.length > 0)
|
|
.map((group) => group.instances.length);
|
|
|
|
set("stack", { id: CLOUDBERRY, count: 1 });
|
|
const one = drawn();
|
|
assertEquals(one.length, 1);
|
|
set("stack", { id: CLOUDBERRY, count: 64 });
|
|
assertEquals(drawn().length, 1);
|
|
assert(drawn()[0] > one[0], `${drawn()} for 64, ${one} for 1`);
|
|
set("stack", undefined);
|
|
assertEquals(drawn(), []);
|
|
done();
|
|
});
|
|
|
|
Deno.test("the juicer's juice fills its 12 by 12 floor, a pixel deep while there's any and 12 when it's full", async () => {
|
|
const { visuals, set, done } = await drawn_juicer();
|
|
const boxes = () => [...visuals.groups()].filter((group) => group.translucent);
|
|
// where the box is on each axis, in pixels of the block
|
|
const box = () =>
|
|
[0, 1, 2].map((axis) => {
|
|
const at = boxes()[0].quads.flatMap((quad) => quad.positions.filter((_, i) => i % 3 === axis));
|
|
return [Math.min(...at) * 16, Math.max(...at) * 16];
|
|
});
|
|
const juice = { id: "bworld:juice", data: { item: CLOUDBERRY } };
|
|
|
|
set("tank", { fluid: juice, amount: 10 });
|
|
assertEquals(box(), [[2, 14], [2, 3], [2, 14]]);
|
|
// in the color of the juice of what it was made from
|
|
assertEquals(boxes()[0].quads[0].tint, [0xe0 / 255, 0xa0 / 255, 0x30 / 255]);
|
|
set("tank", { fluid: juice, amount: 500 });
|
|
assertEquals(box(), [[2, 14], [2, 8], [2, 14]]);
|
|
set("tank", { fluid: juice, amount: 1000 });
|
|
assertEquals(box(), [[2, 14], [2, 14], [2, 14]]);
|
|
set("tank", undefined);
|
|
assertEquals(boxes(), []);
|
|
done();
|
|
});
|
|
|
|
Deno.test("the juicer's press goes down a share of the way each press, easing there, and back up once there's juice", async () => {
|
|
const { visuals, set, done } = await drawn_juicer();
|
|
const [press] = [...visuals.groups()].filter((group) => group.instances.length > 0);
|
|
// how far below its block it is
|
|
const down = () => 100 - press.instances[0].placement[13];
|
|
const frame = (time: number) => visuals.frame([{ x: 0, z: 0 }], time);
|
|
frame(0);
|
|
assertEquals(down(), 0);
|
|
|
|
// half way to the 4th press, which would take it 6 pixels down to the floor
|
|
set("presses", 2);
|
|
frame(1);
|
|
assert(down() > 0 && down() < 3 / 16, `${down()}`);
|
|
frame(5);
|
|
assertEquals(down(), 3 / 16);
|
|
// the juice is made, so it's back up
|
|
set("presses", undefined);
|
|
frame(9);
|
|
assertEquals(down(), 0);
|
|
done();
|
|
});
|