Skip to content

Commit

Permalink
Autoland bugfixes
Browse files Browse the repository at this point in the history
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
  • Loading branch information
Octol1ttle committed Feb 27, 2024
1 parent dda86b5 commit 22ae748
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public MinimumsAlert(AirDataComputer data, FlightPlanner plan) {
@Override
public boolean isTriggered() {
Integer minimums = plan.getMinimums(data.groundLevel);
return minimums != null && data.altitude <= minimums && plan.landingInProgress;
return data.isFlying && plan.landingInProgress && minimums != null && data.altitude <= minimums;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public ComputerHost(@NotNull MinecraftClient mc, HudRenderer renderer) {
this.firework = new FireworkController(mc, data, time);
this.stall = new StallComputer(firework, data);
this.voidLevel = new VoidLevelComputer(data, firework, stall);
this.gpws = new GPWSComputer(data);
this.plan = new FlightPlanner(data);
this.gpws = new GPWSComputer(data, plan);
this.elytra = new ElytraStateController(data);

this.yaw = new YawController(time, data);
this.pitch = new PitchController(data, stall, time, voidLevel, gpws);

this.plan = new FlightPlanner(data);
this.autoflight = new AutoFlightComputer(data, gpws, plan, firework, pitch, yaw);

this.alert = new AlertController(this, mc.getSoundManager(), renderer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public AutoFlightComputer(AirDataComputer data, GPWSComputer gpws, FlightPlanner

@Override
public void tick() {
if (autoFireworkEnabled && gpws.getGPWSLampColor() == FAConfig.hud().frameColor && !plan.shouldFlare()) {
if (autoFireworkEnabled && gpws.getGPWSLampColor() == FAConfig.hud().frameColor) {
Integer targetSpeed = getTargetSpeed();
Integer targetAltitude = getTargetAltitude();
if (targetSpeed != null) {
Expand All @@ -67,9 +67,6 @@ public void tick() {
}

public @Nullable Float getTargetPitch() {
if (plan.shouldFlare()) {
return 10.0f;
}
if (getTargetAltitude() == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public FlightPlanner(AirDataComputer data) {

@Override
public void tick() {
if (!this.contains(targetWaypoint)) {
targetWaypoint = null;
landAltitude = null;
if (targetWaypoint != null && !this.contains(targetWaypoint)) {
nextWaypoint(MathHelper.floor(data.altitude));
}

if (targetWaypoint == null) {
Expand All @@ -37,62 +38,62 @@ public void tick() {

Vector2d target = new Vector2d(targetWaypoint.targetPosition());

landAltitude = null;
if (targetWaypoint instanceof LandingWaypoint) {
landingInProgress = tickLanding(target);
if (landingInProgress) {
targetWaypoint.setTargetAltitude(landAltitude);
setLandTargetAltitude(landAltitude);
}

return;
}

float altitude = targetWaypoint.targetAltitude() != null ? targetWaypoint.targetAltitude() : data.altitude;
if (target.sub(data.position.x, data.position.z).length() <= 20.0f && Math.abs(altitude - data.altitude) <= 10.0f) {
nextWaypoint(altitude);
nextWaypoint(MathHelper.floor(altitude));
}
}

private void nextWaypoint(Float altitude) {
private void nextWaypoint(Integer altitude) {
int nextIndex = this.indexOf(targetWaypoint) + 1;
if (waypointExistsAt(nextIndex)) {
targetWaypoint = this.get(nextIndex);
if (targetWaypoint instanceof LandingWaypoint && altitude != null) {
targetWaypoint.setTargetAltitude(MathHelper.floor(altitude));
}
setLandTargetAltitude(altitude);
} else {
targetWaypoint = null;
}
}

private void setLandTargetAltitude(Integer altitude) {
if (targetWaypoint instanceof LandingWaypoint land && altitude != null) {
land.setTargetAltitude(altitude);
}
}

private boolean tickLanding(Vector2d target) {
double distance = Vector2d.distance(target.x, target.y, data.position.x, data.position.z);
if (distance <= 10.0 && data.heightAboveGround <= 3.0f) {
nextWaypoint(null);
return false;
}

BlockPos landPos = data.findGround(new BlockPos.Mutable(target.x, 320, target.y));
if (landPos == null) {
return false;
}
landAltitude = landPos.getY();
Double distance = getDistanceToNextWaypoint();
assert distance != null;
if (distance < 5.0) {
nextWaypoint(null);
}

float landAngle = FAMathHelper.toDegrees(MathHelper.atan2(landAltitude - data.altitude, distance));
if (landAngle < PitchController.DESCEND_PITCH + 10) {
if (landAngle > 5.0f || landAngle < PitchController.DESCEND_PITCH + 15) {
return false;
}
BlockHitResult result = data.world.raycast(new RaycastContext(data.position, new Vec3d(target.x, landAltitude, target.y), RaycastContext.ShapeType.COLLIDER, RaycastContext.FluidHandling.ANY, data.player));

BlockHitResult result = data.world.raycast(new RaycastContext(data.position, new Vec3d(target.x, landAltitude + 1.0, target.y), RaycastContext.ShapeType.COLLIDER, RaycastContext.FluidHandling.ANY, data.player));

return result.getType() == HitResult.Type.MISS;
return result.getType() == HitResult.Type.MISS || Math.abs(result.getPos().y - landAltitude) <= 5.0;
}

public boolean isOnApproach() {
return targetWaypoint instanceof LandingWaypoint;
}

public boolean shouldFlare() {
return landAltitude != null && data.altitude - landAltitude <= 5.0f;
}

public @Nullable Text formatMinimums() {
if (targetWaypoint instanceof LandingWaypoint land) {
return land.formatMinimums();
Expand Down Expand Up @@ -153,9 +154,7 @@ public boolean shouldFlare() {

public void execute(int waypointIndex) {
targetWaypoint = this.get(waypointIndex);
if (targetWaypoint instanceof LandingWaypoint) {
targetWaypoint.setTargetAltitude(MathHelper.floor(data.altitude));
}
setLandTargetAltitude(MathHelper.floor(data.altitude));
}

public boolean waypointExistsAt(int index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public Text formatMinimums() {
};
}

@Override
public Waypoint setTargetAltitude(Integer targetAltitude) {
public void setTargetAltitude(Integer targetAltitude) {
this.targetAltitude = targetAltitude;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,4 @@ public Integer targetAltitude() {
public Integer targetSpeed() {
return targetSpeed;
}

public Waypoint setTargetAltitude(Integer targetAltitude) {
throw new IllegalStateException("Cannot update altitude on a non-landing waypoint");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.joml.Vector2d;
import ru.octol1ttle.flightassistant.computers.AirDataComputer;
import ru.octol1ttle.flightassistant.computers.ITickableComputer;
import ru.octol1ttle.flightassistant.computers.navigation.FlightPlanner;
import ru.octol1ttle.flightassistant.config.FAConfig;

public class GPWSComputer implements ITickableComputer {
Expand All @@ -26,13 +27,15 @@ public class GPWSComputer implements ITickableComputer {
private static final float PULL_UP_THRESHOLD = 5.0f;
private static final float PITCH_CORRECT_THRESHOLD = 2.5f;
private final AirDataComputer data;
private final FlightPlanner plan;
public float descentImpactTime = STATUS_UNKNOWN;
public float terrainImpactTime = STATUS_UNKNOWN;
public Vector2d terrainAvoidVector = new Vector2d();
public boolean fireworkUseSafe = true;

public GPWSComputer(AirDataComputer data) {
public GPWSComputer(AirDataComputer data, FlightPlanner plan) {
this.data = data;
this.plan = plan;
}

@Override
Expand Down Expand Up @@ -99,7 +102,7 @@ private float computeTerrainImpactTime() {
Vec3d end = data.position.add(data.velocityPerSecond.multiply(TERRAIN_RAYCAST_AHEAD_SECONDS));

BlockHitResult result = data.world.raycast(new RaycastContext(data.position, end, RaycastContext.ShapeType.COLLIDER, RaycastContext.FluidHandling.ANY, data.player));
if (result.getType() != HitResult.Type.BLOCK || result.getSide() == Direction.UP) {
if (plan.landingInProgress || result.getType() != HitResult.Type.BLOCK || result.getSide() == Direction.UP) {
return STATUS_NO_TERRAIN_AHEAD;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ private void renderVerticalMode(DrawContext context, TextRenderer textRenderer)
String type = autoflight.selectedAltitude != null ? ".selected" : ".managed";

if (plan.landingInProgress) {
String key = plan.shouldFlare() ? "mode.flightassistant.vert.flare" : "mode.flightassistant.vert.land";
verticalMode.update(Text.translatable(key, plan.landAltitude));
verticalMode.update(Text.translatable("mode.flightassistant.vert.land", plan.landAltitude));
} else if (!autoflight.autoPilotEnabled || diff <= 5) {
verticalMode.update(Text.translatable("mode.flightassistant.vert.alt_hold" + type, targetAltitude));
} else if (diff <= 10) {
Expand Down

0 comments on commit 22ae748

Please sign in to comment.