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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <simgear/compiler.h>
32 #include <simgear/debug/logstream.hxx>
33 #include <Main/globals.hxx>
34 #include <Airports/runways.hxx>
36 #include "runwayprefs.hxx"
39 /******************************************************************************
41 ***************e*************************************************************/
42 void ScheduleTime::clear()
46 scheduleNames.clear();
50 ScheduleTime::ScheduleTime(const ScheduleTime &other)
53 timeVecConstIterator i;
54 for (i = other.start.begin(); i != other.start.end(); i++)
56 for (i = other.end.begin(); i != other.end.end(); i++)
58 stringVecConstIterator k;
59 for (k = other.scheduleNames.begin(); k != other.scheduleNames.end(); k++)
60 scheduleNames.push_back(*k);
63 //stringVec scheduleNames;
64 tailWind = other.tailWind;
65 crssWind = other.tailWind;
69 ScheduleTime & ScheduleTime::operator= (const ScheduleTime &other)
73 timeVecConstIterator i;
74 for (i = other.start.begin(); i != other.start.end(); i++)
76 for (i = other.end.begin(); i != other.end.end(); i++)
78 stringVecConstIterator k;
79 for (k = other.scheduleNames.begin(); k != other.scheduleNames.end(); k++)
80 scheduleNames.push_back(*k);
83 //stringVec scheduleNames;
84 tailWind = other.tailWind;
85 crssWind = other.tailWind;
88 string ScheduleTime::getName(time_t dayStart)
90 if ((start.size() != end.size()) || (start.size() != scheduleNames.size()))
92 SG_LOG( SG_GENERAL, SG_INFO, "Unable to parse schedule times" );
97 int nrItems = start.size();
98 //cerr << "Nr of items to process: " << nrItems << endl;
101 for (unsigned int i = 0; i < start.size(); i++)
104 if ((dayStart >= start[i]) && (dayStart <= end[i]))
105 return scheduleNames[i];
108 //couldn't find one so return 0;
109 //cerr << "Returning 0 " << endl;
113 /******************************************************************************
115 *****************************************************************************/
117 RunwayList::RunwayList(const RunwayList &other)
120 stringVecConstIterator i;
121 for (i = other.preferredRunways.begin(); i != other.preferredRunways.end(); i++)
122 preferredRunways.push_back(*i);
124 RunwayList& RunwayList::operator= (const RunwayList &other)
127 preferredRunways.clear();
128 stringVecConstIterator i;
129 for (i = other.preferredRunways.begin(); i != other.preferredRunways.end(); i++)
130 preferredRunways.push_back(*i);
133 void RunwayList::set(const string &tp, const string &lst)
135 //weekday = atoi(timeCopy.substr(0,1).c_str());
136 // timeOffsetInDays = weekday - currTimeDate->getGmt()->tm_wday;
137 // timeCopy = timeCopy.substr(2,timeCopy.length());
141 while (rwys.find(",") != string::npos)
143 rwy = rwys.substr(0, rwys.find(",",0));
144 //cerr << "adding runway [" << rwy << "] to the list " << endl;
145 preferredRunways.push_back(rwy);
146 rwys.erase(0, rwys.find(",",0)+1); // erase until after the first whitspace
147 while (rwys[0] == ' ')
148 rwys.erase(0, 1); // Erase any leading whitespaces.
149 //cerr << "Remaining runway list " << rwys;
151 preferredRunways.push_back(rwys);
155 void RunwayList::clear()
158 preferredRunways.clear();
160 /****************************************************************************
162 ***************************************************************************/
164 RunwayGroup::RunwayGroup(const RunwayGroup &other)
167 RunwayListVecConstIterator i;
168 for (i = other.rwyList.begin(); i != other.rwyList.end(); i++)
169 rwyList.push_back(*i);
170 choice[0] = other.choice[0];
171 choice[1] = other.choice[1];
172 nrActive = other.nrActive;
174 RunwayGroup& RunwayGroup:: operator= (const RunwayGroup &other)
178 RunwayListVecConstIterator i;
179 for (i = other.rwyList.begin(); i != other.rwyList.end(); i++)
180 rwyList.push_back(*i);
181 choice[0] = other.choice[0];
182 choice[1] = other.choice[1];
183 nrActive = other.nrActive;
187 void RunwayGroup::setActive(const FGAirport* airport,
192 stringVec *currentlyActive)
196 int activeRwys = rwyList.size(); // get the number of runways active
198 // bool found = true;
205 int bestMatch = 0, bestChoice = 0;
209 // Now downward iterate across all the possible preferences
210 // starting by the least preferred choice working toward the most preferred choice
212 nrOfPreferences = rwyList[0].getRwyList()->size();
213 bool validSelection = true;
214 bool foundValidSelection = false;
215 for (int i = nrOfPreferences-1; i >= 0; i--)
220 // Test each runway listed in the preference to see if it's possible to use
221 // If one runway of the selection isn't allowed, we need to exclude this
222 // preference, however, we don't want to stop right there, because we also
223 // don't want to randomly swap runway preferences, unless there is a need to.
225 validSelection = true;
226 for (int j = 0; j < activeRwys; j++)
228 rwy = airport->getRunwayByIdent(rwyList[j].getRwyList(i));
230 //cerr << "Succes" << endl;
231 hdgDiff = fabs(windHeading - rwy->headingDeg());
232 //cerr << "Wind Heading: " << windHeading << "Runway Heading: " <<rwy._heading << endl;
233 //cerr << "Wind Speed : " << windSpeed << endl;
235 hdgDiff = 360 - hdgDiff;
236 //cerr << "Heading diff: " << hdgDiff << endl;
237 hdgDiff *= ((2*M_PI)/360.0); // convert to radians
238 crossWind = windSpeed * sin(hdgDiff);
239 tailWind = -windSpeed * cos(hdgDiff);
240 //cerr << ". Tailwind : " << tailWind;
241 //cerr << ". Crosswnd : " << crossWind;
242 if ((tailWind > maxTail) || (crossWind > maxCross))
244 //cerr << ". [Invalid] " << endl;
245 validSelection = false;
249 //cerr << ". [Valid] ";
252 } // of active runways iteration
256 //cerr << "Valid selection : " << i << endl;;
257 foundValidSelection = true;
258 for (stringVecIterator it = currentlyActive->begin();
259 it != currentlyActive->end(); it++)
264 if (match >= bestMatch) {
269 //cerr << "Preference " << i << " bestMatch " << bestMatch << " choice " << bestChoice << endl;
271 if (foundValidSelection)
273 //cerr << "Valid runay selection : " << bestChoice << endl;
274 nrActive = activeRwys;
278 // If this didn't work, due to heavy winds, try again
279 // but select only one landing and one takeoff runway.
282 for (int i = activeRwys-1; i; i--)
284 if (rwyList[i].getType() == string("landing"))
286 if (rwyList[i].getType() == string("takeoff"))
289 //cerr << "Choosing " << choice[0] << " for landing and " << choice[1] << "for takeoff" << endl;
290 nrOfPreferences = rwyList[0].getRwyList()->size();
291 for (int i = 0; i < nrOfPreferences; i++)
293 bool validSelection = true;
294 for (int j = 0; j < 2; j++)
296 name = rwyList[choice[j]].getRwyList(i);
297 rwy = airport->getRunwayByIdent(name);
299 //cerr << "Succes" << endl;
300 hdgDiff = fabs(windHeading - rwy->headingDeg());
302 hdgDiff = 360 - hdgDiff;
303 hdgDiff *= ((2*M_PI)/360.0); // convert to radians
304 crossWind = windSpeed * sin(hdgDiff);
305 tailWind = -windSpeed * cos(hdgDiff);
306 if ((tailWind > maxTail) || (crossWind > maxCross))
307 validSelection = false;
313 //cerr << "Valid runay selection : " << i << endl;
324 void RunwayGroup::getActive(int i, string &name, string &type)
330 if (nrActive == (int)rwyList.size())
332 name = rwyList[i].getRwyList(active);
333 type = rwyList[i].getType();
337 name = rwyList[choice[i]].getRwyList(active);
338 type = rwyList[choice[i]].getType();
341 /*****************************************************************************
342 * FGRunway preference
343 ****************************************************************************/
344 FGRunwayPreference::FGRunwayPreference(FGAirport* ap) :
347 //cerr << "Running default Constructor" << endl;
351 FGRunwayPreference::FGRunwayPreference(const FGRunwayPreference &other)
353 initialized = other.initialized;
355 comTimes = other.comTimes; // Commercial Traffic;
356 genTimes = other.genTimes; // General Aviation;
357 milTimes = other.milTimes; // Military Traffic;
359 PreferenceListConstIterator i;
360 for (i = other.preferences.begin(); i != other.preferences.end(); i++)
361 preferences.push_back(*i);
364 FGRunwayPreference & FGRunwayPreference::operator= (const FGRunwayPreference &other)
366 initialized = other.initialized;
368 comTimes = other.comTimes; // Commercial Traffic;
369 genTimes = other.genTimes; // General Aviation;
370 milTimes = other.milTimes; // Military Traffic;
372 PreferenceListConstIterator i;
374 for (i = other.preferences.begin(); i != other.preferences.end(); i++)
375 preferences.push_back(*i);
379 ScheduleTime *FGRunwayPreference::getSchedule(const char *trafficType)
381 if (!(strcmp(trafficType, "com"))) {
384 if (!(strcmp(trafficType, "gen"))) {
387 if (!(strcmp(trafficType, "mil"))) {
393 RunwayGroup *FGRunwayPreference::getGroup(const string &groupName)
395 PreferenceListIterator i = preferences.begin();
396 if (preferences.begin() == preferences.end())
398 while (!(i == preferences.end() || i->getName() == groupName))
400 if (i != preferences.end())
406 string FGRunwayPreference::getId() {