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