RecruitHeroBehavior.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. int currentArmyValue = 0;
  30. for(auto hero : ourHeroes)
  31. {
  32. currentArmyValue += hero.first->getArmyCost();
  33. if(hero.second != HeroRole::MAIN)
  34. continue;
  35. auto newScore = ai->heroManager->evaluateHero(hero.first.get());
  36. if(minScoreToHireMain > newScore)
  37. {
  38. // weakest main hero score
  39. minScoreToHireMain = newScore;
  40. }
  41. }
  42. // If we don't have any heros we might want to lower our expectations.
  43. if (ourHeroes.empty())
  44. minScoreToHireMain = 0;
  45. const CGHeroInstance* bestHeroToHire = nullptr;
  46. const CGTownInstance* bestTownToHireFrom = nullptr;
  47. float bestScore = 0;
  48. bool haveCapitol = false;
  49. ai->dangerHitMap->updateHitMap();
  50. int treasureSourcesCount = 0;
  51. for(auto town : towns)
  52. {
  53. uint8_t closestThreat = UINT8_MAX;
  54. for (auto threat : ai->dangerHitMap->getTownThreats(town))
  55. {
  56. closestThreat = std::min(closestThreat, threat.turn);
  57. }
  58. //Don't hire a hero where there already is one present
  59. if (town->visitingHero && town->garrisonHero)
  60. continue;
  61. float visitability = 0;
  62. for (auto checkHero : ourHeroes)
  63. {
  64. if (ai->dangerHitMap->getClosestTown(checkHero.first.get()->visitablePos()) == town)
  65. visitability++;
  66. }
  67. if(ai->heroManager->canRecruitHero(town))
  68. {
  69. auto availableHeroes = ai->cb->getAvailableHeroes(town);
  70. for (auto obj : ai->objectClusterizer->getNearbyObjects())
  71. {
  72. if ((obj->ID == Obj::RESOURCE)
  73. || obj->ID == Obj::TREASURE_CHEST
  74. || obj->ID == Obj::CAMPFIRE
  75. || isWeeklyRevisitable(ai, obj)
  76. || obj->ID == Obj::ARTIFACT)
  77. {
  78. auto tile = obj->visitablePos();
  79. auto closestTown = ai->dangerHitMap->getClosestTown(tile);
  80. if (town == closestTown)
  81. treasureSourcesCount++;
  82. }
  83. }
  84. for(auto hero : availableHeroes)
  85. {
  86. auto score = ai->heroManager->evaluateHero(hero);
  87. if(score > minScoreToHireMain)
  88. {
  89. score *= score / minScoreToHireMain;
  90. }
  91. score *= (hero->getArmyCost() + currentArmyValue);
  92. if (hero->getFactionID() == town->getFactionID())
  93. score *= 1.5;
  94. if (vstd::isAlmostZero(visitability))
  95. score *= 30 * town->getTownLevel();
  96. else
  97. score *= town->getTownLevel() / visitability;
  98. if (score > bestScore)
  99. {
  100. bestScore = score;
  101. bestHeroToHire = hero;
  102. bestTownToHireFrom = town;
  103. }
  104. }
  105. }
  106. if (town->hasCapitol())
  107. haveCapitol = true;
  108. }
  109. if (bestHeroToHire && bestTownToHireFrom)
  110. {
  111. if (ai->cb->getHeroesInfo().size() == 0
  112. || treasureSourcesCount > ai->cb->getHeroesInfo().size() * 5
  113. || (ai->getFreeResources()[EGameResID::GOLD] > 10000 && !ai->buildAnalyzer->isGoldPressureHigh() && haveCapitol)
  114. || (ai->getFreeResources()[EGameResID::GOLD] > 30000 && !ai->buildAnalyzer->isGoldPressureHigh()))
  115. {
  116. tasks.push_back(Goals::sptr(Goals::RecruitHero(bestTownToHireFrom, bestHeroToHire).setpriority((float)3 / (ourHeroes.size() + 1))));
  117. }
  118. }
  119. return tasks;
  120. }
  121. }