Extracted function SetCurrentNodeToClosestAllowedNode()

c++11
dteviot 10 years ago
parent 76f20b8b20
commit eb2aa965b9

@ -1,5 +1,7 @@
#include "aiwander.hpp" #include "aiwander.hpp"
#include <cfloat>
#include <components/misc/rng.hpp> #include <components/misc/rng.hpp>
#include <components/esm/aisequence.hpp> #include <components/esm/aisequence.hpp>
@ -700,22 +702,29 @@ namespace MWMechanics
} }
if(!mAllowedNodes.empty()) if(!mAllowedNodes.empty())
{ {
osg::Vec3f firstNodePos(PathFinder::MakeOsgVec3(mAllowedNodes[0])); SetCurrentNodeToClosestAllowedNode(npcPos);
float closestNode = (npcPos - firstNodePos).length2(); }
mStoredAvailableNodes = true; // set only if successful in finding allowed nodes
}
}
void AiWander::SetCurrentNodeToClosestAllowedNode(osg::Vec3f npcPos)
{
float distanceToClosestNode = FLT_MAX;
unsigned int index = 0; unsigned int index = 0;
for(unsigned int counterThree = 1; counterThree < mAllowedNodes.size(); counterThree++) for (unsigned int counterThree = 0; counterThree < mAllowedNodes.size(); counterThree++)
{ {
osg::Vec3f nodePos(PathFinder::MakeOsgVec3(mAllowedNodes[counterThree])); osg::Vec3f nodePos(PathFinder::MakeOsgVec3(mAllowedNodes[counterThree]));
float tempDist = (npcPos - nodePos).length2(); float tempDist = (npcPos - nodePos).length2();
if(tempDist < closestNode) if (tempDist < distanceToClosestNode)
{
index = counterThree; index = counterThree;
distanceToClosestNode = tempDist;
}
} }
mCurrentNode = mAllowedNodes[index]; mCurrentNode = mAllowedNodes[index];
mAllowedNodes.erase(mAllowedNodes.begin() + index); mAllowedNodes.erase(mAllowedNodes.begin() + index);
} }
mStoredAvailableNodes = true; // set only if successful in finding allowed nodes
}
}
void AiWander::writeState(ESM::AiSequence::AiSequence &sequence) const void AiWander::writeState(ESM::AiSequence::AiSequence &sequence) const
{ {

@ -121,6 +121,8 @@ namespace MWMechanics
/// convert point from local (i.e. cell) to world co-ordinates /// convert point from local (i.e. cell) to world co-ordinates
void ToWorldCoordinates(ESM::Pathgrid::Point& point, const ESM::Cell * cell); void ToWorldCoordinates(ESM::Pathgrid::Point& point, const ESM::Cell * cell);
void SetCurrentNodeToClosestAllowedNode(osg::Vec3f npcPos);
/// lookup table for converting idleSelect value to groupName /// lookup table for converting idleSelect value to groupName
static const std::string sIdleSelectToGroupName[GroupIndex_MaxIdle - GroupIndex_MinIdle + 1]; static const std::string sIdleSelectToGroupName[GroupIndex_MaxIdle - GroupIndex_MinIdle + 1];
}; };

Loading…
Cancel
Save