]> git.mxchange.org Git - flightgear.git/blobdiff - src/Traffic/Schedule.cxx
Merge branch 'tat/configure' into next
[flightgear.git] / src / Traffic / Schedule.cxx
index 1aae3e683f14e81fcd91840014907ea6b7862b4f..b5297301a1470ae71fefc783b5079a8f28ab294a 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  *
  *
  ****************************************************************************
  *
  *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#define BOGUS 0xFFFF
+
 #include <stdlib.h>
 #include <time.h>
 #include <iostream>
 #include <vector>
 #include <algorithm>
 
-#include <plib/sg.h>
-
 #include <simgear/compiler.h>
-#include <simgear/math/polar3d.hxx>
+#include <simgear/sg_inlines.h>
 #include <simgear/math/sg_geodesy.hxx>
 #include <simgear/props/props.hxx>
 #include <simgear/route/waypoint.hxx>
@@ -42,6 +47,7 @@
 
 #include <AIModel/AIFlightPlan.hxx>
 #include <AIModel/AIManager.hxx>
+#include <AIModel/AIAircraft.hxx>
 #include <Airports/simple.hxx>
 #include <Main/fg_init.hxx>   // That's pretty ugly, but I need fgFindAirportID
 
@@ -49,7 +55,7 @@
 #include "SchedFlight.hxx"
 #include "TrafficMgr.hxx"
 
