]> git.mxchange.org Git - flightgear.git/blobdiff - src/Traffic/Schedule.cxx
Remove hard-coded values wherever possible;
[flightgear.git] / src / Traffic / Schedule.cxx
index 3445e0a7a6d9612bf525c977282789e434c08a50..85343db293fb8d605537eb8e2aea936ec8f06b6e 100644 (file)
@@ -69,6 +69,7 @@ FGAISchedule::FGAISchedule()
   groundOffset = 0;
   distanceToUser = 0;
   valid = true;
+  lastRun = 0;
   //score = 0;
 }
 
@@ -120,6 +121,7 @@ FGAISchedule::FGAISchedule(string model,
   firstRun         = true;
   runCount         = 0;
   hits             = 0;
+  lastRun          = 0;
   initialized      = false;
   valid            = true;
 }
@@ -147,6 +149,7 @@ FGAISchedule::FGAISchedule(const FGAISchedule &other)
   firstRun           = other.firstRun;
   runCount           = other.runCount;
   hits               = other.hits;
+  lastRun            = other.lastRun;
   initialized        = other.initialized;
   valid              = other.valid;
 }
@@ -198,7 +201,7 @@ bool FGAISchedule::update(time_t now, const SGVec3d& userCart)
   if (!valid) {
     return false;
   }
