]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCmgr.cxx
Fix a couple of bugs that resulted in ATIS not being heard at airports with AI traffi...
[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 << " " << tp << " 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 && tp != ATIS) {
289                         SG_LOG(SG_ATC, SG_BULK, "In CommRemoveFromList, service was set by AI\n");
290                         // Don't remove
291                         FGATC* aptr = GetATCPointer((string)id, tp);
292                         switch(chan) {
293                         case 0:
294                                 //cout << "chan 1\n";
295                                 a->set_by_comm[0] = false;
296                                 if(!a->set_by_comm[1]) {
297                                         //cout << "not set by comm2\n";
298                                         if(aptr != NULL) {
299                                                 //cout << "Got pointer\n";
300                                                 aptr->SetNoDisplay();
301                                                 //cout << "Setting no display...\n";
302                                         } else {
303                                                 //cout << "Not got pointer\n";
304                                         }
305                                 }
306                                 break;
307                         case 1:
308                                 a->set_by_comm[1] = false;
309                                 if(!a->set_by_comm[0]) {
310                                         if(aptr != NULL) {
311                                                 aptr->SetNoDisplay();
312                                                 //cout << "Setting no display...\n";
313                                         }
314                                 }
315                                 break;
316                         }
317                         airport_atc_map[(string)id] = a;
318                         return;
319                 } else {
320                         switch(chan) {
321                         case 0:
322                                 a->set_by_comm[0] = false;
323                                 // Remove only if not also set by the other comm channel
324                                 if(!a->set_by_comm[1]) {
325                                         RemoveFromList(id, tp);
326                                 }
327                                 break;
328                         case 1:
329                                 a->set_by_comm[1] = false;
330                                 if(!a->set_by_comm[0]) {
331                                         RemoveFromList(id, tp);
332                                 }
333                                 break;
334                         }
335                 }
336         }
337 }
338     
339
340 // Remove from list - should only be called from above or similar
341 // This function *will* remove it from the list regardless of who else might want it.
342 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
343         //cout << "Requested type = " << tp << '\n';
344         //cout << "id = " << id << '\n';
345         atc_list_itr = atc_list.begin();
346         while(atc_list_itr != atc_list.end()) {
347                 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
348                 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
349                 if( (!strcmp((*atc_list_itr)->get_ident(), id))
350                         && ((*atc_list_itr)->GetType() == tp) ) {
351                                 //Before removing it stop it transmitting!!
352                                 //cout << "OBLITERATING FROM LIST!!!\n";
353                                 (*atc_list_itr)->SetNoDisplay();
354                                 (*atc_list_itr)->Update(0.00833);
355                                 delete (*atc_list_itr);
356                                 atc_list_itr = atc_list.erase(atc_list_itr);
357                                 break;
358                         }  // Note that that can upset where we are in the list but that doesn't really matter
359                 ++atc_list_itr;
360         }
361 }
362
363
364 // Find in list - return a currently active ATC pointer given ICAO code and type
365 // Return NULL if the given service is not in the list
366 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
367 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
368         atc_list_itr = atc_list.begin();
369         while(atc_list_itr != atc_list.end()) {
370                 if( (!strcmp((*atc_list_itr)->get_ident(), id))
371                 && ((*atc_list_itr)->GetType() == tp) ) {
372                         return(*atc_list_itr);
373                 }       // Note that that can upset where we are in the list but that shouldn't really matter
374                 ++atc_list_itr;
375         }
376         // If we get here it's not in the list
377         return(NULL);
378 }
379
380 // Returns true if the airport is found in the map
381 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
382         if(airport_atc_map.find(icao) != airport_atc_map.end()) {
383                 *a = *airport_atc_map[icao];
384                 return(true);
385         } else {
386                 return(false);
387         }
388 }
389
390
391 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
392 // Returns NULL if service doesn't exist - calling function should check for this.
393 // We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
394 // - at the moment all these GetATC... functions exposed are just too complicated.
395 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
396         if(airport_atc_map.find(icao) == airport_atc_map.end()) {
397                 return NULL;
398         }
399         AirportATC *a = airport_atc_map[icao];
400         //cout << "a->lon = " << a->lon << '\n';
401         //cout << "a->elev = " << a->elev << '\n';
402         //cout << "a->tower_freq = " << a->tower_freq << '\n';
403         switch(type) {
404         case TOWER:
405                 if(a->tower_active) {
406                         // Get the pointer from the list
407                         return(FindInList(icao.c_str(), type));
408                 } else {
409                         ATCData data;
410                         if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
411                                 FGTower* t = new FGTower;
412                                 t->SetData(&data);
413                                 atc_list.push_back(t);
414                                 a->tower_active = true;
415                                 airport_atc_map[icao] = a;
416                                 t->Init();
417                                 return(t);
418                         } else {
419                                 SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
420                         }
421                 }
422                 break;
423         case APPROACH:
424                 break;
425         case ATIS:
426                 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
427                 break;
428         case GROUND:
429                 //cout << "IN CASE GROUND" << endl;
430                 if(a->ground_active) {
431                         // Get the pointer from the list
432                         return(FindInList(icao.c_str(), type));
433                 } else {
434                         ATCData data;
435                         if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
436                                 FGGround* g = new FGGround;
437                                 g->SetData(&data);
438                                 atc_list.push_back(g);
439                                 a->ground_active = true;
440                                 airport_atc_map[icao] = a;
441                                 g->Init();
442                                 return(g);
443                         } else {
444                                 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
445                         }
446                 }
447                 break;
448                 case INVALID:
449                 break;
450                 case ENROUTE:
451                 break;
452                 case DEPARTURE:
453                 break;
454         }
455         
456         SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
457         
458         return(NULL);
459 }
460
461 // Return a pointer to an appropriate voice for a given type of ATC
462 // creating the voice if necessary - ie. make sure exactly one copy
463 // of every voice in use exists in memory.
464 //
465 // TODO - in the future this will get more complex and dole out country/airport
466 // specific voices, and possible make sure that the same voice doesn't get used
467 // at different airports in quick succession if a large enough selection are available.
468 FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
469         // TODO - implement me better - maintain a list of loaded voices and other voices!!
470         if(voice) {
471                 switch(type) {
472                 case ATIS:
473                         if(voiceOK) {
474                                 return(v1);
475                         }
476                 case TOWER:
477                         return(NULL);
478                 case APPROACH:
479                         return(NULL);
480                 case GROUND:
481                         return(NULL);
482                 default:
483                         return(NULL);
484                 }
485                 return(NULL);
486         } else {
487                 return(NULL);
488         }
489 }
490
491 // Display a dialog box with options relevant to the currently tuned ATC service.
492 void FGATCMgr::doPopupDialog() {
493         ATCDoDialog(comm_type[0]);      // FIXME - currently hardwired to comm1
494 }
495
496 // Search for ATC stations by frequency
497 void FGATCMgr::FreqSearch(int channel) {
498         int chan = channel - 1;         // Convert to zero-based for the arrays
499
500         ATCData data;   
501         double freq = comm_node[chan]->getDoubleValue();
502         lon = lon_node->getDoubleValue();
503         lat = lat_node->getDoubleValue();
504         elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
505         
506         // Query the data store and get the closest match if any
507         if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
508                 // We have a match
509                 // What's the logic?
510                 // If this channel not previously valid then easy - add ATC to list
511                 // If this channel was valid then - Have we tuned to a different service?
512                 // If so - de-register one and add the other
513                 if(comm_valid[chan]) {
514                         if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
515                                 // Then we're still tuned into the same service so do nought and return
516                                 return;
517                         } else {
518                                 // Something's changed - either the location or the service type
519                                 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
520                                 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
521                         }
522                 }
523                 // At this point we can assume that we need to add the service.
524                 comm_ident[chan] = (data.ident).c_str();
525                 comm_type[chan] = data.type;
526                 comm_x[chan] = (double)data.x;
527                 comm_y[chan] = (double)data.y;
528                 comm_z[chan] = (double)data.z;
529                 comm_lon[chan] = (double)data.lon;
530                 comm_lat[chan] = (double)data.lat;
531                 comm_elev[chan] = (double)data.elev;
532                 comm_valid[chan] = true;
533                 
534                 // This was a switch-case statement but the compiler didn't like the new variable creation with it. 
535                 if(comm_type[chan] == ATIS) {
536                         CommRegisterAirport(comm_ident[chan], chan);
537                         FGATC* app = FindInList(comm_ident[chan], ATIS);
538                         if(app != NULL) {
539                                 // The station is already in the ATC list
540                                 //cout << "In list - flagging SetDisplay..." << endl;
541                                 app->SetDisplay();
542                         } else {
543                                 // Generate the station and put in the ATC list
544                                 //cout << "Not in list - generating..." << endl;
545                                 FGATIS* a = new FGATIS;
546                                 a->SetData(&data);
547                                 comm_atc_ptr[chan] = a;
548                                 a->SetDisplay();
549                                 //a->Init();
550                                 atc_list.push_back(a);
551                         }
552                 } else if (comm_type[chan] == TOWER) {
553                         CommRegisterAirport(comm_ident[chan], chan);
554                         //cout << "Done (TOWER)" << endl;
555                         FGATC* app = FindInList(comm_ident[chan], TOWER);
556                         if(app != NULL) {
557                                 // The station is already in the ATC list
558                                 //cout << "In list - flagging SetDisplay..." << endl;
559                                 app->SetDisplay();
560                         } else {
561                                 // Generate the station and put in the ATC list
562                                 //cout << "Not in list - generating..." << endl;
563                                 FGTower* t = new FGTower;
564                                 t->SetData(&data);
565                                 comm_atc_ptr[chan] = t;
566                                 t->SetDisplay();
567                                 t->Init();
568                                 atc_list.push_back(t);
569                         }
570                 }  else if (comm_type[chan] == GROUND) {
571                         CommRegisterAirport(comm_ident[chan], chan);
572                         //cout << "Done (GROUND)" << endl;
573                         FGATC* app = FindInList(comm_ident[chan], GROUND);
574                         if(app != NULL) {
575                                 // The station is already in the ATC list
576                                 app->SetDisplay();
577                         } else {
578                                 // Generate the station and put in the ATC list
579                                 FGGround* g = new FGGround;
580                                 g->SetData(&data);
581                                 comm_atc_ptr[chan] = g;
582                                 g->SetDisplay();
583                                 g->Init();
584                                 atc_list.push_back(g);
585                         }
586                 } else if (comm_type[chan] == APPROACH) {
587                         // We have to be a bit more carefull here since approaches are also searched by area
588                         CommRegisterAirport(comm_ident[chan], chan);
589                         //cout << "Done (APPROACH)" << endl;
590                         FGATC* app = FindInList(comm_ident[chan], APPROACH);
591                         if(app != NULL) {
592                                 // The station is already in the ATC list
593                                 app->AddPlane("Player");
594                                 app->SetDisplay();
595                                 comm_atc_ptr[chan] = app;
596                         } else {
597                                 // Generate the station and put in the ATC list
598                                 FGApproach* a = new FGApproach;
599                                 a->SetData(&data);
600                                 a->AddPlane("Player");
601                                 atc_list.push_back(a);
602                                 comm_atc_ptr[chan] = a;
603                         }                       
604                 }
605         } else {
606                 if(comm_valid[chan]) {
607                         if(comm_type[chan] != APPROACH) {
608                                 // Currently approaches are removed by Alexander's out-of-range mechanism
609                                 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
610                         }
611                         // Note that we *don't* call SetNoDisplay() here because the other comm channel
612                         // might be tuned into the same station - this is handled by CommRemoveFromList(...)
613                         comm_type[chan] = INVALID;
614                         comm_atc_ptr[chan] = NULL;
615                         comm_valid[chan] = false;
616                 }
617         }
618 }
619
620
621 // Search ATC stations by area in order that we appear 'on the radar'
622 void FGATCMgr::AreaSearch() {
623         // Search for Approach stations
624         comm_list_type approaches;
625         comm_list_iterator app_itr;
626         
627         lon = lon_node->getDoubleValue();
628         lat = lat_node->getDoubleValue();
629         elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
630         
631         // search stations in range
632         int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
633         if (num_app != 0) {
634                 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
635                 
636                 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
637                         
638                         FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
639                         if(app != NULL) {
640                                 // The station is already in the ATC list
641                                 app->AddPlane("Player");
642                                 //app->Update();
643                         } else {
644                                 // Generate the station and put in the ATC list
645                                 FGApproach* a = new FGApproach;
646                                 a->SetData(&(*app_itr));
647                                 a->AddPlane("Player");
648                                 //a->Update();
649                                 atc_list.push_back(a);
650                         }
651                 }
652         }
653         
654         // remove planes which are out of range
655         // TODO - I'm not entirely sure that this belongs here.
656         atc_list_itr = atc_list.begin();
657         while(atc_list_itr != atc_list.end()) {
658                 if((*atc_list_itr)->GetType() == APPROACH ) {
659                         int np = (*atc_list_itr)->RemovePlane();
660                         // if approach has no planes left remove it from ATC list
661                         if ( np == 0) {
662                                 (*atc_list_itr)->SetNoDisplay();
663                                 (*atc_list_itr)->Update(0.00833);
664                                 delete (*atc_list_itr);
665                                 atc_list_itr = atc_list.erase(atc_list_itr);
666                                 break;     // the other stations will be checked next time
667                         }
668                 }
669                 ++atc_list_itr;
670         }
671 }