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 <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 "ATCutils.hxx"
38 FGCommList *current_commlist;
42 FGCommList::FGCommList( void ) {
47 FGCommList::~FGCommList( void ) {
51 // load the navaids and build the map
52 bool FGCommList::init( const SGPath& path ) {
55 commlist_freq.erase(commlist_freq.begin(), commlist_freq.end());
56 commlist_bck.erase(commlist_bck.begin(), commlist_bck.end());
57 temp.append( "ATC/default.atis" );
60 temp.append( "ATC/default.tower" );
63 temp.append( "ATC/default.ground" );
66 temp.append( "ATC/default.approach" );
72 bool FGCommList::LoadComms(const SGPath& path) {
74 sg_gzifstream fin( path.str() );
75 if ( !fin.is_open() ) {
76 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
80 // read in each line of the file
83 while ( !fin.eof() ) {
86 if(a.type == INVALID) {
87 SG_LOG(SG_GENERAL, SG_DEBUG, "WARNING - INVALID type found in " << path.str() << '\n');
89 // Push all stations onto frequency map
90 commlist_freq[a.freq].push_back(a);
92 // Push non-atis stations onto bucket map as well
93 // In fact, push all stations onto bucket map for now so FGATCMgr::GetFrequency() works.
94 //if(a.type != ATIS) {
96 SGBucket bucket(a.lon, a.lat);
97 int bucknum = bucket.gen_index();
98 commlist_bck[bucknum].push_back(a);
110 // query the database for the specified frequency, lon and lat are in
111 // degrees, elev is in meters
112 // If no atc_type is specified, it returns true if any non-invalid type is found
113 // If atc_type is specifed, returns true only if the specified type is found
114 bool FGCommList::FindByFreq( double lon, double lat, double elev, double freq,
115 ATCData* ad, atc_type tp )
117 // HACK - if freq > 1000 assume it's in KHz, otherwise assume MHz.
118 // A bit ugly but it works for now!!!!
119 comm_list_type stations;
121 stations = commlist_freq[(int)freq];
123 stations = commlist_freq[(int)(freq*100.0 + 0.5)];
125 comm_list_iterator current = stations.begin();
126 comm_list_iterator last = stations.end();
128 // double az1, az2, s;
129 SGVec3d aircraft = SGVec3d::fromGeod(SGGeod::fromDegM(lon, lat, elev));
130 const double orig_max_d = 1e100;
131 double max_d = orig_max_d;
132 // TODO - at the moment this loop returns the first match found in range
133 // We want to return the closest match in the event of a frequency conflict
134 for ( ; current != last ; ++current ) {
135 //cout << "testing " << current->get_ident() << endl;
136 SGVec3d station(current->x, current->y, current->z);
137 //cout << "aircraft = " << aircraft << endl;
138 //cout << "station = " << station << endl;
140 double d = distSqr(aircraft, station);
142 //cout << " dist = " << sqrt(d)
143 // << " range = " << current->range * SG_NM_TO_METER << endl;
145 // TODO - match up to twice the published range so we can model
146 // reduced signal strength
147 // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
148 if ( d < (current->range * SG_NM_TO_METER
149 * current->range * SG_NM_TO_METER ) ) {
150 //cout << "matched = " << current->ident << endl;
151 if((tp == INVALID) || (tp == (*current).type)) {
160 if(max_d < orig_max_d) {
167 int FGCommList::FindByPos(double lon, double lat, double elev, double range, comm_list_type* stations, atc_type tp)
169 // number of relevant stations found within range
171 stations->erase(stations->begin(), stations->end());
173 // get bucket number for plane position
174 SGBucket buck(lon, lat);
176 // get neigboring buckets
177 int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2);
178 int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
180 // loop over bucket range
181 for ( int i=-bx; i<=bx; i++) {
182 for ( int j=-by; j<=by; j++) {
183 buck = sgBucketOffset(lon, lat, i, j);
184 long int bucket = buck.gen_index();
185 comm_list_type Fstations = commlist_bck[bucket];
186 comm_list_iterator current = Fstations.begin();
187 comm_list_iterator last = Fstations.end();
189 // double az1, az2, s;
190 SGVec3d aircraft = SGVec3d::fromGeod(SGGeod::fromDegM(lon, lat, elev));
191 for(; current != last; ++current) {
192 if((current->type == tp) || (tp == INVALID)) {
193 SGVec3d station(current->x, current->y, current->z);
194 double d = distSqr(aircraft, station);
195 // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
196 if ( d < (current->range * SG_NM_TO_METER
197 * current->range * SG_NM_TO_METER ) ) {
198 stations->push_back(*current);
209 // Returns the distance in meters to the closest station of a given type,
210 // with the details written into ATCData& ad. If no type is specifed simply
211 // returns the distance to the closest station of any type.
212 // Returns -9999 if no stations found within max_range in nautical miles (default 100 miles).
213 // Note that the search algorithm starts at 10 miles and multiplies by 10 thereafter, so if
214 // say 300 miles is specifed 10, then 100, then 1000 will be searched, breaking at first result
215 // and giving up after 1000.
216 double FGCommList::FindClosest( double lon, double lat, double elev, ATCData& ad, atc_type tp, double max_range) {
217 int num_stations = 0;
219 comm_list_type stations;
220 comm_list_iterator itr;
221 double distance = -9999.0;
223 while(num_stations == 0) {
224 num_stations = FindByPos(lon, lat, elev, range, &stations, tp);
226 double closest = max_range * SG_NM_TO_METER;
228 for(itr = stations.begin(); itr != stations.end(); ++itr) {
230 SGGeod p1 = SGGeod::fromDegM(ad2.lon, ad2.lat, ad2.elev);
231 const FGAirport *a = fgFindAirportID(ad2.ident);
233 SGGeod p2 = SGGeod::fromDegM(lon, lat, elev);
234 tmp = dclGetHorizontalSeparation(p1, p2);
242 //cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
245 if(range > max_range) {
254 // Find by Airport code.
255 // This is basically a wrapper for a call to the airport database to get the airport
256 // position followed by a call to FindByPos(...)
257 bool FGCommList::FindByCode( const string& ICAO, ATCData& ad, atc_type tp ) {
258 const FGAirport *a = fgFindAirportID( ICAO);
260 comm_list_type stations;
261 int found = FindByPos(a->getLongitude(), a->getLatitude(), a->getElevation(), 10.0, &stations, tp);
263 comm_list_iterator itr = stations.begin();
264 while(itr != stations.end()) {
265 if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
279 // TODO - this function should move somewhere else eventually!
280 // Return an appropriate call-sign for an ATIS transmission.
281 int FGCommList::GetCallSign( const string& apt_id, int hours, int mins )
283 atis_transmission_type tran;
285 if(atislog.find(apt_id) == atislog.end()) {
286 // This station has not transmitted yet - return a random identifier
287 // and add the transmission to the log
291 tran.callsign = int(sg_random() * 25) + 1; // This *should* give a random int between 1 and 26
292 //atislog[apt_id].push_back(tran);
293 atislog[apt_id] = tran;
295 // This station has transmitted - calculate the appropriate identifier
296 // and add the transmission to the log if it has changed
297 tran = atislog[apt_id];
298 // This next bit assumes that no-one comes back to the same ATIS station
299 // after running FlightGear for more than 24 hours !!
300 if((tran.hours == hours) && (tran.mins == mins)) {
301 return(tran.callsign);
303 if(tran.hours == hours) {
304 // The minutes must have changed
308 if(hours < tran.hours) {
311 tran.callsign += (hours - tran.hours);
313 // Assume transmissions were made on every hour
319 // Wrap if we've exceeded Zulu
320 if(tran.callsign > 26) {
323 // And write the new transmission to the log
324 atislog[apt_id] = tran;
327 return(tran.callsign);