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>
33 #include "commlist.hxx"
34 //#include "atislist.hxx"
37 FGCommList *current_commlist;
41 FGCommList::FGCommList( void ) {
46 FGCommList::~FGCommList( void ) {
50 // load the navaids and build the map
51 bool FGCommList::init( SGPath path ) {
54 commlist_freq.erase(commlist_freq.begin(), commlist_freq.end());
55 commlist_bck.erase(commlist_bck.begin(), commlist_bck.end());
56 temp.append( "ATC/default.atis" );
59 temp.append( "ATC/default.tower" );
62 temp.append( "ATC/default.approach" );
68 bool FGCommList::LoadComms(SGPath path) {
70 sg_gzifstream fin( path.str() );
71 if ( !fin.is_open() ) {
72 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
76 // read in each line of the file
81 while ( fin.get(c) && c != '\0' ) {
84 while ( !fin.eof() ) {
88 if(a.type == INVALID) {
92 // Push all stations onto frequency map
93 commlist_freq[a.freq].push_back(a);
95 // Push approach stations onto bucket map as well
96 if(a.type == APPROACH) {
98 SGBucket bucket(a.lon, a.lat);
99 int bucknum = bucket.gen_index();
100 commlist_bck[bucknum].push_back(a);
109 // query the database for the specified frequency, lon and lat are in
110 // degrees, elev is in meters
111 // If no atc_type is specified, it returns true if any non-invalid type is found
112 // If atc_type is specifed, returns true only if the specified type is found
113 bool FGCommList::FindByFreq( double lon, double lat, double elev, double freq,
114 ATCData* ad, atc_type tp )
116 lon *= SGD_DEGREES_TO_RADIANS;
117 lat *= SGD_DEGREES_TO_RADIANS;
119 comm_list_type stations = commlist_freq[(int)(freq*100.0 + 0.5)];
120 comm_list_iterator current = stations.begin();
121 comm_list_iterator last = stations.end();
123 // double az1, az2, s;
124 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
127 // TODO - at the moment this loop returns the first match found in range
128 // We want to return the closest match in the event of a frequency conflict
129 for ( ; current != last ; ++current ) {
130 //cout << "testing " << current->get_ident() << endl;
131 station = Point3D(current->x, current->y, current->z);
132 //cout << "aircraft = " << aircraft << endl;
133 //cout << "station = " << station << endl;
135 d = aircraft.distance3Dsquared( station );
137 //cout << " dist = " << sqrt(d)
138 // << " range = " << current->get_range() * SG_NM_TO_METER << endl;
140 // match up to twice the published range so we can model
141 // reduced signal strength
142 if ( d < (2 * current->range * SG_NM_TO_METER
143 * 2 * current->range * SG_NM_TO_METER ) ) {
144 //cout << "matched = " << current->get_ident() << endl;
145 if((tp == INVALID) || (tp == (*current).type)) {
156 int FGCommList::FindByPos(double lon, double lat, double elev, comm_list_type* stations, atc_type tp)
158 // number of relevant stations found within range
160 stations->erase(stations->begin(), stations->end());
162 // get bucket number for plane position
163 SGBucket buck(lon, lat);
165 // get neigboring buckets
167 int bx = (int)( max_range*SG_NM_TO_METER / buck.get_width_m() / 2);
168 int by = (int)( max_range*SG_NM_TO_METER / buck.get_height_m() / 2 );
170 // loop over bucket range
171 for ( int i=-bx; i<bx; i++) {
172 for ( int j=-by; j<by; j++) {
173 buck = sgBucketOffset(lon, lat, i, j);
174 long int bucket = buck.gen_index();
175 comm_list_type Fstations = commlist_bck[bucket];
176 comm_list_iterator current = Fstations.begin();
177 comm_list_iterator last = Fstations.end();
179 double rlon = lon * SGD_DEGREES_TO_RADIANS;
180 double rlat = lat * SGD_DEGREES_TO_RADIANS;
182 // double az1, az2, s;
183 Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
186 for(; current != last; ++current) {
187 if((current->type == tp) || (tp == INVALID)) {
188 station = Point3D(current->x, current->y, current->z);
189 d = aircraft.distance3Dsquared( station );
190 if ( d < (current->range * SG_NM_TO_METER
191 * current->range * SG_NM_TO_METER ) ) {
192 stations->push_back(*current);
203 // TODO - this function should move somewhere else eventually!
204 // Return an appropriate call-sign for an ATIS transmission.
205 int FGCommList::GetCallSign( string apt_id, int hours, int mins )
207 atis_transmission_type tran;
209 if(atislog.find(apt_id) == atislog.end()) {
210 // This station has not transmitted yet - return a random identifier
211 // and add the transmission to the log
215 tran.callsign = int(sg_random() * 25) + 1; // This *should* give a random int between 1 and 26
216 //atislog[apt_id].push_back(tran);
217 atislog[apt_id] = tran;
219 // This station has transmitted - calculate the appropriate identifier
220 // and add the transmission to the log if it has changed
221 tran = atislog[apt_id];
222 // This next bit assumes that no-one comes back to the same ATIS station
223 // after running FlightGear for more than 24 hours !!
224 if((tran.hours == hours) && (tran.mins == mins)) {
225 return(tran.callsign);
227 if(tran.hours == hours) {
228 // The minutes must have changed
232 if(hours < tran.hours) {
235 tran.callsign += (hours - tran.hours);
237 // Assume transmissions were made on every hour
243 // Wrap if we've exceeded Zulu
244 if(tran.callsign > 26) {
247 // And write the new transmission to the log
248 atislog[apt_id] = tran;
251 return(tran.callsign);