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;
+8 -2
View File
@@ -99,11 +99,18 @@ export class PathNavigation {
const next = points[this.#index];
mob.move_control.move_to(next.x, next.y, next.z, this.#speed);
// it stays blocked for as long as the block is solid and in its way, even when its path is searched again, so a
// goal can count how long it's been there
if (this.blocked_by) {
this.blocked_by = this.#solid_block_toward(next);
if (this.blocked_by) return;
this.#closest = Infinity;
this.#stuck = 0;
}
const distance = Math.hypot(next.x - mob.x, next.z - mob.z);
if (distance < this.#closest - 0.01) {
this.#closest = distance;
this.#stuck = 0;
this.blocked_by = undefined;
return;
}
this.#stuck += 1;
@@ -143,7 +150,6 @@ export class PathNavigation {
this.#closest = Infinity;
this.#stuck = 0;
this.#searched_again = searched_again;
this.blocked_by = undefined;
}
// close enough to a waypoint to go on to the next, like minecraft's maxDistanceToWaypoint
+9 -2
View File
@@ -726,10 +726,17 @@ export class ModRuntime {
random: () => mob.random(),
move_to(x, y, z, speed = 1) {
if (![x, y, z, speed].every(Number.isFinite)) throw new Error("move_to takes a position and a speed");
mob.move_control.move_to(x, y, z, speed);
return mob.navigation.move_to(x, y, z, speed);
},
stop() {
mob.move_control.stop();
mob.navigation.stop();
},
get moving() {
return mob.navigation.moving;
},
get blocked_by() {
const blocked = mob.navigation.blocked_by;
return blocked && { ...blocked };
},
look_at(x, y, z) {
if (![x, y, z].every(Number.isFinite)) throw new Error("look_at takes a position");