CPathfinder.cpp 5.7 KB

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