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.addNodes(&parkings);
100 groundNetwork.init();
101 groundNetwork.setTowerController(&towerController);
102 groundNetwork.setParent(_ap);
105 bool FGAirportDynamics::getAvailableParking(double *lat, double *lon,
106 double *heading, int *gateId,
108 const string & flType,
109 const string & acType,
110 const string & airline)
113 bool available = false;
116 FGParkingVecIterator i;
117 if (parkings.begin() == parkings.end()) {
118 //cerr << "Could not find parking spot at " << _ap->getId() << endl;
119 *lat = _ap->getLatitude();
120 *lon = _ap->getLongitude();
124 // First try finding a parking with a designated airline code
125 for (i = parkings.begin(); !(i == parkings.end() || found); i++) {
127 // Taken by another aircraft
128 if (!(i->isAvailable())) {
132 // No airline codes, so skip
133 if (i->getCodes().empty()) {
136 } else { // Airline code doesn't match
137 //cerr << "Code = " << airline << ": Codes " << i->getCodes();
138 if (i->getCodes().find(airline, 0) == string::npos) {
140 //cerr << "Unavailable" << endl;
143 //cerr << "Available" << endl;
146 // Type doesn't match
147 if (i->getType() != flType) {
152 if (i->getRadius() < rad) {
158 *lat = i->getLatitude();
159 *lon = i->getLongitude();
160 *heading = i->getHeading();
161 *gateId = i->getIndex();
162 i->setAvailable(false);
166 // then try again for those without codes.
167 for (i = parkings.begin(); !(i == parkings.end() || found); i++) {
169 if (!(i->isAvailable())) {
173 if (!(i->getCodes().empty())) {
174 if ((i->getCodes().find(airline, 0) == string::npos)) {
179 if (i->getType() != flType) {
184 if (i->getRadius() < rad) {
190 *lat = i->getLatitude();
191 *lon = i->getLongitude();
192 *heading = i->getHeading();
193 *gateId = i->getIndex();
194 i->setAvailable(false);
198 // And finally once more if that didn't work. Now ignore the airline codes, as a last resort
199 for (i = parkings.begin(); !(i == parkings.end() || found); i++) {
201 if (!(i->isAvailable())) {
205 if (i->getType() != flType) {
210 if (i->getRadius() < rad) {
216 *lat = i->getLatitude();
217 *lon = i->getLongitude();
218 *heading = i->getHeading();
219 *gateId = i->getIndex();
220 i->setAvailable(false);
226 //cerr << "Traffic overflow at" << _ap->getId()
227 // << ". flType = " << flType
228 // << ". airline = " << airline
229 // << " Radius = " <<rad
231 *lat = _ap->getLatitude();
232 *lon = _ap->getLongitude();
240 void FGAirportDynamics::getParking(int id, double *lat, double *lon,
244 *lat = _ap->getLatitude();
245 *lon = _ap->getLongitude();
248 FGParkingVecIterator i = parkings.begin();
249 for (i = parkings.begin(); i != parkings.end(); i++) {
250 if (id == i->getIndex()) {
251 *lat = i->getLatitude();
252 *lon = i->getLongitude();
253 *heading = i->getHeading();
259 FGParking *FGAirportDynamics::getParking(int id)
261 FGParkingVecIterator i = parkings.begin();
262 for (i = parkings.begin(); i != parkings.end(); i++) {
263 if (id == i->getIndex()) {
270 string FGAirportDynamics::getParkingName(int id)
272 FGParkingVecIterator i = parkings.begin();
273 for (i = parkings.begin(); i != parkings.end(); i++) {
274 if (id == i->getIndex()) {
279 return string("overflow");
282 void FGAirportDynamics::releaseParking(int id)
286 FGParkingVecIterator i = parkings.begin();
287 for (i = parkings.begin(); i != parkings.end(); i++) {
288 if (id == i->getIndex()) {
289 i->setAvailable(true);
295 void FGAirportDynamics::setRwyUse(const FGRunwayPreference & ref)
298 //cerr << "Exiting due to not implemented yet" << endl;
302 bool FGAirportDynamics::innerGetActiveRunway(const string & trafficType,
303 int action, string & runway,
313 if (!rwyPrefs.available()) {
317 RunwayGroup *currRunwayGroup = 0;
318 int nrActiveRunways = 0;
319 time_t dayStart = fgGetLong("/sim/time/utc/day-seconds");
320 if ((abs((long) (dayStart - lastUpdate)) > 600)
321 || trafficType != prevTrafficType) {
324 lastUpdate = dayStart;
325 prevTrafficType = trafficType;
329 ((FGEnvironmentMgr *) globals->get_subsystem("environment"))
330 ->getEnvironment(getLatitude(), getLongitude(),
333 windSpeed = fgGetInt("/environment/metar/base-wind-speed-kt"); //stationweather.get_wind_speed_kt();
334 windHeading = fgGetInt("/environment/metar/base-wind-dir-deg");
335 //stationweather.get_wind_from_heading_deg();
337 //cerr << "finding active Runway for : " << _ap->getId() << endl;
338 //cerr << "Wind Heading : " << windHeading << endl;
339 //cerr << "Wind Speed : " << windSpeed << endl;
341 //cerr << "Nr of seconds since day start << " << dayStart << endl;
343 ScheduleTime *currSched;
344 //cerr << "A"<< endl;
345 currSched = rwyPrefs.getSchedule(trafficType.c_str());
348 //cerr << "B"<< endl;
349 scheduleName = currSched->getName(dayStart);
350 maxTail = currSched->getTailWind();
351 maxCross = currSched->getCrossWind();
352 //cerr << "Current Schedule = : " << scheduleName << endl;
353 if (scheduleName.empty())
355 //cerr << "C"<< endl;
356 currRunwayGroup = rwyPrefs.getGroup(scheduleName);
357 //cerr << "D"<< endl;
358 if (!(currRunwayGroup))
360 nrActiveRunways = currRunwayGroup->getNrActiveRunways();
362 // Keep a history of the currently active runways, to ensure
363 // that an already established selection of runways will not
364 // be overridden once a more preferred selection becomes
365 // available as that can lead to random runway swapping.
366 if (trafficType == "com") {
367 currentlyActive = &comActive;
368 } else if (trafficType == "gen") {
369 currentlyActive = &genActive;
370 } else if (trafficType == "mil") {
371 currentlyActive = &milActive;
372 } else if (trafficType == "ul") {
373 currentlyActive = &ulActive;
376 //cerr << "Durrently active selection for " << trafficType << ": ";
377 for (stringVecIterator it = currentlyActive->begin();
378 it != currentlyActive->end(); it++) {
379 //cerr << (*it) << " ";
383 currRunwayGroup->setActive(_ap,
386 maxTail, maxCross, currentlyActive);
388 // Note that I SHOULD keep multiple lists in memory, one for
389 // general aviation, one for commercial and one for military
391 currentlyActive->clear();
392 nrActiveRunways = currRunwayGroup->getNrActiveRunways();
393 //cerr << "Choosing runway for " << trafficType << endl;
394 for (int i = 0; i < nrActiveRunways; i++) {
395 type = "unknown"; // initialize to something other than landing or takeoff
396 currRunwayGroup->getActive(i, name, type);
397 if (type == "landing") {
398 landing.push_back(name);
399 currentlyActive->push_back(name);
400 //cerr << "Landing " << name << endl;
402 if (type == "takeoff") {
403 takeoff.push_back(name);
404 currentlyActive->push_back(name);
405 //cerr << "takeoff " << name << endl;
411 if (action == 1) // takeoff
413 int nr = takeoff.size();
415 // Note that the randomization below, is just a placeholder to choose between
416 // multiple active runways for this action. This should be
417 // under ATC control.
418 runway = chooseRwyByHeading(takeoff, heading);
420 runway = chooseRunwayFallback();
424 if (action == 2) // landing
426 int nr = landing.size();
428 runway = chooseRwyByHeading(landing, heading);
430 runway = chooseRunwayFallback();
437 string FGAirportDynamics::chooseRwyByHeading(stringVec rwys,
440 double bestError = 360.0;
441 double rwyHeading, headingError;
443 for (stringVecIterator i = rwys.begin(); i != rwys.end(); i++) {
444 if (!_ap->hasRunwayWithIdent(*i)) {
445 SG_LOG(SG_ATC, SG_WARN, "chooseRwyByHeading: runway " << *i <<
446 " not found at " << _ap->ident());
450 FGRunway *rwy = _ap->getRunwayByIdent((*i));
451 rwyHeading = rwy->headingDeg();
452 headingError = fabs(heading - rwyHeading);
453 if (headingError > 180)
454 headingError = fabs(headingError - 360);
455 if (headingError < bestError) {
457 bestError = headingError;
460 //cerr << "Using active runway " << runway << " for heading " << heading << endl;
464 void FGAirportDynamics::getActiveRunway(const string & trafficType,
465 int action, string & runway,
468 bool ok = innerGetActiveRunway(trafficType, action, runway, heading);
470 runway = chooseRunwayFallback();
474 string FGAirportDynamics::chooseRunwayFallback()
476 FGRunway *rwy = _ap->getActiveRunwayForUsage();
480 void FGAirportDynamics::addParking(FGParking & park)
482 parkings.push_back(park);
485 double FGAirportDynamics::getLatitude() const
487 return _ap->getLatitude();
490 double FGAirportDynamics::getLongitude() const
492 return _ap->getLongitude();
495 double FGAirportDynamics::getElevation() const
497 return _ap->getElevation();
500 const string & FGAirportDynamics::getId() const
505 // Experimental: Return a different ground frequency depending on the leg of the
506 // Flight. Leg should always have a minimum value of two when this function is called.
507 // Note that in this scheme, the assignment of various frequencies to various ground
508 // operations is completely arbitrary. As such, is a short cut I need to take now,
509 // so that at least I can start working on assigning different frequencies to different
512 int FGAirportDynamics::getGroundFrequency(unsigned leg)
514 //return freqGround.size() ? freqGround[0] : 0; };
517 SG_LOG(SG_ATC, SG_ALERT,
518 "Leg value is smaller than two at " << SG_ORIGIN);
520 if (freqGround.size() == 0) {
523 if ((freqGround.size() > leg - 1) && (leg > 1)) {
524 groundFreq = freqGround[leg - 1];
526 if ((freqGround.size() < leg - 1) && (leg > 1)) {
529 (leg - 1)) ? freqGround[freqGround.size() -
530 1] : freqGround[leg - 2];
532 if ((freqGround.size() >= leg - 1) && (leg > 1)) {
533 groundFreq = freqGround[leg - 2];
538 FGAIFlightPlan *FGAirportDynamics::getSID(string activeRunway,
541 return SIDs.getBest(activeRunway, heading);