]> git.mxchange.org Git - flightgear.git/blob - src/ATC/AIPlane.cxx
867bdcd054fceaed7f5b22a629199538eb7d654f
[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                 if(freq == user_freq0 || freq == user_freq1) {
96                         //cout << "Transmitting..." << endl;
97                         // we are on the same frequency, so check distance to the user plane
98                         if(1) {
99                                 // For now assume in range !!!
100                                 // TODO - implement range checking
101                                 Render(plane.callsign, false);
102                         }
103                 }
104                 // Run the callback regardless of whether on same freq as user or not.
105                 //cout << "_callback_code = " << _callback_code << '\n';
106                 if(_callback_code) {
107                         ProcessCallback(_callback_code);
108                 }
109                 _transmit = false;
110                 _transmitting = true;
111         } else if(_transmitting) {
112                 if(_counter >= _max_count) {
113                         NoRender(plane.callsign);
114                         _transmitting = false;
115                         // For now we'll let ATC decide whether to respond
116                         //if(tuned_station) tuned_station->SetResponseReqd(plane.callsign);
117                         if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
118                 }
119                 _counter += dt;
120         }
121         
122         // Fly the plane if necessary
123         if(_trackSet) {
124                 while((_tgtTrack - track) > 180.0) track += 360.0;
125                 while((track - _tgtTrack) > 180.0) track -= 360.0;
126                 double turn_time = 60.0;
127                 track += (360.0 / turn_time) * dt * (_tgtTrack > track ? 1.0 : -1.0);
128                 // TODO - bank a bit less for small turns.
129                 Bank(25.0 * (_tgtTrack > track ? 1.0 : -1.0));
130                 if(fabs(track - _tgtTrack) < 2.0) {             // TODO - might need to optimise the delta there - it's on the large (safe) side atm.
131                         track = _tgtTrack;
132                         LevelWings();
133                 }
134         }
135         
136         if(!_rollSuspended) {
137                 if(fabs(_roll - _tgtRoll) > 0.6) {
138                         // This *should* bank us smoothly to any angle
139                         _roll -= ((_roll - _tgtRoll)/fabs(_roll - _tgtRoll));
140                 } else {
141                         _roll = _tgtRoll;
142                 }
143         }
144 }
145
146 void FGAIPlane::Transmit(int callback_code) {
147         SG_LOG(SG_ATC, SG_INFO, "Transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
148         _pending = true;
149         _callback_code = callback_code;
150         _timeout = 0.0;
151 }
152
153 void FGAIPlane::ConditionalTransmit(double timeout, int callback_code) {
154         SG_LOG(SG_ATC, SG_INFO, "Timed transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
155         _pending = true;
156         _callback_code = callback_code;
157         _timeout = timeout;
158 }
159
160 void FGAIPlane::ImmediateTransmit(int callback_code) {
161         Render(plane.callsign, false);
162         if(callback_code) {
163                 ProcessCallback(callback_code);
164         }
165 }
166
167 // Derived classes should override this.
168 void FGAIPlane::ProcessCallback(int code) {
169 }
170
171 // Render a transmission
172 // Outputs the transmission either on screen or as audio depending on user preference
173 // The refname is a string to identify this sample to the sound manager
174 // The repeating flag indicates whether the message should be repeated continuously or played once.
175 void FGAIPlane::Render(string refname, bool repeating) {
176 #ifdef ENABLE_AUDIO_SUPPORT
177         voice = (voiceOK && fgGetBool("/sim/sound/audible")
178                  && fgGetBool("/sim/sound/voice"));
179         if(voice) {
180                 int len;
181                 unsigned char* buf = vPtr->WriteMessage((char*)pending_transmission.c_str(), len, voice);
182                 if(voice) {
183                         SGSimpleSound* simple = new SGSimpleSound(buf, len);
184                         // TODO - at the moment the volume is always set off comm1 
185                         // and can't be changed after the transmission has started.
186                         simple->set_volume(5.0 * fgGetDouble("/radios/comm[0]/volume"));
187                         globals->get_soundmgr()->add(simple, refname);
188                         if(repeating) {
189                                 globals->get_soundmgr()->play_looped(refname);
190                         } else {
191                                 globals->get_soundmgr()->play_once(refname);
192                         }
193                 }
194                 delete[] buf;
195         }
196 #endif  // ENABLE_AUDIO_SUPPORT
197         if(!voice) {
198                 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
199                 for(unsigned int i = 0; i < pending_transmission.length(); ++i) {
200                         if((pending_transmission.substr(i,1) == "_") || (pending_transmission.substr(i,1) == "/")) {
201                                 pending_transmission[i] = ' ';
202                         }
203                 }
204                 globals->get_ATC_display()->RegisterSingleMessage(pending_transmission, 0.0);
205         }
206         playing = true; 
207 }
208
209
210 // Cease rendering a transmission.
211 void FGAIPlane::NoRender(string refname) {
212         if(playing) {
213                 if(voice) {
214 #ifdef ENABLE_AUDIO_SUPPORT             
215                         globals->get_soundmgr()->stop(refname);
216                         globals->get_soundmgr()->remove(refname);
217 #endif
218                 } else {
219                         globals->get_ATC_display()->CancelRepeatingMessage();
220                 }
221                 playing = false;
222         }
223 }
224
225 /*
226
227 */
228
229 void FGAIPlane::RegisterTransmission(int code) {
230 }
231
232
233 // Return what type of landing we're doing on this circuit
234 LandingType FGAIPlane::GetLandingOption() {
235         return(FULL_STOP);
236 }
237
238
239 ostream& operator << (ostream& os, PatternLeg pl) {
240         switch(pl) {
241         case(TAKEOFF_ROLL):   return(os << "TAKEOFF ROLL");
242         case(CLIMBOUT):       return(os << "CLIMBOUT");
243         case(TURN1):          return(os << "TURN1");
244         case(CROSSWIND):      return(os << "CROSSWIND");
245         case(TURN2):          return(os << "TURN2");
246         case(DOWNWIND):       return(os << "DOWNWIND");
247         case(TURN3):          return(os << "TURN3");
248         case(BASE):           return(os << "BASE");
249         case(TURN4):          return(os << "TURN4");
250         case(FINAL):          return(os << "FINAL");
251         case(LANDING_ROLL):   return(os << "LANDING ROLL");
252         case(LEG_UNKNOWN):    return(os << "UNKNOWN");
253         }
254         return(os << "ERROR - Unknown switch in PatternLeg operator << ");
255 }
256
257
258 ostream& operator << (ostream& os, LandingType lt) {
259         switch(lt) {
260         case(FULL_STOP):      return(os << "FULL STOP");
261         case(STOP_AND_GO):    return(os << "STOP AND GO");
262         case(TOUCH_AND_GO):   return(os << "TOUCH AND GO");
263         case(AIP_LT_UNKNOWN): return(os << "UNKNOWN");
264         }
265         return(os << "ERROR - Unknown switch in LandingType operator << ");
266 }
267