RecruitHeroBehavior.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include "../lib/CHeroHandler.h"
  17. namespace NKAI
  18. {
  19. using namespace Goals;
  20. std::string RecruitHeroBehavior::toString() const
  21. {
  22. return "Recruit hero";
  23. }
  24. Goals::TGoalVec RecruitHeroBehavior::decompose(const Nullkiller * ai) const
  25. {
  26. Goals::TGoalVec tasks;
  27. auto towns = ai->cb->getTownsInfo();
  28. auto ourHeroes = ai->heroManager->getHeroRoles();
  29. auto minScoreToHireMain = std::numeric_limits<float>::max();
  30. for(auto hero : ourHeroes)
  31. {
  32. if(hero.second != HeroRole::MAIN)
  33. continue;
  34. auto newScore = ai->heroManager->evaluateHero(hero.first.get());
  35. if(minScoreToHireMain > newScore)
  36. {
  37. // weakest main hero score
  38. minScoreToHireMain = newScore;
  39. }
  40. }
  41. // If we don't have any heros we might want to lower our expectations.
  42. if (ourHeroes.empty())
  43. minScoreToHireMain = 0;
  44. const CGHeroInstance* bestHeroToHire = nullptr;
  45. const CGTownInstance* bestTownToHireFrom = nullptr;
  46. float bestScore = 0;
  47. bool haveCapitol = false;
  48. ai->dangerHitMap->updateHitMap();
  49. for(auto town : towns)
  50. {
  51. uint8_t closestThreat = UINT8_MAX;
  52. for (auto threat : ai->dangerHitMap->getTownThreats(town))
  53. {
  54. closestThreat = std::min(closestThreat, threat.turn);
  55. }
  56. //Don' hire a hero in a threatened town as one would have to stay outside
  57. if (closestThreat <= 1 && (town->visitingHero || town->garrisonHero))
  58. continue;
  59. if(ai->heroManager->canRecruitHero(town))
  60. {
  61. auto availableHeroes = ai->cb->getAvailableHeroes(town);
  62. for(auto hero : availableHeroes)
  63. {
  64. auto score = ai->heroManager->evaluateHero(hero);
  65. if(score > minScoreToHireMain)
  66. {
  67. score *= score / minScoreToHireMain;
  68. }
  69. score *= hero->getArmyCost();
  70. if (hero->type->heroClass->faction == town->getFaction())
  71. score *= 1.5;
  72. score *= town->getTownLevel();
  73. if (score > bestScore)
  74. {
  75. bestScore = score;
  76. bestHeroToHire = hero;
  77. bestTownToHireFrom = town;
  78. }
  79. }
  80. }
  81. if (town->hasCapitol())
  82. haveCapitol = true;
  83. }
  84. if (bestHeroToHire && bestTownToHireFrom)
  85. {
  86. if (ai->cb->getHeroesInfo().size() < ai->cb->getTownsInfo().size() + 1
  87. || (ai->getFreeResources()[EGameResID::GOLD] > 10000 && !ai->buildAnalyzer->isGoldPressureHigh() && haveCapitol))
  88. {
  89. tasks.push_back(Goals::sptr(Goals::RecruitHero(bestTownToHireFrom, bestHeroToHire).setpriority((float)3 / ourHeroes.size())));
  90. }
  91. }
  92. return tasks;
  93. }
  94. }