Giant chestplate regeneration passive

This commit is contained in:
2026-10-01 18:16:03 -03:00
parent f2c3cb8135
commit 8396e8e895
9 changed files with 168 additions and 8 deletions
+55 -2
View File
@@ -1,6 +1,7 @@
// 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
// 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 "bworld/server";
// where the left column of slots goes, from the inventory screen's left edge. the panel around them ends just left of it
@@ -13,6 +14,17 @@ interface WornModifier {
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.
// a multiple of regeneration I and II's ticks between heals, so topping it up keeps their pace
const UNHURT_EFFECT_TICKS = 50;
export function register_curios(ctx: ServerContext) {
const active = active_slots(ctx.tags);
@@ -20,10 +32,19 @@ export function register_curios(ctx: ServerContext) {
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 {
@@ -52,6 +73,7 @@ export function register_curios(ctx: ServerContext) {
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;
@@ -61,8 +83,11 @@ export function register_curios(ctx: ServerContext) {
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);
}
@@ -70,6 +95,25 @@ export function register_curios(ctx: ServerContext) {
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.player_damage.subscribe(({ 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 });
}
}
});
// the slot types in 2 columns left of the inventory, always there while it's open. the engine draws their panel.
@@ -114,12 +158,21 @@ export function register_curios(ctx: ServerContext) {
// like minecraft's attribute tooltips: "+4 Attack Damage", "+30% Attack Speed"
function describe({ attribute, amount, operation }: WornModifier): string {
const name = attribute.split(":").at(-1)!.split("_").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
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}`;
}
// "bworld:regeneration" is "Regeneration", like minecraft's effect names
function title(id: string): string {
return id.split(":").at(-1)!.split("_").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
}
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 = [