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., 675 Mass Ave, Cambridge, MA 02139, USA.
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>
34 #include "commlist.hxx"
35 //#include "atislist.hxx"
36 #include "ATCutils.hxx"
39 FGCommList *current_commlist;
43 FGCommList::FGCommList( void ) {
48 FGCommList::~FGCommList( void ) {
52 // load the navaids and build the map
53 bool FGCommList::init( SGPath 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" );
61 temp.append( "ATC/default.tower" );
64 temp.append( "ATC/default.ground" );
67 temp.append( "ATC/default.approach" );
73 bool FGCommList::LoadComms(SGPath path) {
75 sg_gzifstream fin( path.str() );
76 if ( !fin.is_open() ) {
77 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
81 // read in each line of the file
86 while ( fin.get(c) && c != '\0' ) {
89 while ( !fin.eof() ) {
93 if(a.type == INVALID) {
94 SG_LOG(SG_GENERAL, SG_DEBUG, "WARNING - INVALID type found in " << path.str() << '\n');
96 // Push all stations onto frequency map
97 commlist_freq[a.freq].push_back(a);
99 // Push non-atis stations onto bucket map as well
100 // In fact, push all stations onto bucket map for now so FGATCMgr::GetFrequency() works.
101 //if(a.type != ATIS) {
103 SGBucket bucket(a.lon, a.lat);
104 int bucknum = bucket.gen_index();
105 commlist_bck[bucknum].push_back(a);
117 // query the database for the specified frequency, lon and lat are in
118 // degrees, elev is in meters
119 // If no atc_type is specified, it returns true if any non-invalid type is found
120 // If atc_type is specifed, returns true only if the specified type is found
121 bool FGCommList::FindByFreq( double lon, double lat, double elev, double freq,
122 ATCData* ad, atc_type tp )
124 lon *= SGD_DEGREES_TO_RADIANS;
125 lat *= SGD_DEGREES_TO_RADIANS;
127 // HACK - if freq > 1000 assume it's in KHz, otherwise assume MHz.
128 // A bit ugly but it works for now!!!!
129 comm_list_type stations;
131 stations = commlist_freq[(int)freq];
133 stations = commlist_freq[(int)(freq*100.0 + 0.5)];
135 comm_list_iterator current = stations.begin();
136 comm_list_iterator last = stations.end();
138 // double az1, az2, s;
139 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
141 const double orig_max_d = 1e100;
142 double max_d = orig_max_d;
144 // TODO - at the moment this loop returns the first match found in range
145 // We want to return the closest match in the event of a frequency conflict
146 for ( ; current != last ; ++current ) {
147 //cout << "testing " << current->get_ident() << endl;
148 station = Point3D(current->x, current->y, current->z);
149 //cout << "aircraft = " << aircraft << endl;
150 //cout << "station = " << station << endl;
152 d = aircraft.distance3Dsquared( station );
154 //cout << " dist = " << sqrt(d)
155 // << " range = " << current->range * SG_NM_TO_METER << endl;
157 // TODO - match up to twice the published range so we can model
158 // reduced signal strength
159 // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
160 if ( d < (current->range * SG_NM_TO_METER
161 * current->range * SG_NM_TO_METER ) ) {
162 //cout << "matched = " << current->ident << endl;
163 if((tp == INVALID) || (tp == (*current).type)) {
172 if(max_d < orig_max_d) {
179 int FGCommList::FindByPos(double lon, double lat, double elev, double range, comm_list_type* stations, atc_type tp)
181 // number of relevant stations found within range
183 stations->erase(stations->begin(), stations->end());
185 // get bucket number for plane position
186 SGBucket buck(lon, lat);
188 // get neigboring buckets
189 int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2);
190 int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
192 // loop over bucket range
193 for ( int i=-bx; i<=bx; i++) {
194 for ( int j=-by; j<=by; j++) {
195 buck = sgBucketOffset(lon, lat, i, j);
196 long int bucket = buck.gen_index();
197 comm_list_type Fstations = commlist_bck[bucket];
198 comm_list_iterator current = Fstations.begin();
199 comm_list_iterator last = Fstations.end();
201 double rlon = lon * SGD_DEGREES_TO_RADIANS;
202 double rlat = lat * SGD_DEGREES_TO_RADIANS;
204 // double az1, az2, s;
205 Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
208 for(; current != last; ++current) {
209 if((current->type == tp) || (tp == INVALID)) {
210 station = Point3D(current->x, current->y, current->z);
211 d = aircraft.distance3Dsquared( station );
212 // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
213 if ( d < (current->range * SG_NM_TO_METER
214 * current->range * SG_NM_TO_METER ) ) {
215 stations->push_back(*current);
226 // Returns the distance in meters to the closest station of a given type,
227 // with the details written into ATCData& ad. If no type is specifed simply
228 // returns the distance to the closest station of any type.
229 // Returns -9999 if no stations found within max_range in nautical miles (default 100 miles).
230 // Note that the search algorithm starts at 10 miles and multiplies by 10 thereafter, so if
231 // say 300 miles is specifed 10, then 100, then 1000 will be searched, breaking at first result
232 // and giving up after 1000.
233 double FGCommList::FindClosest( double lon, double lat, double elev, ATCData& ad, atc_type tp, double max_range) {
234 int num_stations = 0;
236 comm_list_type stations;
237 comm_list_iterator itr;
238 double distance = -9999.0;
240 while(num_stations == 0) {
241 num_stations = FindByPos(lon, lat, elev, range, &stations, tp);
243 double closest = max_range * SG_NM_TO_METER;
245 for(itr = stations.begin(); itr != stations.end(); ++itr) {
247 //Point3D p1(*itr.lon, *itr.lat, *itr.elev);
248 Point3D p1(ad2.lon, ad2.lat, ad2.elev);
250 if(dclFindAirportID(ad2.ident, &a)) {
251 Point3D p2(lon, lat, elev);
252 tmp = dclGetHorizontalSeparation(p1, p2);
260 //cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
263 if(range > max_range) {
272 // Find by Airport code.
273 // This is basically a wrapper for a call to the airport database to get the airport
274 // position followed by a call to FindByPos(...)
275 bool FGCommList::FindByCode( string ICAO, ATCData& ad, atc_type tp ) {
277 if ( dclFindAirportID( ICAO, &a ) ) {
278 comm_list_type stations;
279 int found = FindByPos(a.getLongitude(), a.getLatitude(), a.getElevation(), 10.0, &stations, tp);
281 comm_list_iterator itr = stations.begin();
282 while(itr != stations.end()) {
283 if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
297 // TODO - this function should move somewhere else eventually!
298 // Return an appropriate call-sign for an ATIS transmission.
299 int FGCommList::GetCallSign( string apt_id, int hours, int mins )
301 atis_transmission_type tran;
303 if(atislog.find(apt_id) == atislog.end()) {
304 // This station has not transmitted yet - return a random identifier
305 // and add the transmission to the log
309 tran.callsign = int(sg_random() * 25) + 1; // This *should* give a random int between 1 and 26
310 //atislog[apt_id].push_back(tran);
311 atislog[apt_id] = tran;
313 // This station has transmitted - calculate the appropriate identifier
314 // and add the transmission to the log if it has changed
315 tran = atislog[apt_id];
316 // This next bit assumes that no-one comes back to the same ATIS station
317 // after running FlightGear for more than 24 hours !!
318 if((tran.hours == hours) && (tran.mins == mins)) {
319 return(tran.callsign);
321 if(tran.hours == hours) {
322 // The minutes must have changed
326 if(hours < tran.hours) {
329 tran.callsign += (hours - tran.hours);
331 // Assume transmissions were made on every hour
337 // Wrap if we've exceeded Zulu
338 if(tran.callsign > 26) {
341 // And write the new transmission to the log
342 atislog[apt_id] = tran;
345 return(tran.callsign);