1 // Implementation of FGATC - ATC subsystem base class.
3 // Written by David Luff, started February 2002.
5 // Copyright (C) 2002 David C Luff - david.luff@nottingham.ac.uk
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.
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.
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.
25 #include <simgear/sound/soundmgr_openal.hxx>
27 #include <Main/globals.hxx>
28 #include <Main/fg_props.hxx>
31 #include "ATCdisplay.hxx"
35 FGVoice *p_Voice = new FGVoice();
42 runResponseCounter = false;
43 _runReleaseCounter = false;
50 // Transmission timing stuff
51 pending_transmission = "";
56 _transmitting = false;
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;
73 //cout << "RESPOND\n";
75 responseCounter += dt;
79 if(_runReleaseCounter) {
80 if(_releaseCounter >= _releaseTime) {
82 _runReleaseCounter = false;
84 _releaseCounter += dt;
88 // Transmission stuff cribbed from AIPlane.cxx
91 //cout << "TUNED STATION FREQ CLEAR\n";
95 _transmitting = false;
97 if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
102 // timed out - don't render.
110 _max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
112 //cout << "Transmission = " << pending_transmission << '\n';
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);
119 // Run the callback regardless of whether on same freq as user or not.
120 //cout << "_callback_code = " << _callback_code << '\n';
122 ProcessCallback(_callback_code);
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;
136 _runReleaseCounter = true;
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!!!");
146 void FGATC::SetResponseReqd(const string& rid) {
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??
152 runResponseCounter = true;
153 responseCounter = 0.0;
154 responseTime = 1.8; // TODO - randomize this slightly.
157 void FGATC::NotifyTransmissionFinished(const string& rid) {
158 //cout << "Transmission finished, callsign = " << rid << '\n';
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??
172 void FGATC::Transmit(int callback_code) {
173 SG_LOG(SG_ATC, SG_INFO, "Transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
175 _callback_code = callback_code;
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);
182 _callback_code = callback_code;
186 void FGATC::ImmediateTransmit(int callback_code) {
187 SG_LOG(SG_ATC, SG_INFO, "Immediate transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
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);
195 ProcessCallback(callback_code);
199 // Derived classes should override this.
200 void FGATC::ProcessCallback(int code) {
203 void FGATC::AddPlane(const string& pid) {
206 int FGATC::RemovePlane() {
210 void FGATC::SetData(ATCData* d) {
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 fgSetString("/sim/messages/atc", msg.c_str());
229 #ifdef ENABLE_AUDIO_SUPPORT
230 _voice = (_voiceOK && fgGetBool("/sim/sound/voice"));
233 unsigned char* buf = _vPtr->WriteMessage((char*)msg.c_str(), len, _voice);
235 SGSoundSample *simple
236 = new SGSoundSample(buf, len, 8000);
237 // TODO - at the moment the volume is always set off comm1
238 // and can't be changed after the transmission has started.
239 simple->set_volume(5.0 * fgGetDouble("/instrumentation/comm[0]/volume"));
240 globals->get_soundmgr()->add(simple, refname);
242 globals->get_soundmgr()->play_looped(refname);
244 p_Voice->send_transcript( msg , refname, 1 );
247 globals->get_soundmgr()->play_once(refname);
249 p_Voice->send_transcript( msg , refname, 0 );
255 #endif // ENABLE_AUDIO_SUPPORT
257 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
258 for(unsigned int i = 0; i < msg.length(); ++i) {
259 if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
264 globals->get_ATC_display()->RegisterRepeatingMessage(msg);
266 p_Voice->send_transcript( msg , refname, 1 );
269 globals->get_ATC_display()->RegisterSingleMessage(msg);
271 p_Voice->send_transcript( msg, refname, 0 );
279 // Cease rendering a transmission.
280 void FGATC::NoRender(const string& refname) {
283 #ifdef ENABLE_AUDIO_SUPPORT
284 globals->get_soundmgr()->stop(refname);
285 globals->get_soundmgr()->remove(refname);
287 p_Voice->send_transcript( "--", refname, 2);
291 globals->get_ATC_display()->CancelRepeatingMessage();
297 // Generate the text of a message from its parameters and the current context.
298 string FGATC::GenText(const string& m, int c) {
302 ostream& operator << (ostream& os, atc_type atc) {
304 case(INVALID): return(os << "INVALID");
305 case(ATIS): return(os << "ATIS");
306 case(GROUND): return(os << "GROUND");
307 case(TOWER): return(os << "TOWER");
308 case(APPROACH): return(os << "APPROACH");
309 case(DEPARTURE): return(os << "DEPARTURE");
310 case(ENROUTE): return(os << "ENROUTE");
312 return(os << "ERROR - Unknown switch in atc_type operator << ");