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