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>
29 #include <Environment/environment_mgr.hxx>
30 #include <Environment/environment.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/props/props.hxx>
33 #include <simgear/structure/subsystem_mgr.hxx>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/route/waypoint.hxx>
36 #include <Main/globals.hxx>
37 #include <Main/fg_props.hxx>
38 #include <Airports/runways.hxx>
46 using std::random_shuffle;
49 #include "dynamics.hxx"
51 FGAirportDynamics::FGAirportDynamics(FGAirport * ap):
52 _ap(ap), rwyPrefs(ap), SIDs(ap)
56 // For testing only. This needs to be refined when we move ATIS functionality over.
57 atisInformation = "Sierra";
60 // Note that the ground network should also be copied
62 FGAirportDynamics(const FGAirportDynamics & other):rwyPrefs(other.
66 for (FGParkingVecConstIterator ip = other.parkings.begin();
67 ip != other.parkings.end(); ip++)
68 parkings.push_back(*(ip));
69 // rwyPrefs = other.rwyPrefs;
70 lastUpdate = other.lastUpdate;
72 stringVecConstIterator il;
73 for (il = other.landing.begin(); il != other.landing.end(); il++)
74 landing.push_back(*il);
75 for (il = other.takeoff.begin(); il != other.takeoff.end(); il++)
76 takeoff.push_back(*il);
77 lastUpdate = other.lastUpdate;
78 atisInformation = other.atisInformation;
82 FGAirportDynamics::~FGAirportDynamics()
87 // Initialization required after XMLRead
88 void FGAirportDynamics::init()
90 // This may seem a bit weird to first randomly shuffle the parkings
91 // and then sort them again. However, parkings are sorted here by ascending
92 // radius. Since many parkings have similar radii, with each radius class they will
93 // still be allocated relatively systematically. Randomizing prior to sorting will
94 // prevent any initial orderings to be destroyed, leading (hopefully) to a more
95 // naturalistic gate assignment.
96 random_shuffle(parkings.begin(), parkings.end());
97 sort(parkings.begin(), parkings.end());
98 // add the gate positions to the ground network.
99 groundNetwork.setParent(_ap);
100 groundNetwork.addNodes(&parkings);
101 groundNetwork.init();
102 groundNetwork.setTowerController(&towerController);
106 bool FGAirportDynamics::getAvailableParking(double *lat, double *lon,
107 double *heading, int *gateId,
109 const string & flType,
110 const string & acType,
111 const string & airline)
114 bool available = false;
117 FGParkingVecIterator i;
118 if (parkings.begin() == parkings.end()) {
119 //cerr << "Could not find parking spot at " << _ap->getId() << endl;
120 *lat = _ap->getLatitude();
121 *lon = _ap->getLongitude();
125 // First try finding a parking with a designated airline code
126 for (i = parkings.begin(); !(i == parkings.end() || found); i++) {
128 // Taken by another aircraft
129 if (!(i->isAvailable())) {
133 // No airline codes, so skip
134 if (i->getCodes().empty()) {
137 } else { // Airline code doesn't match
138 //cerr << "Code = " << airline << ": Codes " << i->getCodes();
139 if (i->getCodes().find(airline, 0) == string::npos) {
141 //cerr << "Unavailable" << endl;
144 //cerr << "Available" << endl;
147 // Type doesn't match
148 if (i->getType() != flType) {
153 if (i->getRadius() < rad) {
159 *lat = i->getLatitude();
160 *lon = i->getLongitude();
161 *heading = i->getHeading();
162 *gateId = i->getIndex();
163 i->setAvailable(false);
167 // then try again for those without codes.
168 for (i = parkings.begin(); !(i == parkings.end() || found); i++) {
170 if (!(i->isAvailable())) {
174 if (!(i->getCodes().empty())) {
175 if ((i->getCodes().find(airline, 0) == string::npos)) {
180 if (i->getType() != flType) {
185 if (i->getRadius() < rad) {
191 *lat = i->getLatitude();
192 *lon = i->getLongitude();
193 *heading = i->getHeading();
194 *gateId = i->getIndex();
195 i->setAvailable(false);
199 // And finally once more if that didn't work. Now ignore the airline codes, as a last resort
200 for (i = parkings.begin(); !(i == parkings.end() || found); i++) {
202 if (!(i->isAvailable())) {
206 if (i->getType() != flType) {
211 if (i->getRadius() < rad) {
217 *lat = i->getLatitude();
218 *lon = i->getLongitude();
219 *heading = i->getHeading();
220 *gateId = i->getIndex();
221 i->setAvailable(false);
227 //cerr << "Traffic overflow at" << _ap->getId()
228 // << ". flType = " << flType
229 // << ". airline = " << airline
230 // << " Radius = " <<rad
232 *lat = _ap->getLatitude();
233 *lon = _ap->getLongitude();
241 void FGAirportDynamics::getParking(int id, double *lat, double *lon,
245 *lat = _ap->getLatitude();
246 *lon = _ap->getLongitude();
249 FGParkingVecIterator i = parkings.begin();
250 for (i = parkings.begin(); i != parkings.end(); i++) {
251 if (id == i->getIndex()) {
252 *lat = i->getLatitude();
253 *lon = i->getLongitude();
254 *heading = i->getHeading();
260 FGParking *FGAirportDynamics::getParking(int id)
262 FGParkingVecIterator i = parkings.begin();
263 for (i = parkings.begin(); i != parkings.end(); i++) {
264 if (id == i->getIndex()) {
271 string FGAirportDynamics::getParkingName(int id)
273 FGParkingVecIterator i = parkings.begin();
274 for (i = parkings.begin(); i != parkings.end(); i++) {
275 if (id == i->getIndex()) {
280 return string("overflow");
283 void FGAirportDynamics::releaseParking(int id)
287 FGParkingVecIterator i = parkings.begin();
288 for (i = parkings.begin(); i != parkings.end(); i++) {
289 if (id == i->getIndex()) {
290 i->setAvailable(true);
296 void FGAirportDynamics::setRwyUse(const FGRunwayPreference & ref)
299 //cerr << "Exiting due to not implemented yet" << endl;
303 bool FGAirportDynamics::innerGetActiveRunway(const string & trafficType,
304 int action, string & runway,
314 if (!rwyPrefs.available()) {
318 RunwayGroup *currRunwayGroup = 0;
319 int nrActiveRunways = 0;
320 time_t dayStart = fgGetLong("/sim/time/utc/day-seconds");
321 if ((abs((long) (dayStart - lastUpdate)) > 600)
322 || trafficType != prevTrafficType) {
325 lastUpdate = dayStart;
326 prevTrafficType = trafficType;
330 ((FGEnvironmentMgr *) globals->get_subsystem("environment"))
331 ->getEnvironment(getLatitude(), getLongitude(),
334 windSpeed = fgGetInt("/environment/metar/base-wind-speed-kt"); //stationweather.get_wind_speed_kt();
335 windHeading = fgGetInt("/environment/metar/base-wind-dir-deg");
336 //stationweather.get_wind_from_heading_deg();
338 //cerr << "finding active Runway for : " << _ap->getId() << endl;
339 //cerr << "Wind Heading : " << windHeading << endl;
340 //cerr << "Wind Speed : " << windSpeed << endl;
342 //cerr << "Nr of seconds since day start << " << dayStart << endl;
344 ScheduleTime *currSched;
345 //cerr << "A"<< endl;
346 currSched = rwyPrefs.getSchedule(trafficType.c_str());
349 //cerr << "B"<< endl;
350 scheduleName = currSched->getName(dayStart);
351 maxTail = currSched->getTailWind();
352 maxCross = currSched->getCrossWind();
353 //cerr << "Current Schedule = : " << scheduleName << endl;
354 if (scheduleName.empty())
356 //cerr << "C"<< endl;
357 currRunwayGroup = rwyPrefs.getGroup(scheduleName);
358 //cerr << "D"<< endl;
359 if (!(currRunwayGroup))
361 nrActiveRunways = currRunwayGroup->getNrActiveRunways();
363 // Keep a history of the currently active runways, to ensure
364 // that an already established selection of runways will not
365 // be overridden once a more preferred selection becomes
366 // available as that can lead to random runway swapping.
367 if (trafficType == "com") {
368 currentlyActive = &comActive;
369 } else if (trafficType == "gen") {
370 currentlyActive = &genActive;
371 } else if (trafficType == "mil") {
372 currentlyActive = &milActive;
373 } else if (trafficType == "ul") {
374 currentlyActive = &ulActive;
377 //cerr << "Durrently active selection for " << trafficType << ": ";
378 for (stringVecIterator it = currentlyActive->begin();
379 it != currentlyActive->end(); it++) {
380 //cerr << (*it) << " ";
384 currRunwayGroup->setActive(_ap,
387 maxTail, maxCross, currentlyActive);
389 // Note that I SHOULD keep multiple lists in memory, one for
390 // general aviation, one for commercial and one for military
392 currentlyActive->clear();
393 nrActiveRunways = currRunwayGroup->getNrActiveRunways();
394 //cerr << "Choosing runway for " << trafficType << endl;
395 for (int i = 0; i < nrActiveRunways; i++) {
396 type = "unknown"; // initialize to something other than landing or takeoff
397 currRunwayGroup->getActive(i, name, type);
398 if (type == "landing") {
399 landing.push_back(name);
400 currentlyActive->push_back(name);
401 //cerr << "Landing " << name << endl;
403 if (type == "takeoff") {
404 takeoff.push_back(name);
405 currentlyActive->push_back(name);
406 //cerr << "takeoff " << name << endl;
412 if (action == 1) // takeoff
414 int nr = takeoff.size();
416 // Note that the randomization below, is just a placeholder to choose between
417 // multiple active runways for this action. This should be
418 // under ATC control.
419 runway = chooseRwyByHeading(takeoff, heading);
421 runway = chooseRunwayFallback();
425 if (action == 2) // landing
427 int nr = landing.size();
429 runway = chooseRwyByHeading(landing, heading);
431 runway = chooseRunwayFallback();
438 string FGAirportDynamics::chooseRwyByHeading(stringVec rwys,
441 double bestError = 360.0;
442 double rwyHeading, headingError;
444 for (stringVecIterator i = rwys.begin(); i != rwys.end(); i++) {
445 if (!_ap->hasRunwayWithIdent(*i)) {
446 SG_LOG(SG_ATC, SG_WARN, "chooseRwyByHeading: runway " << *i <<
447 " not found at " << _ap->ident());
451 FGRunway *rwy = _ap->getRunwayByIdent((*i));
452 rwyHeading = rwy->headingDeg();
453 headingError = fabs(heading - rwyHeading);
454 if (headingError > 180)
455 headingError = fabs(headingError - 360);
456 if (headingError < bestError) {
458 bestError = headingError;
461 //cerr << "Using active runway " << runway << " for heading " << heading << endl;
465 void FGAirportDynamics::getActiveRunway(const string & trafficType,
466 int action, string & runway,
469 bool ok = innerGetActiveRunway(trafficType, action, runway, heading);
471 runway = chooseRunwayFallback();
475 string FGAirportDynamics::chooseRunwayFallback()
477 FGRunway *rwy = _ap->getActiveRunwayForUsage();
481 void FGAirportDynamics::addParking(FGParking & park)
483 parkings.push_back(park);
486 double FGAirportDynamics::getLatitude() const
488 return _ap->getLatitude();
491 double FGAirportDynamics::getLongitude() const
493 return _ap->getLongitude();
496 double FGAirportDynamics::getElevation() const
498 return _ap->getElevation();
501 const string & FGAirportDynamics::getId() const
506 // Experimental: Return a different ground frequency depending on the leg of the
507 // Flight. Leg should always have a minimum value of two when this function is called.
508 // Note that in this scheme, the assignment of various frequencies to various ground
509 // operations is completely arbitrary. As such, is a short cut I need to take now,
510 // so that at least I can start working on assigning different frequencies to different
513 int FGAirportDynamics::getGroundFrequency(unsigned leg)
515 //return freqGround.size() ? freqGround[0] : 0; };
518 SG_LOG(SG_ATC, SG_ALERT,
519 "Leg value is smaller than two at " << SG_ORIGIN);
521 if (freqGround.size() == 0) {
524 if ((freqGround.size() > leg - 1) && (leg > 1)) {
525 groundFreq = freqGround[leg - 1];
527 if ((freqGround.size() < leg - 1) && (leg > 1)) {
530 (leg - 1)) ? freqGround[freqGround.size() -
531 1] : freqGround[leg - 2];
533 if ((freqGround.size() >= leg - 1) && (leg > 1)) {
534 groundFreq = freqGround[leg - 2];
539 int FGAirportDynamics::getTowerFrequency(unsigned nr)
543 SG_LOG(SG_ATC, SG_ALERT,
544 "Leg value is smaller than two at " << SG_ORIGIN);
546 if (freqTower.size() == 0) {
549 if ((freqTower.size() > nr - 1) && (nr > 1)) {
550 towerFreq = freqTower[nr - 1];
552 if ((freqTower.size() < nr - 1) && (nr > 1)) {
555 (nr - 1)) ? freqTower[freqTower.size() -
556 1] : freqTower[nr - 2];
558 if ((freqTower.size() >= nr - 1) && (nr > 1)) {
559 towerFreq = freqTower[nr - 2];
565 FGAIFlightPlan *FGAirportDynamics::getSID(string activeRunway,
568 return SIDs.getBest(activeRunway, heading);