Skip to content

Commit 207af60

Browse files
committed
CourseDeviationDisplay
Signed-off-by: Octol1ttle <[email protected]>
1 parent d496f13 commit 207af60

File tree

11 files changed

+134
-12
lines changed

11 files changed

+134
-12
lines changed

src/main/kotlin/ru/octol1ttle/flightassistant/api/util/extensions/VectorExtensions.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ fun Vec3.perSecond(): Vec3 {
88
return this.scale(SharedConstants.TICKS_PER_SECOND.toDouble())
99
}
1010

11+
fun Vec3.toVector2d(): Vector2d {
12+
return Vector2d(this.x, this.z)
13+
}
14+
1115
fun distance2d(x1: Int, z1: Int, x2: Double, z2: Double): Double {
1216
return Vector2d.distance(x1.toDouble(), z1.toDouble(), x2, z2)
1317
}

src/main/kotlin/ru/octol1ttle/flightassistant/config/FAConfigScreen.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ object FAConfigScreen {
255255
binding(current::showFlightDirectors, defaults.showFlightDirectors)
256256
controller(tickBox())
257257
}
258+
rootOptions.register("misc.course_deviation") {
259+
setDisplayName()
260+
binding(current::showCourseDeviation, defaults.showCourseDeviation)
261+
controller(tickBox())
262+
}
258263
}
259264
}
260265

src/main/kotlin/ru/octol1ttle/flightassistant/config/options/DisplayOptions.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class DisplayOptions {
7676
var showAutomationModes: Boolean = true
7777
@SerialEntry
7878
var showFlightDirectors: Boolean = true
79+
@SerialEntry
80+
var showCourseDeviation: Boolean = true
7981

8082
internal fun setMinimal(): DisplayOptions {
8183
this.showAttitude = AttitudeDisplayMode.DISABLED
@@ -85,6 +87,7 @@ class DisplayOptions {
8587
this.showAltitudeScale = false
8688
this.showFlightPathVector = false
8789
this.showVerticalSpeed = false
90+
this.showCourseDeviation = false
8891
return this
8992
}
9093

@@ -106,6 +109,7 @@ class DisplayOptions {
106109
this.showStatusMessages = false
107110
this.showAutomationModes = false
108111
this.showFlightDirectors = false
112+
this.showCourseDeviation = false
109113
return this
110114
}
111115

src/main/kotlin/ru/octol1ttle/flightassistant/impl/alert/gpws/BelowGlideSlopeAlert.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class BelowGlideSlopeAlert(computers: ComputerBus) : Alert(computers), CenteredA
2626
return false
2727
}
2828

29-
val glideSlopeDeviation = computers.plan.getCurrentGlideSlopeTarget()!! - computers.data.altitude
29+
val glideSlopeDeviation = computers.plan.getVerticalDeviation(computers.data.position)!!
3030
return glideSlopeDeviation > 5
3131
}
3232

src/main/kotlin/ru/octol1ttle/flightassistant/impl/alert/gpws/BelowGlideSlopeWarningAlert.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class BelowGlideSlopeWarningAlert(computers: ComputerBus) : Alert(computers), Ce
2727
}
2828

2929
val altitudeAboveGround = computers.data.altitude - computers.gpws.groundOrVoidY
30-
val glideSlopeDeviation = computers.plan.getCurrentGlideSlopeTarget()!! - computers.data.altitude
30+
val glideSlopeDeviation = computers.plan.getVerticalDeviation(computers.data.position)!!
3131
return altitudeAboveGround < glideSlopeDeviation
3232
}
3333

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

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import ru.octol1ttle.flightassistant.api.util.LimitedFIFOQueue
1818
import ru.octol1ttle.flightassistant.api.util.extensions.distance2d
1919
import ru.octol1ttle.flightassistant.api.util.extensions.formatRoot
2020
import ru.octol1ttle.flightassistant.api.util.extensions.getProgressOnTrack
21-
import ru.octol1ttle.flightassistant.api.util.extensions.vec2dFromInts
21+
import ru.octol1ttle.flightassistant.api.util.extensions.toVector2d
2222
import ru.octol1ttle.flightassistant.impl.computer.autoflight.modes.*
2323
import ru.octol1ttle.flightassistant.impl.display.StatusDisplay
2424

@@ -128,14 +128,58 @@ class FlightPlanComputer(computers: ComputerBus) : Computer(computers) {
128128
return enrouteData.maxOfOrNull { it.altitude }
129129
}
130130

