Skip to content

Commit 1f7e881

Browse files
committed
Shape compound rotation finder
1 parent 1b75d99 commit 1f7e881

File tree

7 files changed

+122
-93
lines changed

7 files changed

+122
-93
lines changed

common/src/main/kotlin/com/lambda/event/events/RotationEvent.kt

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lambda.event.events
22

33
import com.lambda.config.RotationSettings
4+
import com.lambda.context.SafeContext
45
import com.lambda.event.Event
56
import com.lambda.event.cancellable.Cancellable
67
import com.lambda.event.cancellable.ICancellable
@@ -13,10 +14,13 @@ import com.lambda.manager.rotation.Rotation.Companion.rotationTo
1314
import com.lambda.manager.interaction.VisibilityChecker.scanVisibleSurfaces
1415
import com.lambda.threading.runSafe
1516
import com.lambda.util.math.VecUtils.distSq
17+
import com.lambda.util.world.raycast.RayCastUtils.blockResult
1618
import com.lambda.util.world.raycast.RayCastUtils.entityResult
1719
import net.minecraft.entity.LivingEntity
1820
import net.minecraft.util.hit.HitResult
21+
import net.minecraft.util.math.BlockPos
1922
import net.minecraft.util.math.Box
23+
import net.minecraft.util.math.Direction
2024
import java.util.*
2125

2226
abstract class RotationEvent : Event {
@@ -31,46 +35,38 @@ abstract class RotationEvent : Event {
3135
}
3236
}
3337

