RuneHive-Game
Loading...
Searching...
No Matches
SimplePathChecker.java
Go to the documentation of this file.
1package com.runehive.game.world.pathfinding.path;
2
3import com.runehive.game.world.Interactable;
4import com.runehive.game.world.entity.mob.Mob;
5import com.runehive.game.world.position.Position;
6import com.runehive.util.Utility;
7
8/**
9 * Represents a {@code PathFinder} which is meant to be used to check
10 * projectiles passage in a straight line.
11 *
12 * @author Artem Batutin <artembatutin@gmail.com>
13 * @author Michael | Chex (improvements to "check" method)
14 */
15public class SimplePathChecker extends PathFinder {
17
18 @Override
19 public Path find(Mob source, Position end, int targetWidth, int targetLength) {
20 /* empty path */
21 return new Path(null);
22 }
23
24 public static boolean checkLine(Interactable source, Interactable target) {
25 Position targetPosition = Utility.findBestInside(source, target);
26 return SIMPLE_PATH_CHECKER.check(source.getPosition(), targetPosition, source.width(), false);
27 }
28
29 public static boolean checkLine(Interactable source, Position target) {
30 return SIMPLE_PATH_CHECKER.check(source.getPosition(), target, source.width(), false);
31 }
32
33 public static boolean checkProjectile(Interactable source, Interactable target) {
34 Position targetPosition = Utility.findBestInside(source, target);
35 return SIMPLE_PATH_CHECKER.check(source.getPosition(), targetPosition, Math.max(1, Math.max(source.width(), source.length())), true);
36 }
37
38 /**
39 * Determines if the projectile can reach it's destination.
40 *
41 * @param start The projectile's starting Position.
42 * @param end The projectile's ending Position.
43 * @param projectile The condition if the check is meant for projectiles.
44 * @return {@code true} if the projectile can reach it's destination, {@code
45 * false} otherwise.
46 */
47 private boolean check(Position start, Position end, int size, boolean projectile) {
48 int dx = end.getX() - start.getX();
49 int dy = end.getY() - start.getY();
50 int xSignum = Integer.signum(dx);
51 int ySignum = Integer.signum(dy);
52 int height = start.getHeight();
53 int nextX = start.getX();
54 int nextY = start.getY();
55 int currentX = nextX;
56 int currentY = nextY;
57
58 while (true) {
59 if (dx == 0) {
60 nextY += ySignum;
61 } else if (dy == 0) {
62 nextX += xSignum;
63 } else if (nextX != end.getX()) {
64 int pointSlope = (nextX + xSignum - start.getX()) * dy / dx + start.getY();
65
66 if (Math.abs(pointSlope - currentY) > 1) {
67 nextY += ySignum;
68 } else {
69 nextX += xSignum;
70 nextY = pointSlope;
71 }
72 }
73
74 Position current = new Position(currentX, currentY, height);
75 Position next = new Position(nextX, nextY, height);
76
77 if (projectile && !projectileCheck(current, next)) {
78 return false;
79 }
80
81 if (!projectile && !traversable(current, next, size)) {
82 return false;
83 }
84
85 if (nextX == end.getX() && nextY == end.getY()) {
86 break;
87 }
88
89 currentX = nextX;
90 currentY = nextY;
91 }
92 return true;
93 }
94
95}
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...
boolean traversable(Position current, int size, Direction... directions)
Returns whether or not a Position walking one step in any of the specified Directions would lead to i...
Represents a single path in the path finding system.
Definition Path.java:13
Represents a PathFinder which is meant to be used to check projectiles passage in a straight line.
boolean check(Position start, Position end, int size, boolean projectile)
Determines if the projectile can reach it's destination.
static boolean checkProjectile(Interactable source, Interactable target)
Path find(Mob source, Position end, int targetWidth, int targetLength)
Finds a valid path from the origin Position to the target one.
static boolean checkLine(Interactable source, Position target)
static boolean checkLine(Interactable source, Interactable target)
Represents a single tile on the game world.
Definition Position.java:14
int getHeight()
Gets the height coordinate, or height.
Definition Position.java:51
int getY()
Gets the absolute y coordinate.
Definition Position.java:46
int getX()
Gets the absolute x coordinate.
Definition Position.java:41
Handles miscellaneous methods.
Definition Utility.java:27
static Position findBestInside(Interactable source, Interactable target)
Definition Utility.java:498
An object implementing Interactable has uses.