RecruitHeroBehavior.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "RecruitHeroBehavior.h"
  12. #include "../VCAI.h"
  13. #include "../AIhelper.h"
  14. #include "../AIUtility.h"
  15. #include "../Goals/RecruitHero.h"
  16. #include "../Goals/ExecuteHeroChain.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 RecruitHeroBehavior::toString() const
  24. {
  25. return "Recruit hero";
  26. }
  27. Goals::TGoalVec RecruitHeroBehavior::getTasks()
  28. {
  29. Goals::TGoalVec tasks;
  30. if(ai->canRecruitAnyHero())
  31. {
  32. if(cb->getHeroesInfo().size() < cb->getTownsInfo().size() + 1
  33. || cb->getResourceAmount(Res::GOLD) > 10000)
  34. {
  35. tasks.push_back(Goals::sptr(Goals::RecruitHero()));
  36. }
  37. }
  38. return tasks;
  39. }
  40. std::string StartupBehavior::toString() const
  41. {
  42. return "Startup";
  43. }
  44. const AIPath getShortestPath(const CGTownInstance * town, const std::vector<AIPath> & paths)
  45. {
  46. auto shortestPath = *vstd::minElementByFun(paths, [town](const AIPath & path) -> float
  47. {
  48. if(town->garrisonHero && path.targetHero == town->garrisonHero.get())
  49. return 1;
  50. return path.movementCost();
  51. });
  52. return shortestPath;
  53. }
  54. const CGHeroInstance * getNearestHero(const CGTownInstance * town)
  55. {
  56. auto paths = ai->ah->getPathsToTile(town->visitablePos());
  57. if(paths.empty())
  58. return nullptr;
  59. auto shortestPath = getShortestPath(town, paths);
  60. if(shortestPath.nodes.size() > 1
  61. || shortestPath.targetHero->visitablePos().dist2dSQ(town->visitablePos()) > 4
  62. || town->garrisonHero && shortestPath.targetHero == town->garrisonHero.get())
  63. return nullptr;
  64. return shortestPath.targetHero;
  65. }
  66. bool needToRecruitHero(const CGTownInstance * startupTown)
  67. {
  68. if(!ai->canRecruitAnyHero(startupTown))
  69. return false;
  70. if(!startupTown->garrisonHero && !startupTown->visitingHero)
  71. return false;
  72. auto heroToCheck = startupTown->garrisonHero ? startupTown->garrisonHero.get() : startupTown->visitingHero.get();
  73. auto paths = cb->getPathsInfo(heroToCheck);
  74. for(auto obj : ai->visitableObjs)
  75. {
  76. if(obj->ID == Obj::RESOURCE && obj->subID == Res::GOLD
  77. || obj->ID == Obj::TREASURE_CHEST
  78. || obj->ID == Obj::CAMPFIRE
  79. || obj->ID == Obj::WATER_WHEEL)
  80. {
  81. auto path = paths->getPathInfo(obj->visitablePos());
  82. if(path->accessible == CGPathNode::BLOCKVIS && path->turns != 255)
  83. {
  84. return true;
  85. }
  86. }
  87. }
  88. return false;
  89. }
  90. Goals::TGoalVec StartupBehavior::getTasks()
  91. {
  92. Goals::TGoalVec tasks;
  93. auto towns = cb->getTownsInfo();
  94. if(!towns.size())
  95. return tasks;
  96. const CGTownInstance * startupTown = towns.front();
  97. bool canRecruitHero = needToRecruitHero(startupTown);
  98. if(towns.size() > 1)
  99. {
  100. startupTown = *vstd::maxElementByFun(towns, [](const CGTownInstance * town) -> float
  101. {
  102. auto closestHero = getNearestHero(town);
  103. if(!closestHero)
  104. return 0;
  105. return ai->ah->evaluateHero(closestHero);
  106. });
  107. }
  108. auto closestHero = getNearestHero(startupTown);
  109. if(closestHero)
  110. {
  111. if(!startupTown->visitingHero)
  112. {
  113. if(ai->ah->howManyReinforcementsCanGet(startupTown->getUpperArmy(), closestHero) > 200)
  114. {
  115. auto paths = ai->ah->getPathsToTile(startupTown->visitablePos());
  116. if(paths.size())
  117. {
  118. auto path = getShortestPath(startupTown, paths);
  119. tasks.push_back(Goals::sptr(Goals::ExecuteHeroChain(path, startupTown).setpriority(100)));
  120. }
  121. }
  122. }
  123. else
  124. {
  125. auto visitingHero = startupTown->visitingHero.get();
  126. auto visitingHeroScore = ai->ah->evaluateHero(visitingHero);
  127. if(startupTown->garrisonHero)
  128. {
  129. auto garrisonHero = startupTown->garrisonHero.get();
  130. auto garrisonHeroScore = ai->ah->evaluateHero(garrisonHero);
  131. if(visitingHeroScore > garrisonHeroScore
  132. || ai->ah->getHeroRole(garrisonHero) == HeroRole::SCOUT && ai->ah->getHeroRole(visitingHero) == HeroRole::MAIN)
  133. {
  134. if(canRecruitHero || ai->ah->howManyReinforcementsCanGet(visitingHero, garrisonHero) > 200)
  135. {
  136. tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, visitingHero).setpriority(100)));
  137. }
  138. }
  139. else if(ai->ah->howManyReinforcementsCanGet(garrisonHero, visitingHero) > 200)
  140. {
  141. tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, garrisonHero).setpriority(100)));
  142. }
  143. }
  144. else if(canRecruitHero)
  145. {
  146. tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, visitingHero).setpriority(100)));
  147. }
  148. }
  149. }
  150. if(tasks.empty() && canRecruitHero && !startupTown->visitingHero)
  151. {
  152. tasks.push_back(Goals::sptr(Goals::RecruitHero()));
  153. }
  154. if(tasks.empty() && towns.size())
  155. {
  156. for(const CGTownInstance * town : towns)
  157. {
  158. if(town->garrisonHero && town->garrisonHero->movement)
  159. tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(town, nullptr).setpriority(0.0001f)));
  160. }
  161. }
  162. return tasks;
  163. }