CaptureObjectsBehavior.cpp 4.9 KB

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