CaptureObjectsBehavior.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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->trace("Scanning objects, count %d", objs.size());
  36. for(auto objToVisit : objs)
  37. {
  38. #ifdef VCMI_TRACE_PATHFINDER
  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 VCMI_TRACE_PATHFINDER
  48. logAi->trace("Found %d paths", paths.size());
  49. #endif
  50. for(auto & path : paths)
  51. {
  52. #ifdef VCMI_TRACE_PATHFINDER
  53. logAi->trace("Path found %s", path.toString());
  54. #endif
  55. if(ai->nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
  56. {
  57. #ifdef VCMI_TRACE_PATHFINDER
  58. logAi->trace("Ignore path. Target hero can be killed by enemy");
  59. #endif
  60. continue;
  61. }
  62. if(!shouldVisit(path.targetHero, objToVisit))
  63. continue;
  64. auto hero = path.targetHero;
  65. auto danger = path.getTotalDanger(hero);
  66. auto isSafe = isSafeToVisit(hero, path.heroArmy, danger);
  67. #ifdef VCMI_TRACE_PATHFINDER
  68. logAi->trace(
  69. "It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
  70. isSafe ? "safe" : "not safe",
  71. objToVisit->instanceName,
  72. hero->name,
  73. path.getHeroStrength(),
  74. danger,
  75. path.armyLoss);
  76. #endif
  77. if(isSafe)
  78. {
  79. auto newWay = std::make_shared<ExecuteHeroChain>(path, objToVisit);
  80. waysToVisitObj.push_back(newWay);
  81. if(!closestWay || closestWay->evaluationContext.movementCost > newWay->evaluationContext.movementCost)
  82. closestWay = newWay;
  83. }
  84. }
  85. if(waysToVisitObj.empty())
  86. continue;
  87. for(auto way : waysToVisitObj)
  88. {
  89. way->evaluationContext.closestWayRatio
  90. = way->evaluationContext.movementCost / closestWay->evaluationContext.movementCost;
  91. tasks.push_back(sptr(*way));
  92. }
  93. }
  94. };
  95. if(specificObjects)
  96. {
  97. captureObjects(objectsToCapture);
  98. }
  99. else
  100. {
  101. captureObjects(std::vector<const CGObjectInstance*>(ai->visitableObjs.begin(), ai->visitableObjs.end()));
  102. }
  103. return tasks;
  104. }
  105. bool CaptureObjectsBehavior::shouldVisitObject(ObjectIdRef obj) const
  106. {
  107. const CGObjectInstance* objInstance = obj;
  108. if(!objInstance)
  109. return false;
  110. if(objectTypes.size() && !vstd::contains(objectTypes, objInstance->ID.num))
  111. {
  112. return false;
  113. }
  114. if(objectSubTypes.size() && !vstd::contains(objectSubTypes, objInstance->subID))
  115. {
  116. return false;
  117. }
  118. const int3 pos = objInstance->visitablePos();
  119. if(vstd::contains(ai->alreadyVisited, objInstance))
  120. {
  121. return false;
  122. }
  123. auto playerRelations = cb->getPlayerRelations(ai->playerID, objInstance->tempOwner);
  124. if(playerRelations != PlayerRelations::ENEMIES && !isWeeklyRevisitable(objInstance))
  125. {
  126. return false;
  127. }
  128. //it may be hero visiting this obj
  129. //we don't try visiting object on which allied or owned hero stands
  130. // -> it will just trigger exchange windows and AI will be confused that obj behind doesn't get visited
  131. const CGObjectInstance * topObj = cb->getTopObj(pos);
  132. if(!topObj)
  133. return false; // partly visible obj but its visitable pos is not visible.
  134. if(topObj->ID == Obj::HERO && cb->getPlayerRelations(ai->playerID, topObj->tempOwner) != PlayerRelations::ENEMIES)
  135. return false;
  136. else
  137. return true; //all of the following is met
  138. }