1
0
Fork 0

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.
This commit is contained in:
James Turner 2012-11-21 14:49:34 +00:00
parent 77ca03da4d
commit bca6e9e890
6 changed files with 24 additions and 80 deletions

View 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
{

View 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; }

View 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()) {

View 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,

View 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;

View file

@ -58,10 +58,6 @@
#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(); }