131-
fun getCurrentGlideSlopeTarget(): Double? {
132-
val origin = enrouteData.lastOrNull() ?: return null
133-
val originVec = vec2dFromInts(origin.coordinatesX, origin.coordinatesZ)
134-
val targetVec = vec2dFromInts(arrivalData.coordinatesX, arrivalData.coordinatesZ)
131+
fun getLateralDeviation(position: Vec3): Double? {
132+
if (enrouteData.isEmpty()) {
133+
return null
134+
}
135+
val origin: Vec3
136+
val target: Vec3
137+
when (currentPhase) {
138+
FlightPhase.TAKEOFF -> {
139+
origin = departureData.vec3()
140+
target = enrouteData.first().vec3()
141+
}
142+
FlightPhase.LANDING -> {
143+
origin = enrouteData.last().vec3()
144+
target = arrivalData.vec3()
145+
}
146+
else -> {
147+
origin = getEnrouteOrigin()?.vec3() ?: return null
148+
target = getEnrouteTarget()!!.vec3()
149+
}
150+
}
151+
152+
val origin2d = origin.toVector2d()
153+
val track = target.toVector2d().sub(origin2d)
154+
val toEntity = position.toVector2d().sub(origin2d)
155+
val cross = track.x * toEntity.y - track.y * toEntity.x
156+
return cross / track.length()
157+
}
158+
159+
fun getVerticalDeviation(position: Vec3): Double? {
160+
if (enrouteData.isEmpty()) {
161+
return null
162+
}
163+
val origin: Vec3
164+
val target: Vec3
165+
when (currentPhase) {
166+
FlightPhase.TAKEOFF -> {
167+
origin = departureData.vec3()
168+
target = enrouteData.first().vec3()
169+
}
170+
FlightPhase.LANDING -> {
171+
origin = enrouteData.last().vec3()
172+
target = arrivalData.vec3()
173+
}
174+
else -> {
175+
origin = getEnrouteOrigin()?.vec3() ?: return null
176+
target = getEnrouteTarget()!!.vec3()
177+
}
178+
}
135179

136-
val track: Vector2d = targetVec.sub(originVec)
137-
val trackProgress: Double = getProgressOnTrack(track, originVec, Vector2d(computers.data.x, computers.data.z))
138-
return Mth.lerp(trackProgress, origin.altitude.toDouble(), arrivalData.elevation.toDouble())
180+
val track = target.toVector2d().sub(origin.toVector2d())
181+
val trackProgress = getProgressOnTrack(track, origin.toVector2d(), Vector2d(position.x, position.z))
182+
return Mth.lerp(trackProgress, origin.y, target.y) - position.y
139183
}
140184

141185
fun getDistanceToTarget(target: EnrouteWaypoint): Double {
@@ -281,4 +325,4 @@ class FlightPlanComputer(computers: ComputerBus) : Computer(computers) {
281325
val DEFAULT: ArrivalData = ArrivalData()
282326
}
283327
}
284-
}
328+
}

