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