Skip to content

Commit 8452c47

Browse files
committed
improve calculation of ground level
Signed-off-by: Octol1ttle <[email protected]>
1 parent 672a981 commit 8452c47

File tree

10 files changed

+41
-48
lines changed

10 files changed

+41
-48
lines changed

src/main/kotlin/ru/octol1ttle/flightassistant/api/autoflight/ControlInput.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ data class ControlInput(val target: Float, val text: Component? = null, val prio
3838
fun highest(first: Status?, second: Status?): Status {
3939
if (first == null) return second ?: DISABLED
4040
if (second == null) return first
41-
return if (first < second) first else second
41+
return if (first <= second) first else second
4242
}
4343

4444
fun fromBooleans(active: Boolean, available: Boolean = true, enabled: Boolean = true): Status {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ class SafetyOptions {
105105
this.obstacleAutoThrust = false
106106
this.obstacleAutoPitch = false
107107

108+
this.altitudeLossAlert = false
109+
this.unsafeTerrainClearanceAlert = false
110+
this.belowGlideSlopeAlertMode = AlertMode.DISABLED
111+
108112
this.fireworkExplosiveAlert = false
109113
this.fireworkLockExplosive = false
110114
return this
@@ -160,7 +164,7 @@ class SafetyOptions {
160164
}
161165

162166
companion object {
163-
fun min(a: AlertMethod, b: AlertMethod): AlertMethod {
167+
fun max(a: AlertMethod, b: AlertMethod): AlertMethod {
164168
return if (a <= b) a else b
165169
}
166170
}

src/main/kotlin/ru/octol1ttle/flightassistant/impl/alert/flight_plan/DepartureElevationDisagreeAlert.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DepartureElevationDisagreeAlert(computers: ComputerBus) : Alert(computers)
2121
}
2222
val x = computers.plan.departureData.coordinatesX
2323
val z = computers.plan.departureData.coordinatesZ
24-
if (!computers.data.isChunkLoaded(x, z)) {
24+
if (!computers.chunk.isLoaded(x, z)) {
2525
return false
2626
}
2727
val actualElevation: Int = computers.data.level.getHeight(Heightmap.Types.MOTION_BLOCKING, x, z)

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
@@ -26,7 +26,7 @@ class BelowGlideSlopeWarningAlert(computers: ComputerBus) : Alert(computers), Ce
2626
return false
2727
}
2828

29-
val altitudeAboveGround = computers.data.altitude - (computers.data.groundY ?: return false)
29+
val altitudeAboveGround = computers.data.altitude - (computers.gpws.groundY ?: return false)
3030
val glideSlopeDeviation = computers.plan.getCurrentGlideSlopeTarget()!! - computers.data.altitude
3131
return altitudeAboveGround < glideSlopeDeviation
3232
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class PullUpAlert(computers: ComputerBus) : Alert(computers), CenteredAlert {
2828
return if (computers.gpws.groundImpactStatus < computers.gpws.obstacleImpactStatus) {
2929
FAConfig.safety.sinkRateAlertMethod
3030
} else if (computers.gpws.groundImpactStatus == computers.gpws.obstacleImpactStatus) {
31-
SafetyOptions.AlertMethod.min(FAConfig.safety.sinkRateAlertMethod, FAConfig.safety.obstacleAlertMethod)
31+
SafetyOptions.AlertMethod.max(FAConfig.safety.sinkRateAlertMethod, FAConfig.safety.obstacleAlertMethod)
3232
} else {
3333
FAConfig.safety.obstacleAlertMethod
3434
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class FlightPlanComputer(computers: ComputerBus) : Computer(computers) {
145145
}
146146
val minimums =
147147
if (arrivalData.minimumsType == ArrivalData.MinimumsType.ABSOLUTE) arrivalData.minimums.toDouble()
148-
else (computers.data.groundY ?: computers.data.voidY.toDouble()) + arrivalData.minimums
148+
else computers.gpws.groundOrVoidY + arrivalData.minimums
149149
return computers.data.altitude <= minimums
150150
}
151151

@@ -231,6 +231,7 @@ class FlightPlanComputer(computers: ComputerBus) : Computer(computers) {
231231

232232
override fun reset() {
233233
currentPhase = FlightPhase.UNKNOWN
234+
groundSpeeds.clear()
234235
}
235236

236237
companion object {

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

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ import kotlin.math.max
55
import net.minecraft.client.Minecraft
66
import net.minecraft.client.multiplayer.ClientLevel
77
import net.minecraft.client.player.LocalPlayer
8-
import net.minecraft.core.Direction
98
import net.minecraft.resources.ResourceLocation
109
import net.minecraft.tags.DamageTypeTags
1110
import net.minecraft.util.Mth
1211
import net.minecraft.world.damagesource.DamageSource
13-
import net.minecraft.world.level.ClipContext
14-
import net.minecraft.world.phys.BlockHitResult
15-
import net.minecraft.world.phys.HitResult
1612
import net.minecraft.world.phys.Vec3
1713
import ru.octol1ttle.flightassistant.FlightAssistant
1814
import ru.octol1ttle.flightassistant.api.computer.Computer
@@ -41,18 +37,14 @@ class AirDataComputer(computers: ComputerBus, private val mc: Minecraft) : Compu
4137
get() = position.y
4238
val voidY: Int
4339
get() = level.bottomY - 64
44-
var groundY: Double? = null
45-
private set(value) {
46-
field = value?.throwIfNotInRange(level.bottomY.toDouble()..Double.MAX_VALUE)
47-
}
4840

4941
private val fallDistance: Double
5042
get() =
51-
if (groundY == null || groundY!! == Double.MAX_VALUE) Double.MAX_VALUE
43+
if (computers.gpws.groundY == null || computers.gpws.groundY!! == Double.MAX_VALUE) Double.MAX_VALUE
5244
//? if >=1.21.5 {
5345
/*else max(player.fallDistance, altitude - groundY!!)
5446
*///?} else
55-
else max(player.fallDistance.toDouble(), altitude - groundY!!)
47+
else max(player.fallDistance.toDouble(), altitude - computers.gpws.groundY!!)
5648

5749
val fallDistanceSafe: Boolean
5850
get() = player.isInWater || fallDistance <= player.maxFallDistance || isInvulnerableTo(player.damageSources().fall())
@@ -79,7 +71,6 @@ class AirDataComputer(computers: ComputerBus, private val mc: Minecraft) : Compu
7971
get() = degrees(asin(velocity.normalize().y).toFloat())
8072

8173
override fun tick() {
82-
groundY = computeGroundLevel()
8374
forwardVelocity = computeForwardVector(velocity)
8475
forwardVelocityPerSecond = forwardVelocity.perSecond()
8576
forwardAcceleration = forwardVelocity.length() - computeForwardVector(player.getDeltaMovementLerped(0.0f)).length()
@@ -101,36 +92,15 @@ class AirDataComputer(computers: ComputerBus, private val mc: Minecraft) : Compu
10192
|| player.abilities.mayfly && source.`is`(DamageTypeTags.IS_FALL)
10293
}
10394

104-
private fun computeGroundLevel(): Double? {
105-
if (!computers.chunk.isCurrentLoaded) {
106-
return groundY
107-
}
108-
109-
val minY: Double = level.bottomY.toDouble().coerceAtLeast(altitude - 2500)
110-
val result: BlockHitResult = level.clip(
111-
ClipContext(
112-
position,
113-
position.with(Direction.Axis.Y, minY),
114-
ClipContext.Block.COLLIDER,
115-
ClipContext.Fluid.ANY,
116-
player
117-
)
118-
)
119-
if (result.type == HitResult.Type.MISS) {
120-
return if (result.location.y > level.bottomY) Double.MAX_VALUE else null
121-
}
122-
return result.location.y
123-
}
124-
12595
fun computeForwardVector(vector: Vec3): Vec3 {
12696
val normalizedLookAngle: Vec3 = player.lookAngle.normalize()
12797
val normalizedVector: Vec3 = vector.normalize()
12898
return vector.scale(normalizedLookAngle.dot(normalizedVector).coerceAtLeast(0.0))
12999
}
130100

131101
override fun reset() {
132-
groundY = null
133102
forwardVelocity = Vec3.ZERO
103+
forwardVelocityPerSecond = Vec3.ZERO
134104
forwardAcceleration = 0.0
135105
}
136106

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ru.octol1ttle.flightassistant.impl.computer.safety
33
import kotlin.math.max
44
import net.minecraft.network.chat.Component
55
import net.minecraft.resources.ResourceLocation
6+
import net.minecraft.world.entity.Entity
67
import net.minecraft.world.level.ClipContext
78
import net.minecraft.world.level.levelgen.Heightmap
89
import net.minecraft.world.phys.BlockHitResult
@@ -16,6 +17,7 @@ import ru.octol1ttle.flightassistant.api.autoflight.thrust.ThrustControllerRegis
1617
import ru.octol1ttle.flightassistant.api.computer.Computer
1718
import ru.octol1ttle.flightassistant.api.computer.ComputerBus
1819
import ru.octol1ttle.flightassistant.api.computer.ComputerQuery
20+
import ru.octol1ttle.flightassistant.api.util.extensions.bottomY
1921
import ru.octol1ttle.flightassistant.api.util.inverseMin
2022
import ru.octol1ttle.flightassistant.api.util.throwIfNotInRange
2123
import ru.octol1ttle.flightassistant.config.FAConfig
@@ -30,12 +32,21 @@ class GroundProximityComputer(computers: ComputerBus) : Computer(computers), Fli
3032
var obstacleImpactStatus: Status = Status.SAFE
3133
private set
3234

35+
var groundY: Double? = null
36+
val groundOrVoidY: Double
37+
get() = if (groundY == null || groundY == Double.MAX_VALUE) computers.data.voidY.toDouble()
38+
else groundY!!
39+
3340
override fun subscribeToEvents() {
3441
ThrustControllerRegistrationCallback.EVENT.register { it.accept(this) }
3542
PitchControllerRegistrationCallback.EVENT.register { it.accept(this) }
3643
}
3744

3845
override fun tick() {
46+
if (computers.chunk.isCurrentLoaded) {
47+
groundY = computeGroundY()?.throwIfNotInRange(computers.data.level.bottomY.toDouble()..Double.MAX_VALUE)
48+
}
49+
3950
val data: AirDataComputer = computers.data
4051
if (!data.flying || data.player.isInWater) {
4152
groundImpactStatus = Status.SAFE
@@ -84,16 +95,22 @@ class GroundProximityComputer(computers: ComputerBus) : Computer(computers), Fli
8495
}
8596
}
8697

98+
private fun computeGroundY(): Double? {
99+
val playerBoundingBox = computers.data.player.boundingBox
100+
val minY: Double = computers.data.level.bottomY.toDouble().coerceAtLeast(computers.data.altitude - 2500.0)
101+
val diffFromMinY = Vec3(0.0, minY - playerBoundingBox.minY, 0.0)
102+
val collisionResult = Entity.collideBoundingBox(computers.data.player, diffFromMinY, playerBoundingBox, computers.data.level, emptyList())
103+
if (collisionResult == diffFromMinY) {
104+
return null
105+
}
106+
return collisionResult.y + playerBoundingBox.minY
107+
}
108+
87109
private fun computeGroundImpactTime(data: AirDataComputer): Double {
88110
if (data.velocity.y >= 0.0) {
89111
return Double.MAX_VALUE
90112
}
91-
92-
val groundLevel: Double? = data.groundY
93-
val impactLevel: Double =
94-
if (groundLevel == null || groundLevel == Double.MAX_VALUE) data.voidY.toDouble()
95-
else groundLevel
96-
return max(0.0, data.altitude - impactLevel) / -data.velocityPerSecond.y
113+
return max(0.0, data.altitude - groundOrVoidY) / -data.velocityPerSecond.y
97114
}
98115

99116
private fun computeObstacleImpactTime(data: AirDataComputer, lookAheadTime: Double): Double {
@@ -173,6 +190,7 @@ class GroundProximityComputer(computers: ComputerBus) : Computer(computers), Fli
173190
groundImpactStatus = Status.SAFE
174191
obstacleImpactTime = Double.MAX_VALUE
175192
obstacleImpactStatus = Status.SAFE
193+
groundY = null
176194
}
177195

178196
enum class Status {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class VoidProximityComputer(computers: ComputerBus) : Computer(computers), Fligh
2424
}
2525

2626
override fun tick() {
27-
status = if (computers.data.groundY != null) {
27+
status = if (computers.gpws.groundY != null) {
2828
Status.ABOVE_GROUND
2929
} else {
3030
val heightAboveDamageAltitude: Double = computers.data.altitude - computers.data.voidY

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class RadarAltitudeDisplay(computers: ComputerBus) : Display(computers) {
1919
}
2020

2121
override fun render(guiGraphics: GuiGraphics) {
22-
val groundLevel: Double? = computers.data.groundY
23-
if (!computers.data.isCurrentChunkLoaded || groundLevel != null && groundLevel > computers.hudData.lerpedAltitude) {
22+
val groundLevel: Double? = computers.gpws.groundY
23+
if (!computers.chunk.isCurrentLoaded || groundLevel != null && groundLevel > computers.hudData.lerpedAltitude) {
2424
renderFaulted(guiGraphics)
2525
return
2626
}

0 commit comments

Comments
 (0)