This commit is contained in:
Leijurv
2018-08-02 11:07:10 -04:00
parent 36d3ab9e1d
commit 38e358b077
4 changed files with 102 additions and 13 deletions

View File

@@ -8,6 +8,9 @@ import net.minecraft.util.math.BlockPos;
import java.util.Collection;
import java.util.List;
/**
* @author leijurv
*/
public interface IPath {
/**
* Ordered list of movements to carry out.

View File

@@ -0,0 +1,70 @@
package baritone.bot.pathing.calc;
import baritone.bot.pathing.goals.Goal;
import baritone.bot.pathing.movements.Movement;
import net.minecraft.util.math.BlockPos;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
/**
* A node based implementation of IPath
*
* @author leijurv
*/
class Path implements IPath {
public final BlockPos start;
public final BlockPos end;
public final Goal goal;
/**
* The blocks on the path. Guaranteed that path.get(0) equals start and
* path.get(path.size()-1) equals end
*/
public final ArrayList<BlockPos> path;
final ArrayList<Movement> movements;
Path(PathNode start, PathNode end, Goal goal) {
this.start = start.pos;
this.end = end.pos;
this.goal = goal;
this.path = new ArrayList<>();
this.movements = new ArrayList<>();
assemblePath(start, end);
}
private final void assemblePath(PathNode start, PathNode end) {
PathNode current = end;
LinkedList<BlockPos> tempPath = new LinkedList<>();//repeatedly inserting to the beginning of an arraylist is O(n^2)
LinkedList<Movement> tempMovements = new LinkedList<>();//instead, do it into a linked list, then convert at the end
while (!current.equals(start)) {
tempPath.addFirst(current.pos);
tempMovements.addFirst(current.previousMovement);
current = current.previous;
}
tempPath.addFirst(start.pos);
path.addAll(tempPath);
movements.addAll(tempMovements);
}
@Override
public List<Movement> movements() {
return movements;
}
@Override
public List<BlockPos> positions() {
return path;
}
@Override
public Collection<BlockPos> getBlocksToMine() {
return null;
}
@Override
public Collection<BlockPos> getBlocksToPlace() {
return null;
}
}

View File

@@ -0,0 +1,69 @@
package baritone.bot.pathing.calc;
import baritone.bot.pathing.goals.Goal;
import baritone.bot.pathing.movements.Movement;
import net.minecraft.util.math.BlockPos;
import java.util.Objects;
/**
* A node in the path, containing the cost and steps to get to it.
*
* @author leijurv
*/
class PathNode {
final BlockPos pos;
final Goal goal;
final double estimatedCostToGoal;
// These three fields are mutable and are changed by PathFinder
double cost;
PathNode previous;
Movement previousMovement;
/**
* Is this a member of the open set in A*? (only used during pathfinding)
*/
boolean isOpen;
/**
* In the linked list of open nodes, which one is next? (only used during pathfinding)
*/
PathNode nextOpen;
public PathNode(BlockPos pos, Goal goal) {
this.pos = pos;
this.previous = null;
this.cost = Short.MAX_VALUE;
this.goal = goal;
this.estimatedCostToGoal = goal.heuristic(pos);
this.previousMovement = null;
this.isOpen = false;
}
// TODO possibly reimplement hashCode and equals. They are necessary for this class to function but they could be done better
@Override
public int hashCode() {//this is some OG code right here
int hash = 3241;
hash = 3457689 * hash + this.pos.getX();
hash = 8734625 * hash + this.pos.getY();
hash = 2873465 * hash + this.pos.getZ();
hash = 3241543 * hash + Objects.hashCode(this.goal);
return hash;
}
@Override
public boolean equals(Object obj) {//autogenerated by netbeans. that's why it looks disgusting.
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final PathNode other = (PathNode) obj;
if (!Objects.equals(this.pos, other.pos)) {
return false;
}
return Objects.equals(this.goal, other.goal);
}
}