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