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;
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ import { LivingEntity } from "./entity/living_entity.ts";
|
||||
import { EntityManager } from "./entity/entity_manager.ts";
|
||||
import { NaturalSpawner } from "./natural_spawner.ts";
|
||||
import { create_entity, entity_type_ids } from "./entity/entity_types.ts";
|
||||
import { DAMAGE, type DamageSource, type SavedEntity } from "./entity/entity.ts";
|
||||
import { damage_source, type DamageSource, type SavedEntity } from "./entity/entity.ts";
|
||||
import { ATTRIBUTES } from "./entity/attributes.ts";
|
||||
|
||||
// how far from a player's eyes a block can be changed, a bit more than the client's reach
|
||||
@@ -1320,8 +1320,7 @@ export class GameServer {
|
||||
|
||||
// hurts a player, killing them at 0. cause is for mods and the death message, like "fall"
|
||||
damage_player(player: ServerPlayer, amount: number, cause: string) {
|
||||
const known: Record<string, DamageSource> = DAMAGE;
|
||||
player.hurt(amount, known[cause] ?? { cause, type: "physical" });
|
||||
player.hurt(amount, damage_source(cause));
|
||||
}
|
||||
|
||||
// everything that hurts a player ends up here, see ServerPlayer.hurt. mods can change or cancel it first
|
||||
|
||||
@@ -8,6 +8,7 @@ import type { WorldSave } from "./saves.ts";
|
||||
import { ModRuntime } from "./mod_runtime.ts";
|
||||
import { register_entity_type } from "./entity/entity_types.ts";
|
||||
import { mod_entity_factory } from "./entity/mod_entity.ts";
|
||||
import { set_effect_types } from "./entity/effects.ts";
|
||||
|
||||
export interface ServerModSource {
|
||||
listing: ModListing;
|
||||
@@ -34,6 +35,8 @@ export async function start_game(
|
||||
const worldgen = await load_worldgen(worldgen_scripts, recipes.ores);
|
||||
|
||||
const runtime = new ModRuntime();
|
||||
// before the world loads, its mobs can have effects
|
||||
set_effect_types(runtime.effect_types());
|
||||
const game = new GameServer(host, save, default_seed, {
|
||||
listings: mods.map((mod) => mod.listing),
|
||||
recipes,
|
||||
|
||||
@@ -11,10 +11,12 @@ import type {
|
||||
CommandRegistry,
|
||||
CommandSource,
|
||||
Container,
|
||||
EffectComponent,
|
||||
EntityRef,
|
||||
EventSignal,
|
||||
ItemComponent,
|
||||
ItemStack,
|
||||
LivingRef,
|
||||
MenuBuilder,
|
||||
MenuHandle,
|
||||
MenuType,
|
||||
@@ -45,7 +47,9 @@ import { AIR_ID } from "$/common/protocol.ts";
|
||||
import type { GameServer } from "./game_server.ts";
|
||||
import type { ServerPlayer } from "./player.ts";
|
||||
import { Menu, type MenuSlot } from "./menu.ts";
|
||||
import type { Entity } from "./entity/entity.ts";
|
||||
import { damage_source, type Entity } from "./entity/entity.ts";
|
||||
import type { LivingEntity } from "./entity/living_entity.ts";
|
||||
import type { EffectJson } from "$/common/mod_data.ts";
|
||||
import { create_entity } from "./entity/entity_types.ts";
|
||||
import { attribute_type, type AttributeType, MODIFIER_OPERATIONS } from "./entity/attributes.ts";
|
||||
import { effect_type, EffectInstance, type EffectType } from "./entity/effects.ts";
|
||||
@@ -108,6 +112,7 @@ interface Timer {
|
||||
export class ModRuntime {
|
||||
block_components = new Map<string, { mod: string; component: BlockComponent }>();
|
||||
item_components = new Map<string, { mod: string; component: ItemComponent }>();
|
||||
effect_components = new Map<string, { mod: string; component: EffectComponent }>();
|
||||
// every command, the engine's and mods', see common/commands.ts
|
||||
commands = new CommandDispatcher<ServerCommandSource>();
|
||||
// who registered each command name: "engine" or a mod's id
|
||||
@@ -218,10 +223,66 @@ export class ModRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
// after every setup ran: registration closes and every component blocks and items use must exist
|
||||
// every effect mods define, from their json. they're made before scripts run, since the world loads first and
|
||||
// its mobs can have effects, so their components are looked up as they tick
|
||||
effect_types(): EffectType[] {
|
||||
return EverythingRegistry.entries<EffectJson>("effects").map(([id, json]) => {
|
||||
const modifiers = (json.modifiers ?? []).map(({ attribute, amount, operation }) => {
|
||||
const type = attribute_type(attribute);
|
||||
if (!type) {
|
||||
throw new ModLoadError(
|
||||
id.split(":")[0],
|
||||
`effect ${id} changes ${attribute}, which isn't an attribute`,
|
||||
);
|
||||
}
|
||||
return { attribute: type, amount, operation };
|
||||
});
|
||||
const components = Object.entries(json.components ?? {});
|
||||
const on_tick = (entity: LivingEntity, ticks: number | undefined, amplifier: number) => {
|
||||
let keep = true;
|
||||
for (const [component_id, params] of components) {
|
||||
const { mod, component } = this.effect_components.get(component_id)!;
|
||||
const due = ticks === undefined || !component.should_tick ||
|
||||
run_guarded(
|
||||
mod,
|
||||
`${component_id} should_tick`,
|
||||
() => component.should_tick!(ticks, amplifier, params),
|
||||
);
|
||||
if (!due || !component.on_tick) continue;
|
||||
const result = run_guarded(
|
||||
mod,
|
||||
`${component_id} on_tick`,
|
||||
() => component.on_tick!(living_ref(entity), amplifier, params),
|
||||
);
|
||||
if (result === false) keep = false;
|
||||
}
|
||||
return keep;
|
||||
};
|
||||
return {
|
||||
id,
|
||||
category: json.category ?? "neutral",
|
||||
instant: json.instant === true,
|
||||
modifiers,
|
||||
on_tick: components.length > 0 ? on_tick : undefined,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// after every setup ran: registration closes and every component blocks, items and effects use must exist
|
||||
finish_setup() {
|
||||
this.#setting_up = false;
|
||||
|
||||
for (const [id, effect] of EverythingRegistry.entries<EffectJson>("effects")) {
|
||||
for (const component of Object.keys(effect.components ?? {})) {
|
||||
if (!this.effect_components.has(component)) {
|
||||
throw new ModLoadError(
|
||||
id.split(":")[0],
|
||||
`effect ${id} uses component ${component}, which nothing registered`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const [id, block] of EverythingRegistry.entries<{ components?: Record<string, unknown> }>("blocks")) {
|
||||
for (const component of Object.keys(block.components ?? {})) {
|
||||
if (!this.block_components.has(component)) {
|
||||
@@ -416,6 +477,14 @@ export class ModRuntime {
|
||||
}
|
||||
this.item_components.set(id, { mod, component: component as ItemComponent });
|
||||
},
|
||||
register_effect: (id, component) => {
|
||||
setup_only("components");
|
||||
own(id, "component");
|
||||
if (this.effect_components.has(id)) {
|
||||
throw new ModLoadError(mod, `component ${id} is registered twice`);
|
||||
}
|
||||
this.effect_components.set(id, { mod, component: component as EffectComponent });
|
||||
},
|
||||
},
|
||||
commands: {
|
||||
register: (name_or_node: string | CommandNode<CommandSource>, command?: Command) => {
|
||||
@@ -998,10 +1067,10 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
player.add_effect(new EffectInstance(type, duration, { ...options, amplifier }));
|
||||
},
|
||||
effect(id) {
|
||||
const effect = player.get_effect(known_effect(id));
|
||||
const effect = player.get_effect(known_effect(id).id);
|
||||
return effect && { amplifier: effect.amplifier, duration: effect.duration };
|
||||
},
|
||||
remove_effect: (id) => player.remove_effect(known_effect(id)),
|
||||
remove_effect: (id) => player.remove_effect(known_effect(id).id),
|
||||
is_swimming: () => player.swimming,
|
||||
};
|
||||
}
|
||||
@@ -1018,6 +1087,26 @@ function known_attribute(id: string): AttributeType {
|
||||
return type;
|
||||
}
|
||||
|
||||
function living_ref(entity: LivingEntity): LivingRef {
|
||||
return {
|
||||
id: entity.id,
|
||||
get health() {
|
||||
return entity.health;
|
||||
},
|
||||
get max_health() {
|
||||
return entity.max_health;
|
||||
},
|
||||
damage(amount, cause = "script") {
|
||||
if (typeof amount !== "number" || !Number.isFinite(amount)) throw new Error(`damage must be a number`);
|
||||
entity.hurt(amount, damage_source(String(cause)));
|
||||
},
|
||||
heal(amount) {
|
||||
if (typeof amount !== "number" || !Number.isFinite(amount)) throw new Error(`heal must be a number`);
|
||||
entity.heal(amount);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function entity_ref(entity: Entity): EntityRef {
|
||||
return {
|
||||
id: entity.id,
|
||||
|
||||
@@ -103,6 +103,7 @@ function as_loaded_mod(file: string, bmod: Bmod): LoadedMod {
|
||||
spawns: entries(bmod.data.spawns),
|
||||
entities: entries(bmod.data.entities),
|
||||
entity_models: entries(bmod.data.entity_models),
|
||||
effects: entries(bmod.data.effects),
|
||||
tags: entries(bmod.data.tags),
|
||||
textures: [...bmod.textures.keys()],
|
||||
texture_files: new Map(),
|
||||
|
||||
Reference in New Issue
Block a user