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., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <simgear/sound/soundmgr.hxx>
27 #include <Main/globals.hxx>
28 #include <Main/fg_props.hxx>
31 #include "ATCdisplay.hxx"
37 runResponseCounter = false;
38 _runReleaseCounter = false;
45 // Transmission timing stuff
46 pending_transmission = "";
51 _transmitting = false;
59 // Derived classes wishing to use the response counter should call this from their own Update(...).
60 void FGATC::Update(double dt) {
61 if(runResponseCounter) {
62 //cout << responseCounter << '\t' << responseTime << '\n';
63 if(responseCounter >= responseTime) {
64 runResponseCounter = false;
66 //cout << "RESPOND\n";
68 responseCounter += dt;
72 if(_runReleaseCounter) {
73 if(_releaseCounter >= _releaseTime) {
75 _runReleaseCounter = false;
77 _releaseCounter += dt;
81 // Transmission stuff cribbed from AIPlane.cxx
84 //cout << "TUNED STATION FREQ CLEAR\n";
88 _transmitting = false;
90 if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
95 // timed out - don't render.
103 _max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
105 //cout << "Transmission = " << pending_transmission << '\n';
107 //Render(pending_transmission, ident, false);
108 // At the moment Render only works for ATIS
109 globals->get_ATC_display()->RegisterSingleMessage(pending_transmission);
111 // Run the callback regardless of whether on same freq as user or not.
112 //cout << "_callback_code = " << _callback_code << '\n';
114 ProcessCallback(_callback_code);
117 _transmitting = true;
118 } else if(_transmitting) {
119 if(_counter >= _max_count) {
120 //NoRender(plane.callsign); commented out since at the moment NoRender is designed just to stop repeating messages,
121 // and this will be primarily used on single messages.
122 _transmitting = false;
123 //if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
124 // TODO - need to let the plane the transmission is aimed at that it's finished.
130 void FGATC::ReceiveUserCallback(int code) {
131 SG_LOG(SG_ATC, SG_WARN, "WARNING - whichever ATC class was intended to receive callback code " << code << " didn't get it!!!");
134 void FGATC::SetResponseReqd(string rid) {
137 respond = false; // TODO - this ignores the fact that more than one plane could call this before response
138 // Shouldn't happen with AI only, but user could confuse things??
140 runResponseCounter = true;
141 responseCounter = 0.0;
142 responseTime = 1.8; // TODO - randomize this slightly.
145 void FGATC::NotifyTransmissionFinished(string rid) {
146 //cout << "Transmission finished, callsign = " << rid << '\n';
150 runResponseCounter = true;
151 responseCounter = 0.0;
152 responseTime = 1.2; // TODO - randomize this slightly, and allow it to be dependent on the transmission and how busy the ATC is.
153 respond = false; // TODO - this ignores the fact that more than one plane could call this before response
154 // Shouldn't happen with AI only, but user could confuse things??
160 void FGATC::Transmit(int callback_code) {
161 SG_LOG(SG_ATC, SG_INFO, "Transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
163 _callback_code = callback_code;
167 void FGATC::ConditionalTransmit(double timeout, int callback_code) {
168 SG_LOG(SG_ATC, SG_INFO, "Timed transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
170 _callback_code = callback_code;
174 void FGATC::ImmediateTransmit(int callback_code) {
175 SG_LOG(SG_ATC, SG_INFO, "Immediate transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
177 //Render(pending_transmission, ident, false);
178 // At the moment Render doesn't work except for ATIS
179 globals->get_ATC_display()->RegisterSingleMessage(pending_transmission);
182 ProcessCallback(callback_code);
186 // Derived classes should override this.
187 void FGATC::ProcessCallback(int code) {
190 void FGATC::AddPlane(string pid) {
193 int FGATC::RemovePlane() {
197 void FGATC::SetData(ATCData* d) {
210 // Render a transmission
211 // Outputs the transmission either on screen or as audio depending on user preference
212 // The refname is a string to identify this sample to the sound manager
213 // The repeating flag indicates whether the message should be repeated continuously or played once.
214 void FGATC::Render(string msg, string refname, bool repeating) {
215 #ifdef ENABLE_AUDIO_SUPPORT
216 voice = (voiceOK && fgGetBool("/sim/sound/audible")
217 && fgGetBool("/sim/sound/voice"));
220 unsigned char* buf = vPtr->WriteMessage((char*)msg.c_str(), len, voice);
222 SGSimpleSound* simple = new SGSimpleSound(buf, len);
223 // TODO - at the moment the volume is always set off comm1
224 // and can't be changed after the transmission has started.
225 simple->set_volume(5.0 * fgGetDouble("/radios/comm[0]/volume"));
226 globals->get_soundmgr()->add(simple, refname);
228 globals->get_soundmgr()->play_looped(refname);
230 globals->get_soundmgr()->play_once(refname);
235 #endif // ENABLE_AUDIO_SUPPORT
237 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
238 for(unsigned int i = 0; i < msg.length(); ++i) {
239 if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
243 globals->get_ATC_display()->RegisterRepeatingMessage(msg);
249 // Cease rendering a transmission.
250 void FGATC::NoRender(string refname) {
253 #ifdef ENABLE_AUDIO_SUPPORT
254 globals->get_soundmgr()->stop(refname);
255 globals->get_soundmgr()->remove(refname);
258 globals->get_ATC_display()->CancelRepeatingMessage();
264 // Generate the text of a message from its parameters and the current context.
265 string FGATC::GenText(const string& m, int c) {
269 ostream& operator << (ostream& os, atc_type atc) {
271 case(INVALID): return(os << "INVALID");
272 case(ATIS): return(os << "ATIS");
273 case(GROUND): return(os << "GROUND");
274 case(TOWER): return(os << "TOWER");
275 case(APPROACH): return(os << "APPROACH");
276 case(DEPARTURE): return(os << "DEPARTURE");
277 case(ENROUTE): return(os << "ENROUTE");
279 return(os << "ERROR - Unknown switch in atc_type operator << ");