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);
171 int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
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();
183 // double az1, az2, s;
184 SGVec3d aircraft = SGVec3d::fromGeod(aPos);
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);
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;
213 comm_list_type stations;
214 comm_list_iterator itr;
215 double distance = -9999.0;
217 while(num_stations == 0) {
218 num_stations = FindByPos(aPos, range, &stations, tp);
220 double closest = max_range * SG_NM_TO_METER;
222 for(itr = stations.begin(); itr != stations.end(); ++itr) {
224 const FGAirport *a = fgFindAirportID(ad2.ident);
226 tmp = dclGetHorizontalSeparation(ad2.geod, aPos);
234 //cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
237 if(range > max_range) {
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);
252 comm_list_type stations;
253 int found = FindByPos(a->geod(), 10.0, &stations, tp);
255 comm_list_iterator itr = stations.begin();
256 while(itr != stations.end()) {
257 if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
259 //cout << "FindByCode returns " << ICAO
260 // << " type: " << tp
261 // << " freq: " << itr->freq
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
276 int FGCommList::GetAtisSequence( const string& apt_id,
277 const double tstamp, const int interval, const int special)
279 atis_transmission_type tran;
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;
290 // calculate the appropriate identifier and update the log
291 tran = atislog[apt_id];
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));