From 198298c270b09911f9f1f84391042a94e80c4c32 Mon Sep 17 00:00:00 2001
From: Joshua Davidson <joshuadavidson2000@gmail.com>
Date: Fri, 28 Sep 2018 15:38:26 -0400
Subject: [PATCH] System: APU Bleed now makes APU wait 1min before shutdown,
 fixes #10

---
 AircraftConfig/acconfig.nas |  8 --------
 Nasal/engines-cfm.nas       |  1 +
 Nasal/engines-common.nas    | 32 ++++++++++++++++++++++++++++++--
 Nasal/engines-iae.nas       |  1 +
 Nasal/libraries.nas         |  3 ---
 Nasal/pneumatics.nas        |  5 +++--
 revision.txt                |  2 +-
 7 files changed, 36 insertions(+), 16 deletions(-)

diff --git a/AircraftConfig/acconfig.nas b/AircraftConfig/acconfig.nas
index 46082679..c2205bab 100644
--- a/AircraftConfig/acconfig.nas
+++ b/AircraftConfig/acconfig.nas
@@ -229,9 +229,6 @@ var colddark_b = func {
 	# Continues the Cold and Dark script, after engines fully shutdown.
 	setprop("/controls/APU/master", 0);
 	setprop("/controls/APU/start", 0);
-	setprop("/controls/bleed/OHP/bleedapu", 0);
-	setprop("/controls/electrical/switches/battery1", 0);
-	setprop("/controls/electrical/switches/battery2", 0);
 	setprop("/controls/gear/brake-left", 0);
 	setprop("/controls/gear/brake-right", 0);
 	setprop("/systems/acconfig/autoconfig-running", 0);
@@ -265,9 +262,6 @@ var beforestart = func {
 	failReset();
 	setprop("/controls/APU/master", 0);
 	setprop("/controls/APU/start", 0);
-	setprop("/controls/bleed/OHP/bleedapu", 0);
-	setprop("/controls/electrical/switches/battery1", 0);
-	setprop("/controls/electrical/switches/battery2", 0);
 	
 	# Now the Startup!
 	setprop("/controls/electrical/switches/battery1", 1);
@@ -343,8 +337,6 @@ var taxi = func {
 	failReset();
 	setprop("/controls/APU/master", 0);
 	setprop("/controls/APU/start", 0);
-	setprop("/controls/electrical/switches/battery1", 0);
-	setprop("/controls/electrical/switches/battery2", 0);
 	
 	# Now the Startup!
 	setprop("/controls/electrical/switches/battery1", 1);
diff --git a/Nasal/engines-cfm.nas b/Nasal/engines-cfm.nas
index 271ee991..a7082f0a 100644
--- a/Nasal/engines-cfm.nas
+++ b/Nasal/engines-cfm.nas
@@ -31,6 +31,7 @@ setprop("/controls/engines/engine[1]/last-igniter", "B");
 var eng_init = func {
 	setprop("/controls/engines/engine[0]/man-start", 0);
 	setprop("/controls/engines/engine[1]/man-start", 0);
+	eng_common_init();
 }
 
 # Trigger Startups and Stops
diff --git a/Nasal/engines-common.nas b/Nasal/engines-common.nas
index 0d9ff2c9..a4cf7a93 100644
--- a/Nasal/engines-common.nas
+++ b/Nasal/engines-common.nas
@@ -17,6 +17,13 @@ var apu_egt_min = 352;
 var apu_egt_max = 704;
 setprop("/systems/apu/rpm", 0);
 setprop("/systems/apu/egt", 42);
+setprop("/systems/apu/bleed-used", 0);
+setprop("/systems/apu/bleed-counting", 0);
+setprop("/systems/apu/bleed-time", 0);
+
+var eng_common_init = func {
+	setprop("/systems/apu/bleed-used", 0);
+}
 
 # Start APU
 setlistener("/controls/APU/start", func {
@@ -60,10 +67,31 @@ setlistener("/controls/APU/master", func {
 });
 
 var apu_stop = func {
-	interpolate("/systems/apu/rpm", 0, 30);
-	interpolate("/systems/apu/egt", 42, 40);
+	if (getprop("/systems/apu/bleed-used") == 1 and getprop("/systems/apu/bleed-counting") != 1) {
+		setprop("/systems/apu/bleed-counting", 1);
+		setprop("/systems/apu/bleed-time", getprop("/sim/time/elapsed-sec"));
+	}
+	if (getprop("/systems/apu/bleed-used") == 1 and getprop("/systems/apu/bleed-counting") == 1) {
+		apuBleedChk.start();
+	} else {
+		apuBleedChk.stop();
+		interpolate("/systems/apu/rpm", 0, 30);
+		interpolate("/systems/apu/egt", 42, 40);
+		setprop("/systems/apu/bleed-counting", 0);
+	}
 }
 
+var apuBleedChk = maketimer(0.1, func {
+	if (getprop("/systems/apu/bleed-used") == 1 and getprop("/systems/apu/bleed-counting") == 1) {
+		if (getprop("/systems/apu/bleed-time") + 60 <= getprop("/sim/time/elapsed-sec")) {
+			apuBleedChk.stop();
+			interpolate("/systems/apu/rpm", 0, 30);
+			interpolate("/systems/apu/egt", 42, 40);
+			setprop("/systems/apu/bleed-counting", 0);
+		}
+	}
+});
+
 # Various Other Stuff
 var doIdleThrust = func {
 	setprop("/controls/engines/engine[0]/throttle", 0.0);
diff --git a/Nasal/engines-iae.nas b/Nasal/engines-iae.nas
index b8ba6853..ec26ddb7 100644
--- a/Nasal/engines-iae.nas
+++ b/Nasal/engines-iae.nas
@@ -31,6 +31,7 @@ setprop("/controls/engines/engine[1]/last-igniter", "B");
 var eng_init = func {
 	setprop("/controls/engines/engine[0]/man-start", 0);
 	setprop("/controls/engines/engine[1]/man-start", 0);
+	eng_common_init();
 }
 
 # Trigger Startups and Stops
diff --git a/Nasal/libraries.nas b/Nasal/libraries.nas
index a7669e9f..7522c476 100644
--- a/Nasal/libraries.nas
+++ b/Nasal/libraries.nas
@@ -16,9 +16,6 @@ setprop("/controls/lighting/ndl-norm", 1);
 setprop("/controls/lighting/ndr-norm", 1);
 setprop("/controls/lighting/upper-norm", 1);
 
-# Surprise
-setprop("/controls/tray/surprise", 1);
-
 # Lights
 setprop("/sim/model/lights/nose-lights", 0);
 setprop("/sim/model/lights/turnoffsw", 0);
diff --git a/Nasal/pneumatics.nas b/Nasal/pneumatics.nas
index 06b469ff..281ca14f 100644
--- a/Nasal/pneumatics.nas
+++ b/Nasal/pneumatics.nas
@@ -200,6 +200,7 @@ var PNEU = {
 		# Air Sources/PSI
 		if (rpmapu >= 94.9 and bleedapu_sw and !bleedapu_fail) {
 			setprop("/systems/pneumatic/bleedapu", 34);
+			setprop("/systems/apu/bleed-used", 1);
 		} else {
 			setprop("/systems/pneumatic/bleedapu", 0);
 		}
@@ -372,10 +373,10 @@ var PNEU = {
 		acess = getprop("/systems/electrical/bus/ac-ess");
 		fanon = getprop("/systems/ventilation/avionics/fan");
 		
-		if ((dcess > 25) or (acess > 110)) {
+		if (dcess > 25 or acess > 110) {
 			setprop("/systems/ventilation/avionics/fan", 1);
 			setprop("/systems/ventilation/lavatory/extractfan", 1);
-		} else if ((dcess == 0) and (acess == 0)) {
+		} else if (dcess == 0 and acess == 0) {
 			setprop("/systems/ventilation/avionics/fan", 0);
 			setprop("/systems/ventilation/lavatory/extractfan", 0);
 		}
diff --git a/revision.txt b/revision.txt
index f3972440..b840a9e9 100644
--- a/revision.txt
+++ b/revision.txt
@@ -1 +1 @@
-4657
\ No newline at end of file
+4658
\ No newline at end of file