Skip to content

Commit 7a0c78e

Browse files
committed
split Computer#tick into tick+renderTick
Signed-off-by: Octol1ttle <[email protected]>
1 parent 6eb4eaa commit 7a0c78e

File tree

11 files changed

+46
-36
lines changed

11 files changed

+46
-36
lines changed

src/main/kotlin/nl/enjarai/doabarrelroll/compat/flightassistant/DaBRRollComputer.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ class DaBRRollComputer(computers: ComputerBus) : Computer(computers), RollSource
2222
return (computers.data.player as RollEntity).`doABarrelRoll$setRoll`(getRoll() + diff)
2323
}
2424

25-
override fun tick() {
26-
}
27-
2825
override fun reset() {
2926
}
3027

src/main/kotlin/ru/octol1ttle/flightassistant/api/computer/Computer.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@ abstract class Computer(val computers: ComputerBus) {
2727
}
2828

2929
/**
30-
* Called once per level render
30+
* Called once per tick
3131
*
3232
* If this method throws an exception or error, it is caught and the computer is considered "faulted".
3333
* It won't be ticked until it is reset and an alert about the issue will be displayed
3434
*/
35-
abstract fun tick()
35+
open fun tick() {}
36+
37+
/**
38+
* Called once per level render.
39+
*
40+
* @see tick
41+
*/
42+
open fun renderTick() {}
3643

