CPathfindingManager.cpp 5.7 KB

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