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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <simgear/sound/soundmgr_openal.hxx>
26 #include <simgear/structure/exception.hxx>
28 #include <Main/globals.hxx>
29 #include <Main/fg_props.hxx>
32 #include "ATCdisplay.hxx"
38 runResponseCounter = false;
39 _runReleaseCounter = false;
46 // Transmission timing stuff
47 pending_transmission = "";
52 _transmitting = false;
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;
69 //cout << "RESPOND\n";
71 responseCounter += dt;
75 if(_runReleaseCounter) {
76 if(_releaseCounter >= _releaseTime) {
78 _runReleaseCounter = false;
80 _releaseCounter += dt;
84 // Transmission stuff cribbed from AIPlane.cxx
87 //cout << "TUNED STATION FREQ CLEAR\n";
91 _transmitting = false;
93 if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
98 // timed out - don't render.
106 _max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
108 //cout << "Transmission = " << pending_transmission << '\n';
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);
115 // Run the callback regardless of whether on same freq as user or not.
116 //cout << "_callback_code = " << _callback_code << '\n';
118 ProcessCallback(_callback_code);
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;
132 _runReleaseCounter = true;
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!!!");
142 void FGATC::SetResponseReqd(const string& rid) {
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??
148 runResponseCounter = true;
149 responseCounter = 0.0;
150 responseTime = 1.8; // TODO - randomize this slightly.
153 void FGATC::NotifyTransmissionFinished(const string& rid) {
154 //cout << "Transmission finished, callsign = " << rid << '\n';
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??
168 void FGATC::Transmit(int callback_code) {
169 SG_LOG(SG_ATC, SG_INFO, "Transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
171 _callback_code = callback_code;
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);
178 _callback_code = callback_code;
182 void FGATC::ImmediateTransmit(int callback_code) {
183 SG_LOG(SG_ATC, SG_INFO, "Immediate transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
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);
191 ProcessCallback(callback_code);
195 // Derived classes should override this.
196 void FGATC::ProcessCallback(int code) {
199 void FGATC::AddPlane(const string& pid) {
202 int FGATC::RemovePlane() {
206 void FGATC::SetData(ATCData* d) {
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) {
225 fgSetString("/sim/messages/atis", msg.c_str());
227 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);
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);
243 globals->get_soundmgr()->play_looped(refname);
245 globals->get_soundmgr()->play_once(refname);
247 } catch ( sg_io_exception &e ) {
248 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
253 #endif // ENABLE_AUDIO_SUPPORT
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) == "/")) {
262 globals->get_ATC_display()->RegisterRepeatingMessage(msg);
264 globals->get_ATC_display()->RegisterSingleMessage(msg);
271 // Cease rendering a transmission.
272 void FGATC::NoRender(const string& refname) {
275 #ifdef ENABLE_AUDIO_SUPPORT
276 globals->get_soundmgr()->stop(refname);
277 globals->get_soundmgr()->remove(refname);
280 globals->get_ATC_display()->CancelRepeatingMessage();
286 // Generate the text of a message from its parameters and the current context.
287 string FGATC::GenText(const string& m, int c) {
291 ostream& operator << (ostream& os, atc_type 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");
301 return(os << "ERROR - Unknown switch in atc_type operator << ");