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