RuneHive-Game
Loading...
Searching...
No Matches
SimplePathFinder.java
Go to the documentation of this file.
1package com.runehive.game.world.pathfinding.path.impl;
2
3import com.runehive.game.world.Interactable;
4import com.runehive.game.world.entity.mob.Direction;
5import com.runehive.game.world.entity.mob.Mob;
6import com.runehive.game.world.pathfinding.path.Path;
7import com.runehive.game.world.pathfinding.path.PathFinder;
8import com.runehive.game.world.position.Position;
9import com.runehive.game.world.region.Region;
10import com.runehive.util.Utility;
11
12import java.util.ArrayDeque;
13import java.util.Deque;
14
15import static com.runehive.game.world.entity.mob.Direction.NONE;
16import static com.runehive.game.world.entity.mob.Direction.getDirection;
17
18/**
19 * Represents a simple path finder which determines a straight path to the first
20 * blocked tile or it's destination. Mostly used by {@link Mob} following and
21 * movement.
22 *
23 * @author Artem Batutin <artembatutin@gmail.com>
24 * @author Michael | Chex
25 */
26public final class SimplePathFinder extends PathFinder {
27
28 /**
29 * A default method to find a path for the specified position.
30 *
31 * @return A {@link Deque} of {@link Position steps} to the specified
32 * destination.
33 */
34 @Override
35 public Path find(Mob source, Position end, int targetWidth, int targetLength) {
36 int approximation = (int) (source.getPosition().getLongestDelta(end) * 1.5);
37 Deque<Position> positions = new ArrayDeque<>(approximation);
38 return new Path(addWalks(source, end, targetWidth, targetLength, positions));
39 }
40
41 /**
42 * Performs the path finding calculations to find the path using the A*
43 * algorithm.
44 *
45 * @param source The path finder's start position.
46 * @param target The path finder's destination.
47 * @param positions The current searched deque of moves.
48 * @return The path to pursue to reach the destination.
49 */
50 private Deque<Position> addWalks(Mob source, Position target, int targetWidth, int targetLength, Deque<Position> positions) {
51 Position current = source.getPosition();
52 Position targ = target;
53 Interactable targInt = Interactable.create(target, targetWidth, targetLength);
54
55 if (targetWidth > 0 || targetLength > 0)
56 target = Utility.findBestInside(source, targInt);
57
58 boolean projectiles = source.pathfinderProjectiles;
59
60 while (!Region.reachable(targ, targetWidth, targetLength, current, source.width(), source.length())) {
61 Direction direction = getDirection(current, target);
62
63 if (!traversable(source, current, targInt, direction, projectiles)) {
64 if (direction == Direction.NORTH_WEST) {
65 if (traversable(source, current, targInt, Direction.WEST, projectiles)) {
66 direction = Direction.WEST;
67 } else if (traversable(source, current, targInt, Direction.NORTH, projectiles)) {
68 direction = Direction.NORTH;
69 } else
70 break;
71 } else if (direction == Direction.NORTH_EAST) {
72 if (traversable(source, current, targInt, Direction.NORTH, projectiles)) {
73 direction = Direction.NORTH;
74 } else if (traversable(source, current, targInt, Direction.EAST, projectiles)) {
75 direction = Direction.EAST;
76 } else
77 break;
78 } else if (direction == Direction.SOUTH_WEST) {
79 if (traversable(source, current, targInt, Direction.SOUTH, projectiles)) {
80 direction = Direction.SOUTH;
81 } else if (traversable(source, current, targInt, Direction.WEST, projectiles)) {
82 direction = Direction.WEST;
83 } else
84 break;
85 } else if (direction == Direction.SOUTH_EAST) {
86 if (traversable(source, current, targInt, Direction.SOUTH, projectiles)) {
87 direction = Direction.SOUTH;
88 } else if (traversable(source, current, targInt, Direction.EAST, projectiles)) {
89 direction = Direction.EAST;
90 } else
91 break;
92 } else
93 break;
94 }
95
96 if (direction == NONE)
97 break;
98
99 current = current.transform(direction.getFaceLocation());
100 positions.addLast(current);
101 }
102 return positions;
103 }
104
105 private boolean traversable(Mob source, Position current, Interactable target, Direction direction, boolean projectiles) {
106 Position next = current.transform(direction.getFaceLocation());
107 return (projectiles ? projectileCheck(current, next) : traversable(current, source.width(), direction))
108 && !Utility.inside(next, source.width(), source.length(), target.getPosition(), target.width(), target.length());
109 }
110
111}
Handles the mob class.
Definition Mob.java:66
An algorithm used to find a path between two Positions.
boolean projectileCheck(Position current, Position going)
Returns whether or not a Position shooting projectile to another Position would lead to is traversabl...
Represents a single path in the path finding system.
Definition Path.java:13
Represents a simple path finder which determines a straight path to the first blocked tile or it's de...
Path find(Mob source, Position end, int targetWidth, int targetLength)
A default method to find a path for the specified position.
boolean traversable(Mob source, Position current, Interactable target, Direction direction, boolean projectiles)
Deque< Position > addWalks(Mob source, Position target, int targetWidth, int targetLength, Deque< Position > positions)
Performs the path finding calculations to find the path using the A* algorithm.
Represents a single tile on the game world.
Definition Position.java:14
int getLongestDelta(Position other)
Gets the longest horizontal or vertical delta between the two positions.
Position transform(int diffX, int diffY, int diffZ)
Creates a new location based on this location.
Represents a single region.
Definition Region.java:21
static boolean reachable(Interactable source, Interactable target)
Definition Region.java:227
Handles miscellaneous methods.
Definition Utility.java:27
static boolean inside(Interactable source, Interactable target)
Definition Utility.java:783
static Position findBestInside(Interactable source, Interactable target)
Definition Utility.java:498
Represents the enumerated directions an entity can walk or face.
An object implementing Interactable has uses.
static Interactable create(Position position)
Creates a new instance of an Interactable.