mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-21 14:45:45 +02:00
improved unstuck mode, goal resuming
This commit is contained in:
parent
05fcc9967c
commit
5482a5dc7e
3 changed files with 40 additions and 34 deletions
|
@ -102,6 +102,7 @@ export class Coder {
|
||||||
if (this.agent.bot.interrupt_code)
|
if (this.agent.bot.interrupt_code)
|
||||||
return interrupt_return;
|
return interrupt_return;
|
||||||
console.log(messages)
|
console.log(messages)
|
||||||
|
this.agent.bot.modes.pause('unstuck');
|
||||||
let res = await this.agent.prompter.promptCoding(JSON.parse(JSON.stringify(messages)));
|
let res = await this.agent.prompter.promptCoding(JSON.parse(JSON.stringify(messages)));
|
||||||
if (this.agent.bot.interrupt_code)
|
if (this.agent.bot.interrupt_code)
|
||||||
return interrupt_return;
|
return interrupt_return;
|
||||||
|
@ -132,6 +133,7 @@ export class Coder {
|
||||||
agent_history.add('system', 'Failed to stage code, something is wrong.');
|
agent_history.add('system', 'Failed to stage code, something is wrong.');
|
||||||
return {success: false, message: null, interrupted: false, timedout: false};
|
return {success: false, message: null, interrupted: false, timedout: false};
|
||||||
}
|
}
|
||||||
|
this.agent.bot.modes.unpause('unstuck');
|
||||||
code_return = await this.execute(async ()=>{
|
code_return = await this.execute(async ()=>{
|
||||||
return await execution_file.main(this.agent.bot);
|
return await execution_file.main(this.agent.bot);
|
||||||
}, settings.code_timeout_mins);
|
}, settings.code_timeout_mins);
|
||||||
|
@ -158,11 +160,12 @@ export class Coder {
|
||||||
}
|
}
|
||||||
|
|
||||||
async executeResume(func=null, timeout=10) {
|
async executeResume(func=null, timeout=10) {
|
||||||
if (func != null) { // start new resume
|
const new_resume = func != null;
|
||||||
|
if (new_resume) { // start new resume
|
||||||
this.resume_func = func;
|
this.resume_func = func;
|
||||||
this.resume_name = this.cur_action_name;
|
this.resume_name = this.cur_action_name;
|
||||||
}
|
}
|
||||||
if (this.resume_func != null && this.agent.isIdle() && !this.agent.self_prompter.on) {
|
if (this.resume_func != null && this.agent.isIdle() && (!this.agent.self_prompter.on || new_resume)) {
|
||||||
this.cur_action_name = this.resume_name;
|
this.cur_action_name = this.resume_name;
|
||||||
let res = await this.execute(this.resume_func, timeout);
|
let res = await this.execute(this.resume_func, timeout);
|
||||||
this.cur_action_name = '';
|
this.cur_action_name = '';
|
||||||
|
|
|
@ -181,6 +181,7 @@ export async function smeltItem(bot, itemName, num=1) {
|
||||||
let total = 0;
|
let total = 0;
|
||||||
let collected_last = true;
|
let collected_last = true;
|
||||||
let smelted_item = null;
|
let smelted_item = null;
|
||||||
|
bot.mods.pause('unstuck');
|
||||||
await new Promise(resolve => setTimeout(resolve, 200));
|
await new Promise(resolve => setTimeout(resolve, 200));
|
||||||
while (total < num) {
|
while (total < num) {
|
||||||
await new Promise(resolve => setTimeout(resolve, 10000));
|
await new Promise(resolve => setTimeout(resolve, 10000));
|
||||||
|
@ -202,6 +203,7 @@ export async function smeltItem(bot, itemName, num=1) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await bot.closeWindow(furnace);
|
await bot.closeWindow(furnace);
|
||||||
|
bot.mods.unpause('unstuck');
|
||||||
|
|
||||||
if (placedFurnace) {
|
if (placedFurnace) {
|
||||||
await collectBlock(bot, 'furnace', 1);
|
await collectBlock(bot, 'furnace', 1);
|
||||||
|
@ -560,10 +562,14 @@ export async function placeBlock(bot, blockType, x, y, z, placeOn='bottom', dont
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
let block = bot.inventory.items().find(item => item.name === blockType);
|
|
||||||
|
let item_name = blockType;
|
||||||
|
if (item_name == "redstone_wire")
|
||||||
|
item_name = "redstone";
|
||||||
|
let block = bot.inventory.items().find(item => item.name === item_name);
|
||||||
if (!block && bot.game.gameMode === 'creative') {
|
if (!block && bot.game.gameMode === 'creative') {
|
||||||
await bot.creative.setInventorySlot(36, mc.makeItem(blockType, 1)); // 36 is first hotbar slot
|
await bot.creative.setInventorySlot(36, mc.makeItem(item_name, 1)); // 36 is first hotbar slot
|
||||||
block = bot.inventory.items().find(item => item.name === blockType);
|
block = bot.inventory.items().find(item => item.name === item_name);
|
||||||
}
|
}
|
||||||
if (!block) {
|
if (!block) {
|
||||||
log(bot, `Don't have any ${blockType} to place.`);
|
log(bot, `Don't have any ${blockType} to place.`);
|
||||||
|
@ -624,7 +630,7 @@ export async function placeBlock(bot, blockType, x, y, z, placeOn='bottom', dont
|
||||||
|
|
||||||
const pos = bot.entity.position;
|
const pos = bot.entity.position;
|
||||||
const pos_above = pos.plus(Vec3(0,1,0));
|
const pos_above = pos.plus(Vec3(0,1,0));
|
||||||
const dont_move_for = ['torch', 'redstone_torch', 'redstone', 'lever', 'button', 'rail', 'detector_rail', 'powered_rail', 'activator_rail', 'tripwire_hook', 'tripwire', 'water_bucket'];
|
const dont_move_for = ['torch', 'redstone_torch', 'redstone_wire', 'lever', 'button', 'rail', 'detector_rail', 'powered_rail', 'activator_rail', 'tripwire_hook', 'tripwire', 'water_bucket'];
|
||||||
if (!dont_move_for.includes(blockType) && (pos.distanceTo(targetBlock.position) < 1 || pos_above.distanceTo(targetBlock.position) < 1)) {
|
if (!dont_move_for.includes(blockType) && (pos.distanceTo(targetBlock.position) < 1 || pos_above.distanceTo(targetBlock.position) < 1)) {
|
||||||
// too close
|
// too close
|
||||||
let goal = new pf.goals.GoalNear(targetBlock.position.x, targetBlock.position.y, targetBlock.position.z, 2);
|
let goal = new pf.goals.GoalNear(targetBlock.position.x, targetBlock.position.y, targetBlock.position.z, 2);
|
||||||
|
@ -964,33 +970,19 @@ export async function followPlayer(bot, username, distance=4) {
|
||||||
bot.pathfinder.setGoal(new pf.goals.GoalFollow(player, distance), true);
|
bot.pathfinder.setGoal(new pf.goals.GoalFollow(player, distance), true);
|
||||||
log(bot, `You are now actively following player ${username}.`);
|
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) {
|
while (!bot.interrupt_code) {
|
||||||
await new Promise(resolve => setTimeout(resolve, 500));
|
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
|
// 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) {
|
if (bot.modes.isOn('cheat') && bot.entity.position.distanceTo(player.position) > 100 && player.isOnGround) {
|
||||||
await goToPlayer(bot, username);
|
await goToPlayer(bot, username);
|
||||||
}
|
}
|
||||||
if (bot.modes.isOn('unstuck')) {
|
if (bot.modes.isOn('unstuck')) {
|
||||||
const far_away = bot.entity.position.distanceTo(player.position) > distance + 1;
|
const is_nearby = bot.entity.position.distanceTo(player.position) <= distance + 1;
|
||||||
if (far_away && bot.entity.position.distanceTo(last_pos) <= 2) {
|
if (is_nearby)
|
||||||
stuck_time += delta;
|
bot.modes.pause('unstuck');
|
||||||
if (stuck_time > 10000) {
|
else
|
||||||
log(bot, `Got stuck, attempting to move away.`);
|
bot.modes.unpause('unstuck');
|
||||||
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1066,6 +1058,7 @@ export async function stay(bot) {
|
||||||
* await skills.stay(bot);
|
* await skills.stay(bot);
|
||||||
**/
|
**/
|
||||||
bot.modes.pause('self_preservation');
|
bot.modes.pause('self_preservation');
|
||||||
|
bot.modes.pause('unstuck');
|
||||||
bot.modes.pause('cowardice');
|
bot.modes.pause('cowardice');
|
||||||
bot.modes.pause('self_defense');
|
bot.modes.pause('self_defense');
|
||||||
bot.modes.pause('hunting');
|
bot.modes.pause('hunting');
|
||||||
|
|
|
@ -77,7 +77,7 @@ const modes = [
|
||||||
{
|
{
|
||||||
name: 'unstuck',
|
name: 'unstuck',
|
||||||
description: 'Attempt to get unstuck when in the same place for a while. Interrupts some actions.',
|
description: 'Attempt to get unstuck when in the same place for a while. Interrupts some actions.',
|
||||||
interrupts: ['collectBlocks', 'goToPlayer', 'collectAllBlocks', 'goToPlace'],
|
interrupts: ['all'],
|
||||||
on: true,
|
on: true,
|
||||||
active: false,
|
active: false,
|
||||||
prev_location: null,
|
prev_location: null,
|
||||||
|
@ -86,7 +86,11 @@ const modes = [
|
||||||
last_time: Date.now(),
|
last_time: Date.now(),
|
||||||
max_stuck_time: 20,
|
max_stuck_time: 20,
|
||||||
update: async function (agent) {
|
update: async function (agent) {
|
||||||
if (agent.isIdle()) return;
|
if (agent.isIdle()) {
|
||||||
|
this.prev_location = null;
|
||||||
|
this.stuck_time = 0;
|
||||||
|
return; // don't get stuck when idle
|
||||||
|
}
|
||||||
const bot = agent.bot;
|
const bot = agent.bot;
|
||||||
if (this.prev_location && this.prev_location.distanceTo(bot.entity.position) < this.distance) {
|
if (this.prev_location && this.prev_location.distanceTo(bot.entity.position) < this.distance) {
|
||||||
this.stuck_time += (Date.now() - this.last_time) / 1000;
|
this.stuck_time += (Date.now() - this.last_time) / 1000;
|
||||||
|
@ -98,7 +102,9 @@ const modes = [
|
||||||
if (this.stuck_time > this.max_stuck_time) {
|
if (this.stuck_time > this.max_stuck_time) {
|
||||||
say(agent, 'I\'m stuck!');
|
say(agent, 'I\'m stuck!');
|
||||||
execute(this, agent, async () => {
|
execute(this, agent, async () => {
|
||||||
|
const crashTimeout = setTimeout(() => { throw new Error('Bot was stuck and could not get unstuck.'); }, 10000);
|
||||||
await skills.moveAway(bot, 5);
|
await skills.moveAway(bot, 5);
|
||||||
|
clearTimeout(crashTimeout);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
this.last_time = Date.now();
|
this.last_time = Date.now();
|
||||||
|
@ -287,6 +293,17 @@ class ModeController {
|
||||||
this.modes_map[mode_name].paused = true;
|
this.modes_map[mode_name].paused = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unpause(mode_name) {
|
||||||
|
this.modes_map[mode_name].paused = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
unPauseAll() {
|
||||||
|
for (let mode of this.modes_list) {
|
||||||
|
if (mode.paused) console.log(`Unpausing mode ${mode.name}`);
|
||||||
|
mode.paused = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getMiniDocs() { // no descriptions
|
getMiniDocs() { // no descriptions
|
||||||
let res = 'Agent Modes:';
|
let res = 'Agent Modes:';
|
||||||
for (let mode of this.modes_list) {
|
for (let mode of this.modes_list) {
|
||||||
|
@ -305,13 +322,6 @@ class ModeController {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
unPauseAll() {
|
|
||||||
for (let mode of this.modes_list) {
|
|
||||||
if (mode.paused) console.log(`Unpausing mode ${mode.name}`);
|
|
||||||
mode.paused = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async update() {
|
async update() {
|
||||||
if (this.agent.isIdle()) {
|
if (this.agent.isIdle()) {
|
||||||
this.unPauseAll();
|
this.unPauseAll();
|
||||||
|
|
Loading…
Add table
Reference in a new issue