From 988124e4448f05a427f3bfd7234fd3baded98e3d Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 5 Aug 2018 21:17:04 -0400 Subject: [PATCH] almost works --- .../bot/behavior/impl/PathingBehavior.java | 2 +- .../bot/pathing/calc/AStarPathFinder.java | 9 +- .../calc/openset/BinaryHeapOpenSet.java | 5 + .../calc/openset/LinkedListOpenSet.java | 4 +- .../bot/pathing/movement/MovementHelper.java | 5 +- .../movement/movements/MovementAscend.java | 2 +- .../movement/movements/MovementTraverse.java | 107 ++++++++++++++++++ 7 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java diff --git a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java index 3d664de1..ac49a2dd 100644 --- a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java +++ b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java @@ -45,7 +45,7 @@ public class PathingBehavior extends Behavior { if (current == null) { return; } - // current.onTick(); + current.onTick(event); if (current.failed() || current.finished()) { current = null; } diff --git a/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java index 6a8e3fee..5c083a85 100644 --- a/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java @@ -6,6 +6,7 @@ import baritone.bot.pathing.goals.Goal; import baritone.bot.pathing.movement.ActionCosts; import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.movements.MovementAscend; +import baritone.bot.pathing.movement.movements.MovementTraverse; import baritone.bot.pathing.path.IPath; import baritone.bot.utils.ToolSet; import net.minecraft.client.Minecraft; @@ -139,10 +140,10 @@ public class AStarPathFinder extends AbstractNodeCostSearch { int y = pos.getY(); int z = pos.getZ(); Movement[] movements = new Movement[8]; - movements[0] = new MovementAscend(pos, new BlockPos(x + 1, y, z)); - movements[1] = new MovementAscend(pos, new BlockPos(x - 1, y, z)); - movements[2] = new MovementAscend(pos, new BlockPos(x, y, z + 1)); - movements[3] = new MovementAscend(pos, new BlockPos(x, y, z - 1)); + movements[0] = new MovementTraverse(pos, new BlockPos(x + 1, y, z)); + movements[1] = new MovementTraverse(pos, new BlockPos(x - 1, y, z)); + movements[2] = new MovementTraverse(pos, new BlockPos(x, y, z + 1)); + movements[3] = new MovementTraverse(pos, new BlockPos(x, y, z - 1)); movements[4] = new MovementAscend(pos, new BlockPos(x + 1, y + 1, z)); movements[5] = new MovementAscend(pos, new BlockPos(x - 1, y + 1, z)); movements[6] = new MovementAscend(pos, new BlockPos(x, y + 1, z + 1)); diff --git a/src/main/java/baritone/bot/pathing/calc/openset/BinaryHeapOpenSet.java b/src/main/java/baritone/bot/pathing/calc/openset/BinaryHeapOpenSet.java index fb97ac7a..0afc227a 100644 --- a/src/main/java/baritone/bot/pathing/calc/openset/BinaryHeapOpenSet.java +++ b/src/main/java/baritone/bot/pathing/calc/openset/BinaryHeapOpenSet.java @@ -4,6 +4,11 @@ import baritone.bot.pathing.calc.PathNode; import java.util.Arrays; +/** + * A binary heap implementation of an open set. This is the one used in the AStarPathFinder. + * + * @author leijurv + */ public class BinaryHeapOpenSet implements IOpenSet { /** diff --git a/src/main/java/baritone/bot/pathing/calc/openset/LinkedListOpenSet.java b/src/main/java/baritone/bot/pathing/calc/openset/LinkedListOpenSet.java index 72a3d6b7..99a14558 100644 --- a/src/main/java/baritone/bot/pathing/calc/openset/LinkedListOpenSet.java +++ b/src/main/java/baritone/bot/pathing/calc/openset/LinkedListOpenSet.java @@ -4,7 +4,9 @@ import baritone.bot.pathing.calc.PathNode; /** * A linked list implementation of an open set. This is the original implementation from MineBot. - * It has incredbly fast insert performance, at the cost of O(n) removeLowest. + * It has incredibly fast insert performance, at the cost of O(n) removeLowest. + * + * @author leijurv */ public class LinkedListOpenSet implements IOpenSet { private Node first = null; diff --git a/src/main/java/baritone/bot/pathing/movement/MovementHelper.java b/src/main/java/baritone/bot/pathing/movement/MovementHelper.java index 81463a2c..ef677007 100644 --- a/src/main/java/baritone/bot/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/bot/pathing/movement/MovementHelper.java @@ -114,11 +114,9 @@ public interface MovementHelper extends ActionCosts, Helper { * through? Includes water because we know that we automatically jump on * lava * - * @param pos * @return */ - static boolean canWalkOn(BlockPos pos) { - IBlockState state = BlockStateInterface.get(pos); + static boolean canWalkOn(BlockPos pos, IBlockState state) { Block block = state.getBlock(); if (block instanceof BlockLadder || block instanceof BlockVine) { return true; @@ -129,6 +127,7 @@ public interface MovementHelper extends ActionCosts, Helper { return state.isBlockNormalCube() && !isLava(block); } + static boolean canFall(BlockPos pos) { return BlockStateInterface.get(pos).getBlock() instanceof BlockFalling; } diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java index 55fb0f11..ecdb413d 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java @@ -41,7 +41,7 @@ public class MovementAscend extends Movement { @Override protected double calculateCost(ToolSet ts) { - if (!MovementHelper.canWalkOn(positionsToPlace[0])) { + if (!MovementHelper.canWalkOn(positionsToPlace[0], BlockStateInterface.get(positionsToPlace[0]))) { if (!MovementHelper.isAir(positionsToPlace[0]) && !MovementHelper.isWater(positionsToPlace[0])) { return COST_INF; } diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java new file mode 100644 index 00000000..7094db42 --- /dev/null +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java @@ -0,0 +1,107 @@ +package baritone.bot.pathing.movement.movements; + +import baritone.bot.InputOverrideHandler; +import baritone.bot.pathing.movement.Movement; +import baritone.bot.pathing.movement.MovementHelper; +import baritone.bot.pathing.movement.MovementState; +import baritone.bot.utils.BlockStateInterface; +import baritone.bot.utils.ToolSet; +import baritone.bot.utils.Utils; +import net.minecraft.block.Block; +import net.minecraft.block.BlockLadder; +import net.minecraft.block.BlockVine; +import net.minecraft.block.state.IBlockState; +import net.minecraft.client.Minecraft; +import net.minecraft.init.Blocks; +import net.minecraft.util.math.BlockPos; + +import java.util.Optional; + +public class MovementTraverse extends Movement { + + BlockPos[] against = new BlockPos[3]; + + public MovementTraverse(BlockPos from, BlockPos to) { + super(from, to, new BlockPos[]{to.up(), to}, new BlockPos[]{to.down()}); + int i = 0; + if (!to.north().equals(from)) { + against[i] = to.north().down(); + i++; + } + if (!to.south().equals(from)) { + against[i] = to.south().down(); + i++; + } + if (!to.east().equals(from)) { + against[i] = to.east().down(); + i++; + } + if (!to.west().equals(from)) { + against[i] = to.west().down(); + i++; + } + //note: do NOT add ability to place against .down().down() + } + + @Override + protected double calculateCost(ToolSet ts) { + IBlockState pb0 = BlockStateInterface.get(positionsToBreak[0]); + IBlockState pb1 = BlockStateInterface.get(positionsToBreak[1]); + double WC = MovementHelper.isWater(pb0.getBlock()) || MovementHelper.isWater(pb1.getBlock()) ? WALK_ONE_IN_WATER_COST : WALK_ONE_BLOCK_COST; + if (MovementHelper.canWalkOn(positionsToPlace[0], BlockStateInterface.get(positionsToPlace[0]))) {//this is a walk, not a bridge + if (MovementHelper.canWalkThrough(positionsToBreak[0], pb0) && MovementHelper.canWalkThrough(positionsToBreak[1], pb1)) { + return WC; + } + //double hardness1 = blocksToBreak[0].getBlockHardness(Minecraft.getMinecraft().world, positionsToBreak[0]); + //double hardness2 = blocksToBreak[1].getBlockHardness(Minecraft.getMinecraft().world, positionsToBreak[1]); + //Out.log("Can't walk through " + blocksToBreak[0] + " (hardness" + hardness1 + ") or " + blocksToBreak[1] + " (hardness " + hardness2 + ")"); + return WC + getTotalHardnessOfBlocksToBreak(ts); + } else {//this is a bridge, so we need to place a block + if (true) { + System.out.println(src + " " + dest); + return COST_INF;//TODO + } + //return 1000000; + Block f = BlockStateInterface.get(src.down()).getBlock(); + if (f instanceof BlockLadder || f instanceof BlockVine) { + return COST_INF; + } + IBlockState pp0 = BlockStateInterface.get(positionsToPlace[0]); + if (pp0.getBlock().equals(Blocks.AIR) || (!MovementHelper.isWater(pp0.getBlock()) && pp0.getBlock().isReplaceable(Minecraft.getMinecraft().world, positionsToPlace[0]))) { + for (BlockPos against1 : against) { + if (BlockStateInterface.get(against1).isBlockNormalCube()) { + return WC + PLACE_ONE_BLOCK_COST + getTotalHardnessOfBlocksToBreak(ts); + } + } + WC = WC * SNEAK_ONE_BLOCK_COST / WALK_ONE_BLOCK_COST;//since we are placing, we are sneaking + return WC + PLACE_ONE_BLOCK_COST + getTotalHardnessOfBlocksToBreak(ts); + } + return COST_INF; + //Out.log("Can't walk on " + Baritone.get(positionsToPlace[0]).getBlock()); + } + } + + @Override + public void onFinish() { + + } + + @Override + public MovementState updateState(MovementState state) { + super.updateState(state); + switch (state.getStatus()) { + case PREPPING: + case UNREACHABLE: + case FAILED: + return state; + case WAITING: + case RUNNING: + state.setTarget(new MovementState.MovementTarget(Optional.empty(), Optional.of(Utils.calcRotationFromCoords(playerFeet(), positionsToBreak[0])))); + MovementState latestState = state.setInput(InputOverrideHandler.Input.MOVE_FORWARD, true); + if (playerFeet().equals(dest)) + latestState.setStatus(MovementState.MovementStatus.SUCCESS); + default: + return state; + } + } +}