]> git.mxchange.org Git - flightgear.git/blob - src/ATC/AIPlane.cxx
Work around for the AI-traffic due to precision issues generated by the radio frequen...
[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., 675 Mass Ave, Cambridge, MA 02139, 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.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                                         }
74                                 }
75                         }
76                 } else {
77                         // Not tuned to ATC - Just go ahead and transmit
78                         //cout << "NOT TUNED TO ATC\n";
79                         _pending = false;
80                         _transmit = true;
81                         _transmitting = false;
82                 }
83         }
84         
85         // This turns on rendering if on the same freq as the user
86         // TODO - turn it off if user switches to another freq - keep track of where in message we are etc.
87         if(_transmit) {
88                 //cout << "transmit\n";
89                 double user_freq0 = fgGetDouble("/radios/comm[0]/frequencies/selected-mhz");
90                 double user_freq1 = fgGetDouble("/radios/comm[1]/frequencies/selected-mhz");
91                 _counter = 0.0;
92                 _max_count = 5.0;               // FIXME - hardwired length of message - need to calculate it!
93                 
94                 //cout << "Transmission = " << pending_transmission << '\n';
95
96                 // The radios dialog seems to set slightly imprecise freqs, eg 118.099998
97                 // The eplison stuff below is a work-around
98                 double eps0 = fabs(freq - user_freq0);
99                 double eps1 = fabs(freq - user_freq1);
100                 if(eps0 < 0.002 || eps1 < 0.002) {
101                         //cout << "Transmitting..." << endl;
102                         // we are on the same frequency, so check distance to the user plane
103                         if(1) {
104                                 // For now assume in range !!!
105                                 // TODO - implement range checking
106                                 Render(plane.callsign, false);
107                         }
108                 }
109                 // Run the callback regardless of whether on same freq as user or not.
110                 //cout << "_callback_code = " << _callback_code << '\n';
111                 if(_callback_code) {
112                         ProcessCallback(_callback_code);
113                 }
114                 _transmit = false;
115                 _transmitting = true;
116         } else if(_transmitting) {
117                 if(_counter >= _max_count) {
118                         NoRender(plane.callsign);
119                         _transmitting = false;
120                         // For now we'll let ATC decide whether to respond
121                         //if(tuned_station) tuned_station->SetResponseReqd(plane.callsign);
122                         if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
123                 }
124                 _counter += dt;
125         }
126         
127         // Fly the plane if necessary
128         if(_trackSet) {
129                 while((_tgtTrack - track) > 180.0) track += 360.0;
130                 while((track - _tgtTrack) > 180.0) track -= 360.0;
131                 double turn_time = 60.0;
132                 track += (360.0 / turn_time) * dt * (_tgtTrack > track ? 1.0 : -1.0);
133                 // TODO - bank a bit less for small turns.
134                 Bank(25.0 * (_tgtTrack > track ? 1.0 : -1.0));
135                 if(fabs(track - _tgtTrack) < 2.0) {             // TODO - might need to optimise the delta there - it's on the large (safe) side atm.
136                         track = _tgtTrack;
137                         LevelWings();
138                 }
139         }
140         
141         if(!_rollSuspended) {
142                 if(fabs(_roll - _tgtRoll) > 0.6) {
143                         // This *should* bank us smoothly to any angle
144                         _roll -= ((_roll - _tgtRoll)/fabs(_roll - _tgtRoll));
145                 } else {
146                         _roll = _tgtRoll;
147                 }
148         }
149 }
150
151 void FGAIPlane::Transmit(int callback_code) {
152         SG_LOG(SG_ATC, SG_INFO, "Transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
153         _pending = true;
154         _callback_code = callback_code;
155         _timeout = 0.0;
156 }
157
158 void FGAIPlane::ConditionalTransmit(double timeout, int callback_code) {
159         SG_LOG(SG_ATC, SG_INFO, "Timed transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
160         _pending = true;
161         _callback_code = callback_code;
162         _timeout = timeout;
163 }
164
165 void FGAIPlane::ImmediateTransmit(int callback_code) {
166         Render(plane.callsign, false);
167         if(callback_code) {
168                 ProcessCallback(callback_code);
169         }
170 }
171
172 // Derived classes should override this.
173 void FGAIPlane::ProcessCallback(int code) {
174 }
175
176 // Render a transmission
177 // Outputs the transmission either on screen or as audio depending on user preference
178 // The refname is a string to identify this sample to the sound manager
179 // The repeating flag indicates whether the message should be repeated continuously or played once.
180 void FGAIPlane::Render(string refname, bool repeating) {
181 #ifdef ENABLE_AUDIO_SUPPORT
182         voice = (voiceOK && fgGetBool("/sim/sound/audible")
183                  && fgGetBool("/sim/sound/voice"));
184         if(voice) {
185                 int len;
186                 unsigned char* buf = vPtr->WriteMessage((char*)pending_transmission.c_str(), len, voice);
187                 if(voice) {
188                         SGSimpleSound* simple = new SGSimpleSound(buf, len);
189                         // TODO - at the moment the volume is always set off comm1 
190                         // and can't be changed after the transmission has started.
191                         simple->set_volume(5.0 * fgGetDouble("/radios/comm[0]/volume"));
192                         globals->get_soundmgr()->add(simple, refname);
193                         if(repeating) {
194                                 globals->get_soundmgr()->play_looped(refname);
195                         } else {
196                                 globals->get_soundmgr()->play_once(refname);
197                         }
198                 }
199                 delete[] buf;
200         }
201 #endif  // ENABLE_AUDIO_SUPPORT
202         if(!voice) {
203                 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
204                 for(unsigned int i = 0; i < pending_transmission.length(); ++i) {
205                         if((pending_transmission.substr(i,1) == "_") || (pending_transmission.substr(i,1) == "/")) {
206                                 pending_transmission[i] = ' ';
207                         }
208                 }
209                 globals->get_ATC_display()->RegisterSingleMessage(pending_transmission, 0.0);
210         }
211         playing = true; 
212 }
213
214
215 // Cease rendering a transmission.
216 void FGAIPlane::NoRender(string refname) {
217         if(playing) {
218                 if(voice) {
219 #ifdef ENABLE_AUDIO_SUPPORT             
220                         globals->get_soundmgr()->stop(refname);
221                         globals->get_soundmgr()->remove(refname);
222 #endif
223                 } else {
224                         globals->get_ATC_display()->CancelRepeatingMessage();
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