]> git.mxchange.org Git - flightgear.git/blob - src/ATC/commlist.cxx
Moved some of the low level scene graph construction code over to simgear.
[flightgear.git] / src / ATC / commlist.cxx
1 // commlist.cxx -- comm frequency lookup class
2 //
3 // Written by David Luff and Alexander Kappes, started Jan 2003.
4 // Based on navlist.cxx by Curtis Olson, started April 2000.
5 //
6 // Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
7 //
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.
12 //
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.
17 //
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.
21
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/misc/sgstream.hxx>
29 #include <simgear/math/sg_geodesy.hxx>
30 #include <simgear/math/sg_random.h>
31 #include <simgear/bucket/newbucket.hxx>
32 #include <Airports/simple.hxx>
33
34 #include "commlist.hxx"
35 //#include "atislist.hxx"
36 #include "ATCutils.hxx"
37
38
39 FGCommList *current_commlist;
40
41
42 // Constructor
43 FGCommList::FGCommList( void ) {
44 }
45
46
47 // Destructor
48 FGCommList::~FGCommList( void ) {
49 }
50
51
52 // load the navaids and build the map
53 bool FGCommList::init( SGPath path ) {
54
55         SGPath temp = path;
56     commlist_freq.erase(commlist_freq.begin(), commlist_freq.end());
57         commlist_bck.erase(commlist_bck.begin(), commlist_bck.end());
58     temp.append( "ATC/default.atis" );
59         LoadComms(temp);
60         temp = path;
61         temp.append( "ATC/default.tower" );
62         LoadComms(temp);
63         temp = path;
64         temp.append( "ATC/default.ground" );
65         LoadComms(temp);
66         temp = path;
67         temp.append( "ATC/default.approach" );
68         LoadComms(temp);
69         return true;
70 }
71         
72
73 bool FGCommList::LoadComms(SGPath path) {
74
75     sg_gzifstream fin( path.str() );
76     if ( !fin.is_open() ) {
77         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
78         exit(-1);
79     }
80         
81     // read in each line of the file
82     fin >> skipcomment;
83
84 #ifdef __MWERKS__
85     char c = 0;
86     while ( fin.get(c) && c != '\0' ) {
87         fin.putback(c);
88 #else
89     while ( !fin.eof() ) {
90 #endif
91         ATCData a;
92                 fin >> a;
93                 if(a.type == INVALID) {
94                         SG_LOG(SG_GENERAL, SG_ALERT, "WARNING - INVALID type found in " << path.str() << '\n');
95                 } else {                
96                         // Push all stations onto frequency map
97                         commlist_freq[a.freq].push_back(a);
98                         
99                         // Push non-atis stations onto bucket map as well
100                         // In fact, push all stations onto bucket map for now so FGATCMgr::GetFrequency() works.
101                         //if(a.type != ATIS) {
102                                 // get bucket number
103                                 SGBucket bucket(a.lon, a.lat);
104                                 int bucknum = bucket.gen_index();
105                                 commlist_bck[bucknum].push_back(a);
106                         //}
107                 }
108                 
109                 fin >> skipcomment;
110         }
111         
112         fin.close();
113         return true;    
114 }
115
116
117 // query the database for the specified frequency, lon and lat are in
118 // degrees, elev is in meters
119 // If no atc_type is specified, it returns true if any non-invalid type is found
120 // If atc_type is specifed, returns true only if the specified type is found
121 bool FGCommList::FindByFreq( double lon, double lat, double elev, double freq,
122                                                 ATCData* ad, atc_type tp )
123 {
124         lon *= SGD_DEGREES_TO_RADIANS;
125         lat *= SGD_DEGREES_TO_RADIANS;
126         
127         // HACK - if freq > 1000 assume it's in KHz, otherwise assume MHz.
128         // A bit ugly but it works for now!!!!
129         comm_list_type stations;
130         if(freq > 1000.0) {
131                 stations = commlist_freq[(int)freq];
132         } else {
133                 stations = commlist_freq[(int)(freq*100.0 + 0.5)];
134         }
135         comm_list_iterator current = stations.begin();
136         comm_list_iterator last = stations.end();
137         
138         // double az1, az2, s;
139         Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
140         Point3D station;
141         double d;
142         // TODO - at the moment this loop returns the first match found in range
143         // We want to return the closest match in the event of a frequency conflict
144         for ( ; current != last ; ++current ) {
145                 //cout << "testing " << current->get_ident() << endl;
146                 station = Point3D(current->x, current->y, current->z);
147                 //cout << "aircraft = " << aircraft << endl;
148                 //cout << "station = " << station << endl;
149                 
150                 d = aircraft.distance3Dsquared( station );
151                 
152                 //cout << "  dist = " << sqrt(d)
153                 //     << "  range = " << current->get_range() * SG_NM_TO_METER << endl;
154                 
155                 // match up to twice the published range so we can model
156                 // reduced signal strength
157                 if ( d < (2 * current->range * SG_NM_TO_METER 
158                 * 2 * current->range * SG_NM_TO_METER ) ) {
159                         //cout << "matched = " << current->get_ident() << endl;
160                         if((tp == INVALID) || (tp == (*current).type)) {
161                                 *ad = *current;
162                                 return true;
163                         }
164                 }
165         }
166         
167         return false;
168 }
169
170 int FGCommList::FindByPos(double lon, double lat, double elev, double range, comm_list_type* stations, atc_type tp)
171 {
172         // number of relevant stations found within range
173         int found = 0;
174         stations->erase(stations->begin(), stations->end());
175         
176         // get bucket number for plane position
177         SGBucket buck(lon, lat);
178
179         // get neigboring buckets
180         int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2);
181         int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
182         
183         // loop over bucket range 
184         for ( int i=-bx; i<=bx; i++) {
185                 for ( int j=-by; j<=by; j++) {
186                         buck = sgBucketOffset(lon, lat, i, j);
187                         long int bucket = buck.gen_index();
188                         comm_list_type Fstations = commlist_bck[bucket];
189                         comm_list_iterator current = Fstations.begin();
190                         comm_list_iterator last = Fstations.end();
191                         
192                         double rlon = lon * SGD_DEGREES_TO_RADIANS;
193                         double rlat = lat * SGD_DEGREES_TO_RADIANS;
194                         
195                         // double az1, az2, s;
196                         Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
197                         Point3D station;
198                         double d;
199                         for(; current != last; ++current) {
200                                 if((current->type == tp) || (tp == INVALID)) {
201                                         station = Point3D(current->x, current->y, current->z);
202                                         d = aircraft.distance3Dsquared( station );
203                                         if ( d < (current->range * SG_NM_TO_METER 
204                                         * current->range * SG_NM_TO_METER ) ) {
205                                                 stations->push_back(*current);
206                                                 ++found;
207                                         }
208                                 }
209                         }
210                 }
211         }
212         return found;
213 }
214
215
216 // Returns the distance in meters to the closest station of a given type,
217 // with the details written into ATCData& ad.  If no type is specifed simply
218 // returns the distance to the closest station of any type.
219 // Returns -9999 if no stations found within max_range in nautical miles (default 100 miles).
220 // Note that the search algorithm starts at 10 miles and multiplies by 10 thereafter, so if
221 // say 300 miles is specifed 10, then 100, then 1000 will be searched, breaking at first result 
222 // and giving up after 1000.
223 double FGCommList::FindClosest( double lon, double lat, double elev, ATCData& ad, atc_type tp, double max_range) {
224         int num_stations = 0;
225         int range = 10;
226         comm_list_type stations;
227         comm_list_iterator itr;
228         double distance = -9999.0;
229         
230         while(num_stations == 0) {
231                 num_stations = FindByPos(lon, lat, elev, range, &stations, tp);
232                 if(num_stations) {
233                         double closest = max_range * SG_NM_TO_METER;
234                         double tmp;
235                         for(itr = stations.begin(); itr != stations.end(); ++itr) {     
236                                 ATCData ad2 = *itr;
237                                 //Point3D p1(*itr.lon, *itr.lat, *itr.elev);
238                                 Point3D p1(ad2.lon, ad2.lat, ad2.elev);
239                                 FGAirport a;
240                                 if(dclFindAirportID(ad2.ident, &a)) {
241                                         Point3D p2(lon, lat, elev);
242                                         tmp = dclGetHorizontalSeparation(p1, p2);
243                                         if(tmp <= closest) {
244                                                 closest = tmp;
245                                                 distance = tmp;
246                                                 ad = *itr;
247                                         }
248                                 }
249                         }
250                         //cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
251                         return(distance);
252                 }
253                 if(range > max_range) {
254                         break;
255                 }
256                 range *= 10;
257         }
258         return(-9999.0);
259 }
260
261
262 // Find by Airport code.
263 // This is basically a wrapper for a call to the airport database to get the airport
264 // position followed by a call to FindByPos(...)
265 bool FGCommList::FindByCode( string ICAO, ATCData& ad, atc_type tp ) {
266     FGAirport a;
267     if ( dclFindAirportID( ICAO, &a ) ) {
268                 comm_list_type stations;
269                 int found = FindByPos(a.longitude, a.latitude, a.elevation, 10.0, &stations, tp);
270                 if(found) {
271                         comm_list_iterator itr = stations.begin();
272                         while(itr != stations.end()) {
273                                 if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
274                                         ad = *itr;
275                                         return true;
276                                 }
277                                 ++itr;
278                         }
279                 }
280     } else {
281         return false;
282     }
283         return false;
284 }
285
286
287 // TODO - this function should move somewhere else eventually!
288 // Return an appropriate call-sign for an ATIS transmission.
289 int FGCommList::GetCallSign( string apt_id, int hours, int mins )
290 {
291         atis_transmission_type tran;
292         
293         if(atislog.find(apt_id) == atislog.end()) {
294                 // This station has not transmitted yet - return a random identifier
295                 // and add the transmission to the log
296                 tran.hours = hours;
297                 tran.mins = mins;
298                 sg_srandom_time();
299                 tran.callsign = int(sg_random() * 25) + 1;      // This *should* give a random int between 1 and 26
300                 //atislog[apt_id].push_back(tran);
301                 atislog[apt_id] = tran;
302         } else {
303                 // This station has transmitted - calculate the appropriate identifier
304                 // and add the transmission to the log if it has changed
305                 tran = atislog[apt_id];
306                 // This next bit assumes that no-one comes back to the same ATIS station
307                 // after running FlightGear for more than 24 hours !!
308                 if((tran.hours == hours) && (tran.mins == mins)) {
309                         return(tran.callsign);
310                 } else {
311                         if(tran.hours == hours) {
312                                 // The minutes must have changed
313                                 tran.mins = mins;
314                                 tran.callsign++;
315                         } else {
316                                 if(hours < tran.hours) {
317                                         hours += 24;
318                                 }
319                                 tran.callsign += (hours - tran.hours);
320                                 if(mins != 0) {
321                                         // Assume transmissions were made on every hour
322                                         tran.callsign++;
323                                 }
324                                 tran.hours = hours;
325                                 tran.mins = mins;
326                         }
327                         // Wrap if we've exceeded Zulu
328                         if(tran.callsign > 26) {
329                                 tran.callsign -= 26;
330                         }
331                         // And write the new transmission to the log
332                         atislog[apt_id] = tran;
333                 }
334         }
335         return(tran.callsign);
336 }