]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/ATCmgr.cxx
Mathias Fröhlich:
[flightgear.git] / src / ATC / ATCmgr.cxx
index da883301c57a0c5398cf7770f17ed8f1d16d2537..8084b0822d1704b246c7f8ec2453498a992f5974 100644 (file)
 // along with this program; if not, write to the Free Software
 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-//#include <Time/event.hxx>
-
 #include <simgear/misc/sg_path.hxx>
-#include <simgear/misc/commands.hxx>
 #include <simgear/debug/logstream.hxx>
+#include <Airports/simple.hxx>
 
 #include "ATCmgr.hxx"
-#include "atislist.hxx"
-//#include "groundlist.hxx"
-#include "towerlist.hxx"
-#include "approachlist.hxx"
+#include "commlist.hxx"
 #include "ATCdisplay.hxx"
+#include "ATCDialog.hxx"
+#include "ATCutils.hxx"
 
 /*
 // periodic radio station search wrapper
@@ -37,158 +34,47 @@ static void fgATCSearch( void ) {
        globals->get_ATC_mgr()->Search();
 }
 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
-
-static char* t0 = "Request landing clearance";
-static char* t1 = "Request departure clearance";
-static char* t2 = "Report Runway vacated";
-static char** towerOptions = new char*[4];
-static char* a0 = "Request vectors";
-static char** approachOptions = new char*[2];
-
-// For the ATC dialog - copied from the Autopilot new heading dialog code!
-static puDialogBox*            atcDialog;
-static puFrame*                        atcDialogFrame;
-static puText*                 atcDialogMessage;
-//static puInput*                      atcDialogInput;
-static puOneShot*              atcDialogOkButton;
-static puOneShot*              atcDialogCancelButton;
-static puButtonBox*            atcDialogCommunicationOptions;
-
-static void ATCDialogCancel(puObject *)
-{
-    //ATCDialogInput->rejectInput();
-    FG_POP_PUI_DIALOG( atcDialog );
-}
-
-static void ATCDialogOK (puObject *me)
-{
-       // Note that currently the dialog is hardwired to comm1 only here.
-       switch(globals->get_ATC_mgr()->GetComm1ATCType()) {
-       case INVALID:
-               break;
-       case ATIS:
-               break;
-       case TOWER: {
-               FGTower* twr = (FGTower*)globals->get_ATC_mgr()->GetComm1ATCPointer();
-               switch(atcDialogCommunicationOptions->getValue()) {
-               case 0:
-                       cout << "Option 0 chosen\n";
-                       twr->RequestLandingClearance("golf bravo echo");
-                       break;
-               case 1:
-                       cout << "Option 1 chosen\n";
-                       twr->RequestDepartureClearance("golf bravo echo");
-                       break;
-               case 2:
-                       cout << "Option 2 chosen\n";
-                       twr->ReportRunwayVacated("golf bravo echo");
-                       break;
-               default:
-                       break;
-               }
-               break;
-       }
-       case GROUND:
-               break;
-       case APPROACH:
-               break;
-       default:
-               break;
-       }
-
-    ATCDialogCancel(me);
-    //if(error) mkDialog(s.c_str());
-}
-
-static void ATCDialog(puObject *cb)
+   // Is this still true?? -EMH-
+
+AirportATC::AirportATC() :
+    lon(0.0),
+    lat(0.0),
+    elev(0.0),
+    atis_freq(0.0),
+    atis_active(false),
+    tower_freq(0.0),
+    tower_active(false),
+    ground_freq(0.0),
+    ground_active(false),
+    set_by_AI(false),
+       numAI(0)
+       //airport_atc_map.clear();
 {
-    //ApHeadingDialogInput   ->    setValue ( heading );
-    //ApHeadingDialogInput    -> acceptInput();
-    FG_PUSH_PUI_DIALOG(atcDialog);
-}
-
-static void ATCDialogInit()
-{
-       char defaultATCLabel[] = "Enter desired option to communicate with ATC:";
-       char *s;
-
-       // Option lists hardwired per ATC type  
-       towerOptions[0] = new char[strlen(t0)];
-       strcpy(towerOptions[0], t0);
-       towerOptions[1] = new char[strlen(t1)];
-       strcpy(towerOptions[1], t1);
-       towerOptions[2] = new char[strlen(t2)];
-       strcpy(towerOptions[2], t2);
-       towerOptions[3] = NULL;
-       
-       approachOptions[0] = new char[strlen(a0)];
-       strcpy(approachOptions[0], a0);
-       approachOptions[1] = NULL;
-
-       atcDialog = new puDialogBox (150, 50);
-       {
-               atcDialogFrame   = new puFrame (0, 0, 500, 250);
-               
-               atcDialogMessage = new puText          (250, 220);
-               atcDialogMessage    -> setDefaultValue (defaultATCLabel);
-               atcDialogMessage    -> getDefaultValue (&s);
-               atcDialogMessage    -> setLabel        (s);
-               atcDialogMessage    -> setLabelPlace   (PUPLACE_TOP_CENTERED);
-
-               atcDialogCommunicationOptions = new puButtonBox (50, 50, 450, 210, NULL, true);
-               
-               atcDialogOkButton     =  new puOneShot         (50, 10, 110, 50);
-               atcDialogOkButton     ->     setLegend         (gui_msg_OK);
-               atcDialogOkButton     ->     makeReturnDefault (TRUE);
-               atcDialogOkButton     ->     setCallback       (ATCDialogOK);
-               
-               atcDialogCancelButton =  new puOneShot         (140, 10, 210, 50);
-               atcDialogCancelButton ->     setLegend         (gui_msg_CANCEL);
-               atcDialogCancelButton ->     setCallback       (ATCDialogCancel);
-               
+       for(int i=0; i<ATC_NUM_TYPES; ++i) {
+               set_by_comm[0][i] = false;
+               set_by_comm[1][i] = false;
        }
-       FG_FINALIZE_PUI_DIALOG(atcDialog);
 }
 
-// For the command manager - maybe eventually this should go in the built in command list
-static bool do_ATC_dialog(const SGPropertyNode* arg) {
-       globals->get_ATC_mgr()->doStandardDialog();
-       return(true);
-}
-
-
 FGATCMgr::FGATCMgr() {
-       comm1_ident = "";
-       comm1_atis_ident = "";
-       comm1_tower_ident = "";
-       comm1_approach_ident = "";
-       last_comm1_ident = "";
-       last_comm1_atis_ident = "";
-       last_comm1_tower_ident = "";
-       last_comm1_approach_ident = "";
-       approach_ident = "";
+       comm_ident[0] = "";
+       comm_ident[1] = "";
+       //last_comm_ident[0] = "";
+       //last_comm_ident[1] = "";
+       //approach_ident = "";
        last_in_range = false;
-       comm1_atis_valid = false;
-       comm1_tower_valid = false;
-       comm1_approach_valid = false;
-       comm1_type = INVALID;
-       comm1_atc_ptr = NULL;
-       comm2_ident = "";
-       comm2_atis_ident = "";
-       comm2_tower_ident = "";
-       comm2_approach_ident = "";
-       last_comm2_ident = "";
-       last_comm2_atis_ident = "";
-       last_comm2_tower_ident = "";
-       last_comm2_approach_ident = "";
-       comm2_atis_valid = false;
-       comm2_tower_valid = false;
-       comm2_approach_valid = false;
-       comm2_type = INVALID;
-       comm2_atc_ptr = NULL;
+       comm_type[0] = INVALID;
+       comm_type[1] = INVALID;
+       comm_atc_ptr[0] = NULL;
+       comm_atc_ptr[1] = NULL;
+       comm_valid[0] = false;
+       comm_valid[1] = false;
+       
+       initDone = false;
 }
 
 FGATCMgr::~FGATCMgr() {
+       delete v1;
 }
 
 void FGATCMgr::bind() {
@@ -198,129 +84,327 @@ void FGATCMgr::unbind() {
 }
 
 void FGATCMgr::init() {
-       comm1_node = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
-       comm2_node = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
+       //cout << "ATCMgr::init called..." << endl;
+       
+       comm_node[0] = fgGetNode("/instrumentation/comm[0]/frequencies/selected-mhz", true);
+       comm_node[1] = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
        lon_node = fgGetNode("/position/longitude-deg", true);
        lat_node = fgGetNode("/position/latitude-deg", true);
        elev_node = fgGetNode("/position/altitude-ft", true);
        atc_list_itr = atc_list.begin();
+       
        // Search for connected ATC stations once per 0.8 seconds or so
-       // global_events.Register( "fgATCSearch()", fgATCSearch,
-       //                  fgEVENT::FG_EVENT_READY, 800);
+       // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
+        //                                 FGEvent::FG_EVENT_READY, 800);
+        //  
        // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
+        // Is this still true after the reorganization of the event managar??
+        // -EMH-
        
-       // Initialise the airport_atc_map - we'll cheat for now and just hardcode KEMT and any others that may be needed for development
-       AirportATC *a = new AirportATC;
-       a->lon = -118.034719;
-       a->lat = 34.086114;
-       a->elev = 296.0;
-       a->atis_freq = 118.75;
-       a->atis_active = false;
-       a->tower_freq = 121.2;
-       a->tower_active = false;
-       a->ground_freq = 125.9;
-       a->ground_active = false;
-       
-       //a->set_by_AI = true;
-       //a->set_by_comm_search = false;
+       // Initialise the frequency search map
+    current_commlist = new FGCommList;
+    SGPath p_comm( globals->get_fg_root() );
+    current_commlist->init( p_comm );
        
-       airport_atc_map[(string)"KEMT"] = a;
+       // Set the user callsign - bit of a hack at the moment - eventually should be read from aircraft file and user-over-rideable
+       fgSetString("/sim/user/callsign", "Golf Foxtrot Sierra");       // C-FGFS
 
 #ifdef ENABLE_AUDIO_SUPPORT    
        // Load all available voices.
        // For now we'll do one hardwired one
-       voiceOK = v1.LoadVoice("default");
-       /* I've loaded the voice even if /sim/sound/audible is false
+       
+       v1 = new FGATCVoice;
+       voiceOK = v1->LoadVoice("default");
+       voice = true;
+       
+       /* I've loaded the voice even if /sim/sound/pause is true
        *  since I know no way of forcing load of the voice if the user
-       *  subsequently switches /sim/sound/audible to true. */
+       *  subsequently switches /sim/sound/audible to true.
+        *  (which is the right thing to do -- CLO) :-) */
 #else
        voice = false;
 #endif
 
        // Initialise the ATC Dialogs
