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