diff --git a/src/main/java/baritone/bot/pathing/movement/Movement.java b/src/main/java/baritone/bot/pathing/movement/Movement.java index b1f8124b6..5ae98c198 100644 --- a/src/main/java/baritone/bot/pathing/movement/Movement.java +++ b/src/main/java/baritone/bot/pathing/movement/Movement.java @@ -104,7 +104,22 @@ public double recalculateCost() { */ public MovementStatus update() { player().setSprinting(false); - MovementState latestState = updateState(currentState); + switch (currentState.getStatus()) { + case PREPPING: + onPrepping(currentState); + break; + case WAITING: + case RUNNING: + onRunning(currentState); + break; + case SUCCESS: + case FAILED: + case CANCELED: + case UNREACHABLE: + onFinished(currentState); + break; + } + MovementState latestState = currentState; if (BlockStateInterface.isLiquid(playerFeet())) { latestState.setInput(Input.JUMP, true); } @@ -118,15 +133,12 @@ public MovementStatus update() { currentState = latestState; if (isFinished()) - onFinish(latestState); + onFinished(latestState); return currentState.getStatus(); } - protected boolean prepared(MovementState state) { - if (state.getStatus() == MovementStatus.WAITING) - return true; - + protected void onPrepping(MovementState state) { boolean somethingInTheWay = false; for (BlockPos blockPos : positionsToBreak) { if (!MovementHelper.canWalkThrough(blockPos)) { @@ -135,7 +147,7 @@ protected boolean prepared(MovementState state) { if (reachable.isPresent()) { player().inventory.currentItem = new ToolSet().getBestSlot(BlockStateInterface.get(blockPos)); state.setTarget(new MovementState.MovementTarget(reachable.get())).setInput(Input.CLICK_LEFT, true); - return false; + return; // still prepping } } } @@ -143,9 +155,17 @@ protected boolean prepared(MovementState state) { // There's a block or blocks that we can't walk through, but we have no target rotation to reach any // So don't return true, actually set state to unreachable state.setStatus(MovementStatus.UNREACHABLE); - return true; } - return true; + state.setStatus(MovementStatus.WAITING); + } + + /** + * Once the Movement has been prepared and is ready to onRunning, do so + * + * @param state + */ + protected void onRunning(MovementState state) { + } public boolean isFinished() { @@ -165,7 +185,7 @@ public BlockPos getDest() { /** * Run cleanup on state finish and declare success. */ - public void onFinish(MovementState state) { + public void onFinished(MovementState state) { state.getInputStates().replaceAll((input, forced) -> false); state.getInputStates().forEach((input, forced) -> Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced)); state.setStatus(MovementStatus.SUCCESS); @@ -217,22 +237,6 @@ public double getTotalHardnessOfBlocksToBreak(ToolSet ts) { return sum; } - - /** - * Calculate latest movement state. - * Gets called once a tick. - * - * @return - */ - public MovementState updateState(MovementState state) { - if (!prepared(state)) - return state.setStatus(MovementStatus.PREPPING); - else if (state.getStatus() == MovementStatus.PREPPING) { - state.setStatus(MovementStatus.WAITING); - } - return state; - } - public ArrayList toBreakCached = null; public ArrayList toPlaceCached = null; public ArrayList toWalkIntoCached = null; 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 7d68608cc..217c697f5 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java @@ -17,7 +17,6 @@ package baritone.bot.pathing.movement.movements; -import baritone.bot.utils.InputOverrideHandler; import baritone.bot.behavior.impl.LookBehaviorUtils; import baritone.bot.pathing.movement.CalculationContext; import baritone.bot.pathing.movement.Movement; @@ -25,6 +24,7 @@ import baritone.bot.pathing.movement.MovementState; import baritone.bot.pathing.movement.MovementState.MovementStatus; import baritone.bot.utils.BlockStateInterface; +import baritone.bot.utils.InputOverrideHandler; import baritone.bot.utils.Utils; import net.minecraft.block.BlockFalling; import net.minecraft.block.state.IBlockState; @@ -93,31 +93,19 @@ protected double calculateCost(CalculationContext context) { } @Override - public MovementState updateState(MovementState state) { - super.updateState(state); + public void onRunning(MovementState state) { // TODO incorporate some behavior from ActionClimb (specifically how it waited until it was at most 1.2 blocks away before starting to jump // for efficiency in ascending minimal height staircases, which is just repeated MovementAscend, so that it doesn't bonk its head on the ceiling repeatedly) - switch (state.getStatus()) { - case PREPPING: - case UNREACHABLE: - case FAILED: - return state; - case WAITING: - case RUNNING: - break; - default: - return state; - } if (playerFeet().equals(dest)) { state.setStatus(MovementStatus.SUCCESS); - return state; + return; } - if (!MovementHelper.canWalkOn(positionsToPlace[0])) { for (BlockPos anAgainst : against) { if (BlockStateInterface.get(anAgainst).isBlockNormalCube()) { if (!MovementHelper.throwaway(true)) {//get ready to place a throwaway block - return state.setStatus(MovementStatus.UNREACHABLE); + state.setStatus(MovementStatus.UNREACHABLE); + return; } double faceX = (dest.getX() + anAgainst.getX() + 1.0D) * 0.5D; double faceY = (dest.getY() + anAgainst.getY()) * 0.5D; @@ -135,14 +123,14 @@ public MovementState updateState(MovementState state) { } } System.out.println("Trying to look at " + anAgainst + ", actually looking at" + LookBehaviorUtils.getSelectedBlock()); - return state; + return; } } - return state.setStatus(MovementStatus.UNREACHABLE); + state.setStatus(MovementStatus.UNREACHABLE); + return; } MovementHelper.moveTowards(state, dest); state.setInput(InputOverrideHandler.Input.JUMP, true); - // TODO check if the below actually helps or hurts, it's weird //double flatDistToNext = Math.abs(to.getX() - from.getX()) * Math.abs((to.getX() + 0.5D) - thePlayer.posX) + Math.abs(to.getZ() - from.getZ()) * Math.abs((to.getZ() + 0.5D) - thePlayer.posZ); //boolean pointingInCorrectDirection = MovementManager.moveTowardsBlock(to); @@ -151,7 +139,5 @@ public MovementState updateState(MovementState state) { //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 Baritone.playerFeet.equals(to); - - return state; } } diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementDescend.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementDescend.java index fa2381e28..c3eb73fa0 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementDescend.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementDescend.java @@ -17,13 +17,13 @@ package baritone.bot.pathing.movement.movements; -import baritone.bot.utils.InputOverrideHandler; import baritone.bot.pathing.movement.CalculationContext; import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.MovementHelper; import baritone.bot.pathing.movement.MovementState; import baritone.bot.pathing.movement.MovementState.MovementStatus; import baritone.bot.utils.BlockStateInterface; +import baritone.bot.utils.InputOverrideHandler; import net.minecraft.block.Block; import net.minecraft.block.BlockLadder; import net.minecraft.block.BlockVine; @@ -56,26 +56,13 @@ protected double calculateCost(CalculationContext context) { int numTicks = 0; @Override - public MovementState updateState(MovementState state) { - super.updateState(state); - switch (state.getStatus()) { - case PREPPING: - case UNREACHABLE: - case FAILED: - return state; - case WAITING: - state.setStatus(MovementStatus.RUNNING); - case RUNNING: - break; - default: - return state; - } + public void onRunning(MovementState state) { BlockPos playerFeet = playerFeet(); if (playerFeet.equals(dest)) { if (BlockStateInterface.isLiquid(dest) || player().posY - playerFeet.getY() < 0.01) { // Wait until we're actually on the ground before saying we're done because sometimes we continue to fall if the next action starts immediately state.setStatus(MovementStatus.SUCCESS); - return state; + return; } else { System.out.println(player().posY + " " + playerFeet.getY() + " " + (player().posY - playerFeet.getY())); } @@ -101,6 +88,5 @@ public MovementState updateState(MovementState state) { MovementHelper.moveTowards(state, dest); } } - return state; } } diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementDiagonal.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementDiagonal.java index 5254d4158..a2cf6195e 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementDiagonal.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementDiagonal.java @@ -48,28 +48,15 @@ public MovementDiagonal(BlockPos start, BlockPos end, BlockPos dir1, BlockPos di } @Override - public MovementState updateState(MovementState state) { - super.updateState(state); - switch (state.getStatus()) { - case PREPPING: - case UNREACHABLE: - case FAILED: - return state; - case WAITING: - case RUNNING: - break; - default: - return state; - } + public void onRunning(MovementState state) { if (playerFeet().equals(dest)) { state.setStatus(MovementState.MovementStatus.SUCCESS); - return state; + return; } if (!BlockStateInterface.isLiquid(playerFeet())) { player().setSprinting(true); } MovementHelper.moveTowards(state, dest); - return state; } @Override @@ -129,8 +116,8 @@ protected double calculateCost(CalculationContext context) { } @Override - protected boolean prepared(MovementState state) { - return true; + protected void onPrepping(MovementState state) { + state.setStatus(MovementState.MovementStatus.RUNNING); } @Override diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementDownward.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementDownward.java index 10c84c5ec..9241e5cf3 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementDownward.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementDownward.java @@ -50,31 +50,18 @@ protected double calculateCost(CalculationContext context) { } @Override - public MovementState updateState(MovementState state) { - super.updateState(state); - switch (state.getStatus()) { - case PREPPING: - case UNREACHABLE: - case FAILED: - return state; - case WAITING: - case RUNNING: - break; - default: - return state; - } + public void onRunning(MovementState state) { if (playerFeet().equals(dest)) { state.setStatus(MovementState.MovementStatus.SUCCESS); - return state; + return; } double diffX = player().posX - (dest.getX() + 0.5); double diffZ = player().posZ - (dest.getZ() + 0.5); double ab = Math.sqrt(diffX * diffX + diffZ * diffZ); if (numTicks++ < 10 && ab < 0.2) { - return state; + return; } MovementHelper.moveTowards(state, positionsToBreak[0]); - return state; } } diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementFall.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementFall.java index d1312a39e..2c179af2b 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementFall.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementFall.java @@ -17,12 +17,12 @@ package baritone.bot.pathing.movement.movements; -import baritone.bot.utils.InputOverrideHandler; import baritone.bot.behavior.impl.LookBehaviorUtils; import baritone.bot.pathing.movement.*; import baritone.bot.pathing.movement.MovementState.MovementStatus; import baritone.bot.pathing.movement.MovementState.MovementTarget; import baritone.bot.utils.BlockStateInterface; +import baritone.bot.utils.InputOverrideHandler; import baritone.bot.utils.Rotation; import baritone.bot.utils.Utils; import net.minecraft.init.Items; @@ -64,26 +64,13 @@ protected double calculateCost(CalculationContext context) { } @Override - public MovementState updateState(MovementState state) { - super.updateState(state); - switch (state.getStatus()) { - case PREPPING: - case UNREACHABLE: - case FAILED: - return state; - case WAITING: - state.setStatus(MovementStatus.RUNNING); - case RUNNING: - break; - default: - return state; - } + public void onRunning(MovementState state) { BlockPos playerFeet = playerFeet(); Optional targetRotation = Optional.empty(); if (!BlockStateInterface.isWater(dest) && src.getY() - dest.getY() > 3 && !playerFeet.equals(dest)) { if (!player().inventory.hasItemStack(STACK_BUCKET_WATER) || world().provider.isNether()) { // TODO check if water bucket is on hotbar or main inventory state.setStatus(MovementStatus.UNREACHABLE); - return state; + return; } if (player().posY - dest.getY() < mc.playerController.getBlockReachDistance()) { player().inventory.currentItem = player().inventory.getSlotFor(STACK_BUCKET_WATER); @@ -101,18 +88,17 @@ public MovementState updateState(MovementState state) { if (BlockStateInterface.isWater(dest) && player().inventory.hasItemStack(STACK_BUCKET_EMPTY)) { player().inventory.currentItem = player().inventory.getSlotFor(STACK_BUCKET_EMPTY); if (player().motionY >= 0) { - return state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true); - } else { - return state; + state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true); } + return; } - return state.setStatus(MovementStatus.SUCCESS); + state.setStatus(MovementStatus.SUCCESS); + return; } Vec3d destCenter = Utils.getBlockPosCenter(dest); // we are moving to the 0.5 center not the edge (like if we were falling on a ladder) if (Math.abs(player().posX - destCenter.x) > 0.2 || Math.abs(player().posZ - destCenter.z) > 0.2) { state.setInput(InputOverrideHandler.Input.MOVE_FORWARD, true); } - return state; } private static BlockPos[] buildPositionsToBreak(BlockPos src, BlockPos dest) { diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementPillar.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementPillar.java index d8d68c873..006dce890 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementPillar.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementPillar.java @@ -17,12 +17,12 @@ package baritone.bot.pathing.movement.movements; -import baritone.bot.utils.InputOverrideHandler; import baritone.bot.pathing.movement.CalculationContext; 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.InputOverrideHandler; import baritone.bot.utils.Rotation; import baritone.bot.utils.Utils; import net.minecraft.block.*; @@ -96,19 +96,7 @@ public static BlockPos getAgainst(BlockPos vine) { } @Override - public MovementState updateState(MovementState state) { - super.updateState(state); - switch (state.getStatus()) { - case PREPPING: - case UNREACHABLE: - case FAILED: - return state; - case WAITING: - case RUNNING: - break; - default: - return state; - } + public void onRunning(MovementState state) { IBlockState fromDown = BlockStateInterface.get(src); boolean ladder = fromDown.getBlock() instanceof BlockLadder || fromDown.getBlock() instanceof BlockVine; boolean vine = fromDown.getBlock() instanceof BlockVine; @@ -124,21 +112,21 @@ public MovementState updateState(MovementState state) { if (against == null) { displayChatMessageRaw("Unable to climb vines"); state.setStatus(MovementState.MovementStatus.UNREACHABLE); - return state; + return; } if (playerFeet().equals(against.up()) || playerFeet().equals(dest)) { state.setStatus(MovementState.MovementStatus.SUCCESS); - return state; + return; } /*if (thePlayer.getPosition0().getX() != from.getX() || thePlayer.getPosition0().getZ() != from.getZ()) { Baritone.moveTowardsBlock(from); }*/ MovementHelper.moveTowards(state, against); - return state; + return; } else { if (!MovementHelper.throwaway(true)) {//get ready to place a throwaway block state.setStatus(MovementState.MovementStatus.UNREACHABLE); - return state; + return; } numTicks++; state.setInput(InputOverrideHandler.Input.JUMP, thePlayer.posY < dest.getY()); //if our Y coordinate is above our goal, stop jumping @@ -166,8 +154,6 @@ public MovementState updateState(MovementState state) { } 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 } - return state; } } \ No newline at end of file diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java index 92d147e27..325e4eed3 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java @@ -17,13 +17,13 @@ package baritone.bot.pathing.movement.movements; -import baritone.bot.utils.InputOverrideHandler; import baritone.bot.behavior.impl.LookBehaviorUtils; import baritone.bot.pathing.movement.CalculationContext; 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.InputOverrideHandler; import baritone.bot.utils.Utils; import net.minecraft.block.Block; import net.minecraft.block.BlockLadder; @@ -115,19 +115,7 @@ protected double calculateCost(CalculationContext context) { } @Override - public MovementState updateState(MovementState state) { - super.updateState(state); - switch (state.getStatus()) { - case PREPPING: - case UNREACHABLE: - case FAILED: - return state; - case WAITING: - case RUNNING: - break; - default: - return state; - } + public void onRunning(MovementState state) { Block fd = BlockStateInterface.get(src.down()).getBlock(); boolean ladder = fd instanceof BlockLadder || fd instanceof BlockVine; boolean isTheBridgeBlockThere = MovementHelper.canWalkOn(positionsToPlace[0]) || ladder; @@ -137,25 +125,25 @@ public MovementState updateState(MovementState state) { if (whereAmI.getY() < dest.getY()) { state.setInput(InputOverrideHandler.Input.JUMP, true); } - return state; + return; } if (isTheBridgeBlockThere) { if (playerFeet().equals(dest)) { state.setStatus(MovementState.MovementStatus.SUCCESS); - return state; + return; } if (wasTheBridgeBlockAlwaysThere && !BlockStateInterface.isLiquid(playerFeet())) { player().setSprinting(true); } MovementHelper.moveTowards(state, positionsToBreak[0]); - return state; } else { wasTheBridgeBlockAlwaysThere = false; for (BlockPos against1 : against) { if (BlockStateInterface.get(against1).isBlockNormalCube()) { if (!MovementHelper.throwaway(true)) { // get ready to place a throwaway block displayChatMessageRaw("bb pls get me some blocks. dirt or cobble"); - return state.setStatus(MovementState.MovementStatus.UNREACHABLE); + state.setStatus(MovementState.MovementStatus.UNREACHABLE); + return; } state.setInput(InputOverrideHandler.Input.SNEAK, true); double faceX = (dest.getX() + against1.getX() + 1.0D) * 0.5D; @@ -172,7 +160,7 @@ public MovementState updateState(MovementState state) { } } System.out.println("Trying to look at " + against1 + ", actually looking at" + LookBehaviorUtils.getSelectedBlock()); - return state; + return; } } state.setInput(InputOverrideHandler.Input.SNEAK, true); @@ -181,7 +169,8 @@ public MovementState updateState(MovementState state) { // 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"); - return state.setStatus(MovementState.MovementStatus.UNREACHABLE); + state.setStatus(MovementState.MovementStatus.UNREACHABLE); + return; } double faceX = (dest.getX() + src.getX() + 1.0D) * 0.5D; double faceY = (dest.getY() + src.getY() - 1.0D) * 0.5D; @@ -194,13 +183,10 @@ public MovementState updateState(MovementState state) { state.setInput(InputOverrideHandler.Input.SNEAK, true); if (Objects.equals(LookBehaviorUtils.getSelectedBlock().orElse(null), goalLook)) { state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true); // wait to right click until we are able to place - return state; } // Out.log("Trying to look at " + goalLook + ", actually looking at" + Baritone.whatAreYouLookingAt()); - return state; } else { MovementHelper.moveTowards(state, positionsToBreak[0]); - return state; // TODO MovementManager.moveTowardsBlock(to); // move towards not look at because if we are bridging for a couple blocks in a row, it is faster if we dont spin around and walk forwards then spin around and place backwards for every block } }