RecruitHeroBehavior.cpp 2.9 KB

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