-       ATCDialogInit();
-       
-       // Add ATC-dialog to the command list
-       globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
+       //cout << "Initing Transmissions..." << endl;
+    SG_LOG(SG_ATC, SG_INFO, "  ATC Transmissions");
+    current_transmissionlist = new FGTransmissionList;
+    SGPath p_transmission( globals->get_fg_root() );
+    p_transmission.append( "ATC/default.transmissions" );
+    current_transmissionlist->init( p_transmission );
+       //cout << "Done Transmissions" << endl;
+
+    SG_LOG(SG_ATC, SG_INFO, "  ATC Dialog System");
+    current_atcdialog = new FGATCDialog;
+    current_atcdialog->Init();
+
+       initDone = true;
+       //cout << "ATCmgr::init done!" << endl;
 }
 
 void FGATCMgr::update(double dt) {
+       if(!initDone) {
+               init();
+               SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
+       }
+       
+       current_atcdialog->Update(dt);
+       
+       //cout << "Entering update..." << endl;
        //Traverse the list of active stations.
        //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
        //Eventually we should only update every so many steps.
-       //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
+       //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << endl;
        if(atc_list.size()) {
                if(atc_list_itr == atc_list.end()) {
                        atc_list_itr = atc_list.begin();
                }
-               (*atc_list_itr)->Update();
+               //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
+               //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
+               (*atc_list_itr)->Update(dt * atc_list.size());
+               //cout << "Done ATC update..." << endl;
                ++atc_list_itr;
        }
        
+       /*
+       cout << "ATC_LIST: " << atc_list.size() << ' ';
+       for(atc_list_iterator it = atc_list.begin(); it != atc_list.end(); it++) {
+               cout << (*it)->get_ident() << ' ';
+       }
+       cout << '\n';
+       */
+       
        // Search the tuned frequencies every now and then - this should be done with the event scheduler
-       static int i = 0;
+       static int i = 0;       // Very ugly - but there should only ever be one instance of FGATCMgr.
+       /*
+       if(i == 7) {
+               //cout << "About to AreaSearch()" << endl;
+               AreaSearch();
+       }
+       */
        if(i == 15) {
-               Search(1);
+               //cout << "About to search(1)" << endl;
+               FreqSearch(1);
        }
        if(i == 30) {
-               Search(2);
+               //cout << "About to search(2)" << endl;
+               FreqSearch(2);
                i = 0;
        }
        ++i;
+       
+       //cout << "comm1 type = " << comm_type[0] << '\n';
+       //cout << "Leaving update..." << endl;
 }
