-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: flight phase computer (preparation for autopilot rework)
- Loading branch information
1 parent
86a120c
commit 089738f
Showing
11 changed files
with
238 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/main/java/ru/octol1ttle/flightassistant/computers/impl/FlightPhaseComputer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package ru.octol1ttle.flightassistant.computers.impl; | ||
|
||
import net.minecraft.text.Text; | ||
import net.minecraft.util.math.MathHelper; | ||
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer; | ||
import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; | ||
import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner; | ||
import ru.octol1ttle.flightassistant.registries.ComputerRegistry; | ||
|
||
public class FlightPhaseComputer implements ITickableComputer { | ||
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class); | ||
private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class); | ||
private final FlightPlanner plan = ComputerRegistry.resolve(FlightPlanner.class); | ||
public FlightPhase phase = FlightPhase.UNKNOWN; | ||
private float minimumHeight = Float.MAX_VALUE; | ||
private boolean wasAutolandAllowed = true; | ||
|
||
@SuppressWarnings("DataFlowIssue") | ||
@Override | ||
public void tick() { | ||
if (data.player().isOnGround()) { | ||
phase = FlightPhase.ON_GROUND; | ||
} | ||
|
||
if (!isAboutToLand()) { | ||
minimumHeight = Float.MAX_VALUE; | ||
} | ||
|
||
if (!data.isFlying()) { | ||
return; | ||
} | ||
|
||
Integer cruiseAltitude = plan.getCruiseAltitude(); | ||
Integer targetAltitude = autoflight.getTargetAltitude(); | ||
if (cruiseAltitude == null || targetAltitude == null) { | ||
phase = FlightPhase.UNKNOWN; | ||
return; | ||
} | ||
|
||
if (phase == FlightPhase.ON_GROUND) { | ||
phase = FlightPhase.TAKEOFF; | ||
} | ||
|
||
if (phase == FlightPhase.TAKEOFF && data.heightAboveGround() <= 10.0f) { | ||
return; | ||
} | ||
|
||
if (!isNearDestination()) { | ||
if (data.altitude() - targetAltitude <= 5.0f) { | ||
phase = FlightPhase.CLIMB; | ||
} else { | ||
phase = FlightPhase.DESCENT; | ||
} | ||
|
||
if (targetAltitude.equals(cruiseAltitude) && Math.abs(cruiseAltitude - data.altitude()) <= 5.0f) { | ||
phase = FlightPhase.CRUISE; | ||
} | ||
} | ||
|
||
if (phase == FlightPhase.GO_AROUND) { | ||
if (plan.getDistanceToWaypoint() > 150.0f) { | ||
phase = FlightPhase.APPROACH; | ||
} | ||
} else { | ||
if (plan.isOnApproach()) { | ||
phase = FlightPhase.APPROACH; | ||
} | ||
|
||
if (phase == FlightPhase.APPROACH && plan.autolandAllowed) { | ||
phase = FlightPhase.LAND; | ||
} | ||
} | ||
|
||
if (isAboutToLand() && plan.landAltitude != null) { | ||
float heightAboveDestination = data.altitude() - plan.landAltitude; | ||
minimumHeight = MathHelper.clamp(heightAboveDestination, 0.0f, minimumHeight); | ||
|
||
if (heightAboveDestination - minimumHeight >= 5.0f || (wasAutolandAllowed && !plan.autolandAllowed)) { | ||
phase = FlightPhase.GO_AROUND; | ||
} | ||
} | ||
|
||
wasAutolandAllowed = plan.autolandAllowed; | ||
} | ||
|
||
private boolean isAboutToLand() { | ||
return phase == FlightPhase.APPROACH || phase == FlightPhase.LAND; | ||
} | ||
|
||
private boolean isNearDestination() { | ||
return isAboutToLand() || phase == FlightPhase.GO_AROUND; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return "flight_phase"; | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
phase = FlightPhase.UNKNOWN; | ||
minimumHeight = Float.MAX_VALUE; | ||
wasAutolandAllowed = true; | ||
} | ||
|
||
public enum FlightPhase { | ||
ON_GROUND("on_ground"), | ||
TAKEOFF("takeoff"), | ||
CLIMB("climb"), | ||
CRUISE("cruise"), | ||
DESCENT("descent"), | ||
APPROACH("approach"), | ||
LAND("land"), | ||
GO_AROUND("go_around"), | ||
UNKNOWN(""); | ||
|
||
public final Text text; | ||
|
||
FlightPhase(Text text) { | ||
this.text = text; | ||
} | ||
|
||
FlightPhase(String key) { | ||
this(Text.translatable("status.flightassistant.phase." + key)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
.../octol1ttle/flightassistant/computers/impl/autoflight/pitch/AutopilotPitchController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ru.octol1ttle.flightassistant.computers.impl.autoflight.pitch; | ||
|
||
import net.minecraft.util.Pair; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import ru.octol1ttle.flightassistant.computers.api.IPitchController; | ||
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer; | ||
import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; | ||
import ru.octol1ttle.flightassistant.registries.ComputerRegistry; | ||
|
||
public class AutopilotPitchController implements ITickableComputer, IPitchController { | ||
private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class); | ||
|
||
@Override | ||
public void tick() { | ||
// TODO | ||
autoflight.disconnectAutopilot(true); | ||
} | ||
|
||
@Override | ||
public @Nullable Pair<@NotNull Float, @NotNull Float> getTargetPitch() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Priority getPriority() { | ||
return Priority.NORMAL; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return "autopilot_pitch_ctl"; | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
autoflight.disconnectAutopilot(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.