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