Search in sources :

Example 1 with IPath

use of baritone.api.pathing.calc.IPath in project baritone by cabaletta.

the class AStarPathFinder method calculate0.

@Override
protected Optional<IPath> calculate0(long primaryTimeout, long failureTimeout) {
    startNode = getNodeAtPosition(startX, startY, startZ, BetterBlockPos.longHash(startX, startY, startZ));
    startNode.cost = 0;
    startNode.combinedCost = startNode.estimatedCostToGoal;
    BinaryHeapOpenSet openSet = new BinaryHeapOpenSet();
    openSet.insert(startNode);
    // 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;
    }
    MutableMoveResult res = new MutableMoveResult();
    BetterWorldBorder worldBorder = new BetterWorldBorder(calcContext.world.getWorldBorder());
    long startTime = System.currentTimeMillis();
    boolean slowPath = Baritone.settings().slowPath.value;
    if (slowPath) {
        logDebug("slowPath is on, path timeout will be " + Baritone.settings().slowPathTimeoutMS.value + "ms instead of " + primaryTimeout + "ms");
    }
    long primaryTimeoutTime = startTime + (slowPath ? Baritone.settings().slowPathTimeoutMS.value : primaryTimeout);
    long failureTimeoutTime = startTime + (slowPath ? Baritone.settings().slowPathTimeoutMS.value : failureTimeout);
    boolean failing = true;
    int numNodes = 0;
    int numMovementsConsidered = 0;
    int numEmptyChunk = 0;
    boolean isFavoring = !favoring.isEmpty();
    int timeCheckInterval = 1 << 6;
    // grab all settings beforehand so that changing settings during pathing doesn't cause a crash or unpredictable behavior
    int pathingMaxChunkBorderFetch = Baritone.settings().pathingMaxChunkBorderFetch.value;
    double minimumImprovement = Baritone.settings().minimumImprovementRepropagation.value ? MIN_IMPROVEMENT : 0;
    Moves[] allMoves = Moves.values();
    while (!openSet.isEmpty() && numEmptyChunk < pathingMaxChunkBorderFetch && !cancelRequested) {
        if ((numNodes & (timeCheckInterval - 1)) == 0) {
            // only call this once every 64 nodes (about half a millisecond)
            // since nanoTime is slow on windows (takes many microseconds)
            long now = System.currentTimeMillis();
            if (now - failureTimeoutTime >= 0 || (!failing && now - primaryTimeoutTime >= 0)) {
                break;
            }
        }
        if (slowPath) {
            try {
                Thread.sleep(Baritone.settings().slowPathTimeDelayMS.value);
            } catch (InterruptedException ignored) {
            }
        }
        PathNode currentNode = openSet.removeLowest();
        mostRecentConsidered = currentNode;
        numNodes++;
        if (goal.isInGoal(currentNode.x, currentNode.y, currentNode.z)) {
            logDebug("Took " + (System.currentTimeMillis() - startTime) + "ms, " + numMovementsConsidered + " movements considered");
            return Optional.of(new Path(startNode, currentNode, numNodes, goal, calcContext));
        }
        for (Moves moves : allMoves) {
            int newX = currentNode.x + moves.xOffset;
            int newZ = currentNode.z + moves.zOffset;
            if ((newX >> 4 != currentNode.x >> 4 || newZ >> 4 != currentNode.z >> 4) && !calcContext.isLoaded(newX, newZ)) {
                // 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 (!moves.dynamicXZ) {
                    // only increment the counter if the movement would have gone out of bounds guaranteed
                    numEmptyChunk++;
                }
                continue;
            }
            if (!moves.dynamicXZ && !worldBorder.entirelyContains(newX, newZ)) {
                continue;
            }
            if (currentNode.y + moves.yOffset > 256 || currentNode.y + moves.yOffset < 0) {
                continue;
            }
            res.reset();
            moves.apply(calcContext, currentNode.x, currentNode.y, currentNode.z, res);
            numMovementsConsidered++;
            double actionCost = res.cost;
            if (actionCost >= ActionCosts.COST_INF) {
                continue;
            }
            if (actionCost <= 0 || Double.isNaN(actionCost)) {
                throw new IllegalStateException(moves + " calculated implausible cost " + actionCost);
            }
            // 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 && !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);
            }
            if (!moves.dynamicY && res.y != currentNode.y + moves.yOffset) {
                throw new IllegalStateException(moves + " " + res.y + " " + (currentNode.y + moves.yOffset));
            }
            long hashCode = BetterBlockPos.longHash(res.x, res.y, res.z);
            if (isFavoring) {
                // see issue #18
                actionCost *= favoring.calculate(hashCode);
            }
            PathNode neighbor = getNodeAtPosition(res.x, res.y, res.z, hashCode);
            double tentativeCost = currentNode.cost + actionCost;
            if (neighbor.cost - tentativeCost > minimumImprovement) {
                neighbor.previous = currentNode;
                neighbor.cost = tentativeCost;
                neighbor.combinedCost = tentativeCost + neighbor.estimatedCostToGoal;
                if (neighbor.isOpen()) {
                    openSet.update(neighbor);
                } else {
                    // dont double count, dont insert into open set if it's already there
                    openSet.insert(neighbor);
                }
                for (int i = 0; i < COEFFICIENTS.length; i++) {
                    double heuristic = neighbor.estimatedCostToGoal + neighbor.cost / COEFFICIENTS[i];
                    if (bestHeuristicSoFar[i] - heuristic > minimumImprovement) {
                        bestHeuristicSoFar[i] = heuristic;
                        bestSoFar[i] = neighbor;
                        if (failing && getDistFromStartSq(neighbor) > MIN_DIST_PATH * MIN_DIST_PATH) {
                            failing = false;
                        }
                    }
                }
            }
        }
    }
    if (cancelRequested) {
        return Optional.empty();
    }
    System.out.println(numMovementsConsidered + " movements considered");
    System.out.println("Open set size: " + openSet.size());
    System.out.println("PathNode map size: " + mapSize());
    System.out.println((int) (numNodes * 1.0 / ((System.currentTimeMillis() - startTime) / 1000F)) + " nodes per second");
    Optional<IPath> result = bestSoFar(true, numNodes);
    if (result.isPresent()) {
        logDebug("Took " + (System.currentTimeMillis() - startTime) + "ms, " + numMovementsConsidered + " movements considered");
    }
    return result;
}
Also used : BinaryHeapOpenSet(baritone.pathing.calc.openset.BinaryHeapOpenSet) IPath(baritone.api.pathing.calc.IPath) IPath(baritone.api.pathing.calc.IPath) Moves(baritone.pathing.movement.Moves) BetterWorldBorder(baritone.utils.pathing.BetterWorldBorder) MutableMoveResult(baritone.utils.pathing.MutableMoveResult)

