Skip to content

Commit

Permalink
prepend feet to the path if start is adjacent to feet
Browse files Browse the repository at this point in the history
  • Loading branch information
babbaj committed Oct 14, 2024
1 parent ef72c56 commit a690e1e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/main/java/baritone/behavior/PathingBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public boolean secretInternalSetGoalAndPath(PathingCommand command) {
if (goal == null) {
return false;
}
if (goal.isInGoal(ctx.playerFeet()) || goal.isInGoal(expectedSegmentStart)) {
if (goal.isInGoal(ctx.playerFeet())) {
return false;
}
synchronized (pathPlanLock) {
Expand Down Expand Up @@ -553,7 +553,7 @@ private void findPathInNewThread(final BlockPos start, final boolean talkAboutIt
});
}

private static AbstractNodeCostSearch createPathfinder(BlockPos start, Goal goal, IPath previous, CalculationContext context) {
private AbstractNodeCostSearch createPathfinder(BlockPos start, Goal goal, IPath previous, CalculationContext context) {
Goal transformed = goal;
if (Baritone.settings().simplifyUnloadedYCoord.value && goal instanceof IGoalRenderPos) {
BlockPos pos = ((IGoalRenderPos) goal).getGoalPos();
Expand All @@ -562,7 +562,14 @@ private static AbstractNodeCostSearch createPathfinder(BlockPos start, Goal goal
}
}
Favoring favoring = new Favoring(context.getBaritone().getPlayerContext(), previous, context);
return new AStarPathFinder(start.getX(), start.getY(), start.getZ(), transformed, favoring, context);
BetterBlockPos feet = ctx.playerFeet();
var realStart = new BetterBlockPos(start);
var sub = feet.subtract(realStart);
if (feet.getY() == realStart.getY() && Math.abs(sub.getX()) <= 1 && Math.abs(sub.getZ()) <= 1) {
realStart = feet;
}
return new AStarPathFinder(realStart, start.getX(), start.getY(), start.getZ(), transformed, favoring, context);

}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/baritone/pathing/calc/AStarPathFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public final class AStarPathFinder extends AbstractNodeCostSearch {
private final Favoring favoring;
private final CalculationContext calcContext;

public AStarPathFinder(int startX, int startY, int startZ, Goal goal, Favoring favoring, CalculationContext context) {
super(startX, startY, startZ, goal, context);
public AStarPathFinder(BetterBlockPos realStart, int startX, int startY, int startZ, Goal goal, Favoring favoring, CalculationContext context) {
super(realStart, startX, startY, startZ, goal, context);
this.favoring = favoring;
this.calcContext = context;
}
Expand Down Expand Up @@ -96,7 +96,7 @@ protected Optional<IPath> calculate0(long primaryTimeout, long failureTimeout) {
numNodes++;
if (goal.isInGoal(currentNode.x, currentNode.y, currentNode.z)) {
logDebug("Took " + (System.currentTimeMillis() - startTime) + "ms, " + numMovementsConsidered + " movements considered");
return Optional.of(new Path(startNode, currentNode, numNodes, goal, calcContext));
return Optional.of(new Path(realStart, startNode, currentNode, numNodes, goal, calcContext));
}
for (Moves moves : allMoves) {
int newX = currentNode.x + moves.xOffset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*/
public abstract class AbstractNodeCostSearch implements IPathFinder, Helper {

protected final BetterBlockPos realStart;
protected final int startX;
protected final int startY;
protected final int startZ;
Expand Down Expand Up @@ -81,7 +82,8 @@ public abstract class AbstractNodeCostSearch implements IPathFinder, Helper {
*/
protected static final double MIN_IMPROVEMENT = 0.01;

AbstractNodeCostSearch(int startX, int startY, int startZ, Goal goal, CalculationContext context) {
AbstractNodeCostSearch(BetterBlockPos realStart, int startX, int startY, int startZ, Goal goal, CalculationContext context) {
this.realStart = realStart;
this.startX = startX;
this.startY = startY;
this.startZ = startZ;
Expand Down Expand Up @@ -177,7 +179,7 @@ protected PathNode getNodeAtPosition(int x, int y, int z, long hashCode) {

@Override
public Optional<IPath> pathToMostRecentNodeConsidered() {
return Optional.ofNullable(mostRecentConsidered).map(node -> new Path(startNode, node, 0, goal, context));
return Optional.ofNullable(mostRecentConsidered).map(node -> new Path(realStart, startNode, node, 0, goal, context));
}

@Override
Expand Down Expand Up @@ -208,7 +210,7 @@ protected Optional<IPath> bestSoFar(boolean logInfo, int numNodes) {
System.out.println("Path goes for " + Math.sqrt(dist) + " blocks");
logDebug("A* cost coefficient " + COEFFICIENTS[i]);
}
return Optional.of(new Path(startNode, bestSoFar[i], numNodes, goal, context));
return Optional.of(new Path(realStart, startNode, bestSoFar[i], numNodes, goal, context));
}
}
// instead of returning bestSoFar[0], be less misleading
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/baritone/pathing/calc/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ class Path extends PathBase {

private volatile boolean verified;

Path(PathNode start, PathNode end, int numNodes, Goal goal, CalculationContext context) {
this.start = new BetterBlockPos(start.x, start.y, start.z);
Path(BetterBlockPos realStart, PathNode start, PathNode end, int numNodes, Goal goal, CalculationContext context) {
this.start = realStart;
var startNodePos = new BetterBlockPos(start.x, start.y, start.z);
this.end = new BetterBlockPos(end.x, end.y, end.z);
this.numNodes = numNodes;
this.movements = new ArrayList<>();
Expand All @@ -85,6 +86,13 @@ class Path extends PathBase {
tempPath.addFirst(new BetterBlockPos(current.x, current.y, current.z));
current = current.previous;
}
if (!realStart.equals(startNodePos)) {
PathNode fakeNode = new PathNode(realStart.x, realStart.y, realStart.z, goal);
fakeNode.cost = 0;
tempNodes.addFirst(fakeNode);
tempPath.addFirst(realStart);
}

// Can't directly convert from the PathNode pseudo linked list to an array because we don't know how long it is
// inserting into a LinkedList<E> keeps track of length, then when we addall (which calls .toArray) it's able
// to performantly do that conversion since it knows the length.
Expand Down

0 comments on commit a690e1e

Please sign in to comment.