From fb04ec6ff4e84950d0e8169665a910f3202b4f1f Mon Sep 17 00:00:00 2001 From: Leijurv Date: Wed, 29 Aug 2018 11:29:26 -0700 Subject: [PATCH 1/3] chunk check optimization --- .../pathing/calc/AStarPathFinder.java | 19 +++++++++++-------- .../utils/pathing/BetterBlockPos.java | 6 +++--- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index c708d3e9..8d0a6989 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -113,15 +113,18 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper { continue; } BetterBlockPos dest = (BetterBlockPos) movementToGetToNeighbor.getDest(); - boolean isPositionCached = false; - if (world != null) { - if (world.isCached(dest)) { - isPositionCached = true; + if (dest.x >> 4 != currentNodePos.x >> 4 || dest.z >> 4 != currentNodePos.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 + boolean isPositionCached = false; + if (world != null) { + if (world.isCached(dest)) { + isPositionCached = true; + } + } + if (!isPositionCached && Minecraft.getMinecraft().world.getChunk(dest) instanceof EmptyChunk) { + numEmptyChunk++; + continue; } - } - if (!isPositionCached && Minecraft.getMinecraft().world.getChunk(dest) instanceof EmptyChunk) { - numEmptyChunk++; - continue; } //long costStart = System.nanoTime(); // TODO cache cost diff --git a/src/main/java/baritone/utils/pathing/BetterBlockPos.java b/src/main/java/baritone/utils/pathing/BetterBlockPos.java index 93512984..8518f668 100644 --- a/src/main/java/baritone/utils/pathing/BetterBlockPos.java +++ b/src/main/java/baritone/utils/pathing/BetterBlockPos.java @@ -27,9 +27,9 @@ import net.minecraft.util.math.Vec3i; * @author leijurv */ public class BetterBlockPos extends BlockPos { - private final int x; - private final int y; - private final int z; + public final int x; + public final int y; + public final int z; private final int hashCode; public BetterBlockPos(int x, int y, int z) { From 81b0e14c9a56aeeec9073aa6b3cb0c6b098aebd7 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Wed, 29 Aug 2018 11:53:37 -0700 Subject: [PATCH 2/3] more debug info --- src/main/java/baritone/pathing/calc/AStarPathFinder.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index 8d0a6989..1f441bd6 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -101,6 +101,7 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper { } if (goal.isInGoal(currentNodePos)) { currentlyRunning = null; + displayChatMessageRaw("Took " + (System.currentTimeMillis() - startTime) + "ms, " + numMovementsConsidered + " movements considered"); return Optional.of(new Path(startNode, currentNode, numNodes)); } //long constructStart = System.nanoTime(); @@ -193,7 +194,7 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper { bestDist = dist; } if (dist > MIN_DIST_PATH * MIN_DIST_PATH) { // square the comparison since distFromStartSq is squared - displayChatMessageRaw("Took " + (System.currentTimeMillis() - startTime) + "ms, A* cost coefficient " + COEFFICIENTS[i]); + displayChatMessageRaw("Took " + (System.currentTimeMillis() - startTime) + "ms, A* cost coefficient " + COEFFICIENTS[i] + ", " + numMovementsConsidered + " movements considered"); if (COEFFICIENTS[i] >= 3) { System.out.println("Warning: cost coefficient is greater than three! Probably means that"); System.out.println("the path I found is pretty terrible (like sneak-bridging for dozens of blocks)"); From 0342136edc207ed1574393c9129af519259347c0 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Wed, 29 Aug 2018 12:19:21 -0700 Subject: [PATCH 3/3] fix chunk cache check performance, fixes #106 --- .../pathing/calc/AStarPathFinder.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index 1f441bd6..0a146ff0 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -32,9 +32,9 @@ import baritone.pathing.path.IPath; import baritone.utils.Helper; import baritone.utils.pathing.BetterBlockPos; import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ChunkProviderClient; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; -import net.minecraft.world.chunk.EmptyChunk; import java.util.Collection; import java.util.HashSet; @@ -71,11 +71,12 @@ 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); + CachedWorld cachedWorld = Optional.ofNullable(WorldProvider.INSTANCE.getCurrentWorld()).map(w -> w.cache).orElse(null); + ChunkProviderClient chunkProvider = Minecraft.getMinecraft().world.getChunkProvider(); long startTime = System.currentTimeMillis(); boolean slowPath = Baritone.settings().slowPath.get(); long timeoutTime = startTime + (slowPath ? Baritone.settings().slowPathTimeoutMS : Baritone.settings().pathTimeoutMS).get(); - long lastPrintout = 0; + //long lastPrintout = 0; int numNodes = 0; int numMovementsConsidered = 0; int numEmptyChunk = 0; @@ -95,10 +96,10 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper { mostRecentConsidered = currentNode; BetterBlockPos currentNodePos = currentNode.pos; numNodes++; - if (System.currentTimeMillis() > lastPrintout + 1000) {//print once a second + /*if (System.currentTimeMillis() > lastPrintout + 1000) {//print once a second System.out.println("searching... at " + currentNodePos + ", considered " + numNodes + " nodes so far"); lastPrintout = System.currentTimeMillis(); - } + }*/ if (goal.isInGoal(currentNodePos)) { currentlyRunning = null; displayChatMessageRaw("Took " + (System.currentTimeMillis() - startTime) + "ms, " + numMovementsConsidered + " movements considered"); @@ -114,18 +115,17 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper { continue; } BetterBlockPos dest = (BetterBlockPos) movementToGetToNeighbor.getDest(); - if (dest.x >> 4 != currentNodePos.x >> 4 || dest.z >> 4 != currentNodePos.z >> 4) { + int chunkX = currentNodePos.x >> 4; + int chunkZ = currentNodePos.z >> 4; + if (dest.x >> 4 != chunkX || dest.z >> 4 != chunkZ) { // only need to check if the destination is a loaded chunk if it's in a different chunk than the start of the movement - boolean isPositionCached = false; - if (world != null) { - if (world.isCached(dest)) { - isPositionCached = true; + if (chunkProvider.getLoadedChunk(chunkX, chunkZ) == null) { + // see issue #106 + if (cachedWorld == null || !cachedWorld.isCached(dest)) { + numEmptyChunk++; + continue; } } - if (!isPositionCached && Minecraft.getMinecraft().world.getChunk(dest) instanceof EmptyChunk) { - numEmptyChunk++; - continue; - } } //long costStart = System.nanoTime(); // TODO cache cost