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.approach" );
70 bool FGCommList::LoadComms(SGPath path) {
72 sg_gzifstream fin( path.str() );
73 if ( !fin.is_open() ) {
74 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
78 // read in each line of the file
83 while ( fin.get(c) && c != '\0' ) {
86 while ( !fin.eof() ) {
90 if(a.type == INVALID) {
91 SG_LOG(SG_GENERAL, SG_ALERT, "WARNING - INVALID type found in " << path.str() << '\n');
93 // Push all stations onto frequency map
94 commlist_freq[a.freq].push_back(a);
96 // Push non-atis stations onto bucket map as well
99 SGBucket bucket(a.lon, a.lat);
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( double lon, double lat, double elev, double freq,
118 ATCData* ad, atc_type tp )
120 lon *= SGD_DEGREES_TO_RADIANS;
121 lat *= SGD_DEGREES_TO_RADIANS;
123 comm_list_type stations = commlist_freq[(int)(freq*100.0 + 0.5)];
124 comm_list_iterator current = stations.begin();
125 comm_list_iterator last = stations.end();
127 // double az1, az2, s;
128 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
131 // TODO - at the moment this loop returns the first match found in range
132 // We want to return the closest match in the event of a frequency conflict
133 for ( ; current != last ; ++current ) {
134 //cout << "testing " << current->get_ident() << endl;
135 station = Point3D(current->x, current->y, current->z);
136 //cout << "aircraft = " << aircraft << endl;
137 //cout << "station = " << station << endl;
139 d = aircraft.distance3Dsquared( station );
141 //cout << " dist = " << sqrt(d)
142 // << " range = " << current->get_range() * SG_NM_TO_METER << endl;
144 // match up to twice the published range so we can model
145 // reduced signal strength
146 if ( d < (2 * current->range * SG_NM_TO_METER
147 * 2 * current->range * SG_NM_TO_METER ) ) {
148 //cout << "matched = " << current->get_ident() << endl;
149 if((tp == INVALID) || (tp == (*current).type)) {
159 int FGCommList::FindByPos(double lon, double lat, double elev, comm_list_type* stations, atc_type tp)
161 // number of relevant stations found within range
163 stations->erase(stations->begin(), stations->end());
165 // get bucket number for plane position
166 SGBucket buck(lon, lat);
168 // get neigboring buckets
170 int bx = (int)( max_range*SG_NM_TO_METER / buck.get_width_m() / 2);
171 int by = (int)( max_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(lon, lat, 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();
182 double rlon = lon * SGD_DEGREES_TO_RADIANS;
183 double rlat = lat * SGD_DEGREES_TO_RADIANS;
185 // double az1, az2, s;
186 Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
189 for(; current != last; ++current) {
190 if((current->type == tp) || (tp == INVALID)) {
191 station = Point3D(current->x, current->y, current->z);
192 d = aircraft.distance3Dsquared( station );
193 if ( d < (current->range * SG_NM_TO_METER
194 * current->range * SG_NM_TO_METER ) ) {
195 stations->push_back(*current);
206 // Find by Airport code.
207 // This is basically a wrapper for a call to the airport database to get the airport
208 // position followed by a call to FindByPos(...)
209 bool FGCommList::FindByCode( string ICAO, ATCData& ad, atc_type tp ) {
211 if ( dclFindAirportID( ICAO, &a ) ) {
212 comm_list_type stations;
213 int found = FindByPos(a.longitude, a.latitude, a.elevation, &stations, tp);
215 comm_list_iterator itr = stations.begin();
216 while(itr != stations.end()) {
217 if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
230 // TODO - this function should move somewhere else eventually!
231 // Return an appropriate call-sign for an ATIS transmission.
232 int FGCommList::GetCallSign( string apt_id, int hours, int mins )
234 atis_transmission_type tran;
236 if(atislog.find(apt_id) == atislog.end()) {
237 // This station has not transmitted yet - return a random identifier
238 // and add the transmission to the log
242 tran.callsign = int(sg_random() * 25) + 1; // This *should* give a random int between 1 and 26
243 //atislog[apt_id].push_back(tran);
244 atislog[apt_id] = tran;
246 // This station has transmitted - calculate the appropriate identifier
247 // and add the transmission to the log if it has changed
248 tran = atislog[apt_id];
249 // This next bit assumes that no-one comes back to the same ATIS station
250 // after running FlightGear for more than 24 hours !!
251 if((tran.hours == hours) && (tran.mins == mins)) {
252 return(tran.callsign);
254 if(tran.hours == hours) {
255 // The minutes must have changed
259 if(hours < tran.hours) {
262 tran.callsign += (hours - tran.hours);
264 // Assume transmissions were made on every hour
270 // Wrap if we've exceeded Zulu
271 if(tran.callsign > 26) {
274 // And write the new transmission to the log
275 atislog[apt_id] = tran;
278 return(tran.callsign);