mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-08 16:25:33 +02:00
fixing bugs with the initial blueprint validation functions
This commit is contained in:
parent
acdc73ad47
commit
5b46d3ee6d
3 changed files with 49 additions and 25 deletions
|
@ -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',
|
description: 'Check if the level is complete and what blocks still need to be placed for the blueprint',
|
||||||
perform: runAsAction(async (agent, levelNum) => {
|
perform: runAsAction(async (agent, levelNum) => {
|
||||||
return await checkLevelBlueprint(agent, levelNum);
|
return await checkLevelBlueprint(agent, levelNum);
|
||||||
|
//todo: if not complete, explain what still needs to be done
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { readFileSync } from 'fs';
|
||||||
import { executeCommand } from './commands/index.js';
|
import { executeCommand } from './commands/index.js';
|
||||||
import { getPosition } from './library/world.js'
|
import { getPosition } from './library/world.js'
|
||||||
import settings from '../../settings.js';
|
import settings from '../../settings.js';
|
||||||
import { check } from 'yargs';
|
import { Vec3 } from 'vec3';
|
||||||
|
|
||||||
export class CraftTaskValidator {
|
export class CraftTaskValidator {
|
||||||
constructor(data, agent) {
|
constructor(data, agent) {
|
||||||
|
@ -45,11 +45,15 @@ export class ConstructionTaskValidator {
|
||||||
try {
|
try {
|
||||||
//todo: somehow make this more of a percentage or something
|
//todo: somehow make this more of a percentage or something
|
||||||
let valid = false;
|
let valid = false;
|
||||||
|
let score = 0;
|
||||||
this.blueprint.checkBluepint(this.agent.bot).then((result) => {
|
this.blueprint.checkBluepint(this.agent.bot).then((result) => {
|
||||||
if (result.mismatches.length === 0) {
|
if (result.mismatches.length === 0) {
|
||||||
valid = true;
|
valid = true;
|
||||||
console.log('Task is complete');
|
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;
|
return valid;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -62,14 +66,14 @@ export class ConstructionTaskValidator {
|
||||||
export async function checkLevelBlueprint(agent, levelNum) {
|
export async function checkLevelBlueprint(agent, levelNum) {
|
||||||
const blueprint = agent.task.blueprint.data;
|
const blueprint = agent.task.blueprint.data;
|
||||||
const bot = agent.bot;
|
const bot = agent.bot;
|
||||||
const levelData = blueprint.levels[levelNum];
|
//todo: in addition to checking the level, explain the differences
|
||||||
return Blueprint.checkLevelBlueprint(bot, levelData);
|
return blueprint.checkLevel(bot, levelNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function checkBlueprint(agent) {
|
export async function checkBlueprint(agent) {
|
||||||
const blueprint = agent.task.blueprint.data;
|
const blueprint = agent.task.blueprint.data;
|
||||||
const bot = agent.bot;
|
const bot = agent.bot;
|
||||||
return Blueprint.checkBlueprint(bot, blueprint);
|
return blueprint.check(bot);
|
||||||
}
|
}
|
||||||
export class Blueprint {
|
export class Blueprint {
|
||||||
constructor(blueprint) {
|
constructor(blueprint) {
|
||||||
|
@ -103,43 +107,43 @@ export class Blueprint {
|
||||||
explainLevel(levelNum) {
|
explainLevel(levelNum) {
|
||||||
const levelData = this.data.levels[levelNum];
|
const levelData = this.data.levels[levelNum];
|
||||||
var explanation = `Level ${levelData.level} `;
|
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);
|
let placement_string = this._getPlacementString(levelData.placement);
|
||||||
explanation += `\n${placement_string}\n`;
|
explanation += `\n${placement_string}\n`;
|
||||||
|
return explanation;
|
||||||
}
|
}
|
||||||
async explainBlueprintDifference(bot, blueprint) {
|
async explainBlueprintDifference(bot) {
|
||||||
var explanation = "";
|
var explanation = "";
|
||||||
for (let levelData of blueprint.levels) {
|
const levels = this.data.levels;
|
||||||
let level_explanation = await this.explainLevelDifference(bot, levelData);
|
for (let i = 0; i < levels.length; i++) {
|
||||||
|
let level_explanation = await this.explainLevelDifference(bot, i);
|
||||||
explanation += level_explanation + "\n";
|
explanation += level_explanation + "\n";
|
||||||
}
|
}
|
||||||
return explanation;
|
return explanation;
|
||||||
}
|
}
|
||||||
|
async explainLevelDifference(bot, levelNum) {
|
||||||
async explainLevelDifference(bot, levelData) {
|
const results = await this.checkLevel(bot, levelNum);
|
||||||
const results = await this.checkLevelBlueprint(bot, levelData);
|
|
||||||
const mismatches = results.mismatches;
|
const mismatches = results.mismatches;
|
||||||
|
const levelData = this.data.levels[levelNum];
|
||||||
|
|
||||||
if (mismatches.length === 0) {
|
if (mismatches.length === 0) {
|
||||||
return `Level ${levelData.level} is correct`;
|
return `Level ${levelData.level} is correct`;
|
||||||
}
|
}
|
||||||
var explanation = `Level ${levelData.level} `;
|
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";
|
explanation += " has the following mismatches:\n";
|
||||||
for (let item of mismatches) {
|
for (let item of mismatches) {
|
||||||
explanation += `At coordinates X: ${item.coordinates[0]}, Y: ${item.coordinates[1]}, Z: ${item.coordinates[2]} `;
|
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 += `expected ${item.expected}, but found ${item.actual}\n`;
|
||||||
}
|
}
|
||||||
return explanation;
|
return explanation;
|
||||||
}
|
}
|
||||||
async checkBluepint(bot) {
|
async check(bot) {
|
||||||
const levels = this.data.levels;
|
const levels = this.data.levels;
|
||||||
const mismatches = [];
|
const mismatches = [];
|
||||||
const matches = [];
|
const matches = [];
|
||||||
|
|
||||||
for (let i = 0; i < levels.length; i++) {
|
for (let i = 0; i < levels.length; i++) {
|
||||||
const levelData = levels[i];
|
const result = await this.checkLevel(bot, i);
|
||||||
const result = await checkLevelBlueprint(bot, levelData);
|
|
||||||
mismatches.push(...result.mismatches);
|
mismatches.push(...result.mismatches);
|
||||||
matches.push(...result.matches);
|
matches.push(...result.matches);
|
||||||
}
|
}
|
||||||
|
@ -148,7 +152,8 @@ export class Blueprint {
|
||||||
"matches": matches
|
"matches": matches
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
async checkLevelBlueprint(bot, levelData) {
|
async checkLevel(bot, levelNum) {
|
||||||
|
const levelData = this.data.levels[levelNum];
|
||||||
const startCoords = levelData.coordinates;
|
const startCoords = levelData.coordinates;
|
||||||
const placement = levelData.placement;
|
const placement = levelData.placement;
|
||||||
const mismatches = [];
|
const mismatches = [];
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import mineflayer from 'mineflayer';
|
import mineflayer from 'mineflayer';
|
||||||
import { Vec3 } from 'vec3';
|
import { Vec3 } from 'vec3';
|
||||||
|
import { ConstructionTaskValidator, Blueprint } from '../src/agent/tasks.js';
|
||||||
|
import { Agent } from '../src/agent/agent.js';
|
||||||
|
|
||||||
const bot = mineflayer.createBot({
|
const bot = mineflayer.createBot({
|
||||||
host: 'localhost', // Replace with your server IP or hostname
|
host: 'localhost', // Replace with your server IP or hostname
|
||||||
|
@ -10,14 +12,28 @@ const bot = mineflayer.createBot({
|
||||||
|
|
||||||
bot.on('spawn', async () => {
|
bot.on('spawn', async () => {
|
||||||
console.log("Bot spawned. Starting blueprint check...");
|
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));
|
const blockAtLocation = await bot.blockAt(new Vec3(142, -60, -179));
|
||||||
console.log(blockAtLocation);
|
console.log(blockAtLocation);
|
||||||
const results = await checkLevelBlueprint(bot, blueprintData.levels[0]);
|
const blueprint = new Blueprint(blueprintData);
|
||||||
console.log(results)
|
console.log(blueprint.explain());
|
||||||
const matchesBlueprint = await checkBluepint(bot, blueprintData);
|
console.log(blueprint.explainLevel(0));
|
||||||
console.log(`Blueprint check result: \n`);
|
try {
|
||||||
console.log(matchesBlueprint.mismatches)
|
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();
|
bot.quit();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -126,3 +142,5 @@ const blueprintData = {
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue