1 // dynamics.cxx - Code to manage the higher order airport ground activities
2 // Written by Durk Talsma, started December 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.
27 #include <simgear/compiler.h>
31 #include <Environment/environment_mgr.hxx>
32 #include <Environment/environment.hxx>
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/props/props.hxx>
35 #include <simgear/structure/subsystem_mgr.hxx>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/route/waypoint.hxx>
38 #include <Main/globals.hxx>
39 #include <Main/fg_props.hxx>
40 #include <Airports/runways.hxx>
48 using std::random_shuffle;
51 #include "dynamics.hxx"
53 FGAirportDynamics::FGAirportDynamics(FGAirport* ap) :
54 _ap(ap), rwyPrefs(ap), SIDs(ap) {
57 // For testing only. This needs to be refined when we move ATIS functionality over.
58 atisInformation = "Sierra";
61 // Note that the ground network should also be copied
62 FGAirportDynamics::FGAirportDynamics(const FGAirportDynamics& other) :
63 rwyPrefs(other.rwyPrefs),
66 for (FGParkingVecConstIterator ip= other.parkings.begin(); ip != other.parkings.end(); ip++)
67 parkings.push_back(*(ip));
68 // rwyPrefs = other.rwyPrefs;
69 lastUpdate = other.lastUpdate;
71 stringVecConstIterator il;
72 for (il = other.landing.begin(); il != other.landing.end(); il++)
73 landing.push_back(*il);
74 for (il = other.takeoff.begin(); il != other.takeoff.end(); il++)
75 takeoff.push_back(*il);
76 lastUpdate = other.lastUpdate;
77 atisInformation = other.atisInformation;
81 FGAirportDynamics::~FGAirportDynamics()
86 // Initialization required after XMLRead
87 void FGAirportDynamics::init()
89 // This may seem a bit weird to first randomly shuffle the parkings
90 // and then sort them again. However, parkings are sorted here by ascending
91 // radius. Since many parkings have similar radii, with each radius class they will
92 // still be allocated relatively systematically. Randomizing prior to sorting will
93 // prevent any initial orderings to be destroyed, leading (hopefully) to a more
94 // naturalistic gate assignment.
95 random_shuffle(parkings.begin(), parkings.end());
96 sort(parkings.begin(), parkings.end());
97 // add the gate positions to the ground network.
98 groundNetwork.addNodes(&parkings);
100 groundNetwork.setTowerController(&towerController);
101 groundNetwork.setParent(_ap);
104 bool FGAirportDynamics::getAvailableParking(double *lat, double *lon, double *heading, int *gateId, double rad, const string &flType, const string &acType, const string &airline)
107 bool available = false;
110 FGParkingVecIterator i;
111 // if (flType == "cargo")
113 // gateType = "RAMP_CARGO";
115 // else if (flType == "ga")
117 // gateType = "RAMP_GA";
119 // else gateType = "GATE";
121 if (parkings.begin() == parkings.end())
123 //cerr << "Could not find parking spot at " << _ap->getId() << endl;
124 *lat = _ap->getLatitude();
125 *lon = _ap->getLongitude();
131 // First try finding a parking with a designated airline code
132 for (i = parkings.begin(); !(i == parkings.end() || found); i++)
134 //cerr << "Gate Id: " << i->getIndex()
135 // << " Type : " << i->getType()
136 // << " Codes : " << i->getCodes()
137 // << " Radius: " << i->getRadius()
138 // << " Name : " << i->getName()
139 // << " Available: " << i->isAvailable() << endl;
141 // Taken by another aircraft
142 if (!(i->isAvailable()))
147 // No airline codes, so skip
148 if (i->getCodes().empty())
153 else // Airline code doesn't match
155 //cerr << "Code = " << airline << ": Codes " << i->getCodes();
156 if (i->getCodes().find(airline, 0) == string::npos)
159 //cerr << "Unavailable" << endl;
164 //cerr << "Available" << endl;
167 // Type doesn't match
168 if (i->getType() != flType)
174 if (i->getRadius() < rad)
182 *lat = i->getLatitude ();
183 *lon = i->getLongitude();
184 *heading = i->getHeading ();
185 *gateId = i->getIndex ();
186 i->setAvailable(false);
190 // then try again for those without codes.
191 for (i = parkings.begin(); !(i == parkings.end() || found); i++)
194 if (!(i->isAvailable()))
199 if (!(i->getCodes().empty()))
201 if ((i->getCodes().find(airline,0) == string::npos))
207 if (i->getType() != flType)
213 if (i->getRadius() < rad)
221 *lat = i->getLatitude ();
222 *lon = i->getLongitude();
223 *heading = i->getHeading ();
224 *gateId = i->getIndex ();
225 i->setAvailable(false);
229 // And finally once more if that didn't work. Now ignore the airline codes, as a last resort
230 for (i = parkings.begin(); !(i == parkings.end() || found); i++)
233 if (!(i->isAvailable()))
238 if (i->getType() != flType)
244 if (i->getRadius() < rad)
252 *lat = i->getLatitude ();
253 *lon = i->getLongitude();
254 *heading = i->getHeading ();
255 *gateId = i->getIndex ();
256 i->setAvailable(false);
263 //cerr << "Traffic overflow at" << _ap->getId()
264 // << ". flType = " << flType
265 // << ". airline = " << airline
266 // << " Radius = " <<rad
268 *lat = _ap->getLatitude();
269 *lon = _ap->getLongitude();
277 void FGAirportDynamics::getParking (int id, double *lat, double* lon, double *heading)
281 *lat = _ap->getLatitude();
282 *lon = _ap->getLongitude();
287 FGParkingVecIterator i = parkings.begin();
288 for (i = parkings.begin(); i != parkings.end(); i++)
290 if (id == i->getIndex())
292 *lat = i->getLatitude();
293 *lon = i->getLongitude();
294 *heading = i->getHeading();
300 FGParking *FGAirportDynamics::getParking(int id)
302 FGParkingVecIterator i = parkings.begin();
303 for (i = parkings.begin(); i != parkings.end(); i++)
305 if (id == i->getIndex()) {
311 string FGAirportDynamics::getParkingName(int id)
313 FGParkingVecIterator i = parkings.begin();
314 for (i = parkings.begin(); i != parkings.end(); i++)
316 if (id == i->getIndex()) {
321 return string("overflow");
323 void FGAirportDynamics::releaseParking(int id)
328 FGParkingVecIterator i = parkings.begin();
329 for (i = parkings.begin(); i != parkings.end(); i++)
331 if (id == i->getIndex())
333 i -> setAvailable(true);
339 void FGAirportDynamics::setRwyUse(const FGRunwayPreference& ref)
342 //cerr << "Exiting due to not implemented yet" << endl;
346 bool FGAirportDynamics::innerGetActiveRunway(const string &trafficType, int action, string &runway, double heading)
355 if (!rwyPrefs.available()) {
359 RunwayGroup *currRunwayGroup = 0;
360 int nrActiveRunways = 0;
361 time_t dayStart = fgGetLong("/sim/time/utc/day-seconds");
362 if ((abs((long)(dayStart - lastUpdate)) > 600) || trafficType != prevTrafficType)
366 lastUpdate = dayStart;
367 prevTrafficType = trafficType;
370 stationweather = ((FGEnvironmentMgr *) globals->get_subsystem("environment"))
371 ->getEnvironment(getLatitude(),
375 windSpeed = stationweather.get_wind_speed_kt();
376 windHeading = stationweather.get_wind_from_heading_deg();
378 //cerr << "finding active Runway for" << _ap->getId() << endl;
379 //cerr << "Nr of seconds since day start << " << dayStart << endl;
381 ScheduleTime *currSched;
382 //cerr << "A"<< endl;
383 currSched = rwyPrefs.getSchedule(trafficType.c_str());
386 //cerr << "B"<< endl;
387 scheduleName = currSched->getName(dayStart);
388 maxTail = currSched->getTailWind ();
389 maxCross = currSched->getCrossWind ();
390 //cerr << "SChedule anme = " << scheduleName << endl;
391 if (scheduleName.empty())
393 //cerr << "C"<< endl;
394 currRunwayGroup = rwyPrefs.getGroup(scheduleName);
395 //cerr << "D"<< endl;
396 if (!(currRunwayGroup))
398 nrActiveRunways = currRunwayGroup->getNrActiveRunways();
400 // Keep a history of the currently active runways, to ensure
401 // that an already established selection of runways will not
402 // be overridden once a more preferred selection becomes
403 // available as that can lead to random runway swapping.
404 if (trafficType == "com") {
405 currentlyActive = &comActive;
406 } else if (trafficType == "gen") {
407 currentlyActive = &genActive;
408 } else if (trafficType == "mil") {
409 currentlyActive = &milActive;
410 } else if (trafficType == "ul") {
411 currentlyActive = &ulActive;
414 currRunwayGroup->setActive(_ap,
421 // Note that I SHOULD keep multiple lists in memory, one for
422 // general aviation, one for commercial and one for military
424 currentlyActive->clear();
425 nrActiveRunways = currRunwayGroup->getNrActiveRunways();
426 //cerr << "Choosing runway for " << trafficType << endl;
427 for (int i = 0; i < nrActiveRunways; i++)
429 type = "unknown"; // initialize to something other than landing or takeoff
430 currRunwayGroup->getActive(i, name, type);
431 if (type == "landing")
433 landing.push_back(name);
434 currentlyActive->push_back(name);
435 //cerr << "Landing " << name << endl;
437 if (type == "takeoff")
439 takeoff.push_back(name);
440 currentlyActive->push_back(name);
441 //cerr << "takeoff " << name << endl;
447 if (action == 1) // takeoff
449 int nr = takeoff.size();
452 // Note that the randomization below, is just a placeholder to choose between
453 // multiple active runways for this action. This should be
454 // under ATC control.
455 runway = chooseRwyByHeading (takeoff, heading);
459 runway = chooseRunwayFallback();
463 if (action == 2) // landing
465 int nr = landing.size();
468 runway = chooseRwyByHeading (landing, heading);
472 runway = chooseRunwayFallback();
479 string FGAirportDynamics::chooseRwyByHeading(stringVec rwys, double heading) {
480 double bestError = 360.0;
481 double rwyHeading, headingError;
483 for (stringVecIterator i = rwys.begin(); i != rwys.end(); i++) {
484 FGRunway *rwy = _ap->getRunwayByIdent((*i));
485 rwyHeading = rwy->headingDeg();
486 headingError = fabs(heading - rwyHeading);
487 if (headingError > 180)
488 headingError = fabs(headingError - 360);
489 if (headingError < bestError) {
491 bestError = headingError;
494 //cerr << "Using active runway " << runway << " for heading " << heading << endl;
498 void FGAirportDynamics::getActiveRunway(const string &trafficType, int action, string &runway, double heading)
500 bool ok = innerGetActiveRunway(trafficType, action, runway, heading);
502 runway = chooseRunwayFallback();
506 string FGAirportDynamics::chooseRunwayFallback()
508 FGRunway* rwy = _ap->getActiveRunwayForUsage();
512 void FGAirportDynamics::addParking(FGParking& park) {
513 parkings.push_back(park);
516 double FGAirportDynamics::getLatitude() const {
517 return _ap->getLatitude();
520 double FGAirportDynamics::getLongitude() const {
521 return _ap->getLongitude();
524 double FGAirportDynamics::getElevation() const {
525 return _ap->getElevation();
528 const string& FGAirportDynamics::getId() const {
532 // Experimental: Return a different ground frequency depending on the leg of the
533 // Flight. Leg should always have a minimum value of two when this function is called.
534 // Note that in this scheme, the assignment of various frequencies to various ground
535 // operations is completely arbitrary. As such, is a short cut I need to take now,
536 // so that at least I can start working on assigning different frequencies to different
539 int FGAirportDynamics::getGroundFrequency(unsigned leg) {
540 //return freqGround.size() ? freqGround[0] : 0; };
543 SG_LOG(SG_ATC, SG_ALERT, "Leg value is smaller than two at " << SG_ORIGIN);
545 if (freqGround.size() == 0) {
548 if ((freqGround.size() > leg-1) && (leg > 1)) {
549 groundFreq = freqGround[leg-1];
551 if ((freqGround.size() < leg-1) && (leg > 1)) {
552 groundFreq = (freqGround.size() < (leg-1)) ? freqGround[freqGround.size()-1] : freqGround[leg-2];
554 if ((freqGround.size() >= leg-1) && (leg > 1)) {
555 groundFreq = freqGround[leg-2];
560 FGAIFlightPlan *FGAirportDynamics::getSID(string activeRunway, double heading)
562 return SIDs.getBest(activeRunway, heading);