// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // a living entity's stats, like minecraft's attributes: a base value that modifiers change without touching it. // numbers are minecraft's, so movement_speed is its per tick value, see LivingEntity.travel export type ModifierOperation = "add_value" | "add_multiplied_base" | "add_multiplied_total"; export const MODIFIER_OPERATIONS: ModifierOperation[] = ["add_value", "add_multiplied_base", "add_multiplied_total"]; export interface AttributeModifier { // unique on the attribute, so it can be replaced or taken off, like "bworld:effect.speed" id: string; amount: number; operation: ModifierOperation; } export interface AttributeType { id: string; default: number; min: number; max: number; } function attribute(id: string, value: number, min: number, max: number): AttributeType { return { id: `cubi:${id}`, default: value, min, max }; } export const ATTRIBUTES = { max_health: attribute("max_health", 20, 1, 1024), // protection from physical and from arcane damage physical_shielding: attribute("physical_shielding", 0, 0, 1024), arcane_shielding: attribute("arcane_shielding", 0, 0, 1024), knockback_resistance: attribute("knockback_resistance", 0, 0, 1), movement_speed: attribute("movement_speed", 0.7, 0, 1024), attack_damage: attribute("attack_damage", 2, 0, 2048), attack_knockback: attribute("attack_knockback", 0, 0, 5), // full strength attacks a second, see ServerPlayer.attack_strength attack_speed: attribute("attack_speed", 4, 0, 1024), // how far mobs notice players from, in blocks follow_range: attribute("follow_range", 32, 0, 2048), // how fast a jump starts, blocks per tick jump_strength: attribute("jump_strength", 0.42, 0, 32), step_height: attribute("step_height", 0.6, 0, 10), // blocks per tick per tick gravity: attribute("gravity", 0.08, -1, 1), // blocks a fall can be without hurting safe_fall_distance: attribute("safe_fall_distance", 3, -1024, 1024), fall_damage_multiplier: attribute("fall_damage_multiplier", 1, 0, 100), // how far a player reaches, in blocks from their eyes: blocks they break and use, and entities they hit block_interaction_range: attribute("block_interaction_range", 6, 0, 64), entity_interaction_range: attribute("entity_interaction_range", 3, 0, 64), } satisfies Record; const BY_ID = new Map(Object.values(ATTRIBUTES).map((type) => [type.id, type])); export function attribute_type(id: string): AttributeType | undefined { return BY_ID.get(id); } // what level.json keeps of an attribute, see AttributeMap.save export interface SavedAttribute { id: string; base: number; modifiers?: AttributeModifier[]; } export class Attribute { readonly type: AttributeType; #base: number; #modifiers = new Map(); #value: number | undefined; constructor(type: AttributeType, base = type.default) { this.type = type; this.#base = base; } get base() { return this.#base; } set base(value: number) { this.#base = value; this.#value = undefined; } get modifiers(): AttributeModifier[] { return [...this.#modifiers.values()]; } has_modifier(id: string) { return this.#modifiers.has(id); } // replaces one with the same id add_modifier(modifier: AttributeModifier) { this.#modifiers.set(modifier.id, { ...modifier }); this.#value = undefined; } remove_modifier(id: string) { this.#value = undefined; return this.#modifiers.delete(id); } // like minecraft: X = base, plus every add_value. Y = X, plus X * amount for every add_multiplied_base, then // times 1 + amount for every add_multiplied_total. clamped to the attribute's range get value(): number { if (this.#value === undefined) { const modifiers = this.modifiers; const of = (operation: ModifierOperation) => modifiers.filter((m) => m.operation === operation); let x = this.#base; for (const modifier of of("add_value")) x += modifier.amount; let y = x; for (const modifier of of("add_multiplied_base")) y += x * modifier.amount; for (const modifier of of("add_multiplied_total")) y *= 1 + modifier.amount; this.#value = Math.min(this.type.max, Math.max(this.type.min, y)); } return this.#value; } } // the attributes one entity has. which ones is up to its kind, like minecraft's default attribute suppliers export class AttributeMap { #attributes = new Map(); // the types it has, with the kind's base values where they aren't the default constructor(types: [AttributeType, number?][]) { for (const [type, base] of types) { this.#attributes.set(type.id, new Attribute(type, base)); } } has(type: AttributeType) { return this.#attributes.has(type.id); } get(type: AttributeType): Attribute | undefined { return this.#attributes.get(type.id); } // the default for attributes it doesn't have value(type: AttributeType): number { return this.#attributes.get(type.id)?.value ?? type.default; } all(): Attribute[] { return [...this.#attributes.values()]; } // only what isn't the kind's defaults: changed bases and any modifiers save(defaults: AttributeMap): SavedAttribute[] { const saved: SavedAttribute[] = []; for (const attribute of this.#attributes.values()) { const modifiers = attribute.modifiers; if (attribute.base === defaults.get(attribute.type)?.base && modifiers.length === 0) continue; saved.push( modifiers.length ? { id: attribute.type.id, base: attribute.base, modifiers } : { id: attribute.type.id, base: attribute.base, }, ); } return saved; } // attributes it doesn't have, or that don't exist anymore, are skipped load(saved: unknown) { if (!Array.isArray(saved)) return; for (const entry of saved as SavedAttribute[]) { const type = attribute_type(entry?.id); const attribute = type && this.#attributes.get(type.id); if (!attribute) continue; if (typeof entry.base === "number" && Number.isFinite(entry.base)) attribute.base = entry.base; for (const modifier of Array.isArray(entry.modifiers) ? entry.modifiers : []) { if ( typeof modifier?.id === "string" && typeof modifier.amount === "number" && MODIFIER_OPERATIONS.includes(modifier.operation) ) { attribute.add_modifier(modifier); } } } } }