// 搜索路径 CPoint* FindPath(CPoint* start, CPoint* end, bool isIgnoreCorner) { m_openVec.push_back(start); while(0 != m_openVec.size()) { CPoint* tmpStart = GetMinFPoint(); // 获取F值最小的点 RemoveFromOpenVec(tmpStart); m_closeVec.push_back(tmpStart); POINTVEC adjacentPoints = GetAdjacentPoints(tmpStart, isIgnoreCorner); for(POINTVEC::iterator it=adjacentPoints.begin(); it != adjacentPoints.end(); ++it) { CPoint* point = *it; if(isInOpenVec(point->X, point->Y)) // 在开启列表 RefreshPoint(tmpStart, point); //else if(inCloseVec(point)) //{ // 检查节点的g值,如果新计算得到的路径开销比该g值低,那么要重新打开该节点(即重新放入OPEN集) //} else NotFoundPoint(tmpStart, end, point); } if(isInOpenVec(end->X, end->Y)) // 目标点已经在开启列表中 { for(int i=0; i < m_openVec.size(); ++i) { if(end->X == m_openVec[i]->X && end->Y == m_openVec[i]->Y) return m_openVec[i]; } } } return end; }
BOOL KPathFinder::FindPath(KCellEx* pSrc, KCellEx* pDest, KLIST_AUTOPATH& lstAutoPath, BOOL bAllObstacle) { BOOL bResult = false; BOOL bRetCode = false; KCellEx* pCurrent = NULL; KCellEx* pSurround = NULL; m_setOpenPoint.clear(); memset(m_pAllWayFindingData, 0, sizeof(KWayFindingData) * m_nXLength * m_nYLength); m_setOpenPoint.insert(pSrc); pSrc->pWayFindingData->bOpenPoint = true; while (m_setOpenPoint.size()) { pCurrent = *m_setOpenPoint.begin(); m_setOpenPoint.erase(m_setOpenPoint.begin()); pCurrent->pWayFindingData->bOpenPoint = false; pCurrent->pWayFindingData->bClosePoint = true; UpdateSurroundPoints(pCurrent, bAllObstacle); for (KVEC_SURROUND_POINT::iterator it = m_vecSurroundPoint.begin(); it != m_vecSurroundPoint.end(); ++it) { pSurround = *it; if (pSurround->pWayFindingData->bOpenPoint) { FoundPoint(pCurrent, pSurround); } else { NotFoundPoint(pCurrent, pDest, pSurround); } } if (pDest->pWayFindingData->bOpenPoint) { MakePath(pDest, lstAutoPath); bResult = true; break; } } return bResult; }