]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/AIPlane.cxx
Add the Sound Manager before any other subsystem that uses it. This makes sure the...
[flightgear.git] / src / ATCDCL / AIPlane.cxx
1 // FGAIPlane - abstract base class for an AI plane
2 //
3 // Written by David Luff, started 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 #include <Main/globals.hxx>
22 #include <Main/fg_props.hxx>
23 #include <simgear/debug/logstream.hxx>
24 #include <simgear/sound/soundmgr_openal.hxx>
25 #include <math.h>
26 #include <string>
27 using std::string;
28
29
30 #include "AIPlane.hxx"
31
32 SGSampleGroup *FGAIPlane::_sgr = 0;
33
34 FGAIPlane::FGAIPlane() {
35         leg = LEG_UNKNOWN;
36         tuned_station = NULL;
37         pending_transmission = "";
38         _timeout = 0;
39         _pending = false;
40         _callback_code = 0;
41         _transmit = false;
42         _transmitting = false;
43         voice = false;
44         playing = false;
45         voiceOK = false;
46         vPtr = NULL;
47         track = 0.0;
48         _tgtTrack = 0.0;
49         _trackSet = false;
50         _tgtRoll = 0.0;
51         _rollSuspended = false;
52
53         if ( !_sgr ) {
54                 SGSoundMgr *smgr;
55                 smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr");
56                 _sgr = smgr->find("atc", true);
57         }
58 }
59
60 FGAIPlane::~FGAIPlane() {
61 }
62
63 void FGAIPlane::Update(double dt) {
64         if(_pending) {
65                 if(tuned_station) {
66                         if(tuned_station->GetFreqClear()) {
67                                 //cout << "TUNED STATION FREQ CLEAR\n";
68                                 tuned_station->SetFreqInUse();
69                                 _pending = false;
70                                 _transmit = true;
71                                 _transmitting = false;
72                         } else {
73                                 if(_timeout > 0.0) {    // allows count down to be avoided by initially setting it to zero
74                                         _timeout -= dt;
75                                         if(_timeout <= 0.0) {
76                                                 _timeout = 0.0;
77                                                 _pending = false;
78                                                 // timed out - don't render.
79                                                 if(_callback_code == 99) {
80                                                         // MEGA-HACK - 99 is the remove self callback - currently this *does* need to be run even if the transmission isn't made.
81                                                         ProcessCallback(_callback_code);
82                                                 }
83                                         }
84                                 }
85                         }
86                 } else {
87                         // Not tuned to ATC - Just go ahead and transmit
88                         //cout << "NOT TUNED TO ATC\n";
89                         _pending = false;
90                         _transmit = true;
91                         _transmitting = false;
92                 }
93         }
94         
95         // This turns on rendering if on the same freq as the user
96         // TODO - turn it off if user switches to another freq - keep track of where in message we are etc.
97         if(_transmit) {
98                 //cout << "transmit\n";
99                 double user_freq0 = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
100                 double user_freq1 = fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
101                 _counter = 0.0;
102                 _max_count = 5.0;               // FIXME - hardwired length of message - need to calculate it!
103                 
104                 //cout << "Transmission = " << pending_transmission << '\n';
105
106                 // The radios dialog seems to set slightly imprecise freqs, eg 118.099998
107                 // The eplison stuff below is a work-around
108                 double eps0 = fabs(freq - user_freq0);
109                 double eps1 = fabs(freq - user_freq1);
110                 if(eps0 < 0.002 || eps1 < 0.002) {
111                         //cout << "Transmitting..." << endl;
112                         // we are on the same frequency, so check distance to the user plane
113                         if(1) {
114                                 // For now assume in range !!!
115                                 // TODO - implement range checking
116                                 // TODO - at the moment the volume is always set off comm1 
117                                 float volume = fgGetFloat("/instrumentation/comm[0]/volume");
118                                 Render(plane.callsign, volume, false);
119                         }
120                 }
121                 // Run the callback regardless of whether on same freq as user or not.
122                 if(_callback_code) {
123                         ProcessCallback(_callback_code);
124                 }
125                 _transmit = false;
126                 _transmitting = true;
127         } else if(_transmitting) {
128                 if(_counter >= _max_count) {
129                         NoRender(plane.callsign);
130                         _transmitting = false;
131                         // For now we'll let ATC decide whether to respond
132                         //if(tuned_station) tuned_station->SetResponseReqd(plane.callsign);
133                         //if(tuned_station->get_ident() == "KRHV") cout << "Notifying transmission finished" << endl;
134                         if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
135                 }
136                 _counter += dt;
137         }
138         
139         // Fly the plane if necessary
140         if(_trackSet) {
141                 while((_tgtTrack - track) > 180.0) track += 360.0;
142                 while((track - _tgtTrack) > 180.0) track -= 360.0;
143                 double turn_time = 60.0;
144                 track += (360.0 / turn_time) * dt * (_tgtTrack > track ? 1.0 : -1.0);
145                 // TODO - bank a bit less for small turns.
146                 Bank(25.0 * (_tgtTrack > track ? 1.0 : -1.0));
147                 if(fabs(track - _tgtTrack) < 2.0) {             // TODO - might need to optimise the delta there - it's on the large (safe) side atm.
148                         track = _tgtTrack;
149                         LevelWings();
150                 }
151         }
152         
153         if(!_rollSuspended) {
154                 if(fabs(_roll - _tgtRoll) > 0.6) {
155                         // This *should* bank us smoothly to any angle
156                         _roll -= ((_roll - _tgtRoll)/fabs(_roll - _tgtRoll));
157                 } else {
158                         _roll = _tgtRoll;
159                 }
160         }
161 }
162
163 void FGAIPlane::Transmit(int callback_code) {
164         SG_LOG(SG_ATC, SG_INFO, "Transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
165         _pending = true;
166         _callback_code = callback_code;
167         _timeout = 0.0;
168 }
169
170 void FGAIPlane::ConditionalTransmit(double timeout, int callback_code) {
171         SG_LOG(SG_ATC, SG_INFO, "Timed transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
172         _pending = true;
173         _callback_code = callback_code;
174         _timeout = timeout;
175 }
176
177 void FGAIPlane::ImmediateTransmit(int callback_code) {
178         // TODO - at the moment the volume is always set off comm1 
179         float volume = fgGetFloat("/instrumentation/comm[0]/volume");
180
181         Render(plane.callsign, volume, false);
182         if(callback_code) {
183                 ProcessCallback(callback_code);
184         }
185 }
186
187 // Derived classes should override this.
188 void FGAIPlane::ProcessCallback(int code) {
189 }
190
191 // Render a transmission
192 // Outputs the transmission either on screen or as audio depending on user preference
193 // The refname is a string to identify this sample to the sound manager
194 // The repeating flag indicates whether the message should be repeated continuously or played once.
195 void FGAIPlane::Render(const string& refname, const float volume, bool repeating) {
196         fgSetString("/sim/messages/ai-plane", pending_transmission.c_str());
197 #ifdef ENABLE_AUDIO_SUPPORT
198         voice = (voiceOK && fgGetBool("/sim/sound/voice"));
199         if(voice) {
200             string buf = vPtr->WriteMessage((char*)pending_transmission.c_str(), voice);
201             if(voice && (volume > 0.05)) {
202                 SGSoundSample* simple = 
203                     new SGSoundSample((unsigned char*)buf.c_str(), buf.length(), 8000 );
204                 // TODO - at the moment the volume can't be changed 
205                 // after the transmission has started.
206                 simple->set_volume(volume);
207                 _sgr->add(simple, refname);
208                 _sgr->play(refname, repeating);
209             }
210         }
211 #endif  // ENABLE_AUDIO_SUPPORT
212         if(!voice) {
213                 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
214                 for(unsigned int i = 0; i < pending_transmission.length(); ++i) {
215                         if((pending_transmission.substr(i,1) == "_") || (pending_transmission.substr(i,1) == "/")) {
216                                 pending_transmission[i] = ' ';
217                         }
218                 }
219         }
220         playing = true; 
221 }
222
223
224 // Cease rendering a transmission.
225 void FGAIPlane::NoRender(const string& refname) {
226         if(playing) {
227                 if(voice) {
228 #ifdef ENABLE_AUDIO_SUPPORT             
229                         _sgr->stop(refname);
230                         _sgr->remove(refname);
231 #endif
232                 }
233                 playing = false;
234         }
235 }
236
237 /*
238
239 */
240
241 void FGAIPlane::RegisterTransmission(int code) {
242 }
243
244
245 // Return what type of landing we're doing on this circuit
246 LandingType FGAIPlane::GetLandingOption() {
247         return(FULL_STOP);
248 }
249
250
251 ostream& operator << (ostream& os, PatternLeg pl) {
252         switch(pl) {
253         case(TAKEOFF_ROLL):   return(os << "TAKEOFF ROLL");
254         case(CLIMBOUT):       return(os << "CLIMBOUT");
255         case(TURN1):          return(os << "TURN1");
256         case(CROSSWIND):      return(os << "CROSSWIND");
257         case(TURN2):          return(os << "TURN2");
258         case(DOWNWIND):       return(os << "DOWNWIND");
259         case(TURN3):          return(os << "TURN3");
260         case(BASE):           return(os << "BASE");
261         case(TURN4):          return(os << "TURN4");
262         case(FINAL):          return(os << "FINAL");
263         case(LANDING_ROLL):   return(os << "LANDING ROLL");
264         case(LEG_UNKNOWN):    return(os << "UNKNOWN");
265         }
266         return(os << "ERROR - Unknown switch in PatternLeg operator << ");
267 }
268
269
270 ostream& operator << (ostream& os, LandingType lt) {
271         switch(lt) {
272         case(FULL_STOP):      return(os << "FULL STOP");
273         case(STOP_AND_GO):    return(os << "STOP AND GO");
274         case(TOUCH_AND_GO):   return(os << "TOUCH AND GO");
275         case(AIP_LT_UNKNOWN): return(os << "UNKNOWN");
276         }
277         return(os << "ERROR - Unknown switch in LandingType operator << ");
278 }
279