Skip to content

Commit 3c1ca3d

Browse files
committed
Kill aura
1 parent 26b9e7d commit 3c1ca3d

File tree

14 files changed

+405
-75
lines changed

14 files changed

+405
-75
lines changed

common/src/main/java/com/lambda/mixin/MinecraftClientMixin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import com.lambda.event.events.ScreenEvent;
77
import com.lambda.event.events.ScreenHandlerEvent;
88
import com.lambda.event.events.TickEvent;
9+
import com.lambda.interaction.RotationManager;
910
import com.lambda.module.modules.player.Interact;
1011
import net.minecraft.client.MinecraftClient;
1112
import net.minecraft.client.gui.screen.Screen;
12-
import net.minecraft.client.gui.screen.ingame.HandledScreen;
1313
import net.minecraft.client.gui.screen.ingame.ScreenHandlerProvider;
1414
import net.minecraft.client.network.ClientPlayerInteractionManager;
1515
import org.jetbrains.annotations.Nullable;
@@ -27,6 +27,7 @@ public class MinecraftClientMixin {
2727
@Inject(method = "tick", at = @At("HEAD"))
2828
void onTickPre(CallbackInfo ci) {
2929
EventFlow.post(new TickEvent.Pre());
30+
RotationManager.update();
3031
}
3132

3233
@Inject(method = "tick", at = @At("RETURN"))

common/src/main/java/com/lambda/mixin/entity/PlayerEntityMixin.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,14 @@ private float injectHeadYaw(PlayerEntity instance) {
2828
Float yaw = RotationManager.getRenderYaw();
2929
return (yaw != null) ? yaw : instance.getYaw();
3030
}
31+
32+
@Redirect(method = "attack", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;getYaw()F"))
33+
private float injectAttackFix(PlayerEntity instance) {
34+
if ((Object) this != MinecraftClient.getInstance().player) {
35+
return instance.getYaw();
36+
}
37+
38+
Float yaw = RotationManager.getMovementYaw();
39+
return (yaw != null) ? yaw : instance.getYaw();
40+
}
3141
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.lambda.config.groups
22

33
import com.lambda.config.Configurable
4-
import com.lambda.util.world.raycast.RayCastMask
54

65
class InteractionSettings(
76
c: Configurable,
7+
defaultReach: Double = 4.9,
88
vis: () -> Boolean = { true },
99
) : InteractionConfig {
10-
override val reach by c.setting("Reach", 4.9, 0.1..10.0, 0.1, "Players reach / range", " blocks", vis)
10+
override val reach by c.setting("Reach", defaultReach, 0.1..10.0, 0.1, "Players reach / range", " blocks", vis)
1111
override val useRayCast by c.setting("Raycast", false, "Verify hit vector with ray casting (for very strict ACs)", vis)
12-
override val resolution by c.setting("Resolution", 5, 1..20, 1, "How many raycast checks per surface (will be squared)") { vis() && useRayCast }
12+
override val resolution by c.setting("Resolution", 10, 1..30, 1, "How many raycast checks per surface (will be squared)") { vis() && useRayCast }
1313
override val swingHand by c.setting("Swing Hand", true, "Swing hand on interactions", vis)
1414
}

common/src/main/kotlin/com/lambda/config/groups/RotationSettings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class RotationSettings(
2222
/**
2323
* If true, rotation will be instant without any transition. If false, rotation will transition over time.
2424
*/
25-
private var instant by c.setting("Instant Rotation", true, "Instantly rotate", vis)
25+
var instant by c.setting("Instant Rotation", true, "Instantly rotate", vis)
2626

2727
/**
2828
* The mean (average/base) value used to calculate rotation speed.

common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ object PlayerPacketManager : Loadable {
5050
val (yaw, pitch) = rotation.float
5151
val onGround = new.onGround
5252

53-
// Fix sensitivity for absolutely any outgoing angle
54-
RotationManager.currentRotation = rotation.fixSensitivity(RotationManager.prevRotation)
55-
5653
if (player.hasVehicle()) {
5754
connection.sendPacket(
5855
Full(

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

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package com.lambda.interaction
22

33
import com.lambda.Lambda.mc
44
import com.lambda.config.groups.RotationSettings
5+
import com.lambda.context.SafeContext
56
import com.lambda.core.Loadable
67
import com.lambda.event.EventFlow.post
78
import com.lambda.event.events.*
89
import com.lambda.event.listener.SafeListener.Companion.listener
910
import com.lambda.event.listener.UnsafeListener.Companion.unsafeListener
1011
import com.lambda.interaction.rotation.Rotation
1112
import com.lambda.interaction.rotation.Rotation.Companion.angleDifference
13+
import com.lambda.interaction.rotation.Rotation.Companion.fixSensitivity
1214
import com.lambda.interaction.rotation.Rotation.Companion.slerp
1315
import com.lambda.interaction.rotation.RotationContext
1416
import com.lambda.interaction.rotation.RotationMode
@@ -33,17 +35,43 @@ object RotationManager : Loadable {
3335
private var keepTicks = 0
3436
private var pauseTicks = 0
3537

36-
init {
37-
listener<TickEvent.Pre> {
38-
RotationEvent.Update(BaritoneProcessor.poolContext()).post {
39-
rotate(context)
38+
fun Any.requestRotation(
39+
priority: Int = 0,
40+
alwaysListen: Boolean = false,
41+
onUpdate: SafeContext.() -> RotationContext?,
42+
onReceive: SafeContext.() -> Unit
43+
) {
44+
var lastCtx: RotationContext? = null
4045

41-
currentContext?.let {
42-
RotationEvent.Post(it).post()
43-
}
46+
this.listener<RotationEvent.Update>(priority, alwaysListen) { event ->
47+
val rotationContext = onUpdate()
48+
49+
rotationContext?.let {
50+
event.context = it
51+
}
52+
53+
lastCtx = rotationContext
54+
}
55+
56+
this.listener<RotationEvent.Post> { event ->
57+
if (event.context == lastCtx && event.context.isValid) {
58+
onReceive()
4459
}
4560
}
61+
}
4662

63+
@JvmStatic
64+
fun update() = runSafe {
65+
RotationEvent.Update(BaritoneProcessor.poolContext()).post {
66+
rotate(context)
67+
68+
currentContext?.let {
69+
RotationEvent.Post(it).post()
70+
}
71+
}
72+
}
73+
74+
init {
4775
listener<PacketEvent.Send.Post> { event ->
4876
val packet = event.packet
4977
if (packet !is PlayerPositionLookS2CPacket) return@listener
@@ -85,6 +113,7 @@ object RotationManager : Loadable {
85113

86114
currentRotation
87115
.slerp(rotationTo, turnSpeed)
116+
.fixSensitivity(prevRotation)
88117
.apply {
89118
if (context.config.rotationMode != RotationMode.LOCK) return@apply
90119
player.yaw = this.yawF

common/src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import com.lambda.interaction.construction.verify.TargetState
1212
import com.lambda.interaction.material.ContainerManager.findBestAvailableTool
1313
import com.lambda.interaction.rotation.Rotation.Companion.rotationTo
1414
import com.lambda.interaction.rotation.RotationContext
15-
import com.lambda.interaction.visibilty.VisibilityChecker.mostCenter
15+
import com.lambda.interaction.visibilty.VisibilityChecker.optimum
1616
import com.lambda.interaction.visibilty.VisibilityChecker.scanVisibleSurfaces
1717
import com.lambda.module.modules.client.TaskFlow
1818
import com.lambda.threading.runSafe
@@ -160,7 +160,7 @@ object BuildSimulator {
160160
return@forEach
161161
}
162162

163-
validHits.keys.mostCenter?.let { optimum ->
163+
validHits.keys.optimum?.let { optimum ->
164164
validHits.minByOrNull { optimum distSq it.key }?.let { closest ->
165165
val optimumRotation = eye.rotationTo(closest.key)
166166
RotationContext(optimumRotation, rotation, closest.value, verify)
@@ -368,7 +368,7 @@ object BuildSimulator {
368368
}
369369
}
370370

371-
validHits.keys.mostCenter?.let { optimum ->
371+
validHits.keys.optimum?.let { optimum ->
372372
validHits.minByOrNull { optimum distSq it.key }?.let { closest ->
373373
val optimumRotation = eye.rotationTo(closest.key)
374374
RotationContext(optimumRotation, rotation, closest.value, verify)

common/src/main/kotlin/com/lambda/interaction/rotation/Rotation.kt

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import com.lambda.Lambda.mc
44
import com.lambda.threading.runSafe
55
import com.lambda.util.math.MathUtils.toDegree
66
import com.lambda.util.math.MathUtils.toRadian
7+
import com.lambda.util.math.Vec2d
8+
import com.lambda.util.math.VecUtils.plus
9+
import com.lambda.util.math.VecUtils.times
710
import com.lambda.util.world.raycast.RayCastMask
811
import com.lambda.util.world.raycast.RayCastUtils.rayCast
912
import net.minecraft.entity.Entity
13+
import net.minecraft.util.math.Box
1014
import net.minecraft.util.math.Direction
1115
import net.minecraft.util.math.MathHelper
1216
import net.minecraft.util.math.Vec3d
@@ -42,6 +46,15 @@ data class Rotation(val yaw: Double, val pitch: Double) {
4246
rayCast(eye ?: player.eyePos, vector, reach, mask, fluids)
4347
}
4448

49+
fun castBox(
50+
box: Box,
51+
reach: Double,
52+
eye: Vec3d? = null
53+
) = runSafe {
54+
val eyeVec = eye ?: player.eyePos
55+
box.raycast(eyeVec, eyeVec + vector * reach).orElse(null)
56+
}
57+
4558
val Direction.yaw: Float
4659
get() = when (this) {
4760
Direction.NORTH -> -180.0f
@@ -79,20 +92,16 @@ data class Rotation(val yaw: Double, val pitch: Double) {
7992
return Rotation(yaw, pitch)
8093
}
8194

82-
fun Rotation.fixSensitivity(last: Rotation): Rotation {
95+
fun Rotation.fixSensitivity(prev: Rotation): Rotation {
8396
val f = mc.options.mouseSensitivity.value * 0.6 + 0.2
84-
val step = f * f * f * 8.0 * 0.15F
85-
86-
val deltaYaw = yaw - last.yaw
87-
var fixedYaw = (deltaYaw / step).roundToInt() * step
88-
fixedYaw += last.yaw
97+
val gcd = f * f * f * 8.0 * 0.15F
8998

90-
val deltaPitch = pitch - last.pitch
91-
var fixedPitch = (deltaPitch / step).roundToInt() * step
92-
fixedPitch += last.pitch
93-
fixedPitch = fixedPitch.coerceIn(-90.0, 90.0)
99+
val r1 = Vec2d(prev.yaw, prev.pitch)
100+
val r2 = Vec2d(this.yaw, this.pitch)
101+
val delta = ((r2 - r1) / gcd).roundToInt() * gcd
102+
val fixed = r1 + delta
94103

95-
return Rotation(fixedYaw, fixedPitch)
104+
return Rotation(fixed.x, fixed.y.coerceIn(-90.0, 90.0))
96105
}
97106

98107
fun Vec3d.rotationTo(vec: Vec3d): Rotation {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lambda.interaction.rotation
22

33
import com.lambda.config.groups.IRotationConfig
4+
import com.lambda.util.world.raycast.RayCastUtils.orMiss
45
import net.minecraft.util.hit.HitResult
56

67
data class RotationContext(
@@ -9,5 +10,5 @@ data class RotationContext(
910
val hitResult: HitResult? = null,
1011
val verify: HitResult.() -> Boolean = { true },
1112
) {
12-
val isValid: Boolean get() = hitResult?.verify() == true
13+
val isValid: Boolean get() = verify(hitResult.orMiss)
1314
}

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.lambda.config.groups.IRotationConfig
44
import com.lambda.config.groups.InteractionConfig
55
import com.lambda.context.SafeContext
66
import com.lambda.interaction.RotationManager
7+
import com.lambda.interaction.rotation.Rotation.Companion.dist
78
import com.lambda.interaction.rotation.Rotation.Companion.rotationTo
89
import com.lambda.interaction.rotation.RotationContext
910
import com.lambda.module.modules.client.TaskFlow
@@ -46,37 +47,42 @@ object VisibilityChecker {
4647

4748
fun SafeContext.findRotation(
4849
boxes: List<Box>,
49-
rotationConfig: IRotationConfig = TaskFlow.rotation,
50-
interact: InteractionConfig = TaskFlow.interact,
50+
rotationConfig: IRotationConfig,
51+
interact: InteractionConfig,
5152
sides: Set<Direction> = emptySet(),
53+
reach: Double = interact.reach,
54+
eye: Vec3d = player.getCameraPosVec(1f),
5255
verify: HitResult.() -> Boolean,
5356
): RotationContext? {
54-
val eye = player.getCameraPosVec(mc.tickDelta)
55-
5657
val currentRotation = RotationManager.currentRotation
57-
val currentCast = currentRotation.rayCast(interact.reach, eye)
58+
val currentCast = currentRotation.rayCast(reach, eye)
5859

5960
if (boxes.any { it.contains(eye) }) {
6061
return RotationContext(currentRotation, rotationConfig, currentCast, verify)
6162
}
6263

6364
val validHits = mutableMapOf<Vec3d, HitResult>()
64-
val reachSq = interact.reach.pow(2)
65+
val reachSq = reach.pow(2)
6566

6667
boxes.forEach { box ->
67-
scanVisibleSurfaces(player.eyePos, box, sides, interact.resolution) { _, vec ->
68+
scanVisibleSurfaces(eye, box, sides, interact.resolution) { _, vec ->
6869
if (eye distSq vec > reachSq) return@scanVisibleSurfaces
6970

7071
val newRotation = eye.rotationTo(vec)
7172

72-
val cast = newRotation.rayCast(interact.reach, eye) ?: return@scanVisibleSurfaces
73+
val cast = newRotation.rayCast(reach, eye) ?: return@scanVisibleSurfaces
7374
if (!cast.verify()) return@scanVisibleSurfaces
7475

7576
validHits[vec] = cast
7677
}
7778
}
7879

79-
validHits.keys.mostCenter?.let { optimum ->
80+
// Way stable
81+
/*validHits.minByOrNull { eye.rotationTo(it.key) dist currentRotation }?.let { closest ->
82+
return RotationContext(eye.rotationTo(closest.key), rotationConfig, closest.value, verify)
83+
}*/
84+
85+
validHits.keys.optimum?.let { optimum ->
8086
validHits.minByOrNull { optimum distSq it.key }?.let { closest ->
8187
val optimumRotation = eye.rotationTo(closest.key)
8288
return RotationContext(optimumRotation, rotationConfig, closest.value, verify)
@@ -93,11 +99,10 @@ object VisibilityChecker {
9399
resolution: Int,
94100
check: (Direction, Vec3d) -> Unit,
95101
) {
96-
val shrunk = box.expand(-0.005)
97102
box.getVisibleSurfaces(eyes)
98103
.forEach { side ->
99104
if (sides.isNotEmpty() && side !in sides) return@forEach
100-
val (minX, minY, minZ, maxX, maxY, maxZ) = shrunk.bounds(side)
105+
val (minX, minY, minZ, maxX, maxY, maxZ) = box.shrink(0.01, 0.01, 0.01).bounds(side)
101106
val stepX = (maxX - minX) / resolution
102107
val stepY = (maxY - minY) / resolution
103108
val stepZ = (maxZ - minZ) / resolution
@@ -112,7 +117,7 @@ object VisibilityChecker {
112117
}
113118
}
114119

115-
val Set<Vec3d>.mostCenter: Vec3d?
120+
val Set<Vec3d>.optimum: Vec3d?
116121
get() = reduceOrNull { acc, vec3d ->
117122
acc.add(vec3d)
118123
}?.multiply(1.0 / size.toDouble())

0 commit comments

Comments
 (0)