From 43943653d33c379a875d18d6335fde5d6c488bd2 Mon Sep 17 00:00:00 2001 From: Kolby Nottingham Date: Wed, 6 Mar 2024 13:18:23 -0800 Subject: [PATCH] sort nearest blocks --- src/agent/library/skills.js | 11 +++++-- src/agent/library/world.js | 58 ++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/agent/library/skills.js b/src/agent/library/skills.js index 2c4be54..a3732b9 100644 --- a/src/agent/library/skills.js +++ b/src/agent/library/skills.js @@ -342,7 +342,7 @@ export async function defendSelf(bot, range=9) { -export async function collectBlock(bot, blockType, num=1) { +export async function collectBlock(bot, blockType, num=1, exclude=null) { /** * Collect one of the given block type. * @param {MinecraftBot} bot, reference to the minecraft bot. @@ -363,7 +363,14 @@ export async function collectBlock(bot, blockType, num=1) { let collected = 0; for (let i=0; i block.position.x !== position.x || block.position.y !== position.y || block.position.z !== position.z + ); + } + } if (blocks.length === 0) { if (collected === 0) log(bot, `No ${blockType} nearby to collect.`); diff --git a/src/agent/library/world.js b/src/agent/library/world.js index 2bb00bb..280f410 100644 --- a/src/agent/library/world.js +++ b/src/agent/library/world.js @@ -38,13 +38,32 @@ export function getNearestFreeSpace(bot, size=1, distance=8) { } -export function getNearestBlocks(bot, block_types, distance=16, count=1) { +export function getNearbyBlocks(bot, maxDistance, count=null) { + if (maxDistance == null) maxDistance = 16; + if (count == null) count = 10000; + let positions = bot.findBlocks({matching: getAllBlockIds(['air']), maxDistance: maxDistance, count: count}); + let blocks = []; + for (let i = 0; i < positions.length; i++) { + let block = bot.blockAt(positions[i]); + let distance = positions[i].distanceTo(bot.entity.position); + blocks.push({ block: block, distance: distance }); + } + blocks.sort((a, b) => a.distance - b.distance); + let res = []; + for (let i = 0; i < blocks.length; i++) { + res.push(blocks[i].block); + } + return res; +} + + +export function getNearestBlocks(bot, block_types, distance=16, count=null) { /** * Get a list of the nearest blocks of the given types. * @param {Bot} bot - The bot to get the nearest block for. * @param {string[]} block_types - The names of the blocks to search for. * @param {number} distance - The maximum distance to search, default 16. - * @param {number} count - The maximum number of blocks to find, default 1. + * @param {number} count - The maximum number of blocks to find, default 10000. * @returns {Block[]} - The nearest blocks of the given type. * @example * let woodBlocks = world.getNearestBlocks(bot, ['oak_log', 'birch_log'], 16, 1); @@ -52,16 +71,13 @@ export function getNearestBlocks(bot, block_types, distance=16, count=1) { // if blocktypes is not a list, make it a list if (!Array.isArray(block_types)) block_types = [block_types]; - let block_locs = bot.findBlocks({ - matching: (block) => { - return block && block_types.some(name => name === block.name); - }, - maxDistance: distance, - count: count - }); let blocks = []; - for (let i = 0; i < block_locs.length; i++) { - blocks.push(bot.blockAt(block_locs[i])); + for (let block of getNearbyBlocks(bot, distance, count)) { + if (block_types.includes(block.name)) { + blocks.push(block); + if (blocks.length >= count) + break; + } } return blocks; } @@ -77,7 +93,7 @@ export function getNearestBlock(bot, block_type, distance=16) { * @example * let coalBlock = world.getNearestBlock(bot, 'coal_ore', 16); **/ - let blocks = getNearestBlocks(bot, [block_type], distance, 1); + let blocks = getNearestBlocks(bot, block_type, distance, 1); if (blocks.length > 0) { return blocks[0]; } @@ -85,24 +101,6 @@ export function getNearestBlock(bot, block_type, distance=16) { } -export function getNearbyBlocks(bot, maxDistance) { - if (maxDistance == null) maxDistance = 16; - let positions = bot.findBlocks({matching: getAllBlockIds(['air']), maxDistance: maxDistance, count: 10000}); - let blocks = []; - for (let i = 0; i < positions.length; i++) { - let block = bot.blockAt(positions[i]); - let distance = positions[i].distanceTo(bot.entity.position); - blocks.push({ block: block, distance: distance }); - } - blocks.sort((a, b) => a.distance - b.distance); - let res = []; - for (let i = 0; i < blocks.length; i++) { - res.push(blocks[i].block); - } - return res; -} - - export function getNearbyEntities(bot, maxDistance=16) { let entities = []; for (const entity of Object.values(bot.entities)) {