From 32a0f4eaac3883a5e049df9c7a73790c9df91d7d Mon Sep 17 00:00:00 2001 From: Brady Date: Sat, 25 Aug 2018 18:30:12 -0500 Subject: [PATCH] All around minor cleanups to movement classes --- .../baritone/pathing/movement/Movement.java | 17 ++++--- .../movement/movements/MovementAscend.java | 50 +++++++++---------- .../movement/movements/MovementDescend.java | 4 +- .../movement/movements/MovementDiagonal.java | 20 ++++---- .../movement/movements/MovementDownward.java | 1 + .../movement/movements/MovementFall.java | 9 ++-- .../movement/movements/MovementPillar.java | 50 ++++++++++++------- .../movement/movements/MovementTraverse.java | 23 +++++---- 8 files changed, 94 insertions(+), 80 deletions(-) diff --git a/src/main/java/baritone/pathing/movement/Movement.java b/src/main/java/baritone/pathing/movement/Movement.java index 73a42121..27cbb08e 100644 --- a/src/main/java/baritone/pathing/movement/Movement.java +++ b/src/main/java/baritone/pathing/movement/Movement.java @@ -33,6 +33,7 @@ import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; import java.util.ArrayList; +import java.util.List; import java.util.Optional; import static baritone.utils.InputOverrideHandler.Input; @@ -268,15 +269,15 @@ public abstract class Movement implements Helper, MovementHelper { return state; } - public ArrayList toBreakCached = null; - public ArrayList toPlaceCached = null; - public ArrayList toWalkIntoCached = null; + public List toBreakCached = null; + public List toPlaceCached = null; + public List toWalkIntoCached = null; - public ArrayList toBreak() { + public List toBreak() { if (toBreakCached != null) { return toBreakCached; } - ArrayList result = new ArrayList<>(); + List result = new ArrayList<>(); for (BlockPos positionToBreak : positionsToBreak) { if (!MovementHelper.canWalkThrough(positionToBreak)) { result.add(positionToBreak); @@ -286,11 +287,11 @@ public abstract class Movement implements Helper, MovementHelper { return result; } - public ArrayList toPlace() { + public List toPlace() { if (toPlaceCached != null) { return toPlaceCached; } - ArrayList result = new ArrayList<>(); + List result = new ArrayList<>(); for (BlockPos positionToBreak : positionsToPlace) { if (!MovementHelper.canWalkOn(positionToBreak)) { result.add(positionToBreak); @@ -300,7 +301,7 @@ public abstract class Movement implements Helper, MovementHelper { return result; } - public ArrayList toWalkInto() { // overridden by movementdiagonal + public List toWalkInto() { // overridden by movementdiagonal if (toWalkIntoCached == null) { toWalkIntoCached = new ArrayList<>(); } diff --git a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java index 956d7091..f3225337 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java @@ -130,9 +130,9 @@ public class MovementAscend extends Movement { default: return state; } + if (playerFeet().equals(dest)) { - state.setStatus(MovementStatus.SUCCESS); - return state; + return state.setStatus(MovementStatus.SUCCESS); } if (!MovementHelper.canWalkOn(positionsToPlace[0])) { @@ -146,17 +146,21 @@ public class MovementAscend extends Movement { double faceZ = (dest.getZ() + anAgainst.getZ() + 1.0D) * 0.5D; state.setTarget(new MovementState.MovementTarget(Utils.calcRotationFromVec3d(playerHead(), new Vec3d(faceX, faceY, faceZ), playerRotations()), true)); EnumFacing side = Minecraft.getMinecraft().objectMouseOver.sideHit; - if (Objects.equals(LookBehaviorUtils.getSelectedBlock().orElse(null), anAgainst) && LookBehaviorUtils.getSelectedBlock().get().offset(side).equals(positionsToPlace[0])) { - ticksWithoutPlacement++; - state.setInput(InputOverrideHandler.Input.SNEAK, true); - if (player().isSneaking()) { - state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true); + + LookBehaviorUtils.getSelectedBlock().ifPresent(selectedBlock -> { + if (Objects.equals(selectedBlock, anAgainst) && selectedBlock.offset(side).equals(positionsToPlace[0])) { + ticksWithoutPlacement++; + state.setInput(InputOverrideHandler.Input.SNEAK, true); + if (player().isSneaking()) { + state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true); + } + if (ticksWithoutPlacement > 20) { + // After 20 ticks without placement, we might be standing in the way, move back + state.setInput(InputOverrideHandler.Input.MOVE_BACK, true); + } } - if (ticksWithoutPlacement > 20) { - state.setInput(InputOverrideHandler.Input.MOVE_BACK, true);//we might be standing in the way, move back - } - } - System.out.println("Trying to look at " + anAgainst + ", actually looking at" + LookBehaviorUtils.getSelectedBlock()); + System.out.println("Trying to look at " + anAgainst + ", actually looking at" + selectedBlock); + }); return state; } } @@ -165,28 +169,22 @@ public class MovementAscend extends Movement { MovementHelper.moveTowards(state, dest); if (headBonkClear()) { - state.setInput(InputOverrideHandler.Input.JUMP, true); - return state; + return state.setInput(InputOverrideHandler.Input.JUMP, true); } int xAxis = Math.abs(src.getX() - dest.getX()); // either 0 or 1 int zAxis = Math.abs(src.getZ() - dest.getZ()); // either 0 or 1 double flatDistToNext = xAxis * Math.abs((dest.getX() + 0.5D) - player().posX) + zAxis * Math.abs((dest.getZ() + 0.5D) - player().posZ); - double sideDist = zAxis * Math.abs((dest.getX() + 0.5D) - player().posX) + xAxis * Math.abs((dest.getZ() + 0.5D) - player().posZ); - //System.out.println(flatDistToNext + " " + sideDist); - if (flatDistToNext > 1.2) { + // System.out.println(flatDistToNext + " " + sideDist); + if (flatDistToNext > 1.2 || sideDist > 0.2) { return state; } - if (sideDist > 0.2) { - return state; - } - //once we are pointing the right way and moving, start jumping - //this is slightly more efficient because otherwise we might start jumping before moving, and fall down without moving onto the block we want to jump onto - //also wait until we are close enough, because we might jump and hit our head on an adjacent block - state.setInput(InputOverrideHandler.Input.JUMP, true); - return state; + // Once we are pointing the right way and moving, start jumping + // This is slightly more efficient because otherwise we might start jumping before moving, and fall down without moving onto the block we want to jump onto + // Also wait until we are close enough, because we might jump and hit our head on an adjacent block + return state.setInput(InputOverrideHandler.Input.JUMP, true); } private boolean headBonkClear() { @@ -194,7 +192,7 @@ public class MovementAscend extends Movement { for (int i = 0; i < 4; i++) { BlockPos check = startUp.offset(EnumFacing.byHorizontalIndex(i)); if (!MovementHelper.canWalkThrough(check)) { - // we might bonk our head + // We might bonk our head return false; } } diff --git a/src/main/java/baritone/pathing/movement/movements/MovementDescend.java b/src/main/java/baritone/pathing/movement/movements/MovementDescend.java index dab57257..6e299bd1 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementDescend.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementDescend.java @@ -73,6 +73,7 @@ public class MovementDescend extends Movement { default: return state; } + BlockPos playerFeet = playerFeet(); if (playerFeet.equals(dest)) { if (BlockStateInterface.isLiquid(dest) || player().posY - playerFeet.getY() < 0.094) { // lilypads @@ -91,9 +92,6 @@ public class MovementDescend extends Movement { double fromStart = Math.sqrt(x * x + z * z); if (!playerFeet.equals(dest) || ab > 0.25) { BlockPos fakeDest = new BlockPos(dest.getX() * 2 - src.getX(), dest.getY(), dest.getZ() * 2 - src.getZ()); - double diffX2 = player().posX - (fakeDest.getX() + 0.5); - double diffZ2 = player().posZ - (fakeDest.getZ() + 0.5); - double d = Math.sqrt(diffX2 * diffX2 + diffZ2 * diffZ2); if (numTicks++ < 20) { MovementHelper.moveTowards(state, fakeDest); if (fromStart > 1.25) { diff --git a/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java b/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java index 8709dfc3..b570d0f5 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java @@ -30,6 +30,7 @@ import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import java.util.ArrayList; +import java.util.List; public class MovementDiagonal extends Movement { @@ -59,6 +60,7 @@ public class MovementDiagonal extends Movement { default: return state; } + if (playerFeet().equals(dest)) { state.setStatus(MovementState.MovementStatus.SUCCESS); return state; @@ -81,7 +83,7 @@ public class MovementDiagonal extends Movement { } double multiplier = WALK_ONE_BLOCK_COST; - // for either possible soul sand, that affects half of our walking + // For either possible soul sand, that affects half of our walking if (destWalkOn.getBlock().equals(Blocks.SOUL_SAND)) { multiplier += (WALK_ONE_OVER_SOUL_SAND_COST - WALK_ONE_BLOCK_COST) / 2; } @@ -117,17 +119,17 @@ public class MovementDiagonal extends Movement { } } if (BlockStateInterface.isWater(src) || BlockStateInterface.isWater(dest)) { - // ignore previous multiplier - // whatever we were walking on (possibly soul sand) doesn't matter as we're actually floating on water - // not even touching the blocks below + // Ignore previous multiplier + // Whatever we were walking on (possibly soul sand) doesn't matter as we're actually floating on water + // Not even touching the blocks below multiplier = WALK_ONE_IN_WATER_COST; } if (optionA != 0 || optionB != 0) { multiplier *= SQRT_2 - 0.001; // TODO tune } if (multiplier == WALK_ONE_BLOCK_COST && context.canSprint()) { - // if we aren't edging around anything, and we aren't in water or soul sand - // we can sprint =D + // If we aren't edging around anything, and we aren't in water or soul sand + // We can sprint =D multiplier = SPRINT_ONE_BLOCK_COST; } return multiplier * SQRT_2; @@ -139,7 +141,7 @@ public class MovementDiagonal extends Movement { } @Override - public ArrayList toBreak() { + public List toBreak() { if (toBreakCached != null) { return toBreakCached; } @@ -154,11 +156,11 @@ public class MovementDiagonal extends Movement { } @Override - public ArrayList toWalkInto() { + public List toWalkInto() { if (toWalkIntoCached == null) { toWalkIntoCached = new ArrayList<>(); } - ArrayList result = new ArrayList<>(); + List result = new ArrayList<>(); for (int i = 0; i < 4; i++) { if (!MovementHelper.canWalkThrough(positionsToBreak[i])) { result.add(positionsToBreak[i]); diff --git a/src/main/java/baritone/pathing/movement/movements/MovementDownward.java b/src/main/java/baritone/pathing/movement/movements/MovementDownward.java index 285053c2..724b8688 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementDownward.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementDownward.java @@ -68,6 +68,7 @@ public class MovementDownward extends Movement { default: return state; } + if (playerFeet().equals(dest)) { state.setStatus(MovementState.MovementStatus.SUCCESS); return state; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementFall.java b/src/main/java/baritone/pathing/movement/movements/MovementFall.java index 6d96b365..a3018c4c 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementFall.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementFall.java @@ -67,10 +67,10 @@ public class MovementFall extends Movement { } for (int i = 2; i < positionsToBreak.length; i++) { // TODO is this the right check here? - // miningDurationTicks is all right, but shouldn't it be canWalkThrough instead? - // lilypads (i think?) are 0 ticks to mine, but they definitely cause fall damage - // same thing for falling through water... we can't actually do that - // and falling through signs is possible, but they do have a mining duration, right? + // MiningDurationTicks is all right, but shouldn't it be canWalkThrough instead? + // Lilypads (i think?) are 0 ticks to mine, but they definitely cause fall damage + // Same thing for falling through water... we can't actually do that + // And falling through signs is possible, but they do have a mining duration, right? if (MovementHelper.getMiningDurationTicks(context, positionsToBreak[i]) > 0) { //can't break while falling return COST_INF; @@ -90,6 +90,7 @@ public class MovementFall extends Movement { default: return state; } + BlockPos playerFeet = playerFeet(); Optional targetRotation = Optional.empty(); if (!BlockStateInterface.isWater(dest) && src.getY() - dest.getY() > Baritone.settings().maxFallHeightNoWater.get() && !playerFeet.equals(dest)) { diff --git a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java index 5dcef948..dba55466 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java @@ -115,6 +115,7 @@ public class MovementPillar extends Movement { default: return state; } + IBlockState fromDown = BlockStateInterface.get(src); boolean ladder = fromDown.getBlock() instanceof BlockLadder || fromDown.getBlock() instanceof BlockVine; boolean vine = fromDown.getBlock() instanceof BlockVine; @@ -123,57 +124,68 @@ public class MovementPillar extends Movement { Utils.getBlockPosCenter(positionsToPlace[0]), new Rotation(mc.player.rotationYaw, mc.player.rotationPitch)), true)); } - EntityPlayerSP thePlayer = Minecraft.getMinecraft().player; + boolean blockIsThere = MovementHelper.canWalkOn(src) || ladder; if (ladder) { BlockPos against = vine ? getAgainst(src) : src.offset(fromDown.getValue(BlockLadder.FACING).getOpposite()); if (against == null) { displayChatMessageRaw("Unable to climb vines"); - state.setStatus(MovementState.MovementStatus.UNREACHABLE); - return state; + return state.setStatus(MovementState.MovementStatus.UNREACHABLE); } - if (playerFeet().equals(against.up()) || playerFeet().equals(dest)) { - state.setStatus(MovementState.MovementStatus.SUCCESS); - return state; + + if (playerFeet().equals(against.up()) || playerFeet().equals(dest)) + return state.setStatus(MovementState.MovementStatus.SUCCESS); + + /* + if (thePlayer.getPosition0().getX() != from.getX() || thePlayer.getPosition0().getZ() != from.getZ()) { + Baritone.moveTowardsBlock(from); } - /*if (thePlayer.getPosition0().getX() != from.getX() || thePlayer.getPosition0().getZ() != from.getZ()) { - Baritone.moveTowardsBlock(from); - }*/ + */ + MovementHelper.moveTowards(state, against); return state; } else { - if (!MovementHelper.throwaway(true)) {//get ready to place a throwaway block + // Get ready to place a throwaway block + if (!MovementHelper.throwaway(true)) { state.setStatus(MovementState.MovementStatus.UNREACHABLE); return state; } + numTicks++; - state.setInput(InputOverrideHandler.Input.JUMP, thePlayer.posY < dest.getY()); //if our Y coordinate is above our goal, stop jumping + // If our Y coordinate is above our goal, stop jumping + state.setInput(InputOverrideHandler.Input.JUMP, player().posY < dest.getY()); state.setInput(InputOverrideHandler.Input.SNEAK, true); - //otherwise jump + + // Otherwise jump if (numTicks > 40) { - double diffX = thePlayer.posX - (dest.getX() + 0.5); - double diffZ = thePlayer.posZ - (dest.getZ() + 0.5); + double diffX = player().posX - (dest.getX() + 0.5); + double diffZ = player().posZ - (dest.getZ() + 0.5); double dist = Math.sqrt(diffX * diffX + diffZ * diffZ); if (dist > 0.17) {//why 0.17? because it seemed like a good number, that's why //[explanation added after baritone port lol] also because it needs to be less than 0.2 because of the 0.3 sneak limit //and 0.17 is reasonably less than 0.2 - state.setInput(InputOverrideHandler.Input.MOVE_FORWARD, true);//if it's been more than forty ticks of trying to jump and we aren't done yet, go forward, maybe we are stuck + + // If it's been more than forty ticks of trying to jump and we aren't done yet, go forward, maybe we are stuck + state.setInput(InputOverrideHandler.Input.MOVE_FORWARD, true); } } + if (!blockIsThere) { Block fr = BlockStateInterface.get(src).getBlock(); if (!(fr instanceof BlockAir || fr.isReplaceable(Minecraft.getMinecraft().world, src))) { state.setInput(InputOverrideHandler.Input.CLICK_LEFT, true); blockIsThere = false; } else if (Minecraft.getMinecraft().player.isSneaking()) { - state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true);//constantly right click + state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true); } } } - if (playerFeet().equals(dest) && blockIsThere) {//if we are at our goal and the block below us is placed - state.setStatus(MovementState.MovementStatus.SUCCESS); - return state;//we are done + + // If we are at our goal and the block below us is placed + if (playerFeet().equals(dest) && blockIsThere) { + return state.setStatus(MovementState.MovementStatus.SUCCESS); } + return state; } } \ No newline at end of file diff --git a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java index 451c5ca6..c784f07b 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java @@ -92,15 +92,15 @@ public class MovementTraverse extends Movement { } if (MovementHelper.canWalkThrough(positionsToBreak[0], pb0) && MovementHelper.canWalkThrough(positionsToBreak[1], pb1)) { if (WC == WALK_ONE_BLOCK_COST && context.canSprint()) { - // if there's nothing in the way, and this isn't water or soul sand, and we aren't sneak placing - // we can sprint =D + // If there's nothing in the way, and this isn't water or soul sand, and we aren't sneak placing + // We can sprint =D WC = SPRINT_ONE_BLOCK_COST; } 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 + ")"); + // 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(context); } else {//this is a bridge, so we need to place a block Block srcDown = BlockStateInterface.get(src.down()).getBlock(); @@ -125,7 +125,7 @@ public class MovementTraverse extends Movement { return WC + context.placeBlockCost() + getTotalHardnessOfBlocksToBreak(context); } return COST_INF; - //Out.log("Can't walk on " + Baritone.get(positionsToPlace[0]).getBlock()); + // Out.log("Can't walk on " + Baritone.get(positionsToPlace[0]).getBlock()); } } @@ -140,6 +140,7 @@ public class MovementTraverse extends Movement { default: return state; } + Block fd = BlockStateInterface.get(src.down()).getBlock(); boolean ladder = fd instanceof BlockLadder || fd instanceof BlockVine; IBlockState pb0 = BlockStateInterface.get(positionsToBreak[0]); @@ -161,6 +162,7 @@ public class MovementTraverse extends Movement { } } } + boolean isTheBridgeBlockThere = MovementHelper.canWalkOn(positionsToPlace[0]) || ladder; BlockPos whereAmI = playerFeet(); if (whereAmI.getY() != dest.getY() && !ladder) { @@ -170,6 +172,7 @@ public class MovementTraverse extends Movement { } return state; } + if (isTheBridgeBlockThere) { if (playerFeet().equals(dest)) { state.setStatus(MovementState.MovementStatus.SUCCESS); @@ -202,20 +205,18 @@ public class MovementTraverse extends Movement { EnumFacing side = Minecraft.getMinecraft().objectMouseOver.sideHit; if (Objects.equals(LookBehaviorUtils.getSelectedBlock().orElse(null), against1) && Minecraft.getMinecraft().player.isSneaking()) { if (LookBehaviorUtils.getSelectedBlock().get().offset(side).equals(positionsToPlace[0])) { - state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true); - return state; + return state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true); } else { // Out.gui("Wrong. " + side + " " + LookBehaviorUtils.getSelectedBlock().get().offset(side) + " " + positionsToPlace[0], Out.Mode.Debug); } } - state.setInput(InputOverrideHandler.Input.CLICK_LEFT, true); System.out.println("Trying to look at " + against1 + ", actually looking at" + LookBehaviorUtils.getSelectedBlock()); - return state; + return state.setInput(InputOverrideHandler.Input.CLICK_LEFT, true); } } state.setInput(InputOverrideHandler.Input.SNEAK, true); if (whereAmI.equals(dest)) { - // if we are in the block that we are trying to get to, we are sneaking over air and we need to place a block beneath us against the one we just walked off of + // If we are in the block that we are trying to get to, we are sneaking over air and we need to place a block beneath us against the one we just walked off of // Out.log(from + " " + to + " " + faceX + "," + faceY + "," + faceZ + " " + whereAmI); if (!MovementHelper.throwaway(true)) {// get ready to place a throwaway block displayChatMessageRaw("bb pls get me some blocks. dirt or cobble");