optimization and bug fix

This commit is contained in:
Kolby Nottingham 2024-02-12 17:48:22 -08:00
parent 899aad7f81
commit d5e118fa3a
2 changed files with 14 additions and 16 deletions

View file

@ -15,6 +15,7 @@ export class Agent {
this.history = new History(this);
this.coder = new Coder(this);
this.item_goal = new ItemGoal(this);
this.item_goal.setGoal('iron_pickaxe', 1);
console.log('Loading examples...');

View file

@ -118,8 +118,8 @@ class ItemNode {
return false;
}
getDepth(quantity=1) {
if (this.isDone(quantity)) {
getDepth(q=1) {
if (this.isDone(q)) {
return 0;
}
let depth = 0;
@ -129,8 +129,8 @@ class ItemNode {
return depth + 1;
}
getFails(quantity=1) {
if (this.isDone(quantity)) {
getFails(q=1) {
if (this.isDone(q)) {
return 0;
}
let fails = 0;
@ -144,16 +144,12 @@ class ItemNode {
if (this.isReady()) {
return this;
}
let furthest_depth = -1;
let furthest_child = null;
for (let [child, quantity] of this.getChildren()) {
let depth = child.getDepth();
if (depth > furthest_depth) {
furthest_depth = depth;
furthest_child = child;
}
let res = child.getNext();
if (res)
return res;
}
return furthest_child.getNext();
return null;
}
async execute() {
@ -258,16 +254,16 @@ class ItemWrapper {
return this.getBestMethod().isDone(quantity);
}
getDepth() {
getDepth(q=1) {
if (this.methods.length === 0)
return 0;
return this.getBestMethod().getDepth();
return this.getBestMethod().getDepth(q);
}
getFails() {
getFails(q=1) {
if (this.methods.length === 0)
return 0;
return this.getBestMethod().getFails();
return this.getBestMethod().getFails(q);
}
getNext() {
@ -296,6 +292,7 @@ export class ItemGoal {
async executeNext() {
let next = this.goal.getNext();
// Prevent unnecessary attempts to obtain blocks that are not nearby
if (next.type === 'block' && !world.getNearbyBlockTypes(this.agent.bot).includes(next.source)) {
next.fails += 1;