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.respond(username, message);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.bot.on('finished_executing', () => {
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!this.coder.executing) {
|
||||||
|
// return to default behavior
|
||||||
|
}
|
||||||
|
}, 10000);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async respond(username, message) {
|
async respond(username, message) {
|
||||||
|
|
|
@ -6,6 +6,8 @@ export class Coder {
|
||||||
this.current_code = '';
|
this.current_code = '';
|
||||||
this.file_counter = 0;
|
this.file_counter = 0;
|
||||||
this.fp = './agent_code/';
|
this.fp = './agent_code/';
|
||||||
|
this.executing = false;
|
||||||
|
this.agent.bot.abort_code = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
queueCode(code) {
|
queueCode(code) {
|
||||||
|
@ -20,6 +22,7 @@ export class Coder {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
code = code.replace(';', '; if(bot.abort_code) return false;')
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,20 +76,35 @@ export class Coder {
|
||||||
try {
|
try {
|
||||||
console.log('executing code...\n');
|
console.log('executing code...\n');
|
||||||
let execution_file = await import('.'+filename);
|
let execution_file = await import('.'+filename);
|
||||||
this.clear();
|
|
||||||
|
await this.clear();
|
||||||
|
this.executing = true;
|
||||||
await execution_file.main(this.agent.bot);
|
await execution_file.main(this.agent.bot);
|
||||||
|
this.executing = false;
|
||||||
|
this.agent.bot.emit('finished_executing');
|
||||||
|
|
||||||
let msg = 'Code executed successfully.';
|
let msg = 'Code executed successfully.';
|
||||||
console.log(msg)
|
console.log(msg)
|
||||||
return {success: true, message: msg};
|
return {success: true, message: msg};
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
this.executing = false;
|
||||||
|
this.agent.bot.emit('finished_executing');
|
||||||
|
await this.clear();
|
||||||
|
|
||||||
console.error("Code execution triggered catch:" + err);
|
console.error("Code execution triggered catch:" + err);
|
||||||
this.clear();
|
|
||||||
return {success: false, message: err};
|
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.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)
|
if (!block)
|
||||||
return false;
|
return false;
|
||||||
await bot.equip(block, 'hand');
|
await bot.equip(block, 'hand');
|
||||||
bot.placeBlock(referenceBlock, faceVec).then(() => {
|
await bot.placeBlock(referenceBlock, faceVec).then(() => {
|
||||||
return true;
|
return true;
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
return false;
|
return false;
|
||||||
|
@ -148,7 +148,7 @@ export async function goToPosition(bot, x, y, z) {
|
||||||
if (z == null) z = bot.entity.position.z;
|
if (z == null) z = bot.entity.position.z;
|
||||||
bot.pathfinder.setMovements(new pf.Movements(bot));
|
bot.pathfinder.setMovements(new pf.Movements(bot));
|
||||||
let pos = { x: x, y: y, z: z };
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,7 +168,7 @@ export async function giveToPlayer(bot, itemType, username) {
|
||||||
return false;
|
return false;
|
||||||
if (getInventoryCounts(bot)[itemType] == 0)
|
if (getInventoryCounts(bot)[itemType] == 0)
|
||||||
return false;
|
return false;
|
||||||
goToPlayer(bot, username);
|
await goToPlayer(bot, username);
|
||||||
let pos = player.position;
|
let pos = player.position;
|
||||||
await bot.lookAt(pos);
|
await bot.lookAt(pos);
|
||||||
await bot.toss(getItemId(itemType), null, 1);
|
await bot.toss(getItemId(itemType), null, 1);
|
||||||
|
@ -191,7 +191,7 @@ export async function goToPlayer(bot, username) {
|
||||||
|
|
||||||
bot.pathfinder.setMovements(new pf.Movements(bot));
|
bot.pathfinder.setMovements(new pf.Movements(bot));
|
||||||
let pos = player.position;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,8 +210,10 @@ export async function followPlayer(bot, username) {
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
bot.pathfinder.setMovements(new pf.Movements(bot));
|
bot.pathfinder.setMovements(new pf.Movements(bot));
|
||||||
let pos = player.position;
|
while (!bot.abort) {
|
||||||
bot.pathfinder.setGoal(new pf.goals.GoalFollow(player, 3), true);
|
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;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue