Код: Выделить всё
public static List getPath(EntityMob start, Vector2 end){
List openList = new ArrayList();
Set closedList = new HashSet();
Vector2 bounds = new Vector2(start.floor.floor_width,start.floor.floor_height);
openList.add(new Node(start.getX(),start.getZ()));
while(!openList.isEmpty()) {
openList.sort(Comparator.comparingInt(n->n.f));
Node current = openList.get(0);
if(current.x==end.x&¤t.z==end.z) {
return reconstructPath(current);
}
openList.remove(current);
closedList.add(current);
for(Node neighbor : getNeighbors(current,bounds)) {
if(closedList.contains(neighbor)||!start.canIStepOn(neighbor.x, neighbor.z)) {
continue;
}
int tentativeG = current.g+1;
if(!openList.contains(neighbor)) {
openList.add(neighbor);
}else if(tentativeG>=neighbor.g) {
continue;
}
neighbor.parent=current;
neighbor.g=tentativeG;
neighbor.h=manhattanDistance(neighbor.asVector(),end);
neighbor.f=neighbor.g+neighbor.h;
}
}
return Collections.emptyList();
}
private static List getNeighbors(Node node, Vector2 bounds){
List neighbors = new ArrayList();
if(node.x>0) neighbors.add(new Node(node.x-1,node.z));
if(node.x0) neighbors.add(new Node(node.x,node.z-1));
if(node.z
Подробнее здесь: [url]https://stackoverflow.com/questions/79033420/how-to-stop-a-from-endlessly-looping-if-theres-no-valid-path-between-points[/url]