CPathfinder.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. using namespace boost::logic;
  8. int3 CPath::startPos()
  9. {
  10. return int3(nodes[nodes.size()-1].coord.x,nodes[nodes.size()-1].coord.y,nodes[nodes.size()-1].coord.z);
  11. }
  12. int3 CPath::endPos()
  13. {
  14. return int3(nodes[0].coord.x,nodes[0].coord.y,nodes[0].coord.z);
  15. }
  16. CPath * CPathfinder::getPath(int3 src, int3 dest, const CGHeroInstance * hero, unsigned char type) //TODO: test it (seems to be finished, but relies on unwritten functions :()
  17. {
  18. //check input
  19. if ((src.x < 0)||(src.y < 0)||(src.z < 0)||(dest.x < 0)||(dest.y < 0)||(dest.z < 0))
  20. {
  21. return NULL;
  22. }
  23. if ((src.x >= CGI->mh->sizes.x)||(src.y >= CGI->mh->sizes.y)||(src.z >= CGI->mh->sizes.z)
  24. ||(dest.x >= CGI->mh->sizes.x)||(dest.y >= CGI->mh->sizes.y)||(dest.z >= CGI->mh->sizes.z))
  25. {
  26. return NULL;
  27. }
  28. int3 hpos = hero->getPosition(false);
  29. tribool blockLandSea; //true - blocks sea, false - blocks land, indeterminate - allows all
  30. if (!hero->canWalkOnSea())
  31. {
  32. if (CGI->mh->ttiles[hpos.x][hpos.y][hpos.z].terType==EterrainType::water)
  33. blockLandSea=false;
  34. else
  35. blockLandSea=true;
  36. }
  37. else
  38. {
  39. blockLandSea = indeterminate;
  40. }
  41. //graph initialization
  42. graph.resize(CGI->ac->map.width);
  43. for(int i=0; i<graph.size(); ++i)
  44. {
  45. graph[i].resize(CGI->ac->map.height);
  46. for(int j=0; j<graph[i].size(); ++j)
  47. {
  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 = &graph[cp.coord.x][cp.coord.y];
  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 = &graph[cp.coord.x][cp.coord.y];
  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 = &graph[cp.coord.x][cp.coord.y];
  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 = &graph[cp.coord.x][cp.coord.y];
  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 = &graph[cp.coord.x][cp.coord.y];
  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 = &graph[cp.coord.x][cp.coord.y];
  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 = &graph[cp.coord.x][cp.coord.y];
  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 = &graph[cp.coord.x][cp.coord.y];
  161. mq.push(dp);
  162. }
  163. }
  164. }
  165. CPathNode curNode = graph[dest.x][dest.y];
  166. if(!curNode.theNodeBefore)
  167. return NULL;
  168. CPath * ret = new CPath;
  169. while(curNode.coord!=graph[src.x][src.y].coord)
  170. {
  171. ret->nodes.push_back(curNode);
  172. curNode = *(curNode.theNodeBefore);
  173. }
  174. ret->nodes.push_back(graph[src.x][src.y]);
  175. return ret;
  176. }
  177. void CPathfinder::convertPath(CPath * path, unsigned int mode) //mode=0 -> from 'manifest' to 'object'
  178. {
  179. if (mode==0)
  180. {
  181. for (int i=0;i<path->nodes.size();i++)
  182. {
  183. path->nodes[i].coord = CGHeroInstance::convertPosition(path->nodes[i].coord,true);
  184. }
  185. }
  186. }