Files
bworld/server/game/loot.ts
T
2026-10-08 00:54:45 -03:00

101 lines
3.9 KiB
TypeScript

// the server's loot tables: rolling them (see common/loot.ts) with the conditions, functions and modifiers mods
// register through ctx.loot. what a mod's code throws is logged under it and the roll carries on without it
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
import {
loot_references,
type LootCondition,
type LootConditionFn,
type LootFunction,
type LootFunctionFn,
type LootItem,
type LootTableJson,
roll_loot,
} from "$/common/loot.ts";
import type { ItemStack, LootContext, LootParams } from "$/common/mod_api/server.ts";
import { ModLoadError } from "$/common/mod_loader.ts";
import { run_guarded } from "./mod_runtime.ts";
type Modifier = (items: ItemStack[], context: LootContext, table: string) => ItemStack[] | void;
export class LootTables {
#conditions = new Map<string, LootConditionFn<LootContext>>();
#functions = new Map<string, LootFunctionFn<LootContext>>();
#modifiers: { mod: string; table: string; fn: Modifier }[] = [];
register_condition(mod: string, id: string, fn: (params: LootParams, context: LootContext) => boolean) {
this.#own(mod, id, "loot condition", this.#conditions);
this.#conditions.set(
id,
(params: LootCondition, context) =>
run_guarded(mod, `loot condition ${id}`, () => fn(params, context)) === true,
);
}
register_function(
mod: string,
id: string,
fn: (item: ItemStack, params: LootParams, context: LootContext) => ItemStack | undefined,
) {
this.#own(mod, id, "loot function", this.#functions);
this.#functions.set(id, (item: LootItem, params: LootFunction, context) => {
// one that throws leaves the item as it was
let ok = false;
const result = run_guarded(mod, `loot function ${id}`, () => {
const changed = fn({ ...item }, params, context);
ok = true;
return changed;
});
return ok ? result : item;
});
}
modify(mod: string, table: string, fn: Modifier) {
this.#modifiers.push({ mod, table, fn });
}
#own(mod: string, id: string, what: string, map: Map<string, unknown>) {
if (!id.startsWith(`${mod}:`)) throw new ModLoadError(mod, `${what} ${id} must be in the namespace "${mod}"`);
if (map.has(id)) throw new ModLoadError(mod, `${what} ${id} is registered twice`);
}
exists(id: string): boolean {
return EverythingRegistry.get<LootTableJson>("loot_tables", id) !== undefined;
}
// what the table gives with the mods' modifiers, in stacks that fit a slot. throws for a table that doesn't exist
roll(id: string, context: LootContext = {}): ItemStack[] {
let items: ItemStack[] = roll_loot(id, context, { conditions: this.#conditions, functions: this.#functions });
for (const { mod, table, fn } of this.#modifiers) {
if (table !== "*" && table !== id) continue;
const list = items.map((item) => ({ ...item }));
const result = run_guarded(mod, `a loot modifier for ${table}`, () => fn(list, context, id) ?? list);
if (result) items = result.filter((item) => item && typeof item.id === "string" && item.count > 0);
}
return items.flatMap(split_stack);
}
// after every setup ran: every mod condition and function a table uses must be registered, like components
check() {
for (const [id, table] of EverythingRegistry.entries<LootTableJson>("loot_tables")) {
for (const custom of loot_references(table).custom) {
if (!this.#conditions.has(custom) && !this.#functions.has(custom)) {
throw new ModLoadError(
id.split(":")[0],
`loot table ${id} uses ${custom}, which nothing registered`,
);
}
}
}
}
}
// more than a slot holds is several stacks, like minecraft's
function split_stack(item: ItemStack): ItemStack[] {
const max = EverythingRegistry.get<ItemRegistry>("items", item.id)?.max_stack ?? 64;
const stacks: ItemStack[] = [];
for (let left = item.count; left > 0; left -= max) {
stacks.push({ ...item, count: Math.min(left, max), data: structuredClone(item.data) });
}
return stacks;
}