diff --git a/src/AIModel/AIAircraft.cxx b/src/AIModel/AIAircraft.cxx index 0fc0bf5ae..83717cff2 100644 --- a/src/AIModel/AIAircraft.cxx +++ b/src/AIModel/AIAircraft.cxx @@ -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 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; diff --git a/src/AIModel/AIAircraft.hxx b/src/AIModel/AIAircraft.hxx index 9b804f90c..a9bb87a0a 100644 --- a/src/AIModel/AIAircraft.hxx +++ b/src/AIModel/AIAircraft.hxx @@ -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); diff --git a/src/AIModel/AIBase.cxx b/src/AIModel/AIBase.cxx index 2356e371d..c2df0b397 100644 --- a/src/AIModel/AIBase.cxx +++ b/src/AIModel/AIBase.cxx @@ -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 f) +{ + fp = std::move(f); +} + +bool FGAIBase::isValid() const +{ //Either no flightplan or it is valid return !fp || fp->isValidPlan(); } diff --git a/src/AIModel/AIBase.hxx b/src/AIModel/AIBase.hxx index fd88c6fd0..02ac0c7c3 100644 --- a/src/AIModel/AIBase.hxx +++ b/src/AIModel/AIBase.hxx @@ -108,7 +108,9 @@ public: int _getSubID() const; bool getDie(); - bool isValid(); + bool isValid() const; + + void setFlightPlan(std::unique_ptr f); SGGeod getGeodPos() const; @@ -211,7 +213,7 @@ protected: double life; - FGAIFlightPlan *fp; + std::unique_ptr fp; bool _impact_reported; bool _collision_reported; diff --git a/src/AIModel/AIShip.cxx b/src/AIModel/AIShip.cxx index 9ec3718bd..26883c0cf 100644 --- a/src/AIModel/AIShip.cxx +++ b/src/AIModel/AIShip.cxx @@ -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 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; } diff --git a/src/AIModel/AIShip.hxx b/src/AIModel/AIShip.hxx index 009b735f5..1c91b6437 100644 --- a/src/AIModel/AIShip.hxx +++ b/src/AIModel/AIShip.hxx @@ -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); diff --git a/src/ATC/atc_mgr.cxx b/src/ATC/atc_mgr.cxx index 355eebe26..78e65e7df 100644 --- a/src/ATC/atc_mgr.cxx +++ b/src/ATC/atc_mgr.cxx @@ -101,7 +101,7 @@ void FGATCManager::init() { flight->setCallSign(callsign); trafficRef->assign(flight); - FGAIFlightPlan *fp = 0; + std::unique_ptr 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,19 +177,14 @@ 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; } diff --git a/src/Traffic/Schedule.cxx b/src/Traffic/Schedule.cxx index 7e53cda06..452c4b1c3 100644 --- a/src/Traffic/Schedule.cxx +++ b/src/Traffic/Schedule.cxx @@ -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 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()->attach(aiAircraft); return true; } else { aiAircraft = NULL; - delete fp; //hand back the flights that had already been scheduled while (!flights.empty()) { flights.front()->release();