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