]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/commlist.cxx
Mathias Fröhlich:
[flightgear.git] / src / ATC / commlist.cxx
index eb0201e9213a7ecade8eefc5ba6c06152931e9d6..18e17173038dafa075ae6e54f1764bed92bb7b65 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
@@ -61,6 +61,9 @@ bool FGCommList::init( SGPath path ) {
        temp.append( "ATC/default.tower" );
        LoadComms(temp);
        temp = path;
+       temp.append( "ATC/default.ground" );
+       LoadComms(temp);
+       temp = path;
        temp.append( "ATC/default.approach" );
        LoadComms(temp);
        return true;
@@ -88,18 +91,19 @@ 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);
                        
                        // Push non-atis stations onto bucket map as well
-                       if(a.type != ATIS) {
+                       // In fact, push all stations onto bucket map for now so FGATCMgr::GetFrequency() works.
+                       //if(a.type != ATIS) {
                                // get bucket number
                                SGBucket bucket(a.lon, a.lat);
                                int bucknum = bucket.gen_index();
                                commlist_bck[bucknum].push_back(a);
-                       }
+                       //}
                }
                
                fin >> skipcomment;
@@ -134,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
@@ -146,24 +152,31 @@ 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, comm_list_type* stations, atc_type tp)
+int FGCommList::FindByPos(double lon, double lat, double elev, double range, comm_list_type* stations, atc_type tp)
 {
        // number of relevant stations found within range
        int found = 0;
@@ -173,13 +186,12 @@ int FGCommList::FindByPos(double lon, double lat, double elev, comm_list_type* s
        SGBucket buck(lon, lat);
 
        // get neigboring buckets
-       int max_range = 100;
-       int bx = (int)( max_range*SG_NM_TO_METER / buck.get_width_m() / 2);
-       int by = (int)( max_range*SG_NM_TO_METER / buck.get_height_m() / 2 );
+       int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2);
+       int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
        
        // loop over bucket range 
-       for ( int i=-bx; i<bx; i++) {
-               for ( int j=-by; j<by; j++) {
+       for ( int i=-bx; i<=bx; i++) {
+               for ( int j=-by; j<=by; j++) {
                        buck = sgBucketOffset(lon, lat, i, j);
                        long int bucket = buck.gen_index();
                        comm_list_type Fstations = commlist_bck[bucket];
@@ -197,8 +209,9 @@ int FGCommList::FindByPos(double lon, double lat, double elev, comm_list_type* s
                                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;
                                        }
@@ -210,6 +223,52 @@ int FGCommList::FindByPos(double lon, double lat, double elev, comm_list_type* s
 }
 
 
+// Returns the distance in meters to the closest station of a given type,
+// with the details written into ATCData& ad.  If no type is specifed simply
+// returns the distance to the closest station of any type.
+// Returns -9999 if no stations found within max_range in nautical miles (default 100 miles).
+// Note that the search algorithm starts at 10 miles and multiplies by 10 thereafter, so if
+// say 300 miles is specifed 10, then 100, then 1000 will be searched, breaking at first result 
+// and giving up after 1000.
+double FGCommList::FindClosest( double lon, double lat, double elev, ATCData& ad, atc_type tp, double max_range) {
+       int num_stations = 0;
+       int range = 10;
+       comm_list_type stations;
+       comm_list_iterator itr;
+       double distance = -9999.0;
+       
+       while(num_stations == 0) {
+               num_stations = FindByPos(lon, lat, elev, range, &stations, tp);
+               if(num_stations) {
+                       double closest = max_range * SG_NM_TO_METER;
+                       double tmp;
+                       for(itr = stations.begin(); itr != stations.end(); ++itr) {     
+                               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)) {
+                                       Point3D p2(lon, lat, elev);
+                                       tmp = dclGetHorizontalSeparation(p1, p2);
+                                       if(tmp <= closest) {
+                                               closest = tmp;
+                                               distance = tmp;
+                                               ad = *itr;
+                                       }
+                               }
+                       }
+                       //cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
+                       return(distance);
+               }
+               if(range > max_range) {
+                       break;
+               }
+               range *= 10;
+       }
+       return(-9999.0);
+}
+
+
 // 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(...)
@@ -217,7 +276,7 @@ bool FGCommList::FindByCode( string ICAO, ATCData& ad, atc_type tp ) {
     FGAirport a;
     if ( dclFindAirportID( ICAO, &a ) ) {
                comm_list_type stations;
-               int found = FindByPos(a.longitude, a.latitude, a.elevation, &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()) {
@@ -225,6 +284,7 @@ bool FGCommList::FindByCode( string ICAO, ATCData& ad, atc_type tp ) {
                                        ad = *itr;
                                        return true;
                                }
+                               ++itr;
                        }
                }
     } else {