more robust path destination verification

This commit is contained in:
Leijurv 2018-09-14 09:21:52 -07:00
parent a38da64c49
commit 001070d406
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
9 changed files with 71 additions and 14 deletions

View File

@ -28,5 +28,6 @@ public enum PathEvent {
AT_GOAL,
PATH_FINISHED_NEXT_STILL_CALCULATING,
NEXT_CALC_FAILED,
DISCARD_NEXT
DISCARD_NEXT,
CANCELED;
}

View File

@ -24,9 +24,16 @@ public final class TickEvent {
private final EventState state;
private final Type type;
private static int count;
public TickEvent(EventState state, Type type) {
this.state = state;
this.type = type;
count++;
}
public int getCount() {
return count;
}
public Type getType() {

View File

@ -17,8 +17,9 @@
package baritone.behavior;
import baritone.api.event.events.PathEvent;
import baritone.api.behavior.Behavior;
import baritone.api.event.events.PathEvent;
import baritone.api.event.events.TickEvent;
import baritone.cache.CachedChunk;
import baritone.cache.ChunkPacker;
import baritone.cache.WorldProvider;
@ -26,16 +27,14 @@ import baritone.cache.WorldScanner;
import baritone.pathing.goals.Goal;
import baritone.pathing.goals.GoalComposite;
import baritone.pathing.goals.GoalTwoBlocks;
import baritone.pathing.path.IPath;
import baritone.utils.BlockStateInterface;
import baritone.utils.Helper;
import net.minecraft.block.Block;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.chunk.EmptyChunk;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -52,6 +51,31 @@ public final class MineBehavior extends Behavior implements Helper {
private List<Block> mining;
@Override
public void onTick(TickEvent event) {
if (mining == null) {
return;
}
if (event.getCount() % 5 == 0) {
updateGoal();
}
Optional<IPath> path = PathingBehavior.INSTANCE.getPath();
if (!path.isPresent()) {
return;
}
Goal currentGoal = PathingBehavior.INSTANCE.getGoal();
if (currentGoal == null) {
return;
}
Goal intended = path.get().getGoal();
BlockPos end = path.get().getDest();
if (intended.isInGoal(end) && !currentGoal.isInGoal(end)) {
// this path used to end in the goal
// but the goal has changed, so there's no reason to continue...
PathingBehavior.INSTANCE.cancel();
}
}
@Override
public void onPathEvent(PathEvent event) {
updateGoal();
@ -113,7 +137,7 @@ public final class MineBehavior extends Behavior implements Helper {
}
public void cancel() {
PathingBehavior.INSTANCE.cancel();
mine((String[]) null);
PathingBehavior.INSTANCE.cancel();
}
}

View File

@ -103,7 +103,7 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper {
if (goal.isInGoal(currentNodePos)) {
currentlyRunning = null;
logDebug("Took " + (System.nanoTime() / 1000000L - startTime) + "ms, " + numMovementsConsidered + " movements considered");
return Optional.of(new Path(startNode, currentNode, numNodes));
return Optional.of(new Path(startNode, currentNode, numNodes, goal));
}
Movement[] possibleMovements = getConnectedPositions(currentNodePos, calcContext);//movement that we could take that start at currentNodePos, in random order
shuffle(possibleMovements);
@ -199,7 +199,7 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper {
}
System.out.println("Path goes for " + Math.sqrt(dist) + " blocks");
currentlyRunning = null;
return Optional.of(new Path(startNode, bestSoFar[i], numNodes));
return Optional.of(new Path(startNode, bestSoFar[i], numNodes, goal));
}
}
logDebug("Even with a cost coefficient of " + COEFFICIENTS[COEFFICIENTS.length - 1] + ", I couldn't get more than " + Math.sqrt(bestDist) + " blocks");

View File

@ -140,7 +140,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
@Override
public Optional<IPath> pathToMostRecentNodeConsidered() {
return Optional.ofNullable(mostRecentConsidered).map(node -> new Path(startNode, node, 0));
return Optional.ofNullable(mostRecentConsidered).map(node -> new Path(startNode, node, 0, goal));
}
@Override
@ -153,7 +153,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
continue;
}
if (getDistFromStartSq(bestSoFar[i]) > MIN_DIST_PATH * MIN_DIST_PATH) { // square the comparison since distFromStartSq is squared
return Optional.of(new Path(startNode, bestSoFar[i], 0));
return Optional.of(new Path(startNode, bestSoFar[i], 0, goal));
}
}
// instead of returning bestSoFar[0], be less misleading

View File

@ -17,6 +17,7 @@
package baritone.pathing.calc;
import baritone.pathing.goals.Goal;
import baritone.pathing.movement.Movement;
import baritone.pathing.path.IPath;
import baritone.utils.pathing.BetterBlockPos;
@ -52,19 +53,27 @@ class Path implements IPath {
final List<Movement> movements;
final Goal goal;
private final int numNodes;
private volatile boolean verified;
Path(PathNode start, PathNode end, int numNodes) {
Path(PathNode start, PathNode end, int numNodes, Goal goal) {
this.start = start.pos;
this.end = end.pos;
this.numNodes = numNodes;
this.path = new ArrayList<>();
this.movements = new ArrayList<>();
this.goal = goal;
assemblePath(start, end);
}
@Override
public Goal getGoal() {
return goal;
}
/**
* Assembles this path given the start and end nodes.
*

View File

@ -17,6 +17,7 @@
package baritone.pathing.path;
import baritone.pathing.goals.Goal;
import baritone.pathing.movement.Movement;
import baritone.utils.pathing.BetterBlockPos;
@ -31,10 +32,18 @@ public class CutoffPath implements IPath {
private final int numNodes;
final Goal goal;
public CutoffPath(IPath prev, int lastPositionToInclude) {
path = prev.positions().subList(0, lastPositionToInclude + 1);
movements = prev.movements().subList(0, lastPositionToInclude + 1);
numNodes = prev.getNumNodesConsidered();
goal = prev.getGoal();
}
@Override
public Goal getGoal() {
return goal;
}
@Override

View File

@ -64,6 +64,13 @@ public interface IPath extends Helper {
return positions().size();
}
/**
* What goal was this path calculated towards?
*
* @return
*/
Goal getGoal();
default Tuple<Double, BlockPos> closestPathPos(double x, double y, double z) {
double best = -1;
BlockPos bestPos = null;

View File

@ -120,9 +120,9 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
return;
}
if (msg.toLowerCase().equals("cancel")) {
PathingBehavior.INSTANCE.cancel();
FollowBehavior.INSTANCE.cancel();
MineBehavior.INSTANCE.cancel();
FollowBehavior.INSTANCE.cancel();
PathingBehavior.INSTANCE.cancel();
event.cancel();
logDirect("ok canceled");
return;