PathfindingManager.cpp 6.3 KB

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