PathfindingManager.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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(int3 tile)
  32. {
  33. Goals::TGoalVec result;
  34. auto heroes = cb->getHeroesInfo();
  35. for(auto hero : heroes)
  36. {
  37. vstd::concatenate(result, howToVisitTile(hero, tile));
  38. }
  39. return result;
  40. }
  41. Goals::TGoalVec PathfindingManager::howToVisitObj(ObjectIdRef obj)
  42. {
  43. Goals::TGoalVec result;
  44. auto heroes = cb->getHeroesInfo();
  45. for(auto hero : heroes)
  46. {
  47. vstd::concatenate(result, howToVisitObj(hero, obj));
  48. }
  49. return result;
  50. }
  51. Goals::TGoalVec PathfindingManager::howToVisitTile(HeroPtr hero, int3 tile, bool allowGatherArmy)
  52. {
  53. auto result = findPath(hero, tile, allowGatherArmy, [&](int3 firstTileToGet) -> Goals::TSubgoal
  54. {
  55. return sptr(Goals::VisitTile(firstTileToGet).sethero(hero).setisAbstract(true));
  56. });
  57. for(Goals::TSubgoal solution : result)
  58. {
  59. solution->setparent(sptr(Goals::VisitTile(tile).sethero(hero).setevaluationContext(solution->evaluationContext)));
  60. }
  61. return result;
  62. }
  63. Goals::TGoalVec PathfindingManager::howToVisitObj(HeroPtr hero, ObjectIdRef obj, bool allowGatherArmy)
  64. {
  65. if(!obj)
  66. {
  67. return Goals::TGoalVec();
  68. }
  69. int3 dest = obj->visitablePos();
  70. auto result = findPath(hero, dest, allowGatherArmy, [&](int3 firstTileToGet) -> Goals::TSubgoal
  71. {
  72. if(obj->ID.num == Obj::HERO && obj->getOwner() == hero->getOwner())
  73. return sptr(Goals::VisitHero(obj->id.getNum()).sethero(hero).setisAbstract(true));
  74. else
  75. return sptr(Goals::VisitObj(obj->id.getNum()).sethero(hero).setisAbstract(true));
  76. });
  77. for(Goals::TSubgoal solution : result)
  78. {
  79. solution->setparent(sptr(Goals::VisitObj(obj->id.getNum()).sethero(hero).setevaluationContext(solution->evaluationContext)));
  80. }
  81. return result;
  82. }
  83. std::vector<AIPath> PathfindingManager::getPathsToTile(HeroPtr hero, int3 tile)
  84. {
  85. return pathfinder->getPathInfo(hero, tile);
  86. }
  87. Goals::TGoalVec PathfindingManager::findPath(
  88. HeroPtr hero,
  89. crint3 dest,
  90. bool allowGatherArmy,
  91. const std::function<Goals::TSubgoal(int3)> doVisitTile)
  92. {
  93. Goals::TGoalVec result;
  94. boost::optional<uint64_t> armyValueRequired;
  95. uint64_t danger;
  96. std::vector<AIPath> chainInfo = pathfinder->getPathInfo(hero, dest);
  97. logAi->trace("Trying to find a way for %s to visit tile %s", hero->name, dest.toString());
  98. for(auto path : chainInfo)
  99. {
  100. int3 firstTileToGet = path.firstTileToGet();
  101. logAi->trace("Path found size=%i, first tile=%s", path.nodes.size(), firstTileToGet.toString());
  102. if(firstTileToGet.valid() && ai->isTileNotReserved(hero.get(), firstTileToGet))
  103. {
  104. danger = path.getTotalDanger(hero);
  105. if(isSafeToVisit(hero, danger))
  106. {
  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. 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());
  124. result.push_back(solution);
  125. continue;
  126. }
  127. if(!armyValueRequired || armyValueRequired > danger)
  128. {
  129. armyValueRequired = boost::make_optional(danger);
  130. }
  131. }
  132. }
  133. danger = armyValueRequired.get_value_or(0);
  134. if(allowGatherArmy && danger > 0)
  135. {
  136. //we need to get army in order to conquer that place
  137. logAi->trace("Gather army for %s, value=%s", hero->name, std::to_string(danger));
  138. result.push_back(sptr(Goals::GatherArmy(danger * SAFE_ATTACK_CONSTANT).sethero(hero).setisAbstract(true)));
  139. }
  140. return result;
  141. }
  142. Goals::TSubgoal PathfindingManager::clearWayTo(HeroPtr hero, int3 firstTileToGet)
  143. {
  144. if(isBlockedBorderGate(firstTileToGet))
  145. {
  146. //FIXME: this way we'll not visit gate and activate quest :?
  147. return sptr(Goals::FindObj(Obj::KEYMASTER, cb->getTile(firstTileToGet)->visitableObjects.back()->subID));
  148. }
  149. auto topObj = cb->getTopObj(firstTileToGet);
  150. if(topObj)
  151. {
  152. if(vstd::contains(ai->reservedObjs, topObj) && !vstd::contains(ai->reservedHeroesMap[hero], topObj))
  153. {
  154. return sptr(Goals::Invalid());
  155. }
  156. if(topObj->ID == Obj::HERO && cb->getPlayerRelations(hero->tempOwner, topObj->tempOwner) != PlayerRelations::ENEMIES)
  157. {
  158. if(topObj != hero.get(true)) //the hero we want to free
  159. {
  160. logAi->error("%s stands in the way of %s", topObj->getObjectName(), hero->getObjectName());
  161. return sptr(Goals::Invalid());
  162. }
  163. }
  164. if(topObj->ID == Obj::QUEST_GUARD || topObj->ID == Obj::BORDERGUARD)
  165. {
  166. if(shouldVisit(hero, topObj))
  167. {
  168. //do NOT use VISIT_TILE, as tile with quets guard can't be visited
  169. return sptr(Goals::VisitObj(topObj->id.getNum()).sethero(hero));
  170. }
  171. auto questObj = dynamic_cast<const IQuestObject*>(topObj);
  172. if(questObj)
  173. {
  174. auto questInfo = QuestInfo(questObj->quest, topObj, topObj->visitablePos());
  175. return sptr(Goals::CompleteQuest(questInfo));
  176. }
  177. return sptr(Goals::Invalid());
  178. }
  179. }
  180. return sptr(Goals::VisitTile(firstTileToGet).sethero(hero).setisAbstract(true));
  181. }
  182. void PathfindingManager::resetPaths()
  183. {
  184. logAi->debug("AIPathfinder has been reseted.");
  185. pathfinder->clear();
  186. }