]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/commlist.cxx
John Denker:
[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 "commlist.hxx"
28
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/misc/sgstream.hxx>
32 #include <simgear/math/sg_geodesy.hxx>
33 #include <simgear/math/sg_random.h>
34 #include <simgear/bucket/newbucket.hxx>
35 #include <Airports/simple.hxx>
36
37 #include "ATCutils.hxx"
38
39
40 FGCommList *current_commlist;
41
42
43 // Constructor
44 FGCommList::FGCommList( void ) {
45       sg_srandom_time();
46 }
47
48
49 // Destructor
50 FGCommList::~FGCommList( void ) {
51 }
52
53
54 // load the navaids and build the map
55 bool FGCommList::init( const SGPath& path ) {
56
57     SGPath temp = path;
58     commlist_freq.erase(commlist_freq.begin(), commlist_freq.end());
59     commlist_bck.erase(commlist_bck.begin(), commlist_bck.end());
60     temp.append( "ATC/default.atis" );
61     LoadComms(temp);
62     temp = path;
63     temp.append( "ATC/default.tower" );
64     LoadComms(temp);
65     temp = path;
66     temp.append( "ATC/default.ground" );
67     LoadComms(temp);
68     temp = path;
69     temp.append( "ATC/default.approach" );
70     LoadComms(temp);
71     return true;
72 }
73     
74
75 bool FGCommList::LoadComms(const SGPath& path) {
76
77     sg_gzifstream fin( path.str() );
78     if ( !fin.is_open() ) {
79         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
80         exit(-1);
81     }
82     
83     // read in each line of the file
84     fin >> skipcomment;
85
86     while ( !fin.eof() ) {
87         ATCData a;
88         fin >> a;
89         if(a.type == INVALID) {
90             SG_LOG(SG_GENERAL, SG_DEBUG, "WARNING - INVALID type found in " << path.str() << '\n');
91         } else {        
92             // Push all stations onto frequency map
93             commlist_freq[a.freq].push_back(a);
94             
95             // Push non-atis stations onto bucket map as well
96             // In fact, push all stations onto bucket map for now so FGATCMgr::GetFrequency() works.
97             //if(a.type != ATIS and/or AWOS?) {
98                 // get bucket number
99                 SGBucket bucket(a.geod);
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(const SGGeod& aPos, double freq,
118                                                 ATCData* ad, atc_type tp )
119 {
120     comm_list_type stations;
121     stations = commlist_freq[kHz10(freq)];
122     comm_list_iterator current = stations.begin();
123     comm_list_iterator last = stations.end();
124     
125     // double az1, az2, s;
126     SGVec3d aircraft = SGVec3d::fromGeod(aPos);
127     const double orig_max_d = 1e100; 
128     double max_d = orig_max_d;
129     double d;
130     // TODO - at the moment this loop returns the first match found in range
131     // We want to return the closest match in the event of a frequency conflict
132     for ( ; current != last ; ++current ) {
133         d = distSqr(aircraft, current->cart);
134         
135         //cout << "  dist = " << sqrt(d)
136         //     << "  range = " << current->range * SG_NM_TO_METER << endl;
137         
138         // TODO - match up to twice the published range so we can model
139         // reduced signal strength
140         // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
141         if ( d < (current->range * SG_NM_TO_METER 
142              * current->range * SG_NM_TO_METER ) ) {
143             //cout << "matched = " << current->ident << endl;
144             if((tp == INVALID) || (tp == (*current).type)) {
145                 if(d < max_d) {
146                     max_d = d;
147                     *ad = *current;
148                 }
149             }
150         }
151     }
152     
153     if(max_d < orig_max_d) {
154         return true;
155     } else {
156         return false;
157     }
158 }
159
160 int FGCommList::FindByPos(const SGGeod& aPos, double range, comm_list_type* stations, atc_type tp)
161 {
162      // number of relevant stations found within range
163      int found = 0;
164      stations->erase(stations->begin(), stations->end());
165      
166      // get bucket number for plane position
167      SGBucket buck(aPos);
168  
169      // get neigboring buckets
170      int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2);
171      int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
172      
173      // loop over bucket range 
174      for ( int i=-bx; i<=bx; i++) {
175          for ( int j=-by; j<=by; j++) {
176              buck = sgBucketOffset(aPos.getLongitudeDeg(), aPos.getLatitudeDeg(), i, j);
177              long int bucket = buck.gen_index();
178              comm_list_type Fstations = commlist_bck[bucket];
179              comm_list_iterator current = Fstations.begin();
180              comm_list_iterator last = Fstations.end();
181              
182              
183              // double az1, az2, s;
184              SGVec3d aircraft = SGVec3d::fromGeod(aPos);
185              double d;
186              for(; current != last; ++current) {
187                  if((current->type == tp) || (tp == INVALID)) {
188                      d = distSqr(aircraft, current->cart);
189                      // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
190                      if ( d < (current->range * SG_NM_TO_METER
191                           * current->range * SG_NM_TO_METER ) ) {
192                          stations->push_back(*current);
193                          ++found;
194                      }
195                  }
196              }
197          }
198      }
199      return found;
200 }
201
202
203 // Returns the distance in meters to the closest station of a given type,
204 // with the details written into ATCData& ad.  If no type is specifed simply
205 // returns the distance to the closest station of any type.
206 // Returns -9999 if no stations found within max_range in nautical miles (default 100 miles).
207 // Note that the search algorithm starts at 10 miles and multiplies by 10 thereafter, so if
208 // say 300 miles is specifed 10, then 100, then 1000 will be searched, breaking at first result 
209 // and giving up after 1000.
210 double FGCommList::FindClosest(const SGGeod& aPos, ATCData& ad, atc_type tp, double max_range) {
211     int num_stations = 0;
212     int range = 10;
213     comm_list_type stations;
214     comm_list_iterator itr;
215     double distance = -9999.0;
216     
217     while(num_stations == 0) {
218         num_stations = FindByPos(aPos, range, &stations, tp);
219         if(num_stations) {
220             double closest = max_range * SG_NM_TO_METER;
221             double tmp;
222             for(itr = stations.begin(); itr != stations.end(); ++itr) { 
223                 ATCData ad2 = *itr;    
224                 const FGAirport *a = fgFindAirportID(ad2.ident);
225                 if (a) {
226                     tmp = dclGetHorizontalSeparation(ad2.geod, aPos);
227                     if(tmp <= closest) {
228                         closest = tmp;
229                         distance = tmp;
230                         ad = *itr;
231                     }
232                 }
233             }
234             //cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
235             return(distance);
236         }
237         if(range > max_range) {
238             break;
239         }
240         range *= 10;
241     }
242     return(-9999.0);
243 }
244
245
246 // Find by Airport code.
247 // This is basically a wrapper for a call to the airport database to get the airport
248 // position followed by a call to FindByPos(...)
249 bool FGCommList::FindByCode( const string& ICAO, ATCData& ad, atc_type tp ) {
250     const FGAirport *a = fgFindAirportID( ICAO);
251     if ( a) {
252         comm_list_type stations;
253         int found = FindByPos(a->geod(), 10.0, &stations, tp);
254         if(found) {
255             comm_list_iterator itr = stations.begin();
256             while(itr != stations.end()) {
257                 if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
258                     ad = *itr;
259                     //cout << "FindByCode returns " << ICAO
260                     //     << "  type: " << tp
261                     //   << "  freq: " << itr->freq
262                     //   << endl;
263                     return true;
264                 }
265                 ++itr;
266             }
267         }
268     }
269     return false;
270 }
271
272 // TODO - this function should move somewhere else eventually!
273 // Return an appropriate sequence number for an ATIS transmission.
274 // Return sequence number + 2600 if sequence is unchanged since 
275 // last time.
276 int FGCommList::GetAtisSequence( const string& apt_id, 
277         const double tstamp, const int interval, const int special)
278 {
279     atis_transmission_type tran;
280     
281     if(atislog.find(apt_id) == atislog.end()) { // New station
282       tran.tstamp = tstamp - interval;
283 // Random number between 0 and 25 inclusive, i.e. 26 equiprobable outcomes:
284       tran.sequence = int(sg_random() * LTRS);
285       atislog[apt_id] = tran;
286       //cout << "New ATIS station: " << apt_id << " seq-1: "
287       //      << tran.sequence << endl;
288     } 
289
290 // calculate the appropriate identifier and update the log
291     tran = atislog[apt_id];
292
293     int delta = int((tstamp - tran.tstamp) / interval);
294     tran.tstamp += delta * interval;
295     if (special && !delta) delta++;     // a "special" ATIS update is required
296     tran.sequence = (tran.sequence + delta) % LTRS;
297     atislog[apt_id] = tran;
298     //if (delta) cout << "New ATIS sequence: " << tran.sequence
299     //      << "  Delta: " << delta << endl;
300     return(tran.sequence + (delta ? 0 : LTRS*1000));
301 }