]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATC.cxx
2653d442f4e27115e2cee96a884bbbf2b5a8e265
[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         _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         _transmit(false),
55         _transmitting(false),
56         _counter(0.0),
57         _max_count(5.0)
58 {
59         SGSoundMgr *smgr = globals->get_soundmgr();
60         _sgr = smgr->find("atc", true);
61         _sgr->tie_to_listener();
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                 _transmit = false;
137                 _transmitting = true;
138         } else if(_transmitting) {
139                 if(_counter >= _max_count) {
140                         //NoRender(plane.callsign);  commented out since at the moment NoRender is designed just to stop repeating messages,
141                         // and this will be primarily used on single messages.
142                         _transmitting = false;
143                         //if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
144                         // TODO - need to let the plane the transmission is aimed at that it's finished.
145                         // However, for now we'll just release the frequency since if we don't it all goes pear-shaped
146                         _releaseCounter = 0.0;
147                         _releaseTime = 0.9;
148                         _runReleaseCounter = true;
149                 }
150                 _counter += dt;
151         }
152 }
153
154 void FGATC::ReceiveUserCallback(int code) {
155         SG_LOG(SG_ATC, SG_WARN, "WARNING - whichever ATC class was intended to receive callback code " << code << " didn't get it!!!");
156 }
157
158 void FGATC::SetResponseReqd(const string& rid) {
159         receiving = false;
160         responseReqd = true;
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         responseID = rid;
164         runResponseCounter = true;
165         responseCounter = 0.0;
166         responseTime = 1.8;             // TODO - randomize this slightly.
167 }
168
169 void FGATC::NotifyTransmissionFinished(const string& rid) {
170         //cout << "Transmission finished, callsign = " << rid << '\n';
171         receiving = false;
172         responseID = rid;
173         if(responseReqd) {
174                 runResponseCounter = true;
175                 responseCounter = 0.0;
176                 responseTime = 1.2;     // TODO - randomize this slightly, and allow it to be dependent on the transmission and how busy the ATC is.
177                 respond = false;        // TODO - this ignores the fact that more than one plane could call this before response
178                                                         // Shouldn't happen with AI only, but user could confuse things??
179         } else {
180                 freqClear = true;
181         }
182 }
183
184 void FGATC::SetStation(flightgear::CommStation* sta) {
185     switch (sta->type()) {
186         case FGPositioned::FREQ_ATIS:   _type = ATIS; break;
187         case FGPositioned::FREQ_AWOS:   _type = AWOS; break;
188         default:
189         throw sg_exception("unsupported comm station type");
190     }
191     
192     _geod = sta->geod();
193     _cart = sta->cart();
194     range = sta->rangeNm();
195     ident = sta->airport()->ident();
196     name = sta->airport()->name();
197     freq = sta->freqKHz();
198 }
199
200 // Render a transmission
201 // Outputs the transmission either on screen or as audio depending on user preference
202 // The refname is a string to identify this sample to the sound manager
203 // The repeating flag indicates whether the message should be repeated continuously or played once.
204 void FGATC::Render(string& msg, const float volume, 
205                                    const string& refname, const bool repeating) {
206         if (volume < 0.05)
207         {
208                 NoRender(refname);
209                 return;
210         }
211
212         if (repeating)
213                 fgSetString("/sim/messages/atis", msg.c_str());
214         else
215                 fgSetString("/sim/messages/atc", msg.c_str());
216
217 #ifdef ENABLE_AUDIO_SUPPORT
218         _voice = (_voiceOK && fgGetBool("/sim/sound/voice"));
219         if(_voice) {
220                 size_t len;
221                 void* buf = _vPtr->WriteMessage((char*)msg.c_str(), &len);
222                 NoRender(refname);
223                 if(buf) {
224                         try {
225 // >>> Beware: must pass a (new) object to the (add) method,
226 // >>> because the (remove) method is going to do a (delete)
227 // >>> whether that's what you want or not.
228                                 SGSoundSample *simple = new SGSoundSample(&buf, len, 8000);
229                                 simple->set_volume(volume);
230                                 _sgr->add(simple, refname);
231                                 _sgr->play(refname, repeating);
232                         } catch ( sg_io_exception &e ) {
233                                 SG_LOG(SG_ATC, SG_ALERT, e.getFormattedMessage());
234                         }
235                 }
236         }
237 #endif  // ENABLE_AUDIO_SUPPORT
238         if(!_voice) {
239                 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
240                 for(unsigned int i = 0; i < msg.length(); ++i) {
241                         if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
242                                 msg[i] = ' ';
243                         }
244                 }
245         }
246         _playing = true;        
247 }
248
249
250 // Cease rendering a transmission.
251 void FGATC::NoRender(const string& refname) {
252         if(_playing) {
253                 if(_voice) {
254 #ifdef ENABLE_AUDIO_SUPPORT             
255                         _sgr->stop(refname);
256                         _sgr->remove(refname);
257 #endif
258                 }
259                 _playing = false;
260         }
261 }
262
263 // Generate the text of a message from its parameters and the current context.
264 string FGATC::GenText(const string& m, int c) {
265         return("");
266 }
267
268 ostream& operator << (ostream& os, atc_type atc) {
269         switch(atc) {
270                 case(AWOS):        return(os << "AWOS");
271                 case(ATIS):        return(os << "ATIS");
272                 case(GROUND):    return(os << "GROUND");
273                 case(TOWER):      return(os << "TOWER");
274                 case(APPROACH):   return(os << "APPROACH");
275                 case(DEPARTURE):  return(os << "DEPARTURE");
276                 case(ENROUTE):  return(os << "ENROUTE");
277                 case(INVALID):  return(os << "INVALID");
278         }
279         return(os << "ERROR - Unknown switch in atc_type operator << ");
280 }
281
282 std::istream& operator >> ( std::istream& fin, ATCData& a )
283 {
284         double f;
285         char ch;
286         char tp;
287         
288         fin >> tp;
289         
290         switch(tp) {
291         case 'I':
292                 a.type = ATIS;
293                 break;
294         case 'T':
295                 a.type = TOWER;
296                 break;
297         case 'G':
298                 a.type = GROUND;
299                 break;
300         case 'A':
301                 a.type = APPROACH;
302                 break;
303         case '[':
304                 a.type = INVALID;
305                 return fin >> skipeol;
306         default:
307                 SG_LOG(SG_ATC, SG_ALERT, "Warning - unknown type \'" << tp << "\' found whilst reading ATC frequency data!\n");
308                 a.type = INVALID;
309                 return fin >> skipeol;
310         }
311         
312         double lat, lon, elev;
313
314         fin >> lat >> lon >> elev >> f >> a.range >> a.ident;
315         a.geod = SGGeod::fromDegM(lon, lat, elev);
316         a.name = "";
317         fin >> ch;
318         if(ch != '"') a.name += ch;
319         while(1) {
320                 //in >> noskipws
321                 fin.unsetf(std::ios::skipws);
322                 fin >> ch;
323                 if((ch == '"') || (ch == 0x0A)) {
324                         break;
325                 }   // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
326                 a.name += ch;
327         }
328         fin.setf(std::ios::skipws);
329         //cout << "Comm name = " << a.name << '\n';
330         
331         a.freq = (int)(f*100.0 + 0.5);
332         
333         // cout << a.ident << endl;
334         
335         // generate cartesian coordinates
336         a.cart = SGVec3d::fromGeod(a.geod);     
337         return fin >> skipeol;
338 }
339