]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATC.cxx
ignore resets for now because every z/Z key press would trigger a call to NOAA. We...
[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         receiving = false;
35         respond = false;
36         runResponseCounter = false;
37         _runReleaseCounter = false;
38         responseID = "";
39         responseReqd = false;
40         _type = INVALID;
41 }
42
43 FGATC::~FGATC() {
44 }
45
46 // Derived classes wishing to use the response counter should call this from their own Update(...).
47 void FGATC::Update(double dt) {
48         if(runResponseCounter) {
49                 //cout << responseCounter << '\t' << responseTime << '\n';
50                 if(responseCounter >= responseTime) {
51                         runResponseCounter = false;
52                         respond = true;
53                         //cout << "RESPOND\n";
54                 } else {
55                         responseCounter += dt;
56                 }
57         }
58         
59         if(_runReleaseCounter) {
60                 if(_releaseCounter >= _releaseTime) {
61                         freqClear = true;
62                         _runReleaseCounter = false;
63                 } else {
64                         _releaseCounter += dt;
65                 }
66         }
67 }
68
69 void FGATC::ReceiveUserCallback(int code) {
70         SG_LOG(SG_ATC, SG_WARN, "WARNING - whichever ATC class was intended to receive callback code " << code << " didn't get it!!!");
71 }
72
73 void FGATC::SetResponseReqd(string rid) {
74         receiving = false;
75         responseReqd = true;
76         respond = false;        // TODO - this ignores the fact that more than one plane could call this before response
77                                                 // Shouldn't happen with AI only, but user could confuse things??
78         responseID = rid;
79         runResponseCounter = true;
80         responseCounter = 0.0;
81         responseTime = 1.8;             // TODO - randomize this slightly.
82 }
83
84 void FGATC::NotifyTransmissionFinished(string rid) {
85         //cout << "Transmission finished, callsign = " << rid << '\n';
86         receiving = false;
87         responseID = rid;
88         if(responseReqd) {
89                 runResponseCounter = true;
90                 responseCounter = 0.0;
91                 responseTime = 1.2;     // TODO - randomize this slightly, and allow it to be dependent on the transmission and how busy the ATC is.
92                 respond = false;        // TODO - this ignores the fact that more than one plane could call this before response
93                                                         // Shouldn't happen with AI only, but user could confuse things??
94         } else {
95                 freqClear = true;
96         }
97 }
98
99 void FGATC::AddPlane(string pid) {
100 }
101
102 int FGATC::RemovePlane() {
103         return 0;
104 }
105
106 void FGATC::SetDisplay() {
107 }
108
109 void FGATC::SetNoDisplay() {
110 }
111
112 void FGATC::SetData(ATCData* d) {
113         lon = d->lon;
114         lat = d->lat;
115         elev = d->elev;
116         x = d->x;
117         y = d->y;
118         z = d->z;
119         range = d->range;
120         ident = d->ident;
121         name = d->name;
122         freq = d->freq;
123 }
124
125 // Render a transmission
126 // Outputs the transmission either on screen or as audio depending on user preference
127 // The refname is a string to identify this sample to the sound manager
128 // The repeating flag indicates whether the message should be repeated continuously or played once.
129 void FGATC::Render(string msg, string refname, bool repeating) {
130         #ifdef ENABLE_AUDIO_SUPPORT
131         voice = (voiceOK && fgGetBool("/sim/sound/audible")
132         && fgGetBool("/sim/sound/voice"));
133         if(voice) {
134                 int len;
135                 unsigned char* buf = vPtr->WriteMessage((char*)msg.c_str(), len, voice);
136                 if(voice) {
137                         SGSimpleSound* simple = new SGSimpleSound(buf, len);
138                         // TODO - at the moment the volume is always set off comm1 
139                         // and can't be changed after the transmission has started.
140                         simple->set_volume(5.0 * fgGetDouble("/radios/comm[0]/volume"));
141                         globals->get_soundmgr()->add(simple, refname);
142                         if(repeating) {
143                                 globals->get_soundmgr()->play_looped(refname);
144                         } else {
145                                 globals->get_soundmgr()->play_once(refname);
146                         }
147                 }
148                 delete[] buf;
149         }
150         #endif  // ENABLE_AUDIO_SUPPORT
151         if(!voice) {
152                 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
153                 for(unsigned int i = 0; i < msg.length(); ++i) {
154                         if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
155                                 msg[i] = ' ';
156                         }
157                 }
158                 globals->get_ATC_display()->RegisterRepeatingMessage(msg);
159         }
160         playing = true; 
161 }
162
163
164 // Cease rendering a transmission.
165 void FGATC::NoRender(string refname) {
166         if(playing) {
167                 if(voice) {
168                         #ifdef ENABLE_AUDIO_SUPPORT             
169                         globals->get_soundmgr()->stop(refname);
170                         globals->get_soundmgr()->remove(refname);
171                         #endif
172                 } else {
173                         globals->get_ATC_display()->CancelRepeatingMessage();
174                 }
175                 playing = false;
176         }
177 }
178
179 // Generate the text of a message from its parameters and the current context.
180 string FGATC::GenText(const string& m, int c) {
181         return("");
182 }
183
184 ostream& operator << (ostream& os, atc_type atc) {
185         switch(atc) {
186                 case(INVALID):    return(os << "INVALID");
187                 case(ATIS):       return(os << "ATIS");
188                 case(GROUND):     return(os << "GROUND");
189                 case(TOWER):      return(os << "TOWER");
190                 case(APPROACH):   return(os << "APPROACH");
191                 case(DEPARTURE):  return(os << "DEPARTURE");
192                 case(ENROUTE):    return(os << "ENROUTE");
193         }
194         return(os << "ERROR - Unknown switch in atc_type operator << ");
195 }