mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-04-21 21:52:07 +02:00
Merge pull request #4 from kolbytn/single-tasking
coder stops previous task
This commit is contained in:
commit
09266c80cc
3 changed files with 39 additions and 11 deletions
8
agent.js
8
agent.js
|
@ -30,6 +30,14 @@ export class Agent {
|
|||
|
||||
this.respond(username, message);
|
||||
});
|
||||
|
||||
this.bot.on('finished_executing', () => {
|
||||
setTimeout(() => {
|
||||
if (!this.coder.executing) {
|
||||
// return to default behavior
|
||||
}
|
||||
}, 10000);
|
||||
})
|
||||
}
|
||||
|
||||
async respond(username, message) {
|
||||
|
|
|
@ -6,6 +6,8 @@ export class Coder {
|
|||
this.current_code = '';
|
||||
this.file_counter = 0;
|
||||
this.fp = './agent_code/';
|
||||
this.executing = false;
|
||||
this.agent.bot.abort_code = false;
|
||||
}
|
||||
|
||||
queueCode(code) {
|
||||
|
@ -20,6 +22,7 @@ export class Coder {
|
|||
return code;
|
||||
}
|
||||
}
|
||||
code = code.replace(';', '; if(bot.abort_code) return false;')
|
||||
return code;
|
||||
}
|
||||
|
||||
|
@ -73,20 +76,35 @@ export class Coder {
|
|||
try {
|
||||
console.log('executing code...\n');
|
||||
let execution_file = await import('.'+filename);
|
||||
this.clear();
|
||||
|
||||
await this.clear();
|
||||
this.executing = true;
|
||||
await execution_file.main(this.agent.bot);
|
||||
this.executing = false;
|
||||
this.agent.bot.emit('finished_executing');
|
||||
|
||||
let msg = 'Code executed successfully.';
|
||||
console.log(msg)
|
||||
return {success: true, message: msg};
|
||||
|
||||
} catch (err) {
|
||||
this.executing = false;
|
||||
this.agent.bot.emit('finished_executing');
|
||||
await this.clear();
|
||||
|
||||
console.error("Code execution triggered catch:" + err);
|
||||
this.clear();
|
||||
return {success: false, message: err};
|
||||
}
|
||||
}
|
||||
|
||||
clear() {
|
||||
async clear() {
|
||||
while (this.executing) {
|
||||
this.agent.bot.abort_code = true;
|
||||
this.agent.bot.collectBlock.cancelTask();
|
||||
this.agent.bot.pathfinder.stop();
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
}
|
||||
this.current_code = '';
|
||||
this.agent.bot.pathfinder.setGoal(null);
|
||||
this.abort_code = false;
|
||||
}
|
||||
}
|
|
@ -100,7 +100,7 @@ export async function placeBlock(bot, blockType, x, y, z, faceVec=new Vec3(0, 1,
|
|||
if (!block)
|
||||
return false;
|
||||
await bot.equip(block, 'hand');
|
||||
bot.placeBlock(referenceBlock, faceVec).then(() => {
|
||||
await bot.placeBlock(referenceBlock, faceVec).then(() => {
|
||||
return true;
|
||||
}).catch((err) => {
|
||||
return false;
|
||||
|
@ -148,7 +148,7 @@ export async function goToPosition(bot, x, y, z) {
|
|||
if (z == null) z = bot.entity.position.z;
|
||||
bot.pathfinder.setMovements(new pf.Movements(bot));
|
||||
let pos = { x: x, y: y, z: z };
|
||||
bot.pathfinder.setGoal(new pf.goals.GoalNear(pos.x, pos.y, pos.z, 1));
|
||||
await bot.pathfinder.goto(new pf.goals.GoalNear(pos.x, pos.y, pos.z, 1));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -168,7 +168,7 @@ export async function giveToPlayer(bot, itemType, username) {
|
|||
return false;
|
||||
if (getInventoryCounts(bot)[itemType] == 0)
|
||||
return false;
|
||||
goToPlayer(bot, username);
|
||||
await goToPlayer(bot, username);
|
||||
let pos = player.position;
|
||||
await bot.lookAt(pos);
|
||||
await bot.toss(getItemId(itemType), null, 1);
|
||||
|
@ -191,7 +191,7 @@ export async function goToPlayer(bot, username) {
|
|||
|
||||
bot.pathfinder.setMovements(new pf.Movements(bot));
|
||||
let pos = player.position;
|
||||
bot.pathfinder.setGoal(new pf.goals.GoalNear(pos.x, pos.y, pos.z, 3));
|
||||
await bot.pathfinder.goto(new pf.goals.GoalNear(pos.x, pos.y, pos.z, 3));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -210,8 +210,10 @@ export async function followPlayer(bot, username) {
|
|||
return false;
|
||||
|
||||
bot.pathfinder.setMovements(new pf.Movements(bot));
|
||||
let pos = player.position;
|
||||
bot.pathfinder.setGoal(new pf.goals.GoalFollow(player, 3), true);
|
||||
while (!bot.abort) {
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
let pos = player.position;
|
||||
await bot.pathfinder.goto(new pf.goals.GoalNear(pos.x, pos.y, pos.z, 3));
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue