From e553ee93b82a8669f3cd975e2dd0a8d8667f3541 Mon Sep 17 00:00:00 2001 From: leijurv Date: Tue, 28 Aug 2018 21:10:41 -0700 Subject: [PATCH 1/2] 20% improvement to cached chunk check performance --- .../utils/pathing/PathingBlockType.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/baritone/utils/pathing/PathingBlockType.java b/src/main/java/baritone/utils/pathing/PathingBlockType.java index a05b0b2b..61dbf2fc 100644 --- a/src/main/java/baritone/utils/pathing/PathingBlockType.java +++ b/src/main/java/baritone/utils/pathing/PathingBlockType.java @@ -23,7 +23,7 @@ package baritone.utils.pathing; */ public enum PathingBlockType { - AIR (0b00), + AIR(0b00), WATER(0b01), AVOID(0b10), SOLID(0b11); @@ -31,7 +31,7 @@ public enum PathingBlockType { private final boolean[] bits; PathingBlockType(int bits) { - this.bits = new boolean[] { + this.bits = new boolean[]{ (bits & 0b10) != 0, (bits & 0b01) != 0 }; @@ -42,11 +42,18 @@ public enum PathingBlockType { } public static PathingBlockType fromBits(boolean b1, boolean b2) { - for (PathingBlockType type : values()) - if (type.bits[0] == b1 && type.bits[1] == b2) - return type; - - // This will never happen, but if it does, assume it's just AIR - return PathingBlockType.AIR; + if (b1) { + if (b2) { + return PathingBlockType.SOLID; + } else { + return PathingBlockType.AVOID; + } + } else { + if (b2) { + return PathingBlockType.WATER; + } else { + return PathingBlockType.AIR; + } + } } } From 8d1570a11bbc313a6e45288f33d462d4c5a16738 Mon Sep 17 00:00:00 2001 From: leijurv Date: Tue, 28 Aug 2018 21:14:50 -0700 Subject: [PATCH 2/2] cherry pick 3x faster cache check --- src/main/java/baritone/chunk/CachedRegion.java | 4 ++++ src/main/java/baritone/chunk/CachedWorld.java | 14 +++++++++++++- .../baritone/pathing/calc/AStarPathFinder.java | 6 ++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/baritone/chunk/CachedRegion.java b/src/main/java/baritone/chunk/CachedRegion.java index 66204d5a..3df6d68a 100644 --- a/src/main/java/baritone/chunk/CachedRegion.java +++ b/src/main/java/baritone/chunk/CachedRegion.java @@ -77,6 +77,10 @@ public final class CachedRegion implements IBlockTypeAccess { return null; } + public final boolean isCached(int x, int z) { + return chunks[x >> 4][z >> 4] != null; + } + public final LinkedList getLocationsOf(String block) { LinkedList res = new LinkedList<>(); for (int chunkX = 0; chunkX < 32; chunkX++) { diff --git a/src/main/java/baritone/chunk/CachedWorld.java b/src/main/java/baritone/chunk/CachedWorld.java index 42b4c13d..703eab0d 100644 --- a/src/main/java/baritone/chunk/CachedWorld.java +++ b/src/main/java/baritone/chunk/CachedWorld.java @@ -59,7 +59,8 @@ public final class CachedWorld implements IBlockTypeAccess { if (!Files.exists(directory)) { try { Files.createDirectories(directory); - } catch (IOException ignored) {} + } catch (IOException ignored) { + } } this.directory = directory.toString(); System.out.println("Cached world directory: " + directory); @@ -102,6 +103,17 @@ public final class CachedWorld implements IBlockTypeAccess { return region.getBlock(x & 511, y, z & 511); } + public final boolean isCached(BlockPos pos) { + int x = pos.getX(); + int z = pos.getZ(); + CachedRegion region = getRegion(x >> 9, z >> 9); + if (region == null) { + return false; + } + return region.isCached(x & 511, z & 511); + } + + public final LinkedList getLocationsOf(String block, int minimum, int maxRegionDistanceSq) { LinkedList res = new LinkedList<>(); int playerRegionX = playerFeet().getX() >> 9; diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index 383c3b90..c708d3e9 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -18,6 +18,7 @@ package baritone.pathing.calc; import baritone.Baritone; +import baritone.chunk.CachedWorld; import baritone.chunk.WorldProvider; import baritone.pathing.calc.openset.BinaryHeapOpenSet; import baritone.pathing.calc.openset.IOpenSet; @@ -70,6 +71,7 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper { CalculationContext calcContext = new CalculationContext(); HashSet favored = favoredPositions.orElse(null); currentlyRunning = this; + CachedWorld world = Optional.ofNullable(WorldProvider.INSTANCE.getCurrentWorld()).map(w -> w.cache).orElse(null); long startTime = System.currentTimeMillis(); boolean slowPath = Baritone.settings().slowPath.get(); long timeoutTime = startTime + (slowPath ? Baritone.settings().slowPathTimeoutMS : Baritone.settings().pathTimeoutMS).get(); @@ -112,8 +114,8 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper { } BetterBlockPos dest = (BetterBlockPos) movementToGetToNeighbor.getDest(); boolean isPositionCached = false; - if (WorldProvider.INSTANCE.getCurrentWorld() != null) { - if (WorldProvider.INSTANCE.getCurrentWorld().cache.getBlock(dest) != null) { + if (world != null) { + if (world.isCached(dest)) { isPositionCached = true; } }