From a1f80d782161dc85a2732344d26b482f4d06ffef Mon Sep 17 00:00:00 2001 From: fullwall Date: Sun, 3 May 2020 02:06:20 +0800 Subject: [PATCH] Bugfixes and micro optimisations --- .../pathfinder/MinecraftBlockExaminer.java | 22 +++++++--------- .../astar/pathfinder/SwimmingExaminer.java | 3 ++- .../api/astar/pathfinder/VectorNode.java | 25 +++++++++++-------- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/citizensnpcs/api/astar/pathfinder/MinecraftBlockExaminer.java b/src/main/java/net/citizensnpcs/api/astar/pathfinder/MinecraftBlockExaminer.java index 36a04487..6bb443bf 100644 --- a/src/main/java/net/citizensnpcs/api/astar/pathfinder/MinecraftBlockExaminer.java +++ b/src/main/java/net/citizensnpcs/api/astar/pathfinder/MinecraftBlockExaminer.java @@ -24,8 +24,8 @@ public class MinecraftBlockExaminer implements BlockExaminer { @Override public float getCost(BlockSource source, PathPoint point) { Vector pos = point.getVector(); - Material above = source.getMaterialAt(pos.clone().add(UP)); - Material below = source.getMaterialAt(pos.clone().add(DOWN)); + Material above = source.getMaterialAt(pos.getBlockX(), pos.getBlockY() + 1, pos.getBlockZ()); + Material below = source.getMaterialAt(pos.getBlockX(), pos.getBlockY() - 1, pos.getBlockZ()); Material in = source.getMaterialAt(pos); if (above == WEB || in == WEB) return 1F; @@ -46,13 +46,13 @@ public PassableState isPassable(BlockSource source, PathPoint point) { if (pos.getBlockY() <= 0 || pos.getBlockY() >= 255) { return PassableState.UNPASSABLE; } - Material above = source.getMaterialAt(pos.clone().add(UP)); - Material below = source.getMaterialAt(pos.clone().add(DOWN)); + Material above = source.getMaterialAt(pos.getBlockX(), pos.getBlockY() + 1, pos.getBlockZ()); + Material below = source.getMaterialAt(pos.getBlockX(), pos.getBlockY() - 1, pos.getBlockZ()); Material in = source.getMaterialAt(pos); if (!below.isBlock() || !canStandOn(below)) { return PassableState.UNPASSABLE; } - if ((isClimbable(above) && isClimbable(in)) || (isClimbable(in) && isClimbable(below))) { + if (isClimbable(in) && (isClimbable(above) || isClimbable(below))) { point.addCallback(new LadderClimber()); } else if (!canStandIn(above) || !canStandIn(in)) { return PassableState.UNPASSABLE; @@ -136,7 +136,6 @@ public static Location findRandomValidLocation(Location base, int xrange, int yr public static Location findRandomValidLocation(Location base, int xrange, int yrange, Function filter, Random random) { - Location found = null; for (int i = 0; i < 10; i++) { int x = base.getBlockX() + random.nextInt(2 * xrange + 1) - xrange; int y = base.getBlockY() + random.nextInt(2 * yrange + 1) - yrange; @@ -146,11 +145,10 @@ public static Location findRandomValidLocation(Location base, int xrange, int yr if (filter != null && !filter.apply(block)) { continue; } - found = block.getLocation().add(0, 1, 0); - break; + return block.getLocation().add(0, 1, 0); } } - return found; + return null; } public static Location findValidLocation(Location location, int radius) { @@ -161,7 +159,7 @@ public static Location findValidLocation(Location location, int radius) { for (int x = -radius; x <= radius; x++) { for (int z = -radius; z <= radius; z++) { Block relative = base.getRelative(x, y, z); - if (canStandOn(base.getRelative(BlockFace.DOWN))) { + if (canStandOn(relative.getRelative(BlockFace.DOWN))) { return relative.getLocation(); } } @@ -178,7 +176,7 @@ public static Location findValidLocation(Location location, int radius, int yrad for (int x = -radius; x <= radius; x++) { for (int z = -radius; z <= radius; z++) { Block relative = base.getRelative(x, y, z); - if (canStandOn(base.getRelative(BlockFace.DOWN))) { + if (canStandOn(relative.getRelative(BlockFace.DOWN))) { return relative.getLocation(); } } @@ -207,12 +205,10 @@ public static boolean validPosition(Block in) { private static final Set DOORS = EnumSet.of(Material.SPRUCE_DOOR, Material.BIRCH_DOOR, Material.JUNGLE_DOOR, Material.ACACIA_DOOR, Material.DARK_OAK_DOOR); - private static final Vector DOWN = new Vector(0, -1, 0); private static final Set LIQUIDS = EnumSet.of(Material.WATER, Material.LAVA); private static final Set NOT_JUMPABLE = EnumSet.of(Material.SPRUCE_FENCE, Material.BIRCH_FENCE, Material.JUNGLE_FENCE, Material.ACACIA_FENCE, Material.DARK_OAK_FENCE); private static final Set UNWALKABLE = EnumSet.of(Material.AIR, Material.LAVA, Material.CACTUS); - private static final Vector UP = new Vector(0, 1, 0); private static Material WEB = SpigotUtil.isUsing1_13API() ? Material.COBWEB : Material.valueOf("WEB"); static { diff --git a/src/main/java/net/citizensnpcs/api/astar/pathfinder/SwimmingExaminer.java b/src/main/java/net/citizensnpcs/api/astar/pathfinder/SwimmingExaminer.java index bc7bffea..3f7c9640 100644 --- a/src/main/java/net/citizensnpcs/api/astar/pathfinder/SwimmingExaminer.java +++ b/src/main/java/net/citizensnpcs/api/astar/pathfinder/SwimmingExaminer.java @@ -23,8 +23,9 @@ public boolean canSwimInLava() { public float getCost(BlockSource source, PathPoint point) { if (SpigotUtil.isUsing1_13API() && npc.getEntity() instanceof WaterMob) { Material in = source.getMaterialAt(point.getVector()); - if (!MinecraftBlockExaminer.isLiquid(in)) + if (!MinecraftBlockExaminer.isLiquid(in)) { return 0.5F; + } } return 0; } 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 18465849..f1430944 100644 --- a/src/main/java/net/citizensnpcs/api/astar/pathfinder/VectorNode.java +++ b/src/main/java/net/citizensnpcs/api/astar/pathfinder/VectorNode.java @@ -16,6 +16,7 @@ public class VectorNode extends AStarNode implements PathPoint { List callbacks; private final PathInfo info; Vector location; + Vector locationClone; public VectorNode(VectorGoal goal, Location location, BlockSource source, BlockExaminer... examiners) { this(null, goal, location.toVector(), source, examiners); @@ -29,7 +30,7 @@ public VectorNode(VectorNode parent, Vector location, PathInfo 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)); + this(parent, location, new PathInfo(source, examiners == null ? EMPTY_BLOCK_EXAMINER : examiners, goal)); } @Override @@ -42,8 +43,7 @@ public void addCallback(PathCallback callback) { @Override public Plan buildPlan() { - Iterable parents = getParents(); - return new Path(parents); + return new Path(getParents()); } @Override @@ -117,18 +117,19 @@ public List getNeighbours(BlockSource source, PathPoint point) { 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.getBlockY() < 0 || mod.getBlockY() > 255) { + int modY = location.getBlockY() + y; + if (modY < 0 || modY > 255) { continue; } + Vector mod = location.clone().add(new Vector(x, y, z)); + if (mod.equals(location)) + continue; if (x != 0 && z != 0) { - if (!isPassable(point.createAtOffset((location.clone().add(new Vector(x, y, 0))))) - || !isPassable(point.createAtOffset((location.clone().add(new Vector(0, y, z)))))) { + if (!isPassable(point.createAtOffset(location.clone().add(new Vector(x, y, 0)))) + || !isPassable(point.createAtOffset(location.clone().add(new Vector(0, y, z))))) { continue; } } - if (mod.equals(location)) - continue; neighbours.add(point.createAtOffset(mod)); } } @@ -143,7 +144,10 @@ public PathPoint getParentPoint() { @Override public Vector getVector() { - return location.clone(); + if (locationClone == null) { + locationClone = location.clone(); + } + return locationClone.setX(location.getBlockX()).setY(location.getBlockY()).setZ(location.getBlockZ()); } @Override @@ -184,5 +188,6 @@ private PathInfo(BlockSource source, BlockExaminer[] examiners, VectorGoal goal) } } + private static final BlockExaminer[] EMPTY_BLOCK_EXAMINER = new BlockExaminer[] {}; private static final float TIEBREAKER = 1.001f; } \ No newline at end of file