]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATC.cxx
59825d25e60f3515187f91ccd50e5b74064dfd75
[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 #include <ATC/CommStation.hxx>
35 #include <Airports/simple.hxx>
36
37 FGATC::FGATC() :
38     freq(0),
39     _currentStation(NULL),
40     range(0),
41     _voice(true),
42         _playing(false),
43         _sgr(NULL),
44 #ifdef OLD_ATC_MGR
45         freqClear(true),
46         receiving(false),
47         respond(false),
48         responseID(""),
49         runResponseCounter(false),
50         _runReleaseCounter(false),
51         responseReqd(false),
52         // Transmission timing stuff
53         pending_transmission(""),
54 #endif
55         _type(INVALID),
56         _display(false)
57 #ifdef OLD_ATC_MGR
58         ,_timeout(0),
59         _pending(false),
60         _transmit(false),
61         _transmitting(false),
62         _counter(0.0),
63         _max_count(5.0)
64 #endif
65 {
66         SGSoundMgr *smgr = globals->get_soundmgr();
67         _sgr = smgr->find("atc", true);
68         _sgr->tie_to_listener();
69
70         _masterVolume = fgGetNode("/sim/sound/atc/volume", true);
71         _enabled = fgGetNode("/sim/sound/atc/enabled", true);
72         _atc_external = fgGetNode("/sim/sound/atc/external-view", true);
73         _internal = fgGetNode("/sim/current-view/internal", true);
74 }
75
76 FGATC::~FGATC() {
77 }
78
79 // Derived classes wishing to use the response counter should 
80 // call this from their own Update(...).
81 void FGATC::Update(double dt) {
82
83 #ifdef ENABLE_AUDIO_SUPPORT
84     bool active = _atc_external->getBoolValue() ||
85               _internal->getBoolValue();
86
87     if ( active && _enabled->getBoolValue() ) {
88         _sgr->set_volume( _masterVolume->getFloatValue() );
89         _sgr->resume(); // no-op if already in resumed state
90     } else {
91         _sgr->suspend();
92     }
93 #endif
94
95 #ifdef OLD_ATC_MGR
96         if(runResponseCounter) {
97                 //cout << responseCounter << '\t' << responseTime << '\n';
98                 if(responseCounter >= responseTime) {
99                         runResponseCounter = false;
100                         respond = true;
101                         //cout << "RESPOND\n";
102                 } else {
103                         responseCounter += dt;
104                 }
105         }
106         
107         if(_runReleaseCounter) {
108                 if(_releaseCounter >= _releaseTime) {
109                         freqClear = true;
110                         _runReleaseCounter = false;
111                 } else {
112                         _releaseCounter += dt;
113                 }
114         }
115         
116         // Transmission stuff cribbed from AIPlane.cxx
117         if(_pending) {
118                 if(GetFreqClear()) {
119                         //cout << "TUNED STATION FREQ CLEAR\n";
120                         SetFreqInUse();
121                         _pending = false;
122                         _transmit = true;
123                         _transmitting = false;
124                 } else {
125                         if(_timeout > 0.0) {    // allows count down to be avoided by initially setting it to zero
126                                 _timeout -= dt;
127                                 if(_timeout <= 0.0) {
128                                         _timeout = 0.0;
129                                         _pending = false;
130                                         // timed out - don't render.
131                                 }
132                         }
133                 }
134         }
135
136         if(_transmit) {
137                 _counter = 0.0;
138                 _max_count = 5.0;               // FIXME - hardwired length of message - need to calculate it!
139                 
140                 //cout << "Transmission = " << pending_transmission << '\n';
141                 if(_display) {
142                         //Render(pending_transmission, ident, false);
143                         Render(pending_transmission);
144                 }
145                 _transmit = false;
146                 _transmitting = true;
147         } else if(_transmitting) {
148                 if(_counter >= _max_count) {
149                         //NoRender(plane.callsign);  commented out since at the moment NoRender is designed just to stop repeating messages,
150                         // and this will be primarily used on single messages.
151                         _transmitting = false;
152                         //if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
153                         // TODO - need to let the plane the transmission is aimed at that it's finished.
154                         // However, for now we'll just release the frequency since if we don't it all goes pear-shaped
155                         _releaseCounter = 0.0;
156                         _releaseTime = 0.9;
157                         _runReleaseCounter = true;
158                 }
159                 _counter += dt;
160         }
161 #endif
162 }
163
164 #ifdef OLD_ATC_MGR
165 void FGATC::ReceiveUserCallback(int code) {
166         SG_LOG(SG_ATC, SG_WARN, "WARNING - whichever ATC class was intended to receive callback code " << code << " didn't get it!!!");
167 }
168
169 void FGATC::SetResponseReqd(const string& rid) {
170         receiving = false;
171         responseReqd = true;
172         respond = false;        // TODO - this ignores the fact that more than one plane could call this before response
173                                                 // Shouldn't happen with AI only, but user could confuse things??
174         responseID = rid;
175         runResponseCounter = true;
176         responseCounter = 0.0;
177         responseTime = 1.8;             // TODO - randomize this slightly.
178 }
179
180 void FGATC::NotifyTransmissionFinished(const string& rid) {
181         //cout << "Transmission finished, callsign = " << rid << '\n';
182         receiving = false;
183         responseID = rid;
184         if(responseReqd) {
185                 runResponseCounter = true;
186                 responseCounter = 0.0;
187                 responseTime = 1.2;     // TODO - randomize this slightly, and allow it to be dependent on the transmission and how busy the ATC is.
188                 respond = false;        // TODO - this ignores the fact that more than one plane could call this before response
189                                                         // Shouldn't happen with AI only, but user could confuse things??
190         } else {
191                 freqClear = true;
192         }
193 }
194 #endif
195
196 void FGATC::SetStation(flightgear::CommStation* sta) {
197     if (_currentStation == sta)
198         return;
199     _currentStation = sta;
200
201     if (sta)
202     {
203         switch (sta->type()) {
204             case FGPositioned::FREQ_ATIS:   _type = ATIS; break;
205             case FGPositioned::FREQ_AWOS:   _type = AWOS; break;
206             default:
207                 sta = NULL;
208                 break;
209         }
210     }
211
212     if (sta == NULL)
213     {
214         range = 0;
215         ident = "";
216         name = "";
217         freq = 0;
218
219         SetNoDisplay();
220         Update(0);     // one last update
221     }
222     else
223     {
224         _geod = sta->geod();
225         _cart = sta->cart();
226
227         range = sta->rangeNm();
228         ident = sta->airport()->ident();
229         name = sta->airport()->name();
230         freq = sta->freqKHz();
231         SetDisplay();
232     }
233 }
234
235 // Render a transmission
236 // Outputs the transmission either on screen or as audio depending on user preference
237 // The refname is a string to identify this sample to the sound manager
238 // The repeating flag indicates whether the message should be repeated continuously or played once.
239 void FGATC::Render(string& msg, const float volume, 
240                                    const string& refname, const bool repeating) {
241         if ((!_display) ||(volume < 0.05))
242         {
243                 NoRender(refname);
244                 return;
245         }
246
247         if (repeating)
248                 fgSetString("/sim/messages/atis", msg.c_str());
249         else
250                 fgSetString("/sim/messages/atc", msg.c_str());
251
252 #ifdef ENABLE_AUDIO_SUPPORT
253         bool useVoice = _voice && fgGetBool("/sim/sound/voice") && fgGetBool("/sim/sound/atc/enabled");
254     SGSoundSample *simple = _sgr->find(refname);
255         if(useVoice) {
256                 if (simple && (_currentMsg == msg))
257                 {
258                     simple->set_volume(volume);
259                 }
260                 else
261                 {
262                     _currentMsg = msg;
263                     size_t len;
264                     void* buf = NULL;
265                     if (!_vPtr)
266                         _vPtr = GetVoicePointer();
267                     if (_vPtr)
268                         buf = _vPtr->WriteMessage((char*)msg.c_str(), &len);
269                     NoRender(refname);
270                     if(buf) {
271                         try {
272 // >>> Beware: must pass a (new) object to the (add) method,
273 // >>> because the (remove) method is going to do a (delete)
274 // >>> whether that's what you want or not.
275                             simple = new SGSoundSample(&buf, len, 8000);
276                             simple->set_volume(volume);
277                             _sgr->add(simple, refname);
278                             _sgr->play(refname, repeating);
279                         } catch ( sg_io_exception &e ) {
280                             SG_LOG(SG_ATC, SG_ALERT, e.getFormattedMessage());
281                         }
282                     }
283                 }
284         }
285         else
286         if (simple)
287         {
288             NoRender(refname);
289         }
290 #endif  // ENABLE_AUDIO_SUPPORT
291
292         if (!useVoice)
293         {
294                 // first rip the underscores and the pause hints out of the string - these are for the convenience of the voice parser
295                 for(unsigned int i = 0; i < msg.length(); ++i) {
296                         if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
297                                 msg[i] = ' ';
298                         }
299                 }
300         }
301         _playing = true;
302 }
303
304
305 // Cease rendering a transmission.
306 void FGATC::NoRender(const string& refname) {
307         if(_playing) {
308                 if(_voice) {
309 #ifdef ENABLE_AUDIO_SUPPORT
310                         _sgr->stop(refname);
311                         _sgr->remove(refname);
312 #endif
313                 }
314                 _playing = false;
315         }
316 }
317
318 #ifdef OLD_ATC_MGR
319 // Generate the text of a message from its parameters and the current context.
320 string FGATC::GenText(const string& m, int c) {
321         return("");
322 }
323
324 ostream& operator << (ostream& os, atc_type atc) {
325         switch(atc) {
326                 case(AWOS):        return(os << "AWOS");
327                 case(ATIS):        return(os << "ATIS");
328                 case(GROUND):    return(os << "GROUND");
329                 case(TOWER):      return(os << "TOWER");
330                 case(APPROACH):   return(os << "APPROACH");
331                 case(DEPARTURE):  return(os << "DEPARTURE");
332                 case(ENROUTE):  return(os << "ENROUTE");
333                 case(INVALID):  return(os << "INVALID");
334         }
335         return(os << "ERROR - Unknown switch in atc_type operator << ");
336 }
337
338 std::istream& operator >> ( std::istream& fin, ATCData& a )
339 {
340         double f;
341         char ch;
342         char tp;
343         
344         fin >> tp;
345         
346         switch(tp) {
347         case 'I':
348                 a.type = ATIS;
349                 break;
350         case 'T':
351                 a.type = TOWER;
352                 break;
353         case 'G':
354                 a.type = GROUND;
355                 break;
356         case 'A':
357                 a.type = APPROACH;
358                 break;
359         case '[':
360                 a.type = INVALID;
361                 return fin >> skipeol;
362         default:
363                 SG_LOG(SG_ATC, SG_ALERT, "Warning - unknown type \'" << tp << "\' found whilst reading ATC frequency data!\n");
364                 a.type = INVALID;
365                 return fin >> skipeol;
366         }
367         
368         double lat, lon, elev;
369
370         fin >> lat >> lon >> elev >> f >> a.range >> a.ident;
371         a.geod = SGGeod::fromDegM(lon, lat, elev);
372         a.name = "";
373         fin >> ch;
374         if(ch != '"') a.name += ch;
375         while(1) {
376                 //in >> noskipws
377                 fin.unsetf(std::ios::skipws);
378                 fin >> ch;
379                 if((ch == '"') || (ch == 0x0A)) {
380                         break;
381                 }   // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
382                 a.name += ch;
383         }
384         fin.setf(std::ios::skipws);
385         //cout << "Comm name = " << a.name << '\n';
386         
387         a.freq = (int)(f*100.0 + 0.5);
388         
389         // cout << a.ident << endl;
390         
391         // generate cartesian coordinates
392         a.cart = SGVec3d::fromGeod(a.geod);     
393         return fin >> skipeol;
394 }
395 #endif