2018-08-08 03:16:53 +00:00
/ *
* This file is part of Baritone .
*
* Baritone is free software : you can redistribute it and / or modify
2018-09-17 22:11:40 +00:00
* it under the terms of the GNU Lesser General Public License as published by
2018-08-08 03:16:53 +00:00
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
2018-08-08 04:15:22 +00:00
* Baritone is distributed in the hope that it will be useful ,
2018-08-08 03:16:53 +00:00
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
2018-09-17 22:11:40 +00:00
* GNU Lesser General Public License for more details .
2018-08-08 03:16:53 +00:00
*
2018-09-17 22:11:40 +00:00
* You should have received a copy of the GNU Lesser General Public License
2018-08-08 03:16:53 +00:00
* along with Baritone . If not , see < https : //www.gnu.org/licenses/>.
* /
2018-08-22 20:15:56 +00:00
package baritone.pathing.calc ;
2018-08-03 13:55:17 +00:00
2018-08-22 20:15:56 +00:00
import baritone.Baritone ;
2018-10-12 21:12:06 +00:00
import baritone.api.pathing.calc.IPath ;
2018-09-23 23:29:03 +00:00
import baritone.api.pathing.goals.Goal ;
2018-09-25 01:32:39 +00:00
import baritone.api.pathing.movement.ActionCosts ;
2018-10-09 01:37:52 +00:00
import baritone.api.utils.BetterBlockPos ;
2018-09-25 01:56:49 +00:00
import baritone.pathing.calc.openset.BinaryHeapOpenSet ;
2018-08-22 20:15:56 +00:00
import baritone.pathing.movement.CalculationContext ;
2018-09-23 21:23:23 +00:00
import baritone.pathing.movement.Moves ;
2018-09-09 16:50:19 +00:00
import baritone.utils.BlockStateInterface ;
2018-08-22 20:15:56 +00:00
import baritone.utils.Helper ;
2018-10-12 21:12:06 +00:00
import baritone.utils.pathing.BetterWorldBorder ;
2018-10-05 19:24:52 +00:00
import baritone.utils.pathing.MutableMoveResult ;
2018-08-03 13:55:17 +00:00
2018-08-16 22:10:15 +00:00
import java.util.HashSet ;
2018-08-06 07:21:47 +00:00
import java.util.Optional ;
2018-08-03 13:55:17 +00:00
/ * *
* The actual A * pathfinding
*
* @author leijurv
* /
2018-09-23 05:00:28 +00:00
public final class AStarPathFinder extends AbstractNodeCostSearch implements Helper {
2018-08-06 07:21:47 +00:00
2018-09-23 15:52:03 +00:00
private final Optional < HashSet < Long > > favoredPositions ;
2018-08-16 22:10:15 +00:00
2018-10-03 14:57:24 +00:00
public AStarPathFinder ( int startX , int startY , int startZ , Goal goal , Optional < HashSet < Long > > favoredPositions ) {
super ( startX , startY , startZ , goal ) ;
2018-09-23 15:54:26 +00:00
this . favoredPositions = favoredPositions ;
2018-08-03 13:55:17 +00:00
}
@Override
2018-09-02 20:51:38 +00:00
protected Optional < IPath > calculate0 ( long timeout ) {
2018-10-09 01:37:52 +00:00
startNode = getNodeAtPosition ( startX , startY , startZ , BetterBlockPos . longHash ( startX , startY , startZ ) ) ;
2018-08-03 13:55:17 +00:00
startNode . cost = 0 ;
2018-08-03 15:45:11 +00:00
startNode . combinedCost = startNode . estimatedCostToGoal ;
2018-08-29 22:35:41 +00:00
BinaryHeapOpenSet openSet = new BinaryHeapOpenSet ( ) ;
2018-08-03 13:55:17 +00:00
openSet . insert ( startNode ) ;
2018-08-16 22:10:15 +00:00
startNode . isOpen = true ;
2018-08-03 13:55:17 +00: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 + + ) {
2018-09-20 02:34:05 +00:00
bestHeuristicSoFar [ i ] = startNode . estimatedCostToGoal ;
bestSoFar [ i ] = startNode ;
2018-08-03 13:55:17 +00:00
}
2018-08-16 22:10:15 +00:00
CalculationContext calcContext = new CalculationContext ( ) ;
2018-10-05 19:24:52 +00:00
MutableMoveResult res = new MutableMoveResult ( ) ;
2018-09-23 15:52:03 +00:00
HashSet < Long > favored = favoredPositions . orElse ( null ) ;
2018-10-12 21:12:06 +00:00
BetterWorldBorder worldBorder = new BetterWorldBorder ( world ( ) . getWorldBorder ( ) ) ;
2018-09-09 16:50:19 +00:00
BlockStateInterface . clearCachedChunk ( ) ;
2018-08-29 23:22:57 +00:00
long startTime = System . nanoTime ( ) / 1000000L ;
2018-08-14 22:04:41 +00:00
boolean slowPath = Baritone . settings ( ) . slowPath . get ( ) ;
2018-09-02 20:51:38 +00:00
if ( slowPath ) {
2018-09-11 20:45:43 +00:00
logDebug ( " slowPath is on, path timeout will be " + Baritone . settings ( ) . slowPathTimeoutMS . < Long > get ( ) + " ms instead of " + timeout + " ms " ) ;
2018-09-02 20:51:38 +00:00
}
long timeoutTime = startTime + ( slowPath ? Baritone . settings ( ) . slowPathTimeoutMS . < Long > get ( ) : timeout ) ;
2018-08-29 19:19:21 +00:00
//long lastPrintout = 0;
2018-08-03 13:55:17 +00:00
int numNodes = 0 ;
2018-08-28 23:15:24 +00:00
int numMovementsConsidered = 0 ;
2018-08-03 13:55:17 +00:00
int numEmptyChunk = 0 ;
2018-08-19 20:18:48 +00:00
boolean favoring = favoredPositions . isPresent ( ) ;
2018-08-23 20:39:13 +00: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 22:10:15 +00:00
double favorCoeff = Baritone . settings ( ) . backtrackCostFavoringCoefficient . get ( ) ;
2018-08-18 02:19:25 +00:00
boolean minimumImprovementRepropagation = Baritone . settings ( ) . minimumImprovementRepropagation . get ( ) ;
2018-09-22 05:14:18 +00:00
loopBegin ( ) ;
2018-08-29 23:22:57 +00:00
while ( ! openSet . isEmpty ( ) & & numEmptyChunk < pathingMaxChunkBorderFetch & & System . nanoTime ( ) / 1000000L - timeoutTime < 0 & & ! cancelRequested ) {
2018-08-14 22:04:41 +00:00
if ( slowPath ) {
2018-08-03 13:55:17 +00:00
try {
2018-08-16 22:10:15 +00:00
Thread . sleep ( Baritone . settings ( ) . slowPathTimeDelayMS . < Long > get ( ) ) ;
2018-08-03 13:55:17 +00:00
} catch ( InterruptedException ex ) {
}
2018-08-05 22:53:11 +00:00
}
2018-08-03 13:55:17 +00:00
PathNode currentNode = openSet . removeLowest ( ) ;
currentNode . isOpen = false ;
2018-08-05 15:30:50 +00:00
mostRecentConsidered = currentNode ;
2018-08-03 13:55:17 +00:00
numNodes + + ;
2018-09-23 15:52:03 +00:00
if ( goal . isInGoal ( currentNode . x , currentNode . y , currentNode . z ) ) {
2018-09-11 20:45:43 +00:00
logDebug ( " Took " + ( System . nanoTime ( ) / 1000000L - startTime ) + " ms, " + numMovementsConsidered + " movements considered " ) ;
2018-09-14 16:21:52 +00:00
return Optional . of ( new Path ( startNode , currentNode , numNodes , goal ) ) ;
2018-08-03 13:55:17 +00:00
}
2018-09-23 05:00:28 +00:00
for ( Moves moves : Moves . values ( ) ) {
2018-09-23 15:52:03 +00:00
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 18:29:26 +00: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-09-23 17:20:19 +00:00
if ( ! BlockStateInterface . isLoaded ( newX , newZ ) ) {
2018-09-23 21:23:23 +00:00
if ( ! moves . dynamicXZ ) { // only increment the counter if the movement would have gone out of bounds guaranteed
numEmptyChunk + + ;
}
2018-09-23 17:20:19 +00:00
continue ;
2018-08-29 18:29:26 +00:00
}
2018-08-04 02:14:50 +00:00
}
2018-10-12 21:12:06 +00:00
if ( ! moves . dynamicXZ & & ! worldBorder . entirelyContains ( newX , newZ ) ) {
continue ;
}
2018-10-12 22:29:25 +00:00
if ( currentNode . y + moves . yOffset > 256 | | currentNode . y + moves . yOffset < 0 ) {
2018-10-12 21:19:11 +00:00
continue ;
}
2018-10-05 19:24:52 +00:00
res . reset ( ) ;
moves . apply ( calcContext , currentNode . x , currentNode . y , currentNode . z , res ) ;
2018-09-23 15:52:03 +00:00
numMovementsConsidered + + ;
double actionCost = res . cost ;
2018-08-03 13:55:17 +00:00
if ( actionCost > = ActionCosts . COST_INF ) {
continue ;
}
2018-10-12 21:12:06 +00:00
if ( actionCost < = 0 ) {
throw new IllegalStateException ( moves + " calculated implausible cost " + actionCost ) ;
}
if ( moves . dynamicXZ & & ! worldBorder . entirelyContains ( res . x , res . z ) ) { // see issue #218
continue ;
}
2018-09-25 01:56:49 +00: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-05 19:24:52 +00:00
if ( ! moves . dynamicXZ & & ( res . x ! = newX | | res . z ! = newZ ) ) {
throw new IllegalStateException ( moves + " " + res . x + " " + newX + " " + res . z + " " + newZ ) ;
2018-09-25 01:56:49 +00:00
}
2018-10-05 19:24:52 +00:00
if ( ! moves . dynamicY & & res . y ! = currentNode . y + moves . yOffset ) {
throw new IllegalStateException ( moves + " " + res . x + " " + newX + " " + res . z + " " + newZ ) ;
2018-10-05 17:10:24 +00:00
}
2018-10-09 01:37:52 +00:00
long hashCode = BetterBlockPos . longHash ( res . x , res . y , res . z ) ;
2018-09-26 22:32:54 +00:00
if ( favoring & & favored . contains ( hashCode ) ) {
2018-08-17 19:24:40 +00:00
// see issue #18
2018-08-16 22:10:15 +00:00
actionCost * = favorCoeff ;
}
2018-10-05 19:24:52 +00:00
PathNode neighbor = getNodeAtPosition ( res . x , res . y , res . z , hashCode ) ;
2018-08-03 13:55:17 +00:00
double tentativeCost = currentNode . cost + actionCost ;
if ( tentativeCost < neighbor . cost ) {
2018-08-06 15:42:26 +00:00
if ( tentativeCost < 0 ) {
2018-09-23 05:00:28 +00:00
throw new IllegalStateException ( moves + " overflowed into negative " + actionCost + " " + neighbor . cost + " " + tentativeCost ) ;
2018-08-06 15:42:26 +00:00
}
2018-08-18 02:19:25 +00: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 13:55:17 +00:00
neighbor . previous = currentNode ;
neighbor . cost = tentativeCost ;
2018-08-03 15:45:11 +00:00
neighbor . combinedCost = tentativeCost + neighbor . estimatedCostToGoal ;
2018-08-05 15:30:50 +00:00
if ( neighbor . isOpen ) {
openSet . update ( neighbor ) ;
} else {
2018-08-03 13:55:17 +00:00
neighbor . isOpen = true ;
2018-09-10 16:22:32 +00:00
openSet . insert ( neighbor ) ; //dont double count, dont insert into open set if it's already there
2018-08-03 13:55:17 +00:00
}
for ( int i = 0 ; i < bestSoFar . length ; i + + ) {
double heuristic = neighbor . estimatedCostToGoal + neighbor . cost / COEFFICIENTS [ i ] ;
if ( heuristic < bestHeuristicSoFar [ i ] ) {
2018-09-01 18:19:42 +00:00
if ( bestHeuristicSoFar [ i ] - heuristic < 0 . 01 & & minimumImprovementRepropagation ) {
continue ;
}
2018-08-03 13:55:17 +00:00
bestHeuristicSoFar [ i ] = heuristic ;
bestSoFar [ i ] = neighbor ;
}
}
}
}
}
2018-08-28 18:17:11 +00:00
if ( cancelRequested ) {
return Optional . empty ( ) ;
}
2018-08-28 23:15:24 +00:00
System . out . println ( numMovementsConsidered + " movements considered " ) ;
2018-08-29 22:35:41 +00:00
System . out . println ( " Open set size: " + openSet . size ( ) ) ;
2018-09-22 05:14:18 +00:00
System . out . println ( " PathNode map size: " + mapSize ( ) ) ;
2018-08-29 23:22:57 +00:00
System . out . println ( ( int ) ( numNodes * 1 . 0 / ( ( System . nanoTime ( ) / 1000000L - startTime ) / 1000F ) ) + " nodes per second " ) ;
2018-08-03 13:55:17 +00: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 20:45:43 +00:00
logDebug ( " Took " + ( System . nanoTime ( ) / 1000000L - startTime ) + " ms, A* cost coefficient " + COEFFICIENTS [ i ] + " , " + numMovementsConsidered + " movements considered " ) ;
2018-08-03 13:55:17 +00: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 22:53:29 +00:00
System . out . println ( " Path goes for " + Math . sqrt ( dist ) + " blocks " ) ;
2018-09-14 16:21:52 +00:00
return Optional . of ( new Path ( startNode , bestSoFar [ i ] , numNodes , goal ) ) ;
2018-08-03 13:55:17 +00:00
}
}
2018-09-11 20:45:43 +00: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 =( " ) ;
2018-08-06 07:21:47 +00:00
return Optional . empty ( ) ;
2018-08-03 13:55:17 +00:00
}
}