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