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