]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/AIMgr.cxx
Fix the nmea and garmin output to a) fake a GSA sentence, b) fix a y2k bug
[flightgear.git] / src / ATC / AIMgr.cxx
index 0fd68bccf6adf829a838ab32ed1b5e59305fa1e5..d44a845114339721d29772b4f2e48a984933ccfa 100644 (file)
 // along with this program; if not, write to the Free Software
 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-#include <Airports/simple.hxx>
-#include <Main/fgfs.hxx>
+#include <simgear/misc/sg_path.hxx>
+
 #include <Main/fg_props.hxx>
 #include <Main/globals.hxx>
-#include <simgear/misc/sg_path.hxx>
-#include <simgear/bucket/newbucket.hxx>
 
 #include <list>
 
@@ -44,6 +42,7 @@ SG_USING_STD(cout);
 
 FGAIMgr::FGAIMgr() {
        ATC = globals->get_ATC_mgr();
+       initDone = false;
 }
 
 FGAIMgr::~FGAIMgr() {
@@ -54,6 +53,10 @@ void FGAIMgr::init() {
        lon_node = fgGetNode("/position/longitude-deg", true);
        lat_node = fgGetNode("/position/latitude-deg", true);
        elev_node = fgGetNode("/position/altitude-ft", true);
+
+       lon = lon_node->getDoubleValue();
+       lat = lat_node->getDoubleValue();
+       elev = elev_node->getDoubleValue();
        
        // go through the $FG_ROOT/ATC directory and find all *.taxi files
        SGPath path(globals->get_fg_root());
@@ -72,7 +75,7 @@ void FGAIMgr::init() {
        path_str = dir + "\\*.*";
        
        if ( ( hfile = _findfirst( path.c_str(), &de ) ) == -1 ) {
-               cout << "cannot open directory " << dir << "\n";
+               SG_LOG(SG_ATC, SG_WARN, "cannot open directory " << dir);
        } else {                
                // load all .taxi files
                do {
@@ -80,7 +83,6 @@ void FGAIMgr::init() {
                        pos = file.find(".");
                        ext = file.substr(pos + 1);
                        if(ext == "taxi") {
-                               cout << "TAXI FILE FOUND!!!\n";
                                f_ident = file.substr(0, pos);
                                FGAirport a;
                                if(dclFindAirportID(f_ident, &a)) {
@@ -93,7 +95,7 @@ void FGAIMgr::init() {
                                                apts->push_back(f_ident);
                                                airports[idx] = apts;
                                        }
-                                       cout << "Mapping " << f_ident << " to bucket " << idx << '\n'
+                                       SG_LOG(SG_ATC, SG_BULK, "Mapping " << f_ident << " to bucket " << idx)
                                }
                        }
                } while ( _findnext( hfile, &de ) == 0 );
@@ -104,19 +106,14 @@ void FGAIMgr::init() {
     struct dirent *de;
 
     if ( (d = opendir( dir.c_str() )) == NULL ) {
-               cout << "cannot open directory " << dir << "\n";
+               SG_LOG(SG_ATC, SG_WARN, "cannot open directory " << dir);
        } else {
-               cout << "Opened directory " << dir << " OK :-)\n";
-               cout << "Contents are:\n";
                // load all .taxi files
                while ( (de = readdir(d)) != NULL ) {
                        file = de->d_name;
                        pos = file.find(".");
-                       cout << file << '\n';
-
                        ext = file.substr(pos + 1);
                        if(ext == "taxi") {
-                               cout << "TAXI FILE FOUND!!!\n";
                                f_ident = file.substr(0, pos);
                                FGAirport a;
                                if(dclFindAirportID(f_ident, &a)) {
@@ -129,7 +126,7 @@ void FGAIMgr::init() {
                                                apts->push_back(f_ident);
                                                airports[idx] = apts;
                                        }
-                                       cout << "Mapping " << f_ident << " to bucket " << idx << '\n'; 
+                                       SG_LOG(SG_ATC, SG_BULK, "Mapping " << f_ident << " to bucket " << idx);
                                }
                        }
                }               
@@ -139,6 +136,8 @@ void FGAIMgr::init() {
        
        // See if are in range at startup and activate if necessary
        SearchByPos(10.0);
+       
+       initDone = true;
 }
 
 void FGAIMgr::bind() {
@@ -148,6 +147,11 @@ void FGAIMgr::unbind() {
 }
 
 void FGAIMgr::update(double dt) {
+       if(!initDone) {
+               init();
+               SG_LOG(SG_ATC, SG_WARN, "Warning - AIMgr::update(...) called before AIMgr::init()");
+       }
+       
        static int i = 0;
        static int j = 0;
 
@@ -195,7 +199,7 @@ void FGAIMgr::ActivateAirport(string ident) {
 // Search for valid airports in the vicinity of the user and activate them if necessary
 void FGAIMgr::SearchByPos(double range)
 {
-       //cout << "In SearchByPos(...)\n";
+       //cout << "In SearchByPos(...)" << endl;
        
        // get bucket number for plane position
        lon = lon_node->getDoubleValue();
@@ -204,9 +208,9 @@ void FGAIMgr::SearchByPos(double range)
 
        // get neigboring buckets
        int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2);
-       //cout << "bx = " << bx << '\n';
+       //cout << "bx = " << bx << endl;
        int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
-       //cout << "by = " << by << '\n';
+       //cout << "by = " << by << endl;
        
        // loop over bucket range 
        for ( int i=-bx; i<=bx; i++) {
@@ -215,28 +219,31 @@ void FGAIMgr::SearchByPos(double range)
                        //cout << "j loop\n";
                        buck = sgBucketOffset(lon, lat, i, j);
                        long int bucket = buck.gen_index();
-                       //cout << "bucket is " << bucket << '\n';
-                       aptID_list_type* apts = airports[bucket];
-                       aptID_list_iterator current = apts->begin();
-                       aptID_list_iterator last = apts->end();
-                       
-                       //cout << "Size of apts is " << apts->size() << '\n';
-                       
-                       //double rlon = lon * SGD_DEGREES_TO_RADIANS;
-                       //double rlat = lat * SGD_DEGREES_TO_RADIANS;
-                       //Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
-                       //Point3D airport;
-                       for(; current != last; ++current) {
-                               //cout << "Found " << *current << '\n';
-                               if(activated.find(*current) == activated.end()) {
-                                       //cout << "Activating " << *current << '\n';
-                                       //FGAirport a;
-                                       //if(dclFindAirportID(*current, &a)) {
-                                       //      // We can do something here based on distance from the user if we wish.
-                                       //}
-                                       ActivateAirport(*current);
-                               } else {
-                                       //cout << *current << " already activated\n";
+                       //cout << "bucket is " << bucket << endl;
+                       if(airports.find(bucket) != airports.end()) {
+                               aptID_list_type* apts = airports[bucket];
+                               aptID_list_iterator current = apts->begin();
+                               aptID_list_iterator last = apts->end();
+                               
+                               //cout << "Size of apts is " << apts->size() << endl;
+                               
+                               //double rlon = lon * SGD_DEGREES_TO_RADIANS;
+                               //double rlat = lat * SGD_DEGREES_TO_RADIANS;
+                               //Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
+                               //Point3D airport;
+                               for(; current != last; ++current) {
+                                       //cout << "Found " << *current << endl;;
+                                       if(activated.find(*current) == activated.end()) {
+                                               //cout << "Activating " << *current << endl;
+                                               //FGAirport a;
+                                               //if(dclFindAirportID(*current, &a)) {
+                                                       //      // We can do something here based on distance from the user if we wish.
+                                               //}
+                                               ActivateAirport(*current);
+                                               //cout << "Activation done" << endl;
+                                       } else {
+                                               //cout << *current << " already activated" << endl;
+                                       }
                                }
                        }
                }