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