Example 2 with IPath

use of baritone.api.pathing.calc.IPath in project baritone by cabaletta.

the class AbstractNodeCostSearch method calculate.

@Override
public synchronized PathCalculationResult calculate(long primaryTimeout, long failureTimeout) {
    if (isFinished) {
        throw new IllegalStateException("Path finder cannot be reused!");
    }
    cancelRequested = false;
    try {
        IPath path = calculate0(primaryTimeout, failureTimeout).map(IPath::postProcess).orElse(null);
        if (cancelRequested) {
            return new PathCalculationResult(PathCalculationResult.Type.CANCELLATION);
        }
        if (path == null) {
            return new PathCalculationResult(PathCalculationResult.Type.FAILURE);
        }
        int previousLength = path.length();
        path = path.cutoffAtLoadedChunks(context.bsi);
        if (path.length() < previousLength) {
            Helper.HELPER.logDebug("Cutting off path at edge of loaded chunks");
            Helper.HELPER.logDebug("Length decreased by " + (previousLength - path.length()));
        } else {
            Helper.HELPER.logDebug("Path ends within loaded chunks");
        }
        previousLength = path.length();
        path = path.staticCutoff(goal);
        if (path.length() < previousLength) {
            Helper.HELPER.logDebug("Static cutoff " + previousLength + " to " + path.length());
        }
        if (goal.isInGoal(path.getDest())) {
            return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_TO_GOAL, path);
        } else {
            return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_SEGMENT, path);
        }
    } catch (Exception e) {
        Helper.HELPER.logDirect("Pathing exception: " + e);
        e.printStackTrace();
        return new PathCalculationResult(PathCalculationResult.Type.EXCEPTION);
    } finally {
        // this is run regardless of what exception may or may not be raised by calculate0
        isFinished = true;
    }
}
Also used : IPath(baritone.api.pathing.calc.IPath) PathCalculationResult(baritone.api.utils.PathCalculationResult)

Example 3 with IPath

use of baritone.api.pathing.calc.IPath in project Spark-Client by Spark-Client-Development.

the class AbstractNodeCostSearch method bestSoFar.

protected Optional<IPath> bestSoFar(boolean logInfo, int numNodes) {
    if (startNode == null) {
        return Optional.empty();
    }
    double bestDist = 0;
    for (int i = 0; i < COEFFICIENTS.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
            if (logInfo) {
                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");
                }
                System.out.println("Path goes for " + Math.sqrt(dist) + " blocks");
                logDebug("A* cost coefficient " + COEFFICIENTS[i]);
            }
            return Optional.of(new Path(startNode, bestSoFar[i], numNodes, goal, context));
        }
    }
    // if it actually won't find any path, don't make them think it will by rendering a dark blue that will never actually happen
    if (logInfo) {
        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 =(");
        if (Baritone.settings().desktopNotifications.getValue()) {
            NotificationHelper.notify("No path found =(", true);
        }
    }
    return Optional.empty();
}
Also used : IPath(baritone.api.pathing.calc.IPath)

