Repository navigation
Expand file tree
/
Copy pathPPJps.java
More file actions
97 lines (83 loc) · 2.56 KB
/
Copy pathPPJps.java
File metadata and controls
97 lines (83 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package org.bk.ass.path;
import java.util.Collections;
import org.bk.ass.grid.Grid;
/**
* Jump point search. Initialize with a {@link PPMap} instance and call {@link #findPath(Position,
* Position)}. This implementation uses preprocessed maps to improve runtime performance (~40%).
* (Note: This is not JPS+, only verticals and horizontals are precomputed)
*/
public class PPJps {
private final PPMap map;
public PPJps(PPMap map) {
this.map = map;
}
public PPJps(Grid<Boolean> map) {
this(PPMap.fromMap(map));
}
/**
* Searches for a path between start and end. Allows concurrent pathing requests if the map
* implementation allows concurrent queries.
*
* @return a valid path or a path with infinite length if none could be found. Never returns null.
*/
public Result findPath(Position start, Position end) {
return findPath(start, end, Float.MAX_VALUE);
}
public Result findPath(Position start, Position end, float maxLength) {
if (start.equals(end)) {
return new Result(0, Collections.singletonList(start));
}
return new PathFinder(end, map, maxLength).searchFrom(start);
}
private static class PathFinder extends AbstractPathFinder {
private final PPMap map;
PathFinder(Position target, PPMap map, float maxLength) {
super(target, map, maxLength);
this.map = map;
}
@Override
protected Position jumpHorizontal(int px, int py, int dx) {
assert dx != 0;
short x;
if (dx < 0) {
x = map.left(px, py);
if (py == target.y
&& px >= target.x
&& (x < 0 && -x - 1 <= target.x || x >= 0 && x <= target.x)) {
x = (short) target.x;
}
} else {
x = map.right(px, py);
if (py == target.y && px <= target.x && (x < 0 && -x - 1 >= target.x || x >= target.x)) {
x = (short) target.x;
}
}
if (x >= 0) {
return new Position(x, py);
}
return null;
}
@Override
protected Position jumpVertical(int px, int py, int dy) {
assert dy != 0;
short y;
if (dy < 0) {
y = map.up(px, py);
if (px == target.x
&& py >= target.y
&& (y < 0 && -y - 1 <= target.y || y >= 0 && y <= target.y)) {
y = (short) target.y;
}
} else {
y = map.down(px, py);
if (px == target.x && py <= target.y && (y < 0 && -y - 1 >= target.y || y >= target.y)) {
y = (short) target.y;
}
}
if (y >= 0) {
return new Position(px, y);
}
return null;
}
}
}