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 - curt@flightgear.org
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) );
142 // TODO - at the moment this loop returns the first match found in range
143 // We want to return the closest match in the event of a frequency conflict
144 for ( ; current != last ; ++current ) {
145 //cout << "testing " << current->get_ident() << endl;
146 station = Point3D(current->x, current->y, current->z);
147 //cout << "aircraft = " << aircraft << endl;
148 //cout << "station = " << station << endl;
150 d = aircraft.distance3Dsquared( station );
152 //cout << " dist = " << sqrt(d)
153 // << " range = " << current->get_range() * SG_NM_TO_METER << endl;
155 // match up to twice the published range so we can model
156 // reduced signal strength
157 if ( d < (2 * current->range * SG_NM_TO_METER
158 * 2 * current->range * SG_NM_TO_METER ) ) {
159 //cout << "matched = " << current->get_ident() << endl;
160 if((tp == INVALID) || (tp == (*current).type)) {
170 int FGCommList::FindByPos(double lon, double lat, double elev, double range, comm_list_type* stations, atc_type tp)
172 // number of relevant stations found within range
174 stations->erase(stations->begin(), stations->end());
176 // get bucket number for plane position
177 SGBucket buck(lon, lat);
179 // get neigboring buckets
180 int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2);
181 int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
183 // loop over bucket range
184 for ( int i=-bx; i<=bx; i++) {
185 for ( int j=-by; j<=by; j++) {
186 buck = sgBucketOffset(lon, lat, i, j);
187 long int bucket = buck.gen_index();
188 comm_list_type Fstations = commlist_bck[bucket];
189 comm_list_iterator current = Fstations.begin();
190 comm_list_iterator last = Fstations.end();
192 double rlon = lon * SGD_DEGREES_TO_RADIANS;
193 double rlat = lat * SGD_DEGREES_TO_RADIANS;
195 // double az1, az2, s;
196 Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
199 for(; current != last; ++current) {
200 if((current->type == tp) || (tp == INVALID)) {
201 station = Point3D(current->x, current->y, current->z);
202 d = aircraft.distance3Dsquared( station );
203 if ( d < (current->range * SG_NM_TO_METER
204 * current->range * SG_NM_TO_METER ) ) {
205 stations->push_back(*current);
216 // Returns the distance in meters to the closest station of a given type,
217 // with the details written into ATCData& ad. If no type is specifed simply
218 // returns the distance to the closest station of any type.
219 // Returns -9999 if no stations found within max_range in nautical miles (default 100 miles).
220 // Note that the search algorithm starts at 10 miles and multiplies by 10 thereafter, so if
221 // say 300 miles is specifed 10, then 100, then 1000 will be searched, breaking at first result
222 // and giving up after 1000.
223 double FGCommList::FindClosest( double lon, double lat, double elev, ATCData& ad, atc_type tp, double max_range) {
224 int num_stations = 0;
226 comm_list_type stations;
227 comm_list_iterator itr;
228 double distance = -9999.0;
230 while(num_stations == 0) {
231 num_stations = FindByPos(lon, lat, elev, range, &stations, tp);
233 double closest = max_range * SG_NM_TO_METER;
235 for(itr = stations.begin(); itr != stations.end(); ++itr) {
237 //Point3D p1(*itr.lon, *itr.lat, *itr.elev);
238 Point3D p1(ad2.lon, ad2.lat, ad2.elev);
240 if(dclFindAirportID(ad2.ident, &a)) {
241 Point3D p2(lon, lat, elev);
242 tmp = dclGetHorizontalSeparation(p1, p2);
250 //cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
253 if(range > max_range) {
262 // Find by Airport code.
263 // This is basically a wrapper for a call to the airport database to get the airport
264 // position followed by a call to FindByPos(...)
265 bool FGCommList::FindByCode( string ICAO, ATCData& ad, atc_type tp ) {
267 if ( dclFindAirportID( ICAO, &a ) ) {
268 comm_list_type stations;
269 int found = FindByPos(a.longitude, a.latitude, a.elevation, 10.0, &stations, tp);
271 comm_list_iterator itr = stations.begin();
272 while(itr != stations.end()) {
273 if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
287 // TODO - this function should move somewhere else eventually!
288 // Return an appropriate call-sign for an ATIS transmission.
289 int FGCommList::GetCallSign( string apt_id, int hours, int mins )
291 atis_transmission_type tran;
293 if(atislog.find(apt_id) == atislog.end()) {
294 // This station has not transmitted yet - return a random identifier
295 // and add the transmission to the log
299 tran.callsign = int(sg_random() * 25) + 1; // This *should* give a random int between 1 and 26
300 //atislog[apt_id].push_back(tran);
301 atislog[apt_id] = tran;
303 // This station has transmitted - calculate the appropriate identifier
304 // and add the transmission to the log if it has changed
305 tran = atislog[apt_id];
306 // This next bit assumes that no-one comes back to the same ATIS station
307 // after running FlightGear for more than 24 hours !!
308 if((tran.hours == hours) && (tran.mins == mins)) {
309 return(tran.callsign);
311 if(tran.hours == hours) {
312 // The minutes must have changed
316 if(hours < tran.hours) {
319 tran.callsign += (hours - tran.hours);
321 // Assume transmissions were made on every hour
327 // Wrap if we've exceeded Zulu
328 if(tran.callsign > 26) {
331 // And write the new transmission to the log
332 atislog[apt_id] = tran;
335 return(tran.callsign);