added unstuck to followPlayer

This commit is contained in:
Maximus 2024-09-29 13:35:15 -07:00
parent 675594db08
commit 14098ab5d2
2 changed files with 23 additions and 2 deletions

View file

@ -129,10 +129,10 @@ export class Agent {
const checkInterrupt = () => this.self_prompter.shouldInterrupt(self_prompt) || this.shut_up;
let behavior_log = this.bot.modes.flushBehaviorLog();
if (behavior_log !== '') {
if (behavior_log.trim().length > 0) {
const MAX_LOG = 500;
if (behavior_log.length > MAX_LOG) {
behavior_log = behavior_log.substring(behavior_log.length - MAX_LOG) + '...';
behavior_log = '...' + behavior_log.substring(behavior_log.length - MAX_LOG);
}
behavior_log = 'Recent behaviors log: \n' + behavior_log.substring(behavior_log.indexOf('\n'));
await this.history.add('system', behavior_log);

View file

@ -964,12 +964,33 @@ export async function followPlayer(bot, username, distance=4) {
bot.pathfinder.setGoal(new pf.goals.GoalFollow(player, distance), true);
log(bot, `You are now actively following player ${username}.`);
let last_time = Date.now();
let stuck_time = 0;
let last_pos = bot.entity.position.clone();
while (!bot.interrupt_code) {
await new Promise(resolve => setTimeout(resolve, 500));
const delta = Date.now() - last_time;
// in cheat mode, if the distance is too far, teleport to the player
if (bot.modes.isOn('cheat') && bot.entity.position.distanceTo(player.position) > 100 && player.isOnGround) {
await goToPlayer(bot, username);
}
if (bot.modes.isOn('unstuck')) {
const far_away = bot.entity.position.distanceTo(player.position) > distance + 1;
if (far_away && bot.entity.position.distanceTo(last_pos) <= 2) {
stuck_time += delta;
if (stuck_time > 10000) {
log(bot, `Got stuck, attempting to move away.`);
bot.pathfinder.stop();
await moveAway(bot, 4);
return false;
}
}
else {
stuck_time = 0;
last_pos = bot.entity.position.clone();
}
}
last_time = Date.now();
}
return true;
}