2024-03-05 16:40:06 -08:00
|
|
|
import { Vec3 } from 'vec3';
|
|
|
|
import * as skills from '../library/skills.js';
|
|
|
|
import * as world from '../library/world.js';
|
|
|
|
import * as mc from '../../utils/mcdata.js';
|
2024-03-06 11:39:17 -08:00
|
|
|
import { blockSatisfied, getTypeOfGeneric } from './utils.js';
|
2024-03-05 16:40:06 -08:00
|
|
|
|
|
|
|
|
|
|
|
export class BuildGoal {
|
|
|
|
constructor(agent) {
|
|
|
|
this.agent = agent;
|
|
|
|
}
|
|
|
|
|
|
|
|
rotateXZ(x, z, orientation, sizex, sizez) {
|
|
|
|
if (orientation === 0) return [x, z];
|
|
|
|
if (orientation === 1) return [z, sizex-x-1];
|
|
|
|
if (orientation === 2) return [sizex-x-1, sizez-z-1];
|
|
|
|
if (orientation === 3) return [sizez-z-1, x];
|
|
|
|
}
|
|
|
|
|
|
|
|
async executeNext(goal, position=null, orientation=null) {
|
|
|
|
let sizex = goal.blocks[0][0].length;
|
|
|
|
let sizez = goal.blocks[0].length;
|
|
|
|
let sizey = goal.blocks.length;
|
|
|
|
if (!position) {
|
|
|
|
for (let x = 0; x < sizex - 1; x++) {
|
|
|
|
position = world.getNearestFreeSpace(this.agent.bot, sizex - x, 16);
|
|
|
|
if (position) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (orientation === null) {
|
|
|
|
orientation = Math.floor(Math.random() * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
let inventory = world.getInventoryCounts(this.agent.bot);
|
2024-03-06 12:18:00 -08:00
|
|
|
let missing = {};
|
2024-03-05 16:40:06 -08:00
|
|
|
let acted = false;
|
|
|
|
for (let y = goal.offset; y < sizey+goal.offset; y++) {
|
|
|
|
for (let z = 0; z < sizez; z++) {
|
|
|
|
for (let x = 0; x < sizex; x++) {
|
|
|
|
|
|
|
|
let [rx, rz] = this.rotateXZ(x, z, orientation, sizex, sizez);
|
|
|
|
let ry = y - goal.offset;
|
|
|
|
let block_name = goal.blocks[ry][rz][rx];
|
|
|
|
if (block_name === null || block_name === '') continue;
|
|
|
|
|
|
|
|
let world_pos = new Vec3(position.x + x, position.y + y, position.z + z);
|
|
|
|
let current_block = this.agent.bot.blockAt(world_pos);
|
|
|
|
|
|
|
|
let res = null;
|
2024-03-06 11:39:17 -08:00
|
|
|
if (!blockSatisfied(block_name, current_block)) {
|
2024-03-05 16:40:06 -08:00
|
|
|
acted = true;
|
|
|
|
|
|
|
|
if (!this.agent.isIdle())
|
|
|
|
return {missing: missing, acted: acted, position: position, orientation: orientation};
|
|
|
|
res = await this.agent.coder.execute(async () => {
|
|
|
|
await skills.breakBlockAt(this.agent.bot, world_pos.x, world_pos.y, world_pos.z);
|
|
|
|
});
|
|
|
|
if (res.interrupted)
|
|
|
|
return {missing: missing, acted: acted, position: position, orientation: orientation};
|
|
|
|
|
2024-03-06 11:39:17 -08:00
|
|
|
let block_typed = getTypeOfGeneric(this.agent.bot, block_name);
|
|
|
|
if (inventory[block_typed] > 0) {
|
2024-03-05 16:40:06 -08:00
|
|
|
|
|
|
|
if (!this.agent.isIdle())
|
|
|
|
return {missing: missing, acted: acted, position: position, orientation: orientation};
|
|
|
|
await this.agent.coder.execute(async () => {
|
2024-03-06 11:39:17 -08:00
|
|
|
await skills.placeBlock(this.agent.bot, block_typed, world_pos.x, world_pos.y, world_pos.z);
|
2024-03-05 16:40:06 -08:00
|
|
|
});
|
|
|
|
if (res.interrupted)
|
|
|
|
return {missing: missing, acted: acted, position: position, orientation: orientation};
|
|
|
|
|
|
|
|
} else {
|
2024-03-06 12:18:00 -08:00
|
|
|
if (missing[block_typed] === undefined)
|
|
|
|
missing[block_typed] = 0;
|
|
|
|
missing[block_typed]++;
|
2024-03-05 16:40:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {missing: missing, acted: acted, position: position, orientation: orientation};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|