Files
baritone/src/main/java/baritone/pathing/calc/AStarPathFinder.java

193 lines
10 KiB
Java
Raw Normal View History

2018-08-07 22:16:53 -05:00
/*
* 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
2018-08-07 22:16:53 -05:00
* 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,
2018-08-07 22:16:53 -05:00
* 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.
2018-08-07 22:16:53 -05:00
*
* You should have received a copy of the GNU Lesser General Public License
2018-08-07 22:16:53 -05:00
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2018-08-22 13:15:56 -07:00
package baritone.pathing.calc;
2018-08-03 09:55:17 -04:00
2018-08-22 13:15:56 -07:00
import baritone.Baritone;
import baritone.api.pathing.goals.Goal;
2018-09-24 20:32:39 -05:00
import baritone.api.pathing.movement.ActionCosts;
2018-09-24 18:56:49 -07:00
import baritone.pathing.calc.openset.BinaryHeapOpenSet;
2018-08-22 13:15:56 -07:00
import baritone.pathing.movement.CalculationContext;
2018-09-23 14:23:23 -07:00
import baritone.pathing.movement.Moves;
2018-08-22 13:15:56 -07:00
import baritone.pathing.path.IPath;
2018-09-09 09:50:19 -07:00
import baritone.utils.BlockStateInterface;
2018-08-22 13:15:56 -07:00
import baritone.utils.Helper;
2018-09-24 18:51:10 -05:00
import baritone.utils.pathing.MoveResult;
2018-08-03 09:55:17 -04:00
2018-08-16 15:10:15 -07:00
import java.util.HashSet;
import java.util.Optional;
2018-08-03 09:55:17 -04:00
/**
* The actual A* pathfinding
*
* @author leijurv
*/
2018-09-22 22:00:28 -07:00
public final class AStarPathFinder extends AbstractNodeCostSearch implements Helper {
private final Optional<HashSet<Long>> favoredPositions;
2018-08-16 15:10:15 -07:00
2018-10-03 07:57:24 -07:00
public AStarPathFinder(int startX, int startY, int startZ, Goal goal, Optional<HashSet<Long>> favoredPositions) {
super(startX, startY, startZ, goal);
2018-09-23 08:54:26 -07:00
this.favoredPositions = favoredPositions;
2018-08-03 09:55:17 -04:00
}
@Override
protected Optional<IPath> calculate0(long timeout) {
2018-10-03 07:57:24 -07:00
startNode = getNodeAtPosition(startX, startY, startZ, posHash(startX, startY, startZ));
2018-08-03 09:55:17 -04:00
startNode.cost = 0;
startNode.combinedCost = startNode.estimatedCostToGoal;
2018-08-29 15:35:41 -07:00
BinaryHeapOpenSet openSet = new BinaryHeapOpenSet();
2018-08-03 09:55:17 -04:00
openSet.insert(startNode);
2018-08-16 15:10:15 -07:00
startNode.isOpen = true;
2018-08-03 09:55:17 -04:00
bestSoFar = new PathNode[COEFFICIENTS.length];//keep track of the best node by the metric of (estimatedCostToGoal + cost / COEFFICIENTS[i])
double[] bestHeuristicSoFar = new double[COEFFICIENTS.length];
for (int i = 0; i < bestHeuristicSoFar.length; i++) {
bestHeuristicSoFar[i] = startNode.estimatedCostToGoal;
bestSoFar[i] = startNode;
2018-08-03 09:55:17 -04:00
}
2018-08-16 15:10:15 -07:00
CalculationContext calcContext = new CalculationContext();
HashSet<Long> favored = favoredPositions.orElse(null);
2018-09-09 09:50:19 -07:00
BlockStateInterface.clearCachedChunk();
long startTime = System.nanoTime() / 1000000L;
2018-08-14 15:04:41 -07:00
boolean slowPath = Baritone.settings().slowPath.get();
if (slowPath) {
2018-09-11 13:45:43 -07:00
logDebug("slowPath is on, path timeout will be " + Baritone.settings().slowPathTimeoutMS.<Long>get() + "ms instead of " + timeout + "ms");
}
long timeoutTime = startTime + (slowPath ? Baritone.settings().slowPathTimeoutMS.<Long>get() : timeout);
//long lastPrintout = 0;
2018-08-03 09:55:17 -04:00
int numNodes = 0;
2018-08-28 16:15:24 -07:00
int numMovementsConsidered = 0;
2018-08-03 09:55:17 -04:00
int numEmptyChunk = 0;
boolean favoring = favoredPositions.isPresent();
2018-08-23 13:39:13 -07:00
int pathingMaxChunkBorderFetch = Baritone.settings().pathingMaxChunkBorderFetch.get(); // grab all settings beforehand so that changing settings during pathing doesn't cause a crash or unpredictable behavior
2018-08-16 15:10:15 -07:00
double favorCoeff = Baritone.settings().backtrackCostFavoringCoefficient.get();
2018-08-17 19:19:25 -07:00
boolean minimumImprovementRepropagation = Baritone.settings().minimumImprovementRepropagation.get();
2018-09-21 22:14:18 -07:00
loopBegin();
while (!openSet.isEmpty() && numEmptyChunk < pathingMaxChunkBorderFetch && System.nanoTime() / 1000000L - timeoutTime < 0 && !cancelRequested) {
2018-08-14 15:04:41 -07:00
if (slowPath) {
2018-08-03 09:55:17 -04:00
try {
2018-08-16 15:10:15 -07:00
Thread.sleep(Baritone.settings().slowPathTimeDelayMS.<Long>get());
2018-08-03 09:55:17 -04:00
} catch (InterruptedException ex) {
}
2018-08-05 18:53:11 -04:00
}
2018-08-03 09:55:17 -04:00
PathNode currentNode = openSet.removeLowest();
currentNode.isOpen = false;
mostRecentConsidered = currentNode;
2018-08-03 09:55:17 -04:00
numNodes++;
if (goal.isInGoal(currentNode.x, currentNode.y, currentNode.z)) {
2018-09-11 13:45:43 -07:00
logDebug("Took " + (System.nanoTime() / 1000000L - startTime) + "ms, " + numMovementsConsidered + " movements considered");
return Optional.of(new Path(startNode, currentNode, numNodes, goal));
2018-08-03 09:55:17 -04:00
}
2018-09-22 22:00:28 -07:00
for (Moves moves : Moves.values()) {
int newX = currentNode.x + moves.xOffset;
int newZ = currentNode.z + moves.zOffset;
if (newX >> 4 != currentNode.x >> 4 || newZ >> 4 != currentNode.z >> 4) {
2018-08-29 11:29:26 -07:00
// only need to check if the destination is a loaded chunk if it's in a different chunk than the start of the movement
if (!BlockStateInterface.isLoaded(newX, newZ)) {
2018-09-23 14:23:23 -07:00
if (!moves.dynamicXZ) { // only increment the counter if the movement would have gone out of bounds guaranteed
numEmptyChunk++;
}
continue;
2018-08-29 11:29:26 -07:00
}
2018-08-03 22:14:50 -04:00
}
MoveResult res = moves.apply(calcContext, currentNode.x, currentNode.y, currentNode.z);
numMovementsConsidered++;
double actionCost = res.cost;
2018-08-03 09:55:17 -04:00
if (actionCost >= ActionCosts.COST_INF) {
continue;
}
2018-09-24 18:56:49 -07:00
// check destination after verifying it's not COST_INF -- some movements return a static IMPOSSIBLE object with COST_INF and destination being 0,0,0 to avoid allocating a new result for every failed calculation
if (!moves.dynamicXZ && (res.destX != newX || res.destZ != newZ)) {
throw new IllegalStateException(moves + " " + res.destX + " " + newX + " " + res.destZ + " " + newZ);
}
2018-08-06 08:42:26 -07:00
if (actionCost <= 0) {
2018-09-22 22:00:28 -07:00
throw new IllegalStateException(moves + " calculated implausible cost " + actionCost);
2018-08-06 08:42:26 -07:00
}
long hashCode = posHash(res.destX, res.destY, res.destZ);
if (favoring && favored.contains(hashCode)) {
2018-08-17 12:24:40 -07:00
// see issue #18
2018-08-16 15:10:15 -07:00
actionCost *= favorCoeff;
}
PathNode neighbor = getNodeAtPosition(res.destX, res.destY, res.destZ, hashCode);
2018-08-03 09:55:17 -04:00
double tentativeCost = currentNode.cost + actionCost;
if (tentativeCost < neighbor.cost) {
2018-08-06 08:42:26 -07:00
if (tentativeCost < 0) {
2018-09-22 22:00:28 -07:00
throw new IllegalStateException(moves + " overflowed into negative " + actionCost + " " + neighbor.cost + " " + tentativeCost);
2018-08-06 08:42:26 -07:00
}
2018-08-17 19:19:25 -07:00
double improvementBy = neighbor.cost - tentativeCost;
// there are floating point errors caused by random combinations of traverse and diagonal over a flat area
// that means that sometimes there's a cost improvement of like 10 ^ -16
// it's not worth the time to update the costs, decrease-key the heap, potentially repropagate, etc
if (improvementBy < 0.01 && minimumImprovementRepropagation) {
// who cares about a hundredth of a tick? that's half a millisecond for crying out loud!
continue;
}
2018-08-03 09:55:17 -04:00
neighbor.previous = currentNode;
neighbor.cost = tentativeCost;
neighbor.combinedCost = tentativeCost + neighbor.estimatedCostToGoal;
if (neighbor.isOpen) {
openSet.update(neighbor);
} else {
2018-08-03 09:55:17 -04:00
neighbor.isOpen = true;
2018-09-10 09:22:32 -07:00
openSet.insert(neighbor);//dont double count, dont insert into open set if it's already there
2018-08-03 09:55:17 -04:00
}
for (int i = 0; i < bestSoFar.length; i++) {
double heuristic = neighbor.estimatedCostToGoal + neighbor.cost / COEFFICIENTS[i];
if (heuristic < bestHeuristicSoFar[i]) {
2018-09-01 11:19:42 -07:00
if (bestHeuristicSoFar[i] - heuristic < 0.01 && minimumImprovementRepropagation) {
continue;
}
2018-08-03 09:55:17 -04:00
bestHeuristicSoFar[i] = heuristic;
bestSoFar[i] = neighbor;
}
}
}
}
}
2018-08-28 11:17:11 -07:00
if (cancelRequested) {
return Optional.empty();
}
2018-08-28 16:15:24 -07:00
System.out.println(numMovementsConsidered + " movements considered");
2018-08-29 15:35:41 -07:00
System.out.println("Open set size: " + openSet.size());
2018-09-21 22:14:18 -07:00
System.out.println("PathNode map size: " + mapSize());
System.out.println((int) (numNodes * 1.0 / ((System.nanoTime() / 1000000L - startTime) / 1000F)) + " nodes per second");
2018-08-03 09:55:17 -04:00
double bestDist = 0;
for (int i = 0; i < bestSoFar.length; i++) {
if (bestSoFar[i] == null) {
continue;
}
double dist = getDistFromStartSq(bestSoFar[i]);
if (dist > bestDist) {
bestDist = dist;
}
if (dist > MIN_DIST_PATH * MIN_DIST_PATH) { // square the comparison since distFromStartSq is squared
2018-09-11 13:45:43 -07:00
logDebug("Took " + (System.nanoTime() / 1000000L - startTime) + "ms, A* cost coefficient " + COEFFICIENTS[i] + ", " + numMovementsConsidered + " movements considered");
2018-08-03 09:55:17 -04:00
if (COEFFICIENTS[i] >= 3) {
System.out.println("Warning: cost coefficient is greater than three! Probably means that");
System.out.println("the path I found is pretty terrible (like sneak-bridging for dozens of blocks)");
System.out.println("But I'm going to do it anyway, because yolo");
}
2018-08-28 15:53:29 -07:00
System.out.println("Path goes for " + Math.sqrt(dist) + " blocks");
return Optional.of(new Path(startNode, bestSoFar[i], numNodes, goal));
2018-08-03 09:55:17 -04:00
}
}
2018-09-11 13:45:43 -07:00
logDebug("Even with a cost coefficient of " + COEFFICIENTS[COEFFICIENTS.length - 1] + ", I couldn't get more than " + Math.sqrt(bestDist) + " blocks");
logDebug("No path found =(");
return Optional.empty();
2018-08-03 09:55:17 -04:00
}
}