block state interface cache region too

This commit is contained in:
Leijurv 2018-09-09 09:50:19 -07:00
parent 760f68cb05
commit 07b0e2cdbf
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
2 changed files with 27 additions and 8 deletions

View File

@ -28,6 +28,7 @@ import baritone.pathing.movement.Movement;
import baritone.pathing.movement.MovementHelper; import baritone.pathing.movement.MovementHelper;
import baritone.pathing.movement.movements.*; import baritone.pathing.movement.movements.*;
import baritone.pathing.path.IPath; import baritone.pathing.path.IPath;
import baritone.utils.BlockStateInterface;
import baritone.utils.Helper; import baritone.utils.Helper;
import baritone.utils.pathing.BetterBlockPos; import baritone.utils.pathing.BetterBlockPos;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
@ -72,6 +73,7 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper {
currentlyRunning = this; currentlyRunning = this;
CachedWorld cachedWorld = 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(); ChunkProviderClient chunkProvider = Minecraft.getMinecraft().world.getChunkProvider();
BlockStateInterface.clearCachedChunk();
long startTime = System.nanoTime() / 1000000L; long startTime = System.nanoTime() / 1000000L;
boolean slowPath = Baritone.settings().slowPath.get(); boolean slowPath = Baritone.settings().slowPath.get();
if (slowPath) { if (slowPath) {

View File

@ -18,6 +18,7 @@
package baritone.utils; package baritone.utils;
import baritone.Baritone; import baritone.Baritone;
import baritone.chunk.CachedRegion;
import baritone.chunk.WorldData; import baritone.chunk.WorldData;
import baritone.chunk.WorldProvider; import baritone.chunk.WorldProvider;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@ -35,6 +36,13 @@ import net.minecraft.world.chunk.Chunk;
public class BlockStateInterface implements Helper { public class BlockStateInterface implements Helper {
private static Chunk prev = null; private static Chunk prev = null;
private static CachedRegion prevCached = null;
private static IBlockState AIR = Blocks.AIR.getDefaultState();
public static final Block waterFlowing = Blocks.FLOWING_WATER;
public static final Block waterStill = Blocks.WATER;
public static final Block lavaFlowing = Blocks.FLOWING_LAVA;
public static final Block lavaStill = Blocks.LAVA;
public static IBlockState get(BlockPos pos) { public static IBlockState get(BlockPos pos) {
return get(pos.getX(), pos.getY(), pos.getZ()); return get(pos.getX(), pos.getY(), pos.getZ());
@ -44,7 +52,7 @@ public class BlockStateInterface implements Helper {
// Invalid vertical position // Invalid vertical position
if (y < 0 || y >= 256) { if (y < 0 || y >= 256) {
return Blocks.AIR.getDefaultState(); return AIR;
} }
if (!Baritone.settings().pathThroughCachedOnly.get()) { if (!Baritone.settings().pathThroughCachedOnly.get()) {
@ -63,30 +71,39 @@ public class BlockStateInterface implements Helper {
return chunk.getBlockState(x, y, z); return chunk.getBlockState(x, y, z);
} }
} }
// same idea here, skip the Long2ObjectOpenHashMap.get if at all possible
// except here, it's 512x512 tiles instead of 16x16, so even better repetition
CachedRegion cached = prevCached;
if (cached != null && cached.getX() == x >> 9 && cached.getZ() == z >> 9) {
IBlockState type = cached.getBlock(x & 511, y, z & 511);
if (type == null) {
return AIR;
}
return type;
}
WorldData world = WorldProvider.INSTANCE.getCurrentWorld(); WorldData world = WorldProvider.INSTANCE.getCurrentWorld();
if (world != null) { if (world != null) {
CachedRegion region = world.cache.getRegion(x >> 9, z >> 9);
if (region != null) {
prevCached = region;
}
IBlockState type = world.cache.getBlock(x, y, z); IBlockState type = world.cache.getBlock(x, y, z);
if (type != null) { if (type != null) {
return type; return type;
} }
} }
return AIR;
return Blocks.AIR.getDefaultState();
} }
public static void clearCachedChunk() { public static void clearCachedChunk() {
prev = null; prev = null;
prevCached = null;
} }
public static Block getBlock(BlockPos pos) { public static Block getBlock(BlockPos pos) {
return get(pos).getBlock(); return get(pos).getBlock();
} }
public static final Block waterFlowing = Blocks.FLOWING_WATER;
public static final Block waterStill = Blocks.WATER;
public static final Block lavaFlowing = Blocks.FLOWING_LAVA;
public static final Block lavaStill = Blocks.LAVA;
/** /**
* Returns whether or not the specified block is * Returns whether or not the specified block is