1 /******************************************************************************
3 * Written by Durk Talsma, started May 5, 2004.
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.
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.
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.
20 ****************************************************************************
22 *****************************************************************************/
40 #include <simgear/compiler.h>
41 #include <simgear/sg_inlines.h>
42 #include <simgear/math/sg_geodesy.hxx>
43 #include <simgear/props/props.hxx>
44 #include <simgear/route/waypoint.hxx>
45 #include <simgear/structure/subsystem_mgr.hxx>
46 #include <simgear/xml/easyxml.hxx>
48 #include <AIModel/AIFlightPlan.hxx>
49 #include <AIModel/AIManager.hxx>
50 #include <AIModel/AIAircraft.hxx>
51 #include <Airports/simple.hxx>
52 #include <Main/fg_init.hxx> // That's pretty ugly, but I need fgFindAirportID
55 #include "SchedFlight.hxx"
56 #include "TrafficMgr.hxx"
58 /******************************************************************************
59 * the FGAISchedule class contains data members and code to maintain a
60 * schedule of Flights for an articically controlled aircraft.
61 *****************************************************************************/
62 FGAISchedule::FGAISchedule()
75 FGAISchedule::FGAISchedule(string mdl,
86 FGScheduledFlightVec flt)*/
87 FGAISchedule::FGAISchedule(string model,
104 flightIdentifier = flightId;
113 /*for (FGScheduledFlightVecIterator i = flt.begin();
116 flights.push_back(new FGScheduledFlight((*(*i))));*/
125 FGAISchedule::FGAISchedule(const FGAISchedule &other)
127 modelPath = other.modelPath;
128 homePort = other.homePort;
129 livery = other.livery;
130 registration = other.registration;
132 flightIdentifier = other.flightIdentifier;
133 flights = other.flights;
134 AIManagerRef = other.AIManagerRef;
135 acType = other.acType;
136 airline = other.airline;
137 m_class = other.m_class;
138 firstRun = other.firstRun;
139 radius = other.radius;
140 groundOffset = other.groundOffset;
141 flightType = other.flightType;
143 distanceToUser = other.distanceToUser;
144 currentDestination = other.currentDestination;
145 firstRun = other.firstRun;
146 runCount = other.runCount;
148 initialized = other.initialized;
153 FGAISchedule::~FGAISchedule()
155 /* for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
162 bool FGAISchedule::init()
165 //SGTime* currTimeDate = globals->get_time_params();
167 //tm *temp = currTimeDate->getGmt();
169 //sgTimeFormatTime(&targetTimeDate, buffer);
170 //cout << "Scheduled Time " << buffer << endl;
171 //cout << "Time :" << time(NULL) << " SGTime : " << sgTimeGetGMT(temp) << endl;
172 /*for (FGScheduledFlightVecIterator i = flights.begin();
176 //i->adjustTime(now);
177 if (!((*i)->initializeAirports()))
180 //sort(flights.begin(), flights.end());
181 // Since time isn't initialized yet when this function is called,
182 // Find the closest possible airport.
183 // This should give a reasonable initialization order.
184 //setClosestDistanceToUser();
188 bool FGAISchedule::update(time_t now, const SGVec3d& userCart)
193 remainingTimeEnroute,
197 if (flights.empty()) { // No flights available for this aircraft
201 // Sort all the scheduled flights according to scheduled departure time.
202 // Because this is done at every update, we only need to check the status
203 // of the first listed flight.
204 //sort(flights.begin(), flights.end(), compareScheduledFlights);
207 if (fgGetBool("/sim/traffic-manager/instantaneous-action") == true) {
208 deptime = now; // + rand() % 300; // Wait up to 5 minutes until traffic starts moving to prevent too many aircraft
209 // from cluttering the gate areas.
214 FGScheduledFlight* flight = flights.front();
216 deptime = flight->getDepartureTime();
217 //cerr << "Settiing departure time " << deptime << endl;
221 // Check if this aircraft has been released.
222 FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("Traffic Manager");
223 if (tmgr->isReleased(AIManagerRef)) {
226 return true; // in visual range, let the AIManager handle it
230 // This flight entry is entirely in the past, do we need to
231 // push it forward in time to the next scheduled departure.
232 if (flight->getArrivalTime() < now) {
233 SG_LOG (SG_GENERAL, SG_BULK, "Traffic Manager: Flight is in the Past");
234 // Don't just update: check whether we need to load a new leg. etc.
235 // This update occurs for distant aircraft, so we can update the current leg
236 // and detach it from the current list of aircraft.
238 flights.erase(flights.begin()); // pop_front(), effectively
242 FGAirport* dep = flight->getDepartureAirport();
243 FGAirport* arr = flight->getArrivalAirport();
248 double speed = 450.0;
250 totalTimeEnroute = flight->getArrivalTime() - flight->getDepartureTime();
251 if (flight->getDepartureTime() < now) {
252 elapsedTimeEnroute = now - flight->getDepartureTime();
253 remainingTimeEnroute = totalTimeEnroute - elapsedTimeEnroute;
254 double x = elapsedTimeEnroute / (double) totalTimeEnroute;
256 // current pos is based on great-circle course between departure/arrival,
257 // with percentage of distance travelled, based upon percentage of time
259 double course, az2, distanceM;
260 SGGeodesy::inverse(dep->geod(), arr->geod(), course, az2, distanceM);
261 double coveredDistance = distanceM * x;
263 SGGeodesy::direct(dep->geod(), course, coveredDistance, position, az2);
265 SG_LOG (SG_GENERAL, SG_BULK, "Traffic Manager: Flight is in progress, %=" << x);
266 speed = ((distanceM - coveredDistance) * SG_METER_TO_NM) / 3600.0;
269 remainingTimeEnroute = totalTimeEnroute;
270 elapsedTimeEnroute = 0;
271 position = dep->geod();
272 SG_LOG (SG_GENERAL, SG_BULK, "Traffic Manager: Flight is pending, departure in "
273 << flight->getDepartureTime() - now << " seconds ");
276 // departure / arrival coincident
277 remainingTimeEnroute = totalTimeEnroute = 0.0;
278 elapsedTimeEnroute = 0;
279 position = dep->geod();
282 // cartesian calculations are more numerically stable over the (potentially)
283 // large distances involved here: see bug #80
284 distanceToUser = dist(userCart, SGVec3d::fromGeod(position)) * SG_METER_TO_NM;
286 // If distance between user and simulated aircaft is less
287 // then 500nm, create this flight. At jet speeds 500 nm is roughly
288 // one hour flight time, so that would be a good approximate point
289 // to start a more detailed simulation of this aircraft.
290 SG_LOG (SG_GENERAL, SG_BULK, "Traffic manager: " << registration << " is scheduled for a flight from "
291 << dep->getId() << " to " << arr->getId() << ". Current distance to user: "
293 if (distanceToUser >= TRAFFICTOAIDISTTOSTART) {
294 return true; // out of visual range, for the moment.
296 return createAIAircraft(flight, speed, deptime);
299 bool FGAISchedule::createAIAircraft(FGScheduledFlight* flight, double speedKnots, time_t deptime)
301 FGAirport* dep = flight->getDepartureAirport();
302 FGAirport* arr = flight->getArrivalAirport();
303 string flightPlanName = dep->getId() + "-" + arr->getId() + ".xml";
304 SG_LOG(SG_GENERAL, SG_INFO, "Traffic manager: Creating AIModel from:" << flightPlanName);
306 // Only allow traffic to be created when the model path (or the AI version of mp) exists
307 SGPath mp(globals->get_fg_root());
310 mp.append(modelPath);
312 mp_ai.append(modelPath);
314 if (!mp.exists() && !mp_ai.exists()) {
315 SG_LOG(SG_INPUT, SG_WARN, "TrafficManager: Could not load model " << mp.str());
319 FGAIAircraft *aircraft = new FGAIAircraft(this);
320 aircraft->setPerformance(m_class); //"jet_transport";
321 aircraft->setCompany(airline); //i->getAirline();
322 aircraft->setAcType(acType); //i->getAcType();
323 aircraft->setPath(modelPath.c_str());
324 //aircraft->setFlightPlan(flightPlanName);
325 aircraft->setLatitude(position.getLatitudeDeg());
326 aircraft->setLongitude(position.getLongitudeDeg());
327 aircraft->setAltitude(flight->getCruiseAlt()*100); // convert from FL to feet
328 aircraft->setSpeed(speedKnots);
329 aircraft->setBank(0);
331 courseToDest = SGGeodesy::courseDeg(position, arr->geod());
332 aircraft->SetFlightPlan(new FGAIFlightPlan(aircraft, flightPlanName, courseToDest, deptime,
333 dep, arr, true, radius,
334 flight->getCruiseAlt()*100,
335 position.getLatitudeDeg(),
336 position.getLongitudeDeg(),
337 speedKnots, flightType, acType,
341 FGAIManager* aimgr = (FGAIManager *) globals-> get_subsystem("ai_model");
342 aimgr->attach(aircraft);
343 AIManagerRef = aircraft->getID();
347 void FGAISchedule::scheduleFlights()
349 if (!flights.empty()) {
353 SG_LOG(SG_GENERAL, SG_BULK, "Scheduling for : " << modelPath << " " << registration << " " << homePort);
354 FGScheduledFlight *flight = NULL;
356 flight = findAvailableFlight(currentDestination, flightIdentifier);
361 currentDestination = flight->getArrivalAirport()->getId();
363 string departurePort = flight->getDepartureAirport()->getId();
364 //cerr << "Scheduled " << registration << " " << score << " for Flight "
365 // << flight-> getCallSign() << " from " << departurePort << " to " << currentDestination << endl;
366 if (fgGetString("/sim/presets/airport-id") == departurePort) {
374 dep = flight->getDepartureTime();
375 arr = flight->getArrivalTime();
376 string depT = asctime(gmtime(&dep));
377 string arrT = asctime(gmtime(&arr));
379 depT = depT.substr(0,24);
380 arrT = arrT.substr(0,24);
381 SG_LOG(SG_GENERAL, SG_BULK, " " << flight->getCallSign() << ":"
382 << " " << flight->getDepartureAirport()->getId() << ":"
383 << " " << depT << ":"
384 << " \"" << flight->getArrivalAirport()->getId() << "\"" << ":"
385 << " " << arrT << ":");
387 flights.push_back(flight);
388 } while (currentDestination != homePort);
389 SG_LOG(SG_GENERAL, SG_BULK, " Done ");
392 bool FGAISchedule::next()
394 if (!flights.empty()) {
395 flights.front()->release();
396 flights.erase(flights.begin());
399 FGScheduledFlight *flight = findAvailableFlight(currentDestination, flightIdentifier);
404 currentDestination = flight->getArrivalAirport()->getId();
407 dep = flight->getDepartureTime();
408 arr = flight->getArrivalTime();
409 string depT = asctime(gmtime(&dep));
410 string arrT = asctime(gmtime(&arr));
412 depT = depT.substr(0,24);
413 arrT = arrT.substr(0,24);
414 //cerr << " " << flight->getCallSign() << ":"
415 // << " " << flight->getDepartureAirport()->getId() << ":"
416 // << " " << depT << ":"
417 // << " \"" << flight->getArrivalAirport()->getId() << "\"" << ":"
418 // << " " << arrT << ":" << endl;
420 flights.push_back(flight);
424 FGScheduledFlight* FGAISchedule::findAvailableFlight (const string ¤tDestination,
427 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
429 FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("Traffic Manager");
430 FGScheduledFlightVecIterator fltBegin, fltEnd;
431 fltBegin = tmgr->getFirstFlight(req);
432 fltEnd = tmgr->getLastFlight(req);
435 //cerr << "Finding available flight " << endl;
437 // Traverse every registered flight
438 if (fltBegin == fltEnd) {
439 //cerr << "No Flights Scheduled for " << req << endl;
442 for (FGScheduledFlightVecIterator i = fltBegin; i != fltEnd; i++) {
443 (*i)->adjustTime(now);
444 //sort(fltBegin, fltEnd, compareScheduledFlights);
445 //cerr << counter++ << endl;
447 std::sort(fltBegin, fltEnd, compareScheduledFlights);
448 for (FGScheduledFlightVecIterator i = fltBegin; i != fltEnd; i++) {
451 if (!(*i)->isAvailable()) {
452 //cerr << (*i)->getCallSign() << "is no longer available" << endl;
455 if (!((*i)->getRequirement() == req)) {
458 if (!(((*i)->getArrivalAirport()) && ((*i)->getDepartureAirport()))) {
461 if (!(currentDestination.empty())) {
462 if (currentDestination != (*i)->getDepartureAirport()->getId()) {
463 //cerr << (*i)->getCallSign() << "Doesn't match destination" << endl;
464 //cerr << "Current Destination " << currentDestination << "Doesnt match flight's " <<
465 // (*i)->getArrivalAirport()->getId() << endl;
470 // So, if we actually get here, we have a winner
471 //cerr << "found flight: " << req << " : " << currentDestination << " : " <<
472 // (*i)->getArrivalAirport()->getId() << endl;
477 // if currentDestination has a value, does it match departure of next flight?
478 // is departure time later than planned arrival?
479 // is departure port valid?
480 // is arrival port valid?
481 //cerr << "Ack no flight found: " << endl;
485 double FGAISchedule::getSpeed()
487 FGScheduledFlightVecIterator i = flights.begin();
489 FGAirport* dep = (*i)->getDepartureAirport(),
490 *arr = (*i)->getArrivalAirport();
491 double dist = SGGeodesy::distanceNm(dep->geod(), arr->geod());
492 double remainingTimeEnroute = (*i)->getArrivalTime() - (*i)->getDepartureTime();
494 double speed = dist / (remainingTimeEnroute/3600.0);
495 SG_CLAMP_RANGE(speed, 300.0, 500.0);
499 void FGAISchedule::setScore ()
502 score = ((double) hits / (double) runCount);
504 if (homePort == fgGetString("/sim/presets/airport-id")) {
513 bool compareSchedules(FGAISchedule*a, FGAISchedule*b)
519 // void FGAISchedule::setClosestDistanceToUser()
528 // elapsedTimeEnroute;
530 // double userLatitude = fgGetDouble("/position/latitude-deg");
531 // double userLongitude = fgGetDouble("/position/longitude-deg");
535 // #if defined( __CYGWIN__) || defined( __MINGW32__)
536 // #define HUGE HUGE_VAL
538 // distanceToUser = HUGE;
539 // FGScheduledFlightVecIterator i = flights.begin();
540 // while (i != flights.end())
542 // dep = i->getDepartureAirport();
546 // SGWayPoint user ( userLongitude,
548 // i->getCruiseAlt());
549 // SGWayPoint current (dep->getLongitude(),
550 // dep->getLatitude(),
552 // user.CourseAndDistance(current, &course, &dist);
553 // if (dist < distanceToUser)
555 // distanceToUser = dist;
556 // //cerr << "Found closest distance to user for " << registration << " to be " << distanceToUser << " at airport " << dep->getId() << endl;
560 // //return distToUser;