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