CPathfinder.cpp 4.6 KB

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