Skip to content

Commit b780fa9

Browse files
committed
feat: add support for old (3.0.x) flightplans
1 parent 7cb0fbe commit b780fa9

15 files changed

Lines changed: 248 additions & 87 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package ru.octol1ttle.flightassistant.api.flightplan
2+
3+
sealed class ActiveSegment {
4+
data class Direct(val index: Int) : ActiveSegment()
5+
data class Leg(val origin: Waypoint, val targetIndex: Int) : ActiveSegment()
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ru.octol1ttle.flightassistant.api.flightplan
2+
3+
import kotlinx.serialization.Serializable
4+
import kotlinx.serialization.Transient
5+
import net.minecraft.world.phys.Vec3
6+
import java.util.UUID
7+
8+
@Serializable
9+
data class Waypoint(val posX: Int = 0, val posZ: Int = 0, val altitude: Int = 0, val speed: Int = 0, val phaseMarker: PhaseMarker = PhaseMarker.NONE, @Transient val uuid: UUID = UUID.randomUUID()) {
10+
fun vec3(): Vec3 {
11+
return Vec3(posX + 0.5, altitude.toDouble(), posZ + 0.5)
12+
}
13+
14+
enum class PhaseMarker {
15+
NONE,
16+
TAKEOFF,
17+
APPROACH,
18+
LANDING
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ru.octol1ttle.flightassistant.api.flightplan.legacy
2+
3+
import kotlinx.serialization.Serializable
4+
import net.minecraft.world.phys.Vec3
5+
6+
@Serializable
7+
@Deprecated("Use new flight plans instead. Only kept for backwards compatibility during loading")
8+
data class ArrivalData(val coordinatesX: Int = 0, val coordinatesZ: Int = 0, val elevation: Int = 0, val landingThrust: Float = 0.0f, val minimums: Int = 0, val minimumsType: MinimumsType = MinimumsType.ABSOLUTE, val goAroundAltitude: Int = 0, val approachReEntryWaypointIndex: Int = 0) {
9+
enum class MinimumsType {
10+
ABSOLUTE,
11+
RELATIVE
12+
}
13+
14+
fun isDefault(): Boolean {
15+
return this == DEFAULT
16+
}
17+
18+
fun vec3(): Vec3 {
19+
return Vec3(coordinatesX.toDouble(), elevation.toDouble(), coordinatesZ.toDouble())
20+
}
21+
22+
companion object {
23+
val DEFAULT: ArrivalData = ArrivalData()
24+
}
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ru.octol1ttle.flightassistant.api.flightplan.legacy
2+
3+
import kotlinx.serialization.Serializable
4+
import net.minecraft.world.phys.Vec3
5+
6+
@Serializable
7+
@Deprecated("Use new flight plans instead. Only kept for backwards compatibility during loading")
8+
data class DepartureData(val coordinatesX: Int = 0, val coordinatesZ: Int = 0, val elevation: Int = 0, val takeoffThrust: Float = 0.0f) {
9+
fun isDefault(): Boolean {
10+
return this == DEFAULT
11+
}
12+
13+
fun vec3(): Vec3 {
14+
return Vec3(coordinatesX.toDouble(), elevation.toDouble(), coordinatesZ.toDouble())
15+
}
16+
17+
companion object {
18+
val DEFAULT: DepartureData = DepartureData()
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ru.octol1ttle.flightassistant.api.flightplan.legacy
2+
3+
import kotlinx.serialization.Serializable
4+
import kotlinx.serialization.Transient
5+
import net.minecraft.world.phys.Vec3
6+
import java.util.UUID
7+
8+
@Serializable
9+
@Deprecated("Use new flight plans instead. Only kept for backwards compatibility during loading")
10+
data class EnrouteWaypoint(val coordinatesX: Int = 0, val coordinatesZ: Int = 0, val altitude: Int = 0, val speed: Int = 0, @Transient var active: Active? = null, @Transient val uuid: UUID = UUID.randomUUID()) {
11+
enum class Active {
12+
ORIGIN,
13+
TARGET
14+
}
15+
16+
fun vec3(): Vec3 {
17+
return Vec3(coordinatesX.toDouble(), altitude.toDouble(), coordinatesZ.toDouble())
18+
}
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ru.octol1ttle.flightassistant.api.flightplan.legacy
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
@Deprecated("Use new flight plans instead. Only kept for backwards compatibility during loading")
7+
data class FlightPlan(val departure: DepartureData, val enroute: List<EnrouteWaypoint>, val arrival: ArrivalData) {
8+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import ru.octol1ttle.flightassistant.api.alert.Alert
77
import ru.octol1ttle.flightassistant.api.alert.AlertData
88
import ru.octol1ttle.flightassistant.api.alert.ECAMAlert
99
import ru.octol1ttle.flightassistant.api.computer.ComputerBus
10+
import ru.octol1ttle.flightassistant.api.flightplan.Waypoint
1011
import ru.octol1ttle.flightassistant.api.util.extensions.cautionColor
1112
import ru.octol1ttle.flightassistant.api.util.extensions.drawString
1213
import ru.octol1ttle.flightassistant.api.util.extensions.primaryAdvisoryColor
13-
import ru.octol1ttle.flightassistant.impl.computer.autoflight.FlightPlanComputer
1414
import kotlin.math.abs
1515

1616
class ArrivalElevationDisagreeAlert(computers: ComputerBus) : Alert(computers), ECAMAlert {
1717
override val data: AlertData = AlertData.MASTER_CAUTION
1818

1919
override fun shouldActivate(): Boolean {
20-
val landing = computers.plan.waypoints.singleOrNull { it.phaseMarker == FlightPlanComputer.Waypoint.PhaseMarker.LANDING } ?: return false
20+
val landing = computers.plan.waypoints.singleOrNull { it.phaseMarker == Waypoint.PhaseMarker.LANDING } ?: return false
2121
val x = landing.posX
2222
val z = landing.posZ
2323
if (!computers.chunk.isLoaded(x, z)) {
@@ -40,4 +40,4 @@ class ArrivalElevationDisagreeAlert(computers: ComputerBus) : Alert(computers),
4040

4141
return i
4242
}
43-
}
43+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ru.octol1ttle.flightassistant.api.alert.Alert
77
import ru.octol1ttle.flightassistant.api.alert.AlertData
88
import ru.octol1ttle.flightassistant.api.alert.ECAMAlert
99
import ru.octol1ttle.flightassistant.api.computer.ComputerBus
10+
import ru.octol1ttle.flightassistant.api.flightplan.Waypoint
1011
import ru.octol1ttle.flightassistant.api.util.extensions.cautionColor
1112
import ru.octol1ttle.flightassistant.api.util.extensions.drawString
1213
import ru.octol1ttle.flightassistant.impl.computer.autoflight.FlightPlanComputer
@@ -20,7 +21,7 @@ class DepartureElevationDisagreeAlert(computers: ComputerBus) : Alert(computers)
2021
return false
2122
}
2223

23-
val departure = computers.plan.waypoints.singleOrNull { it.phaseMarker == FlightPlanComputer.Waypoint.PhaseMarker.TAKEOFF } ?: return false
24+
val departure = computers.plan.waypoints.singleOrNull { it.phaseMarker == Waypoint.PhaseMarker.TAKEOFF } ?: return false
2425
val x = departure.posX
2526
val z = departure.posZ
2627
if (!computers.chunk.isLoaded(x, z)) {
@@ -33,4 +34,4 @@ class DepartureElevationDisagreeAlert(computers: ComputerBus) : Alert(computers)
3334
override fun render(guiGraphics: GuiGraphics, firstLineX: Int, otherLinesX: Int, firstLineY: Int): Int {
3435
return guiGraphics.drawString(Component.translatable("alert.flightassistant.flight_plan.departure_elevation_disagree"), firstLineX, firstLineY, cautionColor)
3536
}
36-
}
37+
}

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package ru.octol1ttle.flightassistant.impl.computer.autoflight
22

3-
import kotlinx.serialization.Serializable
4-
import kotlinx.serialization.Transient
53
import net.minecraft.SharedConstants
64
import net.minecraft.network.chat.Component
75
import net.minecraft.resources.ResourceLocation
@@ -12,6 +10,8 @@ import ru.octol1ttle.flightassistant.FlightAssistant
1210
import ru.octol1ttle.flightassistant.api.computer.Computer
1311
import ru.octol1ttle.flightassistant.api.computer.ComputerBus
1412
import ru.octol1ttle.flightassistant.api.computer.ComputerQuery
13+
import ru.octol1ttle.flightassistant.api.flightplan.ActiveSegment
14+
import ru.octol1ttle.flightassistant.api.flightplan.Waypoint
1515
import ru.octol1ttle.flightassistant.api.util.LimitedFIFOQueue
1616
import ru.octol1ttle.flightassistant.api.util.extensions.distance2d
1717
import ru.octol1ttle.flightassistant.api.util.extensions.formatRoot
@@ -242,23 +242,4 @@ class FlightPlanComputer(computers: ComputerBus) : Computer(computers) {
242242
LANDING,
243243
GO_AROUND
244244
}
245-
246-
sealed class ActiveSegment {
247-
data class Direct(val index: Int) : ActiveSegment()
248-
data class Leg(val origin: Waypoint, val targetIndex: Int) : ActiveSegment()
249-
}
250-
251-
@Serializable
252-
data class Waypoint(val posX: Int = 0, val posZ: Int = 0, val altitude: Int = 0, val speed: Int = 0, val phaseMarker: PhaseMarker = PhaseMarker.NONE, @Transient val uuid: UUID = UUID.randomUUID()) {
253-
fun vec3(): Vec3 {
254-
return Vec3(posX + 0.5, altitude.toDouble(), posZ + 0.5)
255-
}
256-
257-
enum class PhaseMarker {
258-
NONE,
259-
TAKEOFF,
260-
APPROACH,
261-
LANDING
262-
}
263-
}
264245
}

‎src/main/kotlin/ru/octol1ttle/flightassistant/screen/FlightAssistantSetupScreen.kt‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ru.octol1ttle.flightassistant.screen
22

33
import dev.isxander.yacl3.platform.YACLPlatform
4+
import kotlinx.serialization.SerializationException
45
import kotlinx.serialization.json.Json
56
import net.minecraft.ChatFormatting
67
import net.minecraft.Util
@@ -12,6 +13,8 @@ import org.lwjgl.PointerBuffer
1213
import org.lwjgl.system.MemoryUtil
1314
import org.lwjgl.util.tinyfd.TinyFileDialogs
1415
import ru.octol1ttle.flightassistant.FlightAssistant
16+
import ru.octol1ttle.flightassistant.api.flightplan.Waypoint
17+
import ru.octol1ttle.flightassistant.api.flightplan.legacy.FlightPlan
1518
import ru.octol1ttle.flightassistant.api.util.extensions.drawMiddleAlignedString
1619
import ru.octol1ttle.flightassistant.config.FAConfigScreen
1720
import ru.octol1ttle.flightassistant.impl.computer.ComputerHost
@@ -30,6 +33,8 @@ class FlightAssistantSetupScreen : FABaseScreen(null, Component.translatable("me
3033
private lateinit var recallAlertButton: Button
3134

3235
private var saveLoadError = false
36+
private var loadedLegacy = false
37+
private var loadedWithProblems = false
3338

3439
private val hasShiftDown
3540
//? if >=1.21.9 {
@@ -92,6 +97,8 @@ class FlightAssistantSetupScreen : FABaseScreen(null, Component.translatable("me
9297
FlightAssistant.logger.info("Saved flight plan to $path")
9398
}
9499
saveLoadError = false
100+
loadedLegacy = false
101+
loadedWithProblems = false
95102
} catch (e: Throwable) {
96103
FlightAssistant.logger.error("Unable to save flight plan", e)
97104
saveLoadError = true
@@ -109,8 +116,60 @@ class FlightAssistantSetupScreen : FABaseScreen(null, Component.translatable("me
109116

110117
val path = TinyFileDialogs.tinyfd_openFileDialog(title, PLANS_PATH.resolve("flight_plan.json").toAbsolutePath().toString(), patterns, description, false)
111118
if (path != null) {
112-
FileReader(path).use { computers.plan.load(Json.decodeFromString(it.readText())) }
119+
loadedLegacy = false
120+
loadedWithProblems = false
121+
122+
try {
123+
FileReader(path).use { computers.plan.load(Json.decodeFromString(it.readText())) }
124+
} catch (e: SerializationException) {
125+
FlightAssistant.logger.debug("Unable to deserialize flight plan, attempting legacy conversion", e)
126+
127+
// try loading legacy flight plan
128+
val legacy: FlightPlan = FileReader(path).use { Json.decodeFromString(it.readText()) }
129+
// convert it to new format
130+
val waypoints = mutableListOf<Waypoint>()
131+
var indexOffset = 0
132+
if (!legacy.departure.isDefault()) {
133+
waypoints.add(
134+
Waypoint(
135+
legacy.departure.coordinatesX,
136+
legacy.departure.coordinatesZ,
137+
legacy.departure.elevation,
138+
// legacy code uses "thrust %", new code uses speed. roughly convert thrust% into (firework) speed
139+
(legacy.departure.takeoffThrust * 30.0f).toInt(),
140+
Waypoint.PhaseMarker.TAKEOFF
141+
)
142+
)
143+
indexOffset = 1
144+
}
145+
for ((coordinatesX, coordinatesZ, altitude, speed) in legacy.enroute) {
146+
waypoints.add(Waypoint(coordinatesX, coordinatesZ, altitude, speed))
147+
}
148+
if (!legacy.arrival.isDefault()) {
149+
waypoints.add(
150+
Waypoint(
151+
legacy.arrival.coordinatesX,
152+
legacy.arrival.coordinatesZ,
153+
legacy.arrival.elevation,
154+
// legacy code uses "thrust %", new code uses speed. roughly convert thrust% into (firework) speed
155+
(legacy.arrival.landingThrust * 30.0f).toInt(),
156+
Waypoint.PhaseMarker.LANDING
157+
)
158+
)
159+
val sizeWithOffset =
160+
waypoints.size - indexOffset - 1 // takeoff & landing waypoints were not in the main waypoints list in the legacy format
161+
if (legacy.arrival.approachReEntryWaypointIndex in 0..<sizeWithOffset) {
162+
val index = legacy.arrival.approachReEntryWaypointIndex + indexOffset
163+
waypoints[index] = waypoints[index].copy(phaseMarker = Waypoint.PhaseMarker.APPROACH)
164+
}
165+
}
166+
computers.plan.load(waypoints)
167+
loadedLegacy = true
168+
}
113169
WaypointsScreen.reload(computers.plan)
170+
if (WaypointsScreen.problems.isNotEmpty()) {
171+
loadedWithProblems = true
172+
}
114173

115174
FlightAssistant.logger.info("Loaded flight plan from $path")
116175
}
@@ -148,7 +207,11 @@ class FlightAssistantSetupScreen : FABaseScreen(null, Component.translatable("me
148207
super.render(guiGraphics, mouseX, mouseY, delta)
149208

150209
if (saveLoadError) {
151-
guiGraphics.drawMiddleAlignedString(Component.translatable("menu.flightassistant.fms.error"), this.centerX, this.centerY + 75, ChatFormatting.RED.color!!, true)
210+
guiGraphics.drawMiddleAlignedString(Component.translatable("menu.flightassistant.fms.error"), this.centerX, this.centerY + 78, ChatFormatting.RED.color!!, true)
211+
} else if (loadedWithProblems) {
212+
guiGraphics.drawMiddleAlignedString(Component.translatable("menu.flightassistant.fms.loaded_with_problems"), this.centerX, this.centerY + 78, ChatFormatting.YELLOW.color!!, true)
213+
} else if (loadedLegacy) {
214+
guiGraphics.drawMiddleAlignedString(Component.translatable("menu.flightassistant.fms.loaded_legacy"), this.centerX, this.centerY + 78, ChatFormatting.YELLOW.color!!, true)
152215
}
153216
}
154217

0 commit comments

Comments
 (0)