]> git.mxchange.org Git - flightgear.git/blob - src/ATC/commlist.cxx
any wind < 1kt is "calm", not just 0.0
[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 - 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 #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_DEBUG, "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         const double orig_max_d = 1e100; 
142         double max_d = orig_max_d;
143         double d;
144         // TODO - at the moment this loop returns the first match found in range
145         // We want to return the closest match in the event of a frequency conflict
146         for ( ; current != last ; ++current ) {
147                 //cout << "testing " << current->get_ident() << endl;
148                 station = Point3D(current->x, current->y, current->z);
149                 //cout << "aircraft = " << aircraft << endl;
150                 //cout << "station = " << station << endl;
151                 
152                 d = aircraft.distance3Dsquared( station );
153                 
154                 //cout << "  dist = " << sqrt(d)
155                 //     << "  range = " << current->range * SG_NM_TO_METER << endl;
156                 
157                 // TODO - match up to twice the published range so we can model
158                 // reduced signal strength
159                 // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
160                 if ( d < (current->range * SG_NM_TO_METER 
161                      * current->range * SG_NM_TO_METER ) ) {
162                         //cout << "matched = " << current->ident << endl;
163                         if((tp == INVALID) || (tp == (*current).type)) {
164                                 if(d < max_d) {
165                                         max_d = d;
166                                         *ad = *current;
167                                 }
168                         }
169                 }
170         }
171         
172         if(max_d < orig_max_d) {
173                 return true;
174         } else {
175                 return false;
176         }
177 }
178
179 int FGCommList::FindByPos(double lon, double lat, double elev, double range, comm_list_type* stations, atc_type tp)
180 {
181         // number of relevant stations found within range
182         int found = 0;
183         stations->erase(stations->begin(), stations->end());
184         
185         // get bucket number for plane position
186         SGBucket buck(lon, lat);
187
188         // get neigboring buckets
189         int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2);
190         int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
191         
192         // loop over bucket range 
193         for ( int i=-bx; i<=bx; i++) {
194                 for ( int j=-by; j<=by; j++) {
195                         buck = sgBucketOffset(lon, lat, i, j);
196                         long int bucket = buck.gen_index();
197                         comm_list_type Fstations = commlist_bck[bucket];
198                         comm_list_iterator current = Fstations.begin();
199                         comm_list_iterator last = Fstations.end();
200                         
201                         double rlon = lon * SGD_DEGREES_TO_RADIANS;
202                         double rlat = lat * SGD_DEGREES_TO_RADIANS;
203                         
204                         // double az1, az2, s;
205                         Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
206                         Point3D station;
207                         double d;
208                         for(; current != last; ++current) {
209                                 if((current->type == tp) || (tp == INVALID)) {
210                                         station = Point3D(current->x, current->y, current->z);
211                                         d = aircraft.distance3Dsquared( station );
212                                         // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
213                                         if ( d < (current->range * SG_NM_TO_METER
214                                              * current->range * SG_NM_TO_METER ) ) {
215                                                 stations->push_back(*current);
216                                                 ++found;
217                                         }
218                                 }
219                         }
220                 }
221         }
222         return found;
223 }
224
225
226 // Returns the distance in meters to the closest station of a given type,
227 // with the details written into ATCData& ad.  If no type is specifed simply
228 // returns the distance to the closest station of any type.
229 // Returns -9999 if no stations found within max_range in nautical miles (default 100 miles).
230 // Note that the search algorithm starts at 10 miles and multiplies by 10 thereafter, so if
231 // say 300 miles is specifed 10, then 100, then 1000 will be searched, breaking at first result 
232 // and giving up after 1000.
233 double FGCommList::FindClosest( double lon, double lat, double elev, ATCData& ad, atc_type tp, double max_range) {
234         int num_stations = 0;
235         int range = 10;
236         comm_list_type stations;
237         comm_list_iterator itr;
238         double distance = -9999.0;
239         
240         while(num_stations == 0) {
241                 num_stations = FindByPos(lon, lat, elev, range, &stations, tp);
242                 if(num_stations) {
243                         double closest = max_range * SG_NM_TO_METER;
244                         double tmp;
245                         for(itr = stations.begin(); itr != stations.end(); ++itr) {     
246                                 ATCData ad2 = *itr;
247                                 //Point3D p1(*itr.lon, *itr.lat, *itr.elev);
248                                 Point3D p1(ad2.lon, ad2.lat, ad2.elev);
249                                 const FGAirport *a = fgFindAirportID(ad2.ident);
250                                 if (a) {
251                                         Point3D p2(lon, lat, elev);
252                                         tmp = dclGetHorizontalSeparation(p1, p2);
253                                         if(tmp <= closest) {
254                                                 closest = tmp;
255                                                 distance = tmp;
256                                                 ad = *itr;
257                                         }
258                                 }
259                         }
260                         //cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
261                         return(distance);
262                 }
263                 if(range > max_range) {
264                         break;
265                 }
266                 range *= 10;
267         }
268         return(-9999.0);
269 }
270
271
272 // Find by Airport code.
273 // This is basically a wrapper for a call to the airport database to get the airport
274 // position followed by a call to FindByPos(...)
275 bool FGCommList::FindByCode( const string& ICAO, ATCData& ad, atc_type tp ) {
276     const FGAirport *a = fgFindAirportID( ICAO);
277     if ( a) {
278                 comm_list_type stations;
279                 int found = FindByPos(a->getLongitude(), a->getLatitude(), a->getElevation(), 10.0, &stations, tp);
280                 if(found) {
281                         comm_list_iterator itr = stations.begin();
282                         while(itr != stations.end()) {
283                                 if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
284                                         ad = *itr;
285                                         return true;
286                                 }
287                                 ++itr;
288                         }
289                 }
290     } else {
291         return false;
292     }
293         return false;
294 }
295
296
297 // TODO - this function should move somewhere else eventually!
298 // Return an appropriate call-sign for an ATIS transmission.
299 int FGCommList::GetCallSign( const string& apt_id, int hours, int mins )
300 {
301         atis_transmission_type tran;
302         
303         if(atislog.find(apt_id) == atislog.end()) {
304                 // This station has not transmitted yet - return a random identifier
305                 // and add the transmission to the log
306                 tran.hours = hours;
307                 tran.mins = mins;
308                 sg_srandom_time();
309                 tran.callsign = int(sg_random() * 25) + 1;      // This *should* give a random int between 1 and 26
310                 //atislog[apt_id].push_back(tran);
311                 atislog[apt_id] = tran;
312         } else {
313                 // This station has transmitted - calculate the appropriate identifier
314                 // and add the transmission to the log if it has changed
315                 tran = atislog[apt_id];
316                 // This next bit assumes that no-one comes back to the same ATIS station
317                 // after running FlightGear for more than 24 hours !!
318                 if((tran.hours == hours) && (tran.mins == mins)) {
319                         return(tran.callsign);
320                 } else {
321                         if(tran.hours == hours) {
322                                 // The minutes must have changed
323                                 tran.mins = mins;
324                                 tran.callsign++;
325                         } else {
326                                 if(hours < tran.hours) {
327                                         hours += 24;
328                                 }
329                                 tran.callsign += (hours - tran.hours);
330                                 if(mins != 0) {
331                                         // Assume transmissions were made on every hour
332                                         tran.callsign++;
333                                 }
334                                 tran.hours = hours;
335                                 tran.mins = mins;
336                         }
337                         // Wrap if we've exceeded Zulu
338                         if(tran.callsign > 26) {
339                                 tran.callsign -= 26;
340                         }
341                         // And write the new transmission to the log
342                         atislog[apt_id] = tran;
343                 }
344         }
345         return(tran.callsign);
346 }