Skip to content

Commit b322d38

Browse files
committed
Freecam refactor
1 parent f533cd9 commit b322d38

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ object Speed : Module(
105105
}
106106
}
107107

108+
// TODO: Diagonal movement when not jumping
109+
// needs movement prediction engine or a workaround to detect jumping 1 tick before
108110
listener<RotationEvent.Update> { event ->
109111
if (mode != Mode.GRIM_STRAFE) return@listener
110112
if (!shouldWork() || !isInputting) return@listener

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

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.lambda.module.modules.player
22

3-
import baritone.utils.PlayerMovementInput
43
import com.lambda.Lambda.mc
5-
import com.lambda.config.groups.RotationSettings
4+
import com.lambda.config.groups.IRotationConfig
65
import com.lambda.event.events.*
76
import com.lambda.event.listener.SafeListener.Companion.listener
87
import com.lambda.interaction.rotation.Rotation
@@ -11,32 +10,37 @@ import com.lambda.interaction.rotation.RotationContext
1110
import com.lambda.interaction.rotation.RotationMode
1211
import com.lambda.module.Module
1312
import com.lambda.module.tag.ModuleTag
13+
import com.lambda.util.math.VecUtils.plus
14+
import com.lambda.util.math.VecUtils.times
15+
import com.lambda.util.player.MovementUtils.calcMoveRad
1416
import com.lambda.util.player.MovementUtils.cancel
17+
import com.lambda.util.player.MovementUtils.handledByBaritone
18+
import com.lambda.util.player.MovementUtils.isInputting
19+
import com.lambda.util.player.MovementUtils.movementVector
20+
import com.lambda.util.player.MovementUtils.newMovementInput
21+
import com.lambda.util.player.MovementUtils.roundedForward
22+
import com.lambda.util.player.MovementUtils.roundedStrafing
1523
import com.lambda.util.player.MovementUtils.verticalMovement
1624
import com.lambda.util.primitives.extension.interpolate
1725
import com.lambda.util.primitives.extension.partialTicks
1826
import com.lambda.util.primitives.extension.rotation
1927
import com.lambda.util.world.raycast.RayCastUtils.orMiss
2028
import com.lambda.util.world.raycast.RayCastUtils.orNull
21-
import net.minecraft.client.input.KeyboardInput
2229
import net.minecraft.client.option.Perspective
23-
import net.minecraft.entity.Entity
2430
import net.minecraft.util.math.Vec3d
2531

2632
object Freecam : Module(
2733
name = "Freecam",
2834
description = "Move your camera freely",
2935
defaultTags = setOf(ModuleTag.PLAYER)
3036
) {
31-
private val speed by setting("Speed", 0.5f, 0.1f..1.0f, 0.1f)
32-
private val sprint by setting("Sprint Multiplier", 3.0f, 0.1f..10.0f, 0.1f, description = "Set below 1.0 to fly slower on sprint.")
37+
private val speed by setting("Speed", 0.5, 0.1..1.0, 0.1)
38+
private val sprint by setting("Sprint Multiplier", 3.0, 0.1..10.0, 0.1, description = "Set below 1.0 to fly slower on sprint.")
3339
private val reach by setting("Reach", 10.0, 1.0..100.0, 1.0, "Freecam reach distance")
3440
private val rotateToTarget by setting("Rotate to target", true)
3541

36-
private val rotationConfig = RotationSettings(this) {
37-
rotateToTarget
38-
}.apply {
39-
rotationMode = RotationMode.LOCK
42+
private val rotationConfig = object : IRotationConfig.Instant {
43+
override val rotationMode = RotationMode.LOCK
4044
}
4145

4246
private var lastPerspective = Perspective.FIRST_PERSON
@@ -64,7 +68,6 @@ object Freecam : Module(
6468
init {
6569
onEnable {
6670
lastPerspective = mc.options.perspective
67-
mc.options.perspective = Perspective.FIRST_PERSON
6871
position = player.eyePos
6972
rotation = player.rotation
7073
velocity = Vec3d.ZERO
@@ -74,12 +77,12 @@ object Freecam : Module(
7477
mc.options.perspective = lastPerspective
7578
}
7679

77-
listener<RotationEvent.Update>(Int.MAX_VALUE) {
80+
listener<RotationEvent.Update>(Int.MAX_VALUE) { event ->
7881
if (!rotateToTarget) return@listener
7982
val target = mc.crosshairTarget?.orNull ?: return@listener
8083

8184
val rotation = player.eyePos.rotationTo(target.pos)
82-
it.context = RotationContext(rotation, rotationConfig)
85+
event.context = RotationContext(rotation, rotationConfig)
8386
}
8487

8588
listener<EntityEvent.ChangeLookDirection> {
@@ -91,37 +94,35 @@ object Freecam : Module(
9194
}
9295

9396
listener<MovementEvent.InputUpdate> { event ->
97+
mc.options.perspective = Perspective.FIRST_PERSON
98+
9499
// Don't block baritone from working
95-
if (event.input !is PlayerMovementInput) {
100+
if (!event.input.handledByBaritone) {
96101
// Reset actual input
97102
event.input.cancel()
98103
}
99104

100105
// Create new input for freecam
101-
val input = KeyboardInput(mc.options).apply {
102-
tick(false, 1f)
103-
}
104-
105-
val inputVec = Vec3d(
106-
input.movementSideways.toDouble(),
107-
input.verticalMovement.toDouble(),
108-
input.movementForward.toDouble()
109-
)
106+
val input = newMovementInput(assumeBaritoneUsage = false, slowDownCheck = false)
107+
val sprintModifier = if (mc.options.sprintKey.isPressed) sprint else 1.0
108+
val moveDir = calcMoveRad(rotation.yawF, input.roundedForward, input.roundedStrafing)
109+
var moveVec = movementVector(moveDir, input.verticalMovement) * speed * sprintModifier
110+
if (!input.isInputting) moveVec *= Vec3d(0.0, 1.0, 0.0)
110111

111-
val endSpeed = speed * if (mc.options.sprintKey.isPressed) sprint else 1.0f
112-
val velocityDelta = Entity.movementInputToVelocity(inputVec, endSpeed, rotation.yawF)
112+
// Apply movement
113+
velocity += moveVec
114+
velocity *= 0.6
113115

114-
// Move freecam
115-
velocity = velocity.add(velocityDelta).multiply(0.6)
116+
// Update position
116117
prevPosition = position
117-
position = position.add(velocity)
118+
position += velocity
118119
}
119120

120121
listener<RenderEvent.UpdateTarget> {
121122
it.cancel()
122123

123124
mc.crosshairTarget = rotation
124-
.rayCast(reach, eye = interpolatedPosition)
125+
.rayCast(reach, interpolatedPosition)
125126
.orMiss // Can't be null (otherwise mc will spam "Null returned as 'hitResult', this shouldn't happen!")
126127
}
127128

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@ import kotlin.math.*
2121
object MovementUtils {
2222
val Input.roundedForward get() = sign(movementForward).toDouble()
2323
val Input.roundedStrafing get() = sign(movementSideways).toDouble()
24+
val Input.handledByBaritone get() = this !is KeyboardInput
2425

2526
val Input.isInputting get() = roundedForward != 0.0 || roundedStrafing != 0.0
2627
val SafeContext.isInputting get() = player.input.isInputting
2728

28-
fun SafeContext.newMovementInput(assumeBaritoneUsage: Boolean = true): Input {
29-
val input = if (assumeBaritoneUsage && player.input is PlayerMovementInput) {
29+
fun SafeContext.newMovementInput(assumeBaritoneUsage: Boolean = true, slowDownCheck: Boolean = true): Input {
30+
val input = if (assumeBaritoneUsage && player.input.handledByBaritone) {
3031
player.input
3132
} else {
32-
val multiplier = if (!player.shouldSlowDown()) 1f
33-
else (0.3f + getSwiftSneakSpeedBoost(player)).coerceIn(0f, 1f)
33+
var multiplier = 1f
34+
35+
if (slowDownCheck && player.shouldSlowDown()) multiplier =
36+
0.3f + getSwiftSneakSpeedBoost(player)
3437

3538
KeyboardInput(mc.options).apply {
36-
tick(true, multiplier)
39+
tick(true, multiplier.coerceIn(0f, 1f))
3740
}
3841
}
3942

@@ -72,7 +75,7 @@ object MovementUtils {
7275

7376
val Input.verticalMovement
7477
get() =
75-
jumping.toInt() - sneaking.toInt()
78+
(jumping.toInt() - sneaking.toInt()).toDouble()
7679

7780
private fun inputMoveOffset(
7881
moveForward: Double,

0 commit comments

Comments
 (0)