Skip to content

Commit

Permalink
Save memory in the A* pathfinder by reusing generic info
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Apr 8, 2018
1 parent d0d4d9b commit 01a1781
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/main/java/net/citizensnpcs/api/astar/pathfinder/VectorNode.java
Expand Up @@ -13,23 +13,23 @@

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;
private final PathInfo info;
Vector location;

public VectorNode(VectorGoal goal, Location location, BlockSource source, BlockExaminer... examiners) {
this(null, goal, location.toVector(), source, examiners);
}

public VectorNode(VectorNode parent, VectorGoal goal, Vector location, BlockSource source,
BlockExaminer... examiners) {
public VectorNode(VectorNode parent, Vector location, PathInfo info) {
super(parent);
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;
this.info = info;
}

public VectorNode(VectorNode parent, VectorGoal goal, Vector location, BlockSource source,
BlockExaminer... examiners) {
this(parent, location, new PathInfo(source, examiners == null ? new BlockExaminer[] {} : examiners, goal));
}

@Override
Expand All @@ -48,7 +48,7 @@ public Plan buildPlan() {

@Override
public VectorNode createAtOffset(Vector mod) {
return new VectorNode(this, goal, mod, blockSource, examiners);
return new VectorNode(this, mod, info);
}

public float distance(VectorNode to) {
Expand Down Expand Up @@ -77,29 +77,29 @@ public boolean equals(Object obj) {
private float getBlockCost() {
if (blockCost == -1) {
blockCost = 0;
for (BlockExaminer examiner : examiners) {
blockCost += examiner.getCost(blockSource, this);
for (BlockExaminer examiner : info.examiners) {
blockCost += examiner.getCost(info.blockSource, this);
}
}
return blockCost;
}

@Override
public Vector getGoal() {
return goal.goal;
return info.goal.goal;
}

@Override
public Iterable<AStarNode> getNeighbours() {
List<PathPoint> neighbours = null;
for (BlockExaminer examiner : examiners) {
for (BlockExaminer examiner : info.examiners) {
if (examiner instanceof NeighbourGeneratorBlockExaminer) {
neighbours = ((NeighbourGeneratorBlockExaminer) examiner).getNeighbours(blockSource, this);
neighbours = ((NeighbourGeneratorBlockExaminer) examiner).getNeighbours(info.blockSource, this);
break;
}
}
if (neighbours == null) {
neighbours = getNeighbours(blockSource, this);
neighbours = getNeighbours(info.blockSource, this);
}
List<AStarNode> nodes = Lists.newArrayList();
for (PathPoint sub : neighbours) {
Expand Down Expand Up @@ -151,8 +151,8 @@ public float heuristicDistance(Vector goal) {

private boolean isPassable(PathPoint mod) {
boolean passable = false;
for (BlockExaminer examiner : examiners) {
PassableState state = examiner.isPassable(blockSource, mod);
for (BlockExaminer examiner : info.examiners) {
PassableState state = examiner.isPassable(info.blockSource, mod);
if (state == PassableState.IGNORE)
continue;
passable |= state == PassableState.PASSABLE ? true : false;
Expand All @@ -165,5 +165,17 @@ public void setVector(Vector vector) {
this.location = vector;
}

private static class PathInfo {
private final BlockSource blockSource;
private final BlockExaminer[] examiners;
private final VectorGoal goal;

private PathInfo(BlockSource source, BlockExaminer[] examiners, VectorGoal goal) {
this.blockSource = source;
this.examiners = examiners;
this.goal = goal;
}
}

private static final float TIEBREAKER = 1.001f;
}

0 comments on commit 01a1781

Please sign in to comment.