]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/commlist.cxx
Avoid casts if there is a method for the apropriate job.
[flightgear.git] / src / ATCDCL / 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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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( const 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(const 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     while ( !fin.eof() ) {
85         ATCData a;
86                 fin >> a;
87                 if(a.type == INVALID) {
88                         SG_LOG(SG_GENERAL, SG_DEBUG, "WARNING - INVALID type found in " << path.str() << '\n');
89                 } else {                
90                         // Push all stations onto frequency map
91                         commlist_freq[a.freq].push_back(a);
92                         
93                         // Push non-atis stations onto bucket map as well
94                         // In fact, push all stations onto bucket map for now so FGATCMgr::GetFrequency() works.
95                         //if(a.type != ATIS) {
96                                 // get bucket number
97                                 SGBucket bucket(a.lon, a.lat);
98                                 int bucknum = bucket.gen_index();
99                                 commlist_bck[bucknum].push_back(a);
100                         //}
101                 }
102                 
103                 fin >> skipcomment;
104         }
105         
106         fin.close();
107         return true;    
108 }
109
110
111 // query the database for the specified frequency, lon and lat are in
112 // degrees, elev is in meters
113 // If no atc_type is specified, it returns true if any non-invalid type is found
114 // If atc_type is specifed, returns true only if the specified type is found
115 bool FGCommList::FindByFreq( double lon, double lat, double elev, double freq,
116                                                 ATCData* ad, atc_type tp )
117 {
118         lon *= SGD_DEGREES_TO_RADIANS;
119         lat *= SGD_DEGREES_TO_RADIANS;
120         
121         // HACK - if freq > 1000 assume it's in KHz, otherwise assume MHz.
122         // A bit ugly but it works for now!!!!
123         comm_list_type stations;
124         if(freq > 1000.0) {
125                 stations = commlist_freq[(int)freq];
126         } else {
127                 stations = commlist_freq[(int)(freq*100.0 + 0.5)];
128         }
129         comm_list_iterator current = stations.begin();
130         comm_list_iterator last = stations.end();
131         
132         // double az1, az2, s;
133         Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
134         Point3D station;
135         const double orig_max_d = 1e100; 
136         double max_d = orig_max_d;
137         double d;
138         // TODO - at the moment this loop returns the first match found in range
139         // We want to return the closest match in the event of a frequency conflict
140         for ( ; current != last ; ++current ) {
141                 //cout << "testing " << current->get_ident() << endl;
142                 station = Point3D(current->x, current->y, current->z);
143                 //cout << "aircraft = " << aircraft << endl;
144                 //cout << "station = " << station << endl;
145                 
146                 d = aircraft.distance3Dsquared( station );
147                 
148                 //cout << "  dist = " << sqrt(d)
149                 //     << "  range = " << current->range * SG_NM_TO_METER << endl;
150                 
151                 // TODO - match up to twice the published range so we can model
152                 // reduced signal strength
153                 // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
154                 if ( d < (current->range * SG_NM_TO_METER 
155                      * current->range * SG_NM_TO_METER ) ) {
156                         //cout << "matched = " << current->ident << endl;
157                         if((tp == INVALID) || (tp == (*current).type)) {
158                                 if(d < max_d) {
159                                         max_d = d;
160                                         *ad = *current;
161                                 }
162                         }
163                 }
164         }
165         
166         if(max_d < orig_max_d) {
167                 return true;
168         } else {
169                 return false;
170         }
171 }
172
173 int FGCommList::FindByPos(double lon, double lat, double elev, double range, comm_list_type* stations, atc_type tp)
174 {
175         // number of relevant stations found within range
176         int found = 0;
177         stations->erase(stations->begin(), stations->end());
178         
179         // get bucket number for plane position
180         SGBucket buck(lon, lat);
181
182         // get neigboring buckets
183         int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2);
184         int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
185         
186         // loop over bucket range 
187         for ( int i=-bx; i<=bx; i++) {
188                 for ( int j=-by; j<=by; j++) {
189                         buck = sgBucketOffset(lon, lat, i, j);
190                         long int bucket = buck.gen_index();
191                         comm_list_type Fstations = commlist_bck[bucket];
192                         comm_list_iterator current = Fstations.begin();
193                         comm_list_iterator last = Fstations.end();
194                         
195                         double rlon = lon * SGD_DEGREES_TO_RADIANS;
196                         double rlat = lat * SGD_DEGREES_TO_RADIANS;
197                         
198                         // double az1, az2, s;
199                         Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
200                         Point3D station;
201                         double d;
202                         for(; current != last; ++current) {
203                                 if((current->type == tp) || (tp == INVALID)) {
204                                         station = Point3D(current->x, current->y, current->z);
205                                         d = aircraft.distance3Dsquared( station );
206                                         // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
207                                         if ( d < (current->range * SG_NM_TO_METER
208                                              * current->range * SG_NM_TO_METER ) ) {
209                                                 stations->push_back(*current);
210                                                 ++found;
211                                         }
212                                 }
213                         }
214                 }
215         }
216         return found;
217 }
218
219
220 // Returns the distance in meters to the closest station of a given type,
221 // with the details written into ATCData& ad.  If no type is specifed simply
222 // returns the distance to the closest station of any type.
223 // Returns -9999 if no stations found within max_range in nautical miles (default 100 miles).
224 // Note that the search algorithm starts at 10 miles and multiplies by 10 thereafter, so if
225 // say 300 miles is specifed 10, then 100, then 1000 will be searched, breaking at first result 
226 // and giving up after 1000.
227 double FGCommList::FindClosest( double lon, double lat, double elev, ATCData& ad, atc_type tp, double max_range) {
228         int num_stations = 0;
229         int range = 10;
230         comm_list_type stations;
231         comm_list_iterator itr;
232         double distance = -9999.0;
233         
234         while(num_stations == 0) {
235                 num_stations = FindByPos(lon, lat, elev, range, &stations, tp);
236                 if(num_stations) {
237                         double closest = max_range * SG_NM_TO_METER;
238                         double tmp;
239                         for(itr = stations.begin(); itr != stations.end(); ++itr) {     
240                                 ATCData ad2 = *itr;
241                                 //Point3D p1(*itr.lon, *itr.lat, *itr.elev);
242                                 Point3D p1(ad2.lon, ad2.lat, ad2.elev);
243                                 const FGAirport *a = fgFindAirportID(ad2.ident);
244                                 if (a) {
245                                         Point3D p2(lon, lat, elev);
246                                         tmp = dclGetHorizontalSeparation(p1, p2);
247                                         if(tmp <= closest) {
248                                                 closest = tmp;
249                                                 distance = tmp;
250                                                 ad = *itr;
251                                         }
252                                 }
253                         }
254                         //cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
255                         return(distance);
256                 }
257                 if(range > max_range) {
258                         break;
259                 }
260                 range *= 10;
261         }
262         return(-9999.0);
263 }
264
265
266 // Find by Airport code.
267 // This is basically a wrapper for a call to the airport database to get the airport
268 // position followed by a call to FindByPos(...)
269 bool FGCommList::FindByCode( const string& ICAO, ATCData& ad, atc_type tp ) {
270     const FGAirport *a = fgFindAirportID( ICAO);
271     if ( a) {
272                 comm_list_type stations;
273                 int found = FindByPos(a->getLongitude(), a->getLatitude(), a->getElevation(), 10.0, &stations, tp);
274                 if(found) {
275                         comm_list_iterator itr = stations.begin();
276                         while(itr != stations.end()) {
277                                 if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
278                                         ad = *itr;
279                                         return true;
280                                 }
281                                 ++itr;
282                         }
283                 }
284     } else {
285         return false;
286     }
287         return false;
288 }
289
290
291 // TODO - this function should move somewhere else eventually!
292 // Return an appropriate call-sign for an ATIS transmission.
293 int FGCommList::GetCallSign( const string& apt_id, int hours, int mins )
294 {
295         atis_transmission_type tran;
296         
297         if(atislog.find(apt_id) == atislog.end()) {
298                 // This station has not transmitted yet - return a random identifier
299                 // and add the transmission to the log
300                 tran.hours = hours;
301                 tran.mins = mins;
302                 sg_srandom_time();
303                 tran.callsign = int(sg_random() * 25) + 1;      // This *should* give a random int between 1 and 26
304                 //atislog[apt_id].push_back(tran);
305                 atislog[apt_id] = tran;
306         } else {
307                 // This station has transmitted - calculate the appropriate identifier
308                 // and add the transmission to the log if it has changed
309                 tran = atislog[apt_id];
310                 // This next bit assumes that no-one comes back to the same ATIS station
311                 // after running FlightGear for more than 24 hours !!
312                 if((tran.hours == hours) && (tran.mins == mins)) {
313                         return(tran.callsign);
314                 } else {
315                         if(tran.hours == hours) {
316                                 // The minutes must have changed
317                                 tran.mins = mins;
318                                 tran.callsign++;
319                         } else {
320                                 if(hours < tran.hours) {
321                                         hours += 24;
322                                 }
323                                 tran.callsign += (hours - tran.hours);
324                                 if(mins != 0) {
325                                         // Assume transmissions were made on every hour
326                                         tran.callsign++;
327                                 }
328                                 tran.hours = hours;
329                                 tran.mins = mins;
330                         }
331                         // Wrap if we've exceeded Zulu
332                         if(tran.callsign > 26) {
333                                 tran.callsign -= 26;
334                         }
335                         // And write the new transmission to the log
336                         atislog[apt_id] = tran;
337                 }
338         }
339         return(tran.callsign);
340 }