PathfindingManager.cpp 6.3 KB

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