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(hero);
  83. if(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. way->evaluationContext.closestWayRatio
  109. = way->evaluationContext.movementCost / closestWay->evaluationContext.movementCost;
  110. if(way->hero && ai->nullkiller->canMove(way->hero.h))
  111. {
  112. tasks.push_back(sptr(*way));
  113. }
  114. }
  115. }
  116. };
  117. if(specificObjects)
  118. {
  119. captureObjects(objectsToCapture);
  120. }
  121. else
  122. {
  123. captureObjects(std::vector<const CGObjectInstance*>(ai->visitableObjs.begin(), ai->visitableObjs.end()));
  124. }
  125. return tasks;
  126. }
  127. bool CaptureObjectsBehavior::shouldVisitObject(ObjectIdRef obj) const
  128. {
  129. const CGObjectInstance* objInstance = obj;
  130. if(!objInstance)
  131. return false;
  132. if(objectTypes.size() && !vstd::contains(objectTypes, objInstance->ID.num))
  133. {
  134. return false;
  135. }
  136. if(objectSubTypes.size() && !vstd::contains(objectSubTypes, objInstance->subID))
  137. {
  138. return false;
  139. }
  140. const int3 pos = objInstance->visitablePos();
  141. if(objInstance->ID != Obj::CREATURE_GENERATOR1 && vstd::contains(ai->alreadyVisited, objInstance)
  142. || obj->wasVisited(ai->playerID))
  143. {
  144. return false;
  145. }
  146. auto playerRelations = cb->getPlayerRelations(ai->playerID, objInstance->tempOwner);
  147. if(playerRelations != PlayerRelations::ENEMIES && !isWeeklyRevisitable(objInstance))
  148. {
  149. return false;
  150. }
  151. //it may be hero visiting this obj
  152. //we don't try visiting object on which allied or owned hero stands
  153. // -> it will just trigger exchange windows and AI will be confused that obj behind doesn't get visited
  154. const CGObjectInstance * topObj = cb->getTopObj(pos);
  155. if(!topObj)
  156. return false; // partly visible obj but its visitable pos is not visible.
  157. if(topObj->ID == Obj::HERO && cb->getPlayerRelations(ai->playerID, topObj->tempOwner) != PlayerRelations::ENEMIES)
  158. return false;
  159. else
  160. return true; //all of the following is met
  161. }