RecruitHeroBehavior.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. int bestClosestThreat = UINT8_MAX;
  52. for(auto town : towns)
  53. {
  54. uint8_t closestThreat = UINT8_MAX;
  55. for (auto threat : ai->dangerHitMap->getTownThreats(town))
  56. {
  57. closestThreat = std::min(closestThreat, threat.turn);
  58. }
  59. //Don't hire a hero where there already is one present
  60. if (town->visitingHero && town->garrisonHero)
  61. continue;
  62. float visitability = 0;
  63. for (auto checkHero : ourHeroes)
  64. {
  65. if (ai->dangerHitMap->getClosestTown(checkHero.first.get()->visitablePos()) == town)
  66. visitability++;
  67. }
  68. if(ai->heroManager->canRecruitHero(town))
  69. {
  70. auto availableHeroes = ai->cb->getAvailableHeroes(town);
  71. for (auto obj : ai->objectClusterizer->getNearbyObjects())
  72. {
  73. if ((obj->ID == Obj::RESOURCE)
  74. || obj->ID == Obj::TREASURE_CHEST
  75. || obj->ID == Obj::CAMPFIRE
  76. || isWeeklyRevisitable(ai, obj)
  77. || obj->ID == Obj::ARTIFACT)
  78. {
  79. auto tile = obj->visitablePos();
  80. auto closestTown = ai->dangerHitMap->getClosestTown(tile);
  81. if (town == closestTown)
  82. treasureSourcesCount++;
  83. }
  84. }
  85. for(auto hero : availableHeroes)
  86. {
  87. if ((town->visitingHero || town->garrisonHero)
  88. && closestThreat < 1
  89. && hero->getArmyCost() < GameConstants::HERO_GOLD_COST / 3.0)
  90. continue;
  91. auto score = ai->heroManager->evaluateHero(hero);
  92. if(score > minScoreToHireMain)
  93. {
  94. score *= score / minScoreToHireMain;
  95. }
  96. score *= (hero->getArmyCost() + currentArmyValue);
  97. if (hero->getFactionID() == town->getFactionID())
  98. score *= 1.5;
  99. if (vstd::isAlmostZero(visitability))
  100. score *= 30 * town->getTownLevel();
  101. else
  102. score *= town->getTownLevel() / visitability;
  103. if (score > bestScore)
  104. {
  105. bestScore = score;
  106. bestHeroToHire = hero;
  107. bestTownToHireFrom = town;
  108. bestClosestThreat = closestThreat;
  109. }
  110. }
  111. }
  112. if (town->hasCapitol())
  113. haveCapitol = true;
  114. }
  115. if (bestHeroToHire && bestTownToHireFrom)
  116. {
  117. if (ai->cb->getHeroesInfo().size() == 0
  118. || treasureSourcesCount > ai->cb->getHeroesInfo().size() * 5
  119. || (bestHeroToHire->getArmyCost() > GameConstants::HERO_GOLD_COST / 2.0 && (bestClosestThreat < 1 || !ai->buildAnalyzer->isGoldPressureHigh()))
  120. || (ai->getFreeResources()[EGameResID::GOLD] > 10000 && !ai->buildAnalyzer->isGoldPressureHigh() && haveCapitol)
  121. || (ai->getFreeResources()[EGameResID::GOLD] > 30000 && !ai->buildAnalyzer->isGoldPressureHigh()))
  122. {
  123. tasks.push_back(Goals::sptr(Goals::RecruitHero(bestTownToHireFrom, bestHeroToHire).setpriority((float)3 / (ourHeroes.size() + 1))));
  124. }
  125. }
  126. return tasks;
  127. }
  128. }