From 33277e073031d03b9fe011aee32685112a6faea1 Mon Sep 17 00:00:00 2001
From: martin <martin>
Date: Tue, 11 Dec 2007 15:20:00 +0000
Subject: [PATCH] Stuart Buchanan:

1) The chat menu will now select the runway closest to the aircraft
   when the aircraft is on the ground, or very low (< 100ft). This
   handles the case where the user has selected a runway explicitly,
   e.g. 01R for KSFO take-offs for noise abatement. My thanks for
   AnMaster for pointing this out on IRC. Note that above 100 ft, the
   wind-appropriate runway will still be used.
2) Change to keyboard description for the - key, as pointed out by
   Melchior.
---
 gui/dialogs/chat-menu.xml | 68 +++++++++++++++++++++++++++++----------
 keyboard.xml              |  2 +-
 2 files changed, 52 insertions(+), 18 deletions(-)

diff --git a/gui/dialogs/chat-menu.xml b/gui/dialogs/chat-menu.xml
index 6a0337d0e..a6fced67f 100644
--- a/gui/dialogs/chat-menu.xml
+++ b/gui/dialogs/chat-menu.xml
@@ -179,27 +179,61 @@
         };
       }      
       
-      # Determine the active runway.
-      var wind_speed = getprop("/environment/wind-speed-kt");
-      var wind_from = wind_speed ? getprop("/environment/wind-from-heading-deg") : 270;
-      var max = -1;
+      # Determine the active runway. We have two ways to do this:
+      # - If the aircraft is on the ground (or very close to it), we'll try to determine 
+      # the runway it is closest to.
+      # - If the aircraft is in the air, we'll work out the active runway based on the wind.
+      
       var active_runway = "";
-
-      foreach (var r; keys(airport.runways)) {
-        var curr = airport.runways[r];
+      var on_ground = (getprop("/position/altitude-agl-ft") < 100);
+      
+      if (on_ground)
+      {
+        # To find out the closest runway to the aircrafts position, we'll look at the heading 
+        # required to go from the aircraft's current position to the center of each runway. 
+        # The closer this is to the runways real heading, the more likely this is the runway 
+        # we're on. Note that we can't rely on /sim/atc/runway, as this is only set on 
+        # initialization.
+        var max = 360;
+        var loc = geo.aircraft_position();
         
-        var wind = wind_from - curr.heading;
-        while (wind >= 180) wind -= 360;
-        while (wind < -180) wind += 360;
-        
-        var deviation = math.abs(wind) + 1e-20;
-        var v = (0.01 * curr.length + 0.01 * curr.width) / deviation;
-
-        if (v > max) {
-          max = v;
-          active_runway = r;
+        foreach (var r; keys(airport.runways)) {
+          var curr = airport.runways[r];
+          var p = geo.Coord.new();
+          p.set_latlon(curr.lat, curr.lon, airport.elevation);
+          
+          var course = loc.course_to(p);
+          var deviation = math.abs(course - curr.heading);
+          
+          if (deviation < max)
+          {
+            active_runway = r;
+            max = deviation;
+          }
         }
       }
+      else
+      {      
+        var wind_speed = getprop("/environment/wind-speed-kt");
+        var wind_from = wind_speed ? getprop("/environment/wind-from-heading-deg") : 270;
+        var max = -1;
+
+        foreach (var r; keys(airport.runways)) {
+          var curr = airport.runways[r];
+        
+          var wind = wind_from - curr.heading;
+          while (wind >= 180) wind -= 360;
+          while (wind < -180) wind += 360;
+        
+          var deviation = math.abs(wind) + 1e-20;
+          var v = (0.01 * curr.length + 0.01 * curr.width) / deviation;
+
+          if (v > max) {
+            max = v;
+            active_runway = r;
+          }
+        }        
+      }
                
       # Find our distance and cardinal direction to the airport.            
       var directions = split(",", "North,North East,East,South East,South,South West,West,North West");
diff --git a/keyboard.xml b/keyboard.xml
index 0bec7da7a..6bd3ac5c1 100644
--- a/keyboard.xml
+++ b/keyboard.xml
@@ -355,7 +355,7 @@ top down before the key bindings are parsed.
  <key n="45">
   <name>-</name>
   <repeatable type="bool">false</repeatable>
-  <desc>Compose Chat</desc>
+  <desc>Chat Menu</desc>
   <binding>
      <command>dialog-show</command>
      <dialog-name>chat-menu</dialog-name>