]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATC.hxx
0ea9d9c919dbbc7bed13e63482e8d59b29240fcd
[flightgear.git] / src / ATCDCL / ATC.hxx
1 // FGATC - abstract base class for the various actual atc classes 
2 // such as FGATIS, FGTower etc.
3 //
4 // Written by David Luff, started Feburary 2002.
5 //
6 // Copyright (C) 2002  David C. Luff - david.luff@nottingham.ac.uk
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
22 #ifndef _FG_ATC_HXX
23 #define _FG_ATC_HXX
24
25 #include <simgear/constants.h>
26 #include <simgear/compiler.h>
27 #include <simgear/misc/sgstream.hxx>
28 #include <simgear/math/sg_geodesy.hxx>
29 #include <simgear/debug/logstream.hxx>
30
31 #include STL_IOSTREAM
32 #include STL_STRING
33
34 #include "ATCVoice.hxx"
35
36 SG_USING_STD(ostream);
37 SG_USING_STD(string);
38 SG_USING_STD(ios);
39
40 enum plane_type {
41         UNKNOWN,
42         GA_SINGLE,
43         GA_HP_SINGLE,
44         GA_TWIN,
45         GA_JET,
46         MEDIUM,
47         HEAVY,
48         MIL_JET
49 };
50
51 // PlaneRec - a structure holding ATC-centric details of planes under control
52 // This might move or change eventually
53 struct PlaneRec {
54         plane_type type;
55         string callsign;
56         int squawkcode;
57 };
58
59 // Possible types of ATC type that the radios may be tuned to.
60 // INVALID implies not tuned in to anything.
61 enum atc_type {
62         INVALID,
63         ATIS,
64         GROUND,
65         TOWER,
66         APPROACH,
67         DEPARTURE,
68         ENROUTE
69 };
70
71 const int ATC_NUM_TYPES = 7;
72
73 // DCL - new experimental ATC data store
74 struct ATCData {
75         atc_type type;
76         // I've deliberately used float instead of double here to keep the size down - we'll be storing thousands of these in memory.
77         // In fact, we could probably ditch x, y and z and generate on the fly as needed.
78         // On the other hand, we'll probably end up reading this data directly from the DAFIF eventually anyway!!
79         float lon, lat, elev;
80         float x, y, z;
81         //int freq;
82         unsigned short int freq;
83         //int range;
84         unsigned short int range;
85         string ident;
86         string name;
87 };
88
89 // perhaps we could use an FGRunway instead of this.
90 // That wouldn't cache the orthopos though.
91 struct RunwayDetails {
92         Point3D threshold_pos;
93         Point3D end1ortho;      // ortho projection end1 (the threshold ATM)
94         Point3D end2ortho;      // ortho projection end2 (the take off end in the current hardwired scheme)
95         double hdg;             // true runway heading
96         double length;  // In *METERS*
97         double width;   // ditto
98         string rwyID;
99         int patternDirection;   // -1 for left, 1 for right
100 };
101
102 ostream& operator << (ostream& os, atc_type atc);
103
104 class FGATC {
105         
106 public:
107         
108         FGATC();
109         virtual ~FGATC();
110         
111         // Run the internal calculations
112         // Derived classes should call this method from their own Update methods if they 
113         // wish to use the response timer functionality.
114         virtual void Update(double dt);
115         
116         // Recieve a coded callback from the ATC menu system based on the user's selection
117         virtual void ReceiveUserCallback(int code);
118         
119         // Add plane to a stack
120         virtual void AddPlane(const string& pid);
121         
122         // Remove plane from stack
123         virtual int RemovePlane();
124         
125         // Indicate that this instance should output to the display if appropriate 
126         inline void SetDisplay() { _display = true; }
127         
128         // Indicate that this instance should not output to the display
129         inline void SetNoDisplay() { _display = false; }
130         
131         // Generate the text of a message from its parameters and the current context.
132         virtual string GenText(const string& m, int c);
133         
134         // Returns true if OK to transmit on this frequency
135         inline bool GetFreqClear() { return freqClear; }
136         // Indicate that the frequency is in use
137         inline void SetFreqInUse() { freqClear = false; receiving = true; }
138         // Transmission to the ATC is finished and a response is required
139         void SetResponseReqd(const string& rid);
140         // Transmission finished - let ATC decide if a response is reqd and clear freq if necessary
141         void NotifyTransmissionFinished(const string& rid);
142         // Transmission finished and no response required
143         inline void ReleaseFreq() { freqClear = true; receiving = false; }      // TODO - check that the plane releasing the freq is the right one etc.
144         // The above 3 funcs under development!!
145         // The idea is that AI traffic or the user ATC dialog box calls FreqInUse() when they begin transmitting,
146         // and that the tower control sets freqClear back to true following a reply.
147         // AI traffic should check FreqClear() is true prior to transmitting.
148         // The user will just have to wait for a gap in dialog as in real life.
149         
150         // Return the type of ATC station that the class represents
151         inline atc_type GetType() { return _type; }
152         
153         // Set the core ATC data
154         void SetData(ATCData* d);
155         
156         inline double get_lon() const { return lon; }
157         inline void set_lon(const double ln) {lon = ln;}
158         inline double get_lat() const { return lat; }
159         inline void set_lat(const double lt) {lat = lt;}
160         inline double get_elev() const { return elev; }
161         inline void set_elev(const double ev) {elev = ev;}
162         inline double get_x() const { return x; }
163         inline void set_x(const double ecs) {x = ecs;}
164         inline double get_y() const { return y; }
165         inline void set_y(const double why) {y = why;}
166         inline double get_z() const { return z; }
167         inline void set_z(const double zed) {z = zed;}
168         inline int get_freq() const { return freq; }
169         inline void set_freq(const int fq) {freq = fq;}
170         inline int get_range() const { return range; }
171         inline void set_range(const int rg) {range = rg;}
172         inline const string& get_ident() { return ident; }
173         inline void set_ident(const string& id) { ident = id; }
174         inline const string& get_name() { return name; }
175         inline void set_name(const string& nm) { name = nm; }
176         
177 protected:
178         
179         // Render a transmission
180         // Outputs the transmission either on screen or as audio depending on user preference
181         // The refname is a string to identify this sample to the sound manager
182         // The repeating flag indicates whether the message should be repeated continuously or played once.
183         void Render(string& msg, const string& refname = "", bool repeating = false);
184         
185         // Cease rendering all transmission from this station.
186         // Requires the sound manager refname if audio, else "".
187         void NoRender(const string& refname);
188         
189         // Transmit a message when channel becomes free of other dialog
190     void Transmit(int callback_code = 0);
191         
192         // Transmit a message if channel becomes free within timeout (seconds). timeout of zero implies no limit
193         void ConditionalTransmit(double timeout, int callback_code = 0);
194         
195         // Transmit regardless of other dialog on the channel eg emergency
196         void ImmediateTransmit(int callback_code = 0);
197         
198         virtual void ProcessCallback(int code);
199         
200         double lon, lat, elev;
201         double x, y, z;
202         int freq;
203         int range;
204         string ident;           // Code of the airport its at.
205         string name;            // Name transmitted in the broadcast.
206         atc_type _type;
207         
208         // Rendering related stuff
209         bool _voice;                    // Flag - true if we are using voice
210         bool _playing;          // Indicates a message in progress      
211         bool _voiceOK;          // Flag - true if at least one voice has loaded OK
212         FGATCVoice* _vPtr;
213
214         string pending_transmission;    // derived classes set this string before calling Transmit(...) 
215         bool freqClear;         // Flag to indicate if the frequency is clear of ongoing dialog
216         bool receiving;         // Flag to indicate we are receiving a transmission
217         bool responseReqd;      // Flag to indicate we should be responding to a request/report 
218         bool runResponseCounter;        // Flag to indicate the response counter should be run
219         double responseTime;    // Time to take from end of request transmission to beginning of response
220                                                         // The idea is that this will be slightly random.
221         double responseCounter;         // counter to implement the above
222         string responseID;      // ID of the plane to respond to
223         bool respond;   // Flag to indicate now is the time to respond - ie set following the count down of the response timer.
224         // Derived classes only need monitor this flag, and use the response ID, as long as they call FGATC::Update(...)
225         bool _runReleaseCounter;        // A timer for releasing the frequency after giving the message enough time to display
226         double _releaseTime;
227         double _releaseCounter;
228         
229         bool _display;          // Flag to indicate whether we should be outputting to the ATC display.
230         bool _displaying;               // Flag to indicate whether we are outputting to the ATC display.
231         
232 private:
233         // Transmission timing stuff.
234         bool _pending;
235         double _timeout;
236         int _callback_code;     // A callback code to be notified and processed by the derived classes
237                                                 // A value of zero indicates no callback required
238         bool _transmit;         // we are to transmit
239         bool _transmitting;     // we are transmitting
240         double _counter;
241         double _max_count;
242 };
243
244 inline istream&
245 operator >> ( istream& fin, ATCData& a )
246 {
247         double f;
248         char ch;
249         char tp;
250         
251         fin >> tp;
252         
253         switch(tp) {
254         case 'I':
255                 a.type = ATIS;
256                 break;
257         case 'T':
258                 a.type = TOWER;
259                 break;
260         case 'G':
261                 a.type = GROUND;
262                 break;
263         case 'A':
264                 a.type = APPROACH;
265                 break;
266         case '[':
267                 a.type = INVALID;
268                 return fin >> skipeol;
269         default:
270                 SG_LOG(SG_GENERAL, SG_ALERT, "Warning - unknown type \'" << tp << "\' found whilst reading ATC frequency data!\n");
271                 a.type = INVALID;
272                 return fin >> skipeol;
273         }
274         
275         fin >> a.lat >> a.lon >> a.elev >> f >> a.range 
276         >> a.ident;
277         
278         a.name = "";
279         fin >> ch;
280         if(ch != '"') a.name += ch;
281         while(1) {
282                 //in >> noskipws
283                 fin.unsetf(ios::skipws);
284                 fin >> ch;
285                 if((ch == '"') || (ch == 0x0A)) {
286                         break;
287                 }   // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
288                 a.name += ch;
289         }
290         fin.setf(ios::skipws);
291         //cout << "Comm name = " << a.name << '\n';
292         
293         a.freq = (int)(f*100.0 + 0.5);
294         
295         // cout << a.ident << endl;
296         
297         // generate cartesian coordinates
298         Point3D geod( a.lon * SGD_DEGREES_TO_RADIANS, a.lat * SGD_DEGREES_TO_RADIANS, a.elev );
299         Point3D cart = sgGeodToCart( geod );
300         a.x = cart.x();
301         a.y = cart.y();
302         a.z = cart.z();
303         
304         return fin >> skipeol;
305 }
306
307
308 #endif  // _FG_ATC_HXX