Move effects into the modding api
This commit is contained in:
@@ -1,70 +1,31 @@
|
||||
// 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 { 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;
|
||||
beneficial: boolean;
|
||||
category: EffectCategory;
|
||||
// happens once when added instead of lasting, like instant health
|
||||
instant?: boolean;
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
// every n ticks, n halving with each level: minecraft's regeneration and poison
|
||||
const every = (ticks: number) => (duration: number, amplifier: number) => {
|
||||
const interval = ticks >> amplifier;
|
||||
return interval > 0 ? duration % interval === 0 : true;
|
||||
};
|
||||
const registry = new Map<string, EffectType>();
|
||||
|
||||
function effect(id: string, beneficial: boolean, rest: Omit<EffectType, "id" | "beneficial"> = {}): 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" }],
|
||||
}),
|
||||
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);
|
||||
},
|
||||
}),
|
||||
instant_health: effect("instant_health", true, {
|
||||
instant: true,
|
||||
apply_instant: (entity, amplifier) => entity.heal(Math.max(4 << amplifier, 0)),
|
||||
}),
|
||||
// these do their thing where it happens: LivingEntity's damage and air
|
||||
resistance: effect("resistance", true),
|
||||
water_breathing: effect("water_breathing", true),
|
||||
} satisfies Record<string, EffectType>;
|
||||
|
||||
const registry = new Map<string, EffectType>(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);
|
||||
// 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 {
|
||||
@@ -181,9 +142,7 @@ export class EffectInstance {
|
||||
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";
|
||||
}
|
||||
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);
|
||||
|
||||
@@ -37,7 +37,7 @@ export interface DamageSource {
|
||||
// who did it, which knockback pushes away from
|
||||
attacker?: Attacker;
|
||||
by_player?: boolean;
|
||||
// fire resistance stops it
|
||||
// it's fire, like burning
|
||||
fire?: boolean;
|
||||
// even invulnerable entities are hurt, like falling out of the world
|
||||
bypasses_invulnerability?: boolean;
|
||||
@@ -52,6 +52,12 @@ export const DAMAGE = {
|
||||
magic: { cause: "magic", type: "arcane" },
|
||||
} satisfies Record<string, DamageSource>;
|
||||
|
||||
// a cause from a script, like "magic": the engine's source for it, or physical damage under that name
|
||||
export function damage_source(cause: string): DamageSource {
|
||||
const known: Record<string, DamageSource> = DAMAGE;
|
||||
return known[cause] ?? { cause, type: "physical" };
|
||||
}
|
||||
|
||||
// what entities can see and do in the world, the entity manager is one
|
||||
export interface EntityLevel {
|
||||
readonly current_tick: number;
|
||||
|
||||
@@ -5,7 +5,7 @@ import { TICKS_PER_SECOND } from "$/common/constants.ts";
|
||||
import { type ItemData, ItemStack } from "$/common/inventory.ts";
|
||||
import type { EntityData } from "$/common/protocol.ts";
|
||||
import { AttributeMap, ATTRIBUTES, type AttributeType } from "./attributes.ts";
|
||||
import { EffectInstance, EFFECTS, type EffectType, type SavedEffect } from "./effects.ts";
|
||||
import { EffectInstance, type SavedEffect } from "./effects.ts";
|
||||
import { DAMAGE, type DamageSource, Entity, finite, type SavedEntity } from "./entity.ts";
|
||||
|
||||
export const EQUIPMENT_SLOTS = ["head", "chest", "legs", "feet", "mainhand", "offhand", "body", "saddle"] as const;
|
||||
@@ -28,6 +28,9 @@ const SWIM_UP = 0.04;
|
||||
// a jump can't start more often than this while jump is held
|
||||
const JUMP_DELAY = 10;
|
||||
const KNOCKBACK = 0.4;
|
||||
// the base game's effects that work where what they change happens, like minecraft's: in damage and air
|
||||
const RESISTANCE = "bworld:resistance";
|
||||
const WATER_BREATHING = "bworld:water_breathing";
|
||||
|
||||
export interface SavedLivingEntity extends SavedEntity {
|
||||
health?: number;
|
||||
@@ -135,7 +138,7 @@ export abstract class LivingEntity extends Entity {
|
||||
override base_tick() {
|
||||
super.base_tick();
|
||||
if (this.alive) {
|
||||
if (this.eyes_in_water && !this.can_breathe_underwater && !this.has_effect(EFFECTS.water_breathing)) {
|
||||
if (this.eyes_in_water && !this.can_breathe_underwater && !this.has_effect(WATER_BREATHING)) {
|
||||
this.air -= 1;
|
||||
if (this.air <= -20) {
|
||||
this.air = 0;
|
||||
@@ -314,7 +317,7 @@ export abstract class LivingEntity extends Entity {
|
||||
return;
|
||||
}
|
||||
|
||||
const resistance = this.get_effect(EFFECTS.resistance);
|
||||
const resistance = this.get_effect(RESISTANCE);
|
||||
if (resistance && !source.bypasses_invulnerability) {
|
||||
amount = Math.max(0, amount * (25 - (resistance.amplifier + 1) * 5) / 25);
|
||||
}
|
||||
@@ -387,12 +390,12 @@ export abstract class LivingEntity extends Entity {
|
||||
|
||||
// effects
|
||||
|
||||
has_effect(type: EffectType) {
|
||||
return this.effects.has(type.id);
|
||||
has_effect(id: string) {
|
||||
return this.effects.has(id);
|
||||
}
|
||||
|
||||
get_effect(type: EffectType): EffectInstance | undefined {
|
||||
return this.effects.get(type.id);
|
||||
get_effect(id: string): EffectInstance | undefined {
|
||||
return this.effects.get(id);
|
||||
}
|
||||
|
||||
// whether it's affected by that effect at all, like undead and poison
|
||||
@@ -404,14 +407,13 @@ export abstract class LivingEntity extends Entity {
|
||||
add_effect(effect: EffectInstance): boolean {
|
||||
if (!this.alive || !this.can_be_affected(effect)) return false;
|
||||
if (effect.type.instant) {
|
||||
effect.type.apply_instant?.(this, effect.amplifier);
|
||||
effect.type.on_tick?.(this, undefined, effect.amplifier);
|
||||
return true;
|
||||
}
|
||||
const current = this.effects.get(effect.type.id);
|
||||
if (!current) {
|
||||
this.effects.set(effect.type.id, effect);
|
||||
this.#apply_modifiers(effect);
|
||||
effect.type.on_added?.(this, effect.amplifier);
|
||||
return true;
|
||||
}
|
||||
const amplifier = current.amplifier;
|
||||
@@ -420,16 +422,16 @@ export abstract class LivingEntity extends Entity {
|
||||
return changed;
|
||||
}
|
||||
|
||||
remove_effect(type: EffectType) {
|
||||
const effect = this.effects.get(type.id);
|
||||
remove_effect(id: string) {
|
||||
const effect = this.effects.get(id);
|
||||
if (!effect) return false;
|
||||
this.effects.delete(type.id);
|
||||
this.effects.delete(id);
|
||||
this.#effect_ended(effect);
|
||||
return true;
|
||||
}
|
||||
|
||||
clear_effects() {
|
||||
for (const effect of [...this.effects.values()]) this.remove_effect(effect.type);
|
||||
for (const effect of [...this.effects.values()]) this.remove_effect(effect.type.id);
|
||||
}
|
||||
|
||||
tick_effects() {
|
||||
@@ -439,7 +441,7 @@ export abstract class LivingEntity extends Entity {
|
||||
if (this.effects.get(effect.type.id) !== effect) continue;
|
||||
const result = effect.tick(this);
|
||||
if (result === "ended") {
|
||||
this.remove_effect(effect.type);
|
||||
this.remove_effect(effect.type.id);
|
||||
} else if (result === "changed") {
|
||||
this.#apply_modifiers(effect);
|
||||
}
|
||||
@@ -447,7 +449,7 @@ export abstract class LivingEntity extends Entity {
|
||||
}
|
||||
|
||||
#apply_modifiers(effect: EffectInstance) {
|
||||
for (const { attribute, amount, operation } of effect.type.modifiers ?? []) {
|
||||
for (const { attribute, amount, operation } of effect.type.modifiers) {
|
||||
this.attributes.get(attribute)?.add_modifier({
|
||||
id: effect.type.id,
|
||||
amount: amount * (effect.amplifier + 1),
|
||||
@@ -457,10 +459,9 @@ export abstract class LivingEntity extends Entity {
|
||||
}
|
||||
|
||||
#effect_ended(effect: EffectInstance) {
|
||||
for (const { attribute } of effect.type.modifiers ?? []) {
|
||||
for (const { attribute } of effect.type.modifiers) {
|
||||
this.attributes.get(attribute)?.remove_modifier(effect.type.id);
|
||||
}
|
||||
effect.type.on_removed?.(this, effect.amplifier);
|
||||
// back inside what the attributes allow now
|
||||
this.health = this.#health;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user