Entity scripts api

This commit is contained in:
2026-11-21 10:56:59 -03:00
parent 60a7cae86e
commit e1c2b13d1f
2 changed files with 94 additions and 1 deletions
+92
View File
@@ -46,6 +46,8 @@ export interface ServerContext {
readonly config: any;
components: ComponentRegistry; // only during setup
commands: CommandRegistry; // only during setup
// mob behaviors, see docs/modding/entities/index.md. register_behavior only during setup
entities: EntityApi;
events: { before: ServerBeforeEvents; after: ServerAfterEvents };
system: System;
world: ServerWorld;
@@ -186,6 +188,96 @@ export interface ComponentRegistry {
register_effect<P>(id: Id, component: EffectComponent<P>): void;
}
// mob behaviors, like neoforge's Mob.registerGoals: an entity whose json has "behavior": "my_mod:thing" gets its goals
// from the function registered under that id
export interface EntityApi {
// runs once for each mob with this behavior, before its first thought. what the mob has to remember goes in the
// function's closures, one set per mob. only during setup
register_behavior(id: Id, behavior: (mob: MobRef, goals: MobGoals) => void): void;
// the engine's own goals, to add next to a mod's
readonly goals: BuiltinGoals;
}
// the controls a goal needs while it runs. a goal only starts once every flag it has is free, or held by a less
// important goal that can be interrupted, which then stops. "target" is for goals that pick who the mob is after
export type GoalFlag = "move" | "look" | "jump" | "target";
// something a mob does, like minecraft's Goal. every tick, running goals that can't continue stop, then goals that
// can start do, most important first, then every running goal ticks
export interface ScriptGoal {
flags?: GoalFlag[];
// whether it wants to start
can_use(): boolean;
// whether it wants to keep going, can_use when not given
can_continue_to_use?(): boolean;
// whether a more important goal can take its flags, true when not given
is_interruptable?(): boolean;
start?(): void;
stop?(): void;
tick?(): void;
}
// what a behavior adds its goals to. a lower priority number is more important
export interface MobGoals {
// what it does
add(priority: number, goal: ScriptGoal | BuiltinGoal): void;
// who it goes after, like minecraft's targetSelector. these set mob.target
add_target(priority: number, goal: ScriptGoal | BuiltinGoal): void;
}
// one of the engine's goals, made by BuiltinGoals and built for the mob when it's added
export interface BuiltinGoal {
readonly kind: string;
}
// the goals animal and monster behaviors are made of. speeds multiply the mob's movement_speed
export interface BuiltinGoals {
// swims up in water so it doesn't drown
float(): BuiltinGoal;
// wanders to a random spot within range every interval ticks or so
random_stroll(options?: { speed?: number; interval?: number; range?: number }): BuiltinGoal;
// runs around after being hurt or while burning
panic(options?: { speed?: number }): BuiltinGoal;
// looks at a player within range for a couple of seconds, with probability each tick
look_at_player(options?: { range?: number; probability?: number }): BuiltinGoal;
random_look_around(): BuiltinGoal;
// walks to its target and hits it with its attack_damage, at most once every interval ticks
melee_attack(options?: { speed?: number; interval?: number }): BuiltinGoal;
// target goals: whoever hurt it last, and the nearest player within its follow range
hurt_by_target(): BuiltinGoal;
nearest_attackable_player(): BuiltinGoal;
}
// a mob, the way its behavior's goals see it. refs are made fresh, so compare them by id
export interface MobRef extends LivingRef {
readonly type: Id;
readonly position: Readonly<Position>;
// where it sees from and which way it looks, a vector 1 long, like Player's
readonly eye_position: Readonly<Position>;
readonly look_direction: Readonly<Position>;
// in blocks a second
readonly velocity: Readonly<Position>;
readonly on_ground: boolean;
readonly in_water: boolean;
// who it's after, set by target goals or by a script. undefined once they die or leave
target: Player | MobRef | undefined;
// who hurt it last
readonly last_attacker: Player | MobRef | undefined;
// a number from 0 to 1, from the mob's own random, which tests replace
random(): number;
// walks toward x y z until it's there or stop is called. speed multiplies its movement_speed
move_to(x: number, y: number, z: number, speed?: number): void;
stop(): void;
// turns its head, and its body after it, toward a point. it stops after a couple of ticks, so a goal that keeps
// looking at something calls this every tick
look_at(x: number, y: number, z: number): void;
// this tick: jumps if it's on the ground, swims up if it's in water
jump(): void;
// swings its arm and hits target with its attack_damage, wherever target is
attack(target: Player | MobRef): void;
}
// commands, brigadier's way like neoforge's: see common/commands.ts and docs/modding/misc/commands.md
// who ran a command
+2 -1
View File
@@ -7,8 +7,9 @@
import { ATTRIBUTES } from "./attributes.ts";
import type { LivingEntity } from "./living_entity.ts";
import type { Mob } from "./mob.ts";
import type { GoalFlag } from "@/common/mod_api/server.ts";
export type GoalFlag = "move" | "look" | "jump" | "target";
export type { GoalFlag };
export abstract class Goal {
flags: GoalFlag[] = [];