goal quantity

This commit is contained in:
Kolby Nottingham 2024-03-06 12:18:00 -08:00
parent 24965b2e06
commit 6571b3c87b
4 changed files with 47 additions and 31 deletions

View file

@ -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]++;
}
}
}

View file

@ -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;
}
}

View file

@ -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;

View file

@ -160,7 +160,7 @@ class ItemNode {
} else if (this.type === 'hunt') {
for (let i=0; i<quantity; i++) {
res = await skills.attackNearest(this.manager.agent.bot, this.source);
if (!res)
if (!res || this.manager.agent.bot.interrupt_code)
break;
}
} else if (this.type === 'craft') {
@ -285,13 +285,13 @@ export class ItemGoal {
this.failed = [];
}
async executeNext(item_name) {
async executeNext(item_name, item_quantity=1) {
if (this.nodes[item_name] === undefined)
this.nodes[item_name] = new ItemWrapper(this, null, item_name);
this.goal = this.nodes[item_name];
// Get next goal to execute
let next_info = this.goal.getNext();
let next_info = this.goal.getNext(item_quantity);
if (!next_info) {
console.log(`Invalid item goal ${this.goal.name}`);
return;