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