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.
29 #include <simgear/sound/soundmgr_openal.hxx>
30 #include <simgear/structure/exception.hxx>
32 #include <Main/globals.hxx>
33 #include <Main/fg_props.hxx>
34 #include <ATC/CommStation.hxx>
35 #include <Airports/simple.hxx>
39 _currentStation(NULL),
52 runResponseCounter(false),
53 _runReleaseCounter(false),
55 // Transmission timing stuff
56 pending_transmission(""),
65 SGSoundMgr *smgr = globals->get_soundmgr();
66 _sgr = smgr->find("atc", true);
67 _sgr->tie_to_listener();
69 _masterVolume = fgGetNode("/sim/sound/atc/volume", true);
70 _enabled = fgGetNode("/sim/sound/atc/enabled", true);
71 _atc_external = fgGetNode("/sim/sound/atc/external-view", true);
72 _internal = fgGetNode("/sim/current-view/internal", true);
79 // Derived classes wishing to use the response counter should
80 // call this from their own Update(...).
81 void FGATC::update(double dt) {
83 #ifdef ENABLE_AUDIO_SUPPORT
84 bool active = _atc_external->getBoolValue() ||
85 _internal->getBoolValue();
87 if ( active && _enabled->getBoolValue() ) {
88 _sgr->set_volume( _masterVolume->getFloatValue() );
89 _sgr->resume(); // no-op if already in resumed state
97 void FGATC::SetStation(flightgear::CommStation* sta) {
98 if (_currentStation == sta)
100 _currentStation = sta;
104 switch (sta->type()) {
105 case FGPositioned::FREQ_ATIS: _type = ATIS; break;
106 case FGPositioned::FREQ_AWOS: _type = AWOS; break;
121 update(0); // one last update
128 range = sta->rangeNm();
129 ident = sta->airport()->ident();
130 name = sta->airport()->name();
131 freq = sta->freqKHz();
136 // Render a transmission
137 // Outputs the transmission either on screen or as audio depending on user preference
138 // The refname is a string to identify this sample to the sound manager
139 // The repeating flag indicates whether the message should be repeated continuously or played once.
140 void FGATC::Render(string& msg, const float volume,
141 const string& refname, const bool repeating) {
142 if ((!_display) ||(volume < 0.05))
149 fgSetString("/sim/messages/atis", msg.c_str());
151 fgSetString("/sim/messages/atc", msg.c_str());
153 #ifdef ENABLE_AUDIO_SUPPORT
154 bool useVoice = _voice && fgGetBool("/sim/sound/voice") && fgGetBool("/sim/sound/atc/enabled");
155 SGSoundSample *simple = _sgr->find(refname);
157 if (simple && (_currentMsg == msg))
159 simple->set_volume(volume);
167 _vPtr = GetVoicePointer();
169 buf = _vPtr->WriteMessage((char*)msg.c_str(), &len);
173 // >>> Beware: must pass a (new) object to the (add) method,
174 // >>> because the (remove) method is going to do a (delete)
175 // >>> whether that's what you want or not.
176 simple = new SGSoundSample(&buf, len, 8000);
177 simple->set_volume(volume);
178 _sgr->add(simple, refname);
179 _sgr->play(refname, repeating);
180 } catch ( sg_io_exception &e ) {
181 SG_LOG(SG_ATC, SG_ALERT, e.getFormattedMessage());
191 #endif // ENABLE_AUDIO_SUPPORT
195 // first rip the underscores and the pause hints out of the string - these are for the convenience of the voice parser
196 for(unsigned int i = 0; i < msg.length(); ++i) {
197 if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
206 // Cease rendering a transmission.
207 void FGATC::NoRender(const string& refname) {
210 #ifdef ENABLE_AUDIO_SUPPORT
212 _sgr->remove(refname);
220 // Derived classes wishing to use the response counter should
221 // call this from their own Update(...).
222 void FGATC::Update(double dt) {
224 #ifdef ENABLE_AUDIO_SUPPORT
225 bool active = _atc_external->getBoolValue() ||
226 _internal->getBoolValue();
228 if ( active && _enabled->getBoolValue() ) {
229 _sgr->set_volume( _masterVolume->getFloatValue() );
230 _sgr->resume(); // no-op if already in resumed state
236 if(runResponseCounter) {
237 //cout << responseCounter << '\t' << responseTime << '\n';
238 if(responseCounter >= responseTime) {
239 runResponseCounter = false;
241 //cout << "RESPOND\n";
243 responseCounter += dt;
247 if(_runReleaseCounter) {
248 if(_releaseCounter >= _releaseTime) {
250 _runReleaseCounter = false;
252 _releaseCounter += dt;
256 // Transmission stuff cribbed from AIPlane.cxx
259 //cout << "TUNED STATION FREQ CLEAR\n";
263 _transmitting = false;
265 if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
267 if(_timeout <= 0.0) {
270 // timed out - don't render.
278 _max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
280 //cout << "Transmission = " << pending_transmission << '\n';
282 //Render(pending_transmission, ident, false);
283 Render(pending_transmission);
286 _transmitting = true;
287 } else if(_transmitting) {
288 if(_counter >= _max_count) {
289 //NoRender(plane.callsign); commented out since at the moment NoRender is designed just to stop repeating messages,
290 // and this will be primarily used on single messages.
291 _transmitting = false;
292 //if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
293 // TODO - need to let the plane the transmission is aimed at that it's finished.
294 // However, for now we'll just release the frequency since if we don't it all goes pear-shaped
295 _releaseCounter = 0.0;
297 _runReleaseCounter = true;
303 void FGATC::ReceiveUserCallback(int code) {
304 SG_LOG(SG_ATC, SG_WARN, "WARNING - whichever ATC class was intended to receive callback code " << code << " didn't get it!!!");
307 void FGATC::SetResponseReqd(const string& rid) {
310 respond = false; // TODO - this ignores the fact that more than one plane could call this before response
311 // Shouldn't happen with AI only, but user could confuse things??
313 runResponseCounter = true;
314 responseCounter = 0.0;
315 responseTime = 1.8; // TODO - randomize this slightly.
318 void FGATC::NotifyTransmissionFinished(const string& rid) {
319 //cout << "Transmission finished, callsign = " << rid << '\n';
323 runResponseCounter = true;
324 responseCounter = 0.0;
325 responseTime = 1.2; // TODO - randomize this slightly, and allow it to be dependent on the transmission and how busy the ATC is.
326 respond = false; // TODO - this ignores the fact that more than one plane could call this before response
327 // Shouldn't happen with AI only, but user could confuse things??
333 // Generate the text of a message from its parameters and the current context.
334 string FGATC::GenText(const string& m, int c) {
338 ostream& operator << (ostream& os, atc_type atc) {
340 case(AWOS): return(os << "AWOS");
341 case(ATIS): return(os << "ATIS");
342 case(GROUND): return(os << "GROUND");
343 case(TOWER): return(os << "TOWER");
344 case(APPROACH): return(os << "APPROACH");
345 case(DEPARTURE): return(os << "DEPARTURE");
346 case(ENROUTE): return(os << "ENROUTE");
347 case(INVALID): return(os << "INVALID");
349 return(os << "ERROR - Unknown switch in atc_type operator << ");
352 std::istream& operator >> ( std::istream& fin, ATCData& a )
375 return fin >> skipeol;
377 SG_LOG(SG_ATC, SG_ALERT, "Warning - unknown type \'" << tp << "\' found whilst reading ATC frequency data!\n");
379 return fin >> skipeol;
382 double lat, lon, elev;
384 fin >> lat >> lon >> elev >> f >> a.range >> a.ident;
385 a.geod = SGGeod::fromDegM(lon, lat, elev);
388 if(ch != '"') a.name += ch;
391 fin.unsetf(std::ios::skipws);
393 if((ch == '"') || (ch == 0x0A)) {
395 } // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
398 fin.setf(std::ios::skipws);
399 //cout << "Comm name = " << a.name << '\n';
401 a.freq = (int)(f*100.0 + 0.5);
403 // cout << a.ident << endl;
405 // generate cartesian coordinates
406 a.cart = SGVec3d::fromGeod(a.geod);
407 return fin >> skipeol;