From 0f74141d2390ec2cba5f8deaf78dff4738fa809b Mon Sep 17 00:00:00 2001 From: Leijurv Date: Thu, 2 Aug 2018 13:49:31 -0400 Subject: [PATCH] toolset introduction --- .../java/baritone/bot/GameEventHandler.java | 4 - .../bot/pathing/action/ActionWorldHelper.java | 22 ++++- .../baritone/bot/utils}/ToolSet.java | 81 ++++++++++++++----- 3 files changed, 82 insertions(+), 25 deletions(-) rename src/main/{resources/baritone/util => java/baritone/bot/utils}/ToolSet.java (55%) diff --git a/src/main/java/baritone/bot/GameEventHandler.java b/src/main/java/baritone/bot/GameEventHandler.java index 3573c661..77bced08 100755 --- a/src/main/java/baritone/bot/GameEventHandler.java +++ b/src/main/java/baritone/bot/GameEventHandler.java @@ -4,7 +4,6 @@ import baritone.bot.behavior.Behavior; import baritone.bot.event.IGameEventListener; import baritone.bot.event.events.ChatEvent; import baritone.bot.event.events.ChunkEvent; -import baritone.bot.pathing.action.ActionWorldHelper; import net.minecraft.client.settings.KeyBinding; import org.lwjgl.input.Keyboard; @@ -22,9 +21,6 @@ public final class GameEventHandler implements IGameEventListener { @Override public final void onTick() { dispatch(behavior -> onTick()); - while (true) { - System.out.println(ActionWorldHelper.lavaFlowing); - } } @Override diff --git a/src/main/java/baritone/bot/pathing/action/ActionWorldHelper.java b/src/main/java/baritone/bot/pathing/action/ActionWorldHelper.java index ee250023..8f261682 100644 --- a/src/main/java/baritone/bot/pathing/action/ActionWorldHelper.java +++ b/src/main/java/baritone/bot/pathing/action/ActionWorldHelper.java @@ -1,9 +1,11 @@ package baritone.bot.pathing.action; import baritone.bot.utils.BlockStateInterface; +import baritone.bot.utils.ToolSet; import net.minecraft.block.*; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; +import net.minecraft.init.Blocks; import net.minecraft.util.math.BlockPos; /** @@ -11,7 +13,7 @@ import net.minecraft.util.math.BlockPos; * * @author leijurv */ -public interface ActionWorldHelper { +public interface ActionWorldHelper extends ActionCosts { Block waterFlowing = Block.getBlockById(8); Block waterStill = Block.getBlockById(9); Block lavaFlowing = Block.getBlockById(10); @@ -116,4 +118,22 @@ public interface ActionWorldHelper { } return state.isBlockNormalCube() && !isLava(block); } + + static boolean canFall(BlockPos pos) { + return BlockStateInterface.get(pos).getBlock() instanceof BlockFalling; + } + + static double getHardness(ToolSet ts, IBlockState block, BlockPos position) { + if (!block.equals(Blocks.AIR) && !canWalkThrough(position)) { + if (avoidBreaking(position)) { + return COST_INF; + } + //if (!Baritone.allowBreakOrPlace) { + // return COST_INF; + //} + double m = Block.getBlockFromName("minecraft:crafting_table").equals(block) ? 10 : 1; + return m / ts.getStrVsBlock(block, position) + BREAK_ONE_BLOCK_ADD; + } + return 0; + } } diff --git a/src/main/resources/baritone/util/ToolSet.java b/src/main/java/baritone/bot/utils/ToolSet.java similarity index 55% rename from src/main/resources/baritone/util/ToolSet.java rename to src/main/java/baritone/bot/utils/ToolSet.java index bf395f20..83449af6 100644 --- a/src/main/resources/baritone/util/ToolSet.java +++ b/src/main/java/baritone/bot/utils/ToolSet.java @@ -1,7 +1,5 @@ -package baritone.util; +package baritone.bot.utils; -import java.util.ArrayList; -import java.util.HashMap; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; @@ -13,37 +11,59 @@ import net.minecraft.item.ItemTool; import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + /** + * A cached list of the best tools on the hotbar for any block * * @author avecowa */ public class ToolSet { - public ArrayList tools; - public ArrayList slots; - public HashMap cache = new HashMap(); - - public ToolSet(ArrayList tools, ArrayList slots) { - this.tools = tools; - this.slots = slots; - } + /** + * 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; + /** + * 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; + /** + * 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<>(); + /** + * Create a toolset from the current player's inventory (but don't calculate any hardness values just yet) + */ public ToolSet() { EntityPlayerSP p = Minecraft.getMinecraft().player; NonNullList inv = p.inventory.mainInventory; - tools = new ArrayList(); - slots = new ArrayList(); - //Out.log("inv: " + Arrays.toString(inv)); + tools = new ArrayList<>(); + slots = new ArrayList<>(); boolean fnull = false; for (byte i = 0; i < 9; i++) { if (!fnull || ((!(inv.get(i).getItem() instanceof ItemAir)) && inv.get(i).getItem() instanceof ItemTool)) { - tools.add((!(inv.get(i).getItem() instanceof ItemAir)) ? (ItemTool) inv.get(i).getItem() : null); + tools.add(inv.get(i).getItem() instanceof ItemTool ? (ItemTool) inv.get(i).getItem() : null); slots.add(i); fnull |= (inv.get(i).getItem() instanceof ItemAir) || (!inv.get(i).getItem().isDamageable()); } } } + /** + * A caching wrapper around getBestToolIndex + * + * @param b 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())); @@ -51,29 +71,36 @@ public class ToolSet { return tools.get(getBestToolIndex(b)); } + /** + * Calculate which tool on the hotbar is best for mining + * + * @param b the blockstate to be mined + * @return a byte indicating the index in the tools array that worked best + */ private byte getBestToolIndex(IBlockState b) { byte best = 0; - //Out.log("best: " + best); float value = -1; for (byte i = 0; i < tools.size(); i++) { Item item = tools.get(i); if (item == null) { item = Item.getByNameOrId("minecraft:apple"); } - //Out.log(inv[i]); - float v = item.getDestroySpeed(new ItemStack(item), b); - //Out.log("v: " + v); if (v < value || value == -1) { value = v; best = i; } } - //Out.log("best: " + best); cache.put(b.getBlock(), best); return best; } + /** + * Get which hotbar slot should be selected for fastest mining + * + * @param b 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())); @@ -81,6 +108,13 @@ public class ToolSet { return slots.get(getBestToolIndex(b)); } + /** + * Using the best tool on the hotbar, how long would it take to mine this block + * + * @param b 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) { @@ -90,6 +124,13 @@ public class ToolSet { 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); } + /** + * 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;