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