]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/commlist.cxx
If it's a struct, it's not a class
[flightgear.git] / src / ATC / commlist.cxx
index fe52e03afabc510d0ae421dc88f65e41f150d046..4d27e1c794fcde4ccca9f43e182f7306c7a9ee2e 100644 (file)
@@ -3,7 +3,7 @@
 // Written by David Luff and Alexander Kappes, started Jan 2003.
 // Based on navlist.cxx by Curtis Olson, started April 2000.
 //
-// Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
+// Copyright (C) 2000  Curtis L. Olson - http://www.flightgear.org/~curt
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
@@ -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.
 
 
 #ifdef HAVE_CONFIG_H
@@ -50,7 +50,7 @@ FGCommList::~FGCommList( void ) {
 
 
 // load the navaids and build the map
-bool FGCommList::init( SGPath path ) {
+bool FGCommList::init( const SGPath& path ) {
 
        SGPath temp = path;
     commlist_freq.erase(commlist_freq.begin(), commlist_freq.end());
@@ -70,7 +70,7 @@ bool FGCommList::init( SGPath path ) {
 }
        
 
-bool FGCommList::LoadComms(SGPath path) {
+bool FGCommList::LoadComms(const SGPath& path) {
 
     sg_gzifstream fin( path.str() );
     if ( !fin.is_open() ) {
@@ -91,7 +91,7 @@ bool FGCommList::LoadComms(SGPath path) {
         ATCData a;
                fin >> a;
                if(a.type == INVALID) {
-                       SG_LOG(SG_GENERAL, SG_ALERT, "WARNING - INVALID type found in " << path.str() << '\n');
+                       SG_LOG(SG_GENERAL, SG_DEBUG, "WARNING - INVALID type found in " << path.str() << '\n');
                } else {                
                        // Push all stations onto frequency map
                        commlist_freq[a.freq].push_back(a);
@@ -138,6 +138,8 @@ bool FGCommList::FindByFreq( double lon, double lat, double elev, double freq,
        // double az1, az2, s;
        Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
        Point3D station;
+       const double orig_max_d = 1e100; 
+       double max_d = orig_max_d;
        double d;
        // TODO - at the moment this loop returns the first match found in range
        // We want to return the closest match in the event of a frequency conflict
@@ -150,21 +152,28 @@ bool FGCommList::FindByFreq( double lon, double lat, double elev, double freq,
                d = aircraft.distance3Dsquared( station );
                
                //cout << "  dist = " << sqrt(d)
-               //     << "  range = " << current->get_range() * SG_NM_TO_METER << endl;
+               //     << "  range = " << current->range * SG_NM_TO_METER << endl;
                
-               // match up to twice the published range so we can model
+               // TODO - match up to twice the published range so we can model
                // reduced signal strength
-               if ( d < (2 * current->range * SG_NM_TO_METER 
-               * 2 * current->range * SG_NM_TO_METER ) ) {
-                       //cout << "matched = " << current->get_ident() << endl;
+               // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
+               if ( d < (current->range * SG_NM_TO_METER 
+                    * current->range * SG_NM_TO_METER ) ) {
+                       //cout << "matched = " << current->ident << endl;
                        if((tp == INVALID) || (tp == (*current).type)) {
-                               *ad = *current;
-                               return true;
+                               if(d < max_d) {
+                                       max_d = d;
+                                       *ad = *current;
+                               }
                        }
                }
        }
        
-       return false;
+       if(max_d < orig_max_d) {
+               return true;
+       } else {
+               return false;
+       }
 }
 
 int FGCommList::FindByPos(double lon, double lat, double elev, double range, comm_list_type* stations, atc_type tp)
@@ -200,8 +209,9 @@ int FGCommList::FindByPos(double lon, double lat, double elev, double range, com
                                if((current->type == tp) || (tp == INVALID)) {
                                        station = Point3D(current->x, current->y, current->z);
                                        d = aircraft.distance3Dsquared( station );
-                                       if ( d < (current->range * SG_NM_TO_METER 
-                                       * current->range * SG_NM_TO_METER ) ) {
+                                       // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
+                                       if ( d < (current->range * SG_NM_TO_METER
+                                            * current->range * SG_NM_TO_METER ) ) {
                                                stations->push_back(*current);
                                                ++found;
                                        }
@@ -236,8 +246,8 @@ double FGCommList::FindClosest( double lon, double lat, double elev, ATCData& ad
                                ATCData ad2 = *itr;
                                //Point3D p1(*itr.lon, *itr.lat, *itr.elev);
                                Point3D p1(ad2.lon, ad2.lat, ad2.elev);
-                               FGAirport a;
-                               if(dclFindAirportID(ad2.ident, &a)) {
+                               const FGAirport *a = fgFindAirportID(ad2.ident);
+                               if (a) {
                                        Point3D p2(lon, lat, elev);
                                        tmp = dclGetHorizontalSeparation(p1, p2);
                                        if(tmp <= closest) {
@@ -262,11 +272,11 @@ double FGCommList::FindClosest( double lon, double lat, double elev, ATCData& ad
 // Find by Airport code.
 // This is basically a wrapper for a call to the airport database to get the airport
 // position followed by a call to FindByPos(...)
-bool FGCommList::FindByCode( string ICAO, ATCData& ad, atc_type tp ) {
-    FGAirport a;
-    if ( dclFindAirportID( ICAO, &a ) ) {
+bool FGCommList::FindByCode( const string& ICAO, ATCData& ad, atc_type tp ) {
+    const FGAirport *a = fgFindAirportID( ICAO);
+    if ( a) {
                comm_list_type stations;
-               int found = FindByPos(a.longitude, a.latitude, a.elevation, 10.0, &stations, tp);
+               int found = FindByPos(a->getLongitude(), a->getLatitude(), a->getElevation(), 10.0, &stations, tp);
                if(found) {
                        comm_list_iterator itr = stations.begin();
                        while(itr != stations.end()) {
@@ -286,7 +296,7 @@ bool FGCommList::FindByCode( string ICAO, ATCData& ad, atc_type tp ) {
 
 // TODO - this function should move somewhere else eventually!
 // Return an appropriate call-sign for an ATIS transmission.
-int FGCommList::GetCallSign( string apt_id, int hours, int mins )
+int FGCommList::GetCallSign( const string& apt_id, int hours, int mins )
 {
        atis_transmission_type tran;