Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Zombie::findPath(std::vector< std::vector<Tile> >* pVTiles)
- {
- if(targetPosition_ != sf::Vector2i(0.0f, 0.0f))
- {
- //Initiates lists
- std::vector<std::shared_ptr<Node>> openList;
- std::vector<std::shared_ptr<Node>> closedList;
- std::shared_ptr<Node> currentNode;
- bool pathFound = false;
- //Initiates the great journey
- 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)));
- openList.back()->setIsStartNode(true);
- std::push_heap(openList.begin(), openList.end()); //NEED COMPARISON FOR THE POINTERS HERE
- while(!pathFound)
- {
- //Gets the a pointer to the top item in the openList, then moves it to the closed list
- std::pop_heap(openList.begin(), openList.end()); //NEED COMPARISON FOR THE POINTERS HERE
- currentNode = openList.back();
- closedList.push_back(currentNode);
- openList.pop_back();
- //Debug info
- std::cout << std::endl << "TargetPosition: " << targetPosition_.x / 32 << ", " << targetPosition_.y / 32 << std::endl;
- std::cout << "Position: " << currentNode->getPosition().x / 32 << ", " << currentNode->getPosition().y / 32 << std::endl;
- std::cout << "Distance from start node: " << currentNode->getDistanceValue() / 32.0f << std::endl;
- std::cout << "Heuristic Value: " << currentNode->getHeuristicValue() / 32.0f<< std::endl;
- std::cout << "Total: " << currentNode->getTotalValue() / 32.0f << std::endl;
- //For the eight neighboring tiles/nodes
- for (int i = 0; i < 8; ++i)
- {
- int xPos;
- int yPos;
- //xPos
- if(i == 0 || i == 4)
- xPos = 0;
- else if(i > 0 && i < 4)
- xPos = 1;
- else
- xPos = -1;
- //yPos
- if(i == 2 || i == 6)
- yPos = 0;
- else if(i < 2 || i > 6)
- yPos = 1;
- else
- yPos = -1;
- sf::Vector2i nodePosition = currentNode->getPosition() + sf::Vector2i(xPos * 32, yPos * 32);
- //Stop working if the node/tile is a wall or contains a tree
- if(pVTiles->at(nodePosition.x / 32).at(nodePosition.y / 32).getType() == "unwalkable" || pVTiles->at(nodePosition.y / 32).at(nodePosition.x / 32).hasItem())
- continue;
- //Creates a node for the tile
- auto node = std::make_shared<Node>(currentNode, sf::Vector2i(xPos, yPos));
- //Checks to see if it is the target adds node to stack and breaks if so
- if(node->getPosition() == targetPosition_)
- {
- std::cout << "found target!" << std::endl;
- pathFound = true;
- sPNodes_.push(node);
- break;
- }
- //If it's not the target
- if(!pathFound)
- {
- float parentDistanceValue = node->getParentNodePtr()->getDistanceValue();
- //Distance is 1.4f x 32 if diagonal, 1 x 32 otherwise
- if(xPos == yPos)
- node->setDistanceValue(parentDistanceValue + 44.8f);
- else
- node->setDistanceValue(parentDistanceValue + 32.0f);
- //Gets the distance to the target(Heuristic) and then gets the total(Distance + Heuristic)
- node->setHeuristicValue(sqrt(pow(node->getPosition().x - targetPosition_.x, 2) + pow(node->getPosition().y - targetPosition_.y, 2)));
- node->setTotalValue(node->getHeuristicValue() + node->getDistanceValue());
- //Makes sure the node is not already in the open or closed list (NEED TO COMPARE VALUE OF WHAT THE POINTERS POINT TO HERE)
- if(std::find(openList.begin(), openList.end(), node) == openList.end() && std::find(closedList.begin(), closedList.end(), node) == closedList.end())
- {
- openList.push_back(node);
- std::push_heap(openList.begin(), openList.end()); //NEED COMPARISON HERE
- }
- }
- }
- }
- //Keeps stacking parent nodes until the start is reached
- while(!sPNodes_.top()->isStartNode())
- {
- std::cout << "stacking backward..." << std::endl;
- std::cout << "Position: " << sPNodes_.top()->getPosition().x / 32 << ", " << sPNodes_.top()->getPosition().y / 32 << std::endl;
- auto parent = sPNodes_.top()->getParentNodePtr();
- sPNodes_.push(parent);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement