diff --git a/src/agent/npc/build_goal.js b/src/agent/npc/build_goal.js index 7ca0991..e496a30 100644 --- a/src/agent/npc/build_goal.js +++ b/src/agent/npc/build_goal.js @@ -32,7 +32,7 @@ export class BuildGoal { } let inventory = world.getInventoryCounts(this.agent.bot); - let missing = []; + let missing = {}; let acted = false; for (let y = goal.offset; y < sizey+goal.offset; y++) { for (let z = 0; z < sizez; z++) { @@ -70,7 +70,9 @@ export class BuildGoal { return {missing: missing, acted: acted, position: position, orientation: orientation}; } else { - missing.push(block_typed); + if (missing[block_typed] === undefined) + missing[block_typed] = 0; + missing[block_typed]++; } } } diff --git a/src/agent/npc/controller.js b/src/agent/npc/controller.js index 93826e0..76c432c 100644 --- a/src/agent/npc/controller.js +++ b/src/agent/npc/controller.js @@ -40,38 +40,43 @@ export class NPCContoller { } async executeNext() { - let goals = this.data.goals; - if (this.temp_goals !== null && this.temp_goals.length > 0) { - goals = this.temp_goals.concat(goals); - } + // If we need more blocks to complete a building, get those first + let goals = this.temp_goals.concat(this.data.goals); for (let goal of goals) { - if (this.constructions[goal] === undefined) { - let quantity = 0; - for (let item of goals) { - if (item === goal) quantity++; - } - if (!itemSatisfied(this.agent.bot, goal, quantity)) { - await this.item_goal.executeNext(goal); + + // Obtain goal item or block + if (this.constructions[goal.name] === undefined) { + if (!itemSatisfied(this.agent.bot, goal.name, goal.quantity)) { + await this.item_goal.executeNext(goal.name, goal.quantity); break; } - } else { + } + + // Build construction goal + else { let res = null; - if (this.data.built.hasOwnProperty(goal)) { + if (this.data.built.hasOwnProperty(goal.name)) { res = await this.build_goal.executeNext( - this.constructions[goal], - this.data.built[goal].position, - this.data.built[goal].orientation + this.constructions[goal.name], + this.data.built[goal.name].position, + this.data.built[goal.name].orientation ); } else { - res = await this.build_goal.executeNext(this.constructions[goal]); - this.data.built[goal] = { - name: goal, + res = await this.build_goal.executeNext(this.constructions[goal.name]); + this.data.built[goal.name] = { + name: goal.name, position: res.position, orientation: res.orientation }; } - this.temp_goals = res.missing; + this.temp_goals = []; + for (let block_name in res.missing) { + this.temp_goals.push({ + name: block_name, + quantity: res.missing[block_name] + }) + } if (res.acted) break; } } diff --git a/src/agent/npc/data.js b/src/agent/npc/data.js index 7f03705..b160c79 100644 --- a/src/agent/npc/data.js +++ b/src/agent/npc/data.js @@ -5,17 +5,26 @@ export class NPCData { } toObject() { - return { - goals: this.goals, - built: this.built - } + let obj = {}; + if (this.goals.length > 0) + obj.goals = this.goals; + if (Object.keys(this.built).length > 0) + obj.built = this.built; + return obj; } static fromObject(obj) { if (!obj) return null; let npc = new NPCData(); - if (obj.goals) - npc.goals = obj.goals; + if (obj.goals) { + npc.goals = []; + for (let goal of obj.goals) { + if (typeof goal === 'string') + npc.goals.push({name: goal, quantity: 1}); + else + npc.goals.push({name: goal.name, quantity: goal.quantity}); + } + } if (obj.built) npc.built = obj.built; return npc; diff --git a/src/agent/npc/item_goal.js b/src/agent/npc/item_goal.js index b94bcb7..dea4a53 100644 --- a/src/agent/npc/item_goal.js +++ b/src/agent/npc/item_goal.js @@ -160,7 +160,7 @@ class ItemNode { } else if (this.type === 'hunt') { for (let i=0; i