From 1a6b7d184ae33b78e376ca6ca464f8054c30f3d0 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 23 Sep 2018 10:20:19 -0700 Subject: [PATCH] cache chunk load check through block state interface --- .../pathing/calc/AStarPathFinder.java | 16 +++---------- .../baritone/utils/BlockStateInterface.java | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index 37378397..d733931f 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -18,8 +18,6 @@ package baritone.pathing.calc; import baritone.Baritone; -import baritone.cache.CachedWorld; -import baritone.cache.WorldProvider; import baritone.pathing.calc.openset.BinaryHeapOpenSet; import baritone.pathing.goals.Goal; import baritone.pathing.movement.ActionCosts; @@ -27,8 +25,6 @@ import baritone.pathing.movement.CalculationContext; import baritone.pathing.path.IPath; import baritone.utils.BlockStateInterface; import baritone.utils.Helper; -import net.minecraft.client.Minecraft; -import net.minecraft.client.multiplayer.ChunkProviderClient; import net.minecraft.util.math.BlockPos; import java.util.HashSet; @@ -64,8 +60,6 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel } CalculationContext calcContext = new CalculationContext(); HashSet favored = favoredPositions.orElse(null); - CachedWorld cachedWorld = Optional.ofNullable(WorldProvider.INSTANCE.getCurrentWorld()).map(w -> w.cache).orElse(null); - ChunkProviderClient chunkProvider = Minecraft.getMinecraft().world.getChunkProvider(); BlockStateInterface.clearCachedChunk(); long startTime = System.nanoTime() / 1000000L; boolean slowPath = Baritone.settings().slowPath.get(); @@ -102,12 +96,9 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel int newZ = currentNode.z + moves.zOffset; if (newX >> 4 != currentNode.x >> 4 || newZ >> 4 != currentNode.z >> 4) { // only need to check if the destination is a loaded chunk if it's in a different chunk than the start of the movement - if (chunkProvider.isChunkGeneratedAt(newX >> 4, newZ >> 4)) { // TODO could also call BlockStateInterface here - // see issue #106 - if (cachedWorld == null || !cachedWorld.isCached(newX, newZ)) { // TODO isCached could call BlockStateInterface to skip a hashmap lookup - numEmptyChunk++; - continue; - } + if (!BlockStateInterface.isLoaded(newX, newZ)) { + numEmptyChunk++; + continue; } } MoveResult res = moves.apply(calcContext, currentNode.x, currentNode.y, currentNode.z); @@ -119,7 +110,6 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel if (actionCost >= ActionCosts.COST_INF) { continue; } - if (actionCost <= 0) { throw new IllegalStateException(moves + " calculated implausible cost " + actionCost); } diff --git a/src/main/java/baritone/utils/BlockStateInterface.java b/src/main/java/baritone/utils/BlockStateInterface.java index c5d076ea..fda465f4 100644 --- a/src/main/java/baritone/utils/BlockStateInterface.java +++ b/src/main/java/baritone/utils/BlockStateInterface.java @@ -90,6 +90,29 @@ public class BlockStateInterface implements Helper { return type; } + public static boolean isLoaded(int x, int z) { + Chunk prevChunk = prev; + if (prevChunk != null && prevChunk.x == x >> 4 && prevChunk.z == z >> 4) { + return true; + } + if (mc.world.getChunk(x >> 4, z >> 4).isLoaded()) { + return true; + } + CachedRegion prevRegion = prevCached; + if (prevRegion != null && prevRegion.getX() == x >> 9 && prevRegion.getZ() == z >> 9) { + return prevRegion.isCached(x & 511, z & 511); + } + WorldData world = WorldProvider.INSTANCE.getCurrentWorld(); + if (world == null) { + return false; + } + CachedRegion region = world.cache.getRegion(x >> 9, z >> 9); + if (region == null) { + return false; + } + return region.isCached(x & 511, z & 511); + } + public static void clearCachedChunk() { prev = null; prevCached = null;