PathfindingManager.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * PathfindingManager.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "PathfindingManager.h"
  12. #include "AIPathfinder.h"
  13. #include "AIPathfinderConfig.h"
  14. #include "Goals/Goals.h"
  15. #include "../../../lib/CGameInfoCallback.h"
  16. #include "../../../lib/mapping/CMap.h"
  17. PathfindingManager::PathfindingManager(CPlayerSpecificInfoCallback * CB, VCAI * AI)
  18. : ai(AI), cb(CB)
  19. {
  20. }
  21. void PathfindingManager::init(CPlayerSpecificInfoCallback * CB)
  22. {
  23. cb = CB;
  24. pathfinder.reset(new AIPathfinder(cb, ai));
  25. }
  26. void PathfindingManager::setAI(VCAI * AI)
  27. {
  28. ai = AI;
  29. }
  30. Goals::TGoalVec PathfindingManager::howToVisitTile(int3 tile)
  31. {
  32. Goals::TGoalVec result;
  33. auto heroes = cb->getHeroesInfo();
  34. for(auto hero : heroes)
  35. {
  36. vstd::concatenate(result, howToVisitTile(hero, tile));
  37. }
  38. return result;
  39. }
  40. Goals::TGoalVec PathfindingManager::howToVisitObj(ObjectIdRef obj)
  41. {
  42. Goals::TGoalVec result;
  43. auto heroes = cb->getHeroesInfo();
  44. for(auto hero : heroes)
  45. {
  46. vstd::concatenate(result, howToVisitObj(hero, obj));
  47. }
  48. return result;
  49. }
  50. Goals::TGoalVec PathfindingManager::howToVisitTile(HeroPtr hero, int3 tile, bool allowGatherArmy)
  51. {
  52. auto result = findPath(hero, tile, allowGatherArmy, [&](int3 firstTileToGet) -> Goals::TSubgoal
  53. {
  54. return sptr(Goals::VisitTile(firstTileToGet).sethero(hero).setisAbstract(true));
  55. });
  56. for(Goals::TSubgoal solution : result)
  57. {
  58. solution->setparent(sptr(Goals::VisitTile(tile).sethero(hero).setevaluationContext(solution->evaluationContext)));
  59. }
  60. return result;
  61. }
  62. Goals::TGoalVec PathfindingManager::howToVisitObj(HeroPtr hero, ObjectIdRef obj, bool allowGatherArmy)
  63. {
  64. if(!obj)
  65. {
  66. return Goals::TGoalVec();
  67. }
  68. int3 dest = obj->visitablePos();
  69. auto result = findPath(hero, dest, allowGatherArmy, [&](int3 firstTileToGet) -> Goals::TSubgoal
  70. {
  71. if(obj->ID.num == Obj::HERO && obj->getOwner() == hero->getOwner())
  72. return sptr(Goals::VisitHero(obj->id.getNum()).sethero(hero).setisAbstract(true));
  73. else
  74. return sptr(Goals::VisitObj(obj->id.getNum()).sethero(hero).setisAbstract(true));
  75. });
  76. for(Goals::TSubgoal solution : result)
  77. {
  78. solution->setparent(sptr(Goals::VisitObj(obj->id.getNum()).sethero(hero).setevaluationContext(solution->evaluationContext)));
  79. }
  80. return result;
  81. }
  82. std::vector<AIPath> PathfindingManager::getPathsToTile(HeroPtr hero, int3 tile)
  83. {
  84. return pathfinder->getPathInfo(hero, tile);
  85. }
  86. Goals::TGoalVec PathfindingManager::findPath(
  87. HeroPtr hero,
  88. crint3 dest,
  89. bool allowGatherArmy,
  90. const std::function<Goals::TSubgoal(int3)> doVisitTile)
  91. {
  92. Goals::TGoalVec result;
  93. boost::optional<uint64_t> armyValueRequired;
  94. uint64_t danger;
  95. std::vector<AIPath> chainInfo = pathfinder->getPathInfo(hero, dest);
  96. logAi->trace("Trying to find a way for %s to visit tile %s", hero->name, dest.toString());
  97. for(auto path : chainInfo)
  98. {
  99. int3 firstTileToGet = path.firstTileToGet();
  100. logAi->trace("Path found size=%i, first tile=%s", path.nodes.size(), firstTileToGet.toString());
  101. if(firstTileToGet.valid() && ai->isTileNotReserved(hero.get(), firstTileToGet))
  102. {
  103. danger = path.getTotalDanger(hero);
  104. if(isSafeToVisit(hero, danger))
  105. {
  106. logAi->trace("It's safe for %s to visit tile %s with danger %s", hero->name, dest.toString(), std::to_string(danger));
  107. Goals::TSubgoal solution;
  108. if(path.specialAction)
  109. {
  110. solution = path.specialAction->whatToDo(hero);
  111. }
  112. else
  113. {
  114. solution = dest == firstTileToGet
  115. ? doVisitTile(firstTileToGet)
  116. : clearWayTo(hero, firstTileToGet);
  117. }
  118. if(solution->invalid())
  119. continue;
  120. if(solution->evaluationContext.danger < danger)
  121. solution->evaluationContext.danger = danger;
  122. solution->evaluationContext.movementCost += path.movementCost();
  123. result.push_back(solution);
  124. continue;
  125. }
  126. if(!armyValueRequired || armyValueRequired > danger)
  127. {
  128. armyValueRequired = boost::make_optional(danger);
  129. }
  130. }
  131. }
  132. danger = armyValueRequired.get_value_or(0);
  133. if(allowGatherArmy && danger > 0)
  134. {
  135. //we need to get army in order to conquer that place
  136. logAi->trace("Gather army for %s, value=%s", hero->name, std::to_string(danger));
  137. result.push_back(sptr(Goals::GatherArmy(danger * SAFE_ATTACK_CONSTANT).sethero(hero).setisAbstract(true)));
  138. }
  139. return result;
  140. }
  141. Goals::TSubgoal PathfindingManager::clearWayTo(HeroPtr hero, int3 firstTileToGet)
  142. {
  143. if(isBlockedBorderGate(firstTileToGet))
  144. {
  145. //FIXME: this way we'll not visit gate and activate quest :?
  146. return sptr(Goals::FindObj(Obj::KEYMASTER, cb->getTile(firstTileToGet)->visitableObjects.back()->subID));
  147. }
  148. auto topObj = cb->getTopObj(firstTileToGet);
  149. if(topObj)
  150. {
  151. if(vstd::contains(ai->reservedObjs, topObj) && !vstd::contains(ai->reservedHeroesMap[hero], topObj))
  152. {
  153. return sptr(Goals::Invalid());
  154. }
  155. if(topObj->ID == Obj::HERO && cb->getPlayerRelations(hero->tempOwner, topObj->tempOwner) != PlayerRelations::ENEMIES)
  156. {
  157. if(topObj != hero.get(true)) //the hero we want to free
  158. {
  159. logAi->error("%s stands in the way of %s", topObj->getObjectName(), hero->getObjectName());
  160. return sptr(Goals::Invalid());
  161. }
  162. }
  163. if(topObj->ID == Obj::QUEST_GUARD || topObj->ID == Obj::BORDERGUARD)
  164. {
  165. if(shouldVisit(hero, topObj))
  166. {
  167. //do NOT use VISIT_TILE, as tile with quets guard can't be visited
  168. return sptr(Goals::VisitObj(topObj->id.getNum()).sethero(hero));
  169. }
  170. //TODO: we should be able to return apriopriate quest here
  171. //ret.push_back(ai->questToGoal());
  172. //however, visiting obj for firts time will give us quest
  173. //do not access quets guard if we can't complete the quest
  174. logAi->trace("Can not visit this quest guard! Not ready!");
  175. return sptr(Goals::Invalid());
  176. }
  177. }
  178. return sptr(Goals::VisitTile(firstTileToGet).sethero(hero).setisAbstract(true));
  179. }
  180. void PathfindingManager::resetPaths()
  181. {
  182. logAi->debug("AIPathfinder has been reseted.");
  183. pathfinder->clear();
  184. }