groundOffset = 0;
distanceToUser = 0;
valid = true;
+ lastRun = 0;
//score = 0;
}
firstRun = true;
runCount = 0;
hits = 0;
+ lastRun = 0;
initialized = false;
valid = true;
}
firstRun = other.firstRun;
runCount = other.runCount;
hits = other.hits;
+ lastRun = other.lastRun;
initialized = other.initialized;
valid = other.valid;
}
time_t
totalTimeEnroute,
elapsedTimeEnroute,
- remainingTimeEnroute,
+ //remainingTimeEnroute,
deptime = 0;
if (!valid) {
return false;
}
- scheduleFlights();
+ scheduleFlights(now);
if (flights.empty()) { // No flights available for this aircraft
valid = false;
return false;
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 = 0;
} else {
totalTimeEnroute = flight->getArrivalTime() - flight->getDepartureTime();
if (flight->getDepartureTime() < now) {
elapsedTimeEnroute = now - flight->getDepartureTime();
- remainingTimeEnroute = totalTimeEnroute - elapsedTimeEnroute;
+ //remainingTimeEnroute = totalTimeEnroute - elapsedTimeEnroute;
double x = elapsedTimeEnroute / (double) totalTimeEnroute;
// current pos is based on great-circle course between departure/arrival,
speed = ((distanceM - coveredDistance) * SG_METER_TO_NM) / 3600.0;
} else {
// not departed yet
- remainingTimeEnroute = totalTimeEnroute;
+ //remainingTimeEnroute = totalTimeEnroute;
elapsedTimeEnroute = 0;
position = dep->geod();
SG_LOG (SG_GENERAL, SG_BULK, "Traffic Manager: Flight is pending, departure in "
}
} else {
// departure / arrival coincident
- remainingTimeEnroute = totalTimeEnroute = 0.0;
+ //remainingTimeEnroute = totalTimeEnroute = 0.0;
elapsedTimeEnroute = 0;
position = dep->geod();
}
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;
}
aircraft->setLatitude(position.getLatitudeDeg());
aircraft->setLongitude(position.getLongitudeDeg());
aircraft->setAltitude(flight->getCruiseAlt()*100); // convert from FL to feet
- aircraft->setSpeed(speedKnots);
+ aircraft->setSpeed(0);
aircraft->setBank(0);
courseToDest = SGGeodesy::courseDeg(position, arr->geod());
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;
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;
}
-
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;
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);
}
FGScheduledFlight* FGAISchedule::findAvailableFlight (const string ¤tDestination,
- 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);
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;
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();
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;
+}