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