]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATC.cxx
Added two missing files from JSBSim.org that were missing in the last sync.
[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 <simgear/sound/soundmgr_openal.hxx>
26 #include <simgear/structure/exception.hxx>
27
28 #include <Main/globals.hxx>
29 #include <Main/fg_props.hxx>
30
31 #include "ATC.hxx"
32
33 FGATC::FGATC() {
34         freqClear = true;
35         receiving = false;
36         respond = false;
37         runResponseCounter = false;
38         _runReleaseCounter = false;
39         responseID = "";
40         responseReqd = false;
41         _type = INVALID;
42         _display = false;
43         _displaying = false;
44         
45         // Transmission timing stuff
46         pending_transmission = "";
47         _timeout = 0;
48         _pending = false;
49         _callback_code = 0;
50         _transmit = false;
51         _transmitting = false;
52         _counter = 0.0;
53         _max_count = 5.0;
54         
55         _voiceOK = false;
56 }
57
58 FGATC::~FGATC() {
59 }
60
61 // Derived classes wishing to use the response counter should call this from their own Update(...).
62 void FGATC::Update(double dt) {
63         if(runResponseCounter) {
64                 //cout << responseCounter << '\t' << responseTime << '\n';
65                 if(responseCounter >= responseTime) {
66                         runResponseCounter = false;
67                         respond = true;
68                         //cout << "RESPOND\n";
69                 } else {
70                         responseCounter += dt;
71                 }
72         }
73         
74         if(_runReleaseCounter) {
75                 if(_releaseCounter >= _releaseTime) {
76                         freqClear = true;
77                         _runReleaseCounter = false;
78                 } else {
79                         _releaseCounter += dt;
80                 }
81         }
82         
83         // Transmission stuff cribbed from AIPlane.cxx
84         if(_pending) {
85                 if(GetFreqClear()) {
86                         //cout << "TUNED STATION FREQ CLEAR\n";
87                         SetFreqInUse();
88                         _pending = false;
89                         _transmit = true;
90                         _transmitting = false;
91                 } else {
92                         if(_timeout > 0.0) {    // allows count down to be avoided by initially setting it to zero
93                                 _timeout -= dt;
94                                 if(_timeout <= 0.0) {
95                                         _timeout = 0.0;
96                                         _pending = false;
97                                         // timed out - don't render.
98                                 }
99                         }
100                 }
101         }
102
103         if(_transmit) {
104                 _counter = 0.0;
105                 _max_count = 5.0;               // FIXME - hardwired length of message - need to calculate it!
106                 
107                 //cout << "Transmission = " << pending_transmission << '\n';
108                 if(_display) {
109                         //Render(pending_transmission, ident, false);
110                         Render(pending_transmission);
111                 }
112                 // Run the callback regardless of whether on same freq as user or not.
113                 //cout << "_callback_code = " << _callback_code << '\n';
114                 if(_callback_code) {
115                         ProcessCallback(_callback_code);
116                 }
117                 _transmit = false;
118                 _transmitting = true;
119         } else if(_transmitting) {
120                 if(_counter >= _max_count) {
121                         //NoRender(plane.callsign);  commented out since at the moment NoRender is designed just to stop repeating messages,
122                         // and this will be primarily used on single messages.
123                         _transmitting = false;
124                         //if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
125                         // TODO - need to let the plane the transmission is aimed at that it's finished.
126                         // However, for now we'll just release the frequency since if we don't it all goes pear-shaped
127                         _releaseCounter = 0.0;
128                         _releaseTime = 0.9;
129                         _runReleaseCounter = true;
130                 }
131                 _counter += dt;
132         }
133 }
134
135 void FGATC::ReceiveUserCallback(int code) {
136         SG_LOG(SG_ATC, SG_WARN, "WARNING - whichever ATC class was intended to receive callback code " << code << " didn't get it!!!");
137 }
138
139 void FGATC::SetResponseReqd(const string& rid) {
140         receiving = false;
141         responseReqd = true;
142         respond = false;        // TODO - this ignores the fact that more than one plane could call this before response
143                                                 // Shouldn't happen with AI only, but user could confuse things??
144         responseID = rid;
145         runResponseCounter = true;
146         responseCounter = 0.0;
147         responseTime = 1.8;             // TODO - randomize this slightly.
148 }
149
150 void FGATC::NotifyTransmissionFinished(const string& rid) {
151         //cout << "Transmission finished, callsign = " << rid << '\n';
152         receiving = false;
153         responseID = rid;
154         if(responseReqd) {
155                 runResponseCounter = true;
156                 responseCounter = 0.0;
157                 responseTime = 1.2;     // TODO - randomize this slightly, and allow it to be dependent on the transmission and how busy the ATC is.
158                 respond = false;        // TODO - this ignores the fact that more than one plane could call this before response
159                                                         // Shouldn't happen with AI only, but user could confuse things??
160         } else {
161                 freqClear = true;
162         }
163 }
164
165 void FGATC::Transmit(int callback_code) {
166         SG_LOG(SG_ATC, SG_INFO, "Transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
167         _pending = true;
168         _callback_code = callback_code;
169         _timeout = 0.0;
170 }
171
172 void FGATC::ConditionalTransmit(double timeout, int callback_code) {
173         SG_LOG(SG_ATC, SG_INFO, "Timed transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
174         _pending = true;
175         _callback_code = callback_code;
176         _timeout = timeout;
177 }
178
179 void FGATC::ImmediateTransmit(int callback_code) {
180         SG_LOG(SG_ATC, SG_INFO, "Immediate transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
181         if(_display) {
182                 //Render(pending_transmission, ident, false);
183                 Render(pending_transmission);
184                 // At the moment Render doesn't work except for ATIS
185         }
186         if(callback_code) {
187                 ProcessCallback(callback_code);
188         }
189 }
190
191 // Derived classes should override this.
192 void FGATC::ProcessCallback(int code) {
193 }
194
195 void FGATC::AddPlane(const string& pid) {
196 }
197
198 int FGATC::RemovePlane() {
199         return 0;
200 }
201
202 void FGATC::SetData(ATCData* d) {
203         lon = d->lon;
204         lat = d->lat;
205         elev = d->elev;
206         x = d->x;
207         y = d->y;
208         z = d->z;
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 string& refname, bool repeating) {
220         if (repeating)
221                 fgSetString("/sim/messages/atis", msg.c_str());
222         else
223                 fgSetString("/sim/messages/atc", msg.c_str());
224
225         #ifdef ENABLE_AUDIO_SUPPORT
226         _voice = (_voiceOK && fgGetBool("/sim/sound/voice"));
227         if(_voice) {
228                 int len;
229                 unsigned char* buf = _vPtr->WriteMessage((char*)msg.c_str(), len, _voice);
230                 if(_voice) {
231                         try {
232                                 SGSoundSample *simple
233                                                                 = new SGSoundSample(buf, len, 8000);
234                                 // TODO - at the moment the volume is always set off comm1 
235                                 // and can't be changed after the transmission has started.
236                                 simple->set_volume(5.0 * fgGetDouble("/instrumentation/comm[0]/volume"));
237                                 globals->get_soundmgr()->add(simple, refname);
238                                 if(repeating) {
239                                         globals->get_soundmgr()->play_looped(refname);
240                                 } else {
241                                         globals->get_soundmgr()->play_once(refname);
242                                 }
243                         } catch ( sg_io_exception &e ) {
244                                 SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
245                         }
246                 }
247                 delete[] buf;
248         }
249         #endif  // ENABLE_AUDIO_SUPPORT
250         if(!_voice) {
251                 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
252                 for(unsigned int i = 0; i < msg.length(); ++i) {
253                         if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
254                                 msg[i] = ' ';
255                         }
256                 }
257         }
258         _playing = true;        
259 }
260
261
262 // Cease rendering a transmission.
263 void FGATC::NoRender(const string& refname) {
264         if(_playing) {
265                 if(_voice) {
266 #ifdef ENABLE_AUDIO_SUPPORT             
267                         globals->get_soundmgr()->stop(refname);
268                         globals->get_soundmgr()->remove(refname);
269 #endif
270                 }
271                 _playing = false;
272         }
273 }
274
275 // Generate the text of a message from its parameters and the current context.
276 string FGATC::GenText(const string& m, int c) {
277         return("");
278 }
279
280 ostream& operator << (ostream& os, atc_type atc) {
281         switch(atc) {
282                 case(INVALID):    return(os << "INVALID");
283                 case(ATIS):       return(os << "ATIS");
284                 case(GROUND):     return(os << "GROUND");
285                 case(TOWER):      return(os << "TOWER");
286                 case(APPROACH):   return(os << "APPROACH");
287                 case(DEPARTURE):  return(os << "DEPARTURE");
288                 case(ENROUTE):    return(os << "ENROUTE");
289         }
290         return(os << "ERROR - Unknown switch in atc_type operator << ");
291 }