Search in sources :

Example 1 with PathCalculationResult

use of baritone.api.utils.PathCalculationResult in project baritone by cabaletta.

the class PathingBehavior method findPathInNewThread.

/**
 * In a new thread, pathfind to target blockpos
 *
 * @param start
 * @param talkAboutIt
 */
private void findPathInNewThread(final BlockPos start, final boolean talkAboutIt, CalculationContext context) {
    // actually, we can check this, muahaha
    if (!Thread.holdsLock(pathCalcLock)) {
        throw new IllegalStateException("Must be called with synchronization on pathCalcLock");
    // why do it this way? it's already indented so much that putting the whole thing in a synchronized(pathCalcLock) was just too much lol
    }
    if (inProgress != null) {
        // should have been checked by caller
        throw new IllegalStateException("Already doing it");
    }
    if (!context.safeForThreadedUse) {
        throw new IllegalStateException("Improper context thread safety level");
    }
    Goal goal = this.goal;
    if (goal == null) {
        // TODO should this be an exception too? definitely should be checked by caller
        logDebug("no goal");
        return;
    }
    long primaryTimeout;
    long failureTimeout;
    if (current == null) {
        primaryTimeout = Baritone.settings().primaryTimeoutMS.value;
        failureTimeout = Baritone.settings().failureTimeoutMS.value;
    } else {
        primaryTimeout = Baritone.settings().planAheadPrimaryTimeoutMS.value;
        failureTimeout = Baritone.settings().planAheadFailureTimeoutMS.value;
    }
    AbstractNodeCostSearch pathfinder = createPathfinder(start, goal, current == null ? null : current.getPath(), context);
    if (!Objects.equals(pathfinder.getGoal(), goal)) {
        // will return the exact same object if simplification didn't happen
        logDebug("Simplifying " + goal.getClass() + " to GoalXZ due to distance");
    }
    inProgress = pathfinder;
    Baritone.getExecutor().execute(() -> {
        if (talkAboutIt) {
            logDebug("Starting to search for path from " + start + " to " + goal);
        }
        PathCalculationResult calcResult = pathfinder.calculate(primaryTimeout, failureTimeout);
        synchronized (pathPlanLock) {
            Optional<PathExecutor> executor = calcResult.getPath().map(p -> new PathExecutor(PathingBehavior.this, p));
            if (current == null) {
                if (executor.isPresent()) {
                    if (executor.get().getPath().positions().contains(expectedSegmentStart)) {
                        queuePathEvent(PathEvent.CALC_FINISHED_NOW_EXECUTING);
                        current = executor.get();
                        resetEstimatedTicksToGoal(start);
                    } else {
                        logDebug("Warning: discarding orphan path segment with incorrect start");
                    }
                } else {
                    if (calcResult.getType() != PathCalculationResult.Type.CANCELLATION && calcResult.getType() != PathCalculationResult.Type.EXCEPTION) {
                        // don't dispatch CALC_FAILED on cancellation
                        queuePathEvent(PathEvent.CALC_FAILED);
                    }
                }
            } else {
                if (next == null) {
                    if (executor.isPresent()) {
                        if (executor.get().getPath().getSrc().equals(current.getPath().getDest())) {
                            queuePathEvent(PathEvent.NEXT_SEGMENT_CALC_FINISHED);
                            next = executor.get();
                        } else {
                            logDebug("Warning: discarding orphan next segment with incorrect start");
                        }
                    } else {
                        queuePathEvent(PathEvent.NEXT_CALC_FAILED);
                    }
                } else {
                    // throw new IllegalStateException("I have no idea what to do with this path");
                    // no point in throwing an exception here, and it gets it stuck with inProgress being not null
                    logDirect("Warning: PathingBehaivor illegal state! Discarding invalid path!");
                }
            }
            if (talkAboutIt && current != null && current.getPath() != null) {
                if (goal.isInGoal(current.getPath().getDest())) {
                    logDebug("Finished finding a path from " + start + " to " + goal + ". " + current.getPath().getNumNodesConsidered() + " nodes considered");
                } else {
                    logDebug("Found path segment from " + start + " towards " + goal + ". " + current.getPath().getNumNodesConsidered() + " nodes considered");
                }
            }
            synchronized (pathCalcLock) {
                inProgress = null;
            }
        }
    });
}
Also used : Goal(baritone.api.pathing.goals.Goal) PathExecutor(baritone.pathing.path.PathExecutor) AbstractNodeCostSearch(baritone.pathing.calc.AbstractNodeCostSearch) PathCalculationResult(baritone.api.utils.PathCalculationResult)

Example 2 with PathCalculationResult

use of baritone.api.utils.PathCalculationResult 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 PathCalculationResult

use of baritone.api.utils.PathCalculationResult 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)

Example 4 with PathCalculationResult

use of baritone.api.utils.PathCalculationResult in project Spark-Client by Spark-Client-Development.

