CaptureObjectsBehavior.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "../AIhelper.h"
  13. #include "../Goals/ExecuteHeroChain.h"
  14. #include "CaptureObjectsBehavior.h"
  15. #include "../AIUtility.h"
  16. #include "lib/mapping/CMap.h" //for victory conditions
  17. #include "lib/CPathfinder.h"
  18. extern boost::thread_specific_ptr<CCallback> cb;
  19. extern boost::thread_specific_ptr<VCAI> ai;
  20. extern FuzzyHelper * fh;
  21. using namespace Goals;
  22. std::string CaptureObjectsBehavior::toString() const
  23. {
  24. return "Capture objects";
  25. }
  26. Goals::TGoalVec CaptureObjectsBehavior::getTasks()
  27. {
  28. Goals::TGoalVec tasks;
  29. auto captureObjects = [&](const std::vector<const CGObjectInstance*> & objs) -> void{
  30. if(objs.empty())
  31. {
  32. return;
  33. }
  34. for(auto objToVisit : objs)
  35. {
  36. #ifdef VCMI_TRACE_PATHFINDER
  37. logAi->trace("Checking object %s, %s", objToVisit->getObjectName(), objToVisit->visitablePos().toString());
  38. #endif
  39. if(!shouldVisitObject(objToVisit))
  40. continue;
  41. const int3 pos = objToVisit->visitablePos();
  42. auto paths = ai->ah->getPathsToTile(pos);
  43. std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
  44. std::shared_ptr<ExecuteHeroChain> closestWay;
  45. #ifdef VCMI_TRACE_PATHFINDER
  46. logAi->trace("Found %" PRId32 "paths", paths.size());
  47. #endif
  48. for(auto & path : paths)
  49. {
  50. #ifdef VCMI_TRACE_PATHFINDER
  51. logAi->trace("Path found %s", path.toString());
  52. #endif
  53. if(!shouldVisit(path.targetHero, objToVisit))
  54. continue;
  55. auto hero = path.targetHero;
  56. auto danger = path.getTotalDanger(hero);
  57. auto isSafe = isSafeToVisit(hero, path.heroArmy, danger);
  58. #ifdef VCMI_TRACE_PATHFINDER
  59. logAi->trace(
  60. "It is %s to visit %s by %s with army %" PRId64 ", danger %" PRId64 " and army loss %" PRId64,
  61. isSafe ? "safe" : "not safe",
  62. objToVisit->instanceName,
  63. hero->name,
  64. path.getHeroStrength(),
  65. danger,
  66. path.armyLoss);
  67. #endif
  68. if(isSafe)
  69. {
  70. auto newWay = std::make_shared<ExecuteHeroChain>(path, objToVisit);
  71. waysToVisitObj.push_back(newWay);
  72. if(!closestWay || closestWay->evaluationContext.movementCost > newWay->evaluationContext.movementCost)
  73. closestWay = newWay;
  74. }
  75. }
  76. if(waysToVisitObj.empty())
  77. continue;
  78. for(auto way : waysToVisitObj)
  79. {
  80. way->evaluationContext.closestWayRatio
  81. = way->evaluationContext.movementCost / closestWay->evaluationContext.movementCost;
  82. tasks.push_back(sptr(*way));
  83. }
  84. }
  85. };
  86. if(specificObjects)
  87. {
  88. captureObjects(objectsToCapture);
  89. }
  90. else
  91. {
  92. captureObjects(std::vector<const CGObjectInstance*>(ai->visitableObjs.begin(), ai->visitableObjs.end()));
  93. }
  94. return tasks;
  95. }
  96. bool CaptureObjectsBehavior::shouldVisitObject(ObjectIdRef obj) const
  97. {
  98. const CGObjectInstance* objInstance = obj;
  99. if(!objInstance || objectTypes.size() && !vstd::contains(objectTypes, objInstance->ID.num))
  100. {
  101. return false;
  102. }
  103. if(!objInstance || objectSubTypes.size() && !vstd::contains(objectSubTypes, objInstance->subID))
  104. {
  105. return false;
  106. }
  107. const int3 pos = objInstance->visitablePos();
  108. if(vstd::contains(ai->alreadyVisited, objInstance))
  109. {
  110. return false;
  111. }
  112. auto playerRelations = cb->getPlayerRelations(ai->playerID, objInstance->tempOwner);
  113. if(playerRelations != PlayerRelations::ENEMIES && !isWeeklyRevisitable(objInstance))
  114. {
  115. return false;
  116. }
  117. //it may be hero visiting this obj
  118. //we don't try visiting object on which allied or owned hero stands
  119. // -> it will just trigger exchange windows and AI will be confused that obj behind doesn't get visited
  120. const CGObjectInstance * topObj = cb->getTopObj(obj->visitablePos());
  121. if(topObj->ID == Obj::HERO && cb->getPlayerRelations(ai->playerID, topObj->tempOwner) != PlayerRelations::ENEMIES)
  122. return false;
  123. else
  124. return true; //all of the following is met
  125. }