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>
45 runResponseCounter(false),
46 _runReleaseCounter(false),
50 // Transmission timing stuff
51 pending_transmission(""),
60 SGSoundMgr *smgr = globals->get_soundmgr();
61 _sgr = smgr->find("atc", true);
67 // Derived classes wishing to use the response counter should
68 // call this from their own Update(...).
69 void FGATC::Update(double dt) {
70 if(runResponseCounter) {
71 //cout << responseCounter << '\t' << responseTime << '\n';
72 if(responseCounter >= responseTime) {
73 runResponseCounter = false;
75 //cout << "RESPOND\n";
77 responseCounter += dt;
81 if(_runReleaseCounter) {
82 if(_releaseCounter >= _releaseTime) {
84 _runReleaseCounter = false;
86 _releaseCounter += dt;
90 // Transmission stuff cribbed from AIPlane.cxx
93 //cout << "TUNED STATION FREQ CLEAR\n";
97 _transmitting = false;
99 if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
101 if(_timeout <= 0.0) {
104 // timed out - don't render.
112 _max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
114 //cout << "Transmission = " << pending_transmission << '\n';
116 //Render(pending_transmission, ident, false);
117 Render(pending_transmission);
119 // Run the callback regardless of whether on same freq as user or not.
120 //cout << "_callback_code = " << _callback_code << '\n';
122 ProcessCallback(_callback_code);
125 _transmitting = true;
126 } else if(_transmitting) {
127 if(_counter >= _max_count) {
128 //NoRender(plane.callsign); commented out since at the moment NoRender is designed just to stop repeating messages,
129 // and this will be primarily used on single messages.
130 _transmitting = false;
131 //if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
132 // TODO - need to let the plane the transmission is aimed at that it's finished.
133 // However, for now we'll just release the frequency since if we don't it all goes pear-shaped
134 _releaseCounter = 0.0;
136 _runReleaseCounter = true;
142 void FGATC::ReceiveUserCallback(int code) {
143 SG_LOG(SG_ATC, SG_WARN, "WARNING - whichever ATC class was intended to receive callback code " << code << " didn't get it!!!");
146 void FGATC::SetResponseReqd(const string& rid) {
149 respond = false; // TODO - this ignores the fact that more than one plane could call this before response
150 // Shouldn't happen with AI only, but user could confuse things??
152 runResponseCounter = true;
153 responseCounter = 0.0;
154 responseTime = 1.8; // TODO - randomize this slightly.
157 void FGATC::NotifyTransmissionFinished(const string& rid) {
158 //cout << "Transmission finished, callsign = " << rid << '\n';
162 runResponseCounter = true;
163 responseCounter = 0.0;
164 responseTime = 1.2; // TODO - randomize this slightly, and allow it to be dependent on the transmission and how busy the ATC is.
165 respond = false; // TODO - this ignores the fact that more than one plane could call this before response
166 // Shouldn't happen with AI only, but user could confuse things??
172 void FGATC::Transmit(int callback_code) {
173 SG_LOG(SG_ATC, SG_INFO, "Transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
175 _callback_code = callback_code;
179 void FGATC::ConditionalTransmit(double timeout, int callback_code) {
180 SG_LOG(SG_ATC, SG_INFO, "Timed transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
182 _callback_code = callback_code;
186 void FGATC::ImmediateTransmit(int callback_code) {
187 SG_LOG(SG_ATC, SG_INFO, "Immediate transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
189 //Render(pending_transmission, ident, false);
190 Render(pending_transmission);
191 // At the moment Render doesn't work except for ATIS
194 ProcessCallback(callback_code);
198 // Derived classes should override this.
199 void FGATC::ProcessCallback(int code) {
202 void FGATC::AddPlane(const string& pid) {
205 int FGATC::RemovePlane() {
209 void FGATC::SetData(ATCData* d) {
219 // Render a transmission
220 // Outputs the transmission either on screen or as audio depending on user preference
221 // The refname is a string to identify this sample to the sound manager
222 // The repeating flag indicates whether the message should be repeated continuously or played once.
223 void FGATC::Render(string& msg, const float volume,
224 const string& refname, const bool repeating) {
226 fgSetString("/sim/messages/atis", msg.c_str());
228 fgSetString("/sim/messages/atc", msg.c_str());
230 #ifdef ENABLE_AUDIO_SUPPORT
231 _voice = (_voiceOK && fgGetBool("/sim/sound/voice"));
234 void* buf = _vPtr->WriteMessage((char*)msg.c_str(), &len);
235 if(buf && (volume > 0.05)) {
238 // >>> Beware: must pass a (new) object to the (add) method,
239 // >>> because the (remove) method is going to do a (delete)
240 // >>> whether that's what you want or not.
241 SGSoundSample *simple = new SGSoundSample(&buf, len, 8000);
242 simple->set_volume(volume);
243 _sgr->add(simple, refname);
244 _sgr->play(refname, repeating);
245 } catch ( sg_io_exception &e ) {
246 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
250 #endif // ENABLE_AUDIO_SUPPORT
252 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
253 for(unsigned int i = 0; i < msg.length(); ++i) {
254 if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
263 // Cease rendering a transmission.
264 void FGATC::NoRender(const string& refname) {
267 #ifdef ENABLE_AUDIO_SUPPORT
269 _sgr->remove(refname);
276 // Generate the text of a message from its parameters and the current context.
277 string FGATC::GenText(const string& m, int c) {
281 ostream& operator << (ostream& os, atc_type atc) {
283 case(AWOS): return(os << "AWOS");
284 case(ATIS): return(os << "ATIS");
285 case(GROUND): return(os << "GROUND");
286 case(TOWER): return(os << "TOWER");
287 case(APPROACH): return(os << "APPROACH");
288 case(DEPARTURE): return(os << "DEPARTURE");
289 case(ENROUTE): return(os << "ENROUTE");
290 case(INVALID): return(os << "INVALID");
292 return(os << "ERROR - Unknown switch in atc_type operator << ");
295 std::istream& operator >> ( std::istream& fin, ATCData& a )
318 return fin >> skipeol;
320 SG_LOG(SG_GENERAL, SG_ALERT, "Warning - unknown type \'" << tp << "\' found whilst reading ATC frequency data!\n");
322 return fin >> skipeol;
325 double lat, lon, elev;
327 fin >> lat >> lon >> elev >> f >> a.range >> a.ident;
328 a.geod = SGGeod::fromDegM(lon, lat, elev);
331 if(ch != '"') a.name += ch;
334 fin.unsetf(std::ios::skipws);
336 if((ch == '"') || (ch == 0x0A)) {
338 } // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
341 fin.setf(std::ios::skipws);
342 //cout << "Comm name = " << a.name << '\n';
344 a.freq = (int)(f*100.0 + 0.5);
346 // cout << a.ident << endl;
348 // generate cartesian coordinates
349 a.cart = SGVec3d::fromGeod(a.geod);
350 return fin >> skipeol;