RecruitHeroBehavior.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. for(auto town : towns)
  51. {
  52. uint8_t closestThreat = UINT8_MAX;
  53. for (auto threat : ai->dangerHitMap->getTownThreats(town))
  54. {
  55. closestThreat = std::min(closestThreat, threat.turn);
  56. }
  57. //Don't hire a hero where there already is one present
  58. if (town->visitingHero && town->garrisonHero)
  59. continue;
  60. float visitability = 0;
  61. for (auto checkHero : ourHeroes)
  62. {
  63. if (ai->dangerHitMap->getClosestTown(checkHero.first.get()->visitablePos()) == town)
  64. visitability++;
  65. }
  66. if(ai->heroManager->canRecruitHero(town))
  67. {
  68. auto availableHeroes = ai->cb->getAvailableHeroes(town);
  69. for(auto hero : availableHeroes)
  70. {
  71. auto score = ai->heroManager->evaluateHero(hero);
  72. if(score > minScoreToHireMain)
  73. {
  74. score *= score / minScoreToHireMain;
  75. }
  76. score *= (hero->getArmyCost() + currentArmyValue);
  77. if (hero->getFactionID() == town->getFactionID())
  78. score *= 1.5;
  79. if (vstd::isAlmostZero(visitability))
  80. score *= 30 * town->getTownLevel();
  81. else
  82. score *= town->getTownLevel() / visitability;
  83. if (score > bestScore)
  84. {
  85. bestScore = score;
  86. bestHeroToHire = hero;
  87. bestTownToHireFrom = town;
  88. }
  89. }
  90. }
  91. if (town->hasCapitol())
  92. haveCapitol = true;
  93. }
  94. if (bestHeroToHire && bestTownToHireFrom)
  95. {
  96. if (ai->cb->getHeroesInfo().size() < ai->cb->getTownsInfo().size() + 1
  97. || (ai->getFreeResources()[EGameResID::GOLD] > 10000 && !ai->buildAnalyzer->isGoldPressureHigh() && haveCapitol)
  98. || (ai->getFreeResources()[EGameResID::GOLD] > 30000 && !ai->buildAnalyzer->isGoldPressureHigh()))
  99. {
  100. tasks.push_back(Goals::sptr(Goals::RecruitHero(bestTownToHireFrom, bestHeroToHire).setpriority((float)3 / (ourHeroes.size() + 1))));
  101. }
  102. }
  103. return tasks;
  104. }
  105. }