]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCmgr.cxx
Rationalisation of the ATC popup dialog code - moved it all over to AK's dynamic...
[flightgear.git] / src / ATC / 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., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 #include <simgear/misc/sg_path.hxx>
22 #include <simgear/debug/logstream.hxx>
23 #include <Airports/simple.hxx>
24
25 #include "ATCmgr.hxx"
26 #include "commlist.hxx"
27 #include "ATCdisplay.hxx"
28 #include "ATCDialog.hxx"
29 #include "ATCutils.hxx"
30
31 /*
32 // periodic radio station search wrapper
33 static void fgATCSearch( void ) {
34         globals->get_ATC_mgr()->Search();
35 }
36 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
37    // Is this still true?? -EMH-
38
39 AirportATC::AirportATC() :
40     lon(0.0),
41     lat(0.0),
42     elev(0.0),
43     atis_freq(0.0),
44     atis_active(false),
45     tower_freq(0.0),
46     tower_active(false),
47     ground_freq(0.0),
48     ground_active(false),
49     set_by_AI(false),
50         numAI(0)
51 {
52         for(int i=0; i<ATC_NUM_TYPES; ++i) {
53                 set_by_comm[0][i] = false;
54                 set_by_comm[1][i] = false;
55         }
56 }
57
58 FGATCMgr::FGATCMgr() {
59         comm_ident[0] = "";
60         comm_ident[1] = "";
61         last_comm_ident[0] = "";
62         last_comm_ident[1] = "";
63         approach_ident = "";
64         last_in_range = false;
65         comm_type[0] = INVALID;
66         comm_type[1] = INVALID;
67         comm_atc_ptr[0] = NULL;
68         comm_atc_ptr[1] = NULL;
69         comm_valid[0] = false;
70         comm_valid[1] = false;
71         
72         initDone = false;
73 }
74
75 FGATCMgr::~FGATCMgr() {
76         delete v1;
77 }
78
79 void FGATCMgr::bind() {
80 }
81
82 void FGATCMgr::unbind() {
83 }
84
85 void FGATCMgr::init() {
86         comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
87         comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
88         lon_node = fgGetNode("/position/longitude-deg", true);
89         lat_node = fgGetNode("/position/latitude-deg", true);
90         elev_node = fgGetNode("/position/altitude-ft", true);
91         atc_list_itr = atc_list.begin();
92         
93         // Search for connected ATC stations once per 0.8 seconds or so
94         // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
95         //                                 FGEvent::FG_EVENT_READY, 800);
96         //  
97         // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
98         // Is this still true after the reorganization of the event managar??
99         // -EMH-
100         
101         // Initialise the frequency search map
102     current_commlist = new FGCommList;
103     SGPath p_comm( globals->get_fg_root() );
104     current_commlist->init( p_comm );
105
106 #ifdef ENABLE_AUDIO_SUPPORT     
107         // Load all available voices.
108         // For now we'll do one hardwired one
109         
110         v1 = new FGATCVoice;
111         voiceOK = v1->LoadVoice("default");
112         voice = true;
113         
114         /* I've loaded the voice even if /sim/sound/audible is false
115         *  since I know no way of forcing load of the voice if the user
116         *  subsequently switches /sim/sound/audible to true. */
117 #else
118         voice = false;
119 #endif
120
121         // Initialise the ATC Dialogs
122         //cout << "Initing Transmissions..." << endl;
123     SG_LOG(SG_ATC, SG_INFO, "  ATC Transmissions");
124     current_transmissionlist = new FGTransmissionList;
125     SGPath p_transmission( globals->get_fg_root() );
126     p_transmission.append( "ATC/default.transmissions" );
127     current_transmissionlist->init( p_transmission );
128         //cout << "Done Transmissions" << endl;
129
130     SG_LOG(SG_ATC, SG_INFO, "  ATC Dialog System");
131     current_atcdialog = new FGATCDialog;
132     current_atcdialog->Init();
133
134         initDone = true;
135 }
136
137 void FGATCMgr::update(double dt) {
138         if(!initDone) {
139                 init();
140                 SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
141         }
142         
143         //cout << "Entering update..." << endl;
144         //Traverse the list of active stations.
145         //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
146         //Eventually we should only update every so many steps.
147         //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
148         if(atc_list.size()) {
149                 if(atc_list_itr == atc_list.end()) {
150                         atc_list_itr = atc_list.begin();
151                 }
152                 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
153                 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
154                 (*atc_list_itr)->Update(dt * atc_list.size());
155                 //cout << "Done ATC update..." << endl;
156                 ++atc_list_itr;
157         }
158         
159         // Search the tuned frequencies every now and then - this should be done with the event scheduler
160         static int i = 0;       // Very ugly - but there should only ever be one instance of FGATCMgr.
161         /*
162         if(i == 7) {
163                 //cout << "About to AreaSearch()" << endl;
164                 AreaSearch();
165         }
166         */
167         if(i == 15) {
168                 //cout << "About to search(1)" << endl;
169                 FreqSearch(1);
170         }
171         if(i == 30) {
172                 //cout << "About to search(2)" << endl;
173                 FreqSearch(2);
174                 i = 0;
175         }
176         ++i;
177         
178         //cout << "comm1 type = " << comm_type[0] << '\n';
179         //cout << "Leaving update..." << endl;
180 }
181
182
183 // Returns frequency in KHz - should I alter this to return in MHz?
184 unsigned short int FGATCMgr::GetFrequency(string ident, atc_type tp) {
185         ATCData test;
186         bool ok = current_commlist->FindByCode(ident, test, tp);
187         return(ok ? test.freq : 0);
188 }       
189
190
191 // Register the fact that the AI system wants to activate an airport
192 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
193 bool FGATCMgr::AIRegisterAirport(string ident) {
194         SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
195         if(airport_atc_map.find(ident) != airport_atc_map.end()) {
196                 airport_atc_map[ident]->set_by_AI = true;
197                 airport_atc_map[ident]->numAI++;
198                 return(true);
199         } else {
200                 FGAirport ap;
201                 if(dclFindAirportID(ident, &ap)) {
202                         //cout << "ident = " << ident << '\n';
203                         AirportATC *a = new AirportATC;
204                         // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
205                         a->lon = ap.longitude;
206                         a->lat = ap.latitude;
207                         a->elev = ap.elevation;
208                         a->atis_freq = GetFrequency(ident, ATIS);
209                         //cout << "ATIS freq = " << a->atis_freq << '\n';
210                         a->atis_active = false;
211                         a->tower_freq = GetFrequency(ident, TOWER);
212                         //cout << "Tower freq = " << a->tower_freq << '\n';
213                         a->tower_active = false;
214                         a->ground_freq = GetFrequency(ident, GROUND);
215                         //cout << "Ground freq = " << a->ground_freq << '\n';
216                         a->ground_active = false;
217                         // TODO - some airports will have a tower/ground frequency but be inactive overnight.
218                         a->set_by_AI = true;
219                         a->numAI = 1;
220                         airport_atc_map[ident] = a;
221                         return(true);
222                 }
223         }
224         return(false);
225 }
226
227
228 // Register the fact that the comm radio is tuned to an airport
229 // Channel is zero based
230 bool FGATCMgr::CommRegisterAirport(string ident, int chan, atc_type tp) {
231         SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
232         if(airport_atc_map.find(ident) != airport_atc_map.end()) {
233                 //cout << "IN MAP - flagging set by comm..." << endl;
234                 airport_atc_map[ident]->set_by_comm[chan][tp] = true;
235                 return(true);
236         } else {
237                 //cout << "NOT IN MAP - creating new..." << endl;
238                 FGAirport ap;
239                 if(dclFindAirportID(ident, &ap)) {
240                         AirportATC *a = new AirportATC;
241                         // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
242                         a->lon = ap.longitude;
243                         a->lat = ap.latitude;
244                         a->elev = ap.elevation;
245                         a->atis_freq = GetFrequency(ident, ATIS);
246                         a->atis_active = false;
247                         a->tower_freq = GetFrequency(ident, TOWER);
248                         a->tower_active = false;
249                         a->ground_freq = GetFrequency(ident, GROUND);
250                         a->ground_active = false;
251                         // TODO - some airports will have a tower/ground frequency but be inactive overnight.
252                         a->set_by_AI = false;
253                         a->numAI = 0;
254                         a->set_by_comm[chan][tp] = true;                        
255                         airport_atc_map[ident] = a;
256                         return(true);
257                 }
258         }
259         return(false);
260 }
261
262
263 // Remove from list only if not needed by the AI system or the other comm channel
264 // Note that chan is zero based.
265 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
266         SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan);
267         if(airport_atc_map.find(id) != airport_atc_map.end()) {
268                 AirportATC* a = airport_atc_map[id];
269                 //cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
270                 if(a->set_by_AI && tp != ATIS) {
271                         // Set by AI, so don't remove simply because user isn't tuned in any more - just stop displaying
272                         SG_LOG(SG_ATC, SG_BULK, "In CommRemoveFromList, service was set by AI\n");
273                         FGATC* aptr = GetATCPointer(id, tp);
274                         switch(chan) {
275                         case 0:
276                                 //cout << "chan 1\n";
277                                 a->set_by_comm[0][tp] = false;
278                                 if(!a->set_by_comm[1][tp]) {
279                                         //cout << "not set by comm2\n";
280                                         if(aptr != NULL) {
281                                                 //cout << "Got pointer\n";
282                                                 aptr->SetNoDisplay();
283                                                 //cout << "Setting no display...\n";
284                                         } else {
285                                                 //cout << "Not got pointer\n";
286                                         }
287                                 }
288                                 break;
289                         case 1:
290                                 a->set_by_comm[1][tp] = false;
291                                 if(!a->set_by_comm[0][tp]) {
292                                         if(aptr != NULL) {
293                                                 aptr->SetNoDisplay();
294                                                 //cout << "Setting no display...\n";
295                                         }
296                                 }
297                                 break;
298                         }
299                         airport_atc_map[id] = a;
300                         return;
301                 } else {
302                         switch(chan) {
303                         case 0:
304                                 a->set_by_comm[0][tp] = false;
305                                 // Remove only if not also set by the other comm channel
306                                 if(!a->set_by_comm[1][tp]) {
307                                         RemoveFromList(id, tp);
308                                 }
309                                 break;
310                         case 1:
311                                 a->set_by_comm[1][tp] = false;
312                                 if(!a->set_by_comm[0][tp]) {
313                                         RemoveFromList(id, tp);
314                                 }
315                                 break;
316                         }
317                 }
318         }
319 }
320     
321
322 // Remove from list - should only be called from above or similar
323 // This function *will* remove it from the list regardless of who else might want it.
324 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
325         //cout << "Requested type = " << tp << '\n';
326         //cout << "id = " << id << '\n';
327         atc_list_itr = atc_list.begin();
328         while(atc_list_itr != atc_list.end()) {
329                 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
330                 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
331                 if( (!strcmp((*atc_list_itr)->get_ident(), id))
332                         && ((*atc_list_itr)->GetType() == tp) ) {
333                                 //Before removing it stop it transmitting!!
334                                 //cout << "OBLITERATING FROM LIST!!!\n";
335                                 (*atc_list_itr)->SetNoDisplay();
336                                 (*atc_list_itr)->Update(0.00833);
337                                 delete (*atc_list_itr);
338                                 atc_list_itr = atc_list.erase(atc_list_itr);
339                                 break;
340                         }  // Note that that can upset where we are in the list but that doesn't really matter
341                 ++atc_list_itr;
342         }
343 }
344
345
346 // Find in list - return a currently active ATC pointer given ICAO code and type
347 // Return NULL if the given service is not in the list
348 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
349 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
350         atc_list_itr = atc_list.begin();
351         while(atc_list_itr != atc_list.end()) {
352                 if( (!strcmp((*atc_list_itr)->get_ident(), id))
353                 && ((*atc_list_itr)->GetType() == tp) ) {
354                         return(*atc_list_itr);
355                 }       // Note that that can upset where we are in the list but that shouldn't really matter
356                 ++atc_list_itr;
357         }
358         // If we get here it's not in the list
359         return(NULL);
360 }
361
362 // Returns true if the airport is found in the map
363 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
364         if(airport_atc_map.find(icao) != airport_atc_map.end()) {
365                 *a = *airport_atc_map[icao];
366                 return(true);
367         } else {
368                 return(false);
369         }
370 }
371
372
373 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
374 // Returns NULL if service doesn't exist - calling function should check for this.
375 // We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
376 // - at the moment all these GetATC... functions exposed are just too complicated.
377 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
378         if(airport_atc_map.find(icao) == airport_atc_map.end()) {
379                 return NULL;
380         }
381         AirportATC *a = airport_atc_map[icao];
382         //cout << "a->lon = " << a->lon << '\n';
383         //cout << "a->elev = " << a->elev << '\n';
384         //cout << "a->tower_freq = " << a->tower_freq << '\n';
385         switch(type) {
386         case TOWER:
387                 if(a->tower_active) {
388                         // Get the pointer from the list
389                         return(FindInList(icao.c_str(), type));
390                 } else {
391                         ATCData data;
392                         if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
393                                 FGTower* t = new FGTower;
394                                 t->SetData(&data);
395                                 atc_list.push_back(t);
396                                 a->tower_active = true;
397                                 airport_atc_map[icao] = a;
398                                 t->Init();
399                                 return(t);
400                         } else {
401                                 SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
402                         }
403                 }
404                 break;
405         case APPROACH:
406                 break;
407         case ATIS:
408                 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
409                 break;
410         case GROUND:
411                 //cout << "IN CASE GROUND" << endl;
412                 if(a->ground_active) {
413                         // Get the pointer from the list
414                         return(FindInList(icao.c_str(), type));
415                 } else {
416                         ATCData data;
417                         if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
418                                 FGGround* g = new FGGround;
419                                 g->SetData(&data);
420                                 atc_list.push_back(g);
421                                 a->ground_active = true;
422                                 airport_atc_map[icao] = a;
423                                 g->Init();
424                                 return(g);
425                         } else {
426                                 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
427                         }
428                 }
429                 break;
430                 case INVALID:
431                 break;
432                 case ENROUTE:
433                 break;
434                 case DEPARTURE:
435                 break;
436         }
437         
438         SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
439         
440         return(NULL);
441 }
442
443 // Return a pointer to an appropriate voice for a given type of ATC
444 // creating the voice if necessary - ie. make sure exactly one copy
445 // of every voice in use exists in memory.
446 //
447 // TODO - in the future this will get more complex and dole out country/airport
448 // specific voices, and possible make sure that the same voice doesn't get used
449 // at different airports in quick succession if a large enough selection are available.
450 FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
451         // TODO - implement me better - maintain a list of loaded voices and other voices!!
452         if(voice) {
453                 switch(type) {
454                 case ATIS:
455                         if(voiceOK) {
456                                 return(v1);
457                         }
458                 case TOWER:
459                         return(NULL);
460                 case APPROACH:
461                         return(NULL);
462                 case GROUND:
463                         return(NULL);
464                 default:
465                         return(NULL);
466                 }
467                 return(NULL);
468         } else {
469                 return(NULL);
470         }
471 }
472
473 // Search for ATC stations by frequency
474 void FGATCMgr::FreqSearch(int channel) {
475         int chan = channel - 1;         // Convert to zero-based for the arrays
476
477         ATCData data;   
478         double freq = comm_node[chan]->getDoubleValue();
479         lon = lon_node->getDoubleValue();
480         lat = lat_node->getDoubleValue();
481         elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
482         
483         // Query the data store and get the closest match if any
484         if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
485                 // We have a match
486                 // What's the logic?
487                 // If this channel not previously valid then easy - add ATC to list
488                 // If this channel was valid then - Have we tuned to a different service?
489                 // If so - de-register one and add the other
490                 if(comm_valid[chan]) {
491                         if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
492                                 // Then we're still tuned into the same service so do nought and return
493                                 return;
494                         } else {
495                                 // Something's changed - either the location or the service type
496                                 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
497                                 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
498                         }
499                 }
500                 // At this point we can assume that we need to add the service.
501                 comm_ident[chan] = (data.ident).c_str();
502                 comm_type[chan] = data.type;
503                 comm_x[chan] = (double)data.x;
504                 comm_y[chan] = (double)data.y;
505                 comm_z[chan] = (double)data.z;
506                 comm_lon[chan] = (double)data.lon;
507                 comm_lat[chan] = (double)data.lat;
508                 comm_elev[chan] = (double)data.elev;
509                 comm_valid[chan] = true;
510                 
511                 // This was a switch-case statement but the compiler didn't like the new variable creation with it. 
512                 if(comm_type[chan] == ATIS) {
513                         CommRegisterAirport(comm_ident[chan], chan, ATIS);
514                         FGATC* app = FindInList(comm_ident[chan], ATIS);
515                         if(app != NULL) {
516                                 // The station is already in the ATC list
517                                 //cout << "In list - flagging SetDisplay..." << endl;
518                                 app->SetDisplay();
519                         } else {
520                                 // Generate the station and put in the ATC list
521                                 //cout << "Not in list - generating..." << endl;
522                                 FGATIS* a = new FGATIS;
523                                 a->SetData(&data);
524                                 comm_atc_ptr[chan] = a;
525                                 a->SetDisplay();
526                                 //a->Init();
527                                 atc_list.push_back(a);
528                         }
529                 } else if (comm_type[chan] == TOWER) {
530                         CommRegisterAirport(comm_ident[chan], chan, TOWER);
531                         //cout << "Done (TOWER)" << endl;
532                         FGATC* app = FindInList(comm_ident[chan], TOWER);
533                         if(app != NULL) {
534                                 // The station is already in the ATC list
535                                 //cout << "In list - flagging SetDisplay..." << endl;
536                                 app->SetDisplay();
537                         } else {
538                                 // Generate the station and put in the ATC list
539                                 //cout << "Not in list - generating..." << endl;
540                                 FGTower* t = new FGTower;
541                                 t->SetData(&data);
542                                 comm_atc_ptr[chan] = t;
543                                 t->SetDisplay();
544                                 t->Init();
545                                 atc_list.push_back(t);
546                         }
547                 }  else if (comm_type[chan] == GROUND) {
548                         CommRegisterAirport(comm_ident[chan], chan, GROUND);
549                         //cout << "Done (GROUND)" << endl;
550                         FGATC* app = FindInList(comm_ident[chan], GROUND);
551                         if(app != NULL) {
552                                 // The station is already in the ATC list
553                                 app->SetDisplay();
554                         } else {
555                                 // Generate the station and put in the ATC list
556                                 FGGround* g = new FGGround;
557                                 g->SetData(&data);
558                                 comm_atc_ptr[chan] = g;
559                                 g->SetDisplay();
560                                 g->Init();
561                                 atc_list.push_back(g);
562                         }
563                 } else if (comm_type[chan] == APPROACH) {
564                         // We have to be a bit more carefull here since approaches are also searched by area
565                         CommRegisterAirport(comm_ident[chan], chan, APPROACH);
566                         //cout << "Done (APPROACH)" << endl;
567                         FGATC* app = FindInList(comm_ident[chan], APPROACH);
568                         if(app != NULL) {
569                                 // The station is already in the ATC list
570                                 app->AddPlane("Player");
571                                 app->SetDisplay();
572                                 comm_atc_ptr[chan] = app;
573                         } else {
574                                 // Generate the station and put in the ATC list
575                                 FGApproach* a = new FGApproach;
576                                 a->SetData(&data);
577                                 comm_atc_ptr[chan] = a;
578                                 a->SetDisplay();
579                                 a->Init();
580                                 a->AddPlane("Player");
581                                 atc_list.push_back(a);
582                         }                       
583                 }
584         } else {
585                 if(comm_valid[chan]) {
586                         if(comm_type[chan] != APPROACH) {
587                                 // Currently approaches are removed by Alexander's out-of-range mechanism
588                                 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
589                         }
590                         // Note that we *don't* call SetNoDisplay() here because the other comm channel
591                         // might be tuned into the same station - this is handled by CommRemoveFromList(...)
592                         comm_type[chan] = INVALID;
593                         comm_atc_ptr[chan] = NULL;
594                         comm_valid[chan] = false;
595                 }
596         }
597 }
598
599 // Search ATC stations by area in order that we appear 'on the radar'
600 void FGATCMgr::AreaSearch() {
601         // Search for Approach stations
602         comm_list_type approaches;
603         comm_list_iterator app_itr;
604         
605         lon = lon_node->getDoubleValue();
606         lat = lat_node->getDoubleValue();
607         elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
608         
609         // search stations in range
610         int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
611         if (num_app != 0) {
612                 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
613                 
614                 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
615                         
616                         FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
617                         if(app != NULL) {
618                                 // The station is already in the ATC list
619                                 //cout << "In list adding player\n";
620                                 app->AddPlane("Player");
621                                 //app->Update();
622                         } else {
623                                 // Generate the station and put in the ATC list
624                                 FGApproach* a = new FGApproach;
625                                 a->SetData(&(*app_itr));
626                                 //cout << "Adding player\n";
627                                 a->AddPlane("Player");
628                                 //a->Update();
629                                 atc_list.push_back(a);
630                         }
631                 }
632         }
633         
634         // remove planes which are out of range
635         // TODO - I'm not entirely sure that this belongs here.
636         atc_list_itr = atc_list.begin();
637         while(atc_list_itr != atc_list.end()) {
638                 if((*atc_list_itr)->GetType() == APPROACH ) {
639                         int np = (*atc_list_itr)->RemovePlane();
640                         // if approach has no planes left remove it from ATC list
641                         if ( np == 0) {
642                                 (*atc_list_itr)->SetNoDisplay();
643                                 (*atc_list_itr)->Update(0.00833);
644                                 delete (*atc_list_itr);
645                                 atc_list_itr = atc_list.erase(atc_list_itr);
646                                 break;     // the other stations will be checked next time
647                         }
648                 }
649                 ++atc_list_itr;
650         }
651 }