cancel calculation in progress

This commit is contained in:
Leijurv 2018-08-28 11:17:11 -07:00
parent df73e07923
commit c0fb57825c
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
3 changed files with 14 additions and 2 deletions

View File

@ -18,11 +18,11 @@
package baritone.behavior.impl;
import baritone.Baritone;
import baritone.behavior.Behavior;
import baritone.api.event.events.PathEvent;
import baritone.api.event.events.PlayerUpdateEvent;
import baritone.api.event.events.RenderEvent;
import baritone.api.event.events.TickEvent;
import baritone.behavior.Behavior;
import baritone.pathing.calc.AStarPathFinder;
import baritone.pathing.calc.AbstractNodeCostSearch;
import baritone.pathing.calc.IPathFinder;
@ -194,6 +194,7 @@ public class PathingBehavior extends Behavior {
current = null;
next = null;
Baritone.INSTANCE.getInputOverrideHandler().clearAllKeys();
AbstractNodeCostSearch.getCurrentlyRunning().ifPresent(AbstractNodeCostSearch::cancel);
}
public void path() {

View File

@ -80,7 +80,7 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper {
int pathingMaxChunkBorderFetch = Baritone.settings().pathingMaxChunkBorderFetch.get(); // grab all settings beforehand so that changing settings during pathing doesn't cause a crash or unpredictable behavior
double favorCoeff = Baritone.settings().backtrackCostFavoringCoefficient.get();
boolean minimumImprovementRepropagation = Baritone.settings().minimumImprovementRepropagation.get();
while (!openSet.isEmpty() && numEmptyChunk < pathingMaxChunkBorderFetch && System.currentTimeMillis() < timeoutTime) {
while (!openSet.isEmpty() && numEmptyChunk < pathingMaxChunkBorderFetch && System.currentTimeMillis() < timeoutTime && !cancelRequested) {
if (slowPath) {
try {
Thread.sleep(Baritone.settings().slowPathTimeDelayMS.<Long>get());
@ -169,6 +169,10 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper {
}
}
}
if (cancelRequested) {
currentlyRunning = null;
return Optional.empty();
}
double bestDist = 0;
for (int i = 0; i < bestSoFar.length; i++) {
if (bestSoFar[i] == null) {

View File

@ -52,6 +52,8 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
private volatile boolean isFinished;
protected boolean cancelRequested;
/**
* This is really complicated and hard to explain. I wrote a comment in the old version of MineBot but it was so
* long it was easier as a Google Doc (because I could insert charts).
@ -70,10 +72,15 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
this.map = new HashMap<>();
}
public void cancel() {
cancelRequested = true;
}
public synchronized Optional<IPath> calculate() {
if (isFinished) {
throw new IllegalStateException("Path Finder is currently in use, and cannot be reused!");
}
this.cancelRequested = false;
Optional<IPath> path = calculate0();
isFinished = true;
return path;