CaptureObjectsBehavior.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. || goal->evaluationContext.danger * 1.5 > goal->hero->getTotalStrength();
  40. });
  41. if(waysToVisitObj.empty())
  42. continue;
  43. Goals::TSubgoal closestWay = *vstd::minElementByFun(waysToVisitObj, [](Goals::TSubgoal goal) -> float {
  44. return goal->evaluationContext.movementCost;
  45. });
  46. for(Goals::TSubgoal way : waysToVisitObj)
  47. {
  48. if(!way->hero->movement)
  49. continue;
  50. way->evaluationContext.closestWayRatio
  51. = way->evaluationContext.movementCost / closestWay->evaluationContext.movementCost;
  52. logAi->trace("Behavior %s found %s(%s), danger %d", toString(), way->name(), way->tile.toString(), way->evaluationContext.danger);
  53. tasks.push_back(way);
  54. }
  55. }
  56. };
  57. if (specificObjects) {
  58. captureObjects(objectsToCapture);
  59. }
  60. else {
  61. captureObjects(std::vector<const CGObjectInstance*>(ai->visitableObjs.begin(), ai->visitableObjs.end()));
  62. }
  63. return tasks;
  64. }
  65. bool CaptureObjectsBehavior::shouldVisitObject(ObjectIdRef obj) const
  66. {
  67. const CGObjectInstance* objInstance = obj;
  68. if (!objInstance || objectTypes.size() && !vstd::contains(objectTypes, objInstance->ID.num)) {
  69. return false;
  70. }
  71. if (!objInstance || objectSubTypes.size() && !vstd::contains(objectSubTypes, objInstance->subID)) {
  72. return false;
  73. }
  74. const int3 pos = objInstance->visitablePos();
  75. if (vstd::contains(ai->alreadyVisited, objInstance)) {
  76. return false;
  77. }
  78. auto playerRelations = cb->getPlayerRelations(ai->playerID, objInstance->tempOwner);
  79. if (playerRelations != PlayerRelations::ENEMIES && !isWeeklyRevisitable(objInstance)) {
  80. return false;
  81. }
  82. //it may be hero visiting this obj
  83. //we don't try visiting object on which allied or owned hero stands
  84. // -> it will just trigger exchange windows and AI will be confused that obj behind doesn't get visited
  85. const CGObjectInstance * topObj = cb->getTopObj(obj->visitablePos());
  86. if (topObj->ID == Obj::HERO && cb->getPlayerRelations(ai->playerID, topObj->tempOwner) != PlayerRelations::ENEMIES)
  87. return false;
  88. else
  89. return true; //all of the following is met
  90. }