]> git.mxchange.org Git - flightgear.git/blob - src/ATC/commlist.cxx
Better robustness for the ATCData structure istream operator. Adding fin.close(...
[flightgear.git] / src / ATC / commlist.cxx
1 // commlist.cxx -- comm frequency lookup class
2 //
3 // Written by David Luff and Alexander Kappes, started Jan 2003.
4 // Based on navlist.cxx by Curtis Olson, started April 2000.
5 //
6 // Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
7 //
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.
12 //
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.
17 //
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.
21
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
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
33 #include "commlist.hxx"
34 //#include "atislist.hxx"
35
36
37 FGCommList *current_commlist;
38
39
40 // Constructor
41 FGCommList::FGCommList( void ) {
42 }
43
44
45 // Destructor
46 FGCommList::~FGCommList( void ) {
47 }
48
49
50 // load the navaids and build the map
51 bool FGCommList::init( SGPath path ) {
52
53         SGPath temp = 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" );
57         LoadComms(temp);
58         temp = path;
59         temp.append( "ATC/default.tower" );
60         LoadComms(temp);
61         temp = path;
62         temp.append( "ATC/default.approach" );
63         LoadComms(temp);
64         return true;
65 }
66         
67
68 bool FGCommList::LoadComms(SGPath path) {
69
70     sg_gzifstream fin( path.str() );
71     if ( !fin.is_open() ) {
72         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
73         exit(-1);
74     }
75         
76     // read in each line of the file
77     fin >> skipcomment;
78
79 #ifdef __MWERKS__
80     char c = 0;
81     while ( fin.get(c) && c != '\0' ) {
82         fin.putback(c);
83 #else
84     while ( !fin.eof() ) {
85 #endif
86         ATCData a;
87                 fin >> a;
88                 if(a.type == INVALID) {
89                         cout << "WARNING - INVALID type found in " << path.str() << '\n';
90                 } else {                
91                         // Push all stations onto frequency map
92                         commlist_freq[a.freq].push_back(a);
93                         
94                         // Push approach stations onto bucket map as well
95                         if(a.type == APPROACH) {
96                                 // get bucket number
97                                 SGBucket bucket(a.lon, a.lat);
98                                 int bucknum = bucket.gen_index();
99                                 commlist_bck[bucknum].push_back(a);
100                         }
101                 }
102                 
103                 fin >> skipcomment;
104         }
105         
106         fin.close();
107         return true;    
108 }
109
110
111 // query the database for the specified frequency, lon and lat are in
112 // degrees, elev is in meters
113 // If no atc_type is specified, it returns true if any non-invalid type is found
114 // If atc_type is specifed, returns true only if the specified type is found
115 bool FGCommList::FindByFreq( double lon, double lat, double elev, double freq,
116                                                 ATCData* ad, atc_type tp )
117 {
118         lon *= SGD_DEGREES_TO_RADIANS;
119         lat *= SGD_DEGREES_TO_RADIANS;
120         
121         comm_list_type stations = commlist_freq[(int)(freq*100.0 + 0.5)];
122         comm_list_iterator current = stations.begin();
123         comm_list_iterator last = stations.end();
124         
125         // double az1, az2, s;
126         Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
127         Point3D station;
128         double d;
129         // TODO - at the moment this loop returns the first match found in range
130         // We want to return the closest match in the event of a frequency conflict
131         for ( ; current != last ; ++current ) {
132                 //cout << "testing " << current->get_ident() << endl;
133                 station = Point3D(current->x, current->y, current->z);
134                 //cout << "aircraft = " << aircraft << endl;
135                 //cout << "station = " << station << endl;
136                 
137                 d = aircraft.distance3Dsquared( station );
138                 
139                 //cout << "  dist = " << sqrt(d)
140                 //     << "  range = " << current->get_range() * SG_NM_TO_METER << endl;
141                 
142                 // match up to twice the published range so we can model
143                 // reduced signal strength
144                 if ( d < (2 * current->range * SG_NM_TO_METER 
145                 * 2 * current->range * SG_NM_TO_METER ) ) {
146                         //cout << "matched = " << current->get_ident() << endl;
147                         if((tp == INVALID) || (tp == (*current).type)) {
148                                 *ad = *current;
149                                 return true;
150                         }
151                 }
152         }
153         
154         return false;
155 }
156
157
158 int FGCommList::FindByPos(double lon, double lat, double elev, comm_list_type* stations, atc_type tp)
159 {
160         // number of relevant stations found within range
161         int found = 0;
162         stations->erase(stations->begin(), stations->end());
163         
164         // get bucket number for plane position
165         SGBucket buck(lon, lat);
166
167         // get neigboring buckets
168         int max_range = 100;
169         int bx = (int)( max_range*SG_NM_TO_METER / buck.get_width_m() / 2);
170         int by = (int)( max_range*SG_NM_TO_METER / buck.get_height_m() / 2 );
171         
172         // loop over bucket range 
173         for ( int i=-bx; i<bx; i++) {
174                 for ( int j=-by; j<by; j++) {
175                         buck = sgBucketOffset(lon, lat, i, j);
176                         long int bucket = buck.gen_index();
177                         comm_list_type Fstations = commlist_bck[bucket];
178                         comm_list_iterator current = Fstations.begin();
179                         comm_list_iterator last = Fstations.end();
180                         
181                         double rlon = lon * SGD_DEGREES_TO_RADIANS;
182                         double rlat = lat * SGD_DEGREES_TO_RADIANS;
183                         
184                         // double az1, az2, s;
185                         Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
186                         Point3D station;
187                         double d;
188                         for(; current != last; ++current) {
189                                 if((current->type == tp) || (tp == INVALID)) {
190                                         station = Point3D(current->x, current->y, current->z);
191                                         d = aircraft.distance3Dsquared( station );
192                                         if ( d < (current->range * SG_NM_TO_METER 
193                                         * current->range * SG_NM_TO_METER ) ) {
194                                                 stations->push_back(*current);
195                                                 ++found;
196                                         }
197                                 }
198                         }
199                 }
200         }
201         return found;
202 }
203
204
205 // TODO - this function should move somewhere else eventually!
206 // Return an appropriate call-sign for an ATIS transmission.
207 int FGCommList::GetCallSign( string apt_id, int hours, int mins )
208 {
209         atis_transmission_type tran;
210         
211         if(atislog.find(apt_id) == atislog.end()) {
212                 // This station has not transmitted yet - return a random identifier
213                 // and add the transmission to the log
214                 tran.hours = hours;
215                 tran.mins = mins;
216                 sg_srandom_time();
217                 tran.callsign = int(sg_random() * 25) + 1;      // This *should* give a random int between 1 and 26
218                 //atislog[apt_id].push_back(tran);
219                 atislog[apt_id] = tran;
220         } else {
221                 // This station has transmitted - calculate the appropriate identifier
222                 // and add the transmission to the log if it has changed
223                 tran = atislog[apt_id];
224                 // This next bit assumes that no-one comes back to the same ATIS station
225                 // after running FlightGear for more than 24 hours !!
226                 if((tran.hours == hours) && (tran.mins == mins)) {
227                         return(tran.callsign);
228                 } else {
229                         if(tran.hours == hours) {
230                                 // The minutes must have changed
231                                 tran.mins = mins;
232                                 tran.callsign++;
233                         } else {
234                                 if(hours < tran.hours) {
235                                         hours += 24;
236                                 }
237                                 tran.callsign += (hours - tran.hours);
238                                 if(mins != 0) {
239                                         // Assume transmissions were made on every hour
240                                         tran.callsign++;
241                                 }
242                                 tran.hours = hours;
243                                 tran.mins = mins;
244                         }
245                         // Wrap if we've exceeded Zulu
246                         if(tran.callsign > 26) {
247                                 tran.callsign -= 26;
248                         }
249                         // And write the new transmission to the log
250                         atislog[apt_id] = tran;
251                 }
252         }
253         return(tran.callsign);
254 }