]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/tower.cxx
try to fix yet another crash bug (don't worry, there's one for anybody!)
[flightgear.git] / src / ATC / tower.cxx
index 25801f5d63571c937ae841504f43388ee78bed28..b9982546120ff8aa0f28a6c7c2eebdfd3b5fc560 100644 (file)
@@ -16,7 +16,7 @@
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
@@ -34,7 +34,6 @@
 #include <simgear/debug/logstream.hxx>
 
 #include "tower.hxx"
-#include "ATCdisplay.hxx"
 #include "ATCmgr.hxx"
 #include "ATCutils.hxx"
 #include "ATCDialog.hxx"
@@ -191,7 +190,10 @@ FGTower::RemoveAllUserDialogOptions() really ought to be replaced by an ATCDialo
 At the moment planes in the lists are not guaranteed to always have a sensible ETA - it should be set as part of AddList functions, and lists should only be accessed this way. (FAIRLY MAJOR). 
 *******************************************/
 
-FGTower::FGTower() {
+FGTower::FGTower() :
+       separateGround(true),
+       ground(0)
+{
        ATCmgr = globals->get_ATC_mgr();
        
        _type = TOWER;
@@ -204,13 +206,11 @@ FGTower::FGTower() {
        update_count_max = 15;
        
        holdListItr = holdList.begin();
-       appList.clear();
        appListItr = appList.begin();
        depListItr = depList.begin();
        rwyListItr = rwyList.begin();
        circuitListItr = circuitList.begin();
        trafficListItr = trafficList.begin();
-       vacatedList.clear();
        vacatedListItr = vacatedList.begin();
        
        freqClear = true;
@@ -291,7 +291,7 @@ void FGTower::Init() {
        // TODO - attempt to get a departure control pointer to see if we need to hand off departing traffic to departure.
        
        // Get the airport elevation
-       aptElev = dclGetAirportElev(ident.c_str());
+       aptElev = fgGetAirportElev(ident.c_str());
        
        // TODO - this function only assumes one active rwy.
        DoRwyDetails();
@@ -311,6 +311,7 @@ void FGTower::Init() {
                t->planePtr = NULL;
                t->clearedToTakeOff = true;
                rwyList.push_back(t);
+               rwyListItr = rwyList.begin();
                departed = false;
        } else {
                //cout << "User not on active runway\n";
@@ -318,7 +319,7 @@ void FGTower::Init() {
                // TODO FIXME - this will break when user starts on apron, at hold short, etc.
                if(!OnAnyRunway(Point3D(user_lon_node->getDoubleValue(), user_lat_node->getDoubleValue(), 0.0))) {
                        //cout << ident << "  ADD 0\n";
-                       current_atcdialog->add_entry(ident, "@AP Tower @CS @MI miles @CD of the airport for full stop with the ATIS", "Contact tower for VFR arrival (full stop)", TOWER, (int)USER_REQUEST_VFR_ARRIVAL_FULL_STOP);
+                       current_atcdialog->add_entry(ident, "@AP Tower, @CS @MI miles @CD of the airport for full stop with ATIS", "Contact tower for VFR arrival (full stop)", TOWER, (int)USER_REQUEST_VFR_ARRIVAL_FULL_STOP);
                }
        }
 }
@@ -477,7 +478,7 @@ void FGTower::Respond() {
                        string trns = t->plane.callsign;
                        trns += " ";
                        trns += name;
-                       trns += " Tower";
+                       trns += " Tower,";
                        // Should we clear staight in or for downwind entry?
                        // For now we'll clear straight in if greater than 1km from a line drawn through the threshold perpendicular to the rwy.
                        // Later on we might check the actual heading and direct some of those to enter on downwind or base.
@@ -486,7 +487,7 @@ void FGTower::Respond() {
                                trns += " Report three mile straight-in runway ";
                                t->opType = STRAIGHT_IN;
                                if(t->isUser) {
-                                       current_atcdialog->add_entry(ident, "@AP Tower @CS @MI mile final Runway @RW", "Report Final", TOWER, (int)USER_REPORT_3_MILE_FINAL);
+                                       current_atcdialog->add_entry(ident, "@AP Tower, @CS @MI mile final Runway @RW", "Report Final", TOWER, (int)USER_REPORT_3_MILE_FINAL);
                                } else {
                                        t->planePtr->RegisterTransmission(14);
                                }
@@ -501,14 +502,13 @@ void FGTower::Respond() {
                                t->opType = CIRCUIT;
                                // leave it in the app list until it gets into pattern though.
                                if(t->isUser) {
-                                       current_atcdialog->add_entry(ident, "@AP Tower @CS Downwind @RW", "Report Downwind", TOWER, (int)USER_REPORT_DOWNWIND);
+                                       current_atcdialog->add_entry(ident, "@AP Tower, @CS Downwind @RW", "Report Downwind", TOWER, (int)USER_REPORT_DOWNWIND);
                                } else {
                                        t->planePtr->RegisterTransmission(15);
                                }
                        }
                        trns += ConvertRwyNumToSpokenString(activeRwy);
                        if(_display) {
-                               //globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
                                pending_transmission = trns;
                                Transmit();
                        } else {
@@ -528,10 +528,11 @@ void FGTower::Respond() {
                                        ClearHoldingPlane(t);
                                        t->leg = TAKEOFF_ROLL;
                                        rwyList.push_back(t);
+                                       rwyListItr = rwyList.begin();
                                        rwyOccupied = true;
                                        // WARNING - WE ARE ASSUMING ONLY ONE PLANE REPORTING HOLD AT A TIME BELOW
                                        // FIXME TODO - FIX THIS!!!
-                                       if(holdList.size()) {
+                                       if(!holdList.empty()) {
                                                if(holdListItr == holdList.end()) {
                                                        holdListItr = holdList.begin();
                                                }
@@ -545,7 +546,6 @@ void FGTower::Respond() {
                                string trns = t->plane.callsign;
                                trns += " hold position";
                                if(_display) {
-                                       //globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
                                        pending_transmission = trns;
                                        Transmit();
                                }
@@ -585,7 +585,6 @@ void FGTower::Respond() {
                                t->clearedToLand = false;
                        }
                        if(_display && disp) {
-                               //globals->get_ATC_display()->RegisterSingleMessage(trns);
                                pending_transmission = trns;
                                Transmit();
                        }
@@ -632,7 +631,7 @@ void FGTower::ProcessDownwindReport(TowerPlaneRec* t) {
        // This assumes that the number spoken is landing position, not circuit position, since some of the traffic might be on straight-in final.
        trns += " ";
        TowerPlaneRec* tt = NULL;
-       if((i == 1) && (!rwyList.size()) && (t->nextOnRwy) && (!cf)) {  // Unfortunately nextOnRwy currently doesn't handle circuit/straight-in ordering properly at present, hence the cf check below.
+       if((i == 1) && rwyList.empty() && (t->nextOnRwy) && (!cf)) {    // Unfortunately nextOnRwy currently doesn't handle circuit/straight-in ordering properly at present, hence the cf check below.
                trns += "Cleared to land";      // TODO - clear for the option if appropriate
                t->clearedToLand = true;
                if(!t->isUser) t->planePtr->RegisterTransmission(7);
@@ -656,7 +655,7 @@ void FGTower::ProcessDownwindReport(TowerPlaneRec* t) {
                trns += s;
                if((tt->opType) == CIRCUIT) {
                        PatternLeg leg;
-                       if(t->isUser) {
+                       if(tt->isUser) {
                                leg = tt->leg;
                        } else {
                                leg = tt->planePtr->GetLeg();
@@ -682,7 +681,6 @@ void FGTower::ProcessDownwindReport(TowerPlaneRec* t) {
                }
        }
        if(_display) {
-               //globals->get_ATC_display()->RegisterSingleMessage(trns);
                pending_transmission = trns;
                Transmit();
        }
@@ -711,12 +709,11 @@ void FGTower::ProcessRunwayVacatedReport(TowerPlaneRec* t) {
                if(!t->isUser) t->planePtr->RegisterTransmission(5);
        } else {
                // Cop-out!!
-               trns += " cleared for taxi to the GA parking";
+               trns += " cleared for taxi to general aviation parking";
                if(!t->isUser) t->planePtr->RegisterTransmission(6);    // TODO - this is a mega-hack!!
        }
        //cout << "trns = " << trns << '\n';
        if(_display) {
-               //globals->get_ATC_display()->RegisterSingleMessage(trns);
                pending_transmission = trns;
                Transmit();
        }
@@ -829,7 +826,6 @@ void FGTower::ClearHoldingPlane(TowerPlaneRec* t) {
                timeSinceLastDeparture = 0.0;
        }
        if(_display) {
-               //globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
                pending_transmission = trns;
                Transmit();
        }
@@ -843,7 +839,7 @@ void FGTower::ClearHoldingPlane(TowerPlaneRec* t) {
 // Do one plane from the hold list
 void FGTower::CheckHoldList(double dt) {
        //cout << "Entering CheckHoldList..." << endl;
-       if(holdList.size()) {
+       if(!holdList.empty()) {
                //cout << "*holdListItr = " << *holdListItr << endl;
                if(holdListItr == holdList.end()) {
                        holdListItr = holdList.begin();
@@ -866,9 +862,12 @@ void FGTower::CheckHoldList(double dt) {
                                        ClearHoldingPlane(t);
                                        t->leg = TAKEOFF_ROLL;
                                        rwyList.push_back(t);
+                                       rwyListItr = rwyList.begin();
                                        rwyOccupied = true;
                                        holdList.erase(holdListItr);
                                        holdListItr = holdList.begin();
+                                       if (holdList.empty())
+                                         return;
                                }
                        }
                        // TODO - rationalise the considerable code duplication above!
@@ -886,7 +885,7 @@ void FGTower::CheckCircuitList(double dt) {
        downwind_leg_pos = 0.0;
        crosswind_leg_pos = 0.0;
        
-       if(circuitList.size()) {        // Do one plane from the circuit
+       if(!circuitList.empty()) {      // Do one plane from the circuit
                if(circuitListItr == circuitList.end()) {
                        circuitListItr = circuitList.begin();
                }
@@ -928,18 +927,21 @@ void FGTower::CheckCircuitList(double dt) {
                                                        t->opType = OUTBOUND;   // TODO - could check if the user has climbed significantly above circuit altitude as well.
                                                        // Since we are unknown operation we should be in depList already.
                                                        //cout << ident << " Removing user from circuitList (TTT_UNKNOWN)\n";
-                                                       circuitList.erase(circuitListItr);
+                                                       circuitListItr = circuitList.erase(circuitListItr);
                                                        RemoveFromTrafficList(t->plane.callsign);
-                                                       circuitListItr = circuitList.begin();
+                                                       if (circuitList.empty())
+                                                           return;
                                                }
                                        } else if(t->opType == CIRCUIT) {
                                                if(tortho.y() > 10000) {
                                                        // 10 km out - assume the user has abandoned the circuit!!
                                                        t->opType = OUTBOUND;
                                                        depList.push_back(t);
+                                                       depListItr = depList.begin();
                                                        //cout << ident << " removing user from circuitList (CIRCUIT)\n";
-                                                       circuitList.erase(circuitListItr);
-                                                       circuitListItr = circuitList.begin();
+                                                       circuitListItr = circuitList.erase(circuitListItr);
+                                                       if (circuitList.empty())
+                                                         return;
                                                }
                                        }
                                }
@@ -1109,6 +1111,8 @@ void FGTower::CheckCircuitList(double dt) {
                        circuitListItr = circuitList.erase(circuitListItr);
                        if(circuitListItr == circuitList.end() ) {
                                circuitListItr = circuitList.begin();
+                               // avoid increment of circuitListItr (would increment to second element, or crash if no element left)
+                               return;
                        }
                }
                ++circuitListItr;
@@ -1150,20 +1154,24 @@ void FGTower::CheckRunwayList(double dt) {
                                        // It's possible we could be a bit more proactive about this.
                                } else if(t->opType == OUTBOUND) {
                                        depList.push_back(t);
+                                       depListItr = depList.begin();
                                        rwyList.pop_front();
                                        departed = true;
                                        timeSinceLastDeparture = 0.0;
                                } else if(t->opType == CIRCUIT) {
                                        //cout << ident << " adding " << t->plane.callsign << " to circuitList" << endl;
                                        circuitList.push_back(t);
+                                       circuitListItr = circuitList.begin();
                                        AddToTrafficList(t);
                                        rwyList.pop_front();
                                        departed = true;
                                        timeSinceLastDeparture = 0.0;
                                } else if(t->opType == TTT_UNKNOWN) {
                                        depList.push_back(t);
+                                       depListItr = depList.begin();
                                        //cout << ident << " adding " << t->plane.callsign << " to circuitList" << endl;
                                        circuitList.push_back(t);
+                                       circuitListItr = circuitList.begin();
                                        AddToTrafficList(t);
                                        rwyList.pop_front();
                                        departed = true;
@@ -1181,7 +1189,7 @@ void FGTower::CheckRunwayList(double dt) {
 void FGTower::CheckApproachList(double dt) {
        //cout << "CheckApproachList called for " << ident << endl;
        //cout << "AppList.size is " << appList.size() << endl;
-       if(appList.size()) {
+       if(!appList.empty()) {
                if(appListItr == appList.end()) {
                        appListItr = appList.begin();
                }
@@ -1279,6 +1287,9 @@ void FGTower::CheckApproachList(double dt) {
                        if(appListItr == appList.end() ) {
                                appListItr = appList.begin();
                        }
+                       if (appList.empty())
+                         return;
+
                }
                
                ++appListItr;
@@ -1288,7 +1299,7 @@ void FGTower::CheckApproachList(double dt) {
 
 // Do one plane from the departure list
 void FGTower::CheckDepartureList(double dt) {
-       if(depList.size()) {
+       if(!depList.empty()) {
                if(depListItr == depList.end()) {
                        depListItr = depList.begin();
                }
@@ -1308,7 +1319,7 @@ void FGTower::CheckDepartureList(double dt) {
                                // Change the communication options
                                RemoveAllUserDialogOptions();
                                //cout << "ADD A\n";
-                               current_atcdialog->add_entry(ident, "@AP Tower @CS @MI miles @CD of the airport for full stop with the ATIS", "Contact tower for VFR arrival (full stop)", TOWER, (int)USER_REQUEST_VFR_ARRIVAL_FULL_STOP);
+                               current_atcdialog->add_entry(ident, "@AP Tower, @CS @MI miles @CD of the airport for full stop with ATIS", "Contact tower for VFR arrival (full stop)", TOWER, (int)USER_REQUEST_VFR_ARRIVAL_FULL_STOP);
                        } else {
                                // Send a clear-of-airspace signal
                                // TODO - implement this once we actually have departing AI traffic (currently all circuits or arrivals).
@@ -1641,10 +1652,12 @@ bool FGTower::AddToCircuitList(TowerPlaneRec* t) {
                        // It depends on what the two planes are doing and whether there's a conflict what we do.
                        if(tpr->eta - t->eta > separation_time) {       // No probs, plane 2 can squeeze in before plane 1 with no apparent conflict
                                circuitList.insert(twrItr, t);
+                               circuitListItr = circuitList.begin();
                        } else {        // Ooops - this ones tricky - we have a potential conflict!
                                conflict = true;
                                // HACK - just add anyway for now and flag conflict.
                                circuitList.insert(twrItr, t);
+                               circuitListItr = circuitList.begin();
                        }
                        //cout << "\tC\t" << circuitList.size() << '\n';
                        return(conflict);
@@ -1653,6 +1666,7 @@ bool FGTower::AddToCircuitList(TowerPlaneRec* t) {
        // If we get here we must be at the end of the list, or maybe the list is empty.
        //cout << ident << " adding " << t->plane.callsign << " to circuitList" << endl;
        circuitList.push_back(t);       // TODO - check the separation with the preceding plane for the conflict flag.
+       circuitListItr = circuitList.begin();
        //cout << "\tE\t" << circuitList.size() << endl;
        return(conflict);
 }
@@ -2005,6 +2019,7 @@ void FGTower::VFRArrivalContact(const string& ID, const LandingType& opt) {
        responseReqd = true;
        
        appList.push_back(t);   // Not necessarily permanent
+       appListItr = appList.begin();
        AddToTrafficList(t);
        
        current_atcdialog->remove_entry(ident, USER_REQUEST_VFR_ARRIVAL, TOWER);
@@ -2034,6 +2049,7 @@ void FGTower::VFRArrivalContact(const PlaneRec& plane, FGAIPlane* requestee, con
        
        //cout << "Before adding, appList.size = " << appList.size() << " at " << ident << '\n';
        appList.push_back(t);   // Not necessarily permanent
+       appListItr = appList.begin();
        //cout << "After adding, appList.size = " << appList.size() << " at " << ident << '\n';
        AddToTrafficList(t);
 }
@@ -2146,61 +2162,64 @@ void FGTower::RemovePlane(const string& ID) {
        // but we can only delete it once, AT THE END.
        TowerPlaneRec* t = NULL;
        tower_plane_rec_list_iterator twrItr;
-       for(twrItr = appList.begin(); twrItr != appList.end(); twrItr++) {
+       for(twrItr = appList.begin(); twrItr != appList.end();) {
                if((*twrItr)->plane.callsign == ID) {
                        t = *twrItr;
                        twrItr = appList.erase(twrItr);
                        appListItr = appList.begin();
-                       break;
-               }
+                       // HACK: aircraft are sometimes more than once in a list, so we need to
+                       // remove them all before we can delete the TowerPlaneRec class
+                       //break;
+               } else
+                        ++twrItr;
        }
-       for(twrItr = depList.begin(); twrItr != depList.end(); twrItr++) {
+       for(twrItr = depList.begin(); twrItr != depList.end();) {
                if((*twrItr)->plane.callsign == ID) {
                        t = *twrItr;
                        twrItr = depList.erase(twrItr);
                        depListItr = depList.begin();
-                       break;
-               }
+               } else
+                        ++twrItr;
        }
-       for(twrItr = circuitList.begin(); twrItr != circuitList.end(); twrItr++) {
+       for(twrItr = circuitList.begin(); twrItr != circuitList.end();) {
                if((*twrItr)->plane.callsign == ID) {
                        t = *twrItr;
                        twrItr = circuitList.erase(twrItr);
                        circuitListItr = circuitList.begin();
-                       break;
-               }
+                } else
+                        ++twrItr;
        }
-       for(twrItr = holdList.begin(); twrItr != holdList.end(); twrItr++) {
+       for(twrItr = holdList.begin(); twrItr != holdList.end();) {
                if((*twrItr)->plane.callsign == ID) {
                        t = *twrItr;
                        twrItr = holdList.erase(twrItr);
                        holdListItr = holdList.begin();
-                       break;
-               }
+                } else
+                        ++twrItr;
        }
-       for(twrItr = rwyList.begin(); twrItr != rwyList.end(); twrItr++) {
+       for(twrItr = rwyList.begin(); twrItr != rwyList.end();) {
                if((*twrItr)->plane.callsign == ID) {
                        t = *twrItr;
                        twrItr = rwyList.erase(twrItr);
                        rwyListItr = rwyList.begin();
-                       break;
-               }
+                } else
+                        ++twrItr;
        }
-       for(twrItr = vacatedList.begin(); twrItr != vacatedList.end(); twrItr++) {
+       for(twrItr = vacatedList.begin(); twrItr != vacatedList.end();) {
                if((*twrItr)->plane.callsign == ID) {
                        t = *twrItr;
                        twrItr = vacatedList.erase(twrItr);
                        vacatedListItr = vacatedList.begin();
-                       break;
-               }
+                } else
+                        ++twrItr;
        }
-       for(twrItr = trafficList.begin(); twrItr != trafficList.end(); twrItr++) {
+       for(twrItr = trafficList.begin(); twrItr != trafficList.end();) {
                if((*twrItr)->plane.callsign == ID) {
                        t = *twrItr;
                        twrItr = trafficList.erase(twrItr);
                        trafficListItr = trafficList.begin();
-                       break;
-               }
+                } else
+                        ++twrItr;
        }
        // And finally, delete the record.
        delete t;
@@ -2242,7 +2261,7 @@ void FGTower::ReportGoingAround(const string& ID) {
        if(ID == "USER") {
                uid = fgGetString("/sim/user/callsign");
                RemoveAllUserDialogOptions();   // TODO - it would be much more efficient if ATCDialog simply had a clear() function!!!
-               current_atcdialog->add_entry(ident, "@AP Tower @CS Downwind @RW", "Report Downwind", TOWER, (int)USER_REPORT_DOWNWIND);
+               current_atcdialog->add_entry(ident, "@AP Tower, @CS Downwind @RW", "Report Downwind", TOWER, (int)USER_REPORT_DOWNWIND);
        }
        TowerPlaneRec* t = FindPlane(uid);
        if(t) {
@@ -2415,7 +2434,7 @@ string FGTower::GenText(const string& m, int c) {
                        //break;
                //}
        //}
-       if ( mes != "" ) return mes;
+       if ( mes[0] ) return mes;
        else return "No transmission found";
 }