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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <Main/globals.hxx>
22 #include <Main/fg_props.hxx>
23 #include <simgear/debug/logstream.hxx>
24 #include <simgear/sound/soundmgr_openal.hxx>
30 #include "AIPlane.hxx"
32 SGSampleGroup *FGAIPlane::_sgr = 0;
34 FGAIPlane::FGAIPlane() {
37 pending_transmission = "";
42 _transmitting = false;
51 _rollSuspended = false;
55 smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr");
56 _sgr = smgr->find("atc", true);
57 _sgr->tie_to_listener();
61 FGAIPlane::~FGAIPlane() {
64 void FGAIPlane::Update(double dt) {
67 if(tuned_station->GetFreqClear()) {
68 //cout << "TUNED STATION FREQ CLEAR\n";
69 tuned_station->SetFreqInUse();
72 _transmitting = false;
74 if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
79 // timed out - don't render.
80 if(_callback_code == 99) {
81 // MEGA-HACK - 99 is the remove self callback - currently this *does* need to be run even if the transmission isn't made.
82 ProcessCallback(_callback_code);
88 // Not tuned to ATC - Just go ahead and transmit
89 //cout << "NOT TUNED TO ATC\n";
92 _transmitting = false;
96 // This turns on rendering if on the same freq as the user
97 // TODO - turn it off if user switches to another freq - keep track of where in message we are etc.
99 //cout << "transmit\n";
100 double user_freq0 = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
101 double user_freq1 = fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
103 _max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
105 //cout << "Transmission = " << pending_transmission << '\n';
107 // The radios dialog seems to set slightly imprecise freqs, eg 118.099998
108 // The eplison stuff below is a work-around
109 double eps0 = fabs(freq - user_freq0);
110 double eps1 = fabs(freq - user_freq1);
111 if(eps0 < 0.002 || eps1 < 0.002) {
112 //cout << "Transmitting..." << endl;
113 // we are on the same frequency, so check distance to the user plane
115 // For now assume in range !!!
116 // TODO - implement range checking
117 // TODO - at the moment the volume is always set off comm1
118 float volume = fgGetFloat("/instrumentation/comm[0]/volume");
119 Render(plane.callsign, volume, false);
122 // Run the callback regardless of whether on same freq as user or not.
124 ProcessCallback(_callback_code);
127 _transmitting = true;
128 } else if(_transmitting) {
129 if(_counter >= _max_count) {
130 NoRender(plane.callsign);
131 _transmitting = false;
132 // For now we'll let ATC decide whether to respond
133 //if(tuned_station) tuned_station->SetResponseReqd(plane.callsign);
134 //if(tuned_station->get_ident() == "KRHV") cout << "Notifying transmission finished" << endl;
135 if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
140 // Fly the plane if necessary
142 while((_tgtTrack - track) > 180.0) track += 360.0;
143 while((track - _tgtTrack) > 180.0) track -= 360.0;
144 double turn_time = 60.0;
145 track += (360.0 / turn_time) * dt * (_tgtTrack > track ? 1.0 : -1.0);
146 // TODO - bank a bit less for small turns.
147 Bank(25.0 * (_tgtTrack > track ? 1.0 : -1.0));
148 if(fabs(track - _tgtTrack) < 2.0) { // TODO - might need to optimise the delta there - it's on the large (safe) side atm.
154 if(!_rollSuspended) {
155 if(fabs(_roll - _tgtRoll) > 0.6) {
156 // This *should* bank us smoothly to any angle
157 _roll -= ((_roll - _tgtRoll)/fabs(_roll - _tgtRoll));
164 void FGAIPlane::Transmit(int callback_code) {
165 SG_LOG(SG_ATC, SG_INFO, "Transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
167 _callback_code = callback_code;
171 void FGAIPlane::ConditionalTransmit(double timeout, int callback_code) {
172 SG_LOG(SG_ATC, SG_INFO, "Timed transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
174 _callback_code = callback_code;
178 void FGAIPlane::ImmediateTransmit(int callback_code) {
179 // TODO - at the moment the volume is always set off comm1
180 float volume = fgGetFloat("/instrumentation/comm[0]/volume");
182 Render(plane.callsign, volume, false);
184 ProcessCallback(callback_code);
188 // Derived classes should override this.
189 void FGAIPlane::ProcessCallback(int code) {
192 // Render a transmission
193 // Outputs the transmission either on screen or as audio depending on user preference
194 // The refname is a string to identify this sample to the sound manager
195 // The repeating flag indicates whether the message should be repeated continuously or played once.
196 void FGAIPlane::Render(const string& refname, const float volume, bool repeating) {
197 fgSetString("/sim/messages/ai-plane", pending_transmission.c_str());
198 #ifdef ENABLE_AUDIO_SUPPORT
199 voice = (voiceOK && fgGetBool("/sim/sound/voice"));
201 string buf = vPtr->WriteMessage((char*)pending_transmission.c_str(), voice);
202 if(voice && (volume > 0.05)) {
203 SGSoundSample* simple =
204 new SGSoundSample((unsigned char*)buf.c_str(), buf.length(), 8000 );
205 // TODO - at the moment the volume can't be changed
206 // after the transmission has started.
207 simple->set_volume(volume);
208 _sgr->add(simple, refname);
209 _sgr->play(refname, repeating);
212 #endif // ENABLE_AUDIO_SUPPORT
214 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
215 for(unsigned int i = 0; i < pending_transmission.length(); ++i) {
216 if((pending_transmission.substr(i,1) == "_") || (pending_transmission.substr(i,1) == "/")) {
217 pending_transmission[i] = ' ';
225 // Cease rendering a transmission.
226 void FGAIPlane::NoRender(const string& refname) {
229 #ifdef ENABLE_AUDIO_SUPPORT
231 _sgr->remove(refname);
242 void FGAIPlane::RegisterTransmission(int code) {
246 // Return what type of landing we're doing on this circuit
247 LandingType FGAIPlane::GetLandingOption() {
252 ostream& operator << (ostream& os, PatternLeg pl) {
254 case(TAKEOFF_ROLL): return(os << "TAKEOFF ROLL");
255 case(CLIMBOUT): return(os << "CLIMBOUT");
256 case(TURN1): return(os << "TURN1");
257 case(CROSSWIND): return(os << "CROSSWIND");
258 case(TURN2): return(os << "TURN2");
259 case(DOWNWIND): return(os << "DOWNWIND");
260 case(TURN3): return(os << "TURN3");
261 case(BASE): return(os << "BASE");
262 case(TURN4): return(os << "TURN4");
263 case(FINAL): return(os << "FINAL");
264 case(LANDING_ROLL): return(os << "LANDING ROLL");
265 case(LEG_UNKNOWN): return(os << "UNKNOWN");
267 return(os << "ERROR - Unknown switch in PatternLeg operator << ");
271 ostream& operator << (ostream& os, LandingType lt) {
273 case(FULL_STOP): return(os << "FULL STOP");
274 case(STOP_AND_GO): return(os << "STOP AND GO");
275 case(TOUCH_AND_GO): return(os << "TOUCH AND GO");
276 case(AIP_LT_UNKNOWN): return(os << "UNKNOWN");
278 return(os << "ERROR - Unknown switch in LandingType operator << ");