CaptureObjectsBehavior.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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, Nullkiller * nullkiller, 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(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(nullkiller, 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(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 = nullkiller->heroManager->getHeroRole(path.targetHero);
  112. auto & closestWay = closestWaysByRole[heroRole];
  113. if(!closestWay || closestWay->movementCost() > path.movementCost())
  114. {
  115. closestWay = &path;
  116. }
  117. if(!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 = 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. std::mutex sync;
  140. Nullkiller * nullkiller = ai->nullkiller.get();
  141. auto captureObjects = [&](const std::vector<const CGObjectInstance *> & objs) -> void
  142. {
  143. if(objs.empty())
  144. {
  145. return;
  146. }
  147. logAi->debug("Scanning objects, count %d", objs.size());
  148. tbb::parallel_for(
  149. tbb::blocked_range<size_t>(0, objs.size()),
  150. [this, &objs, &sync, &tasks, nullkiller](const tbb::blocked_range<size_t> & r)
  151. {
  152. std::vector<AIPath> paths;
  153. Goals::TGoalVec tasksLocal;
  154. for(auto i = r.begin(); i != r.end(); i++)
  155. {
  156. auto objToVisit = objs[i];
  157. if(!objectMatchesFilter(objToVisit))
  158. continue;
  159. #if NKAI_TRACE_LEVEL >= 1
  160. logAi->trace("Checking object %s, %s", objToVisit->getObjectName(), objToVisit->visitablePos().toString());
  161. #endif
  162. const int3 pos = objToVisit->visitablePos();
  163. bool useObjectGraph = nullkiller->settings->isObjectGraphAllowed()
  164. && nullkiller->getScanDepth() != ScanDepth::SMALL;
  165. nullkiller->pathfinder->calculatePathInfo(paths, pos, useObjectGraph);
  166. std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
  167. std::shared_ptr<ExecuteHeroChain> closestWay;
  168. #if NKAI_TRACE_LEVEL >= 1
  169. logAi->trace("Found %d paths", paths.size());
  170. #endif
  171. vstd::concatenate(tasksLocal, getVisitGoals(paths, nullkiller, objToVisit));
  172. }
  173. vstd::erase_if(tasksLocal, [](TSubgoal task) -> bool
  174. {
  175. return task->invalid();
  176. });
  177. std::lock_guard<std::mutex> lock(sync);
  178. vstd::concatenate(tasks, tasksLocal);
  179. });
  180. };
  181. if(specificObjects)
  182. {
  183. captureObjects(objectsToCapture);
  184. }
  185. else if(objectTypes.size())
  186. {
  187. captureObjects(
  188. std::vector<const CGObjectInstance *>(
  189. nullkiller->memory->visitableObjs.begin(),
  190. nullkiller->memory->visitableObjs.end()));
  191. }
  192. else
  193. {
  194. captureObjects(nullkiller->objectClusterizer->getNearbyObjects());
  195. if(tasks.empty() || nullkiller->getScanDepth() != ScanDepth::SMALL)
  196. captureObjects(nullkiller->objectClusterizer->getFarObjects());
  197. }
  198. return tasks;
  199. }
  200. bool CaptureObjectsBehavior::objectMatchesFilter(const CGObjectInstance * obj) const
  201. {
  202. if(objectTypes.size() && !vstd::contains(objectTypes, obj->ID.num))
  203. {
  204. return false;
  205. }
  206. if(objectSubTypes.size() && !vstd::contains(objectSubTypes, obj->subID))
  207. {
  208. return false;
  209. }
  210. return true;
  211. }
  212. }