1 // commlist.cxx -- comm frequency lookup class
3 // Written by David Luff and Alexander Kappes, started Jan 2003.
4 // Based on navlist.cxx by Curtis Olson, started April 2000.
6 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
27 #include "commlist.hxx"
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>
37 #include "ATCutils.hxx"
40 FGCommList *current_commlist;
44 FGCommList::FGCommList( void ) {
50 FGCommList::~FGCommList( void ) {
54 // load the navaids and build the map
55 bool FGCommList::init( const SGPath& 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" );
63 temp.append( "ATC/default.tower" );
66 temp.append( "ATC/default.ground" );
69 temp.append( "ATC/default.approach" );
75 bool FGCommList::LoadComms(const SGPath& path) {
77 sg_gzifstream fin( path.str() );
78 if ( !fin.is_open() ) {
79 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
83 // read in each line of the file
86 while ( !fin.eof() ) {
89 if(a.type == INVALID) {
90 SG_LOG(SG_GENERAL, SG_DEBUG, "WARNING - INVALID type found in " << path.str() << '\n');
92 // Push all stations onto frequency map
93 commlist_freq[a.freq].push_back(a);
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?) {
99 SGBucket bucket(a.geod);
100 int bucknum = bucket.gen_index();
101 commlist_bck[bucknum].push_back(a);
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 )
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();
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;
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);
135 //cout << " dist = " << sqrt(d)
136 // << " range = " << current->range * SG_NM_TO_METER << endl;
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)) {
153 if(max_d < orig_max_d) {
160 int FGCommList::FindByPos(const SGGeod& aPos, double range, comm_list_type* stations, atc_type tp)
162 // number of relevant stations found within range
164 stations->erase(stations->begin(), stations->end());
166 // get bucket number for plane position
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;
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();
184 // double az1, az2, s;
185 SGVec3d aircraft = SGVec3d::fromGeod(aPos);
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);
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;
214 comm_list_type stations;
215 comm_list_iterator itr;
216 double distance = -9999.0;
218 while(num_stations == 0) {
219 num_stations = FindByPos(aPos, range, &stations, tp);
221 double closest = max_range * SG_NM_TO_METER;
223 for(itr = stations.begin(); itr != stations.end(); ++itr) {
225 const FGAirport *a = fgFindAirportID(ad2.ident);
227 tmp = dclGetHorizontalSeparation(ad2.geod, aPos);
235 //cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
238 if(range > max_range) {
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);
253 comm_list_type stations;
254 int found = FindByPos(a->geod(), 10.0, &stations, tp);
256 comm_list_iterator itr = stations.begin();
257 while(itr != stations.end()) {
258 if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
260 //cout << "FindByCode returns " << ICAO
261 // << " type: " << tp
262 // << " freq: " << itr->freq
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
277 int FGCommList::GetAtisSequence( const string& apt_id,
278 const double tstamp, const int interval, const int special)
280 atis_transmission_type tran;
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;
291 // calculate the appropriate identifier and update the log
292 tran = atislog[apt_id];
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));