1 // FGAIPlane - abstract base class for an AI plane
3 // Written by David Luff, started 2002.
5 // Copyright (C) 2002 David C. Luff - david.luff@nottingham.ac.uk
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.
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.
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.
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>
31 #include "AIPlane.hxx"
32 #include "ATCdisplay.hxx"
34 FGAIPlane::FGAIPlane() {
37 pending_transmission = "";
42 _transmitting = false;
49 FGAIPlane::~FGAIPlane() {
52 void FGAIPlane::Update(double dt) {
55 if(tuned_station->GetFreqClear()) {
56 //cout << "TUNED STATION FREQ CLEAR\n";
57 tuned_station->SetFreqInUse();
60 _transmitting = false;
62 if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
67 // timed out - don't render.
72 // Not tuned to ATC - Just go ahead and transmit
73 //cout << "NOT TUNED TO ATC\n";
76 _transmitting = false;
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.
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");
87 _max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
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
94 // For now assume in range !!!
95 // TODO - implement range checking
96 Render(plane.callsign, false);
99 // Run the callback regardless of whether on same freq as user or not.
100 //cout << "_callback_code = " << _callback_code << '\n';
102 ProcessCallback(_callback_code);
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);
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));
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));
133 void FGAIPlane::Transmit(int callback_code) {
134 SG_LOG(SG_ATC, SG_INFO, "Transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
136 _callback_code = callback_code;
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);
143 _callback_code = callback_code;
147 void FGAIPlane::ImmediateTransmit(int callback_code) {
148 Render(plane.callsign, false);
150 ProcessCallback(callback_code);
154 // Derived classes should override this.
155 void FGAIPlane::ProcessCallback(int code) {
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"));
168 unsigned char* buf = vPtr->WriteMessage((char*)pending_transmission.c_str(), len, 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);
176 globals->get_soundmgr()->play_looped(refname);
178 globals->get_soundmgr()->play_once(refname);
183 #endif // ENABLE_AUDIO_SUPPORT
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] = ' ';
191 globals->get_ATC_display()->RegisterSingleMessage(pending_transmission, 0.0);
197 // Cease rendering a transmission.
198 void FGAIPlane::NoRender(string refname) {
201 #ifdef ENABLE_AUDIO_SUPPORT
202 globals->get_soundmgr()->stop(refname);
203 globals->get_soundmgr()->remove(refname);
206 globals->get_ATC_display()->CancelRepeatingMessage();
216 void FGAIPlane::RegisterTransmission(int code) {
220 // Return what type of landing we're doing on this circuit
221 LandingType FGAIPlane::GetLandingOption() {
226 ostream& operator << (ostream& os, PatternLeg 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");
241 return(os << "ERROR - Unknown switch in PatternLeg operator << ");
245 ostream& operator << (ostream& os, LandingType 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");
252 return(os << "ERROR - Unknown switch in LandingType operator << ");