当前位置: 首页>>代码示例>>C++>>正文


C++ AStarNode::setVisitor方法代码示例

本文整理汇总了C++中AStarNode::setVisitor方法的典型用法代码示例。如果您正苦于以下问题:C++ AStarNode::setVisitor方法的具体用法?C++ AStarNode::setVisitor怎么用?C++ AStarNode::setVisitor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在AStarNode的用法示例。


在下文中一共展示了AStarNode::setVisitor方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: startAlgorithm

AStarNode* AStarAlgorithm::startAlgorithm(AStarNode* startNode, AStarNode* endNode)
{
	std::cout << "A*-ALGORITHM WILL BE STARTED" << std::endl;

	std::vector<AStarNode*> pathList;
	std::cout << startNode->getName() << " GOES INTO THE PATHLIST" << std::endl;
	pathList.push_back(startNode);

	std::vector<AStarNode*> wastedList;

	AStarNode* tmp;
	bool wasIn = false;
	bool shorterPathFound = false;

	int distanceTraveled = 0;

	bool secondOut = false;
	AStarNode* returnNode;
	returnNode = startNode;

	while (pathList.back()->getName() != endNode->getName() && !pathList.empty())
	{
		std::cout << "FRONT ELEMENT OF THE LIST GOES OUT: " << pathList.back()->getName()<< "(" << pathList.back()->getTemporary() << ")" << std::endl;
		tmp = pathList.back();
		wastedList.push_back(pathList.back());
		pathList.pop_back();

		std::cout << "THE OTHER ELEMENTS IN THE LIST ARE: ";
		for (int bb = 0; bb < pathList.size(); bb++)
		{
			std::cout << pathList.at(bb)->getName() << "(" << pathList.at(bb)->getTemporary() << ")";
		}
		std::cout << std::endl;

		if (secondOut)
		{
			returnNode = tmp;
			secondOut = false;
		}

		if (tmp->getName() == startNode->getName())
		{
			secondOut = true;
		}

		for (int i = 0; i < tmp->getPaths()->size(); i++)
		{
			//Test, if the endnode of the path that we want to add, was visited beforehand. If this was the case, it would be an returnpath which we do not want here.
			if (tmp->getPaths()->at(i)->getEndNode()->getName() != tmp->getVisitor()->getName() && tmp->getPaths()->at(i)->getEndNode()->getName() != startNode->getName())
			{
				/*Now we have to check if there is a node with the same name in our pathlist.
				*If there is, we want to copy it, because we want to be able to safe more than one path to a node*/
				AStarNode* copyNode = new AStarNode();
				for (int j = 0; j < pathList.size(); j++)
				{
					if (tmp->getPaths()->at(i)->getEndNode()->getName() == pathList.at(j)->getName())
					{
						wasIn = true;
						copyNode->setPathList(*pathList.at(j)->getPaths());
						copyNode->setName(pathList.at(j)->getName());
						break;
					}
				}
				for (int m = 0; m < wastedList.size(); m++)
				{
					if (tmp->getPaths()->at(i)->getEndNode()->getName() == wastedList.at(m)->getName())
					{
						wasIn = true;
						copyNode->setPathList(*wastedList.at(m)->getPaths());
						copyNode->setName(wastedList.at(m)->getName());
						break;
					}
				}

				//If the node was not found in the pathlist, it will added normally
				if (!wasIn){
					tmp->getPaths()->at(i)->getEndNode()->setDistanceTravelled(tmp->getPaths()->at(i)->getTimeToTravel() + tmp->getDistanceTravelled());
					tmp->getPaths()->at(i)->getEndNode()->setTemporary(tmp->getPaths()->at(i)->getEndNode()->getDistanceToGoal() + tmp->getPaths()->at(i)->getEndNode()->getDistanceTravelled());
					
					//We just add the path, if there is no path in the list which is shorter
					for (int n = 0; n < pathList.size(); n++)
					{
						//There is a path with a shorter travelled disitance
						if (pathList.at(n)->getName() == tmp->getPaths()->at(i)->getEndNode()->getName() && pathList.at(n)->getDistanceTravelled() < tmp->getPaths()->at(i)->getEndNode()->getDistanceTravelled())
						{
							//setting the boolean
							shorterPathFound = true;

						}
						//the existing path to the node which has a longer distanceTravelled will be deleted from the list
						else if (pathList.at(n)->getName() == tmp->getPaths()->at(i)->getEndNode()->getName() && pathList.at(n)->getDistanceTravelled() >= tmp->getPaths()->at(i)->getEndNode()->getDistanceTravelled()){
							pathList.erase(pathList.begin() + n);
						}
					}

					if (!shorterPathFound)
					{
						pathList.push_back(tmp->getPaths()->at(i)->getEndNode());
						tmp->getPaths()->at(i)->getEndNode()->setVisitor(tmp);
					}
//.........这里部分代码省略.........
开发者ID:GameEngineKoblenz,项目名称:GeKo,代码行数:101,代码来源:AStarAlgorithm.cpp


注:本文中的AStarNode::setVisitor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。