diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/makamys/lodmod/renderer/LODChunk.java | 65 | ||||
-rw-r--r-- | src/main/java/makamys/lodmod/renderer/LODRegion.java | 13 | ||||
-rw-r--r-- | src/main/java/makamys/lodmod/renderer/LODRenderer.java | 8 |
3 files changed, 61 insertions, 25 deletions
diff --git a/src/main/java/makamys/lodmod/renderer/LODChunk.java b/src/main/java/makamys/lodmod/renderer/LODChunk.java index 1e49406..7abc662 100644 --- a/src/main/java/makamys/lodmod/renderer/LODChunk.java +++ b/src/main/java/makamys/lodmod/renderer/LODChunk.java @@ -15,6 +15,8 @@ public class LODChunk { public boolean needsChunk = true; int lod = 0; boolean visible; + boolean dirty; + boolean discardedMesh; SimpleChunkMesh[] simpleMeshes = new SimpleChunkMesh[2]; ChunkMesh[] chunkMeshes = new ChunkMesh[32]; @@ -32,16 +34,18 @@ public class LODChunk { this.x = nbt.getInteger("x"); this.z = nbt.getInteger("z"); - NBTTagCompound chunkMeshesCompound = nbt.getCompoundTag("chunkMeshes"); + loadChunkMeshesNBT(nbt.getCompoundTag("chunkMeshes"), spriteList); + } + + private void loadChunkMeshesNBT(NBTTagCompound chunkMeshesCompound, List<String> spriteList) { for(Object o : chunkMeshesCompound.func_150296_c()) { - String key = (String)o; - int keyInt = Integer.parseInt(key); - - byte[] data = chunkMeshesCompound.getByteArray(key); - - chunkMeshes[keyInt] = new ChunkMesh(x, keyInt / 2, z, new ChunkMesh.Flags(true, true, true, false), data.length / (2 + 4 * (3 + 2 + 2 + 4)), data, spriteList, keyInt % 2); - } - + String key = (String)o; + int keyInt = Integer.parseInt(key); + + byte[] data = chunkMeshesCompound.getByteArray(key); + + chunkMeshes[keyInt] = new ChunkMesh(x, keyInt / 2, z, new ChunkMesh.Flags(true, true, true, false), data.length / (2 + 4 * (3 + 2 + 2 + 4)), data, spriteList, keyInt % 2); + } } @Override @@ -67,6 +71,8 @@ public class LODChunk { chunkMeshes[cy * 2 + i] = newChunkMesh; } LODMod.renderer.lodChunkChanged(this); + dirty = true; + discardedMesh = false; } // nice copypasta @@ -98,25 +104,50 @@ public class LODChunk { public void tick(Entity player) { double distSq = distSq(player); if(distSq < Math.pow((LODMod.renderer.renderRange / 2) * 16, 2)) { - renderer.setLOD(this, 2); + setLOD(2); } else if(distSq < Math.pow((LODMod.renderer.renderRange) * 16, 2)) { - renderer.setLOD(this, 1); + setLOD(1); } else { - renderer.setLOD(this, 0); + setLOD(0); } } - public NBTTagCompound saveToNBT() { + public void setLOD(int lod) { + if(lod == this.lod) return; + + this.lod = lod; + LODMod.renderer.lodChunkChanged(this); + if(!dirty) { + if(lod < 2) { + for(int i = 0; i < chunkMeshes.length; i++) { + if(chunkMeshes[i] != null) { + chunkMeshes[i].destroy(); + chunkMeshes[i] = null; + discardedMesh = true; + } + } + } + } + } + + public NBTTagCompound saveToNBT(NBTTagCompound oldNbt, List<String> oldStringTable) { NBTTagCompound nbt = new NBTTagCompound(); nbt.setInteger("x", x); nbt.setInteger("z", z); - NBTTagCompound chunkMeshesCompound = new NBTTagCompound(); - for(int i = 0; i < chunkMeshes.length; i++) { - if(chunkMeshes[i] != null) { - chunkMeshesCompound.setTag(String.valueOf(i), chunkMeshes[i].nbtData); + + NBTTagCompound chunkMeshesCompound = oldNbt == null ? new NBTTagCompound() : oldNbt.getCompoundTag("chunkMeshes"); + if(!discardedMesh) { + for(int i = 0; i < chunkMeshes.length; i++) { + if(chunkMeshes[i] != null) { + chunkMeshesCompound.setTag(String.valueOf(i), chunkMeshes[i].nbtData); + } } + } else if(oldNbt != null && discardedMesh && lod == 2) { + loadChunkMeshesNBT(chunkMeshesCompound, oldStringTable); + LODMod.renderer.lodChunkChanged(this); } nbt.setTag("chunkMeshes", chunkMeshesCompound); + dirty = false; return nbt; } diff --git a/src/main/java/makamys/lodmod/renderer/LODRegion.java b/src/main/java/makamys/lodmod/renderer/LODRegion.java index b797531..2fa4f01 100644 --- a/src/main/java/makamys/lodmod/renderer/LODRegion.java +++ b/src/main/java/makamys/lodmod/renderer/LODRegion.java @@ -79,15 +79,26 @@ public class LODRegion { File saveFile = getSavePath(saveDir, regionX, regionZ).toFile(); saveFile.getParentFile().mkdirs(); + NBTTagCompound oldNbt = null; + NBTTagList oldList = null; + List<String> oldStringTable = null; + if(saveFile.exists()) { + oldNbt = CompressedStreamTools.readCompressed(new FileInputStream(saveFile)); + oldList = oldNbt.getTagList("chunks", NBT.TAG_COMPOUND);; + oldStringTable = Arrays.asList(oldNbt.getString("stringTable").split("\\n")); + } + NBTTagCompound nbt = new NBTTagCompound(); nbt.setByte("V", (byte)0); nbt.setString("stringTable", String.join("\n", (List<String>) ((TextureMap)Minecraft.getMinecraft().getTextureManager().getTexture(TextureMap.locationBlocksTexture)).mapUploadedSprites.keySet().stream().collect(Collectors.toList()))); NBTTagList list = new NBTTagList(); + int idx = 0; for(int i = 0; i < 32; i++) { for(int j = 0; j < 32; j++) { - list.appendTag(data[i][j].saveToNBT()); + list.appendTag(data[i][j].saveToNBT(oldNbt == null ? null : oldList.getCompoundTagAt(idx++), + oldNbt == null? null : oldStringTable)); } } nbt.setTag("chunks", list); diff --git a/src/main/java/makamys/lodmod/renderer/LODRenderer.java b/src/main/java/makamys/lodmod/renderer/LODRenderer.java index 7cbb29d..4b2e016 100644 --- a/src/main/java/makamys/lodmod/renderer/LODRenderer.java +++ b/src/main/java/makamys/lodmod/renderer/LODRenderer.java @@ -108,6 +108,7 @@ public class LODRenderer { gcInterval = 10 * 1000; if(lastGCTime == -1 || (System.currentTimeMillis() - lastGCTime) > gcInterval) { runGC(); + onSave(); lastGCTime = System.currentTimeMillis(); } @@ -496,13 +497,6 @@ public class LODRenderer { setVisible(lodChunk, true, true); } - public void setLOD(LODChunk lodChunk, int lod) { - if(lod == lodChunk.lod) return; - - lodChunk.lod = lod; - lodChunkChanged(lodChunk); - } - public void setVisible(LODChunk chunk, boolean visible) { setVisible(chunk, visible, false); } |