Skip to content

Commit 5c258b8

Browse files
committed
TargetStrafe
1 parent cfe04d4 commit 5c258b8

File tree

4 files changed

+164
-2
lines changed

4 files changed

+164
-2
lines changed

common/src/main/java/com/lambda/mixin/input/KeyBindingMixin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.lambda.module.modules.movement.Speed;
44
import com.lambda.module.modules.movement.Sprint;
5+
import com.lambda.module.modules.movement.TargetStrafe;
56
import net.minecraft.client.option.KeyBinding;
67
import org.spongepowered.asm.mixin.Mixin;
78
import org.spongepowered.asm.mixin.injection.At;
@@ -19,5 +20,6 @@ void autoSprint(CallbackInfoReturnable<Boolean> cir) {
1920

2021
if (Sprint.INSTANCE.isEnabled()) cir.setReturnValue(true);
2122
if (Speed.INSTANCE.isEnabled() && Speed.getMode() == Speed.Mode.GRIM_STRAFE) cir.setReturnValue(true);
23+
if (TargetStrafe.INSTANCE.isEnabled() && TargetStrafe.isActive()) cir.setReturnValue(true);
2224
}
2325
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import com.lambda.module.Module
1313
import com.lambda.module.tag.ModuleTag
1414
import com.lambda.util.Nameable
1515
import com.lambda.util.player.MovementUtils.addSpeed
16+
import com.lambda.util.player.MovementUtils.buildMovementInput
1617
import com.lambda.util.player.MovementUtils.calcMoveYaw
1718
import com.lambda.util.player.MovementUtils.handledByBaritone
1819
import com.lambda.util.player.MovementUtils.isInputting
20+
import com.lambda.util.player.MovementUtils.mergeFrom
1921
import com.lambda.util.player.MovementUtils.motionY
2022
import com.lambda.util.player.MovementUtils.moveDelta
2123
import com.lambda.util.player.MovementUtils.newMovementInput
@@ -25,6 +27,7 @@ import com.lambda.util.player.MovementUtils.setSpeed
2527
import com.lambda.util.primitives.extension.contains
2628
import com.lambda.util.world.WorldUtils.getFastEntities
2729
import net.minecraft.entity.LivingEntity
30+
import net.minecraft.entity.decoration.ArmorStandEntity
2831
import net.minecraft.entity.vehicle.BoatEntity
2932

