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