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

View file

@ -548,6 +548,9 @@ void FGAIFlightPlan::addWaypoint(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();
if (waypoints.size()>0) {
double dist = SGGeodesy::distanceM( waypoints.back()->getPos(), wpt->getPos());
@ -587,7 +590,10 @@ double FGAIFlightPlan::checkTrackLength(const string& wptName) const {
wpt_vector_iterator wptvec = waypoints.begin();
++wptvec;
++wptvec;
while ((wptvec != waypoints.end()) && (!((*wptvec)->contains(wptName)))) {
while ((wptvec != waypoints.end())) {
if (*wptvec!=nullptr && (!((*wptvec)->contains(wptName)))) {
break;
}
trackDistance += (*wptvec)->getTrackLength();
++wptvec;
}

View file

@ -33,19 +33,19 @@ class SGPath;
class FGAIWaypoint {
private:
std::string name;
std::string name = "unnamed";
SGGeod pos;
double speed;
double crossat;
bool finished;
bool gear_down;
double flaps;
double speed = 0.0;
double crossat = 0.0;
bool finished = false;
bool gear_down = true;
double flaps = 0.0;
double spoilers = 0.0;
double speedbrakes = 0.0;
bool on_ground;
int routeIndex; // For AI/ATC purposes;
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;
bool beacon_light = false;

View file

@ -117,10 +117,20 @@ bool FGAIFlightPlan::create(FGAIAircraft * ac, FGAirport * dep,
" this is probably an internal program error");
break;
}
wpt_iterator = waypoints.begin() + currWpt;
//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
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"));
// "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();
}
SG_LOG(SG_AI, SG_BULK, "Setting Node " << waypoints[1]->getName() << " to a leg end");
waypoints[1]->setName( (waypoints[1]->getName() + string("legend")));
if (!waypoints.empty()) {
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;
}

View file

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

View file

@ -483,10 +483,10 @@ FGTaxiRoute FGGroundNetwork::findShortestRoute(FGTaxiNode* start, FGTaxiNode* en
if (searchData[end].score == HUGE_VAL) {
// no valid route found
if (fullSearch) {
if (fullSearch && start && end) {
SG_LOG(SG_GENERAL, SG_ALERT,
"Failed to find route from waypoint " << start << " to "
<< end << " at " << parent->getId());
"Failed to find route from waypoint " << start->getIndex() << " to "
<< end->getIndex() << " at " << parent->getId());
}
return FGTaxiRoute();