CaptureObjectsBehavior.cpp 4.1 KB

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