PathfindingManager.cpp 6.3 KB

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