Advertisement
Guest User

Zombie

a guest
Feb 8th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. void Zombie::findPath(std::vector< std::vector<Tile> >* pVTiles)
  2. {
  3.   if(targetPosition_ != sf::Vector2i(0.0f, 0.0f))
  4.     {
  5.       //Initiates lists
  6.       std::vector<std::shared_ptr<Node>> openList;
  7.       std::vector<std::shared_ptr<Node>> closedList;
  8.       std::shared_ptr<Node> currentNode;    
  9.       bool pathFound = false;
  10.      
  11.       //Initiates the great journey
  12.       openList.push_back(std::make_shared<Node>(sf::Vector2i(positionGlobal_.x - fmod(positionGlobal_.x,  32.0f) + 16.0f, positionGlobal_.y - fmod(positionGlobal_.y, 32.0f) + 16.0f)));
  13.       openList.back()->setIsStartNode(true);
  14.       std::push_heap(openList.begin(), openList.end()); //NEED COMPARISON FOR THE POINTERS HERE
  15.  
  16.       while(!pathFound)
  17.     {
  18.       //Gets the a pointer to the top item in the openList, then moves it to the closed list
  19.       std::pop_heap(openList.begin(), openList.end()); //NEED COMPARISON FOR THE POINTERS HERE
  20.       currentNode = openList.back();
  21.       closedList.push_back(currentNode);
  22.       openList.pop_back();
  23.  
  24.       //Debug info
  25.       std::cout << std::endl << "TargetPosition: " << targetPosition_.x / 32 << ", " << targetPosition_.y / 32 << std::endl;
  26.       std::cout << "Position: " << currentNode->getPosition().x / 32 << ", " << currentNode->getPosition().y / 32 << std::endl;
  27.       std::cout << "Distance from start node: " << currentNode->getDistanceValue() / 32.0f << std::endl;
  28.       std::cout << "Heuristic Value: " << currentNode->getHeuristicValue() / 32.0f<< std::endl;
  29.       std::cout << "Total: " << currentNode->getTotalValue() / 32.0f << std::endl;
  30.  
  31.       //For the eight neighboring tiles/nodes
  32.       for (int i = 0; i < 8; ++i)
  33.         {
  34.           int xPos;
  35.           int yPos;  
  36.          
  37.           //xPos
  38.           if(i == 0 || i == 4)
  39.         xPos = 0;
  40.           else if(i > 0 && i < 4)
  41.         xPos = 1;
  42.           else
  43.         xPos = -1;
  44.          
  45.           //yPos
  46.           if(i == 2 || i == 6)
  47.         yPos = 0;
  48.           else if(i < 2 || i > 6)
  49.         yPos = 1;
  50.           else
  51.         yPos = -1;
  52.          
  53.           sf::Vector2i nodePosition = currentNode->getPosition() + sf::Vector2i(xPos * 32, yPos * 32);
  54.  
  55.           //Stop working if the node/tile is a wall or contains a tree
  56.           if(pVTiles->at(nodePosition.x / 32).at(nodePosition.y / 32).getType() == "unwalkable" || pVTiles->at(nodePosition.y / 32).at(nodePosition.x / 32).hasItem())     
  57.           continue;
  58.  
  59.           //Creates a node for the tile
  60.           auto node = std::make_shared<Node>(currentNode, sf::Vector2i(xPos, yPos));
  61.  
  62.           //Checks to see if it is the target adds node to stack and breaks if so
  63.           if(node->getPosition() == targetPosition_)
  64.         {
  65.           std::cout << "found target!" << std::endl;
  66.           pathFound = true;
  67.           sPNodes_.push(node);
  68.           break;
  69.         }
  70.  
  71.           //If it's not the target
  72.           if(!pathFound)
  73.         {
  74.           float parentDistanceValue = node->getParentNodePtr()->getDistanceValue();
  75.  
  76.           //Distance is 1.4f x 32 if diagonal, 1 x 32 otherwise
  77.           if(xPos == yPos)
  78.             node->setDistanceValue(parentDistanceValue + 44.8f);
  79.           else
  80.             node->setDistanceValue(parentDistanceValue + 32.0f);
  81.  
  82.           //Gets the distance to the target(Heuristic) and then gets the total(Distance + Heuristic)
  83.           node->setHeuristicValue(sqrt(pow(node->getPosition().x - targetPosition_.x, 2) + pow(node->getPosition().y - targetPosition_.y, 2)));
  84.           node->setTotalValue(node->getHeuristicValue() + node->getDistanceValue());
  85.          
  86.           //Makes sure the node is not already in the open or closed list (NEED TO COMPARE VALUE OF WHAT THE POINTERS POINT TO HERE)
  87.           if(std::find(openList.begin(), openList.end(), node) == openList.end() && std::find(closedList.begin(), closedList.end(), node) == closedList.end())
  88.             {      
  89.               openList.push_back(node);
  90.               std::push_heap(openList.begin(), openList.end()); //NEED COMPARISON HERE
  91.             }    
  92.         }
  93.         }
  94.     }
  95.  
  96.       //Keeps stacking parent nodes until the start is reached
  97.       while(!sPNodes_.top()->isStartNode())
  98.     {
  99.       std::cout << "stacking backward..." << std::endl;
  100.       std::cout << "Position: " << sPNodes_.top()->getPosition().x / 32 << ", " << sPNodes_.top()->getPosition().y / 32 << std::endl;
  101.       auto parent = sPNodes_.top()->getParentNodePtr();
  102.       sPNodes_.push(parent);    
  103.     }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement