locations cache

This commit is contained in:
Leijurv 2018-09-15 12:56:35 -07:00
parent 27b9324cf1
commit 43cf2062bc
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A

View File

@ -51,6 +51,7 @@ public final class MineBehavior extends Behavior implements Helper {
} }
private List<Block> mining; private List<Block> mining;
private List<BlockPos> locationsCache;
@Override @Override
public void onTick(TickEvent event) { public void onTick(TickEvent event) {
@ -92,12 +93,18 @@ public final class MineBehavior extends Behavior implements Helper {
if (mining == null) { if (mining == null) {
return; return;
} }
if (!locationsCache.isEmpty()) {
locationsCache = prune(new ArrayList<>(locationsCache), mining, 64);
PathingBehavior.INSTANCE.setGoal(new GoalComposite(locationsCache.stream().map(GoalTwoBlocks::new).toArray(Goal[]::new)));
PathingBehavior.INSTANCE.path();
}
List<BlockPos> locs = scanFor(mining, 64); List<BlockPos> locs = scanFor(mining, 64);
if (locs.isEmpty()) { if (locs.isEmpty()) {
logDebug("No locations for " + mining + " known, cancelling"); logDebug("No locations for " + mining + " known, cancelling");
cancel(); cancel();
return; return;
} }
locationsCache = locs;
PathingBehavior.INSTANCE.setGoal(new GoalComposite(locs.stream().map(GoalTwoBlocks::new).toArray(Goal[]::new))); PathingBehavior.INSTANCE.setGoal(new GoalComposite(locs.stream().map(GoalTwoBlocks::new).toArray(Goal[]::new)));
PathingBehavior.INSTANCE.path(); PathingBehavior.INSTANCE.path();
} }
@ -119,6 +126,10 @@ public final class MineBehavior extends Behavior implements Helper {
locs.addAll(WorldScanner.INSTANCE.scanLoadedChunks(uninteresting, max)); locs.addAll(WorldScanner.INSTANCE.scanLoadedChunks(uninteresting, max));
//System.out.println("Scan of loaded chunks took " + (System.currentTimeMillis() - before) + "ms"); //System.out.println("Scan of loaded chunks took " + (System.currentTimeMillis() - before) + "ms");
} }
return prune(locs, mining, max);
}
public static List<BlockPos> prune(List<BlockPos> locs, List<Block> mining, int max) {
BlockPos playerFeet = MineBehavior.INSTANCE.playerFeet(); BlockPos playerFeet = MineBehavior.INSTANCE.playerFeet();
locs.sort(Comparator.comparingDouble(playerFeet::distanceSq)); locs.sort(Comparator.comparingDouble(playerFeet::distanceSq));
@ -135,11 +146,13 @@ public final class MineBehavior extends Behavior implements Helper {
public void mine(String... blocks) { public void mine(String... blocks) {
this.mining = blocks == null || blocks.length == 0 ? null : Arrays.stream(blocks).map(ChunkPacker::stringToBlock).collect(Collectors.toList()); this.mining = blocks == null || blocks.length == 0 ? null : Arrays.stream(blocks).map(ChunkPacker::stringToBlock).collect(Collectors.toList());
this.locationsCache = new ArrayList<>();
updateGoal(); updateGoal();
} }
public void mine(Block... blocks) { public void mine(Block... blocks) {
this.mining = blocks == null || blocks.length == 0 ? null : Arrays.asList(blocks); this.mining = blocks == null || blocks.length == 0 ? null : Arrays.asList(blocks);
this.locationsCache = new ArrayList<>();
updateGoal(); updateGoal();
} }