2
0

PathfindingManager.cpp 5.8 KB

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