| 1 | /****************************************************************************** |
| 2 | * Schedule.cxx |
| 3 | * Written by Durk Talsma, started May 5, 2004. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of the |
| 8 | * License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | * |
| 20 | **************************************************************************** |
| 21 | * |
| 22 | *****************************************************************************/ |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include "config.h" |
| 26 | #endif |
| 27 | |
| 28 | #include <algorithm> |
| 29 | #include <fstream> |
| 30 | #include <iostream> |
| 31 | #include <stdlib.h> |
| 32 | #include <string> |
| 33 | #include <time.h> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include <simgear/compiler.h> |
| 37 | #include <simgear/debug/ErrorReportingCallback.hxx> |
| 38 | #include <simgear/math/sg_geodesy.hxx> |
| 39 | #include <simgear/props/props.hxx> |
| 40 | #include <simgear/sg_inlines.h> |
| 41 | #include <simgear/structure/subsystem_mgr.hxx> |
| 42 | #include <simgear/timing/sg_time.hxx> |
| 43 | #include <simgear/xml/easyxml.hxx> |
| 44 | |
| 45 | #include <AIModel/AIAircraft.hxx> |
| 46 | #include <AIModel/AIFlightPlan.hxx> |
| 47 | #include <AIModel/AIManager.hxx> |
| 48 | #include <Airports/airport.hxx> |
| 49 | #include <Main/fg_props.hxx> |
| 50 | #include <Main/globals.hxx> |
| 51 | |
| 52 | #include "SchedFlight.hxx" |
| 53 | #include "TrafficMgr.hxx" |
| 54 | |
| 55 | using std::string; |
| 56 | |
| 57 | /****************************************************************************** |
| 58 | * the FGAISchedule class contains data members and code to maintain a |
| 59 | * schedule of Flights for an artificially controlled aircraft. |
| 60 | *****************************************************************************/ |
| 61 | FGAISchedule::FGAISchedule() |
| 62 | : heavy(false), |
| 63 | radius(0), |
| 64 | groundOffset(0), |
| 65 | distanceToUser(0), |
| 66 | score(0), |
| 67 | runCount(0), |
| 68 | hits(0), |
| 69 | lastRun(0), |
| 70 | firstRun(false), |
| 71 | courseToDest(0), |
| 72 | initialized(false), |
| 73 | valid(false), |
| 74 | scheduleComplete(false) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | |
| 79 | FGAISchedule::FGAISchedule(const string& model, |
| 80 | const string& lvry, |
| 81 | const string& port, |
| 82 | const string& reg, |
| 83 | const string& flightId, |
| 84 | bool hvy, |
| 85 | const string& act, |
| 86 | const string& arln, |
| 87 | const string& mclass, |
| 88 | const string& fltpe, |
| 89 | double rad, |
| 90 | double grnd) |
| 91 | : modelPath{model}, |
| 92 | homePort{port}, |
| 93 | livery{lvry}, |
| 94 | registration{reg}, |
| 95 | airline{arln}, |
| 96 | acType{act}, |
| 97 | m_class{mclass}, |
| 98 | flightType{fltpe}, |
| 99 | flightIdentifier{flightId}, |
| 100 | heavy(hvy), |
| 101 | radius(rad), |
| 102 | groundOffset(grnd), |
| 103 | distanceToUser(0), |
| 104 | score(0), |
| 105 | runCount(0), |
| 106 | hits(0), |
| 107 | lastRun(0), |
| 108 | firstRun(true), |
| 109 | courseToDest(0), |
| 110 | initialized(false), |
| 111 | valid(true), |
| 112 | scheduleComplete(false) |
| 113 | { |
| 114 | } |
| 115 | |
| 116 | FGAISchedule::FGAISchedule(const FGAISchedule& other) : modelPath{other.modelPath}, |
| 117 | homePort{other.homePort}, |
| 118 | livery{other.livery}, |
| 119 | registration{other.registration}, |
| 120 | airline{other.airline}, |
| 121 | acType{other.acType}, |
| 122 | m_class{other.m_class}, |
| 123 | flightType{other.flightType}, |
| 124 | flightIdentifier{other.flightIdentifier}, |
| 125 | currentDestination{other.currentDestination}, |
| 126 | flights{other.flights}, |
| 127 | aiAircraft{other.aiAircraft} |
| 128 | { |
| 129 | heavy = other.heavy; |
| 130 | firstRun = other.firstRun; |
| 131 | radius = other.radius; |
| 132 | groundOffset = other.groundOffset; |
| 133 | score = other.score; |
| 134 | distanceToUser = other.distanceToUser; |
| 135 | runCount = other.runCount; |
| 136 | hits = other.hits; |
| 137 | lastRun = other.lastRun; |
| 138 | courseToDest = other.courseToDest; |
| 139 | initialized = other.initialized; |
| 140 | valid = other.valid; |
| 141 | scheduleComplete = other.scheduleComplete; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | FGAISchedule::~FGAISchedule() |
| 146 | { |
| 147 | // remove related object from AI manager |
| 148 | if (aiAircraft) { |
| 149 | aiAircraft->setDie(true); |
| 150 | } |
| 151 | |
| 152 | /* for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++) |
| 153 | { |
| 154 | delete (*flt); |
| 155 | } |
| 156 | flights.clear();*/ |
| 157 | } |
| 158 | |
| 159 | bool FGAISchedule::init() |
| 160 | { |
| 161 | //tm targetTimeDate; |
| 162 | //SGTime* currTimeDate = globals->get_time_params(); |
| 163 | |
| 164 | //tm *temp = currTimeDate->getGmt(); |
| 165 | //char buffer[512]; |
| 166 | //sgTimeFormatTime(&targetTimeDate, buffer); |
| 167 | //cout << "Scheduled Time " << buffer << endl; |
| 168 | //cout << "Time :" << time(NULL) << " SGTime : " << sgTimeGetGMT(temp) << endl; |
| 169 | /*for (FGScheduledFlightVecIterator i = flights.begin(); |
| 170 | i != flights.end(); |
| 171 | i++) |
| 172 | { |
| 173 | //i->adjustTime(now); |
| 174 | if (!((*i)->initializeAirports())) |
| 175 | return false; |
| 176 | } */ |
| 177 | //sort(flights.begin(), flights.end()); |
| 178 | // Since time isn't initialized yet when this function is called, |
| 179 | // Find the closest possible airport. |
| 180 | // This should give a reasonable initialization order. |
| 181 | //setClosestDistanceToUser(); |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns true when processing is complete. |
| 187 | * Returns false when processing was aborted due to timeout, so |
| 188 | * more time required - and another call is requested (next sim iteration). |
| 189 | */ |
| 190 | bool FGAISchedule::update(time_t now, const SGVec3d& userCart) |
| 191 | { |
| 192 | time_t totalTimeEnroute; |
| 193 | time_t elapsedTimeEnroute; |
| 194 | time_t remainingTimeEnroute; |
| 195 | time_t deptime = 0; |
| 196 | |
| 197 | if (!valid) { |
| 198 | return true; // processing complete |
| 199 | } |
| 200 | |
| 201 | if (!scheduleComplete) { |
| 202 | scheduleComplete = scheduleFlights(now); |
| 203 | } |
| 204 | |
| 205 | if (!scheduleComplete) { |
| 206 | return false; // not ready yet, continue processing in next iteration |
| 207 | } |
| 208 | |
| 209 | if (flights.empty()) { // No flights available for this aircraft |
| 210 | valid = false; |
| 211 | return true; // processing complete |
| 212 | } |
| 213 | |
| 214 | |
| 215 | // Sort all the scheduled flights according to scheduled departure time. |
| 216 | // Because this is done at every update, we only need to check the status |
| 217 | // of the first listed flight. |
| 218 | //sort(flights.begin(), flights.end(), compareScheduledFlights); |
| 219 | |
| 220 | if (firstRun) { |
| 221 | if (fgGetBool("/sim/traffic-manager/instantaneous-action") == true) { |
| 222 | deptime = now; // + rand() % 300; // Wait up to 5 minutes until traffic starts moving to prevent too many aircraft |
| 223 | // from cluttering the gate areas. |
| 224 | } |
| 225 | firstRun = false; |
| 226 | } |
| 227 | |
| 228 | FGScheduledFlight* flight = flights.front(); |
| 229 | if (!deptime) { |
| 230 | deptime = flight->getDepartureTime(); |
| 231 | //cerr << "Setting departure time " << deptime << endl; |
| 232 | } |
| 233 | |
| 234 | if (aiAircraft) { |
| 235 | if (aiAircraft->getDie()) { |
| 236 | aiAircraft = NULL; |
| 237 | } else { |
| 238 | return true; // in visual range, let the AIManager handle it |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // This flight entry is entirely in the past, do we need to |
| 243 | // push it forward in time to the next scheduled departure. |
| 244 | if (flight->getArrivalTime() < now) { |
| 245 | SG_LOG(SG_AI, SG_BULK, "Traffic Manager: " << flight->getCallSign() << " is in the Past"); |
| 246 | // Don't just update: check whether we need to load a new leg. etc. |
| 247 | // This update occurs for distant aircraft, so we can update the current leg |
| 248 | // and detach it from the current list of aircraft. |
| 249 | flight->update(); |
| 250 | flights.erase(flights.begin()); // pop_front(), effectively |
| 251 | return true; // processing complete |
| 252 | } |
| 253 | |
| 254 | FGAirport* dep = flight->getDepartureAirport(); |
| 255 | FGAirport* arr = flight->getArrivalAirport(); |
| 256 | if (!dep || !arr) { |
| 257 | return true; // processing complete |
| 258 | } |
| 259 | |
| 260 | double speed = 450.0; |
| 261 | int remainingWaitTime = 0; |
| 262 | if (dep != arr) { |
| 263 | totalTimeEnroute = flight->getArrivalTime() - flight->getDepartureTime(); |
| 264 | if (flight->getDepartureTime() < now) { |
| 265 | elapsedTimeEnroute = now - flight->getDepartureTime(); |
| 266 | remainingTimeEnroute = totalTimeEnroute - elapsedTimeEnroute; |
| 267 | double x = elapsedTimeEnroute / (double)totalTimeEnroute; |
| 268 | |
| 269 | // current pos is based on great-circle course between departure/arrival, |
| 270 | // with percentage of distance traveled, based upon percentage of time |
| 271 | // enroute elapsed. |
| 272 | double course, az2, distanceM; |
| 273 | SGGeodesy::inverse(dep->geod(), arr->geod(), course, az2, distanceM); |
| 274 | double coveredDistance = distanceM * x; |
| 275 | |
| 276 | //FIXME very crude that doesn't harmonise with Legs |
| 277 | SGGeodesy::direct(dep->geod(), course, coveredDistance, position, az2); |
| 278 | |
| 279 | SG_LOG(SG_AI, SG_BULK, "Traffic Manager: " << flight->getCallSign() << " is in progress " << (x * 100) << "%"); |
| 280 | speed = ((distanceM - coveredDistance) * SG_METER_TO_NM) / 3600.0; |
| 281 | } else { |
| 282 | // not departed yet |
| 283 | remainingTimeEnroute = totalTimeEnroute; |
| 284 | elapsedTimeEnroute = 0; |
| 285 | position = dep->geod(); |
| 286 | remainingWaitTime = flight->getDepartureTime() - now; |
| 287 | if (remainingWaitTime < 600) { |
| 288 | SG_LOG(SG_AI, SG_BULK, "Traffic Manager: " << flight->getCallSign() << " is pending, departure in " << remainingWaitTime << " seconds "); |
| 289 | } |
| 290 | } |
| 291 | } else { |
| 292 | // departure / arrival coincident |
| 293 | remainingTimeEnroute = totalTimeEnroute = flight->getArrivalTime() - flight->getDepartureTime(); |
| 294 | elapsedTimeEnroute = 0; |
| 295 | position = dep->geod(); |
| 296 | } |
| 297 | |
| 298 | // cartesian calculations are more numerically stable over the (potentially) |
| 299 | // large distances involved here: see bug #80 |
| 300 | distanceToUser = dist(userCart, SGVec3d::fromGeod(position)) * SG_METER_TO_NM; |
| 301 | |
| 302 | |
| 303 | // If distance between user and simulated aircraft is less |
| 304 | // then 500nm, create this flight. At jet speeds 500 nm is roughly |
| 305 | // one hour flight time, so that would be a good approximate point |
| 306 | // to start a more detailed simulation of this aircraft. |
| 307 | if (remainingWaitTime < 600) { |
| 308 | SG_LOG(SG_AI, SG_BULK, "Traffic manager: " << registration << " is scheduled for a flight from " << dep->getId() << " to " << arr->getId() << ". Current distance to user: " << distanceToUser); |
| 309 | } |
| 310 | if (distanceToUser >= TRAFFIC_TO_AI_DIST_TO_START) { |
| 311 | return true; // out of visual range, for the moment. |
| 312 | } |
| 313 | |
| 314 | if (!createAIAircraft(flight, speed, deptime, remainingTimeEnroute)) { |
| 315 | valid = false; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | return true; // processing complete |
| 320 | } |
| 321 | |
| 322 | bool FGAISchedule::validModelPath(const std::string& modelPath) |
| 323 | { |
| 324 | return (resolveModelPath(modelPath) != SGPath()); |
| 325 | } |
| 326 | |
| 327 | SGPath FGAISchedule::resolveModelPath(const std::string& modelPath) |
| 328 | { |
| 329 | for (auto aiPath : globals->get_data_paths("AI")) { |
| 330 | aiPath.append(modelPath); |
| 331 | if (aiPath.exists()) { |
| 332 | return aiPath; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // check aircraft dirs |
| 337 | for (auto aircraftPath : globals->get_aircraft_paths()) { |
| 338 | SGPath mp = aircraftPath / modelPath; |
| 339 | if (mp.exists()) { |
| 340 | return mp; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | return SGPath(); |
| 345 | } |
| 346 | |
| 347 | bool FGAISchedule::createAIAircraft(FGScheduledFlight* flight, double speedKnots, time_t deptime, time_t remainingTime) |
| 348 | { |
| 349 | //FIXME The position must be set here not in update |
| 350 | FGAirport* dep = flight->getDepartureAirport(); |
| 351 | FGAirport* arr = flight->getArrivalAirport(); |
| 352 | string flightPlanName = dep->getId() + "-" + arr->getId() + ".xml"; |
| 353 | SG_LOG(SG_AI, SG_DEBUG, flight->getCallSign() << "|Traffic manager: Creating AIModel from:" << flightPlanName); |
| 354 | |
| 355 | aiAircraft = new FGAIAircraft(this); |
| 356 | aiAircraft->setPerformance(acType, m_class); //"jet_transport"; |
| 357 | aiAircraft->setCompany(airline); //i->getAirline(); |
| 358 | aiAircraft->setAcType(acType); //i->getAcType(); |
| 359 | aiAircraft->setPath(modelPath.c_str()); |
| 360 | //aircraft->setFlightPlan(flightPlanName); |
| 361 | aiAircraft->setLatitude(position.getLatitudeDeg()); |
| 362 | aiAircraft->setLongitude(position.getLongitudeDeg()); |
| 363 | aiAircraft->setAltitude(flight->getCruiseAlt() * 100); // convert from FL to feet |
| 364 | aiAircraft->setSpeed(0); |
| 365 | aiAircraft->setBank(0); |
| 366 | |
| 367 | courseToDest = SGGeodesy::courseDeg(position, arr->geod()); |
| 368 | std::unique_ptr<FGAIFlightPlan> fp(new FGAIFlightPlan(aiAircraft, |
| 369 | flightPlanName, |
| 370 | courseToDest, |
| 371 | deptime, |
| 372 | remainingTime, |
| 373 | dep, |
| 374 | arr, |
| 375 | true, |
| 376 | radius, |
| 377 | flight->getCruiseAlt() * 100, |
| 378 | position.getLatitudeDeg(), |
| 379 | position.getLongitudeDeg(), |
| 380 | speedKnots, flightType, acType, |
| 381 | airline)); |
| 382 | if (fp->isValidPlan()) { |
| 383 | // set this here so it's available inside attach, which calls AIBase::init |
| 384 | simgear::ErrorReportContext ec{"traffic-aircraft-callsign", flight->getCallSign()}; |
| 385 | |
| 386 | aiAircraft->FGAIBase::setFlightPlan(std::move(fp)); |
| 387 | globals->get_subsystem<FGAIManager>()->attach(aiAircraft); |
| 388 | if (aiAircraft->_getProps()) { |
| 389 | SGPropertyNode* nodeForAircraft = aiAircraft->_getProps(); |
| 390 | if (dep) { |
| 391 | nodeForAircraft->getChild("departure-airport-id", 0, true)->setStringValue(dep->getId()); |
| 392 | nodeForAircraft->getChild("departure-time-sec", 0, true)->setIntValue(deptime); |
| 393 | } |
| 394 | if (arr) { |
| 395 | nodeForAircraft->getChild("arrival-airport-id", 0, true)->setStringValue(arr->getId()); |
| 396 | // arrival time not known here |
| 397 | } |
| 398 | } |
| 399 | return true; |
| 400 | } else { |
| 401 | aiAircraft = NULL; |
| 402 | //hand back the flights that had already been scheduled |
| 403 | while (!flights.empty()) { |
| 404 | flights.front()->release(); |
| 405 | flights.erase(flights.begin()); |
| 406 | } |
| 407 | return false; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | void FGAISchedule::setHeading() |
| 412 | { |
| 413 | courseToDest = SGGeodesy::courseDeg((*flights.begin())->getDepartureAirport()->geod(), (*flights.begin())->getArrivalAirport()->geod()); |
| 414 | } |
| 415 | |
| 416 | void FGAISchedule::assign(FGScheduledFlight* ref) { flights.push_back(ref); } |
| 417 | |
| 418 | /** |
| 419 | Warning - will empty the flights vector no matter what. Use with caution! |
| 420 | */ |
| 421 | void FGAISchedule::clearAllFlights() { flights.clear(); } |
| 422 | |
| 423 | bool FGAISchedule::scheduleFlights(time_t now) |
| 424 | { |
| 425 | //string startingPort; |
| 426 | const string& userPort = fgGetString("/sim/presets/airport-id"); |
| 427 | SG_LOG(SG_AI, SG_BULK, "Scheduling Flights for : " << modelPath << " " << registration << " " << homePort); |
| 428 | FGScheduledFlight* flight = NULL; |
| 429 | SGTimeStamp start; |
| 430 | start.stamp(); |
| 431 | |
| 432 | bool first = true; |
| 433 | if (currentDestination.empty()) |
| 434 | flight = findAvailableFlight(userPort, flightIdentifier, now, (now + 6400)); |
| 435 | |
| 436 | do { |
| 437 | if ((!flight) || (!first)) { |
| 438 | flight = findAvailableFlight(currentDestination, flightIdentifier); |
| 439 | } |
| 440 | if (!flight) { |
| 441 | break; |
| 442 | } |
| 443 | |
| 444 | first = false; |
| 445 | currentDestination = flight->getArrivalAirport()->getId(); |
| 446 | //cerr << "Current destination " << currentDestination << endl; |
| 447 | if (!initialized) { |
| 448 | const string& departurePort = flight->getDepartureAirport()->getId(); |
| 449 | if (userPort == departurePort) { |
| 450 | lastRun = 1; |
| 451 | hits++; |
| 452 | } else { |
| 453 | lastRun = 0; |
| 454 | } |
| 455 | //runCount++; |
| 456 | initialized = true; |
| 457 | } |
| 458 | |
| 459 | time_t arr, dep; |
| 460 | dep = flight->getDepartureTime(); |
| 461 | arr = flight->getArrivalTime(); |
| 462 | string depT = asctime(gmtime(&dep)); |
| 463 | string arrT = asctime(gmtime(&arr)); |
| 464 | depT.resize(24); |
| 465 | arrT.resize(24); |
| 466 | SG_LOG(SG_AI, SG_BULK, " Flight " << flight->getCallSign() << ":" |
| 467 | << " " << flight->getDepartureAirport()->getId() << ":" |
| 468 | << " " << depT << ":" |
| 469 | << " \"" << flight->getArrivalAirport()->getId() << "\"" |
| 470 | << ":" |
| 471 | << " " << arrT << ":"); |
| 472 | |
| 473 | flights.push_back(flight); |
| 474 | |
| 475 | // continue processing until complete, or preempt after timeout |
| 476 | } while ((currentDestination != homePort) && |
| 477 | (start.elapsedMSec() < 3.0)); |
| 478 | |
| 479 | if (flight && (currentDestination != homePort)) { |
| 480 | // processing preempted, need to continue in next iteration |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | SG_LOG(SG_AI, SG_BULK, " Done "); |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | bool FGAISchedule::next() |
| 489 | { |
| 490 | if (!flights.empty()) { |
| 491 | flights.front()->release(); |
| 492 | flights.erase(flights.begin()); |
| 493 | } |
| 494 | |
| 495 | FGScheduledFlight* flight = findAvailableFlight(currentDestination, flightIdentifier); |
| 496 | if (!flight) { |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | currentDestination = flight->getArrivalAirport()->getId(); |
| 501 | /* |
| 502 | time_t arr, dep; |
| 503 | dep = flight->getDepartureTime(); |
| 504 | arr = flight->getArrivalTime(); |
| 505 | string depT = asctime(gmtime(&dep)); |
| 506 | string arrT = asctime(gmtime(&arr)); |
| 507 | |
| 508 | depT = depT.substr(0,24); |
| 509 | arrT = arrT.substr(0,24); |
| 510 | //cerr << " " << flight->getCallSign() << ":" |
| 511 | // << " " << flight->getDepartureAirport()->getId() << ":" |
| 512 | // << " " << depT << ":" |
| 513 | // << " \"" << flight->getArrivalAirport()->getId() << "\"" << ":" |
| 514 | // << " " << arrT << ":" << endl; |
| 515 | */ |
| 516 | flights.push_back(flight); |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | time_t FGAISchedule::getDepartureTime() |
| 521 | { |
| 522 | if (flights.empty()) |
| 523 | return 0; |
| 524 | |
| 525 | return (*flights.begin())->getDepartureTime(); |
| 526 | } |
| 527 | |
| 528 | FGAirport* FGAISchedule::getDepartureAirport() |
| 529 | { |
| 530 | if (flights.empty()) |
| 531 | return 0; |
| 532 | |
| 533 | return (*flights.begin())->getDepartureAirport(); |
| 534 | } |
| 535 | |
| 536 | FGAirport* FGAISchedule::getArrivalAirport() |
| 537 | { |
| 538 | if (flights.empty()) |
| 539 | return 0; |
| 540 | |
| 541 | return (*flights.begin())->getArrivalAirport(); |
| 542 | } |
| 543 | |
| 544 | int FGAISchedule::getCruiseAlt() |
| 545 | { |
| 546 | if (flights.empty()) |
| 547 | return 0; |
| 548 | |
| 549 | return (*flights.begin())->getCruiseAlt(); |
| 550 | } |
| 551 | |
| 552 | std::string FGAISchedule::getCallSign() |
| 553 | { |
| 554 | if (flights.empty()) |
| 555 | return std::string(); |
| 556 | |
| 557 | return (*flights.begin())->getCallSign(); |
| 558 | } |
| 559 | |
| 560 | std::string FGAISchedule::getFlightRules() |
| 561 | { |
| 562 | if (flights.empty()) |
| 563 | return std::string(); |
| 564 | |
| 565 | return (*flights.begin())->getFlightRules(); |
| 566 | } |
| 567 | |
| 568 | FGScheduledFlight* FGAISchedule::findAvailableFlight(const string& currentDestination, |
| 569 | const string& req, |
| 570 | time_t min, time_t max) |
| 571 | { |
| 572 | time_t now = globals->get_time_params()->get_cur_time(); |
| 573 | |
| 574 | auto tmgr = globals->get_subsystem<FGTrafficManager>(); |
| 575 | FGScheduledFlightVecIterator fltBegin, fltEnd; |
| 576 | fltBegin = tmgr->getFirstFlight(req); |
| 577 | fltEnd = tmgr->getLastFlight(req); |
| 578 | |
| 579 | |
| 580 | SG_LOG(SG_AI, SG_BULK, "Finding available flight for " << req << " at " << now); |
| 581 | // For Now: |
| 582 | // Traverse every registered flight |
| 583 | if (fltBegin == fltEnd) { |
| 584 | SG_LOG(SG_AI, SG_BULK, "No Flights Scheduled for " << req); |
| 585 | } |
| 586 | |
| 587 | for (FGScheduledFlightVecIterator i = fltBegin; i != fltEnd; ++i) { |
| 588 | (*i)->adjustTime(now); |
| 589 | } |
| 590 | std::sort(fltBegin, fltEnd, FGScheduledFlight::compareScheduledFlights); |
| 591 | for (FGScheduledFlightVecIterator i = fltBegin; i != fltEnd; ++i) { |
| 592 | //bool valid = true; |
| 593 | if (!(*i)->isAvailable()) { |
| 594 | SG_LOG(SG_AI, SG_BULK, "" << (*i)->getCallSign() << "is no longer available"); |
| 595 | |
| 596 | //cerr << (*i)->getCallSign() << "is no longer available" << endl; |
| 597 | continue; |
| 598 | } |
| 599 | if (!((*i)->getRequirement() == req)) { |
| 600 | SG_LOG(SG_AI, SG_BULK, "" << (*i)->getCallSign() << " no requirement " << (*i)->getRequirement() << " " << req); |
| 601 | continue; |
| 602 | } |
| 603 | if (!(((*i)->getArrivalAirport()) && ((*i)->getDepartureAirport()))) { |
| 604 | continue; |
| 605 | } |
| 606 | if (!(currentDestination.empty())) { |
| 607 | if (currentDestination != (*i)->getDepartureAirport()->getId()) { |
| 608 | SG_LOG(SG_AI, SG_BULK, (*i)->getCallSign() << " not matching departure."); |
| 609 | //cerr << (*i)->getCallSign() << "Doesn't match destination" << endl; |
| 610 | //cerr << "Current Destination " << currentDestination << "doesn't match flight's " << |
| 611 | // (*i)->getArrivalAirport()->getId() << endl; |
| 612 | continue; |
| 613 | } |
| 614 | } |
| 615 | if (!flights.empty()) { |
| 616 | time_t arrival = flights.back()->getArrivalTime(); |
| 617 | time_t departure = (*i)->getDepartureTime(); |
| 618 | int groundTime = groundTimeFromRadius(); |
| 619 | if (departure < (arrival + (groundTime))) { |
| 620 | SG_LOG(SG_AI, SG_BULK, "Not flight candidate : " << (*i)->getCallSign() << " Flight Arrival : " << arrival << " Planned Departure : " << departure << " < " << (arrival + groundTime) << " Diff between arrival + groundtime and departure : " << (arrival + groundTime - departure) << " Groundtime : " << groundTime); |
| 621 | continue; |
| 622 | } else { |
| 623 | SG_LOG(SG_AI, SG_BULK, "Next flight candidate : " << (*i)->getCallSign()); |
| 624 | } |
| 625 | } |
| 626 | if (min != 0) { |
| 627 | time_t dep = (*i)->getDepartureTime(); |
| 628 | if ((dep < min) || (dep > max)) |
| 629 | continue; |
| 630 | } |
| 631 | |
| 632 | // So, if we actually get here, we have a winner |
| 633 | //cerr << "found flight: " << req << " : " << currentDestination << " : " << |
| 634 | // (*i)->getArrivalAirport()->getId() << endl; |
| 635 | (*i)->lock(); |
| 636 | return (*i); |
| 637 | } |
| 638 | // matches req? |
| 639 | // if currentDestination has a value, does it match departure of next flight? |
| 640 | // is departure time later than planned arrival? |
| 641 | // is departure port valid? |
| 642 | // is arrival port valid? |
| 643 | //cerr << "Ack no flight found: " << endl; |
| 644 | return NULL; |
| 645 | } |
| 646 | |
| 647 | int FGAISchedule::groundTimeFromRadius() |
| 648 | { |
| 649 | if (radius < 10) |
| 650 | return 15 * 60; |
| 651 | else if (radius < 15) |
| 652 | return 20 * 60; |
| 653 | else if (radius < 20) |
| 654 | return 30 * 60; |
| 655 | else if (radius < 25) |
| 656 | return 50 * 60; |
| 657 | else if (radius < 30) |
| 658 | return 90 * 60; |
| 659 | else |
| 660 | return 120 * 60; |
| 661 | } |
| 662 | |
| 663 | |
| 664 | double FGAISchedule::getSpeed() |
| 665 | { |
| 666 | FGScheduledFlightVecIterator i = flights.begin(); |
| 667 | |
| 668 | FGAirport *dep = (*i)->getDepartureAirport(), |
| 669 | *arr = (*i)->getArrivalAirport(); |
| 670 | double dist = SGGeodesy::distanceNm(dep->geod(), arr->geod()); |
| 671 | double remainingTimeEnroute = (*i)->getArrivalTime() - (*i)->getDepartureTime(); |
| 672 | |
| 673 | double speed = 0.0; |
| 674 | if (remainingTimeEnroute > 0.01) |
| 675 | speed = dist / (remainingTimeEnroute / 3600.0); |
| 676 | |
| 677 | SG_CLAMP_RANGE(speed, 300.0, 500.0); |
| 678 | return speed; |
| 679 | } |
| 680 | |
| 681 | void FGAISchedule::setScore() |
| 682 | { |
| 683 | if (runCount) { |
| 684 | score = ((double)hits / (double)runCount); |
| 685 | } else { |
| 686 | if (homePort == fgGetString("/sim/presets/airport-id")) { |
| 687 | score = 0.1; |
| 688 | } else { |
| 689 | score = 0.0; |
| 690 | } |
| 691 | } |
| 692 | runCount++; |
| 693 | } |
| 694 | |
| 695 | bool FGAISchedule::compareSchedules(const FGAISchedule* a, const FGAISchedule* b) |
| 696 | { |
| 697 | return (*a) < (*b); |
| 698 | } |
| 699 | |
| 700 | bool FGAISchedule::operator<(const FGAISchedule& other) const |
| 701 | { |
| 702 | //cerr << "Sorting " << registration << " and " << other.registration << endl; |
| 703 | double currentScore = score * (1.5 - lastRun); |
| 704 | double otherScore = other.score * (1.5 - other.lastRun); |
| 705 | return currentScore > otherScore; |
| 706 | } |