CaptureObjectsBehavior.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "../VCAI.h"
  12. #include "../Engine/Nullkiller.h"
  13. #include "../AIhelper.h"
  14. #include "../Goals/Composition.h"
  15. #include "../Goals/ExecuteHeroChain.h"
  16. #include "CaptureObjectsBehavior.h"
  17. #include "../AIUtility.h"
  18. #include "lib/mapping/CMap.h" //for victory conditions
  19. #include "lib/CPathfinder.h"
  20. extern boost::thread_specific_ptr<CCallback> cb;
  21. extern boost::thread_specific_ptr<VCAI> ai;
  22. extern FuzzyHelper * fh;
  23. using namespace Goals;
  24. template <typename T>
  25. bool vectorEquals(const std::vector<T> & v1, const std::vector<T> & v2)
  26. {
  27. return vstd::contains_if(v1, [&](T o) -> bool
  28. {
  29. return vstd::contains(v2, o);
  30. });
  31. }
  32. std::string CaptureObjectsBehavior::toString() const
  33. {
  34. return "Capture objects";
  35. }
  36. bool CaptureObjectsBehavior::operator==(const CaptureObjectsBehavior & other) const
  37. {
  38. if(specificObjects != other.specificObjects)
  39. return false;
  40. if(specificObjects)
  41. return vectorEquals(objectsToCapture, other.objectsToCapture);
  42. return vectorEquals(objectTypes, other.objectTypes)
  43. && vectorEquals(objectSubTypes, other.objectSubTypes);
  44. }
  45. Goals::TGoalVec CaptureObjectsBehavior::decompose() const
  46. {
  47. Goals::TGoalVec tasks;
  48. auto captureObjects = [&](const std::vector<const CGObjectInstance*> & objs) -> void{
  49. if(objs.empty())
  50. {
  51. return;
  52. }
  53. logAi->debug("Scanning objects, count %d", objs.size());
  54. for(auto objToVisit : objs)
  55. {
  56. #if AI_TRACE_LEVEL >= 1
  57. logAi->trace("Checking object %s, %s", objToVisit->getObjectName(), objToVisit->visitablePos().toString());
  58. #endif
  59. if(!shouldVisitObject(objToVisit))
  60. continue;
  61. const int3 pos = objToVisit->visitablePos();
  62. auto paths = ai->ah->getPathsToTile(pos);
  63. std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
  64. std::shared_ptr<ExecuteHeroChain> closestWay;
  65. #if AI_TRACE_LEVEL >= 1
  66. logAi->trace("Found %d paths", paths.size());
  67. #endif
  68. for(auto & path : paths)
  69. {
  70. #if AI_TRACE_LEVEL >= 2
  71. logAi->trace("Path found %s", path.toString());
  72. #endif
  73. if(ai->nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
  74. {
  75. #if AI_TRACE_LEVEL >= 2
  76. logAi->trace("Ignore path. Target hero can be killed by enemy. Our power %lld", path.heroArmy->getArmyStrength());
  77. #endif
  78. continue;
  79. }
  80. if(!shouldVisit(path.targetHero, objToVisit))
  81. continue;
  82. auto hero = path.targetHero;
  83. auto danger = path.getTotalDanger();
  84. if(ai->ah->getHeroRole(hero) == HeroRole::SCOUT && danger == 0 && path.exchangeCount > 1)
  85. continue;
  86. auto firstBlockedAction = path.getFirstBlockedAction();
  87. if(firstBlockedAction)
  88. {
  89. auto subGoal = firstBlockedAction->decompose(path.targetHero);
  90. #if AI_TRACE_LEVEL >= 2
  91. logAi->trace("Decomposing special action %s returns %s", firstBlockedAction->toString(), subGoal->toString());
  92. #endif
  93. if(!subGoal->invalid())
  94. {
  95. Composition composition;
  96. composition.addNext(ExecuteHeroChain(path, objToVisit));
  97. composition.addNext(subGoal);
  98. tasks.push_back(sptr(composition));
  99. }
  100. continue;
  101. }
  102. auto isSafe = isSafeToVisit(hero, path.heroArmy, danger);
  103. #if AI_TRACE_LEVEL >= 2
  104. logAi->trace(
  105. "It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
  106. isSafe ? "safe" : "not safe",
  107. objToVisit->getObjectName(),
  108. hero->name,
  109. path.getHeroStrength(),
  110. danger,
  111. path.getTotalArmyLoss());
  112. #endif
  113. if(isSafe)
  114. {
  115. auto newWay = std::make_shared<ExecuteHeroChain>(path, objToVisit);
  116. waysToVisitObj.push_back(newWay);
  117. if(!closestWay || closestWay->getPath().movementCost() > newWay->getPath().movementCost())
  118. closestWay = newWay;
  119. }
  120. }
  121. if(waysToVisitObj.empty())
  122. continue;
  123. for(auto way : waysToVisitObj)
  124. {
  125. if(ai->nullkiller->arePathHeroesLocked(way->getPath()))
  126. continue;
  127. way->closestWayRatio
  128. = closestWay->getPath().movementCost() / way->getPath().movementCost();
  129. tasks.push_back(sptr(*way));
  130. }
  131. }
  132. };
  133. if(specificObjects)
  134. {
  135. captureObjects(objectsToCapture);
  136. }
  137. else
  138. {
  139. captureObjects(std::vector<const CGObjectInstance*>(ai->visitableObjs.begin(), ai->visitableObjs.end()));
  140. }
  141. return tasks;
  142. }
  143. bool CaptureObjectsBehavior::shouldVisitObject(ObjectIdRef obj) const
  144. {
  145. const CGObjectInstance* objInstance = obj;
  146. if(!objInstance)
  147. return false;
  148. if(objectTypes.size() && !vstd::contains(objectTypes, objInstance->ID.num))
  149. {
  150. return false;
  151. }
  152. if(objectSubTypes.size() && !vstd::contains(objectSubTypes, objInstance->subID))
  153. {
  154. return false;
  155. }
  156. if(isObjectRemovable(obj))
  157. {
  158. return true;
  159. }
  160. const int3 pos = objInstance->visitablePos();
  161. if(objInstance->ID != Obj::CREATURE_GENERATOR1 && vstd::contains(ai->alreadyVisited, objInstance)
  162. || obj->wasVisited(ai->playerID))
  163. {
  164. return false;
  165. }
  166. auto playerRelations = cb->getPlayerRelations(ai->playerID, objInstance->tempOwner);
  167. if(playerRelations != PlayerRelations::ENEMIES && !isWeeklyRevisitable(objInstance))
  168. {
  169. return false;
  170. }
  171. //it may be hero visiting this obj
  172. //we don't try visiting object on which allied or owned hero stands
  173. // -> it will just trigger exchange windows and AI will be confused that obj behind doesn't get visited
  174. const CGObjectInstance * topObj = cb->getTopObj(pos);
  175. if(!topObj)
  176. return false; // partly visible obj but its visitable pos is not visible.
  177. if(topObj->ID == Obj::HERO && cb->getPlayerRelations(ai->playerID, topObj->tempOwner) != PlayerRelations::ENEMIES)
  178. return false;
  179. else
  180. return true; //all of the following is met
  181. }