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 tuned_station->SetFreqInUse();
59 _transmitting = false;
61 if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
66 // timed out - don't render.
71 // Not tuned to ATC - Just go ahead and transmit
74 _transmitting = false;
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.
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");
85 _max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
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
92 // For now assume in range !!!
93 // TODO - implement range checking
94 Render(plane.callsign, false);
97 // Run the callback regardless of whether on same freq as user or not.
98 //cout << "_callback_code = " << _callback_code << '\n';
100 ProcessCallback(_callback_code);
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);
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));
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));
131 void FGAIPlane::Transmit(int callback_code) {
132 SG_LOG(SG_ATC, SG_INFO, "Transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
134 _callback_code = callback_code;
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);
141 _callback_code = callback_code;
145 void FGAIPlane::ImmediateTransmit(int callback_code) {
146 Render(plane.callsign, false);
148 ProcessCallback(callback_code);
152 // Derived classes should override this.
153 void FGAIPlane::ProcessCallback(int code) {
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"));
166 unsigned char* buf = vPtr->WriteMessage((char*)pending_transmission.c_str(), len, 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);
174 globals->get_soundmgr()->play_looped(refname);
176 globals->get_soundmgr()->play_once(refname);
181 #endif // ENABLE_AUDIO_SUPPORT
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] = ' ';
189 globals->get_ATC_display()->RegisterSingleMessage(pending_transmission, 0.0);
195 // Cease rendering a transmission.
196 void FGAIPlane::NoRender(string refname) {
199 #ifdef ENABLE_AUDIO_SUPPORT
200 globals->get_soundmgr()->stop(refname);
201 globals->get_soundmgr()->remove(refname);
204 globals->get_ATC_display()->CancelRepeatingMessage();
214 void FGAIPlane::RegisterTransmission(int code) {
218 // Return what type of landing we're doing on this circuit
219 LandingType FGAIPlane::GetLandingOption() {
224 ostream& operator << (ostream& os, PatternLeg 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");
239 return(os << "ERROR - Unknown switch in PatternLeg operator << ");
243 ostream& operator << (ostream& os, LandingType 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");
250 return(os << "ERROR - Unknown switch in LandingType operator << ");