From 14098ab5d225a775d84925443f71b726ca3462e5 Mon Sep 17 00:00:00 2001 From: Maximus Date: Sun, 29 Sep 2024 13:35:15 -0700 Subject: [PATCH] added unstuck to followPlayer --- src/agent/agent.js | 4 ++-- src/agent/library/skills.js | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/agent/agent.js b/src/agent/agent.js index c3d7ad9..547c06b 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -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); diff --git a/src/agent/library/skills.js b/src/agent/library/skills.js index 7ef87b2..0782562 100644 --- a/src/agent/library/skills.js +++ b/src/agent/library/skills.js @@ -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; }