Skip to content

Commit a2c7d40

Browse files
committed
ref: huge blockpos memory allocation
1 parent 9077032 commit a2c7d40

File tree

5 files changed

+30
-25
lines changed

5 files changed

+30
-25
lines changed

common/src/main/kotlin/com/lambda/graphics/renderer/esp/ChunkedESP.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,19 @@ import com.lambda.event.events.TickEvent
2222
import com.lambda.event.events.WorldEvent
2323
import com.lambda.event.listener.SafeListener.Companion.concurrentListener
2424
import com.lambda.event.listener.SafeListener.Companion.listener
25-
import com.lambda.graphics.buffer.BufferUsage
2625
import com.lambda.graphics.renderer.esp.impl.ESPRenderer
2726
import com.lambda.graphics.renderer.esp.impl.StaticESPRenderer
2827
import com.lambda.module.modules.client.RenderSettings
2928
import com.lambda.threading.awaitMainThread
3029
import net.minecraft.util.math.ChunkPos
31-
import net.minecraft.world.WorldView
30+
import net.minecraft.world.World
3231
import net.minecraft.world.chunk.WorldChunk
3332
import java.util.concurrent.ConcurrentHashMap
3433
import java.util.concurrent.ConcurrentLinkedDeque
3534

3635
class ChunkedESP private constructor(
3736
owner: Any,
38-
private val update: StaticESPRenderer.(WorldView, Int, Int, Int) -> Unit
37+
private val update: StaticESPRenderer.(World, Int, Int, Int) -> Unit
3938
) {
4039
private val rendererMap = ConcurrentHashMap<Long, EspChunk>()
4140
private val WorldChunk.renderer
@@ -96,7 +95,7 @@ class ChunkedESP private constructor(
9695

9796
companion object {
9897
fun Any.newChunkedESP(
99-
update: StaticESPRenderer.(WorldView, Int, Int, Int) -> Unit
98+
update: StaticESPRenderer.(World, Int, Int, Int) -> Unit
10099
) = ChunkedESP(this, update)
101100
}
102101

@@ -120,9 +119,7 @@ class ChunkedESP private constructor(
120119
}
121120

122121
suspend fun rebuild() {
123-
val newRenderer = awaitMainThread {
124-
StaticESPRenderer(BufferUsage.STATIC)
125-
}
122+
val newRenderer = awaitMainThread { StaticESPRenderer() }
126123

127124
iterateChunk { x, y, z ->
128125
owner.update(newRenderer, chunk.world, x, y, z)

common/src/main/kotlin/com/lambda/graphics/renderer/esp/DirectionMask.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package com.lambda.graphics.renderer.esp
1919

20+
import com.lambda.util.world.FastVector
21+
import com.lambda.util.world.offset
2022
import net.minecraft.util.math.BlockPos
2123
import net.minecraft.util.math.Direction
2224

@@ -39,11 +41,11 @@ object DirectionMask {
3941
fun Int.exclude(direction: Direction) = exclude(direction.mask)
4042
fun Int.hasDirection(dir: Int) = (this and dir) != 0
4143

42-
fun buildSideMesh(blockPos: BlockPos, filter: (BlockPos) -> Boolean): Int {
44+
fun buildSideMesh(position: FastVector, filter: (FastVector) -> Boolean): Int {
4345
var sides = ALL
4446

4547
Direction.entries
46-
.filter { filter(blockPos.offset(it)) }
48+
.filter { filter(position.offset(it)) }
4749
.forEach { sides = sides.exclude(it.mask) }
4850

4951
return sides

common/src/main/kotlin/com/lambda/module/modules/player/Scaffold.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import com.lambda.util.player.MovementUtils.newMovementInput
5252
import com.lambda.util.player.MovementUtils.roundedForward
5353
import com.lambda.util.player.MovementUtils.roundedStrafing
5454
import com.lambda.util.world.raycast.RayCastUtils.blockResult
55+
import com.lambda.util.world.toFastVec
5556
import net.minecraft.util.Hand
5657
import net.minecraft.util.hit.BlockHitResult
5758
import net.minecraft.util.math.BlockPos
@@ -313,11 +314,11 @@ object Scaffold : Module(
313314
renderInfo.removeIf {
314315
val (info, time) = it
315316

316-
val pos = info.placedPos
317+
val pos = info.placedPos.toFastVec()
317318
val seconds = (currentTime - time) / 1000.0
318319

319320
val sides = buildSideMesh(pos) { meshPos ->
320-
renderInfo.any { it.first.placedPos == meshPos }
321+
renderInfo.any { it.first.placedPos.toFastVec() == meshPos }
321322
}
322323

323324
val box = Box(info.placedPos)

common/src/main/kotlin/com/lambda/module/modules/render/BlockESP.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ import com.lambda.graphics.renderer.esp.builders.buildOutline
2626
import com.lambda.graphics.renderer.esp.impl.StaticESPRenderer
2727
import com.lambda.module.Module
2828
import com.lambda.module.tag.ModuleTag
29-
import com.lambda.util.BlockUtils.blockState
29+
import com.lambda.util.extension.getBlockState
30+
import com.lambda.util.world.fastVectorOf
3031
import net.minecraft.block.Block
3132
import net.minecraft.block.Blocks
3233
import net.minecraft.client.render.model.BakedModel
33-
import net.minecraft.util.math.BlockPos
3434
import net.minecraft.util.math.Box
3535
import java.awt.Color
3636

@@ -93,18 +93,22 @@ object BlockESP : Module(
9393
}
9494
}
9595

96-
private val esp = newChunkedESP { view, x, y, z ->
97-
val blockPos = BlockPos(x, y, z)
98-
val state = view.getBlockState(blockPos)
96+
private val esp = newChunkedESP { world, x, y, z ->
97+
val position = fastVectorOf(x, y, z)
98+
val state = world.getBlockState(position)
9999
if (state.block !in blocks) return@newChunkedESP
100100

101101
val sides = if (mesh) {
102-
buildSideMesh(blockPos) {
103-
it.blockState(view).block in blocks
102+
buildSideMesh(position) {
103+
world.getBlockState(it).block in blocks
104104
}
105105
} else DirectionMask.ALL
106106

107-
build(Box(blockPos), sides)
107+
build(
108+
// big hack
109+
Box(x.toDouble(), y.toDouble(), z.toDouble(), x.toDouble()+1, y.toDouble()+1, z.toDouble()+1),
110+
sides
111+
)
108112
}
109113

110114
private fun StaticESPRenderer.build(

common/src/main/kotlin/com/lambda/util/world/Position.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.util.world
1919

2020
import net.minecraft.util.math.BlockPos
21+
import net.minecraft.util.math.Direction
2122
import net.minecraft.util.math.Vec3d
2223
import net.minecraft.util.math.Vec3i
2324

@@ -131,13 +132,11 @@ infix fun FastVector.plus(vec: FastVector): FastVector = fastVectorOf(x + vec.x,
131132

132133
/**
133134
* Adds the given vector to the position.
134-
* @return The new position.
135135
*/
136136
infix fun FastVector.plus(vec: Vec3i): FastVector = fastVectorOf(x + vec.x, y + vec.y, z + vec.z)
137137

138138
/**
139139
* Adds the given vector to the position.
140-
* @return The new position.
141140
*/
142141
infix fun FastVector.plus(vec: Vec3d): FastVector =
143142
fastVectorOf(x + vec.x.toLong(), y + vec.y.toLong(), z + vec.z.toLong())
@@ -149,13 +148,11 @@ infix fun FastVector.minus(vec: FastVector): FastVector = fastVectorOf(x - vec.x
149148

150149
/**
151150
* Subtracts the given vector from the position.
152-
* @return The new position.
153151
*/
154152
infix fun FastVector.minus(vec: Vec3i): FastVector = fastVectorOf(x - vec.x, y - vec.y, z - vec.z)
155153

156154
/**
157155
* Subtracts the given vector from the position.
158-
* @return The new position.
159156
*/
160157
infix fun FastVector.minus(vec: Vec3d): FastVector =
161158
fastVectorOf(x - vec.x.toLong(), y - vec.y.toLong(), z - vec.z.toLong())
@@ -223,15 +220,19 @@ infix fun FastVector.distSq(other: Vec3d): Double {
223220
return (dx * dx + dy * dy + dz * dz).toDouble()
224221
}
225222

223+
/**
224+
* Adds a [net.minecraft.util.math.Direction] offset to the fast vector
225+
*/
226+
fun FastVector.offset(dir: Direction) =
227+
fastVectorOf(x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ)
228+
226229
/**
227230
* Converts a [Vec3i] to a [FastVector].
228-
* @return The encoded position.
229231
*/
230232
fun Vec3i.toFastVec(): FastVector = fastVectorOf(x.toLong(), y.toLong(), z.toLong())
231233

232234
/**
233235
* Converts a [Vec3d] to a [FastVector].
234-
* @return The encoded position.
235236
*/
236237
fun Vec3d.toFastVec(): FastVector = fastVectorOf(x.toLong(), y.toLong(), z.toLong())
237238

0 commit comments

Comments
 (0)