RecruitHeroBehavior.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. int currentArmyValue = 0;
  31. for(auto hero : ourHeroes)
  32. {
  33. currentArmyValue += hero.first->getArmyCost();
  34. if(hero.second != HeroRole::MAIN)
  35. continue;
  36. auto newScore = ai->heroManager->evaluateHero(hero.first.get());
  37. if(minScoreToHireMain > newScore)
  38. {
  39. // weakest main hero score
  40. minScoreToHireMain = newScore;
  41. }
  42. }
  43. // If we don't have any heros we might want to lower our expectations.
  44. if (ourHeroes.empty())
  45. minScoreToHireMain = 0;
  46. const CGHeroInstance* bestHeroToHire = nullptr;
  47. const CGTownInstance* bestTownToHireFrom = nullptr;
  48. float bestScore = 0;
  49. bool haveCapitol = false;
  50. ai->dangerHitMap->updateHitMap();
  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 hero : availableHeroes)
  71. {
  72. auto score = ai->heroManager->evaluateHero(hero);
  73. if(score > minScoreToHireMain)
  74. {
  75. score *= score / minScoreToHireMain;
  76. }
  77. score *= (hero->getArmyCost() + currentArmyValue);
  78. if (hero->type->heroClass->faction == town->getFaction())
  79. score *= 1.5;
  80. if (vstd::isAlmostZero(visitability))
  81. score *= 30 * town->getTownLevel();
  82. else
  83. score *= town->getTownLevel() / visitability;
  84. if (score > bestScore)
  85. {
  86. bestScore = score;
  87. bestHeroToHire = hero;
  88. bestTownToHireFrom = town;
  89. }
  90. }
  91. }
  92. if (town->hasCapitol())
  93. haveCapitol = true;
  94. }
  95. if (bestHeroToHire && bestTownToHireFrom)
  96. {
  97. if (ai->cb->getHeroesInfo().size() < ai->cb->getTownsInfo().size() + 1
  98. || (ai->getFreeResources()[EGameResID::GOLD] > 10000 && !ai->buildAnalyzer->isGoldPressureHigh() && haveCapitol)
  99. || (ai->getFreeResources()[EGameResID::GOLD] > 30000 && !ai->buildAnalyzer->isGoldPressureHigh()))
  100. {
  101. tasks.push_back(Goals::sptr(Goals::RecruitHero(bestTownToHireFrom, bestHeroToHire).setpriority((float)3 / (ourHeroes.size() + 1))));
  102. }
  103. }
  104. return tasks;
  105. }
  106. }