This commit is contained in:
Leijurv 2018-08-02 11:07:10 -04:00
parent 36d3ab9e1d
commit 38e358b077
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
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

@ -1,32 +1,47 @@
package baritone.pathfinding;
package baritone.bot.pathing.calc;
import baritone.pathfinding.goals.Goal;
import baritone.pathfinding.actions.Action;
import java.util.Objects;
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
*/
public class Node {
class PathNode {
final BlockPos pos;
double cost;
Node previous;
final Goal goal;
final double estimatedCostToGoal;
Action previousAction;
// 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;
Node nextOpen;
public Node(BlockPos pos, Goal goal) {
/**
* 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.previousAction = null;
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;
@ -36,6 +51,7 @@ public class Node {
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) {
@ -44,7 +60,7 @@ public class Node {
if (getClass() != obj.getClass()) {
return false;
}
final Node other = (Node) obj;
final PathNode other = (PathNode) obj;
if (!Objects.equals(this.pos, other.pos)) {
return false;
}

View File

@ -78,7 +78,7 @@ public class Out {
* Out.mode is Debug or Ludicrous. Do not use a Mode.None or a
* Mode.Ludicrous in this parameter.
* @exception IllegalArgumentException This will only be triggered if a
* messages given with a req of Node or Ludicrous
* messages given with a req of PathNode or Ludicrous
*/
public static void gui(Object o, Mode req) throws IllegalArgumentException {
if (req.equals(Mode.None) || req.equals(Mode.Ludicrous)) {