382 lines
10 KiB
TypeScript
382 lines
10 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";
|
|
|
|
export type GoalFlag = "move" | "look" | "jump" | "target";
|
|
|
|
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
|
|
export class RandomStrollGoal extends Goal {
|
|
override flags: GoalFlag[] = ["move"];
|
|
// there's no pathfinding, so it gives up on a spot it can't get to
|
|
#ticks = 0;
|
|
#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;
|
|
}
|
|
|
|
override can_continue_to_use() {
|
|
const wanted = this.#wanted;
|
|
return wanted !== undefined && this.#ticks < 200 &&
|
|
(wanted.x - this.mob.x) ** 2 + (wanted.z - this.mob.z) ** 2 > 0.25;
|
|
}
|
|
|
|
override start() {
|
|
this.#ticks = 0;
|
|
const { x, y, z } = this.#wanted!;
|
|
this.mob.move_control.move_to(x, y, z, this.speed_modifier);
|
|
}
|
|
|
|
override tick() {
|
|
this.#ticks += 1;
|
|
}
|
|
|
|
override stop() {
|
|
this.#wanted = undefined;
|
|
this.mob.move_control.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() {
|
|
const wanted = this.#wanted;
|
|
return wanted !== undefined && (wanted.x - this.mob.x) ** 2 + (wanted.z - this.mob.z) ** 2 > 0.25;
|
|
}
|
|
|
|
override start() {
|
|
const { x, y, z } = this.#wanted!;
|
|
this.mob.move_control.move_to(x, y, z, this.speed_modifier);
|
|
}
|
|
|
|
override stop() {
|
|
this.#wanted = undefined;
|
|
this.mob.move_control.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);
|
|
}
|
|
}
|
|
|
|
// walks to its target and hits it when close, like minecraft's MeleeAttackGoal without the pathfinding
|
|
export class MeleeAttackGoal extends Goal {
|
|
override flags: GoalFlag[] = ["move", "look"];
|
|
#cooldown = 0;
|
|
|
|
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;
|
|
}
|
|
|
|
override stop() {
|
|
this.mob.move_control.stop();
|
|
}
|
|
|
|
override tick() {
|
|
const mob = this.mob;
|
|
const target = mob.target!;
|
|
mob.look_control.look_at_entity(target);
|
|
mob.move_control.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_liquid(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;
|
|
}
|