]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATC.cxx
Ima Sudonim:
[flightgear.git] / src / ATC / 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., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <simgear/sound/soundmgr_openal.hxx>
26
27 #include <Main/globals.hxx>
28 #include <Main/fg_props.hxx>
29
30 #include "ATC.hxx"
31 #include "ATCdisplay.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                         // At the moment Render only works for ATIS
112                         //globals->get_ATC_display()->RegisterSingleMessage(pending_transmission);
113                 }
114                 // Run the callback regardless of whether on same freq as user or not.
115                 //cout << "_callback_code = " << _callback_code << '\n';
116                 if(_callback_code) {
117                         ProcessCallback(_callback_code);
118                 }
119                 _transmit = false;
120                 _transmitting = true;
121         } else if(_transmitting) {
122                 if(_counter >= _max_count) {
123                         //NoRender(plane.callsign);  commented out since at the moment NoRender is designed just to stop repeating messages,
124                         // and this will be primarily used on single messages.
125                         _transmitting = false;
126                         //if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
127                         // TODO - need to let the plane the transmission is aimed at that it's finished.
128                         // However, for now we'll just release the frequency since if we don't it all goes pear-shaped
129                         _releaseCounter = 0.0;
130                         _releaseTime = 0.9;
131                         _runReleaseCounter = true;
132                 }
133                 _counter += dt;
134         }
135 }
136
137 void FGATC::ReceiveUserCallback(int code) {
138         SG_LOG(SG_ATC, SG_WARN, "WARNING - whichever ATC class was intended to receive callback code " << code << " didn't get it!!!");
139 }
140
141 void FGATC::SetResponseReqd(string rid) {
142         receiving = false;
143         responseReqd = true;
144         respond = false;        // TODO - this ignores the fact that more than one plane could call this before response
145                                                 // Shouldn't happen with AI only, but user could confuse things??
146         responseID = rid;
147         runResponseCounter = true;
148         responseCounter = 0.0;
149         responseTime = 1.8;             // TODO - randomize this slightly.
150 }
151
152 void FGATC::NotifyTransmissionFinished(string rid) {
153         //cout << "Transmission finished, callsign = " << rid << '\n';
154         receiving = false;
155         responseID = rid;
156         if(responseReqd) {
157                 runResponseCounter = true;
158                 responseCounter = 0.0;
159                 responseTime = 1.2;     // TODO - randomize this slightly, and allow it to be dependent on the transmission and how busy the ATC is.
160                 respond = false;        // TODO - this ignores the fact that more than one plane could call this before response
161                                                         // Shouldn't happen with AI only, but user could confuse things??
162         } else {
163                 freqClear = true;
164         }
165 }
166
167 void FGATC::Transmit(int callback_code) {
168         SG_LOG(SG_ATC, SG_INFO, "Transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
169         _pending = true;
170         _callback_code = callback_code;
171         _timeout = 0.0;
172 }
173
174 void FGATC::ConditionalTransmit(double timeout, int callback_code) {
175         SG_LOG(SG_ATC, SG_INFO, "Timed transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
176         _pending = true;
177         _callback_code = callback_code;
178         _timeout = timeout;
179 }
180
181 void FGATC::ImmediateTransmit(int callback_code) {
182         SG_LOG(SG_ATC, SG_INFO, "Immediate transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
183         if(_display) {
184                 //Render(pending_transmission, ident, false);
185                 Render(pending_transmission);
186                 // At the moment Render doesn't work except for ATIS
187                 //globals->get_ATC_display()->RegisterSingleMessage(pending_transmission);
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(string pid) {
199 }
200
201 int FGATC::RemovePlane() {
202         return 0;
203 }
204
205 void FGATC::SetData(ATCData* d) {
206         lon = d->lon;
207         lat = d->lat;
208         elev = d->elev;
209         x = d->x;
210         y = d->y;
211         z = d->z;
212         range = d->range;
213         ident = d->ident;
214         name = d->name;
215         freq = d->freq;
216 }
217
218 // Render a transmission
219 // Outputs the transmission either on screen or as audio depending on user preference
220 // The refname is a string to identify this sample to the sound manager
221 // The repeating flag indicates whether the message should be repeated continuously or played once.
222 void FGATC::Render(string msg, string refname, bool repeating) {
223         #ifdef ENABLE_AUDIO_SUPPORT
224         _voice = (_voiceOK && fgGetBool("/sim/sound/voice"));
225         if(_voice) {
226                 int len;
227                 unsigned char* buf = _vPtr->WriteMessage((char*)msg.c_str(), len, _voice);
228                 if(_voice) {
229                         SGSoundSample *simple
230                             = new SGSoundSample(buf, len, 8000, false);
231                         // TODO - at the moment the volume is always set off comm1 
232                         // and can't be changed after the transmission has started.
233                         simple->set_volume(5.0 * fgGetDouble("/instrumentation/comm[0]/volume"));
234                         globals->get_soundmgr()->add(simple, refname);
235                         if(repeating) {
236                                 globals->get_soundmgr()->play_looped(refname);
237                         } else {
238                                 globals->get_soundmgr()->play_once(refname);
239                         }
240                 }
241                 delete[] buf;
242         }
243         #endif  // ENABLE_AUDIO_SUPPORT
244         if(!_voice) {
245                 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
246                 for(unsigned int i = 0; i < msg.length(); ++i) {
247                         if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
248                                 msg[i] = ' ';
249                         }
250                 }
251                 if(repeating) {
252                         globals->get_ATC_display()->RegisterRepeatingMessage(msg);
253                 } else {
254                         globals->get_ATC_display()->RegisterSingleMessage(msg);
255                 }
256         }
257         _playing = true;        
258 }
259
260
261 // Cease rendering a transmission.
262 void FGATC::NoRender(string refname) {
263         if(_playing) {
264                 if(_voice) {
265                         #ifdef ENABLE_AUDIO_SUPPORT             
266                         globals->get_soundmgr()->stop(refname);
267                         globals->get_soundmgr()->remove(refname);
268                         #endif
269                 } else {
270                         globals->get_ATC_display()->CancelRepeatingMessage();
271                 }
272                 _playing = false;
273         }
274 }
275
276 // Generate the text of a message from its parameters and the current context.
277 string FGATC::GenText(const string& m, int c) {
278         return("");
279 }
280
281 ostream& operator << (ostream& os, atc_type atc) {
282         switch(atc) {
283                 case(INVALID):    return(os << "INVALID");
284                 case(ATIS):       return(os << "ATIS");
285                 case(GROUND):     return(os << "GROUND");
286                 case(TOWER):      return(os << "TOWER");
287                 case(APPROACH):   return(os << "APPROACH");
288                 case(DEPARTURE):  return(os << "DEPARTURE");
289                 case(ENROUTE):    return(os << "ENROUTE");
290         }
291         return(os << "ERROR - Unknown switch in atc_type operator << ");
292 }