Remove int conversions from pathfinding

This commit is contained in:
Evil Eye 2025-07-15 21:57:12 +02:00
parent 3b7b97c58c
commit 931555c7ff
4 changed files with 15 additions and 15 deletions

View File

@ -393,13 +393,13 @@ namespace MWMechanics
osg::Vec3f localPos = actor.getRefData().getPosition().asVec3();
coords.toLocal(localPos);
int closestPointIndex = PathFinder::getClosestPoint(pathgrid, localPos);
for (int i = 0; i < static_cast<int>(pathgrid->mPoints.size()); i++)
size_t closestPointIndex = PathFinder::getClosestPoint(pathgrid, localPos);
for (size_t i = 0; i < pathgrid->mPoints.size(); i++)
{
if (i != closestPointIndex
&& getPathGridGraph(pathgrid).isPointConnected(closestPointIndex, i))
{
points.push_back(pathgrid->mPoints[static_cast<size_t>(i)]);
points.push_back(pathgrid->mPoints[i]);
}
}

View File

@ -827,7 +827,7 @@ namespace MWMechanics
if (pathgrid == nullptr || pathgrid->mPoints.empty())
return;
int index = PathFinder::getClosestPoint(pathgrid, PathFinder::makeOsgVec3(dest));
size_t index = PathFinder::getClosestPoint(pathgrid, PathFinder::makeOsgVec3(dest));
getPathGridGraph(pathgrid).getNeighbouringPoints(index, points);
}
@ -860,7 +860,7 @@ namespace MWMechanics
const osg::Vec3f npcPos = converter.toLocalVec3(mInitialActorPosition);
// Find closest pathgrid point
int closestPointIndex = PathFinder::getClosestPoint(pathgrid, npcPos);
size_t closestPointIndex = PathFinder::getClosestPoint(pathgrid, npcPos);
// mAllowedNodes for this actor with pathgrid point indexes based on mDistance
// and if the point is connected to the closest current point

View File

@ -25,15 +25,15 @@
namespace
{
// Chooses a reachable end pathgrid point. start is assumed reachable.
std::pair<int, bool> getClosestReachablePoint(
const ESM::Pathgrid* grid, const MWMechanics::PathgridGraph* graph, const osg::Vec3f& pos, int start)
std::pair<size_t, bool> getClosestReachablePoint(
const ESM::Pathgrid* grid, const MWMechanics::PathgridGraph* graph, const osg::Vec3f& pos, size_t start)
{
assert(grid && !grid->mPoints.empty());
float closestDistanceBetween = std::numeric_limits<float>::max();
float closestDistanceReachable = std::numeric_limits<float>::max();
int closestIndex = 0;
int closestReachableIndex = 0;
size_t closestIndex = 0;
size_t closestReachableIndex = 0;
// TODO: if this full scan causes performance problems mapping pathgrid
// points to a quadtree may help
for (size_t counter = 0; counter < grid->mPoints.size(); counter++)
@ -62,7 +62,7 @@ namespace
// allowed nodes if not. Hence a path needs to be created even if the start
// and the end points are the same.
return std::pair<int, bool>(closestReachableIndex, closestReachableIndex == closestIndex);
return { closestReachableIndex, closestReachableIndex == closestIndex };
}
float sqrDistance(const osg::Vec2f& lhs, const osg::Vec2f& rhs)
@ -197,10 +197,10 @@ namespace MWMechanics
// point right behind the wall that is closer than any pathgrid
// point outside the wall
osg::Vec3f startPointInLocalCoords(converter.toLocalVec3(startPoint));
int startNode = getClosestPoint(pathgrid, startPointInLocalCoords);
size_t startNode = getClosestPoint(pathgrid, startPointInLocalCoords);
osg::Vec3f endPointInLocalCoords(converter.toLocalVec3(endPoint));
std::pair<int, bool> endNode
std::pair<size_t, bool> endNode
= getClosestReachablePoint(pathgrid, &pathgridGraph, endPointInLocalCoords, startNode);
// if it's shorter for actor to travel from start to end, than to travel from either

View File

@ -178,16 +178,16 @@ namespace MWMechanics
//
// NOTE: pos is expected to be in local coordinates, as is grid->mPoints
//
static int getClosestPoint(const ESM::Pathgrid* grid, const osg::Vec3f& pos)
static size_t getClosestPoint(const ESM::Pathgrid* grid, const osg::Vec3f& pos)
{
assert(grid && !grid->mPoints.empty());
float distanceBetween = distanceSquared(grid->mPoints[0], pos);
int closestIndex = 0;
size_t closestIndex = 0;
// TODO: if this full scan causes performance problems mapping pathgrid
// points to a quadtree may help
for (unsigned int counter = 1; counter < grid->mPoints.size(); counter++)
for (size_t counter = 1; counter < grid->mPoints.size(); counter++)
{
float potentialDistBetween = distanceSquared(grid->mPoints[counter], pos);
if (potentialDistBetween < distanceBetween)