]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCmgr.cxx
Remove legacy interactive tower and ground control
[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
28 #include <Airports/simple.hxx>
29
30 #include "ATCmgr.hxx"
31 #include "commlist.hxx"
32 #include "ATCDialog.hxx"
33 #include "ATCutils.hxx"
34
35
36 /*
37 // periodic radio station search wrapper
38 static void fgATCSearch( void ) {
39     globals->get_ATC_mgr()->Search();
40 }
41 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
42    // Is this still true?? -EMH-
43
44 AirportATC::AirportATC() :
45     atis_freq(0.0),
46     atis_active(false)
47     //airport_atc_map.clear();
48 {
49 }
50
51 FGATCMgr::FGATCMgr() :
52     initDone(false),
53     atc_list(new atc_list_type),
54 #ifdef ENABLE_AUDIO_SUPPORT
55     voiceOK(false),
56     voice(true),
57 #else
58     voice(false),
59 #endif
60     last_in_range(false),
61     v1(0)
62 {
63 }
64
65 FGATCMgr::~FGATCMgr() {
66     delete v1;
67 }
68
69 void FGATCMgr::bind() {
70 }
71
72 void FGATCMgr::unbind() {
73 }
74
75 void FGATCMgr::init() {
76     //cout << "ATCMgr::init called..." << endl;
77     
78     lon_node = fgGetNode("/position/longitude-deg", true);
79     lat_node = fgGetNode("/position/latitude-deg", true);
80     elev_node = fgGetNode("/position/altitude-ft", true);
81     atc_list_itr = atc_list->begin();
82     
83     // Search for connected ATC stations once per 0.8 seconds or so
84     // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
85         //                                 FGEvent::FG_EVENT_READY, 800);
86         //  
87     // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
88         // Is this still true after the reorganization of the event managar??
89         // -EMH-
90     
91     // Initialise the ATC Dialogs
92     SG_LOG(SG_ATC, SG_INFO, "  ATC Dialog System");
93     current_atcdialog = new FGATCDialog;
94     current_atcdialog->Init();
95
96     initDone = true;
97     //cout << "ATCmgr::init done!" << endl;
98 }
99
100 void FGATCMgr::update(double dt) {
101     if(!initDone) {
102         init();
103         SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
104     }
105     
106     current_atcdialog->Update(dt);
107     
108     //cout << "Entering update..." << endl;
109     //Traverse the list of active stations.
110     //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
111     //Eventually we should only update every so many steps.
112     //cout << "In FGATCMgr::update - atc_list.size = " << atc_list->size() << endl;
113     if(atc_list->size()) {
114         if(atc_list_itr == atc_list->end()) {
115             atc_list_itr = atc_list->begin();
116         }
117         //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
118         //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
119         //cout << "Updating...\n";
120         (*atc_list_itr).second->Update(dt * atc_list->size());
121         //cout << "Done ATC update..." << endl;
122         ++atc_list_itr;
123     }
124     
125 #ifdef ATC_TEST
126     //cout << "ATC_LIST: " << atc_list->size() << ' ';
127     for(atc_list_iterator it = atc_list->begin(); it != atc_list->end(); it++) {
128         cout << (*it)->get_ident() << ' ';
129     }
130     //cout << '\n';
131 #endif
132     
133     // Search the tuned frequencies every now and then - this should be done with the event scheduler
134     static int i = 0;   // Very ugly - but there should only ever be one instance of FGATCMgr.
135     if(i == 15) {
136         //cout << "About to search navcomm1" << endl;
137         FreqSearch("comm", 0);
138         FreqSearch("nav", 0);
139     }
140     if(i == 30) {
141         //cout << "About to search navcomm2" << endl;
142         FreqSearch("comm", 1);
143         FreqSearch("nav", 1);
144         i = 0;
145     }
146     ++i;
147     
148     //cout << "comm1 type = " << comm_type[0] << '\n';
149     //cout << "Leaving update..." << endl;
150 }
151
152
153 // Returns frequency in KHz - should I alter this to return in MHz?
154 unsigned short int FGATCMgr::GetFrequency(const string& ident, const atc_type& tp) {
155     ATCData test;
156     bool ok = current_commlist->FindByCode(ident, test, tp);
157     return(ok ? test.freq : 0);
158 }   
159
160 // Register the fact that the comm radio is tuned to an airport
161 // Channel is zero based
162 bool FGATCMgr::CommRegisterAirport(const string& ident, int chan, const atc_type& tp) {
163     SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
164     //cout << "Comm channel " << chan << " registered airport " << ident << ' ' << tp << '\n';
165     if(airport_atc_map.find(ident) != airport_atc_map.end()) {
166         if(tp == ATIS || tp == AWOS) {
167             airport_atc_map[ident]->atis_active = true;
168         }
169         return(true);
170     } else {
171         //cout << "NOT IN MAP - creating new..." << endl;
172         const FGAirport *ap = fgFindAirportID(ident);
173         if (ap) {
174             AirportATC *a = new AirportATC;
175             // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
176             a->geod = ap->geod();
177             a->atis_freq = GetFrequency(ident, ATIS)
178                     || GetFrequency(ident, AWOS);
179             a->atis_active = false;
180             if(tp == ATIS || tp == AWOS) {
181                 a->atis_active = true;
182             }
183             airport_atc_map[ident] = a;
184             return(true);
185         }
186     }
187     return(false);
188 }
189
190 typedef map<string,int> MSI;
191
192 void FGATCMgr::ZapOtherService(const string ncunit, const string svc_name){
193   for (atc_list_iterator svc = atc_list->begin(); svc != atc_list->end(); svc++) {
194    
195     if (svc->first != svc_name) {
196       MSI &actv = svc->second->active_on;
197       // OK, we have found some OTHER service;
198       // see if it is (was) active on our unit:
199       if (!actv.count(ncunit)) continue;
200       //cout << "Eradicating '" << svc->first << "' from: " << ncunit << endl;
201       actv.erase(ncunit);
202       if (!actv.size()) {
203         //cout << "Eradicating service: '" << svc->first << "'" << endl;
204         svc->second->SetNoDisplay();
205         svc->second->Update(0);     // one last update
206         SG_LOG(SG_GENERAL, SG_INFO, "would have erased ATC service:" << svc->second->get_name()<< "/"
207           << svc->second->get_ident());
208         // delete svc->second;
209         atc_list->erase(svc);
210         // ALL pointers into the ATC list are now invalid,
211         // so let's reset them:
212         atc_list_itr = atc_list->begin();
213       }
214       break;        // cannot be duplicates in the active list
215     }
216   }
217 }
218
219 // Find in list - return a currently active ATC pointer given ICAO code and type
220 // Return NULL if the given service is not in the list
221 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
222 FGATC* FGATCMgr::FindInList(const string& id, const atc_type& tp) {
223   string ndx = id + decimalNumeral(tp);
224   if (!atc_list->count(ndx)) return 0;
225   return (*atc_list)[ndx];
226 }
227
228 // Returns true if the airport is found in the map
229 bool FGATCMgr::GetAirportATCDetails(const string& icao, AirportATC* a) {
230     if(airport_atc_map.find(icao) != airport_atc_map.end()) {
231         *a = *airport_atc_map[icao];
232         return(true);
233     } else {
234         return(false);
235     }
236 }
237
238 // Return a pointer to an appropriate voice for a given type of ATC
239 // creating the voice if necessary - ie. make sure exactly one copy
240 // of every voice in use exists in memory.
241 //
242 // TODO - in the future this will get more complex and dole out country/airport
243 // specific voices, and possible make sure that the same voice doesn't get used
244 // at different airports in quick succession if a large enough selection are available.
245 FGATCVoice* FGATCMgr::GetVoicePointer(const atc_type& type) {
246     // TODO - implement me better - maintain a list of loaded voices and other voices!!
247     if(voice) {
248         switch(type) {
249         case ATIS: case AWOS:
250 #ifdef ENABLE_AUDIO_SUPPORT
251             // Delayed loading fo all available voices, needed because the
252             // soundmanager might not be initialized (at all) at this point.
253             // For now we'll do one hardwired one
254
255             /* I've loaded the voice even if /sim/sound/pause is true
256              *  since I know no way of forcing load of the voice if the user
257              *  subsequently switches /sim/sound/audible to true.
258              *  (which is the right thing to do -- CLO) :-)
259              */
260             if (!voiceOK && fgGetBool("/sim/sound/working")) {
261                 v1 = new FGATCVoice;
262                 try {
263                     voiceOK = v1->LoadVoice("default");
264                     voice = voiceOK;
265                 } catch ( sg_io_exception & e) {
266                     voiceOK  = false;
267                     SG_LOG(SG_ATC, SG_ALERT, "Unable to load default voice : "
268                                             << e.getFormattedMessage().c_str());
269                     voice = false;
270                     delete v1;
271                     v1 = 0;
272                 }
273             }
274 #endif
275             if(voiceOK) {
276                 return(v1);
277             }
278         case TOWER:
279             return(NULL);
280         case APPROACH:
281             return(NULL);
282         case GROUND:
283             return(NULL);
284         default:
285             return(NULL);
286         }
287         return(NULL);
288     } else {
289         return(NULL);
290     }
291 }
292
293 // Search for ATC stations by frequency
294 void FGATCMgr::FreqSearch(const string navcomm, const int unit) {
295
296
297     string ncunit = navcomm + "[" + decimalNumeral(unit) + "]";
298     string commbase = "/instrumentation/" + ncunit;
299     string commfreq = commbase + "/frequencies/selected-mhz";
300     SGPropertyNode_ptr comm_node = fgGetNode(commfreq.c_str(), false);
301
302     //cout << "FreqSearch: " << ncunit
303     //  << "  node: " << comm_node << endl;
304     if (!comm_node) return; // no such radio unit
305
306     ATCData data;   
307     double freq = comm_node->getDoubleValue();
308     _aircraftPos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
309         lat_node->getDoubleValue(), elev_node->getDoubleValue());
310     
311     // Query the data store and get the closest match if any
312     //cout << "Will FindByFreq: " << lat << " " << lon << " " << elev
313     //      << "  freq: " << freq << endl;
314     if(current_commlist->FindByFreq(_aircraftPos, freq, &data)) {
315         //cout << "FoundByFreq: " << freq
316         //  << "  ident: " << data.ident
317         //  << "  type: " << data.type << " ***" << endl;
318         // We are in range of something.
319
320
321         // Get rid of any *other* service that was on this radio unit:
322         string svc_name = data.ident+decimalNumeral(data.type);
323         ZapOtherService(ncunit, svc_name);
324         // See if the service already exists, possibly connected to
325         // some other radio unit:
326         if (atc_list->count(svc_name)) {
327             // make sure the service knows it's tuned on this radio:
328             FGATC* svc = (*atc_list)[svc_name];
329             svc->active_on[ncunit] = 1;
330             svc->SetDisplay();
331             return;
332         }
333
334         CommRegisterAirport(data.ident, unit, data.type);
335
336         // This was a switch-case statement but the compiler didn't like
337         // the new variable creation with it.
338         if(data.type == ATIS || data.type == AWOS) {
339             (*atc_list)[svc_name] = new FGATIS;
340             FGATC* svc = (*atc_list)[svc_name];
341             if(svc != NULL) {
342                 svc->SetData(&data);
343                 svc->active_on[ncunit] = 1;
344                 svc->SetDisplay();
345                 svc->Init();
346             }
347         }
348     } else {
349         // No services in range.  Zap any service on this unit.
350         ZapOtherService(ncunit, "x x x");
351     } 
352 }