-SG_USING_STD( sort );
+using std::sort;
 
 /******************************************************************************
  * the FGAISchedule class contains data members and code to maintain a
@@ -66,8 +72,10 @@ FGAISchedule::FGAISchedule()
   radius = 0;
   groundOffset = 0;
   distanceToUser = 0;
+  //score = 0;
 }
 
+/*
 FGAISchedule::FGAISchedule(string    mdl, 
                           string    liv, 
                           string    reg, 
@@ -78,53 +86,78 @@ FGAISchedule::FGAISchedule(string    mdl,
                           string fltpe,
                           double rad,
                           double grnd,
-                          FGScheduledFlightVec flt)
+                          int    scre,
+                          FGScheduledFlightVec flt)*/
+FGAISchedule::FGAISchedule(string model, 
+                           string lvry,
+                           string port, 
+                           string reg, 
+                           string flightId,
+                           bool   hvy, 
+                           string act, 
+                           string arln, 
+                           string mclass, 
+                           string fltpe, 
+                           double rad, 
+                           double grnd)
 {
-  modelPath    = mdl; 
-  livery       = liv; 
-  registration = reg;
-  acType       = act;
-  airline      = arln;
-  m_class      = mclass;
-  flightType   = fltpe;
-  lat = 0;
-  lon = 0;
-  radius       = rad;
-  groundOffset = grnd;
-  distanceToUser = 0;
-  heavy = hvy;
-  for (FGScheduledFlightVecIterator i = flt.begin();
+  modelPath        = model; 
+  livery           = lvry; 
+  homePort         = port;
+  registration     = reg;
+  flightIdentifier = flightId;
+  acType           = act;
+  airline          = arln;
+  m_class          = mclass;
+  flightType       = fltpe;
+  lat              = 0;
+  lon              = 0;
+  radius           = rad;
+  groundOffset     = grnd;
+  distanceToUser   = 0;
+  heavy            = hvy;
+  /*for (FGScheduledFlightVecIterator i = flt.begin();
        i != flt.end();
        i++)
-    flights.push_back(FGScheduledFlight((*i)));
-  AIManagerRef = 0;
-  firstRun = true;
+    flights.push_back(new FGScheduledFlight((*(*i))));*/
+  AIManagerRef     = 0;
+  //score    = scre;
+  firstRun         = true;
 }
 
 FGAISchedule::FGAISchedule(const FGAISchedule &other)
 {
-  modelPath    = other.modelPath;
-  livery       = other.livery;
-  registration = other.registration;
-  heavy        = other.heavy;
-  flights      = other.flights;
-  lat          = other.lat;
-  lon          = other.lon;
-  AIManagerRef = other.AIManagerRef;
-  acType       = other.acType;
-  airline      = other.airline;
-  m_class      = other.m_class;
-  firstRun     = other.firstRun;
-  radius       = other.radius;
-  groundOffset = other.groundOffset;
-  flightType   = other.flightType;
-  distanceToUser = other.distanceToUser;
+  modelPath          = other.modelPath;
+  homePort           = other.homePort;
+  livery             = other.livery;
+  registration       = other.registration;
+  heavy              = other.heavy;
+  flightIdentifier   = other.flightIdentifier;
+  flights            = other.flights;
+  lat                = other.lat;
+  lon                = other.lon;
+  AIManagerRef       = other.AIManagerRef;
+  acType             = other.acType;
+  airline            = other.airline;
+  m_class            = other.m_class;
+  firstRun           = other.firstRun;
+  radius             = other.radius;
+  groundOffset       = other.groundOffset;
+  flightType         = other.flightType;
+  //score            = other.score;
+  distanceToUser     = other.distanceToUser;
+  currentDestination = other.currentDestination;
+  firstRun           = other.firstRun;
 }
 
 
 FGAISchedule::~FGAISchedule()
 {
-  
+/*  for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
+    {
+      delete (*flt);
+    }
+  flights.clear();*/
 } 
 
 bool FGAISchedule::init()
@@ -137,43 +170,37 @@ bool FGAISchedule::init()
   //sgTimeFormatTime(&targetTimeDate, buffer);
   //cout << "Scheduled Time " << buffer << endl; 
   //cout << "Time :" << time(NULL) << " SGTime : " << sgTimeGetGMT(temp) << endl;
-  for (FGScheduledFlightVecIterator i = flights.begin(); 
+  /*for (FGScheduledFlightVecIterator i = flights.begin(); 
        i != flights.end(); 
        i++)
     {
       //i->adjustTime(now);
-      if (!(i->initializeAirports()))
+      if (!((*i)->initializeAirports()))
        return false;
-    } 
+    } */
   //sort(flights.begin(), flights.end());
   // Since time isn't initialized yet when this function is called,
   // Find the closest possible airport.
   // This should give a reasonable initialization order. 
-  setClosestDistanceToUser();
+  //setClosestDistanceToUser();
   return true;
 }
 
 bool FGAISchedule::update(time_t now)
-{
+{ 
   FGAirport *dep;
   FGAirport *arr;
-  sgdVec3 a, b, cross;
-  sgdVec3 newPos;
-  sgdMat4 matrix;
   double angle;
 
   FGAIManager *aimgr;
   string airport;
-  
-  double courseToUser,   courseToDest;
-  double distanceToDest;
+
   double speed;
 
-  Point3D temp;
   time_t 
     totalTimeEnroute, 
     elapsedTimeEnroute,
-    remainingTimeEnroute;
+    remainingTimeEnroute, deptime = 0;
   double
     userLatitude,
     userLongitude;
@@ -182,35 +209,55 @@ bool FGAISchedule::update(time_t now)
     return true;
   
   aimgr = (FGAIManager *) globals-> get_subsystem("ai_model");  
-  // Before the flight status of this traffic entity is updated 
-  // for the first time, we need to roll back it's flight schedule so
-  // so that all the flights are centered around this simulated week's time
-  // table. This is to avoid the situation where the first scheduled flight is
-  // in the future, causing the traffic manager to not generate traffic until
-  // simulated time has caught up with the real world time at initialization.
-  // This is to counter a more general initialization bug, caused by the fact
-  // that warp is not yet set when the  schedule is initialized. This is
-  // especially a problem when using a negative time offset.
-  // i.e let's say we specify FlightGear to run with --time-offset=-24:00:00. 
-  // Then the schedule will initialize using today, but we will fly yesterday.
-  // Thus, it would take a whole day of simulation before the traffic manager
-  // finally kicks in. 
-  if (firstRun)
-    {
-      for (FGScheduledFlightVecIterator i = flights.begin(); 
-          i != flights.end(); 
-          i++)
-       {
-         i->adjustTime(now);
-       }
-      firstRun = false;
+    // Out-of-work aircraft seeks employment. Willing to work irregular hours ...
+    //cerr << "About to find a flight " << endl;
+    if (flights.empty()) {
+        //execute this loop at least once. 
+        SG_LOG(SG_GENERAL, SG_BULK, "Scheduling for : " << modelPath << " " <<  registration << " " << homePort);
+        FGScheduledFlight *flight = 0;
+         do {
+            flight = findAvailableFlight(currentDestination, flightIdentifier);
+            if (flight) {
+                currentDestination = flight->getArrivalAirport()->getId();
+                time_t arr, dep;
+                dep = flight->getDepartureTime();
+                arr = flight->getArrivalTime();
+                string depT = asctime(gmtime(&dep));
+                string arrT = asctime(gmtime(&arr));
+
+                depT = depT.substr(0,24);
+                arrT = arrT.substr(0,24);
+                SG_LOG(SG_GENERAL, SG_BULK, "  " << flight->getCallSign() << ":" 
+                                         << "  " << flight->getDepartureAirport()->getId() << ":"
+                                         << "  " << depT << ":"
+                                         << " \"" << flight->getArrivalAirport()->getId() << "\"" << ":"
+                                         << "  " << arrT << ":");
+            flights.push_back(flight);
+            }
+        } while ((currentDestination != homePort) && (flight != 0));
+        SG_LOG(SG_GENERAL, SG_BULK, cerr << " Done " << endl);
     }
-  
+    //cerr << " Done " << endl;
+   // No flights available for this aircraft
+  if (flights.size() == 0) {
+      return false;
+  }
   // Sort all the scheduled flights according to scheduled departure time.
   // Because this is done at every update, we only need to check the status
   // of the first listed flight. 
-  sort(flights.begin(), flights.end());
+  //sort(flights.begin(), flights.end(), compareScheduledFlights);
+ if (firstRun) {
+     if (fgGetBool("/sim/traffic-manager/instantaneous-action") == true) {
+         deptime = now + rand() % 300; // Wait up to 5 minutes until traffic starts moving to prevent too many aircraft 
+                                   // from cluttering the gate areas.
+         cerr << "Scheduling " << registration << " for instantaneous action flight " << endl;
+     }
+     firstRun = false;
+  }
+  if (!deptime)
+    deptime = (*flights.begin())->getDepartureTime();
   FGScheduledFlightVecIterator i = flights.begin();
+  SG_LOG (SG_GENERAL, SG_DEBUG,"Traffic Manager: Processing registration " << registration << " with callsign " << (*i)->getCallSign());
   if (AIManagerRef)
     {
       // Check if this aircraft has been released. 
@@ -227,9 +274,16 @@ bool FGAISchedule::update(time_t now)
       //cerr << "Estimated minimum distance to user: " << distanceToUser << endl;
       // This flight entry is entirely in the past, do we need to 
       // push it forward in time to the next scheduled departure. 
-      if ((i->getDepartureTime() < now) && (i->getArrivalTime() < now))
+      if (((*i)->getDepartureTime() < now) && ((*i)->getArrivalTime() < now))
        {
-         i->update();
+          SG_LOG (SG_GENERAL, SG_DEBUG, "Traffic Manager:      Flight is in the Past");
+          //cerr << modelPath << " " <<  registration << ": Flights from the past belong to the past :-)" << endl;
+          //exit(1);
+          // Don't just update: check whether we need to load a new leg. etc.
+          // This update occurs for distant aircraft, so we can update the current leg
+          // and detach it from the current list of aircraft. 
+         (*i)->update();
+          i = flights.erase(i);
          return true;
        }
 
@@ -239,50 +293,36 @@ bool FGAISchedule::update(time_t now)
       // object for it. 
       //if ((i->getDepartureTime() < now) && (i->getArrivalTime() > now))
       
-
       // Part of this flight is in the future.
-      if (i->getArrivalTime() > now)
+      if ((*i)->getArrivalTime() > now)
        {
-         dep = i->getDepartureAirport();
-         arr = i->getArrivalAirport  ();
+          
+         dep = (*i)->getDepartureAirport();
+         arr = (*i)->getArrivalAirport  ();
          if (!(dep && arr))
            return false;
          
-         temp = sgPolarToCart3d(Point3D(dep->getLongitude() * 
-                                        SG_DEGREES_TO_RADIANS, 
-                                        dep->getLatitude()  * 
-                                        SG_DEGREES_TO_RADIANS, 
-                                        1.0));
-         a[0] = temp.x();
-         a[1] = temp.y();
-         a[2] = temp.z();
+          SGVec3d a = SGVec3d::fromGeoc(SGGeoc::fromDegM(dep->getLongitude(),
+                                                dep->getLatitude(), 1));
+          SGVec3d b = SGVec3d::fromGeoc(SGGeoc::fromDegM(arr->getLongitude(),
+                                                arr->getLatitude(), 1));
+          SGVec3d _cross = cross(b, a);
          
-         temp = sgPolarToCart3d(Point3D(arr->getLongitude() *
-                                        SG_DEGREES_TO_RADIANS,
-                                        arr->getLatitude()  *
-                                        SG_DEGREES_TO_RADIANS, 
-                                        1.0));
-         b[0] = temp.x();
-         b[1] = temp.y();
-         b[2] = temp.z();
-         sgdNormaliseVec3(a);
-         sgdNormaliseVec3(b);
-         sgdVectorProductVec3(cross,b,a);
-         
-         angle = sgACos(sgdScalarProductVec3(a,b));
+         angle = sgACos(dot(a, b));
          
          // Okay, at this point we have the angle between departure and 
          // arrival airport, in degrees. From here we can interpolate the
          // position of the aircraft by calculating the ratio between 
          // total time enroute and elapsed time enroute. 
  
-         totalTimeEnroute     = i->getArrivalTime() - i->getDepartureTime();
-         if (now > i->getDepartureTime())
+         totalTimeEnroute     = (*i)->getArrivalTime() - (*i)->getDepartureTime();
+         if (now > (*i)->getDepartureTime())
            {
              //err << "Lat = " << lat << ", lon = " << lon << endl;
              //cerr << "Time diff: " << now-i->getDepartureTime() << endl;
-             elapsedTimeEnroute   = now - i->getDepartureTime();
-             remainingTimeEnroute = i->getArrivalTime()   - now;  
+             elapsedTimeEnroute   = now - (*i)->getDepartureTime();
+             remainingTimeEnroute = (*i)->getArrivalTime()   - now;  
+              SG_LOG (SG_GENERAL, SG_DEBUG, "Traffic Manager:      Flight is in progress.");
            }
          else
            {
@@ -290,6 +330,7 @@ bool FGAISchedule::update(time_t now)
              lon = dep->getLongitude();
              elapsedTimeEnroute = 0;
              remainingTimeEnroute = totalTimeEnroute;
+              SG_LOG (SG_GENERAL, SG_DEBUG, "Traffic Manager:      Flight is pending.");
            }
                  
          angle *= ( (double) elapsedTimeEnroute/ (double) totalTimeEnroute);
@@ -297,60 +338,46 @@ bool FGAISchedule::update(time_t now)
          
          //cout << "a = " << a[0] << " " << a[1] << " " << a[2] 
          //     << "b = " << b[0] << " " << b[1] << " " << b[2] << endl;  
-         sgdMakeRotMat4(matrix, angle, cross); 
+          sgdMat4 matrix;
+         sgdMakeRotMat4(matrix, angle, _cross.data()); 
+          SGVec3d newPos(0, 0, 0);
          for(int j = 0; j < 3; j++)
            {
-             newPos[j] =0.0;
              for (int k = 0; k<3; k++)
                {
                  newPos[j] += matrix[j][k]*a[k];
                }
            }
          
-         temp = sgCartToPolar3d(Point3D(newPos[0], newPos[1],newPos[2]));
-         if (now > i->getDepartureTime())
+    SGGeod current;
+         if (now > (*i)->getDepartureTime())
            {
-             //cerr << "Lat = " << lat << ", lon = " << lon << endl;
-             //cerr << "Time diff: " << now-i->getDepartureTime() << endl;
-             lat = temp.lat() * SG_RADIANS_TO_DEGREES;
-             lon = temp.lon() * SG_RADIANS_TO_DEGREES; 
+        current = SGGeod::fromCart(newPos);
            }
          else
            {
-             lat = dep->getLatitude();
-             lon = dep->getLongitude();
-           }
-         
+             current = dep->geod();
+      }
          
-         SGWayPoint current  (lon,
-                              lat,
-                              i->getCruiseAlt());
-         SGWayPoint user (   userLongitude,
-                             userLatitude,
-                             i->getCruiseAlt());
-         SGWayPoint dest (   arr->getLongitude(),
-                             arr->getLatitude(),
-                             i->getCruiseAlt());
-         // We really only need distance to user
-         // and course to destination 
-         user.CourseAndDistance(current, &courseToUser, &distanceToUser);
-         dest.CourseAndDistance(current, &courseToDest, &distanceToDest);
-         speed =  (distanceToDest*SG_METER_TO_NM) / 
+    SGGeod user = SGGeod::fromDegM(userLongitude, userLatitude, (*i)->getCruiseAlt());
+    speed = SGGeodesy::distanceNm(current, arr->geod()) / 
            ((double) remainingTimeEnroute/3600.0);
-         
+    
+    distanceToUser = SGGeodesy::distanceNm(current, user);
 
          // If distance between user and simulated aircaft is less
          // then 500nm, create this flight. At jet speeds 500 nm is roughly
          // one hour flight time, so that would be a good approximate point
          // to start a more detailed simulation of this aircraft.
-         //cerr << registration << " is currently enroute from " 
-         //   << dep->_id << " to " << arr->_id << "distance : " 
-          //   << distanceToUser*SG_METER_TO_NM << endl;
-         if ((distanceToUser*SG_METER_TO_NM) < TRAFFICTOAIDIST)
+         SG_LOG (SG_GENERAL, SG_DEBUG, "Traffic manager: " << registration << " is scheduled for a flight from " 
+            << dep->getId() << " to " << arr->getId() << ". Current distance to user: " 
+             << distanceToUser);
+         if (distanceToUser < TRAFFICTOAIDISTTOSTART)
            {
              string flightPlanName = dep->getId() + string("-") + arr->getId() + 
                string(".xml");
-             int alt;
+              SG_LOG (SG_GENERAL, SG_DEBUG, "Traffic manager: Creating AIModel");
+             //int alt;
              //if  ((i->getDepartureTime() < now))
              //{
              //          alt = i->getCruiseAlt() *100;
@@ -360,33 +387,50 @@ bool FGAISchedule::update(time_t now)
              //          alt = dep->_elevation+19;
              //        }
 
-              FGAIModelEntity entity;
-
-              entity.m_class = m_class; //"jet_transport";
-             entity.company = airline; //i->getAirline();
-             entity.acType  = acType; //i->getAcType();
-              entity.path = modelPath.c_str();
-              entity.flightplan = flightPlanName.c_str();
-              entity.latitude = lat;
-              entity.longitude = lon;
-              entity.altitude = i->getCruiseAlt() *100; // convert from FL to feet
-              entity.speed = speed;
-             entity.roll  = 0.0;
-             entity.fp = new FGAIFlightPlan(&entity, courseToDest, i->getDepartureTime(), dep, 
-                                            arr,true, radius, flightType, acType, airline);
-             
-             // Fixme: A non-existent model path results in an
-             // abort, due to an unhandled exeption, in fg main loop.
-              FGAIBase *aircraft = (FGAIBase*)aimgr->createAircraft( &entity, this);
-             AIManagerRef = aircraft->getID();
-             //cerr << "Class: " << m_class << ". acType: " << acType << ". Airline: " << airline << ". Speed = " << speed << ". From " << dep->getId() << " to " << arr->getId() << ". Time Fraction = " << (remainingTimeEnroute/(double) totalTimeEnroute) << endl;
-             //cerr << "Latitude : " << lat << ". Longitude : " << lon << endl;
-             //cerr << "Dep      : " << dep->getLatitude()<< ", "<< dep->getLongitude() << endl;
-             //cerr << "Arr      : " << arr->getLatitude()<< ", "<< arr->getLongitude() << endl;
-             //cerr << "Time remaining = " << (remainingTimeEnroute/3600.0) << endl;
-             //cerr << "Total time     = " << (totalTimeEnroute/3600.0) << endl;
-             //cerr << "Distance remaining = " << distanceToDest*SG_METER_TO_NM << endl;
-             
+             // Only allow traffic to be created when the model path (or the AI version of mp) exists
+             SGPath mp(globals->get_fg_root());
+             SGPath mp_ai = mp;
+
+             mp.append(modelPath);
+             mp_ai.append("AI");
+             mp_ai.append(modelPath);
+
+             if (mp.exists() || mp_ai.exists())
+             {
+                 FGAIAircraft *aircraft = new FGAIAircraft(this);
+                 aircraft->setPerformance(m_class); //"jet_transport";
+                 aircraft->setCompany(airline); //i->getAirline();
+                 aircraft->setAcType(acType); //i->getAcType();
+                 aircraft->setPath(modelPath.c_str());
+                 //aircraft->setFlightPlan(flightPlanName);
+                 aircraft->setLatitude(lat);
+                 aircraft->setLongitude(lon);
+                 aircraft->setAltitude((*i)->getCruiseAlt()*100); // convert from FL to feet
+                 aircraft->setSpeed(speed);
+                 aircraft->setBank(0);
+      
+      courseToDest = SGGeodesy::courseDeg(current, arr->geod());
+                 aircraft->SetFlightPlan(new FGAIFlightPlan(aircraft, flightPlanName, courseToDest, deptime, 
+                                                            dep, arr,true, radius, 
+                                                            (*i)->getCruiseAlt()*100, 
+                                                            lat, lon, speed, flightType, acType, 
+                                                            airline));
+                 aimgr->attach(aircraft);
+                 
+                 
+                 AIManagerRef = aircraft->getID();
+                 //cerr << "Class: " << m_class << ". acType: " << acType << ". Airline: " << airline << ". Speed = " << speed << ". From " << dep->getId() << " to " << arr->getId() << ". Time Fraction = " << (remainingTimeEnroute/(double) totalTimeEnroute) << endl;
+                 //cerr << "Latitude : " << lat << ". Longitude : " << lon << endl;
+                 //cerr << "Dep      : " << dep->getLatitude()<< ", "<< dep->getLongitude() << endl;
+                 //cerr << "Arr      : " << arr->getLatitude()<< ", "<< arr->getLongitude() << endl;
+                 //cerr << "Time remaining = " << (remainingTimeEnroute/3600.0) << endl;
+                 //cerr << "Total time     = " << (totalTimeEnroute/3600.0) << endl;
+                 //cerr << "Distance remaining = " << distanceToDest*SG_METER_TO_NM << endl;
+                 }
+             else
+               {
+                 SG_LOG(SG_INPUT, SG_WARN, "TrafficManager: Could not load model " << mp.str());
+               }
            }
          return true;
     }
@@ -396,9 +440,9 @@ bool FGAISchedule::update(time_t now)
       // Currently this status is mostly ignored, but in future
       // versions, code should go here that -if within user range-
       // positions these aircraft at parking locations at the airport.
-  if ((i->getDepartureTime() > now) && (i->getArrivalTime() > now))
+      if (((*i)->getDepartureTime() > now) && ((*i)->getArrivalTime() > now))
        { 
-         dep = i->getDepartureAirport();
+         dep = (*i)->getDepartureAirport();
          return true;
        } 
     }
@@ -414,80 +458,166 @@ bool FGAISchedule::update(time_t now)
 }
 
 
-void FGAISchedule::next()
+bool FGAISchedule::next()
 {
-  flights.begin()->update();
-  sort(flights.begin(), flights.end());
+  FGScheduledFlightVecIterator i = flights.begin();
+  (*i)->release();
+  //FIXME: remove first entry, 
+  // load new flights until back at home airport
+  // Lock loaded flights
+  //sort(flights.begin(), flights.end(), compareScheduledFlights);
+  // until that time
+  i = flights.erase(i);
+  //cerr << "Next: scheduling for : " << modelPath << " " <<  registration << endl;
+  FGScheduledFlight *flight = findAvailableFlight(currentDestination, flightIdentifier);
+  if (flight) {
+      currentDestination = flight->getArrivalAirport()->getId();
+      time_t arr, dep;
+      dep = flight->getDepartureTime();
+      arr = flight->getArrivalTime();
+      string depT = asctime(gmtime(&dep));
+      string arrT = asctime(gmtime(&arr));
+
+      depT = depT.substr(0,24);
+      arrT = arrT.substr(0,24);
+      //cerr << "  " << flight->getCallSign() << ":" 
+      //     << "  " << flight->getDepartureAirport()->getId() << ":"
+      //     << "  " << depT << ":"
+      //     << " \"" << flight->getArrivalAirport()->getId() << "\"" << ":"
+      //     << "  " << arrT << ":" << endl;
+
+       flights.push_back(flight);
+       return true;
+  } else {
+       return false;
+  }
+  //cerr << "FGAISchedule :: next needs updating" << endl;
+  //exit(1);
 }
 
-double FGAISchedule::getSpeed()
+FGScheduledFlight* FGAISchedule::findAvailableFlight (const string &currentDestination,
+                                                      const string &req)
 {
-  double courseToUser,   courseToDest;
-  double distanceToUser, distanceToDest;
-  double speed, remainingTimeEnroute;
-  FGAirport *dep, *arr;
+    time_t now = time(NULL) + fgGetLong("/sim/time/warp");
+
+    FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("Traffic Manager");
+    FGScheduledFlightVecIterator fltBegin, fltEnd;
+    fltBegin = tmgr->getFirstFlight(req);
+    fltEnd   = tmgr->getLastFlight(req);
+
+
+     //cerr << "Finding available flight " << endl;
+     // For Now:
+     // Traverse every registered flight
+     if (fltBegin == fltEnd) {
+          //cerr << "No Flights Scheduled for " << req << endl;
+     }
+     int counter = 0;
+     for (FGScheduledFlightVecIterator i = fltBegin; i != fltEnd; i++) {
+          (*i)->adjustTime(now);
+           //sort(fltBegin, fltEnd, compareScheduledFlights);
+           //cerr << counter++ << endl;
+     }
+     sort(fltBegin, fltEnd, compareScheduledFlights);
+     for (FGScheduledFlightVecIterator i = fltBegin; i != fltEnd; i++) {
+          //bool valid = true;
+          counter++;
+          if (!(*i)->isAvailable()) {
+               //cerr << (*i)->getCallSign() << "is no longer available" << endl;
+               continue;
+          }
+          if (!((*i)->getRequirement() == req)) {
+               continue;
+          }
+          if (!(((*i)->getArrivalAirport()) && ((*i)->getDepartureAirport()))) {
+              continue;
+          }
+          if (!(currentDestination.empty())) {
+              if (currentDestination != (*i)->getDepartureAirport()->getId()) {
+                   //cerr << (*i)->getCallSign() << "Doesn't match destination" << endl;
+                   //cerr << "Current Destination " << currentDestination << "Doesnt match flight's " <<
+                   //          (*i)->getArrivalAirport()->getId() << endl;
+                   continue;
+              }
+          }
+          //TODO: check time
+          // So, if we actually get here, we have a winner
+          //cerr << "found flight: " << req << " : " << currentDestination << " : " <<       
+          //         (*i)->getArrivalAirport()->getId() << endl;
+          (*i)->lock();
+          return (*i);
+     }
+     // matches req?
+     // if currentDestination has a value, does it match departure of next flight?
+     // is departure time later than planned arrival?
+     // is departure port valid?
+     // is arrival port valid?
+     //cerr << "Ack no flight found: " << endl;
+     return 0;
+}
 
+double FGAISchedule::getSpeed()
+{
   FGScheduledFlightVecIterator i = flights.begin();
-  dep = i->getDepartureAirport();
-  arr = i->getArrivalAirport  ();
-  if (!(dep && arr))
-    return 0;
  
-  SGWayPoint dest (   dep->getLongitude(),
-                     dep->getLatitude(),
-                     i->getCruiseAlt()); 
-  SGWayPoint curr (    arr->getLongitude(),
-                     arr->getLatitude(),
-                     i->getCruiseAlt());
-  remainingTimeEnroute     = i->getArrivalTime() - i->getDepartureTime();
-  dest.CourseAndDistance(curr, &courseToDest, &distanceToDest);
-  speed =  (distanceToDest*SG_METER_TO_NM) / 
-    ((double) remainingTimeEnroute/3600.0);
+  FGAirport* dep = (*i)->getDepartureAirport(),
+   *arr = (*i)->getArrivalAirport();
+  double dist = SGGeodesy::distanceNm(dep->geod(), arr->geod());
+  double remainingTimeEnroute = (*i)->getArrivalTime() - (*i)->getDepartureTime();
+
+  double speed = dist / (remainingTimeEnroute/3600.0);
+  SG_CLAMP_RANGE(speed, 300.0, 500.0);
   return speed;
 }
+/*
+bool compareSchedules(FGAISchedule*a, FGAISchedule*b)
+{ 
+  //return (*a) < (*b); 
+} 
+*/
 
-
-void FGAISchedule::setClosestDistanceToUser()
-{
+// void FGAISchedule::setClosestDistanceToUser()
+// {
   
   
-  double course;
-  double dist;
+//   double course;
+//   double dist;
 
-  Point3D temp;
-  time_t 
-    totalTimeEnroute, 
-    elapsedTimeEnroute;
+//   Point3D temp;
+//   time_t 
+//     totalTimeEnroute, 
+//     elapsedTimeEnroute;
  
-  double userLatitude  = fgGetDouble("/position/latitude-deg");
-  double userLongitude = fgGetDouble("/position/longitude-deg");
+//   double userLatitude  = fgGetDouble("/position/latitude-deg");
+//   double userLongitude = fgGetDouble("/position/longitude-deg");
 
-  FGAirport *dep;
+//   FGAirport *dep;
   
-#if defined( __CYGWIN__) || defined( __MINGW32__)
-  #define HUGE HUGE_VAL
-#endif
-  distanceToUser = HUGE;
-  FGScheduledFlightVecIterator i = flights.begin();
-  while (i != flights.end())
-    {
-      dep = i->getDepartureAirport();
-      //if (!(dep))
-      //return HUGE;
+// #if defined( __CYGWIN__) || defined( __MINGW32__)
+//   #define HUGE HUGE_VAL
+// #endif
+//   distanceToUser = HUGE;
+//   FGScheduledFlightVecIterator i = flights.begin();
+//   while (i != flights.end())
+//     {
+//       dep = i->getDepartureAirport();
+//       //if (!(dep))
+//       //return HUGE;
       
-      SGWayPoint user (   userLongitude,
-                         userLatitude,
-                         i->getCruiseAlt());
-      SGWayPoint current (dep->getLongitude(),
-                         dep->getLatitude(),
-                         0);
-      user.CourseAndDistance(current, &course, &dist);
-      if (dist < distanceToUser)
-       {
-         distanceToUser = dist;
-         //cerr << "Found closest distance to user for " << registration << " to be " << distanceToUser << " at airport " << dep->getId() << endl;
-       }
-      i++;
-    }
-  //return distToUser;
-}
+//       SGWayPoint user (   userLongitude,
+//                       userLatitude,
+//                       i->getCruiseAlt());
+//       SGWayPoint current (dep->getLongitude(),
+//                       dep->getLatitude(),
+//                       0);
+//       user.CourseAndDistance(current, &course, &dist);
+//       if (dist < distanceToUser)
+//     {
+//       distanceToUser = dist;
+//       //cerr << "Found closest distance to user for " << registration << " to be " << distanceToUser << " at airport " << dep->getId() << endl;
+//     }
+//       i++;
+//     }
+//   //return distToUser;
+// }
+