]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/ATCmgr.cxx
Mathias Fröhlich:
[flightgear.git] / src / ATC / ATCmgr.cxx
index acdf63d1416526dae5495132e952946b455a7982..8084b0822d1704b246c7f8ec2453498a992f5974 100644 (file)
@@ -48,6 +48,7 @@ AirportATC::AirportATC() :
     ground_active(false),
     set_by_AI(false),
        numAI(0)
+       //airport_atc_map.clear();
 {
        for(int i=0; i<ATC_NUM_TYPES; ++i) {
                set_by_comm[0][i] = false;
@@ -58,9 +59,9 @@ AirportATC::AirportATC() :
 FGATCMgr::FGATCMgr() {
        comm_ident[0] = "";
        comm_ident[1] = "";
-       last_comm_ident[0] = "";
-       last_comm_ident[1] = "";
-       approach_ident = "";
+       //last_comm_ident[0] = "";
+       //last_comm_ident[1] = "";
+       //approach_ident = "";
        last_in_range = false;
        comm_type[0] = INVALID;
        comm_type[1] = INVALID;
@@ -83,8 +84,10 @@ void FGATCMgr::unbind() {
 }
 
 void FGATCMgr::init() {
-       comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
-       comm_node[1] = 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);
@@ -102,6 +105,9 @@ void FGATCMgr::init() {
     current_commlist = new FGCommList;
     SGPath p_comm( globals->get_fg_root() );
     current_commlist->init( p_comm );
+       
+       // 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.
@@ -111,9 +117,10 @@ void FGATCMgr::init() {
        voiceOK = v1->LoadVoice("default");
        voice = true;
        
-       /* I've loaded the voice even if /sim/sound/audible is false
+       /* 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
@@ -131,13 +138,8 @@ void FGATCMgr::init() {
     current_atcdialog = new FGATCDialog;
     current_atcdialog->Init();
 
-       ATCDialogInit();
-       
-       // DCL - testing
-       //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
-       //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
-       
        initDone = true;
+       //cout << "ATCmgr::init done!" << endl;
 }
 
 void FGATCMgr::update(double dt) {
@@ -146,11 +148,13 @@ void FGATCMgr::update(double dt) {
                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();
@@ -162,6 +166,14 @@ void FGATCMgr::update(double dt) {
                ++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;       // Very ugly - but there should only ever be one instance of FGATCMgr.
        /*
@@ -198,6 +210,7 @@ unsigned short int FGATCMgr::GetFrequency(string ident, atc_type tp) {
 // 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++;
@@ -208,9 +221,9 @@ bool FGATCMgr::AIRegisterAirport(string ident) {
                        //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.longitude;
-                       a->lat = ap.latitude;
-                       a->elev = ap.elevation;
+                       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;
@@ -225,6 +238,8 @@ bool FGATCMgr::AIRegisterAirport(string ident) {
                        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);
@@ -235,9 +250,19 @@ bool FGATCMgr::AIRegisterAirport(string ident) {
 // 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;
@@ -245,19 +270,28 @@ bool FGATCMgr::CommRegisterAirport(string ident, int chan, atc_type tp) {
                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.longitude;
-                       a->lat = ap.latitude;
-                       a->elev = ap.elevation;
+                       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;                        
+                       a->set_by_comm[chan][tp] = true;
                        airport_atc_map[ident] = a;
                        return(true);
                }
@@ -268,15 +302,16 @@ bool FGATCMgr::CommRegisterAirport(string ident, int chan, atc_type tp) {
 
 // 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(const char* id, atc_type tp, int chan) {
+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);
-       if(airport_atc_map.find((string)id) != airport_atc_map.end()) {
-               AirportATC* a = airport_atc_map[(string)id];
+       //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((string)id, tp);
+                       FGATC* aptr = GetATCPointer(id, tp);
                        switch(chan) {
                        case 0:
                                //cout << "chan 1\n";
@@ -302,7 +337,7 @@ void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
                                }
                                break;
                        }
-                       airport_atc_map[(string)id] = a;
+                       //airport_atc_map[id] = a;
                        return;
                } else {
                        switch(chan) {
@@ -310,12 +345,16 @@ void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
                                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;
@@ -327,24 +366,26 @@ void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
 
 // Remove from list - should only be called from above or similar
 // This function *will* remove it from the list regardless of who else might want it.
-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)->get_ident() << '\n';
-               if( (!strcmp((*atc_list_itr)->get_ident(), 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(0.00833);
-                               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;
+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;
        }
 }
 
@@ -352,16 +393,18 @@ void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
 // Find in list - return a currently active ATC pointer given ICAO code and type
 // Return NULL if the given service is not in the list
 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
-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)->get_ident(), 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;
+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;
        }
        // If we get here it's not in the list
+       //cout << "Couldn't find it in the list though :-(" << endl;
        return(NULL);
 }
 
@@ -382,8 +425,10 @@ bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
 // - 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';
@@ -392,7 +437,7 @@ FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
        case TOWER:
                if(a->tower_active) {
                        // Get the pointer from the list
-                       return(FindInList(icao.c_str(), type));
+                       return(FindInList(icao, type));
                } else {
                        ATCData data;
                        if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
@@ -401,6 +446,7 @@ FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
                                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 {
@@ -417,7 +463,7 @@ FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
                //cout << "IN CASE GROUND" << endl;
                if(a->ground_active) {
                        // Get the pointer from the list
-                       return(FindInList(icao.c_str(), type));
+                       return(FindInList(icao, type));
                } else {
                        ATCData data;
                        if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
@@ -442,6 +488,7 @@ FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
        }
        
        SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
+       //cout << "ERROR IN FGATCMgr - reached end of GetATCPointer" << endl;
        
        return(NULL);
 }
@@ -476,11 +523,6 @@ FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
        }
 }
 
-// Display a dialog box with options relevant to the currently tuned ATC service.
-void FGATCMgr::doPopupDialog() {
-       ATCDoDialog(comm_type[0]);      // FIXME - currently hardwired to comm1
-}
-
 // Search for ATC stations by frequency
 void FGATCMgr::FreqSearch(int channel) {
        int chan = channel - 1;         // Convert to zero-based for the arrays
@@ -509,7 +551,7 @@ void FGATCMgr::FreqSearch(int channel) {
                        }
                }
                // At this point we can assume that we need to add the service.
-               comm_ident[chan] = (data.ident).c_str();
+               comm_ident[chan] = data.ident;
                comm_type[chan] = data.type;
                comm_x[chan] = (double)data.x;
                comm_y[chan] = (double)data.y;
@@ -526,6 +568,7 @@ void FGATCMgr::FreqSearch(int channel) {
                        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
@@ -538,21 +581,26 @@ void FGATCMgr::FreqSearch(int channel) {
                                atc_list.push_back(a);
                        }
                } 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
-                               //cout << "In list - flagging SetDisplay..." << endl;
+                               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
-                               //cout << "Not in list - generating..." << endl;
+                               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->SetData(&data);
                                comm_atc_ptr[chan] = t;
-                               t->SetDisplay();
+                               //cout << "Initing tower in FreqSearch()\n";
                                t->Init();
+                               t->SetDisplay();
                                atc_list.push_back(t);
                        }
                }  else if (comm_type[chan] == GROUND) {
@@ -561,14 +609,15 @@ void FGATCMgr::FreqSearch(int channel) {
                        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->SetData(&data);
                                comm_atc_ptr[chan] = g;
-                               g->SetDisplay();
                                g->Init();
+                               g->SetDisplay();
                                atc_list.push_back(g);
                        }
                } else if (comm_type[chan] == APPROACH) {
@@ -586,8 +635,8 @@ void FGATCMgr::FreqSearch(int channel) {
                                FGApproach* a = new FGApproach;
                                a->SetData(&data);
                                comm_atc_ptr[chan] = a;
-                               a->SetDisplay();
                                a->Init();
+                               a->SetDisplay();
                                a->AddPlane("Player");
                                atc_list.push_back(a);
                        }                       
@@ -624,7 +673,7 @@ void FGATCMgr::AreaSearch() {
                
                for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
                        
-                       FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
+                       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";
@@ -644,19 +693,21 @@ void FGATCMgr::AreaSearch() {
        
        // remove planes which are out of range
        // TODO - I'm not entirely sure that this belongs here.
-       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();
+       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) {
-                               (*atc_list_itr)->SetNoDisplay();
-                               (*atc_list_itr)->Update(0.00833);
-                               delete (*atc_list_itr);
-                               atc_list_itr = atc_list.erase(atc_list_itr);
+                               //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
                        }
                }
-               ++atc_list_itr;
+               ++it;
        }
 }