Example 4 with IPath

use of baritone.api.pathing.calc.IPath in project Spark-Client by Spark-Client-Development.

the class AStarPathFinder method calculate0.

@Override
protected Optional<IPath> calculate0(long primaryTimeout, long failureTimeout) {
    startNode = getNodeAtPosition(startX, startY, startZ, BetterBlockPos.longHash(startX, startY, startZ));
    startNode.cost = 0;
    startNode.combinedCost = startNode.estimatedCostToGoal;
    BinaryHeapOpenSet openSet = new BinaryHeapOpenSet();
    openSet.insert(startNode);
    // 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;
    }
    MutableMoveResult res = new MutableMoveResult();
    BetterWorldBorder worldBorder = new BetterWorldBorder(calcContext.world.getWorldBorder());
    long startTime = System.currentTimeMillis();
    boolean slowPath = Baritone.settings().slowPath.getValue();
    if (slowPath) {
        logDebug("slowPath is on, path timeout will be " + Baritone.settings().slowPathTimeoutMS.getValue() + "ms instead of " + primaryTimeout + "ms");
    }
    long primaryTimeoutTime = startTime + (slowPath ? Baritone.settings().slowPathTimeoutMS.getValue() : primaryTimeout);
    long failureTimeoutTime = startTime + (slowPath ? Baritone.settings().slowPathTimeoutMS.getValue() : failureTimeout);
    boolean failing = true;
    int numNodes = 0;
    int numMovementsConsidered = 0;
    int numEmptyChunk = 0;
    boolean isFavoring = !favoring.isEmpty();
    int timeCheckInterval = 1 << 6;
    // grab all settings beforehand so that changing settings during pathing doesn't cause a crash or unpredictable behavior
    int pathingMaxChunkBorderFetch = Baritone.settings().pathingMaxChunkBorderFetch.getValue();
    double minimumImprovement = Baritone.settings().minimumImprovementRepropagation.getValue() ? MIN_IMPROVEMENT : 0;
    Moves[] allMoves = Moves.values();
    while (!openSet.isEmpty() && numEmptyChunk < pathingMaxChunkBorderFetch && !cancelRequested) {
        if ((numNodes & (timeCheckInterval - 1)) == 0) {
            // only call this once every 64 nodes (about half a millisecond)
            // since nanoTime is slow on windows (takes many microseconds)
            long now = System.currentTimeMillis();
            if (now - failureTimeoutTime >= 0 || (!failing && now - primaryTimeoutTime >= 0)) {
                break;
            }
        }
        if (slowPath) {
            try {
                Thread.sleep(Baritone.settings().slowPathTimeDelayMS.getValue());
            } catch (InterruptedException ignored) {
            }
        }
        PathNode currentNode = openSet.removeLowest();
        mostRecentConsidered = currentNode;
        numNodes++;
        if (goal.isInGoal(currentNode.x, currentNode.y, currentNode.z)) {
            logDebug("Took " + (System.currentTimeMillis() - startTime) + "ms, " + numMovementsConsidered + " movements considered");
            return Optional.of(new Path(startNode, currentNode, numNodes, goal, calcContext));
        }
        for (Moves moves : allMoves) {
            int newX = currentNode.x + moves.xOffset;
            int newZ = currentNode.z + moves.zOffset;
            if ((newX >> 4 != currentNode.x >> 4 || newZ >> 4 != currentNode.z >> 4) && !calcContext.isLoaded(newX, newZ)) {
                // 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 (!moves.dynamicXZ) {
                    // only increment the counter if the movement would have gone out of bounds guaranteed
                    numEmptyChunk++;
                }
                continue;
            }
            if (!moves.dynamicXZ && !worldBorder.entirelyContains(newX, newZ)) {
                continue;
            }
            if (currentNode.y + moves.yOffset > 256 || currentNode.y + moves.yOffset < 0) {
                continue;
            }
            res.reset();
            moves.apply(calcContext, currentNode.x, currentNode.y, currentNode.z, res);
            numMovementsConsidered++;
            double actionCost = res.cost;
            if (actionCost >= ActionCosts.COST_INF) {
                continue;
            }
            if (actionCost <= 0 || Double.isNaN(actionCost)) {
                throw new IllegalStateException(moves + " calculated implausible cost " + actionCost);
            }
            // 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 && !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);
            }
            if (!moves.dynamicY && res.y != currentNode.y + moves.yOffset) {
                throw new IllegalStateException(moves + " " + res.y + " " + (currentNode.y + moves.yOffset));
            }
            long hashCode = BetterBlockPos.longHash(res.x, res.y, res.z);
            if (isFavoring) {
                // see issue #18
                actionCost *= favoring.calculate(hashCode);
            }
            PathNode neighbor = getNodeAtPosition(res.x, res.y, res.z, hashCode);
            double tentativeCost = currentNode.cost + actionCost;
            if (neighbor.cost - tentativeCost > minimumImprovement) {
                neighbor.previous = currentNode;
                neighbor.cost = tentativeCost;
                neighbor.combinedCost = tentativeCost + neighbor.estimatedCostToGoal;
                if (neighbor.isOpen()) {
                    openSet.update(neighbor);
                } else {
                    // dont double count, dont insert into open set if it's already there
                    openSet.insert(neighbor);
                }
                for (int i = 0; i < COEFFICIENTS.length; i++) {
                    double heuristic = neighbor.estimatedCostToGoal + neighbor.cost / COEFFICIENTS[i];
                    if (bestHeuristicSoFar[i] - heuristic > minimumImprovement) {
                        bestHeuristicSoFar[i] = heuristic;
                        bestSoFar[i] = neighbor;
                        if (failing && getDistFromStartSq(neighbor) > MIN_DIST_PATH * MIN_DIST_PATH) {
                            failing = false;
                        }
                    }
                }
            }
        }
    }
    if (cancelRequested) {
        return Optional.empty();
    }
    System.out.println(numMovementsConsidered + " movements considered");
    System.out.println("Open set size: " + openSet.size());
    System.out.println("PathNode map size: " + mapSize());
    System.out.println((int) (numNodes * 1.0 / ((System.currentTimeMillis() - startTime) / 1000F)) + " nodes per second");
    Optional<IPath> result = bestSoFar(true, numNodes);
    if (result.isPresent()) {
        logDebug("Took " + (System.currentTimeMillis() - startTime) + "ms, " + numMovementsConsidered + " movements considered");
    }
    return result;
}
Also used : BinaryHeapOpenSet(baritone.pathing.calc.openset.BinaryHeapOpenSet) IPath(baritone.api.pathing.calc.IPath) IPath(baritone.api.pathing.calc.IPath) Moves(baritone.pathing.movement.Moves) BetterWorldBorder(baritone.utils.pathing.BetterWorldBorder) MutableMoveResult(baritone.utils.pathing.MutableMoveResult)

