mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-17 15:35:21 +02:00
Create craftHelper.js
This commit is contained in:
parent
70f3ebfc4f
commit
d70a7f7631
1 changed files with 61 additions and 0 deletions
61
metagpt/actions/minecraft/control_primitives/craftHelper.js
Normal file
61
metagpt/actions/minecraft/control_primitives/craftHelper.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
function failedCraftFeedback(bot, name, item, craftingTable) {
|
||||
const recipes = bot.recipesAll(item.id, null, craftingTable);
|
||||
if (!recipes.length) {
|
||||
throw new Error(`No crafting table nearby`);
|
||||
} else {
|
||||
const recipes = bot.recipesAll(
|
||||
item.id,
|
||||
null,
|
||||
mcData.blocksByName.crafting_table.id
|
||||
);
|
||||
// find the recipe with the fewest missing ingredients
|
||||
var min = 999;
|
||||
var min_recipe = null;
|
||||
for (const recipe of recipes) {
|
||||
const delta = recipe.delta;
|
||||
var missing = 0;
|
||||
for (const delta_item of delta) {
|
||||
if (delta_item.count < 0) {
|
||||
const inventory_item = bot.inventory.findInventoryItem(
|
||||
mcData.items[delta_item.id].name,
|
||||
null
|
||||
);
|
||||
if (!inventory_item) {
|
||||
missing += -delta_item.count;
|
||||
} else {
|
||||
missing += Math.max(
|
||||
-delta_item.count - inventory_item.count,
|
||||
0
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (missing < min) {
|
||||
min = missing;
|
||||
min_recipe = recipe;
|
||||
}
|
||||
}
|
||||
const delta = min_recipe.delta;
|
||||
let message = "";
|
||||
for (const delta_item of delta) {
|
||||
if (delta_item.count < 0) {
|
||||
const inventory_item = bot.inventory.findInventoryItem(
|
||||
mcData.items[delta_item.id].name,
|
||||
null
|
||||
);
|
||||
if (!inventory_item) {
|
||||
message += ` ${-delta_item.count} more ${
|
||||
mcData.items[delta_item.id].name
|
||||
}, `;
|
||||
} else {
|
||||
if (inventory_item.count < -delta_item.count) {
|
||||
message += `${
|
||||
-delta_item.count - inventory_item.count
|
||||
} more ${mcData.items[delta_item.id].name}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
bot.chat(`I cannot make ${name} because I need: ${message}`);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue