CPathfinder.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "stdafx.h"
  2. #include "global.h"
  3. #include "CPathfinder.h"
  4. #include "CGameInfo.h"
  5. #include "hch\CAmbarCendamo.h"
  6. #include "mapHandler.h"
  7. int3 CPath::endPos()
  8. {
  9. return int3(nodes[0].coord.x,nodes[0].coord.y,nodes[0].coord.z);
  10. }
  11. int3 CPath::startPos()
  12. {
  13. return int3(nodes[nodes.size()-1].coord.x,nodes[nodes.size()-1].coord.y,nodes[nodes.size()-1].coord.z);
  14. }
  15. CPath * CPathfinder::getPath(const int3 &src, const int3 &dest, const CHeroInstance * hero) //TODO: test it (seems to be finished, but relies on unwritten functions :()
  16. {
  17. if(src.z!=dest.z) //first check
  18. return NULL;
  19. //graph initialization
  20. graph.resize(CGI->ac->map.width);
  21. for(int i=0; i<graph.size(); ++i)
  22. {
  23. graph[i].resize(CGI->ac->map.height);
  24. for(int j=0; j<graph[i].size(); ++j)
  25. {
  26. graph[i][j] = new CPathNode;
  27. graph[i][j]->accesible = !CGI->mh->ttiles[i][j][src.z].blocked;
  28. if(i==dest.x && j==dest.y && CGI->mh->ttiles[i][j][src.z].visitable)
  29. graph[i][j]->accesible = true; //for allowing visiting objects
  30. graph[i][j]->dist = -1;
  31. graph[i][j]->theNodeBefore = NULL;
  32. graph[i][j]->visited = false;
  33. graph[i][j]->coord.x = i;
  34. graph[i][j]->coord.y = j;
  35. graph[i][j]->coord.z = dest.z;
  36. }
  37. }
  38. //graph initialized
  39. graph[src.x][src.y]->dist = 0;
  40. std::queue<CPathNode *> mq;
  41. mq.push(graph[src.x][src.y]);
  42. unsigned int curDist = 4000000000;
  43. while(!mq.empty())
  44. {
  45. CPathNode * cp = mq.front();
  46. mq.pop();
  47. if ((cp->coord.x == dest.x) && (cp->coord.y==dest.y))
  48. {
  49. if (cp->dist < curDist)
  50. curDist=cp->dist;
  51. }
  52. else
  53. {
  54. if (cp->dist > curDist)
  55. continue;
  56. }
  57. if(cp->coord.x>0)
  58. {
  59. CPathNode * dp = graph[cp->coord.x-1][cp->coord.y];
  60. if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y, src.z), hero))) && dp->accesible)
  61. {
  62. dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y, src.z), hero);
  63. dp->theNodeBefore = cp;
  64. mq.push(dp);
  65. }
  66. }
  67. if(cp->coord.y>0)
  68. {
  69. CPathNode * dp = graph[cp->coord.x][cp->coord.y-1];
  70. if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x, cp->coord.y-1, src.z), hero))) && dp->accesible)
  71. {
  72. dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y, src.z), hero);
  73. dp->theNodeBefore = cp;
  74. mq.push(dp);
  75. }
  76. }
  77. if(cp->coord.x>0 && cp->coord.y>0)
  78. {
  79. CPathNode * dp = graph[cp->coord.x-1][cp->coord.y-1];
  80. if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y-1, src.z), hero))) && dp->accesible)
  81. {
  82. dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y-1, src.z), hero);
  83. dp->theNodeBefore = cp;
  84. mq.push(dp);
  85. }
  86. }
  87. if(cp->coord.x<graph.size()-1)
  88. {
  89. CPathNode * dp = graph[cp->coord.x+1][cp->coord.y];
  90. if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x+1, cp->coord.y, src.z), hero))) && dp->accesible)
  91. {
  92. dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x+1, cp->coord.y, src.z), hero);
  93. dp->theNodeBefore = cp;
  94. mq.push(dp);
  95. }
  96. }
  97. if(cp->coord.y<graph[0].size()-1)
  98. {
  99. CPathNode * dp = graph[cp->coord.x][cp->coord.y+1];
  100. if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x, cp->coord.y+1, src.z), hero))) && dp->accesible)
  101. {
  102. dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x, cp->coord.y+1, src.z), hero);
  103. dp->theNodeBefore = cp;
  104. mq.push(dp);
  105. }
  106. }
  107. if(cp->coord.x<graph.size()-1 && cp->coord.y<graph[0].size()-1)
  108. {
  109. CPathNode * dp = graph[cp->coord.x+1][cp->coord.y+1];
  110. if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x+1, cp->coord.y+1, src.z), hero))) && dp->accesible)
  111. {
  112. dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x+1, cp->coord.y+1, src.z), hero);
  113. dp->theNodeBefore = cp;
  114. mq.push(dp);
  115. }
  116. }
  117. if(cp->coord.x>0 && cp->coord.y<graph[0].size()-1)
  118. {
  119. CPathNode * dp = graph[cp->coord.x-1][cp->coord.y+1];
  120. if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y+1, src.z), hero))) && dp->accesible)
  121. {
  122. dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y+1, src.z), hero);
  123. dp->theNodeBefore = cp;
  124. mq.push(dp);
  125. }
  126. }
  127. if(cp->coord.x<graph.size()-1 && cp->coord.y>0)
  128. {
  129. CPathNode * dp = graph[cp->coord.x+1][cp->coord.y-1];
  130. if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x+1, cp->coord.y-1, src.z), hero))) && dp->accesible)
  131. {
  132. dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x+1, cp->coord.y-1, src.z), hero);
  133. dp->theNodeBefore = cp;
  134. mq.push(dp);
  135. }
  136. }
  137. }
  138. CPathNode * curNode = graph[dest.x][dest.y];
  139. CPath * ret = new CPath;
  140. while(curNode!=graph[src.x][src.y] && curNode != NULL)
  141. {
  142. ret->nodes.push_back(*curNode);
  143. curNode = curNode->theNodeBefore;
  144. }
  145. ret->nodes.push_back(*graph[src.x][src.y]);
  146. for(int i=0; i<graph.size(); ++i)
  147. {
  148. for(int j=0; j<graph[0].size(); ++j)
  149. delete graph[i][j];
  150. }
  151. return ret;
  152. }