]> git.mxchange.org Git - flightgear.git/blobdiff - src/Traffic/TrafficMgr.cxx
Make sure 'make dist' keeps working.
[flightgear.git] / src / Traffic / TrafficMgr.cxx
index 43cce5f746a7f1cf5a9dfaf745f191e6e0c6f107..3a8eb5513cdc5fd180927dbbd8cb7b078d2aea4e 100644 (file)
@@ -49,6 +49,7 @@
 #include <algorithm>
 
 #include <plib/sg.h>
+#include <plib/ul.h>
 
 #include <simgear/compiler.h>
 #include <simgear/math/polar3d.hxx>
@@ -69,7 +70,7 @@
 
 #include "TrafficMgr.hxx"
 
-SG_USING_STD(sort);
+using std::sort;
  
 /******************************************************************************
  * TrafficManager
@@ -77,6 +78,7 @@ SG_USING_STD(sort);
 FGTrafficManager::FGTrafficManager()
 {
   score = 0;
+  runCount = 0;
 }
 
 FGTrafficManager:: ~FGTrafficManager()
@@ -113,29 +115,76 @@ void FGTrafficManager::init()
   //   }
   // Sort by points: Aircraft with frequent visits to the
   // startup airport will be processed first
+  ulDir* d, *d2;
+  ulDirEnt* dent, *dent2;
+  SGPath aircraftDir = globals->get_fg_root();
+
+  /* keep the following three lines (which mimicks the old "fixed path" behavior)
+   * until we have some AI models with traffic in the base package
+   */ 
+  SGPath path = aircraftDir;
+  path.append("Traffic/fgtraffic.xml");
+  if (path.exists())
+    readXML(path.str(),*this);
+
+  aircraftDir.append("AI/Aircraft");
+  if ((d = ulOpenDir(aircraftDir.c_str())) != NULL)
+    {
+      while((dent = ulReadDir(d)) != NULL) {
+       //cerr << "Scanning : " << dent->d_name << endl;
+       if (string(dent->d_name) != string(".")  && 
+           string(dent->d_name) != string("..") &&
+           dent->d_isdir)
+         {
+           SGPath currACDir = aircraftDir;
+           currACDir.append(dent->d_name);
+           if ((d2 = ulOpenDir(currACDir.c_str())) == NULL)
+             return;
+           while ((dent2 = ulReadDir(d2)) != NULL) {
+             SGPath currFile = currACDir;
+             currFile.append(dent2->d_name);
+             if (currFile.extension() == string("xml"))
+               {
+                 //cerr << "found " << dent2->d_name << " for parsing" << endl;
+                 SGPath currFile = currACDir;
+                 currFile.append(dent2->d_name);
+                 SG_LOG(SG_GENERAL, SG_INFO, "Scanning " << currFile.str() << " for traffic");
+                 readXML(currFile.str(),*this);
+               }
+           }
+           ulCloseDir(d2);
+         }
+      }
+      ulCloseDir(d);
+    }
+  // Sort by points: Aircraft with frequent visits to the
+  // startup airport will be processed first
   sort(scheduledAircraft.begin(), scheduledAircraft.end(), compareSchedules);
   currAircraft = scheduledAircraft.begin();
   currAircraftClosest = scheduledAircraft.begin();
   //cerr << "Done initializing schedules" << endl;
 }
 
-void FGTrafficManager::update(double something)
+void FGTrafficManager::update(double /*dt*/)
 {
+
   time_t now = time(NULL) + fgGetLong("/sim/time/warp");
-  if (scheduledAircraft.size() == 0)
+  if (scheduledAircraft.size() == 0) {
+    //SG_LOG( SG_GENERAL, SG_INFO, "Returned Running TrafficManager::Update() ");
     return;
+  }
   if(currAircraft == scheduledAircraft.end())
     {
-      //cerr << "resetting schedule " << endl;
       currAircraft = scheduledAircraft.begin();
     }
   if (!((*currAircraft)->update(now)))
     {
       // after proper initialization, we shouldnt get here.
       // But let's make sure
-      cerr << "Failed to update aircraft schedule in traffic manager" << endl;
+      SG_LOG( SG_GENERAL, SG_ALERT, "Failed to update aircraft schedule in traffic manager");
     }
   currAircraft++;
+  //SG_LOG( SG_GENERAL, SG_INFO, "Done Running TrafficManager::Update() ");
 }
 
 void FGTrafficManager::release(int id)
@@ -182,12 +231,16 @@ void  FGTrafficManager::startElement (const char * name, const XMLAttributes &at
        path.append(attval);
        readXML(path.str(), *this);
       }
+  elementValueStack.push_back( "" );
   //  cout << "  " << atts.getName(i) << '=' << atts.getValue(i) << endl; 
 }
 
 void  FGTrafficManager::endElement (const char * name) {
   //cout << "End element " << name << endl;
   string element(name);
+  string value = elementValueStack.back();
+  elementValueStack.pop_back();
+
   if (element == string("model"))
     mdl = value;
   else if (element == string("livery"))
@@ -258,8 +311,10 @@ void  FGTrafficManager::endElement (const char * name) {
     }
   else if (element == string("aircraft"))
     {
-      //cerr << "Pushing back aircraft " << registration << endl;
-      scheduledAircraft.push_back(new FGAISchedule(mdl, 
+      int proportion = (int) (fgGetDouble("/sim/traffic-manager/proportion") * 100);
+      int randval = rand() & 100;
+      if (randval < proportion) {
+          scheduledAircraft.push_back(new FGAISchedule(mdl, 
                                               livery, 
                                               registration, 
                                               heavy,
@@ -274,7 +329,8 @@ void  FGTrafficManager::endElement (const char * name) {
      //  while(flights.begin() != flights.end()) {
 //     flights.pop_back();
 //       }
-      for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
+      }
+  for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
     {
       delete (*flt);
     }
@@ -290,10 +346,7 @@ void  FGTrafficManager::endElement (const char * name) {
 void  FGTrafficManager::data (const char * s, int len) {
   string token = string(s,len);
   //cout << "Character data " << string(s,len) << endl;
-  if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
-      value += token;
-  else
-    value = string("");
+  elementValueStack.back() += token;
 }
 
 void  FGTrafficManager::pi (const char * target, const char * data) {