From f95dc40f4c7c970861afd60b2f9e3267178c4dab Mon Sep 17 00:00:00 2001 From: Leijurv Date: Thu, 10 Jan 2019 17:23:20 -0800 Subject: [PATCH] consolidate triple duplicated placement code --- .../baritone/behavior/PathingBehavior.java | 4 +- .../pathing/movement/MovementHelper.java | 47 ++++++++++ .../movement/movements/MovementAscend.java | 57 +++--------- .../movement/movements/MovementParkour.java | 38 +------- .../movement/movements/MovementTraverse.java | 86 +++++++------------ 5 files changed, 97 insertions(+), 135 deletions(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 767de6b8..283033fa 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -375,7 +375,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, } if (MovementHelper.canWalkOn(ctx, possibleSupport.down()) && MovementHelper.canWalkThrough(ctx, possibleSupport) && MovementHelper.canWalkThrough(ctx, possibleSupport.up())) { // this is plausible - logDebug("Faking path start assuming player is standing off the edge of a block"); + //logDebug("Faking path start assuming player is standing off the edge of a block"); return possibleSupport; } } @@ -384,7 +384,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, // !onGround // we're in the middle of a jump if (MovementHelper.canWalkOn(ctx, feet.down().down())) { - logDebug("Faking path start assuming player is midair and falling"); + //logDebug("Faking path start assuming player is midair and falling"); return feet.down(); } } diff --git a/src/main/java/baritone/pathing/movement/MovementHelper.java b/src/main/java/baritone/pathing/movement/MovementHelper.java index 96c4a769..790144f9 100644 --- a/src/main/java/baritone/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/pathing/movement/MovementHelper.java @@ -19,6 +19,7 @@ package baritone.pathing.movement; import baritone.Baritone; import baritone.api.pathing.movement.ActionCosts; +import baritone.api.pathing.movement.MovementStatus; import baritone.api.utils.*; import baritone.api.utils.input.Input; import baritone.pathing.movement.MovementState.MovementTarget; @@ -35,6 +36,10 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.RayTraceResult; +import net.minecraft.util.math.Vec3d; + +import static baritone.pathing.movement.Movement.HORIZONTALS_BUT_ALSO_DOWN____SO_EVERY_DIRECTION_EXCEPT_UP; /** * Static helpers for cost calculation @@ -487,4 +492,46 @@ public interface MovementHelper extends ActionCosts, Helper { return state.getBlock() instanceof BlockLiquid && state.getValue(BlockLiquid.LEVEL) != 0; } + + static PlaceResult attemptToPlaceABlock(MovementState state, IPlayerContext ctx, BlockPos placeAt, boolean preferDown) { + boolean found = false; + for (int i = 0; i < 5; i++) { + BlockPos against1 = placeAt.offset(HORIZONTALS_BUT_ALSO_DOWN____SO_EVERY_DIRECTION_EXCEPT_UP[i]); + if (MovementHelper.canPlaceAgainst(ctx, against1)) { + if (!MovementHelper.throwaway(ctx, true)) { // get ready to place a throwaway block + Helper.HELPER.logDebug("bb pls get me some blocks. dirt or cobble"); + state.setStatus(MovementStatus.UNREACHABLE); + return PlaceResult.NO_OPTION; + } + double faceX = (placeAt.getX() + against1.getX() + 1.0D) * 0.5D; + double faceY = (placeAt.getY() + against1.getY() + 1.0D) * 0.5D; + double faceZ = (placeAt.getZ() + against1.getZ() + 1.0D) * 0.5D; + Rotation place = RotationUtils.calcRotationFromVec3d(ctx.playerHead(), new Vec3d(faceX, faceY, faceZ), ctx.playerRotations()); + RayTraceResult res = RayTraceUtils.rayTraceTowards(ctx.player(), place, ctx.playerController().getBlockReachDistance()); + if (res != null && res.typeOfHit == RayTraceResult.Type.BLOCK && res.getBlockPos().equals(against1) && res.getBlockPos().offset(res.sideHit).equals(placeAt)) { + state.setTarget(new MovementState.MovementTarget(place, true)); + found = true; + + if (!preferDown) { + // if preferDown is true, we want the last option + // if preferDown is false, we want the first + break; + } + } + } + } + if (ctx.getSelectedBlock().isPresent()) { + BlockPos selectedBlock = ctx.getSelectedBlock().get(); + EnumFacing side = ctx.objectMouseOver().sideHit; + // only way for selectedBlock.equals(placeAt) to be true is if it's replacable + if (selectedBlock.equals(placeAt) || (MovementHelper.canPlaceAgainst(ctx, selectedBlock) && selectedBlock.offset(side).equals(placeAt))) { + return PlaceResult.READY_TO_PLACE; + } + } + return found ? PlaceResult.ATTEMPTING : PlaceResult.NO_OPTION; + } + + enum PlaceResult { + READY_TO_PLACE, ATTEMPTING, NO_OPTION; + } } diff --git a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java index 5fafdeed..8508e776 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java @@ -21,7 +21,6 @@ import baritone.Baritone; import baritone.api.IBaritone; import baritone.api.pathing.movement.MovementStatus; import baritone.api.utils.BetterBlockPos; -import baritone.api.utils.RotationUtils; import baritone.api.utils.input.Input; import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.Movement; @@ -32,10 +31,6 @@ import net.minecraft.block.BlockFalling; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; import net.minecraft.util.EnumFacing; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Vec3d; - -import java.util.Objects; public class MovementAscend extends Movement { @@ -161,55 +156,29 @@ public class MovementAscend extends Movement { IBlockState jumpingOnto = BlockStateInterface.get(ctx, positionToPlace); if (!MovementHelper.canWalkOn(ctx, positionToPlace, jumpingOnto)) { - for (int i = 0; i < 5; i++) { - BlockPos anAgainst = positionToPlace.offset(HORIZONTALS_BUT_ALSO_DOWN____SO_EVERY_DIRECTION_EXCEPT_UP[i]); - if (anAgainst.equals(src)) { - continue; - } - if (MovementHelper.canPlaceAgainst(ctx, anAgainst)) { - if (!MovementHelper.throwaway(ctx, true)) {//get ready to place a throwaway block - return state.setStatus(MovementStatus.UNREACHABLE); - } - double faceX = (dest.getX() + anAgainst.getX() + 1.0D) * 0.5D; - double faceY = (dest.getY() + anAgainst.getY()) * 0.5D; - double faceZ = (dest.getZ() + anAgainst.getZ() + 1.0D) * 0.5D; - state.setTarget(new MovementState.MovementTarget(RotationUtils.calcRotationFromVec3d(ctx.playerHead(), new Vec3d(faceX, faceY, faceZ), ctx.playerRotations()), true)); - EnumFacing side = ctx.objectMouseOver().sideHit; - - ctx.getSelectedBlock().ifPresent(selectedBlock -> { - if (Objects.equals(selectedBlock, anAgainst) && selectedBlock.offset(side).equals(positionToPlace)) { - ticksWithoutPlacement++; - state.setInput(Input.SNEAK, true); - if (ctx.player().isSneaking()) { - state.setInput(Input.CLICK_RIGHT, true); - } - if (ticksWithoutPlacement > 10) { - // After 10 ticks without placement, we might be standing in the way, move back - state.setInput(Input.MOVE_BACK, true); - } - } else { - state.setInput(Input.CLICK_LEFT, true); // break whatever replaceable block is in the way - } - //System.out.println("Trying to look at " + anAgainst + ", actually looking at" + selectedBlock); - }); - return state; + ticksWithoutPlacement++; + if (MovementHelper.attemptToPlaceABlock(state, ctx, dest.down(), false) == PlaceResult.READY_TO_PLACE) { + state.setInput(Input.SNEAK, true); + if (ctx.player().isSneaking()) { + state.setInput(Input.CLICK_RIGHT, true); } } - return state.setStatus(MovementStatus.UNREACHABLE); + if (ticksWithoutPlacement > 10) { + // After 10 ticks without placement, we might be standing in the way, move back + state.setInput(Input.MOVE_BACK, true); + } + + return state; } MovementHelper.moveTowards(ctx, state, dest); if (MovementHelper.isBottomSlab(jumpingOnto) && !MovementHelper.isBottomSlab(BlockStateInterface.get(ctx, src.down()))) { return state; // don't jump while walking from a non double slab into a bottom slab } - - if (Baritone.settings().assumeStep.get()) { + if (Baritone.settings().assumeStep.get() || ctx.playerFeet().equals(src.up())) { + // no need to hit space if we're already jumping return state; } - if (ctx.playerFeet().equals(src.up())) { - return state; // no need to hit space if we're already jumping - } - if (headBonkClear()) { return state.setInput(Input.JUMP, true); } diff --git a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java index 7c4ca19a..2d668bfb 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java @@ -20,9 +20,6 @@ package baritone.pathing.movement.movements; import baritone.api.IBaritone; import baritone.api.pathing.movement.MovementStatus; import baritone.api.utils.BetterBlockPos; -import baritone.api.utils.RayTraceUtils; -import baritone.api.utils.Rotation; -import baritone.api.utils.RotationUtils; import baritone.api.utils.input.Input; import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.Movement; @@ -35,9 +32,6 @@ import net.minecraft.block.BlockStairs; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; import net.minecraft.util.EnumFacing; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.RayTraceResult; -import net.minecraft.util.math.Vec3d; public class MovementParkour extends Movement { @@ -211,35 +205,9 @@ public class MovementParkour extends Movement { } } else if (!ctx.playerFeet().equals(src)) { if (ctx.playerFeet().equals(src.offset(direction)) || ctx.player().posY - ctx.playerFeet().getY() > 0.0001) { - - if (!MovementHelper.canWalkOn(ctx, dest.down()) && !ctx.player().onGround) { - BlockPos positionToPlace = dest.down(); - for (int i = 4; i >= 0; i--) { // go in the opposite order to check DOWN before all horizontals -- down is preferable because you don't have to look to the side while in midair, which could mess up the trajectory - BlockPos against1 = positionToPlace.offset(HORIZONTALS_BUT_ALSO_DOWN____SO_EVERY_DIRECTION_EXCEPT_UP[i]); - if (against1.up().equals(src.offset(direction, 3))) { // we can't turn around that fast - continue; - } - if (MovementHelper.canPlaceAgainst(ctx, against1)) { - if (!MovementHelper.throwaway(ctx, true)) {//get ready to place a throwaway block - return state.setStatus(MovementStatus.UNREACHABLE); - } - double faceX = (dest.getX() + against1.getX() + 1.0D) * 0.5D; - double faceY = (dest.getY() + against1.getY()) * 0.5D; - double faceZ = (dest.getZ() + against1.getZ() + 1.0D) * 0.5D; - Rotation place = RotationUtils.calcRotationFromVec3d(ctx.playerHead(), new Vec3d(faceX, faceY, faceZ), ctx.playerRotations()); - RayTraceResult res = RayTraceUtils.rayTraceTowards(ctx.player(), place, ctx.playerController().getBlockReachDistance()); - if (res != null && res.typeOfHit == RayTraceResult.Type.BLOCK && res.getBlockPos().equals(against1) && res.getBlockPos().offset(res.sideHit).equals(dest.down())) { - state.setTarget(new MovementState.MovementTarget(place, true)); - break; - } - } - } - ctx.getSelectedBlock().ifPresent(selectedBlock -> { - EnumFacing side = ctx.objectMouseOver().sideHit; - if (MovementHelper.canPlaceAgainst(ctx, selectedBlock) && selectedBlock.offset(side).equals(dest.down())) { - state.setInput(Input.CLICK_RIGHT, true); - } - }); + if (!MovementHelper.canWalkOn(ctx, dest.down()) && !ctx.player().onGround && MovementHelper.attemptToPlaceABlock(state, ctx, dest.down(), true) == PlaceResult.READY_TO_PLACE) { + // go in the opposite order to check DOWN before all horizontals -- down is preferable because you don't have to look to the side while in midair, which could mess up the trajectory + state.setInput(Input.CLICK_RIGHT, true); } if (dist == 3) { // this is a 2 block gap, dest = src + direction * 3 double xDiff = (src.x + 0.5) - ctx.player().posX; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java index 1c5c4f2e..6c78e7d3 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java @@ -36,7 +36,6 @@ import net.minecraft.block.BlockFenceGate; import net.minecraft.block.BlockSlab; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; -import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; @@ -248,63 +247,40 @@ public class MovementTraverse extends Movement { return state; } else { wasTheBridgeBlockAlwaysThere = false; - for (int i = 0; i < 5; i++) { - BlockPos against1 = dest.offset(HORIZONTALS_BUT_ALSO_DOWN____SO_EVERY_DIRECTION_EXCEPT_UP[i]); - if (against1.equals(src)) { - continue; - } - against1 = against1.down(); - if (MovementHelper.canPlaceAgainst(ctx, against1)) { - if (!MovementHelper.throwaway(ctx, true)) { // get ready to place a throwaway block - logDebug("bb pls get me some blocks. dirt or cobble"); - return state.setStatus(MovementStatus.UNREACHABLE); - } - if (!Baritone.settings().assumeSafeWalk.get()) { - state.setInput(Input.SNEAK, true); - } - Block standingOn = BlockStateInterface.get(ctx, ctx.playerFeet().down()).getBlock(); - if (standingOn.equals(Blocks.SOUL_SAND) || standingOn instanceof BlockSlab) { // see issue #118 - double dist = Math.max(Math.abs(dest.getX() + 0.5 - ctx.player().posX), Math.abs(dest.getZ() + 0.5 - ctx.player().posZ)); - if (dist < 0.85) { // 0.5 + 0.3 + epsilon - MovementHelper.moveTowards(ctx, state, dest); - return state.setInput(Input.MOVE_FORWARD, false) - .setInput(Input.MOVE_BACK, true); - } - } - state.setInput(Input.MOVE_BACK, false); - double faceX = (dest.getX() + against1.getX() + 1.0D) * 0.5D; - double faceY = (dest.getY() + against1.getY()) * 0.5D; - double faceZ = (dest.getZ() + against1.getZ() + 1.0D) * 0.5D; - Rotation rot = RotationUtils.calcRotationFromVec3d(ctx.playerHead(), new Vec3d(faceX, faceY, faceZ), ctx.playerRotations()); - state.setTarget(new MovementState.MovementTarget(rot, true)); - - EnumFacing side = ctx.objectMouseOver().sideHit; - if ((Objects.equals(ctx.getSelectedBlock(), dest.down()) || (Objects.equals(ctx.getSelectedBlock().orElse(null), against1) && ctx.getSelectedBlock().get().offset(side).equals(positionToPlace))) && (ctx.player().isSneaking() || Baritone.settings().assumeSafeWalk.get())) { - return state.setInput(Input.CLICK_RIGHT, true); - } - if (ctx.playerRotations().isReallyCloseTo(rot)) { - double dist = Math.max(Math.abs(ctx.player().posX - (dest.getX() + 0.5D)), Math.abs(ctx.player().posZ - (dest.getZ() + 0.5D))); - if (dist > 0.83) { - // might need to go forward a bit - return state.setInput(Input.MOVE_FORWARD, true); - } - // don't left click for one tick - return state.setInput(Input.CLICK_LEFT, true); - } - return state; - //System.out.println("Trying to look at " + against1 + ", actually looking at" + RayTraceUtils.getSelectedBlock()); - } - } if (!Baritone.settings().assumeSafeWalk.get()) { state.setInput(Input.SNEAK, true); } + Block standingOn = BlockStateInterface.get(ctx, ctx.playerFeet().down()).getBlock(); + if (standingOn.equals(Blocks.SOUL_SAND) || standingOn instanceof BlockSlab) { // see issue #118 + double dist = Math.max(Math.abs(dest.getX() + 0.5 - ctx.player().posX), Math.abs(dest.getZ() + 0.5 - ctx.player().posZ)); + if (dist < 0.85) { // 0.5 + 0.3 + epsilon + MovementHelper.moveTowards(ctx, state, dest); + return state.setInput(Input.MOVE_FORWARD, false) + .setInput(Input.MOVE_BACK, true); + } + } + state.setInput(Input.MOVE_BACK, false); + switch (MovementHelper.attemptToPlaceABlock(state, ctx, dest.down(), false)) { + case READY_TO_PLACE: + if (ctx.player().isSneaking() || Baritone.settings().assumeSafeWalk.get()) { + state.setInput(Input.CLICK_RIGHT, true); + } + return state; + case ATTEMPTING: + double dist = Math.max(Math.abs(ctx.player().posX - (dest.getX() + 0.5D)), Math.abs(ctx.player().posZ - (dest.getZ() + 0.5D))); + if (dist > 0.83) { + // might need to go forward a bit + return state.setInput(Input.MOVE_FORWARD, true); + } else if (ctx.playerRotations().isReallyCloseTo(state.getGoal().rotation)) { + // well i guess theres something in the way + return state.setInput(Input.CLICK_LEFT, true); + } + case NO_OPTION: + break; + } 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 // Out.log(from + " " + to + " " + faceX + "," + faceY + "," + faceZ + " " + whereAmI); - if (!MovementHelper.throwaway(ctx, true)) {// get ready to place a throwaway block - logDebug("bb pls get me some blocks. dirt or cobble"); - return state.setStatus(MovementStatus.UNREACHABLE); - } double faceX = (dest.getX() + src.getX() + 1.0D) * 0.5D; double faceY = (dest.getY() + src.getY() - 1.0D) * 0.5D; double faceZ = (dest.getZ() + src.getZ() + 1.0D) * 0.5D; @@ -321,12 +297,14 @@ public class MovementTraverse extends Movement { } else { state.setTarget(new MovementState.MovementTarget(backToFace, true)); } - state.setInput(Input.SNEAK, true); if (Objects.equals(ctx.getSelectedBlock().orElse(null), goalLook)) { return state.setInput(Input.CLICK_RIGHT, true); // wait to right click until we are able to place } // Out.log("Trying to look at " + goalLook + ", actually looking at" + Baritone.whatAreYouLookingAt()); - return state.setInput(Input.CLICK_LEFT, true); + if (ctx.playerRotations().isReallyCloseTo(state.getGoal().rotation)) { + state.setInput(Input.CLICK_LEFT, true); + } + return state; } else { MovementHelper.moveTowards(ctx, state, positionsToBreak[0]); return state;