]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATC.cxx
initlialize _playing for FGATC. Proper listerner orientation based on view offset...
[flightgear.git] / src / ATCDCL / ATC.cxx
1 // Implementation of FGATC - ATC subsystem base class.
2 //
3 // Written by David Luff, started February 2002.
4 //
5 // Copyright (C) 2002  David C Luff - david.luff@nottingham.ac.uk
6 //
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.
11 //
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.
16 //
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.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include "ATC.hxx"
26
27 #include <iostream>
28
29 #include <simgear/sound/soundmgr_openal.hxx>
30 #include <simgear/structure/exception.hxx>
31
32 #include <Main/globals.hxx>
33 #include <Main/fg_props.hxx>
34
35
36
37 FGATC::FGATC() :
38         _voiceOK(false),
39         _playing(false),
40         _sgr(NULL),
41         freqClear(true),
42         receiving(false),
43         respond(false),
44         responseID(""),
45         runResponseCounter(false),
46         _runReleaseCounter(false),
47         responseReqd(false),
48         _type(INVALID),
49         _display(false),
50         // Transmission timing stuff
51         pending_transmission(""),
52         _timeout(0),
53         _pending(false),
54         _callback_code(0),
55         _transmit(false),
56         _transmitting(false),
57         _counter(0.0),
58         _max_count(5.0)
59 {
60         SGSoundMgr *smgr = globals->get_soundmgr();
61         _sgr = smgr->find("atc", true);
62 }
63
64 FGATC::~FGATC() {
65 }
66
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;
74                         respond = true;
75                         //cout << "RESPOND\n";
76                 } else {
77                         responseCounter += dt;
78                 }
79         }
80         
81         if(_runReleaseCounter) {
82                 if(_releaseCounter >= _releaseTime) {
83                         freqClear = true;
84                         _runReleaseCounter = false;
85                 } else {
86                         _releaseCounter += dt;
87                 }
88         }
89         
90         // Transmission stuff cribbed from AIPlane.cxx
91         if(_pending) {
92                 if(GetFreqClear()) {
93                         //cout << "TUNED STATION FREQ CLEAR\n";
94                         SetFreqInUse();
95                         _pending = false;
96                         _transmit = true;
97                         _transmitting = false;
98                 } else {
99                         if(_timeout > 0.0) {    // allows count down to be avoided by initially setting it to zero
100                                 _timeout -= dt;
101                                 if(_timeout <= 0.0) {
102                                         _timeout = 0.0;
103                                         _pending = false;
104                                         // timed out - don't render.
105                                 }
106                         }
107                 }
108         }
109
110         if(_transmit) {
111                 _counter = 0.0;
112                 _max_count = 5.0;               // FIXME - hardwired length of message - need to calculate it!
113                 
114                 //cout << "Transmission = " << pending_transmission << '\n';
115                 if(_display) {
116                         //Render(pending_transmission, ident, false);
117                         Render(pending_transmission);
118                 }
119                 // Run the callback regardless of whether on same freq as user or not.
120                 //cout << "_callback_code = " << _callback_code << '\n';
121                 if(_callback_code) {
122                         ProcessCallback(_callback_code);
123                 }
124                 _transmit = false;
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;
135                         _releaseTime = 0.9;
136                         _runReleaseCounter = true;
137                 }
138                 _counter += dt;
139         }
140 }
141
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!!!");
144 }
145
146 void FGATC::SetResponseReqd(const string& rid) {
147         receiving = false;
148         responseReqd = true;
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??
151         responseID = rid;
152         runResponseCounter = true;
153         responseCounter = 0.0;
154         responseTime = 1.8;             // TODO - randomize this slightly.
155 }
156
157 void FGATC::NotifyTransmissionFinished(const string& rid) {
158         //cout << "Transmission finished, callsign = " << rid << '\n';
159         receiving = false;
160         responseID = rid;
161         if(responseReqd) {
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??
167         } else {
168                 freqClear = true;
169         }
170 }
171
172 void FGATC::Transmit(int callback_code) {
173         SG_LOG(SG_ATC, SG_INFO, "Transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
174         _pending = true;
175         _callback_code = callback_code;
176         _timeout = 0.0;
177 }
178
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);
181         _pending = true;
182         _callback_code = callback_code;
183         _timeout = timeout;
184 }
185
186 void FGATC::ImmediateTransmit(int callback_code) {
187         SG_LOG(SG_ATC, SG_INFO, "Immediate transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
188         if(_display) {
189                 //Render(pending_transmission, ident, false);
190                 Render(pending_transmission);
191                 // At the moment Render doesn't work except for ATIS
192         }
193         if(callback_code) {
194                 ProcessCallback(callback_code);
195         }
196 }
197
198 // Derived classes should override this.
199 void FGATC::ProcessCallback(int code) {
200 }
201
202 void FGATC::AddPlane(const string& pid) {
203 }
204
205 int FGATC::RemovePlane() {
206         return 0;
207 }
208
209 void FGATC::SetData(ATCData* d) {
210         _type = d->type;
211         _geod = d->geod;
212         _cart = d->cart;
213         range = d->range;
214         ident = d->ident;
215         name = d->name;
216         freq = d->freq;
217 }
218
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) {
225         if (repeating)
226                 fgSetString("/sim/messages/atis", msg.c_str());
227         else
228                 fgSetString("/sim/messages/atc", msg.c_str());
229
230 #ifdef ENABLE_AUDIO_SUPPORT
231         _voice = (_voiceOK && fgGetBool("/sim/sound/voice"));
232         if(_voice) {
233                 size_t len;
234                 void* buf = _vPtr->WriteMessage((char*)msg.c_str(), &len);
235                 if(buf && (volume > 0.05)) {
236                         NoRender(refname);
237                         try {
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());
247                         }
248                 }
249         }
250 #endif  // ENABLE_AUDIO_SUPPORT
251         if(!_voice) {
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) == "/")) {
255                                 msg[i] = ' ';
256                         }
257                 }
258         }
259         _playing = true;        
260 }
261
262
263 // Cease rendering a transmission.
264 void FGATC::NoRender(const string& refname) {
265         if(_playing) {
266                 if(_voice) {
267 #ifdef ENABLE_AUDIO_SUPPORT             
268                         _sgr->stop(refname);
269                         _sgr->remove(refname);
270 #endif
271                 }
272                 _playing = false;
273         }
274 }
275
276 // Generate the text of a message from its parameters and the current context.
277 string FGATC::GenText(const string& m, int c) {
278         return("");
279 }
280
281 ostream& operator << (ostream& os, atc_type atc) {
282         switch(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");
291         }
292         return(os << "ERROR - Unknown switch in atc_type operator << ");
293 }
294
295 std::istream& operator >> ( std::istream& fin, ATCData& a )
296 {
297         double f;
298         char ch;
299         char tp;
300         
301         fin >> tp;
302         
303         switch(tp) {
304         case 'I':
305                 a.type = ATIS;
306                 break;
307         case 'T':
308                 a.type = TOWER;
309                 break;
310         case 'G':
311                 a.type = GROUND;
312                 break;
313         case 'A':
314                 a.type = APPROACH;
315                 break;
316         case '[':
317                 a.type = INVALID;
318                 return fin >> skipeol;
319         default:
320                 SG_LOG(SG_GENERAL, SG_ALERT, "Warning - unknown type \'" << tp << "\' found whilst reading ATC frequency data!\n");
321                 a.type = INVALID;
322                 return fin >> skipeol;
323         }
324         
325         double lat, lon, elev;
326   
327         fin >> lat >> lon >> elev >> f >> a.range >> a.ident;
328         a.geod = SGGeod::fromDegM(lon, lat, elev);
329         a.name = "";
330         fin >> ch;
331         if(ch != '"') a.name += ch;
332         while(1) {
333                 //in >> noskipws
334                 fin.unsetf(std::ios::skipws);
335                 fin >> ch;
336                 if((ch == '"') || (ch == 0x0A)) {
337                         break;
338                 }   // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
339                 a.name += ch;
340         }
341         fin.setf(std::ios::skipws);
342         //cout << "Comm name = " << a.name << '\n';
343         
344         a.freq = (int)(f*100.0 + 0.5);
345         
346         // cout << a.ident << endl;
347         
348         // generate cartesian coordinates
349         a.cart = SGVec3d::fromGeod(a.geod);     
350         return fin >> skipeol;
351 }
352