From ba4976e4e7c7f52917461ef6bbc1e418c8396ef4 Mon Sep 17 00:00:00 2001
From: Joshua Davidson <joshuadavidson2000@gmail.com>
Date: Mon, 1 Jan 2018 10:30:11 -0500
Subject: [PATCH] A32X: Various FMGS fixed, including #121

---
 Nasal/FMGC-b.nas             | 28 +++++++++++++++++++---------
 Sounds/A320-common-sound.xml | 30 ++++++++++++++++--------------
 revision.txt                 |  2 +-
 3 files changed, 36 insertions(+), 24 deletions(-)

diff --git a/Nasal/FMGC-b.nas b/Nasal/FMGC-b.nas
index 14d7b867..4038c1cb 100644
--- a/Nasal/FMGC-b.nas
+++ b/Nasal/FMGC-b.nas
@@ -105,7 +105,7 @@ setlistener("/it-autoflight/input/ap1", func {
 	var law = getprop("/it-fbw/law");
 	if (apmas == 0) {
 		setprop("/it-autoflight/output/ap1", 0);
-		if (getprop("/it-autoflight/sound/enableapoffsound") == 1) {
+		if (getprop("/it-autoflight/sound/enableapoffsound") == 1 and getprop("/it-autoflight/output/ap2") == 0) {
 			setprop("/it-autoflight/sound/apoffsound", 1);
 			setprop("/it-autoflight/sound/enableapoffsound", 0);	  
 		}
@@ -124,6 +124,9 @@ setlistener("/it-autoflight/input/ap1", func {
 				}
 			}
 			setprop("/it-autoflight/output/ap1", 1);
+			if (getprop("/it-autoflight/output/ap2") == 1 and (getprop("/it-autoflight/output/appr-armed") != 1 and getprop("/it-autoflight/output/lat") != 2 and getprop("/it-autoflight/output/lat") != 6)) {
+				setprop("/it-autoflight/input/ap2", 0);
+			}
 			setprop("/it-autoflight/sound/enableapoffsound", 1);
 			setprop("/it-autoflight/sound/apoffsound", 0);
 			fmabox();
@@ -138,7 +141,7 @@ setlistener("/it-autoflight/input/ap2", func {
 	var law = getprop("/it-fbw/law");
 	if (apmas == 0) {
 		setprop("/it-autoflight/output/ap2", 0);
-		if (getprop("/it-autoflight/sound/enableapoffsound2") == 1) {
+		if (getprop("/it-autoflight/sound/enableapoffsound2") == 1 and getprop("/it-autoflight/output/ap1") == 0) {
 			setprop("/it-autoflight/sound/apoffsound2", 1);	
 			setprop("/it-autoflight/sound/enableapoffsound2", 0);	  
 		}
@@ -157,6 +160,9 @@ setlistener("/it-autoflight/input/ap2", func {
 				}
 			}
 			setprop("/it-autoflight/output/ap2", 1);
+			if (getprop("/it-autoflight/output/ap1") == 1 and (getprop("/it-autoflight/output/appr-armed") != 1 and getprop("/it-autoflight/output/lat") != 2 and getprop("/it-autoflight/output/lat") != 6)) {
+				setprop("/it-autoflight/input/ap1", 0);
+			}
 			setprop("/it-autoflight/sound/enableapoffsound2", 1);
 			setprop("/it-autoflight/sound/apoffsound2", 0);
 			fmabox();
@@ -213,14 +219,18 @@ var fmabox = func {
 		setprop("/it-autoflight/output/fma-pwr", 0);
 	} else {
 		setprop("/it-autoflight/output/fma-pwr", 1);
-		setprop("/it-autoflight/input/lat", 3);
-		if (getprop("/it-autoflight/custom/trk-fpa") == 0) {
-			setprop("/it-autoflight/input/vert", 1);
-		} else if (getprop("/it-autoflight/custom/trk-fpa") == 1) {
-			setprop("/it-autoflight/input/vert", 5);
+		if (getprop("/it-autoflight/output/lat") == 9) {
+			setprop("/it-autoflight/input/lat", 3);
+		}
+		if (getprop("/it-autoflight/output/vert") == 9) {
+			if (getprop("/it-autoflight/custom/trk-fpa") == 0) {
+				setprop("/it-autoflight/input/vert", 1);
+			} else if (getprop("/it-autoflight/custom/trk-fpa") == 1) {
+				setprop("/it-autoflight/input/vert", 5);
+			}
+			setprop("/it-autoflight/input/vs", math.round(getprop("/it-autoflight/internal/vert-speed-fpm"), 100));
+			setprop("/it-autoflight/input/fpa", math.round(getprop("/it-autoflight/internal/fpa"), 0.1));
 		}
-		setprop("/it-autoflight/input/vs", math.round(getprop("/it-autoflight/internal/vert-speed-fpm"), 100));
-		setprop("/it-autoflight/input/fpa", math.round(getprop("/it-autoflight/internal/fpa"), 0.1));
 	}
 }
 
diff --git a/Sounds/A320-common-sound.xml b/Sounds/A320-common-sound.xml
index 80b8978d..12c401cd 100644
--- a/Sounds/A320-common-sound.xml
+++ b/Sounds/A320-common-sound.xml
@@ -797,29 +797,31 @@
 			<reference-dist>20.0</reference-dist>
 			<max-dist>100.0</max-dist>
 		</retard>
-	
+		
 		<apoff>
 			<name>apoff</name>
 			<path>/Aircraft/IDG-A32X/Sounds/apoff.wav</path>
 			<condition>
-				<property>/it-autoflight/sound/apoffsound</property>
+				<and>
+					<equals>
+						<property>/it-autoflight/output/ap1</property>
+						<value>0</value>
+					</equals>
+					<equals>
+						<property>/it-autoflight/output/ap2</property>
+						<value>0</value>
+					</equals>
+					<or>
+						<property>/it-autoflight/sound/apoffsound</property>
+						<property>/it-autoflight/sound/apoffsound2</property>
+					</or>
+				</and>
 			</condition>
 			<volume>
 				<factor>40.0</factor>
 			</volume>
 		</apoff>
-  
-		<apoff>
-			<name>apoff2</name>
-			<path>/Aircraft/IDG-A32X/Sounds/apoff.wav</path>
-			<condition>
-				<property>/it-autoflight/sound/apoffsound2</property>
-			</condition>
-			<volume>
-				<factor>40.0</factor>
-			</volume>
-		</apoff>
-	
+		
 		<gear>
 			<name>gear0</name>
 			<mode>in-transit</mode>
diff --git a/revision.txt b/revision.txt
index e0e9ecdf..04e8d94a 100644
--- a/revision.txt
+++ b/revision.txt
@@ -1 +1 @@
-4216
\ No newline at end of file
+4217
\ No newline at end of file