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