From 5b46d3ee6d2865a7f72bdc38410479bb328df891 Mon Sep 17 00:00:00 2001 From: Isadora White Date: Fri, 27 Dec 2024 20:42:34 -0600 Subject: [PATCH] fixing bugs with the initial blueprint validation functions --- src/agent/commands/actions.js | 1 + src/agent/tasks.js | 43 +++++++++++++++++++---------------- test/test_check_blocks.js | 30 +++++++++++++++++++----- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/agent/commands/actions.js b/src/agent/commands/actions.js index 4cbe49f..bce7af9 100644 --- a/src/agent/commands/actions.js +++ b/src/agent/commands/actions.js @@ -414,6 +414,7 @@ export const actionsList = [ description: 'Check if the level is complete and what blocks still need to be placed for the blueprint', perform: runAsAction(async (agent, levelNum) => { return await checkLevelBlueprint(agent, levelNum); + //todo: if not complete, explain what still needs to be done }) }, { diff --git a/src/agent/tasks.js b/src/agent/tasks.js index 12f510c..0ab51bf 100644 --- a/src/agent/tasks.js +++ b/src/agent/tasks.js @@ -2,7 +2,7 @@ import { readFileSync } from 'fs'; import { executeCommand } from './commands/index.js'; import { getPosition } from './library/world.js' import settings from '../../settings.js'; -import { check } from 'yargs'; +import { Vec3 } from 'vec3'; export class CraftTaskValidator { constructor(data, agent) { @@ -45,11 +45,15 @@ export class ConstructionTaskValidator { try { //todo: somehow make this more of a percentage or something let valid = false; + let score = 0; this.blueprint.checkBluepint(this.agent.bot).then((result) => { if (result.mismatches.length === 0) { valid = true; console.log('Task is complete'); } + let total_blocks = result.mismatches.length + result.matches.length; + score = (result.matches.length / total_blocks) * 100; + console.log(`Task is ${score}% complete`); }); return valid; } catch (error) { @@ -62,14 +66,14 @@ export class ConstructionTaskValidator { export async function checkLevelBlueprint(agent, levelNum) { const blueprint = agent.task.blueprint.data; const bot = agent.bot; - const levelData = blueprint.levels[levelNum]; - return Blueprint.checkLevelBlueprint(bot, levelData); + //todo: in addition to checking the level, explain the differences + return blueprint.checkLevel(bot, levelNum); } export async function checkBlueprint(agent) { const blueprint = agent.task.blueprint.data; const bot = agent.bot; - return Blueprint.checkBlueprint(bot, blueprint); + return blueprint.check(bot); } export class Blueprint { constructor(blueprint) { @@ -103,43 +107,43 @@ export class Blueprint { explainLevel(levelNum) { const levelData = this.data.levels[levelNum]; var explanation = `Level ${levelData.level} `; - explanation += `at coordinates X: ${levelData.coordinates[0]}, Y: ${levelData.coordinates[1]}, Z: ${levelData.coordinates[2]}`; + explanation += `starting at coordinates X: ${levelData.coordinates[0]}, Y: ${levelData.coordinates[1]}, Z: ${levelData.coordinates[2]}`; let placement_string = this._getPlacementString(levelData.placement); explanation += `\n${placement_string}\n`; + return explanation; } - async explainBlueprintDifference(bot, blueprint) { + async explainBlueprintDifference(bot) { var explanation = ""; - for (let levelData of blueprint.levels) { - let level_explanation = await this.explainLevelDifference(bot, levelData); + const levels = this.data.levels; + for (let i = 0; i < levels.length; i++) { + let level_explanation = await this.explainLevelDifference(bot, i); explanation += level_explanation + "\n"; } return explanation; } - - async explainLevelDifference(bot, levelData) { - const results = await this.checkLevelBlueprint(bot, levelData); + async explainLevelDifference(bot, levelNum) { + const results = await this.checkLevel(bot, levelNum); const mismatches = results.mismatches; + const levelData = this.data.levels[levelNum]; if (mismatches.length === 0) { return `Level ${levelData.level} is correct`; } var explanation = `Level ${levelData.level} `; - explanation += `at coordinates X: ${levelData.coordinates[0]}, Y: ${levelData.coordinates[1]}, Z: ${levelData.coordinates[2]}`; + // explanation += `at coordinates X: ${levelData.coordinates[0]}, Y: ${levelData.coordinates[1]}, Z: ${levelData.coordinates[2]}`; explanation += " has the following mismatches:\n"; for (let item of mismatches) { - explanation += `At coordinates X: ${item.coordinates[0]}, Y: ${item.coordinates[1]}, Z: ${item.coordinates[2]}`; - explanation += `Expected ${item.expected}, but found ${item.actual}\n`; + explanation += `At coordinates X: ${item.coordinates[0]}, Y: ${item.coordinates[1]}, Z: ${item.coordinates[2]} `; + explanation += `expected ${item.expected}, but found ${item.actual}\n`; } return explanation; } - async checkBluepint(bot) { + async check(bot) { const levels = this.data.levels; const mismatches = []; const matches = []; - for (let i = 0; i < levels.length; i++) { - const levelData = levels[i]; - const result = await checkLevelBlueprint(bot, levelData); + const result = await this.checkLevel(bot, i); mismatches.push(...result.mismatches); matches.push(...result.matches); } @@ -148,7 +152,8 @@ export class Blueprint { "matches": matches }; } - async checkLevelBlueprint(bot, levelData) { + async checkLevel(bot, levelNum) { + const levelData = this.data.levels[levelNum]; const startCoords = levelData.coordinates; const placement = levelData.placement; const mismatches = []; diff --git a/test/test_check_blocks.js b/test/test_check_blocks.js index da90512..8acde84 100644 --- a/test/test_check_blocks.js +++ b/test/test_check_blocks.js @@ -1,5 +1,7 @@ import mineflayer from 'mineflayer'; import { Vec3 } from 'vec3'; +import { ConstructionTaskValidator, Blueprint } from '../src/agent/tasks.js'; +import { Agent } from '../src/agent/agent.js'; const bot = mineflayer.createBot({ host: 'localhost', // Replace with your server IP or hostname @@ -10,14 +12,28 @@ const bot = mineflayer.createBot({ bot.on('spawn', async () => { console.log("Bot spawned. Starting blueprint check..."); - await new Promise((resolve) => setTimeout(resolve, 10000)); + await new Promise((resolve) => setTimeout(resolve, 5000)); const blockAtLocation = await bot.blockAt(new Vec3(142, -60, -179)); console.log(blockAtLocation); - const results = await checkLevelBlueprint(bot, blueprintData.levels[0]); - console.log(results) - const matchesBlueprint = await checkBluepint(bot, blueprintData); - console.log(`Blueprint check result: \n`); - console.log(matchesBlueprint.mismatches) + const blueprint = new Blueprint(blueprintData); + console.log(blueprint.explain()); + console.log(blueprint.explainLevel(0)); + try { + const check_level = await blueprint.checkLevel(bot, 0); + console.log(check_level); + let check_blueprint = await blueprint.check(bot); + console.log(check_blueprint); + let level_diff = await blueprint.explainLevelDifference(bot, 0); + console.log(level_diff); + let blueprint_diff = await blueprint.explainBlueprintDifference(bot); + console.log(blueprint_diff); + } catch (err) { + console.error("Error checking blueprint:", err); + } + // console.log(blueprint.checkLevel(bot, 0)); + // console.log(blueprint.check(bot)); + // console.log(blueprint.explainBlueprintDifference(bot, blueprintData)); + // console.log(blueprint.explainLevelDifference(bot, 0)); bot.quit(); }); @@ -126,3 +142,5 @@ const blueprintData = { ] }; + +