]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/AIMgr.cxx
Migrate FlightGear code to use "#include SG_GL*" defined in
[flightgear.git] / src / ATC / AIMgr.cxx
index 2c2a1bf569f11898e86e1bffb64b5f17508d7206..5a9d3f6d31dc258a2b51f440776cb3eb46b38129 100644 (file)
 #  include <dirent.h>          // for directory reading
 #endif
 
-#ifdef FG_WEATHERCM
-# include <WeatherCM/FGLocalWeatherDatabase.h>
-#else
-# include <Environment/environment_mgr.hxx>
-# include <Environment/environment.hxx>
-#endif
+#include <Environment/environment_mgr.hxx>
+#include <Environment/environment.hxx>
 
 #include "AIMgr.hxx"
 #include "AILocalTraffic.hxx"
@@ -60,6 +56,8 @@ FGAIMgr::FGAIMgr() {
 }
 
 FGAIMgr::~FGAIMgr() {
+       _defaultModel->deRef();
+       _piperModel->deRef();
 }
 
 void FGAIMgr::init() {
@@ -78,16 +76,20 @@ void FGAIMgr::init() {
        // Hack alert - Hardwired paths!!
        string planepath = "Aircraft/c172/Models/c172-dpm.ac";
        _defaultModel = sgLoad3DModel( globals->get_fg_root(),
-                                         planepath.c_str(),
-                                         globals->get_props(),
-                                         globals->get_sim_time_sec() );
-                                                                         
+                                      planepath.c_str(),
+                                      globals->get_props(),
+                                      globals->get_sim_time_sec() );
+
        planepath = "Aircraft/pa28-161/Models/pa28-161.ac";
        _piperModel = sgLoad3DModel( globals->get_fg_root(),
-                                         planepath.c_str(),
-                                         globals->get_props(),
-                                         globals->get_sim_time_sec() );
-                                                                         
+                                    planepath.c_str(),
+                                    globals->get_props(),
+                                    globals->get_sim_time_sec() );
+
+       // We need to keep one ref of the models open to stop ssg deleting them behind our back!
+       _defaultModel->ref();
+       _piperModel->ref();
+
        // go through the $FG_ROOT/ATC directory and find all *.taxi files
        SGPath path(globals->get_fg_root());
        path.append("ATC/");
@@ -96,50 +98,14 @@ void FGAIMgr::init() {
        string file, f_ident;
        int pos;
        
-       // WARNING - I (DCL) haven't tested this on MSVC - this is simply cribbed from TerraGear
-#ifdef _MSC_VER        
-       long hfile;
-       struct _finddata_t de;
-       string path_str;
-       
-       path_str = dir + "\\*.*";
+       ulDir *d;
+       struct ulDirEnt *de;
        
-       if ( ( hfile = _findfirst( path.c_str(), &de ) ) == -1 ) {
-               SG_LOG(SG_ATC, SG_WARN, "cannot open directory " << dir);
-       } else {                
-               // load all .taxi files
-               do {
-                       file = de.name;
-                       pos = file.find(".");
-                       ext = file.substr(pos + 1);
-                       if(ext == "taxi") {
-                               f_ident = file.substr(0, pos);
-                               FGAirport a;
-                               if(dclFindAirportID(f_ident, &a)) {
-                                       SGBucket sgb(a.longitude, a.latitude);
-                                       int idx = sgb.gen_index();
-                                       if(facilities.find(idx) != facilities.end()) {
-                                               facilities[idx]->push_back(f_ident);
-                                       } else {
-                                               ID_list_type* apts = new ID_list_type;
-                                               apts->push_back(f_ident);
-                                               facilities[idx] = apts;
-                                       }
-                                       SG_LOG(SG_ATC, SG_BULK, "Mapping " << f_ident << " to bucket " << idx); 
-                               }
-                       }
-               } while ( _findnext( hfile, &de ) == 0 );
-       }
-#else
-
-    DIR *d;
-    struct dirent *de;
-
-    if ( (d = opendir( dir.c_str() )) == NULL ) {
+       if ( (d = ulOpenDir( dir.c_str() )) == NULL ) {
                SG_LOG(SG_ATC, SG_WARN, "cannot open directory " << dir);
        } else {
                // load all .taxi files
-               while ( (de = readdir(d)) != NULL ) {
+               while ( (de = ulReadDir(d)) != NULL ) {
                        file = de->d_name;
                        pos = file.find(".");
                        ext = file.substr(pos + 1);
@@ -160,10 +126,9 @@ void FGAIMgr::init() {
                                }
                        }
                }               
-               closedir(d);
+               ulCloseDir(d);
        }
-#endif
-       
+
        // See if are in range at startup and activate if necessary
        SearchByPos(15.0);
        
@@ -258,7 +223,7 @@ void FGAIMgr::update(double dt) {
                                bool gen = false;
                                //cout << "Size of list is " << (*it).second.size() << " at " << s << '\n';
                                if((*it).second.size()) {
-                                       FGAIEntity* e = *((*it).second.rbegin());
+                                       FGAIEntity* e = *((*it).second.rbegin());       // Get the last airplane currently scheduled to arrive at this airport.
                                        cd = dclGetHorizontalSeparation(e->GetPos(), dclGetAirportPos(s));
                                        if(cd < (d < 5000 ? 10000 : d + 5000)) {
                                                gen = true;
@@ -270,7 +235,8 @@ void FGAIMgr::update(double dt) {
                                if(gen) {
                                        //cout << "Generating extra traffic at airport " << s << ", at least " << cd << " meters out\n";
                                        //GenerateSimpleAirportTraffic(s, cd);
-                                       GenerateSimpleAirportTraffic(s, cd + 2000.0);   // The random seems a bit wierd - traffic could get far too bunched without the +2000.
+                                       GenerateSimpleAirportTraffic(s, cd + 3000.0);   // The random seems a bit wierd - traffic could get far too bunched without the +3000.
+                                       // TODO - make the anti-random constant variable depending on the ai-traffic level.
                                }
                        }
                        ++it;
@@ -379,19 +345,10 @@ void FGAIMgr::GenerateSimpleAirportTraffic(string ident, double min_dist) {
        
        // Check that the visibility is OK for IFR operation.
        double visibility;
-       #ifdef FG_WEATHERCM
-       //sgVec3 position = { aptpos.lat(), aptpos.lon(), aptpos.elev() };
-       //FGPhysicalProperty stationweather = WeatherDatabase->get(position);
-       #else
        FGEnvironment stationweather =
             ((FGEnvironmentMgr *)globals->get_subsystem("environment"))
               ->getEnvironment(aptpos.lat(), aptpos.lon(), aptpos.elev());     // TODO - check whether this should take ft or m for elev.
-       #endif
-       #ifdef FG_WEATHERCM
-       visibility = fgGetDouble("/environment/visibility-m");
-       #else
        visibility = stationweather.get_visibility_m();
-       #endif
        // Technically we can do VFR down to 1 mile (1600m) but that's pretty murky!
        //cout << "vis = " << visibility << '\n';
        if(visibility < 3000) return;
@@ -486,6 +443,7 @@ void FGAIMgr::GenerateSimpleAirportTraffic(string ident, double min_dist) {
                        double dir = int(sg_random() * 36);
                        if(dir == 36) dir--;
                        dir *= 10;
+                       
                        if(sg_random() < 0.3) cessna = false;
                        else cessna = true;
                        string s = GenerateShortForm(GenerateUniqueCallsign(), (cessna ? "Cessna-" : "Piper-"));