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>
44 runResponseCounter(false),
45 _runReleaseCounter(false),
49 // Transmission timing stuff
50 pending_transmission(""),
64 // Derived classes wishing to use the response counter should
65 // call this from their own Update(...).
66 void FGATC::Update(double dt) {
67 if(runResponseCounter) {
68 //cout << responseCounter << '\t' << responseTime << '\n';
69 if(responseCounter >= responseTime) {
70 runResponseCounter = false;
72 //cout << "RESPOND\n";
74 responseCounter += dt;
78 if(_runReleaseCounter) {
79 if(_releaseCounter >= _releaseTime) {
81 _runReleaseCounter = false;
83 _releaseCounter += dt;
87 // Transmission stuff cribbed from AIPlane.cxx
90 //cout << "TUNED STATION FREQ CLEAR\n";
94 _transmitting = false;
96 if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
101 // timed out - don't render.
109 _max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
111 //cout << "Transmission = " << pending_transmission << '\n';
113 //Render(pending_transmission, ident, false);
114 Render(pending_transmission);
116 // Run the callback regardless of whether on same freq as user or not.
117 //cout << "_callback_code = " << _callback_code << '\n';
119 ProcessCallback(_callback_code);
122 _transmitting = true;
123 } else if(_transmitting) {
124 if(_counter >= _max_count) {
125 //NoRender(plane.callsign); commented out since at the moment NoRender is designed just to stop repeating messages,
126 // and this will be primarily used on single messages.
127 _transmitting = false;
128 //if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
129 // TODO - need to let the plane the transmission is aimed at that it's finished.
130 // However, for now we'll just release the frequency since if we don't it all goes pear-shaped
131 _releaseCounter = 0.0;
133 _runReleaseCounter = true;
139 void FGATC::ReceiveUserCallback(int code) {
140 SG_LOG(SG_ATC, SG_WARN, "WARNING - whichever ATC class was intended to receive callback code " << code << " didn't get it!!!");
143 void FGATC::SetResponseReqd(const string& rid) {
146 respond = false; // TODO - this ignores the fact that more than one plane could call this before response
147 // Shouldn't happen with AI only, but user could confuse things??
149 runResponseCounter = true;
150 responseCounter = 0.0;
151 responseTime = 1.8; // TODO - randomize this slightly.
154 void FGATC::NotifyTransmissionFinished(const string& rid) {
155 //cout << "Transmission finished, callsign = " << rid << '\n';
159 runResponseCounter = true;
160 responseCounter = 0.0;
161 responseTime = 1.2; // TODO - randomize this slightly, and allow it to be dependent on the transmission and how busy the ATC is.
162 respond = false; // TODO - this ignores the fact that more than one plane could call this before response
163 // Shouldn't happen with AI only, but user could confuse things??
169 void FGATC::Transmit(int callback_code) {
170 SG_LOG(SG_ATC, SG_INFO, "Transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
172 _callback_code = callback_code;
176 void FGATC::ConditionalTransmit(double timeout, int callback_code) {
177 SG_LOG(SG_ATC, SG_INFO, "Timed transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
179 _callback_code = callback_code;
183 void FGATC::ImmediateTransmit(int callback_code) {
184 SG_LOG(SG_ATC, SG_INFO, "Immediate transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
186 //Render(pending_transmission, ident, false);
187 Render(pending_transmission);
188 // At the moment Render doesn't work except for ATIS
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) {
216 // Render a transmission
217 // Outputs the transmission either on screen or as audio depending on user preference
218 // The refname is a string to identify this sample to the sound manager
219 // The repeating flag indicates whether the message should be repeated continuously or played once.
220 void FGATC::Render(string& msg, const float volume,
221 const string& refname, const bool repeating) {
223 fgSetString("/sim/messages/atis", msg.c_str());
225 fgSetString("/sim/messages/atc", msg.c_str());
227 #ifdef ENABLE_AUDIO_SUPPORT
228 _voice = (_voiceOK && fgGetBool("/sim/sound/voice"));
230 string buf = _vPtr->WriteMessage((char*)msg.c_str(), _voice);
231 if(_voice && (volume > 0.05)) {
234 // >>> Beware: must pass a (new) object to the (add) method,
235 // >>> because the (remove) method is going to do a (delete)
236 // >>> whether that's what you want or not.
237 SGSoundSample *simple =
238 new SGSoundSample((unsigned char*) buf.c_str(), buf.length(), 8000);
239 // TODO - at the moment the volume can't be changed
240 // after the transmission has started.
243 smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr");
244 _sgr = smgr->find("atc", true);
246 simple->set_volume(volume);
247 _sgr->add(simple, refname);
248 _sgr->play(refname, repeating);
249 } catch ( sg_io_exception &e ) {
250 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
254 #endif // ENABLE_AUDIO_SUPPORT
256 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
257 for(unsigned int i = 0; i < msg.length(); ++i) {
258 if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
267 // Cease rendering a transmission.
268 void FGATC::NoRender(const string& refname) {
271 #ifdef ENABLE_AUDIO_SUPPORT
273 _sgr->remove(refname);
280 // Generate the text of a message from its parameters and the current context.
281 string FGATC::GenText(const string& m, int c) {
285 ostream& operator << (ostream& os, atc_type atc) {
287 case(AWOS): return(os << "AWOS");
288 case(ATIS): return(os << "ATIS");
289 case(GROUND): return(os << "GROUND");
290 case(TOWER): return(os << "TOWER");
291 case(APPROACH): return(os << "APPROACH");
292 case(DEPARTURE): return(os << "DEPARTURE");
293 case(ENROUTE): return(os << "ENROUTE");
294 case(INVALID): return(os << "INVALID");
296 return(os << "ERROR - Unknown switch in atc_type operator << ");
299 std::istream& operator >> ( std::istream& fin, ATCData& a )
322 return fin >> skipeol;
324 SG_LOG(SG_GENERAL, SG_ALERT, "Warning - unknown type \'" << tp << "\' found whilst reading ATC frequency data!\n");
326 return fin >> skipeol;
329 double lat, lon, elev;
331 fin >> lat >> lon >> elev >> f >> a.range >> a.ident;
332 a.geod = SGGeod::fromDegM(lon, lat, elev);
335 if(ch != '"') a.name += ch;
338 fin.unsetf(std::ios::skipws);
340 if((ch == '"') || (ch == 0x0A)) {
342 } // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
345 fin.setf(std::ios::skipws);
346 //cout << "Comm name = " << a.name << '\n';
348 a.freq = (int)(f*100.0 + 0.5);
350 // cout << a.ident << endl;
352 // generate cartesian coordinates
353 a.cart = SGVec3d::fromGeod(a.geod);
354 return fin >> skipeol;