the class PathingBehavior method findPathInNewThread.

/**
 * In a new thread, pathfind to target blockpos
 *
 * @param start
 * @param talkAboutIt
 */
private void findPathInNewThread(final BlockPos start, final boolean talkAboutIt, CalculationContext context) {
    // actually, we can check this, muahaha
    if (!Thread.holdsLock(pathCalcLock)) {
        throw new IllegalStateException("Must be called with synchronization on pathCalcLock");
    // why do it this way? it's already indented so much that putting the whole thing in a synchronized(pathCalcLock) was just too much lol
    }
    if (inProgress != null) {
        // should have been checked by caller
        throw new IllegalStateException("Already doing it");
    }
    if (!context.safeForThreadedUse) {
        throw new IllegalStateException("Improper context thread safety level");
    }
    Goal goal = this.goal;
    if (goal == null) {
        // TODO should this be an exception too? definitely should be checked by caller
        logDebug("no goal");
        return;
    }
    long primaryTimeout;
    long failureTimeout;
    if (current == null) {
        primaryTimeout = Baritone.settings().primaryTimeoutMS.getValue();
        failureTimeout = Baritone.settings().failureTimeoutMS.getValue();
    } else {
        primaryTimeout = Baritone.settings().planAheadPrimaryTimeoutMS.getValue();
        failureTimeout = Baritone.settings().planAheadFailureTimeoutMS.getValue();
    }
    AbstractNodeCostSearch pathfinder = createPathfinder(start, goal, current == null ? null : current.getPath(), context);
    if (!Objects.equals(pathfinder.getGoal(), goal)) {
        // will return the exact same object if simplification didn't happen
        logDebug("Simplifying " + goal.getClass() + " to GoalXZ due to distance");
    }
    inProgress = pathfinder;
    Baritone.getExecutor().execute(() -> {
        if (talkAboutIt) {
            logDebug("Starting to search for path from " + start + " to " + goal);
        }
        PathCalculationResult calcResult = pathfinder.calculate(primaryTimeout, failureTimeout);
        synchronized (pathPlanLock) {
            Optional<PathExecutor> executor = calcResult.getPath().map(p -> new PathExecutor(PathingBehavior.this, p));
            if (current == null) {
                if (executor.isPresent()) {
                    if (executor.get().getPath().positions().contains(expectedSegmentStart)) {
                        queuePathEvent(PathEvent.CALC_FINISHED_NOW_EXECUTING);
                        current = executor.get();
                        resetEstimatedTicksToGoal(start);
                    } else {
                        logDebug("Warning: discarding orphan path segment with incorrect start");
                    }
                } else {
                    if (calcResult.getType() != PathCalculationResult.Type.CANCELLATION && calcResult.getType() != PathCalculationResult.Type.EXCEPTION) {
                        // don't dispatch CALC_FAILED on cancellation
                        queuePathEvent(PathEvent.CALC_FAILED);
                    }
                }
            } else {
                if (next == null) {
                    if (executor.isPresent()) {
                        if (executor.get().getPath().getSrc().equals(current.getPath().getDest())) {
                            queuePathEvent(PathEvent.NEXT_SEGMENT_CALC_FINISHED);
                            next = executor.get();
                        } else {
                            logDebug("Warning: discarding orphan next segment with incorrect start");
                        }
                    } else {
                        queuePathEvent(PathEvent.NEXT_CALC_FAILED);
                    }
                } else {
                    // throw new IllegalStateException("I have no idea what to do with this path");
                    // no point in throwing an exception here, and it gets it stuck with inProgress being not null
                    logDirect("Warning: PathingBehaivor illegal state! Discarding invalid path!");
                }
            }
            if (talkAboutIt && current != null && current.getPath() != null) {
                if (goal.isInGoal(current.getPath().getDest())) {
                    logDebug("Finished finding a path from " + start + " to " + goal + ". " + current.getPath().getNumNodesConsidered() + " nodes considered");
                } else {
                    logDebug("Found path segment from " + start + " towards " + goal + ". " + current.getPath().getNumNodesConsidered() + " nodes considered");
                }
            }
            synchronized (pathCalcLock) {
                inProgress = null;
            }
        }
    });
}
Also used : Goal(baritone.api.pathing.goals.Goal) PathExecutor(baritone.pathing.path.PathExecutor) AbstractNodeCostSearch(baritone.pathing.calc.AbstractNodeCostSearch) PathCalculationResult(baritone.api.utils.PathCalculationResult)

Aggregations

PathCalculationResult (baritone.api.utils.PathCalculationResult)4 IPath (baritone.api.pathing.calc.IPath)2 Goal (baritone.api.pathing.goals.Goal)2 AbstractNodeCostSearch (baritone.pathing.calc.AbstractNodeCostSearch)2 PathExecutor (baritone.pathing.path.PathExecutor)2