Files
bworld/server/game/entity/attributes.ts
T
2026-09-28 15:12:06 -03:00

178 lines
5.7 KiB
TypeScript

// 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";
const 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: `bworld:${id}`, default: value, min, max };
}
export const ATTRIBUTES = {
max_health: attribute("max_health", 20, 1, 1024),
max_absorption: attribute("max_absorption", 0, 0, 2048),
armor: attribute("armor", 0, 0, 30),
armor_toughness: attribute("armor_toughness", 0, 0, 20),
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),
// 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),
} satisfies Record<string, AttributeType>;
const BY_ID = new Map<string, AttributeType>(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<string, AttributeModifier>();
#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<string, Attribute>();
// 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" &&
OPERATIONS.includes(modifier.operation)
) {
attribute.add_modifier(modifier);
}
}
}
}
}