I am rewriting some functions of a game. The thing is that there are now larger nodes to be searched than before. You can see that I have removed sqrt operations in the hopes of making this function very time critical. Is there any way it can be optimized more?
Code:
int CPathFind::FindNodeClosestToCoors(float fX, float fY, float fZ, unsigned char iPathDataFor, float fRangeCoefficient, bool bCheckIgnored, bool bCheckRestrictedAccess, bool bCheckUnkFlagFor2, bool bIsVehicleBoat){
int iStartNodeIndex, iEndNodeIndex;
switch (iPathDataFor)
{
case PATHDATAFOR_CAR:
iStartNodeIndex = 0;
iEndNodeIndex = m_nCarAttachedNodes;
break;
case PATHDATAFOR_PED:
iStartNodeIndex = m_nCarAttachedNodes;
iEndNodeIndex = m_nAttachedNodes;
break;
}
float fPrevFoundRangeCoeff = 10000.0f;
int iPrevFoundRangedNode = 0;
CPathNode* pNode = m_AttachedPaths[iStartNodeIndex];
for (int i = iStartNodeIndex; i < iEndNodeIndex; i++){
if ((bCheckIgnored == false || !(pNode->bitIgnoredNode)) &&
(bCheckRestrictedAccess == false || !(pNode->bitRestrictedAccess)) &&
(bCheckUnkFlagFor2 == false || !(pNode->bitUnkFlagFor2)) &&
(bIsVehicleBoat == pNode->bitIsVehicleBoat))
{
fXDiff = utl::abs<float>(((float)pNode->wX / 8.0f) - fX);
fYDiff = utl::abs<float>(((float)pNode->wY / 8.0f) - fY);
fZDiff = utl::abs<float>(((float)pNode->wZ / 8.0f) - fZ);
float fCurrentCoeff = fXDiff + fYDiff + fZDiff * 3.0f;
if ( fCurrentCoeff < fPrevFoundRangeCoeff){
fPrevFoundRangeCoeff = fCurrentCoeff;
iPrevFoundRangedNode = i;
}
}
pNode++;
}
if ( fPrevFoundRangeCoeff < fRangeCoefficient)
return iPrevFoundRangedNode;
else
return -1;
}