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