diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index a991049..6c8a7f1 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -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
(id: Id, component: EffectComponent
): 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