diff --git a/src/main/java/baritone/bot/behavior/PathExecution.java b/src/main/java/baritone/bot/behavior/PathExecution.java new file mode 100644 index 00000000..7b5c4adc --- /dev/null +++ b/src/main/java/baritone/bot/behavior/PathExecution.java @@ -0,0 +1,38 @@ +package baritone.bot.behavior; + +import baritone.bot.pathing.IPath; +import baritone.bot.pathing.movements.Movement; +import baritone.bot.utils.Helper; +import net.minecraft.util.Tuple; +import net.minecraft.util.math.BlockPos; + +/** + * @author Brady + * @since 8/1/2018 5:38 PM + */ +public class PathExecution extends Behavior implements Helper { + private static final double MAX_DIST_FROM_PATH = 2; + private final IPath path; + + public PathExecution(IPath path) { + this.path = path; + } + + public void onTick() { + BlockPos playerFeet = playerFeet(); + // TODO copy logic from Path in resources + if (path.isInPath(playerFeet)) { + // TODO the old Path used to keep track of the index in the path + // and only increment it when the movement said it was done, not when it detected that the player feet had + // moved into the next position + Movement movement = path.subsequentMovement(playerFeet); + //movement.tick() + } else { + Tuple closest = path.closestPathPos(player.posX, player.posY, player.posZ); + if (closest.getFirst() > MAX_DIST_FROM_PATH) { + // TODO how to indicate failure? Exception? + } + } + } + +} diff --git a/src/main/java/baritone/bot/behavior/PathFinding.java b/src/main/java/baritone/bot/behavior/PathFinding.java deleted file mode 100644 index 131712c7..00000000 --- a/src/main/java/baritone/bot/behavior/PathFinding.java +++ /dev/null @@ -1,8 +0,0 @@ -package baritone.bot.behavior; - -/** - * @author Brady - * @since 8/1/2018 5:38 PM - */ -public class PathFinding { -} diff --git a/src/main/java/baritone/bot/pathing/IPath.java b/src/main/java/baritone/bot/pathing/IPath.java index 1ff15de5..a7b64c6b 100644 --- a/src/main/java/baritone/bot/pathing/IPath.java +++ b/src/main/java/baritone/bot/pathing/IPath.java @@ -1,5 +1,8 @@ package baritone.bot.pathing; +import baritone.bot.pathing.movements.Movement; +import baritone.bot.utils.Utils; +import net.minecraft.util.Tuple; import net.minecraft.util.math.BlockPos; import java.util.Collection; @@ -7,9 +10,12 @@ import java.util.List; public interface IPath { /** - * + * Ordered list of movements to carry out. + * movements.get(i).getSrc() should equal positions.get(i) + * movements.get(i).getDest() should equal positions.get(i+1) + * movements.size() should equal positions.size()-1 */ - List movements(); + List movements(); /** * All positions along the way. @@ -17,6 +23,44 @@ public interface IPath { */ List positions(); + /** + * What's the next step + * + * @param currentPosition the current position + * @return + */ + default Movement subsequentMovement(BlockPos currentPosition) { + List pos = positions(); + List moves = movements(); + for (int i = 0; i < pos.size(); i++) { + if (currentPosition.equals(pos.get(i))) { + return moves.get(i); + } + } + throw new UnsupportedOperationException(currentPosition + " not in path"); + } + + /** + * @param currentPosition + * @return + */ + default boolean isInPath(BlockPos currentPosition) { + return positions().stream().anyMatch(pos -> currentPosition.equals(pos)); + } + + default Tuple closestPathPos(double x, double y, double z) { + double best = -1; + BlockPos bestPos = null; + for (BlockPos pos : positions()) { + double dist = Utils.distanceToCenter(pos, x, y, z); + if (dist < best || best == -1) { + best = dist; + bestPos = pos; + } + } + return new Tuple<>(best, bestPos); + } + /** * Where does this path start */ @@ -29,12 +73,14 @@ public interface IPath { /** * For rendering purposes, what blocks should be highlighted in red + * * @return an unordered collection of positions */ Collection getBlocksToMine(); /** * For rendering purposes, what blocks should be highlighted in green + * * @return an unordered collection of positions */ Collection getBlocksToPlace(); diff --git a/src/main/java/baritone/bot/pathing/action/Action.java b/src/main/java/baritone/bot/pathing/action/Action.java index fe78a2dd..83e559f8 100644 --- a/src/main/java/baritone/bot/pathing/action/Action.java +++ b/src/main/java/baritone/bot/pathing/action/Action.java @@ -2,10 +2,10 @@ package baritone.bot.pathing.action; import baritone.bot.Baritone; import baritone.bot.behavior.Behavior; +import baritone.bot.pathing.action.ActionState.ActionStatus; import baritone.bot.utils.Utils; import net.minecraft.util.Tuple; import net.minecraft.util.math.BlockPos; -import baritone.bot.pathing.action.ActionState.ActionStatus; public abstract class Action extends Behavior { @@ -29,7 +29,9 @@ public abstract class Action extends Behavior { * * @return Cost */ - public double cost() { return 0; } + public double cost() { + return 0; + } @Override public void onTick() { @@ -41,13 +43,13 @@ public abstract class Action extends Behavior { }); currentState = latestState; - if(isFinished()) + if (isFinished()) return; } public boolean isFinished() { - return(currentState.getStatus() != ActionStatus.RUNNING - && currentState.getStatus() != ActionStatus.WAITING); + return (currentState.getStatus() != ActionStatus.RUNNING + && currentState.getStatus() != ActionStatus.WAITING); } /** diff --git a/src/main/java/baritone/bot/utils/Helper.java b/src/main/java/baritone/bot/utils/Helper.java index 0591acf1..8e3e82ae 100755 --- a/src/main/java/baritone/bot/utils/Helper.java +++ b/src/main/java/baritone/bot/utils/Helper.java @@ -3,6 +3,7 @@ package baritone.bot.utils; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.multiplayer.WorldClient; +import net.minecraft.util.math.BlockPos; /** * @author Brady @@ -14,4 +15,8 @@ public interface Helper { EntityPlayerSP player = mc.player; WorldClient world = mc.world; + default BlockPos playerFeet() { + return new BlockPos(player.posX, player.posY, player.posZ); + } + } diff --git a/src/main/java/baritone/bot/utils/Utils.java b/src/main/java/baritone/bot/utils/Utils.java index e53f7908..ca69a0e3 100755 --- a/src/main/java/baritone/bot/utils/Utils.java +++ b/src/main/java/baritone/bot/utils/Utils.java @@ -30,4 +30,11 @@ public final class Utils { orig.getY() + yDiff, orig.getZ() + zDiff); } + + public static double distanceToCenter(BlockPos pos, double x, double y, double z) { + double xdiff = x - (pos.getX() + 0.5D); + double ydiff = y - (pos.getY() + 0.5D); + double zdiff = z - (pos.getZ() + 0.5D); + return Math.sqrt(xdiff * xdiff + ydiff * ydiff + zdiff * zdiff); + } }