/
VectorNode.java
103 lines (86 loc) · 3.02 KB
/
VectorNode.java
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
98
99
100
101
102
103
package net.citizensnpcs.api.astar.pathfinder;
import java.util.List;
import net.citizensnpcs.api.astar.AStarNode;
import net.citizensnpcs.api.astar.Plan;
import org.bukkit.Location;
import org.bukkit.util.Vector;
import com.google.common.collect.Lists;
public class VectorNode extends AStarNode implements PathPoint {
private float blockCost = -1;
private final BlockSource blockSource;
List<PathCallback> callbacks;
private final BlockExaminer[] examiners;
final Vector location;
public VectorNode(Location location, BlockSource source, BlockExaminer... examiners) {
this(location.toVector(), source, examiners);
}
public VectorNode(Vector location, BlockSource source, BlockExaminer... examiners) {
this.location = location.setX(location.getBlockX()).setY(location.getBlockY()).setZ(location.getBlockZ());
this.blockSource = source;
this.examiners = examiners == null ? new BlockExaminer[] {} : examiners;
}
@Override
public void addCallback(PathCallback callback) {
if (callbacks == null) {
callbacks = Lists.newArrayList();
}
callbacks.add(callback);
}
@Override
public Plan buildPlan() {
Iterable<VectorNode> parents = getParents();
return new Path(parents);
}
public float distance(VectorNode to) {
return (float) location.distance(to.location);
}
private float getBlockCost() {
if (blockCost == -1) {
blockCost = 0;
for (BlockExaminer examiner : examiners) {
blockCost += examiner.getCost(blockSource, this);
}
}
return blockCost;
}
@Override
public Iterable<AStarNode> getNeighbours() {
List<AStarNode> nodes = Lists.newArrayList();
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -1; z <= 1; z++) {
if (x == 0 && y == 0 && z == 0)
continue;
Vector mod = location.clone().add(new Vector(x, y, z));
if (mod.equals(location))
continue;
VectorNode sub = getNewNode(mod);
if (!isPassable(sub))
continue;
nodes.add(sub);
}
}
}
return nodes;
}
private VectorNode getNewNode(Vector mod) {
return new VectorNode(mod, blockSource, examiners);
}
@Override
public Vector getVector() {
return location;
}
public float heuristicDistance(Vector goal) {
return (float) (location.distance(goal) + getBlockCost()) * TIEBREAKER;
}
private boolean isPassable(PathPoint mod) {
for (BlockExaminer examiner : examiners) {
boolean passable = examiner.isPassable(blockSource, mod);
if (!passable) {
return false;
}
}
return true;
}
private static final float TIEBREAKER = 1.001f;
}