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