-  scheduleFlights();
+  scheduleFlights(now);
   if (flights.empty()) { // No flights available for this aircraft
       valid = false;
       return false;
@@ -225,9 +228,9 @@ bool FGAISchedule::update(time_t now, const SGVec3d& userCart)
     
   if (AIManagerRef) {
     // Check if this aircraft has been released. 
-    FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("Traffic Manager");
+    FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("traffic-manager");
     if (tmgr->isReleased(AIManagerRef)) {
-      AIManagerRef = NULL;
+      AIManagerRef = 0;
     } else {
       return true; // in visual range, let the AIManager handle it
     }
@@ -318,7 +321,7 @@ bool FGAISchedule::createAIAircraft(FGScheduledFlight* flight, double speedKnots
   mp_ai.append(modelPath);
 
   if (!mp.exists() && !mp_ai.exists()) {
-    SG_LOG(SG_INPUT, SG_WARN, "TrafficManager: Could not load model " << mp.str());
+    SG_LOG(SG_GENERAL, SG_WARN, "TrafficManager: Could not load model " << mp.str());
     return true;
   }
 
@@ -344,7 +347,7 @@ bool FGAISchedule::createAIAircraft(FGScheduledFlight* flight, double speedKnots
                                             airline);
   if (fp->isValidPlan()) {
         aircraft->SetFlightPlan(fp);
-        FGAIManager* aimgr = (FGAIManager *) globals-> get_subsystem("ai_model");
+        FGAIManager* aimgr = (FGAIManager *) globals-> get_subsystem("ai-model");
         aimgr->attach(aircraft);
         AIManagerRef = aircraft->getID();
         return true;
@@ -366,27 +369,40 @@ void FGAISchedule::setHeading()
     courseToDest = SGGeodesy::courseDeg((*flights.begin())->getDepartureAirport()->geod(), (*flights.begin())->getArrivalAirport()->geod());
 }
 
-void FGAISchedule::scheduleFlights()
+void FGAISchedule::scheduleFlights(time_t now)
 {
   if (!flights.empty()) {
     return;
   }
-  
-  SG_LOG(SG_GENERAL, SG_BULK, "Scheduling for : " << modelPath << " " <<  registration << " " << homePort);
+  //string startingPort;
+  string userPort = fgGetString("/sim/presets/airport-id");
+  SG_LOG(SG_GENERAL, SG_BULK, "Scheduling Flights for : " << modelPath << " " <<  registration << " " << homePort);
   FGScheduledFlight *flight = NULL;
   do {
-    flight = findAvailableFlight(currentDestination, flightIdentifier);
+    if (currentDestination.empty()) {
+        flight = findAvailableFlight(userPort, flightIdentifier, now, (now+6400));
+        if (!flight)
+            flight = findAvailableFlight(currentDestination, flightIdentifier);
+    } else {
+        flight = findAvailableFlight(currentDestination, flightIdentifier);
+    }
     if (!flight) {
       break;
     }
-    
+    //if (startingPort.empty()) {
+    //    startingPort = flight->getDepartureAirport()->getId();
+    //}
     currentDestination = flight->getArrivalAirport()->getId();
+    //cerr << "Current destination " <<  currentDestination << endl;
     if (!initialized) {
         string departurePort = flight->getDepartureAirport()->getId();
        //cerr << "Scheduled " << registration <<  " " << score << " for Flight " 
        //     << flight-> getCallSign() << " from " << departurePort << " to " << currentDestination << endl;
-        if (fgGetString("/sim/presets/airport-id") == departurePort) {
+        if (userPort == departurePort) {
+            lastRun = 1;
             hits++;
+        } else {
+            lastRun = 0;
         }
         //runCount++;
         initialized = true;
@@ -400,11 +416,11 @@ void FGAISchedule::scheduleFlights()
 
     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 << ":");
+    SG_LOG(SG_GENERAL, SG_BULK, "  Flight " << flight->getCallSign() << ":" 
+                             << "  "        << flight->getDepartureAirport()->getId() << ":"
+                             << "  "        << depT << ":"
+                             << " \""       << flight->getArrivalAirport()->getId() << "\"" << ":"
+                             << "  "        << arrT << ":");
   
     flights.push_back(flight);
   } while (currentDestination != homePort);
@@ -444,11 +460,12 @@ bool FGAISchedule::next()
 }
 
 FGScheduledFlight* FGAISchedule::findAvailableFlight (const string &currentDestination,
-                                                      const string &req)
+                                                      const string &req,
+                                                     time_t min, time_t max)
 {
     time_t now = time(NULL) + fgGetLong("/sim/time/warp");
 
-    FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("Traffic Manager");
+    FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("traffic-manager");
     FGScheduledFlightVecIterator fltBegin, fltEnd;
     fltBegin = tmgr->getFirstFlight(req);
     fltEnd   = tmgr->getLastFlight(req);
@@ -488,7 +505,18 @@ FGScheduledFlight* FGAISchedule::findAvailableFlight (const string &currentDesti
                    continue;
               }
           }
-          //TODO: check time
+          if (flights.size()) {
+            time_t arrival = flights.back()->getArrivalTime();
+            int groundTime = groundTimeFromRadius();
+            if ((*i)->getDepartureTime() < (arrival+(groundTime)))
+                continue;
+          }
+          if (min != 0) {
+              time_t dep = (*i)->getDepartureTime();
+              if ((dep < min) || (dep > max))
+                  continue;
+          }
+
           // So, if we actually get here, we have a winner
           //cerr << "found flight: " << req << " : " << currentDestination << " : " <<       
           //         (*i)->getArrivalAirport()->getId() << endl;
@@ -504,6 +532,23 @@ FGScheduledFlight* FGAISchedule::findAvailableFlight (const string &currentDesti
      return NULL;
 }
 
+int FGAISchedule::groundTimeFromRadius()
+{
+    if (radius < 10) 
+        return 15 * 60;
+    else if (radius < 15)
+        return 20 * 60;
+    else if (radius < 20)
+        return 30 * 60;
+    else if (radius < 25)
+        return 50 * 60;
+    else if (radius < 30)
+        return 90 * 60;
+    else 
+        return 120 * 60;
+}
+
+
 double FGAISchedule::getSpeed()
 {
   FGScheduledFlightVecIterator i = flights.begin();
@@ -537,48 +582,11 @@ bool compareSchedules(FGAISchedule*a, FGAISchedule*b)
   return (*a) < (*b); 
 } 
 
-
-// void FGAISchedule::setClosestDistanceToUser()
-// {
-  
-  
-//   double course;
-//   double dist;
-
-//   time_t 
-//     totalTimeEnroute, 
-//     elapsedTimeEnroute;
-//   double userLatitude  = fgGetDouble("/position/latitude-deg");
-//   double userLongitude = fgGetDouble("/position/longitude-deg");
-
-//   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;
-      
-//       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;
-// }
+bool FGAISchedule::operator< (const FGAISchedule &other) const
+{ 
+    //cerr << "Sorting " << registration << " and "  << other.registration << endl;
+    double currentScore = score       * (1.5 - lastRun);
+    double otherScore   = other.score * (1.5 - other.lastRun);
+    return currentScore > otherScore;
+}