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