CaptureObjectsBehavior.cpp 4.8 KB

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