only search nearby regions

This commit is contained in:
Leijurv 2018-08-23 13:12:13 -07:00
parent e9308d7e70
commit 709822ef47
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
2 changed files with 31 additions and 12 deletions

View File

@ -85,13 +85,32 @@ public final class CachedWorld implements IBlockTypeAccess {
return region.getBlock(x & 511, y, z & 511);
}
public final LinkedList<BlockPos> getLocationsOf(String block) {
public final LinkedList<BlockPos> getLocationsOf(String block, int minimum, int maxRegionDistanceSq) {
LinkedList<BlockPos> res = new LinkedList<>();
this.cachedRegions.values().forEach(region -> {
if (region != null)
for (BlockPos pos : region.getLocationsOf(block))
res.add(pos);
});
int playerRegionX = playerFeet().getX() >> 9;
int playerRegionZ = playerFeet().getZ() >> 9;
int searchRadius = 0;
while (searchRadius <= maxRegionDistanceSq) {
for (int xoff = -searchRadius; xoff <= searchRadius; xoff++) {
for (int zoff = -searchRadius; zoff <= searchRadius; zoff++) {
double distance = xoff * xoff + zoff * zoff;
if (distance != searchRadius) {
continue;
}
int regionX = xoff + playerRegionX;
int regionZ = zoff + playerRegionZ;
CachedRegion region = getOrCreateRegion(regionX, regionZ);
if (region != null)
for (BlockPos pos : region.getLocationsOf(block))
res.add(pos);
}
}
if (res.size() >= minimum) {
return res;
}
searchRadius++;
}
return res;
}
@ -139,7 +158,7 @@ public final class CachedWorld implements IBlockTypeAccess {
* @param regionZ The region Z coordinate
* @return The region located at the specified coordinates
*/
private CachedRegion getOrCreateRegion(int regionX, int regionZ) {
private synchronized CachedRegion getOrCreateRegion(int regionX, int regionZ) {
return cachedRegions.computeIfAbsent(getRegionID(regionX, regionZ), id -> {
CachedRegion newRegion = new CachedRegion(regionX, regionZ);
newRegion.load(this.directory);

View File

@ -120,7 +120,7 @@ public class ExampleBaritoneControl extends Behavior {
}
if (msg.toLowerCase().startsWith("find")) {
String blockType = msg.toLowerCase().substring(4).trim();
LinkedList<BlockPos> locs = WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(blockType);
LinkedList<BlockPos> locs = WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(blockType, 1, 4);
displayChatMessageRaw("Have " + locs.size() + " locations");
for (BlockPos pos : locs) {
Block actually = BlockStateInterface.get(pos).getBlock();
@ -133,7 +133,7 @@ public class ExampleBaritoneControl extends Behavior {
}
if (msg.toLowerCase().startsWith("mine")) {
String blockType = msg.toLowerCase().substring(4).trim();
List<BlockPos> locs = new ArrayList<>(WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(blockType));
List<BlockPos> locs = new ArrayList<>(WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(1, 1));
if (locs.isEmpty()) {
displayChatMessageRaw("No locations known");
event.cancel();
@ -148,9 +148,9 @@ public class ExampleBaritoneControl extends Behavior {
.filter(pos -> !ChunkPacker.blockToString(BlockStateInterface.get(pos).getBlock()).equalsIgnoreCase(blockType))
.collect(Collectors.toList()));
if (locs.size() > 10) {
displayChatMessageRaw("Pathing to any of closest 10");
locs = locs.subList(0, 10);
if (locs.size() > 30) {
displayChatMessageRaw("Pathing to any of closest 30");
locs = locs.subList(0, 30);
}
PathingBehavior.INSTANCE.setGoal(new GoalComposite(locs.stream().map(GoalTwoBlocks::new).toArray(Goal[]::new)));
PathingBehavior.INSTANCE.path();