From 9b17c5f7e6b100ffa691cc83312b9e0080bac053 Mon Sep 17 00:00:00 2001
From: Josh Davidson <joshdavidson2000@outlook.com>
Date: Sat, 12 Mar 2022 13:30:41 -0500
Subject: [PATCH] FMGC: Fix V/S engage bug, sync with ITAF core pt2

---
 Nasal/Libraries/libraries.nas |  2 +-
 Systems/fmgc-drivers.xml      | 93 +++++++++++++++++++++++----------
 Systems/fmgc-pitch.xml        | 60 ++-------------------
 Systems/fmgc-roll-yaw.xml     | 98 +++--------------------------------
 4 files changed, 76 insertions(+), 177 deletions(-)

diff --git a/Nasal/Libraries/libraries.nas b/Nasal/Libraries/libraries.nas
index 6d4fcd6f..153a1ad9 100644
--- a/Nasal/Libraries/libraries.nas
+++ b/Nasal/Libraries/libraries.nas
@@ -339,7 +339,7 @@ setlistener("/controls/flight/auto-coordination", func() {
 	print("System: Auto Coordination has been turned off as it is not compatible with the fly-by-wire of this aircraft.");
 	screen.log.write("Auto Coordination has been disabled as it is not compatible with the fly-by-wire of this aircraft", 1, 0, 0);
 	screen.log.write("Tiller will now be controlled by aileron, rather than rudder", 1, 0, 0);
-}, 1, 0);
+}, 0, 0);
 
 # Legacy FCU
 var APPanel = {
diff --git a/Systems/fmgc-drivers.xml b/Systems/fmgc-drivers.xml
index d1ab836c..eb45cb7d 100644
--- a/Systems/fmgc-drivers.xml
+++ b/Systems/fmgc-drivers.xml
@@ -5,6 +5,47 @@
 
 <PropertyList>
 	
+	<!-- Logic -->
+	<logic>
+		<input>
+			<or>
+				<equals>
+					<property>/it-autoflight/output/ap1</property>
+					<value>1</value>
+				</equals>
+				<equals>
+					<property>/it-autoflight/output/ap2</property>
+					<value>1</value>
+				</equals>
+				<equals>
+					<property>/it-autoflight/output/fd1</property>
+					<value>1</value>
+				</equals>
+				<equals>
+					<property>/it-autoflight/output/fd2</property>
+					<value>1</value>
+				</equals>
+			</or>
+		</input>
+		<output>/it-autoflight/output/apfd-on</output>
+	</logic>
+	
+	<logic>
+		<input>
+			<or>
+				<equals>
+					<property>/it-autoflight/output/ap1</property>
+					<value>1</value>
+				</equals>
+				<equals>
+					<property>/it-autoflight/output/ap2</property>
+					<value>1</value>
+				</equals>
+			</or>
+		</input>
+		<output>/it-autoflight/output/ap-on</output>
+	</logic>
+	
 	<!-- Predictors -->
 	<predict-simple>
 		<name>IAS 5 Second Predictor</name>
@@ -288,24 +329,7 @@
 							<value>7</value>
 						</equals>
 					</or>
-					<or>
-						<equals>
-							<property>/it-autoflight/output/ap1</property>
-							<value>1</value>
-						</equals>
-						<equals>
-							<property>/it-autoflight/output/ap2</property>
-							<value>1</value>
-						</equals>
-						<equals>
-							<property>/it-autoflight/output/fd1</property>
-							<value>1</value>
-						</equals>
-						<equals>
-							<property>/it-autoflight/output/fd2</property>
-							<value>1</value>
-						</equals>
-					</or>
+					<property>/it-autoflight/output/apfd-on</property>
 					<equals>
 						<property>/gear/gear[1]/wow</property>
 						<value>0</value>
@@ -636,6 +660,28 @@
 		<type>noise-spike</type>
 		<feedback-if-disabled>true</feedback-if-disabled>
 		<initialize-to>output</initialize-to>
+		<input>
+			<condition>
+				<not>
+					<and>
+						<property>/it-autoflight/output/ap-on</property>
+						<equals>
+							<property>/it-autoflight/output/vert</property>
+							<value>1</value>
+						</equals>
+						<equals>
+							<property>/gear/gear[1]/wow</property>
+							<value>0</value>
+						</equals>
+						<equals>
+							<property>/gear/gear[2]/wow</property>
+							<value>0</value>
+						</equals>
+					</and>
+				</not>
+			</condition>
+			<property>/it-autoflight/internal/vert-speed-fpm</property>
+		</input>
 		<input>
 			<condition>
 				<property>/it-autoflight/internal/overspeed-vs-prot</property>
@@ -653,16 +699,7 @@
 		<max-rate-of-change>
 			<condition>
 				<and>
-					<or>
-						<equals>
-							<property>/it-autoflight/output/ap1</property>
-							<value>1</value>
-						</equals>
-						<equals>
-							<property>/it-autoflight/output/ap2</property>
-							<value>1</value>
-						</equals>
-					</or>
+					<property>/it-autoflight/output/ap-on</property>
 					<not><property>/it-autoflight/internal/overspeed-vs-prot</property></not>
 					<not><property>/it-autoflight/internal/underspeed-vs-prot</property></not>
 					<equals>
diff --git a/Systems/fmgc-pitch.xml b/Systems/fmgc-pitch.xml
index 90e91a22..ccb635a9 100644
--- a/Systems/fmgc-pitch.xml
+++ b/Systems/fmgc-pitch.xml
@@ -37,24 +37,7 @@
 						<value>7</value>
 					</equals>
 				</or>
-				<or>
-					<equals>
-						<property>/it-autoflight/output/ap1</property>
-						<value>1</value>
-					</equals>
-					<equals>
-						<property>/it-autoflight/output/ap2</property>
-						<value>1</value>
-					</equals>
-					<equals>
-						<property>/it-autoflight/output/fd1</property>
-						<value>1</value>
-					</equals>
-					<equals>
-						<property>/it-autoflight/output/fd2</property>
-						<value>1</value>
-					</equals>
-				</or>
+				<property>/it-autoflight/output/apfd-on</property>
 			</and>
 		</input>
 		<output>/it-autoflight/internal/flch-active</output>
@@ -185,24 +168,7 @@
 					<property>/it-autoflight/output/vert</property>
 					<value>5</value>
 				</equals>
-				<or>
-					<equals>
-						<property>/it-autoflight/output/ap1</property>
-						<value>1</value>
-					</equals>
-					<equals>
-						<property>/it-autoflight/output/ap2</property>
-						<value>1</value>
-					</equals>
-					<equals>
-						<property>/it-autoflight/output/fd1</property>
-						<value>1</value>
-					</equals>
-					<equals>
-						<property>/it-autoflight/output/fd2</property>
-						<value>1</value>
-					</equals>
-				</or>
+				<property>/it-autoflight/output/apfd-on</property>
 			</and>
 		</input>
 		<output>/it-autoflight/internal/fpa-active</output>
@@ -497,16 +463,7 @@
 						<property>/gear/gear[2]/wow</property>
 						<value>0</value>
 					</equals>
-					<or>
-						<equals>
-							<property>/it-autoflight/output/ap1</property>
-							<value>1</value>
-						</equals>
-						<equals>
-							<property>/it-autoflight/output/ap2</property>
-							<value>1</value>
-						</equals>
-					</or>
+					<property>/it-autoflight/output/ap-on</property>
 					<not-equals>
 						<property>/it-autoflight/output/vert</property>
 						<value>9</value>
@@ -653,16 +610,7 @@
 							<property>/gear/gear[2]/wow</property>
 							<value>0</value>
 						</equals>
-						<or>
-							<equals>
-								<property>/it-autoflight/output/ap1</property>
-								<value>1</value>
-							</equals>
-							<equals>
-								<property>/it-autoflight/output/ap2</property>
-								<value>1</value>
-							</equals>
-						</or>
+						<property>/it-autoflight/output/ap-on</property>
 						<not-equals>
 							<property>/it-autoflight/output/vert</property>
 							<value>9</value>
diff --git a/Systems/fmgc-roll-yaw.xml b/Systems/fmgc-roll-yaw.xml
index 6f3ba37c..187ddcc1 100644
--- a/Systems/fmgc-roll-yaw.xml
+++ b/Systems/fmgc-roll-yaw.xml
@@ -31,24 +31,7 @@
 							<value>1</value>
 						</equals>
 					</or>
-					<or>
-						<equals>
-							<property>/it-autoflight/output/ap1</property>
-							<value>1</value>
-						</equals>
-						<equals>
-							<property>/it-autoflight/output/ap2</property>
-							<value>1</value>
-						</equals>
-						<equals>
-							<property>/it-autoflight/output/fd1</property>
-							<value>1</value>
-						</equals>
-						<equals>
-							<property>/it-autoflight/output/fd2</property>
-							<value>1</value>
-						</equals>
-					</or>
+					<property>/it-autoflight/output/apfd-on</property>
 				</and>
 			</condition>
 		</enable>
@@ -81,24 +64,7 @@
 						<property>/it-autoflight/output/lat</property>
 						<value>2</value>
 					</equals>
-					<or>
-						<equals>
-							<property>/it-autoflight/output/ap1</property>
-							<value>1</value>
-						</equals>
-						<equals>
-							<property>/it-autoflight/output/ap2</property>
-							<value>1</value>
-						</equals>
-						<equals>
-							<property>/it-autoflight/output/fd1</property>
-							<value>1</value>
-						</equals>
-						<equals>
-							<property>/it-autoflight/output/fd2</property>
-							<value>1</value>
-						</equals>
-					</or>
+					<property>/it-autoflight/output/apfd-on</property>
 				</and>
 			</condition>
 		</enable>
@@ -141,24 +107,7 @@
 		<enable>
 			<condition>
 				<or>
-					<and>
-						<not-equals>
-							<property>/it-autoflight/output/ap1</property>
-							<value>1</value>
-						</not-equals>
-						<not-equals>
-							<property>/it-autoflight/output/ap2</property>
-							<value>1</value>
-						</not-equals>
-						<not-equals>
-							<property>/it-autoflight/output/fd1</property>
-							<value>1</value>
-						</not-equals>
-						<not-equals>
-							<property>/it-autoflight/output/fd2</property>
-							<value>1</value>
-						</not-equals>
-					</and>
+					<not><property>/it-autoflight/output/apfd-on</property></not>
 					<equals>
 						<property>/it-autoflight/output/lat</property>
 						<value>9</value>
@@ -183,24 +132,7 @@
 		<max-rate-of-change>
 			<condition>
 				<or>
-					<and>
-						<not-equals>
-							<property>/it-autoflight/output/ap1</property>
-							<value>1</value>
-						</not-equals>
-						<not-equals>
-							<property>/it-autoflight/output/ap2</property>
-							<value>1</value>
-						</not-equals>
-						<not-equals>
-							<property>/it-autoflight/output/fd1</property>
-							<value>1</value>
-						</not-equals>
-						<not-equals>
-							<property>/it-autoflight/output/fd2</property>
-							<value>1</value>
-						</not-equals>
-					</and>
+					<not><property>/it-autoflight/output/apfd-on</property></not>
 					<equals>
 						<property>/it-autoflight/output/lat</property>
 						<value>9</value>
@@ -211,16 +143,7 @@
 		</max-rate-of-change>
 		<max-rate-of-change>
 			<condition>
-				<and>
-					<not-equals>
-						<property>/it-autoflight/output/ap1</property>
-						<value>1</value>
-					</not-equals>
-					<not-equals>
-						<property>/it-autoflight/output/ap2</property>
-						<value>1</value>
-					</not-equals>
-				</and>
+				<not><property>/it-autoflight/output/ap-on</property></not>
 			</condition>
 			<value>15</value>
 		</max-rate-of-change>
@@ -281,16 +204,7 @@
 		<input>
 			<condition>
 				<and>
-					<or>
-						<equals>
-							<property>/it-autoflight/output/ap1</property>
-							<value>1</value>
-						</equals>
-						<equals>
-							<property>/it-autoflight/output/ap2</property>
-							<value>1</value>
-						</equals>
-					</or>
+					<property>/it-autoflight/output/ap-on</property>
 					<equals>
 						<property>/it-autoflight/output/lat</property>
 						<value>4</value>