Skip to content

Commit

Permalink
god i'm tired of this
Browse files Browse the repository at this point in the history
Signed-off-by: Octol1ttle <[email protected]>
  • Loading branch information
Octol1ttle committed Feb 17, 2025
1 parent 358a984 commit bf8fd91
Show file tree
Hide file tree
Showing 22 changed files with 224 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class DaBRThrustComputer(computers: ComputerView) : Computer(computers), ThrustS
return ModConfig.INSTANCE.enableThrust
}

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

override fun tick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ object FAKeyBindings {
}

while (autopilotDisconnect.wasPressed()) {
computers.autoflight.setAutoPilot(false, alert = false)
computers.automations.setAutoPilot(false, alert = false)
}
computers.pitch.manualOverride = manualPitchOverride.isPressed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package ru.octol1ttle.flightassistant.api

import net.minecraft.util.Identifier

interface SystemController<T> : SystemView<T> {
fun register(identifier: Identifier, system: T)
interface ModuleController<T> : ModuleView<T> {
fun register(identifier: Identifier, module: T)
fun setEnabled(identifier: Identifier, enabled: Boolean): Boolean
fun toggleEnabled(identifier: Identifier): Boolean {
return setEnabled(identifier, !this.isEnabled(identifier))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ru.octol1ttle.flightassistant.api

import net.minecraft.util.Identifier

interface SystemView<T> {
interface ModuleView<T> {
fun identifiers(): Collection<Identifier>
fun get(identifier: Identifier): T
fun isEnabled(identifier: Identifier): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface ThrustSource {
/**
* Calculates the thrust required to achieve the target speed.
*/
fun calculateThrustForSpeed(targetSpeed: Int): Float
fun calculateThrustForSpeed(targetSpeed: Float): Float

/**
* Defines the priority, which determines which thrust source to choose in the case there are multiple sources available ([isAvailable]).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package ru.octol1ttle.flightassistant.api.computer

import ru.octol1ttle.flightassistant.api.SystemView
import ru.octol1ttle.flightassistant.api.ModuleView
import ru.octol1ttle.flightassistant.impl.computer.AirDataComputer
import ru.octol1ttle.flightassistant.impl.computer.autoflight.*
import ru.octol1ttle.flightassistant.impl.computer.safety.*

interface ComputerView : SystemView<Computer> {
interface ComputerView : ModuleView<Computer> {
val data: AirDataComputer
get() = get(AirDataComputer.ID) as AirDataComputer

val autoflight: AutoFlightComputer
get() = get(AutoFlightComputer.ID) as AutoFlightComputer
val automations: AutomationsComputer
get() = get(AutomationsComputer.ID) as AutomationsComputer

val autopilot: AutopilotLogicComputer
get() = get(AutopilotLogicComputer.ID) as AutopilotLogicComputer

val firework: FireworkComputer
get() = get(FireworkComputer.ID) as FireworkComputer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AutoThrustOffAlert(computers: ComputerView) : Alert(computers), ECAMAlert
override val priorityOffset: Int = 20

override fun shouldActivate(): Boolean {
return computers.autoflight.autoThrustAlert
return computers.automations.autoThrustAlert
}

override fun render(drawContext: DrawContext, firstLineX: Int, otherLinesX: Int, firstLineY: Int): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import ru.octol1ttle.flightassistant.api.util.extensions.warningColor

class AutopilotOffAlert(computers: ComputerView) : Alert(computers), ECAMAlert {
override val data: AlertData
get() = if (computers.autoflight.autopilotAlert) AlertData.FORCE_AUTOPILOT_OFF else AlertData.PLAYER_AUTOPILOT_OFF
get() = if (computers.automations.autopilotAlert) AlertData.FORCE_AUTOPILOT_OFF else AlertData.PLAYER_AUTOPILOT_OFF
private var age: Int = 0
private var wasAutopilot: Boolean = false

override fun shouldActivate(): Boolean {
if (computers.autoflight.autopilotAlert || age > 80) {
if (computers.automations.autopilotAlert || age > 80) {
wasAutopilot = false
age = 0
return computers.autoflight.autopilotAlert
return computers.automations.autopilotAlert
}

if (computers.autoflight.autopilot) {
if (computers.automations.autopilot) {
wasAutopilot = true
}
val autopilotOff: Boolean = wasAutopilot && !computers.autoflight.autopilot
val autopilotOff: Boolean = wasAutopilot && !computers.automations.autopilot
if (autopilotOff) {
age += FATickCounter.ticksPassed
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ru.octol1ttle.flightassistant.impl.computer
import net.minecraft.util.Identifier
import ru.octol1ttle.flightassistant.FlightAssistant
import ru.octol1ttle.flightassistant.FlightAssistant.mc
import ru.octol1ttle.flightassistant.api.SystemController
import ru.octol1ttle.flightassistant.api.ModuleController
import ru.octol1ttle.flightassistant.api.computer.Computer
import ru.octol1ttle.flightassistant.api.computer.ComputerRegistrationCallback
import ru.octol1ttle.flightassistant.api.computer.ComputerView
Expand All @@ -12,7 +12,7 @@ import ru.octol1ttle.flightassistant.config.FAConfig
import ru.octol1ttle.flightassistant.impl.computer.autoflight.*
import ru.octol1ttle.flightassistant.impl.computer.safety.*

internal object ComputerHost : SystemController<Computer>, ComputerView {
internal object ComputerHost : ModuleController<Computer>, ComputerView {
private val computers: MutableMap<Identifier, Computer> = HashMap()

override fun isEnabled(identifier: Identifier): Boolean {
Expand Down Expand Up @@ -41,14 +41,14 @@ internal object ComputerHost : SystemController<Computer>, ComputerView {
return computers.keys
}

override fun register(identifier: Identifier, system: Computer) {
override fun register(identifier: Identifier, module: Computer) {
if (FlightAssistant.initComplete) {
throw IllegalStateException("Initialization is already complete, but trying to register a computer with identifier: $identifier")
}
if (computers.containsKey(identifier)) {
throw IllegalArgumentException("Already registered computer with identifier: $identifier")
}
computers[identifier] = system
computers[identifier] = module
}

private fun registerBuiltin() {
Expand All @@ -61,7 +61,8 @@ internal object ComputerHost : SystemController<Computer>, ComputerView {
register(ElytraStatusComputer.ID, ElytraStatusComputer(this))
register(ChunkStatusComputer.ID, ChunkStatusComputer(this))

register(AutoFlightComputer.ID, AutoFlightComputer(this))
register(AutomationsComputer.ID, AutomationsComputer(this))
register(AutopilotLogicComputer.ID, AutopilotLogicComputer(this))
register(FireworkComputer.ID, FireworkComputer(this, mc))
register(PitchComputer.ID, PitchComputer(this))
register(HeadingComputer.ID, HeadingComputer(this))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.octol1ttle.flightassistant.impl.computer.autoflight

import kotlin.math.abs
import net.minecraft.text.Text
import net.minecraft.util.Identifier
import ru.octol1ttle.flightassistant.FlightAssistant
import ru.octol1ttle.flightassistant.api.autoflight.ControlInput
Expand All @@ -16,7 +15,7 @@ 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), FlightController {
class AutomationsComputer(computers: ComputerView) : Computer(computers), FlightController {
var flightDirectors: Boolean = false
private set

Expand All @@ -32,21 +31,14 @@ class AutoFlightComputer(computers: ComputerView) : Computer(computers), FlightC
private var pitchResistance: Float = 0.0f
private var headingResistance: Float = 0.0f

var selectedSpeed: Int? = null
var selectedAltitude: Int? = null
var selectedHeading: Int? = null

override fun subscribeToEvents() {
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) {
autoThrustAlert = true
}
autoThrust = false
if (input?.identifier != AutopilotLogicComputer.ID) {
setAutoThrust(false, alert = true)
}
if (input == null && autoThrustAlert) {
autoThrustAlert = false
Expand All @@ -59,8 +51,7 @@ class AutoFlightComputer(computers: ComputerView) : Computer(computers), FlightC
output.add(ControlInput(0.0f, ControlInput.Priority.NORMAL))
return@ChangeLookDirection
}
autopilot = false
autopilotAlert = true
setAutoPilot(false, alert = true)
}

pitchResistance = 0.0f
Expand All @@ -72,8 +63,7 @@ class AutoFlightComputer(computers: ComputerView) : Computer(computers), FlightC
output.add(ControlInput(0.0f, ControlInput.Priority.NORMAL))
return@ChangeLookDirection
}
autopilot = false
autopilotAlert = true
setAutoPilot(false, alert = true)
}

headingResistance = 0.0f
Expand All @@ -96,107 +86,65 @@ class AutoFlightComputer(computers: ComputerView) : Computer(computers), FlightC
if (autoThrust) {
autoThrustAlert = false
if (computers.thrust.faulted) {
autoThrust = false
autoThrustAlert = true
setAutoThrust(false, alert = true)
}
}

if (autopilot) {
autopilotAlert = false

val pitchInput: ControlInput? = computers.pitch.activeInput
if (computers.pitch.disabledOrFaulted() || pitchInput != null && pitchInput.identifier != ID) {
if (computers.pitch.disabledOrFaulted() || pitchInput != null && pitchInput.identifier != AutopilotLogicComputer.ID) {
setAutoPilot(false, alert = true)
}

val headingInput: ControlInput? = computers.heading.activeInput
if (computers.heading.disabledOrFaulted() || headingInput != null && headingInput.identifier != ID) {
if (computers.heading.disabledOrFaulted() || headingInput != null && headingInput.identifier != AutopilotLogicComputer.ID) {
setAutoPilot(false, alert = true)
}
}
}

fun setFlightDirectors(flightDirectors: Boolean) {
if (flightDirectors) {
setDefaultSelections()
}
this.flightDirectors = flightDirectors
}

fun setAutoThrust(autoThrust: Boolean, alert: Boolean? = null) {
if (autoThrust && !this.autoThrust && this.selectedSpeed == null) {
this.selectedSpeed = (computers.data.forwardVelocity.length() * 20).toInt().coerceAtLeast(1)
}
this.autoThrust = autoThrust
if (alert != null) {
this.autoThrustAlert = !autoThrust && alert
this.autoThrustAlert = this.autoThrust && !autoThrust && alert
}
this.autoThrust = autoThrust
}

fun setAutoPilot(autopilot: Boolean, alert: Boolean? = null) {
if (autopilot) {
setDefaultSelections()
}
this.autopilot = autopilot
if (alert != null) {
this.autopilotAlert = !autopilot && alert
}
}

private fun setDefaultSelections() {
if (!this.flightDirectors && !this.autopilot && this.selectedAltitude == null && this.selectedHeading == null) {
this.selectedAltitude = computers.data.altitude.toInt()
this.selectedHeading = computers.data.heading.toInt()
this.autopilotAlert = this.autopilot && !autopilot && alert
}
this.autopilot = autopilot
}

override fun getThrustInput(): ControlInput? {
if (!autoThrust) {
return null
}

val target: Int = selectedSpeed ?: return null

return ControlInput(
computers.thrust.calculateThrustForSpeed(target) ?: 0.0f,
ControlInput.Priority.NORMAL,
Text.translatable("mode.flightassistant.thrust.speed", target),
identifier = ID
)
return computers.autopilot.computeThrust()
}

override fun getPitchInput(): ControlInput? {
if (!flightDirectors && !autopilot) {
return null
}

val altitude: Int = selectedAltitude ?: return null
val altitudeDiff: Double = altitude - computers.data.altitude
val pitch: Float = (TODO()).toFloat().coerceIn(-35.0f..computers.thrust.getOptimumClimbPitch())

return ControlInput(
pitch,
ControlInput.Priority.NORMAL,
Text.translatable("mode.flightassistant.pitch.altitude", altitude),
active = autopilot,
identifier = ID
)
return computers.autopilot.computePitch(autopilot)
}

override fun getHeadingInput(): ControlInput? {
if (!flightDirectors && !autopilot) {
return null
}

val heading: Int = selectedHeading ?: return null

return ControlInput(
heading.toFloat(),
ControlInput.Priority.NORMAL,
Text.translatable("mode.flightassistant.heading.selected", heading),
active = autopilot,
identifier = ID
)
return computers.autopilot.computeHeading(autopilot)
}

override fun getRollInput(): ControlInput? {
Expand All @@ -222,6 +170,6 @@ class AutoFlightComputer(computers: ComputerView) : Computer(computers), FlightC
}

companion object {
val ID: Identifier = FlightAssistant.id("autopilot")
val ID: Identifier = FlightAssistant.id("automations")
}
}
Loading

0 comments on commit bf8fd91

Please sign in to comment.