mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-04 14:25:43 +02:00
removed idle
This commit is contained in:
parent
546d5e7cef
commit
a6971a4c4a
5 changed files with 12 additions and 37 deletions
|
@ -21,7 +21,6 @@ export class Agent {
|
|||
this.bot = initBot(name);
|
||||
|
||||
initModes(this);
|
||||
this.idle = true;
|
||||
|
||||
this.bot.on('login', async () => {
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@ import { Examples } from '../utils/examples.js';
|
|||
export class Coder {
|
||||
constructor(agent) {
|
||||
this.agent = agent;
|
||||
this.current_code = '';
|
||||
this.file_counter = 0;
|
||||
this.fp = '/bots/'+agent.name+'/action-code/';
|
||||
this.executing = false;
|
||||
|
@ -59,7 +58,6 @@ export class Coder {
|
|||
console.error('Error writing code execution file: ' + result);
|
||||
return null;
|
||||
}
|
||||
this.current_code = code;
|
||||
return await import('../..' + this.fp + filename);
|
||||
}
|
||||
|
||||
|
@ -149,7 +147,7 @@ export class Coder {
|
|||
return;
|
||||
}
|
||||
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
// returns {success: bool, message: string, interrupted: bool, timedout: false}
|
||||
|
@ -173,19 +171,15 @@ export class Coder {
|
|||
let interrupted = this.agent.bot.interrupt_code;
|
||||
let timedout = this.timedout;
|
||||
this.clear();
|
||||
this.agent.bot.emit("code_terminated");
|
||||
return {success:true, message: output, interrupted, timedout};
|
||||
} catch (err) {
|
||||
this.executing = false;
|
||||
clearTimeout(TIMEOUT);
|
||||
|
||||
console.error("Code execution triggered catch: " + err);
|
||||
clearTimeout(TIMEOUT);
|
||||
await this.stop();
|
||||
let message = this.formatOutput(this.agent.bot);
|
||||
message += '!!Code threw exception!! Error: ' + err;
|
||||
|
||||
let message = this.formatOutput(this.agent.bot) + '!!Code threw exception!! Error: ' + err;
|
||||
let interrupted = this.agent.bot.interrupt_code;
|
||||
this.clear();
|
||||
this.agent.bot.emit("code_terminated");
|
||||
return {success: false, message, interrupted, timedout: false};
|
||||
}
|
||||
}
|
||||
|
@ -217,7 +211,6 @@ export class Coder {
|
|||
}
|
||||
|
||||
clear() {
|
||||
this.current_code = '';
|
||||
this.agent.bot.output = '';
|
||||
this.agent.bot.interrupt_code = false;
|
||||
this.timedout = false;
|
||||
|
|
|
@ -3,13 +3,11 @@ import * as skills from '../library/skills.js';
|
|||
|
||||
function wrapExecution(func, timeout=-1) {
|
||||
return async function (agent, ...args) {
|
||||
agent.idle = false;
|
||||
let code_return = await agent.coder.execute(async () => {
|
||||
await func(agent, ...args);
|
||||
}, timeout);
|
||||
if (code_return.interrupted && !code_return.timedout)
|
||||
return;
|
||||
agent.idle = true;
|
||||
return code_return.message;
|
||||
}
|
||||
}
|
||||
|
@ -19,11 +17,7 @@ export const actionsList = [
|
|||
name: '!newAction',
|
||||
description: 'Perform new and unknown custom behaviors that are not available as a command by writing code.',
|
||||
perform: async function (agent) {
|
||||
agent.idle = false;
|
||||
let res = await agent.coder.generateCode(agent.history);
|
||||
agent.idle = true;
|
||||
if (res)
|
||||
return '\n' + res + '\n';
|
||||
await agent.coder.generateCode(agent.history);
|
||||
}
|
||||
},
|
||||
{
|
||||
|
@ -32,7 +26,6 @@ export const actionsList = [
|
|||
perform: async function (agent) {
|
||||
await agent.coder.stop();
|
||||
agent.coder.clear();
|
||||
agent.idle = true;
|
||||
return 'Agent stopped.';
|
||||
}
|
||||
},
|
||||
|
|
|
@ -117,12 +117,5 @@ export const queryList = [
|
|||
perform: function (agent) {
|
||||
return agent.bot.modes.getStr();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "!currentAction",
|
||||
description: "Get the currently executing code.",
|
||||
perform: function (agent) {
|
||||
return pad("Current code:\n`" + agent.coder.current_code +"`");
|
||||
}
|
||||
},
|
||||
];
|
||||
|
|
|
@ -34,7 +34,7 @@ const modes = [
|
|||
on: true,
|
||||
active: false,
|
||||
update: function (agent) {
|
||||
if (agent.idle) {
|
||||
if (!agent.coder.executing) {
|
||||
const huntable = world.getNearestEntityWhere(agent.bot, entity => mc.isHuntable(entity), 8);
|
||||
if (huntable) {
|
||||
execute(this, agent, async () => {
|
||||
|
@ -51,7 +51,7 @@ const modes = [
|
|||
on: true,
|
||||
active: false,
|
||||
update: function (agent) {
|
||||
if (agent.idle) {
|
||||
if (!agent.coder.executing) {
|
||||
let item = world.getNearestEntityWhere(agent.bot, entity => entity.name === 'item', 8);
|
||||
if (item) {
|
||||
execute(this, agent, async () => {
|
||||
|
@ -70,7 +70,7 @@ const modes = [
|
|||
active: false,
|
||||
update: function (agent) {
|
||||
if (this.active) return;
|
||||
if (agent.idle) {
|
||||
if (!agent.coder.executing) {
|
||||
// TODO: check light level instead of nearby torches, block.light is broken
|
||||
const near_torch = world.getNearestBlock(agent.bot, 'torch', 8);
|
||||
if (!near_torch) {
|
||||
|
@ -96,7 +96,7 @@ const modes = [
|
|||
last_entity: null,
|
||||
next_change: 0,
|
||||
update: function (agent) {
|
||||
if (agent.idle) {
|
||||
if (!agent.coder.executing) {
|
||||
this.active = true;
|
||||
const entity = agent.bot.nearestEntity();
|
||||
let entity_in_view = entity && entity.position.distanceTo(agent.bot.entity.position) < 10 && entity.name !== 'enderman';
|
||||
|
@ -131,13 +131,10 @@ const modes = [
|
|||
|
||||
async function execute(mode, agent, func, timeout=-1) {
|
||||
mode.active = true;
|
||||
await agent.coder.stop();
|
||||
agent.idle = false;
|
||||
let code_return = await agent.coder.execute(async () => {
|
||||
await func();
|
||||
}, timeout);
|
||||
mode.active = false;
|
||||
agent.idle = true;
|
||||
console.log(`Mode ${mode.name} finished executing, code_return: ${code_return.message}`);
|
||||
}
|
||||
|
||||
|
@ -177,7 +174,7 @@ class ModeController {
|
|||
}
|
||||
|
||||
update() {
|
||||
if (this.agent.idle) {
|
||||
if (!this.agent.coder.executing) {
|
||||
// other actions might pause a mode to override it
|
||||
// when idle, unpause all modes
|
||||
for (let mode of this.modes_list) {
|
||||
|
|
Loading…
Add table
Reference in a new issue