3033
object Speed : Module(
@@ -110,7 +113,7 @@ object Speed : Module(
110113
listener<RotationEvent.Update> { event ->
111114
if (mode != Mode.GRIM_STRAFE) return@listener
112115
if (!shouldWork() || !isInputting) return@listener
113-
if (player.input.handledByBaritone) return@listener
116+
if (player.input.handledByBaritone || TargetStrafe.isActive) return@listener
114117

115118
val input = newMovementInput()
116119
val yaw = calcMoveYaw(player.yaw, input.roundedForward, input.roundedStrafing)
@@ -119,6 +122,18 @@ object Speed : Module(
119122
event.context = RotationContext(rotation, rotationConfig)
120123
}
121124

125+
listener<MovementEvent.InputUpdate> { event ->
126+
if (mode != Mode.GRIM_STRAFE) return@listener
127+
if (!shouldWork() || !isInputting) return@listener
128+
if (player.input.handledByBaritone || TargetStrafe.isActive) return@listener
129+
130+
event.input.mergeFrom(
131+
buildMovementInput(
132+
1.0, 0.0, event.input.jumping, event.input.sneaking
133+
)
134+
)
135+
}
136+
122137
onEnable {
123138
reset()
124139
}
@@ -131,7 +146,7 @@ object Speed : Module(
131146

132147
getFastEntities<LivingEntity>(
133148
player.pos, 3.0,
134-
predicate = { player.boundingBox.expand(1.0) in it.boundingBox },
149+
predicate = { player.boundingBox.expand(1.0) in it.boundingBox && it !is ArmorStandEntity },
135150
iterator = { e, _ ->
136151
val colliding = player.boundingBox in e.boundingBox
137152
val multiplier = if (colliding) grimCollideMultiplier else 1.0
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.lambda.module.modules.movement
2+
3+
import com.lambda.config.groups.IRotationConfig
4+
import com.lambda.event.events.MovementEvent
5+
import com.lambda.event.events.RotationEvent
6+
import com.lambda.event.events.TickEvent
7+
import com.lambda.event.listener.SafeListener.Companion.listener
8+
import com.lambda.interaction.rotation.Rotation.Companion.rotationTo
9+
import com.lambda.interaction.rotation.RotationContext
10+
import com.lambda.interaction.rotation.RotationMode
11+
import com.lambda.module.Module
12+
import com.lambda.module.tag.ModuleTag
13+
import com.lambda.util.math.VecUtils.distSq
14+
import com.lambda.util.player.MovementUtils.buildMovementInput
15+
import com.lambda.util.player.MovementUtils.mergeFrom
16+
import com.lambda.util.world.WorldUtils.getClosestEntity
17+
import net.minecraft.entity.LivingEntity
18+
import net.minecraft.entity.decoration.ArmorStandEntity
19+
import kotlin.math.pow
20+
21+
object TargetStrafe : Module(
22+
name = "TargetStrafe",
23+
description = "Automatically strafes around entities",
24+
defaultTags = setOf(ModuleTag.MOVEMENT)
25+
) {
26+
private val range by setting("Range", 6.0, 2.0..10.0, 0.5)
27+
private val targetDistance by setting("Target Distance", 1.0, 0.0..5.0, 0.5)
28+
private val jitterCompensation by setting("Jitter Compensation", 0.0, 0.0..1.0, 0.1)
29+
private val stabilize by setting("Stabilize", StabilizationMode.NORMAL)
30+
31+
enum class StabilizationMode {
32+
NONE, WEAK, NORMAL, STRONG
33+
}
34+
35+
private var targetEntity: LivingEntity? = null
36+
37+
private var forwardDirection = 1
38+
private var strafeDirection = 1
39+
40+
private val rotationConfig = object : IRotationConfig.Instant {
41+
override val rotationMode = RotationMode.SYNC
42+
}
43+
44+
@JvmStatic val isActive get() = targetEntity != null
45+
46+
init {
47+
listener<TickEvent.Post> {
48+
// ToDo: Use KillAura.target instead
49+
targetEntity = getClosestEntity<LivingEntity>(
50+
player.pos, range,
51+
predicate = { e -> e !is ArmorStandEntity }
52+
)
53+
54+
if (player.horizontalCollision) strafeDirection *= -1
55+
56+
if (targetEntity == null) {
57+
forwardDirection = 1
58+
strafeDirection = 1
59+
}
60+
}
61+
62+
listener<RotationEvent.Strafe> { event ->
63+
targetEntity?.let {
64+
event.strafeYaw = player.eyePos.rotationTo(it.boundingBox.center).yaw
65+
}
66+
}
67+
68+
listener<RotationEvent.Update> { event ->
69+
targetEntity?.let {
70+
event.context = RotationContext(
71+
player.eyePos.rotationTo(it.boundingBox.center),
72+
rotationConfig
73+
)
74+
}
75+
}
76+
77+
listener<MovementEvent.InputUpdate> { event ->
78+
targetEntity?.let { target ->
79+
val distSq = player.pos distSq target.pos
80+
val keepRange = 0.5 * jitterCompensation
81+
82+
forwardDirection = when {
83+
distSq > (targetDistance + keepRange).pow(2) -> 1
84+
distSq < (targetDistance - keepRange).pow(2) -> -1
85+
else -> forwardDirection
86+
}
87+
88+
val shouldStabilize = when (stabilize) {
89+
StabilizationMode.NONE -> false
90+
StabilizationMode.WEAK -> player.age % 3 == 0
91+
StabilizationMode.NORMAL -> player.age % 2 == 0
92+
StabilizationMode.STRONG -> true
93+
}
94+
95+
var strafe = strafeDirection.toDouble()
96+
if (shouldStabilize && distSq > (targetDistance + 0.5).pow(2)) strafe = 0.0
97+
98+
event.input.mergeFrom(
99+
buildMovementInput(
100+
forwardDirection.toDouble(),
101+
strafe,
102+
true
103+
)
104+
)
105+
}
106+
}
107+
108+
onEnable {
109+
targetEntity = null
110+
forwardDirection = 1
111+
strafeDirection = 1
112+
}
113+
}
114+
}

common/src/main/kotlin/com/lambda/util/player/MovementUtils.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,37 @@ object MovementUtils {
4141
}
4242
}
4343

44+
fun buildMovementInput(
45+
forward: Double,
46+
strafe: Double,
47+
jump: Boolean = false,
48+
sneak: Boolean = false
49+
) = Input().apply {
50+
movementForward = forward.toFloat()
51+
movementSideways = strafe.toFloat()
52+
53+
pressingForward = forward > 0.0
54+
pressingBack = forward < 0.0
55+
pressingLeft = strafe < 0.0
56+
pressingRight = strafe > 0.0
57+
58+
jumping = jump
59+
sneaking = sneak
60+
}
61+
62+
fun Input.mergeFrom(input: Input) {
63+
movementForward = input.movementForward
64+
movementSideways = input.movementSideways
65+
66+
pressingForward = input.pressingForward
67+
pressingBack = input.pressingBack
68+
pressingLeft = input.pressingLeft
69+
pressingRight = input.pressingRight
70+
71+
jumping = input.jumping
72+
sneaking = input.sneaking
73+
}
74+
4475
fun Input.cancel(cancelVertical: Boolean = true) {
4576
movementForward = 0f
4677
movementSideways = 0f

0 commit comments

Comments
 (0)