diff --git a/apps/openmw/mwmechanics/aicombat.cpp b/apps/openmw/mwmechanics/aicombat.cpp index b42c3bdcc..6270779a4 100644 --- a/apps/openmw/mwmechanics/aicombat.cpp +++ b/apps/openmw/mwmechanics/aicombat.cpp @@ -458,28 +458,10 @@ namespace MWMechanics followTarget = false; - buildNewPath(actor, target); //may fail to build a path, check before use + buildNewPath(actor, target); - // if current actor pos is closer to target then last point of path (excluding target itself) then go straight on target - // This works on the borders between the path grid and areas with no waypoints. - if(inLOS && mPathFinder.getPath().size() > 1) - { - // get point just before target - std::list::const_iterator pntIter = --mPathFinder.getPath().end(); - --pntIter; - osg::Vec3f vBeforeTarget(PathFinder::MakeOsgVec3(*pntIter)); - - if(distToTarget <= (vTargetPos - vBeforeTarget).length()) - { - mPathFinder.clearPath(); - } - } - - // if there is no new path, then go straight on target - if (!mPathFinder.isPathConstructed()) - { - movement.mRotation[2] = getZAngleToDir((vTargetPos-vActorPos)); - } + // should always return a path (even if it's just go straight on target.) + assert(mPathFinder.isPathConstructed()); } if (readyToAttack) diff --git a/apps/openmw/mwmechanics/pathfinding.cpp b/apps/openmw/mwmechanics/pathfinding.cpp index f26d3e109..dbe20fdc0 100644 --- a/apps/openmw/mwmechanics/pathfinding.cpp +++ b/apps/openmw/mwmechanics/pathfinding.cpp @@ -215,6 +215,17 @@ namespace MWMechanics endPointInLocalCoords, startNode); + // if it's shorter for actor to travel from start to end, than to travel from either + // start or end to nearest pathgrid point, just travel from start to end. + float startToEndLength2 = (endPointInLocalCoords - startPointInLocalCoords).length2(); + float endTolastNodeLength2 = distanceSquared(mPathgrid->mPoints[endNode.first], endPointInLocalCoords); + float startTo1stNodeLength2 = distanceSquared(mPathgrid->mPoints[startNode], startPointInLocalCoords); + if ((startToEndLength2 < startTo1stNodeLength2) || (startToEndLength2 < endTolastNodeLength2)) + { + mPath.push_back(endPoint); + return; + } + // AiWander has logic that depends on whether a path was created, // deleting allowed nodes if not. Hence a path needs to be created // even if the start and the end points are the same.