CaptureObjectsBehavior.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "CaptureObjectsBehavior.h"
  14. #include "../AIUtility.h"
  15. #include "lib/mapping/CMap.h" //for victory conditions
  16. #include "lib/CPathfinder.h"
  17. extern boost::thread_specific_ptr<CCallback> cb;
  18. extern boost::thread_specific_ptr<VCAI> ai;
  19. extern FuzzyHelper * fh;
  20. using namespace Goals;
  21. std::string CaptureObjectsBehavior::toString() const {
  22. return "Capture objects";
  23. }
  24. Goals::TGoalVec CaptureObjectsBehavior::getTasks() {
  25. Goals::TGoalVec tasks;
  26. auto captureObjects = [&](std::vector<const CGObjectInstance*> objs) -> void {
  27. if (objs.empty()) {
  28. return;
  29. }
  30. for (auto objToVisit : objs) {
  31. if(!shouldVisitObject(objToVisit))
  32. continue;
  33. const int3 pos = objToVisit->visitablePos();
  34. Goals::TGoalVec waysToVisitObj = ai->ah->howToVisitObj(objToVisit, false);
  35. vstd::erase_if(waysToVisitObj, [objToVisit](Goals::TSubgoal goal) -> bool
  36. {
  37. return !goal->hero.validAndSet()
  38. || !shouldVisit(goal->hero, objToVisit);
  39. });
  40. if(waysToVisitObj.empty())
  41. continue;
  42. Goals::TSubgoal closestWay = *vstd::minElementByFun(waysToVisitObj, [](Goals::TSubgoal goal) -> float {
  43. return goal->evaluationContext.movementCost;
  44. });
  45. for(Goals::TSubgoal way : waysToVisitObj)
  46. {
  47. if(!way->hero->movement)
  48. continue;
  49. way->evaluationContext.closestWayRatio
  50. = way->evaluationContext.movementCost / closestWay->evaluationContext.movementCost;
  51. logAi->trace("Behavior %s found %s(%s), danger %d", toString(), way->name(), way->tile.toString(), way->evaluationContext.danger);
  52. tasks.push_back(way);
  53. }
  54. }
  55. };
  56. if (specificObjects) {
  57. captureObjects(objectsToCapture);
  58. }
  59. else {
  60. captureObjects(std::vector<const CGObjectInstance*>(ai->visitableObjs.begin(), ai->visitableObjs.end()));
  61. }
  62. return tasks;
  63. }
  64. bool CaptureObjectsBehavior::shouldVisitObject(ObjectIdRef obj) const
  65. {
  66. const CGObjectInstance* objInstance = obj;
  67. if (!objInstance || objectTypes.size() && !vstd::contains(objectTypes, objInstance->ID.num)) {
  68. return false;
  69. }
  70. if (!objInstance || objectSubTypes.size() && !vstd::contains(objectSubTypes, objInstance->subID)) {
  71. return false;
  72. }
  73. const int3 pos = objInstance->visitablePos();
  74. if (vstd::contains(ai->alreadyVisited, objInstance)) {
  75. return false;
  76. }
  77. auto playerRelations = cb->getPlayerRelations(ai->playerID, objInstance->tempOwner);
  78. if (playerRelations != PlayerRelations::ENEMIES && !isWeeklyRevisitable(objInstance)) {
  79. return false;
  80. }
  81. //it may be hero visiting this obj
  82. //we don't try visiting object on which allied or owned hero stands
  83. // -> it will just trigger exchange windows and AI will be confused that obj behind doesn't get visited
  84. const CGObjectInstance * topObj = cb->getTopObj(obj->visitablePos());
  85. if (topObj->ID == Obj::HERO && cb->getPlayerRelations(ai->playerID, topObj->tempOwner) != PlayerRelations::ENEMIES)
  86. return false;
  87. else
  88. return true; //all of the following is met
  89. }