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>
37 runResponseCounter = false;
38 _runReleaseCounter = false;
45 // Transmission timing stuff
46 pending_transmission = "";
51 _transmitting = false;
61 // Derived classes wishing to use the response counter should call this from their own Update(...).
62 void FGATC::Update(double dt) {
63 if(runResponseCounter) {
64 //cout << responseCounter << '\t' << responseTime << '\n';
65 if(responseCounter >= responseTime) {
66 runResponseCounter = false;
68 //cout << "RESPOND\n";
70 responseCounter += dt;
74 if(_runReleaseCounter) {
75 if(_releaseCounter >= _releaseTime) {
77 _runReleaseCounter = false;
79 _releaseCounter += dt;
83 // Transmission stuff cribbed from AIPlane.cxx
86 //cout << "TUNED STATION FREQ CLEAR\n";
90 _transmitting = false;
92 if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
97 // timed out - don't render.
105 _max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
107 //cout << "Transmission = " << pending_transmission << '\n';
109 //Render(pending_transmission, ident, false);
110 Render(pending_transmission);
112 // Run the callback regardless of whether on same freq as user or not.
113 //cout << "_callback_code = " << _callback_code << '\n';
115 ProcessCallback(_callback_code);
118 _transmitting = true;
119 } else if(_transmitting) {
120 if(_counter >= _max_count) {
121 //NoRender(plane.callsign); commented out since at the moment NoRender is designed just to stop repeating messages,
122 // and this will be primarily used on single messages.
123 _transmitting = false;
124 //if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
125 // TODO - need to let the plane the transmission is aimed at that it's finished.
126 // However, for now we'll just release the frequency since if we don't it all goes pear-shaped
127 _releaseCounter = 0.0;
129 _runReleaseCounter = true;
135 void FGATC::ReceiveUserCallback(int code) {
136 SG_LOG(SG_ATC, SG_WARN, "WARNING - whichever ATC class was intended to receive callback code " << code << " didn't get it!!!");
139 void FGATC::SetResponseReqd(const string& rid) {
142 respond = false; // TODO - this ignores the fact that more than one plane could call this before response
143 // Shouldn't happen with AI only, but user could confuse things??
145 runResponseCounter = true;
146 responseCounter = 0.0;
147 responseTime = 1.8; // TODO - randomize this slightly.
150 void FGATC::NotifyTransmissionFinished(const string& rid) {
151 //cout << "Transmission finished, callsign = " << rid << '\n';
155 runResponseCounter = true;
156 responseCounter = 0.0;
157 responseTime = 1.2; // TODO - randomize this slightly, and allow it to be dependent on the transmission and how busy the ATC is.
158 respond = false; // TODO - this ignores the fact that more than one plane could call this before response
159 // Shouldn't happen with AI only, but user could confuse things??
165 void FGATC::Transmit(int callback_code) {
166 SG_LOG(SG_ATC, SG_INFO, "Transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
168 _callback_code = callback_code;
172 void FGATC::ConditionalTransmit(double timeout, int callback_code) {
173 SG_LOG(SG_ATC, SG_INFO, "Timed transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
175 _callback_code = callback_code;
179 void FGATC::ImmediateTransmit(int callback_code) {
180 SG_LOG(SG_ATC, SG_INFO, "Immediate transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
182 //Render(pending_transmission, ident, false);
183 Render(pending_transmission);
184 // At the moment Render doesn't work except for ATIS
187 ProcessCallback(callback_code);
191 // Derived classes should override this.
192 void FGATC::ProcessCallback(int code) {
195 void FGATC::AddPlane(const string& pid) {
198 int FGATC::RemovePlane() {
202 void FGATC::SetData(ATCData* d) {
215 // Render a transmission
216 // Outputs the transmission either on screen or as audio depending on user preference
217 // The refname is a string to identify this sample to the sound manager
218 // The repeating flag indicates whether the message should be repeated continuously or played once.
219 void FGATC::Render(string& msg, const string& refname, bool repeating) {
221 fgSetString("/sim/messages/atis", msg.c_str());
223 fgSetString("/sim/messages/atc", msg.c_str());
225 #ifdef ENABLE_AUDIO_SUPPORT
226 _voice = (_voiceOK && fgGetBool("/sim/sound/voice"));
229 unsigned char* buf = _vPtr->WriteMessage((char*)msg.c_str(), len, _voice);
232 SGSoundSample *simple
233 = new SGSoundSample(buf, len, 8000);
234 // TODO - at the moment the volume is always set off comm1
235 // and can't be changed after the transmission has started.
236 simple->set_volume(5.0 * fgGetDouble("/instrumentation/comm[0]/volume"));
237 globals->get_soundmgr()->add(simple, refname);
239 globals->get_soundmgr()->play_looped(refname);
241 globals->get_soundmgr()->play_once(refname);
243 } catch ( sg_io_exception &e ) {
244 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
249 #endif // ENABLE_AUDIO_SUPPORT
251 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
252 for(unsigned int i = 0; i < msg.length(); ++i) {
253 if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
262 // Cease rendering a transmission.
263 void FGATC::NoRender(const string& refname) {
266 #ifdef ENABLE_AUDIO_SUPPORT
267 globals->get_soundmgr()->stop(refname);
268 globals->get_soundmgr()->remove(refname);
275 // Generate the text of a message from its parameters and the current context.
276 string FGATC::GenText(const string& m, int c) {
280 ostream& operator << (ostream& os, atc_type atc) {
282 case(INVALID): return(os << "INVALID");
283 case(ATIS): return(os << "ATIS");
284 case(GROUND): return(os << "GROUND");
285 case(TOWER): return(os << "TOWER");
286 case(APPROACH): return(os << "APPROACH");
287 case(DEPARTURE): return(os << "DEPARTURE");
288 case(ENROUTE): return(os << "ENROUTE");
290 return(os << "ERROR - Unknown switch in atc_type operator << ");