// status effects on living entities, like minecraft's MobEffect and MobEffectInstance. an effect is a type plus // how strong it is and for how long. a weaker one that lasts longer waits hidden under the stronger one, and takes // over when that runs out import { ATTRIBUTES, type AttributeType, type ModifierOperation } from "./attributes.ts"; import { DAMAGE, finite } from "./entity.ts"; import type { LivingEntity } from "./living_entity.ts"; export interface EffectType { id: string; beneficial: boolean; // happens once when added instead of lasting, like instant health instant?: boolean; // while it's on. amount is for level 1 and goes up with each level, like minecraft modifiers?: { attribute: AttributeType; amount: number; operation: ModifierOperation }[]; // whether on_tick runs this tick, from the ticks left (or its age for infinite ones) should_tick?(duration: number, amplifier: number): boolean; // returning false takes the effect off on_tick?(entity: LivingEntity, amplifier: number): boolean | void; apply_instant?(entity: LivingEntity, amplifier: number): void; on_added?(entity: LivingEntity, amplifier: number): void; on_removed?(entity: LivingEntity, amplifier: number): void; } // every n ticks, n halving with each level: minecraft's regeneration, poison and wither const every = (ticks: number) => (duration: number, amplifier: number) => { const interval = ticks >> amplifier; return interval > 0 ? duration % interval === 0 : true; }; function effect(id: string, beneficial: boolean, rest: Omit = {}): EffectType { return { id: `bworld:${id}`, beneficial, ...rest }; } export const EFFECTS = { speed: effect("speed", true, { modifiers: [{ attribute: ATTRIBUTES.movement_speed, amount: 0.2, operation: "add_multiplied_total" }], }), slowness: effect("slowness", false, { modifiers: [{ attribute: ATTRIBUTES.movement_speed, amount: -0.15, operation: "add_multiplied_total" }], }), strength: effect("strength", true, { modifiers: [{ attribute: ATTRIBUTES.attack_damage, amount: 3, operation: "add_value" }], }), weakness: effect("weakness", false, { modifiers: [{ attribute: ATTRIBUTES.attack_damage, amount: -4, operation: "add_value" }], }), jump_boost: effect("jump_boost", true, { modifiers: [{ attribute: ATTRIBUTES.safe_fall_distance, amount: 1, operation: "add_value" }], }), health_boost: effect("health_boost", true, { modifiers: [{ attribute: ATTRIBUTES.max_health, amount: 4, operation: "add_value" }], on_removed: (entity) => entity.health = Math.min(entity.health, entity.max_health), }), regeneration: effect("regeneration", true, { should_tick: every(50), on_tick: (entity) => { if (entity.health < entity.max_health) entity.heal(1); }, }), poison: effect("poison", false, { should_tick: every(25), on_tick: (entity) => { // never kills if (entity.health > 1) entity.hurt(1, DAMAGE.magic); }, }), wither: effect("wither", false, { should_tick: every(40), on_tick: (entity) => void entity.hurt(1, DAMAGE.wither), }), instant_health: effect("instant_health", true, { instant: true, apply_instant: (entity, amplifier) => entity.heal(Math.max(4 << amplifier, 0)), }), instant_damage: effect("instant_damage", false, { instant: true, apply_instant: (entity, amplifier) => void entity.hurt(Math.max(6 << amplifier, 0), DAMAGE.magic), }), // these do their thing where it happens: LivingEntity's damage, air and travel, and synced data resistance: effect("resistance", true), fire_resistance: effect("fire_resistance", true), water_breathing: effect("water_breathing", true), slow_falling: effect("slow_falling", true), glowing: effect("glowing", false), } satisfies Record; const registry = new Map(Object.values(EFFECTS).map((type) => [type.id, type])); // adds an effect type, or replaces one with the same id export function register_effect(type: EffectType) { registry.set(type.id, type); } export function effect_type(id: string): EffectType | undefined { return registry.get(id); } // what level.json keeps of an effect, the doc's active_effects entries export interface SavedEffect { id: string; amplifier?: number; duration: number; ambient?: boolean; show_particles?: boolean; show_icon?: boolean; // without its id, it's the same effect hidden_effect?: Omit; } export interface EffectOptions { // level minus 1 amplifier?: number; // from a beacon, particles are fainter ambient?: boolean; show_particles?: boolean; show_icon?: boolean; } export class EffectInstance { readonly type: EffectType; // ticks left, -1 for forever duration: number; amplifier: number; ambient: boolean; show_particles: boolean; show_icon: boolean; // the same effect, weaker but longer, waiting for this one to run out hidden: EffectInstance | undefined; constructor(type: EffectType, duration: number, options: EffectOptions = {}) { this.type = type; this.duration = duration; this.amplifier = options.amplifier ?? 0; this.ambient = options.ambient ?? false; this.show_particles = options.show_particles ?? true; this.show_icon = options.show_icon ?? true; } get infinite() { return this.duration === -1; } get has_time_left() { return this.infinite || this.duration > 0; } copy(): EffectInstance { const copy = new EffectInstance(this.type, this.duration, this); copy.hidden = this.hidden?.copy(); return copy; } #shorter_than(other: EffectInstance) { return !this.infinite && (this.duration < other.duration || other.infinite); } #take_details(other: EffectInstance) { this.duration = other.duration; this.amplifier = other.amplifier; this.ambient = other.ambient; this.show_particles = other.show_particles; this.show_icon = other.show_icon; } // the same effect added again, like minecraft's MobEffectInstance.update. returns whether this one changed, // a longer weaker one only goes into hidden update(other: EffectInstance): boolean { let changed = false; if (other.amplifier > this.amplifier) { if (other.#shorter_than(this)) { const old_hidden = this.hidden; this.hidden = this.copy(); this.hidden.hidden = old_hidden; } this.amplifier = other.amplifier; this.duration = other.duration; changed = true; } else if (this.#shorter_than(other)) { if (other.amplifier === this.amplifier) { this.duration = other.duration; changed = true; } else if (!this.hidden) { this.hidden = other.copy(); } else { this.hidden.update(other); } } if ((!other.ambient && this.ambient) || changed) { this.ambient = other.ambient; changed = true; } if (other.show_particles !== this.show_particles) { this.show_particles = other.show_particles; changed = true; } if (other.show_icon !== this.show_icon) { this.show_icon = other.show_icon; changed = true; } return changed; } // one tick: does what it does, counts down, and lets the hidden one take over when it's done. returns "ended", // "changed" when a hidden one took over, or undefined tick(entity: LivingEntity): "ended" | "changed" | undefined { if (!this.has_time_left) return "ended"; const count = this.infinite ? entity.age : this.duration; if (this.type.should_tick?.(count, this.amplifier) && this.type.on_tick?.(entity, this.amplifier) === false) { return "ended"; } this.#count_down(); if (this.duration === 0 && this.hidden) { this.#take_details(this.hidden); this.hidden = this.hidden.hidden; return "changed"; } return this.has_time_left ? undefined : "ended"; } #count_down() { if (this.hidden) this.hidden.#count_down(); if (!this.infinite) this.duration = Math.max(0, this.duration - 1); } save(): SavedEffect { return { id: this.type.id, ...this.#save_details() }; } #save_details(): Omit { const saved: Omit = { duration: this.duration }; if (this.amplifier) saved.amplifier = this.amplifier; if (this.ambient) saved.ambient = true; if (!this.show_particles) saved.show_particles = false; if (!this.show_icon) saved.show_icon = false; if (this.hidden) saved.hidden_effect = this.hidden.#save_details(); return saved; } // undefined for effects that don't exist anymore static load(saved: SavedEffect): EffectInstance | undefined { const type = effect_type(saved?.id); return type && EffectInstance.#load_details(type, saved); } static #load_details(type: EffectType, saved: Omit): EffectInstance { const effect = new EffectInstance(type, finite(saved.duration, 0), { amplifier: finite(saved.amplifier, 0), ambient: saved.ambient === true, show_particles: saved.show_particles !== false, show_icon: saved.show_icon !== false, }); if (typeof saved.hidden_effect === "object" && saved.hidden_effect !== null) { effect.hidden = EffectInstance.#load_details(type, saved.hidden_effect); } return effect; } }