-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
VectorNode.java
167 lines (144 loc) · 4.92 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package net.citizensnpcs.api.astar.pathfinder;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.util.Vector;
import com.google.common.collect.Lists;
import net.citizensnpcs.api.astar.AStarNode;
import net.citizensnpcs.api.astar.Plan;
import net.citizensnpcs.api.astar.pathfinder.BlockExaminer.PassableState;
public class VectorNode extends AStarNode implements PathPoint {
private float blockCost = -1;
private final BlockSource blockSource;
List<PathCallback> callbacks;
private final BlockExaminer[] examiners;
private final VectorGoal goal;
Vector location;
public VectorNode(VectorGoal goal, Location location, BlockSource source, BlockExaminer... examiners) {
this(goal, location.toVector(), source, examiners);
}
public VectorNode(VectorGoal goal, 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;
this.goal = goal;
}
@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);
}
@Override
public VectorNode createAtOffset(Vector mod) {
return new VectorNode(goal, mod, blockSource, examiners);
}
public float distance(VectorNode to) {
return (float) location.distance(to.location);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
VectorNode other = (VectorNode) obj;
if (location == null) {
if (other.location != null) {
return false;
}
} else if (!location.equals(other.location)) {
return false;
}
return true;
}
private float getBlockCost() {
if (blockCost == -1) {
blockCost = 0;
for (BlockExaminer examiner : examiners) {
blockCost += examiner.getCost(blockSource, this);
}
}
return blockCost;
}
@Override
public Vector getGoal() {
return goal.goal;
}
@Override
public Iterable<AStarNode> getNeighbours() {
List<PathPoint> neighbours = null;
for (BlockExaminer examiner : examiners) {
if (examiner instanceof NeighbourGeneratorBlockExaminer) {
neighbours = ((NeighbourGeneratorBlockExaminer) examiner).getNeighbours(blockSource, this);
break;
}
}
if (neighbours == null) {
neighbours = getNeighbours(blockSource, this);
}
List<AStarNode> nodes = Lists.newArrayList();
for (PathPoint sub : neighbours) {
if (!isPassable(sub))
continue;
nodes.add((AStarNode) sub);
}
return nodes;
}
public List<PathPoint> getNeighbours(BlockSource source, PathPoint point) {
List<PathPoint> neighbours = 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;
if (x != 0 && z != 0)
continue;
Vector mod = location.clone().add(new Vector(x, y, z));
if (mod.equals(location))
continue;
neighbours.add(point.createAtOffset(mod));
}
}
}
return neighbours;
}
@Override
public PathPoint getParentPoint() {
return (PathPoint) getParent();
}
@Override
public Vector getVector() {
return location.clone();
}
@Override
public int hashCode() {
final int prime = 31;
return prime + ((location == null) ? 0 : location.hashCode());
}
public float heuristicDistance(Vector goal) {
return (float) (location.distance(goal) + getBlockCost()) * TIEBREAKER;
}
private boolean isPassable(PathPoint mod) {
boolean passable = false;
for (BlockExaminer examiner : examiners) {
PassableState state = examiner.isPassable(blockSource, mod);
if (state == PassableState.IGNORE)
continue;
passable |= state == PassableState.PASSABLE ? true : false;
}
return passable;
}
@Override
public void setVector(Vector vector) {
this.location = vector;
}
private static final float TIEBREAKER = 1.001f;
}