Skip to content

Commit e3c4bb4

Browse files
committed
ElytraAttitudeControl: Lint
1 parent 9a68185 commit e3c4bb4

File tree

1 file changed

+96
-88
lines changed

1 file changed

+96
-88
lines changed

src/main/kotlin/com/lambda/module/modules/movement/ElytraAttitudeControl.kt

Lines changed: 96 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -32,119 +32,127 @@ import net.minecraft.util.math.Vec3d
3232
import kotlin.time.Duration.Companion.seconds
3333

3434
object ElytraAttitudeControl : Module(
35-
name = "ElytraAttitudeControl",
36-
description = "Automatically control attitude or speed while elytra flying",
37-
tag = ModuleTag.MOVEMENT,
35+
name = "ElytraAttitudeControl",
36+
description = "Automatically control attitude or speed while elytra flying",
37+
tag = ModuleTag.MOVEMENT,
3838
) {
39-
val controlValue by setting("Control Value", Mode.Altitude)
39+
val controlValue by setting("Control Value", Mode.Altitude)
4040

41-
val maxPitchAngle by setting("Max Pitch Angle", 45.0, 0.0..90.0, 1.0, unit = "°", description = "Maximum pitch angle")
42-
val disableOnFirework by setting("Disable On Firework", false, description = "Disables the module when a firework is used")
41+
val maxPitchAngle by setting("Max Pitch Angle", 45.0, 0.0..90.0, 1.0, unit = "°", description = "Maximum pitch angle")
42+
val disableOnFirework by setting("Disable On Firework", false, description = "Disables the module when a firework is used")
4343

44-
val targetAltitude by setting("Target Altitude", 120, 0..256, 10, unit = " blocks", description = "Adjusts pitch to control altitude") { controlValue == Mode.Altitude }
44+
val targetAltitude by setting("Target Altitude", 120, 0..256, 10, unit = " blocks", description = "Adjusts pitch to control altitude")
45+
{ controlValue == Mode.Altitude }
4546
val altitudeControllerP by setting("Altitude Control P", 1.2, 0.0..2.0, 0.05).group(Group.AltitudeControl)
4647
val altitudeControllerD by setting("Altitude Control D", 0.85, 0.0..1.0, 0.05).group(Group.AltitudeControl)
4748
val altitudeControllerI by setting("Altitude Control I", 0.04, 0.0..1.0, 0.05).group(Group.AltitudeControl)
4849
val altitudeControllerConst by setting("Altitude Control Const", 0.0, 0.0..10.0, 0.1).group(Group.AltitudeControl)
4950

50-
val targetSpeed by setting("Target Speed", 28.0, 0.1..50.0, 0.1, unit = " m/s", description = "Adjusts pitch to control speed") { controlValue == Mode.Speed }
51-
val horizontalSpeed by setting("Horizontal Speed", false, description = "Uses horizontal speed instead of total speed for speed control") { controlValue == Mode.Speed }
51+
val targetSpeed by setting("Target Speed", 28.0, 0.1..50.0, 0.1, unit = " m/s", description = "Adjusts pitch to control speed")
52+
{ controlValue == Mode.Speed }
53+
val horizontalSpeed by setting("Horizontal Speed", false, description = "Uses horizontal speed instead of total speed for speed control")
54+
{ controlValue == Mode.Speed }
5255
val speedControllerP by setting("Speed Control P", 6.75, 0.0..10.0, 0.05).group(Group.SpeedControl)
5356
val speedControllerD by setting("Speed Control D", 4.5, 0.0..5.0, 0.05).group(Group.SpeedControl)
5457
val speedControllerI by setting("Speed Control I", 0.3, 0.0..1.0, 0.05).group(Group.SpeedControl)
5558

5659
val useFireworkOnHeight by setting("Use Firework On Height", false, "Use fireworks when below a certain height")
57-
val minHeight by setting("Min Height", 50, 0..256, 10, unit = " blocks", description = "Minimum height to use firework") { useFireworkOnHeight }
60+
val minHeight by setting("Min Height", 50, 0..256, 10, unit = " blocks", description = "Minimum height to use firework")
61+
{ useFireworkOnHeight }
5862

5963
val useFireworkOnSpeed by setting("Use Firework On Speed", false, "Use fireworks based on speed")
60-
val minSpeed by setting("Min Speed", 20.0, 0.1..50.0, 0.1, unit = " m/s", description = "Minimum speed to use fireworks") { useFireworkOnSpeed }
64+
val minSpeed by setting("Min Speed", 20.0, 0.1..50.0, 0.1, unit = " m/s", description = "Minimum speed to use fireworks")
65+
{ useFireworkOnSpeed }
6166

62-
var lastPos : Vec3d = Vec3d.ZERO
63-
val speedController: PIController = PIController({ speedControllerP }, { speedControllerD }, { speedControllerI }, { 0.0 })
64-
val altitudeController: PIController = PIController({ altitudeControllerP }, { altitudeControllerD }, { altitudeControllerI }, { altitudeControllerConst })
67+
var lastPos: Vec3d = Vec3d.ZERO
68+
val speedController: PIController = PIController({ speedControllerP }, { speedControllerD }, { speedControllerI }, { 0.0 })
69+
val altitudeController: PIController = PIController(
70+
{ altitudeControllerP }, { altitudeControllerD }, { altitudeControllerI },
71+
{ altitudeControllerConst })
6572

6673
val usageDelay = Timer()
6774

68-
init {
69-
listen<TickEvent.Pre> {
70-
if (!player.isGliding) return@listen
71-
if (player.hasFirework && disableOnFirework) return@listen
75+
init {
76+
listen<TickEvent.Pre> {
77+
if (!player.isGliding) return@listen
78+
if (player.hasFirework && disableOnFirework) return@listen
7279

73-
val outputPitch = when (controlValue) {
74-
Mode.Speed -> {
75-
var speed = player.pos.subtract(lastPos)
76-
if (horizontalSpeed) {
80+
val outputPitch = when (controlValue) {
81+
Mode.Speed -> {
82+
var speed = player.pos.subtract(lastPos)
83+
if (horizontalSpeed) {
7784
speed = Vec3d(speed.x, 0.0, speed.z)
78-
}
79-
80-
speedController.getOutput(targetSpeed, SpeedUnit.MetersPerSecond.convertFromMinecraft(speed.length()))
81-
}
82-
Mode.Altitude -> {
83-
val currentAltitude = player.y
84-
-1 * altitudeController.getOutput(targetAltitude.toDouble(), currentAltitude) // Negative because in minecraft pitch > 0 is looking down not up
85-
}
86-
}
87-
val newPitch = outputPitch.coerceIn(-maxPitchAngle, maxPitchAngle)
88-
// lookAt(Rotation(player.yaw, newPitch.toFloat())).requestBy(this@ElytraAutopilot) // TODO: Use this when rotation system accepts pitch changes
89-
player.pitch = newPitch.toFloat()
90-
91-
lastPos = player.pos
92-
93-
if (usageDelay.timePassed(2.seconds) && !player.hasFirework) {
94-
if (useFireworkOnHeight && minHeight > player.y) {
95-
usageDelay.reset()
96-
runSafe {
97-
startFirework(true)
98-
}
99-
}
100-
if (useFireworkOnSpeed && minSpeed > SpeedUnit.MetersPerSecond.convertFromMinecraft(player.velocity.length())) {
85+
}
86+
87+
speedController.getOutput(targetSpeed, SpeedUnit.MetersPerSecond.convertFromMinecraft(speed.length()))
88+
}
89+
Mode.Altitude -> {
90+
val currentAltitude = player.y
91+
-1 * altitudeController.getOutput(targetAltitude.toDouble(), currentAltitude) // Negative because in minecraft pitch > 0 is looking down not up
92+
}
93+
}
94+
val newPitch = outputPitch.coerceIn(-maxPitchAngle, maxPitchAngle)
95+
// lookAt(Rotation(player.yaw, newPitch.toFloat())).requestBy(this@ElytraAutopilot) // TODO: Use this when rotation system accepts pitch changes
96+
player.pitch = newPitch.toFloat()
97+
98+
lastPos = player.pos
99+
100+
if (usageDelay.timePassed(2.seconds) && !player.hasFirework) {
101+
if (useFireworkOnHeight && minHeight > player.y) {
101102
usageDelay.reset()
102-
runSafe {
103-
startFirework(true)
104-
}
105-
}
106-
}
107-
}
108-
109-
onEnable {
110-
speedController.reset()
111-
altitudeController.reset()
112-
lastPos = player.pos
113-
}
114-
}
115-
116-
val ClientPlayerEntity.hasFirework: Boolean
117-
get() = clientWorld.getEntitiesByType(
118-
EntityType.FIREWORK_ROCKET,
119-
boundingBox.expand(4.0),
120-
{ it.distanceTo(this) < 4.0 }
121-
).isNotEmpty()
103+
runSafe {
104+
startFirework(true)
105+
}
106+
}
107+
if (useFireworkOnSpeed && minSpeed > SpeedUnit.MetersPerSecond.convertFromMinecraft(player.velocity.length())) {
108+
usageDelay.reset()
109+
runSafe {
110+
startFirework(true)
111+
}
112+
}
113+
}
114+
}
115+
116+
onEnable {
117+
speedController.reset()
118+
altitudeController.reset()
119+
lastPos = player.pos
120+
}
121+
}
122+
123+
val ClientPlayerEntity.hasFirework: Boolean
124+
get() = clientWorld.getEntitiesByType(
125+
EntityType.FIREWORK_ROCKET,
126+
boundingBox.expand(4.0),
127+
{ it.distanceTo(this) < 4.0 }
128+
).isNotEmpty()
122129

123130
class PIController(val valueP: () -> Double, val valueD: () -> Double, val valueI: () -> Double, val constant: () -> Double) {
124-
var accumulator = 0.0 // Integral term accumulator
131+
var accumulator = 0.0 // Integral term accumulator
125132
var lastDiff = 0.0
126-
fun getOutput(target: Double, current: Double): Double {
127-
val diff = target - current
128-
val diffDt = diff - lastDiff
133+
fun getOutput(target: Double, current: Double): Double {
134+
val diff = target - current
135+
val diffDt = diff - lastDiff
129136
accumulator += diff
130137

131-
accumulator = accumulator.coerceIn(-100.0, 100.0) // Prevent integral windup
132-
lastDiff = diff
133-
134-
return diffDt * valueD() + diff * valueP() + accumulator * valueI() + constant()
135-
}
136-
137-
fun reset() {
138-
accumulator = 0.0
139-
}
140-
}
141-
142-
enum class Mode {
143-
Speed,
144-
Altitude;
145-
}
146-
enum class Group(override val displayName: String) : NamedEnum {
147-
SpeedControl("Speed Control"),
148-
AltitudeControl("Altitude Control");
149-
}
138+
accumulator = accumulator.coerceIn(-100.0, 100.0) // Prevent integral windup
139+
lastDiff = diff
140+
141+
return diffDt * valueD() + diff * valueP() + accumulator * valueI() + constant()
142+
}
143+
144+
fun reset() {
145+
accumulator = 0.0
146+
}
147+
}
148+
149+
enum class Mode {
150+
Speed,
151+
Altitude;
152+
}
153+
154+
enum class Group(override val displayName: String) : NamedEnum {
155+
SpeedControl("Speed Control"),
156+
AltitudeControl("Altitude Control");
157+
}
150158
}

0 commit comments

Comments
 (0)