34-
fun rotateTo(
35-
config: IRotationConfig,
36-
rotation: Rotation,
37-
priority: Int = 0,
38-
) {
39-
requests.add(RotationRequest(priority, config, rotation))
40-
}
41-
42-
fun lookAt(
38+
fun SafeContext.lookAt(
4339
rotationConfig: IRotationConfig,
4440
interact: InteractionConfig,
45-
box: Box,
41+
boxes: List<Box>,
4642
priority: Int = 0,
4743
hitCheck: HitResult.() -> Boolean,
4844
) {
49-
runSafe {
50-
if (box.contains(player.eyePos)) {
51-
stay(priority, rotationConfig)
52-
return@runSafe
53-
}
45+
if (boxes.any { it.contains(player.eyePos) }) {
46+
stay(priority, rotationConfig)
47+
return
48+
}
5449

55-
val currentRotation = RotationManager.currentRotation
56-
val currentCast = currentRotation.rayCast(
57-
interact.reach,
58-
interact.rayCastMask,
59-
player.eyePos
60-
)
61-
val check = currentCast?.let { it.hitCheck() } ?: false
62-
63-
// Slowdown or freeze if looking correct
64-
(rotationConfig as? RotationSettings)?.slowdownIf(check) ?: run {
65-
if (check) stay(priority, rotationConfig)
66-
return@runSafe
67-
}
50+
val currentRotation = RotationManager.currentRotation
51+
val currentCast = currentRotation.rayCast(
52+
interact.reach,
53+
interact.rayCastMask,
54+
player.eyePos
55+
)
56+
val check = currentCast?.let { it.hitCheck() } ?: false
57+
58+
// Slowdown or freeze if looking correct
59+
(rotationConfig as? RotationSettings)?.slowdownIf(check) ?: run {
60+
if (check) stay(priority, rotationConfig)
61+
return
62+
}
6863

69-
val reachSq = interact.reach * interact.reach
64+
val reachSq = interact.reach * interact.reach
7065

71-
var closestRotation: Rotation? = null
72-
var rotationDist = 0.0
66+
var closestRotation: Rotation? = null
67+
var rotationDist = 0.0
7368

69+
boxes.forEach { box ->
7470
scanVisibleSurfaces(box, interact.resolution) { vec ->
7571
if (player.eyePos distSq vec > reachSq) return@scanVisibleSurfaces
7672

@@ -89,13 +85,12 @@ abstract class RotationEvent : Event {
8985
rotationDist = dist
9086
closestRotation = newRotation
9187
}
92-
93-
// Rotate to selected point
94-
closestRotation?.let { rotation ->
95-
requests.add(RotationRequest(priority, rotationConfig, rotation))
96-
}
9788
}
9889

90+
// Rotate to selected point
91+
closestRotation?.let { rotation ->
92+
requests.add(RotationRequest(priority, rotationConfig, rotation))
93+
}
9994
}
10095

10196
fun lookAt(
@@ -104,8 +99,27 @@ abstract class RotationEvent : Event {
10499
entity: LivingEntity,
105100
priority: Int = 0,
106101
) {
107-
lookAt(rotationConfig, interactionConfig, entity.boundingBox, priority) {
108-
entityResult?.entity == entity
102+
runSafe {
103+
lookAt(rotationConfig, interactionConfig, listOf(entity.boundingBox), priority) {
104+
entityResult?.entity == entity
105+
}
106+
}
107+
}
108+
109+
fun lookAt(
110+
rotationConfig: IRotationConfig,
111+
interactionConfig: InteractionConfig,
112+
blockPos: BlockPos,
113+
side: Direction,
114+
priority: Int = 0,
115+
) {
116+
runSafe {
117+
val state = world.getBlockState(blockPos)
118+
val voxelShape = state.getOutlineShape(world, blockPos)
119+
val boundingBoxes = voxelShape.boundingBoxes.map { it.offset(blockPos) }
120+
lookAt(rotationConfig, interactionConfig, boundingBoxes, priority) {
121+
blockResult?.blockPos == blockPos && blockResult?.side == side
122+
}
109123
}
110124
}
111125

common/src/main/kotlin/com/lambda/manager/RotationManager.kt

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ object RotationManager : Loadable {
3333
var currentRotation = Rotation.ZERO
3434
private var prevRotation = Rotation.ZERO
3535

36+
var currentContext: RotationContext? = null
37+
38+
private var keepTicks = 0
39+
private var pauseTicks = 0
40+
3641
@JvmStatic
3742
fun update() =
3843
runSafe {
@@ -42,49 +47,6 @@ object RotationManager : Loadable {
4247
EventFlow.post(RotationEvent.Post())
4348
}
4449

45-
var currentContext: RotationContext? = null
46-
47-
private var keepTicks = 0
48-
private var pauseTicks = 0
49-
50-
private val smoothRotation get() =
51-
lerp(prevRotation, currentRotation, mc.partialTicks)
52-
53-
@JvmStatic val lockRotation get() =
54-
if (currentContext?.config?.rotationMode == RotationMode.LOCK) smoothRotation else null
55-
56-
@JvmStatic val renderYaw get() =
57-
if (currentContext?.config == null) null else smoothRotation.yaw.toFloat()
58-
59-
@JvmStatic val renderPitch get() =
60-
if (currentContext?.config == null) null else smoothRotation.pitch.toFloat()
61-
62-
@JvmStatic val handYaw get() =
63-
if (currentContext?.config?.rotationMode == RotationMode.LOCK) currentRotation.yaw.toFloat() else null
64-
65-
@JvmStatic val handPitch get() =
66-
if (currentContext?.config?.rotationMode == RotationMode.LOCK) currentRotation.pitch.toFloat() else null
67-
68-
@JvmStatic val movementYaw: Float? get() {
69-
val config = currentContext?.config ?: return null
70-
if (config.rotationMode == RotationMode.SILENT) return null
71-
return currentRotation.yaw.toFloat()
72-
}
73-
74-
@JvmStatic val movementPitch: Float? get() {
75-
val config = currentContext?.config ?: return null
76-
if (config.rotationMode == RotationMode.SILENT) return null
77-
return currentRotation.pitch.toFloat()
78-
}
79-
80-
@JvmStatic fun getRotationForVector(deltaTime: Double): Vec2d? {
81-
val config = currentContext?.config ?: return null
82-
if (config.rotationMode == RotationMode.SILENT) return null
83-
84-
val rot = lerp(prevRotation, currentRotation, deltaTime)
85-
return Vec2d(rot.yaw, rot.pitch)
86-
}
87-
8850
init {
8951
listener<PacketEvent.Send.Post> { event ->
9052
val packet = event.packet
@@ -145,6 +107,44 @@ object RotationManager : Loadable {
145107
pauseTicks = 3
146108
}
147109

110+
private val smoothRotation get() =
111+
lerp(prevRotation, currentRotation, mc.partialTicks)
112+
113+
@JvmStatic val lockRotation get() =
114+
if (currentContext?.config?.rotationMode == RotationMode.LOCK) smoothRotation else null
115+
116+
@JvmStatic val renderYaw get() =
117+
if (currentContext?.config == null) null else smoothRotation.yaw.toFloat()
118+
119+
@JvmStatic val renderPitch get() =
120+
if (currentContext?.config == null) null else smoothRotation.pitch.toFloat()
121+
122+
@JvmStatic val handYaw get() =
123+
if (currentContext?.config?.rotationMode == RotationMode.LOCK) currentRotation.yaw.toFloat() else null
124+
125+
@JvmStatic val handPitch get() =
126+
if (currentContext?.config?.rotationMode == RotationMode.LOCK) currentRotation.pitch.toFloat() else null
127+
128+
@JvmStatic val movementYaw: Float? get() {
129+
val config = currentContext?.config ?: return null
130+
if (config.rotationMode == RotationMode.SILENT) return null
131+
return currentRotation.yaw.toFloat()
132+
}
133+
134+
@JvmStatic val movementPitch: Float? get() {
135+
val config = currentContext?.config ?: return null
136+
if (config.rotationMode == RotationMode.SILENT) return null
137+
return currentRotation.pitch.toFloat()
138+
}
139+
140+
@JvmStatic fun getRotationForVector(deltaTime: Double): Vec2d? {
141+
val config = currentContext?.config ?: return null
142+
if (config.rotationMode == RotationMode.SILENT) return null
143+
144+
val rot = lerp(prevRotation, currentRotation, deltaTime)
145+
return Vec2d(rot.yaw, rot.pitch)
146+
}
147+
148148
object BaritoneProcessor {
149149
var baritoneContext: RotationContext? = null; private set
150150

common/src/main/kotlin/com/lambda/manager/interaction/VisibilityChecker.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.lambda.manager.interaction
22

33
import com.lambda.context.SafeContext
4+
import com.lambda.util.primitives.extension.component6
45
import net.minecraft.util.math.*
56
import java.util.*
67

78
object VisibilityChecker {
8-
operator fun DoubleArray.component6() = this[5]
9-
109
inline fun SafeContext.scanVisibleSurfaces(box: Box, resolution: Int, check: (Vec3d) -> Unit) {
11-
val shrunk = box.expand(-0.025)
10+
val shrunk = box.expand(-0.05)
1211
getVisibleSides(box).forEach { side ->
1312
val (minX, minY, minZ, maxX, maxY, maxZ) = shrunk.bounds(side)
1413
val stepX = (maxX - minX) / resolution

common/src/main/kotlin/com/lambda/module/Module.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.lambda.config.Configuration
66
import com.lambda.config.configurations.ModuleConfig
77
import com.lambda.config.settings.comparable.BooleanSetting
88
import com.lambda.config.settings.numeric.DoubleSetting
9+
import com.lambda.context.SafeContext
910
import com.lambda.event.Muteable
1011
import com.lambda.event.events.KeyPressEvent
1112
import com.lambda.event.listener.Listener
@@ -123,19 +124,19 @@ abstract class Module(
123124
isEnabled = !isEnabled
124125
}
125126

126-
protected fun onEnable(block: () -> Unit) {
127+
protected fun onEnable(block: SafeContext.() -> Unit) {
127128
isEnabledSetting.listener { from, to ->
128129
if (!from && to) block()
129130
}
130131
}
131132

132-
protected fun onDisable(block: () -> Unit) {
133+
protected fun onDisable(block: SafeContext.() -> Unit) {
133134
isEnabledSetting.listener { from, to ->
134135
if (from && !to) block()
135136
}
136137
}
137138

138-
protected fun onToggle(block: (to: Boolean) -> Unit) {
139+
protected fun onToggle(block: SafeContext.(to: Boolean) -> Unit) {
139140
isEnabledSetting.listener { from, to ->
140141
if (from != to) block(to)
141142
}

common/src/main/kotlin/com/lambda/module/modules/RotationTest.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import com.lambda.event.listener.SafeListener.Companion.listener
77
import com.lambda.module.Module
88
import com.lambda.util.world.EntityUtils.getClosestEntity
99
import net.minecraft.entity.passive.VillagerEntity
10+
import net.minecraft.util.hit.BlockHitResult
11+
import net.minecraft.util.math.BlockPos
12+
import net.minecraft.util.math.Direction
1013

1114
object RotationTest : Module(
1215
name = "RotationTest",
@@ -16,13 +19,22 @@ object RotationTest : Module(
1619
private val rotationConfig = RotationSettings(this)
1720
private val interactionConfig = InteractionSettings(this)
1821

22+
private var pos: BlockPos = BlockPos.ORIGIN
23+
private var side = Direction.UP
24+
1925
init {
26+
onEnable {
27+
val hit = mc.crosshairTarget as? BlockHitResult ?: return@onEnable
28+
pos = hit.blockPos
29+
side = hit.side
30+
}
31+
2032
listener<RotationEvent.Pre> {
21-
val target = getClosestEntity<VillagerEntity>(
22-
player.eyePos, interaction.reachDistance.toDouble()
23-
) ?: return@listener
33+
// val target = getClosestEntity<VillagerEntity>(
34+
// player.eyePos, interaction.reachDistance.toDouble()
35+
// ) ?: return@listener
2436

25-
it.lookAt(rotationConfig, interactionConfig, target)
37+
it.lookAt(rotationConfig, interactionConfig, pos, side)
2638
}
2739
}
2840
}

common/src/main/kotlin/com/lambda/module/modules/movement/RocketExtend.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ object RocketExtend : Module(
3939
event.cancel()
4040
}
4141

42-
onDisable(::reset)
42+
onDisable {
43+
reset()
44+
}
4345
}
4446

4547
private fun reset() = runSafe {

common/src/main/kotlin/com/lambda/util/primitives/extension/Components.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ operator fun Vec2f.component1() = this.x
77
operator fun Vec2f.component2() = this.y
88
operator fun Vec3d.component1() = this.x
99
operator fun Vec3d.component2() = this.y
10-
operator fun Vec3d.component3() = this.z
10+
operator fun Vec3d.component3() = this.z
11+
operator fun DoubleArray.component6() = this[5]

0 commit comments

Comments
 (0)