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