Example 5 with IPath

use of baritone.api.pathing.calc.IPath in project Spark-Client by Spark-Client-Development.

the class AbstractNodeCostSearch method calculate.

@Override
public synchronized PathCalculationResult calculate(long primaryTimeout, long failureTimeout) {
    if (isFinished) {
        throw new IllegalStateException("Path finder cannot be reused!");
    }
    cancelRequested = false;
    try {
        IPath path = calculate0(primaryTimeout, failureTimeout).map(IPath::postProcess).orElse(null);
        if (cancelRequested) {
            return new PathCalculationResult(PathCalculationResult.Type.CANCELLATION);
        }
        if (path == null) {
            return new PathCalculationResult(PathCalculationResult.Type.FAILURE);
        }
        int previousLength = path.length();
        path = path.cutoffAtLoadedChunks(context.bsi);
        if (path.length() < previousLength) {
            Helper.HELPER.logDebug("Cutting off path at edge of loaded chunks");
            Helper.HELPER.logDebug("Length decreased by " + (previousLength - path.length()));
        } else {
            Helper.HELPER.logDebug("Path ends within loaded chunks");
        }
        previousLength = path.length();
        path = path.staticCutoff(goal);
        if (path.length() < previousLength) {
            Helper.HELPER.logDebug("Static cutoff " + previousLength + " to " + path.length());
        }
        if (goal.isInGoal(path.getDest())) {
            return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_TO_GOAL, path);
        } else {
            return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_SEGMENT, path);
        }
    } catch (Exception e) {
        Helper.HELPER.logDirect("Pathing exception: " + e);
        e.printStackTrace();
        return new PathCalculationResult(PathCalculationResult.Type.EXCEPTION);
    } finally {
        // this is run regardless of what exception may or may not be raised by calculate0
        isFinished = true;
    }
}
Also used : IPath(baritone.api.pathing.calc.IPath) PathCalculationResult(baritone.api.utils.PathCalculationResult)

Aggregations

IPath (baritone.api.pathing.calc.IPath)8 BetterBlockPos (baritone.api.utils.BetterBlockPos)2 PathCalculationResult (baritone.api.utils.PathCalculationResult)2 BinaryHeapOpenSet (baritone.pathing.calc.openset.BinaryHeapOpenSet)2 Moves (baritone.pathing.movement.Moves)2 BetterWorldBorder (baritone.utils.pathing.BetterWorldBorder)2 MutableMoveResult (baritone.utils.pathing.MutableMoveResult)2