Skip to content

Commit

Permalink
fix(Minimap): no free available atlas positions (CCBlueX#4726)
Browse files Browse the repository at this point in the history
  • Loading branch information
MukjepScarlet authored and DataM0del committed Dec 12, 2024
1 parent ec1a1ca commit b10fe95
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package net.ccbluex.liquidbounce.integration.theme.component.types.minimap

import net.ccbluex.liquidbounce.utils.client.logger
import net.ccbluex.liquidbounce.utils.client.mc
import net.minecraft.block.BlockState
import net.minecraft.block.MapColor
Expand Down Expand Up @@ -123,15 +124,19 @@ class MinimapHeightmapManager {

val pos = BlockPos.Mutable(x, maxHeight, z)

while (pos.y > chunk.bottomY) {
val state = chunk.getBlockState(pos)
if (isSurface(pos, state)) {
return pos.y
try {
while (pos.y > chunk.bottomY) {
val state = chunk.getBlockState(pos)
if (isSurface(pos, state)) {
return pos.y
}
pos.y--
}
pos.y--
} catch (e: Exception) {
logger.warn("Exception in height calculation", e)
}

return chunk.bottomY
return pos.y
}

private fun isSurface(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import net.ccbluex.liquidbounce.utils.math.Vec2i
import net.minecraft.client.texture.NativeImage
import net.minecraft.client.texture.NativeImageBackedTexture
import net.minecraft.util.math.ChunkPos
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.Semaphore
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read
import kotlin.concurrent.write
Expand All @@ -40,11 +42,13 @@ private const val ATLAS_SIZE: Int = 64
*/
private const val FULL_UPLOAD_THRESHOLD: Int = 15

private const val MAX_ATLAS_POSITIONS: Int = ATLAS_SIZE * ATLAS_SIZE - 1

private val NOT_LOADED_ATLAS_POSITION = MinimapTextureAtlasManager.AtlasPosition(0, 0)

class MinimapTextureAtlasManager {
private val texture = NativeImageBackedTexture(ATLAS_SIZE * 16, ATLAS_SIZE * 16, false)
private val availableAtlasPositions = ArrayList<AtlasPosition>(ATLAS_SIZE * ATLAS_SIZE - 1)
private val availableAtlasPositions: ArrayBlockingQueue<AtlasPosition>
private val dirtyAtlasPositions = hashSetOf<AtlasPosition>()
private val chunkPosAtlasPosMap = hashMapOf<ChunkPos, AtlasPosition>()

Expand All @@ -53,15 +57,17 @@ class MinimapTextureAtlasManager {
private var allocated = false

init {
val atlasPositions = ArrayList<AtlasPosition>(MAX_ATLAS_POSITIONS)
for (x in 0 until ATLAS_SIZE) {
for (y in 0 until ATLAS_SIZE) {
if (x == 0 && y == 0) {
continue
}

availableAtlasPositions.add(AtlasPosition(x, y))
atlasPositions.add(AtlasPosition(x, y))
}
}
availableAtlasPositions = ArrayBlockingQueue(MAX_ATLAS_POSITIONS, false, atlasPositions)

for (x in 0..15) {
for (y in 0..15) {
Expand All @@ -75,7 +81,7 @@ class MinimapTextureAtlasManager {
}

private fun allocate(chunkPos: ChunkPos): AtlasPosition {
val atlasPosition = availableAtlasPositions.removeLastOrNull() ?: error("No more space in the texture atlas!")
val atlasPosition = availableAtlasPositions.take() ?: error("No more space in the texture atlas!")

chunkPosAtlasPosMap[chunkPos] = atlasPosition

Expand All @@ -89,9 +95,11 @@ class MinimapTextureAtlasManager {
}

fun deallocateAll() {
availableAtlasPositions.addAll(chunkPosAtlasPosMap.values)
chunkPosAtlasPosMap.clear()
dirtyAtlasPositions.clear()
lock.write {
availableAtlasPositions.addAll(chunkPosAtlasPosMap.values)
chunkPosAtlasPosMap.clear()
dirtyAtlasPositions.clear()
}
}

fun getOrNotLoadedTexture(chunkPos: ChunkPos): AtlasPosition {
Expand Down

0 comments on commit b10fe95

Please sign in to comment.