From 6b523801b8bbafc2e003d4eab8d53db05dbe9985 Mon Sep 17 00:00:00 2001
From: Matthew Maring <56924612+hayden2000@users.noreply.github.com>
Date: Wed, 10 Jun 2020 19:22:08 -0400
Subject: [PATCH] Implement enroute navaids wind, save wind values across
 flightplan changes

---
 Nasal/FMGC/winds.nas   |  40 ++++++++-------
 Nasal/MCDU/MCDU.nas    |  16 +++---
 Nasal/MCDU/WINDCLB.nas |   6 +--
 Nasal/MCDU/WINDCRZ.nas | 114 +++++++++++++++++++++++++++++++++++------
 4 files changed, 130 insertions(+), 46 deletions(-)

diff --git a/Nasal/FMGC/winds.nas b/Nasal/FMGC/winds.nas
index 0ab71805..e0319c23 100644
--- a/Nasal/FMGC/winds.nas
+++ b/Nasal/FMGC/winds.nas
@@ -159,7 +159,7 @@ var windController = {
 	},
 	
 	destroyTemporaryWinds: func(n, a) { # a = 1 activate, a = 0 erase
-		print("destroying temporary ", n);
+		#print("destroying temporary ", n);
 		if (a == 1) {
 			me.resetWind(2);
 			me.clb_winds[2] = me.copyClbWind(n);
@@ -203,14 +203,14 @@ var windController = {
 				me.winds[plan][index] = waypoint_winds.new(id, "waypoint", 0);
 			}
 		} else {
-			print("insert invalid id: ", id, ", plan: ", plan, ", index: ", index, ", size: ", me.windSizes[plan]);
-			debug.dump(me.winds);
-			debug.dump(me.windSizes);
+			#print("insert invalid id: ", id, ", plan: ", plan, ", index: ", index, ", size: ", me.windSizes[plan]);
+			#debug.dump(me.winds);
+			#debug.dump(me.windSizes);
 			return;
 		}
-		print("insert plan: ", plan, ", index: ", index);
-		debug.dump(me.winds);
-		debug.dump(me.windSizes);
+		#print("insert plan: ", plan, ", index: ", index);
+		#debug.dump(me.winds);
+		#debug.dump(me.windSizes);
 	},
 	
 	deleteWind: func(plan, index) {
@@ -224,14 +224,14 @@ var windController = {
 			pop(me.winds[plan]);
 			me.windSizes[plan] -= 1;
 		} else {
-			print("delete invalid plan: ", plan, ", index: ", index, ", size: ", me.windSizes[plan]);
-			debug.dump(me.winds);
-			debug.dump(me.windSizes);
+			#print("delete invalid plan: ", plan, ", index: ", index, ", size: ", me.windSizes[plan]);
+			#debug.dump(me.winds);
+			#debug.dump(me.windSizes);
 			return;
 		}
-		print("delete plan: ", plan, ", index: ", index);
-		debug.dump(me.winds);
-		debug.dump(me.windSizes);
+		#print("delete plan: ", plan, ", index: ", index);
+		#debug.dump(me.winds);
+		#debug.dump(me.windSizes);
 	},
 	
 	updatePlans: func() {
@@ -245,7 +245,7 @@ var windController = {
 		for (plan = 0; plan <= 2; plan += 1) {
 			for (i = 0; i < fmgc.flightPlanController.flightplans[plan].getPlanSize(); i += 1) {
 				var waypoint = fmgc.flightPlanController.flightplans[plan].getWP(i);
-				print(waypoint.wp_role, "| : |", waypoint.wp_type);
+				#print(waypoint.wp_role, "| : |", waypoint.wp_type);
 				if (waypoint.wp_role == "sid") {
 					append(me.winds[plan], waypoint_winds.new(waypoint.id, "departure", 0));
 					me.windSizes[plan] += 1;
@@ -255,7 +255,7 @@ var windController = {
 				} else if (waypoint.wp_role == nil and waypoint.wp_type == "navaid") {
 					var found = 0;
 					for (index = 0; index < windSizes_copy[plan]; index += 1) {
-						print(waypoint.id, " : ", winds_copy[plan][index].id);
+						#print(waypoint.id, " : ", winds_copy[plan][index].id);
 						if (waypoint.id == winds_copy[plan][index].id) {
 							append(me.winds[plan], winds_copy[plan][index]);
 							append(me.nav_indicies[plan], i);
@@ -271,13 +271,15 @@ var windController = {
 					append(me.winds[plan], waypoint_winds.new(waypoint.id, "waypoint", 0));
 					me.windSizes[plan] += 1;
 				}
-				print("insert plan: ", plan, ", index: ", i);
-				debug.dump(me.winds);
-				debug.dump(me.nav_indicies);
-				debug.dump(me.windSizes);
+				#print("insert plan: ", plan, ", index: ", i);
+				#debug.dump(me.winds);
+				#debug.dump(me.nav_indicies);
+				#debug.dump(me.windSizes);
 			}
 		}
 		
+		debug.dump(me.nav_indicies);
+		
 		if (canvas_mcdu.myCLBWIND[1] != nil) {
 			canvas_mcdu.myCLBWIND[1]._setupPageWithData();
 		}
diff --git a/Nasal/MCDU/MCDU.nas b/Nasal/MCDU/MCDU.nas
index 15609c39..4b085354 100644
--- a/Nasal/MCDU/MCDU.nas
+++ b/Nasal/MCDU/MCDU.nas
@@ -588,12 +588,12 @@ var rskbutton = func(btn, i) {
 			setprop("MCDU[" ~ i ~ "]/page", "WINDCLB");
 		} else if (getprop("/MCDU[" ~ i ~ "]/page") == "WINDDES") {
 			if (fmgc.flightPlanController.temporaryFlag[i]) {
-				if (getprop("/FMGC/internal/tofrom-set") and size(fmgc.flightPlanController.getWaypointList(i)) > 0) {
+				if (getprop("/FMGC/internal/tofrom-set") and size(fmgc.windController.nav_indicies[i]) > 0) {
 					if (canvas_mcdu.myCRZWIND[i] != nil) {
 						canvas_mcdu.myCRZWIND[i].del();
 					}
 					canvas_mcdu.myCRZWIND[i] = nil;
-					canvas_mcdu.myCRZWIND[i] = windCRZPage.new(i, fmgc.flightPlanController.getWaypointList(i)[0], 0);
+					canvas_mcdu.myCRZWIND[i] = windCRZPage.new(i, fmgc.flightPlanController.flightplans[i].getWP(fmgc.windController.nav_indicies[i][0]), 0);
 				} else {
 					if (canvas_mcdu.myCRZWIND[i] == nil) {
 						canvas_mcdu.myCRZWIND[i] = windCRZPage.new(i, nil, nil);
@@ -602,12 +602,12 @@ var rskbutton = func(btn, i) {
 					}
 				}
 			} else {
-				if (getprop("/FMGC/internal/tofrom-set") and size(fmgc.flightPlanController.getWaypointList(2)) > 0) {
+				if (getprop("/FMGC/internal/tofrom-set") and size(fmgc.windController.nav_indicies[2]) > 0) {
 					if (canvas_mcdu.myCRZWIND[i] != nil) {
 						canvas_mcdu.myCRZWIND[i].del();
 					}
 					canvas_mcdu.myCRZWIND[i] = nil;
-					canvas_mcdu.myCRZWIND[i] = windCRZPage.new(i, fmgc.flightPlanController.getWaypointList(2)[0], 0);
+					canvas_mcdu.myCRZWIND[i] = windCRZPage.new(i, fmgc.flightPlanController.flightplans[2].getWP(fmgc.windController.nav_indicies[2][0]), 0);
 				} else {
 					if (canvas_mcdu.myCRZWIND[i] == nil) {
 						canvas_mcdu.myCRZWIND[i] = windCRZPage.new(i, nil, nil);
@@ -641,12 +641,12 @@ var rskbutton = func(btn, i) {
 			initInputB("R5",i);
 		} else if (getprop("/MCDU[" ~ i ~ "]/page") == "WINDCLB") {
 			if (fmgc.flightPlanController.temporaryFlag[i]) {
-				if (getprop("/FMGC/internal/tofrom-set") and size(fmgc.flightPlanController.getWaypointList(i)) > 0) {
+				if (getprop("/FMGC/internal/tofrom-set") and size(fmgc.windController.nav_indicies[i]) > 0) {
 					if (canvas_mcdu.myCRZWIND[i] != nil) {
 						canvas_mcdu.myCRZWIND[i].del();
 					}
 					canvas_mcdu.myCRZWIND[i] = nil;
-					canvas_mcdu.myCRZWIND[i] = windCRZPage.new(i, fmgc.flightPlanController.getWaypointList(i)[0], 0);
+					canvas_mcdu.myCRZWIND[i] = windCRZPage.new(i, fmgc.flightPlanController.flightplans[i].getWP(fmgc.windController.nav_indicies[i][0]), 0);
 				} else {
 					if (canvas_mcdu.myCRZWIND[i] == nil) {
 						canvas_mcdu.myCRZWIND[i] = windCRZPage.new(i, nil, nil);
@@ -655,12 +655,12 @@ var rskbutton = func(btn, i) {
 					}
 				}
 			} else {
-				if (getprop("/FMGC/internal/tofrom-set") and size(fmgc.flightPlanController.getWaypointList(2)) > 0) {
+				if (getprop("/FMGC/internal/tofrom-set") and size(fmgc.windController.nav_indicies[2]) > 0) {
 					if (canvas_mcdu.myCRZWIND[i] != nil) {
 						canvas_mcdu.myCRZWIND[i].del();
 					}
 					canvas_mcdu.myCRZWIND[i] = nil;
-					canvas_mcdu.myCRZWIND[i] = windCRZPage.new(i, fmgc.flightPlanController.getWaypointList(2)[0], 0);
+					canvas_mcdu.myCRZWIND[i] = windCRZPage.new(i, fmgc.flightPlanController.flightplans[2].getWP(fmgc.windController.nav_indicies[2][0]), 0);
 				} else {
 					if (canvas_mcdu.myCRZWIND[i] == nil) {
 						canvas_mcdu.myCRZWIND[i] = windCRZPage.new(i, nil, nil);
diff --git a/Nasal/MCDU/WINDCLB.nas b/Nasal/MCDU/WINDCLB.nas
index a5cba4e6..175b9cfc 100644
--- a/Nasal/MCDU/WINDCLB.nas
+++ b/Nasal/MCDU/WINDCLB.nas
@@ -52,9 +52,9 @@ var windCLBPage = {
 			computer_temp = me.computer;
 		}
 		
-		debug.dump(fmgc.windController.clb_winds[0]);
-		debug.dump(fmgc.windController.clb_winds[1]);
-		debug.dump(fmgc.windController.clb_winds[2]);
+		#debug.dump(fmgc.windController.clb_winds[0]);
+		#debug.dump(fmgc.windController.clb_winds[1]);
+		#debug.dump(fmgc.windController.clb_winds[2]);
 		
 		if (fmgc.windController.clb_winds[computer_temp] == 0 or fmgc.windController.clb_winds[computer_temp].wind1.altitude == "") {
 			me.items = 1;
diff --git a/Nasal/MCDU/WINDCRZ.nas b/Nasal/MCDU/WINDCRZ.nas
index 25178fa9..ac0f35e1 100644
--- a/Nasal/MCDU/WINDCRZ.nas
+++ b/Nasal/MCDU/WINDCRZ.nas
@@ -30,6 +30,7 @@ var windCRZPage = {
 	index: nil,
 	computer: nil,
 	cur_location: 0,
+	match_location: 0,
 	items: 0,
 	singleCRZ: 0,
 	new: func(computer, waypoint, cur_location) {
@@ -39,8 +40,6 @@ var windCRZPage = {
 		wcp.cur_location = cur_location;
 		if (waypoint == nil) {
 			wcp.singleCRZ = 1;
-		} else {
-			#wcp.match_location = first item in match list;
 		}
 		wcp._setupPageWithData();
 		return wcp;
@@ -64,9 +63,9 @@ var windCRZPage = {
 			computer_temp = me.computer;
 		}
 		
-		debug.dump(fmgc.windController.crz_winds[0]);
-		debug.dump(fmgc.windController.crz_winds[1]);
-		debug.dump(fmgc.windController.crz_winds[2]);
+		#debug.dump(fmgc.windController.crz_winds[0]);
+		#debug.dump(fmgc.windController.crz_winds[1]);
+		#debug.dump(fmgc.windController.crz_winds[2]);
 		
 		if (me.singleCRZ == 1) {
 			if (fmgc.windController.crz_winds[computer_temp] == 0 or fmgc.windController.crz_winds[computer_temp].wind1.altitude == "") {
@@ -79,7 +78,16 @@ var windCRZPage = {
 				me.items = 4;
 			}
 		} else {
-			
+			me.match_location = fmgc.windController.nav_indicies[computer_temp][me.cur_location];
+			if (size(fmgc.windController.winds[computer_temp]) == 0 or fmgc.windController.winds[computer_temp][me.match_location].wind1.altitude == "") {
+				me.items = 1;
+			} else if (fmgc.windController.winds[computer_temp][me.match_location].wind2.altitude == "") {
+				me.items = 2;
+			} else if (fmgc.windController.crz_winds[computer_temp][me.match_location].wind3.altitude == "") {
+				me.items = 3;
+			} else {
+				me.items = 4;
+			}
 		}
 		
 		# load data
@@ -134,7 +142,55 @@ var windCRZPage = {
 				}
 			}
 		} else {
-			
+			if (me.items >= 4) {
+				wind = fmgc.windController.winds[computer_temp][me.match_location].wind4;
+				if (wind.altitude != "") {
+					me.L4 = [wind.heading ~ "/" ~ wind.magnitude ~ "/" ~ wind.altitude, nil, "blu"];
+					me.fontMatrix[0][3] = 0;
+				} else {
+					me.L4 = ["[  ]/[  ]/[   ]", nil, "blu"];
+					me.fontMatrix[0][3] = 1;
+				}
+			} else {
+				me.L4 = [nil, nil, "ack"];
+			}
+		
+			if (me.items >= 3) {
+				wind = fmgc.windController.winds[computer_temp][me.match_location].wind3;
+				if (wind.altitude != "") {
+					me.L3 = [wind.heading ~ "/" ~ wind.magnitude ~ "/" ~ wind.altitude, nil, "blu"];
+					me.fontMatrix[0][2] = 0;
+				} else {
+					me.L3 = ["[  ]/[  ]/[   ]", nil, "blu"];
+					me.fontMatrix[0][2] = 1;
+				}
+			} else {
+				me.L3 = [nil, nil, "ack"];
+			}
+		
+			if (me.items >= 2) {
+				wind = fmgc.windController.winds[computer_temp][me.match_location].wind2;
+				if (wind.altitude != "") {
+					me.L2 = [wind.heading ~ "/" ~ wind.magnitude ~ "/" ~ wind.altitude, nil, "blu"];
+					me.fontMatrix[0][1] = 0;
+				} else {
+					me.L2 = ["[  ]/[  ]/[   ]", nil, "blu"];
+					me.fontMatrix[0][1] = 1;
+				}
+			} else {
+				me.L2 = [nil, nil, "ack"];
+			}
+		
+			if (me.items >= 1) {
+				wind = fmgc.windController.winds[computer_temp][me.match_location].wind1;
+				if (wind.altitude != "") {
+					me.L1 = [wind.heading ~ "/" ~ wind.magnitude ~ "/" ~ wind.altitude, "TRU WIND/ALT", "blu"];
+					me.fontMatrix[0][0] = 0;
+				} else {
+					me.L1 = ["[  ]/[  ]/[   ]", "TRU WIND/ALT", "blu"];
+					me.fontMatrix[0][0] = 1;
+				}
+			}
 		}
 		
 		me.L5 = ["[  ]/[   ]", "SAT / ALT", "blu"];
@@ -216,7 +272,27 @@ var windCRZPage = {
 							fmgc.windController.crz_winds[computer_temp].wind1.altitude = winds[2];
 						}
 					} else {
-						
+						if (index == 5) {
+							fmgc.windController.winds[computer_temp][me.match_location].wind5.heading = winds[0];
+							fmgc.windController.winds[computer_temp][me.match_location].wind5.magnitude = winds[1];
+							fmgc.windController.winds[computer_temp][me.match_location].wind5.altitude = winds[2];
+						} else if (index == 4) {
+							fmgc.windController.winds[computer_temp][me.match_location].wind4.heading = winds[0];
+							fmgc.windController.winds[computer_temp][me.match_location].wind4.magnitude = winds[1];
+							fmgc.windController.winds[computer_temp][me.match_location].wind4.altitude = winds[2];
+						} else if (index == 3) {
+							fmgc.windController.winds[computer_temp][me.match_location].wind3.heading = winds[0];
+							fmgc.windController.winds[computer_temp][me.match_location].wind3.magnitude = winds[1];
+							fmgc.windController.winds[computer_temp][me.match_location].wind3.altitude = winds[2];
+						} else if (index == 2) {
+							fmgc.windController.winds[computer_temp][me.match_location].wind2.heading = winds[0];
+							fmgc.windController.winds[computer_temp][me.match_location].wind2.magnitude = winds[1];
+							fmgc.windController.winds[computer_temp][me.match_location].wind2.altitude = winds[2];
+						} else if (index == 1) {
+							fmgc.windController.winds[computer_temp][me.match_location].wind1.heading = winds[0];
+							fmgc.windController.winds[computer_temp][me.match_location].wind1.magnitude = winds[1];
+							fmgc.windController.winds[computer_temp][me.match_location].wind1.altitude = winds[2];
+						}
 					}
 					mcdu_scratchpad.scratchpads[me.computer].empty();
 					if (me.items == index and index != 5) {
@@ -235,25 +311,31 @@ var windCRZPage = {
 		}
 	},
 	pushButtonUp: func() {
-		if (me.cur_location < size(fmgc.flightPlanController.getWaypointList(2)) - 1) {
+		var computer_temp = 2;
+		if (fmgc.flightPlanController.temporaryFlag[me.computer]) {
+			computer_temp = me.computer;
+		}
+		if (me.cur_location < size(fmgc.windController.nav_indicies[computer_temp]) - 1) {
 			me.cur_location = me.cur_location + 1;
 		} else {
 			me.cur_location = 0;
 		}
-		me.waypoint = fmgc.flightPlanController.getWaypointList(2)[me.cur_location];
-		me.match_location = fmgc.flightPlanController.getWaypointMapping(2)[me.cur_location];
-		#load stored data here
+		me.match_location = fmgc.windController.nav_indicies[computer_temp][me.cur_location];
+		me.waypoint = fmgc.flightPlanController.flightplans[computer_temp].getWP(me.match_location);
 		me.reload();
 	},
 	pushButtonDown: func() {
+		var computer_temp = 2;
+		if (fmgc.flightPlanController.temporaryFlag[me.computer]) {
+			computer_temp = me.computer;
+		}
 		if (me.cur_location > 0) {
 			me.cur_location = me.cur_location - 1;
 		} else {
-			me.cur_location = size(fmgc.flightPlanController.getWaypointList(2)) - 1;
+			me.cur_location = size(fmgc.windController.nav_indicies[computer_temp]) - 1;
 		}
-		me.waypoint = fmgc.flightPlanController.getWaypointList(2)[me.cur_location];
-		me.match_location = fmgc.flightPlanController.getWaypointMapping(2)[me.cur_location];
-		#load stored data here
+		me.match_location = fmgc.windController.nav_indicies[computer_temp][me.cur_location];
+		me.waypoint = fmgc.flightPlanController.flightplans[computer_temp].getWP(me.match_location);
 		me.reload();
 	}
 };
\ No newline at end of file