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