Skip to content

Commit 7c12aca

Browse files
committed
keep yaw and pitch ordered the same everywhere
1 parent 6c1374e commit 7c12aca

File tree

2 files changed

+65
-66
lines changed

2 files changed

+65
-66
lines changed

src/main/kotlin/com/lambda/interaction/managers/rotating/RotationManager.kt

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,21 @@ object RotationManager : Manager<RotationRequest>(
143143

144144
private fun acceptAndSetRequests(request: RotationRequest) =
145145
when (request) {
146-
is IRotationRequest.Full -> {
147-
if (request.rotation.value != null && requests.all { it?.overridable != false }) {
148-
pitchRequest = request
149-
yawRequest = request
150-
true
151-
} else false
152-
}
153-
is IRotationRequest.Pitch -> {
154-
if (request.pitch.value != null && pitchRequest?.overridable != false) {
155-
pitchRequest = request
156-
true
157-
} else false
158-
}
159-
is IRotationRequest.Yaw -> {
160-
if (request.yaw.value != null && yawRequest?.overridable != false) {
161-
yawRequest = request
162-
true
163-
} else false
164-
}
146+
is IRotationRequest.Full ->
147+
(request.rotation.value != null && requests.all { it?.overridable != false }).also { accepted ->
148+
if (accepted) {
149+
pitchRequest = request
150+
yawRequest = request
151+
}
152+
}
153+
is IRotationRequest.Yaw ->
154+
(request.yaw.value != null && yawRequest?.overridable != false).also { accepted ->
155+
if (accepted) yawRequest = request
156+
}
157+
is IRotationRequest.Pitch ->
158+
(request.pitch.value != null && pitchRequest?.overridable != false).also { accepted ->
159+
if (accepted) pitchRequest = request
160+
}
165161
else -> false
166162
}
167163

@@ -187,8 +183,8 @@ object RotationManager : Manager<RotationRequest>(
187183
runSafe {
188184
usingBaritoneRotation = true
189185
val request = IRotationRequest.Full(BaritoneManager) { Rotation(yaw, pitch) }
190-
pitchRequest = request
191186
yawRequest = request
187+
pitchRequest = request
192188
updateActiveRotation()
193189
changedThisTick = true
194190
}
@@ -285,35 +281,38 @@ object RotationManager : Manager<RotationRequest>(
285281
prevServerRotation = serverRotation
286282
serverRotation = activeRotation
287283

288-
if (pitchRequest?.rotationConfig?.rotationMode == RotationMode.Lock)
289-
mc.player?.pitch = serverRotation.pitchF
290284
if (yawRequest?.rotationConfig?.rotationMode == RotationMode.Lock)
291285
mc.player?.yaw = serverRotation.yawF
286+
if (pitchRequest?.rotationConfig?.rotationMode == RotationMode.Lock)
287+
mc.player?.pitch = serverRotation.pitchF
292288
}
293289

294290
/**
295291
* Updates the [activeRotation]. If [activeRequest] is null, the player's rotation is used.
296292
* Otherwise, the [serverRotation] is interpolated towards the [RotationRequest.target] rotation.
297293
*/
298294
private fun SafeContext.updateActiveRotation() {
299-
val newPitch = pitchRequest?.let { pitchRequest ->
300-
val toPitch = if (pitchRequest.keepTicks >= 0)
301-
pitchRequest.pitch.value ?: activeRotation.pitch
302-
else player.rotation.pitch
303-
serverRotation.slerpPitch(toPitch, pitchRequest.rotationConfig.turnSpeed)
304-
} ?: player.rotation.pitch
305295
val newYaw = yawRequest?.let { yawRequest ->
306296
val toYaw = if (yawRequest.keepTicks >= 0)
307297
yawRequest.yaw.value ?: activeRotation.yaw
308298
else player.rotation.yaw
309299
serverRotation.slerpYaw(toYaw, yawRequest.rotationConfig.turnSpeed)
310300
} ?: player.rotation.yaw
301+
302+
val newPitch = pitchRequest?.let { pitchRequest ->
303+
val toPitch = if (pitchRequest.keepTicks >= 0)
304+
pitchRequest.pitch.value ?: activeRotation.pitch
305+
else player.rotation.pitch
306+
serverRotation.slerpPitch(toPitch, pitchRequest.rotationConfig.turnSpeed)
307+
} ?: player.rotation.pitch
308+
311309
requests.forEach { request ->
312310
if (request == null) return@forEach
313311
if (request.keepTicks-- <= 0) {
314312
request.decayTicks--
315313
}
316314
}
315+
317316
activeRotation = Rotation(newYaw, newPitch)
318317
}
319318

@@ -374,9 +373,9 @@ object RotationManager : Manager<RotationRequest>(
374373

375374
@JvmStatic
376375
fun getRotationForVector(deltaTime: Double): Vec2d? = runSafe {
377-
val pitch = activeRotation.pitchF.takeIf { pitchRequest != null && pitchRequest?.rotationConfig?.rotationMode != RotationMode.Silent }
378376
val yaw = activeRotation.yawF.takeIf { yawRequest != null && yawRequest?.rotationConfig?.rotationMode != RotationMode.Silent }
379-
if (pitch == null && yaw == null) return@runSafe null
377+
val pitch = activeRotation.pitchF.takeIf { pitchRequest != null && pitchRequest?.rotationConfig?.rotationMode != RotationMode.Silent }
378+
if (yaw == null && pitch == null) return@runSafe null
380379

381380
val rot = lerp(deltaTime, serverRotation, Rotation(yaw ?: player.yaw, pitch ?: player.pitch))
382381
return Vec2d(rot.yaw, rot.pitch)

src/main/kotlin/com/lambda/interaction/managers/rotating/RotationRequest.kt

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,6 @@ interface IRotationRequest : Automated {
5353

5454
fun updateRotation()
5555

56-
class Pitch(
57-
automated: Automated,
58-
val buildPitch: SafeContext.() -> Double
59-
) : RotationRequest(automated), PitchRot {
60-
override val pitch = updatableLazy { runSafe { buildPitch() } }
61-
override var keepTicks = rotationConfig.keepTicks
62-
override var decayTicks = rotationConfig.decayTicks
63-
override var age = 0
64-
65-
override val done get(): Boolean {
66-
return abs(RotationManager.activeRotation.pitch - (pitch.value ?: return false)) <= 0.001
67-
}
68-
69-
override fun dist(rotation: Rotation): Double {
70-
return wrap((pitch.value ?: return Double.MAX_VALUE) - rotation.pitch)
71-
}
72-
73-
override fun updateRotation() = pitch.update()
74-
}
75-
7656
class Yaw(
7757
automated: Automated,
7858
val buildYaw: SafeContext.() -> Double
@@ -96,6 +76,26 @@ interface IRotationRequest : Automated {
9676
override fun updateRotation() = yaw.update()
9777
}
9878

79+
class Pitch(
80+
automated: Automated,
81+
val buildPitch: SafeContext.() -> Double
82+
) : RotationRequest(automated), PitchRot {
83+
override val pitch = updatableLazy { runSafe { buildPitch() } }
84+
override var keepTicks = rotationConfig.keepTicks
85+
override var decayTicks = rotationConfig.decayTicks
86+
override var age = 0
87+
88+
override val done get(): Boolean {
89+
return abs(RotationManager.activeRotation.pitch - (pitch.value ?: return false)) <= 0.001
90+
}
91+
92+
override fun dist(rotation: Rotation): Double {
93+
return wrap((pitch.value ?: return Double.MAX_VALUE) - rotation.pitch)
94+
}
95+
96+
override fun updateRotation() = pitch.update()
97+
}
98+
9999
class Full(
100100
automated: Automated,
101101
val buildRotation: SafeContext.() -> Rotation
@@ -122,42 +122,42 @@ interface IRotationRequest : Automated {
122122
override fun updateRotation() = rotation.update()
123123
}
124124

125-
interface PitchRot : IRotationRequest { val pitch: UpdatableLazy<Double?> }
126125
interface YawRot : IRotationRequest { val yaw: UpdatableLazy<Double?> }
126+
interface PitchRot : IRotationRequest { val pitch: UpdatableLazy<Double?> }
127127
interface FullRot : YawRot, PitchRot { val rotation: UpdatableLazy<Rotation?> }
128128

129129
class RotationRequestBuilder {
130130
var pitchBuilder: (SafeContext.() -> Double)? = null
131131
var yawBuilder: (SafeContext.() -> Double)? = null
132132
var rotationBuilder: (SafeContext.() -> Rotation)? = null
133133

134-
@JvmName("pitchBuilder1")
134+
@JvmName("yawBuilder1")
135135
@RotationRequestDsl
136-
fun pitch(builder: SafeContext.() -> Double) { pitchBuilder = builder }
136+
fun yaw(builder: SafeContext.() -> Double) { yawBuilder = builder }
137137

138-
@JvmName("pitchBuilder2")
138+
@JvmName("yawBuilder2")
139139
@RotationRequestDsl
140-
fun pitch(builder: SafeContext.() -> Float) { pitchBuilder = { builder().toDouble() } }
140+
fun yaw(builder: SafeContext.() -> Float) { yawBuilder = { builder().toDouble() } }
141141

142142
@RotationRequestDsl
143-
fun pitch(pitch: Double) { pitchBuilder = { pitch } }
143+
fun yaw(yaw: Double) { yawBuilder = { yaw } }
144144

145145
@RotationRequestDsl
146-
fun pitch(pitch: Float) { pitchBuilder = { pitch.toDouble() } }
146+
fun yaw(yaw: Float) { yawBuilder = { yaw.toDouble() } }
147147

148-
@JvmName("yawBuilder1")
148+
@JvmName("pitchBuilder1")
149149
@RotationRequestDsl
150-
fun yaw(builder: SafeContext.() -> Double) { yawBuilder = builder }
150+
fun pitch(builder: SafeContext.() -> Double) { pitchBuilder = builder }
151151

152-
@JvmName("yawBuilder2")
152+
@JvmName("pitchBuilder2")
153153
@RotationRequestDsl
154-
fun yaw(builder: SafeContext.() -> Float) { yawBuilder = { builder().toDouble() } }
154+
fun pitch(builder: SafeContext.() -> Float) { pitchBuilder = { builder().toDouble() } }
155155

156156
@RotationRequestDsl
157-
fun yaw(yaw: Double) { yawBuilder = { yaw } }
157+
fun pitch(pitch: Double) { pitchBuilder = { pitch } }
158158

159159
@RotationRequestDsl
160-
fun yaw(yaw: Float) { yawBuilder = { yaw.toDouble() } }
160+
fun pitch(pitch: Float) { pitchBuilder = { pitch.toDouble() } }
161161

162162
@RotationRequestDsl
163163
fun rotation(builder: SafeContext.() -> Rotation) { rotationBuilder = builder }
@@ -182,14 +182,14 @@ interface IRotationRequest : Automated {
182182
@RotationRequestDsl
183183
context(automated: Automated)
184184
private fun RotationRequestBuilder.build(): RotationRequest {
185-
val pitchBuilder = pitchBuilder
186185
val yawBuilder = yawBuilder
186+
val pitchBuilder = pitchBuilder
187187
val rotationBuilder = rotationBuilder
188188
return when {
189189
rotationBuilder != null -> Full(automated, rotationBuilder)
190-
pitchBuilder != null && yawBuilder != null -> Full(automated) { Rotation(pitchBuilder(), yawBuilder()) }
191-
pitchBuilder != null -> Pitch(automated, pitchBuilder)
190+
yawBuilder != null && pitchBuilder != null -> Full(automated) { Rotation(yawBuilder(), pitchBuilder()) }
192191
yawBuilder != null -> Yaw(automated, yawBuilder)
192+
pitchBuilder != null -> Pitch(automated, pitchBuilder)
193193
else -> throw IllegalArgumentException("Must specify at least one rotation value to build a rotation request")
194194
}
195195
}

0 commit comments

Comments
 (0)