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