]> git.mxchange.org Git - flightgear.git/blobdiff - src/Airports/runways.cxx
- export font properties to the property tree again
[flightgear.git] / src / Airports / runways.cxx
index 4018cc5bcc5d735b10c0d60a0b4015ee11f2730c..c1b22452259df4cf70eeb705d176bbb1ea90f505 100644 (file)
 #  include <config.h>
 #endif
 
-#include <math.h>               // fabs()
-#include <stdio.h>              // sprintf()
+#include <cmath>               // fabs()
+#include <cstdio>              // sprintf()
+#include <cstdlib>             // atoi()
 
 #include <simgear/compiler.h>
-
 #include <simgear/debug/logstream.hxx>
+#include <Main/fg_props.hxx>
 
 #include STL_STRING
 #include <map>
@@ -216,63 +217,78 @@ bool FGRunwayList::search( const string& aptid, const int tgt_hdg,
 
 
 // Return the runway number of the runway closest to a given heading
-string FGRunwayList::search( const string& aptid, const int tgt_hdg ) {
+string FGRunwayList::search( const string& aptid, const int hdg ) {
+    //SG_LOG(SG_GENERAL, SG_ALERT, "searching runway for " << aptid
+    //        << " with target heading " << hdg);
+
     FGRunway r;
-    FGRunway tmp_r;    
-    string rn;
-    double found_dir = 0.0;
-
-    if ( !search( aptid, &tmp_r ) ) {
-       SG_LOG( SG_GENERAL, SG_ALERT,
-                "Failed to find " << aptid << " in database." );
-       return "NN";
+    if (!search(aptid, &r)) {
+        SG_LOG(SG_GENERAL, SG_ALERT, "Failed to find "
+                << aptid << " in database.");
+        return "NN";
     }
-    
-    double diff;
-    double min_diff = 360.0;
-    
-    while ( tmp_r._id == aptid ) {
-       // forward direction
-       diff = tgt_hdg - tmp_r._heading;
-       while ( diff < -180.0 ) { diff += 360.0; }
-       while ( diff >  180.0 ) { diff -= 360.0; }
-       diff = fabs(diff);
-        // SG_LOG( SG_GENERAL, SG_INFO,
-        //        "Runway " << tmp_r._rwy_no << " heading = "
-       //         << tmp_r._heading << " diff = " << diff );
-       if ( diff < min_diff ) {
-           min_diff = diff;
-           r = tmp_r;
-           found_dir = 0;
-       }
-       
-       // reverse direction
-       diff = tgt_hdg - tmp_r._heading - 180.0;
-       while ( diff < -180.0 ) { diff += 360.0; }
-       while ( diff >  180.0 ) { diff -= 360.0; }
-       diff = fabs(diff);
-        // SG_LOG( SG_GENERAL, SG_INFO,
-        //        "Runway -" << tmp_r._rwy_no << " heading = " <<
-        //        tmp_r._heading + 180.0 <<
-        //        " diff = " << diff );
-       if ( diff < min_diff ) {
-           min_diff = diff;
-           r = tmp_r;
-           found_dir = 180.0;
-       }
-       
-        if (!next( &tmp_r ))
+
+    SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
+    double lenwgt = param->getDoubleValue("length-weight", 0.01);
+    double widwgt = param->getDoubleValue("width-weight", 0.01);
+    double surfwgt = param->getDoubleValue("surface-weight", 10);
+    double devwgt = param->getDoubleValue("deviation-weight", 1);
+
+    FGRunway best;
+    double max = 0.0;
+    bool reversed = false;
+
+    do {
+        if (r._id != aptid)
             break;
-    }
-    
-    // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r._rwy_no
-    //        << " + " << found_dir );
-    rn = r._rwy_no;
-    if ( found_dir == 180 ) {
-       rn = GetReverseRunwayNo(rn);
-    }  
-    
-    return rn;
+        if (r._type != "runway")
+            continue;
+
+        int surface = 1;
+        if (r._surface_code == 12 || r._surface_code == 5) // dry lakebed & gravel
+            surface = 2;
+        else if (r._surface_code == 1 || r._surface_code == 2) // asphalt & concrete
+            surface = 3;
+
+        double quality, bad, diff;
+        double good = lenwgt * r._length + widwgt * r._width + surfwgt * surface + 1e-20;
+
+        // this side
+        diff = hdg - r._heading;
+        while (diff < -180)
+            diff += 360;
+        while (diff >= 180)
+            diff -= 360;
+        bad = fabs(devwgt * diff) + 1e-20;
+
+        quality = good / bad;
+        //SG_LOG(SG_GENERAL, SG_ALERT, "  runway " << r._rwy_no <<  " -> " << quality);
+        if (quality > max) {
+            max = quality;
+            best = r;
+            reversed = false;
+        }
+
+        // other side
+        diff = hdg - r._heading - 180;
+        while (diff < -180)
+            diff += 360;
+        while (diff >= 180)
+            diff -= 360;
+        bad = fabs(devwgt * diff) + 1e-20;
+
+        quality = good / bad;
+        //SG_LOG(SG_GENERAL, SG_ALERT, "  runway " << GetReverseRunwayNo(r._rwy_no)
+        //        <<  " -> " << quality);
+        if (quality > max) {
+            max = quality;
+            best = r;
+            reversed = true;
+        }
+
+    } while (next(&r));
+
+    return reversed ? GetReverseRunwayNo(best._rwy_no) : best._rwy_no;
 }