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.history = new History(this);
this.coder = new Coder(this); this.coder = new Coder(this);
this.item_goal = new ItemGoal(this); this.item_goal = new ItemGoal(this);
this.item_goal.setGoal('iron_pickaxe', 1);
console.log('Loading examples...'); console.log('Loading examples...');

View file

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