]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATC.cxx
Catch sound exceptions at the earliest, report problem has an alert, and continue...
[flightgear.git] / src / ATC / ATC.cxx
1 // Implementation of FGATC - ATC subsystem base class.
2 //
3 // Written by David Luff, started February 2002.
4 //
5 // Copyright (C) 2002  David C Luff - david.luff@nottingham.ac.uk
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <simgear/sound/soundmgr_openal.hxx>
26 #include <simgear/structure/exception.hxx>
27
28 #include <Main/globals.hxx>
29 #include <Main/fg_props.hxx>
30
31 #include "ATC.hxx"
32 #include "ATCdisplay.hxx"
33
34 FGATC::FGATC() {
35         freqClear = true;
36         receiving = false;
37         respond = false;
38         runResponseCounter = false;
39         _runReleaseCounter = false;
40         responseID = "";
41         responseReqd = false;
42         _type = INVALID;
43         _display = false;
44         _displaying = false;
45         
46         // Transmission timing stuff
47         pending_transmission = "";
48         _timeout = 0;
49         _pending = false;
50         _callback_code = 0;
51         _transmit = false;
52         _transmitting = false;
53         _counter = 0.0;
54         _max_count = 5.0;
55         
56         _voiceOK = false;
57 }
58
59 FGATC::~FGATC() {
60 }
61
62 // Derived classes wishing to use the response counter should call this from their own Update(...).
63 void FGATC::Update(double dt) {
64         if(runResponseCounter) {
65                 //cout << responseCounter << '\t' << responseTime << '\n';
66                 if(responseCounter >= responseTime) {
67                         runResponseCounter = false;
68                         respond = true;
69                         //cout << "RESPOND\n";
70                 } else {
71                         responseCounter += dt;
72                 }
73         }
74         
75         if(_runReleaseCounter) {
76                 if(_releaseCounter >= _releaseTime) {
77                         freqClear = true;
78                         _runReleaseCounter = false;
79                 } else {
80                         _releaseCounter += dt;
81                 }
82         }
83         
84         // Transmission stuff cribbed from AIPlane.cxx
85         if(_pending) {
86                 if(GetFreqClear()) {
87                         //cout << "TUNED STATION FREQ CLEAR\n";
88                         SetFreqInUse();
89                         _pending = false;
90                         _transmit = true;
91                         _transmitting = false;
92                 } else {
93                         if(_timeout > 0.0) {    // allows count down to be avoided by initially setting it to zero
94                                 _timeout -= dt;
95                                 if(_timeout <= 0.0) {
96                                         _timeout = 0.0;
97                                         _pending = false;
98                                         // timed out - don't render.
99                                 }
100                         }
101                 }
102         }
103
104         if(_transmit) {
105                 _counter = 0.0;
106                 _max_count = 5.0;               // FIXME - hardwired length of message - need to calculate it!
107                 
108                 //cout << "Transmission = " << pending_transmission << '\n';
109                 if(_display) {
110                         //Render(pending_transmission, ident, false);
111                         Render(pending_transmission);
112                         // At the moment Render only works for ATIS
113                         //globals->get_ATC_display()->RegisterSingleMessage(pending_transmission);
114                 }
115                 // Run the callback regardless of whether on same freq as user or not.
116                 //cout << "_callback_code = " << _callback_code << '\n';
117                 if(_callback_code) {
118                         ProcessCallback(_callback_code);
119                 }
120                 _transmit = false;
121                 _transmitting = true;
122         } else if(_transmitting) {
123                 if(_counter >= _max_count) {
124                         //NoRender(plane.callsign);  commented out since at the moment NoRender is designed just to stop repeating messages,
125                         // and this will be primarily used on single messages.
126                         _transmitting = false;
127                         //if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
128                         // TODO - need to let the plane the transmission is aimed at that it's finished.
129                         // However, for now we'll just release the frequency since if we don't it all goes pear-shaped
130                         _releaseCounter = 0.0;
131                         _releaseTime = 0.9;
132                         _runReleaseCounter = true;
133                 }
134                 _counter += dt;
135         }
136 }
137
138 void FGATC::ReceiveUserCallback(int code) {
139         SG_LOG(SG_ATC, SG_WARN, "WARNING - whichever ATC class was intended to receive callback code " << code << " didn't get it!!!");
140 }
141
142 void FGATC::SetResponseReqd(const string& rid) {
143         receiving = false;
144         responseReqd = true;
145         respond = false;        // TODO - this ignores the fact that more than one plane could call this before response
146                                                 // Shouldn't happen with AI only, but user could confuse things??
147         responseID = rid;
148         runResponseCounter = true;
149         responseCounter = 0.0;
150         responseTime = 1.8;             // TODO - randomize this slightly.
151 }
152
153 void FGATC::NotifyTransmissionFinished(const string& rid) {
154         //cout << "Transmission finished, callsign = " << rid << '\n';
155         receiving = false;
156         responseID = rid;
157         if(responseReqd) {
158                 runResponseCounter = true;
159                 responseCounter = 0.0;
160                 responseTime = 1.2;     // TODO - randomize this slightly, and allow it to be dependent on the transmission and how busy the ATC is.
161                 respond = false;        // TODO - this ignores the fact that more than one plane could call this before response
162                                                         // Shouldn't happen with AI only, but user could confuse things??
163         } else {
164                 freqClear = true;
165         }
166 }
167
168 void FGATC::Transmit(int callback_code) {
169         SG_LOG(SG_ATC, SG_INFO, "Transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
170         _pending = true;
171         _callback_code = callback_code;
172         _timeout = 0.0;
173 }
174
175 void FGATC::ConditionalTransmit(double timeout, int callback_code) {
176         SG_LOG(SG_ATC, SG_INFO, "Timed transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
177         _pending = true;
178         _callback_code = callback_code;
179         _timeout = timeout;
180 }
181
182 void FGATC::ImmediateTransmit(int callback_code) {
183         SG_LOG(SG_ATC, SG_INFO, "Immediate transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
184         if(_display) {
185                 //Render(pending_transmission, ident, false);
186                 Render(pending_transmission);
187                 // At the moment Render doesn't work except for ATIS
188                 //globals->get_ATC_display()->RegisterSingleMessage(pending_transmission);
189         }
190         if(callback_code) {
191                 ProcessCallback(callback_code);
192         }
193 }
194
195 // Derived classes should override this.
196 void FGATC::ProcessCallback(int code) {
197 }
198
199 void FGATC::AddPlane(const string& pid) {
200 }
201
202 int FGATC::RemovePlane() {
203         return 0;
204 }
205
206 void FGATC::SetData(ATCData* d) {
207         lon = d->lon;
208         lat = d->lat;
209         elev = d->elev;
210         x = d->x;
211         y = d->y;
212         z = d->z;
213         range = d->range;
214         ident = d->ident;
215         name = d->name;
216         freq = d->freq;
217 }
218
219 // Render a transmission
220 // Outputs the transmission either on screen or as audio depending on user preference
221 // The refname is a string to identify this sample to the sound manager
222 // The repeating flag indicates whether the message should be repeated continuously or played once.
223 void FGATC::Render(string& msg, const string& refname, bool repeating) {
224         if (repeating)
225                 fgSetString("/sim/messages/atis", msg.c_str());
226         else
227                 fgSetString("/sim/messages/atc", msg.c_str());
228
229         #ifdef ENABLE_AUDIO_SUPPORT
230         _voice = (_voiceOK && fgGetBool("/sim/sound/voice"));
231         if(_voice) {
232                 int len;
233                 unsigned char* buf = _vPtr->WriteMessage((char*)msg.c_str(), len, _voice);
234                 if(_voice) {
235                         try {
236                                 SGSoundSample *simple
237                                                                 = new SGSoundSample(buf, len, 8000);
238                                 // TODO - at the moment the volume is always set off comm1 
239                                 // and can't be changed after the transmission has started.
240                                 simple->set_volume(5.0 * fgGetDouble("/instrumentation/comm[0]/volume"));
241                                 globals->get_soundmgr()->add(simple, refname);
242                                 if(repeating) {
243                                         globals->get_soundmgr()->play_looped(refname);
244                                 } else {
245                                         globals->get_soundmgr()->play_once(refname);
246                                 }
247                         } catch ( sg_io_exception &e ) {
248                                 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
249                         }
250                 }
251                 delete[] buf;
252         }
253         #endif  // ENABLE_AUDIO_SUPPORT
254         if(!_voice) {
255                 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
256                 for(unsigned int i = 0; i < msg.length(); ++i) {
257                         if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
258                                 msg[i] = ' ';
259                         }
260                 }
261                 if(repeating) {
262                         globals->get_ATC_display()->RegisterRepeatingMessage(msg);
263                 } else {
264                         globals->get_ATC_display()->RegisterSingleMessage(msg);
265                 }
266         }
267         _playing = true;        
268 }
269
270
271 // Cease rendering a transmission.
272 void FGATC::NoRender(const string& refname) {
273         if(_playing) {
274                 if(_voice) {
275 #ifdef ENABLE_AUDIO_SUPPORT             
276                         globals->get_soundmgr()->stop(refname);
277                         globals->get_soundmgr()->remove(refname);
278 #endif
279                 } else {
280                         globals->get_ATC_display()->CancelRepeatingMessage();
281                 }
282                 _playing = false;
283         }
284 }
285
286 // Generate the text of a message from its parameters and the current context.
287 string FGATC::GenText(const string& m, int c) {
288         return("");
289 }
290
291 ostream& operator << (ostream& os, atc_type atc) {
292         switch(atc) {
293                 case(INVALID):    return(os << "INVALID");
294                 case(ATIS):       return(os << "ATIS");
295                 case(GROUND):     return(os << "GROUND");
296                 case(TOWER):      return(os << "TOWER");
297                 case(APPROACH):   return(os << "APPROACH");
298                 case(DEPARTURE):  return(os << "DEPARTURE");
299                 case(ENROUTE):    return(os << "ENROUTE");
300         }
301         return(os << "ERROR - Unknown switch in atc_type operator << ");
302 }