Skip to content

Commit

Permalink
Bugfixes and micro optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed May 2, 2020
1 parent 0a4010b commit a1f80d7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Block, Boolean> 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;
Expand All @@ -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) {
Expand All @@ -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();
}
}
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -207,12 +205,10 @@ public static boolean validPosition(Block in) {

private static final Set<Material> 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<Material> LIQUIDS = EnumSet.of(Material.WATER, Material.LAVA);
private static final Set<Material> NOT_JUMPABLE = EnumSet.of(Material.SPRUCE_FENCE, Material.BIRCH_FENCE,
Material.JUNGLE_FENCE, Material.ACACIA_FENCE, Material.DARK_OAK_FENCE);
private static final Set<Material> 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 {
Expand Down
Expand Up @@ -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;
}
Expand Down
25 changes: 15 additions & 10 deletions src/main/java/net/citizensnpcs/api/astar/pathfinder/VectorNode.java
Expand Up @@ -16,6 +16,7 @@ public class VectorNode extends AStarNode implements PathPoint {
List<PathCallback> 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);
Expand All @@ -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
Expand All @@ -42,8 +43,7 @@ public void addCallback(PathCallback callback) {

@Override
public Plan buildPlan() {
Iterable<VectorNode> parents = getParents();
return new Path(parents);
return new Path(getParents());
}

@Override
Expand Down Expand Up @@ -117,18 +117,19 @@ public List<PathPoint> 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));
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

0 comments on commit a1f80d7

Please sign in to comment.