]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATC.cxx
Further progress towards AI/ATC dialog
[flightgear.git] / src / ATC / ATC.cxx
1 // Implementation of FGATC - ATC subsystem base class.
2 //
3 // Written by David Luff, started February 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 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <simgear/sound/soundmgr.hxx>
26
27 #include <Main/fg_props.hxx>
28
29 #include "ATC.hxx"
30 #include "ATCdisplay.hxx"
31
32 FGATC::FGATC() {
33         freqClear = true;
34 }
35
36 FGATC::~FGATC() {
37 }
38
39 // Derived classes wishing to use the response counter should call this from their own Update(...).
40 void FGATC::Update(double dt) {
41         if(responseReqd) {
42                 if(responseCounter >= responseTime) {
43                         responseReqd = false;
44                         respond = true;
45                 } else {
46                         responseCounter += dt;
47                 }
48         }
49 }
50
51 void FGATC::SetResponseReqd(string rid) {
52         responseReqd = true;
53         respond = false;        // TODO - this ignores the fact that more than one plane could call this before response
54                                                 // Shouldn't happen with AI only, but user could confuse things??
55         responseID = rid;
56         responseCounter = 0.0;
57         responseTime = 2.5;             // TODO - randomize this slightly.
58 }
59
60 void FGATC::AddPlane(string pid) {
61 }
62
63 int FGATC::RemovePlane() {
64         return 0;
65 }
66
67 void FGATC::SetDisplay() {
68 }
69
70 void FGATC::SetNoDisplay() {
71 }
72
73 atc_type FGATC::GetType() {
74         return INVALID;
75 }
76
77 void FGATC::SetData(ATCData* d) {
78         lon = d->lon;
79         lat = d->lat;
80         elev = d->elev;
81         x = d->x;
82         y = d->y;
83         z = d->z;
84         range = d->range;
85         ident = d->ident;
86         name = d->name;
87         freq = d->freq;
88 }
89
90 // Render a transmission
91 // Outputs the transmission either on screen or as audio depending on user preference
92 // The refname is a string to identify this sample to the sound manager
93 // The repeating flag indicates whether the message should be repeated continuously or played once.
94 void FGATC::Render(string msg, string refname, bool repeating) {
95         #ifdef ENABLE_AUDIO_SUPPORT
96         voice = (voiceOK && fgGetBool("/sim/sound/audible")
97         && fgGetBool("/sim/sound/voice"));
98         if(voice) {
99                 int len;
100                 unsigned char* buf = vPtr->WriteMessage((char*)msg.c_str(), len, voice);
101                 if(voice) {
102                         SGSimpleSound* simple = new SGSimpleSound(buf, len);
103                         // TODO - at the moment the volume is always set off comm1 
104                         // and can't be changed after the transmission has started.
105                         simple->set_volume(5.0 * fgGetDouble("/radios/comm[0]/volume"));
106                         globals->get_soundmgr()->add(simple, refname);
107                         if(repeating) {
108                                 globals->get_soundmgr()->play_looped(refname);
109                         } else {
110                                 globals->get_soundmgr()->play_once(refname);
111                         }
112                 }
113                 delete[] buf;
114         }
115         #endif  // ENABLE_AUDIO_SUPPORT
116         if(!voice) {
117                 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
118                 for(unsigned int i = 0; i < msg.length(); ++i) {
119                         if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
120                                 msg[i] = ' ';
121                         }
122                 }
123                 globals->get_ATC_display()->RegisterRepeatingMessage(msg);
124         }
125         playing = true; 
126 }
127
128
129 // Cease rendering a transmission.
130 void FGATC::NoRender(string refname) {
131         if(playing) {
132                 if(voice) {
133                         #ifdef ENABLE_AUDIO_SUPPORT             
134                         globals->get_soundmgr()->stop(refname);
135                         globals->get_soundmgr()->remove(refname);
136                         #endif
137                 } else {
138                         globals->get_ATC_display()->CancelRepeatingMessage();
139                 }
140                 playing = false;
141         }
142 }
143
144 ostream& operator << (ostream& os, atc_type atc) {
145         switch(atc) {
146                 case(INVALID):    return(os << "INVALID");
147                 case(ATIS):       return(os << "ATIS");
148                 case(GROUND):     return(os << "GROUND");
149                 case(TOWER):      return(os << "TOWER");
150                 case(APPROACH):   return(os << "APPROACH");
151                 case(DEPARTURE):  return(os << "DEPARTURE");
152                 case(ENROUTE):    return(os << "ENROUTE");
153         }
154         return(os << "ERROR - Unknown switch in atc_type operator << ");
155 }