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>
32 #include <Environment/environment_mgr.hxx>
33 #include <Environment/environment.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/props/props.hxx>
36 #include <simgear/structure/subsystem_mgr.hxx>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/route/waypoint.hxx>
39 #include <Main/globals.hxx>
40 #include <Main/fg_props.hxx>
41 #include <Airports/runways.hxx>
49 SG_USING_STD(random_shuffle);
52 #include "dynamics.hxx"
54 FGAirportDynamics::FGAirportDynamics(FGAirport* ap) :
55 _ap(ap), rwyPrefs(ap) {
57 for (int i = 0; i < 10; i++)
59 avWindHeading [i] = 0;
64 // Note that the ground network should also be copied
65 FGAirportDynamics::FGAirportDynamics(const FGAirportDynamics& other) :
66 rwyPrefs(other.rwyPrefs)
68 for (FGParkingVecConstIterator ip= other.parkings.begin(); ip != other.parkings.end(); ip++)
69 parkings.push_back(*(ip));
70 // rwyPrefs = other.rwyPrefs;
71 lastUpdate = other.lastUpdate;
73 stringVecConstIterator il;
74 for (il = other.landing.begin(); il != other.landing.end(); il++)
75 landing.push_back(*il);
76 for (il = other.takeoff.begin(); il != other.takeoff.end(); il++)
77 takeoff.push_back(*il);
78 lastUpdate = other.lastUpdate;
79 for (int i = 0; i < 10; i++)
81 avWindHeading [i] = other.avWindHeading[i];
82 avWindSpeed [i] = other.avWindSpeed [i];
87 FGAirportDynamics::~FGAirportDynamics()
92 // Initialization required after XMLRead
93 void FGAirportDynamics::init()
95 // This may seem a bit weird to first randomly shuffle the parkings
96 // and then sort them again. However, parkings are sorted here by ascending
97 // radius. Since many parkings have similar radii, with each radius class they will
98 // still be allocated relatively systematically. Randomizing prior to sorting will
99 // prevent any initial orderings to be destroyed, leading (hopefully) to a more
100 // naturalistic gate assignment.
101 random_shuffle(parkings.begin(), parkings.end());
102 sort(parkings.begin(), parkings.end());
103 // add the gate positions to the ground network.
104 groundNetwork.addNodes(&parkings);
105 groundNetwork.init();
106 groundNetwork.setTowerController(&towerController);
107 groundNetwork.setParent(_ap);
110 bool FGAirportDynamics::getAvailableParking(double *lat, double *lon, double *heading, int *gateId, double rad, const string &flType, const string &acType, const string &airline)
113 bool available = false;
116 FGParkingVecIterator i;
117 // if (flType == "cargo")
119 // gateType = "RAMP_CARGO";
121 // else if (flType == "ga")
123 // gateType = "RAMP_GA";
125 // else gateType = "GATE";
127 if (parkings.begin() == parkings.end())
129 //cerr << "Could not find parking spot at " << _ap->getId() << endl;
130 *lat = _ap->getLatitude();
131 *lon = _ap->getLongitude();
137 // First try finding a parking with a designated airline code
138 for (i = parkings.begin(); !(i == parkings.end() || found); i++)
140 //cerr << "Gate Id: " << i->getIndex()
141 // << " Type : " << i->getType()
142 // << " Codes : " << i->getCodes()
143 // << " Radius: " << i->getRadius()
144 // << " Name : " << i->getName()
145 // << " Available: " << i->isAvailable() << endl;
147 // Taken by another aircraft
148 if (!(i->isAvailable()))
153 // No airline codes, so skip
154 if (i->getCodes().empty())
159 else // Airline code doesn't match
161 //cerr << "Code = " << airline << ": Codes " << i->getCodes();
162 if (i->getCodes().find(airline, 0) == string::npos)
165 //cerr << "Unavailable" << endl;
170 //cerr << "Available" << endl;
173 // Type doesn't match
174 if (i->getType() != flType)
180 if (i->getRadius() < rad)
188 *lat = i->getLatitude ();
189 *lon = i->getLongitude();
190 *heading = i->getHeading ();
191 *gateId = i->getIndex ();
192 i->setAvailable(false);
196 // then try again for those without codes.
197 for (i = parkings.begin(); !(i == parkings.end() || found); i++)
200 if (!(i->isAvailable()))
205 if (!(i->getCodes().empty()))
207 if ((i->getCodes().find(airline,0) == string::npos))
213 if (i->getType() != flType)
219 if (i->getRadius() < rad)
227 *lat = i->getLatitude ();
228 *lon = i->getLongitude();
229 *heading = i->getHeading ();
230 *gateId = i->getIndex ();
231 i->setAvailable(false);
235 // And finally once more if that didn't work. Now ignore the airline codes, as a last resort
236 for (i = parkings.begin(); !(i == parkings.end() || found); i++)
239 if (!(i->isAvailable()))
244 if (i->getType() != flType)
250 if (i->getRadius() < rad)
258 *lat = i->getLatitude ();
259 *lon = i->getLongitude();
260 *heading = i->getHeading ();
261 *gateId = i->getIndex ();
262 i->setAvailable(false);
269 //cerr << "Traffic overflow at" << _ap->getId()
270 // << ". flType = " << flType
271 // << ". airline = " << airline
272 // << " Radius = " <<rad
274 *lat = _ap->getLatitude();
275 *lon = _ap->getLongitude();
283 void FGAirportDynamics::getParking (int id, double *lat, double* lon, double *heading)
287 *lat = _ap->getLatitude();
288 *lon = _ap->getLongitude();
293 FGParkingVecIterator i = parkings.begin();
294 for (i = parkings.begin(); i != parkings.end(); i++)
296 if (id == i->getIndex())
298 *lat = i->getLatitude();
299 *lon = i->getLongitude();
300 *heading = i->getHeading();
306 FGParking *FGAirportDynamics::getParking(int i)
308 if (i < (int)parkings.size())
309 return &(parkings[i]);
313 string FGAirportDynamics::getParkingName(int i)
315 if (i < (int)parkings.size() && i >= 0)
316 return (parkings[i].getName());
318 return string("overflow");
320 void FGAirportDynamics::releaseParking(int id)
325 FGParkingVecIterator i = parkings.begin();
326 for (i = parkings.begin(); i != parkings.end(); i++)
328 if (id == i->getIndex())
330 i -> setAvailable(true);
336 void FGAirportDynamics::setRwyUse(const FGRunwayPreference& ref)
339 //cerr << "Exiting due to not implemented yet" << endl;
342 void FGAirportDynamics::getActiveRunway(const string &trafficType, int action, string &runway)
351 if (!(rwyPrefs.available()))
353 runway = chooseRunwayFallback();
354 return; // generic fall back goes here
358 RunwayGroup *currRunwayGroup = 0;
359 int nrActiveRunways = 0;
360 time_t dayStart = fgGetLong("/sim/time/utc/day-seconds");
361 if ((abs((long)(dayStart - lastUpdate)) > 600) || trafficType != prevTrafficType)
365 lastUpdate = dayStart;
366 prevTrafficType = trafficType;
369 stationweather = ((FGEnvironmentMgr *) globals->get_subsystem("environment"))
370 ->getEnvironment(getLatitude(),
374 windSpeed = stationweather.get_wind_speed_kt();
375 windHeading = stationweather.get_wind_from_heading_deg();
376 // double averageWindSpeed = 0;
377 // double averageWindHeading = 0;
378 // double cosHeading = 0;
379 // double sinHeading = 0;
380 // // Initialize at the beginning of the next day or startup
381 // if ((lastUpdate == 0) || (dayStart < lastUpdate))
383 // for (int i = 0; i < 10; i++)
385 // avWindHeading [i] = windHeading;
386 // avWindSpeed [i] = windSpeed;
391 // if (windSpeed != avWindSpeed[9]) // update if new metar data
393 // // shift the running average
394 // for (int i = 0; i < 9 ; i++)
396 // avWindHeading[i] = avWindHeading[i+1];
397 // avWindSpeed [i] = avWindSpeed [i+1];
400 // avWindHeading[9] = windHeading;
401 // avWindSpeed [9] = windSpeed;
404 // for (int i = 0; i < 10; i++)
406 // averageWindSpeed += avWindSpeed [i];
407 // //averageWindHeading += avWindHeading [i];
408 // cosHeading += cos(avWindHeading[i] * SG_DEGREES_TO_RADIANS);
409 // sinHeading += sin(avWindHeading[i] * SG_DEGREES_TO_RADIANS);
411 // averageWindSpeed /= 10;
412 // //averageWindHeading /= 10;
415 // averageWindHeading = atan2(sinHeading, cosHeading) *SG_RADIANS_TO_DEGREES;
416 // if (averageWindHeading < 0)
417 // averageWindHeading += 360.0;
418 // //cerr << "Wind Heading " << windHeading << " average " << averageWindHeading << endl;
419 // //cerr << "Wind Speed " << windSpeed << " average " << averageWindSpeed << endl;
420 // lastUpdate = dayStart;
421 // //if (wind_speed == 0) {
422 // // wind_heading = 270; This forces West-facing rwys to be used in no-wind situations
423 // // which is consistent with Flightgear's initial setup.
426 //string rwy_no = globals->get_runways()->search(apt->getId(), int(wind_heading));
428 //cerr << "finding active Runway for" << _ap->getId() << endl;
429 //cerr << "Nr of seconds since day start << " << dayStart << endl;
431 ScheduleTime *currSched;
432 //cerr << "A"<< endl;
433 currSched = rwyPrefs.getSchedule(trafficType.c_str());
436 //cerr << "B"<< endl;
437 scheduleName = currSched->getName(dayStart);
438 maxTail = currSched->getTailWind ();
439 maxCross = currSched->getCrossWind ();
440 //cerr << "SChedule anme = " << scheduleName << endl;
441 if (scheduleName.empty())
443 //cerr << "C"<< endl;
444 currRunwayGroup = rwyPrefs.getGroup(scheduleName);
445 //cerr << "D"<< endl;
446 if (!(currRunwayGroup))
448 nrActiveRunways = currRunwayGroup->getNrActiveRunways();
449 //cerr << "Nr of Active Runways = " << nrActiveRunways << endl;
452 currRunwayGroup->setActive(_ap->getId(),
459 // Note that I SHOULD keep three lists in memory, one for
460 // general aviation, one for commercial and one for military
462 currentlyActive.clear();
463 nrActiveRunways = currRunwayGroup->getNrActiveRunways();
464 for (int i = 0; i < nrActiveRunways; i++)
466 type = "unknown"; // initialize to something other than landing or takeoff
467 currRunwayGroup->getActive(i, name, type);
468 if (type == "landing")
470 landing.push_back(name);
471 currentlyActive.push_back(name);
472 //cerr << "Landing " << name << endl;
474 if (type == "takeoff")
476 takeoff.push_back(name);
477 currentlyActive.push_back(name);
478 //cerr << "takeoff " << name << endl;
482 if (action == 1) // takeoff
484 int nr = takeoff.size();
487 // Note that the randomization below, is just a placeholder to choose between
488 // multiple active runways for this action. This should be
489 // under ATC control.
490 runway = takeoff[(rand() % nr)];
494 runway = chooseRunwayFallback();
497 if (action == 2) // landing
499 int nr = landing.size();
502 runway = landing[(rand() % nr)];
506 runway = chooseRunwayFallback();
510 //runway = globals->get_runways()->search(_ap->getId(), int(windHeading));
511 //cerr << "Seleceted runway: " << runway << endl;
515 string FGAirportDynamics::chooseRunwayFallback()
518 stationweather = ((FGEnvironmentMgr *) globals->get_subsystem("environment"))
519 ->getEnvironment(getLatitude(),
523 double windSpeed = stationweather.get_wind_speed_kt();
524 double windHeading = stationweather.get_wind_from_heading_deg();
525 if (windSpeed == 0) {
526 windHeading = 270; // This forces West-facing rwys to be used in no-wind situations
527 //which is consistent with Flightgear's initial setup.
530 return globals->get_runways()->search(_ap->getId(), int(windHeading));
533 void FGAirportDynamics::addParking(FGParking& park) {
534 parkings.push_back(park);
537 double FGAirportDynamics::getLatitude() const {
538 return _ap->getLatitude();
541 double FGAirportDynamics::getLongitude() const {
542 return _ap->getLongitude();
545 double FGAirportDynamics::getElevation() const {
546 return _ap->getElevation();
549 const string& FGAirportDynamics::getId() const {