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),
51 runResponseCounter(false),
52 _runReleaseCounter(false),
54 // Transmission timing stuff
55 pending_transmission(""),
64 SGSoundMgr *smgr = globals->get_soundmgr();
65 _sgr = smgr->find("atc", true);
66 _sgr->tie_to_listener();
68 _masterVolume = fgGetNode("/sim/sound/atc/volume", true);
69 _enabled = fgGetNode("/sim/sound/atc/enabled", true);
70 _atc_external = fgGetNode("/sim/sound/atc/external-view", true);
71 _internal = fgGetNode("/sim/current-view/internal", true);
78 // Derived classes wishing to use the response counter should
79 // call this from their own Update(...).
80 void FGATC::Update(double dt) {
82 #ifdef ENABLE_AUDIO_SUPPORT
83 bool active = _atc_external->getBoolValue() ||
84 _internal->getBoolValue();
86 if ( active && _enabled->getBoolValue() ) {
87 _sgr->set_volume( _masterVolume->getFloatValue() );
88 _sgr->resume(); // no-op if already in resumed state
96 void FGATC::SetStation(flightgear::CommStation* sta) {
97 if (_currentStation == sta)
99 _currentStation = sta;
103 switch (sta->type()) {
104 case FGPositioned::FREQ_ATIS: _type = ATIS; break;
105 case FGPositioned::FREQ_AWOS: _type = AWOS; break;
120 Update(0); // one last update
127 range = sta->rangeNm();
128 ident = sta->airport()->ident();
129 name = sta->airport()->name();
130 freq = sta->freqKHz();
135 // Render a transmission
136 // Outputs the transmission either on screen or as audio depending on user preference
137 // The refname is a string to identify this sample to the sound manager
138 // The repeating flag indicates whether the message should be repeated continuously or played once.
139 void FGATC::Render(string& msg, const float volume,
140 const string& refname, const bool repeating) {
141 if ((!_display) ||(volume < 0.05))
148 fgSetString("/sim/messages/atis", msg.c_str());
150 fgSetString("/sim/messages/atc", msg.c_str());
152 #ifdef ENABLE_AUDIO_SUPPORT
153 bool useVoice = _voice && fgGetBool("/sim/sound/voice") && fgGetBool("/sim/sound/atc/enabled");
154 SGSoundSample *simple = _sgr->find(refname);
156 if (simple && (_currentMsg == msg))
158 simple->set_volume(volume);
166 _vPtr = GetVoicePointer();
168 buf = _vPtr->WriteMessage((char*)msg.c_str(), &len);
172 // >>> Beware: must pass a (new) object to the (add) method,
173 // >>> because the (remove) method is going to do a (delete)
174 // >>> whether that's what you want or not.
175 simple = new SGSoundSample(&buf, len, 8000);
176 simple->set_volume(volume);
177 _sgr->add(simple, refname);
178 _sgr->play(refname, repeating);
179 } catch ( sg_io_exception &e ) {
180 SG_LOG(SG_ATC, SG_ALERT, e.getFormattedMessage());
190 #endif // ENABLE_AUDIO_SUPPORT
194 // first rip the underscores and the pause hints out of the string - these are for the convenience of the voice parser
195 for(unsigned int i = 0; i < msg.length(); ++i) {
196 if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
205 // Cease rendering a transmission.
206 void FGATC::NoRender(const string& refname) {
209 #ifdef ENABLE_AUDIO_SUPPORT
211 _sgr->remove(refname);
219 // Derived classes wishing to use the response counter should
220 // call this from their own Update(...).
221 void FGATC::Update(double dt) {
223 #ifdef ENABLE_AUDIO_SUPPORT
224 bool active = _atc_external->getBoolValue() ||
225 _internal->getBoolValue();
227 if ( active && _enabled->getBoolValue() ) {
228 _sgr->set_volume( _masterVolume->getFloatValue() );
229 _sgr->resume(); // no-op if already in resumed state
235 if(runResponseCounter) {
236 //cout << responseCounter << '\t' << responseTime << '\n';
237 if(responseCounter >= responseTime) {
238 runResponseCounter = false;
240 //cout << "RESPOND\n";
242 responseCounter += dt;
246 if(_runReleaseCounter) {
247 if(_releaseCounter >= _releaseTime) {
249 _runReleaseCounter = false;
251 _releaseCounter += dt;
255 // Transmission stuff cribbed from AIPlane.cxx
258 //cout << "TUNED STATION FREQ CLEAR\n";
262 _transmitting = false;
264 if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
266 if(_timeout <= 0.0) {
269 // timed out - don't render.
277 _max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
279 //cout << "Transmission = " << pending_transmission << '\n';
281 //Render(pending_transmission, ident, false);
282 Render(pending_transmission);
285 _transmitting = true;
286 } else if(_transmitting) {
287 if(_counter >= _max_count) {
288 //NoRender(plane.callsign); commented out since at the moment NoRender is designed just to stop repeating messages,
289 // and this will be primarily used on single messages.
290 _transmitting = false;
291 //if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
292 // TODO - need to let the plane the transmission is aimed at that it's finished.
293 // However, for now we'll just release the frequency since if we don't it all goes pear-shaped
294 _releaseCounter = 0.0;
296 _runReleaseCounter = true;
302 void FGATC::ReceiveUserCallback(int code) {
303 SG_LOG(SG_ATC, SG_WARN, "WARNING - whichever ATC class was intended to receive callback code " << code << " didn't get it!!!");
306 void FGATC::SetResponseReqd(const string& rid) {
309 respond = false; // TODO - this ignores the fact that more than one plane could call this before response
310 // Shouldn't happen with AI only, but user could confuse things??
312 runResponseCounter = true;
313 responseCounter = 0.0;
314 responseTime = 1.8; // TODO - randomize this slightly.
317 void FGATC::NotifyTransmissionFinished(const string& rid) {
318 //cout << "Transmission finished, callsign = " << rid << '\n';
322 runResponseCounter = true;
323 responseCounter = 0.0;
324 responseTime = 1.2; // TODO - randomize this slightly, and allow it to be dependent on the transmission and how busy the ATC is.
325 respond = false; // TODO - this ignores the fact that more than one plane could call this before response
326 // Shouldn't happen with AI only, but user could confuse things??
332 // Generate the text of a message from its parameters and the current context.
333 string FGATC::GenText(const string& m, int c) {
337 ostream& operator << (ostream& os, atc_type atc) {
339 case(AWOS): return(os << "AWOS");
340 case(ATIS): return(os << "ATIS");
341 case(GROUND): return(os << "GROUND");
342 case(TOWER): return(os << "TOWER");
343 case(APPROACH): return(os << "APPROACH");
344 case(DEPARTURE): return(os << "DEPARTURE");
345 case(ENROUTE): return(os << "ENROUTE");
346 case(INVALID): return(os << "INVALID");
348 return(os << "ERROR - Unknown switch in atc_type operator << ");
351 std::istream& operator >> ( std::istream& fin, ATCData& a )
374 return fin >> skipeol;
376 SG_LOG(SG_ATC, SG_ALERT, "Warning - unknown type \'" << tp << "\' found whilst reading ATC frequency data!\n");
378 return fin >> skipeol;
381 double lat, lon, elev;
383 fin >> lat >> lon >> elev >> f >> a.range >> a.ident;
384 a.geod = SGGeod::fromDegM(lon, lat, elev);
387 if(ch != '"') a.name += ch;
390 fin.unsetf(std::ios::skipws);
392 if((ch == '"') || (ch == 0x0A)) {
394 } // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
397 fin.setf(std::ios::skipws);
398 //cout << "Comm name = " << a.name << '\n';
400 a.freq = (int)(f*100.0 + 0.5);
402 // cout << a.ident << endl;
404 // generate cartesian coordinates
405 a.cart = SGVec3d::fromGeod(a.geod);
406 return fin >> skipeol;