]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
Fixed an overly ambitious checkForCircularWaits() function. AI Aircraft
[flightgear.git] / src / Airports / simple.cxx
1 //
2 // simple.cxx -- a really simplistic class to manage airport ID,
3 //               lat, lon of the center of one of it's runways, and
4 //               elevation in feet.
5 //
6 // Written by Curtis Olson, started April 1998.
7 // Updated by Durk Talsma, started December, 2004.
8 //
9 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
10 //
11 // This program is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of the
14 // License, or (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful, but
17 // WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 // General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24 //
25 // $Id$
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <math.h>
32 #include <algorithm>
33
34 #include <simgear/compiler.h>
35
36 #include <plib/sg.h>
37 #include <plib/ul.h>
38
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/route/waypoint.hxx>
45 #include <simgear/debug/logstream.hxx>
46 #include <Main/globals.hxx>
47 #include <Main/fg_props.hxx>
48 #include <Airports/runways.hxx>
49
50 #include STL_STRING
51
52 #include "simple.hxx"
53 #include "xmlloader.hxx"
54
55 SG_USING_STD(sort);
56 SG_USING_STD(random_shuffle);
57
58
59
60
61 /***************************************************************************
62  * FGAirport
63  ***************************************************************************/
64 FGAirport::FGAirport() : _longitude(0), _latitude(0), _elevation(0)
65 {
66     dynamics = 0;
67 }
68
69
70 FGAirport::FGAirport(const string &id, double lon, double lat, double elev, const string &name, bool has_metar)
71 {
72     _id = id;
73     _longitude = lon;
74     _latitude  = lat;
75     _elevation = elev;
76     _name      = name;
77     _has_metar = has_metar;
78     dynamics   = 0;
79 }
80
81
82 FGAirport::~FGAirport()
83 {
84     delete dynamics;
85 }
86
87
88 FGAirportDynamics * FGAirport::getDynamics()
89 {
90     if (dynamics != 0) {
91         return dynamics;
92     } else {
93         //cerr << "Trying to load dynamics for " << _id << endl;
94         dynamics = new FGAirportDynamics(this);
95         XMLLoader::load(dynamics);
96
97         FGRunwayPreference rwyPrefs(this);
98         XMLLoader::load(&rwyPrefs);
99         dynamics->setRwyUse(rwyPrefs);
100    }
101     return dynamics;
102 }
103
104
105
106
107
108 /******************************************************************************
109  * FGAirportList
110  *****************************************************************************/
111
112 // Populates a list of subdirectories of $FG_ROOT/Airports/AI so that
113 // the add() method doesn't have to try opening 2 XML files in each of
114 // thousands of non-existent directories.  FIXME: should probably add
115 // code to free this list after parsing of apt.dat is finished;
116 // non-issue at the moment, however, as there are no AI subdirectories
117 // in the base package.
118 //
119 // Note: 2005/12/23: This is probably not necessary anymore, because I'm
120 // Switching to runtime airport dynamics loading (DT).
121 FGAirportList::FGAirportList()
122 {
123 //     ulDir* d;
124 //     ulDirEnt* dent;
125 //     SGPath aid( globals->get_fg_root() );
126 //     aid.append( "/Airports/AI" );
127 //     if((d = ulOpenDir(aid.c_str())) == NULL)
128 //         return;
129 //     while((dent = ulReadDir(d)) != NULL) {
130 //         SG_LOG( SG_GENERAL, SG_DEBUG, "Dent: " << dent->d_name );
131 //         ai_dirs.insert(dent->d_name);
132 //     }
133 //     ulCloseDir(d);
134 }
135
136
137 FGAirportList::~FGAirportList( void )
138 {
139     for (unsigned int i = 0; i < airports_array.size(); ++i) {
140         delete airports_array[i];
141     }
142 }
143
144
145 // add an entry to the list
146 void FGAirportList::add( const string &id, const double longitude,
147                          const double latitude, const double elevation,
148                          const string &name, const bool has_metar )
149 {
150     FGAirport* a = new FGAirport(id, longitude, latitude, elevation, name, has_metar);
151
152     airports_by_id[a->getId()] = a;
153     // try and read in an auxilary file
154
155     airports_array.push_back( a );
156     SG_LOG( SG_GENERAL, SG_BULK, "Adding " << id << " pos = " << longitude
157             << ", " << latitude << " elev = " << elevation );
158 }
159
160
161 // search for the specified id
162 FGAirport* FGAirportList::search( const string& id)
163 {
164     airport_map_iterator itr = airports_by_id.find(id);
165     return (itr == airports_by_id.end() ? NULL : itr->second);
166 }
167
168
169 // search for first subsequent alphabetically to supplied id
170 const FGAirport* FGAirportList::findFirstById( const string& id, bool exact )
171 {
172     airport_map_iterator itr;
173     if (exact) {
174         itr = airports_by_id.find(id);
175     } else {
176         itr = airports_by_id.lower_bound(id);
177     }
178     if (itr == airports_by_id.end()) {
179         return (NULL);
180     } else {
181         return (itr->second);
182     }
183 }
184
185
186 // search for the airport nearest the specified position
187 FGAirport* FGAirportList::search( double lon_deg, double lat_deg,
188                                   bool with_metar )
189 {
190     int closest = -1;
191     double min_dist = 360.0;
192     unsigned int i;
193     for ( i = 0; i < airports_array.size(); ++i ) {
194         // crude manhatten distance based on lat/lon difference
195         double d = fabs(lon_deg - airports_array[i]->getLongitude())
196             + fabs(lat_deg - airports_array[i]->getLatitude());
197         if ( d < min_dist ) {
198             if ( !with_metar || (with_metar&&airports_array[i]->getMetar()) ) {
199                 closest = i;
200                 min_dist = d;
201             }
202         }
203     }
204
205     return ( closest > -1 ? airports_array[closest] : NULL );
206 }
207
208
209 int
210 FGAirportList::size () const
211 {
212     return airports_array.size();
213 }
214
215
216 const FGAirport *FGAirportList::getAirport( unsigned int index ) const
217 {
218     if (index < airports_array.size()) {
219         return(airports_array[index]);
220     } else {
221         return(NULL);
222     }
223 }
224
225
226 /**
227  * Mark the specified airport record as not having metar
228  */
229 void FGAirportList::no_metar( const string &id )
230 {
231     if(airports_by_id.find(id) != airports_by_id.end()) {
232         airports_by_id[id]->setMetar(false);
233     }
234 }
235
236
237 /**
238  * Mark the specified airport record as (yes) having metar
239  */
240 void FGAirportList::has_metar( const string &id )
241 {
242     if(airports_by_id.find(id) != airports_by_id.end()) {
243         airports_by_id[id]->setMetar(true);
244     }
245 }
246
247
248 // find basic airport location info from airport database
249 const FGAirport *fgFindAirportID( const string& id)
250 {
251     const FGAirport* result = NULL;
252     if ( id.length() ) {
253         SG_LOG( SG_GENERAL, SG_BULK, "Searching for airport code = " << id );
254
255         result = globals->get_airports()->search( id );
256
257         if ( result == NULL ) {
258             SG_LOG( SG_GENERAL, SG_ALERT,
259                     "Failed to find " << id << " in apt.dat.gz" );
260             return NULL;
261         }
262     } else {
263         return NULL;
264     }
265     SG_LOG( SG_GENERAL, SG_BULK,
266             "Position for " << id << " is ("
267             << result->getLongitude() << ", "
268             << result->getLatitude() << ")" );
269
270     return result;
271 }
272
273
274 // get airport elevation
275 double fgGetAirportElev( const string& id )
276 {
277     SG_LOG( SG_GENERAL, SG_BULK,
278             "Finding elevation for airport: " << id );
279
280     const FGAirport *a=fgFindAirportID( id);
281     if (a) {
282         return a->getElevation();
283     } else {
284         return -9999.0;
285     }
286 }
287
288
289 // get airport position
290 Point3D fgGetAirportPos( const string& id )
291 {
292     SG_LOG( SG_ATC, SG_BULK,
293             "Finding position for airport: " << id );
294
295     const FGAirport *a = fgFindAirportID( id);
296
297     if (a) {
298         return Point3D(a->getLongitude(), a->getLatitude(), a->getElevation());
299     } else {
300         return Point3D(0.0, 0.0, -9999.0);
301     }
302 }