From bf8fd915d34c4e24afb9a6b55a8854bf90940ddd Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Mon, 17 Feb 2025 20:00:35 +0500 Subject: [PATCH] god i'm tired of this Signed-off-by: Octol1ttle --- .../flightassistant/DaBRThrustComputer.kt | 4 +- .../flightassistant/FAKeyBindings.kt | 2 +- ...ystemController.kt => ModuleController.kt} | 4 +- .../api/{SystemView.kt => ModuleView.kt} | 2 +- .../api/autoflight/thrust/ThrustSource.kt | 2 +- .../api/computer/ComputerView.kt | 11 ++- .../alert/autoflight/AutoThrustOffAlert.kt | 2 +- .../alert/autoflight/AutopilotOffAlert.kt | 10 +- .../impl/computer/ComputerHost.kt | 11 ++- ...ightComputer.kt => AutomationsComputer.kt} | 84 +++------------- .../autoflight/AutopilotLogicComputer.kt | 70 +++++++++++++ .../computer/autoflight/ThrustComputer.kt | 2 +- .../impl/computer/safety/AlertComputer.kt | 2 +- .../impl/display/AutomationModesDisplay.kt | 6 +- .../impl/display/FlightDirectorsDisplay.kt | 6 +- .../impl/display/HudDisplayHost.kt | 8 +- .../screen/AutoFlightScreen.kt | 97 +++---------------- .../screen/status/SystemStatusListWidget.kt | 6 +- .../screen/widgets/AbstractParentWidget.kt | 29 ++++++ .../widgets/autoflight/ThrustModeWidget.kt | 43 ++++++++ .../assets/flightassistant/lang/en_us.yml | 12 ++- .../assets/flightassistant/lang/ru_ru.yml | 3 +- 22 files changed, 224 insertions(+), 192 deletions(-) rename src/main/kotlin/ru/octol1ttle/flightassistant/api/{SystemController.kt => ModuleController.kt} (73%) rename src/main/kotlin/ru/octol1ttle/flightassistant/api/{SystemView.kt => ModuleView.kt} (91%) rename src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/{AutoFlightComputer.kt => AutomationsComputer.kt} (66%) create mode 100644 src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/AutopilotLogicComputer.kt create mode 100644 src/main/kotlin/ru/octol1ttle/flightassistant/screen/widgets/AbstractParentWidget.kt create mode 100644 src/main/kotlin/ru/octol1ttle/flightassistant/screen/widgets/autoflight/ThrustModeWidget.kt diff --git a/src/main/kotlin/nl/enjarai/doabarrelroll/compat/flightassistant/DaBRThrustComputer.kt b/src/main/kotlin/nl/enjarai/doabarrelroll/compat/flightassistant/DaBRThrustComputer.kt index d182514..b05c498 100644 --- a/src/main/kotlin/nl/enjarai/doabarrelroll/compat/flightassistant/DaBRThrustComputer.kt +++ b/src/main/kotlin/nl/enjarai/doabarrelroll/compat/flightassistant/DaBRThrustComputer.kt @@ -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() { diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/FAKeyBindings.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/FAKeyBindings.kt index a6bab65..c63aa15 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/FAKeyBindings.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/FAKeyBindings.kt @@ -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 diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/api/SystemController.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/api/ModuleController.kt similarity index 73% rename from src/main/kotlin/ru/octol1ttle/flightassistant/api/SystemController.kt rename to src/main/kotlin/ru/octol1ttle/flightassistant/api/ModuleController.kt index fc7bfd6..6207adc 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/api/SystemController.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/api/ModuleController.kt @@ -2,8 +2,8 @@ package ru.octol1ttle.flightassistant.api import net.minecraft.util.Identifier -interface SystemController : SystemView { - fun register(identifier: Identifier, system: T) +interface ModuleController : ModuleView { + fun register(identifier: Identifier, module: T) fun setEnabled(identifier: Identifier, enabled: Boolean): Boolean fun toggleEnabled(identifier: Identifier): Boolean { return setEnabled(identifier, !this.isEnabled(identifier)) diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/api/SystemView.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/api/ModuleView.kt similarity index 91% rename from src/main/kotlin/ru/octol1ttle/flightassistant/api/SystemView.kt rename to src/main/kotlin/ru/octol1ttle/flightassistant/api/ModuleView.kt index 5100117..1886bd8 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/api/SystemView.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/api/ModuleView.kt @@ -2,7 +2,7 @@ package ru.octol1ttle.flightassistant.api import net.minecraft.util.Identifier -interface SystemView { +interface ModuleView { fun identifiers(): Collection fun get(identifier: Identifier): T fun isEnabled(identifier: Identifier): Boolean diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/api/autoflight/thrust/ThrustSource.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/api/autoflight/thrust/ThrustSource.kt index e18c243..a8e8cce 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/api/autoflight/thrust/ThrustSource.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/api/autoflight/thrust/ThrustSource.kt @@ -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]). diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/api/computer/ComputerView.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/api/computer/ComputerView.kt index 33aab29..43b1d98 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/api/computer/ComputerView.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/api/computer/ComputerView.kt @@ -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 { +interface ComputerView : ModuleView { 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 diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/alert/autoflight/AutoThrustOffAlert.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/alert/autoflight/AutoThrustOffAlert.kt index 52713de..69e7c13 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/alert/autoflight/AutoThrustOffAlert.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/alert/autoflight/AutoThrustOffAlert.kt @@ -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 { diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/alert/autoflight/AutopilotOffAlert.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/alert/autoflight/AutopilotOffAlert.kt index e0ec338..87956b8 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/alert/autoflight/AutopilotOffAlert.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/alert/autoflight/AutopilotOffAlert.kt @@ -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 } diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/ComputerHost.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/ComputerHost.kt index 52a7e63..f2e4284 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/ComputerHost.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/ComputerHost.kt @@ -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 @@ -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, ComputerView { +internal object ComputerHost : ModuleController, ComputerView { private val computers: MutableMap = HashMap() override fun isEnabled(identifier: Identifier): Boolean { @@ -41,14 +41,14 @@ internal object ComputerHost : SystemController, 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() { @@ -61,7 +61,8 @@ internal object ComputerHost : SystemController, 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)) diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/AutoFlightComputer.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/AutomationsComputer.kt similarity index 66% rename from src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/AutoFlightComputer.kt rename to src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/AutomationsComputer.kt index 0ed04f6..9d1edbc 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/AutoFlightComputer.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/AutomationsComputer.kt @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -96,8 +86,7 @@ class AutoFlightComputer(computers: ComputerView) : Computer(computers), FlightC if (autoThrust) { autoThrustAlert = false if (computers.thrust.faulted) { - autoThrust = false - autoThrustAlert = true + setAutoThrust(false, alert = true) } } @@ -105,49 +94,33 @@ class AutoFlightComputer(computers: ComputerView) : Computer(computers), FlightC 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? { @@ -155,14 +128,7 @@ class AutoFlightComputer(computers: ComputerView) : Computer(computers), FlightC 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? { @@ -170,17 +136,7 @@ class AutoFlightComputer(computers: ComputerView) : Computer(computers), FlightC 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? { @@ -188,15 +144,7 @@ class AutoFlightComputer(computers: ComputerView) : Computer(computers), FlightC 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? { @@ -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") } } diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/AutopilotLogicComputer.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/AutopilotLogicComputer.kt new file mode 100644 index 0000000..6890498 --- /dev/null +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/AutopilotLogicComputer.kt @@ -0,0 +1,70 @@ +package ru.octol1ttle.flightassistant.impl.computer.autoflight + +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.computer.Computer +import ru.octol1ttle.flightassistant.api.computer.ComputerView + +class AutopilotLogicComputer(computers: ComputerView) : Computer(computers) { + var thrustMode: ThrustMode = ThrustMode(ThrustMode.Type.SelectedSpeed, 15.0f) + var verticalMode: VerticalMode = VerticalMode(VerticalMode.Type.SelectedPitch, 0.0f) + var lateralMode: LateralMode = LateralMode(LateralMode.Type.SelectedHeading, 360.0f) + + fun computeThrust(): ControlInput { + return when (thrustMode.type) { + ThrustMode.Type.SelectedSpeed -> + ControlInput( + computers.thrust.calculateThrustForSpeed(thrustMode.speed) ?: 0.0f, + ControlInput.Priority.NORMAL, + Text.translatable("mode.flightassistant.thrust.selected_speed"), + identifier = ID + ) + ThrustMode.Type.VerticalTarget -> TODO() + ThrustMode.Type.WaypointThrust -> TODO() + } + } + + fun computePitch(active: Boolean): ControlInput { + TODO() + } + + fun computeHeading(active: Boolean): ControlInput { + TODO() + } + + override fun tick() { + } + + override fun reset() { + } + + class ThrustMode(var type: Type, var speed: Float) { + enum class Type { + SelectedSpeed, + VerticalTarget, + WaypointThrust + } + } + + class VerticalMode(var type: Type, var pitchOrAltitude: Float) { + enum class Type { + SelectedPitch, + SelectedAltitude, + WaypointAltitude + } + } + + class LateralMode(var type: Type, var heading: Float? = null, var x: Double? = null, var z: Double? = null) { + enum class Type { + SelectedHeading, + SelectedCoordinates, + WaypointCoordinates + } + } + + companion object { + val ID: Identifier = FlightAssistant.id("autopilot_logic") + } +} diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/ThrustComputer.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/ThrustComputer.kt index 2c9e8a9..aa36410 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/ThrustComputer.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/autoflight/ThrustComputer.kt @@ -110,7 +110,7 @@ class ThrustComputer(computers: ComputerView) : Computer(computers) { return 55.0f } - fun calculateThrustForSpeed(targetSpeed: Int): Float? { + fun calculateThrustForSpeed(targetSpeed: Float): Float? { val thrustSource: ThrustSource? = sources.filterNonFaulted().filter { it.isAvailable() }.minByOrNull { it.priority.value } if (thrustSource != null) { return thrustSource.calculateThrustForSpeed(targetSpeed) diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/safety/AlertComputer.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/safety/AlertComputer.kt index c7a6506..356db90 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/safety/AlertComputer.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/safety/AlertComputer.kt @@ -63,7 +63,7 @@ class AlertComputer(computers: ComputerView, private val soundManager: SoundMana ) register( AlertCategory(Text.translatable("alerts.flightassistant.autoflight")) - .add(ComputerFaultAlert(computers, AutoFlightComputer.ID, Text.translatable("alerts.flightassistant.autoflight.fault"))) + .add(ComputerFaultAlert(computers, AutomationsComputer.ID, Text.translatable("alerts.flightassistant.autoflight.fault"))) .add(ComputerFaultAlert(computers, PitchComputer.ID, Text.translatable("alerts.flightassistant.autoflight.pitch_fault"))) .add(ComputerFaultAlert(computers, HeadingComputer.ID, Text.translatable("alerts.flightassistant.autoflight.heading_fault"))) .add(ComputerFaultAlert(computers, RollComputer.ID, Text.translatable("alerts.flightassistant.autoflight.roll_fault"))) diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/display/AutomationModesDisplay.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/display/AutomationModesDisplay.kt index 8d65590..4b68046 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/display/AutomationModesDisplay.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/display/AutomationModesDisplay.kt @@ -20,13 +20,13 @@ class AutomationModesDisplay(computers: ComputerView) : Display(computers) { private val headingDisplay: ModeDisplay = ModeDisplay(3) { toPair(computers.heading.activeInput) } private val autoFlightDisplay: ModeDisplay = ModeDisplay(5) { val text: MutableText = Text.empty() - if (computers.autoflight.flightDirectors) { + if (computers.automations.flightDirectors) { text.appendWithSeparation(Text.translatable("short.flightassistant.flight_directors")) } - if (computers.autoflight.autoThrust) { + if (computers.automations.autoThrust) { text.appendWithSeparation(Text.translatable("short.flightassistant.auto_thrust")) } - if (computers.autoflight.autopilot) { + if (computers.automations.autopilot) { text.appendWithSeparation(Text.translatable("short.flightassistant.autopilot")) } diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/display/FlightDirectorsDisplay.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/display/FlightDirectorsDisplay.kt index 99b651c..a9ac930 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/display/FlightDirectorsDisplay.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/display/FlightDirectorsDisplay.kt @@ -10,7 +10,7 @@ import ru.octol1ttle.flightassistant.api.display.HudFrame import ru.octol1ttle.flightassistant.api.util.ScreenSpace import ru.octol1ttle.flightassistant.api.util.extensions.* import ru.octol1ttle.flightassistant.config.FAConfig -import ru.octol1ttle.flightassistant.impl.computer.autoflight.AutoFlightComputer +import ru.octol1ttle.flightassistant.impl.computer.autoflight.AutomationsComputer class FlightDirectorsDisplay(computers: ComputerView) : Display(computers) { override fun allowedByConfig(): Boolean { @@ -18,10 +18,10 @@ class FlightDirectorsDisplay(computers: ComputerView) : Display(computers) { } override fun render(drawContext: DrawContext) { - if (!computers.autoflight.flightDirectors) { + if (!computers.automations.flightDirectors) { return } - if (computers.pitch.activeInput?.identifier != AutoFlightComputer.ID || computers.heading.activeInput?.identifier != AutoFlightComputer.ID) { + if (computers.pitch.activeInput?.identifier != AutomationsComputer.ID || computers.heading.activeInput?.identifier != AutomationsComputer.ID) { renderFaulted(drawContext) return } diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/display/HudDisplayHost.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/display/HudDisplayHost.kt index d76a2d3..91b3746 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/impl/display/HudDisplayHost.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/impl/display/HudDisplayHost.kt @@ -7,7 +7,7 @@ import net.minecraft.client.gui.DrawContext import net.minecraft.text.Text import net.minecraft.util.Identifier import ru.octol1ttle.flightassistant.FlightAssistant -import ru.octol1ttle.flightassistant.api.SystemController +import ru.octol1ttle.flightassistant.api.ModuleController import ru.octol1ttle.flightassistant.api.computer.ComputerView import ru.octol1ttle.flightassistant.api.display.Display import ru.octol1ttle.flightassistant.api.display.HudDisplayRegistrationCallback @@ -21,7 +21,7 @@ import ru.octol1ttle.flightassistant.api.util.extensions.drawMiddleAlignedText import ru.octol1ttle.flightassistant.api.util.extensions.primaryColor import ru.octol1ttle.flightassistant.config.FAConfig -internal object HudDisplayHost: SystemController { +internal object HudDisplayHost: ModuleController { private val displays: MutableMap = HashMap() override fun get(identifier: Identifier): Display { @@ -52,7 +52,7 @@ internal object HudDisplayHost: SystemController { return displays.keys } - override fun register(identifier: Identifier, system: Display) { + override fun register(identifier: Identifier, module: Display) { if (FlightAssistant.initComplete) { throw IllegalStateException("Initialization is already complete, but trying to register a display with identifier: $identifier") } @@ -60,7 +60,7 @@ internal object HudDisplayHost: SystemController { throw IllegalArgumentException("Already registered display with identifier: $identifier") } - displays[identifier] = system + displays[identifier] = module } private fun registerBuiltin(computers: ComputerView) { diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/screen/AutoFlightScreen.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/screen/AutoFlightScreen.kt index 9ad49b4..0ff4009 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/screen/AutoFlightScreen.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/screen/AutoFlightScreen.kt @@ -1,110 +1,45 @@ package ru.octol1ttle.flightassistant.screen -import java.util.function.Consumer -import java.util.function.Predicate import net.minecraft.client.gui.DrawContext -import net.minecraft.client.gui.widget.TextFieldWidget -import net.minecraft.client.gui.widget.TextWidget import net.minecraft.text.Text import net.minecraft.util.Formatting -import ru.octol1ttle.flightassistant.FlightAssistant.mc +import ru.octol1ttle.flightassistant.api.computer.ComputerView import ru.octol1ttle.flightassistant.impl.computer.ComputerHost -import ru.octol1ttle.flightassistant.impl.computer.autoflight.AutoFlightComputer import ru.octol1ttle.flightassistant.screen.widgets.ColoredButtonWidget +import ru.octol1ttle.flightassistant.screen.widgets.autoflight.ThrustModeWidget class AutoFlightScreen : FABaseScreen(Text.translatable("menu.flightassistant.autoflight")) { private lateinit var flightDirectors: ColoredButtonWidget private lateinit var autoThrust: ColoredButtonWidget private lateinit var autopilot: ColoredButtonWidget - private var targetSpeed: TextFieldWidget? = null - private var targetAltitude: TextFieldWidget? = null - private var targetHeading: TextFieldWidget? = null - override fun init() { super.init() - val autoflight: AutoFlightComputer = ComputerHost.autoflight - - flightDirectors = ColoredButtonWidget.builder(Text.translatable("menu.flightassistant.autoflight.flight_directors")) { - autoflight.setFlightDirectors(!autoflight.flightDirectors) - }.position(this.centerX - 100, this.centerY + 50).width(200).build() - autoThrust = ColoredButtonWidget.builder(Text.translatable("menu.flightassistant.autoflight.auto_thrust")) { - autoflight.setAutoThrust(!autoflight.autoThrust, true) - }.position(this.centerX - 100, this.centerY + 80).width(95).build() - autopilot = ColoredButtonWidget.builder(Text.translatable("menu.flightassistant.autoflight.autopilot")) { - autoflight.setAutoPilot(!autoflight.autopilot, true) - }.position(this.centerX + 5, this.centerY + 80).width(95).build() - - targetSpeed = createAutoFlightWidget(this.centerX - 100, this.centerY - 30, 40, 15, - targetSpeed, - Text.translatable("menu.flightassistant.autoflight.speed"), - Predicate { - if (it.isEmpty()) return@Predicate true - val v: Int = it.toIntOrNull() ?: return@Predicate false - return@Predicate v > 0 - }, - { - autoflight.selectedSpeed = it.toIntOrNull() - }, - autoflight.selectedSpeed?.toString() - ) - - targetAltitude = createAutoFlightWidget(this.centerX - 50, this.centerY - 30, 40, 15, - targetAltitude, - Text.translatable("menu.flightassistant.autoflight.altitude"), - Predicate { - if (it.isEmpty()) return@Predicate true - return@Predicate it.toIntOrNull() != null - }, - { - autoflight.selectedAltitude = it.toIntOrNull() - }, - if (autoflight.selectedAltitude == null) null - else autoflight.selectedAltitude.toString() - ) - - targetHeading = createAutoFlightWidget(this.centerX, this.centerY - 30, 40, 15, - targetHeading, - Text.translatable("menu.flightassistant.autoflight.heading"), - Predicate { - if (it.isEmpty()) return@Predicate true - val v: Int = it.toIntOrNull() ?: return@Predicate false - return@Predicate v in 0..360 - }, - { - autoflight.selectedHeading = it.toIntOrNull() - }, - autoflight.selectedHeading?.toString() - ) - - this.addDrawableChild(flightDirectors) - this.addDrawableChild(autoThrust) - this.addDrawableChild(autopilot) - } + val computers: ComputerView = ComputerHost - private fun createAutoFlightWidget(x: Int, y: Int, width: Int, height: Int, copyFrom: TextFieldWidget?, title: Text, textPredicate: Predicate, changedListener: Consumer, initialValue: String?): TextFieldWidget { - val widget = TextFieldWidget(mc.textRenderer, x, y, width, height, copyFrom, title) - widget.setTextPredicate(textPredicate) - widget.setChangedListener(changedListener) - if (initialValue != null) { - widget.text = initialValue - } - this.addDrawableChild(TextWidget(x, y - 20, width, height, title, mc.textRenderer)) - this.addDrawableChild(widget) + flightDirectors = this.addDrawableChild(ColoredButtonWidget.builder(Text.translatable("menu.flightassistant.autoflight.flight_directors")) { + computers.automations.setFlightDirectors(!computers.automations.flightDirectors) + }.position(this.centerX - 100, this.centerY + 50).width(200).build()) + autoThrust = this.addDrawableChild(ColoredButtonWidget.builder(Text.translatable("menu.flightassistant.autoflight.auto_thrust")) { + computers.automations.setAutoThrust(!computers.automations.autoThrust, true) + }.position(this.centerX - 100, this.centerY + 80).width(95).build()) + autopilot = this.addDrawableChild(ColoredButtonWidget.builder(Text.translatable("menu.flightassistant.autoflight.autopilot")) { + computers.automations.setAutoPilot(!computers.automations.autopilot, true) + }.position(this.centerX + 5, this.centerY + 80).width(95).build()) - return widget + this.addDrawableChild(ThrustModeWidget(computers)) } override fun render(context: DrawContext, mouseX: Int, mouseY: Int, delta: Float) { flightDirectors.color = - if (ComputerHost.autoflight.flightDirectors) Formatting.GREEN.colorValue!! + if (ComputerHost.automations.flightDirectors) Formatting.GREEN.colorValue!! else 0xFFFFFF autoThrust.color = - if (ComputerHost.autoflight.autoThrust) Formatting.GREEN.colorValue!! + if (ComputerHost.automations.autoThrust) Formatting.GREEN.colorValue!! else 0xFFFFFF autopilot.color = - if (ComputerHost.autoflight.autopilot) Formatting.GREEN.colorValue!! + if (ComputerHost.automations.autopilot) Formatting.GREEN.colorValue!! else 0xFFFFFF super.render(context, mouseX, mouseY, delta) diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/screen/status/SystemStatusListWidget.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/screen/status/SystemStatusListWidget.kt index 003c90a..bb23b6e 100644 --- a/src/main/kotlin/ru/octol1ttle/flightassistant/screen/status/SystemStatusListWidget.kt +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/screen/status/SystemStatusListWidget.kt @@ -10,11 +10,11 @@ import net.minecraft.client.gui.widget.TextWidget import net.minecraft.text.Text import net.minecraft.util.Identifier import ru.octol1ttle.flightassistant.FlightAssistant.mc -import ru.octol1ttle.flightassistant.api.SystemController +import ru.octol1ttle.flightassistant.api.ModuleController import ru.octol1ttle.flightassistant.api.util.extensions.cautionColor import ru.octol1ttle.flightassistant.api.util.extensions.textRenderer -class SystemStatusListWidget(width: Int, height: Int, top: Int, @Suppress("UNUSED_PARAMETER", "KotlinRedundantDiagnosticSuppress") bottom: Int, left: Int, controller: SystemController<*>, baseKey: String) +class SystemStatusListWidget(width: Int, height: Int, top: Int, @Suppress("UNUSED_PARAMETER", "KotlinRedundantDiagnosticSuppress") bottom: Int, left: Int, controller: ModuleController<*>, baseKey: String) : ElementListWidget(mc, width, height, top, /*? if <1.21 {*/ bottom, //?} 25) { @@ -43,7 +43,7 @@ class SystemStatusListWidget(width: Int, height: Int, top: Int, @Suppress("UNUSE return this.width } - class SystemStatusWidgetEntry(val x: Int, val y: Int, private val listWidth: Int, val identifier: Identifier, displayNameText: Text, private val controller: SystemController<*>) + class SystemStatusWidgetEntry(val x: Int, val y: Int, private val listWidth: Int, val identifier: Identifier, displayNameText: Text, private val controller: ModuleController<*>) : Entry() { private val displayName: TextWidget = TextWidget(x, y, this.listWidth / 2, 9, displayNameText, textRenderer).alignLeft() private val faultText: TextWidget = TextWidget(x, y, this.listWidth / 8, 9, FAULT_TEXT, textRenderer) diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/screen/widgets/AbstractParentWidget.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/screen/widgets/AbstractParentWidget.kt new file mode 100644 index 0000000..4ec8867 --- /dev/null +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/screen/widgets/AbstractParentWidget.kt @@ -0,0 +1,29 @@ +package ru.octol1ttle.flightassistant.screen.widgets + +import kotlin.jvm.optionals.getOrNull +import net.minecraft.client.gui.* +import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder + +abstract class AbstractParentWidget : AbstractParentElement(), Drawable, Selectable { + private var hoveredElement: Element? = null + + override fun render(context: DrawContext?, mouseX: Int, mouseY: Int, delta: Float) { + for (child: Element in children()) { + if (child is Drawable) { + child.render(context, mouseX, mouseY, delta) + } + } + this.hoveredElement = this.hoveredElement(mouseX.toDouble(), mouseY.toDouble()).getOrNull() + } + + override fun appendNarrations(builder: NarrationMessageBuilder?) { + } + + override fun getType(): Selectable.SelectionType { + return if (this.isFocused) { + Selectable.SelectionType.FOCUSED + } else { + if (this.hoveredElement != null) Selectable.SelectionType.HOVERED else Selectable.SelectionType.NONE + } + } +} diff --git a/src/main/kotlin/ru/octol1ttle/flightassistant/screen/widgets/autoflight/ThrustModeWidget.kt b/src/main/kotlin/ru/octol1ttle/flightassistant/screen/widgets/autoflight/ThrustModeWidget.kt new file mode 100644 index 0000000..3a94741 --- /dev/null +++ b/src/main/kotlin/ru/octol1ttle/flightassistant/screen/widgets/autoflight/ThrustModeWidget.kt @@ -0,0 +1,43 @@ +package ru.octol1ttle.flightassistant.screen.widgets.autoflight + +import java.util.ArrayList +import java.util.EnumMap +import net.minecraft.client.gui.Element +import net.minecraft.client.gui.widget.ButtonWidget +import net.minecraft.client.gui.widget.TextFieldWidget +import net.minecraft.text.Text +import ru.octol1ttle.flightassistant.FlightAssistant.mc +import ru.octol1ttle.flightassistant.api.computer.ComputerView +import ru.octol1ttle.flightassistant.impl.computer.autoflight.AutopilotLogicComputer +import ru.octol1ttle.flightassistant.screen.widgets.AbstractParentWidget + +class ThrustModeWidget(val computers: ComputerView) : AbstractParentWidget() { + private val buttons: EnumMap = EnumMap(AutopilotLogicComputer.ThrustMode.Type::class.java) + private val textFields: EnumMap> = EnumMap(AutopilotLogicComputer.ThrustMode.Type::class.java) + + init { + buttons[AutopilotLogicComputer.ThrustMode.Type.SelectedSpeed] = ButtonWidget.builder( + Text.translatable("menu.flightassistant.autoflight.thrust.selected_speed") + ) { computers.autopilot.thrustMode.type = AutopilotLogicComputer.ThrustMode.Type.SelectedSpeed }.build() + textFields[AutopilotLogicComputer.ThrustMode.Type.SelectedSpeed] = TextFieldWidget( + mc.textRenderer, TODO() + ) + + + buttons[AutopilotLogicComputer.ThrustMode.Type.VerticalTarget] = ButtonWidget.builder( + Text.translatable("menu.flightassistant.autoflight.thrust.vertical_target") + ) { computers.autopilot.thrustMode.type = AutopilotLogicComputer.ThrustMode.Type.VerticalTarget }.build() + buttons[AutopilotLogicComputer.ThrustMode.Type.WaypointThrust] = ButtonWidget.builder( + Text.translatable("menu.flightassistant.autoflight.thrust.waypoint_thrust") + ) { computers.autopilot.thrustMode.type = AutopilotLogicComputer.ThrustMode.Type.WaypointThrust }.build() + } + + override fun children(): MutableList { + val list = ArrayList() + list.addAll(buttons.values) + textFields[computers.autopilot.thrustMode.type]?.let { + list.addAll(it) + } + return list + } +} diff --git a/src/main/resources/assets/flightassistant/lang/en_us.yml b/src/main/resources/assets/flightassistant/lang/en_us.yml index a9d473a..51c2600 100644 --- a/src/main/resources/assets/flightassistant/lang/en_us.yml +++ b/src/main/resources/assets/flightassistant/lang/en_us.yml @@ -218,7 +218,8 @@ menu.flightassistant: flightassistant:ground_proximity: Ground proximity computer flightassistant:stall: Stall computer flightassistant:void_proximity: Void proximity computer - flightassistant:autopilot: Automatics computer + flightassistant:automations: Automatics computer + flightassistant:autopilot_logic: Autopilot logic computer flightassistant:firework: Firework computer flightassistant:heading: Heading computer flightassistant:pitch: Pitch computer @@ -229,9 +230,10 @@ menu.flightassistant: flight_directors: Flight directors auto_thrust: Auto thrust autopilot: Autopilot - speed: Speed - altitude: Altitude - heading: Heading + thrust: + selected_speed: SPD + vertical_target: VERT + waypoint_thrust: F/PLN mode.flightassistant: thrust: @@ -240,7 +242,7 @@ mode.flightassistant: locked: THR LK %s locked_toga: TOGA LK toga: THR TOGA - speed: SPD %s + selected_speed: SPD %s pitch: void_protection: VOID PROT void_escape: VOID ESC diff --git a/src/main/resources/assets/flightassistant/lang/ru_ru.yml b/src/main/resources/assets/flightassistant/lang/ru_ru.yml index a807c61..eb10516 100644 --- a/src/main/resources/assets/flightassistant/lang/ru_ru.yml +++ b/src/main/resources/assets/flightassistant/lang/ru_ru.yml @@ -222,7 +222,8 @@ menu.flightassistant: flightassistant:ground_proximity: Компьютер близости земли flightassistant:stall: Компьютер обнаружения сваливания flightassistant:void_proximity: Компьютер близости пустоты - flightassistant:autopilot: Компьютер автоматики + flightassistant:automations: Компьютер автоматики + flightassistant:autopilot_logic: Компьютер логики автопилота flightassistant:firework: Компьютер фейерверков flightassistant:heading: Компьютер управления курсом flightassistant:pitch: Компьютер управления углом наклона