]> git.mxchange.org Git - flightgear.git/blobdiff - src/Airports/runwayprefs.cxx
Fix for a misplaced boolean assignment that caused erratic runway selection.
[flightgear.git] / src / Airports / runwayprefs.cxx
index f98e965bf5075cf041d90be09281e921c6eaad2a..1a7f1dd117d8f81f20389160dc66f248754e1763 100644 (file)
@@ -17,7 +17,7 @@
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
@@ -25,9 +25,6 @@
 #  include <config.h>
 #endif
 
-#ifdef _MSC_VER
-#  define _USE_MATH_DEFINES
-#endif
 #include <math.h>
 //#include <algorithm>
 
@@ -200,7 +197,8 @@ void RunwayGroup::setActive(const string &aptId,
                            double windSpeed, 
                            double windHeading, 
                            double maxTail, 
-                           double maxCross)
+                           double maxCross,
+                           stringVec *currentlyActive)
 {
 
   FGRunway rwy;
@@ -212,16 +210,31 @@ void RunwayGroup::setActive(const string &aptId,
   double crossWind;
   double tailWind;
   string name;
+  //stringVec names;
+  int bestMatch = 0, bestChoice = 0;
 
   if (activeRwys > 0)
-    {
+    {  
+      // Now downward iterate across all the possible preferences
+      // starting by the least preferred choice working toward the most preferred choice
+
       nrOfPreferences = rwyList[0].getRwyList()->size();
-      for (int i = 0; i < nrOfPreferences; i++)
+      bool validSelection = true;
+      bool foundValidSelection = false;
+      for (int i = nrOfPreferences-1; i >= 0; i--)
        {
-         bool validSelection = true;
+         int match = 0;
+         
+
+         // Test each runway listed in the preference to see if it's possible to use
+         // If one runway of the selection isn't allowed, we need to exclude this
+         // preference, however, we don't want to stop right there, because we also
+         // don't want to randomly swap runway preferences, unless there is a need to. 
+         //
+         validSelection = true;
          for (int j = 0; j < activeRwys; j++)
            {
-             //cerr << "I J " << i << " " << j << endl;
+            
              name = rwyList[j].getRwyList(i);
              //cerr << "Name of Runway: " << name << endl;
              if (globals->get_runways()->search( aptId, 
@@ -241,20 +254,42 @@ void RunwayGroup::setActive(const string &aptId,
                  //cerr << "Tailwind : " << tailWind << endl;
                  //cerr << "Crosswnd : " << crossWind << endl;
                  if ((tailWind > maxTail) || (crossWind > maxCross))
-                   validSelection = false;
+                   {
+                     //cerr << "Invalid : ";
+                     validSelection = false;
+                  }
+                 else 
+                   {
+                     //cerr << "Valid   : ";
+                 }
                }else {
-                 SG_LOG( SG_GENERAL, SG_INFO, "Failed to find runway " << name << " at " << aptId );
-                 exit(1);
-               }
-
+               SG_LOG( SG_GENERAL, SG_INFO, "Failed to find runway " << name << " at " << aptId );
+               exit(1);
+             }
            }
-         if (validSelection)
+         if (validSelection) 
            {
-             //cerr << "Valid runay selection : " << i << endl;
-             nrActive = activeRwys;
-             active = i;
-             return;
-           }
+             //cerr << "Valid   : ";
+             foundValidSelection = true;
+             for (stringVecIterator it = currentlyActive->begin(); 
+                  it != currentlyActive->end(); it++)
+               {
+                 if ((*it) == name)
+                   match++;
+                 if (match >= bestMatch) {
+                   bestMatch = match;
+                   bestChoice = i;
+                 }
+               }
+           } 
+         //cerr << "Preference " << i << " bestMatch " << bestMatch << " choice " << bestChoice << endl;
+       }
+      if (foundValidSelection)
+       {
+         //cerr << "Valid runay selection : " << bestChoice << endl;
+         nrActive = activeRwys;
+         active = bestChoice;
+         return;
        }
       // If this didn't work, due to heavy winds, try again
       // but select only one landing and one takeoff runway. 
@@ -283,16 +318,11 @@ void RunwayGroup::setActive(const string &aptId,
                {
                  //cerr << "Succes" << endl;
                  hdgDiff = fabs(windHeading - rwy._heading);
-                 //cerr << "Wind Heading: " << windHeading << "Runway Heading: " <<rwy._heading << endl;
-                 //cerr << "Wind Speed  : " << windSpeed << endl;
                  if (hdgDiff > 180)
                    hdgDiff = 360 - hdgDiff;
-                 //cerr << "Heading diff: " << hdgDiff << endl;
                  hdgDiff *= ((2*M_PI)/360.0); // convert to radians
                  crossWind = windSpeed * sin(hdgDiff);
                  tailWind  = -windSpeed * cos(hdgDiff);
-                 //cerr << "Tailwind : " << tailWind << endl;
-                 //cerr << "Crosswnd : " << crossWind << endl;
                  if ((tailWind > maxTail) || (crossWind > maxCross))
                    validSelection = false;
                }else {
@@ -311,19 +341,7 @@ void RunwayGroup::setActive(const string &aptId,
        }
     }
   active = -1;
-  //RunwayListVectorIterator i; // = rwlist.begin();
-  //stringVecIterator j;
-  //for (i = rwyList.begin(); i != rwyList.end(); i++)
-  //  {
-  //    cerr << i->getType();
-  //    for (j = i->getRwyList()->begin(); j != i->getRwyList()->end(); j++)
-  //   {                                 
-  //     cerr << (*j);
-  //   }
-  //    cerr << endl;
-  //  }
-  //for (int
-
+  nrActive = 0;
 }
 
 void RunwayGroup::getActive(int i, string &name, string &type)
@@ -548,11 +566,11 @@ void  FGRunwayPreference::pi (const char * target, const char * data) {
 }
 
 void  FGRunwayPreference::warning (const char * message, int line, int column) {
-  cout << "Warning: " << message << " (" << line << ',' << column << ')'   
-       << endl;
+  //cout << "Warning: " << message << " (" << line << ',' << column << ')'   
+  //     << endl;
 }
 
 void  FGRunwayPreference::error (const char * message, int line, int column) {
-  cout << "Error: " << message << " (" << line << ',' << column << ')'
-       << endl;
+  //cout << "Error: " << message << " (" << line << ',' << column << ')'
+  //     << endl;
 }