diff --git a/client/gui/tooltip.ts b/client/gui/tooltip.ts index cc06f08..5101ad4 100644 --- a/client/gui/tooltip.ts +++ b/client/gui/tooltip.ts @@ -4,7 +4,7 @@ // the box next to the mouse saying what an item is, like minecraft's: its name, the fluid in it, what it's worth, then // its lore import { EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts"; -import { contained_fluid, type ItemStack } from "$/common/inventory.ts"; +import { contained_fluid, type ItemStack, sell_value } from "$/common/inventory.ts"; import { display_name } from "$/common/utils.ts"; import { fluid_rules } from "$/client/fluid_rules.ts"; import { canvas, draw_text, draw_texture_region, measure_text, type Texture } from "$/client/renderer/mod.ts"; @@ -49,8 +49,9 @@ export function item_tooltip(item: ItemStack): TooltipLine[] { if (info?.food) { lines.push({ text: `Restores ${info.food.nutrition} hunger`, color: FOOD_COLOR }); } - if (info?.base_sell_value !== undefined) { - lines.push({ icon: VALUE_ICON, text: `${info.base_sell_value}`, color: VALUE_COLOR }); + const value = sell_value(item.type_id, item.data, fluid_rules); + if (value !== undefined) { + lines.push({ icon: VALUE_ICON, text: `${value}`, color: VALUE_COLOR }); } const static_lore = info?.lore; for (const lore of [static_lore, item.lore]) { diff --git a/common/fluids.ts b/common/fluids.ts index 8c5bd5a..1a43ee5 100644 --- a/common/fluids.ts +++ b/common/fluids.ts @@ -1,9 +1,9 @@ // SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later -// fluids with their data (see FluidVariant in mod_api/shared.ts) and the rules shared scripts give them, which name and -// color a fluid by its data. each side has its own FluidRules from the same scripts, so the server's tooltips and a -// client's drawing agree +// fluids with their data (see FluidVariant in mod_api/shared.ts) and the rules shared scripts give them, which name, +// color and value a fluid by its data. each side has its own FluidRules from the same scripts, so the server's tooltips +// and a client's drawing agree import type { FluidApi, FluidRules as Rules, FluidVariant } from "./mod_api/shared.ts"; import { EverythingRegistry } from "./everything_registry.ts"; import type { FluidJson } from "./mod_data.ts"; @@ -25,16 +25,25 @@ export class FluidRules { name(fluid: FluidVariant): string { const json = known_fluid(fluid.id); - const name = this.#ask(fluid, "name", "a name", (value) => typeof value === "string" && value.length > 0); + const valid = (value: unknown): value is string => typeof value === "string" && value.length > 0; + const name = this.#ask(fluid, "name", "a name", valid); return name ?? json.name ?? name_from_id(fluid.id); } color(fluid: FluidVariant): string { const json = known_fluid(fluid.id); - const valid = (value: unknown) => color_schema.safeParse(value).success; + const valid = (value: unknown): value is string => color_schema.safeParse(value).success; return this.#ask(fluid, "color", 'a color like "#c42d39"', valid) ?? json.color; } + // what 1000 mB sells for, undefined when it can't be sold + sell_value(fluid: FluidVariant): number | undefined { + const json = known_fluid(fluid.id); + const valid = (value: unknown): value is number => + typeof value === "number" && Number.isFinite(value) && value >= 0; + return this.#ask(fluid, "sell_value", "a number of 0 or more", valid) ?? json.base_sell_value; + } + // ctx.fluids for server and client scripts api(): FluidApi { return { @@ -46,14 +55,14 @@ export class FluidRules { } // what the fluid's rule says, or undefined when it has none, says nothing, or says something that isn't one - #ask(fluid: FluidVariant, rule: "name" | "color", wanted: string, valid: (value: unknown) => boolean) { + #ask(fluid: FluidVariant, rule: keyof Rules, wanted: string, valid: (value: unknown) => value is T) { const entry = this.#rules.get(fluid.id); - const fn = entry?.rules[rule]; + const fn: ((data: unknown) => unknown) | undefined = entry?.rules[rule]; if (!entry || !fn) return undefined; return this.run(entry.mod, `the ${rule} rule of ${fluid.id}`, () => { const value = fn.call(entry.rules, fluid.data); if (value !== undefined && !valid(value)) throw new Error(`gave ${JSON.stringify(value)}, not ${wanted}`); - return value; + return value as T | undefined; }); } } diff --git a/common/inventory.ts b/common/inventory.ts index dc461aa..c4e7990 100644 --- a/common/inventory.ts +++ b/common/inventory.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later import { EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts"; +import type { FluidRules } from "./fluids.ts"; import type { FluidVariant } from "./mod_api/shared.ts"; import { is_object } from "./schema.ts"; import { same_data } from "./utils.ts"; @@ -40,6 +41,18 @@ export function contained_fluid(id: string, data?: unknown): FluidVariant | unde return fluid as unknown as FluidVariant; } +// what one of a stack sells for: its item's base_sell_value, and a full fluid container's fluid on top, rounded up. +// undefined when neither has a value, so it can't be sold +export function sell_value(id: string, data: unknown, fluids: FluidRules): number | undefined { + const item = EverythingRegistry.get("items", id); + const fluid = contained_fluid(id, data); + const per_bucket = fluid && fluids.sell_value(fluid); + if (per_bucket === undefined) return item?.base_sell_value; + const value = per_bucket * item!.fluid_container!.capacity / 1000; + // so float error doesn't round 18.000000000000004 up to 19 + return (item!.base_sell_value ?? 0) + Math.ceil(value - 1e-9); +} + export class ItemStack { type_id: string; amount: number; diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index 8d57a67..d76927d 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -538,8 +538,9 @@ export interface ItemApi { fluid_capacity(id: Id): number | undefined; // the tool type it is, like "pickaxe" or "shovel", from the item's "tool" tool(id: Id): string | undefined; - // what one of it sells for, from the item's "base_sell_value", or undefined if it can't be sold - sell_value(id: Id): number | undefined; + // what one of it sells for with this data, or undefined if it can't be sold: the item's "base_sell_value", and a full + // fluid container's fluid on top, rounded up. see docs/modding/resources/fluids.md + sell_value(id: Id, data?: unknown): number | undefined; // the components its json lists and their params, a copy. a mod can read params of its components this way // without a stack, like the curios slots do for bworld:curio_modifiers components(id: Id): Record; diff --git a/common/mod_api/shared.ts b/common/mod_api/shared.ts index ec0351d..f006a49 100644 --- a/common/mod_api/shared.ts +++ b/common/mod_api/shared.ts @@ -43,6 +43,8 @@ export interface FluidRules { name?(data: unknown): string | undefined; // a color like "#c42d39" color?(data: unknown): string | undefined; + // what 1000 mB of it is worth when sold, 0 or more and not always whole + sell_value?(data: unknown): number | undefined; } // the registries tags are for, see docs/modding/resources/tags.md @@ -112,6 +114,8 @@ export interface SharedContext { name(id: Id): string; // the components its json lists and their params, a copy components(id: Id): Record; + // what one sells for, from its "base_sell_value", or undefined if it can't be sold + sell_value(id: Id): number | undefined; }; readonly fluids: { // the rules of one of the mod's fluids, which the server and every client name and color it by diff --git a/common/mod_data.ts b/common/mod_data.ts index 88c8d3f..c9071cb 100644 --- a/common/mod_data.ts +++ b/common/mod_data.ts @@ -486,6 +486,8 @@ const fluid_schema = z.object({ color, // what a box of it is drawn with, tinted by its color, like neoforge's still texture. engine:missing when not set texture: z.string().optional(), + // what 1000 mB of it is worth when sold in a fluid container, like an item's base_sell_value + base_sell_value: z.int().min(0).optional(), }); export type FluidJson = z.infer; diff --git a/common/utils.ts b/common/utils.ts index 9326772..0d70c06 100644 --- a/common/utils.ts +++ b/common/utils.ts @@ -98,6 +98,7 @@ export function shared_items_api(mod: string): SharedContext["items"] { exists: (id) => EverythingRegistry.get("items", id) !== undefined, name: (id) => (known(id), display_name(id)), components: (id) => structuredClone(known(id).components ?? {}), + sell_value: (id) => known(id).base_sell_value, }; } diff --git a/docs/modding/blocks/components.md b/docs/modding/blocks/components.md index 7996305..fa30175 100644 --- a/docs/modding/blocks/components.md +++ b/docs/modding/blocks/components.md @@ -299,7 +299,7 @@ catch up. If it's further behind than that, it skips the rest and logs a warning | Component | Params | What it does | | ----------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `bworld:storage` | `rows` | A chest: `rows` × 9 slots, dropped when it's broken. | -| `bworld:shipping_bin` | `rows` | Like `bworld:storage`, and every morning at 06:00 sells what's in it with a `base_sell_value` for coins. | +| `bworld:shipping_bin` | `rows` | Like `bworld:storage`, and every morning at 06:00 sells what's in it that has a sell value for coins. | | `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. | | `bworld:smithing_table` | none | Upgrades tools with `smithing` recipes. | | `bworld:stonecutter` | none | Cuts one item into another with `bworld:stonecutting` recipes, picked from a list. | diff --git a/docs/modding/items/food.md b/docs/modding/items/food.md index 3bdcb20..c10c051 100644 --- a/docs/modding/items/food.md +++ b/docs/modding/items/food.md @@ -45,6 +45,10 @@ Juices from different items are different fluids, so they never share a bottle o (`bworld:glass_bottle`, 250 mB) and buckets (`bworld:bucket`, 1000 mB) hold it, and whatever makes juice reads the component. +Juice sells for 1.2 times the items it took to make, rounded up, so it's always worth juicing something. A bottle of +juice from an item worth 30 that makes 200 mB took 1.25 of them, and sells for 45. Juice from an item without a +`base_sell_value` can't be sold. + ## Hunger and Health Hunger and health work like Minecraft's: diff --git a/docs/modding/items/index.md b/docs/modding/items/index.md index e8f1161..357680f 100644 --- a/docs/modding/items/index.md +++ b/docs/modding/items/index.md @@ -250,6 +250,7 @@ ctx.items.max_stack(id, data); // the same for a stack with this data, fewer for ctx.items.fluid_capacity("bworld:bucket"); // how much a fluid container holds full, or undefined ctx.items.tool("bworld:wood_pickaxe"); // "pickaxe", or undefined for items that aren't tools ctx.items.sell_value("bworld:copper_coin"); // 1, or undefined for items without a base_sell_value +ctx.items.sell_value(id, data); // the same for a stack with this data, with a full fluid container's fluid on top player.give_item("bworld:carrot", 3); // what doesn't fit drops at their feet player.exchange_held({ id: "bworld:bucket", count: 1, data }); // one of the held item becomes this diff --git a/docs/modding/resources/fluids.md b/docs/modding/resources/fluids.md index 3428e6e..12cbc82 100644 --- a/docs/modding/resources/fluids.md +++ b/docs/modding/resources/fluids.md @@ -17,17 +17,19 @@ here has no block, so it can't be placed. "id": "copper_tools:maple_syrup", "name": "Maple Syrup", "color": "#a0522d", - "texture": "bworld:water_still" + "texture": "bworld:water_still", + "base_sell_value": 40 } } ``` -| Field | Required | What it does | -| --------- | -------- | ----------------------------------------------------------------------------------------------------------------------------- | -| `id` | yes | The fluid's id, in the mod's [namespace](../concepts/registries.md#identifiers). | -| `name` | no | What players see. `copper_tools:maple_syrup` is "Maple Syrup" when not set, like an item's. | -| `color` | yes | Its color, like `"#a0522d"`, for whatever shows it. | -| `texture` | no | What a [box of it](../blocks/visuals.md#fluids) is drawn with, tinted by its color, like NeoForge's still texture. See below. | +| Field | Required | What it does | +| ----------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------- | +| `id` | yes | The fluid's id, in the mod's [namespace](../concepts/registries.md#identifiers). | +| `name` | no | What players see. `copper_tools:maple_syrup` is "Maple Syrup" when not set, like an item's. | +| `color` | yes | Its color, like `"#a0522d"`, for whatever shows it. | +| `texture` | no | What a [box of it](../blocks/visuals.md#fluids) is drawn with, tinted by its color, like NeoForge's still texture. See below. | +| `base_sell_value` | no | What 1000 mB is worth sold in a [container](#containers), a whole number of 0 or more. None when not set. | Like a fluid container's layer, the texture is multiplied by the fluid's color, so a light gray one comes out in the fluid's own color, and its alpha is how much shows through. The base game's `bworld:water_still` is such a texture, @@ -46,8 +48,8 @@ a tank. ## Rules -A fluid's name and color can depend on its data. The mod's [shared script](../concepts/sides.md#shared-scripts) gives -one of its fluids rules for that, like NeoForge's `FluidType.getDescription` and `FluidTintSource.colorAsStack`: +A fluid's name, color and value can depend on its data. The mod's [shared script](../concepts/sides.md#shared-scripts) +gives one of its fluids rules for that, like NeoForge's `FluidType.getDescription` and `FluidTintSource.colorAsStack`: ```ts import type { SharedContext } from "bworld/shared"; @@ -66,22 +68,29 @@ export function setup(ctx: SharedContext) { const { tree } = (data ?? {}) as SyrupData; return tree === "bworld:birch_log" ? "#d9a441" : undefined; }, + // worth twice the logs it takes to make, 4 to a bucket + sell_value(data) { + const { tree } = (data ?? {}) as SyrupData; + const value = tree && ctx.items.sell_value(tree); + return value ? value * 4 * 2 : undefined; + }, }); } ``` -| Rule | Returns | -| ------------- | -------------------------------------------------------- | -| `name(data)` | What players see, or `undefined` for the JSON's name. | -| `color(data)` | A color like `"#d9a441"`, or `undefined` for the JSON's. | +| Rule | Returns | +| ------------------ | ------------------------------------------------------------------------------------------------------- | +| `name(data)` | What players see, or `undefined` for the JSON's name. | +| `color(data)` | A color like `"#d9a441"`, or `undefined` for the JSON's. | +| `sell_value(data)` | What 1000 mB is worth, 0 or more and not always whole, or `undefined` for the JSON's `base_sell_value`. | `data` is the variant's, `undefined` when it has none, and mustn't be changed. A rule that throws, or returns something -that isn't a name or a color, is logged and the JSON's is used. A fluid has one set of rules, from its own mod, and they -can only be registered during `setup`. +that isn't a name, a color or a value, is logged and the JSON's is used. A fluid has one set of rules, from its own mod, +and they can only be registered during `setup`. The rules are in the shared script because both sides need them: the server for tooltips, and clients to draw the fluid. -Shared scripts have `ctx.items.exists(id)`, `ctx.items.name(id)` and `ctx.items.components(id)` for rules that read an -item, like syrup named after its tree. +Shared scripts have `ctx.items.exists(id)`, `ctx.items.name(id)`, `ctx.items.components(id)` and +`ctx.items.sell_value(id)` for rules that read an item, like syrup named after its tree. ## Using Fluids @@ -127,6 +136,11 @@ What's in it is drawn with a [layer](../items/index.md#layers) tinted `"fluid"`, where the fluid shows, and light gray, since the color multiplies it: white comes out as the fluid's own color. Its tooltip says what's in it and how much, like "Birch Log Syrup, 250 mB". +A full one sells for its own `base_sell_value` and what its fluid is worth on top: the fluid's value for 1000 mB times +how much it holds, rounded up to a whole number. A jar of maple syrup worth 40 a bucket sells for 10, and the jar's own +value when it has one. It can't be sold if neither has a value. Server scripts get this from +`ctx.items.sell_value(id, data)`, which the base game's shipping bin sells by, and tooltips show it. + A server script fills one with [`player.exchange_held`](../entities/players.md#the-player-api), which turns one of the held stack into the full one, and `ctx.items.fluid_capacity(id)` says how much it takes: diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index 5fafbf0..91a4a96 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -132,8 +132,8 @@ function register_storage(ctx: ServerContext) { return menu_type; } -// a shipping bin, like stardew valley's: a chest that every morning at 06:00 sells everything in it with a -// base_sell_value, and pays for it in as few coins as it can +// a shipping bin, like stardew valley's: a chest that every morning at 06:00 sells everything in it with a sell +// value, and pays for it in as few coins as it can const COINS = [ "bworld:cobalt_coin", @@ -165,7 +165,7 @@ function register_shipping_bin(ctx: ServerContext, storage_menu: ReturnType { const item = (data as { item?: unknown } | undefined)?.item; return typeof item === "string" && ctx.items.exists(item) ? item : undefined; }; + const juiceable = (item: string) => ctx.items.components(item)["bworld:juiceable"] as JuiceableParams | undefined; ctx.fluids.register("bworld:juice", { name(data) { const item = item_of(data); @@ -168,7 +173,14 @@ function register_juice(ctx: SharedContext) { }, color(data) { const item = item_of(data); - return item && (ctx.items.components(item)["bworld:juiceable"] as JuiceableParams | undefined)?.color; + return item && juiceable(item)?.color; + }, + sell_value(data) { + const item = item_of(data); + if (!item) return undefined; + const juice = juiceable(item)?.juice; + const value = ctx.items.sell_value(item); + return juice && value !== undefined ? JUICE_VALUE * value * 1000 / juice : undefined; }, }); } diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index 6c3edd1..692d024 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -4,7 +4,12 @@ // runs mods' server scripts: builds each one's ServerContext and keeps what they register. // see docs/modding/. parts not built yet throw when used, saying so import { BlockRegistry, EverythingRegistry, fluid_height, ItemRegistry } from "$/common/everything_registry.ts"; -import { Container as EngineContainer, ItemStack as EngineItemStack, max_stack } from "$/common/inventory.ts"; +import { + Container as EngineContainer, + ItemStack as EngineItemStack, + max_stack, + sell_value, +} from "$/common/inventory.ts"; import { block_states, fluid_block, @@ -921,10 +926,9 @@ export class ModRuntime { if (!item) throw new Error(`unknown item ${id}`); return item.tool?.type; }, - sell_value: (id) => { - const item = EverythingRegistry.get("items", id); - if (!item) throw new Error(`unknown item ${id}`); - return item.base_sell_value; + sell_value: (id, data) => { + if (!EverythingRegistry.get("items", id)) throw new Error(`unknown item ${id}`); + return sell_value(id, data, this.fluid_rules); }, components: (id) => { const item = EverythingRegistry.get("items", id); diff --git a/tests/base_game_test.ts b/tests/base_game_test.ts index 68623c8..ecff7a5 100644 --- a/tests/base_game_test.ts +++ b/tests/base_game_test.ts @@ -4,6 +4,7 @@ // the base game's block and item behavior: hoeing, chests, furnaces, crops and the watering can import { assert, assertEquals } from "@std/assert"; import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts"; +import { sell_value } from "$/common/inventory.ts"; import { CYCLE_TICKS } from "$/common/time.ts"; import { display_name, get_state_value } from "$/common/utils.ts"; import { container_items, menu_data, menu_items, opened_menu, stone_floor, test_game } from "./helpers.ts"; @@ -131,6 +132,32 @@ Deno.test("a shipping bin sells what's in it every morning for as few coins as i ]); }); +Deno.test("a shipping bin sells a stack for what its data makes it worth, like a bottle by its juice", async () => { + const t = await setup(); + const [fruit] = EverythingRegistry.entries("items") + .find(([, item]) => item.components?.["bworld:juiceable"] && item.base_sell_value !== undefined)!; + const full = { fluid: { id: "bworld:juice", data: { item: fruit } } }; + t.send(1, { type: "chat", text: `/give bworld:glass_bottle 3 ${JSON.stringify(full)}` }); + t.game.set_block(1, 100, 0, "bworld:shipping_bin"); + const bottles = slot_of(t.take(1), "bworld:glass_bottle"); + + t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); + const opened = opened_menu(t.take(1)); + move_to_menu(t, opened, bottles, 0); + t.send(1, { type: "close_menu", menu: opened.id }); + t.take(1); + t.game.set_time((t.game.world.day + 1) * CYCLE_TICKS - 1); + t.game.tick(); + + t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); + const sold = menu_items(t.take(1)).slice(0, 27).filter((item: Msg) => item); + const coin_value = (id: string) => EverythingRegistry.get("items", id)!.base_sell_value!; + const paid = sold.reduce((total: number, coin: Msg) => total + coin.count * coin_value(coin.id), 0); + const value = sell_value("bworld:glass_bottle", full, t.game.mods.fluid_rules)!; + assert(value > 0); + assertEquals(paid, 3 * value); +}); + Deno.test("a furnace burns fuel to smelt, shows its progress, and only lets the result be taken", async () => { const t = await setup([["bworld:iron_ore", 2], ["bworld:coal", 1]]); t.game.set_block(1, 100, 0, "bworld:furnace"); diff --git a/tests/fluid_container_test.ts b/tests/fluid_container_test.ts index 8612af7..7d00ecc 100644 --- a/tests/fluid_container_test.ts +++ b/tests/fluid_container_test.ts @@ -1,18 +1,20 @@ // 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, and turning the held one into another +// 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 } from "$/common/inventory.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, and a jar of 250 mB that stacks to 16 empty and 4 full, drawn with the syrup in it -// and a red label +// 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`); @@ -21,7 +23,10 @@ async function setup() { 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" } }); + 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: { @@ -32,6 +37,10 @@ async function setup() { layers: [{ texture: "engine:break_0", tint: "fluid" }, { texture: "engine:break_1", tint: "#ff0000" }], }, }); + write("items/flask.json", { + format_version: 1, + item: { id: FLASK, texture: "engine:missing", base_sell_value: 2, fluid_container: { capacity: 1000 } }, + }); const t = await test_game(dir); Deno.removeSync(dir, { recursive: true }); t.join(1, "alice"); @@ -65,6 +74,17 @@ Deno.test("a layer tinted fluid is drawn in the fluid's color while there's one, assertEquals(jar(FULL), [{ texture: "engine: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); diff --git a/tests/juice_test.ts b/tests/juice_test.ts index 9e68f4f..e458e98 100644 --- a/tests/juice_test.ts +++ b/tests/juice_test.ts @@ -1,17 +1,19 @@ // SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later -// juice: one fluid named and colored after the bworld:juiceable item in its data, and the juicer that makes it +// 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"; @@ -63,6 +65,26 @@ Deno.test("juice made from any item with bworld:juiceable is named after it, in ]); }); +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("items") + .filter(([, item]) => item.components?.["bworld:juiceable"] && item.base_sell_value !== undefined); + const containers = EverythingRegistry.entries("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");