From a73f5c1359c720d113a7f6ecd576b718deaeea33 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Mon, 4 Mar 2019 21:20:13 -0800 Subject: [PATCH] crapton of performance improvements --- src/api/java/baritone/api/Settings.java | 5 +++++ src/api/java/baritone/api/cache/ICachedWorld.java | 4 ++-- src/main/java/baritone/cache/CachedChunk.java | 4 ++-- src/main/java/baritone/cache/CachedRegion.java | 13 +++++-------- src/main/java/baritone/cache/CachedWorld.java | 7 +++---- src/main/java/baritone/process/MineProcess.java | 5 +++-- .../java/baritone/utils/ExampleBaritoneControl.java | 2 +- 7 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/api/java/baritone/api/Settings.java b/src/api/java/baritone/api/Settings.java index 7b47a2a6..6be16dad 100644 --- a/src/api/java/baritone/api/Settings.java +++ b/src/api/java/baritone/api/Settings.java @@ -535,6 +535,11 @@ public final class Settings { */ public final Setting mineGoalUpdateInterval = new Setting<>(5); + /** + * After finding this many instances of the target block in the cache, it will stop expanding outward the chunk search. + */ + public final Setting maxCachedWorldScanCount = new Setting<>(10); + /** * When GetToBlock doesn't know any locations for the desired block, explore randomly instead of giving up. */ diff --git a/src/api/java/baritone/api/cache/ICachedWorld.java b/src/api/java/baritone/api/cache/ICachedWorld.java index a435ebe0..e681ce51 100644 --- a/src/api/java/baritone/api/cache/ICachedWorld.java +++ b/src/api/java/baritone/api/cache/ICachedWorld.java @@ -20,7 +20,7 @@ package baritone.api.cache; import net.minecraft.util.math.BlockPos; import net.minecraft.world.chunk.Chunk; -import java.util.LinkedList; +import java.util.ArrayList; /** * @author Brady @@ -68,7 +68,7 @@ public interface ICachedWorld { * @param maxRegionDistanceSq The maximum region distance, squared * @return The locations found that match the special block */ - LinkedList getLocationsOf(String block, int maximum, int centerX, int centerZ, int maxRegionDistanceSq); + ArrayList getLocationsOf(String block, int maximum, int centerX, int centerZ, int maxRegionDistanceSq); /** * Reloads all of the cached regions in this world from disk. Anything that is not saved diff --git a/src/main/java/baritone/cache/CachedChunk.java b/src/main/java/baritone/cache/CachedChunk.java index 10c17898..08c80141 100644 --- a/src/main/java/baritone/cache/CachedChunk.java +++ b/src/main/java/baritone/cache/CachedChunk.java @@ -215,11 +215,11 @@ public final class CachedChunk { return specialBlockLocations; } - public final LinkedList getAbsoluteBlocks(String blockType) { + public final ArrayList getAbsoluteBlocks(String blockType) { if (specialBlockLocations.get(blockType) == null) { return null; } - LinkedList res = new LinkedList<>(); + ArrayList res = new ArrayList<>(); for (BlockPos pos : specialBlockLocations.get(blockType)) { res.add(new BlockPos(pos.getX() + x * 16, pos.getY(), pos.getZ() + z * 16)); } diff --git a/src/main/java/baritone/cache/CachedRegion.java b/src/main/java/baritone/cache/CachedRegion.java index d8426197..0081d941 100644 --- a/src/main/java/baritone/cache/CachedRegion.java +++ b/src/main/java/baritone/cache/CachedRegion.java @@ -87,19 +87,16 @@ public final class CachedRegion implements ICachedRegion { return chunks[x >> 4][z >> 4] != null; } - public final LinkedList getLocationsOf(String block) { - LinkedList res = new LinkedList<>(); + public final ArrayList getLocationsOf(String block) { + ArrayList res = new ArrayList<>(); for (int chunkX = 0; chunkX < 32; chunkX++) { for (int chunkZ = 0; chunkZ < 32; chunkZ++) { if (chunks[chunkX][chunkZ] == null) { continue; } - List locs = chunks[chunkX][chunkZ].getAbsoluteBlocks(block); - if (locs == null) { - continue; - } - for (BlockPos pos : locs) { - res.add(pos); + ArrayList locs = chunks[chunkX][chunkZ].getAbsoluteBlocks(block); + if (locs != null) { + res.addAll(locs); } } } diff --git a/src/main/java/baritone/cache/CachedWorld.java b/src/main/java/baritone/cache/CachedWorld.java index db85339d..025ec773 100644 --- a/src/main/java/baritone/cache/CachedWorld.java +++ b/src/main/java/baritone/cache/CachedWorld.java @@ -32,7 +32,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; import java.util.concurrent.LinkedBlockingQueue; @@ -109,8 +108,8 @@ public final class CachedWorld implements ICachedWorld, Helper { } @Override - public final LinkedList getLocationsOf(String block, int maximum, int centerX, int centerZ, int maxRegionDistanceSq) { - LinkedList res = new LinkedList<>(); + public final ArrayList getLocationsOf(String block, int maximum, int centerX, int centerZ, int maxRegionDistanceSq) { + ArrayList res = new ArrayList<>(); int centerRegionX = centerX >> 9; int centerRegionZ = centerZ >> 9; @@ -127,7 +126,7 @@ public final class CachedWorld implements ICachedWorld, Helper { CachedRegion region = getOrCreateRegion(regionX, regionZ); if (region != null) { // TODO: 100% verify if this or addAll is faster. - region.getLocationsOf(block).forEach(res::add); + res.addAll(region.getLocationsOf(block)); } } } diff --git a/src/main/java/baritone/process/MineProcess.java b/src/main/java/baritone/process/MineProcess.java index a299bf31..699cf998 100644 --- a/src/main/java/baritone/process/MineProcess.java +++ b/src/main/java/baritone/process/MineProcess.java @@ -210,7 +210,8 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro //long b = System.currentTimeMillis(); for (Block m : mining) { if (CachedChunk.BLOCKS_TO_KEEP_TRACK_OF.contains(m)) { - locs.addAll(ctx.worldData.getCachedWorld().getLocationsOf(ChunkPacker.blockToString(m), 1, ctx.getBaritone().getPlayerContext().playerFeet().getX(), ctx.getBaritone().getPlayerContext().playerFeet().getZ(), 2)); + // maxRegionDistanceSq 2 means adjacent directly or adjacent diagonally; nothing further than that + locs.addAll(ctx.worldData.getCachedWorld().getLocationsOf(ChunkPacker.blockToString(m), Baritone.settings().maxCachedWorldScanCount.get(), ctx.getBaritone().getPlayerContext().playerFeet().getX(), ctx.getBaritone().getPlayerContext().playerFeet().getZ(), 2)); } else { uninteresting.add(m); } @@ -221,7 +222,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro } if (!uninteresting.isEmpty()) { //long before = System.currentTimeMillis(); - locs.addAll(WorldScanner.INSTANCE.scanChunkRadius(ctx.getBaritone().getPlayerContext(), uninteresting, max, 10, 26)); + locs.addAll(WorldScanner.INSTANCE.scanChunkRadius(ctx.getBaritone().getPlayerContext(), uninteresting, max, 10, 5)); // maxSearchRadius is NOT sq //System.out.println("Scan of loaded chunks took " + (System.currentTimeMillis() - before) + "ms"); } locs.addAll(alreadyKnown); diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 041a5401..61b9ebf1 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -376,7 +376,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { } if (msg.startsWith("find")) { String blockType = msg.substring(4).trim(); - LinkedList locs = baritone.getWorldProvider().getCurrentWorld().getCachedWorld().getLocationsOf(blockType, 1, ctx.playerFeet().getX(), ctx.playerFeet().getZ(), 4); + ArrayList locs = baritone.getWorldProvider().getCurrentWorld().getCachedWorld().getLocationsOf(blockType, 1, ctx.playerFeet().getX(), ctx.playerFeet().getZ(), 4); logDirect("Have " + locs.size() + " locations"); for (BlockPos pos : locs) { Block actually = BlockStateInterface.get(ctx, pos).getBlock();