]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATC.hxx
Initial commit of the new sound system, expect more updates to follow
[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 <iosfwd>
32 #include <string>
33
34 #include "ATCVoice.hxx"
35
36 class SGSampleGroup;
37
38 // Convert a frequency in MHz to tens of kHz
39 // so we can use it e.g. as an index into commlist_freq
40 //
41 // If freq > 1000 assume it's already in tens of KHz;
42 // otherwise assume MHz.
43 //
44 // Note:  122.375 must be rounded DOWN to 12237 
45 // in order to be consistent with apt.dat et cetera.
46 inline int kHz10(double freq)
47 {
48  if (freq > 1000.) return int(freq);
49  return int(freq*100.0 + 0.25);
50 }
51  
52 enum plane_type {
53         UNKNOWN,
54         GA_SINGLE,
55         GA_HP_SINGLE,
56         GA_TWIN,
57         GA_JET,
58         MEDIUM,
59         HEAVY,
60         MIL_JET
61 };
62
63 // PlaneRec - a structure holding ATC-centric details of planes under control
64 // This might move or change eventually
65 struct PlaneRec {
66         plane_type type;
67         std::string callsign;
68         int squawkcode;
69 };
70
71 // Possible types of ATC type that the radios may be tuned to.
72 // INVALID implies not tuned in to anything.
73 enum atc_type {
74         AWOS,
75         ATIS,
76         GROUND,
77         TOWER,
78         APPROACH,
79         DEPARTURE,
80         ENROUTE,
81   INVALID        /* must be last element;  see ATC_NUM_TYPES */
82 };
83
84 const int ATC_NUM_TYPES = 1 + INVALID;
85
86 // DCL - new experimental ATC data store
87 struct ATCData {
88         atc_type type;
89         SGGeod geod;
90         SGVec3d cart;
91         unsigned short int freq;
92         unsigned short int range;
93         std::string ident;
94         std::string name;
95 };
96
97 // perhaps we could use an FGRunway instead of this.
98 // That wouldn't cache the orthopos though.
99 struct RunwayDetails {
100         SGGeod threshold_pos;
101         SGVec3d end1ortho;      // ortho projection end1 (the threshold ATM)
102         SGVec3d end2ortho;      // ortho projection end2 (the take off end in the current hardwired scheme)
103         double hdg;             // true runway heading
104         double length;  // In *METERS*
105         double width;   // ditto
106         std::string rwyID;
107         int patternDirection;   // -1 for left, 1 for right
108 };
109
110 std::ostream& operator << (std::ostream& os, atc_type atc);
111
112 class FGATC {
113   friend class FGATCMgr;
114 public:
115         
116         FGATC();
117         virtual ~FGATC();
118         
119         virtual void Init()=0;
120   
121         // Run the internal calculations
122         // Derived classes should call this method from their own Update methods if they 
123         // wish to use the response timer functionality.
124         virtual void Update(double dt);
125         
126         // Recieve a coded callback from the ATC menu system based on the user's selection
127         virtual void ReceiveUserCallback(int code);
128         
129         // Add plane to a stack
130         virtual void AddPlane(const std::string& pid);
131         
132         // Remove plane from stack
133         virtual int RemovePlane();
134         
135         // Indicate that this instance should output to the display if appropriate 
136         inline void SetDisplay() { _display = true; }
137         
138         // Indicate that this instance should not output to the display
139         inline void SetNoDisplay() { _display = false; }
140         
141         // Generate the text of a message from its parameters and the current context.
142         virtual std::string GenText(const std::string& m, int c);
143         
144         // Returns true if OK to transmit on this frequency
145         inline bool GetFreqClear() { return freqClear; }
146         // Indicate that the frequency is in use
147         inline void SetFreqInUse() { freqClear = false; receiving = true; }
148         // Transmission to the ATC is finished and a response is required
149         void SetResponseReqd(const std::string& rid);
150         // Transmission finished - let ATC decide if a response is reqd and clear freq if necessary
151         void NotifyTransmissionFinished(const std::string& rid);
152         // Transmission finished and no response required
153         inline void ReleaseFreq() { freqClear = true; receiving = false; }      // TODO - check that the plane releasing the freq is the right one etc.
154         // The above 3 funcs under development!!
155         // The idea is that AI traffic or the user ATC dialog box calls FreqInUse() when they begin transmitting,
156         // and that the tower control sets freqClear back to true following a reply.
157         // AI traffic should check FreqClear() is true prior to transmitting.
158         // The user will just have to wait for a gap in dialog as in real life.
159         
160         // Return the type of ATC station that the class represents
161         inline atc_type GetType() { return _type; }
162         
163         // Set the core ATC data
164         void SetData(ATCData* d);
165
166         inline int get_freq() const { return freq; }
167         inline void set_freq(const int fq) {freq = fq;}
168         inline int get_range() const { return range; }
169         inline void set_range(const int rg) {range = rg;}
170         inline const std::string& get_ident() { return ident; }
171         inline void set_ident(const std::string& id) { ident = id; }
172         inline const std::string& get_name() { return name; }
173         inline void set_name(const std::string& nm) { name = nm; }
174         
175 protected:
176         
177         // Render a transmission
178         // Outputs the transmission either on screen or as audio depending on user preference
179         // The refname is a string to identify this sample to the sound manager
180         // The repeating flag indicates whether the message should be repeated continuously or played once.
181         void Render(std::string& msg, const float volume = 1.0, 
182         const std::string& refname = "", bool repeating = false);
183         
184         // Cease rendering all transmission from this station.
185         // Requires the sound manager refname if audio, else "".
186         void NoRender(const std::string& refname);
187         
188         // Transmit a message when channel becomes free of other dialog
189         void Transmit(int callback_code = 0);
190         
191         // Transmit a message if channel becomes free within timeout (seconds). timeout of zero implies no limit
192         void ConditionalTransmit(double timeout, int callback_code = 0);
193         
194         // Transmit regardless of other dialog on the channel eg emergency
195         void ImmediateTransmit(int callback_code = 0);
196         
197         virtual void ProcessCallback(int code);
198         
199         SGGeod _geod;
200         SGVec3d _cart;
201         int freq;
202         std::map<std::string,int> active_on;
203   
204         int range;
205         std::string ident;      // Code of the airport its at.
206         std::string name;       // Name transmitted in the broadcast.
207
208         
209         // Rendering related stuff
210         bool _voice;    // Flag - true if we are using voice
211         bool _playing;  // Indicates a message in progress      
212         bool _voiceOK;  // Flag - true if at least one voice has loaded OK
213         FGATCVoice* _vPtr;
214
215         SGSampleGroup *_sgr; // default sample group;
216
217         
218         bool freqClear; // Flag to indicate if the frequency is clear of ongoing dialog
219         bool receiving; // Flag to indicate we are receiving a transmission
220         
221         
222         double responseTime; // Time to take from end of request transmission to beginning of response
223                                                  // The idea is that this will be slightly random.
224         
225         bool respond;   // Flag to indicate now is the time to respond - ie set following the count down of the response timer.
226         std::string responseID; // ID of the plane to respond to
227         bool runResponseCounter;        // Flag to indicate the response counter should be run
228         double responseCounter; // counter to implement the above
229         // Derived classes only need monitor this flag, and use the response ID, as long as they call FGATC::Update(...)
230         bool _runReleaseCounter;        // A timer for releasing the frequency after giving the message enough time to display
231         bool responseReqd;      // Flag to indicate we should be responding to a request/report 
232         double _releaseTime;
233         double _releaseCounter;
234   atc_type _type;
235         bool _display;  // Flag to indicate whether we should be outputting to the ATC display.
236         std::string pending_transmission; // derived classes set this string before calling Transmit(...)       
237         
238 private:
239         // Transmission timing stuff.
240         double _timeout;
241         bool _pending;
242         
243         int _callback_code;     // A callback code to be notified and processed by the derived classes
244                                                 // A value of zero indicates no callback required
245         bool _transmit;         // we are to transmit
246         bool _transmitting;     // we are transmitting
247         double _counter;
248         double _max_count;
249 };
250
251 std::istream& operator>> ( std::istream& fin, ATCData& a );
252
253 #endif  // _FG_ATC_HXX