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

180 lines
9.4 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;
2018-10-12 14:12:06 -07:00
import baritone.api.pathing.calc.IPath;
import baritone.api.pathing.goals.Goal;
2018-09-24 20:32:39 -05:00
import baritone.api.pathing.movement.ActionCosts;
2018-10-08 20:37:52 -05:00
import baritone.api.utils.BetterBlockPos;
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-10-12 14:12:06 -07:00
import baritone.utils.pathing.BetterWorldBorder;
2018-12-02 19:25:59 -08:00
import baritone.utils.pathing.Favoring;
import baritone.utils.pathing.MutableMoveResult;
2018-08-03 09:55:17 -04:00
import java.util.Optional;
2018-08-03 09:55:17 -04:00
/**
* The actual A* pathfinding
*
* @author leijurv
*/
2019-01-30 15:49:37 -08:00
public final class AStarPathFinder extends AbstractNodeCostSearch {
2018-12-02 19:25:59 -08:00
private final Favoring favoring;
private final CalculationContext calcContext;
2018-08-16 15:10:15 -07:00
public AStarPathFinder(BetterBlockPos realStart, int startX, int startY, int startZ, Goal goal, Favoring favoring, CalculationContext context) {
super(realStart, startX, startY, startZ, goal, context);
2018-12-02 19:25:59 -08:00
this.favoring = favoring;
this.calcContext = context;
2018-08-03 09:55:17 -04:00
}
@Override
2018-11-22 09:39:54 -08:00
protected Optional<IPath> calculate0(long primaryTimeout, long failureTimeout) {
int minY = calcContext.world.dimensionType().minY();
int height = calcContext.world.dimensionType().height();
2018-10-08 20:37:52 -05:00
startNode = getNodeAtPosition(startX, startY, startZ, BetterBlockPos.longHash(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);
2019-01-30 15:49:37 -08:00
double[] bestHeuristicSoFar = new double[COEFFICIENTS.length];//keep track of the best node by the metric of (estimatedCostToGoal + cost / COEFFICIENTS[i])
2018-08-03 09:55:17 -04:00
for (int i = 0; i < bestHeuristicSoFar.length; i++) {
bestHeuristicSoFar[i] = startNode.estimatedCostToGoal;
bestSoFar[i] = startNode;
2018-08-03 09:55:17 -04:00
}
MutableMoveResult res = new MutableMoveResult();
2018-12-20 21:22:18 -08:00
BetterWorldBorder worldBorder = new BetterWorldBorder(calcContext.world.getWorldBorder());
long startTime = System.currentTimeMillis();
2019-03-04 21:30:04 -08:00
boolean slowPath = Baritone.settings().slowPath.value;
if (slowPath) {
2019-03-04 21:30:04 -08:00
logDebug("slowPath is on, path timeout will be " + Baritone.settings().slowPathTimeoutMS.value + "ms instead of " + primaryTimeout + "ms");
}
2019-03-04 21:30:04 -08:00
long primaryTimeoutTime = startTime + (slowPath ? Baritone.settings().slowPathTimeoutMS.value : primaryTimeout);
long failureTimeoutTime = startTime + (slowPath ? Baritone.settings().slowPathTimeoutMS.value : failureTimeout);
2018-11-22 09:39:54 -08:00
boolean failing = true;
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;
2019-01-30 15:49:37 -08:00
boolean isFavoring = !favoring.isEmpty();
2018-12-18 16:16:32 -08:00
int timeCheckInterval = 1 << 6;
2019-03-04 21:30:04 -08:00
int pathingMaxChunkBorderFetch = Baritone.settings().pathingMaxChunkBorderFetch.value; // grab all settings beforehand so that changing settings during pathing doesn't cause a crash or unpredictable behavior
double minimumImprovement = Baritone.settings().minimumImprovementRepropagation.value ? MIN_IMPROVEMENT : 0;
2019-08-15 03:19:19 -05:00
Moves[] allMoves = Moves.values();
2018-11-22 09:39:54 -08:00
while (!openSet.isEmpty() && numEmptyChunk < pathingMaxChunkBorderFetch && !cancelRequested) {
2018-12-18 16:16:32 -08:00
if ((numNodes & (timeCheckInterval - 1)) == 0) { // only call this once every 64 nodes (about half a millisecond)
long now = System.currentTimeMillis(); // since nanoTime is slow on windows (takes many microseconds)
2021-10-09 14:55:20 -06:00
if (now - failureTimeoutTime >= 0 || (!failing && now - primaryTimeoutTime >= 0)) {
break;
}
2018-11-22 09:39:54 -08:00
}
2018-08-14 15:04:41 -07:00
if (slowPath) {
2018-08-03 09:55:17 -04:00
try {
2019-03-04 21:30:04 -08:00
Thread.sleep(Baritone.settings().slowPathTimeDelayMS.value);
2019-06-06 04:15:43 -05:00
} catch (InterruptedException ignored) {}
2018-08-05 18:53:11 -04:00
}
2018-08-03 09:55:17 -04:00
PathNode currentNode = openSet.removeLowest();
mostRecentConsidered = currentNode;
2018-08-03 09:55:17 -04:00
numNodes++;
if (goal.isInGoal(currentNode.x, currentNode.y, currentNode.z)) {
logDebug("Took " + (System.currentTimeMillis() - startTime) + "ms, " + numMovementsConsidered + " movements considered");
return Optional.of(new Path(realStart, startNode, currentNode, numNodes, goal, calcContext));
2018-08-03 09:55:17 -04:00
}
2019-08-15 03:19:19 -05:00
for (Moves moves : allMoves) {
int newX = currentNode.x + moves.xOffset;
int newZ = currentNode.z + moves.zOffset;
2018-11-11 17:36:54 -08:00
if ((newX >> 4 != currentNode.x >> 4 || newZ >> 4 != currentNode.z >> 4) && !calcContext.isLoaded(newX, newZ)) {
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
2018-10-27 14:41:25 -07:00
if (!moves.dynamicXZ) { // only increment the counter if the movement would have gone out of bounds guaranteed
numEmptyChunk++;
2018-08-29 11:29:26 -07:00
}
2018-10-27 14:41:25 -07:00
continue;
2018-08-03 22:14:50 -04:00
}
2018-10-12 14:12:06 -07:00
if (!moves.dynamicXZ && !worldBorder.entirelyContains(newX, newZ)) {
continue;
}
if (currentNode.y + moves.yOffset > height || currentNode.y + moves.yOffset < minY) {
continue;
}
res.reset();
moves.apply(calcContext, currentNode.x, currentNode.y, currentNode.z, res);
numMovementsConsidered++;
double actionCost = res.cost;
2018-08-03 09:55:17 -04:00
if (actionCost >= ActionCosts.COST_INF) {
continue;
}
if (actionCost <= 0 || Double.isNaN(actionCost)) {
2018-10-12 14:12:06 -07:00
throw new IllegalStateException(moves + " calculated implausible cost " + actionCost);
}
2019-01-12 22:33:46 -08: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
2018-10-12 14:12:06 -07:00
if (moves.dynamicXZ && !worldBorder.entirelyContains(res.x, res.z)) { // see issue #218
continue;
}
if (!moves.dynamicXZ && (res.x != newX || res.z != newZ)) {
throw new IllegalStateException(moves + " " + res.x + " " + newX + " " + res.z + " " + newZ);
2018-09-24 18:56:49 -07:00
}
if (!moves.dynamicY && res.y != currentNode.y + moves.yOffset) {
2018-11-01 15:30:33 -07:00
throw new IllegalStateException(moves + " " + res.y + " " + (currentNode.y + moves.yOffset));
2018-10-05 10:10:24 -07:00
}
2018-10-08 20:37:52 -05:00
long hashCode = BetterBlockPos.longHash(res.x, res.y, res.z);
2019-01-30 15:49:37 -08:00
if (isFavoring) {
2018-08-17 12:24:40 -07:00
// see issue #18
2019-01-30 15:49:37 -08:00
actionCost *= favoring.calculate(hashCode);
2018-08-16 15:10:15 -07:00
}
PathNode neighbor = getNodeAtPosition(res.x, res.y, res.z, hashCode);
2018-08-03 09:55:17 -04:00
double tentativeCost = currentNode.cost + actionCost;
2019-01-30 15:49:37 -08:00
if (neighbor.cost - tentativeCost > minimumImprovement) {
2018-08-03 09:55:17 -04:00
neighbor.previous = currentNode;
neighbor.cost = tentativeCost;
neighbor.combinedCost = tentativeCost + neighbor.estimatedCostToGoal;
2018-12-19 12:59:07 -08:00
if (neighbor.isOpen()) {
openSet.update(neighbor);
} else {
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
}
2019-01-30 15:49:37 -08:00
for (int i = 0; i < COEFFICIENTS.length; i++) {
2018-08-03 09:55:17 -04:00
double heuristic = neighbor.estimatedCostToGoal + neighbor.cost / COEFFICIENTS[i];
2019-01-30 15:49:37 -08:00
if (bestHeuristicSoFar[i] - heuristic > minimumImprovement) {
2018-08-03 09:55:17 -04:00
bestHeuristicSoFar[i] = heuristic;
bestSoFar[i] = neighbor;
2019-02-03 19:46:59 -08:00
if (failing && getDistFromStartSq(neighbor) > MIN_DIST_PATH * MIN_DIST_PATH) {
2018-11-22 09:39:54 -08:00
failing = false;
}
2018-08-03 09:55:17 -04:00
}
}
}
}
}
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.currentTimeMillis() - startTime) / 1000F)) + " nodes per second");
2019-01-30 15:49:37 -08:00
Optional<IPath> result = bestSoFar(true, numNodes);
if (result.isPresent()) {
logDebug("Took " + (System.currentTimeMillis() - startTime) + "ms, " + numMovementsConsidered + " movements considered");
2018-08-03 09:55:17 -04:00
}
2019-01-30 15:49:37 -08:00
return result;
2018-08-03 09:55:17 -04:00
}
}