Skip to content

Commit

Permalink
feat: add roll controller & source support
Browse files Browse the repository at this point in the history
Signed-off-by: Octol1ttle <[email protected]>
  • Loading branch information
Octol1ttle committed Feb 16, 2025
1 parent d9a0b21 commit b625c91
Show file tree
Hide file tree
Showing 29 changed files with 250 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package nl.enjarai.doabarrelroll.compat.flightassistant

import dev.architectury.platform.Platform
import net.minecraft.text.Text
import ru.octol1ttle.flightassistant.FlightAssistant
import ru.octol1ttle.flightassistant.api.alert.AlertCategory
import ru.octol1ttle.flightassistant.api.alert.AlertCategoryRegistrationCallback
import ru.octol1ttle.flightassistant.api.autoflight.roll.RollSourceRegistrationCallback
import ru.octol1ttle.flightassistant.api.autoflight.thrust.ThrustSourceRegistrationCallback
import ru.octol1ttle.flightassistant.api.computer.ComputerRegistrationCallback
import ru.octol1ttle.flightassistant.impl.alert.fault.computer.ComputerFaultAlert

object DaBRCompatFA {
//? if do-a-barrel-roll {
private lateinit var rollComputer: DaBRRollComputer
private lateinit var thrustComputer: DaBRThrustComputer

fun init() {
Expand All @@ -20,15 +18,12 @@ object DaBRCompatFA {
FlightAssistant.logger.info("Initializing support for Do a Barrel Roll")

ComputerRegistrationCallback.EVENT.register(ComputerRegistrationCallback { computers, registerFunction ->
rollComputer = DaBRRollComputer(computers)
registerFunction.accept(DaBRRollComputer.ID, rollComputer)
thrustComputer = DaBRThrustComputer(computers)
registerFunction.accept(DaBRThrustComputer.ID, thrustComputer)
})
AlertCategoryRegistrationCallback.EVENT.register(AlertCategoryRegistrationCallback { computers, registerFunction ->
registerFunction.accept(
AlertCategory(Text.translatable("alerts.do_a_barrel_roll.thrust"))
.add(ComputerFaultAlert(computers, DaBRThrustComputer.ID, Text.translatable("alerts.do_a_barrel_roll.thrust.fault")))
)
})
RollSourceRegistrationCallback.EVENT.register { it.accept(rollComputer) }
ThrustSourceRegistrationCallback.EVENT.register { it.accept(thrustComputer) }
}
//?} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package nl.enjarai.doabarrelroll.compat.flightassistant

//? if do-a-barrel-roll {
import net.minecraft.util.Identifier
import nl.enjarai.doabarrelroll.DoABarrelRoll
import nl.enjarai.doabarrelroll.api.RollEntity
import ru.octol1ttle.flightassistant.api.autoflight.roll.RollSource
import ru.octol1ttle.flightassistant.api.computer.Computer
import ru.octol1ttle.flightassistant.api.computer.ComputerView
import ru.octol1ttle.flightassistant.api.util.FATickCounter

class DaBRRollComputer(computers: ComputerView) : Computer(computers), RollSource {
override fun isActive(): Boolean {
return (computers.data.player as RollEntity).`doABarrelRoll$isRolling`()
}

override fun getRoll(): Float {
return (computers.data.player as RollEntity).`doABarrelRoll$getRoll`(FATickCounter.tickProgress)
}

override fun addRoll(diff: Float) {
return (computers.data.player as RollEntity).`doABarrelRoll$setRoll`(getRoll() + diff)
}

override fun tick() {
}

override fun reset() {
}

companion object {
val ID: Identifier = DoABarrelRoll.id("roll")
}
}

//?}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DaBRThrustComputer(computers: ComputerView) : Computer(computers), ThrustS
}

override fun calculateThrustForSpeed(targetSpeed: Int): Float {
return targetSpeed / ModConfig.INSTANCE.maxThrust.toFloat()
return targetSpeed / (ModConfig.INSTANCE.maxThrust * 20.0).toFloat()
}

override fun tick() {
Expand All @@ -41,9 +41,6 @@ class DaBRThrustComputer(computers: ComputerView) : Computer(computers), ThrustS
override fun reset() {
}

override fun tickThrust(currentThrust: Float) {
}

companion object {
val ID: Identifier = DoABarrelRoll.id("thrust")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ data class ControlInput(val target: Float, val priority: Priority, val text: Tex
HIGHEST(0),
HIGH(1),
NORMAL(2),
LOW(3);
LOW(3),
LOWEST(4);

fun isHigherOrSame(other: Priority?): Boolean {
if (other == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.octol1ttle.flightassistant.api.autoflight

/**
* Represents a flight controller that can provide thrust, pitch, heading or roll inputs. Implementing all methods is optional.
* @see [ControlInput]
*/
interface FlightController {
fun getThrustInput(): ControlInput? {
return null
}
fun getPitchInput(): ControlInput? {
return null
}
fun getHeadingInput(): ControlInput? {
return null
}
fun getRollInput(): ControlInput? {
return null
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package ru.octol1ttle.flightassistant.api.autoflight.heading
import dev.architectury.event.Event
import dev.architectury.event.EventFactory
import java.util.function.Consumer
import ru.octol1ttle.flightassistant.api.autoflight.FlightController

fun interface HeadingControllerRegistrationCallback {
/**
* Called during [ru.octol1ttle.flightassistant.api.computer.Computer.invokeEvents].
* Register your custom heading controllers in this event using the provided function
*/
fun register(registerFunction: Consumer<HeadingController>)
fun register(registerFunction: Consumer<FlightController>)

companion object {
@JvmField
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package ru.octol1ttle.flightassistant.api.autoflight.pitch
import dev.architectury.event.Event
import dev.architectury.event.EventFactory
import java.util.function.Consumer
import ru.octol1ttle.flightassistant.api.autoflight.FlightController

fun interface PitchControllerRegistrationCallback {
/**
* Called during [ru.octol1ttle.flightassistant.api.computer.Computer.invokeEvents].
* Register your custom pitch controllers in this event using the provided function
*/
fun register(registerFunction: Consumer<PitchController>)
fun register(registerFunction: Consumer<FlightController>)

companion object {
@JvmField
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.octol1ttle.flightassistant.api.autoflight.roll

import dev.architectury.event.Event
import dev.architectury.event.EventFactory
import java.util.function.Consumer
import ru.octol1ttle.flightassistant.api.autoflight.FlightController

fun interface RollControllerRegistrationCallback {
/**
* Called during [ru.octol1ttle.flightassistant.api.computer.Computer.invokeEvents].
* Register your custom roll controllers in this event using the provided function
*/
fun register(registerFunction: Consumer<FlightController>)

companion object {
@JvmField
val EVENT: Event<RollControllerRegistrationCallback> = EventFactory.createLoop()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.octol1ttle.flightassistant.api.autoflight.roll

/**
* Defines a source of roll to be used by the [ru.octol1ttle.flightassistant.impl.computer.autoflight.RollComputer]
*/
interface RollSource {
/**
* @return whether this roll source is currently active. In the event there are multiple active roll sources, none of them are used.
*/
fun isActive(): Boolean

fun getRoll(): Float
fun addRoll(diff: Float)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.octol1ttle.flightassistant.api.autoflight.roll

import dev.architectury.event.Event
import dev.architectury.event.EventFactory
import java.util.function.Consumer

fun interface RollSourceRegistrationCallback {
/**
* Called during [ru.octol1ttle.flightassistant.api.computer.Computer.invokeEvents].
* Register your custom roll sources in this event using the provided function
*/
fun register(registerFunction: Consumer<RollSource>)

companion object {
@JvmField
val EVENT: Event<RollSourceRegistrationCallback> = EventFactory.createLoop()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package ru.octol1ttle.flightassistant.api.autoflight.thrust
import dev.architectury.event.Event
import dev.architectury.event.EventFactory
import java.util.function.Consumer
import ru.octol1ttle.flightassistant.api.autoflight.FlightController

fun interface ThrustControllerRegistrationCallback {
/**
* Called during [ru.octol1ttle.flightassistant.api.computer.Computer.invokeEvents].
* Register your custom thrust controllers in this event using the provided function
*/
fun register(registerFunction: Consumer<ThrustController>)
fun register(registerFunction: Consumer<FlightController>)

companion object {
@JvmField
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ru.octol1ttle.flightassistant.api.autoflight.thrust

/**
* Defines a source of thrust to be used by the [ru.octol1ttle.flightassistant.impl.computer.autoflight.ThrustComputer]
*/
interface ThrustSource {
/**
* Defines the priority of this thrust source.
Expand All @@ -19,7 +22,7 @@ interface ThrustSource {
val optimumClimbPitch: Float

/**
* Defines whether this thrust source currently can be used. For example, there are fireworks in the player's hand that can be used.
* @return whether this thrust source can be used right now. For example, there are fireworks in the player's hand that can be used.
*/
fun isAvailable(): Boolean

Expand All @@ -28,7 +31,7 @@ interface ThrustSource {
*
* @param currentThrust The current requested thrust. Ranges are `[0.0, 1.0]` or `[-1.0, 1.0]` depending on whether this source [supportsReverse]
*/
fun tickThrust(currentThrust: Float)
fun tickThrust(currentThrust: Float) {}

/**
* Calculates the thrust required to achieve the target speed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ interface ComputerView : SystemView<Computer> {
val pitch: PitchComputer
get() = get(PitchComputer.ID) as PitchComputer

val roll: RollComputer
get() = get(RollComputer.ID) as RollComputer

val thrust: ThrustComputer
get() = get(ThrustComputer.ID) as ThrustComputer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,16 @@ fun Double.requireIn(range: ClosedRange<Double>): Double {

return this
}

fun findShortestPath(from: Float, to: Float, valueRange: Float): Float {
var diff: Float = to - from

if (diff < -valueRange * 0.5) {
diff += valueRange
}
if (diff > valueRange * 0.5) {
diff -= valueRange
}

return diff
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ internal object ComputerHost : SystemController<Computer>, ComputerView {
if (computers.containsKey(identifier)) {
throw IllegalArgumentException("Already registered computer with identifier: $identifier")
}

computers[identifier] = system
}

Expand All @@ -66,6 +65,7 @@ internal object ComputerHost : SystemController<Computer>, ComputerView {
register(FireworkComputer.ID, FireworkComputer(this, mc))
register(PitchComputer.ID, PitchComputer(this))
register(HeadingComputer.ID, HeadingComputer(this))
register(RollComputer.ID, RollComputer(this))
register(ThrustComputer.ID, ThrustComputer(this))

register(AlertComputer.ID, AlertComputer(this, mc.soundManager))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import net.minecraft.text.Text
import net.minecraft.util.Identifier
import ru.octol1ttle.flightassistant.FlightAssistant
import ru.octol1ttle.flightassistant.api.autoflight.ControlInput
import ru.octol1ttle.flightassistant.api.autoflight.heading.HeadingController
import ru.octol1ttle.flightassistant.api.autoflight.FlightController
import ru.octol1ttle.flightassistant.api.autoflight.heading.HeadingControllerRegistrationCallback
import ru.octol1ttle.flightassistant.api.autoflight.pitch.PitchController
import ru.octol1ttle.flightassistant.api.autoflight.pitch.PitchControllerRegistrationCallback
import ru.octol1ttle.flightassistant.api.autoflight.roll.RollControllerRegistrationCallback
import ru.octol1ttle.flightassistant.api.autoflight.thrust.ThrustChangeCallback
import ru.octol1ttle.flightassistant.api.autoflight.thrust.ThrustController
import ru.octol1ttle.flightassistant.api.autoflight.thrust.ThrustControllerRegistrationCallback
import ru.octol1ttle.flightassistant.api.computer.Computer
import ru.octol1ttle.flightassistant.api.computer.ComputerView
import ru.octol1ttle.flightassistant.api.util.FATickCounter
import ru.octol1ttle.flightassistant.api.util.event.ChangeLookDirectionEvents

class AutoFlightComputer(computers: ComputerView) : Computer(computers), ThrustController, PitchController, HeadingController {
class AutoFlightComputer(computers: ComputerView) : Computer(computers), FlightController {
var flightDirectors: Boolean = false
private set

Expand All @@ -41,6 +40,7 @@ class AutoFlightComputer(computers: ComputerView) : Computer(computers), ThrustC
ThrustControllerRegistrationCallback.EVENT.register { it.accept(this) }
PitchControllerRegistrationCallback.EVENT.register { it.accept(this) }
HeadingControllerRegistrationCallback.EVENT.register { it.accept(this) }
RollControllerRegistrationCallback.EVENT.register { it.accept(this) }
ThrustChangeCallback.EVENT.register(ThrustChangeCallback { _, _, input ->
if (input?.identifier != ID) {
if (autoThrust) {
Expand Down Expand Up @@ -199,6 +199,14 @@ class AutoFlightComputer(computers: ComputerView) : Computer(computers), ThrustC
)
}

override fun getRollInput(): ControlInput? {
if (!autopilot) {
return null
}

return ControlInput(0.0f, ControlInput.Priority.NORMAL)
}

override fun reset() {
flightDirectors = false
if (autoThrust) {
Expand Down
Loading

0 comments on commit b625c91

Please sign in to comment.