CaptureObjectsBehavior.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * CaptureObjectsBehavior.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 "../AIGateway.h"
  12. #include "../Engine/Nullkiller.h"
  13. #include "../Goals/Composition.h"
  14. #include "../Goals/ExecuteHeroChain.h"
  15. #include "../Goals/Invalid.h"
  16. #include "CaptureObjectsBehavior.h"
  17. #include "../AIUtility.h"
  18. namespace NKAI
  19. {
  20. using namespace Goals;
  21. template <typename T>
  22. bool vectorEquals(const std::vector<T> & v1, const std::vector<T> & v2)
  23. {
  24. return vstd::contains_if(v1, [&](T o) -> bool
  25. {
  26. return vstd::contains(v2, o);
  27. });
  28. }
  29. std::string CaptureObjectsBehavior::toString() const
  30. {
  31. return "Capture objects";
  32. }
  33. bool CaptureObjectsBehavior::operator==(const CaptureObjectsBehavior & other) const
  34. {
  35. if(specificObjects != other.specificObjects)
  36. return false;
  37. if(specificObjects)
  38. return vectorEquals(objectsToCapture, other.objectsToCapture);
  39. return vectorEquals(objectTypes, other.objectTypes)
  40. && vectorEquals(objectSubTypes, other.objectSubTypes);
  41. }
  42. Goals::TGoalVec CaptureObjectsBehavior::getVisitGoals(const std::vector<AIPath> & paths, const CGObjectInstance * objToVisit)
  43. {
  44. Goals::TGoalVec tasks;
  45. tasks.reserve(paths.size());
  46. std::unordered_map<HeroRole, const AIPath *> closestWaysByRole;
  47. std::vector<ExecuteHeroChain *> waysToVisitObj;
  48. for(auto & path : paths)
  49. {
  50. tasks.push_back(sptr(Goals::Invalid()));
  51. #if NKAI_TRACE_LEVEL >= 2
  52. logAi->trace("Path found %s", path.toString());
  53. #endif
  54. if(ai->nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
  55. {
  56. #if NKAI_TRACE_LEVEL >= 2
  57. logAi->trace("Ignore path. Target hero can be killed by enemy. Our power %lld", path.getHeroStrength());
  58. #endif
  59. continue;
  60. }
  61. if(objToVisit && !shouldVisit(ai->nullkiller.get(), path.targetHero, objToVisit))
  62. {
  63. #if NKAI_TRACE_LEVEL >= 2
  64. logAi->trace("Ignore path. Hero %s should not visit obj %s", path.targetHero->getNameTranslated(), objToVisit->getObjectName());
  65. #endif
  66. continue;
  67. }
  68. auto hero = path.targetHero;
  69. auto danger = path.getTotalDanger();
  70. if(ai->nullkiller->heroManager->getHeroRole(hero) == HeroRole::SCOUT
  71. && (path.getTotalDanger() == 0 || path.turn() > 0)
  72. && path.exchangeCount > 1)
  73. {
  74. #if NKAI_TRACE_LEVEL >= 2
  75. logAi->trace("Ignore path. Hero %s is SCOUT, chain used and no danger", path.targetHero->getNameTranslated());
  76. #endif
  77. continue;
  78. }
  79. auto firstBlockedAction = path.getFirstBlockedAction();
  80. if(firstBlockedAction)
  81. {
  82. auto subGoal = firstBlockedAction->decompose(path.targetHero);
  83. #if NKAI_TRACE_LEVEL >= 2
  84. logAi->trace("Decomposing special action %s returns %s", firstBlockedAction->toString(), subGoal->toString());
  85. #endif
  86. if(!subGoal->invalid())
  87. {
  88. Composition composition;
  89. composition.addNext(ExecuteHeroChain(path, objToVisit));
  90. composition.addNext(subGoal);
  91. tasks[tasks.size() - 1] = sptr(composition);
  92. }
  93. continue;
  94. }
  95. auto isSafe = isSafeToVisit(hero, path.heroArmy, danger);
  96. #if NKAI_TRACE_LEVEL >= 2
  97. logAi->trace(
  98. "It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
  99. isSafe ? "safe" : "not safe",
  100. objToVisit ? objToVisit->getObjectName() : path.targetTile().toString(),
  101. hero->getObjectName(),
  102. path.getHeroStrength(),
  103. danger,
  104. path.getTotalArmyLoss());
  105. #endif
  106. if(isSafe)
  107. {
  108. auto newWay = new ExecuteHeroChain(path, objToVisit);
  109. TSubgoal sharedPtr;
  110. sharedPtr.reset(newWay);
  111. auto heroRole = ai->nullkiller->heroManager->getHeroRole(path.targetHero);
  112. auto & closestWay = closestWaysByRole[heroRole];
  113. if(!closestWay || closestWay->movementCost() > path.movementCost())
  114. {
  115. closestWay = &path;
  116. }
  117. if(!ai->nullkiller->arePathHeroesLocked(path))
  118. {
  119. waysToVisitObj.push_back(newWay);
  120. tasks[tasks.size() - 1] = sharedPtr;
  121. }
  122. }
  123. }
  124. for(auto way : waysToVisitObj)
  125. {
  126. auto heroRole = ai->nullkiller->heroManager->getHeroRole(way->getPath().targetHero);
  127. auto closestWay = closestWaysByRole[heroRole];
  128. if(closestWay)
  129. {
  130. way->closestWayRatio
  131. = closestWay->movementCost() / way->getPath().movementCost();
  132. }
  133. }
  134. return tasks;
  135. }
  136. Goals::TGoalVec CaptureObjectsBehavior::decompose() const
  137. {
  138. Goals::TGoalVec tasks;
  139. auto captureObjects = [&](const std::vector<const CGObjectInstance*> & objs) -> void
  140. {
  141. if(objs.empty())
  142. {
  143. return;
  144. }
  145. logAi->debug("Scanning objects, count %d", objs.size());
  146. for(auto objToVisit : objs)
  147. {
  148. if(!objectMatchesFilter(objToVisit))
  149. continue;
  150. #if NKAI_TRACE_LEVEL >= 1
  151. logAi->trace("Checking object %s, %s", objToVisit->getObjectName(), objToVisit->visitablePos().toString());
  152. #endif
  153. const int3 pos = objToVisit->visitablePos();
  154. bool useObjectGraph = ai->nullkiller->settings->isObjectGraphAllowed()
  155. && ai->nullkiller->getScanDepth() != ScanDepth::SMALL;
  156. auto paths = ai->nullkiller->pathfinder->getPathInfo(pos, useObjectGraph);
  157. std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
  158. std::shared_ptr<ExecuteHeroChain> closestWay;
  159. #if NKAI_TRACE_LEVEL >= 1
  160. logAi->trace("Found %d paths", paths.size());
  161. #endif
  162. vstd::concatenate(tasks, getVisitGoals(paths, objToVisit));
  163. }
  164. vstd::erase_if(tasks, [](TSubgoal task) -> bool
  165. {
  166. return task->invalid();
  167. });
  168. };
  169. if(specificObjects)
  170. {
  171. captureObjects(objectsToCapture);
  172. }
  173. else if(objectTypes.size())
  174. {
  175. captureObjects(
  176. std::vector<const CGObjectInstance *>(
  177. ai->nullkiller->memory->visitableObjs.begin(),
  178. ai->nullkiller->memory->visitableObjs.end()));
  179. }
  180. else
  181. {
  182. captureObjects(ai->nullkiller->objectClusterizer->getNearbyObjects());
  183. if(tasks.empty() || ai->nullkiller->getScanDepth() != ScanDepth::SMALL)
  184. captureObjects(ai->nullkiller->objectClusterizer->getFarObjects());
  185. }
  186. return tasks;
  187. }
  188. bool CaptureObjectsBehavior::objectMatchesFilter(const CGObjectInstance * obj) const
  189. {
  190. if(objectTypes.size() && !vstd::contains(objectTypes, obj->ID.num))
  191. {
  192. return false;
  193. }
  194. if(objectSubTypes.size() && !vstd::contains(objectSubTypes, obj->subID))
  195. {
  196. return false;
  197. }
  198. return true;
  199. }
  200. }