From 3cac37d1a5c87f83257d4c7c5086ffc841ee9bec Mon Sep 17 00:00:00 2001 From: Leijurv Date: Mon, 15 Oct 2018 15:19:10 -0700 Subject: [PATCH] cached regions in ram should not expand without bound --- .../java/baritone/cache/CachedRegion.java | 15 +++++++ src/main/java/baritone/cache/CachedWorld.java | 42 +++++++++++++++++++ .../pathing/movement/MovementHelper.java | 7 ++-- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/main/java/baritone/cache/CachedRegion.java b/src/main/java/baritone/cache/CachedRegion.java index 200e0f16..c25139ac 100644 --- a/src/main/java/baritone/cache/CachedRegion.java +++ b/src/main/java/baritone/cache/CachedRegion.java @@ -319,6 +319,21 @@ public final class CachedRegion implements ICachedRegion { } } + public synchronized final CachedChunk mostRecentlyModified() { + CachedChunk recent = null; + for (int x = 0; x < 32; x++) { + for (int z = 0; z < 32; z++) { + if (this.chunks[x][z] == null) { + continue; + } + if (recent == null || this.chunks[x][z].cacheTimestamp > recent.cacheTimestamp) { + recent = this.chunks[x][z]; + } + } + } + return recent; + } + /** * @return The region x coordinate */ diff --git a/src/main/java/baritone/cache/CachedWorld.java b/src/main/java/baritone/cache/CachedWorld.java index cb05360b..60126fca 100644 --- a/src/main/java/baritone/cache/CachedWorld.java +++ b/src/main/java/baritone/cache/CachedWorld.java @@ -147,6 +147,7 @@ public final class CachedWorld implements ICachedWorld, Helper { region.removeExpired(); } }); // even if we aren't saving to disk, still delete expired old chunks from RAM + prune(); return; } long start = System.nanoTime() / 1000000L; @@ -157,6 +158,47 @@ public final class CachedWorld implements ICachedWorld, Helper { }); long now = System.nanoTime() / 1000000L; System.out.println("World save took " + (now - start) + "ms"); + prune(); + } + + /** + * Delete regions that are too far from the player + */ + private synchronized void prune() { + BlockPos pruneCenter = guessPosition(); + for (CachedRegion region : allRegions()) { + int distX = (region.getX() * 512 + 256) - pruneCenter.getX(); + int distZ = (region.getZ() * 512 + 256) - pruneCenter.getZ(); + double dist = Math.sqrt(distX * distX + distZ * distZ); + if (dist > 1024) { + logDebug("Deleting cached region " + region.getX() + "," + region.getZ() + " from ram"); + cachedRegions.remove(getRegionID(region.getX(), region.getZ())); + } + } + } + + /** + * If we are still in this world and dimension, return player feet, otherwise return most recently modified chunk + */ + private BlockPos guessPosition() { + WorldData data = WorldProvider.INSTANCE.getCurrentWorld(); + if (data != null && data.getCachedWorld() == this) { + return playerFeet(); + } + CachedChunk mostRecentlyModified = null; + for (CachedRegion region : allRegions()) { + CachedChunk ch = region.mostRecentlyModified(); + if (ch == null) { + continue; + } + if (mostRecentlyModified == null || mostRecentlyModified.cacheTimestamp < ch.cacheTimestamp) { + mostRecentlyModified = ch; + } + } + if (mostRecentlyModified == null) { + return new BlockPos(0, 0, 0); + } + return new BlockPos(mostRecentlyModified.x * 16 + 8, 0, mostRecentlyModified.z * 16 + 8); } private synchronized List allRegions() { diff --git a/src/main/java/baritone/pathing/movement/MovementHelper.java b/src/main/java/baritone/pathing/movement/MovementHelper.java index 08d6f916..c2e3450c 100644 --- a/src/main/java/baritone/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/pathing/movement/MovementHelper.java @@ -21,7 +21,10 @@ import baritone.Baritone; import baritone.api.pathing.movement.ActionCosts; import baritone.api.utils.*; import baritone.pathing.movement.MovementState.MovementTarget; -import baritone.utils.*; +import baritone.utils.BlockStateInterface; +import baritone.utils.Helper; +import baritone.utils.InputOverrideHandler; +import baritone.utils.ToolSet; import net.minecraft.block.*; import net.minecraft.block.properties.PropertyBool; import net.minecraft.block.state.IBlockState; @@ -35,8 +38,6 @@ import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.world.chunk.EmptyChunk; -import java.util.Optional; - /** * Static helpers for cost calculation *