1 // runwayprefs.cxx - class implementations corresponding to runwayprefs.hxx
2 // assignments by the AI code
4 // Written by Durk Talsma, started January 2005.
6 // Copyright (C) 2004 Durk Talsma.
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 # define _USE_MATH_DEFINES
32 //#include <algorithm>
34 #include <simgear/compiler.h>
36 //#include <plib/sg.h>
37 //#include <plib/ul.h>
39 //#include <Environment/environment_mgr.hxx>
40 //#include <Environment/environment.hxx>
41 //#include <simgear/misc/sg_path.hxx>
42 //#include <simgear/props/props.hxx>
43 //#include <simgear/structure/subsystem_mgr.hxx>
44 #include <simgear/debug/logstream.hxx>
45 #include <Main/globals.hxx>
46 //#include <Main/fg_props.hxx>
47 #include <Airports/runways.hxx>
49 #include "runwayprefs.hxx"
51 /******************************************************************************
53 ***************e*************************************************************/
54 void ScheduleTime::clear()
58 scheduleNames.clear();
62 ScheduleTime::ScheduleTime(const ScheduleTime &other)
65 timeVecConstIterator i;
66 for (i = other.start.begin(); i != other.start.end(); i++)
68 for (i = other.end.begin(); i != other.end.end(); i++)
70 stringVecConstIterator k;
71 for (k = other.scheduleNames.begin(); k != other.scheduleNames.end(); k++)
72 scheduleNames.push_back(*k);
75 //stringVec scheduleNames;
76 tailWind = other.tailWind;
77 crssWind = other.tailWind;
81 ScheduleTime & ScheduleTime::operator= (const ScheduleTime &other)
85 timeVecConstIterator i;
86 for (i = other.start.begin(); i != other.start.end(); i++)
88 for (i = other.end.begin(); i != other.end.end(); i++)
90 stringVecConstIterator k;
91 for (k = other.scheduleNames.begin(); k != other.scheduleNames.end(); k++)
92 scheduleNames.push_back(*k);
95 //stringVec scheduleNames;
96 tailWind = other.tailWind;
97 crssWind = other.tailWind;
100 string ScheduleTime::getName(time_t dayStart)
102 if ((start.size() != end.size()) || (start.size() != scheduleNames.size()))
104 SG_LOG( SG_GENERAL, SG_INFO, "Unable to parse schedule times" );
109 int nrItems = start.size();
110 //cerr << "Nr of items to process: " << nrItems << endl;
113 for (unsigned int i = 0; i < start.size(); i++)
116 if ((dayStart >= start[i]) && (dayStart <= end[i]))
117 return scheduleNames[i];
120 //couldn't find one so return 0;
121 //cerr << "Returning 0 " << endl;
125 /******************************************************************************
127 *****************************************************************************/
129 RunwayList::RunwayList(const RunwayList &other)
132 stringVecConstIterator i;
133 for (i = other.preferredRunways.begin(); i != other.preferredRunways.end(); i++)
134 preferredRunways.push_back(*i);
136 RunwayList& RunwayList::operator= (const RunwayList &other)
139 preferredRunways.clear();
140 stringVecConstIterator i;
141 for (i = other.preferredRunways.begin(); i != other.preferredRunways.end(); i++)
142 preferredRunways.push_back(*i);
145 void RunwayList::set(const string &tp, const string &lst)
147 //weekday = atoi(timeCopy.substr(0,1).c_str());
148 // timeOffsetInDays = weekday - currTimeDate->getGmt()->tm_wday;
149 // timeCopy = timeCopy.substr(2,timeCopy.length());
153 while (rwys.find(",") != string::npos)
155 rwy = rwys.substr(0, rwys.find(",",0));
156 //cerr << "adding runway [" << rwy << "] to the list " << endl;
157 preferredRunways.push_back(rwy);
158 rwys.erase(0, rwys.find(",",0)+1); // erase until after the first whitspace
159 while (rwys[0] == ' ')
160 rwys.erase(0, 1); // Erase any leading whitespaces.
161 //cerr << "Remaining runway list " << rwys;
163 preferredRunways.push_back(rwys);
167 void RunwayList::clear()
170 preferredRunways.clear();
172 /****************************************************************************
174 ***************************************************************************/
176 RunwayGroup::RunwayGroup(const RunwayGroup &other)
179 RunwayListVecConstIterator i;
180 for (i = other.rwyList.begin(); i != other.rwyList.end(); i++)
181 rwyList.push_back(*i);
182 choice[0] = other.choice[0];
183 choice[1] = other.choice[1];
184 nrActive = other.nrActive;
186 RunwayGroup& RunwayGroup:: operator= (const RunwayGroup &other)
190 RunwayListVecConstIterator i;
191 for (i = other.rwyList.begin(); i != other.rwyList.end(); i++)
192 rwyList.push_back(*i);
193 choice[0] = other.choice[0];
194 choice[1] = other.choice[1];
195 nrActive = other.nrActive;
199 void RunwayGroup::setActive(const string &aptId,
207 int activeRwys = rwyList.size(); // get the number of runways active
209 // bool found = true;
218 nrOfPreferences = rwyList[0].getRwyList()->size();
219 for (int i = 0; i < nrOfPreferences; i++)
221 bool validSelection = true;
222 for (int j = 0; j < activeRwys; j++)
224 //cerr << "I J " << i << " " << j << endl;
225 name = rwyList[j].getRwyList(i);
226 //cerr << "Name of Runway: " << name << endl;
227 if (globals->get_runways()->search( aptId,
231 //cerr << "Succes" << endl;
232 hdgDiff = fabs(windHeading - rwy._heading);
233 //cerr << "Wind Heading: " << windHeading << "Runway Heading: " <<rwy._heading << endl;
234 //cerr << "Wind Speed : " << windSpeed << endl;
236 hdgDiff = 360 - hdgDiff;
237 //cerr << "Heading diff: " << hdgDiff << endl;
238 hdgDiff *= ((2*M_PI)/360.0); // convert to radians
239 crossWind = windSpeed * sin(hdgDiff);
240 tailWind = -windSpeed * cos(hdgDiff);
241 //cerr << "Tailwind : " << tailWind << endl;
242 //cerr << "Crosswnd : " << crossWind << endl;
243 if ((tailWind > maxTail) || (crossWind > maxCross))
244 validSelection = false;
246 SG_LOG( SG_GENERAL, SG_INFO, "Failed to find runway " << name << " at " << aptId );
253 //cerr << "Valid runay selection : " << i << endl;
254 nrActive = activeRwys;
259 // If this didn't work, due to heavy winds, try again
260 // but select only one landing and one takeoff runway.
263 for (int i = activeRwys-1; i; i--)
265 if (rwyList[i].getType() == string("landing"))
267 if (rwyList[i].getType() == string("takeoff"))
270 //cerr << "Choosing " << choice[0] << " for landing and " << choice[1] << "for takeoff" << endl;
271 nrOfPreferences = rwyList[0].getRwyList()->size();
272 for (int i = 0; i < nrOfPreferences; i++)
274 bool validSelection = true;
275 for (int j = 0; j < 2; j++)
277 //cerr << "I J " << i << " " << j << endl;
278 name = rwyList[choice[j]].getRwyList(i);
279 //cerr << "Name of Runway: " << name << endl;
280 if (globals->get_runways()->search( aptId,
284 //cerr << "Succes" << endl;
285 hdgDiff = fabs(windHeading - rwy._heading);
286 //cerr << "Wind Heading: " << windHeading << "Runway Heading: " <<rwy._heading << endl;
287 //cerr << "Wind Speed : " << windSpeed << endl;
289 hdgDiff = 360 - hdgDiff;
290 //cerr << "Heading diff: " << hdgDiff << endl;
291 hdgDiff *= ((2*M_PI)/360.0); // convert to radians
292 crossWind = windSpeed * sin(hdgDiff);
293 tailWind = -windSpeed * cos(hdgDiff);
294 //cerr << "Tailwind : " << tailWind << endl;
295 //cerr << "Crosswnd : " << crossWind << endl;
296 if ((tailWind > maxTail) || (crossWind > maxCross))
297 validSelection = false;
299 SG_LOG( SG_GENERAL, SG_INFO, "Failed to find runway " << name << " at " << aptId );
306 //cerr << "Valid runay selection : " << i << endl;
314 //RunwayListVectorIterator i; // = rwlist.begin();
315 //stringVecIterator j;
316 //for (i = rwyList.begin(); i != rwyList.end(); i++)
318 // cerr << i->getType();
319 // for (j = i->getRwyList()->begin(); j != i->getRwyList()->end(); j++)
329 void RunwayGroup::getActive(int i, string &name, string &type)
335 if (nrActive == (int)rwyList.size())
337 name = rwyList[i].getRwyList(active);
338 type = rwyList[i].getType();
342 name = rwyList[choice[i]].getRwyList(active);
343 type = rwyList[choice[i]].getType();
346 /*****************************************************************************
347 * FGRunway preference
348 ****************************************************************************/
349 FGRunwayPreference::FGRunwayPreference()
351 //cerr << "Running default Constructor" << endl;
355 FGRunwayPreference::FGRunwayPreference(const FGRunwayPreference &other)
357 initialized = other.initialized;
359 scheduleName = other.scheduleName;
361 comTimes = other.comTimes; // Commercial Traffic;
362 genTimes = other.genTimes; // General Aviation;
363 milTimes = other.milTimes; // Military Traffic;
364 currTimes= other.currTimes; // Needed for parsing;
366 rwyList = other.rwyList;
367 rwyGroup = other.rwyGroup;
368 PreferenceListConstIterator i;
369 for (i = other.preferences.begin(); i != other.preferences.end(); i++)
370 preferences.push_back(*i);
373 FGRunwayPreference & FGRunwayPreference::operator= (const FGRunwayPreference &other)
375 initialized = other.initialized;
377 scheduleName = other.scheduleName;
379 comTimes = other.comTimes; // Commercial Traffic;
380 genTimes = other.genTimes; // General Aviation;
381 milTimes = other.milTimes; // Military Traffic;
382 currTimes= other.currTimes; // Needed for parsing;
384 rwyList = other.rwyList;
385 rwyGroup = other.rwyGroup;
386 PreferenceListConstIterator i;
388 for (i = other.preferences.begin(); i != other.preferences.end(); i++)
389 preferences.push_back(*i);
393 ScheduleTime *FGRunwayPreference::getSchedule(const char *trafficType)
395 if (!(strcmp(trafficType, "com"))) {
398 if (!(strcmp(trafficType, "gen"))) {
401 if (!(strcmp(trafficType, "mil"))) {
407 RunwayGroup *FGRunwayPreference::getGroup(const string &groupName)
409 PreferenceListIterator i = preferences.begin();
410 if (preferences.begin() == preferences.end())
412 while (!(i == preferences.end() || i->getName() == groupName))
414 if (i != preferences.end())
420 void FGRunwayPreference::startXML () {
421 // cout << "Start XML" << endl;
424 void FGRunwayPreference::endXML () {
425 cout << "End XML" << endl;
428 void FGRunwayPreference::startElement (const char * name, const XMLAttributes &atts) {
429 //cout << "StartElement " << name << endl;
431 if (!(strcmp(name, "wind"))) {
432 //cerr << "Will be processing Wind" << endl;
433 for (int i = 0; i < atts.size(); i++)
435 //cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl;
436 //attname = atts.getName(i);
437 if (atts.getName(i) == string("tail")) {
438 //cerr << "Tail Wind = " << atts.getValue(i) << endl;
439 currTimes.setTailWind(atof(atts.getValue(i)));
441 if (atts.getName(i) == string("cross")) {
442 //cerr << "Cross Wind = " << atts.getValue(i) << endl;
443 currTimes.setCrossWind(atof(atts.getValue(i)));
447 if (!(strcmp(name, "time"))) {
448 //cerr << "Will be processing time" << endl;
449 for (int i = 0; i < atts.size(); i++)
451 if (atts.getName(i) == string("start")) {
452 //cerr << "Start Time = " << atts.getValue(i) << endl;
453 currTimes.addStartTime(processTime(atts.getValue(i)));
455 if (atts.getName(i) == string("end")) {
456 //cerr << "End time = " << atts.getValue(i) << endl;
457 currTimes.addEndTime(processTime(atts.getValue(i)));
459 if (atts.getName(i) == string("schedule")) {
460 //cerr << "Schedule Name = " << atts.getValue(i) << endl;
461 currTimes.addScheduleName(atts.getValue(i));
465 if (!(strcmp(name, "takeoff"))) {
468 if (!(strcmp(name, "landing")))
472 if (!(strcmp(name, "schedule"))) {
473 for (int i = 0; i < atts.size(); i++)
475 //cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl;
476 //attname = atts.getName(i);
477 if (atts.getName(i) == string("name")) {
478 //cerr << "Schedule name = " << atts.getValue(i) << endl;
479 scheduleName = atts.getValue(i);
485 //based on a string containing hour and minute, return nr seconds since day start.
486 time_t FGRunwayPreference::processTime(const string &tme)
488 string hour = tme.substr(0, tme.find(":",0));
489 string minute = tme.substr(tme.find(":",0)+1, tme.length());
491 //cerr << "hour = " << hour << " Minute = " << minute << endl;
492 return (atoi(hour.c_str()) * 3600 + atoi(minute.c_str()) * 60);
495 void FGRunwayPreference::endElement (const char * name) {
496 //cout << "End element " << name << endl;
497 if (!(strcmp(name, "rwyuse"))) {
500 if (!(strcmp(name, "com"))) { // Commercial Traffic
501 //cerr << "Setting time table for commerical traffic" << endl;
502 comTimes = currTimes;
505 if (!(strcmp(name, "gen"))) { // General Aviation
506 //cerr << "Setting time table for general aviation" << endl;
507 genTimes = currTimes;
510 if (!(strcmp(name, "mil"))) { // Military Traffic
511 //cerr << "Setting time table for military traffic" << endl;
512 genTimes = currTimes;
516 if (!(strcmp(name, "takeoff"))) {
517 //cerr << "Adding takeoff: " << value << endl;
518 rwyList.set(name, value);
519 rwyGroup.add(rwyList);
521 if (!(strcmp(name, "landing"))) {
522 //cerr << "Adding landing: " << value << endl;
523 rwyList.set(name, value);
524 rwyGroup.add(rwyList);
526 if (!(strcmp(name, "schedule"))) {
527 //cerr << "Adding schedule" << scheduleName << endl;
528 rwyGroup.setName(scheduleName);
529 //rwyGroup.addRunways(rwyList);
530 preferences.push_back(rwyGroup);
536 void FGRunwayPreference::data (const char * s, int len) {
537 string token = string(s,len);
538 //cout << "Character data " << string(s,len) << endl;
539 //if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
542 // value = string("");
546 void FGRunwayPreference::pi (const char * target, const char * data) {
547 //cout << "Processing instruction " << target << ' ' << data << endl;
550 void FGRunwayPreference::warning (const char * message, int line, int column) {
551 cout << "Warning: " << message << " (" << line << ',' << column << ')'
555 void FGRunwayPreference::error (const char * message, int line, int column) {
556 cout << "Error: " << message << " (" << line << ',' << column << ')'