From 01a1781ab684481d1a2d9404696b34365458a66f Mon Sep 17 00:00:00 2001 From: fullwall Date: Sun, 8 Apr 2018 23:53:25 +0800 Subject: [PATCH] Save memory in the A* pathfinder by reusing generic info --- .../api/astar/pathfinder/VectorNode.java | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/citizensnpcs/api/astar/pathfinder/VectorNode.java b/src/main/java/net/citizensnpcs/api/astar/pathfinder/VectorNode.java index ea5ec375..afccaae8 100644 --- a/src/main/java/net/citizensnpcs/api/astar/pathfinder/VectorNode.java +++ b/src/main/java/net/citizensnpcs/api/astar/pathfinder/VectorNode.java @@ -13,23 +13,23 @@ public class VectorNode extends AStarNode implements PathPoint { private float blockCost = -1; - private final BlockSource blockSource; List 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 @@ -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) { @@ -77,8 +77,8 @@ 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; @@ -86,20 +86,20 @@ private float getBlockCost() { @Override public Vector getGoal() { - return goal.goal; + return info.goal.goal; } @Override public Iterable getNeighbours() { List 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 nodes = Lists.newArrayList(); for (PathPoint sub : neighbours) { @@ -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; @@ -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; } \ No newline at end of file