Skip to content

Commit 247f108

Browse files
authored
Merge pull request #555 from rfresh2/search-update-2
improve search efficiency
2 parents efbed9e + f157528 commit 247f108

File tree

2 files changed

+75
-12
lines changed

2 files changed

+75
-12
lines changed

src/main/kotlin/com/lambda/client/module/modules/render/Search.kt

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ import com.lambda.client.util.graphics.ESPRenderer
1616
import com.lambda.client.util.graphics.GeometryMasks
1717
import com.lambda.client.util.graphics.LambdaTessellator
1818
import com.lambda.client.util.graphics.ShaderHelper
19-
import com.lambda.client.util.math.VectorUtils.distanceTo
19+
import com.lambda.client.util.items.id
20+
import com.lambda.client.util.math.VectorUtils.manhattanDistanceTo
2021
import com.lambda.client.util.text.MessageSendHelper
2122
import com.lambda.client.util.text.formatValue
2223
import com.lambda.client.util.threads.defaultScope
2324
import com.lambda.client.util.threads.runSafe
2425
import com.lambda.client.util.threads.safeListener
2526
import com.lambda.client.util.world.isWater
27+
import it.unimi.dsi.fastutil.ints.IntOpenHashSet
2628
import kotlinx.coroutines.Job
2729
import kotlinx.coroutines.isActive
2830
import kotlinx.coroutines.launch
@@ -38,11 +40,13 @@ import net.minecraft.init.Blocks
3840
import net.minecraft.network.play.server.SPacketBlockChange
3941
import net.minecraft.network.play.server.SPacketMultiBlockChange
4042
import net.minecraft.tileentity.TileEntitySign
43+
import net.minecraft.util.ResourceLocation
4144
import net.minecraft.util.math.BlockPos
4245
import net.minecraft.util.math.ChunkPos
4346
import net.minecraft.util.text.TextComponentString
4447
import net.minecraft.world.chunk.Chunk
4548
import net.minecraftforge.fml.common.gameevent.TickEvent
49+
import net.minecraftforge.fml.common.registry.ForgeRegistries
4650
import java.util.concurrent.ConcurrentHashMap
4751
import java.util.concurrent.ConcurrentMap
4852
import kotlin.collections.set
@@ -80,6 +84,7 @@ object Search : Module(
8084

8185
var overrideWarning by setting("Override Warning", false, { false })
8286
val blockSearchList = setting(CollectionSetting("Search List", defaultSearchList, { false }))
87+
private var blockSearchIdHashSet: IntOpenHashSet = IntOpenHashSet()
8388
val entitySearchList = setting(CollectionSetting("Entity Search List", linkedSetOf(EntityList.getKey((EntityItemFrame::class.java))!!.path), { false }))
8489
val blockSearchDimensionFilter = setting(CollectionSetting("Block Dimension Filter", linkedSetOf(), entryType = DimensionFilter::class.java, visibility = { false }))
8590
val entitySearchDimensionFilter = setting(CollectionSetting("Entity Dimension Filter", linkedSetOf(), entryType = DimensionFilter::class.java, visibility = { false }))
@@ -139,6 +144,10 @@ object Search : Module(
139144
}
140145

141146
safeListener<TickEvent.ClientTickEvent> {
147+
if (blockSearchIdHashSet.size != blockSearchList.size) {
148+
updateBlockSearchIdSet()
149+
defaultScope.launch { searchAllLoadedChunks() }
150+
}
142151
if (blockRenderUpdateJob == null || blockRenderUpdateJob?.isCompleted == true) {
143152
blockRenderUpdateJob = defaultScope.launch {
144153
blockRenderUpdate()
@@ -175,17 +184,28 @@ object Search : Module(
175184
}
176185

177186
safeListener<ConnectionEvent.Disconnect> {
178-
blockRenderer.clear()
179-
entityRenderer.clear()
187+
blockRenderer.replaceAll(mutableListOf()) // safe replace during possible iterator to prevent crashes
188+
entityRenderer.replaceAll(mutableListOf())
180189
foundBlockMap.clear()
181190
}
182191
}
183192

184193
private fun blockSearchListUpdateListener(newBool: Boolean) {
185194
foundBlockMap.entries
186-
.filterNot { blockSearchList.contains(it.value.block.registryName.toString()) }
195+
.filterNot { blockSearchIdHashSet.contains(it.value.block.id) }
187196
.forEach { foundBlockMap.remove(it.key) }
188-
if (newBool) runSafe { searchAllLoadedChunks() }
197+
if (newBool) runSafe {
198+
updateBlockSearchIdSet()
199+
searchAllLoadedChunks()
200+
}
201+
}
202+
203+
private fun updateBlockSearchIdSet() {
204+
val idSet = IntOpenHashSet()
205+
blockSearchList.forEach {
206+
idSet.add(ForgeRegistries.BLOCKS.getValue(ResourceLocation(it))?.id)
207+
}
208+
blockSearchIdHashSet = idSet
189209
}
190210

191211
private fun SafeClientEvent.searchLoadedEntities() {
@@ -201,9 +221,9 @@ object Search : Module(
201221
entitySearchDimensionFilter.value.find { dimFilter -> dimFilter.searchKey == entityName }?.dim
202222
}?.contains(player.dimension) ?: true
203223
}
204-
.sortedBy { it.distanceTo(player.getPositionEyes(1f)) }
224+
.sortedBy { it.manhattanDistanceTo(player.position) }
205225
.take(maximumEntities)
206-
.filter { it.distanceTo(player.getPositionEyes(1f)) < range }
226+
.filter { it.manhattanDistanceTo(player.position) < range }
207227
.map {
208228
Triple(
209229
it.renderBoundingBox.offset(EntityUtils.getInterpolatedAmount(it, LambdaTessellator.pTicks())),
@@ -252,7 +272,7 @@ object Search : Module(
252272
// unload rendering on block pos > range
253273
foundBlockMap
254274
.filter {
255-
playerPos.distanceTo(it.key) > max(mc.gameSettings.renderDistanceChunks * 16, range)
275+
playerPos.manhattanDistanceTo(it.key) > max(mc.gameSettings.renderDistanceChunks * 16, range)
256276
}
257277
.map { it.key }
258278
.forEach { foundBlockMap.remove(it) }
@@ -261,11 +281,11 @@ object Search : Module(
261281
.filter {
262282
blockSearchDimensionFilter.value
263283
.find {
264-
dimFilter -> dimFilter.searchKey == it.value.block.registryName.toString()
284+
dimFilter -> dimFilter.getId() == it.value.block.id
265285
}?.dim?.contains(player.dimension) ?: true
266286
}
267287
.map {
268-
player.getPositionEyes(1f).distanceTo(it.key) to it.key
288+
player.position.manhattanDistanceTo(it.key) to it.key
269289
}
270290
.filter { it.first < range }
271291
.take(maximumBlocks)
@@ -322,9 +342,9 @@ object Search : Module(
322342
private fun SafeClientEvent.searchQuery(state: IBlockState, pos: BlockPos): Boolean {
323343
val block = state.block
324344
if (block == Blocks.AIR) return false
325-
return (blockSearchList.contains(block.registryName.toString())
345+
return (blockSearchIdHashSet.contains(block.id)
326346
&& blockSearchDimensionFilter.value.find { dimFilter ->
327-
dimFilter.searchKey == block.registryName.toString()
347+
dimFilter.getId() == block.id
328348
}?.dim?.contains(player.dimension) ?: true)
329349
|| isIllegalBedrock(state, pos)
330350
|| isIllegalWater(state)
@@ -395,6 +415,17 @@ object Search : Module(
395415
}
396416

397417
data class DimensionFilter(val searchKey: String, val dim: LinkedHashSet<Int>) {
418+
private var id: Int? = null
419+
private var idInitTried = false // need to do this whole thing because gson doesn't init this field
420+
421+
fun getId(): Int? {
422+
if (id == null && !idInitTried) {
423+
idInitTried = true
424+
id = ForgeRegistries.BLOCKS.getValue(ResourceLocation(searchKey))?.id
425+
}
426+
return id
427+
}
428+
398429
override fun toString(): String {
399430
return "$searchKey -> $dim"
400431
}

src/main/kotlin/com/lambda/client/util/math/VectorUtils.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,31 +112,63 @@ object VectorUtils {
112112
return sqrt((xDiff * xDiff + yDiff * yDiff + zDiff * zDiff).toDouble())
113113
}
114114

115+
fun Vec3i.manhattanDistanceTo(vec3i: Vec3i): Int {
116+
val xDiff = abs(vec3i.x - x)
117+
val yDiff = abs(vec3i.y - y)
118+
val zDiff = abs(vec3i.z - z)
119+
return xDiff + yDiff + zDiff
120+
}
121+
115122
fun Vec3d.distanceTo(vec3i: Vec3i): Double {
116123
val xDiff = vec3i.x + 0.5 - x
117124
val yDiff = vec3i.y + 0.5 - y
118125
val zDiff = vec3i.z + 0.5 - z
119126
return sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff)
120127
}
121128

129+
fun Vec3d.manhattanDistanceTo(vec3i: Vec3i): Double {
130+
val xDiff = abs(vec3i.x + 0.5 - x)
131+
val yDiff = abs(vec3i.y + 0.5 - y)
132+
val zDiff = abs(vec3i.z + 0.5 - z)
133+
return xDiff + yDiff + zDiff
134+
}
135+
122136
fun Entity.distanceTo(vec3i: Vec3i): Double {
123137
val xDiff = vec3i.x + 0.5 - posX
124138
val yDiff = vec3i.y + 0.5 - posY
125139
val zDiff = vec3i.z + 0.5 - posZ
126140
return sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff)
127141
}
128142

143+
fun Entity.manhattanDistanceTo(vec3i: Vec3i): Double {
144+
val xDiff = abs(vec3i.x + 0.5 - posX)
145+
val yDiff = abs(vec3i.y + 0.5 - posY)
146+
val zDiff = abs(vec3i.z + 0.5 - posZ)
147+
return xDiff + yDiff + zDiff
148+
}
149+
129150
fun Entity.distanceTo(vec3d: Vec3d): Double {
130151
val xDiff = vec3d.x - posX
131152
val yDiff = vec3d.y - posY
132153
val zDiff = vec3d.z - posZ
133154
return sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff)
134155
}
135156

157+
fun Entity.manhattanDistanceTo(vec3d: Vec3d): Double {
158+
val xDiff = abs(vec3d.x - posX)
159+
val yDiff = abs(vec3d.y - posY)
160+
val zDiff = abs(vec3d.z - posZ)
161+
return xDiff + yDiff + zDiff
162+
}
163+
136164
fun Entity.distanceTo(chunkPos: ChunkPos): Double {
137165
return hypot(chunkPos.x * 16 + 8 - posX, chunkPos.z * 16 + 8 - posZ)
138166
}
139167

168+
fun Entity.manhattanDistanceTo(chunkPos: ChunkPos): Double {
169+
return abs(chunkPos.x * 16 + 8 - posX) + abs(chunkPos.z * 16 + 8 - posZ)
170+
}
171+
140172
fun Vec3i.multiply(multiplier: Int): Vec3i {
141173
return Vec3i(x * multiplier, y * multiplier, z * multiplier)
142174
}

0 commit comments

Comments
 (0)