]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/trafficcontrol.cxx
Different fix for traffic shutdown crash.
[flightgear.git] / src / ATC / trafficcontrol.cxx
index 065658058b1a3d452bb09767825a5926ec7ea2a6..6b77890a0acc5d461ea85eb97c2c7fce92e53010 100644 (file)
@@ -36,6 +36,8 @@
 #include <simgear/scene/material/matlib.hxx>
 #include <simgear/scene/material/mat.hxx>
 #include <simgear/scene/util/OsgMath.hxx>
+#include <simgear/timing/sg_time.hxx>
+
 #include <Scenery/scenery.hxx>
 
 #include "trafficcontrol.hxx"
@@ -43,7 +45,6 @@
 #include <AIModel/AIAircraft.hxx>
 #include <AIModel/AIFlightPlan.hxx>
 #include <AIModel/performancedata.hxx>
-#include <AIModel/performancedb.hxx>
 #include <ATC/atc_mgr.hxx>
 #include <Traffic/TrafficMgr.hxx>
 #include <Airports/groundnetwork.hxx>
@@ -57,6 +58,30 @@ using std::string;
 using std::cout;
 using std::endl;
 
+namespace {
+
+TrafficVectorIterator findTraffic(TrafficVector& vec, int id)
+{
+    TrafficVectorIterator it = vec.begin();
+    for (; it != vec.end(); ++it) {
+        if (it->getId() == id) {
+            return it;
+        }
+    }
+
+    return it; // vec.end, effectively
+}
+
+void clearTrafficControllers(TrafficVector& vec)
+{
+    TrafficVectorIterator it = vec.begin();
+    for (; it != vec.end(); ++it) {
+        it->getAircraft()->clearATCController();
+    }
+}
+
+} // of anonymous namespace
+
 /***************************************************************************
  * ActiveRunway
  **************************************************************************/
@@ -133,7 +158,9 @@ time_t ActiveRunway::requestTimeSlot(time_t eta)
     estimatedArrivalTimes.push_back(newEta);
     sort(estimatedArrivalTimes.begin(), estimatedArrivalTimes.end());
     // do some housekeeping : remove any timestamps that are past
-    time_t now = time(NULL) + fgGetLong("/sim/time/warp");
+
+    time_t now = globals->get_time_params()->get_cur_time();
+
     TimeVectorIterator i = estimatedArrivalTimes.begin();
     while (i != estimatedArrivalTimes.end()) {
         if ((*i) < now) {
@@ -181,8 +208,11 @@ FGTrafficRecord::FGTrafficRecord():
         allowPushback(true),
         priority(0),
         timer(0),
-        latitude(0), longitude(0), heading(0), speed(0), altitude(0), radius(0),
-        aircraft(NULL)
+        latitude(0), longitude(0), heading(0), speed(0), altitude(0), radius(0)
+{
+}
+
+FGTrafficRecord::~FGTrafficRecord()
 {
 }
 
@@ -209,6 +239,17 @@ void FGTrafficRecord::setPositionAndIntentions(int pos,
         }
     }
 }
+
+void FGTrafficRecord::setAircraft(FGAIAircraft *ref)
+{
+    aircraft = ref;
+}
+
+FGAIAircraft* FGTrafficRecord::getAircraft() const
+{
+    return aircraft.ptr();
+}
+
 /**
  * Check if another aircraft is ahead of the current one, and on the same
  * return true / false is the is/isn't the case.
@@ -418,7 +459,7 @@ bool FGTrafficRecord::isOpposing(FGGroundNetwork * net,
 
 bool FGTrafficRecord::isActive(int margin) const
 {
-    time_t now = time(NULL) + fgGetLong("/sim/time/warp");
+    time_t now = globals->get_time_params()->get_cur_time();
     time_t deptime = aircraft->getTrafficRef()->getDepartureTime();
     return ((now + margin) > deptime);
 }
@@ -490,7 +531,8 @@ FGATCController::FGATCController()
 
 FGATCController::~FGATCController()
 {
-    //cerr << "running FGATController destructor" << endl;
+    FGATCManager *mgr = (FGATCManager*) globals->get_subsystem("ATC");
+    mgr->removeController(this);
 }
 
 string FGATCController::getGateName(FGAIAircraft * ref)
@@ -807,6 +849,15 @@ FGTowerController::FGTowerController(FGAirportDynamics *par) :
     parent = par;
 }
 
+FGTowerController::~FGTowerController()
+{
+    // to avoid the exception described in:
+    // https://sourceforge.net/p/flightgear/codetickets/1864/
+    // we want to ensure AI aircraft signing-off is a no-op now
+
+    clearTrafficControllers(activeTraffic);
+}
+
 //
 void FGTowerController::announcePosition(int id,
         FGAIFlightPlan * intendedRoute,
@@ -1072,6 +1123,11 @@ FGStartupController::FGStartupController(FGAirportDynamics *par):
     parent = par;
 }
 
+FGStartupController::~FGStartupController()
+{
+    clearTrafficControllers(activeTraffic);
+}
+
 void FGStartupController::announcePosition(int id,
         FGAIFlightPlan * intendedRoute,
         int currentPosition, double lat,
@@ -1255,7 +1311,8 @@ void FGStartupController::updateAircraftInformation(int id, double lat, double l
     // The user controlled aircraft should have crased here, because it doesn't have a traffic reference.
     // NOTE: if we create a traffic schedule for the user aircraft, we can use this to plan a flight.
     time_t startTime = i->getAircraft()->getTrafficRef()->getDepartureTime();
-    time_t now = time(NULL) + fgGetLong("/sim/time/warp");
+    time_t now = globals->get_time_params()->get_cur_time();
+
     //cerr << i->getAircraft()->getTrafficRef()->getCallSign()
     //     << " is scheduled to depart in " << startTime-now << " seconds. Available = " << available
     //     << " at parking " << getGateName(i->getAircraft()) << endl;
@@ -1338,7 +1395,8 @@ void FGStartupController::render(bool visible)
 
         //for ( FGTaxiSegmentVectorIterator i = segments.begin(); i != segments.end(); i++) {
         double dx = 0;
-        time_t now = time(NULL) + fgGetLong("/sim/time/warp");
+        time_t now = globals->get_time_params()->get_cur_time();
+
         for   (TrafficVectorIterator i = activeTraffic.begin(); i != activeTraffic.end(); i++) {
             if (i->isActive(300)) {
                 // Handle start point
@@ -1529,6 +1587,11 @@ FGApproachController::FGApproachController(FGAirportDynamics *par):
     parent = par;
 }
 
+FGApproachController::~FGApproachController()
+{
+    clearTrafficControllers(activeTraffic);
+}
+
 //
 void FGApproachController::announcePosition(int id,
         FGAIFlightPlan * intendedRoute,