From bca6e9e8901f47cb868b2ad4d6795164e7961fa0 Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 21 Nov 2012 14:49:34 +0000 Subject: [PATCH] Break upwards dependency on Traffic in the AI code 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 | 22 ----------------- src/AIModel/AIManager.hxx | 1 - src/Traffic/Schedule.cxx | 48 ++++++++++++++++---------------------- src/Traffic/Schedule.hxx | 5 +++- src/Traffic/TrafficMgr.cxx | 20 ---------------- src/Traffic/TrafficMgr.hxx | 8 ------- 6 files changed, 24 insertions(+), 80 deletions(-) diff --git a/src/AIModel/AIManager.cxx b/src/AIModel/AIManager.cxx index 671859334..f7193ba81 100644 --- a/src/AIModel/AIManager.cxx +++ b/src/AIModel/AIManager.cxx @@ -28,9 +28,7 @@ #include #include
- #include -#include #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 { diff --git a/src/AIModel/AIManager.hxx b/src/AIModel/AIManager.hxx index d34223e17..d9ce28780 100644 --- a/src/AIModel/AIManager.hxx +++ b/src/AIModel/AIManager.hxx @@ -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; } diff --git a/src/Traffic/Schedule.cxx b/src/Traffic/Schedule.cxx index 3d4db5cb7..49f933119 100644 --- a/src/Traffic/Schedule.cxx +++ b/src/Traffic/Schedule.cxx @@ -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()) { diff --git a/src/Traffic/Schedule.hxx b/src/Traffic/Schedule.hxx index a85d46df6..bff785b68 100644 --- a/src/Traffic/Schedule.hxx +++ b/src/Traffic/Schedule.hxx @@ -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 aiAircraft; public: FGAISchedule(); // constructor FGAISchedule(string model, diff --git a/src/Traffic/TrafficMgr.cxx b/src/Traffic/TrafficMgr.cxx index 0d895a8c9..0abf47665 100644 --- a/src/Traffic/TrafficMgr.cxx +++ b/src/Traffic/TrafficMgr.cxx @@ -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; diff --git a/src/Traffic/TrafficMgr.hxx b/src/Traffic/TrafficMgr.hxx index 5be7fd3cd..8cf3d8ef8 100644 --- a/src/Traffic/TrafficMgr.hxx +++ b/src/Traffic/TrafficMgr.hxx @@ -58,10 +58,6 @@ #include "SchedFlight.hxx" #include "Schedule.hxx" - -typedef std::vector IdList; -typedef std::vector::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(); } -- 2.39.5