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