1
0
Fork 0

Use a unique_ptr to manage FGAIFlightPlan

This commit is contained in:
James Turner 2017-09-21 18:37:14 +01:00
parent c4aa3434ab
commit 66eb3d2690
8 changed files with 32 additions and 53 deletions

View file

@ -247,24 +247,15 @@ void FGAIAircraft::setFlightPlan(const std::string& flightplan, bool repeat)
return;
}
FGAIFlightPlan* fp = new FGAIFlightPlan(flightplan);
if (fp->isValidPlan()) {
fp->setRepeat(repeat);
SetFlightPlan(fp);
std::unique_ptr<FGAIFlightPlan> plan(new FGAIFlightPlan(flightplan));
if (plan->isValidPlan()) {
plan->setRepeat(repeat);
FGAIBase::setFlightPlan(std::move(plan));
} else {
SG_LOG(SG_AI, SG_WARN, "setFlightPlan: invalid flightplan specified:" << flightplan);
delete fp;
}
}
void FGAIAircraft::SetFlightPlan(FGAIFlightPlan *f)
{
delete fp;
fp = f;
}
void FGAIAircraft::ProcessFlightPlan( double dt, time_t now ) {
// the one behind you
@ -634,7 +625,7 @@ void FGAIAircraft::announcePositionToController() {
}
prevController = controller;
if (controller) {
controller->announcePosition(getID(), fp, fp->getCurrentWaypoint()->getRouteIndex(),
controller->announcePosition(getID(), fp.get(), fp->getCurrentWaypoint()->getRouteIndex(),
_getLatitude(), _getLongitude(), hdg, speed, altitude_ft,
trafficRef->getRadius(), leg, this);
}
@ -650,7 +641,7 @@ void FGAIAircraft::scheduleForATCTowerDepartureControl(int state) {
cerr << "Error: Could not find Dynamics at airport : " << trafficRef->getDepartureAirport()->getId() << endl;
}
if (towerController) {
towerController->announcePosition(getID(), fp, fp->getCurrentWaypoint()->getRouteIndex(),
towerController->announcePosition(getID(), fp.get(), fp->getCurrentWaypoint()->getRouteIndex(),
_getLatitude(), _getLongitude(), hdg, speed, altitude_ft,
trafficRef->getRadius(), leg, this);
//cerr << "Scheduling " << trafficRef->getCallSign() << " for takeoff " << endl;

View file

@ -49,9 +49,9 @@ public:
// void setPerformance(PerformanceData *ps);
void setFlightPlan(const std::string& fp, bool repat = false);
void SetFlightPlan(FGAIFlightPlan *f);
void initializeFlightPlan();
FGAIFlightPlan* GetFlightPlan() const { return fp; };
FGAIFlightPlan* GetFlightPlan() const { return fp.get(); };
void ProcessFlightPlan( double dt, time_t now );
time_t checkForArrivalTime(const std::string& wptName);

View file

@ -127,7 +127,6 @@ FGAIBase::FGAIBase(object_type ot, bool enableHot) :
model_removed( fgGetNode("/ai/models/model-removed", true) ),
manager( NULL ),
_installed(false),
fp( NULL ),
_impact_lat(0),
_impact_lon(0),
_impact_elev(0),
@ -205,10 +204,6 @@ FGAIBase::~FGAIBase() {
}
removeSoundFx();
if (fp)
delete fp;
fp = 0;
}
/** Cleanly remove the model
@ -935,7 +930,13 @@ int FGAIBase::_newAIModelID() {
return id;
}
bool FGAIBase::isValid() {
void FGAIBase::setFlightPlan(std::unique_ptr<FGAIFlightPlan> f)
{
fp = std::move(f);
}
bool FGAIBase::isValid() const
{
//Either no flightplan or it is valid
return !fp || fp->isValidPlan();
}

View file

@ -108,7 +108,9 @@ public:
int _getSubID() const;
bool getDie();
bool isValid();
bool isValid() const;
void setFlightPlan(std::unique_ptr<FGAIFlightPlan> f);
SGGeod getGeodPos() const;
@ -211,7 +213,7 @@ protected:
double life;
FGAIFlightPlan *fp;
std::unique_ptr<FGAIFlightPlan> fp;
bool _impact_reported;
bool _collision_reported;

View file

@ -106,12 +106,9 @@ void FGAIShip::readFromScenario(SGPropertyNode* scFileNode) {
setRollFactor(scFileNode->getDoubleValue("roll-factor", 1));
if (!flightplan.empty()) {
SG_LOG(SG_AI, SG_ALERT, "getting flightplan: " << _name );
FGAIFlightPlan* fp = new FGAIFlightPlan(flightplan);
setFlightPlan(fp);
std::unique_ptr<FGAIFlightPlan> plan(new FGAIFlightPlan(flightplan));
setFlightPlan(std::move(plan));
}
}
bool FGAIShip::init(bool search_in_AI_path) {
@ -457,10 +454,6 @@ double FGAIShip::sign(double x) {
return 1.0;
}
void FGAIShip::setFlightPlan(FGAIFlightPlan* f) {
fp = f;
}
void FGAIShip::setStartTime(const string& st) {
_start_time = st;
}

View file

@ -42,7 +42,6 @@ public:
virtual void update(double dt);
virtual void reinit();
void setFlightPlan(FGAIFlightPlan* f);
void setRudder(float r);
void setRoll(double rl);
void ProcessFlightPlan( double dt);

View file

@ -101,7 +101,7 @@ void FGATCManager::init() {
flight->setCallSign(callsign);
trafficRef->assign(flight);
FGAIFlightPlan *fp = 0;
std::unique_ptr<FGAIFlightPlan> fp ;
ai_ac->setTrafficRef(trafficRef);
string flightPlanName = airport + "-" + airport + ".xml";
@ -118,7 +118,7 @@ void FGATCManager::init() {
// No valid parking location, so either at the runway or at a random location.
if (pk.isValid()) {
dcs->setParkingAvailable(pk.parking(), false);
fp = new FGAIFlightPlan;
fp.reset(new FGAIFlightPlan);
controller = dcs->getStartupController();
int stationFreq = dcs->getGroundFrequency(1);
if (stationFreq > 0)
@ -155,7 +155,7 @@ void FGATCManager::init() {
//cerr << "Setting radio frequency to in airfrequency: " << stationFreq << endl;
fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0));
}
fp = new FGAIFlightPlan;
fp.reset(new FGAIFlightPlan);
leg = 3;
string fltType = "ga";
fp->setRunway(runway);
@ -177,18 +177,13 @@ void FGATCManager::init() {
if (fp) {
fp->restart();
fp->setLeg(leg);
ai_ac->SetFlightPlan(fp);
ai_ac->FGAIBase::setFlightPlan(std::move(fp));
}
if (controller) {
controller->announcePosition(ai_ac->getID(), fp, fp->getCurrentWaypoint()->getRouteIndex(),
FGAIFlightPlan* plan = ai_ac->GetFlightPlan();
controller->announcePosition(ai_ac->getID(), plan, plan->getCurrentWaypoint()->getRouteIndex(),
ai_ac->_getLatitude(), ai_ac->_getLongitude(), heading, speed, altitude,
aircraftRadius, leg, ai_ac);
//dialog.init();
//osg::Node* node = apt->getDynamics()->getGroundNetwork()->getRenderNode();
//cerr << "Adding groundnetWork to the scenegraph::init" << endl;
//globals->get_scenery()->get_scene_graph()->addChild(node);
}
initSucceeded = true;
}

View file

@ -373,21 +373,19 @@ bool FGAISchedule::createAIAircraft(FGScheduledFlight* flight, double speedKnots
aiAircraft->setBank(0);
courseToDest = SGGeodesy::courseDeg(position, arr->geod());
FGAIFlightPlan *fp = new FGAIFlightPlan(aiAircraft, flightPlanName, courseToDest, deptime,
std::unique_ptr<FGAIFlightPlan> fp(new FGAIFlightPlan(aiAircraft, flightPlanName, courseToDest, deptime,
dep, arr, true, radius,
flight->getCruiseAlt()*100,
position.getLatitudeDeg(),
position.getLongitudeDeg(),
speedKnots, flightType, acType,
airline);
airline));
if (fp->isValidPlan()) {
aiAircraft->SetFlightPlan(fp);
FGAIManager* aimgr = (FGAIManager *) globals-> get_subsystem("ai-model");
aimgr->attach(aiAircraft);
aiAircraft->FGAIBase::setFlightPlan(std::move(fp));
globals->get_subsystem<FGAIManager>()->attach(aiAircraft);
return true;
} else {
aiAircraft = NULL;
delete fp;
//hand back the flights that had already been scheduled
while (!flights.empty()) {
flights.front()->release();