]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/commlist.cxx
Remove confusing default (missing) path from 2D panel code.
[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) + 1;
171      int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 ) + 1;
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_map_const_iterator Fstations = commlist_bck.find(bucket);
179              if (Fstations == commlist_bck.end()) continue;
180              comm_list_const_iterator current = Fstations->second.begin();
181              comm_list_const_iterator last = Fstations->second.end();
182              
183              
184              // double az1, az2, s;
185              SGVec3d aircraft = SGVec3d::fromGeod(aPos);
186              double d;
187              for(; current != last; ++current) {
188                  if((current->type == tp) || (tp == INVALID)) {
189                      d = distSqr(aircraft, current->cart);
190                      // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
191                      if ( d < (current->range * SG_NM_TO_METER
192                           * current->range * SG_NM_TO_METER ) ) {
193                          stations->push_back(*current);
194                          ++found;
195                      }
196                  }
197              }
198          }
199      }
200      return found;
201 }
202
203
204 // Returns the distance in meters to the closest station of a given type,
205 // with the details written into ATCData& ad.  If no type is specifed simply
206 // returns the distance to the closest station of any type.
207 // Returns -9999 if no stations found within max_range in nautical miles (default 100 miles).
208 // Note that the search algorithm starts at 10 miles and multiplies by 10 thereafter, so if
209 // say 300 miles is specifed 10, then 100, then 1000 will be searched, breaking at first result 
210 // and giving up after 1000.
211 double FGCommList::FindClosest(const SGGeod& aPos, ATCData& ad, atc_type tp, double max_range) {
212     int num_stations = 0;
213     int range = 10;
214     comm_list_type stations;
215     comm_list_iterator itr;
216     double distance = -9999.0;
217     
218     while(num_stations == 0) {
219         num_stations = FindByPos(aPos, range, &stations, tp);
220         if(num_stations) {
221             double closest = max_range * SG_NM_TO_METER;
222             double tmp;
223             for(itr = stations.begin(); itr != stations.end(); ++itr) { 
224                 ATCData ad2 = *itr;    
225                 const FGAirport *a = fgFindAirportID(ad2.ident);
226                 if (a) {
227                     tmp = dclGetHorizontalSeparation(ad2.geod, aPos);
228                     if(tmp <= closest) {
229                         closest = tmp;
230                         distance = tmp;
231                         ad = *itr;
232                     }
233                 }
234             }
235             //cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
236             return(distance);
237         }
238         if(range > max_range) {
239             break;
240         }
241         range *= 10;
242     }
243     return(-9999.0);
244 }
245
246
247 // Find by Airport code.
248 // This is basically a wrapper for a call to the airport database to get the airport
249 // position followed by a call to FindByPos(...)
250 bool FGCommList::FindByCode( const string& ICAO, ATCData& ad, atc_type tp ) {
251     const FGAirport *a = fgFindAirportID( ICAO);
252     if ( a) {
253         comm_list_type stations;
254         int found = FindByPos(a->geod(), 10.0, &stations, tp);
255         if(found) {
256             comm_list_iterator itr = stations.begin();
257             while(itr != stations.end()) {
258                 if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
259                     ad = *itr;
260                     //cout << "FindByCode returns " << ICAO
261                     //     << "  type: " << tp
262                     //   << "  freq: " << itr->freq
263                     //   << endl;
264                     return true;
265                 }
266                 ++itr;
267             }
268         }
269     }
270     return false;
271 }
272
273 // TODO - this function should move somewhere else eventually!
274 // Return an appropriate sequence number for an ATIS transmission.
275 // Return sequence number + 2600 if sequence is unchanged since 
276 // last time.
277 int FGCommList::GetAtisSequence( const string& apt_id, 
278         const double tstamp, const int interval, const int special)
279 {
280     atis_transmission_type tran;
281     
282     if(atislog.find(apt_id) == atislog.end()) { // New station
283       tran.tstamp = tstamp - interval;
284 // Random number between 0 and 25 inclusive, i.e. 26 equiprobable outcomes:
285       tran.sequence = int(sg_random() * LTRS);
286       atislog[apt_id] = tran;
287       //cout << "New ATIS station: " << apt_id << " seq-1: "
288       //      << tran.sequence << endl;
289     } 
290
291 // calculate the appropriate identifier and update the log
292     tran = atislog[apt_id];
293
294     int delta = int((tstamp - tran.tstamp) / interval);
295     tran.tstamp += delta * interval;
296     if (special && !delta) delta++;     // a "special" ATIS update is required
297     tran.sequence = (tran.sequence + delta) % LTRS;
298     atislog[apt_id] = tran;
299     //if (delta) cout << "New ATIS sequence: " << tran.sequence
300     //      << "  Delta: " << delta << endl;
301     return(tran.sequence + (delta ? 0 : LTRS*1000));
302 }