]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCmgr.cxx
Merge branch 'next' of gitorious.org:fg/flightgear into next
[flightgear.git] / src / ATCDCL / ATCmgr.cxx
1 // ATCmgr.cxx - Implementation of FGATCMgr - a global Flightgear ATC manager.
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <simgear/misc/sg_path.hxx>
26 #include <simgear/debug/logstream.hxx>
27 #include <simgear/structure/exception.hxx>
28
29 #include <Airports/simple.hxx>
30 #include <ATC/CommStation.hxx>
31 #include <Main/fg_props.hxx>
32
33 #include "ATCmgr.hxx"
34 #include "ATCDialogOld.hxx"
35 #include "ATCutils.hxx"
36 #include "atis.hxx"
37
38 using flightgear::CommStation;
39
40 FGATCMgr::FGATCMgr() :
41     initDone(false),
42     atc_list(new atc_list_type),
43 #ifdef ENABLE_AUDIO_SUPPORT
44     voice(true),
45     voiceOK(false),
46     v1(0)
47 #else
48     voice(false),
49 #endif
50 {
51     globals->set_ATC_mgr(this);
52 }
53
54 FGATCMgr::~FGATCMgr() {
55     delete v1;
56 }
57
58 void FGATCMgr::bind() {
59 }
60
61 void FGATCMgr::unbind() {
62 }
63
64 void FGATCMgr::init() {
65     //cout << "ATCMgr::init called..." << endl;
66     
67     lon_node = fgGetNode("/position/longitude-deg", true);
68     lat_node = fgGetNode("/position/latitude-deg", true);
69     elev_node = fgGetNode("/position/altitude-ft", true);
70     atc_list_itr = atc_list->begin();
71     
72     // Search for connected ATC stations once per 0.8 seconds or so
73     // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
74         //                                 FGEvent::FG_EVENT_READY, 800);
75         //  
76     // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
77         // Is this still true after the reorganization of the event managar??
78         // -EMH-
79     
80     // Initialise the ATC Dialogs
81     SG_LOG(SG_ATC, SG_INFO, "  ATC Dialog System");
82     current_atcdialog = new FGATCDialog;
83     current_atcdialog->Init();
84
85     initDone = true;
86     //cout << "ATCmgr::init done!" << endl;
87 }
88
89 void FGATCMgr::update(double dt) {
90     if(!initDone) {
91         init();
92         SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
93     }
94     
95     current_atcdialog->Update(dt);
96     
97     //cout << "Entering update..." << endl;
98     //Traverse the list of active stations.
99     //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
100     //Eventually we should only update every so many steps.
101     //cout << "In FGATCMgr::update - atc_list.size = " << atc_list->size() << endl;
102     if(atc_list->size()) {
103         if(atc_list_itr == atc_list->end()) {
104             atc_list_itr = atc_list->begin();
105         }
106         //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
107         //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
108         //cout << "Updating...\n";
109         (*atc_list_itr).second->Update(dt * atc_list->size());
110         //cout << "Done ATC update..." << endl;
111         ++atc_list_itr;
112     }
113     
114 #ifdef ATC_TEST
115     //cout << "ATC_LIST: " << atc_list->size() << ' ';
116     for(atc_list_iterator it = atc_list->begin(); it != atc_list->end(); it++) {
117         cout << (*it)->get_ident() << ' ';
118     }
119     //cout << '\n';
120 #endif
121     
122     // Search the tuned frequencies every now and then - this should be done with the event scheduler
123     static int i = 0;   // Very ugly - but there should only ever be one instance of FGATCMgr.
124     if(i == 15) {
125         //cout << "About to search navcomm1" << endl;
126         FreqSearch("comm", 0);
127         FreqSearch("nav", 0);
128     }
129     if(i == 30) {
130         //cout << "About to search navcomm2" << endl;
131         FreqSearch("comm", 1);
132         FreqSearch("nav", 1);
133         i = 0;
134     }
135     ++i;
136     
137     //cout << "comm1 type = " << comm_type[0] << '\n';
138     //cout << "Leaving update..." << endl;
139 }
140
141 typedef map<string,int> MSI;
142
143 void FGATCMgr::ZapOtherService(const string ncunit, const string svc_name){
144   for (atc_list_iterator svc = atc_list->begin(); svc != atc_list->end(); svc++) {
145    
146     if (svc->first != svc_name) {
147       MSI &actv = svc->second->active_on;
148       // OK, we have found some OTHER service;
149       // see if it is (was) active on our unit:
150       if (!actv.count(ncunit)) continue;
151       //cout << "Eradicating '" << svc->first << "' from: " << ncunit << endl;
152       actv.erase(ncunit);
153       if (!actv.size()) {
154         //cout << "Eradicating service: '" << svc->first << "'" << endl;
155         svc->second->SetNoDisplay();
156         svc->second->Update(0);     // one last update
157         SG_LOG(SG_GENERAL, SG_INFO, "would have erased ATC service:" << svc->second->get_name()<< "/"
158           << svc->second->get_ident());
159         // delete svc->second;
160         atc_list->erase(svc);
161         // ALL pointers into the ATC list are now invalid,
162         // so let's reset them:
163         atc_list_itr = atc_list->begin();
164       }
165       break;        // cannot be duplicates in the active list
166     }
167   }
168 }
169
170 // Find in list - return a currently active ATC pointer given ICAO code and type
171 // Return NULL if the given service is not in the list
172 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
173 FGATC* FGATCMgr::FindInList(const string& id, const atc_type& tp) {
174   string ndx = id + decimalNumeral(tp);
175   if (!atc_list->count(ndx)) return 0;
176   return (*atc_list)[ndx];
177 }
178
179 // Return a pointer to an appropriate voice for a given type of ATC
180 // creating the voice if necessary - ie. make sure exactly one copy
181 // of every voice in use exists in memory.
182 //
183 // TODO - in the future this will get more complex and dole out country/airport
184 // specific voices, and possible make sure that the same voice doesn't get used
185 // at different airports in quick succession if a large enough selection are available.
186 FGATCVoice* FGATCMgr::GetVoicePointer(const atc_type& type) {
187     // TODO - implement me better - maintain a list of loaded voices and other voices!!
188     if(voice) {
189         switch(type) {
190         case ATIS: case AWOS:
191 #ifdef ENABLE_AUDIO_SUPPORT
192             // Delayed loading fo all available voices, needed because the
193             // soundmanager might not be initialized (at all) at this point.
194             // For now we'll do one hardwired one
195
196             /* I've loaded the voice even if /sim/sound/pause is true
197              *  since I know no way of forcing load of the voice if the user
198              *  subsequently switches /sim/sound/audible to true.
199              *  (which is the right thing to do -- CLO) :-)
200              */
201             if (!voiceOK && fgGetBool("/sim/sound/working")) {
202                 v1 = new FGATCVoice;
203                 try {
204                     voiceOK = v1->LoadVoice("default");
205                     voice = voiceOK;
206                 } catch ( sg_io_exception & e) {
207                     voiceOK  = false;
208                     SG_LOG(SG_ATC, SG_ALERT, "Unable to load default voice : "
209                                             << e.getFormattedMessage().c_str());
210                     voice = false;
211                     delete v1;
212                     v1 = 0;
213                 }
214             }
215 #endif
216             if(voiceOK) {
217                 return(v1);
218             }
219         case TOWER:
220             return(NULL);
221         case APPROACH:
222             return(NULL);
223         case GROUND:
224             return(NULL);
225         default:
226             return(NULL);
227         }
228         return(NULL);
229     } else {
230         return(NULL);
231     }
232 }
233
234 // Search for ATC stations by frequency
235 void FGATCMgr::FreqSearch(const string navcomm, const int unit) {
236
237
238     string ncunit = navcomm + "[" + decimalNumeral(unit) + "]";
239     string commbase = "/instrumentation/" + ncunit;
240     string commfreq = commbase + "/frequencies/selected-mhz";
241     SGPropertyNode_ptr comm_node = fgGetNode(commfreq.c_str(), false);
242
243     //cout << "FreqSearch: " << ncunit
244     //  << "  node: " << comm_node << endl;
245     if (!comm_node) return; // no such radio unit
246
247     ATCData data;   
248     // Note:  122.375 must be rounded DOWN to 12237 
249     // in order to be consistent with apt.dat et cetera.
250     int freqKhz = static_cast<int>(comm_node->getDoubleValue() * 100.0 + 0.25);
251     
252     _aircraftPos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
253         lat_node->getDoubleValue(), elev_node->getDoubleValue());
254
255     class RangeFilter : public CommStation::Filter {
256     public:
257         RangeFilter( const SGGeod & pos ) : 
258           CommStation::Filter(), 
259           _cart(SGVec3d::fromGeod(pos)),
260           _pos(pos)
261         {}
262       
263         virtual bool pass(FGPositioned* aPos) const
264         {
265             flightgear::CommStation * stn = dynamic_cast<flightgear::CommStation*>(aPos);
266             if( NULL == stn ) return false;
267           // do the range check in cartesian space, since the distances are potentially
268           // large enough that the geodetic functions become unstable
269           // (eg, station on opposite side of the planet)
270             double rangeM = SGMiscd::max( stn->rangeNm(), 10.0 ) * SG_NM_TO_METER;
271             double d2 = distSqr( aPos->cart(), _cart);
272           
273             return d2 <= (rangeM * rangeM);
274         }
275     private:
276         SGVec3d _cart;
277         SGGeod _pos;
278     };
279
280     RangeFilter rangeFilter(_aircraftPos );
281     
282     CommStation* sta = CommStation::findByFreq(freqKhz, _aircraftPos, &rangeFilter );
283     if (!sta) {
284         ZapOtherService(ncunit, "x x x");
285         return;
286     }
287     
288     // Get rid of any *other* service that was on this radio unit:
289     FGPositioned::Type ty = sta->type();
290     string svc_name = sta->ident() + FGPositioned::nameForType(ty);
291     ZapOtherService(ncunit, svc_name);
292     // See if the service already exists, possibly connected to
293     // some other radio unit:
294     if (atc_list->count(svc_name)) {
295         // make sure the service knows it's tuned on this radio:
296         FGATC* svc = (*atc_list)[svc_name];
297         svc->active_on[ncunit] = 1;
298         svc->SetDisplay();
299         return;
300     }
301
302     // This was a switch-case statement but the compiler didn't like
303     // the new variable creation with it.
304     if(ty == FGPositioned::FREQ_ATIS || ty == FGPositioned::FREQ_AWOS) {
305         (*atc_list)[svc_name] = new FGATIS;
306         FGATC* svc = (*atc_list)[svc_name];
307         if(svc != NULL) {
308             svc->SetStation(sta);
309             svc->active_on[ncunit] = 1;
310             svc->SetDisplay();
311             svc->Init();
312         }
313     }
314
315 }