PathfindingManager.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. pathfinder->init();
  26. }
  27. void PathfindingManager::setAI(VCAI * AI)
  28. {
  29. ai = AI;
  30. }
  31. Goals::TGoalVec PathfindingManager::howToVisitTile(const int3 & tile, bool allowGatherArmy) const
  32. {
  33. Goals::TGoalVec result;
  34. auto heroes = cb->getHeroesInfo();
  35. result.reserve(heroes.size());
  36. for(auto hero : heroes)
  37. {
  38. vstd::concatenate(result, howToVisitTile(hero, tile, allowGatherArmy));
  39. }
  40. return result;
  41. }
  42. Goals::TGoalVec PathfindingManager::howToVisitObj(ObjectIdRef obj, bool allowGatherArmy) const
  43. {
  44. Goals::TGoalVec result;
  45. auto heroes = cb->getHeroesInfo();
  46. result.reserve(heroes.size());
  47. for(auto hero : heroes)
  48. {
  49. vstd::concatenate(result, howToVisitObj(hero, obj, allowGatherArmy));
  50. }
  51. return result;
  52. }
  53. Goals::TGoalVec PathfindingManager::howToVisitTile(const HeroPtr & hero, const int3 & tile, bool allowGatherArmy) const
  54. {
  55. auto result = findPaths(tile, allowGatherArmy, hero, [&](int3 firstTileToGet) -> Goals::TSubgoal
  56. {
  57. return sptr(Goals::VisitTile(firstTileToGet).sethero(hero).setisAbstract(true));
  58. });
  59. for(Goals::TSubgoal solution : result)
  60. {
  61. solution->setparent(sptr(Goals::VisitTile(tile).sethero(hero).setevaluationContext(solution->evaluationContext)));
  62. }
  63. return result;
  64. }
  65. Goals::TGoalVec PathfindingManager::howToVisitObj(const HeroPtr & hero, ObjectIdRef obj, bool allowGatherArmy) const
  66. {
  67. if(!obj)
  68. {
  69. return Goals::TGoalVec();
  70. }
  71. int3 dest = obj->visitablePos();
  72. auto result = findPaths(dest, allowGatherArmy, hero, [&](int3 firstTileToGet) -> Goals::TSubgoal
  73. {
  74. if(obj->ID.num == Obj::HERO && obj->getOwner() == hero->getOwner())
  75. return sptr(Goals::VisitHero(obj->id.getNum()).sethero(hero).setisAbstract(true));
  76. else
  77. return sptr(Goals::VisitObj(obj->id.getNum()).sethero(hero).setisAbstract(true));
  78. });
  79. for(Goals::TSubgoal solution : result)
  80. {
  81. solution->setparent(sptr(Goals::VisitObj(obj->id.getNum()).sethero(hero).setevaluationContext(solution->evaluationContext)));
  82. }
  83. return result;
  84. }
  85. std::vector<AIPath> PathfindingManager::getPathsToTile(const HeroPtr & hero, const int3 & tile) const
  86. {
  87. auto paths = pathfinder->getPathInfo(tile);
  88. vstd::erase_if(paths, [&](AIPath & path) -> bool{
  89. return path.targetHero != hero.h;
  90. });
  91. return paths;
  92. }
  93. std::vector<AIPath> PathfindingManager::getPathsToTile(const int3 & tile) const
  94. {
  95. return pathfinder->getPathInfo(tile);
  96. }
  97. Goals::TGoalVec PathfindingManager::findPaths(
  98. crint3 dest,
  99. bool allowGatherArmy,
  100. HeroPtr hero,
  101. const std::function<Goals::TSubgoal(int3)> doVisitTile) const
  102. {
  103. Goals::TGoalVec result;
  104. boost::optional<uint64_t> armyValueRequired;
  105. uint64_t danger;
  106. std::vector<AIPath> chainInfo = pathfinder->getPathInfo(dest);
  107. #ifdef VCMI_TRACE_PATHFINDER
  108. logAi->trace("Trying to find a way for %s to visit tile %s", hero->name, dest.toString());
  109. #endif
  110. for(auto path : chainInfo)
  111. {
  112. if((hero && hero.get() != path.targetHero) || path.nodes.empty())
  113. continue;
  114. const AIPathNodeInfo & firstNode = path.firstNode();
  115. int3 firstTileToGet = firstNode.coord;
  116. #ifdef VCMI_TRACE_PATHFINDER
  117. std::stringstream str;
  118. str << "Path found ";
  119. for(auto node : path.nodes)
  120. str << node.targetHero->name << "->" << node.coord.toString() << "; ";
  121. logAi->trace(str.str());
  122. #endif
  123. if(ai->isTileNotReserved(hero.get(), firstTileToGet))
  124. {
  125. danger = path.getTotalDanger(hero);
  126. if(isSafeToVisit(hero, path.heroArmy, danger))
  127. {
  128. Goals::TSubgoal solution;
  129. if(path.specialAction)
  130. {
  131. solution = path.specialAction->whatToDo(hero);
  132. }
  133. else
  134. {
  135. solution = dest == firstTileToGet
  136. ? doVisitTile(firstTileToGet)
  137. : clearWayTo(firstNode.targetHero, firstTileToGet);
  138. }
  139. if(solution->invalid())
  140. continue;
  141. if(solution->evaluationContext.danger < danger)
  142. solution->evaluationContext.danger = danger;
  143. solution->evaluationContext.movementCost += path.movementCost();
  144. solution->evaluationContext.armyLoss += path.armyLoss;
  145. solution->evaluationContext.heroStrength = path.getHeroStrength();
  146. #ifdef VCMI_TRACE_PATHFINDER
  147. logAi->trace("It's safe for %s to visit tile %s with danger %s, loss %s, army strength %s, goal %s",
  148. hero->name,
  149. dest.toString(),
  150. std::to_string(danger),
  151. std::to_string(path.armyLoss),
  152. std::to_string(path.heroArmy->getArmyStrength()),
  153. solution->name());
  154. #endif
  155. result.push_back(solution);
  156. continue;
  157. }
  158. if(!armyValueRequired || armyValueRequired > danger)
  159. {
  160. armyValueRequired = boost::make_optional(danger);
  161. }
  162. }
  163. }
  164. danger = armyValueRequired.get_value_or(0);
  165. if(allowGatherArmy && danger > 0)
  166. {
  167. //we need to get army in order to conquer that place
  168. #ifdef VCMI_TRACE_PATHFINDER
  169. logAi->trace("Gather army for %s, value=%s", hero->name, std::to_string(danger));
  170. #endif
  171. result.push_back(sptr(Goals::GatherArmy(danger * SAFE_ATTACK_CONSTANT).sethero(hero).setisAbstract(true)));
  172. }
  173. return result;
  174. }
  175. Goals::TSubgoal PathfindingManager::clearWayTo(HeroPtr hero, int3 firstTileToGet) const
  176. {
  177. if(isBlockedBorderGate(firstTileToGet))
  178. {
  179. //FIXME: this way we'll not visit gate and activate quest :?
  180. return sptr(Goals::FindObj(Obj::KEYMASTER, cb->getTile(firstTileToGet)->visitableObjects.back()->subID));
  181. }
  182. auto topObj = cb->getTopObj(firstTileToGet);
  183. if(topObj)
  184. {
  185. if(vstd::contains(ai->reservedObjs, topObj) && !vstd::contains(ai->reservedHeroesMap[hero], topObj))
  186. {
  187. return sptr(Goals::Invalid());
  188. }
  189. if(topObj->ID == Obj::HERO && cb->getPlayerRelations(hero->tempOwner, topObj->tempOwner) != PlayerRelations::ENEMIES)
  190. {
  191. if(topObj != hero.get(true)) //the hero we want to free
  192. {
  193. logAi->warn("%s stands in the way of %s", topObj->getObjectName(), hero->getObjectName());
  194. }
  195. }
  196. if(topObj->ID == Obj::QUEST_GUARD || topObj->ID == Obj::BORDERGUARD)
  197. {
  198. if(shouldVisit(hero, topObj))
  199. {
  200. //do NOT use VISIT_TILE, as tile with quets guard can't be visited
  201. return sptr(Goals::VisitObj(topObj->id.getNum()).sethero(hero));
  202. }
  203. auto questObj = dynamic_cast<const IQuestObject*>(topObj);
  204. if(questObj)
  205. {
  206. auto questInfo = QuestInfo(questObj->quest, topObj, topObj->visitablePos());
  207. return sptr(Goals::CompleteQuest(questInfo));
  208. }
  209. return sptr(Goals::Invalid());
  210. }
  211. }
  212. return sptr(Goals::VisitTile(firstTileToGet).sethero(hero).setisAbstract(true));
  213. }
  214. void PathfindingManager::updatePaths(std::vector<HeroPtr> heroes, bool useHeroChain)
  215. {
  216. logAi->debug("AIPathfinder has been reseted.");
  217. pathfinder->updatePaths(heroes, useHeroChain);
  218. }