3744
/**
3845
* Called when this computer should be reset. This computer's state should be reset to the initial ("everything is good") state.

src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/ComputerHost.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,24 @@ internal object ComputerHost : ModuleController<Computer>, ComputerBus {
118118
return
119119
}
120120

121+
mc.profiler.push("flightassistant:computer_host")
121122
for ((id: ResourceLocation, computer: Computer) in computers) {
123+
mc.profiler.push(id.toString())
122124
if (!computer.isDisabledOrFaulted()) {
123125
try {
124-
computer.tick()
126+
repeat(FATickCounter.ticksPassed) {
127+
computer.tick()
128+
}
129+
computer.renderTick()
125130
} catch (t: Throwable) {
126131
onComputerFault(computer)
127132

128133
FlightAssistant.logger.error("Exception ticking computer with identifier: $id", t)
129134
}
130135
}
136+
mc.profiler.pop()
131137
}
138+
mc.profiler.pop()
132139
}
133140

134141
@Deprecated("Will be private")

src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/FlightPlanComputer.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import ru.octol1ttle.flightassistant.FlightAssistant
1212
import ru.octol1ttle.flightassistant.api.computer.Computer
1313
import ru.octol1ttle.flightassistant.api.computer.ComputerBus
1414
import ru.octol1ttle.flightassistant.api.computer.ComputerQuery
15-
import ru.octol1ttle.flightassistant.api.util.FATickCounter
1615
import ru.octol1ttle.flightassistant.api.util.LimitedFIFOQueue
1716
import ru.octol1ttle.flightassistant.api.util.extensions.distance2d
1817
import ru.octol1ttle.flightassistant.api.util.extensions.formatRoot
@@ -34,9 +33,7 @@ class FlightPlanComputer(computers: ComputerBus) : Computer(computers) {
3433
override fun tick() {
3534
updateEnrouteData()
3635
currentPhase = updateFlightPhase()
37-
if (FATickCounter.ticksPassed != 0) {
38-
groundSpeeds.add(computers.data.velocityPerSecond.horizontalDistance())
39-
}
36+
groundSpeeds.add(computers.data.velocityPerSecond.horizontalDistance())
4037
}
4138

4239
private fun updateFlightPhase(): FlightPhase {

src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/base/HeadingComputer.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,20 @@ class HeadingComputer(computers: ComputerBus) : Computer(computers) {
3131
return
3232
}
3333

34-
val heading: Float = computers.data.heading
3534
val finalInput: ControlInput? = inputs.getActiveHighestPriority().firstOrNull()
3635
if (finalInput == null) {
3736
activeInput = null
3837
return
3938
}
4039

4140
activeInput = finalInput
42-
if (computers.data.automationsAllowed() && finalInput.status == ControlInput.Status.ACTIVE) {
43-
smoothSetHeading(computers.data.player, heading, finalInput.target.throwIfNotInRange(0.0f..360.0f), finalInput.deltaTimeMultiplier.throwIfNotInRange(0.001f..Float.MAX_VALUE))
41+
}
42+
43+
override fun renderTick() {
44+
val input = activeInput ?: return
45+
46+
if (computers.data.automationsAllowed() && input.status == ControlInput.Status.ACTIVE) {
47+
smoothSetHeading(computers.data.player, computers.data.heading, input.target.throwIfNotInRange(0.0f..360.0f), input.deltaTimeMultiplier.throwIfNotInRange(0.001f..Float.MAX_VALUE))
4448
}
4549
}
4650

src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/base/PitchComputer.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,27 @@ class PitchComputer(computers: ComputerBus) : Computer(computers), FlightControl
6363
return
6464
}
6565

66-
val pitch: Float = computers.data.pitch
6766
val finalInput: ControlInput? = inputs.getActiveHighestPriority().maxByOrNull { it.target }
6867
if (finalInput == null) {
6968
activeInput = null
7069
return
7170
}
7271

7372
activeInput = finalInput
74-
if (canMoveOrBlockPitch() && finalInput.status == ControlInput.Status.ACTIVE) {
75-
var target: Float = finalInput.target
76-
if (!finalInput.priority.isHigherOrSame(minimumPitch?.priority)) {
73+
}
74+
75+
override fun renderTick() {
76+
val input = activeInput ?: return
77+
78+
if (canMoveOrBlockPitch() && input.status == ControlInput.Status.ACTIVE) {
79+
var target: Float = input.target
80+
if (!input.priority.isHigherOrSame(minimumPitch?.priority)) {
7781
target = target.coerceAtLeast(minimumPitch!!.target + 1.0f)
7882
}
79-
if (!finalInput.priority.isHigherOrSame(maximumPitch?.priority)) {
83+
if (!input.priority.isHigherOrSame(maximumPitch?.priority)) {
8084
target = target.coerceAtMost(maximumPitch!!.target - 1.0f)
8185
}
82-
smoothSetPitch(computers.data.player, pitch, target.throwIfNotInRange(-90.0f..90.0f), finalInput.deltaTimeMultiplier.throwIfNotInRange(0.001f..Float.MAX_VALUE))
86+
smoothSetPitch(computers.data.player, computers.data.pitch, target.throwIfNotInRange(-90.0f..90.0f), input.deltaTimeMultiplier.throwIfNotInRange(0.001f..Float.MAX_VALUE))
8387
}
8488
}
8589

@@ -132,7 +136,6 @@ class PitchComputer(computers: ComputerBus) : Computer(computers), FlightControl
132136
}
133137

134138
override fun reset() {
135-
manualOverride = true
136139
minimumPitch = null
137140
maximumPitch = null
138141
activeInput = null

src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/base/RollComputer.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ class RollComputer(computers: ComputerBus) : Computer(computers) {
2525
RollControllerRegistrationCallback.EVENT.invoker().register(controllers::add)
2626
}
2727

28-
override fun tick() {
29-
val rollSource: RollSource = sources.filterWorking().singleOrNull { computers.guardedCall(it, RollSource::isActive) == true } ?: return
30-
28+
override fun renderTick() {
3129
val inputs: List<ControlInput> = controllers.filterWorking().mapNotNull { computers.guardedCall(it, FlightController::getRollInput) }.sortedBy { it.priority.value }
3230
if (inputs.isEmpty()) {
3331
return
3432
}
3533
val finalInput: ControlInput = inputs.getActiveHighestPriority().firstOrNull() ?: return
3634

3735
if (computers.data.automationsAllowed() && finalInput.status == ControlInput.Status.ACTIVE) {
36+
val rollSource: RollSource = sources.filterWorking().singleOrNull { computers.guardedCall(it, RollSource::isActive) == true } ?: return
3837
smoothSetRoll(rollSource, finalInput.target.throwIfNotInRange(-180.0f..180.0f), finalInput.deltaTimeMultiplier.throwIfNotInRange(0.001f..Float.MAX_VALUE))
3938
}
4039
}

src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/modes/BuiltInThrustModes.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ru.octol1ttle.flightassistant.impl.computer.autoflight.modes
22

33
import kotlin.math.abs
44
import kotlin.math.pow
5+
import net.minecraft.SharedConstants
56
import net.minecraft.network.chat.Component
67
import ru.octol1ttle.flightassistant.api.autoflight.ControlInput
78
import ru.octol1ttle.flightassistant.api.computer.ComputerBus
@@ -17,11 +18,8 @@ data class ConstantThrustMode(val target: Float, override val textOverride: Comp
1718
data class SpeedThrustMode(override val targetSpeed: Int, override val textOverride: Component? = null) : AutoFlightComputer.ThrustMode, AutoFlightComputer.FollowsSpeedMode {
1819
override fun getControlInput(computers: ComputerBus): ControlInput {
1920
val currentThrust: Float = computers.thrust.current
20-
if (FATickCounter.ticksPassed == 0) {
21-
return ControlInput(currentThrust, Component.translatable("mode.flightassistant.thrust.speed"))
22-
}
2321
val currentSpeed: Double = computers.data.forwardVelocityPerSecond.length()
24-
val acceleration: Double = computers.data.forwardAcceleration * 20.0
22+
val acceleration: Double = computers.data.forwardAcceleration * SharedConstants.TICKS_PER_SECOND
2523

2624
val speedCorrection: Double = (targetSpeed - currentSpeed) * FATickCounter.timePassed.pow(1.5f)
2725
val accelerationDamping: Double = -acceleration * FATickCounter.timePassed

src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/modes/BuiltInVerticalModes.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import net.minecraft.util.Mth
66
import org.joml.Vector2d
77
import ru.octol1ttle.flightassistant.api.autoflight.ControlInput
88
import ru.octol1ttle.flightassistant.api.computer.ComputerBus
9-
import ru.octol1ttle.flightassistant.api.util.FATickCounter
109
import ru.octol1ttle.flightassistant.api.util.PIDController
1110
import ru.octol1ttle.flightassistant.api.util.extensions.getProgressOnTrack
1211
import ru.octol1ttle.flightassistant.api.util.extensions.vec2dFromInts
@@ -20,24 +19,17 @@ data class PitchVerticalMode(override val targetPitch: Float, override val textO
2019

2120
data class VerticalSpeedVerticalMode(val targetVerticalSpeed: Double, override val textOverride: Component? = null) : AutoFlightComputer.VerticalMode {
2221
override fun getControlInput(computers: ComputerBus): ControlInput {
23-
if (FATickCounter.ticksPassed == 0) {
24-
return ControlInput(lastPitchCommand)
25-
}
26-
2722
val currentVerticalSpeed: Double = computers.data.velocityPerSecond.y
2823
val target: Float = controller.calculate(targetVerticalSpeed, currentVerticalSpeed, computers.data.pitch.toDouble()).toFloat()
29-
lastPitchCommand = target
3024

3125
return ControlInput(target)
3226
}
3327

3428
companion object {
3529
private val controller: PIDController = PIDController(1.8, 0.1, 0.35, 10, -85.0, 85.0)
36-
private var lastPitchCommand: Float = 0.0f
3730
}
3831
}
3932

40-
// TODO: tick-based plsssssssss
4133
data class SelectedAltitudeVerticalMode(override val targetAltitude: Int, override val textOverride: Component? = null) : AutoFlightComputer.VerticalMode, AutoFlightComputer.FollowsAltitudeMode {
4234
override fun getControlInput(computers: ComputerBus): ControlInput {
4335
val diff: Double = targetAltitude - computers.data.altitude

src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/data/HudDisplayDataComputer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class HudDisplayDataComputer(computers: ComputerBus, private val mc: Minecraft)
3333
val roll: Float
3434
get() = degrees(atan2(-RenderMatrices.worldSpaceMatrix.m10(), RenderMatrices.worldSpaceMatrix.m11())).throwIfNotInRange(-180.0f..180.0f)
3535

36-
override fun tick() {
36+
override fun renderTick() {
3737
lerpedPosition = player.getPosition(FATickCounter.partialTick)
3838
lerpedVelocity = player.getDeltaMovementLerped(FATickCounter.partialTick)
3939
lerpedForwardVelocity = computers.data.computeForwardVector(lerpedVelocity)

0 commit comments

Comments
 (0)