Files
bworld/server/game/entity/goals.ts
T
2026-11-21 23:07:42 -03:00

390 lines
11 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// what mobs decide to do, like minecraft's Goal and GoalSelector. each goal says when it wants to run and which
// controls it needs (its flags), and the most important goals that can run get them. a lower priority number is
// more important, and can take a flag from a less important goal that's running
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 };
// how often a mob going after its target searches for a new way to it, at most
const RECALCULATE_TICKS = 10;
export abstract class Goal {
flags: GoalFlag[] = [];
// whether it wants to start
abstract can_use(): boolean;
// whether it wants to keep going
can_continue_to_use() {
return this.can_use();
}
// a more important goal can take its flags
is_interruptable() {
return true;
}
start() {}
stop() {}
tick() {}
}
interface WrappedGoal {
priority: number;
goal: Goal;
running: boolean;
}
export class GoalSelector {
#goals: WrappedGoal[] = [];
// which running goal holds each flag
#locks = new Map<GoalFlag, WrappedGoal>();
// flags no goal can have, like "move" while ridden
disabled = new Set<GoalFlag>();
add_goal(priority: number, goal: Goal) {
this.#goals.push({ priority, goal, running: false });
this.#goals.sort((a, b) => a.priority - b.priority);
}
remove_goal(goal: Goal) {
for (const wrapped of this.#goals.filter((w) => w.goal === goal)) this.#stop(wrapped);
this.#goals = this.#goals.filter((w) => w.goal !== goal);
}
get running(): Goal[] {
return this.#goals.filter((w) => w.running).map((w) => w.goal);
}
tick() {
for (const wrapped of this.#goals) {
if (
wrapped.running &&
(wrapped.goal.flags.some((flag) => this.disabled.has(flag)) || !wrapped.goal.can_continue_to_use())
) {
this.#stop(wrapped);
}
}
for (const wrapped of this.#goals) {
if (wrapped.running || wrapped.goal.flags.some((flag) => this.disabled.has(flag))) continue;
const free = wrapped.goal.flags.every((flag) => {
const holder = this.#locks.get(flag);
return !holder || (holder.priority > wrapped.priority && holder.goal.is_interruptable());
});
if (!free || !wrapped.goal.can_use()) continue;
for (const flag of wrapped.goal.flags) {
const holder = this.#locks.get(flag);
if (holder) this.#stop(holder);
this.#locks.set(flag, wrapped);
}
wrapped.running = true;
wrapped.goal.start();
}
for (const wrapped of this.#goals) {
if (wrapped.running) wrapped.goal.tick();
}
}
#stop(wrapped: WrappedGoal) {
if (!wrapped.running) return;
wrapped.running = false;
wrapped.goal.stop();
for (const [flag, holder] of this.#locks) {
if (holder === wrapped) this.#locks.delete(flag);
}
}
}
// some goals most mobs use, like minecraft's
// swims up in water so it doesn't drown
export class FloatGoal extends Goal {
override flags: GoalFlag[] = ["jump"];
constructor(readonly mob: Mob) {
super();
}
can_use() {
return this.mob.in_water;
}
override tick() {
if (this.mob.random() < 0.8) this.mob.jump_control.jump();
}
}
// wanders to a random spot nearby every now and then, staying near home, along a path to it
export class RandomStrollGoal extends Goal {
override flags: GoalFlag[] = ["move"];
#wanted: { x: number; y: number; z: number } | undefined;
// interval is how many ticks it waits between strolls on average
constructor(readonly mob: Mob, readonly speed_modifier = 1, readonly interval = 120, readonly range = 10) {
super();
}
can_use() {
if (this.mob.random() * this.interval >= 1) return false;
this.#wanted = random_stand_position(this.mob, this.range, 7);
return this.#wanted !== undefined;
}
// until it gets there, or as close as it can
override can_continue_to_use() {
return this.mob.navigation.moving;
}
override start() {
const { x, y, z } = this.#wanted!;
this.mob.navigation.move_to(x, y, z, this.speed_modifier);
}
override stop() {
this.#wanted = undefined;
this.mob.navigation.stop();
}
}
// runs around fast after being hurt or while burning, like minecraft's PanicGoal
export class PanicGoal extends Goal {
override flags: GoalFlag[] = ["move"];
#wanted: { x: number; y: number; z: number } | undefined;
constructor(readonly mob: Mob, readonly speed_modifier = 1.25) {
super();
}
#should_panic() {
const mob = this.mob;
return mob.last_hurt_by_mob !== undefined || mob.last_hurt_by_player_memory_time > 0 || mob.is_on_fire;
}
can_use() {
if (!this.#should_panic()) return false;
this.#wanted = random_stand_position(this.mob, 5, 4);
return this.#wanted !== undefined;
}
override can_continue_to_use() {
return this.mob.navigation.moving;
}
override start() {
const { x, y, z } = this.#wanted!;
this.mob.navigation.move_to(x, y, z, this.speed_modifier);
}
override stop() {
this.#wanted = undefined;
this.mob.navigation.stop();
}
}
// looks at a nearby player for a couple of seconds, now and then
export class LookAtPlayerGoal extends Goal {
override flags: GoalFlag[] = ["look"];
#player: LivingEntity | undefined;
#ticks = 0;
constructor(readonly mob: Mob, readonly range = 8, readonly probability = 0.02) {
super();
}
can_use() {
if (this.mob.random() >= this.probability) return false;
this.#player = this.mob.level.nearest_player(this.mob.x, this.mob.y, this.mob.z, this.range);
return this.#player !== undefined;
}
override can_continue_to_use() {
const player = this.#player;
return player !== undefined && this.#ticks > 0 &&
Math.hypot(player.x - this.mob.x, player.y - this.mob.y, player.z - this.mob.z) <= this.range;
}
override start() {
this.#ticks = 40 + Math.floor(this.mob.random() * 40);
}
override stop() {
this.#player = undefined;
}
override tick() {
this.#ticks -= 1;
this.mob.look_control.look_at_entity(this.#player!);
}
}
// looks around at nothing in particular
export class RandomLookAroundGoal extends Goal {
override flags: GoalFlag[] = ["move", "look"];
#dx = 0;
#dz = 0;
#ticks = 0;
constructor(readonly mob: Mob) {
super();
}
can_use() {
return this.mob.random() < 0.02;
}
override can_continue_to_use() {
return this.#ticks >= 0;
}
override start() {
const angle = this.mob.random() * Math.PI * 2;
this.#dx = Math.cos(angle);
this.#dz = Math.sin(angle);
this.#ticks = 20 + Math.floor(this.mob.random() * 20);
}
override tick() {
this.#ticks -= 1;
const mob = this.mob;
mob.look_control.look_at(mob.x + this.#dx, mob.y + mob.eye_height, mob.z + this.#dz);
}
}
// follows a path to its target and hits it when close, like minecraft's MeleeAttackGoal. it searches again at most
// every RECALCULATE_TICKS, and only once the target is in another block than the one it last searched for
export class MeleeAttackGoal extends Goal {
override flags: GoalFlag[] = ["move", "look"];
#cooldown = 0;
#recalculate = 0;
#searched_for: string | undefined;
constructor(readonly mob: Mob, readonly speed_modifier = 1, readonly attack_interval = 20) {
super();
}
can_use() {
const target = this.mob.target;
return target !== undefined && is_alive(target);
}
override start() {
this.#cooldown = 0;
this.#recalculate = 0;
this.#searched_for = undefined;
}
override stop() {
this.mob.navigation.stop();
}
override tick() {
const mob = this.mob;
// a script's goal ticking before this one can have let it go, it stops next tick
const target = mob.target;
if (!target) return;
mob.look_control.look_at_entity(target);
this.#recalculate -= 1;
const block = `${Math.floor(target.x)},${Math.floor(target.y)},${Math.floor(target.z)}`;
if (this.#recalculate <= 0 && block !== this.#searched_for) {
this.#searched_for = block;
this.#recalculate = RECALCULATE_TICKS;
mob.navigation.move_to(target.x, target.y, target.z, this.speed_modifier);
}
this.#cooldown = Math.max(0, this.#cooldown - 1);
// minecraft's reach: its width doubled, squared, plus the target's width
const reach = (mob.width * 2) ** 2 + target.width;
const distance = (target.x - mob.x) ** 2 + (target.y - mob.y) ** 2 + (target.z - mob.z) ** 2;
if (distance <= reach && this.#cooldown === 0) {
this.#cooldown = this.attack_interval;
// clients see its arm swing, hit or not
mob.level.entity_event(mob, "swing");
mob.do_hurt_target(target);
}
}
}
// picks the mob's target, and keeps it while it's alive and within follow_range
abstract class TargetGoal extends Goal {
override flags: GoalFlag[] = ["target"];
constructor(readonly mob: Mob) {
super();
}
override can_continue_to_use() {
const target = this.mob.target;
const mob = this.mob;
return target !== undefined && is_alive(target) &&
Math.hypot(target.x - mob.x, target.y - mob.y, target.z - mob.z) <= mob.attribute(ATTRIBUTES.follow_range);
}
override stop() {
this.mob.target = undefined;
}
}
// goes after the nearest player it can see within follow_range
export class NearestAttackablePlayerGoal extends TargetGoal {
#find() {
const mob = this.mob;
return mob.level.nearest_player(mob.x, mob.y, mob.z, mob.attribute(ATTRIBUTES.follow_range));
}
can_use() {
return this.mob.random() < 0.1 && this.#find() !== undefined;
}
override start() {
this.mob.target = this.#find();
}
}
// goes after whoever hurt it last, like minecraft's HurtByTargetGoal, for as long as they're within follow_range
export class HurtByTargetGoal extends TargetGoal {
// the Mob.times_hurt_by_attacker it last went after someone for, so each hit only starts it once
#handled = 0;
can_use() {
const attacker = this.mob.last_attacker;
return this.mob.times_hurt_by_attacker !== this.#handled && attacker !== undefined && is_alive(attacker);
}
override start() {
this.#handled = this.mob.times_hurt_by_attacker;
this.mob.target = this.mob.last_attacker;
}
}
function is_alive(target: LivingEntity) {
return target.alive;
}
// somewhere random within range it could stand: solid ground under it, room for its box, and inside its home.
// undefined when the tries all fail
export function random_stand_position(mob: Mob, range: number, vertical: number, tries = 10) {
const level = mob.level;
const free = (x: number, y: number, z: number) => !level.has_collision(x, y, z) && !level.is_fluid(x, y, z);
for (let i = 0; i < tries; i++) {
const x = Math.floor(mob.x + (mob.random() * 2 - 1) * range);
const z = Math.floor(mob.z + (mob.random() * 2 - 1) * range);
const top = Math.floor(mob.y + (mob.random() * 2 - 1) * vertical);
for (let y = top; y > top - vertical * 2; y--) {
if (!level.has_collision(x, y - 1, z)) continue;
const room = Math.ceil(mob.height);
let fits = true;
for (let h = 0; h < room && fits; h++) fits = free(x, y + h, z);
if (fits && mob.is_within_home(x + 0.5, y, z + 0.5)) return { x: x + 0.5, y, z: z + 0.5 };
break;
}
}
return undefined;
}