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