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