This commit is contained in:
Leijurv 2018-08-03 01:14:33 -04:00
parent 9bd94fc8d2
commit 33ab4af3a3
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
2 changed files with 24 additions and 17 deletions

View File

@ -56,6 +56,8 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
} }
protected PathNode getNodeAtPosition(BlockPos pos) { protected PathNode getNodeAtPosition(BlockPos pos) {
//technically I think this could be map.computeIfAbsent(pos, pos -> new PathNode(pos, goal))
//but this is so core to the pathfinder that I'm wary of the lambda performance, hmmm
PathNode alr = map.get(pos); PathNode alr = map.get(pos);
if (alr == null) { if (alr == null) {
PathNode node = new PathNode(pos, goal); PathNode node = new PathNode(pos, goal);

View File

@ -1,54 +1,51 @@
package baritone.pathfinding; package baritone.pathfinding;
import baritone.Baritone;
import baritone.pathfinding.actions.*;
import baritone.pathfinding.goals.Goal; import baritone.pathfinding.goals.Goal;
import baritone.pathfinding.actions.ActionDescendTwo; import baritone.util.Out;
import baritone.pathfinding.actions.ActionBridge; import baritone.util.ToolSet;
import baritone.pathfinding.actions.ActionClimb; import net.minecraft.client.Minecraft;
import baritone.pathfinding.actions.Action; import net.minecraft.util.EnumFacing;
import baritone.pathfinding.actions.ActionDescend; import net.minecraft.util.math.BlockPos;
import baritone.pathfinding.actions.ActionFall; import net.minecraft.world.chunk.EmptyChunk;
import baritone.pathfinding.actions.ActionPillar;
import java.util.HashMap; import java.util.HashMap;
import java.util.Random; import java.util.Random;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import baritone.Baritone;
import baritone.pathfinding.actions.ActionDescendThree;
import baritone.pathfinding.actions.ActionWalkDiagonal;
import baritone.util.Out;
import baritone.util.ToolSet;
import net.minecraft.client.Minecraft;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.chunk.EmptyChunk;
/** /**
*
* @author leijurv * @author leijurv
*/ */
public class PathFinder { public class PathFinder {
final BlockPos start; final BlockPos start;
final Goal goal; final Goal goal;
final HashMap<BlockPos, Node> map; final HashMap<BlockPos, Node> map;
public PathFinder(BlockPos start, Goal goal) { public PathFinder(BlockPos start, Goal goal) {
this.start = start; this.start = start;
this.goal = goal; this.goal = goal;
this.map = new HashMap<BlockPos, Node>(); this.map = new HashMap<BlockPos, Node>();
} }
static final double[] COEFFICIENTS = {1.5, 2, 2.5, 3, 4, 5, 10}; static final double[] COEFFICIENTS = {1.5, 2, 2.5, 3, 4, 5, 10};
public static PathFinder currentlyRunning = null; public static PathFinder currentlyRunning = null;
Node[] bestSoFar; Node[] bestSoFar;
Node startNode; Node startNode;
Node mostRecentConsidered; Node mostRecentConsidered;
public Path getTempSolution() { public Path getTempSolution() {
if (startNode == null || bestSoFar[0] == null) { if (startNode == null || bestSoFar[0] == null) {
return null; return null;
} }
return new Path(startNode, bestSoFar[0], goal, 0); return new Path(startNode, bestSoFar[0], goal, 0);
} }
public Path getMostRecentNodeConsidered() { public Path getMostRecentNodeConsidered() {
return mostRecentConsidered == null ? null : new Path(startNode, mostRecentConsidered, goal, 0); return mostRecentConsidered == null ? null : new Path(startNode, mostRecentConsidered, goal, 0);
} }
/** /**
* Do the actual path calculation. The returned path might not actually go * Do the actual path calculation. The returned path might not actually go
* to goal, but it will get as close as I could get * to goal, but it will get as close as I could get
@ -159,13 +156,16 @@ public class PathFinder {
currentlyRunning = null; currentlyRunning = null;
return null; return null;
} }
private double distFromStart(Node n) { private double distFromStart(Node n) {
int xDiff = n.pos.getX() - start.getX(); int xDiff = n.pos.getX() - start.getX();
int yDiff = n.pos.getY() - start.getY(); int yDiff = n.pos.getY() - start.getY();
int zDiff = n.pos.getZ() - start.getZ(); int zDiff = n.pos.getZ() - start.getZ();
return Math.sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff); return Math.sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff);
} }
private final double MIN_DIST_PATH = 5; private final double MIN_DIST_PATH = 5;
private Node getNodeAtPosition(BlockPos pos) { private Node getNodeAtPosition(BlockPos pos) {
Node alr = map.get(pos); Node alr = map.get(pos);
if (alr == null) { if (alr == null) {
@ -175,6 +175,7 @@ public class PathFinder {
} }
return alr; return alr;
} }
private static Action[] getConnectedPositions(BlockPos pos) { private static Action[] getConnectedPositions(BlockPos pos) {
int x = pos.getX(); int x = pos.getX();
int y = pos.getY(); int y = pos.getY();
@ -208,7 +209,9 @@ public class PathFinder {
actions[25] = new ActionWalkDiagonal(pos, EnumFacing.SOUTH, EnumFacing.EAST); actions[25] = new ActionWalkDiagonal(pos, EnumFacing.SOUTH, EnumFacing.EAST);
return actions; return actions;
} }
private final Random random = new Random(); private final Random random = new Random();
private void shuffle(Action[] list) { private void shuffle(Action[] list) {
int len = list.length; int len = list.length;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
@ -224,6 +227,7 @@ public class PathFinder {
*/ */
private static class OpenSet { private static class OpenSet {
Node first = null; Node first = null;
public Node removeLowest() { public Node removeLowest() {
if (first == null) { if (first == null) {
return null; return null;
@ -255,6 +259,7 @@ public class PathFinder {
beforeBest.nextOpen = bestNode.nextOpen; beforeBest.nextOpen = bestNode.nextOpen;
return bestNode; return bestNode;
} }
public void insert(Node node) { public void insert(Node node) {
node.nextOpen = first; node.nextOpen = first;
first = node; first = node;