Advertisement
Guest User

Untitled

a guest
Nov 27th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. /*
  2. ROUTER PLUGIN
  3. - GPS ADDITION TO SA-MP
  4. - Made By Gamer_Z a.k.a. grasmanek94 , Rafal Grasman
  5.  
  6. October-2011
  7.  
  8.  
  9.  
  10. http://gpb.googlecode.com/
  11. */
  12.  
  13. #include "Graph.h"
  14. #include <iostream>
  15. #include <algorithm>
  16. #include <vector>
  17. #include <stack>
  18.  
  19. Graph::Graph(void)
  20. {
  21. }
  22.  
  23. Graph::~Graph(void)
  24. {
  25. while(!mNodes.empty())
  26. {
  27. delete mNodes.back();
  28. mNodes.pop_back();
  29. }
  30. }
  31.  
  32. void Graph::addNode(int name, bool exists, Node** NodeID )
  33. {
  34. Node* pStart = NULL;
  35. mNodes.push_back(new Node(name,exists));
  36. std::vector<Node*>::iterator itr;
  37. itr = mNodes.begin()+mNodes.size()-1;
  38. pStart = (*itr);
  39. if(exists == true)pStart->DoesExist_yes();
  40. *NodeID = pStart;
  41. }
  42.  
  43. void Graph::connect_oneway(Node* pFirst, Node* pSecond, int moveCost)
  44. {
  45. if(pFirst != NULL && pSecond != NULL)
  46. {
  47. pFirst->createEdge(pSecond, moveCost);
  48. }
  49. }
  50.  
  51. #define MAX_NODES (32768)
  52. #define MAX_CONNECTIONS (5)
  53. #include <time.h>
  54.  
  55. void Graph::findPath_r(Node* pStart, Node* pEnd, std::vector<cell> &road, cell &costMx)
  56. {
  57. if(pStart == pEnd)
  58. {
  59. return;
  60. }
  61.  
  62. std::vector<Node*> openList;
  63. openList.push_back(pStart);
  64. Node* pCurrNode = NULL;
  65.  
  66. while(!openList.empty())
  67. {
  68. //Get best node from open list (lowest F value).
  69. //Since we sort the list at the end of the previous loop we know
  70. //the front node is the best
  71. pCurrNode = openList.front();
  72.  
  73. //Exit if we're are the goal
  74. if(pCurrNode == pEnd)
  75. break;
  76.  
  77. //Remove the node from the open list and place it in the closed
  78. openList.erase(openList.begin());
  79. pCurrNode->setClosed(true); //We use a flag instead of a list for speed
  80. //Test all of the edge nodes from the current node
  81. std::vector<Edge*>* pEdges = pCurrNode->getEdges();
  82. Node* pEdgeNode = NULL;
  83. for(std::vector<Edge*>::iterator i = pEdges->begin(); i != pEdges->end(); ++i)
  84. {
  85. pEdgeNode = (*i)->pNode;
  86. //If it's closed we've already analysed it
  87. if(!pEdgeNode->getClosed() && pCurrNode->DoesExist() == true)
  88. {
  89. if(!inList(pEdgeNode,&openList))
  90. {
  91. openList.push_back(pEdgeNode);
  92. pEdgeNode->setGCost(pCurrNode->getGCost()+(*i)->moveCost);
  93. pEdgeNode->calcFCost();
  94. pEdgeNode->setParent(pCurrNode);
  95. }
  96. else
  97. {
  98. //If this is a better node (lower G cost)
  99. if(pEdgeNode->getGCost() > pCurrNode->getGCost()+(*i)->moveCost)
  100. {
  101. pEdgeNode->setGCost(pCurrNode->getGCost()+(*i)->moveCost);
  102. pEdgeNode->calcFCost();
  103. pEdgeNode->setParent(pCurrNode);
  104. }
  105. }
  106. }
  107. }
  108. //Place the lowest F cost item in the open list at the top, so we can
  109. //access it easily next iteration
  110. std::sort(openList.begin(), openList.end(), Graph::compareNodes);
  111. }
  112. //Make sure we actually found a path
  113. if(pEnd->getParent() != NULL)
  114. {
  115. //Output the path
  116. //Use a stack because it is LIFO
  117. std::stack<Node*> path;
  118. while(pCurrNode != NULL)
  119. {
  120. path.push(pCurrNode);
  121. pCurrNode = pCurrNode->getParent();
  122. }
  123. while(!path.empty())
  124. {
  125. road.push_back(path.top()->getName());
  126. costMx += path.top()->getGCost();
  127. path.pop();
  128. }
  129. return;
  130. }
  131. return;
  132. }
  133.  
  134. bool Graph::inList(Node* pNode, std::vector<Node*>* pList)
  135. {
  136. for(std::vector<Node*>::iterator i = pList->begin(); i != pList->end(); ++i)
  137. {
  138. if((*i) == pNode)
  139. {
  140. return true;
  141. }
  142. }
  143.  
  144. return false;
  145. }
  146.  
  147. bool Graph::compareNodes(Node* pFirst, Node* pSecond)
  148. {
  149. return pFirst->getFCost() < pSecond->getFCost();
  150. }
  151.  
  152. void Graph::reset(void)
  153. {
  154. for(std::vector<Node*>::iterator i = mNodes.begin(); i != mNodes.end(); ++i)
  155. {
  156. (*i)->reset();
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement