]> git.mxchange.org Git - flightgear.git/blobdiff - src/Traffic/Schedule.cxx
MSVC fix
[flightgear.git] / src / Traffic / Schedule.cxx
index cc9a90e45872540ce47fba33be10c382092e794e..29bd9a743bdfda52b97fddd4fb85ea0685da6f09 100644 (file)
@@ -122,7 +122,7 @@ void FGAISchedule::update(time_t now)
     userLatitude,
     userLongitude;
 
-  if (fgGetBool("/sim/ai/enabled") == false)
+  if (fgGetBool("/sim/traffic-manager/enabled") == false)
     return;
   
   aimgr = (FGAIManager *) globals-> get_subsystem("ai_model");  
@@ -155,6 +155,14 @@ void FGAISchedule::update(time_t now)
   // of the first listed flight. 
   sort(flights.begin(), flights.end());
   FGScheduledFlightVecIterator i = flights.begin();
+  if (AIManagerRef)
+    {
+      // Check if this aircraft has been released. 
+      FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("Traffic Manager");
+      if (tmgr->isReleased(AIManagerRef))
+       AIManagerRef = 0;
+    }
+
   if (!AIManagerRef)
     {
       userLatitude  = fgGetDouble("/position/latitude-deg");
@@ -173,7 +181,11 @@ void FGAISchedule::update(time_t now)
       // This flight is in progress, so we need to calculate it's
       // approximate position and -if in range- create an AIAircraft
       // object for it. 
-      if ((i->getDepartureTime() < now) && (i->getArrivalTime() > now))
+      //if ((i->getDepartureTime() < now) && (i->getArrivalTime() > now))
+      
+
+      // Part of this flight is in the future.
+      if (i->getArrivalTime() > now)
        {
          dep = i->getDepartureAirport();
          arr = i->getArrivalAirport  ();
@@ -205,6 +217,7 @@ void FGAISchedule::update(time_t now)
          // 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();
          elapsedTimeEnroute   = now - i->getDepartureTime();
          remainingTimeEnroute = i->getArrivalTime()   - now;  
@@ -226,8 +239,20 @@ void FGAISchedule::update(time_t now)
 
          temp = sgCartToPolar3d(Point3D(newPos[0], newPos[1],newPos[2]));
 
-         lat = temp.lat() * SG_RADIANS_TO_DEGREES;
-         lon = temp.lon() * SG_RADIANS_TO_DEGREES;
+         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; 
+             //err << "Lat = " << lat << ", lon = " << lon << endl;
+             //cerr << "Time diff: " << now-i->getDepartureTime() << endl;
+           }
+         else
+           {
+             lat = dep->latitude;
+             lon = dep->longitude;
+           }
          
          SGWayPoint current  (lon,
                               lat,
@@ -240,8 +265,8 @@ void FGAISchedule::update(time_t now)
                              i->getCruiseAlt());
          // We really only need distance to user
          // and course to destination 
-         current.CourseAndDistance(user, &courseToUser, &distanceToUser);
-         current.CourseAndDistance(dest, &courseToDest, &distanceToDest);
+         user.CourseAndDistance(current, &courseToUser, &distanceToUser);
+         dest.CourseAndDistance(current, &courseToDest, &distanceToDest);
          speed =  (distanceToDest*SG_METER_TO_NM) / 
            ((double) remainingTimeEnroute/3600.0);
          
@@ -251,36 +276,44 @@ void FGAISchedule::update(time_t now)
          // 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;
+         //   << dep->id << " to " << arr->id << "distance : " 
+          //   << distanceToUser*SG_METER_TO_NM << endl;
          if ((distanceToUser*SG_METER_TO_NM) < 500.0)
            {
-             string flightPlanName = dep->id + string("-") + arr->id + string(".xml");
+             string flightPlanName = dep->id + string("-") + arr->id + 
+               string(".xml");
+             int alt;
+             //if  ((i->getDepartureTime() < now))
+             //{
+             //          alt = i->getCruiseAlt() *100;
+             //        }
+             //else
+             //{
+             //          alt = dep->elevation+19;
+             //        }
+
+              FGAIModelEntity entity;
+
+              entity.m_class = "jet_transport";
+              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 = 450;
+             entity.fp = new FGAIFlightPlan(&entity, courseToDest, i->getDepartureTime(), dep, arr);
 
-             FGAIFlightPlan* f;
-             // If we're less then 10 minutes behind schedule, do a normal
-             // full flight plan initialization, otherwise, do a modified 
-             // in-air initializition, at the location where this flight is
-             // according to the Traffic Manager
-             if (elapsedTimeEnroute < 600)
-               f = new FGAIFlightPlan(flightPlanName);
-             else
-               f = new FGAIFlightPlan(flightPlanName, 
-                                      lat, 
-                                      lon, 
-                                      i->getCruiseAlt() * 1000, // convert from FL to feet
-                                      speed, 
-                                      courseToDest);
              // Fixme: A non-existent model path results in an
              // abort, due to an unhandled exeption, in fg main loop.
-             AIManagerRef = aimgr->createAircraft("jet_transport", 
-                                              modelPath, f); 
+             AIManagerRef = aimgr->createAircraft( &entity, this);
              //cerr << "Created: " << AIManagerRef << endl;
            }
          return;
        }
+
       // Both departure and arrival time are in the future, so this
       // the aircraft is parked at the departure airport.
-      // Currently this status is mostly ignored, but in furture
+      // 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))
@@ -290,3 +323,11 @@ void FGAISchedule::update(time_t now)
        } 
     }
 }
+
+
+void FGAISchedule::next()
+{
+  flights.begin()->update();
+  sort(flights.begin(), flights.end());
+}
+