Search in sources :

Example 1 with AbstractNodeCostSearch

use of baritone.pathing.calc.AbstractNodeCostSearch 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 AbstractNodeCostSearch

use of baritone.pathing.calc.AbstractNodeCostSearch 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

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