// 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 type { EffectCategory } from "$/common/mod_data.ts"; import type { AttributeType, ModifierOperation } from "./attributes.ts"; import { finite } from "./entity.ts"; import type { LivingEntity } from "./living_entity.ts"; // a kind of effect: a mod's effect json with the components it lists, see ModRuntime.finish_setup. the base game's // are in mods/bworld/effects export interface EffectType { id: string; category: EffectCategory; // 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 }[]; // each tick it's on, and once for instant ones, with ticks undefined. ticks is what's left, or how long it's been // on for ones that last forever. returning false takes the effect off on_tick?(entity: LivingEntity, ticks: number | undefined, amplifier: number): boolean | void; } const registry = new Map(); // every effect mods define, replacing the ones before export function set_effect_types(types: EffectType[]) { registry.clear(); for (const type of types) registry.set(type.id, type); } export function effect_type(id: string): EffectType | undefined { return registry.get(id); } export function effect_type_ids(): string[] { return [...registry.keys()]; } // 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.on_tick?.(entity, count, 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; } }