fix digdown

This commit is contained in:
MaxRobinsonTheGreat 2025-03-13 15:42:23 -05:00
parent cbe5804f73
commit ef37a400b0
2 changed files with 34 additions and 28 deletions

View file

@ -409,8 +409,8 @@ export const actionsList = [
},
{
name: '!digDown',
description: 'Digs down a specified distance.',
params: {'distance': { type: 'int', description: 'Distance to dig down'}},
description: 'Digs down a specified distance. Will stop if it reaches lava, water, or a fall of >=4 blocks below the bot.',
params: {'distance': { type: 'int', description: 'Distance to dig down', domain: [1, Number.MAX_SAFE_INTEGER] }},
perform: runAsAction(async (agent, distance) => {
await skills.digDown(agent.bot, distance)
})

View file

@ -1379,51 +1379,57 @@ export async function activateNearestBlock(bot, type) {
export async function digDown(bot, distance = 10) {
/**
* Digs down a specified distance.
* Digs down a specified distance. Will stop if it reaches lava, water, or a fall of >=4 blocks below the bot.
* @param {MinecraftBot} bot, reference to the minecraft bot.
* @param {int} distance, distance to dig down.
* @returns {Promise<boolean>} true if successfully dug down.
* @returns {Promise<boolean>} true if successfully dug all the way down.
* @example
* await skills.digDown(bot, 10);
**/
for (let i = 0; i < distance; i++) {
const targetBlock = bot.blockAt(bot.entity.position.offset(0, -1, 0));
const belowBlock = bot.blockAt(bot.entity.position.offset(0, -2, 0));
let start_block_pos = bot.blockAt(bot.entity.position).position;
for (let i = 1; i <= distance; i++) {
const targetBlock = bot.blockAt(start_block_pos.offset(0, -i, 0));
let belowBlock = bot.blockAt(start_block_pos.offset(0, -i-1, 0));
if (!targetBlock || !belowBlock) {
log(bot, `Dug down ${i-1} blocks, but reached the end of the world.`);
return true;
}
// Check for lava, water
if (!targetBlock || targetBlock.name === 'lava' || targetBlock.name === 'water' ||
(belowBlock && (belowBlock.name === 'lava' || belowBlock.name === 'water'))) {
console.log(`Dug down ${i} blocks, but reached ${belowBlock ? belowBlock.name : '(lava/water)'}`);
log(bot, `Dug down ${i} blocks, but reached ${belowBlock ? belowBlock.name : '(lava/water)'}`)
if (targetBlock.name === 'lava' || targetBlock.name === 'water' ||
belowBlock.name === 'lava' || belowBlock.name === 'water') {
log(bot, `Dug down ${i-1} blocks, but reached ${belowBlock ? belowBlock.name : '(lava/water)'}`)
return false;
}
// Check for a fall of more than 5 blocks below the bot
let isSafe = false;
for (let j = 1; j <= 5; j++) {
const belowBlock = bot.blockAt(bot.entity.position.offset(0, -j-1, 0));
if (!belowBlock || belowBlock.name !== 'air') {
isSafe = true;
const MAX_FALL_BLOCKS = 2;
let num_fall_blocks = 0;
for (let j = 0; j <= MAX_FALL_BLOCKS; j++) {
if (!belowBlock || (belowBlock.name !== 'air' && belowBlock.name !== 'cave_air')) {
break;
}
num_fall_blocks++;
belowBlock = bot.blockAt(belowBlock.position.offset(0, -1, 0));
}
if (!targetBlock || !isSafe) {
console.log(`Dug down ${i} blocks, but reached fall`);
log(bot, `Dug down ${i} blocks, but reached fall`);
if (num_fall_blocks > MAX_FALL_BLOCKS) {
log(bot, `Dug down ${i-1} blocks, but reached a drop below the next block.`);
return false;
}
if (bot.canDigBlock(targetBlock)) {
await breakBlockAt(bot, bot.entity.position.x, bot.entity.position.y - 1, bot.entity.position.z);
await bot.waitForTicks(10); // wait for a short period to avoid issues
await bot.entity.position.offset(0, -1, 0);
} else {
console.log('Cannot dig block at position:', bot.entity.position.offset(0, -1, 0));
log(bot, 'Cannot dig block at position:' + bot.entity.position.offset(0, -1, 0))
if (targetBlock.name === 'air' || targetBlock.name === 'cave_air') {
log(bot, 'Skipping air block');
console.log(targetBlock.position);
continue;
}
let dug = await breakBlockAt(bot, targetBlock.position.x, targetBlock.position.y, targetBlock.position.z);
if (!dug) {
log(bot, 'Failed to dig block at position:' + targetBlock.position);
return false;
}
}
log(bot, `Dug down ${distance} blocks.`);
return true;
}