]> git.mxchange.org Git - flightgear.git/blobdiff - src/Traffic/TrafficMgr.cxx
don't destroy iterated map entries; delete _menubar; restore closed
[flightgear.git] / src / Traffic / TrafficMgr.cxx
index ca663a8d2b00153c6d8902c4482f49182a437428..1e3b73a2ce8cc034f917e6790edd0e333e164fd9 100644 (file)
 
 #include <string>
 #include <vector>
+#include <algorithm>
 
 #include <plib/sg.h>
 
 #include <simgear/compiler.h>
 #include <simgear/math/polar3d.hxx>
 #include <simgear/math/sg_geodesy.hxx>
+#include <simgear/misc/sg_path.hxx>
 #include <simgear/props/props.hxx>
 #include <simgear/route/waypoint.hxx>
 #include <simgear/structure/subsystem_mgr.hxx>
 #include <simgear/xml/easyxml.hxx>
 
+#include <AIModel/AIAircraft.hxx>
 #include <AIModel/AIFlightPlan.hxx>
 #include <AIModel/AIBase.hxx>
 #include <Airports/simple.hxx>
@@ -61,6 +64,7 @@
 
 #include "TrafficMgr.hxx"
 
+SG_USING_STD(sort);
  
 /******************************************************************************
  * TrafficManager
@@ -71,30 +75,53 @@ FGTrafficManager::FGTrafficManager()
 
 
 void FGTrafficManager::init()
-{
+{ 
+  //cerr << "Initializing Schedules" << endl;
+  time_t now = time(NULL) + fgGetLong("/sim/time/warp");
+  currAircraft = scheduledAircraft.begin();
+  while (currAircraft != scheduledAircraft.end())
+    {
+      if (!(currAircraft->init()))
+       {
+         currAircraft=scheduledAircraft.erase(currAircraft);
+         //cerr << "Erasing " << currAircraft->getRegistration() << endl;
+         currAircraft--;
+       }
+      else 
+       {
+         currAircraft++;
+       }
+    }
+  //cerr << "Sorting by distance " << endl;
+  sort(scheduledAircraft.begin(), scheduledAircraft.end());
   currAircraft = scheduledAircraft.begin();
+  currAircraftClosest = scheduledAircraft.begin();
+  //cerr << "Done initializing schedules" << endl;
 }
 
 void FGTrafficManager::update(double something)
 {
-
-  //static const SGPropertyNode *warp = globals->get_props()->getNode("/sim/time/warp");
-  
-  //time_t now = time(NULL) + globals->get_warp();
   time_t now = time(NULL) + fgGetLong("/sim/time/warp");
-  //  cerr << "TrafficManager update" << globals->get_warp() << endl;
-    if(currAircraft == scheduledAircraft.end())
+  if(currAircraft == scheduledAircraft.end())
+    {
+      //cerr << "resetting schedule " << endl;
       currAircraft = scheduledAircraft.begin();
-    currAircraft->update(now);
-    currAircraft++;
+    }
+  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;
+    }
+  currAircraft++;
 }
 
-void FGTrafficManager::release(void *id)
+void FGTrafficManager::release(int id)
 {
   releaseList.push_back(id);
 }
 
-bool FGTrafficManager::isReleased(void *id)
+bool FGTrafficManager::isReleased(int id)
 {
   IdListIterator i = releaseList.begin();
   while (i != releaseList.end())
@@ -127,11 +154,11 @@ void  FGTrafficManager::startElement (const char * name, const XMLAttributes &at
   if (attval != 0)
       {
        //cout << "including " << attval << endl;
-       string path = 
-         globals->get_fg_root()   + 
-         string("/Traffic/")      + 
-         string(attval);
-       readXML(path, *this);
+       SGPath path = 
+         globals->get_fg_root();
+       path.append("/Traffic/");
+       path.append(attval);
+       readXML(path.str(), *this);
       }
   //  cout << "  " << atts.getName(i) << '=' << atts.getValue(i) << endl; 
 }
@@ -145,6 +172,18 @@ void  FGTrafficManager::endElement (const char * name) {
     livery = value;
   else if (element == string("registration"))
     registration = value;
+  else if (element == string("airline"))
+    airline = value;
+  else if (element == string("actype"))
+    acType = value;
+  else if (element == string("flighttype"))
+    flighttype = value;
+  else if (element == string("radius"))
+    radius = atoi(value.c_str());
+  else if (element == string("offset"))
+    offset = atoi(value.c_str());
+  else if (element == string("performance-class"))
+    m_class = value;
   else if (element == string("heavy"))
     {
       if(value == string("true"))
@@ -194,10 +233,16 @@ void  FGTrafficManager::endElement (const char * name) {
     {
       //cerr << "Pushing back aircraft " << registration << endl;
       scheduledAircraft.push_back(FGAISchedule(mdl, 
-                                               livery, 
-                                               registration, 
-                                               heavy, 
-                                               flights));
+                                              livery, 
+                                              registration, 
+                                              heavy,
+                                              acType, 
+                                              airline, 
+                                              m_class, 
+                                              flighttype,
+                                              radius,
+                                              offset,
+                                              flights));
       while(flights.begin() != flights.end())
        flights.pop_back();
     }