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