mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-13 10:45:34 +02:00
sort nearest blocks
This commit is contained in:
parent
cb9de90d11
commit
43943653d3
2 changed files with 37 additions and 32 deletions
|
@ -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.
|
* Collect one of the given block type.
|
||||||
* @param {MinecraftBot} bot, reference to the minecraft bot.
|
* @param {MinecraftBot} bot, reference to the minecraft bot.
|
||||||
|
@ -363,7 +363,14 @@ export async function collectBlock(bot, blockType, num=1) {
|
||||||
let collected = 0;
|
let collected = 0;
|
||||||
|
|
||||||
for (let i=0; i<num; i++) {
|
for (let i=0; i<num; i++) {
|
||||||
const blocks = world.getNearestBlocks(bot, blocktypes, 64, 1);
|
const blocks = world.getNearestBlocks(bot, blocktypes, 64);
|
||||||
|
if (exclude) {
|
||||||
|
for (let position of exclude) {
|
||||||
|
blocks = blocks.filter(
|
||||||
|
block => block.position.x !== position.x || block.position.y !== position.y || block.position.z !== position.z
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (blocks.length === 0) {
|
if (blocks.length === 0) {
|
||||||
if (collected === 0)
|
if (collected === 0)
|
||||||
log(bot, `No ${blockType} nearby to collect.`);
|
log(bot, `No ${blockType} nearby to collect.`);
|
||||||
|
|
|
@ -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.
|
* Get a list of the nearest blocks of the given types.
|
||||||
* @param {Bot} bot - The bot to get the nearest block for.
|
* @param {Bot} bot - The bot to get the nearest block for.
|
||||||
* @param {string[]} block_types - The names of the blocks to search 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} 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.
|
* @returns {Block[]} - The nearest blocks of the given type.
|
||||||
* @example
|
* @example
|
||||||
* let woodBlocks = world.getNearestBlocks(bot, ['oak_log', 'birch_log'], 16, 1);
|
* 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 blocktypes is not a list, make it a list
|
||||||
if (!Array.isArray(block_types))
|
if (!Array.isArray(block_types))
|
||||||
block_types = [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 = [];
|
let blocks = [];
|
||||||
for (let i = 0; i < block_locs.length; i++) {
|
for (let block of getNearbyBlocks(bot, distance, count)) {
|
||||||
blocks.push(bot.blockAt(block_locs[i]));
|
if (block_types.includes(block.name)) {
|
||||||
|
blocks.push(block);
|
||||||
|
if (blocks.length >= count)
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return blocks;
|
return blocks;
|
||||||
}
|
}
|
||||||
|
@ -77,7 +93,7 @@ export function getNearestBlock(bot, block_type, distance=16) {
|
||||||
* @example
|
* @example
|
||||||
* let coalBlock = world.getNearestBlock(bot, 'coal_ore', 16);
|
* 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) {
|
if (blocks.length > 0) {
|
||||||
return blocks[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) {
|
export function getNearbyEntities(bot, maxDistance=16) {
|
||||||
let entities = [];
|
let entities = [];
|
||||||
for (const entity of Object.values(bot.entities)) {
|
for (const entity of Object.values(bot.entities)) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue