1package com.osroyale.game.world.pathfinding.path.impl;
3import com.osroyale.game.world.Interactable;
4import com.osroyale.game.world.entity.mob.Direction;
5import com.osroyale.game.world.entity.mob.Mob;
6import com.osroyale.game.world.pathfinding.distance.Distance;
7import com.osroyale.game.world.pathfinding.path.Path;
8import com.osroyale.game.world.pathfinding.path.PathFinder;
9import com.osroyale.game.world.position.Position;
10import com.osroyale.game.world.region.Region;
64 private final Mob character;
65 private final Map<Position, Node> nodes =
new HashMap<>(
Region.SIZE *
Region.SIZE);
66 private final Set<Node> open =
new HashSet<>(
Region.SIZE *
Region.SIZE);
67 private final Queue<Node> sorted =
new PriorityQueue<>(
Region.SIZE *
Region.SIZE);
68 private final Deque<Position> shortest =
new ArrayDeque<>(100);
75 this.character = character;
76 this.heuristic = heuristic;
87 Position origin = character.getPosition();
89 return new Path(
null);
90 return find(character, destination);
101 Position origin = character.getPosition();
103 return new Path(
null);
104 return find(character, destination);
119 return new Path(
null);
121 Node start =
new Node(source.
getPosition()), end =
new Node(target);
129 nodes.put(target, end);
136 boolean found =
false;
140 active = getCheapest(sorted);
141 Position position = active.getPosition();
143 if (position.equals(target)) {
152 next = position.
transform(direction.getFaceLocation());
154 Node node = nodes.computeIfAbsent(next, Node::new);
155 compare(active, node);
158 }
while (!open.isEmpty() && sorted.size() < distance);
162 if (end.hasParent()) {
166 if (active.hasParent()) {
167 Position position = active.getPosition();
170 shortest.addFirst(position);
171 active = active.getParent();
172 position = active.getPosition();
174 if (shortest.size() >= 100) {
180 return new Path(shortest);
182 return new Path(
null);
185 private void compare(Node active, Node other) {
186 int cost = active.getCost() + heuristic.
calculate(active.getPosition(), other.getPosition());
187 if (other.getCost() > cost) {
190 }
else if (other.isOpen() && !open.contains(other)) {
192 other.setParent(active);
204 private Node getCheapest(Queue<Node> nodes) {
205 Node node = nodes.peek();
206 while (!node.isOpen()) {
223final class Node
implements Comparable<Node> {
229 private boolean open =
true;
235 private final Position position;
242 Node(Position position) {
252 private Node(Position position,
int cost) {
253 this.position = position;
262 void setCost(
int cost) {
278 public void close() {
287 public boolean isOpen() {
296 void setParent(Node parent) {
297 this.parent = parent;
316 boolean hasParent() {
317 return parent !=
null;
325 public Position getPosition() {
336 public int compareTo(Node other) {
337 return Integer.compare(cost, other.cost);
348 public final boolean equals(Object obj) {
349 if (
this == obj)
return true;
350 if (!(obj instanceof Node))
return false;
351 Node other = (Node) obj;
352 return getPosition().getX() == other.getPosition().getX()
353 && getPosition().getY() == other.getPosition().getY()
354 && getPosition().getHeight() == other.getPosition().getHeight()
355 && getCost() == other.getCost()
356 && getParent().getCost() == other.getParent().getCost()
357 && getParent().getPosition().getX() == other.getParent().getPosition().getX()
358 && getParent().getPosition().getY() == other.getParent().getPosition().getY()
359 && getParent().getPosition().getHeight() == other.getPosition().getHeight();
368 public final int hashCode() {
369 final int prime = 31;
371 result = prime * result + position.getX();
372 result = prime * result + position.getY();
373 result = prime * result + position.getHeight();
375 Position p = parent.getPosition();
376 result = prime * result + p.getX();
377 result = prime * result + p.getY();
378 result = prime * result + p.getHeight();
380 result = prime * result + cost;
boolean traversable(Position current, int size, Direction... directions)
AStarPathFinder(Mob character, Distance heuristic)
Path find(Position destination)
Path find(Mob source, Position target, int targetWidth, int targetLength)
Path find(Interactable destination)
double getDistance(Position other)
Position transform(int diffX, int diffY, int diffZ)
int calculate(Position from, Position to)