Giant chestplate regeneration passive
This commit is contained in:
@@ -159,6 +159,8 @@ export interface ServerAfterEvents {
|
||||
chat_send: EventSignal<Readonly<ChatSendEvent>>;
|
||||
player_join: EventSignal<{ readonly player: Player }>;
|
||||
player_leave: EventSignal<{ readonly player: Player }>;
|
||||
// they were hurt, after before handlers had their say. amount is what they took
|
||||
player_damage: EventSignal<Readonly<PlayerDamageEvent>>;
|
||||
// they drop what they carried and respawn right after
|
||||
player_death: EventSignal<PlayerDeathEvent>;
|
||||
server_start: EventSignal<Record<never, never>>;
|
||||
@@ -239,6 +241,20 @@ export interface Player {
|
||||
add_modifier(attribute: Id, modifier: AttributeModifier): void;
|
||||
// whether it had it
|
||||
remove_modifier(attribute: Id, id: string): boolean;
|
||||
// status effects, like minecraft's: "bworld:regeneration", "bworld:speed"... duration in ticks, -1 for forever.
|
||||
// adding one they have merges them like minecraft: the stronger wins, and a weaker longer one waits under it
|
||||
add_effect(id: Id, duration: number, options?: EffectOptions): void;
|
||||
// the one they have of that type, undefined when they don't
|
||||
effect(id: Id): { readonly amplifier: number; readonly duration: number } | undefined;
|
||||
// whether they had it
|
||||
remove_effect(id: Id): boolean;
|
||||
}
|
||||
|
||||
export interface EffectOptions {
|
||||
amplifier?: number; // level minus 1: 1 is regeneration II
|
||||
ambient?: boolean; // fainter particles, like from a beacon
|
||||
show_particles?: boolean;
|
||||
show_icon?: boolean;
|
||||
}
|
||||
|
||||
// like minecraft's: the base, plus every add_value, plus that times each add_multiplied_base, then times 1 + each
|
||||
|
||||
@@ -48,7 +48,7 @@ showed. These events fire for **every** player's actions, since they all go thro
|
||||
| `block_place` | ✓ | ✓ | `player`, `block` (`{ id, x, y, z }`), `face` clicked, `item` held |
|
||||
| `block_interact` | ✓ | ✓ | `player`, `block`, `item` held. Before fires for every right click on a block, after only when a component's `on_interact` handled it |
|
||||
| `chat_send` | ✓ | ✓ | `player`, `message`. Before handlers can change `message` |
|
||||
| `player_damage` | ✓ | | `player`, `amount`, `cause`. Before handlers can change `amount` |
|
||||
| `player_damage` | ✓ | ✓ | `player`, `amount`, `cause`. Before handlers can change `amount`, after fires only when they were hurt, with what they took |
|
||||
| `player_death` | | ✓ | `player`, `cause`. They've dropped what they carried and respawn right after |
|
||||
| `player_join` | | ✓ | `player` |
|
||||
| `player_leave` | | ✓ | `player` |
|
||||
|
||||
@@ -42,6 +42,10 @@ interface Player {
|
||||
attribute(id: string): number;
|
||||
add_modifier(attribute: string, modifier: AttributeModifier): void;
|
||||
remove_modifier(attribute: string, id: string): boolean;
|
||||
|
||||
add_effect(id: string, duration: number, options?: EffectOptions): void;
|
||||
effect(id: string): { amplifier: number; duration: number } | undefined;
|
||||
remove_effect(id: string): boolean;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -93,6 +97,24 @@ Like Minecraft, an attack right after the last one, or right after changing the
|
||||
damage, rising to full after `20 / attack_speed` ticks. A bar under the crosshair shows it filling up, and the server
|
||||
sends players their `attack_speed` whenever it changes so the bar matches.
|
||||
|
||||
## Status Effects
|
||||
|
||||
Players have Minecraft's status effects: `bworld:speed`, `bworld:slowness`, `bworld:strength`, `bworld:weakness`,
|
||||
`bworld:jump_boost`, `bworld:health_boost`, `bworld:regeneration`, `bworld:poison`, `bworld:wither`,
|
||||
`bworld:instant_health`, `bworld:instant_damage`, `bworld:resistance`, `bworld:fire_resistance`,
|
||||
`bworld:water_breathing`, `bworld:slow_falling` and `bworld:glowing`.
|
||||
|
||||
```ts
|
||||
player.add_effect("bworld:regeneration", 100, { amplifier: 1 }); // regeneration II for 5 seconds
|
||||
player.effect("bworld:regeneration"); // { amplifier: 1, duration: 100 }
|
||||
player.remove_effect("bworld:regeneration");
|
||||
```
|
||||
|
||||
`duration` is in ticks, `-1` for forever. `amplifier` is the level minus 1. The options also take `ambient`,
|
||||
`show_particles` and `show_icon`. Like Minecraft, adding an effect they already have keeps the stronger one, and a
|
||||
weaker one that lasts longer waits under it, taking over when the stronger one runs out. A player's effects aren't
|
||||
saved: they're gone when the player leaves, and when they die.
|
||||
|
||||
## Worn Items
|
||||
|
||||
`set_worn(key, items)` says what a mod shows on a player, like the base game's curios slots do for what's in them. Each
|
||||
|
||||
+17
-1
@@ -13,7 +13,8 @@ necklaces and belts, for any mod's items. They're in 2 columns left of the playe
|
||||
Shift clicking a curio in the inventory puts it in the first slot it fits.
|
||||
|
||||
A slot type only shows up once some item is in its tag. The base game's one curio is the giant chestplate, a body curio
|
||||
that gives 20 more max health while worn, so the body slot is always there.
|
||||
that gives 20 more max health while worn, and regeneration II after 10 seconds without being hurt, so the body slot is
|
||||
always there.
|
||||
|
||||
### Making an Item a Curio
|
||||
|
||||
@@ -50,3 +51,18 @@ The Power Glove from Artifacts:
|
||||
"bworld:curio_modifiers": [{ "attribute": "bworld:attack_damage", "amount": 4, "operation": "add_value" }]
|
||||
}
|
||||
```
|
||||
|
||||
### Effects When Unhurt
|
||||
|
||||
An item with the `bworld:curio_unhurt_effect` component gives the wearer a
|
||||
[status effect](../../docs/modding/entities/players.md#status-effects) once they've gone `after` ticks without being
|
||||
hurt, and keeps it on them until they're hurt again. The giant chestplate's regeneration II after 10 seconds:
|
||||
|
||||
```json
|
||||
"components": {
|
||||
"bworld:curio_unhurt_effect": { "effect": "bworld:regeneration", "amplifier": 1, "after": 200 }
|
||||
}
|
||||
```
|
||||
|
||||
It's given 2.5 seconds at a time, so it lasts up to that long after they're hurt. A stronger one they already have, like
|
||||
from a potion, is left alone.
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
"components": {
|
||||
"bworld:curio_modifiers": [
|
||||
{ "attribute": "bworld:max_health", "amount": 20, "operation": "add_value" }
|
||||
]
|
||||
],
|
||||
"bworld:curio_unhurt_effect": { "effect": "bworld:regeneration", "amplifier": 2, "after": 200 }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 = [
|
||||
|
||||
@@ -1336,7 +1336,9 @@ export class GameServer {
|
||||
if (before.cancel || !(final > 0)) {
|
||||
return false;
|
||||
}
|
||||
return player.apply_hurt(final, source);
|
||||
if (!player.apply_hurt(final, source)) return false;
|
||||
this.mods.after.player_damage.emit({ player: before.player, amount: final, cause: source.cause });
|
||||
return true;
|
||||
}
|
||||
|
||||
heal_player(player: ServerPlayer, amount: number) {
|
||||
|
||||
@@ -48,6 +48,7 @@ import { Menu, type MenuSlot } from "./menu.ts";
|
||||
import type { Entity } from "./entity/entity.ts";
|
||||
import { create_entity } from "./entity/entity_types.ts";
|
||||
import { attribute_type, type AttributeType, MODIFIER_OPERATIONS } from "./entity/attributes.ts";
|
||||
import { effect_type, EffectInstance, type EffectType } from "./entity/effects.ts";
|
||||
|
||||
// a list of handlers that can't break each other: one throwing is logged under its mod and the rest still run
|
||||
export class Signal<T> {
|
||||
@@ -126,6 +127,7 @@ export class ModRuntime {
|
||||
chat_send: new Signal(),
|
||||
player_join: new Signal(),
|
||||
player_leave: new Signal(),
|
||||
player_damage: new Signal(),
|
||||
player_death: new Signal(),
|
||||
server_start: new Signal(),
|
||||
tick: new Signal(),
|
||||
@@ -932,7 +934,7 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
game.send_chat(player, String(text));
|
||||
},
|
||||
set_worn(key, items) {
|
||||
if (typeof key !== "string" || key.length === 0) throw new Error("set_worn needs a key, like \"mymod:hats\"");
|
||||
if (typeof key !== "string" || key.length === 0) throw new Error('set_worn needs a key, like "mymod:hats"');
|
||||
if (!Array.isArray(items)) throw new Error("set_worn needs a list of items, [] for none");
|
||||
if (items.length === 0) player.worn.delete(key);
|
||||
else player.worn.set(key, items.map(new_stack));
|
||||
@@ -984,9 +986,31 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
},
|
||||
remove_modifier: (attribute, id) =>
|
||||
player.attributes.get(known_attribute(attribute))?.remove_modifier(id) ?? false,
|
||||
add_effect(id, duration, options = {}) {
|
||||
const type = known_effect(id);
|
||||
if (!Number.isInteger(duration) || (duration < 1 && duration !== -1)) {
|
||||
throw new Error("an effect's duration is a whole number of ticks, or -1 for forever");
|
||||
}
|
||||
const amplifier = options.amplifier ?? 0;
|
||||
if (!Number.isInteger(amplifier) || amplifier < 0) {
|
||||
throw new Error("amplifier must be a whole number of 0 or more");
|
||||
}
|
||||
player.add_effect(new EffectInstance(type, duration, { ...options, amplifier }));
|
||||
},
|
||||
effect(id) {
|
||||
const effect = player.get_effect(known_effect(id));
|
||||
return effect && { amplifier: effect.amplifier, duration: effect.duration };
|
||||
},
|
||||
remove_effect: (id) => player.remove_effect(known_effect(id)),
|
||||
};
|
||||
}
|
||||
|
||||
function known_effect(id: string): EffectType {
|
||||
const type = effect_type(id);
|
||||
if (!type) throw new Error(`unknown effect ${id}`);
|
||||
return type;
|
||||
}
|
||||
|
||||
function known_attribute(id: string): AttributeType {
|
||||
const type = attribute_type(id);
|
||||
if (!type) throw new Error(`unknown attribute ${id}`);
|
||||
|
||||
+27
-1
@@ -4,6 +4,7 @@ import { assert, assertEquals } from "@std/assert";
|
||||
import { PLAYER_MENU_EXTRA, PROTOCOL_VERSION } from "$/common/protocol.ts";
|
||||
import { copy_dir, test_game } from "./helpers.ts";
|
||||
import type { WorldSave } from "$/server/game/saves.ts";
|
||||
import { EFFECTS } from "$/server/game/entity/effects.ts";
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
type Msg = any;
|
||||
@@ -131,7 +132,7 @@ Deno.test("the giant chestplate goes in the body slot and gives 20 more health w
|
||||
assertEquals(curios_items(t.take(1))[body], {
|
||||
id: "bworld:giant_chestplate",
|
||||
count: 1,
|
||||
lore: "When worn:\n+20 Max Health",
|
||||
lore: "When worn:\n+20 Max Health\nAfter 10s unhurt: Regeneration II",
|
||||
});
|
||||
assertEquals(alice.max_health, 40);
|
||||
|
||||
@@ -155,6 +156,31 @@ Deno.test("the giant chestplate goes in the body slot and gives 20 more health w
|
||||
assertEquals(t.game.players()[0].health, 20);
|
||||
});
|
||||
|
||||
Deno.test("the giant chestplate gives regeneration II after 10 seconds without being hurt", async () => {
|
||||
const t = await setup();
|
||||
t.send(1, { type: "chat", text: "/give bworld:giant_chestplate" });
|
||||
const slot = last_container(t.take(1), "inventory").findIndex((i: Msg) => i?.id === "bworld:giant_chestplate");
|
||||
const alice = t.game.players()[0];
|
||||
t.send(1, { type: "click", menu: 0, slot, button: 0, shift: true });
|
||||
t.game.damage_player(alice, 2, "test");
|
||||
const regeneration = () => alice.get_effect(EFFECTS.regeneration);
|
||||
|
||||
for (let i = 0; i < 199; i++) t.game.tick();
|
||||
assertEquals(regeneration(), undefined);
|
||||
t.game.tick();
|
||||
assertEquals(regeneration()?.amplifier, 1);
|
||||
// it keeps being topped up while they stay unhurt
|
||||
for (let i = 0; i < 200; i++) t.game.tick();
|
||||
assertEquals(regeneration()?.amplifier, 1);
|
||||
|
||||
// being hurt starts the 10 seconds again, what they were given runs out
|
||||
t.game.damage_player(alice, 2, "test");
|
||||
for (let i = 0; i < 199; i++) t.game.tick();
|
||||
assertEquals(regeneration(), undefined);
|
||||
t.game.tick();
|
||||
assertEquals(regeneration()?.amplifier, 1);
|
||||
});
|
||||
|
||||
Deno.test("everyone sees what a player wears that has a worn model, and players joining later too", async () => {
|
||||
const t = await setup();
|
||||
const alice = t.game.players()[0].id;
|
||||
|
||||
Reference in New Issue
Block a user