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