From 6a7820ba7609879dd82ba589366b493533448318 Mon Sep 17 00:00:00 2001 From: ShadowAlzazel Date: Sat, 16 Nov 2024 17:50:45 -0800 Subject: [PATCH 1/4] Environmental Awarenss Feature --- src/agent/commands/queries.js | 7 ++++ src/agent/library/world.js | 71 +++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/src/agent/commands/queries.js b/src/agent/commands/queries.js index 2ce3428..3d59135 100644 --- a/src/agent/commands/queries.js +++ b/src/agent/commands/queries.js @@ -17,6 +17,13 @@ export const queryList = [ let pos = bot.entity.position; // display position to 2 decimal places res += `\n- Position: x: ${pos.x.toFixed(2)}, y: ${pos.y.toFixed(2)}, z: ${pos.z.toFixed(2)}`; + // Environmental Awareness + res += `\n- Block Above: ${world.getBlockAtPosition(bot, 0, 2, 0).name}`; + res += `\n- Block Below: ${world.getBlockAtPosition(bot, 0, -1, 0).name}`; + res += `\n- Block at Head: ${world.getBlockAtPosition(bot, 0, 1, 0).name}`; + res += `\n- Block at Legs: ${world.getBlockAtPosition(bot, 0, 0, 0).name}`; + res += `\n- Lowest Block Above: ${world.getLowestBlock(bot, null, null, 32).name}`; + // Gameplay res += `\n- Gamemode: ${bot.game.gameMode}`; res += `\n- Health: ${Math.round(bot.health)} / 20`; res += `\n- Hunger: ${Math.round(bot.food)} / 20`; diff --git a/src/agent/library/world.js b/src/agent/library/world.js index 01d54c3..949838e 100644 --- a/src/agent/library/world.js +++ b/src/agent/library/world.js @@ -39,6 +39,77 @@ export function getNearestFreeSpace(bot, size=1, distance=8) { } +export function getBlockAtPosition(bot, x=0, y=0, z=0) { + /** + * Get a block from the bot's relative position + * @param {Bot} bot - The bot to get the block for. + * @param {number} x - The relative x offset to serach, default 0. + * @param {number} y - The relative y offset to serach, default 0. + * @param {number} y - The relative z offset to serach, default 0. + * @returns {Block} - The nearest block. + * @example + * let blockBelow = world.getBlockAtPosition(bot, 0, -1, 0); + * let blockAbove = world.getBlockAtPosition(bot, 0, 2, 0); since minecraft position is at the feet + **/ + let block = bot.blockAt(bot.entity.position.offset(x, y, z)); + if (!block) block = {name: 'air'}; + + return block; +} + + +export function getLowestBlock(bot, block_types=null, ignore_types=null, distance=32) { + /** + * Searches a column from the bot's position for the lowest block + * @param {Bot} bot - The bot to get the block for. + * @param {string[]} block_types - The names of the blocks to search for. + * @param {string[]} block_types - The names of the blocks to ignore. + * @param {number} distance - The maximum distance to search, default 32. + * @returns {Block} - The lowest block. + * @example + * let lowestBlock = world.getLowestBlock(bot, 0, -1, 0); + **/ + // if blocktypes is not a list, make it a list + let search_blocks = []; + if (block_types) { + if (!Array.isArray(block_types)) + block_types = [block_types]; + for(let block_type of block_types) { + if (mc.getBlockId(block_type)) search_blocks.push(block_type); + } + } + // if ignore_types is not a list, make it a list + let ignore_blocks = []; + if (ignore_types === null) ignore_blocks = ['air']; + else { + if (!Array.isArray(ignore_types)) + ignore_types = [ignore_types]; + for(let ingnore_type of ignore_types) { + if (mc.getBlockId(ingnore_type)) ignore_blocks.push(ingnore_type); + } + } + // The lowest block, stops when it finds a block + let lowest_block = {name: 'air'}; + for (let i = 0; i < distance; i++) { + let block = bot.blockAt(bot.entity.position.offset(0, i+2, 0)); + if (!block) block = {name: 'air'}; + // Ignore and continue + if (ignore_blocks.includes(block.name)) continue; + // Defaults to any block + else if (block_types === null) { + lowest_block = block; + break; + } + else if (search_blocks.includes(block.name)) { + lowest_block = block; + break; + } + } + + return lowest_block; +} + + export function getNearestBlocks(bot, block_types=null, distance=16, count=10000) { /** * Get a list of the nearest blocks of the given types. From 67eb491568c4869d41a04b3dda3e11db3f43aa1f Mon Sep 17 00:00:00 2001 From: ShadowAlzazel Date: Sat, 16 Nov 2024 18:54:25 -0800 Subject: [PATCH 2/4] Added wood types for cherry and mangrove Added descriptors for !nearbyBlocks --- src/agent/commands/queries.js | 8 ++++++++ src/agent/library/world.js | 2 +- src/utils/mcdata.js | 6 ++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/agent/commands/queries.js b/src/agent/commands/queries.js index 3d59135..19d5844 100644 --- a/src/agent/commands/queries.js +++ b/src/agent/commands/queries.js @@ -105,6 +105,14 @@ export const queryList = [ } if (blocks.length == 0) { res += ': none'; + } + else { + // Environmental Awareness + res += `\nBLOCK_ABOVE: ${world.getBlockAtPosition(bot, 0, 2, 0).name}`; + res += `\nBLOCK_BELOW: ${world.getBlockAtPosition(bot, 0, -1, 0).name}`; + res += `\nBLOCK_AT_HEAD: ${world.getBlockAtPosition(bot, 0, 1, 0).name}`; + res += `\nBLOCK_AT_LEGS: ${world.getBlockAtPosition(bot, 0, 0, 0).name}`; + res += `\nLOWEST_BLOCK_ABOVE: ${world.getLowestBlock(bot, null, null, 32).name}`; } return pad(res); } diff --git a/src/agent/library/world.js b/src/agent/library/world.js index 949838e..62570d3 100644 --- a/src/agent/library/world.js +++ b/src/agent/library/world.js @@ -67,7 +67,7 @@ export function getLowestBlock(bot, block_types=null, ignore_types=null, distanc * @param {number} distance - The maximum distance to search, default 32. * @returns {Block} - The lowest block. * @example - * let lowestBlock = world.getLowestBlock(bot, 0, -1, 0); + * let lowestBlock = world.getLowestBlock(bot, null, null, 32); **/ // if blocktypes is not a list, make it a list let search_blocks = []; diff --git a/src/utils/mcdata.js b/src/utils/mcdata.js index 4df8b2a..58cfbdb 100644 --- a/src/utils/mcdata.js +++ b/src/utils/mcdata.js @@ -18,7 +18,7 @@ const Item = prismarine_items(mc_version); * @typedef {string} BlockName */ -export const WOOD_TYPES = ['oak', 'spruce', 'birch', 'jungle', 'acacia', 'dark_oak']; +export const WOOD_TYPES = ['oak', 'spruce', 'birch', 'jungle', 'acacia', 'dark_oak', 'mangrove', 'cherry']; export const MATCHING_WOOD_BLOCKS = [ 'log', 'planks', @@ -202,7 +202,7 @@ export function isSmeltable(itemName) { } export function getSmeltingFuel(bot) { - let fuel = bot.inventory.items().find(i => i.name === 'coal' || i.name === 'charcoal') + let fuel = bot.inventory.items().find(i => i.name === 'coal' || i.name === 'charcoal' || i.name === 'blaze_rod') if (fuel) return fuel; fuel = bot.inventory.items().find(i => i.name.includes('log') || i.name.includes('planks')) @@ -214,6 +214,8 @@ export function getSmeltingFuel(bot) { export function getFuelSmeltOutput(fuelName) { if (fuelName === 'coal' || fuelName === 'charcoal') return 8; + if (fuelName === 'blaze_rod') + return 12; if (fuelName.includes('log') || fuelName.includes('planks')) return 1.5 if (fuelName === 'coal_block') From 154e75d1994f66b04d8fdb3b292250a27bc6fa1e Mon Sep 17 00:00:00 2001 From: ShadowAlzazel Date: Tue, 19 Nov 2024 00:07:36 -0800 Subject: [PATCH 3/4] Added Requested Changes for Awareness --- src/agent/commands/queries.js | 14 +++----- src/agent/library/world.js | 66 +++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 41 deletions(-) diff --git a/src/agent/commands/queries.js b/src/agent/commands/queries.js index 19d5844..5b3ec58 100644 --- a/src/agent/commands/queries.js +++ b/src/agent/commands/queries.js @@ -18,11 +18,8 @@ export const queryList = [ // display position to 2 decimal places res += `\n- Position: x: ${pos.x.toFixed(2)}, y: ${pos.y.toFixed(2)}, z: ${pos.z.toFixed(2)}`; // Environmental Awareness - res += `\n- Block Above: ${world.getBlockAtPosition(bot, 0, 2, 0).name}`; - res += `\n- Block Below: ${world.getBlockAtPosition(bot, 0, -1, 0).name}`; - res += `\n- Block at Head: ${world.getBlockAtPosition(bot, 0, 1, 0).name}`; - res += `\n- Block at Legs: ${world.getBlockAtPosition(bot, 0, 0, 0).name}`; - res += `\n- Lowest Block Above: ${world.getLowestBlock(bot, null, null, 32).name}`; + res += '\n- ' + world.getSurroundingBlocks(bot).join('\n- ') + res += `\n- First Solid Block Above Head: ${world.getFirstBlockAboveHead(bot, null, 32)}`; // Gameplay res += `\n- Gamemode: ${bot.game.gameMode}`; res += `\n- Health: ${Math.round(bot.health)} / 20`; @@ -108,11 +105,8 @@ export const queryList = [ } else { // Environmental Awareness - res += `\nBLOCK_ABOVE: ${world.getBlockAtPosition(bot, 0, 2, 0).name}`; - res += `\nBLOCK_BELOW: ${world.getBlockAtPosition(bot, 0, -1, 0).name}`; - res += `\nBLOCK_AT_HEAD: ${world.getBlockAtPosition(bot, 0, 1, 0).name}`; - res += `\nBLOCK_AT_LEGS: ${world.getBlockAtPosition(bot, 0, 0, 0).name}`; - res += `\nLOWEST_BLOCK_ABOVE: ${world.getLowestBlock(bot, null, null, 32).name}`; + res += '\n- ' + world.getSurroundingBlocks(bot).join('\n- ') + res += `\n- First Solid Block Above Head: ${world.getFirstBlockAboveHead(bot, null, 32)}`; } return pad(res); } diff --git a/src/agent/library/world.js b/src/agent/library/world.js index 62570d3..a6428c0 100644 --- a/src/agent/library/world.js +++ b/src/agent/library/world.js @@ -58,29 +58,37 @@ export function getBlockAtPosition(bot, x=0, y=0, z=0) { } -export function getLowestBlock(bot, block_types=null, ignore_types=null, distance=32) { - /** - * Searches a column from the bot's position for the lowest block +export function getSurroundingBlocks(bot) { + /** + * Get the surrounding blocks from the bot's environment. * @param {Bot} bot - The bot to get the block for. - * @param {string[]} block_types - The names of the blocks to search for. - * @param {string[]} block_types - The names of the blocks to ignore. - * @param {number} distance - The maximum distance to search, default 32. - * @returns {Block} - The lowest block. + * @returns {string[]} - A list of block results as strings. * @example - * let lowestBlock = world.getLowestBlock(bot, null, null, 32); **/ - // if blocktypes is not a list, make it a list - let search_blocks = []; - if (block_types) { - if (!Array.isArray(block_types)) - block_types = [block_types]; - for(let block_type of block_types) { - if (mc.getBlockId(block_type)) search_blocks.push(block_type); - } - } - // if ignore_types is not a list, make it a list + // Create a list of block position results that can be unpacked. + let res = []; + res.push(`Block Above: ${getBlockAtPosition(bot, 0, 2, 0).name}`); + res.push(`Block Below: ${getBlockAtPosition(bot, 0, -1, 0).name}`); + res.push(`Block at Head: ${getBlockAtPosition(bot, 0, 1, 0).name}`); + res.push(`Block at Legs: ${getBlockAtPosition(bot, 0, 0, 0).name}`); + + return res; +} + + +export function getFirstBlockAboveHead(bot, ignore_types=null, distance=32) { + /** + * Searches a column from the bot's position for the first solid block above its head + * @param {Bot} bot - The bot to get the block for. + * @param {string[]} ignore_types - The names of the blocks to ignore. + * @param {number} distance - The maximum distance to search, default 32. + * @returns {string} - The fist block above head. + * @example + * let firstBlockAboveHead = world.getFirstBlockAboveHead(bot, null, 32); + **/ + // if ignore_types is not a list, make it a list. let ignore_blocks = []; - if (ignore_types === null) ignore_blocks = ['air']; + if (ignore_types === null) ignore_blocks = ['air', 'cave_air']; else { if (!Array.isArray(ignore_types)) ignore_types = [ignore_types]; @@ -88,25 +96,21 @@ export function getLowestBlock(bot, block_types=null, ignore_types=null, distanc if (mc.getBlockId(ingnore_type)) ignore_blocks.push(ingnore_type); } } - // The lowest block, stops when it finds a block - let lowest_block = {name: 'air'}; + // The block above, stops when it finds a solid block . + let block_above = {name: 'air'}; + let height = 0 for (let i = 0; i < distance; i++) { let block = bot.blockAt(bot.entity.position.offset(0, i+2, 0)); if (!block) block = {name: 'air'}; // Ignore and continue if (ignore_blocks.includes(block.name)) continue; // Defaults to any block - else if (block_types === null) { - lowest_block = block; - break; - } - else if (search_blocks.includes(block.name)) { - lowest_block = block; - break; - } + block_above = block; + height = i; + break; } - - return lowest_block; + + return `${block_above.name} (${height} blocks up)`; } From 13dbbbefcadcba9e4a4c39f656628f1bedfa24f2 Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Sun, 5 Jan 2025 15:52:04 -0600 Subject: [PATCH 4/4] little cleanup of environmental awareness --- src/agent/commands/queries.js | 6 +++--- src/agent/library/world.js | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/agent/commands/queries.js b/src/agent/commands/queries.js index 5b3ec58..f10b6e1 100644 --- a/src/agent/commands/queries.js +++ b/src/agent/commands/queries.js @@ -17,9 +17,6 @@ export const queryList = [ let pos = bot.entity.position; // display position to 2 decimal places res += `\n- Position: x: ${pos.x.toFixed(2)}, y: ${pos.y.toFixed(2)}, z: ${pos.z.toFixed(2)}`; - // Environmental Awareness - res += '\n- ' + world.getSurroundingBlocks(bot).join('\n- ') - res += `\n- First Solid Block Above Head: ${world.getFirstBlockAboveHead(bot, null, 32)}`; // Gameplay res += `\n- Gamemode: ${bot.game.gameMode}`; res += `\n- Health: ${Math.round(bot.health)} / 20`; @@ -35,6 +32,9 @@ export const queryList = [ // res += `\n- Artficial light: ${block.skyLight}`; // res += `\n- Sky light: ${block.light}`; // light properties are bugged, they are not accurate + res += '\n- ' + world.getSurroundingBlocks(bot).join('\n- ') + res += `\n- First Solid Block Above Head: ${world.getFirstBlockAboveHead(bot, null, 32)}`; + if (bot.time.timeOfDay < 6000) { res += '\n- Time: Morning'; diff --git a/src/agent/library/world.js b/src/agent/library/world.js index a6428c0..1f147d2 100644 --- a/src/agent/library/world.js +++ b/src/agent/library/world.js @@ -67,10 +67,9 @@ export function getSurroundingBlocks(bot) { **/ // Create a list of block position results that can be unpacked. let res = []; - res.push(`Block Above: ${getBlockAtPosition(bot, 0, 2, 0).name}`); res.push(`Block Below: ${getBlockAtPosition(bot, 0, -1, 0).name}`); - res.push(`Block at Head: ${getBlockAtPosition(bot, 0, 1, 0).name}`); res.push(`Block at Legs: ${getBlockAtPosition(bot, 0, 0, 0).name}`); + res.push(`Block at Head: ${getBlockAtPosition(bot, 0, 1, 0).name}`); return res; } @@ -92,8 +91,8 @@ export function getFirstBlockAboveHead(bot, ignore_types=null, distance=32) { else { if (!Array.isArray(ignore_types)) ignore_types = [ignore_types]; - for(let ingnore_type of ignore_types) { - if (mc.getBlockId(ingnore_type)) ignore_blocks.push(ingnore_type); + for(let ignore_type of ignore_types) { + if (mc.getBlockId(ignore_type)) ignore_blocks.push(ignore_type); } } // The block above, stops when it finds a solid block . @@ -109,6 +108,8 @@ export function getFirstBlockAboveHead(bot, ignore_types=null, distance=32) { height = i; break; } + + if (ignore_blocks.includes(block_above.name)) return 'none'; return `${block_above.name} (${height} blocks up)`; }