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