-/*
-// Remove from list only if not needed by the AI system
-void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp) {
-       AirportATC a;
-       if(GetAirportATCDetails((string)id, &a)) {
-               if(a.set_by_AI) {
-                       // Don't remove
-                       a.set_by_comm_search = false;
-                       airport_atc_map[(string)id] = a;
+
+
+// Returns frequency in KHz - should I alter this to return in MHz?
+unsigned short int FGATCMgr::GetFrequency(string ident, atc_type tp) {
+       ATCData test;
+       bool ok = current_commlist->FindByCode(ident, test, tp);
+       return(ok ? test.freq : 0);
+}      
+
+
+// Register the fact that the AI system wants to activate an airport
+// Might need more sophistication in this in the future - eg registration by aircraft call-sign.
+bool FGATCMgr::AIRegisterAirport(string ident) {
+       SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
+       //cout << "AI registered airport " << ident << " with the ATC system" << '\n';
+       if(airport_atc_map.find(ident) != airport_atc_map.end()) {
+               airport_atc_map[ident]->set_by_AI = true;
+               airport_atc_map[ident]->numAI++;
+               return(true);
+       } else {
+               FGAirport ap;
+               if(dclFindAirportID(ident, &ap)) {
+                       //cout << "ident = " << ident << '\n';
+                       AirportATC *a = new AirportATC;
+                       // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
+                       a->lon = ap.getLongitude();
+                       a->lat = ap.getLatitude();
+                       a->elev = ap.getElevation();
+                       a->atis_freq = GetFrequency(ident, ATIS);
+                       //cout << "ATIS freq = " << a->atis_freq << '\n';
+                       a->atis_active = false;
+                       a->tower_freq = GetFrequency(ident, TOWER);
+                       //cout << "Tower freq = " << a->tower_freq << '\n';
+                       a->tower_active = false;
+                       a->ground_freq = GetFrequency(ident, GROUND);
+                       //cout << "Ground freq = " << a->ground_freq << '\n';
+                       a->ground_active = false;
+                       // TODO - some airports will have a tower/ground frequency but be inactive overnight.
+                       a->set_by_AI = true;
+                       a->numAI = 1;
+                       airport_atc_map[ident] = a;
+                       return(true);
+               } else {
+                       SG_LOG(SG_ATC, SG_ALERT, "ERROR - can't find airport " << ident << " in AIRegisterAirport(...)");
+               }
+       }
+       return(false);
+}
+
+
+// Register the fact that the comm radio is tuned to an airport
+// Channel is zero based
+bool FGATCMgr::CommRegisterAirport(string ident, int chan, atc_type tp) {
+       SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
+       //cout << "Comm channel " << chan << " registered airport " << ident << ' ' << tp << '\n';
+       if(airport_atc_map.find(ident) != airport_atc_map.end()) {
+               //cout << "IN MAP - flagging set by comm..." << endl;
+               airport_atc_map[ident]->set_by_comm[chan][tp] = true;
+               if(tp == ATIS) {
+                       airport_atc_map[ident]->atis_active = true;
+               } else if(tp == TOWER) {
+                       airport_atc_map[ident]->tower_active = true;
+               } else if(tp == GROUND) {
+                       airport_atc_map[ident]->ground_active = true;
+               } else if(tp == APPROACH) {
+                       //a->approach_active = true;
+               }       // TODO - there *must* be a better way to do this!!!
+               return(true);
+       } else {
+               //cout << "NOT IN MAP - creating new..." << endl;
+               FGAirport ap;
+               if(dclFindAirportID(ident, &ap)) {
+                       AirportATC *a = new AirportATC;
+                       // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
+                       a->lon = ap.getLongitude();
+                       a->lat = ap.getLatitude();
+                       a->elev = ap.getElevation();
+                       a->atis_freq = GetFrequency(ident, ATIS);
+                       a->atis_active = false;
+                       a->tower_freq = GetFrequency(ident, TOWER);
+                       a->tower_active = false;
+                       a->ground_freq = GetFrequency(ident, GROUND);
+                       a->ground_active = false;
+                       if(tp == ATIS) {
+                               a->atis_active = true;
+                       } else if(tp == TOWER) {
+                               a->tower_active = true;
+                       } else if(tp == GROUND) {
+                               a->ground_active = true;
+                       } else if(tp == APPROACH) {
+                               //a->approach_active = true;
+                       }       // TODO - there *must* be a better way to do this!!!
+                       // TODO - some airports will have a tower/ground frequency but be inactive overnight.
+                       a->set_by_AI = false;
+                       a->numAI = 0;
+                       a->set_by_comm[chan][tp] = true;
+                       airport_atc_map[ident] = a;
+                       return(true);
+               }
+       }
+       return(false);
+}
+
+
+// Remove from list only if not needed by the AI system or the other comm channel
+// Note that chan is zero based.
+void FGATCMgr::CommRemoveFromList(string id, atc_type tp, int chan) {
+       SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan);
+       //cout << "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan << '\n';
+       if(airport_atc_map.find(id) != airport_atc_map.end()) {
+               AirportATC* a = airport_atc_map[id];
+               //cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
+               if(a->set_by_AI && tp != ATIS) {
+                       // Set by AI, so don't remove simply because user isn't tuned in any more - just stop displaying
+                       SG_LOG(SG_ATC, SG_BULK, "In CommRemoveFromList, service was set by AI\n");
+                       FGATC* aptr = GetATCPointer(id, tp);
+                       switch(chan) {
+                       case 0:
+                               //cout << "chan 1\n";
+                               a->set_by_comm[0][tp] = false;
+                               if(!a->set_by_comm[1][tp]) {
+                                       //cout << "not set by comm2\n";
+                                       if(aptr != NULL) {
+                                               //cout << "Got pointer\n";
+                                               aptr->SetNoDisplay();
+                                               //cout << "Setting no display...\n";
+                                       } else {
+                                               //cout << "Not got pointer\n";
+                                       }
+                               }
+                               break;
+                       case 1:
+                               a->set_by_comm[1][tp] = false;
+                               if(!a->set_by_comm[0][tp]) {
+                                       if(aptr != NULL) {
+                                               aptr->SetNoDisplay();
+                                               //cout << "Setting no display...\n";
+                                       }
+                               }
+                               break;
+                       }
+                       //airport_atc_map[id] = a;
                        return;
                } else {
-                       // remove
+                       switch(chan) {
+                       case 0:
+                               a->set_by_comm[0][tp] = false;
+                               // Remove only if not also set by the other comm channel
+                               if(!a->set_by_comm[1][tp]) {
+                                       a->tower_active = false;
+                                       a->ground_active = false;
+                                       RemoveFromList(id, tp);
+                               }
+                               break;
+                       case 1:
+                               a->set_by_comm[1][tp] = false;
+                               if(!a->set_by_comm[0][tp]) {
+                                       a->tower_active = false;
+                                       a->ground_active = false;
+                                       RemoveFromList(id, tp);
+                               }
+                               break;
+                       }
                }
        }
 }
-*/    
+    
 
 // Remove from list - should only be called from above or similar
