From 27b9324cf14813552228ca74a2664a52f20ad499 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sat, 15 Sep 2018 12:51:37 -0700 Subject: [PATCH] add goal axis and fix movement fall path detection --- src/main/java/baritone/Settings.java | 5 ++ .../java/baritone/pathing/goals/GoalAxis.java | 54 +++++++++++++++++++ .../baritone/pathing/goals/GoalBlock.java | 4 +- .../pathing/goals/GoalGetToBlock.java | 5 ++ .../baritone/pathing/goals/GoalRunAway.java | 6 ++- .../java/baritone/pathing/goals/GoalXZ.java | 2 +- .../baritone/pathing/goals/GoalYLevel.java | 14 +++-- .../movement/movements/MovementFall.java | 19 ++++--- .../baritone/pathing/path/PathExecutor.java | 2 +- .../utils/ExampleBaritoneControl.java | 6 +++ 10 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 src/main/java/baritone/pathing/goals/GoalAxis.java diff --git a/src/main/java/baritone/Settings.java b/src/main/java/baritone/Settings.java index d14d75ee..67ac6ab5 100644 --- a/src/main/java/baritone/Settings.java +++ b/src/main/java/baritone/Settings.java @@ -338,6 +338,11 @@ public class Settings { */ public Setting cancelOnGoalInvalidation = new Setting<>(true); + /** + * The "axis" command (aka GoalAxis) will go to a axis, or diagonal axis, at this Y level. + */ + public Setting axisHeight = new Setting<>(120); + public final Map> byLowerName; public final List> allSettings; diff --git a/src/main/java/baritone/pathing/goals/GoalAxis.java b/src/main/java/baritone/pathing/goals/GoalAxis.java new file mode 100644 index 00000000..9e6dc697 --- /dev/null +++ b/src/main/java/baritone/pathing/goals/GoalAxis.java @@ -0,0 +1,54 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Baritone. If not, see . + */ + +package baritone.pathing.goals; + +import baritone.Baritone; +import net.minecraft.util.math.BlockPos; + +public class GoalAxis implements Goal { + + private static final double SQRT_2_OVER_2 = Math.sqrt(2) / 2; + + @Override + public boolean isInGoal(BlockPos pos) { + int x = pos.getX(); + int y = pos.getY(); + int z = pos.getZ(); + return y == Baritone.settings().axisHeight.get() && (x == 0 || z == 0 || Math.abs(x) == Math.abs(z)); + } + + @Override + public double heuristic(BlockPos pos) { + int x = Math.abs(pos.getX()); + int y = pos.getY(); + int z = Math.abs(pos.getZ()); + + int shrt = Math.min(x, z); + int lng = Math.max(x, z); + int diff = lng - shrt; + + double flatAxisDistance = Math.min(x, Math.min(z, diff * SQRT_2_OVER_2)); + + return flatAxisDistance * Baritone.settings().costHeuristic.get() + GoalYLevel.calculate(Baritone.settings().axisHeight.get(), y); + } + + @Override + public String toString() { + return "GoalAxis"; + } +} diff --git a/src/main/java/baritone/pathing/goals/GoalBlock.java b/src/main/java/baritone/pathing/goals/GoalBlock.java index 9e6ea556..124740a3 100644 --- a/src/main/java/baritone/pathing/goals/GoalBlock.java +++ b/src/main/java/baritone/pathing/goals/GoalBlock.java @@ -77,7 +77,7 @@ public class GoalBlock implements Goal, IGoalRenderPos { @Override public String toString() { - return "Goal{x=" + x + ",y=" + y + ",z=" + z + "}"; + return "GoalBlock{x=" + x + ",y=" + y + ",z=" + z + "}"; } /** @@ -92,7 +92,7 @@ public class GoalBlock implements Goal, IGoalRenderPos { // if yDiff is 1 that means that pos.getY()-this.y==1 which means that we're 1 block below where we should be // therefore going from 0,0,0 to a GoalYLevel of pos.getY()-this.y is accurate - heuristic += new GoalYLevel(yDiff).heuristic(new BlockPos(0, 0, 0)); + heuristic += GoalYLevel.calculate(yDiff, 0); //use the pythagorean and manhattan mixture from GoalXZ heuristic += GoalXZ.calculate(xDiff, zDiff); diff --git a/src/main/java/baritone/pathing/goals/GoalGetToBlock.java b/src/main/java/baritone/pathing/goals/GoalGetToBlock.java index 4e81018b..16cf9f4b 100644 --- a/src/main/java/baritone/pathing/goals/GoalGetToBlock.java +++ b/src/main/java/baritone/pathing/goals/GoalGetToBlock.java @@ -61,4 +61,9 @@ public class GoalGetToBlock implements Goal, IGoalRenderPos { int zDiff = pos.getZ() - this.z; return GoalBlock.calculate(xDiff, yDiff, zDiff); } + + @Override + public String toString() { + return "GoalGetToBlock{x=" + x + ",y=" + y + ",z=" + z + "}"; + } } diff --git a/src/main/java/baritone/pathing/goals/GoalRunAway.java b/src/main/java/baritone/pathing/goals/GoalRunAway.java index 620794d2..90e2f68e 100644 --- a/src/main/java/baritone/pathing/goals/GoalRunAway.java +++ b/src/main/java/baritone/pathing/goals/GoalRunAway.java @@ -17,11 +17,13 @@ package baritone.pathing.goals; -import java.util.Arrays; import net.minecraft.util.math.BlockPos; +import java.util.Arrays; + /** * Useful for automated combat (retreating specifically) + * * @author leijurv */ public class GoalRunAway implements Goal { @@ -65,6 +67,6 @@ public class GoalRunAway implements Goal { @Override public String toString() { - return "GoalRunAwayFrom[" + Arrays.asList(from) + "]"; + return "GoalRunAwayFrom" + Arrays.asList(from); } } diff --git a/src/main/java/baritone/pathing/goals/GoalXZ.java b/src/main/java/baritone/pathing/goals/GoalXZ.java index 71808222..7b876665 100644 --- a/src/main/java/baritone/pathing/goals/GoalXZ.java +++ b/src/main/java/baritone/pathing/goals/GoalXZ.java @@ -61,7 +61,7 @@ public class GoalXZ implements Goal { @Override public String toString() { - return "Goal{x=" + x + ",z=" + z + "}"; + return "GoalXZ{x=" + x + ",z=" + z + "}"; } public static double calculate(double xDiff, double zDiff) { diff --git a/src/main/java/baritone/pathing/goals/GoalYLevel.java b/src/main/java/baritone/pathing/goals/GoalYLevel.java index 89cef250..445a450b 100644 --- a/src/main/java/baritone/pathing/goals/GoalYLevel.java +++ b/src/main/java/baritone/pathing/goals/GoalYLevel.java @@ -42,19 +42,23 @@ public class GoalYLevel implements Goal { @Override public double heuristic(BlockPos pos) { - if (pos.getY() > level) { + return calculate(level, pos.getY()); + } + + static double calculate(int goalY, int currentY) { + if (currentY > goalY) { // need to descend - return FALL_N_BLOCKS_COST[2] / 2 * (pos.getY() - level); + return FALL_N_BLOCKS_COST[2] / 2 * (currentY - goalY); } - if (pos.getY() < level) { + if (currentY < goalY) { // need to ascend - return (level - pos.getY()) * JUMP_ONE_BLOCK_COST; + return (goalY - currentY) * JUMP_ONE_BLOCK_COST; } return 0; } @Override public String toString() { - return "Goal{y=" + level + "}"; + return "GoalYLevel{y=" + level + "}"; } } diff --git a/src/main/java/baritone/pathing/movement/movements/MovementFall.java b/src/main/java/baritone/pathing/movement/movements/MovementFall.java index 60a5a0a5..e0356444 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementFall.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementFall.java @@ -134,15 +134,22 @@ public class MovementFall extends Movement { state.setTarget(new MovementTarget(Utils.calcRotationFromVec3d(playerHead(), Utils.getBlockPosCenter(dest)), false)); } if (playerFeet.equals(dest) && (player().posY - playerFeet.getY() < 0.094 || BlockStateInterface.isWater(dest))) { // 0.094 because lilypads - if (BlockStateInterface.isWater(dest) && InventoryPlayer.isHotbar(player().inventory.getSlotFor(STACK_BUCKET_EMPTY))) { - player().inventory.currentItem = player().inventory.getSlotFor(STACK_BUCKET_EMPTY); - if (player().motionY >= 0) { - return state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true); + if (BlockStateInterface.isWater(dest)) { + if (InventoryPlayer.isHotbar(player().inventory.getSlotFor(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; + } } else { - return state; + if (player().motionY >= 0) { + return state.setStatus(MovementStatus.SUCCESS); + } // don't else return state; we need to stay centered because this water might be flowing under the surface } + } else { + return state.setStatus(MovementStatus.SUCCESS); } - return state.setStatus(MovementStatus.SUCCESS); } 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) { diff --git a/src/main/java/baritone/pathing/path/PathExecutor.java b/src/main/java/baritone/pathing/path/PathExecutor.java index 4f955611..0e2750e0 100644 --- a/src/main/java/baritone/pathing/path/PathExecutor.java +++ b/src/main/java/baritone/pathing/path/PathExecutor.java @@ -264,7 +264,7 @@ public class PathExecutor implements Helper { // when we're midair in the middle of a fall, we're very far from both the beginning and the end, but we aren't actually off path if (path.movements().get(pathPosition) instanceof MovementFall) { BlockPos fallDest = path.positions().get(pathPosition + 1); // .get(pathPosition) is the block we fell off of - if (Utils.playerFlatDistanceToCenter(fallDest) < 0.5) { + if (Utils.playerFlatDistanceToCenter(fallDest) < leniency) { // ignore Y by using flat distance return false; } return true; diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 65208d76..cb6f1382 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -119,6 +119,12 @@ public class ExampleBaritoneControl extends Behavior implements Helper { event.cancel(); return; } + if (msg.equals("axis")) { + PathingBehavior.INSTANCE.setGoal(new GoalAxis()); + PathingBehavior.INSTANCE.path(); + event.cancel(); + return; + } if (msg.toLowerCase().equals("cancel")) { MineBehavior.INSTANCE.cancel(); FollowBehavior.INSTANCE.cancel();