]> git.mxchange.org Git - flightgear.git/commitdiff
Break upwards dependency on Traffic in the AI code
authorJames Turner <zakalawe@mac.com>
Wed, 21 Nov 2012 14:49:34 +0000 (14:49 +0000)
committerJames Turner <zakalawe@mac.com>
Wed, 21 Nov 2012 14:49:34 +0000 (14:49 +0000)
AIManager had unfortunate code to pass some state to the traffic-manager; this is no longer required since traffic schedules hold onto their AI-Aircraft directly as needed.

src/AIModel/AIManager.cxx
src/AIModel/AIManager.hxx
src/Traffic/Schedule.cxx
src/Traffic/Schedule.hxx
src/Traffic/TrafficMgr.cxx
src/Traffic/TrafficMgr.hxx

index 6718593344f8c7cad6d25d8f44203f00b35f53d6..f7193ba8149677a9cc1b76b14b8479665d6ee2e2 100644 (file)
@@ -28,9 +28,7 @@
 #include <boost/foreach.hpp>
 
 #include <Main/globals.hxx>
-
 #include <Airports/simple.hxx>
-#include <Traffic/TrafficMgr.hxx>
 
 #include "AIManager.hxx"
 #include "AIAircraft.hxx"
@@ -135,11 +133,6 @@ FGAIManager::unbind() {
 
 void FGAIManager::removeDeadItem(FGAIBase* base)
 {
-    FGTrafficManager *tmgr = (FGTrafficManager*) globals->get_subsystem("traffic-manager");
-    if (tmgr) {
-        tmgr->release(base->getID());
-    }
-    
     SGPropertyNode *props = base->_getProps();
     
     props->setBoolValue("valid", false);
@@ -227,21 +220,6 @@ FGAIManager::attach(FGAIBase *model)
     p->setBoolValue("valid", true);
 }
 
-void
-FGAIManager::destroyObject( int ID ) {
-    ai_list_iterator ai_list_itr = ai_list.begin();
-
-    while(ai_list_itr != ai_list.end()) {
-
-        if ((*ai_list_itr)->getID() == ID) {
-            (*ai_list_itr)->unbind();
-            ai_list_itr = ai_list.erase(ai_list_itr);
-        } else
-            ++ai_list_itr;
-    }
-
-}
-
 int
 FGAIManager::getNumAiObjects(void) const
 {
index d34223e17bed2a1b2e5b3c5fa51e0fe5de474afe..d9ce28780700cd425478b31ea4ceddae23f5c21c 100644 (file)
@@ -70,7 +70,6 @@ public:
     void updateLOD(SGPropertyNode* node);
     void attach(FGAIBase *model);
 
-    void destroyObject( int ID );
     const FGAIBase *calcCollision(double alt, double lat, double lon, double fuse_range);
 
     inline double get_user_latitude() const { return user_latitude; }
index 3d4db5cb7683517f85e257e5f7599b5148730e2a..49f9331198d58c4d1083000f08ed9dbc3cfe70a6 100644 (file)
@@ -61,7 +61,6 @@
 FGAISchedule::FGAISchedule()
 {
   firstRun     = true;
-  AIManagerRef = 0;
 
   heavy = false;
   radius = 0;
@@ -115,7 +114,6 @@ FGAISchedule::FGAISchedule(string model,
        i != flt.end();
        i++)
     flights.push_back(new FGScheduledFlight((*(*i))));*/
-  AIManagerRef     = 0;
   score    =         0;
   firstRun         = true;
   runCount         = 0;
@@ -134,7 +132,7 @@ FGAISchedule::FGAISchedule(const FGAISchedule &other)
   heavy              = other.heavy;
   flightIdentifier   = other.flightIdentifier;
   flights            = other.flights;
-  AIManagerRef       = other.AIManagerRef;
+  aiAircraft         = other.aiAircraft;
   acType             = other.acType;
   airline            = other.airline;
   m_class            = other.m_class;
@@ -158,12 +156,9 @@ FGAISchedule::FGAISchedule(const FGAISchedule &other)
 FGAISchedule::~FGAISchedule()
 {
     // remove related object from AI manager
-    if (AIManagerRef)
+    if (aiAircraft)
     {
-        FGAIManager* aimgr = (FGAIManager *) globals-> get_subsystem("ai-model");
-        if (aimgr)
-            aimgr->destroyObject(AIManagerRef);
-        AIManagerRef = 0;
+      aiAircraft->setDie(true);
     }
 
 /*  for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
@@ -234,11 +229,9 @@ bool FGAISchedule::update(time_t now, const SGVec3d& userCart)
     //cerr << "Settiing departure time " << deptime << endl;
   }
     
-  if (AIManagerRef) {
-    // Check if this aircraft has been released. 
-    FGTrafficManager *tmgr = (FGTrafficManager *) globals->get_subsystem("traffic-manager");
-    if (tmgr->isReleased(AIManagerRef)) {
-      AIManagerRef = 0;
+  if (aiAircraft) {
+    if (aiAircraft->getDie()) {
+      aiAircraft = NULL;
     } else {
       return true; // in visual range, let the AIManager handle it
     }
@@ -345,20 +338,20 @@ bool FGAISchedule::createAIAircraft(FGScheduledFlight* flight, double speedKnots
     return true;
   }
 
-  FGAIAircraft *aircraft = new FGAIAircraft(this);
-  aircraft->setPerformance(acType, m_class); //"jet_transport";
-  aircraft->setCompany(airline); //i->getAirline();
-  aircraft->setAcType(acType); //i->getAcType();
-  aircraft->setPath(modelPath.c_str());
+  aiAircraft = new FGAIAircraft(this);
+  aiAircraft->setPerformance(acType, m_class); //"jet_transport";
+  aiAircraft->setCompany(airline); //i->getAirline();
+  aiAircraft->setAcType(acType); //i->getAcType();
+  aiAircraft->setPath(modelPath.c_str());
   //aircraft->setFlightPlan(flightPlanName);
-  aircraft->setLatitude(position.getLatitudeDeg());
-  aircraft->setLongitude(position.getLongitudeDeg());
-  aircraft->setAltitude(flight->getCruiseAlt()*100); // convert from FL to feet
-  aircraft->setSpeed(0);
-  aircraft->setBank(0);
+  aiAircraft->setLatitude(position.getLatitudeDeg());
+  aiAircraft->setLongitude(position.getLongitudeDeg());
+  aiAircraft->setAltitude(flight->getCruiseAlt()*100); // convert from FL to feet
+  aiAircraft->setSpeed(0);
+  aiAircraft->setBank(0);
       
   courseToDest = SGGeodesy::courseDeg(position, arr->geod());
-  FGAIFlightPlan *fp = new FGAIFlightPlan(aircraft, flightPlanName, courseToDest, deptime,
+  FGAIFlightPlan *fp = new FGAIFlightPlan(aiAircraft, flightPlanName, courseToDest, deptime,
                                             dep, arr, true, radius, 
                                             flight->getCruiseAlt()*100, 
                                             position.getLatitudeDeg(), 
@@ -366,13 +359,12 @@ bool FGAISchedule::createAIAircraft(FGScheduledFlight* flight, double speedKnots
                                             speedKnots, flightType, acType, 
                                             airline);
   if (fp->isValidPlan()) {
-        aircraft->SetFlightPlan(fp);
+        aiAircraft->SetFlightPlan(fp);
         FGAIManager* aimgr = (FGAIManager *) globals-> get_subsystem("ai-model");
-        aimgr->attach(aircraft);
-        AIManagerRef = aircraft->getID();
+        aimgr->attach(aiAircraft);
         return true;
   } else {
-        delete aircraft;
+        aiAircraft = NULL;
         delete fp;
         //hand back the flights that had already been scheduled
         while (!flights.empty()) {
index a85d46df6be5aecbdfc7faff828f6a25b57e359e..bff785b685761781b932d99fe6a3d44da389d7db 100644 (file)
@@ -32,6 +32,8 @@
 #define TRAFFICTOAIDISTTOSTART 150.0
 #define TRAFFICTOAIDISTTODIE   200.0
 
+// forward decls
+class FGAIAircraft;
 
 class FGAISchedule
 {
@@ -52,7 +54,6 @@ class FGAISchedule
   double radius;
   double groundOffset;
   double distanceToUser;
-  int AIManagerRef;
   double score;
   unsigned int runCount;
   unsigned int hits;
@@ -71,6 +72,8 @@ class FGAISchedule
    */
   bool createAIAircraft(FGScheduledFlight* flight, double speedKnots, time_t deptime);
   
+  // the aiAircraft associated with us
+  SGSharedPtr<FGAIAircraft> aiAircraft;
  public:
   FGAISchedule();                                           // constructor
   FGAISchedule(string model, 
index 0d895a8c9dae751cf3fa5797c492d7d1820a012a..0abf476657c751fe3c51b2090767b67590b57a0c 100644 (file)
@@ -216,7 +216,6 @@ void FGTrafficManager::shutdown()
     }
     scheduledAircraft.clear();
     flights.clear();
-    releaseList.clear();
 
     currAircraft = scheduledAircraft.begin();
     doingInit = false;
@@ -416,25 +415,6 @@ void FGTrafficManager::update(double dt)
     currAircraft++;
 }
 
-void FGTrafficManager::release(int id)
-{
-    releaseList.push_back(id);
-}
-
-bool FGTrafficManager::isReleased(int id)
-{
-    IdListIterator i = releaseList.begin();
-    while (i != releaseList.end()) {
-        if ((*i) == id) {
-            releaseList.erase(i);
-            return true;
-        }
-        i++;
-    }
-    return false;
-}
-
-
 void FGTrafficManager::readTimeTableFromFile(SGPath infileName)
 {
     string model;
index 5be7fd3cdf0b21fbcbdd4864c983920e568ad758..8cf3d8ef8c77a3a1219539ad6e80ed24c875e2e3 100644 (file)
 #include "SchedFlight.hxx"
 #include "Schedule.hxx"
 
-
-typedef std::vector<int> IdList;
-typedef std::vector<int>::iterator IdListIterator;
-
 class Heuristic
 {
 public:
@@ -104,8 +100,6 @@ private:
   int score, runCount, acCounter;
   double radius, offset;
   bool heavy;
-
-  IdList releaseList;
     
   FGScheduledFlightMap flights;
 
@@ -134,8 +128,6 @@ public:
   ~FGTrafficManager();
   void init();
   void update(double time);
-  void release(int ref);
-  bool isReleased(int id);
 
   FGScheduledFlightVecIterator getFirstFlight(const string &ref) { return flights[ref].begin(); }
   FGScheduledFlightVecIterator getLastFlight(const string &ref) { return flights[ref].end(); }