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