-void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
-       //cout << "Requested type = " << tp << '\n';
-       //cout << "id = " << id << '\n';
-       atc_list_itr = atc_list.begin();
-       while(atc_list_itr != atc_list.end()) {
-               //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
-               //cout << "Ident = " << (*atc_list_itr)->GetIdent() << '\n';
-               if( (!strcmp((*atc_list_itr)->GetIdent(), id))
-                       && ((*atc_list_itr)->GetType() == tp) ) {
-                               //Before removing it stop it transmitting!!
-                               //cout << "OBLITERATING FROM LIST!!!\n";
-                               (*atc_list_itr)->SetNoDisplay();
-                               (*atc_list_itr)->Update();
-                               delete (*atc_list_itr);
-                               atc_list_itr = atc_list.erase(atc_list_itr);
-                               break;
-                       }  // Note that that can upset where we are in the list but that doesn't really matter
-               ++atc_list_itr;
+// This function *will* remove it from the list regardless of who else might want it.
+void FGATCMgr::RemoveFromList(string id, atc_type tp) {
+       //cout << "FGATCMgr::RemoveFromList called..." << endl;
+       //cout << "Requested type = " << tp << endl;
+       //cout << "id = " << id << endl;
+       atc_list_iterator it = atc_list.begin();
+       while(it != atc_list.end()) {
+               //cout << "type = " << (*it)->GetType() << '\n';
+               //cout << "Ident = " << (*it)->get_ident() << '\n';
+               if( ((*it)->get_ident() == id)
+                       && ((*it)->GetType() == tp) ) {
+                       //Before removing it stop it transmitting!!
+                       //cout << "OBLITERATING FROM LIST!!!\n";
+                       (*it)->SetNoDisplay();
+                       (*it)->Update(0.00833);
+                       delete (*it);
+                       atc_list.erase(it);
+                       atc_list_itr = atc_list.begin();        // Reset the persistent itr incase we've left it off the end.
+                       break;
+               }
+               ++it;
        }
 }
 
 
-//DCL - this routine untested so far.
 // Find in list - return a currently active ATC pointer given ICAO code and type
-FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
-       atc_list_itr = atc_list.begin();
-       while(atc_list_itr != atc_list.end()) {
-               if( (!strcmp((*atc_list_itr)->GetIdent(), id))
-               && ((*atc_list_itr)->GetType() == tp) ) {
-                       return(*atc_list_itr);
-               }       // Note that that can upset where we are in the list but that shouldn't really matter
-               ++atc_list_itr;
+// Return NULL if the given service is not in the list
+// - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
+FGATC* FGATCMgr::FindInList(string id, atc_type tp) {
+       //cout << "Entering FindInList for " << id << ' ' << tp << endl;
+       atc_list_iterator it = atc_list.begin();
+       while(it != atc_list.end()) {
+               if( ((*it)->get_ident() == id)
+                   && ((*it)->GetType() == tp) ) {
+                       return(*it);
+               }
+               ++it;
        }
-       // We need a fallback position
-       SG_LOG(SG_GENERAL, SG_ALERT, "*** Failed to find FGATC* in FGATCMgr::FindInList - this should not happen!");
+       // If we get here it's not in the list
+       //cout << "Couldn't find it in the list though :-(" << endl;
        return(NULL);
 }
 
@@ -336,39 +420,64 @@ bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
 
 
 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
-// ONLY CALL THIS FUNCTION AFTER FIRST CHECKING THE SERVICE EXISTS BY CALLING GetAirportATCDetails
-// FIXME - we really ought to take out the necessity for two function calls by simply returning
-// a NULL pointer if the service doesn't exist and requiring the caller to check for it (NULL).
+// Returns NULL if service doesn't exist - calling function should check for this.
+// We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
+// - at the moment all these GetATC... functions exposed are just too complicated.
 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
+       if(airport_atc_map.find(icao) == airport_atc_map.end()) {
+               //cout << "Unable to find " << icao << ' ' << type << " in the airport_atc_map" << endl;
+               return NULL;
+       }
+       //cout << "In GetATCPointer, found " << icao << ' ' << type << endl;
        AirportATC *a = airport_atc_map[icao];
        //cout << "a->lon = " << a->lon << '\n';
        //cout << "a->elev = " << a->elev << '\n';
        //cout << "a->tower_freq = " << a->tower_freq << '\n';
        switch(type) {
-               case TOWER:
+       case TOWER:
                if(a->tower_active) {
                        // Get the pointer from the list
-                       return(FindInList(icao.c_str(), type)); // DCL - this untested so far.
+                       return(FindInList(icao, type));
                } else {
-                       FGTower* t = new FGTower;
-                       if(current_towerlist->query(a->lon, a->lat, a->elev, a->tower_freq, &tower)) {
-                               *t = tower;
+                       ATCData data;
+                       if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
+                               FGTower* t = new FGTower;
+                               t->SetData(&data);
                                atc_list.push_back(t);
                                a->tower_active = true;
                                airport_atc_map[icao] = a;
+                               //cout << "Initing tower " << icao << " in GetATCPointer()\n";
+                               t->Init();
                                return(t);
                        } else {
-                               cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
+                               SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
                        }
                }
                break;
-               // Lets add the rest to get rid of the compiler warnings even though we don't need them yet.
-               case APPROACH:
+       case APPROACH:
                break;
-               case ATIS:
-               SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
+       case ATIS:
+               SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
                break;
-               case GROUND:
+       case GROUND:
+               //cout << "IN CASE GROUND" << endl;
+               if(a->ground_active) {
+                       // Get the pointer from the list
+                       return(FindInList(icao, type));
+               } else {
+                       ATCData data;
+                       if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
+                               FGGround* g = new FGGround;
+                               g->SetData(&data);
+                               atc_list.push_back(g);
+                               a->ground_active = true;
+                               airport_atc_map[icao] = a;
+                               g->Init();
+                               return(g);
+                       } else {
+                               SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
+                       }
+               }
                break;
                case INVALID:
                break;
@@ -378,414 +487,227 @@ FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
                break;
        }
        
-       SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
+       SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
+       //cout << "ERROR IN FGATCMgr - reached end of GetATCPointer" << endl;
        
        return(NULL);
 }
 
