From 4efd517b43d133561c4591c25dd44d23faf5167e Mon Sep 17 00:00:00 2001
From: Joshua Davidson <joshuadavidson2000@gmail.com>
Date: Fri, 17 Nov 2017 19:37:22 -0500
Subject: [PATCH] A3XX: FMGC NAV bugfix

---
 Nasal/FMGC-b.nas | 64 ++++++++++++++++++++++++++----------------------
 1 file changed, 35 insertions(+), 29 deletions(-)

diff --git a/Nasal/FMGC-b.nas b/Nasal/FMGC-b.nas
index 72f53a4a..542d856c 100644
--- a/Nasal/FMGC-b.nas
+++ b/Nasal/FMGC-b.nas
@@ -552,36 +552,42 @@ var ap_various = func {
 }
 
 var lnavCalc = maketimer(0.05, func {
-	var f = flightplan();
-	var total_distance = getprop("/autopilot/route-manager/total-distance");
-	var distance = total_distance - getprop("/autopilot/route-manager/distance-remaining-nm");
-	var destination_elevation = getprop("/autopilot/route-manager/destination/field-elevation-ft");
-	var leg = f.currentWP();
-	if (leg == nil) {
-		setprop("/it-autoflight/input/lat", 3);
-		return;
+	if (getprop("/autopilot/route-manager/active") == 1) {
+		var f = flightplan();
+		var total_distance = getprop("/autopilot/route-manager/total-distance");
+		var distance = total_distance - getprop("/autopilot/route-manager/distance-remaining-nm");
+		var destination_elevation = getprop("/autopilot/route-manager/destination/field-elevation-ft");
+		var leg = f.currentWP();
+		if (leg == nil) {
+			if (getprop("/it-autoflight/output/vert") == 1) {
+				setprop("/it-autoflight/input/lat", 3);
+			}
+			return;
+		}
+		var groundspeed = getprop("/velocities/groundspeed-kt");
+		var log_distance = getprop("/instrumentation/gps/wp/wp[1]/distance-nm");
+		var along_route = total_distance - getprop("/autopilot/route-manager/distance-remaining-nm");
+		var geocoord = geo.aircraft_position();
+		var referenceCourse = f.pathGeod(f.indexOfWP(f.destination_runway), -getprop("/autopilot/route-manager/distance-remaining-nm"));
+		var courseCoord = geo.Coord.new().set_latlon(referenceCourse.lat, referenceCourse.lon);
+		var CourseError = (geocoord.distance_to(courseCoord) / 1852) + 1;
+		var change_wp = abs(getprop("/autopilot/route-manager/wp/bearing-deg") - getprop("/orientation/heading-deg"));
+		if (change_wp > 180) {
+			change_wp = (360 - change_wp);
+		}
+		CourseError += (change_wp / 20);
+		var targetCourse = f.pathGeod(f.indexOfWP(f.destination_runway), (-getprop("autopilot/route-manager/distance-remaining-nm") + CourseError));
+		courseCoord = geo.Coord.new().set_latlon(targetCourse.lat, targetCourse.lon);
+		CourseError = (geocoord.course_to(courseCoord) - getprop("/orientation/heading-deg"));
+		if (CourseError < -180) {
+			CourseError += 360;
+		} else if (CourseError > 180) {
+			CourseError -= 360;
+		}
+		setprop("/it-autoflight/internal/lnav-error-deg", CourseError);
+	} else {
+		setprop("/it-autoflight/internal/lnav-error-deg", 0);
 	}
-	var groundspeed = getprop("/velocities/groundspeed-kt");
-	var log_distance = getprop("/instrumentation/gps/wp/wp[1]/distance-nm");
-	var along_route = total_distance - getprop("/autopilot/route-manager/distance-remaining-nm");
-	var geocoord = geo.aircraft_position();
-	var referenceCourse = f.pathGeod(f.indexOfWP(f.destination_runway), -getprop("/autopilot/route-manager/distance-remaining-nm"));
-	var courseCoord = geo.Coord.new().set_latlon(referenceCourse.lat, referenceCourse.lon);
-	var CourseError = (geocoord.distance_to(courseCoord) / 1852) + 1;
-	var change_wp = abs(getprop("/autopilot/route-manager/wp/bearing-deg") - getprop("/orientation/heading-deg"));
-	if (change_wp > 180) {
-		change_wp = (360 - change_wp);
-	}
-	CourseError += (change_wp / 20);
-	var targetCourse = f.pathGeod(f.indexOfWP(f.destination_runway), (-getprop("autopilot/route-manager/distance-remaining-nm") + CourseError));
-	courseCoord = geo.Coord.new().set_latlon(targetCourse.lat, targetCourse.lon);
-	CourseError = (geocoord.course_to(courseCoord) - getprop("/orientation/heading-deg"));
-	if (CourseError < -180) {
-		CourseError += 360;
-	} else if (CourseError > 180) {
-		CourseError -= 360;
-	}
-	setprop("/it-autoflight/internal/lnav-error-deg", CourseError);
 });
 
 var flch_on = func {