CPathfinder.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #include "CGameState.h"
  8. #include "hch/CObjectHandler.h"
  9. #include "hch/CDefObjInfoHandler.h"
  10. using namespace boost::logic;
  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. int3 CPath::endPos()
  16. {
  17. return int3(nodes[0].coord.x,nodes[0].coord.y,nodes[0].coord.z);
  18. }
  19. 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 :()
  20. {
  21. //check input
  22. if ((src.x < 0)||(src.y < 0)||(src.z < 0)||(dest.x < 0)||(dest.y < 0)||(dest.z < 0))
  23. {
  24. return NULL;
  25. }
  26. if ((src.x >= CGI->mh->sizes.x)||(src.y >= CGI->mh->sizes.y)||(src.z >= CGI->mh->sizes.z)
  27. ||(dest.x >= CGI->mh->sizes.x)||(dest.y >= CGI->mh->sizes.y)||(dest.z >= CGI->mh->sizes.z))
  28. {
  29. return NULL;
  30. }
  31. int3 hpos = hero->getPosition(false);
  32. tribool blockLandSea; //true - blocks sea, false - blocks land, indeterminate - allows all
  33. if (!hero->canWalkOnSea())
  34. {
  35. if (CGI->mh->ttiles[hpos.x][hpos.y][hpos.z].tileInfo->tertype==water)
  36. blockLandSea=false;
  37. else
  38. blockLandSea=true;
  39. }
  40. else
  41. {
  42. blockLandSea = indeterminate;
  43. }
  44. //graph initialization
  45. graph.resize(CGI->mh->sizes.x);
  46. for(size_t i=0; i<graph.size(); ++i)
  47. {
  48. graph[i].resize(CGI->mh->sizes.y);
  49. for(size_t j=0; j<graph[i].size(); ++j)
  50. {
  51. graph[i][j].accesible = !CGI->mh->ttiles[i][j][src.z].tileInfo->blocked;
  52. if(i==dest.x && j==dest.y && CGI->mh->ttiles[i][j][src.z].tileInfo->visitable)
  53. {
  54. graph[i][j].accesible = true; //for allowing visiting objects
  55. }
  56. graph[i][j].dist = -1;
  57. graph[i][j].theNodeBefore = NULL;
  58. graph[i][j].visited = false;
  59. graph[i][j].coord.x = i;
  60. graph[i][j].coord.y = j;
  61. graph[i][j].coord.z = dest.z;
  62. if (CGI->mh->ttiles[i][j][src.z].tileInfo->tertype==rock)
  63. {
  64. graph[i][j].accesible = false;
  65. }
  66. if ((blockLandSea) && (CGI->mh->ttiles[i][j][src.z].tileInfo->tertype==water))
  67. {
  68. graph[i][j].accesible = false;
  69. }
  70. else if ((!blockLandSea) && (CGI->mh->ttiles[i][j][src.z].tileInfo->tertype!=water))
  71. {
  72. graph[i][j].accesible = false;
  73. }
  74. if(graph[i][j].accesible)
  75. {
  76. graph[i][j].accesible = CGI->state->players[hero->tempOwner].fogOfWarMap[i][j][src.z];
  77. }
  78. }
  79. }
  80. //graph initialized
  81. graph[src.x][src.y].dist = 0;
  82. std::queue<CPathNode> mq;
  83. mq.push(graph[src.x][src.y]);
  84. unsigned int curDist = 4000000000; //XXX 2 147 483 648 // only in C90 //but numeric limit shows 0-4294967295...confused
  85. while(!mq.empty())
  86. {
  87. CPathNode cp = mq.front();
  88. mq.pop();
  89. if ((cp.coord.x == dest.x) && (cp.coord.y==dest.y))
  90. {
  91. if (cp.dist < curDist)
  92. curDist=cp.dist;
  93. }
  94. else
  95. {
  96. if (cp.dist > curDist)
  97. continue;
  98. }
  99. if(cp.coord.x>0)
  100. {
  101. CPathNode & dp = graph[cp.coord.x-1][cp.coord.y];
  102. processNode(dp, hero, mq, cp, src, false);
  103. }
  104. if(cp.coord.y>0)
  105. {
  106. CPathNode & dp = graph[cp.coord.x][cp.coord.y-1];
  107. processNode(dp, hero, mq, cp, src, false);
  108. }
  109. if(cp.coord.x>0 && cp.coord.y>0)
  110. {
  111. CPathNode & dp = graph[cp.coord.x-1][cp.coord.y-1];
  112. processNode(dp, hero, mq, cp, src, true);
  113. }
  114. if(cp.coord.x<graph.size()-1)
  115. {
  116. CPathNode & dp = graph[cp.coord.x+1][cp.coord.y];
  117. processNode(dp, hero, mq, cp, src, false);
  118. }
  119. if(cp.coord.y<graph[0].size()-1)
  120. {
  121. CPathNode & dp = graph[cp.coord.x][cp.coord.y+1];
  122. processNode(dp, hero, mq, cp, src, false);
  123. }
  124. if(cp.coord.x<graph.size()-1 && cp.coord.y<graph[0].size()-1)
  125. {
  126. CPathNode & dp = graph[cp.coord.x+1][cp.coord.y+1];
  127. processNode(dp, hero, mq, cp, src, true);
  128. }
  129. if(cp.coord.x>0 && cp.coord.y<graph[0].size()-1)
  130. {
  131. CPathNode & dp = graph[cp.coord.x-1][cp.coord.y+1];
  132. processNode(dp, hero, mq, cp, src, true);
  133. }
  134. if(cp.coord.x<graph.size()-1 && cp.coord.y>0)
  135. {
  136. CPathNode & dp = graph[cp.coord.x+1][cp.coord.y-1];
  137. processNode(dp, hero, mq, cp, src, true);
  138. }
  139. }
  140. CPathNode curNode = graph[dest.x][dest.y];
  141. if(!curNode.theNodeBefore)
  142. return NULL;
  143. CPath * ret = new CPath;
  144. while(curNode.coord!=graph[src.x][src.y].coord)
  145. {
  146. ret->nodes.push_back(curNode);
  147. curNode = *(curNode.theNodeBefore);
  148. }
  149. ret->nodes.push_back(graph[src.x][src.y]);
  150. return ret;
  151. }
  152. void CPathfinder::convertPath(CPath * path, unsigned int mode) //mode=0 -> from 'manifest' to 'object'
  153. {
  154. if (mode==0)
  155. {
  156. for (int i=0;i<path->nodes.size();i++)
  157. {
  158. path->nodes[i].coord = CGHeroInstance::convertPosition(path->nodes[i].coord,true);
  159. }
  160. }
  161. }
  162. void CPathfinder::processNode(CPathNode & dp, const CGHeroInstance * hero, std::queue<CPathNode> & mq, const CPathNode & cp, const int3 & src, bool diagonal)
  163. {
  164. const TerrainTile * tinfo = CGI->mh->ttiles[dp.coord.x][dp.coord.y][src.z].tileInfo;
  165. int cost = hero->getTileCost(tinfo->tertype, tinfo->malle, tinfo->nuine, hero->movement - cp.dist);
  166. if(diagonal && (hero->movement - cp.dist) > 145) //second condition - workaround for strange behaviour manifested by Heroes III
  167. cost *= std::sqrt(2.0);
  168. if((dp.dist==-1 || (dp.dist > cp.dist + cost)) && dp.accesible && checkForVisitableDir(cp.coord, dp.coord) && checkForVisitableDir(dp.coord, cp.coord))
  169. {
  170. dp.dist = cp.dist + cost;
  171. dp.theNodeBefore = &graph[cp.coord.x][cp.coord.y];
  172. mq.push(dp);
  173. }
  174. }
  175. bool CPathfinder::checkForVisitableDir(const int3 & src, const int3 & dst) const
  176. {
  177. for(int b=0; b<CGI->mh->ttiles[dst.x][dst.y][dst.z].tileInfo->visitableObjects.size(); ++b) //checking destination tile
  178. {
  179. const TerrainTile * pom = CGI->mh->ttiles[dst.x][dst.y][dst.z].tileInfo;
  180. if(!vstd::contains(pom->blockingObjects, pom->visitableObjects[b]))
  181. continue;
  182. CGDefInfo * di = pom->visitableObjects[b]->defInfo;
  183. if( (dst.x == src.x-1 && dst.y == src.y-1) && !(di->visitDir & (1<<4)) )
  184. {
  185. return false;
  186. }
  187. if( (dst.x == src.x && dst.y == src.y-1) && !(di->visitDir & (1<<5)) )
  188. {
  189. return false;
  190. }
  191. if( (dst.x == src.x+1 && dst.y == src.y-1) && !(di->visitDir & (1<<6)) )
  192. {
  193. return false;
  194. }
  195. if( (dst.x == src.x+1 && dst.y == src.y) && !(di->visitDir & (1<<7)) )
  196. {
  197. return false;
  198. }
  199. if( (dst.x == src.x+1 && dst.y == src.y+1) && !(di->visitDir & (1<<0)) )
  200. {
  201. return false;
  202. }
  203. if( (dst.x == src.x && dst.y == src.y+1) && !(di->visitDir & (1<<1)) )
  204. {
  205. return false;
  206. }
  207. if( (dst.x == src.x-1 && dst.y == src.y+1) && !(di->visitDir & (1<<2)) )
  208. {
  209. return false;
  210. }
  211. if( (dst.x == src.x-1 && dst.y == src.y) && !(di->visitDir & (1<<3)) )
  212. {
  213. return false;
  214. }
  215. }
  216. return true;
  217. }