From 55a677308609250e6514795ce3ef22e2f74a3c13 Mon Sep 17 00:00:00 2001 From: makamys Date: Mon, 10 May 2021 16:54:16 +0200 Subject: Unload regions based on distance --- src/main/java/makamys/lodmod/renderer/LODRegion.java | 13 +++++++++++++ src/main/java/makamys/lodmod/renderer/LODRenderer.java | 16 +++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main/java/makamys/lodmod/renderer/LODRegion.java b/src/main/java/makamys/lodmod/renderer/LODRegion.java index 0d91e22..b797531 100644 --- a/src/main/java/makamys/lodmod/renderer/LODRegion.java +++ b/src/main/java/makamys/lodmod/renderer/LODRegion.java @@ -149,4 +149,17 @@ public class LODRegion { } } + public double distanceTaxicab(Entity entity) { + double centerX = ((regionX * 32) + 16) * 16; + double centerZ = ((regionZ * 32) + 16) * 16; + + return Math.max(Math.abs(centerX - entity.posX), Math.abs(centerZ - entity.posZ)); + + } + + @Override + public String toString() { + return "LODRegion(" + regionX + ", " + regionZ + ")"; + } + } diff --git a/src/main/java/makamys/lodmod/renderer/LODRenderer.java b/src/main/java/makamys/lodmod/renderer/LODRenderer.java index 56ca600..3a0ff9d 100644 --- a/src/main/java/makamys/lodmod/renderer/LODRenderer.java +++ b/src/main/java/makamys/lodmod/renderer/LODRenderer.java @@ -159,12 +159,14 @@ public class LODRenderer { for(int x = -renderRange; x <= renderRange; x++) { for(int z = -renderRange; z <= renderRange; z++) { - int chunkX = centerX + x; - int chunkZ = centerZ + z; - - if(getLODChunk(chunkX, chunkZ).needsChunk) { - newServerChunkLoadQueue.add(new ChunkCoordIntPair(chunkX, chunkZ)); - getLODChunk(chunkX, chunkZ).needsChunk = false; + if(x * x + z * z < renderRange * renderRange) { + int chunkX = centerX + x; + int chunkZ = centerZ + z; + + if(getLODChunk(chunkX, chunkZ).needsChunk) { + newServerChunkLoadQueue.add(new ChunkCoordIntPair(chunkX, chunkZ)); + getLODChunk(chunkX, chunkZ).needsChunk = false; + } } } } @@ -178,7 +180,7 @@ public class LODRenderer { ChunkCoordIntPair k = it.next(); LODRegion v = loadedRegionsMap.get(k); - if(!v.tick(player)) { + if(v.distanceTaxicab(player) > renderRange * 16 + 16 * 16) { System.out.println("unloading " + v); v.destroy(getSaveDir()); it.remove(); -- cgit