RecruitHeroBehavior.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * RecruitHeroBehavior.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 "../AIGateway.h"
  13. #include "../AIUtility.h"
  14. #include "../Goals/RecruitHero.h"
  15. #include "../Goals/ExecuteHeroChain.h"
  16. namespace NKAI
  17. {
  18. using namespace Goals;
  19. std::string RecruitHeroBehavior::toString() const
  20. {
  21. return "Recruit hero";
  22. }
  23. Goals::TGoalVec RecruitHeroBehavior::decompose(const Nullkiller * ai) const
  24. {
  25. Goals::TGoalVec tasks;
  26. auto towns = ai->cb->getTownsInfo();
  27. auto ourHeroes = ai->heroManager->getHeroRoles();
  28. auto minScoreToHireMain = std::numeric_limits<float>::max();
  29. for(auto hero : ourHeroes)
  30. {
  31. if(hero.second != HeroRole::MAIN)
  32. continue;
  33. auto newScore = ai->heroManager->evaluateHero(hero.first.get());
  34. if(minScoreToHireMain > newScore)
  35. {
  36. // weakest main hero score
  37. minScoreToHireMain = newScore;
  38. }
  39. }
  40. // If we don't have any heros we might want to lower our expectations.
  41. if (ourHeroes.empty())
  42. minScoreToHireMain = 0;
  43. for(auto town : towns)
  44. {
  45. if(ai->heroManager->canRecruitHero(town))
  46. {
  47. auto availableHeroes = ai->cb->getAvailableHeroes(town);
  48. for(auto hero : availableHeroes)
  49. {
  50. auto score = ai->heroManager->evaluateHero(hero);
  51. if(score > minScoreToHireMain || hero->getArmyCost() > GameConstants::HERO_GOLD_COST)
  52. {
  53. tasks.push_back(Goals::sptr(Goals::RecruitHero(town, hero).setpriority(200)));
  54. break;
  55. }
  56. }
  57. int treasureSourcesCount = 0;
  58. for(auto obj : ai->objectClusterizer->getNearbyObjects())
  59. {
  60. if((obj->ID == Obj::RESOURCE)
  61. || obj->ID == Obj::TREASURE_CHEST
  62. || obj->ID == Obj::CAMPFIRE
  63. || isWeeklyRevisitable(ai, obj)
  64. || obj->ID ==Obj::ARTIFACT)
  65. {
  66. auto tile = obj->visitablePos();
  67. auto closestTown = ai->dangerHitMap->getClosestTown(tile);
  68. if(town == closestTown)
  69. treasureSourcesCount++;
  70. }
  71. }
  72. if(ai->cb->getHeroesInfo().size() < ai->cb->getTownsInfo().size() + 1
  73. || (ai->getFreeResources()[EGameResID::GOLD] > 10000 && !ai->buildAnalyzer->isGoldPressureHigh()))
  74. {
  75. tasks.push_back(Goals::sptr(Goals::RecruitHero(town).setpriority(3)));
  76. }
  77. }
  78. }
  79. return tasks;
  80. }
  81. }