203 lines
8.3 KiB
TypeScript
203 lines
8.3 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// curios, from C4's Curios mod: accessory slots, in 2 columns left of the player's inventory. each player's curios are
|
|
// a container of their own, and fall out when they die like the rest of what they carry. items with the
|
|
// bworld:curio_modifiers component change the wearer's attributes, like curios' attribute modifiers, and ones with
|
|
// bworld:curio_unhurt_effect give them an effect once they've gone a while without being hurt
|
|
import type { AttributeModifier, Container, ItemStack, Player, ServerContext, TagApi } from "cubi/server";
|
|
import { title } from "./shared.ts";
|
|
|
|
// where the left column of slots goes, from the inventory screen's left edge. the panel around them ends just left of it
|
|
const COLUMNS_X = -45;
|
|
|
|
// bworld:curio_modifiers' params, a list of these
|
|
interface WornModifier {
|
|
attribute: string;
|
|
amount: number;
|
|
operation: AttributeModifier["operation"];
|
|
}
|
|
|
|
// bworld:curio_unhurt_effect's params: the effect, kept on them while they go after ticks without being hurt
|
|
interface UnhurtEffect {
|
|
effect: string;
|
|
amplifier?: number;
|
|
after: number;
|
|
}
|
|
|
|
// how long each unhurt effect is given for, topped up whenever it runs out, so it outlasts being hurt by at most this.
|
|
// two of regeneration's heals at that level (every 50 ticks, halving each level), so topping it up keeps its pace
|
|
const unhurt_effect_ticks = (amplifier: number) => Math.max(1, 50 >> amplifier) * 2;
|
|
|
|
export function register_curios(ctx: ServerContext) {
|
|
const active = active_slots(ctx.tags);
|
|
|
|
ctx.components.register_item<WornModifier[]>("bworld:curio_modifiers", {
|
|
get_lore: (_item, modifiers) => ["When worn:", ...modifiers.map(describe)].join("\n"),
|
|
});
|
|
const modifiers_of = (id: string) => (ctx.items.components(id)["bworld:curio_modifiers"] ?? []) as WornModifier[];
|
|
ctx.components.register_item<UnhurtEffect>("bworld:curio_unhurt_effect", {
|
|
get_lore: (_item, { effect, amplifier = 0, after }) =>
|
|
`After ${after / 20}s unhurt: ${title(effect)}${amplifier > 0 ? ` ${roman(amplifier + 1)}` : ""}`,
|
|
});
|
|
const unhurt_effect_of = (id: string) =>
|
|
ctx.items.components(id)["bworld:curio_unhurt_effect"] as UnhurtEffect | undefined;
|
|
|
|
// the modifiers each online player's curios gave them, by player id, to take them off again
|
|
const applied = new Map<string, { attribute: string; id: string }[]>();
|
|
const stop_watching = new Map<string, () => void>();
|
|
// the unhurt effects each online player's curios give them, and the tick they were last hurt
|
|
const unhurt_effects = new Map<string, UnhurtEffect[]>();
|
|
const last_hurt = new Map<string, number>();
|
|
|
|
// by player name, like the players themselves are saved. undefined until they first join with curios
|
|
function saved_curios(player: Player): Container | undefined {
|
|
const id = ctx.storage.get<Record<string, string>>("curios")?.[player.name];
|
|
return id ? ctx.containers.get(id) : undefined;
|
|
}
|
|
|
|
function curios_of(player: Player): Container {
|
|
const existing = saved_curios(player);
|
|
if (existing) return existing;
|
|
const container = ctx.containers.create(SLOTS.length);
|
|
const ids = ctx.storage.get<Record<string, string>>("curios") ?? {};
|
|
ctx.storage.set("curios", { ...ids, [player.name]: container.id });
|
|
return container;
|
|
}
|
|
|
|
// keeps what their curios give them up to date
|
|
function watch(player: Player, curios: Container) {
|
|
stop_watching.get(player.id)?.();
|
|
stop_watching.set(player.id, curios.on_change(() => apply_worn(player, curios)));
|
|
apply_worn(player, curios);
|
|
}
|
|
|
|
// what they wear: its attribute modifiers, and its worn model on them for everyone to see
|
|
function apply_worn(player: Player, curios: Container) {
|
|
for (const { attribute, id } of applied.get(player.id) ?? []) player.remove_modifier(attribute, id);
|
|
const now: { attribute: string; id: string }[] = [];
|
|
const worn: ItemStack[] = [];
|
|
const effects: UnhurtEffect[] = [];
|
|
for (const [index, slot] of SLOTS.entries()) {
|
|
const item = curios.get(index);
|
|
if (!item || !active.includes(slot)) continue;
|
|
worn.push(item);
|
|
for (const [i, { attribute, amount, operation }] of modifiers_of(item.id).entries()) {
|
|
const id = `bworld:curio/${slot}/${i}`;
|
|
player.add_modifier(attribute, { id, amount, operation });
|
|
now.push({ attribute, id });
|
|
}
|
|
const effect = unhurt_effect_of(item.id);
|
|
if (effect) effects.push(effect);
|
|
}
|
|
applied.set(player.id, now);
|
|
unhurt_effects.set(player.id, effects);
|
|
player.set_worn("bworld:curios", worn);
|
|
}
|
|
|
|
ctx.events.after.player_leave.subscribe(({ player }) => {
|
|
stop_watching.get(player.id)?.();
|
|
stop_watching.delete(player.id);
|
|
applied.delete(player.id);
|
|
unhurt_effects.delete(player.id);
|
|
last_hurt.delete(player.id);
|
|
});
|
|
|
|
ctx.events.after.player_join.subscribe(({ player }) => last_hurt.set(player.id, ctx.system.current_tick));
|
|
ctx.events.after.entity_damage.subscribe(({ player }) => {
|
|
if (player) last_hurt.set(player.id, ctx.system.current_tick);
|
|
});
|
|
ctx.events.after.tick.subscribe(() => {
|
|
for (const [id, effects] of unhurt_effects) {
|
|
const player = ctx.players.get(id);
|
|
if (!player) continue;
|
|
const unhurt_for = ctx.system.current_tick - (last_hurt.get(id) ?? ctx.system.current_tick);
|
|
for (const { effect, amplifier = 0, after } of effects) {
|
|
if (unhurt_for < after) continue;
|
|
// only once what they have runs out, or when it's weaker. a stronger one, like from a potion, is left alone
|
|
const current = player.effect(effect);
|
|
if (current && current.amplifier >= amplifier) continue;
|
|
player.add_effect(effect, unhurt_effect_ticks(amplifier), { amplifier });
|
|
}
|
|
}
|
|
});
|
|
|
|
// the slot types in 2 columns left of the inventory, always there while it's open. the engine draws their panel.
|
|
// this runs as they join, before their health is checked against their max health, so what their curios give them
|
|
// comes back first
|
|
ctx.menus.extend_player_menu((menu, player) => {
|
|
if (active.length === 0) return;
|
|
const curios = curios_of(player);
|
|
watch(player, curios);
|
|
// what's in slot types no item goes in anymore, since the mod that put items in them was removed, goes back
|
|
// to them
|
|
for (const [index, slot] of SLOTS.entries()) {
|
|
const item = curios.get(index);
|
|
if (item && !active.includes(slot)) {
|
|
player.give_item(item.id, item.count, item.data);
|
|
curios.set(index, undefined);
|
|
}
|
|
}
|
|
for (const [i, slot] of active.entries()) {
|
|
menu.add_slot(curios, SLOTS.indexOf(slot), {
|
|
x: COLUMNS_X + (i % 2) * 18,
|
|
y: 7 + Math.floor(i / 2) * 18,
|
|
filter: (item) => fits(ctx.tags, slot, item.id),
|
|
icon: `bworld:empty_${slot}_slot`,
|
|
});
|
|
}
|
|
});
|
|
|
|
ctx.events.after.player_death.subscribe(({ player }) => {
|
|
const curios = saved_curios(player);
|
|
if (!curios) return;
|
|
const { x, y, z } = player.position;
|
|
for (let i = 0; i < curios.size; i++) {
|
|
const item = curios.get(i);
|
|
if (!item) continue;
|
|
const { id, count, data } = item;
|
|
curios.set(i, undefined);
|
|
ctx.world.drop_item(Math.floor(x), Math.floor(Math.max(y, 0)), Math.floor(z), { id, count, data });
|
|
}
|
|
});
|
|
}
|
|
|
|
// like minecraft's attribute tooltips: "+4 Attack Damage", "+30% Attack Speed"
|
|
function describe({ attribute, amount, operation }: WornModifier): string {
|
|
const name = title(attribute);
|
|
const sign = amount < 0 ? "-" : "+";
|
|
const value = operation === "add_value" ? `${Math.abs(amount)}` : `${Math.round(Math.abs(amount) * 100)}%`;
|
|
return `${sign}${value} ${name}`;
|
|
}
|
|
|
|
function roman(level: number): string {
|
|
return ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"][level - 1] ?? `${level}`;
|
|
}
|
|
|
|
// the slot types, in curios' order. every player has one of each, at its index here in their curios container, so
|
|
// slots keep their items when mods come and go
|
|
const SLOTS = [
|
|
"curio",
|
|
"head",
|
|
"necklace",
|
|
"back",
|
|
"body",
|
|
"bracelet",
|
|
"hands",
|
|
"ring",
|
|
"belt",
|
|
"feet",
|
|
"charm",
|
|
];
|
|
|
|
// like curios, a slot type is only there once some item goes in it: something is in its item tag, #bworld:curios/ring
|
|
// for rings
|
|
function active_slots(tags: TagApi): string[] {
|
|
return SLOTS.filter((slot) => tags.get("items", `bworld:curios/${slot}`).length > 0);
|
|
}
|
|
|
|
// curios' tag validator: an item goes in the slot types whose tag it's in, and #bworld:curios/curio items go in any
|
|
function fits(tags: TagApi, slot: string, id: string): boolean {
|
|
return tags.has("items", `bworld:curios/${slot}`, id) || tags.has("items", "bworld:curios/curio", id);
|
|
}
|