144 lines
5.2 KiB
TypeScript
144 lines
5.2 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// mods' mob behaviors, see EntityApi in common/mod_api/server.ts: the goals scripts write, run by the same goal
|
|
// selectors as the engine's own, and the engine's goals made for scripts to add
|
|
import type { BuiltinGoal, BuiltinGoals, GoalFlag, MobGoals, ScriptGoal } from "@/common/mod_api/server.ts";
|
|
import {
|
|
FloatGoal,
|
|
Goal,
|
|
type GoalSelector,
|
|
HurtByTargetGoal,
|
|
LookAtPlayerGoal,
|
|
MeleeAttackGoal,
|
|
NearestAttackablePlayerGoal,
|
|
PanicGoal,
|
|
RandomLookAroundGoal,
|
|
RandomStrollGoal,
|
|
} from "./entity/goals.ts";
|
|
import type { Mob } from "./entity/mob.ts";
|
|
import { run_guarded } from "./mod_runtime.ts";
|
|
|
|
const GOAL_FLAGS: readonly GoalFlag[] = ["move", "look", "jump", "target"];
|
|
|
|
// a script's goal, with each call guarded. one that throws is dropped: it never runs again, so it doesn't log every
|
|
// tick, and the mob's other goals go on
|
|
class ScriptedGoal extends Goal {
|
|
#broken = false;
|
|
|
|
constructor(readonly mod: string, readonly behavior: string, readonly goal: ScriptGoal) {
|
|
super();
|
|
this.flags = [...(goal.flags ?? [])];
|
|
}
|
|
|
|
#call<T>(what: string, fn: () => T, fallback: T): T {
|
|
if (this.#broken) return fallback;
|
|
let done = false;
|
|
const result = run_guarded(this.mod, `${this.behavior} goal ${what}`, () => {
|
|
const result = fn();
|
|
done = true;
|
|
return result;
|
|
});
|
|
if (!done) this.#broken = true;
|
|
return done ? result as T : fallback;
|
|
}
|
|
|
|
can_use() {
|
|
return this.#call("can_use", () => Boolean(this.goal.can_use()), false);
|
|
}
|
|
|
|
override can_continue_to_use() {
|
|
const goal = this.goal;
|
|
return this.#call(
|
|
"can_continue_to_use",
|
|
() => Boolean(goal.can_continue_to_use ? goal.can_continue_to_use() : goal.can_use()),
|
|
false,
|
|
);
|
|
}
|
|
|
|
override is_interruptable() {
|
|
if (!this.goal.is_interruptable) return true;
|
|
return this.#call("is_interruptable", () => Boolean(this.goal.is_interruptable!()), true);
|
|
}
|
|
|
|
override start() {
|
|
if (this.goal.start) this.#call("start", () => this.goal.start!(), undefined);
|
|
}
|
|
|
|
override stop() {
|
|
if (this.goal.stop) this.#call("stop", () => this.goal.stop!(), undefined);
|
|
}
|
|
|
|
override tick() {
|
|
if (this.goal.tick) this.#call("tick", () => this.goal.tick!(), undefined);
|
|
}
|
|
}
|
|
|
|
// what makes each BuiltinGoal for a mob. one BuiltinGoal can be added to many mobs, each gets its own
|
|
const builders = new WeakMap<BuiltinGoal, (mob: Mob) => Goal>();
|
|
|
|
function builtin(kind: string, build: (mob: Mob) => Goal): BuiltinGoal {
|
|
const goal = Object.freeze({ kind });
|
|
builders.set(goal, build);
|
|
return goal;
|
|
}
|
|
|
|
// undefined leaves the goal's own default
|
|
function option(options: Record<string, unknown> | undefined, name: string): number | undefined {
|
|
const value = options?.[name];
|
|
if (value === undefined) return undefined;
|
|
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
|
|
throw new Error(`a goal's ${name} must be a number, 0 or more`);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
export const BUILTIN_GOALS = Object.freeze<BuiltinGoals>({
|
|
float: () => builtin("float", (mob) => new FloatGoal(mob)),
|
|
random_stroll(options) {
|
|
const [speed, interval, range] = ["speed", "interval", "range"].map((name) => option(options, name));
|
|
return builtin("random_stroll", (mob) => new RandomStrollGoal(mob, speed, interval, range));
|
|
},
|
|
panic(options) {
|
|
const speed = option(options, "speed");
|
|
return builtin("panic", (mob) => new PanicGoal(mob, speed));
|
|
},
|
|
look_at_player(options) {
|
|
const [range, probability] = ["range", "probability"].map((name) => option(options, name));
|
|
return builtin("look_at_player", (mob) => new LookAtPlayerGoal(mob, range, probability));
|
|
},
|
|
random_look_around: () => builtin("random_look_around", (mob) => new RandomLookAroundGoal(mob)),
|
|
melee_attack(options) {
|
|
const [speed, interval] = ["speed", "interval"].map((name) => option(options, name));
|
|
return builtin("melee_attack", (mob) => new MeleeAttackGoal(mob, speed, interval));
|
|
},
|
|
hurt_by_target: () => builtin("hurt_by_target", (mob) => new HurtByTargetGoal(mob)),
|
|
nearest_attackable_player: () =>
|
|
builtin("nearest_attackable_player", (mob) => new NearestAttackablePlayerGoal(mob)),
|
|
});
|
|
|
|
// what a behavior adds the mob's goals to
|
|
export function mob_goals(mod: string, behavior: string, mob: Mob): MobGoals {
|
|
const adder = (selector: GoalSelector) => (priority: number, goal: ScriptGoal | BuiltinGoal) => {
|
|
if (typeof priority !== "number" || !Number.isFinite(priority)) {
|
|
throw new Error("a goal's priority must be a number");
|
|
}
|
|
selector.add_goal(priority, engine_goal(mod, behavior, mob, goal));
|
|
};
|
|
return { add: adder(mob.goals), add_target: adder(mob.target_goals) };
|
|
}
|
|
|
|
function engine_goal(mod: string, behavior: string, mob: Mob, goal: ScriptGoal | BuiltinGoal): Goal {
|
|
const build = builders.get(goal as BuiltinGoal);
|
|
if (build) return build(mob);
|
|
const script_goal = goal as ScriptGoal;
|
|
if (typeof script_goal?.can_use !== "function") {
|
|
throw new Error("a goal needs a can_use function, or has to come from ctx.entities.goals");
|
|
}
|
|
const flags = script_goal.flags ?? [];
|
|
if (!Array.isArray(flags) || flags.some((flag) => !GOAL_FLAGS.includes(flag))) {
|
|
throw new Error(`a goal's flags must be some of ${GOAL_FLAGS.map((flag) => `"${flag}"`).join(", ")}`);
|
|
}
|
|
return new ScriptedGoal(mod, behavior, script_goal);
|
|
}
|