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>
43 runResponseCounter(false),
44 _runReleaseCounter(false),
48 // Transmission timing stuff
49 pending_transmission(""),
63 // Derived classes wishing to use the response counter should
64 // call this from their own Update(...).
65 void FGATC::Update(double dt) {
66 if(runResponseCounter) {
67 //cout << responseCounter << '\t' << responseTime << '\n';
68 if(responseCounter >= responseTime) {
69 runResponseCounter = false;
71 //cout << "RESPOND\n";
73 responseCounter += dt;
77 if(_runReleaseCounter) {
78 if(_releaseCounter >= _releaseTime) {
80 _runReleaseCounter = false;
82 _releaseCounter += dt;
86 // Transmission stuff cribbed from AIPlane.cxx
89 //cout << "TUNED STATION FREQ CLEAR\n";
93 _transmitting = false;
95 if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
100 // timed out - don't render.
108 _max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
110 //cout << "Transmission = " << pending_transmission << '\n';
112 //Render(pending_transmission, ident, false);
113 Render(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
190 ProcessCallback(callback_code);
194 // Derived classes should override this.
195 void FGATC::ProcessCallback(int code) {
198 void FGATC::AddPlane(const string& pid) {
201 int FGATC::RemovePlane() {
205 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 double volume,
220 const string& refname, const bool repeating) {
222 fgSetString("/sim/messages/atis", msg.c_str());
224 fgSetString("/sim/messages/atc", msg.c_str());
226 #ifdef ENABLE_AUDIO_SUPPORT
227 _voice = (_voiceOK && fgGetBool("/sim/sound/voice"));
229 string buf = _vPtr->WriteMessage((char*)msg.c_str(), _voice);
233 // >>> Beware: must pass a (new) object to the (add) method,
234 // >>> because the (remove) method is going to do a (delete)
235 // >>> whether that's what you want or not.
236 SGSoundSample *simple =
237 new SGSoundSample((unsigned char*) buf.c_str(),
238 buf.length(), 8000, AL_FORMAT_MONO8);
239 // TODO - at the moment the volume can't be changed
240 // after the transmission has started.
241 simple->set_volume(volume);
242 globals->get_soundmgr()->add(simple, refname);
244 globals->get_soundmgr()->play_looped(refname);
246 globals->get_soundmgr()->play_once(refname);
248 } catch ( sg_io_exception &e ) {
249 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) == "/")) {
266 // Cease rendering a transmission.
267 void FGATC::NoRender(const string& refname) {
270 #ifdef ENABLE_AUDIO_SUPPORT
271 globals->get_soundmgr()->stop(refname);
272 globals->get_soundmgr()->remove(refname);
279 // Generate the text of a message from its parameters and the current context.
280 string FGATC::GenText(const string& m, int c) {
284 ostream& operator << (ostream& os, atc_type atc) {
286 case(AWOS): return(os << "AWOS");
287 case(ATIS): return(os << "ATIS");
288 case(GROUND): return(os << "GROUND");
289 case(TOWER): return(os << "TOWER");
290 case(APPROACH): return(os << "APPROACH");
291 case(DEPARTURE): return(os << "DEPARTURE");
292 case(ENROUTE): return(os << "ENROUTE");
293 case(INVALID): return(os << "INVALID");
295 return(os << "ERROR - Unknown switch in atc_type operator << ");
298 std::istream& operator >> ( std::istream& fin, ATCData& a )
321 return fin >> skipeol;
323 SG_LOG(SG_GENERAL, SG_ALERT, "Warning - unknown type \'" << tp << "\' found whilst reading ATC frequency data!\n");
325 return fin >> skipeol;
328 double lat, lon, elev;
330 fin >> lat >> lon >> elev >> f >> a.range >> a.ident;
331 a.geod = SGGeod::fromDegM(lon, lat, elev);
334 if(ch != '"') a.name += ch;
337 fin.unsetf(std::ios::skipws);
339 if((ch == '"') || (ch == 0x0A)) {
341 } // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
344 fin.setf(std::ios::skipws);
345 //cout << "Comm name = " << a.name << '\n';
347 a.freq = (int)(f*100.0 + 0.5);
349 // cout << a.ident << endl;
351 // generate cartesian coordinates
352 a.cart = SGVec3d::fromGeod(a.geod);
353 return fin >> skipeol;