src/main/kotlin/ru/octol1ttle/flightassistant/impl/computer/safety/GroundProximityComputer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class GroundProximityComputer(computers: ComputerBus) : Computer(computers), Fli
227227
val sinkRateInputStatus = getControlInputStatus(groundImpactStatus, FAConfig.safety.sinkRateAutoPitch, false)
228228
val terrainInputStatus = getControlInputStatus(obstacleImpactStatus, FAConfig.safety.obstacleAutoPitch, false)
229229
if (sinkRateInputStatus != null || terrainInputStatus != null) {
230-
val deltaTimeMultiplier: Double = inverseMin(groundImpactTime, obstacleImpactTime) ?: return null
230+
val deltaTimeMultiplier: Double = max(1.0, inverseMin(groundImpactTime, obstacleImpactTime) ?: return null)
231231
return ControlInput(
232232
90.0f,
233233
Component.translatable("mode.flightassistant.vertical.terrain_escape"),
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package ru.octol1ttle.flightassistant.impl.display
2+
3+
import kotlin.math.roundToInt
4+
import net.minecraft.client.gui.GuiGraphics
5+
import net.minecraft.network.chat.Component
6+
import net.minecraft.resources.ResourceLocation
7+
import ru.octol1ttle.flightassistant.FlightAssistant
8+
import ru.octol1ttle.flightassistant.api.computer.ComputerBus
9+
import ru.octol1ttle.flightassistant.api.display.Display
10+
import ru.octol1ttle.flightassistant.api.display.HudFrame
11+
import ru.octol1ttle.flightassistant.api.util.extensions.*
12+
import ru.octol1ttle.flightassistant.config.FAConfig
13+
14+
class CourseDeviationDisplay(computers: ComputerBus) : Display(computers) {
15+
override fun allowedByConfig(): Boolean {
16+
return FAConfig.display.showCourseDeviation
17+
}
18+
19+
override fun render(guiGraphics: GuiGraphics) {
20+
with(guiGraphics) {
21+
renderLateralDeviation()
22+
renderVerticalDeviation()
23+
}
24+
}
25+
26+
private fun GuiGraphics.renderLateralDeviation() {
27+
val deviation = (computers.plan.getLateralDeviation(computers.hudData.lerpedPosition) ?: return).coerceIn(-22.5, 22.5)
28+
val pixelsPerBlock = 2
29+
30+
val step = 10
31+
for (i in (-step * 2)..(step * 2) step step) {
32+
drawMiddleAlignedString("", centerX - i * pixelsPerBlock, HudFrame.bottom - 10, secondaryColor)
33+
}
34+
renderOutline((centerX - deviation * pixelsPerBlock - 4).roundToInt(), HudFrame.bottom - 11, 9, 9, secondaryAdvisoryColor)
35+
}
36+
37+
private fun GuiGraphics.renderVerticalDeviation() {
38+
val deviation = (computers.plan.getVerticalDeviation(computers.hudData.lerpedPosition) ?: return).coerceIn(-10.0, 10.0)
39+
val pixelsPerBlock = 4
40+
41+
val step = 5
42+
for (i in (-step * 2)..(step * 2) step step) {
43+
drawRightAlignedString("", HudFrame.right - 10, centerY - i * pixelsPerBlock - 4, secondaryColor)
44+
}
45+
renderOutline(HudFrame.right - 17, (centerY - deviation * pixelsPerBlock - 5).roundToInt(), 9, 9, secondaryAdvisoryColor)
46+
}
47+
48+
override fun renderFaulted(guiGraphics: GuiGraphics) {
49+
with(guiGraphics) {
50+
drawMiddleAlignedString(Component.translatable("short.flightassistant.lateral_deviation"), centerX, HudFrame.bottom - 10, warningColor)
51+
drawRightAlignedString(Component.translatable("short.flightassistant.vertical_deviation"), HudFrame.right - 10, centerY - 5, warningColor)
52+
}
53+
}
54+
55+
companion object {
56+
val ID: ResourceLocation = FlightAssistant.id("course_deviation")
57+
}
58+
}

src/main/kotlin/ru/octol1ttle/flightassistant/impl/display/HudDisplayHost.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ internal object HudDisplayHost: ModuleController<Display> {
6969
register(AttitudeDisplay.ID, AttitudeDisplay(computers))
7070
register(AutomationModesDisplay.ID, AutomationModesDisplay(computers))
7171
register(CoordinatesDisplay.ID, CoordinatesDisplay(computers))
72+
register(CourseDeviationDisplay.ID, CourseDeviationDisplay(computers))
7273
register(ElytraDurabilityDisplay.ID, ElytraDurabilityDisplay(computers))
7374
register(FlightDirectorsDisplay.ID, FlightDirectorsDisplay(computers))
7475
register(FlightPathDisplay.ID, FlightPathDisplay(computers))

src/main/resources/assets/flightassistant/lang/en_us.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ config.flightassistant:
169169
status_messages: Show status messages
170170
automation_modes: Show automation modes
171171
flight_directors: Show flight directors
172+
course_deviation: Show course deviation
172173
safety:
173174
alert_volume: Alert sound volume
174175
consider_invulnerability: Disable alerts and protections when invulnerable
@@ -390,11 +391,13 @@ short.flightassistant:
390391
ground: GRND
391392
ground_speed: 'G/S%s'
392393
heading: HDG
394+
lateral_deviation: L/DEV
393395
infinite: INFN
394396
radar_altitude: R/ALT
395397
speed: SPD
396398
status: STATUS
397399
time: TIME
400+
vertical_deviation: V/DEV
398401
vertical_speed: 'V/S%s'
399402
void: VOID
400403

0 commit comments

Comments
 (0)