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

251 lines
13 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 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,
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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* 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-08-22 15:35:32 -07:00
import baritone.chunk.WorldProvider;
2018-08-22 13:15:56 -07:00
import baritone.pathing.calc.openset.BinaryHeapOpenSet;
import baritone.pathing.calc.openset.IOpenSet;
import baritone.pathing.goals.Goal;
import baritone.pathing.movement.ActionCosts;
import baritone.pathing.movement.CalculationContext;
import baritone.pathing.movement.Movement;
import baritone.pathing.movement.MovementHelper;
import baritone.pathing.movement.movements.*;
import baritone.pathing.path.IPath;
import baritone.utils.Helper;
import baritone.utils.pathing.BetterBlockPos;
2018-08-03 09:55:17 -04:00
import net.minecraft.client.Minecraft;
2018-08-06 19:48:09 -07:00
import net.minecraft.util.EnumFacing;
2018-08-03 09:55:17 -04:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.chunk.EmptyChunk;
2018-08-16 15:10:15 -07:00
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
2018-08-03 09:55:17 -04:00
import java.util.Random;
/**
* The actual A* pathfinding
*
* @author leijurv
*/
2018-08-11 15:03:14 -07:00
public class AStarPathFinder extends AbstractNodeCostSearch implements Helper {
2018-08-16 15:10:15 -07:00
private final Optional<HashSet<BetterBlockPos>> favoredPositions;
public AStarPathFinder(BlockPos start, Goal goal, Optional<Collection<BetterBlockPos>> favoredPositions) {
2018-08-03 09:55:17 -04:00
super(start, goal);
2018-08-16 15:10:15 -07:00
this.favoredPositions = favoredPositions.map(HashSet::new); // <-- okay this is epic
2018-08-03 09:55:17 -04:00
}
@Override
protected Optional<IPath> calculate0() {
2018-08-03 09:55:17 -04:00
startNode = getNodeAtPosition(start);
startNode.cost = 0;
startNode.combinedCost = startNode.estimatedCostToGoal;
IOpenSet 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] = Double.MAX_VALUE;
}
2018-08-16 15:10:15 -07:00
CalculationContext calcContext = new CalculationContext();
HashSet<BetterBlockPos> favored = favoredPositions.orElse(null);
2018-08-03 09:55:17 -04:00
currentlyRunning = this;
long startTime = System.currentTimeMillis();
2018-08-14 15:04:41 -07:00
boolean slowPath = Baritone.settings().slowPath.get();
2018-08-16 15:10:15 -07:00
long timeoutTime = startTime + (slowPath ? Baritone.settings().slowPathTimeoutMS : Baritone.settings().pathTimeoutMS).<Long>get();
2018-08-03 09:55:17 -04:00
long lastPrintout = 0;
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-08-28 11:17:11 -07:00
while (!openSet.isEmpty() && numEmptyChunk < pathingMaxChunkBorderFetch && System.currentTimeMillis() < timeoutTime && !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-14 10:47:31 -07:00
BetterBlockPos currentNodePos = currentNode.pos;
2018-08-03 09:55:17 -04:00
numNodes++;
if (System.currentTimeMillis() > lastPrintout + 1000) {//print once a second
System.out.println("searching... at " + currentNodePos + ", considered " + numNodes + " nodes so far");
lastPrintout = System.currentTimeMillis();
}
if (goal.isInGoal(currentNodePos)) {
currentlyRunning = null;
2018-08-14 10:50:41 -07:00
return Optional.of(new Path(startNode, currentNode, numNodes));
2018-08-03 09:55:17 -04:00
}
//long constructStart = System.nanoTime();
2018-08-16 15:10:15 -07:00
Movement[] possibleMovements = getConnectedPositions(currentNodePos, calcContext);//movement that we could take that start at currentNodePos, in random order
2018-08-03 09:55:17 -04:00
shuffle(possibleMovements);
//long constructEnd = System.nanoTime();
//System.out.println(constructEnd - constructStart);
for (Movement movementToGetToNeighbor : possibleMovements) {
2018-08-06 19:48:09 -07:00
if (movementToGetToNeighbor == null) {
continue;
}
2018-08-16 15:10:15 -07:00
BetterBlockPos dest = (BetterBlockPos) movementToGetToNeighbor.getDest();
2018-08-07 19:41:13 -05:00
boolean isPositionCached = false;
2018-08-23 13:39:13 -07:00
if (WorldProvider.INSTANCE.getCurrentWorld() != null) {
if (WorldProvider.INSTANCE.getCurrentWorld().cache.getBlock(dest) != null) {
isPositionCached = true;
2018-08-13 20:17:16 -07:00
}
}
2018-08-16 15:10:15 -07:00
if (!isPositionCached && Minecraft.getMinecraft().world.getChunk(dest) instanceof EmptyChunk) {
2018-08-03 22:14:50 -04:00
numEmptyChunk++;
continue;
}
2018-08-03 09:55:17 -04:00
//long costStart = System.nanoTime();
// TODO cache cost
2018-08-08 15:41:58 -07:00
double actionCost = movementToGetToNeighbor.getCost(calcContext);
2018-08-03 09:55:17 -04:00
//long costEnd = System.nanoTime();
//System.out.println(movementToGetToNeighbor.getClass() + "" + (costEnd - costStart));
2018-08-28 16:15:24 -07:00
numMovementsConsidered++;
2018-08-03 09:55:17 -04:00
if (actionCost >= ActionCosts.COST_INF) {
continue;
}
2018-08-06 08:42:26 -07:00
if (actionCost <= 0) {
throw new IllegalStateException(movementToGetToNeighbor.getClass() + " " + movementToGetToNeighbor + " calculated implausible cost " + actionCost);
}
2018-08-16 15:10:15 -07:00
if (favoring && favored.contains(dest)) {
2018-08-17 12:24:40 -07:00
// see issue #18
2018-08-16 15:10:15 -07:00
actionCost *= favorCoeff;
}
PathNode neighbor = getNodeAtPosition(dest);
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) {
throw new IllegalStateException(movementToGetToNeighbor.getClass() + " " + movementToGetToNeighbor + " overflowed into negative " + actionCost + " " + neighbor.cost + " " + tentativeCost);
}
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.previousMovement = movementToGetToNeighbor;
neighbor.cost = tentativeCost;
neighbor.combinedCost = tentativeCost + neighbor.estimatedCostToGoal;
if (neighbor.isOpen) {
openSet.update(neighbor);
} else {
2018-08-03 09:55:17 -04:00
openSet.insert(neighbor);//dont double count, dont insert into open set if it's already there
neighbor.isOpen = true;
}
for (int i = 0; i < bestSoFar.length; i++) {
double heuristic = neighbor.estimatedCostToGoal + neighbor.cost / COEFFICIENTS[i];
if (heuristic < bestHeuristicSoFar[i]) {
bestHeuristicSoFar[i] = heuristic;
bestSoFar[i] = neighbor;
}
}
}
}
}
2018-08-28 11:17:11 -07:00
if (cancelRequested) {
currentlyRunning = null;
return Optional.empty();
}
2018-08-28 16:15:24 -07:00
System.out.println(numMovementsConsidered + " movements considered");
System.out.println("Open set size: " + ((BinaryHeapOpenSet) openSet).size());
System.out.println((int) (numNodes * 1.0 / ((System.currentTimeMillis() - 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-08-17 19:19:25 -07:00
displayChatMessageRaw("Took " + (System.currentTimeMillis() - startTime) + "ms, A* cost coefficient " + COEFFICIENTS[i]);
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");
2018-08-03 09:55:17 -04:00
currentlyRunning = null;
2018-08-14 10:50:41 -07:00
return Optional.of(new Path(startNode, bestSoFar[i], numNodes));
2018-08-03 09:55:17 -04:00
}
}
2018-08-27 13:51:22 -05:00
displayChatMessageRaw("Even with a cost coefficient of " + COEFFICIENTS[COEFFICIENTS.length - 1] + ", I couldn't get more than " + bestDist + " blocks");
2018-08-11 15:03:14 -07:00
displayChatMessageRaw("No path found =(");
2018-08-03 09:55:17 -04:00
currentlyRunning = null;
return Optional.empty();
2018-08-03 09:55:17 -04:00
}
public static Movement[] getConnectedPositions(BetterBlockPos pos, CalculationContext calcContext) {
2018-08-03 09:55:17 -04:00
int x = pos.getX();
int y = pos.getY();
int z = pos.getZ();
2018-08-14 10:47:31 -07:00
BetterBlockPos east = new BetterBlockPos(x + 1, y, z);
BetterBlockPos west = new BetterBlockPos(x - 1, y, z);
BetterBlockPos south = new BetterBlockPos(x, y, z + 1);
BetterBlockPos north = new BetterBlockPos(x, y, z - 1);
2018-08-08 15:41:58 -07:00
return new Movement[]{
2018-08-14 10:47:31 -07:00
new MovementTraverse(pos, east),
new MovementTraverse(pos, west),
new MovementTraverse(pos, north),
new MovementTraverse(pos, south),
new MovementAscend(pos, new BetterBlockPos(x + 1, y + 1, z)),
new MovementAscend(pos, new BetterBlockPos(x - 1, y + 1, z)),
new MovementAscend(pos, new BetterBlockPos(x, y + 1, z + 1)),
new MovementAscend(pos, new BetterBlockPos(x, y + 1, z - 1)),
MovementHelper.generateMovementFallOrDescend(pos, east, calcContext),
MovementHelper.generateMovementFallOrDescend(pos, west, calcContext),
MovementHelper.generateMovementFallOrDescend(pos, north, calcContext),
MovementHelper.generateMovementFallOrDescend(pos, south, calcContext),
new MovementDownward(pos, new BetterBlockPos(x, y - 1, z)),
2018-08-07 22:03:41 -05:00
new MovementDiagonal(pos, EnumFacing.NORTH, EnumFacing.WEST),
new MovementDiagonal(pos, EnumFacing.NORTH, EnumFacing.EAST),
new MovementDiagonal(pos, EnumFacing.SOUTH, EnumFacing.WEST),
2018-08-11 18:45:02 -07:00
new MovementDiagonal(pos, EnumFacing.SOUTH, EnumFacing.EAST),
2018-08-14 10:47:31 -07:00
new MovementPillar(pos, new BetterBlockPos(x, y + 1, z))
2018-08-07 22:03:41 -05:00
};
2018-08-03 09:55:17 -04:00
}
private final Random random = new Random();
private <T> void shuffle(T[] list) {
int len = list.length;
for (int i = 0; i < len; i++) {
int j = random.nextInt(len);
T t = list[j];
list[j] = list[i];
list[i] = t;
}
}
}