From 1245e222a761ffc3ac4e8be831b01140e5b057df Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 8 Oct 2018 19:23:43 -0500 Subject: [PATCH 01/12] Begin path api prep --- .../baritone/behavior/PathingBehavior.java | 26 ++++++++- .../baritone/pathing/path/CutoffResult.java | 54 +++++++++++++++++++ .../java/baritone/pathing/path/IPath.java | 29 +++++----- 3 files changed, 90 insertions(+), 19 deletions(-) create mode 100644 src/main/java/baritone/pathing/path/CutoffResult.java diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 2092c656..16c88b92 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -30,6 +30,7 @@ import baritone.pathing.calc.AStarPathFinder; import baritone.pathing.calc.AbstractNodeCostSearch; import baritone.pathing.calc.IPathFinder; import baritone.pathing.movement.MovementHelper; +import baritone.pathing.path.CutoffResult; import baritone.pathing.path.IPath; import baritone.pathing.path.PathExecutor; import baritone.utils.BlockBreakHelper; @@ -283,9 +284,30 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, Optional path = findPath(start, previous); if (Baritone.settings().cutoffAtLoadBoundary.get()) { - path = path.map(IPath::cutoffAtLoadedChunks); + path = path.map(p -> { + CutoffResult result = p.cutoffAtLoadedChunks(); + + if (result.wasCut()) { + logDebug("Cutting off path at edge of loaded chunks"); + logDebug("Length decreased by " + result.getRemoved()); + } else { + logDebug("Path ends within loaded chunks"); + } + + return result.getPath(); + }); } - Optional executor = path.map(p -> p.staticCutoff(goal)).map(PathExecutor::new); + + Optional executor = path.map(p -> { + CutoffResult result = p.staticCutoff(goal); + + if (result.wasCut()) { + logDebug("Static cutoff " + p.length() + " to " + result.getPath().length()); + } + + return result.getPath(); + }).map(PathExecutor::new); + synchronized (pathPlanLock) { if (current == null) { if (executor.isPresent()) { diff --git a/src/main/java/baritone/pathing/path/CutoffResult.java b/src/main/java/baritone/pathing/path/CutoffResult.java new file mode 100644 index 00000000..d1c03c1e --- /dev/null +++ b/src/main/java/baritone/pathing/path/CutoffResult.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 Lesser 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.pathing.path; + +/** + * @author Brady + * @since 10/8/2018 + */ +public final class CutoffResult { + + private final IPath path; + + private final int removed; + + private CutoffResult(IPath path, int removed) { + this.path = path; + this.removed = removed; + } + + public final boolean wasCut() { + return this.removed > 0; + } + + public final int getRemoved() { + return this.removed; + } + + public final IPath getPath() { + return this.path; + } + + public static CutoffResult cutoffPath(IPath path, int index) { + return new CutoffResult(new CutoffPath(path, index), path.positions().size() - index - 1); + } + + public static CutoffResult preservePath(IPath path) { + return new CutoffResult(path, 0); + } +} diff --git a/src/main/java/baritone/pathing/path/IPath.java b/src/main/java/baritone/pathing/path/IPath.java index 0701abf6..5741c440 100644 --- a/src/main/java/baritone/pathing/path/IPath.java +++ b/src/main/java/baritone/pathing/path/IPath.java @@ -17,10 +17,9 @@ package baritone.pathing.path; -import baritone.Baritone; +import baritone.api.BaritoneAPI; import baritone.api.pathing.goals.Goal; import baritone.pathing.movement.Movement; -import baritone.utils.Helper; import baritone.utils.Utils; import baritone.utils.pathing.BetterBlockPos; import net.minecraft.client.Minecraft; @@ -33,7 +32,7 @@ import java.util.List; /** * @author leijurv */ -public interface IPath extends Helper { +public interface IPath { /** * Ordered list of movements to carry out. @@ -87,7 +86,7 @@ public interface IPath extends Helper { /** * Where does this path start */ - default BetterBlockPos getSrc() { + default BlockPos getSrc() { return positions().get(0); } @@ -110,29 +109,25 @@ public interface IPath extends Helper { int getNumNodesConsidered(); - default IPath cutoffAtLoadedChunks() { + default CutoffResult cutoffAtLoadedChunks() { for (int i = 0; i < positions().size(); i++) { BlockPos pos = positions().get(i); if (Minecraft.getMinecraft().world.getChunk(pos) instanceof EmptyChunk) { - logDebug("Cutting off path at edge of loaded chunks"); - logDebug("Length decreased by " + (positions().size() - i - 1)); - return new CutoffPath(this, i); + return CutoffResult.cutoffPath(this, i); } } - logDebug("Path ends within loaded chunks"); - return this; + return CutoffResult.preservePath(this); } - default IPath staticCutoff(Goal destination) { - if (length() < Baritone.settings().pathCutoffMinimumLength.get()) { - return this; + default CutoffResult staticCutoff(Goal destination) { + if (length() < BaritoneAPI.getSettings().pathCutoffMinimumLength.get()) { + return CutoffResult.preservePath(this); } if (destination == null || destination.isInGoal(getDest())) { - return this; + return CutoffResult.preservePath(this); } - double factor = Baritone.settings().pathCutoffFactor.get(); + double factor = BaritoneAPI.getSettings().pathCutoffFactor.get(); int newLength = (int) (length() * factor); - logDebug("Static cutoff " + length() + " to " + newLength); - return new CutoffPath(this, newLength); + return CutoffResult.cutoffPath(this, newLength); } } From d177db5a35ba146876b8540bdb116e36dee06713 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 8 Oct 2018 19:57:22 -0500 Subject: [PATCH 02/12] IMovement Hey would you look at that --- src/main/java/baritone/pathing/calc/Path.java | 3 +- .../pathing/movement/CalculationContext.java | 18 ++--- .../baritone/pathing/movement/IMovement.java | 67 +++++++++++++++++++ .../baritone/pathing/movement/Movement.java | 31 +++++++-- .../pathing/movement/MovementState.java | 4 -- .../pathing/movement/MovementStatus.java | 27 ++++++++ .../movement/movements/MovementAscend.java | 6 +- .../movement/movements/MovementDescend.java | 2 +- .../movement/movements/MovementDiagonal.java | 9 +-- .../movement/movements/MovementDownward.java | 9 +-- .../movement/movements/MovementFall.java | 2 +- .../movement/movements/MovementParkour.java | 15 ++--- .../movement/movements/MovementPillar.java | 17 ++--- .../movement/movements/MovementTraverse.java | 17 ++--- .../baritone/pathing/path/CutoffPath.java | 6 +- .../java/baritone/pathing/path/IPath.java | 8 +-- .../baritone/pathing/path/PathExecutor.java | 27 +++----- .../utils/ExampleBaritoneControl.java | 4 +- 18 files changed, 179 insertions(+), 93 deletions(-) create mode 100644 src/main/java/baritone/pathing/movement/IMovement.java create mode 100644 src/main/java/baritone/pathing/movement/MovementStatus.java diff --git a/src/main/java/baritone/pathing/calc/Path.java b/src/main/java/baritone/pathing/calc/Path.java index f1a31553..6606cfba 100644 --- a/src/main/java/baritone/pathing/calc/Path.java +++ b/src/main/java/baritone/pathing/calc/Path.java @@ -18,6 +18,7 @@ package baritone.pathing.calc; import baritone.api.pathing.goals.Goal; +import baritone.pathing.movement.IMovement; import baritone.pathing.movement.Movement; import baritone.pathing.movement.Moves; import baritone.pathing.path.IPath; @@ -161,7 +162,7 @@ class Path implements IPath { } @Override - public List movements() { + public List movements() { if (!verified) { throw new IllegalStateException(); } diff --git a/src/main/java/baritone/pathing/movement/CalculationContext.java b/src/main/java/baritone/pathing/movement/CalculationContext.java index 0b40539e..464703ff 100644 --- a/src/main/java/baritone/pathing/movement/CalculationContext.java +++ b/src/main/java/baritone/pathing/movement/CalculationContext.java @@ -17,7 +17,7 @@ package baritone.pathing.movement; -import baritone.Baritone; +import baritone.api.BaritoneAPI; import baritone.api.pathing.movement.ActionCosts; import baritone.utils.Helper; import baritone.utils.ToolSet; @@ -51,20 +51,20 @@ public class CalculationContext implements Helper { public CalculationContext(ToolSet toolSet) { this.toolSet = toolSet; - this.hasThrowaway = Baritone.settings().allowPlace.get() && MovementHelper.throwaway(false); - this.hasWaterBucket = Baritone.settings().allowWaterBucketFall.get() && InventoryPlayer.isHotbar(player().inventory.getSlotFor(STACK_BUCKET_WATER)) && !world().provider.isNether(); - this.canSprint = Baritone.settings().allowSprint.get() && player().getFoodStats().getFoodLevel() > 6; - this.placeBlockCost = Baritone.settings().blockPlacementPenalty.get(); - this.allowBreak = Baritone.settings().allowBreak.get(); - this.maxFallHeightNoWater = Baritone.settings().maxFallHeightNoWater.get(); - this.maxFallHeightBucket = Baritone.settings().maxFallHeightBucket.get(); + this.hasThrowaway = BaritoneAPI.getSettings().allowPlace.get() && MovementHelper.throwaway(false); + this.hasWaterBucket = BaritoneAPI.getSettings().allowWaterBucketFall.get() && InventoryPlayer.isHotbar(player().inventory.getSlotFor(STACK_BUCKET_WATER)) && !world().provider.isNether(); + this.canSprint = BaritoneAPI.getSettings().allowSprint.get() && player().getFoodStats().getFoodLevel() > 6; + this.placeBlockCost = BaritoneAPI.getSettings().blockPlacementPenalty.get(); + this.allowBreak = BaritoneAPI.getSettings().allowBreak.get(); + this.maxFallHeightNoWater = BaritoneAPI.getSettings().maxFallHeightNoWater.get(); + this.maxFallHeightBucket = BaritoneAPI.getSettings().maxFallHeightBucket.get(); int depth = EnchantmentHelper.getDepthStriderModifier(player()); if (depth > 3) { depth = 3; } float mult = depth / 3.0F; this.waterWalkSpeed = ActionCosts.WALK_ONE_IN_WATER_COST * (1 - mult) + ActionCosts.WALK_ONE_BLOCK_COST * mult; - this.breakBlockAdditionalCost = Baritone.settings().blockBreakAdditionalPenalty.get(); + this.breakBlockAdditionalCost = BaritoneAPI.getSettings().blockBreakAdditionalPenalty.get(); // why cache these things here, why not let the movements just get directly from settings? // because if some movements are calculated one way and others are calculated another way, // then you get a wildly inconsistent path that isn't optimal for either scenario. diff --git a/src/main/java/baritone/pathing/movement/IMovement.java b/src/main/java/baritone/pathing/movement/IMovement.java new file mode 100644 index 00000000..83b65019 --- /dev/null +++ b/src/main/java/baritone/pathing/movement/IMovement.java @@ -0,0 +1,67 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.pathing.movement; + +import baritone.utils.pathing.BetterBlockPos; +import net.minecraft.util.math.BlockPos; + +import java.util.List; + +/** + * @author Brady + * @since 10/8/2018 + */ +public interface IMovement { + + double getCost(); + + MovementStatus update(); + + /** + * Resets the current state status to {@link MovementStatus#PREPPING} + */ + void reset(); + + /** + * Resets the cache for special break, place, and walk into blocks + */ + void resetBlockCache(); + + /** + * @return Whether or not it is safe to cancel the current movement state + */ + boolean safeToCancel(); + + double recalculateCost(); + + double calculateCostWithoutCaching(); + + boolean calculatedWhileLoaded(); + + BetterBlockPos getSrc(); + + BetterBlockPos getDest(); + + BlockPos getDirection(); + + List toBreak(); + + List toPlace(); + + List toWalkInto(); +} diff --git a/src/main/java/baritone/pathing/movement/Movement.java b/src/main/java/baritone/pathing/movement/Movement.java index 2b87c1e0..b529873a 100644 --- a/src/main/java/baritone/pathing/movement/Movement.java +++ b/src/main/java/baritone/pathing/movement/Movement.java @@ -21,7 +21,6 @@ import baritone.Baritone; import baritone.api.utils.Rotation; import baritone.behavior.LookBehavior; import baritone.behavior.LookBehaviorUtils; -import baritone.pathing.movement.MovementState.MovementStatus; import baritone.utils.*; import baritone.utils.pathing.BetterBlockPos; import net.minecraft.block.BlockLiquid; @@ -36,7 +35,7 @@ import java.util.Optional; import static baritone.utils.InputOverrideHandler.Input; -public abstract class Movement implements Helper, MovementHelper { +public abstract class Movement implements IMovement, Helper, MovementHelper { protected static final EnumFacing[] HORIZONTALS = {EnumFacing.NORTH, EnumFacing.SOUTH, EnumFacing.EAST, EnumFacing.WEST}; @@ -64,7 +63,7 @@ public abstract class Movement implements Helper, MovementHelper { public List toPlaceCached = null; public List toWalkIntoCached = null; - private Boolean calculatedWhileLoaded; + private boolean calculatedWhileLoaded; protected Movement(BetterBlockPos src, BetterBlockPos dest, BetterBlockPos[] toBreak, BetterBlockPos toPlace) { this.src = src; @@ -77,24 +76,27 @@ public abstract class Movement implements Helper, MovementHelper { this(src, dest, toBreak, null); } - public double getCost(CalculationContext context) { + @Override + public double getCost() { if (cost == null) { - cost = calculateCost(context != null ? context : new CalculationContext()); + cost = calculateCost(new CalculationContext()); } return cost; } protected abstract double calculateCost(CalculationContext context); + @Override public double recalculateCost() { cost = null; - return getCost(null); + return getCost(); } protected void override(double cost) { this.cost = cost; } + @Override public double calculateCostWithoutCaching() { return calculateCost(new CalculationContext()); } @@ -105,6 +107,7 @@ public abstract class Movement implements Helper, MovementHelper { * * @return Status */ + @Override public MovementStatus update() { player().capabilities.allowFlying = false; MovementState latestState = updateState(currentState); @@ -187,6 +190,7 @@ public abstract class Movement implements Helper, MovementHelper { return true; } + @Override public boolean safeToCancel() { return safeToCancel(currentState); } @@ -201,10 +205,12 @@ public abstract class Movement implements Helper, MovementHelper { && currentState.getStatus() != MovementStatus.WAITING); } + @Override public BetterBlockPos getSrc() { return src; } + @Override public BetterBlockPos getDest() { return dest; } @@ -223,6 +229,7 @@ public abstract class Movement implements Helper, MovementHelper { currentState.setStatus(MovementStatus.CANCELED); } + @Override public void reset() { currentState = new MovementState().setStatus(MovementStatus.PREPPING); } @@ -247,6 +254,7 @@ public abstract class Movement implements Helper, MovementHelper { return state; } + @Override public BlockPos getDirection() { return getDest().subtract(getSrc()); } @@ -255,10 +263,19 @@ public abstract class Movement implements Helper, MovementHelper { calculatedWhileLoaded = !(world().getChunk(getDest()) instanceof EmptyChunk); } + @Override public boolean calculatedWhileLoaded() { return calculatedWhileLoaded; } + @Override + public void resetBlockCache() { + toBreakCached = null; + toPlaceCached = null; + toWalkIntoCached = null; + } + + @Override public List toBreak() { if (toBreakCached != null) { return toBreakCached; @@ -273,6 +290,7 @@ public abstract class Movement implements Helper, MovementHelper { return result; } + @Override public List toPlace() { if (toPlaceCached != null) { return toPlaceCached; @@ -285,6 +303,7 @@ public abstract class Movement implements Helper, MovementHelper { return result; } + @Override public List toWalkInto() { // overridden by movementdiagonal if (toWalkIntoCached == null) { toWalkIntoCached = new ArrayList<>(); diff --git a/src/main/java/baritone/pathing/movement/MovementState.java b/src/main/java/baritone/pathing/movement/MovementState.java index 6d0262e6..acf223d4 100644 --- a/src/main/java/baritone/pathing/movement/MovementState.java +++ b/src/main/java/baritone/pathing/movement/MovementState.java @@ -72,10 +72,6 @@ public class MovementState { return this.inputState; } - public enum MovementStatus { - PREPPING, WAITING, RUNNING, SUCCESS, UNREACHABLE, FAILED, CANCELED - } - public static class MovementTarget { /** diff --git a/src/main/java/baritone/pathing/movement/MovementStatus.java b/src/main/java/baritone/pathing/movement/MovementStatus.java new file mode 100644 index 00000000..4b4b92c3 --- /dev/null +++ b/src/main/java/baritone/pathing/movement/MovementStatus.java @@ -0,0 +1,27 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.pathing.movement; + +/** + * @author Brady + * @since 10/8/2018 + */ +public enum MovementStatus { + + PREPPING, WAITING, RUNNING, SUCCESS, UNREACHABLE, FAILED, CANCELED +} diff --git a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java index b5e21eea..9ad42d99 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java @@ -19,11 +19,7 @@ package baritone.pathing.movement.movements; import baritone.Baritone; import baritone.behavior.LookBehaviorUtils; -import baritone.pathing.movement.CalculationContext; -import baritone.pathing.movement.Movement; -import baritone.pathing.movement.MovementHelper; -import baritone.pathing.movement.MovementState; -import baritone.pathing.movement.MovementState.MovementStatus; +import baritone.pathing.movement.*; import baritone.utils.BlockStateInterface; import baritone.utils.InputOverrideHandler; import baritone.utils.Utils; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementDescend.java b/src/main/java/baritone/pathing/movement/movements/MovementDescend.java index 3bba5f1d..227a6b89 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementDescend.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementDescend.java @@ -22,7 +22,7 @@ import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.Movement; import baritone.pathing.movement.MovementHelper; import baritone.pathing.movement.MovementState; -import baritone.pathing.movement.MovementState.MovementStatus; +import baritone.pathing.movement.MovementStatus; import baritone.utils.BlockStateInterface; import baritone.utils.InputOverrideHandler; import baritone.utils.pathing.BetterBlockPos; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java b/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java index 58027a8d..b8ef01c3 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java @@ -17,10 +17,7 @@ package baritone.pathing.movement.movements; -import baritone.pathing.movement.CalculationContext; -import baritone.pathing.movement.Movement; -import baritone.pathing.movement.MovementHelper; -import baritone.pathing.movement.MovementState; +import baritone.pathing.movement.*; import baritone.utils.BlockStateInterface; import baritone.utils.InputOverrideHandler; import baritone.utils.pathing.BetterBlockPos; @@ -140,12 +137,12 @@ public class MovementDiagonal extends Movement { @Override public MovementState updateState(MovementState state) { super.updateState(state); - if (state.getStatus() != MovementState.MovementStatus.RUNNING) { + if (state.getStatus() != MovementStatus.RUNNING) { return state; } if (playerFeet().equals(dest)) { - state.setStatus(MovementState.MovementStatus.SUCCESS); + state.setStatus(MovementStatus.SUCCESS); return state; } if (!BlockStateInterface.isLiquid(playerFeet())) { diff --git a/src/main/java/baritone/pathing/movement/movements/MovementDownward.java b/src/main/java/baritone/pathing/movement/movements/MovementDownward.java index 148dc3b3..d808d993 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementDownward.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementDownward.java @@ -17,10 +17,7 @@ package baritone.pathing.movement.movements; -import baritone.pathing.movement.CalculationContext; -import baritone.pathing.movement.Movement; -import baritone.pathing.movement.MovementHelper; -import baritone.pathing.movement.MovementState; +import baritone.pathing.movement.*; import baritone.utils.BlockStateInterface; import baritone.utils.pathing.BetterBlockPos; import net.minecraft.block.Block; @@ -64,12 +61,12 @@ public class MovementDownward extends Movement { @Override public MovementState updateState(MovementState state) { super.updateState(state); - if (state.getStatus() != MovementState.MovementStatus.RUNNING) { + if (state.getStatus() != MovementStatus.RUNNING) { return state; } if (playerFeet().equals(dest)) { - return state.setStatus(MovementState.MovementStatus.SUCCESS); + return state.setStatus(MovementStatus.SUCCESS); } double diffX = player().posX - (dest.getX() + 0.5); double diffZ = player().posZ - (dest.getZ() + 0.5); diff --git a/src/main/java/baritone/pathing/movement/movements/MovementFall.java b/src/main/java/baritone/pathing/movement/movements/MovementFall.java index f901c61f..3ce2848f 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementFall.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementFall.java @@ -23,7 +23,7 @@ import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.Movement; import baritone.pathing.movement.MovementHelper; import baritone.pathing.movement.MovementState; -import baritone.pathing.movement.MovementState.MovementStatus; +import baritone.pathing.movement.MovementStatus; import baritone.pathing.movement.MovementState.MovementTarget; import baritone.utils.BlockStateInterface; import baritone.utils.InputOverrideHandler; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java index e128bb3c..7ecb4c04 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java @@ -20,10 +20,7 @@ package baritone.pathing.movement.movements; import baritone.Baritone; import baritone.api.utils.Rotation; import baritone.behavior.LookBehaviorUtils; -import baritone.pathing.movement.CalculationContext; -import baritone.pathing.movement.Movement; -import baritone.pathing.movement.MovementHelper; -import baritone.pathing.movement.MovementState; +import baritone.pathing.movement.*; import baritone.utils.*; import baritone.utils.pathing.BetterBlockPos; import baritone.utils.pathing.MutableMoveResult; @@ -168,13 +165,13 @@ public class MovementParkour extends Movement { // once this movement is instantiated, the state is default to PREPPING // but once it's ticked for the first time it changes to RUNNING // since we don't really know anything about momentum, it suffices to say Parkour can only be canceled on the 0th tick - return state.getStatus() != MovementState.MovementStatus.RUNNING; + return state.getStatus() != MovementStatus.RUNNING; } @Override public MovementState updateState(MovementState state) { super.updateState(state); - if (state.getStatus() != MovementState.MovementStatus.RUNNING) { + if (state.getStatus() != MovementStatus.RUNNING) { return state; } if (dist >= 4) { @@ -186,10 +183,10 @@ public class MovementParkour extends Movement { if (d == Blocks.VINE || d == Blocks.LADDER) { // it physically hurt me to add support for parkour jumping onto a vine // but i did it anyway - return state.setStatus(MovementState.MovementStatus.SUCCESS); + return state.setStatus(MovementStatus.SUCCESS); } if (player().posY - playerFeet().getY() < 0.094) { // lilypads - state.setStatus(MovementState.MovementStatus.SUCCESS); + state.setStatus(MovementStatus.SUCCESS); } } else if (!playerFeet().equals(src)) { if (playerFeet().equals(src.offset(direction)) || player().posY - playerFeet().getY() > 0.0001) { @@ -203,7 +200,7 @@ public class MovementParkour extends Movement { } if (MovementHelper.canPlaceAgainst(against1)) { if (!MovementHelper.throwaway(true)) {//get ready to place a throwaway block - return state.setStatus(MovementState.MovementStatus.UNREACHABLE); + return state.setStatus(MovementStatus.UNREACHABLE); } double faceX = (dest.getX() + against1.getX() + 1.0D) * 0.5D; double faceY = (dest.getY() + against1.getY()) * 0.5D; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java index be524185..7680fb43 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java @@ -18,10 +18,7 @@ package baritone.pathing.movement.movements; import baritone.api.utils.Rotation; -import baritone.pathing.movement.CalculationContext; -import baritone.pathing.movement.Movement; -import baritone.pathing.movement.MovementHelper; -import baritone.pathing.movement.MovementState; +import baritone.pathing.movement.*; import baritone.utils.BlockStateInterface; import baritone.utils.InputOverrideHandler; import baritone.utils.Utils; @@ -148,7 +145,7 @@ public class MovementPillar extends Movement { @Override public MovementState updateState(MovementState state) { super.updateState(state); - if (state.getStatus() != MovementState.MovementStatus.RUNNING) { + if (state.getStatus() != MovementStatus.RUNNING) { return state; } @@ -161,7 +158,7 @@ public class MovementPillar extends Movement { state.setInput(InputOverrideHandler.Input.MOVE_FORWARD, true); } if (playerFeet().equals(dest)) { - return state.setStatus(MovementState.MovementStatus.SUCCESS); + return state.setStatus(MovementStatus.SUCCESS); } return state; } @@ -178,11 +175,11 @@ public class MovementPillar extends Movement { BlockPos against = vine ? getAgainst(src) : src.offset(fromDown.getValue(BlockLadder.FACING).getOpposite()); if (against == null) { logDebug("Unable to climb vines"); - return state.setStatus(MovementState.MovementStatus.UNREACHABLE); + return state.setStatus(MovementStatus.UNREACHABLE); } if (playerFeet().equals(against.up()) || playerFeet().equals(dest)) { - return state.setStatus(MovementState.MovementStatus.SUCCESS); + return state.setStatus(MovementStatus.SUCCESS); } if (MovementHelper.isBottomSlab(src.down())) { state.setInput(InputOverrideHandler.Input.JUMP, true); @@ -198,7 +195,7 @@ public class MovementPillar extends Movement { } else { // Get ready to place a throwaway block if (!MovementHelper.throwaway(true)) { - return state.setStatus(MovementState.MovementStatus.UNREACHABLE); + return state.setStatus(MovementStatus.UNREACHABLE); } numTicks++; @@ -233,7 +230,7 @@ public class MovementPillar extends Movement { // 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.setStatus(MovementStatus.SUCCESS); } return state; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java index 7c4ba237..00000ac4 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java @@ -20,10 +20,7 @@ package baritone.pathing.movement.movements; import baritone.Baritone; import baritone.api.utils.Rotation; import baritone.behavior.LookBehaviorUtils; -import baritone.pathing.movement.CalculationContext; -import baritone.pathing.movement.Movement; -import baritone.pathing.movement.MovementHelper; -import baritone.pathing.movement.MovementState; +import baritone.pathing.movement.*; import baritone.utils.BlockStateInterface; import baritone.utils.InputOverrideHandler; import baritone.utils.Utils; @@ -144,13 +141,13 @@ public class MovementTraverse extends Movement { @Override public MovementState updateState(MovementState state) { super.updateState(state); - if (state.getStatus() != MovementState.MovementStatus.RUNNING) { + if (state.getStatus() != MovementStatus.RUNNING) { // if the setting is enabled if (!Baritone.settings().walkWhileBreaking.get()) { return state; } // and if we're prepping (aka mining the block in front) - if (state.getStatus() != MovementState.MovementStatus.PREPPING) { + if (state.getStatus() != MovementStatus.PREPPING) { return state; } // and if it's fine to walk into the blocks in front @@ -225,7 +222,7 @@ public class MovementTraverse extends Movement { if (isTheBridgeBlockThere) { if (playerFeet().equals(dest)) { - return state.setStatus(MovementState.MovementStatus.SUCCESS); + return state.setStatus(MovementStatus.SUCCESS); } if (wasTheBridgeBlockAlwaysThere && !BlockStateInterface.isLiquid(playerFeet())) { state.setInput(InputOverrideHandler.Input.SPRINT, true); @@ -248,7 +245,7 @@ public class MovementTraverse extends Movement { if (MovementHelper.canPlaceAgainst(against1)) { if (!MovementHelper.throwaway(true)) { // get ready to place a throwaway block logDebug("bb pls get me some blocks. dirt or cobble"); - return state.setStatus(MovementState.MovementStatus.UNREACHABLE); + return state.setStatus(MovementStatus.UNREACHABLE); } if (!Baritone.settings().assumeSafeWalk.get()) { state.setInput(InputOverrideHandler.Input.SNEAK, true); @@ -287,7 +284,7 @@ public class MovementTraverse extends Movement { // Out.log(from + " " + to + " " + faceX + "," + faceY + "," + faceZ + " " + whereAmI); if (!MovementHelper.throwaway(true)) {// get ready to place a throwaway block logDebug("bb pls get me some blocks. dirt or cobble"); - return state.setStatus(MovementState.MovementStatus.UNREACHABLE); + 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; @@ -324,7 +321,7 @@ public class MovementTraverse extends Movement { // if we're in the process of breaking blocks before walking forwards // or if this isn't a sneak place (the block is already there) // then it's safe to cancel this - return state.getStatus() != MovementState.MovementStatus.RUNNING || MovementHelper.canWalkOn(dest.down()); + return state.getStatus() != MovementStatus.RUNNING || MovementHelper.canWalkOn(dest.down()); } @Override diff --git a/src/main/java/baritone/pathing/path/CutoffPath.java b/src/main/java/baritone/pathing/path/CutoffPath.java index e517452e..916b6c52 100644 --- a/src/main/java/baritone/pathing/path/CutoffPath.java +++ b/src/main/java/baritone/pathing/path/CutoffPath.java @@ -18,7 +18,7 @@ package baritone.pathing.path; import baritone.api.pathing.goals.Goal; -import baritone.pathing.movement.Movement; +import baritone.pathing.movement.IMovement; import baritone.utils.pathing.BetterBlockPos; import java.util.Collections; @@ -28,7 +28,7 @@ public class CutoffPath implements IPath { private final List path; - private final List movements; + private final List movements; private final int numNodes; @@ -47,7 +47,7 @@ public class CutoffPath implements IPath { } @Override - public List movements() { + public List movements() { return Collections.unmodifiableList(movements); } diff --git a/src/main/java/baritone/pathing/path/IPath.java b/src/main/java/baritone/pathing/path/IPath.java index 5741c440..afc1bce3 100644 --- a/src/main/java/baritone/pathing/path/IPath.java +++ b/src/main/java/baritone/pathing/path/IPath.java @@ -19,7 +19,7 @@ package baritone.pathing.path; import baritone.api.BaritoneAPI; import baritone.api.pathing.goals.Goal; -import baritone.pathing.movement.Movement; +import baritone.pathing.movement.IMovement; import baritone.utils.Utils; import baritone.utils.pathing.BetterBlockPos; import net.minecraft.client.Minecraft; @@ -40,7 +40,7 @@ public interface IPath { * 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. @@ -86,7 +86,7 @@ public interface IPath { /** * Where does this path start */ - default BlockPos getSrc() { + default BetterBlockPos getSrc() { return positions().get(0); } @@ -102,7 +102,7 @@ public interface IPath { double sum = 0; //this is fast because we aren't requesting recalculation, it's just cached for (int i = pathPosition; i < movements().size(); i++) { - sum += movements().get(i).getCost(null); + sum += movements().get(i).getCost(); } return sum; } diff --git a/src/main/java/baritone/pathing/path/PathExecutor.java b/src/main/java/baritone/pathing/path/PathExecutor.java index 4afd8b76..a60b4a29 100644 --- a/src/main/java/baritone/pathing/path/PathExecutor.java +++ b/src/main/java/baritone/pathing/path/PathExecutor.java @@ -21,10 +21,7 @@ import baritone.Baritone; import baritone.api.event.events.TickEvent; import baritone.api.pathing.movement.ActionCosts; import baritone.pathing.calc.AbstractNodeCostSearch; -import baritone.pathing.movement.CalculationContext; -import baritone.pathing.movement.Movement; -import baritone.pathing.movement.MovementHelper; -import baritone.pathing.movement.MovementState; +import baritone.pathing.movement.*; import baritone.pathing.movement.movements.*; import baritone.utils.*; import baritone.utils.pathing.BetterBlockPos; @@ -34,7 +31,7 @@ import net.minecraft.util.math.BlockPos; import java.util.*; -import static baritone.pathing.movement.MovementState.MovementStatus.*; +import static baritone.pathing.movement.MovementStatus.*; /** * Behavior to execute a precomputed path. Does not (yet) deal with path segmentation or stitching @@ -182,13 +179,11 @@ public class PathExecutor implements Helper { if (i < 0 || i >= path.movements().size()) { continue; } - Movement m = path.movements().get(i); + IMovement m = path.movements().get(i); HashSet prevBreak = new HashSet<>(m.toBreak()); HashSet prevPlace = new HashSet<>(m.toPlace()); HashSet prevWalkInto = new HashSet<>(m.toWalkInto()); - m.toBreakCached = null; - m.toPlaceCached = null; - m.toWalkIntoCached = null; + m.resetBlockCache(); if (!prevBreak.equals(new HashSet<>(m.toBreak()))) { recalcBP = true; } @@ -217,12 +212,12 @@ public class PathExecutor implements Helper { if (end - start > 0) { System.out.println("Recalculating break and place took " + (end - start) + "ms"); }*/ - Movement movement = path.movements().get(pathPosition); + IMovement movement = path.movements().get(pathPosition); boolean canCancel = movement.safeToCancel(); if (costEstimateIndex == null || costEstimateIndex != pathPosition) { costEstimateIndex = pathPosition; // do this only once, when the movement starts, and deliberately get the cost as cached when this path was calculated, not the cost as it is right now - currentMovementOriginalCostEstimate = movement.getCost(null); + currentMovementOriginalCostEstimate = movement.getCost(); for (int i = 1; i < Baritone.settings().costVerificationLookahead.get() && pathPosition + i < path.length() - 1; i++) { if (path.movements().get(pathPosition + i).calculateCostWithoutCaching() >= ActionCosts.COST_INF && canCancel) { logDebug("Something has changed in the world and a future movement has become impossible. Cancelling."); @@ -246,7 +241,7 @@ public class PathExecutor implements Helper { logDebug("Pausing since current best path is a backtrack"); return true; } - MovementState.MovementStatus movementStatus = movement.update(); + MovementStatus movementStatus = movement.update(); if (movementStatus == UNREACHABLE || movementStatus == FAILED) { logDebug("Movement returns status " + movementStatus); cancel(); @@ -346,7 +341,7 @@ public class PathExecutor implements Helper { Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(InputOverrideHandler.Input.SPRINT,false); // however, descend doesn't request sprinting, beceause it doesn't know the context of what movement comes after it - Movement current = path.movements().get(pathPosition); + IMovement current = path.movements().get(pathPosition); if (current instanceof MovementDescend && pathPosition < path.length() - 2) { // (dest - src) + dest is offset 1 more in the same direction @@ -361,7 +356,7 @@ public class PathExecutor implements Helper { } } - Movement next = path.movements().get(pathPosition + 1); + IMovement next = path.movements().get(pathPosition + 1); if (next instanceof MovementAscend && current.getDirection().up().equals(next.getDirection().down())) { // a descend then an ascend in the same direction if (!player().isSprinting()) { @@ -385,7 +380,7 @@ public class PathExecutor implements Helper { //logDebug("Turning off sprinting " + movement + " " + next + " " + movement.getDirection() + " " + next.getDirection().down() + " " + next.getDirection().down().equals(movement.getDirection())); } if (current instanceof MovementAscend && pathPosition != 0) { - Movement prev = path.movements().get(pathPosition - 1); + IMovement prev = path.movements().get(pathPosition - 1); if (prev instanceof MovementDescend && prev.getDirection().up().equals(current.getDirection().down())) { BlockPos center = current.getSrc().up(); if (player().posY >= center.getY()) { // playerFeet adds 0.1251 to account for soul sand @@ -400,7 +395,7 @@ public class PathExecutor implements Helper { player().setSprinting(false); } - private static boolean canSprintInto(Movement current, Movement next) { + private static boolean canSprintInto(IMovement current, IMovement next) { if (next instanceof MovementDescend) { if (next.getDirection().equals(current.getDirection())) { return true; diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index cca6e3a2..fd733d7f 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -467,10 +467,10 @@ public class ExampleBaritoneControl extends Behavior implements Helper { while (moves.contains(null)) { moves.remove(null); } - moves.sort(Comparator.comparingDouble(movement -> movement.getCost(new CalculationContext()))); + moves.sort(Comparator.comparingDouble(Movement::getCost)); for (Movement move : moves) { String[] parts = move.getClass().toString().split("\\."); - double cost = move.getCost(new CalculationContext()); + double cost = move.getCost(); String strCost = cost + ""; if (cost >= ActionCosts.COST_INF) { strCost = "IMPOSSIBLE"; From 1449edb8af81c9fbed3cbf5503480d41f14f738f Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 8 Oct 2018 19:59:25 -0500 Subject: [PATCH 03/12] Didn't need to do that --- .../pathing/movement/CalculationContext.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/baritone/pathing/movement/CalculationContext.java b/src/main/java/baritone/pathing/movement/CalculationContext.java index 464703ff..0b40539e 100644 --- a/src/main/java/baritone/pathing/movement/CalculationContext.java +++ b/src/main/java/baritone/pathing/movement/CalculationContext.java @@ -17,7 +17,7 @@ package baritone.pathing.movement; -import baritone.api.BaritoneAPI; +import baritone.Baritone; import baritone.api.pathing.movement.ActionCosts; import baritone.utils.Helper; import baritone.utils.ToolSet; @@ -51,20 +51,20 @@ public class CalculationContext implements Helper { public CalculationContext(ToolSet toolSet) { this.toolSet = toolSet; - this.hasThrowaway = BaritoneAPI.getSettings().allowPlace.get() && MovementHelper.throwaway(false); - this.hasWaterBucket = BaritoneAPI.getSettings().allowWaterBucketFall.get() && InventoryPlayer.isHotbar(player().inventory.getSlotFor(STACK_BUCKET_WATER)) && !world().provider.isNether(); - this.canSprint = BaritoneAPI.getSettings().allowSprint.get() && player().getFoodStats().getFoodLevel() > 6; - this.placeBlockCost = BaritoneAPI.getSettings().blockPlacementPenalty.get(); - this.allowBreak = BaritoneAPI.getSettings().allowBreak.get(); - this.maxFallHeightNoWater = BaritoneAPI.getSettings().maxFallHeightNoWater.get(); - this.maxFallHeightBucket = BaritoneAPI.getSettings().maxFallHeightBucket.get(); + this.hasThrowaway = Baritone.settings().allowPlace.get() && MovementHelper.throwaway(false); + this.hasWaterBucket = Baritone.settings().allowWaterBucketFall.get() && InventoryPlayer.isHotbar(player().inventory.getSlotFor(STACK_BUCKET_WATER)) && !world().provider.isNether(); + this.canSprint = Baritone.settings().allowSprint.get() && player().getFoodStats().getFoodLevel() > 6; + this.placeBlockCost = Baritone.settings().blockPlacementPenalty.get(); + this.allowBreak = Baritone.settings().allowBreak.get(); + this.maxFallHeightNoWater = Baritone.settings().maxFallHeightNoWater.get(); + this.maxFallHeightBucket = Baritone.settings().maxFallHeightBucket.get(); int depth = EnchantmentHelper.getDepthStriderModifier(player()); if (depth > 3) { depth = 3; } float mult = depth / 3.0F; this.waterWalkSpeed = ActionCosts.WALK_ONE_IN_WATER_COST * (1 - mult) + ActionCosts.WALK_ONE_BLOCK_COST * mult; - this.breakBlockAdditionalCost = BaritoneAPI.getSettings().blockBreakAdditionalPenalty.get(); + this.breakBlockAdditionalCost = Baritone.settings().blockBreakAdditionalPenalty.get(); // why cache these things here, why not let the movements just get directly from settings? // because if some movements are calculated one way and others are calculated another way, // then you get a wildly inconsistent path that isn't optimal for either scenario. From e23a9c976a6b1f4a618a4eac6da8442ef66739a8 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 8 Oct 2018 20:01:44 -0500 Subject: [PATCH 04/12] Bad code --- src/main/java/baritone/pathing/movement/Movement.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baritone/pathing/movement/Movement.java b/src/main/java/baritone/pathing/movement/Movement.java index b529873a..bb699d0e 100644 --- a/src/main/java/baritone/pathing/movement/Movement.java +++ b/src/main/java/baritone/pathing/movement/Movement.java @@ -63,7 +63,7 @@ public abstract class Movement implements IMovement, Helper, MovementHelper { public List toPlaceCached = null; public List toWalkIntoCached = null; - private boolean calculatedWhileLoaded; + private Boolean calculatedWhileLoaded; protected Movement(BetterBlockPos src, BetterBlockPos dest, BetterBlockPos[] toBreak, BetterBlockPos toPlace) { this.src = src; From 2b4512ee3f8ab8217c7ba81083d899ca92104b18 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 8 Oct 2018 20:37:52 -0500 Subject: [PATCH 05/12] Move to API --- .../api/behavior/IPathingBehavior.java | 29 +++++++++++++++++++ .../api}/pathing/calc/IPathFinder.java | 4 +-- .../api}/pathing/movement/IMovement.java | 4 +-- .../api}/pathing/movement/MovementStatus.java | 2 +- .../api}/pathing/path/CutoffPath.java | 6 ++-- .../api}/pathing/path/CutoffResult.java | 2 +- .../baritone/api}/pathing/path/IPath.java | 21 ++------------ .../api/pathing/path/IPathExecutor.java | 27 +++++++++++++++++ .../baritone/api/utils}/BetterBlockPos.java | 26 ++++++++++++++--- .../launch/mixins/MixinMinecraft.java | 1 - .../java/baritone/behavior/MineBehavior.java | 6 ++-- .../baritone/behavior/PathingBehavior.java | 18 ++++++------ .../pathing/calc/AStarPathFinder.java | 7 +++-- .../pathing/calc/AbstractNodeCostSearch.java | 26 ++++------------- src/main/java/baritone/pathing/calc/Path.java | 6 ++-- .../java/baritone/pathing/calc/PathNode.java | 3 +- .../baritone/pathing/movement/Movement.java | 4 ++- .../pathing/movement/MovementHelper.java | 2 +- .../pathing/movement/MovementState.java | 1 + .../java/baritone/pathing/movement/Moves.java | 2 +- .../movement/movements/MovementAscend.java | 8 +++-- .../movement/movements/MovementDescend.java | 4 +-- .../movement/movements/MovementDiagonal.java | 8 +++-- .../movement/movements/MovementDownward.java | 8 +++-- .../movement/movements/MovementFall.java | 4 +-- .../movement/movements/MovementParkour.java | 8 +++-- .../movement/movements/MovementPillar.java | 8 +++-- .../movement/movements/MovementTraverse.java | 8 +++-- .../baritone/pathing/path/PathExecutor.java | 28 ++++++++++++++---- .../utils/ExampleBaritoneControl.java | 1 - src/main/java/baritone/utils/Helper.java | 2 +- .../java/baritone/utils/PathRenderer.java | 4 +-- .../utils/pathing/BetterBlockPosTest.java | 1 + 33 files changed, 190 insertions(+), 99 deletions(-) rename src/{main/java/baritone => api/java/baritone/api}/pathing/calc/IPathFinder.java (96%) rename src/{main/java/baritone => api/java/baritone/api}/pathing/movement/IMovement.java (95%) rename src/{main/java/baritone => api/java/baritone/api}/pathing/movement/MovementStatus.java (95%) rename src/{main/java/baritone => api/java/baritone/api}/pathing/path/CutoffPath.java (93%) rename src/{main/java/baritone => api/java/baritone/api}/pathing/path/CutoffResult.java (97%) rename src/{main/java/baritone => api/java/baritone/api}/pathing/path/IPath.java (85%) create mode 100644 src/api/java/baritone/api/pathing/path/IPathExecutor.java rename src/{main/java/baritone/utils/pathing => api/java/baritone/api/utils}/BetterBlockPos.java (86%) diff --git a/src/api/java/baritone/api/behavior/IPathingBehavior.java b/src/api/java/baritone/api/behavior/IPathingBehavior.java index e21d999d..5960688a 100644 --- a/src/api/java/baritone/api/behavior/IPathingBehavior.java +++ b/src/api/java/baritone/api/behavior/IPathingBehavior.java @@ -17,7 +17,10 @@ package baritone.api.behavior; +import baritone.api.pathing.calc.IPathFinder; import baritone.api.pathing.goals.Goal; +import baritone.api.pathing.path.IPath; +import baritone.api.pathing.path.IPathExecutor; import java.util.Optional; @@ -65,4 +68,30 @@ public interface IPathingBehavior extends IBehavior { * Cancels the pathing behavior or the current path calculation. */ void cancel(); + + /** + * Returns the current path, from the current path executor, if there is one. + * + * @return The current path + */ + default Optional getPath() { + return Optional.ofNullable(getCurrent()).map(IPathExecutor::getPath); + } + + /** + * @return The current path finder being executed + */ + Optional getCurrentPathSearch(); + + /** + * @return The current path executor + */ + IPathExecutor getCurrent(); + + /** + * Returns the next path executor, created when planning ahead. + * + * @return The next path executor + */ + IPathExecutor getNext(); } diff --git a/src/main/java/baritone/pathing/calc/IPathFinder.java b/src/api/java/baritone/api/pathing/calc/IPathFinder.java similarity index 96% rename from src/main/java/baritone/pathing/calc/IPathFinder.java rename to src/api/java/baritone/api/pathing/calc/IPathFinder.java index e6f3be06..31fed89d 100644 --- a/src/main/java/baritone/pathing/calc/IPathFinder.java +++ b/src/api/java/baritone/api/pathing/calc/IPathFinder.java @@ -15,10 +15,10 @@ * along with Baritone. If not, see . */ -package baritone.pathing.calc; +package baritone.api.pathing.calc; import baritone.api.pathing.goals.Goal; -import baritone.pathing.path.IPath; +import baritone.api.pathing.path.IPath; import java.util.Optional; diff --git a/src/main/java/baritone/pathing/movement/IMovement.java b/src/api/java/baritone/api/pathing/movement/IMovement.java similarity index 95% rename from src/main/java/baritone/pathing/movement/IMovement.java rename to src/api/java/baritone/api/pathing/movement/IMovement.java index 83b65019..7b3eca5f 100644 --- a/src/main/java/baritone/pathing/movement/IMovement.java +++ b/src/api/java/baritone/api/pathing/movement/IMovement.java @@ -15,9 +15,9 @@ * along with Baritone. If not, see . */ -package baritone.pathing.movement; +package baritone.api.pathing.movement; -import baritone.utils.pathing.BetterBlockPos; +import baritone.api.utils.BetterBlockPos; import net.minecraft.util.math.BlockPos; import java.util.List; diff --git a/src/main/java/baritone/pathing/movement/MovementStatus.java b/src/api/java/baritone/api/pathing/movement/MovementStatus.java similarity index 95% rename from src/main/java/baritone/pathing/movement/MovementStatus.java rename to src/api/java/baritone/api/pathing/movement/MovementStatus.java index 4b4b92c3..6b5215ad 100644 --- a/src/main/java/baritone/pathing/movement/MovementStatus.java +++ b/src/api/java/baritone/api/pathing/movement/MovementStatus.java @@ -15,7 +15,7 @@ * along with Baritone. If not, see . */ -package baritone.pathing.movement; +package baritone.api.pathing.movement; /** * @author Brady diff --git a/src/main/java/baritone/pathing/path/CutoffPath.java b/src/api/java/baritone/api/pathing/path/CutoffPath.java similarity index 93% rename from src/main/java/baritone/pathing/path/CutoffPath.java rename to src/api/java/baritone/api/pathing/path/CutoffPath.java index 916b6c52..a702b7fd 100644 --- a/src/main/java/baritone/pathing/path/CutoffPath.java +++ b/src/api/java/baritone/api/pathing/path/CutoffPath.java @@ -15,11 +15,11 @@ * along with Baritone. If not, see . */ -package baritone.pathing.path; +package baritone.api.pathing.path; import baritone.api.pathing.goals.Goal; -import baritone.pathing.movement.IMovement; -import baritone.utils.pathing.BetterBlockPos; +import baritone.api.pathing.movement.IMovement; +import baritone.api.utils.BetterBlockPos; import java.util.Collections; import java.util.List; diff --git a/src/main/java/baritone/pathing/path/CutoffResult.java b/src/api/java/baritone/api/pathing/path/CutoffResult.java similarity index 97% rename from src/main/java/baritone/pathing/path/CutoffResult.java rename to src/api/java/baritone/api/pathing/path/CutoffResult.java index d1c03c1e..eeaa4ad1 100644 --- a/src/main/java/baritone/pathing/path/CutoffResult.java +++ b/src/api/java/baritone/api/pathing/path/CutoffResult.java @@ -15,7 +15,7 @@ * along with Baritone. If not, see . */ -package baritone.pathing.path; +package baritone.api.pathing.path; /** * @author Brady diff --git a/src/main/java/baritone/pathing/path/IPath.java b/src/api/java/baritone/api/pathing/path/IPath.java similarity index 85% rename from src/main/java/baritone/pathing/path/IPath.java rename to src/api/java/baritone/api/pathing/path/IPath.java index afc1bce3..2d4b08e3 100644 --- a/src/main/java/baritone/pathing/path/IPath.java +++ b/src/api/java/baritone/api/pathing/path/IPath.java @@ -15,15 +15,13 @@ * along with Baritone. If not, see . */ -package baritone.pathing.path; +package baritone.api.pathing.path; import baritone.api.BaritoneAPI; import baritone.api.pathing.goals.Goal; -import baritone.pathing.movement.IMovement; -import baritone.utils.Utils; -import baritone.utils.pathing.BetterBlockPos; +import baritone.api.pathing.movement.IMovement; +import baritone.api.utils.BetterBlockPos; import net.minecraft.client.Minecraft; -import net.minecraft.util.Tuple; import net.minecraft.util.math.BlockPos; import net.minecraft.world.chunk.EmptyChunk; @@ -70,19 +68,6 @@ public interface IPath { */ Goal getGoal(); - default Tuple closestPathPos() { - double best = -1; - BlockPos bestPos = null; - for (BlockPos pos : positions()) { - double dist = Utils.playerDistanceToCenter(pos); - if (dist < best || best == -1) { - best = dist; - bestPos = pos; - } - } - return new Tuple<>(best, bestPos); - } - /** * Where does this path start */ diff --git a/src/api/java/baritone/api/pathing/path/IPathExecutor.java b/src/api/java/baritone/api/pathing/path/IPathExecutor.java new file mode 100644 index 00000000..bf701224 --- /dev/null +++ b/src/api/java/baritone/api/pathing/path/IPathExecutor.java @@ -0,0 +1,27 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.api.pathing.path; + +/** + * @author Brady + * @since 10/8/2018 + */ +public interface IPathExecutor { + + IPath getPath(); +} diff --git a/src/main/java/baritone/utils/pathing/BetterBlockPos.java b/src/api/java/baritone/api/utils/BetterBlockPos.java similarity index 86% rename from src/main/java/baritone/utils/pathing/BetterBlockPos.java rename to src/api/java/baritone/api/utils/BetterBlockPos.java index e81319db..a1a3cb32 100644 --- a/src/main/java/baritone/utils/pathing/BetterBlockPos.java +++ b/src/api/java/baritone/api/utils/BetterBlockPos.java @@ -15,9 +15,8 @@ * along with Baritone. If not, see . */ -package baritone.utils.pathing; +package baritone.api.utils; -import baritone.pathing.calc.AbstractNodeCostSearch; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; @@ -54,11 +53,30 @@ public final class BetterBlockPos extends BlockPos { @Override public int hashCode() { - return (int) AbstractNodeCostSearch.posHash(x, y, z); + return (int) longHash(x, y, z); } public static long longHash(BetterBlockPos pos) { - return AbstractNodeCostSearch.posHash(pos.x, pos.y, pos.z); + return longHash(pos.x, pos.y, pos.z); + } + + public static long longHash(int x, int y, int z) { + /* + * This is the hashcode implementation of Vec3i (the superclass of the class which I shall not name) + * + * public int hashCode() { + * return (this.getY() + this.getZ() * 31) * 31 + this.getX(); + * } + * + * That is terrible and has tons of collisions and makes the HashMap terribly inefficient. + * + * That's why we grab out the X, Y, Z and calculate our own hashcode + */ + long hash = 3241; + hash = 3457689L * hash + x; + hash = 8734625L * hash + y; + hash = 2873465L * hash + z; + return hash; } @Override diff --git a/src/launch/java/baritone/launch/mixins/MixinMinecraft.java b/src/launch/java/baritone/launch/mixins/MixinMinecraft.java index 2f0d1ab4..5d2ed420 100644 --- a/src/launch/java/baritone/launch/mixins/MixinMinecraft.java +++ b/src/launch/java/baritone/launch/mixins/MixinMinecraft.java @@ -40,7 +40,6 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.LocalCapture; /** diff --git a/src/main/java/baritone/behavior/MineBehavior.java b/src/main/java/baritone/behavior/MineBehavior.java index 0f1a6b34..205c677d 100644 --- a/src/main/java/baritone/behavior/MineBehavior.java +++ b/src/main/java/baritone/behavior/MineBehavior.java @@ -22,13 +22,13 @@ import baritone.api.behavior.IMineBehavior; import baritone.api.event.events.PathEvent; import baritone.api.event.events.TickEvent; import baritone.api.pathing.goals.Goal; +import baritone.api.pathing.goals.GoalBlock; +import baritone.api.pathing.goals.GoalComposite; +import baritone.api.pathing.goals.GoalTwoBlocks; import baritone.cache.CachedChunk; import baritone.cache.ChunkPacker; import baritone.cache.WorldProvider; import baritone.cache.WorldScanner; -import baritone.api.pathing.goals.GoalBlock; -import baritone.api.pathing.goals.GoalComposite; -import baritone.api.pathing.goals.GoalTwoBlocks; import baritone.utils.BlockStateInterface; import baritone.utils.Helper; import net.minecraft.block.Block; diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 16c88b92..45163852 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -25,19 +25,19 @@ import baritone.api.event.events.RenderEvent; import baritone.api.event.events.TickEvent; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.GoalXZ; +import baritone.api.pathing.path.CutoffResult; +import baritone.api.pathing.path.IPath; +import baritone.api.utils.BetterBlockPos; import baritone.api.utils.interfaces.IGoalRenderPos; import baritone.pathing.calc.AStarPathFinder; import baritone.pathing.calc.AbstractNodeCostSearch; -import baritone.pathing.calc.IPathFinder; +import baritone.api.pathing.calc.IPathFinder; import baritone.pathing.movement.MovementHelper; -import baritone.pathing.path.CutoffResult; -import baritone.pathing.path.IPath; import baritone.pathing.path.PathExecutor; import baritone.utils.BlockBreakHelper; import baritone.utils.BlockStateInterface; import baritone.utils.Helper; import baritone.utils.PathRenderer; -import baritone.utils.pathing.BetterBlockPos; import net.minecraft.init.Blocks; import net.minecraft.util.math.BlockPos; import net.minecraft.world.chunk.EmptyChunk; @@ -191,19 +191,19 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, return goal; } + @Override public PathExecutor getCurrent() { return current; } + @Override public PathExecutor getNext() { return next; } - // TODO: Expose this method in the API? - // In order to do so, we'd need to move over IPath which has a whole lot of references to other - // things that may not need to be exposed necessarily, so we'll need to figure that out. - public Optional getPath() { - return Optional.ofNullable(current).map(PathExecutor::getPath); + @Override + public Optional getCurrentPathSearch() { + return Optional.ofNullable(AbstractNodeCostSearch.currentlyRunning()); } @Override diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index 583428e9..3afafda9 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -20,10 +20,11 @@ package baritone.pathing.calc; import baritone.Baritone; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.movement.ActionCosts; +import baritone.api.pathing.path.IPath; +import baritone.api.utils.BetterBlockPos; import baritone.pathing.calc.openset.BinaryHeapOpenSet; import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.Moves; -import baritone.pathing.path.IPath; import baritone.utils.BlockStateInterface; import baritone.utils.Helper; import baritone.utils.pathing.MutableMoveResult; @@ -47,7 +48,7 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel @Override protected Optional calculate0(long timeout) { - startNode = getNodeAtPosition(startX, startY, startZ, posHash(startX, startY, startZ)); + startNode = getNodeAtPosition(startX, startY, startZ, BetterBlockPos.longHash(startX, startY, startZ)); startNode.cost = 0; startNode.combinedCost = startNode.estimatedCostToGoal; BinaryHeapOpenSet openSet = new BinaryHeapOpenSet(); @@ -122,7 +123,7 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel if (actionCost <= 0) { throw new IllegalStateException(moves + " calculated implausible cost " + actionCost); } - long hashCode = posHash(res.x, res.y, res.z); + long hashCode = BetterBlockPos.longHash(res.x, res.y, res.z); if (favoring && favored.contains(hashCode)) { // see issue #18 actionCost *= favorCoeff; diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index 68a523fa..adac5e28 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -18,8 +18,9 @@ package baritone.pathing.calc; import baritone.Baritone; +import baritone.api.pathing.calc.IPathFinder; import baritone.api.pathing.goals.Goal; -import baritone.pathing.path.IPath; +import baritone.api.pathing.path.IPath; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import java.util.Optional; @@ -140,25 +141,6 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { return node; } - public static long posHash(int x, int y, int z) { - /* - * This is the hashcode implementation of Vec3i (the superclass of the class which I shall not name) - * - * public int hashCode() { - * return (this.getY() + this.getZ() * 31) * 31 + this.getX(); - * } - * - * That is terrible and has tons of collisions and makes the HashMap terribly inefficient. - * - * That's why we grab out the X, Y, Z and calculate our own hashcode - */ - long hash = 3241; - hash = 3457689L * hash + x; - hash = 8734625L * hash + y; - hash = 2873465L * hash + z; - return hash; - } - public static void forceCancel() { currentlyRunning = null; } @@ -225,4 +207,8 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { public static Optional getCurrentlyRunning() { return Optional.ofNullable(currentlyRunning); } + + public static AbstractNodeCostSearch currentlyRunning() { + return currentlyRunning; + } } diff --git a/src/main/java/baritone/pathing/calc/Path.java b/src/main/java/baritone/pathing/calc/Path.java index 6606cfba..ddd51dc2 100644 --- a/src/main/java/baritone/pathing/calc/Path.java +++ b/src/main/java/baritone/pathing/calc/Path.java @@ -18,11 +18,11 @@ package baritone.pathing.calc; import baritone.api.pathing.goals.Goal; -import baritone.pathing.movement.IMovement; +import baritone.api.pathing.movement.IMovement; +import baritone.api.pathing.path.IPath; +import baritone.api.utils.BetterBlockPos; import baritone.pathing.movement.Movement; import baritone.pathing.movement.Moves; -import baritone.pathing.path.IPath; -import baritone.utils.pathing.BetterBlockPos; import net.minecraft.util.math.BlockPos; import java.util.ArrayList; diff --git a/src/main/java/baritone/pathing/calc/PathNode.java b/src/main/java/baritone/pathing/calc/PathNode.java index 8e42d564..8f316132 100644 --- a/src/main/java/baritone/pathing/calc/PathNode.java +++ b/src/main/java/baritone/pathing/calc/PathNode.java @@ -19,6 +19,7 @@ package baritone.pathing.calc; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.movement.ActionCosts; +import baritone.api.utils.BetterBlockPos; /** * A node in the path, containing the cost and steps to get to it. @@ -85,7 +86,7 @@ public final class PathNode { */ @Override public int hashCode() { - return (int) AbstractNodeCostSearch.posHash(x, y, z); + return (int) BetterBlockPos.longHash(x, y, z); } @Override diff --git a/src/main/java/baritone/pathing/movement/Movement.java b/src/main/java/baritone/pathing/movement/Movement.java index bb699d0e..cffa28ee 100644 --- a/src/main/java/baritone/pathing/movement/Movement.java +++ b/src/main/java/baritone/pathing/movement/Movement.java @@ -18,11 +18,13 @@ package baritone.pathing.movement; import baritone.Baritone; +import baritone.api.pathing.movement.IMovement; +import baritone.api.pathing.movement.MovementStatus; +import baritone.api.utils.BetterBlockPos; import baritone.api.utils.Rotation; import baritone.behavior.LookBehavior; import baritone.behavior.LookBehaviorUtils; import baritone.utils.*; -import baritone.utils.pathing.BetterBlockPos; import net.minecraft.block.BlockLiquid; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; diff --git a/src/main/java/baritone/pathing/movement/MovementHelper.java b/src/main/java/baritone/pathing/movement/MovementHelper.java index 28b14e9e..5b5be77e 100644 --- a/src/main/java/baritone/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/pathing/movement/MovementHelper.java @@ -19,11 +19,11 @@ package baritone.pathing.movement; import baritone.Baritone; import baritone.api.pathing.movement.ActionCosts; +import baritone.api.utils.BetterBlockPos; import baritone.api.utils.Rotation; import baritone.behavior.LookBehaviorUtils; import baritone.pathing.movement.MovementState.MovementTarget; import baritone.utils.*; -import baritone.utils.pathing.BetterBlockPos; import net.minecraft.block.*; import net.minecraft.block.properties.PropertyBool; import net.minecraft.block.state.IBlockState; diff --git a/src/main/java/baritone/pathing/movement/MovementState.java b/src/main/java/baritone/pathing/movement/MovementState.java index acf223d4..db99ce5d 100644 --- a/src/main/java/baritone/pathing/movement/MovementState.java +++ b/src/main/java/baritone/pathing/movement/MovementState.java @@ -17,6 +17,7 @@ package baritone.pathing.movement; +import baritone.api.pathing.movement.MovementStatus; import baritone.api.utils.Rotation; import baritone.utils.InputOverrideHandler.Input; import net.minecraft.util.math.Vec3d; diff --git a/src/main/java/baritone/pathing/movement/Moves.java b/src/main/java/baritone/pathing/movement/Moves.java index c5b1069d..3d53ff5b 100644 --- a/src/main/java/baritone/pathing/movement/Moves.java +++ b/src/main/java/baritone/pathing/movement/Moves.java @@ -17,8 +17,8 @@ package baritone.pathing.movement; +import baritone.api.utils.BetterBlockPos; import baritone.pathing.movement.movements.*; -import baritone.utils.pathing.BetterBlockPos; import baritone.utils.pathing.MutableMoveResult; import net.minecraft.util.EnumFacing; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java index 9ad42d99..6989179b 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java @@ -18,12 +18,16 @@ package baritone.pathing.movement.movements; import baritone.Baritone; +import baritone.api.pathing.movement.MovementStatus; +import baritone.api.utils.BetterBlockPos; import baritone.behavior.LookBehaviorUtils; -import baritone.pathing.movement.*; +import baritone.pathing.movement.CalculationContext; +import baritone.pathing.movement.Movement; +import baritone.pathing.movement.MovementHelper; +import baritone.pathing.movement.MovementState; import baritone.utils.BlockStateInterface; import baritone.utils.InputOverrideHandler; import baritone.utils.Utils; -import baritone.utils.pathing.BetterBlockPos; import net.minecraft.block.BlockFalling; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementDescend.java b/src/main/java/baritone/pathing/movement/movements/MovementDescend.java index 227a6b89..ba3deb7d 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementDescend.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementDescend.java @@ -18,14 +18,14 @@ package baritone.pathing.movement.movements; import baritone.Baritone; +import baritone.api.pathing.movement.MovementStatus; +import baritone.api.utils.BetterBlockPos; import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.Movement; import baritone.pathing.movement.MovementHelper; import baritone.pathing.movement.MovementState; -import baritone.pathing.movement.MovementStatus; import baritone.utils.BlockStateInterface; import baritone.utils.InputOverrideHandler; -import baritone.utils.pathing.BetterBlockPos; import baritone.utils.pathing.MutableMoveResult; import net.minecraft.block.Block; import net.minecraft.block.BlockFalling; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java b/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java index b8ef01c3..8ff681c6 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java @@ -17,10 +17,14 @@ package baritone.pathing.movement.movements; -import baritone.pathing.movement.*; +import baritone.api.pathing.movement.MovementStatus; +import baritone.api.utils.BetterBlockPos; +import baritone.pathing.movement.CalculationContext; +import baritone.pathing.movement.Movement; +import baritone.pathing.movement.MovementHelper; +import baritone.pathing.movement.MovementState; import baritone.utils.BlockStateInterface; import baritone.utils.InputOverrideHandler; -import baritone.utils.pathing.BetterBlockPos; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementDownward.java b/src/main/java/baritone/pathing/movement/movements/MovementDownward.java index d808d993..0ac4f2b0 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementDownward.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementDownward.java @@ -17,9 +17,13 @@ package baritone.pathing.movement.movements; -import baritone.pathing.movement.*; +import baritone.api.pathing.movement.MovementStatus; +import baritone.api.utils.BetterBlockPos; +import baritone.pathing.movement.CalculationContext; +import baritone.pathing.movement.Movement; +import baritone.pathing.movement.MovementHelper; +import baritone.pathing.movement.MovementState; import baritone.utils.BlockStateInterface; -import baritone.utils.pathing.BetterBlockPos; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementFall.java b/src/main/java/baritone/pathing/movement/movements/MovementFall.java index 3ce2848f..e09eb579 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementFall.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementFall.java @@ -18,18 +18,18 @@ package baritone.pathing.movement.movements; import baritone.Baritone; +import baritone.api.pathing.movement.MovementStatus; +import baritone.api.utils.BetterBlockPos; import baritone.api.utils.Rotation; import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.Movement; import baritone.pathing.movement.MovementHelper; import baritone.pathing.movement.MovementState; -import baritone.pathing.movement.MovementStatus; import baritone.pathing.movement.MovementState.MovementTarget; import baritone.utils.BlockStateInterface; import baritone.utils.InputOverrideHandler; import baritone.utils.RayTraceUtils; import baritone.utils.Utils; -import baritone.utils.pathing.BetterBlockPos; import baritone.utils.pathing.MutableMoveResult; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.init.Items; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java index 7ecb4c04..94b7dfea 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java @@ -18,11 +18,15 @@ package baritone.pathing.movement.movements; import baritone.Baritone; +import baritone.api.pathing.movement.MovementStatus; +import baritone.api.utils.BetterBlockPos; import baritone.api.utils.Rotation; import baritone.behavior.LookBehaviorUtils; -import baritone.pathing.movement.*; +import baritone.pathing.movement.CalculationContext; +import baritone.pathing.movement.Movement; +import baritone.pathing.movement.MovementHelper; +import baritone.pathing.movement.MovementState; import baritone.utils.*; -import baritone.utils.pathing.BetterBlockPos; import baritone.utils.pathing.MutableMoveResult; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java index 7680fb43..60803a3c 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java @@ -17,12 +17,16 @@ package baritone.pathing.movement.movements; +import baritone.api.pathing.movement.MovementStatus; +import baritone.api.utils.BetterBlockPos; import baritone.api.utils.Rotation; -import baritone.pathing.movement.*; +import baritone.pathing.movement.CalculationContext; +import baritone.pathing.movement.Movement; +import baritone.pathing.movement.MovementHelper; +import baritone.pathing.movement.MovementState; import baritone.utils.BlockStateInterface; import baritone.utils.InputOverrideHandler; import baritone.utils.Utils; -import baritone.utils.pathing.BetterBlockPos; import net.minecraft.block.*; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; diff --git a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java index 00000ac4..f029619a 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java @@ -18,13 +18,17 @@ package baritone.pathing.movement.movements; import baritone.Baritone; +import baritone.api.pathing.movement.MovementStatus; +import baritone.api.utils.BetterBlockPos; import baritone.api.utils.Rotation; import baritone.behavior.LookBehaviorUtils; -import baritone.pathing.movement.*; +import baritone.pathing.movement.CalculationContext; +import baritone.pathing.movement.Movement; +import baritone.pathing.movement.MovementHelper; +import baritone.pathing.movement.MovementState; import baritone.utils.BlockStateInterface; import baritone.utils.InputOverrideHandler; import baritone.utils.Utils; -import baritone.utils.pathing.BetterBlockPos; import net.minecraft.block.*; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; diff --git a/src/main/java/baritone/pathing/path/PathExecutor.java b/src/main/java/baritone/pathing/path/PathExecutor.java index a60b4a29..5f8c3f89 100644 --- a/src/main/java/baritone/pathing/path/PathExecutor.java +++ b/src/main/java/baritone/pathing/path/PathExecutor.java @@ -20,18 +20,23 @@ package baritone.pathing.path; import baritone.Baritone; import baritone.api.event.events.TickEvent; import baritone.api.pathing.movement.ActionCosts; +import baritone.api.pathing.movement.IMovement; +import baritone.api.pathing.movement.MovementStatus; +import baritone.api.pathing.path.IPath; +import baritone.api.pathing.path.IPathExecutor; +import baritone.api.utils.BetterBlockPos; import baritone.pathing.calc.AbstractNodeCostSearch; -import baritone.pathing.movement.*; +import baritone.pathing.movement.CalculationContext; +import baritone.pathing.movement.MovementHelper; import baritone.pathing.movement.movements.*; import baritone.utils.*; -import baritone.utils.pathing.BetterBlockPos; import net.minecraft.init.Blocks; import net.minecraft.util.Tuple; import net.minecraft.util.math.BlockPos; import java.util.*; -import static baritone.pathing.movement.MovementStatus.*; +import static baritone.api.pathing.movement.MovementStatus.*; /** * Behavior to execute a precomputed path. Does not (yet) deal with path segmentation or stitching @@ -39,7 +44,7 @@ import static baritone.pathing.movement.MovementStatus.*; * * @author leijurv */ -public class PathExecutor implements Helper { +public class PathExecutor implements IPathExecutor, Helper { private static final double MAX_MAX_DIST_FROM_PATH = 3; private static final double MAX_DIST_FROM_PATH = 2; @@ -125,7 +130,7 @@ public class PathExecutor implements Helper { } } } - Tuple status = path.closestPathPos(); + Tuple status = closestPathPos(path); if (possiblyOffPath(status, MAX_DIST_FROM_PATH)) { ticksAway++; System.out.println("FAR AWAY FROM PATH FOR " + ticksAway + " TICKS. Current distance: " + status.getFirst() + ". Threshold: " + MAX_DIST_FROM_PATH); @@ -269,6 +274,19 @@ public class PathExecutor implements Helper { return false; // movement is in progress } + private Tuple closestPathPos(IPath path) { + double best = -1; + BlockPos bestPos = null; + for (BlockPos pos : path.positions()) { + double dist = Utils.playerDistanceToCenter(pos); + if (dist < best || best == -1) { + best = dist; + bestPos = pos; + } + } + return new Tuple<>(best, bestPos); + } + private boolean shouldPause() { Optional current = AbstractNodeCostSearch.getCurrentlyRunning(); if (!current.isPresent()) { diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index fd733d7f..c851a087 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -31,7 +31,6 @@ import baritone.cache.ChunkPacker; import baritone.cache.Waypoint; import baritone.cache.WorldProvider; import baritone.pathing.calc.AbstractNodeCostSearch; -import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.Movement; import baritone.pathing.movement.MovementHelper; import baritone.pathing.movement.Moves; diff --git a/src/main/java/baritone/utils/Helper.java b/src/main/java/baritone/utils/Helper.java index a0ffdb96..f026ec38 100755 --- a/src/main/java/baritone/utils/Helper.java +++ b/src/main/java/baritone/utils/Helper.java @@ -18,8 +18,8 @@ package baritone.utils; import baritone.Baritone; +import baritone.api.utils.BetterBlockPos; import baritone.api.utils.Rotation; -import baritone.utils.pathing.BetterBlockPos; import net.minecraft.block.BlockSlab; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; diff --git a/src/main/java/baritone/utils/PathRenderer.java b/src/main/java/baritone/utils/PathRenderer.java index 6891a299..de2630c9 100644 --- a/src/main/java/baritone/utils/PathRenderer.java +++ b/src/main/java/baritone/utils/PathRenderer.java @@ -23,12 +23,12 @@ import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.GoalComposite; import baritone.api.pathing.goals.GoalTwoBlocks; import baritone.api.pathing.goals.GoalXZ; +import baritone.api.pathing.path.IPath; +import baritone.api.utils.BetterBlockPos; import baritone.api.utils.interfaces.IGoalRenderPos; import baritone.behavior.PathingBehavior; import baritone.pathing.calc.AbstractNodeCostSearch; -import baritone.pathing.path.IPath; import baritone.pathing.path.PathExecutor; -import baritone.utils.pathing.BetterBlockPos; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; diff --git a/src/test/java/baritone/utils/pathing/BetterBlockPosTest.java b/src/test/java/baritone/utils/pathing/BetterBlockPosTest.java index 13b76c73..a21f0cd4 100644 --- a/src/test/java/baritone/utils/pathing/BetterBlockPosTest.java +++ b/src/test/java/baritone/utils/pathing/BetterBlockPosTest.java @@ -17,6 +17,7 @@ package baritone.utils.pathing; +import baritone.api.utils.BetterBlockPos; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import org.junit.Test; From 7d0aa4d7a5cb57fa2bb3c431f47cb7e9122c2681 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 8 Oct 2018 20:39:33 -0500 Subject: [PATCH 06/12] Add override annotation to getPath in PathExecutor --- src/main/java/baritone/pathing/path/PathExecutor.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/baritone/pathing/path/PathExecutor.java b/src/main/java/baritone/pathing/path/PathExecutor.java index 5f8c3f89..cbf078fd 100644 --- a/src/main/java/baritone/pathing/path/PathExecutor.java +++ b/src/main/java/baritone/pathing/path/PathExecutor.java @@ -451,6 +451,7 @@ public class PathExecutor implements IPathExecutor, Helper { return pathPosition; } + @Override public IPath getPath() { return path; } From 8278576227f2c3b8043e358c4f926791aea68fa3 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 8 Oct 2018 21:02:55 -0500 Subject: [PATCH 07/12] Rename getCurrentPathSearch to getPathFinder --- src/api/java/baritone/api/behavior/IPathingBehavior.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/java/baritone/api/behavior/IPathingBehavior.java b/src/api/java/baritone/api/behavior/IPathingBehavior.java index 5960688a..f92a00ca 100644 --- a/src/api/java/baritone/api/behavior/IPathingBehavior.java +++ b/src/api/java/baritone/api/behavior/IPathingBehavior.java @@ -81,7 +81,7 @@ public interface IPathingBehavior extends IBehavior { /** * @return The current path finder being executed */ - Optional getCurrentPathSearch(); + Optional getPathFinder(); /** * @return The current path executor From 6a4a8ab2d9184ba04fef38d85043e7aa4f88b677 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 8 Oct 2018 21:09:54 -0500 Subject: [PATCH 08/12] I'm an IDIOT --- src/main/java/baritone/behavior/PathingBehavior.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 45163852..c701e7a5 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -202,7 +202,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, } @Override - public Optional getCurrentPathSearch() { + public Optional getPathFinder() { return Optional.ofNullable(AbstractNodeCostSearch.currentlyRunning()); } From 875f01c358279e334d38c2e304f31a7c4b6aee44 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 8 Oct 2018 23:29:16 -0500 Subject: [PATCH 09/12] More comprehensive IPath javadocs --- .../baritone/api/pathing/path/CutoffPath.java | 2 +- .../java/baritone/api/pathing/path/IPath.java | 54 +++++++++++++++---- .../pathing/calc/AbstractNodeCostSearch.java | 2 +- src/main/java/baritone/pathing/calc/Path.java | 2 +- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/api/java/baritone/api/pathing/path/CutoffPath.java b/src/api/java/baritone/api/pathing/path/CutoffPath.java index a702b7fd..1fcbdadc 100644 --- a/src/api/java/baritone/api/pathing/path/CutoffPath.java +++ b/src/api/java/baritone/api/pathing/path/CutoffPath.java @@ -34,7 +34,7 @@ public class CutoffPath implements IPath { private final Goal goal; - public CutoffPath(IPath prev, int lastPositionToInclude) { + CutoffPath(IPath prev, int lastPositionToInclude) { path = prev.positions().subList(0, lastPositionToInclude + 1); movements = prev.movements().subList(0, lastPositionToInclude + 1); numNodes = prev.getNumNodesConsidered(); diff --git a/src/api/java/baritone/api/pathing/path/IPath.java b/src/api/java/baritone/api/pathing/path/IPath.java index 2d4b08e3..275f0f97 100644 --- a/src/api/java/baritone/api/pathing/path/IPath.java +++ b/src/api/java/baritone/api/pathing/path/IPath.java @@ -18,6 +18,7 @@ package baritone.api.pathing.path; import baritone.api.BaritoneAPI; +import baritone.api.Settings; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.movement.IMovement; import baritone.api.utils.BetterBlockPos; @@ -28,7 +29,7 @@ import net.minecraft.world.chunk.EmptyChunk; import java.util.List; /** - * @author leijurv + * @author leijurv, Brady */ public interface IPath { @@ -37,12 +38,16 @@ public interface IPath { * 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 + * + * @return All of the movements to carry out */ List movements(); /** * All positions along the way. * Should begin with the same as getSrc and end with the same as getDest + * + * @return All of the positions along this path */ List positions(); @@ -50,10 +55,10 @@ public interface IPath { * This path is actually going to be executed in the world. Do whatever additional processing is required. * (as opposed to Path objects that are just constructed every frame for rendering) */ - default void postprocess() {} + default void postProcess() {} /** - * Number of positions in this path + * Returns the number of positions in this path. Equivalent to {@code positions().size()}. * * @return Number of positions in this path */ @@ -62,27 +67,45 @@ public interface IPath { } /** - * What goal was this path calculated towards? - * - * @return + * @return The goal that this path was calculated towards */ Goal getGoal(); /** - * Where does this path start + * Returns the number of nodes that were considered during calculation before + * this path was found. + * + * @return The number of nodes that were considered before finding this path + */ + int getNumNodesConsidered(); + + /** + * Returns the start position of this path. This is the first element in the + * {@link List} that is returned by {@link IPath#positions()}. + * + * @return The start position of this path */ default BetterBlockPos getSrc() { return positions().get(0); } /** - * Where does this path end + * Returns the end position of this path. This is the last element in the + * {@link List} that is returned by {@link IPath#positions()}. + * + * @return The end position of this path. */ default BetterBlockPos getDest() { List pos = positions(); return pos.get(pos.size() - 1); } + /** + * Returns the estimated number of ticks to complete the path from the given node index. + * + * @param pathPosition The index of the node we're calculating from + * @return The estimated number of ticks remaining frm the given position + */ default double ticksRemainingFrom(int pathPosition) { double sum = 0; //this is fast because we aren't requesting recalculation, it's just cached @@ -92,8 +115,11 @@ public interface IPath { return sum; } - int getNumNodesConsidered(); - + /** + * Cuts off this path at the loaded chunk border, and returns the {@link CutoffResult}. + * + * @return The result of this cut-off operation + */ default CutoffResult cutoffAtLoadedChunks() { for (int i = 0; i < positions().size(); i++) { BlockPos pos = positions().get(i); @@ -104,6 +130,14 @@ public interface IPath { return CutoffResult.preservePath(this); } + /** + * Cuts off this path using the min length and cutoff factor settings, and returns the {@link CutoffResult}. + * + * @see Settings#pathCutoffMinimumLength + * @see Settings#pathCutoffFactor + * + * @return The result of this cut-off operation + */ default CutoffResult staticCutoff(Goal destination) { if (length() < BaritoneAPI.getSettings().pathCutoffMinimumLength.get()) { return CutoffResult.preservePath(this); diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index adac5e28..3c033a93 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -89,7 +89,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { this.cancelRequested = false; try { Optional path = calculate0(timeout); - path.ifPresent(IPath::postprocess); + path.ifPresent(IPath::postProcess); isFinished = true; return path; } finally { diff --git a/src/main/java/baritone/pathing/calc/Path.java b/src/main/java/baritone/pathing/calc/Path.java index ddd51dc2..4983de54 100644 --- a/src/main/java/baritone/pathing/calc/Path.java +++ b/src/main/java/baritone/pathing/calc/Path.java @@ -150,7 +150,7 @@ class Path implements IPath { } @Override - public void postprocess() { + public void postProcess() { if (verified) { throw new IllegalStateException(); } From 2e69bbe371a03cfbcf3c7c41b63d60743280bc64 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 8 Oct 2018 23:34:12 -0500 Subject: [PATCH 10/12] Minimal CutoffResult Javadocs --- .../api/pathing/path/CutoffResult.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/api/java/baritone/api/pathing/path/CutoffResult.java b/src/api/java/baritone/api/pathing/path/CutoffResult.java index eeaa4ad1..d4175e85 100644 --- a/src/api/java/baritone/api/pathing/path/CutoffResult.java +++ b/src/api/java/baritone/api/pathing/path/CutoffResult.java @@ -18,13 +18,21 @@ package baritone.api.pathing.path; /** + * The result of a path cut-off operation. + * * @author Brady * @since 10/8/2018 */ public final class CutoffResult { + /** + * The resulting path + */ private final IPath path; + /** + * The amount of movements that were removed + */ private final int removed; private CutoffResult(IPath path, int removed) { @@ -32,22 +40,44 @@ public final class CutoffResult { this.removed = removed; } + /** + * @return Whether or not the path was cut + */ public final boolean wasCut() { return this.removed > 0; } + /** + * @return The amount of movements that were removed + */ public final int getRemoved() { return this.removed; } + /** + * @return The resulting path + */ public final IPath getPath() { return this.path; } + /** + * Creates a new result from a successful cut-off operation. + * + * @param path The input path + * @param index The index to cut the path at + * @return The result of the operation + */ public static CutoffResult cutoffPath(IPath path, int index) { return new CutoffResult(new CutoffPath(path, index), path.positions().size() - index - 1); } + /** + * Creates a new result in which no cut-off occurred. + * + * @param path The input path + * @return The result of the operation + */ public static CutoffResult preservePath(IPath path) { return new CutoffResult(path, 0); } From 0fb5f3233f4f1c2996550d40a0aa326294a0dbde Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 8 Oct 2018 23:52:36 -0500 Subject: [PATCH 11/12] MovementStatus javadocs --- .../api/pathing/movement/MovementStatus.java | 49 ++++++++++++++++++- .../baritone/pathing/movement/Movement.java | 9 +--- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/api/java/baritone/api/pathing/movement/MovementStatus.java b/src/api/java/baritone/api/pathing/movement/MovementStatus.java index 6b5215ad..0190f8e1 100644 --- a/src/api/java/baritone/api/pathing/movement/MovementStatus.java +++ b/src/api/java/baritone/api/pathing/movement/MovementStatus.java @@ -23,5 +23,52 @@ package baritone.api.pathing.movement; */ public enum MovementStatus { - PREPPING, WAITING, RUNNING, SUCCESS, UNREACHABLE, FAILED, CANCELED + /** + * We are preparing the movement to be executed. This is when any blocks obstructing the destination are broken. + */ + PREPPING(false), + + /** + * We are waiting for the movement to begin, after {@link MovementStatus#PREPPING}. + */ + WAITING(false), + + /** + * The movement is currently in progress, after {@link MovementStatus#WAITING} + */ + RUNNING(false), + + /** + * The movement has been completed and we are at our destination + */ + SUCCESS(true), + + /** + * There was a change in state between calculation and actual + * movement execution, and the movement has now become impossible. + */ + UNREACHABLE(true), + + /** + * Unused + */ + FAILED(true), + + /** + * "Unused" + */ + CANCELED(true); + + /** + * Whether or not this status indicates a complete movement. + */ + private final boolean complete; + + MovementStatus(boolean complete) { + this.complete = complete; + } + + public final boolean isComplete() { + return this.complete; + } } diff --git a/src/main/java/baritone/pathing/movement/Movement.java b/src/main/java/baritone/pathing/movement/Movement.java index cffa28ee..92a1f4db 100644 --- a/src/main/java/baritone/pathing/movement/Movement.java +++ b/src/main/java/baritone/pathing/movement/Movement.java @@ -152,7 +152,8 @@ public abstract class Movement implements IMovement, Helper, MovementHelper { currentState = latestState; - if (isFinished()) { + // If the current status indicates a completed movement + if (currentState.getStatus().isComplete()) { onFinish(latestState); } @@ -201,12 +202,6 @@ public abstract class Movement implements IMovement, Helper, MovementHelper { return true; } - public boolean isFinished() { - return (currentState.getStatus() != MovementStatus.RUNNING - && currentState.getStatus() != MovementStatus.PREPPING - && currentState.getStatus() != MovementStatus.WAITING); - } - @Override public BetterBlockPos getSrc() { return src; From 771e892b31f3f3463a976b0e7e19d9ba0e9821fa Mon Sep 17 00:00:00 2001 From: Brady Date: Tue, 9 Oct 2018 00:10:50 -0500 Subject: [PATCH 12/12] Get Cutoff implementation out of API we DON'T need you --- .../api/behavior/IPathingBehavior.java | 2 +- .../api/pathing/{path => calc}/IPath.java | 34 ++------ .../api/pathing/calc/IPathFinder.java | 1 - .../api/pathing/path/CutoffResult.java | 84 ------------------- .../api/pathing/path/IPathExecutor.java | 2 + .../baritone/behavior/PathingBehavior.java | 20 ++--- .../pathing/calc/AStarPathFinder.java | 2 +- .../pathing/calc/AbstractNodeCostSearch.java | 2 +- .../baritone/pathing/calc}/CutoffPath.java | 3 +- src/main/java/baritone/pathing/calc/Path.java | 29 ++++++- .../baritone/pathing/path/PathExecutor.java | 2 +- .../java/baritone/utils/PathRenderer.java | 2 +- 12 files changed, 56 insertions(+), 127 deletions(-) rename src/api/java/baritone/api/pathing/{path => calc}/IPath.java (77%) delete mode 100644 src/api/java/baritone/api/pathing/path/CutoffResult.java rename src/{api/java/baritone/api/pathing/path => main/java/baritone/pathing/calc}/CutoffPath.java (96%) diff --git a/src/api/java/baritone/api/behavior/IPathingBehavior.java b/src/api/java/baritone/api/behavior/IPathingBehavior.java index f92a00ca..cfc93d2d 100644 --- a/src/api/java/baritone/api/behavior/IPathingBehavior.java +++ b/src/api/java/baritone/api/behavior/IPathingBehavior.java @@ -19,7 +19,7 @@ package baritone.api.behavior; import baritone.api.pathing.calc.IPathFinder; import baritone.api.pathing.goals.Goal; -import baritone.api.pathing.path.IPath; +import baritone.api.pathing.calc.IPath; import baritone.api.pathing.path.IPathExecutor; import java.util.Optional; diff --git a/src/api/java/baritone/api/pathing/path/IPath.java b/src/api/java/baritone/api/pathing/calc/IPath.java similarity index 77% rename from src/api/java/baritone/api/pathing/path/IPath.java rename to src/api/java/baritone/api/pathing/calc/IPath.java index 275f0f97..c9d58818 100644 --- a/src/api/java/baritone/api/pathing/path/IPath.java +++ b/src/api/java/baritone/api/pathing/calc/IPath.java @@ -15,16 +15,12 @@ * along with Baritone. If not, see . */ -package baritone.api.pathing.path; +package baritone.api.pathing.calc; -import baritone.api.BaritoneAPI; import baritone.api.Settings; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.movement.IMovement; import baritone.api.utils.BetterBlockPos; -import net.minecraft.client.Minecraft; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.chunk.EmptyChunk; import java.util.List; @@ -116,37 +112,25 @@ public interface IPath { } /** - * Cuts off this path at the loaded chunk border, and returns the {@link CutoffResult}. + * Cuts off this path at the loaded chunk border, and returns the resulting path. Default + * implementation just returns this path, without the intended functionality. * * @return The result of this cut-off operation */ - default CutoffResult cutoffAtLoadedChunks() { - for (int i = 0; i < positions().size(); i++) { - BlockPos pos = positions().get(i); - if (Minecraft.getMinecraft().world.getChunk(pos) instanceof EmptyChunk) { - return CutoffResult.cutoffPath(this, i); - } - } - return CutoffResult.preservePath(this); + default IPath cutoffAtLoadedChunks() { + return this; } /** - * Cuts off this path using the min length and cutoff factor settings, and returns the {@link CutoffResult}. + * Cuts off this path using the min length and cutoff factor settings, and returns the resulting path. + * Default implementation just returns this path, without the intended functionality. * * @see Settings#pathCutoffMinimumLength * @see Settings#pathCutoffFactor * * @return The result of this cut-off operation */ - default CutoffResult staticCutoff(Goal destination) { - if (length() < BaritoneAPI.getSettings().pathCutoffMinimumLength.get()) { - return CutoffResult.preservePath(this); - } - if (destination == null || destination.isInGoal(getDest())) { - return CutoffResult.preservePath(this); - } - double factor = BaritoneAPI.getSettings().pathCutoffFactor.get(); - int newLength = (int) (length() * factor); - return CutoffResult.cutoffPath(this, newLength); + default IPath staticCutoff(Goal destination) { + return this; } } diff --git a/src/api/java/baritone/api/pathing/calc/IPathFinder.java b/src/api/java/baritone/api/pathing/calc/IPathFinder.java index 31fed89d..446f7e05 100644 --- a/src/api/java/baritone/api/pathing/calc/IPathFinder.java +++ b/src/api/java/baritone/api/pathing/calc/IPathFinder.java @@ -18,7 +18,6 @@ package baritone.api.pathing.calc; import baritone.api.pathing.goals.Goal; -import baritone.api.pathing.path.IPath; import java.util.Optional; diff --git a/src/api/java/baritone/api/pathing/path/CutoffResult.java b/src/api/java/baritone/api/pathing/path/CutoffResult.java deleted file mode 100644 index d4175e85..00000000 --- a/src/api/java/baritone/api/pathing/path/CutoffResult.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * This file is part of Baritone. - * - * Baritone is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Baritone. If not, see . - */ - -package baritone.api.pathing.path; - -/** - * The result of a path cut-off operation. - * - * @author Brady - * @since 10/8/2018 - */ -public final class CutoffResult { - - /** - * The resulting path - */ - private final IPath path; - - /** - * The amount of movements that were removed - */ - private final int removed; - - private CutoffResult(IPath path, int removed) { - this.path = path; - this.removed = removed; - } - - /** - * @return Whether or not the path was cut - */ - public final boolean wasCut() { - return this.removed > 0; - } - - /** - * @return The amount of movements that were removed - */ - public final int getRemoved() { - return this.removed; - } - - /** - * @return The resulting path - */ - public final IPath getPath() { - return this.path; - } - - /** - * Creates a new result from a successful cut-off operation. - * - * @param path The input path - * @param index The index to cut the path at - * @return The result of the operation - */ - public static CutoffResult cutoffPath(IPath path, int index) { - return new CutoffResult(new CutoffPath(path, index), path.positions().size() - index - 1); - } - - /** - * Creates a new result in which no cut-off occurred. - * - * @param path The input path - * @return The result of the operation - */ - public static CutoffResult preservePath(IPath path) { - return new CutoffResult(path, 0); - } -} diff --git a/src/api/java/baritone/api/pathing/path/IPathExecutor.java b/src/api/java/baritone/api/pathing/path/IPathExecutor.java index bf701224..f72060dc 100644 --- a/src/api/java/baritone/api/pathing/path/IPathExecutor.java +++ b/src/api/java/baritone/api/pathing/path/IPathExecutor.java @@ -17,6 +17,8 @@ package baritone.api.pathing.path; +import baritone.api.pathing.calc.IPath; + /** * @author Brady * @since 10/8/2018 diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index c701e7a5..e91769b8 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -25,8 +25,8 @@ import baritone.api.event.events.RenderEvent; import baritone.api.event.events.TickEvent; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.GoalXZ; -import baritone.api.pathing.path.CutoffResult; -import baritone.api.pathing.path.IPath; +import baritone.pathing.calc.CutoffPath; +import baritone.api.pathing.calc.IPath; import baritone.api.utils.BetterBlockPos; import baritone.api.utils.interfaces.IGoalRenderPos; import baritone.pathing.calc.AStarPathFinder; @@ -285,27 +285,27 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, Optional path = findPath(start, previous); if (Baritone.settings().cutoffAtLoadBoundary.get()) { path = path.map(p -> { - CutoffResult result = p.cutoffAtLoadedChunks(); + IPath result = p.cutoffAtLoadedChunks(); - if (result.wasCut()) { + if (result instanceof CutoffPath) { logDebug("Cutting off path at edge of loaded chunks"); - logDebug("Length decreased by " + result.getRemoved()); + logDebug("Length decreased by " + (p.length() - result.length())); } else { logDebug("Path ends within loaded chunks"); } - return result.getPath(); + return result; }); } Optional executor = path.map(p -> { - CutoffResult result = p.staticCutoff(goal); + IPath result = p.staticCutoff(goal); - if (result.wasCut()) { - logDebug("Static cutoff " + p.length() + " to " + result.getPath().length()); + if (result instanceof CutoffPath) { + logDebug("Static cutoff " + p.length() + " to " + result.length()); } - return result.getPath(); + return result; }).map(PathExecutor::new); synchronized (pathPlanLock) { diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index 3afafda9..f4adc7e2 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -20,7 +20,7 @@ package baritone.pathing.calc; import baritone.Baritone; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.movement.ActionCosts; -import baritone.api.pathing.path.IPath; +import baritone.api.pathing.calc.IPath; import baritone.api.utils.BetterBlockPos; import baritone.pathing.calc.openset.BinaryHeapOpenSet; import baritone.pathing.movement.CalculationContext; diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index 3c033a93..424c512e 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -20,7 +20,7 @@ package baritone.pathing.calc; import baritone.Baritone; import baritone.api.pathing.calc.IPathFinder; import baritone.api.pathing.goals.Goal; -import baritone.api.pathing.path.IPath; +import baritone.api.pathing.calc.IPath; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import java.util.Optional; diff --git a/src/api/java/baritone/api/pathing/path/CutoffPath.java b/src/main/java/baritone/pathing/calc/CutoffPath.java similarity index 96% rename from src/api/java/baritone/api/pathing/path/CutoffPath.java rename to src/main/java/baritone/pathing/calc/CutoffPath.java index 1fcbdadc..bcf66b79 100644 --- a/src/api/java/baritone/api/pathing/path/CutoffPath.java +++ b/src/main/java/baritone/pathing/calc/CutoffPath.java @@ -15,10 +15,11 @@ * along with Baritone. If not, see . */ -package baritone.api.pathing.path; +package baritone.pathing.calc; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.movement.IMovement; +import baritone.api.pathing.calc.IPath; import baritone.api.utils.BetterBlockPos; import java.util.Collections; diff --git a/src/main/java/baritone/pathing/calc/Path.java b/src/main/java/baritone/pathing/calc/Path.java index 4983de54..f7bf9638 100644 --- a/src/main/java/baritone/pathing/calc/Path.java +++ b/src/main/java/baritone/pathing/calc/Path.java @@ -17,13 +17,16 @@ package baritone.pathing.calc; +import baritone.api.BaritoneAPI; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.movement.IMovement; -import baritone.api.pathing.path.IPath; +import baritone.api.pathing.calc.IPath; import baritone.api.utils.BetterBlockPos; import baritone.pathing.movement.Movement; import baritone.pathing.movement.Moves; +import net.minecraft.client.Minecraft; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.chunk.EmptyChunk; import java.util.ArrayList; import java.util.Collections; @@ -188,4 +191,28 @@ class Path implements IPath { public BetterBlockPos getDest() { return end; } + + @Override + public IPath cutoffAtLoadedChunks() { + for (int i = 0; i < positions().size(); i++) { + BlockPos pos = positions().get(i); + if (Minecraft.getMinecraft().world.getChunk(pos) instanceof EmptyChunk) { + return new CutoffPath(this, i); + } + } + return this; + } + + @Override + public IPath staticCutoff(Goal destination) { + if (length() < BaritoneAPI.getSettings().pathCutoffMinimumLength.get()) { + return this; + } + if (destination == null || destination.isInGoal(getDest())) { + return this; + } + double factor = BaritoneAPI.getSettings().pathCutoffFactor.get(); + int newLength = (int) (length() * factor); + return new CutoffPath(this, newLength); + } } diff --git a/src/main/java/baritone/pathing/path/PathExecutor.java b/src/main/java/baritone/pathing/path/PathExecutor.java index cbf078fd..6fba09a3 100644 --- a/src/main/java/baritone/pathing/path/PathExecutor.java +++ b/src/main/java/baritone/pathing/path/PathExecutor.java @@ -22,7 +22,7 @@ import baritone.api.event.events.TickEvent; import baritone.api.pathing.movement.ActionCosts; import baritone.api.pathing.movement.IMovement; import baritone.api.pathing.movement.MovementStatus; -import baritone.api.pathing.path.IPath; +import baritone.api.pathing.calc.IPath; import baritone.api.pathing.path.IPathExecutor; import baritone.api.utils.BetterBlockPos; import baritone.pathing.calc.AbstractNodeCostSearch; diff --git a/src/main/java/baritone/utils/PathRenderer.java b/src/main/java/baritone/utils/PathRenderer.java index de2630c9..841876d8 100644 --- a/src/main/java/baritone/utils/PathRenderer.java +++ b/src/main/java/baritone/utils/PathRenderer.java @@ -23,7 +23,7 @@ import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.GoalComposite; import baritone.api.pathing.goals.GoalTwoBlocks; import baritone.api.pathing.goals.GoalXZ; -import baritone.api.pathing.path.IPath; +import baritone.api.pathing.calc.IPath; import baritone.api.utils.BetterBlockPos; import baritone.api.utils.interfaces.IGoalRenderPos; import baritone.behavior.PathingBehavior;