Skip to content

Commit

Permalink
Add utility methods to pick random locations and migrate WanderGoal t…
Browse files Browse the repository at this point in the history
…o new API
  • Loading branch information
fullwall committed Apr 18, 2020
1 parent 94d0eca commit 1fedd98
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
39 changes: 18 additions & 21 deletions src/main/java/net/citizensnpcs/api/ai/goals/WanderGoal.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class WanderGoal extends BehaviorGoalAdapter implements Listener {
private boolean forceFinish;
private final NPC npc;
private boolean paused;
private final Random random = new Random();
private final Supplier<PhTreeSolid<Boolean>> tree;
private int xrange;
private int yrange;
Expand All @@ -47,26 +46,21 @@ private WanderGoal(NPC npc, int xrange, int yrange, Supplier<PhTreeSolid<Boolean
}

private Location findRandomPosition() {
Location base = npc.getEntity().getLocation();
Location found = null;
for (int i = 0; i < 10; i++) {
int x = base.getBlockX() + random.nextInt(2 * xrange) - xrange;
int y = base.getBlockY() + random.nextInt(2 * yrange) - yrange;
int z = base.getBlockZ() + random.nextInt(2 * xrange) - xrange;
Block block = base.getWorld().getBlockAt(x, y, z);
if (MinecraftBlockExaminer.canStandOn(block)) {
if ((block.getRelative(BlockFace.UP).isLiquid() || block.getRelative(0, 2, 0).isLiquid())
&& npc.getNavigator().getDefaultParameters().avoidWater()) {
continue;
}
long[] pt = { x, y, z };
if (tree != null && tree.get() != null && !tree.get().queryIntersect(pt, pt).hasNext()) {
continue;
}
found = block.getLocation().add(0, 1, 0);
break;
}
}
Location found = MinecraftBlockExaminer.findRandomValidLocation(npc.getEntity().getLocation(NPC_LOCATION),
xrange, yrange, new Function<Block, Boolean>() {
@Override
public Boolean apply(Block block) {
if ((block.getRelative(BlockFace.UP).isLiquid() || block.getRelative(0, 2, 0).isLiquid())
&& npc.getNavigator().getDefaultParameters().avoidWater()) {
return false;
}
long[] pt = { block.getX(), block.getY(), block.getZ() };
if (tree != null && tree.get() != null && !tree.get().queryIntersect(pt, pt).hasNext()) {
return false;
}
return true;
}
}, RANDOM);
if (found == null && fallback != null) {
return fallback.apply(npc);
}
Expand Down Expand Up @@ -157,4 +151,7 @@ public static WanderGoal createWithNPCAndRangeAndTreeAndFallback(NPC npc, int xr
Supplier<PhTreeSolid<Boolean>> tree, Function<NPC, Location> fallback) {
return new WanderGoal(npc, xrange, yrange, tree, fallback);
}

private static final Location NPC_LOCATION = new Location(null, 0, 0, 0);
private static final Random RANDOM = new Random();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.util.EnumSet;
import java.util.ListIterator;
import java.util.Random;
import java.util.Set;
import java.util.function.Function;

import org.bukkit.Location;
import org.bukkit.Material;
Expand Down Expand Up @@ -123,6 +125,34 @@ public static boolean canStandOn(Material mat) {
return !UNWALKABLE.contains(mat) && mat.isSolid();
}

public static Location findRandomValidLocation(Location base, int xrange, int yrange) {
return findRandomValidLocation(base, xrange, yrange, null, new Random());
}

public static Location findRandomValidLocation(Location base, int xrange, int yrange,
Function<Block, Boolean> filter) {
return findRandomValidLocation(base, xrange, yrange, filter, new Random());
}

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) - xrange;
int y = base.getBlockY() + random.nextInt(2 * yrange) - yrange;
int z = base.getBlockZ() + random.nextInt(2 * xrange) - xrange;
Block block = base.getWorld().getBlockAt(x, y, z);
if (MinecraftBlockExaminer.canStandOn(block)) {
if (filter != null && !filter.apply(block)) {
continue;
}
found = block.getLocation().add(0, 1, 0);
break;
}
}
return found;
}

public static Location findValidLocation(Location location, int radius) {
Block base = location.getBlock();
if (canStandOn(base.getRelative(BlockFace.DOWN)))
Expand Down

0 comments on commit 1fedd98

Please sign in to comment.