-
-// Render a transmission
-// Outputs the transmission either on screen or as audio depending on user preference
-// The refname is a string to identify this sample to the sound manager
-// The repeating flag indicates whether the message should be repeated continuously or played once.
-void FGATCMgr::Render(string msg, string refname, bool repeating) {
-#ifdef ENABLE_AUDIO_SUPPORT
-       voice = voiceOK && fgGetBool("/sim/sound/audible");
+// Return a pointer to an appropriate voice for a given type of ATC
+// creating the voice if necessary - ie. make sure exactly one copy
+// of every voice in use exists in memory.
+//
+// TODO - in the future this will get more complex and dole out country/airport
+// specific voices, and possible make sure that the same voice doesn't get used
+// at different airports in quick succession if a large enough selection are available.
+FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
+       // TODO - implement me better - maintain a list of loaded voices and other voices!!
        if(voice) {
-               int len;
-               unsigned char* buf = v1.WriteMessage((char*)msg.c_str(), len, voice);
-               if(voice) {
-                       FGSimpleSound* simple = new FGSimpleSound(buf, len);
-                       simple->set_volume(2.0);
-                       globals->get_soundmgr()->add(simple, refname);
-                       if(repeating) {
-                               globals->get_soundmgr()->play_looped(refname);
-                       } else {
-                               globals->get_soundmgr()->play_once(refname);
-                       }
-               }
-               delete[] buf;
-       }
-#endif // ENABLE_AUDIO_SUPPORT
-       if(!voice) {
-               // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
-               for(unsigned int i = 0; i < msg.length(); ++i) {
-                       if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
-                               msg[i] = ' ';
+               switch(type) {
+               case ATIS:
+                       if(voiceOK) {
+                               return(v1);
                        }
+               case TOWER:
+                       return(NULL);
+               case APPROACH:
+                       return(NULL);
+               case GROUND:
+                       return(NULL);
+               default:
+                       return(NULL);
                }
-               globals->get_ATC_display()->RegisterRepeatingMessage(msg);
-       }
-       playing = true; 
-}
-
-
-// Cease rendering a transmission.
-void FGATCMgr::NoRender(string refname) {
-       if(playing) {
-               if(voice) {
-#ifdef ENABLE_AUDIO_SUPPORT            
-                       globals->get_soundmgr()->stop(refname);
-                       globals->get_soundmgr()->remove(refname);
-#endif
-               } else {
-                       globals->get_ATC_display()->CancelRepeatingMessage();
-               }
-               playing = false;
+               return(NULL);
+       } else {
+               return(NULL);
        }
 }
 
+// Search for ATC stations by frequency
+void FGATCMgr::FreqSearch(int channel) {
+       int chan = channel - 1;         // Convert to zero-based for the arrays
 
-// Display a dialog box with options relevant to the currently tuned ATC service.
-void FGATCMgr::doStandardDialog() {
-       /* DCL 2002/12/06 - This function currently in development 
-          and dosen't display anything usefull to the end-user */
-       //cout << "FGATCMgr::doStandardDialog called..." << endl;
-       
-       // First - need to determine which ATC service (if any) the user is tuned to.
-       //cout << "comm1_type = " << comm1_type << endl;
-       
-       // Second - customise the dialog box
-       switch(comm1_type) {
-       case INVALID:
-               atcDialogCommunicationOptions->newList(NULL);
-               atcDialogMessage->setLabel("Not tuned in to any ATC service.");
-               break;
-       case ATIS:
-               atcDialogCommunicationOptions->newList(NULL);
-               atcDialogMessage->setLabel("Tuned in to ATIS: no communication possible.");
-               break;
-       case TOWER: 
-               atcDialogCommunicationOptions->newList(towerOptions);
-               atcDialogMessage->setLabel("Tuned in to Tower - select communication to transmit:");
-               break;
-       case GROUND:
-               atcDialogCommunicationOptions->newList(NULL);
-               atcDialogMessage->setLabel("Tuned in to Ground - select communication to transmit:");
-               break;
-       case APPROACH:
-               atcDialogCommunicationOptions->newList(approachOptions);
-               atcDialogMessage->setLabel("Tuned in to Approach - select communication to transmit:");
-               break;
-       default:
-               atcDialogCommunicationOptions->newList(NULL);
-               atcDialogMessage->setLabel("Tuned in to unknown ATC service - enter transmission:");
-               break;
-       }
-       
-       // Third - display the dialog without pausing sim.
-       ATCDialog(NULL);
-       
-       // Forth - need to direct input back from the dialog to the relevant ATC service.
-       // This is in ATCDialogOK()
-}
-
-////////////////////////////////////////////////////////////////
-//
-// TODO - The whole ATC frequency storage and search is really
-//        really ugly and needs reworking at some point.
-//
-////////////////////////////////////////////////////////////////
-// Search the specified comm channel (1 or 2)
-void FGATCMgr::Search(int chan) {
+       ATCData data;   
+       double freq = comm_node[chan]->getDoubleValue();
+       lon = lon_node->getDoubleValue();
+       lat = lat_node->getDoubleValue();
+       elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
        
-       if(chan == 1) {
-               ////////////////////////////////////////////////////////////////////////
-               // Comm1.
-               ////////////////////////////////////////////////////////////////////////
-               //cout << "In FGATCMgr::Search() - atc_list.size = " << atc_list.size() << '\n';
-               
-               comm1_freq = comm1_node->getDoubleValue();
-               //cout << "************* comm1_freq = " << comm1_freq << '\n';
-               double lon = lon_node->getDoubleValue();
-               double lat = lat_node->getDoubleValue();
-               double elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
-               
-               // We must be able to generalise some of the repetetive searching below!
+       // Query the data store and get the closest match if any
+       if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
+               // We have a match
+               // What's the logic?
+               // If this channel not previously valid then easy - add ATC to list
+               // If this channel was valid then - Have we tuned to a different service?
+               // If so - de-register one and add the other
+               if(comm_valid[chan]) {
+                       if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
+                               // Then we're still tuned into the same service so do nought and return
+                               return;
+                       } else {
+                               // Something's changed - either the location or the service type
+                               // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
+                               CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
+                       }
+               }
+               // At this point we can assume that we need to add the service.
+               comm_ident[chan] = data.ident;
+               comm_type[chan] = data.type;
+               comm_x[chan] = (double)data.x;
+               comm_y[chan] = (double)data.y;
+               comm_z[chan] = (double)data.z;
+               comm_lon[chan] = (double)data.lon;
+               comm_lat[chan] = (double)data.lat;
+               comm_elev[chan] = (double)data.elev;
+               comm_valid[chan] = true;
                
-               //Search for ATIS first
-               if(current_atislist->query(lon, lat, elev, comm1_freq, &atis)) {
-                       //cout << "atis found in radiostack search !!!!" << endl;
-                       //cout << "last_comm1_atis_ident = " << last_comm1_atis_ident << '\n';
-                       //cout << "comm1_type " << comm1_type << '\n';
-                       comm1_atis_ident = atis.GetIdent();
-                       comm1_atis_valid = true;
-                       if(last_comm1_atis_ident != comm1_atis_ident) {
-                               if(last_comm1_atis_ident != "") {
-                                       RemoveFromList(last_comm1_atis_ident, ATIS);
-                               }
-                               last_comm1_atis_ident = comm1_atis_ident;
-                               //cout << "last_comm1_atis_ident = " << last_comm1_atis_ident << '\n';
-                               comm1_type = ATIS;
-                               comm1_elev = atis.get_elev();
-                               comm1_range = FG_ATIS_DEFAULT_RANGE;
-                               comm1_effective_range = comm1_range;
-                               comm1_x = atis.get_x();
-                               comm1_y = atis.get_y();
-                               comm1_z = atis.get_z();
+               // This was a switch-case statement but the compiler didn't like the new variable creation with it. 
+               if(comm_type[chan] == ATIS) {
+                       CommRegisterAirport(comm_ident[chan], chan, ATIS);
+                       FGATC* app = FindInList(comm_ident[chan], ATIS);
+                       if(app != NULL) {
+                               // The station is already in the ATC list
+                               //cout << "In list - flagging SetDisplay..." << endl;
+                               comm_atc_ptr[chan] = app;
+                               app->SetDisplay();
+                       } else {
+                               // Generate the station and put in the ATC list
+                               //cout << "Not in list - generating..." << endl;
                                FGATIS* a = new FGATIS;
-                               *a = atis;
-                               comm1_atc_ptr = a;
+                               a->SetData(&data);
+                               comm_atc_ptr[chan] = a;
                                a->SetDisplay();
-                               a->set_refname("atis1");
+                               //a->Init();
                                atc_list.push_back(a);
-                               //cout << "Found a new atis station in range" << endl;
-                               //cout << " id = " << atis.GetIdent() << endl;
-                               return;  //This rather assumes that we never have more than one type of station in range.
-                       }
-               } else {
-                       if(comm1_atis_valid) {
-                               //cout << "Removing ATIS " << comm1_atis_ident << " from list\n";
-                               RemoveFromList(comm1_atis_ident, ATIS);
-                               comm1_atis_valid = false;
-                               if(comm1_type == ATIS) {
-                                       comm1_type = INVALID;
-                               }
-                               comm1_atis_ident = "";
-                               //comm1_trans_ident = "";
-                               last_comm1_atis_ident = "";
-                               comm1_atc_ptr = NULL;
                        }
-                       //cout << "not picking up atis" << endl;
-               }
-               
-               //Next search for tower
-               //cout << "comm1_freq = " << comm1_freq << '\n';
-               if(current_towerlist->query(lon, lat, elev, comm1_freq, &tower)) {
-                       //cout << "tower found in radiostack search !!!!" << endl;
-                       comm1_tower_ident = tower.GetIdent();
-                       //cout << "comm1_tower_ident = " << comm1_tower_ident << '\n';
-                       comm1_tower_valid = true;
-                       if(last_comm1_tower_ident != comm1_tower_ident) {
-                               if(last_comm1_tower_ident != "") {
-                                       RemoveFromList(last_comm1_tower_ident, TOWER);
-                               }
-                               last_comm1_tower_ident = comm1_tower_ident;
-                               comm1_type = TOWER;
-                               comm1_elev = tower.get_elev();
-                               comm1_range = FG_TOWER_DEFAULT_RANGE;
-                               comm1_effective_range = comm1_range;
-                               comm1_x = tower.get_x();
-                               comm1_y = tower.get_y();
-                               comm1_z = tower.get_z();
+               } else if (comm_type[chan] == TOWER) {
+                       //cout << "TOWER TOWER TOWER\n";
+                       CommRegisterAirport(comm_ident[chan], chan, TOWER);
+                       //cout << "Done (TOWER)" << endl;
+                       FGATC* app = FindInList(comm_ident[chan], TOWER);
+                       if(app != NULL) {
+                               // The station is already in the ATC list
+                               SG_LOG(SG_GENERAL, SG_DEBUG, comm_ident[chan] << " is in list - flagging SetDisplay...");
+                               //cout << comm_ident[chan] << " is in list - flagging SetDisplay...\n";
+                               comm_atc_ptr[chan] = app;
+                               app->SetDisplay();
+                       } else {
+                               // Generate the station and put in the ATC list
+                               SG_LOG(SG_GENERAL, SG_DEBUG, comm_ident[chan] << " is not in list - generating...");
+                               //cout << comm_ident[chan] << " is not in list - generating...\n";
                                FGTower* t = new FGTower;
-                               *t = tower;
-                               comm1_atc_ptr = t;
+                               t->SetData(&data);
+                               comm_atc_ptr[chan] = t;
+                               //cout << "Initing tower in FreqSearch()\n";
+                               t->Init();
                                t->SetDisplay();
                                atc_list.push_back(t);
-                               //cout << "Found a new tower station in range" << endl;
-                               //cout << " id = " << tower.GetIdent() << endl;
-                               return;  //This rather assumes that we never have more than one type of station in range.
                        }
-               } else {
-                       if(comm1_tower_valid) {
-                               //cout << "removing tower\n";
-                               RemoveFromList(comm1_tower_ident, TOWER);
-                               //comm1_valid = false;
-                               if(comm1_type == TOWER) {
-                                       comm1_type = INVALID;   // Only invalidate if we haven't switched it to something else
-                               }
-                               comm1_tower_valid = false;
-                               comm1_tower_ident = "";
-                               last_comm1_tower_ident = "";
-                               comm1_atc_ptr = NULL;
-                               //comm1_ident = "";
-                               //comm1_trans_ident = "";
-                               //last_comm1_ident = "";
-                       }
-                       //cout << "not picking up tower" << endl;
-               }
-               /*
-               //Next search for Ground control
-               if(current_groundlist->query(lon, lat, elev, comm1_freq, &ground)) {
-                       //cout << "Ground Control found in radiostack search !!!!" << endl;
-                       comm1_ident = ground.GetIdent();
-                       comm1_valid = true;
-                       if((last_comm1_ident != comm1_ident) || (comm1_type != GROUND)) {
-                               if(last_comm1_ident != "") {
-                                       RemoveFromList(last_comm1_ident, GROUND);
-                               }
-                               last_comm1_ident = comm1_ident;
-                               comm1_type = GROUND;
-                               comm1_elev = ground.get_elev();
-                               comm1_range = FG_GROUND_DEFAULT_RANGE;
-                               comm1_effective_range = comm1_range;
-                               comm1_x = ground.get_x();
-                               comm1_y = ground.get_y();
-                               comm1_z = ground.get_z();
+               }  else if (comm_type[chan] == GROUND) {
+                       CommRegisterAirport(comm_ident[chan], chan, GROUND);
+                       //cout << "Done (GROUND)" << endl;
+                       FGATC* app = FindInList(comm_ident[chan], GROUND);
+                       if(app != NULL) {
+                               // The station is already in the ATC list
+                               comm_atc_ptr[chan] = app;
+                               app->SetDisplay();
+                       } else {
+                               // Generate the station and put in the ATC list
                                FGGround* g = new FGGround;
-                               *g = ground;
+                               g->SetData(&data);
+                               comm_atc_ptr[chan] = g;
+                               g->Init();
                                g->SetDisplay();
                                atc_list.push_back(g);
-                               // For now we will automatically make contact with ground when the radio is tuned.
-                               // This rather assumes that the user tunes the radio at the appropriate place
-                               // (ie. having just turned off the runway) and only uses ground control on arrival
-                               // but its a start!
-                               g->NewArrival(current_plane);
-                               //cout << "Found a new ground station in range" << endl;
-                               //cout << " id = " << ground.GetIdent() << endl;
-                               return;  //This rather assumes that we never have more than one type of station in range.
-                       }
-               } else {
-                       if((comm1_valid) && (comm1_type == GROUND)) {
-                               RemoveFromList(comm1_ident, GROUND);
-                               comm1_valid = false;
-                               comm1_type = INVALID;
-                               comm1_ident = "";
-                               //comm1_trans_ident = "";
-                               last_comm1_ident = "";
-                       }
-                       //cout << "not picking up ground control" << endl;
-               }
-               */
-               // ================================================================================
-               // Search for Approach stations
-               // ================================================================================
-               // init number of approach stations reachable by plane
-               int  num_app = 0;
-               
-               // search stations in range
-               current_approachlist->query_bck(lon, lat, elev, approaches, max_app, num_app);
-               if (num_app != 0) {
-                       //cout << num_app << " approaches found in radiostack search !!!!" << endl;
-                       
-                       for ( int i=0; i<num_app; i++ ) {
-                               bool new_app = true;
-                               approach_ident = approaches[i].GetIdent();
-                               
-                               // check if station already exists on ATC stack
-                               atc_list_itr = atc_list.begin();
-                               while(atc_list_itr != atc_list.end()) {
-                                       //cout << "ATC list: " << (*atc_list_itr)->GetIdent() << endl;
-                                       if((!strcmp((*atc_list_itr)->GetIdent(), approach_ident))
-                                               && ((*atc_list_itr)->GetType() == APPROACH) ) {
-                                                       new_app = false;
-                                                       string pid = "Player";
-                                                       (*atc_list_itr)->AddPlane(pid);
-                                                       (*atc_list_itr)->Update();
-                                                       break;
-                                               }
-                                       ++atc_list_itr;
-                               }
-                               // generate new Approach on ATC stack
-                               if (new_app) {
-                                       FGApproach* a = new FGApproach;
-                                       *a = approaches[i];
-                                       string pid = "Player";
-                                       a->AddPlane(pid);
-                                       a->Update();
-                                       a->SetDisplay();
-                                       comm1_atc_ptr = a;
-                                       atc_list.push_back(a);
-                                       //cout << "Found a new approach station in range: Id = " 
-                                       //     << approaches[i].GetIdent() << endl;
-                               }
                        }
+               } else if (comm_type[chan] == APPROACH) {
+                       // We have to be a bit more carefull here since approaches are also searched by area
+                       CommRegisterAirport(comm_ident[chan], chan, APPROACH);
+                       //cout << "Done (APPROACH)" << endl;
+                       FGATC* app = FindInList(comm_ident[chan], APPROACH);
+                       if(app != NULL) {
+                               // The station is already in the ATC list
+                               app->AddPlane("Player");
+                               app->SetDisplay();
+                               comm_atc_ptr[chan] = app;
+                       } else {
+                               // Generate the station and put in the ATC list
+                               FGApproach* a = new FGApproach;
+                               a->SetData(&data);
+                               comm_atc_ptr[chan] = a;
+                               a->Init();
+                               a->SetDisplay();
+                               a->AddPlane("Player");
+                               atc_list.push_back(a);
+                       }                       
                }
-               
-               // remove planes which are out of range
-               atc_list_itr = atc_list.begin();
-               while(atc_list_itr != atc_list.end()) {
-                       if((*atc_list_itr)->GetType() == APPROACH ) {
-                               int np = (*atc_list_itr)->RemovePlane();
-                               // if approach has no planes left remove it from ATC list
-                               if ( np == 0) {
-                                       (*atc_list_itr)->SetNoDisplay();
-                                       (*atc_list_itr)->Update();
-                                       delete (*atc_list_itr);
-                                       atc_list_itr = atc_list.erase(atc_list_itr);
-                                       break;     // the other stations will be checked next time
-                               }
+       } else {
+               if(comm_valid[chan]) {
+                       if(comm_type[chan] != APPROACH) {
+                               // Currently approaches are removed by Alexander's out-of-range mechanism
+                               CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
                        }
-                       ++atc_list_itr;
+                       // Note that we *don't* call SetNoDisplay() here because the other comm channel
+                       // might be tuned into the same station - this is handled by CommRemoveFromList(...)
+                       comm_type[chan] = INVALID;
+                       comm_atc_ptr[chan] = NULL;
+                       comm_valid[chan] = false;
                }
+       }
+}
+
+// Search ATC stations by area in order that we appear 'on the radar'
+void FGATCMgr::AreaSearch() {
+       // Search for Approach stations
+       comm_list_type approaches;
+       comm_list_iterator app_itr;
+       
+       lon = lon_node->getDoubleValue();
+       lat = lat_node->getDoubleValue();
+       elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
+       
+       // search stations in range
+       int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
+       if (num_app != 0) {
+               //cout << num_app << " approaches found in radiostack search !!!!" << endl;
                
-       } else {        // chan = 2
-               
-               ////////////////////////////////////////////////////////////////////////
-               // Comm2.
-               ////////////////////////////////////////////////////////////////////////
-               //cout << "In FGATCMgr::Search() - atc_list.size = " << atc_list.size() << '\n';
-               
-               comm2_freq = comm2_node->getDoubleValue();
-               //cout << "************* comm1_freq = " << comm1_freq << '\n';
-               double lon = lon_node->getDoubleValue();
-               double lat = lat_node->getDoubleValue();
-               double elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
-               
-               if(current_atislist->query(lon, lat, elev, comm2_freq, &atis)) {
-                       comm2_atis_ident = atis.GetIdent();
-                       comm2_atis_valid = true;
-                       if(last_comm2_atis_ident != comm2_atis_ident) {
-                               if(last_comm2_atis_ident != "") {
-                                       RemoveFromList(last_comm2_atis_ident, ATIS);
-                               }
-                               last_comm2_atis_ident = comm2_atis_ident;
-                               comm2_type = ATIS;
-                               comm2_elev = atis.get_elev();
-                               comm2_range = FG_ATIS_DEFAULT_RANGE;
-                               comm2_effective_range = comm2_range;
-                               comm2_x = atis.get_x();
-                               comm2_y = atis.get_y();
-                               comm2_z = atis.get_z();
-                               FGATIS* a = new FGATIS;
-                               *a = atis;
-                               comm2_atc_ptr = a;
-                               a->SetDisplay();
-                               a->set_refname("atis2");
+               for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
+                       
+                       FGATC* app = FindInList(app_itr->ident, app_itr->type);
+                       if(app != NULL) {
+                               // The station is already in the ATC list
+                               //cout << "In list adding player\n";
+                               app->AddPlane("Player");
+                               //app->Update();
+                       } else {
+                               // Generate the station and put in the ATC list
+                               FGApproach* a = new FGApproach;
+                               a->SetData(&(*app_itr));
+                               //cout << "Adding player\n";
+                               a->AddPlane("Player");
+                               //a->Update();
                                atc_list.push_back(a);
-                               //cout << "Found a new atis station in range" << endl;
-                               //cout << " id = " << atis.GetIdent() << endl;
-                               return;  //This rather assumes that we never have more than one type of station in range.
-                       }
-               } else {
-                       if(comm2_atis_valid) {
-                               RemoveFromList(comm2_atis_ident, ATIS);
-                               comm2_atis_valid = false;
-                               if(comm2_type == ATIS) {
-                                       comm2_type = INVALID;
-                               }
-                               comm2_atis_ident = "";
-                               //comm2_trans_ident = "";
-                               last_comm2_atis_ident = "";
-                               comm2_atc_ptr = NULL;
                        }
                }
-               
-               if(current_towerlist->query(lon, lat, elev, comm2_freq, &tower)) {
-                       //cout << "tower found in radiostack search !!!!" << endl;
-                       comm2_tower_ident = tower.GetIdent();
-                       comm2_tower_valid = true;
-                       if(last_comm2_tower_ident != comm2_tower_ident) {
-                               if(last_comm2_tower_ident != "") {
-                                       RemoveFromList(last_comm2_tower_ident, TOWER);
-                               }
-                               last_comm2_tower_ident = comm2_tower_ident;
-                               comm2_type = TOWER;
-                               comm2_elev = tower.get_elev();
-                               comm2_range = FG_TOWER_DEFAULT_RANGE;
-                               comm2_effective_range = comm2_range;
-                               comm2_x = tower.get_x();
-                               comm2_y = tower.get_y();
-                               comm2_z = tower.get_z();
-                               FGTower* t = new FGTower;
-                               *t = tower;
-                               comm2_atc_ptr = t;
-                               t->SetDisplay();
-                               atc_list.push_back(t);
-                               return;
-                       }
-               } else {
-                       if(comm2_tower_valid) {
-                               RemoveFromList(comm2_tower_ident, TOWER);
-                               if(comm2_type == TOWER) {
-                                       comm2_type = INVALID;   // Only invalidate if we haven't switched it to something else
-                               }
-                               comm2_tower_valid = false;
-                               comm2_tower_ident = "";
-                               last_comm2_tower_ident = "";
-                               comm2_atc_ptr = NULL;
+       }
+       
+       // remove planes which are out of range
+       // TODO - I'm not entirely sure that this belongs here.
+       atc_list_iterator it = atc_list.begin();
+       while(it != atc_list.end()) {
+               if((*it)->GetType() == APPROACH ) {
+                       int np = (*it)->RemovePlane();
+                       // if approach has no planes left remove it from ATC list
+                       if ( np == 0) {
+                               //cout << "REMOVING AN APPROACH STATION WITH NO PLANES..." << endl;
+                               (*it)->SetNoDisplay();
+                               (*it)->Update(0.00833);
+                               delete (*it);
+                               atc_list.erase(it);
+                               atc_list_itr = atc_list.begin();        // Reset the persistent itr incase we've left it off the end.
+                               break;     // the other stations will be checked next time
                        }
                }
+               ++it;
        }
 }