CaptureObjectsBehavior.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. return "Capture objects";
  24. }
  25. Goals::TGoalVec CaptureObjectsBehavior::getTasks() {
  26. Goals::TGoalVec tasks;
  27. auto captureObjects = [&](std::vector<const CGObjectInstance*> objs) -> void {
  28. if (objs.empty()) {
  29. return;
  30. }
  31. for (auto objToVisit : objs) {
  32. if(!shouldVisitObject(objToVisit))
  33. continue;
  34. const int3 pos = objToVisit->visitablePos();
  35. auto paths = ai->ah->getPathsToTile(pos);
  36. std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
  37. std::shared_ptr<ExecuteHeroChain> closestWay;
  38. for(auto & path : paths)
  39. {
  40. #ifdef VCMI_TRACE_PATHFINDER
  41. std::stringstream str;
  42. str << "Path found ";
  43. for(auto node : path.nodes)
  44. str << node.targetHero->name << "->" << node.coord.toString() << "; ";
  45. logAi->trace(str.str());
  46. #endif
  47. if(!shouldVisit(path.targetHero, objToVisit))
  48. continue;
  49. auto hero = path.targetHero;
  50. auto danger = path.getTotalDanger(hero);
  51. if(isSafeToVisit(hero, path.heroArmy, danger))
  52. {
  53. auto newWay = std::make_shared<ExecuteHeroChain>(path, objToVisit);
  54. waysToVisitObj.push_back(newWay);
  55. if(!closestWay || closestWay->evaluationContext.movementCost > newWay->evaluationContext.movementCost)
  56. closestWay = newWay;
  57. }
  58. }
  59. if(waysToVisitObj.empty())
  60. continue;
  61. for(auto way : waysToVisitObj)
  62. {
  63. way->evaluationContext.closestWayRatio
  64. = way->evaluationContext.movementCost / closestWay->evaluationContext.movementCost;
  65. logAi->trace("Behavior %s found %s(%s), danger %d", toString(), way->name(), way->tile.toString(), way->evaluationContext.danger);
  66. tasks.push_back(sptr(*way));
  67. }
  68. }
  69. };
  70. if (specificObjects) {
  71. captureObjects(objectsToCapture);
  72. }
  73. else {
  74. captureObjects(std::vector<const CGObjectInstance*>(ai->visitableObjs.begin(), ai->visitableObjs.end()));
  75. }
  76. return tasks;
  77. }
  78. bool CaptureObjectsBehavior::shouldVisitObject(ObjectIdRef obj) const
  79. {
  80. const CGObjectInstance* objInstance = obj;
  81. if (!objInstance || objectTypes.size() && !vstd::contains(objectTypes, objInstance->ID.num)) {
  82. return false;
  83. }
  84. if (!objInstance || objectSubTypes.size() && !vstd::contains(objectSubTypes, objInstance->subID)) {
  85. return false;
  86. }
  87. const int3 pos = objInstance->visitablePos();
  88. if (vstd::contains(ai->alreadyVisited, objInstance)) {
  89. return false;
  90. }
  91. auto playerRelations = cb->getPlayerRelations(ai->playerID, objInstance->tempOwner);
  92. if (playerRelations != PlayerRelations::ENEMIES && !isWeeklyRevisitable(objInstance)) {
  93. return false;
  94. }
  95. //it may be hero visiting this obj
  96. //we don't try visiting object on which allied or owned hero stands
  97. // -> it will just trigger exchange windows and AI will be confused that obj behind doesn't get visited
  98. const CGObjectInstance * topObj = cb->getTopObj(obj->visitablePos());
  99. if (topObj->ID == Obj::HERO && cb->getPlayerRelations(ai->playerID, topObj->tempOwner) != PlayerRelations::ENEMIES)
  100. return false;
  101. else
  102. return true; //all of the following is met
  103. }