]> git.mxchange.org Git - flightgear.git/blob - src/ATC/AIPlane.cxx
Various mods to allow querying for nearest airport (with optional ability to
[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 }
48
49 FGAIPlane::~FGAIPlane() {
50 }
51
52 void FGAIPlane::Update(double dt) {
53         if(_pending) {
54                 if(tuned_station) {
55                         if(tuned_station->GetFreqClear()) {
56                                 //cout << "TUNED STATION FREQ CLEAR\n";
57                                 tuned_station->SetFreqInUse();
58                                 _pending = false;
59                                 _transmit = true;
60                                 _transmitting = false;
61                         } else {
62                                 if(_timeout > 0.0) {    // allows count down to be avoided by initially setting it to zero
63                                         _timeout -= dt;
64                                         if(_timeout <= 0.0) {
65                                                 _timeout = 0.0;
66                                                 _pending = false;
67                                                 // timed out - don't render.
68                                         }
69                                 }
70                         }
71                 } else {
72                         // Not tuned to ATC - Just go ahead and transmit
73                         //cout << "NOT TUNED TO ATC\n";
74                         _pending = false;
75                         _transmit = true;
76                         _transmitting = false;
77                 }
78         }
79         
80         // This turns on rendering if on the same freq as the user
81         // TODO - turn it off if user switches to another freq - keep track of where in message we are etc.
82         if(_transmit) {
83                 //cout << "transmit\n";
84                 double user_freq0 = fgGetDouble("/radios/comm[0]/frequencies/selected-mhz");
85                 double user_freq1 = fgGetDouble("/radios/comm[1]/frequencies/selected-mhz");
86                 _counter = 0.0;
87                 _max_count = 5.0;               // FIXME - hardwired length of message - need to calculate it!
88                 
89                 //cout << "Transmission = " << pending_transmission << '\n';
90                 if(freq == user_freq0 || freq == user_freq1) {
91                         //cout << "Transmitting..." << endl;
92                         // we are on the same frequency, so check distance to the user plane
93                         if(1) {
94                                 // For now assume in range !!!
95                                 // TODO - implement range checking
96                                 Render(plane.callsign, false);
97                         }
98                 }
99                 // Run the callback regardless of whether on same freq as user or not.
100                 //cout << "_callback_code = " << _callback_code << '\n';
101                 if(_callback_code) {
102                         ProcessCallback(_callback_code);
103                 }
104                 _transmit = false;
105                 _transmitting = true;
106         } else if(_transmitting) {
107                 if(_counter >= _max_count) {
108                         NoRender(plane.callsign);
109                         _transmitting = false;
110                         // For now we'll let ATC decide whether to respond
111                         //if(tuned_station) tuned_station->SetResponseReqd(plane.callsign);
112                         if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
113                 }
114                 _counter += dt;
115         }
116 }
117
118 void FGAIPlane::Bank(double angle) {
119         // This *should* bank us smoothly to any angle
120         if(fabs(_roll - angle) > 0.6) {
121                 _roll -= ((_roll - angle)/fabs(_roll - angle));  
122         }
123 }
124
125 // Duplication of Bank(0.0) really - should I cut this?
126 void FGAIPlane::LevelWings(void) {
127         // bring the plane back to level smoothly (this should work to come out of either bank)
128         if(fabs(_roll) > 0.6) {
129                 _roll -= (_roll/fabs(_roll));
130         }
131 }
132
133 void FGAIPlane::Transmit(int callback_code) {
134         SG_LOG(SG_ATC, SG_INFO, "Transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
135         _pending = true;
136         _callback_code = callback_code;
137         _timeout = 0.0;
138 }
139
140 void FGAIPlane::ConditionalTransmit(double timeout, int callback_code) {
141         SG_LOG(SG_ATC, SG_INFO, "Timed transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
142         _pending = true;
143         _callback_code = callback_code;
144         _timeout = timeout;
145 }
146
147 void FGAIPlane::ImmediateTransmit(int callback_code) {
148         Render(plane.callsign, false);
149         if(callback_code) {
150                 ProcessCallback(callback_code);
151         }
152 }
153
154 // Derived classes should override this.
155 void FGAIPlane::ProcessCallback(int code) {
156 }
157
158 // Render a transmission
159 // Outputs the transmission either on screen or as audio depending on user preference
160 // The refname is a string to identify this sample to the sound manager
161 // The repeating flag indicates whether the message should be repeated continuously or played once.
162 void FGAIPlane::Render(string refname, bool repeating) {
163 #ifdef ENABLE_AUDIO_SUPPORT
164         voice = (voiceOK && fgGetBool("/sim/sound/audible")
165                  && fgGetBool("/sim/sound/voice"));
166         if(voice) {
167                 int len;
168                 unsigned char* buf = vPtr->WriteMessage((char*)pending_transmission.c_str(), len, voice);
169                 if(voice) {
170                         SGSimpleSound* simple = new SGSimpleSound(buf, len);
171                         // TODO - at the moment the volume is always set off comm1 
172                         // and can't be changed after the transmission has started.
173                         simple->set_volume(5.0 * fgGetDouble("/radios/comm[0]/volume"));
174                         globals->get_soundmgr()->add(simple, refname);
175                         if(repeating) {
176                                 globals->get_soundmgr()->play_looped(refname);
177                         } else {
178                                 globals->get_soundmgr()->play_once(refname);
179                         }
180                 }
181                 delete[] buf;
182         }
183 #endif  // ENABLE_AUDIO_SUPPORT
184         if(!voice) {
185                 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
186                 for(unsigned int i = 0; i < pending_transmission.length(); ++i) {
187                         if((pending_transmission.substr(i,1) == "_") || (pending_transmission.substr(i,1) == "/")) {
188                                 pending_transmission[i] = ' ';
189                         }
190                 }
191                 globals->get_ATC_display()->RegisterSingleMessage(pending_transmission, 0.0);
192         }
193         playing = true; 
194 }
195
196
197 // Cease rendering a transmission.
198 void FGAIPlane::NoRender(string refname) {
199         if(playing) {
200                 if(voice) {
201 #ifdef ENABLE_AUDIO_SUPPORT             
202                         globals->get_soundmgr()->stop(refname);
203                         globals->get_soundmgr()->remove(refname);
204 #endif
205                 } else {
206                         globals->get_ATC_display()->CancelRepeatingMessage();
207                 }
208                 playing = false;
209         }
210 }
211
212 /*
213
214 */
215
216 void FGAIPlane::RegisterTransmission(int code) {
217 }
218
219
220 // Return what type of landing we're doing on this circuit
221 LandingType FGAIPlane::GetLandingOption() {
222         return(FULL_STOP);
223 }
224
225
226 ostream& operator << (ostream& os, PatternLeg pl) {
227         switch(pl) {
228         case(TAKEOFF_ROLL):   return(os << "TAKEOFF ROLL");
229         case(CLIMBOUT):       return(os << "CLIMBOUT");
230         case(TURN1):          return(os << "TURN1");
231         case(CROSSWIND):      return(os << "CROSSWIND");
232         case(TURN2):          return(os << "TURN2");
233         case(DOWNWIND):       return(os << "DOWNWIND");
234         case(TURN3):          return(os << "TURN3");
235         case(BASE):           return(os << "BASE");
236         case(TURN4):          return(os << "TURN4");
237         case(FINAL):          return(os << "FINAL");
238         case(LANDING_ROLL):   return(os << "LANDING ROLL");
239         case(LEG_UNKNOWN):    return(os << "UNKNOWN");
240         }
241         return(os << "ERROR - Unknown switch in PatternLeg operator << ");
242 }
243
244
245 ostream& operator << (ostream& os, LandingType lt) {
246         switch(lt) {
247         case(FULL_STOP):      return(os << "FULL STOP");
248         case(STOP_AND_GO):    return(os << "STOP AND GO");
249         case(TOUCH_AND_GO):   return(os << "TOUCH AND GO");
250         case(AIP_LT_UNKNOWN): return(os << "UNKNOWN");
251         }
252         return(os << "ERROR - Unknown switch in LandingType operator << ");
253 }
254