]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atislist.cxx
* src/ATC/atis.cxx
[flightgear.git] / src / ATC / atislist.cxx
1 // atislist.cxx -- navaids management class
2 //
3 // Written by David Luff, started October 2001.
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
32 #include "atislist.hxx"
33
34
35 FGATISList *current_atislist;
36
37
38 // Constructor
39 FGATISList::FGATISList( void ) {
40 }
41
42
43 // Destructor
44 FGATISList::~FGATISList( void ) {
45 }
46
47
48 // load the navaids and build the map
49 bool FGATISList::init( SGPath path ) {
50
51     atislist.erase( atislist.begin(), atislist.end() );
52
53     sg_gzifstream in( path.str() );
54     if ( !in.is_open() ) {
55         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
56         exit(-1);
57     }
58
59     // read in each line of the file
60
61     // in >> skipeol;
62     in >> skipcomment;
63
64     // double min = 100000;
65     // double max = 0;
66
67 #ifdef __MWERKS__
68     char c = 0;
69     while ( in.get(c) && c != '\0' ) {
70         in.putback(c);
71 #else
72     while ( ! in.eof() ) {
73 #endif
74     
75         FGATIS a;
76         in >> a;
77         if ( a.get_type() == '[' ) {
78             break;
79         }
80         
81         /* cout << "id = " << a.GetIdent() << endl;
82         cout << " type = " << a.get_type() << endl;
83         cout << " lon = " << a.get_lon() << endl;
84         cout << " lat = " << a.get_lat() << endl;
85         cout << " elev = " << a.get_elev() << endl;
86         cout << " freq = " << a.get_freq() << endl;
87         cout << " range = " << a.get_range() << endl << endl; */
88
89         atislist[a.get_freq()].push_back(a);
90         in >> skipcomment;
91
92         /* if ( a.get_type() != 'N' ) {
93             if ( a.get_freq() < min ) {
94                 min = a.get_freq();
95             }
96             if ( a.get_freq() > max ) {
97                 max = a.get_freq();
98             }
99         } */
100     }
101
102     // cout << "min freq = " << min << endl;
103     // cout << "max freq = " << max << endl;
104
105     return true;
106 }
107
108
109 // query the database for the specified frequency, lon and lat are in
110 // degrees, elev is in meters
111 bool FGATISList::query( double lon, double lat, double elev, double freq,
112                        FGATIS *a )
113 {
114     atis_list_type stations = atislist[(int)(freq*100.0 + 0.5)];
115
116     atis_list_iterator current = stations.begin();
117     atis_list_iterator last = stations.end();
118
119     // double az1, az2, s;
120     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
121     Point3D station;
122     double d;
123     for ( ; current != last ; ++current ) {
124         //cout << "testing " << current->get_ident() << endl;
125         station = Point3D(current->get_x(), current->get_y(), current->get_z());
126         //cout << "aircraft = " << aircraft << endl;
127         //cout << "station = " << station << endl;
128
129         d = aircraft.distance3Dsquared( station );
130
131         //cout << "  dist = " << sqrt(d)
132         //     << "  range = " << current->get_range() * SG_NM_TO_METER << endl;
133
134         // match up to twice the published range so we can model
135         // reduced signal strength
136         if ( d < (2 * current->get_range() * SG_NM_TO_METER 
137                   * 2 * current->get_range() * SG_NM_TO_METER ) ) {
138             //cout << "matched = " << current->get_ident() << endl;
139             *a = *current;
140             return true;
141         }
142     }
143
144     return false;
145 }
146
147
148 int FGATISList::GetCallSign( string apt_id, int hours, int mins )
149 {
150     atis_transmission_type tran;
151
152     if(atislog.find(apt_id) == atislog.end()) {
153         // This station has not transmitted yet - return a random identifier
154         // and add the transmission to the log
155         tran.hours = hours;
156         tran.mins = mins;
157         sg_srandom_time();
158         tran.callsign = int(sg_random() * 25) + 1;      // This *should* give a random int between 1 and 26
159         //atislog[apt_id].push_back(tran);
160         atislog[apt_id] = tran;
161     } else {
162         // This station has transmitted - calculate the appropriate identifier
163         // and add the transmission to the log if it has changed
164         tran = atislog[apt_id];
165         // This next bit assumes that no-one comes back to the same ATIS station
166         // after running FlightGear for more than 24 hours !!
167         if((tran.hours == hours) && (tran.mins == mins)) {
168             return(tran.callsign);
169         } else {
170             if(tran.hours == hours) {
171                 // The minutes must have changed
172                 tran.mins = mins;
173                 tran.callsign++;
174             } else {
175                 if(hours < tran.hours) {
176                     hours += 24;
177                 }
178                 tran.callsign += (hours - tran.hours);
179                 if(mins != 0) {
180                     // Assume transmissions were made on every hour
181                     tran.callsign++;
182                 }
183                 tran.hours = hours;
184                 tran.mins = mins;
185             }
186             // Wrap if we've exceeded Zulu
187             if(tran.callsign > 26) {
188                 tran.callsign -= 26;
189             }
190             // And write the new transmission to the log
191             atislog[apt_id] = tran;
192         }
193     }
194     return(tran.callsign);
195 }