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