CPathfinder.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. //check input
  18. if ((src.x < 0)||(src.y < 0)||(src.z < 0)||(dest.x < 0)||(dest.y < 0)||(dest.z < 0))
  19. {
  20. return NULL;
  21. }
  22. if ((src.x >= CGI->mh->sizes.x)||(src.y >= CGI->mh->sizes.y)||(src.z >= CGI->mh->sizes.z)
  23. ||(dest.x >= CGI->mh->sizes.x)||(dest.y >= CGI->mh->sizes.y)||(dest.z >= CGI->mh->sizes.z))
  24. {
  25. return NULL;
  26. }
  27. int3 hpos = hero->getPosition(false);
  28. tribool blockLandSea; //true - blocks sea, false - blocks land, indeterminate - allows all
  29. if (!hero->canWalkOnSea())
  30. {
  31. if (CGI->mh->ttiles[hpos.x][hpos.y][hpos.z].terType==EterrainType::water)
  32. blockLandSea=false;
  33. else
  34. blockLandSea=true;
  35. }
  36. else
  37. {
  38. blockLandSea = indeterminate;
  39. }
  40. //graph initialization
  41. graph.resize(CGI->ac->map.width);
  42. for(int i=0; i<graph.size(); ++i)
  43. {
  44. graph[i].resize(CGI->ac->map.height);
  45. for(int j=0; j<graph[i].size(); ++j)
  46. {
  47. graph[i][j] = new CPathNode;
  48. graph[i][j]->accesible = !CGI->mh->ttiles[i][j][src.z].blocked;
  49. if(i==dest.x && j==dest.y && CGI->mh->ttiles[i][j][src.z].visitable)
  50. graph[i][j]->accesible = true; //for allowing visiting objects
  51. graph[i][j]->dist = -1;
  52. graph[i][j]->theNodeBefore = NULL;
  53. graph[i][j]->visited = false;
  54. graph[i][j]->coord.x = i;
  55. graph[i][j]->coord.y = j;
  56. graph[i][j]->coord.z = dest.z;
  57. if (CGI->mh->ttiles[i][j][src.z].terType==EterrainType::rock)
  58. graph[i][j]->accesible = false;
  59. if ((blockLandSea) && (CGI->mh->ttiles[i][j][src.z].terType==EterrainType::water))
  60. graph[i][j]->accesible = false;
  61. else if ((!blockLandSea) && (CGI->mh->ttiles[i][j][src.z].terType!=EterrainType::water))
  62. graph[i][j]->accesible = false;
  63. }
  64. }
  65. //graph initialized
  66. graph[src.x][src.y]->dist = 0;
  67. std::queue<CPathNode *> mq;
  68. mq.push(graph[src.x][src.y]);
  69. unsigned int curDist = 4000000000;
  70. while(!mq.empty())
  71. {
  72. CPathNode * cp = mq.front();
  73. mq.pop();
  74. if ((cp->coord.x == dest.x) && (cp->coord.y==dest.y))
  75. {
  76. if (cp->dist < curDist)
  77. curDist=cp->dist;
  78. }
  79. else
  80. {
  81. if (cp->dist > curDist)
  82. continue;
  83. }
  84. if(cp->coord.x>0)
  85. {
  86. CPathNode * dp = graph[cp->coord.x-1][cp->coord.y];
  87. 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)
  88. {
  89. 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);
  90. dp->theNodeBefore = cp;
  91. mq.push(dp);
  92. }
  93. }
  94. if(cp->coord.y>0)
  95. {
  96. CPathNode * dp = graph[cp->coord.x][cp->coord.y-1];
  97. 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)
  98. {
  99. 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);
  100. dp->theNodeBefore = cp;
  101. mq.push(dp);
  102. }
  103. }
  104. if(cp->coord.x>0 && cp->coord.y>0)
  105. {
  106. CPathNode * dp = graph[cp->coord.x-1][cp->coord.y-1];
  107. 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)
  108. {
  109. 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);
  110. dp->theNodeBefore = cp;
  111. mq.push(dp);
  112. }
  113. }
  114. if(cp->coord.x<graph.size()-1)
  115. {
  116. CPathNode * dp = graph[cp->coord.x+1][cp->coord.y];
  117. 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)
  118. {
  119. 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);
  120. dp->theNodeBefore = cp;
  121. mq.push(dp);
  122. }
  123. }
  124. if(cp->coord.y<graph[0].size()-1)
  125. {
  126. CPathNode * dp = graph[cp->coord.x][cp->coord.y+1];
  127. 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)
  128. {
  129. 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);
  130. dp->theNodeBefore = cp;
  131. mq.push(dp);
  132. }
  133. }
  134. if(cp->coord.x<graph.size()-1 && cp->coord.y<graph[0].size()-1)
  135. {
  136. CPathNode * dp = graph[cp->coord.x+1][cp->coord.y+1];
  137. 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)
  138. {
  139. 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);
  140. dp->theNodeBefore = cp;
  141. mq.push(dp);
  142. }
  143. }
  144. if(cp->coord.x>0 && cp->coord.y<graph[0].size()-1)
  145. {
  146. CPathNode * dp = graph[cp->coord.x-1][cp->coord.y+1];
  147. 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)
  148. {
  149. 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);
  150. dp->theNodeBefore = cp;
  151. mq.push(dp);
  152. }
  153. }
  154. if(cp->coord.x<graph.size()-1 && cp->coord.y>0)
  155. {
  156. CPathNode * dp = graph[cp->coord.x+1][cp->coord.y-1];
  157. 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)
  158. {
  159. 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);
  160. dp->theNodeBefore = cp;
  161. mq.push(dp);
  162. }
  163. }
  164. }
  165. CPathNode * curNode = graph[dest.x][dest.y];
  166. CPath * ret = new CPath;
  167. while(curNode!=graph[src.x][src.y] && curNode != NULL)
  168. {
  169. ret->nodes.push_back(*curNode);
  170. curNode = curNode->theNodeBefore;
  171. }
  172. ret->nodes.push_back(*graph[src.x][src.y]);
  173. for(int i=0; i<graph.size(); ++i)
  174. {
  175. for(int j=0; j<graph[0].size(); ++j)
  176. delete graph[i][j];
  177. }
  178. return ret;
  179. }
  180. void CPathfinder::convertPath(CPath * path, unsigned int mode) //mode=0 -> from 'manifest' to 'object'
  181. {
  182. if (mode==0)
  183. {
  184. for (int i=0;i<path->nodes.size();i++)
  185. {
  186. path->nodes[i].coord = CHeroInstance::convertPosition(path->nodes[i].coord,true);
  187. }
  188. }
  189. }