This commit is contained in:
2026-11-21 14:52:33 -03:00
parent 357f8f2bd4
commit b4f07272d8
7 changed files with 195 additions and 30 deletions
+25 -20
View File
@@ -11,6 +11,9 @@ 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[] = [];
@@ -119,11 +122,9 @@ export class FloatGoal extends Goal {
}
}
// wanders to a random spot nearby every now and then, staying near home
// 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"];
// 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
@@ -137,25 +138,19 @@ export class RandomStrollGoal extends Goal {
return this.#wanted !== undefined;
}
// until it gets there, or as close as it can
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;
return this.mob.navigation.moving;
}
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;
this.mob.navigation.move_to(x, y, z, this.speed_modifier);
}
override stop() {
this.#wanted = undefined;
this.mob.move_control.stop();
this.mob.navigation.stop();
}
}
@@ -180,18 +175,17 @@ export class PanicGoal extends Goal {
}
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;
return this.mob.navigation.moving;
}
override start() {
const { x, y, z } = this.#wanted!;
this.mob.move_control.move_to(x, y, z, this.speed_modifier);
this.mob.navigation.move_to(x, y, z, this.speed_modifier);
}
override stop() {
this.#wanted = undefined;
this.mob.move_control.stop();
this.mob.navigation.stop();
}
}
@@ -264,10 +258,13 @@ export class RandomLookAroundGoal extends Goal {
}
}
// walks to its target and hits it when close, like minecraft's MeleeAttackGoal without the pathfinding
// 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();
@@ -280,17 +277,25 @@ export class MeleeAttackGoal extends Goal {
override start() {
this.#cooldown = 0;
this.#recalculate = 0;
this.#searched_for = undefined;
}
override stop() {
this.mob.move_control.stop();
this.mob.navigation.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.#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;