1
0
Fork 0

Fix segfaults in AI code

This commit is contained in:
portree_kid 2022-06-19 10:28:54 +02:00
parent 0942cb0b7f
commit 5a3528f782
6 changed files with 66 additions and 32 deletions

View file

@ -348,7 +348,11 @@ void FGAIAircraft::ProcessFlightPlan( double dt, time_t now ) {
} }
} }
if (!curr) { if (!curr) {
SG_LOG(SG_AI, SG_WARN, "No current WP" << next->getName()); if (!next) {
SG_LOG(SG_AI, SG_WARN, getCallSign() << "|No more WPs");
} else {
SG_LOG(SG_AI, SG_WARN, getCallSign() << "|No current WP" << next->getName());
}
return; return;
} }
@ -1016,6 +1020,8 @@ bool FGAIAircraft::handleAirportEndPoints(FGAIWaypoint* prev, time_t now) {
if (!( dep && arr)) if (!( dep && arr))
return false; return false;
if (!prev)
return false;
// This waypoint marks the fact that the aircraft has passed the initial taxi // This waypoint marks the fact that the aircraft has passed the initial taxi
// departure waypoint, so it can release the parking. // departure waypoint, so it can release the parking.
@ -1471,17 +1477,25 @@ bool FGAIAircraft::reachedEndOfCruise(double &distance) {
void FGAIAircraft::resetPositionFromFlightPlan() void FGAIAircraft::resetPositionFromFlightPlan()
{ {
if (fp->empty()) {
return;
}
// the one behind you // the one behind you
FGAIWaypoint* prev = 0; FGAIWaypoint* prev = nullptr;
// the one ahead // the one ahead
FGAIWaypoint* curr = 0; FGAIWaypoint* curr = nullptr;
// the next plus 1 // the next plus 1
FGAIWaypoint* next = 0; FGAIWaypoint* next = nullptr;
prev = fp->getPreviousWaypoint(); prev = fp->getPreviousWaypoint();
curr = fp->getCurrentWaypoint(); curr = fp->getCurrentWaypoint();
next = fp->getNextWaypoint(); next = fp->getNextWaypoint();
if (curr==nullptr || next==nullptr) {
SG_LOG(SG_AI, SG_WARN, getCallSign() << "|Repositioned without curr/next");
return;
}
setLatitude(prev->getLatitude()); setLatitude(prev->getLatitude());
setLongitude(prev->getLongitude()); setLongitude(prev->getLongitude());
setHeading(fp->getBearing(curr, next)); setHeading(fp->getBearing(curr, next));

View file

@ -548,6 +548,9 @@ void FGAIFlightPlan::addWaypoint(FGAIWaypoint* wpt)
void FGAIFlightPlan::pushBackWaypoint(FGAIWaypoint *wpt) void FGAIFlightPlan::pushBackWaypoint(FGAIWaypoint *wpt)
{ {
if (!wpt) {
SG_LOG(SG_AI, SG_WARN, "Null WPT added");
}
size_t pos = wpt_iterator - waypoints.begin(); size_t pos = wpt_iterator - waypoints.begin();
if (waypoints.size()>0) { if (waypoints.size()>0) {
double dist = SGGeodesy::distanceM( waypoints.back()->getPos(), wpt->getPos()); double dist = SGGeodesy::distanceM( waypoints.back()->getPos(), wpt->getPos());
@ -587,9 +590,12 @@ double FGAIFlightPlan::checkTrackLength(const string& wptName) const {
wpt_vector_iterator wptvec = waypoints.begin(); wpt_vector_iterator wptvec = waypoints.begin();
++wptvec; ++wptvec;
++wptvec; ++wptvec;
while ((wptvec != waypoints.end()) && (!((*wptvec)->contains(wptName)))) { while ((wptvec != waypoints.end())) {
trackDistance += (*wptvec)->getTrackLength(); if (*wptvec!=nullptr && (!((*wptvec)->contains(wptName)))) {
++wptvec; break;
}
trackDistance += (*wptvec)->getTrackLength();
++wptvec;
} }
if (wptvec == waypoints.end()) { if (wptvec == waypoints.end()) {
trackDistance = 0; // name not found trackDistance = 0; // name not found

View file

@ -32,21 +32,21 @@
class SGPath; class SGPath;
class FGAIWaypoint { class FGAIWaypoint {
private: private:
std::string name; std::string name = "unnamed";
SGGeod pos; SGGeod pos;
double speed; double speed = 0.0;
double crossat; double crossat = 0.0;
bool finished; bool finished = false;
bool gear_down; bool gear_down = true;
double flaps; double flaps = 0.0;
double spoilers = 0.0; double spoilers = 0.0;
double speedbrakes = 0.0; double speedbrakes = 0.0;
bool on_ground; bool on_ground;
int routeIndex; // For AI/ATC purposes; int routeIndex; // For AI/ATC purposes;
double time_sec; double time_sec;
double trackLength; // distance from previous FGAIWaypoint (for AI purposes); double trackLength = 0.0; // distance from previous FGAIWaypoint (for AI purposes);
std::string time; std::string time;
bool beacon_light = false; bool beacon_light = false;
bool landing_light = false; bool landing_light = false;
@ -55,7 +55,7 @@ private:
bool taxi_light = false; bool taxi_light = false;
bool cabin_light = false; bool cabin_light = false;
public: public:
FGAIWaypoint(); FGAIWaypoint();
virtual ~FGAIWaypoint() {}; virtual ~FGAIWaypoint() {};
@ -99,7 +99,7 @@ public:
bool contains(const std::string& name); bool contains(const std::string& name);
const std::string& getName() { return name; }; const std::string& getName() { return name; };
const SGGeod& getPos () { return pos; }; const SGGeod& getPos () { return pos; };
double getLatitude (); double getLatitude ();
double getLongitude (); double getLongitude ();

View file

@ -117,10 +117,20 @@ bool FGAIFlightPlan::create(FGAIAircraft * ac, FGAirport * dep,
" this is probably an internal program error"); " this is probably an internal program error");
break; break;
} }
wpt_iterator = waypoints.begin() + currWpt;
//don't increment leg right away, but only once we pass the actual last waypoint that was created. //don't increment leg right away, but only once we pass the actual last waypoint that was created.
// to do so, mark the last waypoint with a special status flag // to do so, mark the last waypoint with a special status flag
if (retVal) { if (retVal) {
if (waypoints.empty()) {
SG_LOG(SG_AI, SG_WARN, ac->getCallSign() << "|AIFlightPlan::create() Leg " << legNr <<
" created empty waypoints." );
return retVal;
}
wpt_iterator = waypoints.begin() + currWpt;
if (waypoints.back()->getName().size()==0) {
SG_LOG(SG_AI, SG_WARN, ac->getCallSign() <<
" Empty wpt name");
}
waypoints.back()->setName( waypoints.back()->getName() + string("legend")); waypoints.back()->setName( waypoints.back()->getName() + string("legend"));
// "It's pronounced Leg-end" (Roger Glover (Deep Purple): come Hell or High Water DvD, 1993) // "It's pronounced Leg-end" (Roger Glover (Deep Purple): come Hell or High Water DvD, 1993)
} }
@ -1029,8 +1039,10 @@ bool FGAIFlightPlan::createDescent(FGAIAircraft * ac, FGAirport * apt,
ac->resetPositionFromFlightPlan(); ac->resetPositionFromFlightPlan();
} }
SG_LOG(SG_AI, SG_BULK, "Setting Node " << waypoints[1]->getName() << " to a leg end"); if (!waypoints.empty()) {
waypoints[1]->setName( (waypoints[1]->getName() + string("legend"))); SG_LOG(SG_AI, SG_BULK, "Setting Node " << waypoints.back()->getName() << " to a leg end");
waypoints.back()->setName( (waypoints.back()->getName() + string("legend")));
}
return true; return true;
} }

View file

@ -287,7 +287,8 @@ bool FGAIFlightPlan::createCruise(FGAIAircraft *ac, bool firstFlight, FGAirport
FGAirport *arr, FGAirport *arr,
const SGGeod& current, const SGGeod& current,
double speed, double speed,
double alt, const string& fltType) double alt,
const string& fltType)
{ {
double vCruise = ac->getPerformance()->vCruise(); double vCruise = ac->getPerformance()->vCruise();
FGAIWaypoint *wpt; FGAIWaypoint *wpt;
@ -305,7 +306,8 @@ bool FGAIFlightPlan::createCruise(FGAIAircraft *ac, bool firstFlight, FGAirport
double heading = ac->getTrafficRef()->getCourse(); double heading = ac->getTrafficRef()->getCourse();
arr->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway, heading); arr->getDynamics()->getActiveRunway(rwyClass, 2, activeRunway, heading);
if (!arr->hasRunwayWithIdent(activeRunway)) { if (!arr->hasRunwayWithIdent(activeRunway)) {
return false; SG_LOG(SG_AI, SG_WARN, ac->getCallSign() << " cruise to" << arr->getId() << activeRunway << " not active");
return false;
} }
FGRunway* rwy = arr->getRunwayByIdent(activeRunway); FGRunway* rwy = arr->getRunwayByIdent(activeRunway);

View file

@ -306,7 +306,7 @@ FGTaxiNodeRef FGGroundNetwork::findNearestNodeOnRunwayExit(const SGGeod & aGeod,
if (aRunway) { if (aRunway) {
double headingTowardsExit = SGGeodesy::courseDeg(aGeod, (*it)->geod()); double headingTowardsExit = SGGeodesy::courseDeg(aGeod, (*it)->geod());
double diff = fabs(aRunway->headingDeg() - headingTowardsExit); double diff = fabs(aRunway->headingDeg() - headingTowardsExit);
SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExit " << aRunway->headingDeg() << " " SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExit " << aRunway->headingDeg() << " "
<< " Diff : " << diff << " " << (*it)->getIndex()); << " Diff : " << diff << " " << (*it)->getIndex());
if (diff > 10) { if (diff > 10) {
// Only ahead // Only ahead
@ -345,13 +345,13 @@ FGTaxiNodeRef FGGroundNetwork::findNearestNodeOnRunwayExit(const SGGeod & aGeod,
if (aRunway) { if (aRunway) {
double headingTowardsExit = SGGeodesy::courseDeg(aGeod, (*it)->geod()); double headingTowardsExit = SGGeodesy::courseDeg(aGeod, (*it)->geod());
double diff = fabs(aRunway->headingDeg() - headingTowardsExit); double diff = fabs(aRunway->headingDeg() - headingTowardsExit);
SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExitFallback1 " << aRunway->headingDeg() << " " SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExitFallback1 " << aRunway->headingDeg() << " "
<< " Diff : " << diff << " " << (*it)->getIndex()); << " Diff : " << diff << " " << (*it)->getIndex());
if (diff > 10) { if (diff > 10) {
// Only ahead // Only ahead
continue; continue;
} }
} }
if (localDistanceSqr < d) { if (localDistanceSqr < d) {
SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExitFallback1 " << localDistanceSqr); SG_LOG(SG_AI, SG_BULK, "findNearestNodeOnRunwayExitFallback1 " << localDistanceSqr);
d = localDistanceSqr; d = localDistanceSqr;
@ -483,10 +483,10 @@ FGTaxiRoute FGGroundNetwork::findShortestRoute(FGTaxiNode* start, FGTaxiNode* en
if (searchData[end].score == HUGE_VAL) { if (searchData[end].score == HUGE_VAL) {
// no valid route found // no valid route found
if (fullSearch) { if (fullSearch && start && end) {
SG_LOG(SG_GENERAL, SG_ALERT, SG_LOG(SG_GENERAL, SG_ALERT,
"Failed to find route from waypoint " << start << " to " "Failed to find route from waypoint " << start->getIndex() << " to "
<< end << " at " << parent->getId()); << end->getIndex() << " at " << parent->getId());
} }
return FGTaxiRoute(); return FGTaxiRoute();