-
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: firework-efficient autopilot pitch controller
- Loading branch information
1 parent
ec860e0
commit 3b7c9ed
Showing
9 changed files
with
129 additions
and
76 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
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
118 changes: 118 additions & 0 deletions
118
...ru/octol1ttle/flightassistant/computers/impl/autoflight/pitch/AutopilotPitchComputer.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,118 @@ | ||
package ru.octol1ttle.flightassistant.computers.impl.autoflight.pitch; | ||
|
||
import net.minecraft.util.Pair; | ||
import net.minecraft.util.math.MathHelper; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.joml.Vector2d; | ||
import ru.octol1ttle.flightassistant.FAMathHelper; | ||
import ru.octol1ttle.flightassistant.computers.api.IPitchController; | ||
import ru.octol1ttle.flightassistant.computers.api.ITickableComputer; | ||
import ru.octol1ttle.flightassistant.computers.impl.AirDataComputer; | ||
import ru.octol1ttle.flightassistant.computers.impl.FlightPhaseComputer; | ||
import ru.octol1ttle.flightassistant.computers.impl.autoflight.AutoFlightComputer; | ||
import ru.octol1ttle.flightassistant.computers.impl.autoflight.PitchController; | ||
import ru.octol1ttle.flightassistant.computers.impl.navigation.FlightPlanner; | ||
import ru.octol1ttle.flightassistant.registries.ComputerRegistry; | ||
|
||
public class AutopilotPitchComputer implements ITickableComputer, IPitchController { | ||
private final AirDataComputer data = ComputerRegistry.resolve(AirDataComputer.class); | ||
private final AutoFlightComputer autoflight = ComputerRegistry.resolve(AutoFlightComputer.class); | ||
private final FlightPhaseComputer phase = ComputerRegistry.resolve(FlightPhaseComputer.class); | ||
private final FlightPlanner plan = ComputerRegistry.resolve(FlightPlanner.class); | ||
public Float targetPitch; | ||
|
||
@Override | ||
public void tick() { | ||
targetPitch = computeTargetPitch(); | ||
} | ||
|
||
private Float computeTargetPitch() { | ||
if (phase.phase == FlightPhaseComputer.FlightPhase.TAKEOFF | ||
|| phase.phase == FlightPhaseComputer.FlightPhase.GO_AROUND && data.heightAboveGround() < 15.0f) { | ||
return PitchController.CLIMB_PITCH; | ||
} | ||
|
||
Integer targetAltitude = autoflight.getTargetAltitude(); | ||
if (targetAltitude == null) { | ||
return null; | ||
} | ||
Vector2d planPos = plan.getTargetPosition(); | ||
|
||
float diff = targetAltitude - data.altitude(); | ||
boolean landing = phase.phase == FlightPhaseComputer.FlightPhase.LAND; | ||
if (!landing && diff > -10.0f && diff < 5.0f) { | ||
return (PitchController.GLIDE_PITCH + PitchController.ALTITUDE_PRESERVE_PITCH) * 0.5f; | ||
} | ||
|
||
if (data.altitude() < targetAltitude) { | ||
return computeClimbPitch(diff, planPos); | ||
} | ||
|
||
if (planPos == null || autoflight.selectedAltitude != null) { | ||
if (diff > -15.0f) { | ||
return PitchController.GLIDE_PITCH; | ||
} | ||
|
||
return (PitchController.GLIDE_PITCH + PitchController.DESCEND_PITCH) * 0.5f; | ||
} | ||
|
||
float degrees = FAMathHelper.toDegrees( | ||
MathHelper.atan2( | ||
diff, | ||
Vector2d.distance(data.position().x, data.position().z, planPos.x, planPos.y) | ||
) | ||
); | ||
if (!landing && diff > -15.0f) { | ||
return Math.max(PitchController.GLIDE_PITCH, degrees); | ||
} | ||
|
||
return degrees; | ||
} | ||
|
||
private float computeClimbPitch(float diff, Vector2d target) { | ||
if (target == null || autoflight.selectedAltitude != null) { | ||
if (diff <= 15.0f) { | ||
return PitchController.ALTITUDE_PRESERVE_PITCH; | ||
} | ||
|
||
return (PitchController.ALTITUDE_PRESERVE_PITCH + PitchController.CLIMB_PITCH) * 0.5f; | ||
} | ||
|
||
float degrees = Math.max(5.0f, FAMathHelper.toDegrees( | ||
MathHelper.atan2( | ||
diff, | ||
Vector2d.distance(data.position().x, data.position().z, target.x, target.y) | ||
) | ||
)); | ||
if (diff <= 15.0f) { | ||
return Math.min(PitchController.ALTITUDE_PRESERVE_PITCH, degrees); | ||
} | ||
|
||
return degrees; | ||
} | ||
|
||
@Override | ||
public @Nullable Pair<@NotNull Float, @NotNull Float> getTargetPitch() { | ||
if (!autoflight.autoPilotEnabled || targetPitch == null) { | ||
return null; | ||
} | ||
|
||
return new Pair<>(targetPitch, 1.0f); | ||
} | ||
|
||
@Override | ||
public Priority getPriority() { | ||
return Priority.NORMAL; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return "autopilot_pitch_ctl"; | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
autoflight.disconnectAutopilot(true); | ||
} | ||
} |
39 changes: 0 additions & 39 deletions
39
.../octol1ttle/flightassistant/computers/impl/autoflight/pitch/AutopilotPitchController.java
This file was deleted.
Oops, something went wrong.
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