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