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