From 22a6a4285f2438bd90e09c7e60d5955d5de67dda Mon Sep 17 00:00:00 2001 From: Brady Date: Fri, 10 Aug 2018 00:08:29 -0500 Subject: [PATCH] Use the actual game's code in ToolSet#getStrVsBlock --- .../bot/pathing/movement/ActionCosts.java | 7 -- .../bot/pathing/movement/MovementHelper.java | 2 +- src/main/java/baritone/bot/utils/ToolSet.java | 76 ++++++++----------- 3 files changed, 33 insertions(+), 52 deletions(-) diff --git a/src/main/java/baritone/bot/pathing/movement/ActionCosts.java b/src/main/java/baritone/bot/pathing/movement/ActionCosts.java index 5875a97f..793f7186 100644 --- a/src/main/java/baritone/bot/pathing/movement/ActionCosts.java +++ b/src/main/java/baritone/bot/pathing/movement/ActionCosts.java @@ -44,12 +44,5 @@ public interface ActionCosts extends ActionCostsButOnlyTheOnesThatMakeMickeyDieI */ double PLACE_ONE_BLOCK_COST = 20; - /** - * Add this to the cost of breaking any block. The cost of breaking any - * block is calculated as the number of ticks that block takes to break with - * the tools you have. You add this because there's always a little overhead - * (e.g. looking at the block) - */ - double BREAK_ONE_BLOCK_ADD = 4; double COST_INF = 1000000; } diff --git a/src/main/java/baritone/bot/pathing/movement/MovementHelper.java b/src/main/java/baritone/bot/pathing/movement/MovementHelper.java index 1bf78439..2349f856 100644 --- a/src/main/java/baritone/bot/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/bot/pathing/movement/MovementHelper.java @@ -139,7 +139,7 @@ public interface MovementHelper extends ActionCosts, Helper { // return COST_INF; //} double m = Blocks.CRAFTING_TABLE.equals(block) ? 10 : 1; - return m / ts.getStrVsBlock(state, position) + BREAK_ONE_BLOCK_ADD; + return m / ts.getStrVsBlock(state, position); } return 0; } diff --git a/src/main/java/baritone/bot/utils/ToolSet.java b/src/main/java/baritone/bot/utils/ToolSet.java index 0119d509..f88f3100 100644 --- a/src/main/java/baritone/bot/utils/ToolSet.java +++ b/src/main/java/baritone/bot/utils/ToolSet.java @@ -39,26 +39,26 @@ import java.util.Map; * * @author avecowa */ -public class ToolSet { - - private static final Item FALLBACK_ITEM = Items.APPLE; +public class ToolSet implements Helper { /** * A list of tools on the hotbar that should be considered. * Note that if there are no tools on the hotbar this list will still have one (null) entry. */ - List tools; + private List tools; + /** * A mapping from the tools array to what hotbar slots the tool is actually in. * tools.get(i) will be on your hotbar in slot slots.get(i) */ - List slots; + private List slots; + /** * A mapping from a block to which tool index is best for it. * The values in this map are *not* hotbar slots indexes, they need to be looked up in slots * in order to be converted into hotbar slots. */ - Map cache = new HashMap<>(); + private Map cache = new HashMap<>(); /** * Create a toolset from the current player's inventory (but don't calculate any hardness values just yet) @@ -81,14 +81,11 @@ public class ToolSet { /** * A caching wrapper around getBestToolIndex * - * @param b the blockstate to be mined + * @param state the blockstate to be mined * @return get which tool on the hotbar is best for mining it */ - public Item getBestTool(IBlockState b) { - if (cache.get(b.getBlock()) != null) { - return tools.get(cache.get(b.getBlock())); - } - return tools.get(getBestToolIndex(b)); + public Item getBestTool(IBlockState state) { + return tools.get(cache.computeIfAbsent(state.getBlock(), block -> getBestToolIndex(state))); } /** @@ -102,60 +99,51 @@ public class ToolSet { float value = -1; for (byte i = 0; i < tools.size(); i++) { Item item = tools.get(i); - if (item == null) { - item = FALLBACK_ITEM; - } + if (item == null) + continue; + float v = item.getDestroySpeed(new ItemStack(item), b); if (v < value || value == -1) { value = v; best = i; } } - cache.put(b.getBlock(), best); return best; } /** * Get which hotbar slot should be selected for fastest mining * - * @param b the blockstate to be mined + * @param state the blockstate to be mined * @return a byte indicating which hotbar slot worked best */ - public byte getBestSlot(IBlockState b) { - if (cache.get(b.getBlock()) != null) { - return slots.get(cache.get(b.getBlock())); - } - return slots.get(getBestToolIndex(b)); + public byte getBestSlot(IBlockState state) { + return slots.get(cache.computeIfAbsent(state.getBlock(), block -> getBestToolIndex(state))); } /** * Using the best tool on the hotbar, how long would it take to mine this block * - * @param b the blockstate to be mined + * @param state the blockstate to be mined * @param pos the blockpos to be mined * @return how long it would take in ticks */ - public double getStrVsBlock(IBlockState b, BlockPos pos) { - Item item = this.getBestTool(b); - if (item == null) { - item = FALLBACK_ITEM; - } - float f = b.getBlockHardness(Minecraft.getMinecraft().world, pos); - return f < 0.0F ? 0.0F : (!canHarvest(b, item) ? item.getDestroySpeed(new ItemStack(item), b) / f / 100.0F : item.getDestroySpeed(new ItemStack(item), b) / f / 30.0F); - } + public double getStrVsBlock(IBlockState state, BlockPos pos) { + // Calculate the slot with the best item + byte slot = this.getBestSlot(state); - /** - * Whether a tool can be efficiently used against a block - * - * @param blockIn the blockstate to be mined - * @param item the tool to be used - * @return whether or not this tool would help - */ - public boolean canHarvest(IBlockState blockIn, Item item) { - if (blockIn.getMaterial().isToolNotRequired()) { - return true; - } else { - return new ItemStack(item).canHarvestBlock(blockIn); - } + // Save the old current slot + int oldSlot = player().inventory.currentItem; + + // Set the best slot + player().inventory.currentItem = slot; + + // Calculate the relative hardness of the block to the player + float hardness = state.getPlayerRelativeBlockHardness(player(), world(), pos); + + // Restore the old slot + player().inventory.currentItem = oldSlot; + + return hardness; } }