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
2018-09-17 17:11:40 -05:00
* 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 .
*
2018-08-07 23:15:22 -05:00
* 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
2018-09-17 17:11:40 -05:00
* GNU Lesser General Public License for more details .
2018-08-07 22:16:53 -05:00
*
2018-09-17 17:11:40 -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 ;
2018-09-23 18:29:03 -05:00
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-08-22 13:15:56 -07:00
import baritone.utils.Helper ;
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 ;
2018-10-05 12:24:52 -07:00
import baritone.utils.pathing.MutableMoveResult ;
2018-08-03 09:55:17 -04:00
2018-08-06 02:21:47 -05:00
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 {
2018-08-06 02:21:47 -05:00
2018-12-02 19:25:59 -08:00
private final Favoring favoring ;
2018-10-29 18:58:52 -07:00
private final CalculationContext calcContext ;
2018-08-16 15:10:15 -07:00
2018-12-02 19:25:59 -08:00
public AStarPathFinder ( int startX , int startY , int startZ , Goal goal , Favoring favoring , CalculationContext context ) {
2018-11-09 17:21:02 -08:00
super ( startX , startY , startZ , goal , context ) ;
2018-12-02 19:25:59 -08:00
this . favoring = favoring ;
2018-10-29 18:58:52 -07:00
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 ) {
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 ;
2018-08-03 11:45:11 -04:00
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 ) ;
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-19 19:34:05 -07:00
bestHeuristicSoFar [ i ] = startNode . estimatedCostToGoal ;
bestSoFar [ i ] = startNode ;
2018-08-03 09:55:17 -04:00
}
2018-10-05 12:24:52 -07:00
MutableMoveResult res = new MutableMoveResult ( ) ;
2018-12-02 19:25:59 -08:00
Favoring favored = favoring ;
2018-12-20 21:22:18 -08:00
BetterWorldBorder worldBorder = new BetterWorldBorder ( calcContext . world . getWorldBorder ( ) ) ;
2018-08-29 18:22:57 -05:00
long startTime = System . nanoTime ( ) / 1000000L ;
2018-08-14 15:04:41 -07:00
boolean slowPath = Baritone . settings ( ) . slowPath . get ( ) ;
2018-09-02 13:51:38 -07:00
if ( slowPath ) {
2018-11-22 09:39:54 -08:00
logDebug ( " slowPath is on, path timeout will be " + Baritone . settings ( ) . slowPathTimeoutMS . < Long > get ( ) + " ms instead of " + primaryTimeout + " ms " ) ;
2018-09-02 13:51:38 -07:00
}
2018-11-22 09:39:54 -08:00
long primaryTimeoutTime = startTime + ( slowPath ? Baritone . settings ( ) . slowPathTimeoutMS . < Long > get ( ) : primaryTimeout ) ;
long failureTimeoutTime = startTime + ( slowPath ? Baritone . settings ( ) . slowPathTimeoutMS . < Long > get ( ) : failureTimeout ) ;
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 ;
2018-12-02 19:39:10 -08:00
boolean favoring = ! favored . isEmpty ( ) ;
2018-12-18 16:16:32 -08:00
int timeCheckInterval = 1 < < 6 ;
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-17 19:19:25 -07:00
boolean minimumImprovementRepropagation = Baritone . settings ( ) . minimumImprovementRepropagation . get ( ) ;
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 . nanoTime ( ) / 1000000L ; // since nanoTime is slow on windows (takes many microseconds)
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 {
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 ( ) ;
2018-08-05 11:30:50 -04:00
mostRecentConsidered = currentNode ;
2018-08-03 09:55:17 -04:00
numNodes + + ;
2018-09-23 08:52:03 -07:00
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 " ) ;
2018-11-09 17:21:02 -08:00
return Optional . of ( new Path ( startNode , currentNode , numNodes , goal , calcContext ) ) ;
2018-08-03 09:55:17 -04:00
}
2018-09-22 22:00:28 -07:00
for ( Moves moves : Moves . values ( ) ) {
2018-09-23 08:52:03 -07:00
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 ;
}
2018-10-12 15:29:25 -07:00
if ( currentNode . y + moves . yOffset > 256 | | currentNode . y + moves . yOffset < 0 ) {
2018-10-12 14:19:11 -07:00
continue ;
}
2018-10-05 12:24:52 -07:00
res . reset ( ) ;
moves . apply ( calcContext , currentNode . x , currentNode . y , currentNode . z , res ) ;
2018-09-23 08:52:03 -07:00
numMovementsConsidered + + ;
double actionCost = res . cost ;
2018-08-03 09:55:17 -04:00
if ( actionCost > = ActionCosts . COST_INF ) {
continue ;
}
2018-12-04 18:12:04 -08:00
if ( actionCost < = 0 | | Double . isNaN ( actionCost ) ) {
2018-10-12 14:12:06 -07:00
throw new IllegalStateException ( moves + " calculated implausible cost " + actionCost ) ;
}
if ( moves . dynamicXZ & & ! worldBorder . entirelyContains ( res . x , res . z ) ) { // see issue #218
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
2018-10-05 12:24:52 -07:00
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
}
2018-10-05 12:24:52 -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 ) ;
2018-12-02 18:27:49 -08:00
if ( favoring ) {
2018-08-17 12:24:40 -07:00
// see issue #18
2018-12-02 19:39:10 -08:00
actionCost * = favored . calculate ( hashCode ) ;
2018-08-16 15:10:15 -07:00
}
2018-10-05 12:24:52 -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 ;
if ( tentativeCost < neighbor . cost ) {
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 ;
2018-08-03 11:45:11 -04:00
neighbor . combinedCost = tentativeCost + neighbor . estimatedCostToGoal ;
2018-12-19 12:59:07 -08:00
if ( neighbor . isOpen ( ) ) {
2018-08-05 11:30:50 -04:00
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
}
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-11-22 09:39:54 -08:00
if ( getDistFromStartSq ( neighbor ) > MIN_DIST_PATH * MIN_DIST_PATH ) {
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 ( ) ) ;
2018-08-29 18:22:57 -05:00
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 " ) ;
2018-11-09 17:21:02 -08:00
return Optional . of ( new Path ( startNode , bestSoFar [ i ] , numNodes , goal , calcContext ) ) ;
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 =( " ) ;
2018-08-06 02:21:47 -05:00
return Optional . empty ( ) ;
2018-08-03 09:55:17 -04:00
}
}