cache chunk load check through block state interface

This commit is contained in:
Leijurv 2018-09-23 10:20:19 -07:00
parent c623250387
commit 1a6b7d184a
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
2 changed files with 26 additions and 13 deletions

View File

@ -18,8 +18,6 @@
package baritone.pathing.calc; package baritone.pathing.calc;
import baritone.Baritone; import baritone.Baritone;
import baritone.cache.CachedWorld;
import baritone.cache.WorldProvider;
import baritone.pathing.calc.openset.BinaryHeapOpenSet; import baritone.pathing.calc.openset.BinaryHeapOpenSet;
import baritone.pathing.goals.Goal; import baritone.pathing.goals.Goal;
import baritone.pathing.movement.ActionCosts; import baritone.pathing.movement.ActionCosts;
@ -27,8 +25,6 @@ import baritone.pathing.movement.CalculationContext;
import baritone.pathing.path.IPath; import baritone.pathing.path.IPath;
import baritone.utils.BlockStateInterface; import baritone.utils.BlockStateInterface;
import baritone.utils.Helper; import baritone.utils.Helper;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ChunkProviderClient;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import java.util.HashSet; import java.util.HashSet;
@ -64,8 +60,6 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel
} }
CalculationContext calcContext = new CalculationContext(); CalculationContext calcContext = new CalculationContext();
HashSet<Long> favored = favoredPositions.orElse(null); HashSet<Long> 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(); BlockStateInterface.clearCachedChunk();
long startTime = System.nanoTime() / 1000000L; long startTime = System.nanoTime() / 1000000L;
boolean slowPath = Baritone.settings().slowPath.get(); boolean slowPath = Baritone.settings().slowPath.get();
@ -102,14 +96,11 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel
int newZ = currentNode.z + moves.zOffset; int newZ = currentNode.z + moves.zOffset;
if (newX >> 4 != currentNode.x >> 4 || newZ >> 4 != currentNode.z >> 4) { 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 // 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 if (!BlockStateInterface.isLoaded(newX, newZ)) {
// see issue #106
if (cachedWorld == null || !cachedWorld.isCached(newX, newZ)) { // TODO isCached could call BlockStateInterface to skip a hashmap lookup
numEmptyChunk++; numEmptyChunk++;
continue; continue;
} }
} }
}
MoveResult res = moves.apply(calcContext, currentNode.x, currentNode.y, currentNode.z); MoveResult res = moves.apply(calcContext, currentNode.x, currentNode.y, currentNode.z);
if (res.destX != newX || res.destZ != newZ) { if (res.destX != newX || res.destZ != newZ) {
throw new IllegalStateException(moves + " " + res.destX + " " + newX + " " + res.destZ + " " + newZ); throw new IllegalStateException(moves + " " + res.destX + " " + newX + " " + res.destZ + " " + newZ);
@ -119,7 +110,6 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel
if (actionCost >= ActionCosts.COST_INF) { if (actionCost >= ActionCosts.COST_INF) {
continue; continue;
} }
if (actionCost <= 0) { if (actionCost <= 0) {
throw new IllegalStateException(moves + " calculated implausible cost " + actionCost); throw new IllegalStateException(moves + " calculated implausible cost " + actionCost);
} }

View File

@ -90,6 +90,29 @@ public class BlockStateInterface implements Helper {
return type; 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() { public static void clearCachedChunk() {
prev = null; prev = null;
prevCached = null; prevCached = null;