Juice sell price
This commit is contained in:
+17
-8
@@ -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<T>(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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<ItemRegistry>("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<T = unknown | undefined> {
|
||||
type_id: string;
|
||||
amount: number;
|
||||
|
||||
@@ -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<Id, unknown>;
|
||||
|
||||
@@ -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<Id, unknown>;
|
||||
// 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
|
||||
|
||||
@@ -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<typeof fluid_schema>;
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user