]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
Adding some more intelligence to the AI system step 2: Added a system to
[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
54 SG_USING_STD(sort);
55 SG_USING_STD(random_shuffle);
56
57
58
59
60 /***************************************************************************
61  * FGAirport
62  ***************************************************************************/
63 FGAirport::FGAirport() : _longitude(0), _latitude(0), _elevation(0)
64 {
65     dynamics = 0;
66 }
67
68
69 FGAirport::FGAirport(const string &id, double lon, double lat, double elev, const string &name, bool has_metar)
70 {
71     _id = id;
72     _longitude = lon;
73     _latitude  = lat;
74     _elevation = elev;
75     _name      = name;
76     _has_metar = has_metar;
77     dynamics   = 0;
78 }
79
80
81 FGAirport::~FGAirport()
82 {
83     delete dynamics;
84 }
85
86
87 FGAirportDynamics * FGAirport::getDynamics()
88 {
89     if (dynamics != 0) {
90         return dynamics;
91     } else {
92         FGRunwayPreference rwyPrefs;
93         //cerr << "Trying to load dynamics for " << _id << endl;
94         dynamics = new FGAirportDynamics(_latitude, _longitude, _elevation, _id);
95
96         SGPath parkpath( globals->get_fg_root() );
97         parkpath.append( "/Airports/AI/" );
98         parkpath.append(_id);
99         parkpath.append("parking.xml");
100
101         SGPath rwyPrefPath( globals->get_fg_root() );
102         rwyPrefPath.append( "/Airports/AI/" );
103         rwyPrefPath.append(_id);
104         rwyPrefPath.append("rwyuse.xml");
105
106         //if (ai_dirs.find(id.c_str()) != ai_dirs.end()
107         //  && parkpath.exists())
108         if (parkpath.exists()) {
109             try {
110                 readXML(parkpath.str(),*dynamics);
111                 dynamics->init();
112             } catch (const sg_exception &e) {
113                 //cerr << "unable to read " << parkpath.str() << endl;
114             }
115         }
116
117         //if (ai_dirs.find(id.c_str()) != ai_dirs.end()
118         //  && rwyPrefPath.exists())
119         if (rwyPrefPath.exists()) {
120             try {
121                 readXML(rwyPrefPath.str(), rwyPrefs);
122                 dynamics->setRwyUse(rwyPrefs);
123             } catch (const sg_exception &e) {
124                 //cerr << "unable to read " << rwyPrefPath.str() << endl;
125                 //exit(1);
126             }
127         }
128         //exit(1);
129     }
130     return dynamics;
131 }
132
133
134
135
136
137 /******************************************************************************
138  * FGAirportList
139  *****************************************************************************/
140
141 // Populates a list of subdirectories of $FG_ROOT/Airports/AI so that
142 // the add() method doesn't have to try opening 2 XML files in each of
143 // thousands of non-existent directories.  FIXME: should probably add
144 // code to free this list after parsing of apt.dat is finished;
145 // non-issue at the moment, however, as there are no AI subdirectories
146 // in the base package.
147 //
148 // Note: 2005/12/23: This is probably not necessary anymore, because I'm
149 // Switching to runtime airport dynamics loading (DT).
150 FGAirportList::FGAirportList()
151 {
152 //     ulDir* d;
153 //     ulDirEnt* dent;
154 //     SGPath aid( globals->get_fg_root() );
155 //     aid.append( "/Airports/AI" );
156 //     if((d = ulOpenDir(aid.c_str())) == NULL)
157 //         return;
158 //     while((dent = ulReadDir(d)) != NULL) {
159 //         SG_LOG( SG_GENERAL, SG_DEBUG, "Dent: " << dent->d_name );
160 //         ai_dirs.insert(dent->d_name);
161 //     }
162 //     ulCloseDir(d);
163 }
164
165
166 FGAirportList::~FGAirportList( void )
167 {
168     for (unsigned int i = 0; i < airports_array.size(); ++i) {
169         delete airports_array[i];
170     }
171 }
172
173
174 // add an entry to the list
175 void FGAirportList::add( const string &id, const double longitude,
176                          const double latitude, const double elevation,
177                          const string &name, const bool has_metar )
178 {
179     FGRunwayPreference rwyPrefs;
180     FGAirport* a = new FGAirport(id, longitude, latitude, elevation, name, has_metar);
181
182     airports_by_id[a->getId()] = a;
183     // try and read in an auxilary file
184
185     airports_array.push_back( a );
186     SG_LOG( SG_GENERAL, SG_BULK, "Adding " << id << " pos = " << longitude
187             << ", " << latitude << " elev = " << elevation );
188 }
189
190
191 // search for the specified id
192 FGAirport* FGAirportList::search( const string& id)
193 {
194     airport_map_iterator itr = airports_by_id.find(id);
195     return (itr == airports_by_id.end() ? NULL : itr->second);
196 }
197
198
199 // search for first subsequent alphabetically to supplied id
200 const FGAirport* FGAirportList::findFirstById( const string& id, bool exact )
201 {
202     airport_map_iterator itr;
203     if (exact) {
204         itr = airports_by_id.find(id);
205     } else {
206         itr = airports_by_id.lower_bound(id);
207     }
208     if (itr == airports_by_id.end()) {
209         return (NULL);
210     } else {
211         return (itr->second);
212     }
213 }
214
215
216 // search for the airport nearest the specified position
217 FGAirport* FGAirportList::search( double lon_deg, double lat_deg,
218                                   bool with_metar )
219 {
220     int closest = -1;
221     double min_dist = 360.0;
222     unsigned int i;
223     for ( i = 0; i < airports_array.size(); ++i ) {
224         // crude manhatten distance based on lat/lon difference
225         double d = fabs(lon_deg - airports_array[i]->getLongitude())
226             + fabs(lat_deg - airports_array[i]->getLatitude());
227         if ( d < min_dist ) {
228             if ( !with_metar || (with_metar&&airports_array[i]->getMetar()) ) {
229                 closest = i;
230                 min_dist = d;
231             }
232         }
233     }
234
235     return ( closest > -1 ? airports_array[closest] : NULL );
236 }
237
238
239 int
240 FGAirportList::size () const
241 {
242     return airports_array.size();
243 }
244
245
246 const FGAirport *FGAirportList::getAirport( unsigned int index ) const
247 {
248     if (index < airports_array.size()) {
249         return(airports_array[index]);
250     } else {
251         return(NULL);
252     }
253 }
254
255
256 /**
257  * Mark the specified airport record as not having metar
258  */
259 void FGAirportList::no_metar( const string &id )
260 {
261     if(airports_by_id.find(id) != airports_by_id.end()) {
262         airports_by_id[id]->setMetar(false);
263     }
264 }
265
266
267 /**
268  * Mark the specified airport record as (yes) having metar
269  */
270 void FGAirportList::has_metar( const string &id )
271 {
272     if(airports_by_id.find(id) != airports_by_id.end()) {
273         airports_by_id[id]->setMetar(true);
274     }
275 }
276
277
278 // find basic airport location info from airport database
279 const FGAirport *fgFindAirportID( const string& id)
280 {
281     const FGAirport* result = NULL;
282     if ( id.length() ) {
283         SG_LOG( SG_GENERAL, SG_BULK, "Searching for airport code = " << id );
284
285         result = globals->get_airports()->search( id );
286
287         if ( result == NULL ) {
288             SG_LOG( SG_GENERAL, SG_ALERT,
289                     "Failed to find " << id << " in apt.dat.gz" );
290             return NULL;
291         }
292     } else {
293         return NULL;
294     }
295     SG_LOG( SG_GENERAL, SG_BULK,
296             "Position for " << id << " is ("
297             << result->getLongitude() << ", "
298             << result->getLatitude() << ")" );
299
300     return result;
301 }
302
303
304 // get airport elevation
305 double fgGetAirportElev( const string& id )
306 {
307     SG_LOG( SG_GENERAL, SG_BULK,
308             "Finding elevation for airport: " << id );
309
310     const FGAirport *a=fgFindAirportID( id);
311     if (a) {
312         return a->getElevation();
313     } else {
314         return -9999.0;
315     }
316 }
317
318
319 // get airport position
320 Point3D fgGetAirportPos( const string& id )
321 {
322     SG_LOG( SG_ATC, SG_BULK,
323             "Finding position for airport: " << id );
324
325     const FGAirport *a = fgFindAirportID( id);
326
327     if (a) {
328         return Point3D(a->getLongitude(), a->getLatitude(), a->getElevation());
329     } else {
330         return Point3D(0.0, 0.0, -9999.0);
331     }
332 }